Merge tag 'for-linus-3.12-merge' of git://git.kernel.org/pub/scm/linux/kernel/git...
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / video / efifb.c
1 /*
2  * Framebuffer driver for EFI/UEFI based system
3  *
4  * (c) 2006 Edgar Hucek <gimli@dark-green.com>
5  * Original efi driver written by Gerd Knorr <kraxel@goldbach.in-berlin.de>
6  *
7  */
8
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/errno.h>
12 #include <linux/fb.h>
13 #include <linux/platform_device.h>
14 #include <linux/screen_info.h>
15 #include <linux/dmi.h>
16 #include <linux/pci.h>
17 #include <video/vga.h>
18 #include <asm/sysfb.h>
19
20 static bool request_mem_succeeded = false;
21
22 static struct pci_dev *default_vga;
23
24 static struct fb_var_screeninfo efifb_defined = {
25         .activate               = FB_ACTIVATE_NOW,
26         .height                 = -1,
27         .width                  = -1,
28         .right_margin           = 32,
29         .upper_margin           = 16,
30         .lower_margin           = 4,
31         .vsync_len              = 4,
32         .vmode                  = FB_VMODE_NONINTERLACED,
33 };
34
35 static struct fb_fix_screeninfo efifb_fix = {
36         .id                     = "EFI VGA",
37         .type                   = FB_TYPE_PACKED_PIXELS,
38         .accel                  = FB_ACCEL_NONE,
39         .visual                 = FB_VISUAL_TRUECOLOR,
40 };
41
42 static int efifb_setcolreg(unsigned regno, unsigned red, unsigned green,
43                            unsigned blue, unsigned transp,
44                            struct fb_info *info)
45 {
46         /*
47          *  Set a single color register. The values supplied are
48          *  already rounded down to the hardware's capabilities
49          *  (according to the entries in the `var' structure). Return
50          *  != 0 for invalid regno.
51          */
52
53         if (regno >= info->cmap.len)
54                 return 1;
55
56         if (regno < 16) {
57                 red   >>= 8;
58                 green >>= 8;
59                 blue  >>= 8;
60                 ((u32 *)(info->pseudo_palette))[regno] =
61                         (red   << info->var.red.offset)   |
62                         (green << info->var.green.offset) |
63                         (blue  << info->var.blue.offset);
64         }
65         return 0;
66 }
67
68 static void efifb_destroy(struct fb_info *info)
69 {
70         if (info->screen_base)
71                 iounmap(info->screen_base);
72         if (request_mem_succeeded)
73                 release_mem_region(info->apertures->ranges[0].base,
74                                    info->apertures->ranges[0].size);
75         fb_dealloc_cmap(&info->cmap);
76         framebuffer_release(info);
77 }
78
79 static struct fb_ops efifb_ops = {
80         .owner          = THIS_MODULE,
81         .fb_destroy     = efifb_destroy,
82         .fb_setcolreg   = efifb_setcolreg,
83         .fb_fillrect    = cfb_fillrect,
84         .fb_copyarea    = cfb_copyarea,
85         .fb_imageblit   = cfb_imageblit,
86 };
87
88 struct pci_dev *vga_default_device(void)
89 {
90         return default_vga;
91 }
92
93 EXPORT_SYMBOL_GPL(vga_default_device);
94
95 void vga_set_default_device(struct pci_dev *pdev)
96 {
97         default_vga = pdev;
98 }
99
100 static int efifb_setup(char *options)
101 {
102         char *this_opt;
103         int i;
104         struct pci_dev *dev = NULL;
105
106         if (options && *options) {
107                 while ((this_opt = strsep(&options, ",")) != NULL) {
108                         if (!*this_opt) continue;
109
110                         for (i = 0; i < M_UNKNOWN; i++) {
111                                 if (!strcmp(this_opt, efifb_dmi_list[i].optname) &&
112                                     efifb_dmi_list[i].base != 0) {
113                                         screen_info.lfb_base = efifb_dmi_list[i].base;
114                                         screen_info.lfb_linelength = efifb_dmi_list[i].stride;
115                                         screen_info.lfb_width = efifb_dmi_list[i].width;
116                                         screen_info.lfb_height = efifb_dmi_list[i].height;
117                                 }
118                         }
119                         if (!strncmp(this_opt, "base:", 5))
120                                 screen_info.lfb_base = simple_strtoul(this_opt+5, NULL, 0);
121                         else if (!strncmp(this_opt, "stride:", 7))
122                                 screen_info.lfb_linelength = simple_strtoul(this_opt+7, NULL, 0) * 4;
123                         else if (!strncmp(this_opt, "height:", 7))
124                                 screen_info.lfb_height = simple_strtoul(this_opt+7, NULL, 0);
125                         else if (!strncmp(this_opt, "width:", 6))
126                                 screen_info.lfb_width = simple_strtoul(this_opt+6, NULL, 0);
127                 }
128         }
129
130         for_each_pci_dev(dev) {
131                 int i;
132
133                 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
134                         continue;
135
136                 for (i=0; i < DEVICE_COUNT_RESOURCE; i++) {
137                         resource_size_t start, end;
138
139                         if (!(pci_resource_flags(dev, i) & IORESOURCE_MEM))
140                                 continue;
141
142                         start = pci_resource_start(dev, i);
143                         end  = pci_resource_end(dev, i);
144
145                         if (!start || !end)
146                                 continue;
147
148                         if (screen_info.lfb_base >= start &&
149                             (screen_info.lfb_base + screen_info.lfb_size) < end)
150                                 default_vga = dev;
151                 }
152         }
153
154         return 0;
155 }
156
157 static int efifb_probe(struct platform_device *dev)
158 {
159         struct fb_info *info;
160         int err;
161         unsigned int size_vmode;
162         unsigned int size_remap;
163         unsigned int size_total;
164         char *option = NULL;
165
166         if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI)
167                 return -ENODEV;
168
169         if (fb_get_options("efifb", &option))
170                 return -ENODEV;
171         efifb_setup(option);
172
173         /* We don't get linelength from UGA Draw Protocol, only from
174          * EFI Graphics Protocol.  So if it's not in DMI, and it's not
175          * passed in from the user, we really can't use the framebuffer.
176          */
177         if (!screen_info.lfb_linelength)
178                 return -ENODEV;
179
180         if (!screen_info.lfb_depth)
181                 screen_info.lfb_depth = 32;
182         if (!screen_info.pages)
183                 screen_info.pages = 1;
184         if (!screen_info.lfb_base) {
185                 printk(KERN_DEBUG "efifb: invalid framebuffer address\n");
186                 return -ENODEV;
187         }
188         printk(KERN_INFO "efifb: probing for efifb\n");
189
190         /* just assume they're all unset if any are */
191         if (!screen_info.blue_size) {
192                 screen_info.blue_size = 8;
193                 screen_info.blue_pos = 0;
194                 screen_info.green_size = 8;
195                 screen_info.green_pos = 8;
196                 screen_info.red_size = 8;
197                 screen_info.red_pos = 16;
198                 screen_info.rsvd_size = 8;
199                 screen_info.rsvd_pos = 24;
200         }
201
202         efifb_fix.smem_start = screen_info.lfb_base;
203         efifb_defined.bits_per_pixel = screen_info.lfb_depth;
204         efifb_defined.xres = screen_info.lfb_width;
205         efifb_defined.yres = screen_info.lfb_height;
206         efifb_fix.line_length = screen_info.lfb_linelength;
207
208         /*   size_vmode -- that is the amount of memory needed for the
209          *                 used video mode, i.e. the minimum amount of
210          *                 memory we need. */
211         size_vmode = efifb_defined.yres * efifb_fix.line_length;
212
213         /*   size_total -- all video memory we have. Used for
214          *                 entries, ressource allocation and bounds
215          *                 checking. */
216         size_total = screen_info.lfb_size;
217         if (size_total < size_vmode)
218                 size_total = size_vmode;
219
220         /*   size_remap -- the amount of video memory we are going to
221          *                 use for efifb.  With modern cards it is no
222          *                 option to simply use size_total as that
223          *                 wastes plenty of kernel address space. */
224         size_remap  = size_vmode * 2;
225         if (size_remap > size_total)
226                 size_remap = size_total;
227         if (size_remap % PAGE_SIZE)
228                 size_remap += PAGE_SIZE - (size_remap % PAGE_SIZE);
229         efifb_fix.smem_len = size_remap;
230
231         if (request_mem_region(efifb_fix.smem_start, size_remap, "efifb")) {
232                 request_mem_succeeded = true;
233         } else {
234                 /* We cannot make this fatal. Sometimes this comes from magic
235                    spaces our resource handlers simply don't know about */
236                 printk(KERN_WARNING
237                        "efifb: cannot reserve video memory at 0x%lx\n",
238                         efifb_fix.smem_start);
239         }
240
241         info = framebuffer_alloc(sizeof(u32) * 16, &dev->dev);
242         if (!info) {
243                 printk(KERN_ERR "efifb: cannot allocate framebuffer\n");
244                 err = -ENOMEM;
245                 goto err_release_mem;
246         }
247         info->pseudo_palette = info->par;
248         info->par = NULL;
249
250         info->apertures = alloc_apertures(1);
251         if (!info->apertures) {
252                 err = -ENOMEM;
253                 goto err_release_fb;
254         }
255         info->apertures->ranges[0].base = efifb_fix.smem_start;
256         info->apertures->ranges[0].size = size_remap;
257
258         info->screen_base = ioremap_wc(efifb_fix.smem_start, efifb_fix.smem_len);
259         if (!info->screen_base) {
260                 printk(KERN_ERR "efifb: abort, cannot ioremap video memory "
261                                 "0x%x @ 0x%lx\n",
262                         efifb_fix.smem_len, efifb_fix.smem_start);
263                 err = -EIO;
264                 goto err_release_fb;
265         }
266
267         printk(KERN_INFO "efifb: framebuffer at 0x%lx, mapped to 0x%p, "
268                "using %dk, total %dk\n",
269                efifb_fix.smem_start, info->screen_base,
270                size_remap/1024, size_total/1024);
271         printk(KERN_INFO "efifb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
272                efifb_defined.xres, efifb_defined.yres,
273                efifb_defined.bits_per_pixel, efifb_fix.line_length,
274                screen_info.pages);
275
276         efifb_defined.xres_virtual = efifb_defined.xres;
277         efifb_defined.yres_virtual = efifb_fix.smem_len /
278                                         efifb_fix.line_length;
279         printk(KERN_INFO "efifb: scrolling: redraw\n");
280         efifb_defined.yres_virtual = efifb_defined.yres;
281
282         /* some dummy values for timing to make fbset happy */
283         efifb_defined.pixclock     = 10000000 / efifb_defined.xres *
284                                         1000 / efifb_defined.yres;
285         efifb_defined.left_margin  = (efifb_defined.xres / 8) & 0xf8;
286         efifb_defined.hsync_len    = (efifb_defined.xres / 8) & 0xf8;
287
288         efifb_defined.red.offset    = screen_info.red_pos;
289         efifb_defined.red.length    = screen_info.red_size;
290         efifb_defined.green.offset  = screen_info.green_pos;
291         efifb_defined.green.length  = screen_info.green_size;
292         efifb_defined.blue.offset   = screen_info.blue_pos;
293         efifb_defined.blue.length   = screen_info.blue_size;
294         efifb_defined.transp.offset = screen_info.rsvd_pos;
295         efifb_defined.transp.length = screen_info.rsvd_size;
296
297         printk(KERN_INFO "efifb: %s: "
298                "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
299                "Truecolor",
300                screen_info.rsvd_size,
301                screen_info.red_size,
302                screen_info.green_size,
303                screen_info.blue_size,
304                screen_info.rsvd_pos,
305                screen_info.red_pos,
306                screen_info.green_pos,
307                screen_info.blue_pos);
308
309         efifb_fix.ypanstep  = 0;
310         efifb_fix.ywrapstep = 0;
311
312         info->fbops = &efifb_ops;
313         info->var = efifb_defined;
314         info->fix = efifb_fix;
315         info->flags = FBINFO_FLAG_DEFAULT | FBINFO_MISC_FIRMWARE;
316
317         if ((err = fb_alloc_cmap(&info->cmap, 256, 0)) < 0) {
318                 printk(KERN_ERR "efifb: cannot allocate colormap\n");
319                 goto err_unmap;
320         }
321         if ((err = register_framebuffer(info)) < 0) {
322                 printk(KERN_ERR "efifb: cannot register framebuffer\n");
323                 goto err_fb_dealoc;
324         }
325         printk(KERN_INFO "fb%d: %s frame buffer device\n",
326                 info->node, info->fix.id);
327         return 0;
328
329 err_fb_dealoc:
330         fb_dealloc_cmap(&info->cmap);
331 err_unmap:
332         iounmap(info->screen_base);
333 err_release_fb:
334         framebuffer_release(info);
335 err_release_mem:
336         if (request_mem_succeeded)
337                 release_mem_region(efifb_fix.smem_start, size_total);
338         return err;
339 }
340
341 static struct platform_driver efifb_driver = {
342         .driver = {
343                 .name = "efi-framebuffer",
344                 .owner = THIS_MODULE,
345         },
346         .probe = efifb_probe,
347 };
348
349 module_platform_driver(efifb_driver);
350 MODULE_LICENSE("GPL");