blob: 4a56f46af40a35a0eeb76a307a4b0420c36699d2 [file] [log] [blame]
Huang, Ying7c831722007-11-28 16:21:55 -08001/*
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>
Peter Jones7c08c9a2008-10-15 22:03:43 -070015#include <linux/dmi.h>
Huang, Ying7c831722007-11-28 16:21:55 -080016
17#include <video/vga.h>
18
19static struct fb_var_screeninfo efifb_defined __initdata = {
20 .activate = FB_ACTIVATE_NOW,
21 .height = -1,
22 .width = -1,
23 .right_margin = 32,
24 .upper_margin = 16,
25 .lower_margin = 4,
26 .vsync_len = 4,
27 .vmode = FB_VMODE_NONINTERLACED,
28};
29
30static struct fb_fix_screeninfo efifb_fix __initdata = {
31 .id = "EFI VGA",
32 .type = FB_TYPE_PACKED_PIXELS,
33 .accel = FB_ACCEL_NONE,
34 .visual = FB_VISUAL_TRUECOLOR,
35};
36
Peter Jones7c08c9a2008-10-15 22:03:43 -070037enum {
38 M_I17, /* 17-Inch iMac */
39 M_I20, /* 20-Inch iMac */
40 M_I20_SR, /* 20-Inch iMac (Santa Rosa) */
41 M_I24, /* 24-Inch iMac */
42 M_MINI, /* Mac Mini */
43 M_MB, /* MacBook */
44 M_MB_2, /* MacBook, 2nd rev. */
45 M_MB_3, /* MacBook, 3rd rev. */
46 M_MB_SR, /* MacBook, 2nd gen, (Santa Rosa) */
47 M_MBA, /* MacBook Air */
48 M_MBP, /* MacBook Pro */
49 M_MBP_2, /* MacBook Pro 2nd gen */
50 M_MBP_SR, /* MacBook Pro (Santa Rosa) */
51 M_MBP_4, /* MacBook Pro, 4th gen */
Thomas Gerlach8a3bdfe2010-04-23 13:17:50 -040052 M_MBP_5_1, /* MacBook Pro, 5,1th gen */
Peter Jones7c08c9a2008-10-15 22:03:43 -070053 M_UNKNOWN /* placeholder */
54};
55
56static struct efifb_dmi_info {
57 char *optname;
58 unsigned long base;
59 int stride;
60 int width;
61 int height;
62} dmi_list[] = {
63 [M_I17] = { "i17", 0x80010000, 1472 * 4, 1440, 900 },
64 [M_I20] = { "i20", 0x80010000, 1728 * 4, 1680, 1050 }, /* guess */
65 [M_I20_SR] = { "imac7", 0x40010000, 1728 * 4, 1680, 1050 },
66 [M_I24] = { "i24", 0x80010000, 2048 * 4, 1920, 1200 }, /* guess */
67 [M_MINI]= { "mini", 0x80000000, 2048 * 4, 1024, 768 },
68 [M_MB] = { "macbook", 0x80000000, 2048 * 4, 1280, 800 },
69 [M_MBA] = { "mba", 0x80000000, 2048 * 4, 1280, 800 },
70 [M_MBP] = { "mbp", 0x80010000, 1472 * 4, 1440, 900 },
71 [M_MBP_2] = { "mbp2", 0, 0, 0, 0 }, /* placeholder */
72 [M_MBP_SR] = { "mbp3", 0x80030000, 2048 * 4, 1440, 900 },
73 [M_MBP_4] = { "mbp4", 0xc0060000, 2048 * 4, 1920, 1200 },
Thomas Gerlach8a3bdfe2010-04-23 13:17:50 -040074 [M_MBP_5_1] = { "mbp51", 0xc0010000, 2048 * 4, 1440, 900 },
Peter Jones7c08c9a2008-10-15 22:03:43 -070075 [M_UNKNOWN] = { NULL, 0, 0, 0, 0 }
76};
77
78static int set_system(const struct dmi_system_id *id);
79
80#define EFIFB_DMI_SYSTEM_ID(vendor, name, enumid) \
81 { set_system, name, { \
82 DMI_MATCH(DMI_BIOS_VENDOR, vendor), \
83 DMI_MATCH(DMI_PRODUCT_NAME, name) }, \
84 &dmi_list[enumid] }
85
86static struct dmi_system_id __initdata dmi_system_table[] = {
87 EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "iMac4,1", M_I17),
88 /* At least one of these two will be right; maybe both? */
89 EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "iMac5,1", M_I20),
90 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac5,1", M_I20),
91 /* At least one of these two will be right; maybe both? */
92 EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "iMac6,1", M_I24),
93 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac6,1", M_I24),
94 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "iMac7,1", M_I20_SR),
95 EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "Macmini1,1", M_MINI),
96 EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBook1,1", M_MB),
97 /* At least one of these two will be right; maybe both? */
98 EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBook2,1", M_MB),
99 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook2,1", M_MB),
100 /* At least one of these two will be right; maybe both? */
101 EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBook3,1", M_MB),
102 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook3,1", M_MB),
103 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook4,1", M_MB),
104 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookAir1,1", M_MBA),
105 EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBookPro1,1", M_MBP),
106 EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBookPro2,1", M_MBP_2),
107 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro2,1", M_MBP_2),
108 EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBookPro3,1", M_MBP_SR),
109 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro3,1", M_MBP_SR),
110 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro4,1", M_MBP_4),
Thomas Gerlach8a3bdfe2010-04-23 13:17:50 -0400111 EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookPro5,1", M_MBP_5_1),
Peter Jones7c08c9a2008-10-15 22:03:43 -0700112 {},
113};
114
115static int set_system(const struct dmi_system_id *id)
116{
117 struct efifb_dmi_info *info = id->driver_data;
118 if (info->base == 0)
119 return -ENODEV;
120
121 printk(KERN_INFO "efifb: dmi detected %s - framebuffer at %p "
122 "(%dx%d, stride %d)\n", id->ident,
123 (void *)info->base, info->width, info->height,
124 info->stride);
125
126 /* Trust the bootloader over the DMI tables */
127 if (screen_info.lfb_base == 0)
128 screen_info.lfb_base = info->base;
129 if (screen_info.lfb_linelength == 0)
130 screen_info.lfb_linelength = info->stride;
131 if (screen_info.lfb_width == 0)
132 screen_info.lfb_width = info->width;
133 if (screen_info.lfb_height == 0)
134 screen_info.lfb_height = info->height;
Brian Malye14a685d2009-03-31 15:25:50 -0700135 if (screen_info.orig_video_isVGA == 0)
136 screen_info.orig_video_isVGA = VIDEO_TYPE_EFI;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700137
138 return 0;
139}
140
Huang, Ying7c831722007-11-28 16:21:55 -0800141static int efifb_setcolreg(unsigned regno, unsigned red, unsigned green,
142 unsigned blue, unsigned transp,
143 struct fb_info *info)
144{
145 /*
146 * Set a single color register. The values supplied are
147 * already rounded down to the hardware's capabilities
148 * (according to the entries in the `var' structure). Return
149 * != 0 for invalid regno.
150 */
151
152 if (regno >= info->cmap.len)
153 return 1;
154
155 if (regno < 16) {
156 red >>= 8;
157 green >>= 8;
158 blue >>= 8;
159 ((u32 *)(info->pseudo_palette))[regno] =
160 (red << info->var.red.offset) |
161 (green << info->var.green.offset) |
162 (blue << info->var.blue.offset);
163 }
164 return 0;
165}
166
Marcin Slusarz89f3f212010-02-22 12:44:22 -0800167static void efifb_destroy(struct fb_info *info)
168{
169 if (info->screen_base)
170 iounmap(info->screen_base);
Marcin Slusarz1471ca92010-05-16 17:27:03 +0200171 release_mem_region(info->apertures->ranges[0].base, info->apertures->ranges[0].size);
Marcin Slusarz89f3f212010-02-22 12:44:22 -0800172 framebuffer_release(info);
173}
174
Huang, Ying7c831722007-11-28 16:21:55 -0800175static struct fb_ops efifb_ops = {
176 .owner = THIS_MODULE,
Marcin Slusarz89f3f212010-02-22 12:44:22 -0800177 .fb_destroy = efifb_destroy,
Huang, Ying7c831722007-11-28 16:21:55 -0800178 .fb_setcolreg = efifb_setcolreg,
179 .fb_fillrect = cfb_fillrect,
180 .fb_copyarea = cfb_copyarea,
181 .fb_imageblit = cfb_imageblit,
182};
183
Peter Jones7c08c9a2008-10-15 22:03:43 -0700184static int __init efifb_setup(char *options)
185{
186 char *this_opt;
187 int i;
188
189 if (!options || !*options)
190 return 0;
191
192 while ((this_opt = strsep(&options, ",")) != NULL) {
193 if (!*this_opt) continue;
194
195 for (i = 0; i < M_UNKNOWN; i++) {
196 if (!strcmp(this_opt, dmi_list[i].optname) &&
197 dmi_list[i].base != 0) {
198 screen_info.lfb_base = dmi_list[i].base;
199 screen_info.lfb_linelength = dmi_list[i].stride;
200 screen_info.lfb_width = dmi_list[i].width;
201 screen_info.lfb_height = dmi_list[i].height;
202 }
203 }
204 if (!strncmp(this_opt, "base:", 5))
205 screen_info.lfb_base = simple_strtoul(this_opt+5, NULL, 0);
206 else if (!strncmp(this_opt, "stride:", 7))
207 screen_info.lfb_linelength = simple_strtoul(this_opt+7, NULL, 0) * 4;
208 else if (!strncmp(this_opt, "height:", 7))
209 screen_info.lfb_height = simple_strtoul(this_opt+7, NULL, 0);
210 else if (!strncmp(this_opt, "width:", 6))
211 screen_info.lfb_width = simple_strtoul(this_opt+6, NULL, 0);
212 }
213 return 0;
214}
215
Uwe Kleine-Königc2e13032010-02-04 20:56:51 +0100216static int __devinit efifb_probe(struct platform_device *dev)
Huang, Ying7c831722007-11-28 16:21:55 -0800217{
218 struct fb_info *info;
219 int err;
220 unsigned int size_vmode;
221 unsigned int size_remap;
222 unsigned int size_total;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700223 int request_succeeded = 0;
224
Peter Jones7c08c9a2008-10-15 22:03:43 -0700225 if (!screen_info.lfb_depth)
226 screen_info.lfb_depth = 32;
227 if (!screen_info.pages)
228 screen_info.pages = 1;
Matthew Garrett133bb072009-04-13 14:39:44 -0700229 if (!screen_info.lfb_base) {
230 printk(KERN_DEBUG "efifb: invalid framebuffer address\n");
231 return -ENODEV;
232 }
233 printk(KERN_INFO "efifb: probing for efifb\n");
Peter Jones7c08c9a2008-10-15 22:03:43 -0700234
235 /* just assume they're all unset if any are */
236 if (!screen_info.blue_size) {
237 screen_info.blue_size = 8;
238 screen_info.blue_pos = 0;
239 screen_info.green_size = 8;
240 screen_info.green_pos = 8;
241 screen_info.red_size = 8;
242 screen_info.red_pos = 16;
243 screen_info.rsvd_size = 8;
244 screen_info.rsvd_pos = 24;
245 }
Huang, Ying7c831722007-11-28 16:21:55 -0800246
247 efifb_fix.smem_start = screen_info.lfb_base;
248 efifb_defined.bits_per_pixel = screen_info.lfb_depth;
249 efifb_defined.xres = screen_info.lfb_width;
250 efifb_defined.yres = screen_info.lfb_height;
251 efifb_fix.line_length = screen_info.lfb_linelength;
252
253 /* size_vmode -- that is the amount of memory needed for the
254 * used video mode, i.e. the minimum amount of
255 * memory we need. */
256 size_vmode = efifb_defined.yres * efifb_fix.line_length;
257
258 /* size_total -- all video memory we have. Used for
259 * entries, ressource allocation and bounds
260 * checking. */
261 size_total = screen_info.lfb_size;
262 if (size_total < size_vmode)
263 size_total = size_vmode;
264
265 /* size_remap -- the amount of video memory we are going to
266 * use for efifb. With modern cards it is no
267 * option to simply use size_total as that
268 * wastes plenty of kernel address space. */
269 size_remap = size_vmode * 2;
Huang, Ying7c831722007-11-28 16:21:55 -0800270 if (size_remap > size_total)
271 size_remap = size_total;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700272 if (size_remap % PAGE_SIZE)
273 size_remap += PAGE_SIZE - (size_remap % PAGE_SIZE);
Huang, Ying7c831722007-11-28 16:21:55 -0800274 efifb_fix.smem_len = size_remap;
275
Peter Jones7c08c9a2008-10-15 22:03:43 -0700276 if (request_mem_region(efifb_fix.smem_start, size_remap, "efifb")) {
277 request_succeeded = 1;
278 } else {
Huang, Ying7c831722007-11-28 16:21:55 -0800279 /* We cannot make this fatal. Sometimes this comes from magic
280 spaces our resource handlers simply don't know about */
281 printk(KERN_WARNING
282 "efifb: cannot reserve video memory at 0x%lx\n",
283 efifb_fix.smem_start);
Peter Jones7c08c9a2008-10-15 22:03:43 -0700284 }
Huang, Ying7c831722007-11-28 16:21:55 -0800285
286 info = framebuffer_alloc(sizeof(u32) * 16, &dev->dev);
287 if (!info) {
Peter Jones7c08c9a2008-10-15 22:03:43 -0700288 printk(KERN_ERR "efifb: cannot allocate framebuffer\n");
Huang, Ying7c831722007-11-28 16:21:55 -0800289 err = -ENOMEM;
290 goto err_release_mem;
291 }
292 info->pseudo_palette = info->par;
293 info->par = NULL;
294
Marcin Slusarz1471ca92010-05-16 17:27:03 +0200295 info->apertures = alloc_apertures(1);
296 if (!info->apertures) {
297 err = -ENOMEM;
298 goto err_release_fb;
299 }
300 info->apertures->ranges[0].base = efifb_fix.smem_start;
301 info->apertures->ranges[0].size = size_remap;
Dave Airlie4410f392009-06-16 15:34:38 -0700302
Huang, Ying7c831722007-11-28 16:21:55 -0800303 info->screen_base = ioremap(efifb_fix.smem_start, efifb_fix.smem_len);
304 if (!info->screen_base) {
305 printk(KERN_ERR "efifb: abort, cannot ioremap video memory "
306 "0x%x @ 0x%lx\n",
307 efifb_fix.smem_len, efifb_fix.smem_start);
308 err = -EIO;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700309 goto err_release_fb;
Huang, Ying7c831722007-11-28 16:21:55 -0800310 }
311
312 printk(KERN_INFO "efifb: framebuffer at 0x%lx, mapped to 0x%p, "
313 "using %dk, total %dk\n",
314 efifb_fix.smem_start, info->screen_base,
315 size_remap/1024, size_total/1024);
316 printk(KERN_INFO "efifb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
317 efifb_defined.xres, efifb_defined.yres,
318 efifb_defined.bits_per_pixel, efifb_fix.line_length,
319 screen_info.pages);
320
321 efifb_defined.xres_virtual = efifb_defined.xres;
322 efifb_defined.yres_virtual = efifb_fix.smem_len /
323 efifb_fix.line_length;
324 printk(KERN_INFO "efifb: scrolling: redraw\n");
325 efifb_defined.yres_virtual = efifb_defined.yres;
326
327 /* some dummy values for timing to make fbset happy */
328 efifb_defined.pixclock = 10000000 / efifb_defined.xres *
329 1000 / efifb_defined.yres;
330 efifb_defined.left_margin = (efifb_defined.xres / 8) & 0xf8;
331 efifb_defined.hsync_len = (efifb_defined.xres / 8) & 0xf8;
332
333 efifb_defined.red.offset = screen_info.red_pos;
334 efifb_defined.red.length = screen_info.red_size;
335 efifb_defined.green.offset = screen_info.green_pos;
336 efifb_defined.green.length = screen_info.green_size;
337 efifb_defined.blue.offset = screen_info.blue_pos;
338 efifb_defined.blue.length = screen_info.blue_size;
339 efifb_defined.transp.offset = screen_info.rsvd_pos;
340 efifb_defined.transp.length = screen_info.rsvd_size;
341
342 printk(KERN_INFO "efifb: %s: "
343 "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
344 "Truecolor",
345 screen_info.rsvd_size,
346 screen_info.red_size,
347 screen_info.green_size,
348 screen_info.blue_size,
349 screen_info.rsvd_pos,
350 screen_info.red_pos,
351 screen_info.green_pos,
352 screen_info.blue_pos);
353
354 efifb_fix.ypanstep = 0;
355 efifb_fix.ywrapstep = 0;
356
357 info->fbops = &efifb_ops;
358 info->var = efifb_defined;
359 info->fix = efifb_fix;
Dave Airlie4410f392009-06-16 15:34:38 -0700360 info->flags = FBINFO_FLAG_DEFAULT | FBINFO_MISC_FIRMWARE;
Huang, Ying7c831722007-11-28 16:21:55 -0800361
Peter Jones7c08c9a2008-10-15 22:03:43 -0700362 if ((err = fb_alloc_cmap(&info->cmap, 256, 0)) < 0) {
363 printk(KERN_ERR "efifb: cannot allocate colormap\n");
Huang, Ying7c831722007-11-28 16:21:55 -0800364 goto err_unmap;
365 }
Peter Jones7c08c9a2008-10-15 22:03:43 -0700366 if ((err = register_framebuffer(info)) < 0) {
367 printk(KERN_ERR "efifb: cannot register framebuffer\n");
Huang, Ying7c831722007-11-28 16:21:55 -0800368 goto err_fb_dealoc;
369 }
370 printk(KERN_INFO "fb%d: %s frame buffer device\n",
Peter Jones7c08c9a2008-10-15 22:03:43 -0700371 info->node, info->fix.id);
Huang, Ying7c831722007-11-28 16:21:55 -0800372 return 0;
373
374err_fb_dealoc:
375 fb_dealloc_cmap(&info->cmap);
376err_unmap:
377 iounmap(info->screen_base);
Peter Jones7c08c9a2008-10-15 22:03:43 -0700378err_release_fb:
Huang, Ying7c831722007-11-28 16:21:55 -0800379 framebuffer_release(info);
380err_release_mem:
Peter Jones7c08c9a2008-10-15 22:03:43 -0700381 if (request_succeeded)
382 release_mem_region(efifb_fix.smem_start, size_total);
Huang, Ying7c831722007-11-28 16:21:55 -0800383 return err;
384}
385
386static struct platform_driver efifb_driver = {
387 .probe = efifb_probe,
388 .driver = {
389 .name = "efifb",
390 },
391};
392
393static struct platform_device efifb_device = {
394 .name = "efifb",
395};
396
397static int __init efifb_init(void)
398{
399 int ret;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700400 char *option = NULL;
Huang, Ying7c831722007-11-28 16:21:55 -0800401
Brian Malye14a685d2009-03-31 15:25:50 -0700402 dmi_check_system(dmi_system_table);
403
Huang, Ying7c831722007-11-28 16:21:55 -0800404 if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI)
405 return -ENODEV;
Peter Jones7c08c9a2008-10-15 22:03:43 -0700406
407 if (fb_get_options("efifb", &option))
408 return -ENODEV;
409 efifb_setup(option);
410
411 /* We don't get linelength from UGA Draw Protocol, only from
412 * EFI Graphics Protocol. So if it's not in DMI, and it's not
413 * passed in from the user, we really can't use the framebuffer.
414 */
415 if (!screen_info.lfb_linelength)
416 return -ENODEV;
Huang, Ying7c831722007-11-28 16:21:55 -0800417
418 ret = platform_driver_register(&efifb_driver);
419
420 if (!ret) {
421 ret = platform_device_register(&efifb_device);
422 if (ret)
423 platform_driver_unregister(&efifb_driver);
424 }
425 return ret;
426}
427module_init(efifb_init);
428
429MODULE_LICENSE("GPL");