blob: bacfabd9ce1653825e14e85a6d702b067f54d9c6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * platinumfb.c -- frame buffer device for the PowerMac 'platinum' display
3 *
4 * Copyright (C) 1998 Franz Sirl
5 *
6 * Frame buffer structure from:
7 * drivers/video/controlfb.c -- frame buffer device for
8 * Apple 'control' display chip.
9 * Copyright (C) 1998 Dan Jacobowitz
10 *
11 * Hardware information from:
12 * platinum.c: Console support for PowerMac "platinum" display adaptor.
13 * Copyright (C) 1996 Paul Mackerras and Mark Abene
14 *
15 * This file is subject to the terms and conditions of the GNU General Public
16 * License. See the file COPYING in the main directory of this archive for
17 * more details.
18 */
19
Benjamin Herrenschmidt4c2a54b2007-09-19 14:50:22 +100020#undef DEBUG
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/errno.h>
25#include <linux/string.h>
26#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/slab.h>
28#include <linux/vmalloc.h>
29#include <linux/delay.h>
30#include <linux/interrupt.h>
31#include <linux/fb.h>
32#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/nvram.h>
Stephen Rothwellcec0dd92008-05-23 16:39:58 +100034#include <linux/of_device.h>
35#include <linux/of_platform.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/io.h>
37#include <asm/prom.h>
38#include <asm/pgtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#include "macmodes.h"
41#include "platinumfb.h"
42
43static int default_vmode = VMODE_NVRAM;
44static int default_cmode = CMODE_NVRAM;
45
46struct fb_info_platinum {
47 struct fb_info *info;
48
49 int vmode, cmode;
50 int xres, yres;
51 int vxres, vyres;
52 int xoffset, yoffset;
53
54 struct {
55 __u8 red, green, blue;
56 } palette[256];
Antonino A. Daplas02c2c202007-07-17 04:05:38 -070057 u32 pseudo_palette[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 volatile struct cmap_regs __iomem *cmap_regs;
60 unsigned long cmap_regs_phys;
61
62 volatile struct platinum_regs __iomem *platinum_regs;
63 unsigned long platinum_regs_phys;
64
65 __u8 __iomem *frame_buffer;
66 volatile __u8 __iomem *base_frame_buffer;
67 unsigned long frame_buffer_phys;
68
69 unsigned long total_vram;
70 int clktype;
71 int dactype;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +110072
73 struct resource rsrc_fb, rsrc_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074};
75
76/*
77 * Frame buffer device API
78 */
79
80static int platinumfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
81 u_int transp, struct fb_info *info);
82static int platinumfb_blank(int blank_mode, struct fb_info *info);
83static int platinumfb_set_par (struct fb_info *info);
84static int platinumfb_check_var (struct fb_var_screeninfo *var, struct fb_info *info);
85
86/*
87 * internal functions
88 */
89
90static inline int platinum_vram_reqd(int video_mode, int color_mode);
91static int read_platinum_sense(struct fb_info_platinum *pinfo);
92static void set_platinum_clock(struct fb_info_platinum *pinfo);
93static void platinum_set_hardware(struct fb_info_platinum *pinfo);
94static int platinum_var_to_par(struct fb_var_screeninfo *var,
95 struct fb_info_platinum *pinfo,
96 int check_only);
97
98/*
99 * Interface used by the world
100 */
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static struct fb_ops platinumfb_ops = {
103 .owner = THIS_MODULE,
104 .fb_check_var = platinumfb_check_var,
105 .fb_set_par = platinumfb_set_par,
106 .fb_setcolreg = platinumfb_setcolreg,
107 .fb_blank = platinumfb_blank,
108 .fb_fillrect = cfb_fillrect,
109 .fb_copyarea = cfb_copyarea,
110 .fb_imageblit = cfb_imageblit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111};
112
113/*
114 * Checks a var structure
115 */
116static int platinumfb_check_var (struct fb_var_screeninfo *var, struct fb_info *info)
117{
118 return platinum_var_to_par(var, info->par, 1);
119}
120
121/*
122 * Applies current var to display
123 */
124static int platinumfb_set_par (struct fb_info *info)
125{
126 struct fb_info_platinum *pinfo = info->par;
127 struct platinum_regvals *init;
128 int err, offset = 0x20;
129
130 if((err = platinum_var_to_par(&info->var, pinfo, 0))) {
131 printk (KERN_ERR "platinumfb_set_par: error calling"
132 " platinum_var_to_par: %d.\n", err);
133 return err;
134 }
135
136 platinum_set_hardware(pinfo);
137
138 init = platinum_reg_init[pinfo->vmode-1];
139
Benjamin Herrenschmidt9cf84d72005-12-13 17:48:35 +1100140 if ((pinfo->vmode == VMODE_832_624_75) && (pinfo->cmode > CMODE_8))
141 offset = 0x10;
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 info->screen_base = pinfo->frame_buffer + init->fb_offset + offset;
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700144 mutex_lock(&info->mm_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 info->fix.smem_start = (pinfo->frame_buffer_phys) + init->fb_offset + offset;
Krzysztof Helt537a1bf2009-06-30 11:41:29 -0700146 mutex_unlock(&info->mm_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 info->fix.visual = (pinfo->cmode == CMODE_8) ?
148 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_DIRECTCOLOR;
Benjamin Herrenschmidt9cf84d72005-12-13 17:48:35 +1100149 info->fix.line_length = vmode_attrs[pinfo->vmode-1].hres * (1<<pinfo->cmode)
150 + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 printk("line_length: %x\n", info->fix.line_length);
152 return 0;
153}
154
155static int platinumfb_blank(int blank, struct fb_info *fb)
156{
157/*
158 * Blank the screen if blank_mode != 0, else unblank. If blank == NULL
159 * then the caller blanks by setting the CLUT (Color Look Up Table) to all
160 * black. Return 0 if blanking succeeded, != 0 if un-/blanking failed due
161 * to e.g. a video mode which doesn't support it. Implements VESA suspend
162 * and powerdown modes on hardware that supports disabling hsync/vsync:
163 * blank_mode == 2: suspend vsync
164 * blank_mode == 3: suspend hsync
165 * blank_mode == 4: powerdown
166 */
167/* [danj] I think there's something fishy about those constants... */
168/*
169 struct fb_info_platinum *info = (struct fb_info_platinum *) fb;
170 int ctrl;
171
172 ctrl = ld_le32(&info->platinum_regs->ctrl.r) | 0x33;
173 if (blank)
174 --blank_mode;
175 if (blank & VESA_VSYNC_SUSPEND)
176 ctrl &= ~3;
177 if (blank & VESA_HSYNC_SUSPEND)
178 ctrl &= ~0x30;
179 out_le32(&info->platinum_regs->ctrl.r, ctrl);
180*/
181/* TODO: Figure out how the heck to powerdown this thing! */
182 return 0;
183}
184
185static int platinumfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
186 u_int transp, struct fb_info *info)
187{
188 struct fb_info_platinum *pinfo = info->par;
189 volatile struct cmap_regs __iomem *cmap_regs = pinfo->cmap_regs;
190
191 if (regno > 255)
192 return 1;
193
194 red >>= 8;
195 green >>= 8;
196 blue >>= 8;
197
198 pinfo->palette[regno].red = red;
199 pinfo->palette[regno].green = green;
200 pinfo->palette[regno].blue = blue;
201
202 out_8(&cmap_regs->addr, regno); /* tell clut what addr to fill */
203 out_8(&cmap_regs->lut, red); /* send one color channel at */
204 out_8(&cmap_regs->lut, green); /* a time... */
205 out_8(&cmap_regs->lut, blue);
206
207 if (regno < 16) {
208 int i;
209 u32 *pal = info->pseudo_palette;
210 switch (pinfo->cmode) {
211 case CMODE_16:
212 pal[regno] = (regno << 10) | (regno << 5) | regno;
213 break;
214 case CMODE_32:
215 i = (regno << 8) | regno;
216 pal[regno] = (i << 16) | i;
217 break;
218 }
219 }
220
221 return 0;
222}
223
224static inline int platinum_vram_reqd(int video_mode, int color_mode)
225{
226 return vmode_attrs[video_mode-1].vres *
Benjamin Herrenschmidt9cf84d72005-12-13 17:48:35 +1100227 (vmode_attrs[video_mode-1].hres * (1<<color_mode) +
228 ((video_mode == VMODE_832_624_75) &&
229 (color_mode > CMODE_8)) ? 0x10 : 0x20) + 0x1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
232#define STORE_D2(a, d) { \
233 out_8(&cmap_regs->addr, (a+32)); \
234 out_8(&cmap_regs->d2, (d)); \
235}
236
237static void set_platinum_clock(struct fb_info_platinum *pinfo)
238{
239 volatile struct cmap_regs __iomem *cmap_regs = pinfo->cmap_regs;
240 struct platinum_regvals *init;
241
242 init = platinum_reg_init[pinfo->vmode-1];
243
244 STORE_D2(6, 0xc6);
245 out_8(&cmap_regs->addr,3+32);
246
247 if (in_8(&cmap_regs->d2) == 2) {
248 STORE_D2(7, init->clock_params[pinfo->clktype][0]);
249 STORE_D2(8, init->clock_params[pinfo->clktype][1]);
250 STORE_D2(3, 3);
251 } else {
252 STORE_D2(4, init->clock_params[pinfo->clktype][0]);
253 STORE_D2(5, init->clock_params[pinfo->clktype][1]);
254 STORE_D2(3, 2);
255 }
256
257 __delay(5000);
258 STORE_D2(9, 0xa6);
259}
260
261
262/* Now how about actually saying, Make it so! */
263/* Some things in here probably don't need to be done each time. */
264static void platinum_set_hardware(struct fb_info_platinum *pinfo)
265{
266 volatile struct platinum_regs __iomem *platinum_regs = pinfo->platinum_regs;
267 volatile struct cmap_regs __iomem *cmap_regs = pinfo->cmap_regs;
268 struct platinum_regvals *init;
269 int i;
270 int vmode, cmode;
271
272 vmode = pinfo->vmode;
273 cmode = pinfo->cmode;
274
275 init = platinum_reg_init[vmode - 1];
276
277 /* Initialize display timing registers */
278 out_be32(&platinum_regs->reg[24].r, 7); /* turn display off */
279
280 for (i = 0; i < 26; ++i)
281 out_be32(&platinum_regs->reg[i+32].r, init->regs[i]);
282
283 out_be32(&platinum_regs->reg[26+32].r, (pinfo->total_vram == 0x100000 ?
284 init->offset[cmode] + 4 - cmode :
285 init->offset[cmode]));
286 out_be32(&platinum_regs->reg[16].r, (unsigned) pinfo->frame_buffer_phys+init->fb_offset+0x10);
287 out_be32(&platinum_regs->reg[18].r, init->pitch[cmode]);
288 out_be32(&platinum_regs->reg[19].r, (pinfo->total_vram == 0x100000 ?
289 init->mode[cmode+1] :
290 init->mode[cmode]));
291 out_be32(&platinum_regs->reg[20].r, (pinfo->total_vram == 0x100000 ? 0x11 : 0x1011));
292 out_be32(&platinum_regs->reg[21].r, 0x100);
293 out_be32(&platinum_regs->reg[22].r, 1);
294 out_be32(&platinum_regs->reg[23].r, 1);
295 out_be32(&platinum_regs->reg[26].r, 0xc00);
296 out_be32(&platinum_regs->reg[27].r, 0x235);
297 /* out_be32(&platinum_regs->reg[27].r, 0x2aa); */
298
299 STORE_D2(0, (pinfo->total_vram == 0x100000 ?
300 init->dacula_ctrl[cmode] & 0xf :
301 init->dacula_ctrl[cmode]));
302 STORE_D2(1, 4);
303 STORE_D2(2, 0);
304
305 set_platinum_clock(pinfo);
306
307 out_be32(&platinum_regs->reg[24].r, 0); /* turn display on */
308}
309
310/*
311 * Set misc info vars for this driver
312 */
313static void __devinit platinum_init_info(struct fb_info *info, struct fb_info_platinum *pinfo)
314{
315 /* Fill fb_info */
316 info->fbops = &platinumfb_ops;
317 info->pseudo_palette = pinfo->pseudo_palette;
318 info->flags = FBINFO_DEFAULT;
319 info->screen_base = pinfo->frame_buffer + 0x20;
320
321 fb_alloc_cmap(&info->cmap, 256, 0);
322
323 /* Fill fix common fields */
324 strcpy(info->fix.id, "platinum");
325 info->fix.mmio_start = pinfo->platinum_regs_phys;
326 info->fix.mmio_len = 0x1000;
327 info->fix.type = FB_TYPE_PACKED_PIXELS;
328 info->fix.smem_start = pinfo->frame_buffer_phys + 0x20; /* will be updated later */
329 info->fix.smem_len = pinfo->total_vram - 0x20;
330 info->fix.ywrapstep = 0;
331 info->fix.xpanstep = 0;
332 info->fix.ypanstep = 0;
333 info->fix.type_aux = 0;
334 info->fix.accel = FB_ACCEL_NONE;
335}
336
337
338static int __devinit platinum_init_fb(struct fb_info *info)
339{
340 struct fb_info_platinum *pinfo = info->par;
341 struct fb_var_screeninfo var;
342 int sense, rc;
343
344 sense = read_platinum_sense(pinfo);
345 printk(KERN_INFO "platinumfb: Monitor sense value = 0x%x, ", sense);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (default_vmode == VMODE_NVRAM) {
Ben Collinsf3f6f9a2006-10-18 08:52:48 -0400347#ifdef CONFIG_NVRAM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 default_vmode = nvram_read_byte(NV_VMODE);
349 if (default_vmode <= 0 || default_vmode > VMODE_MAX ||
350 !platinum_reg_init[default_vmode-1])
Ben Collinsf3f6f9a2006-10-18 08:52:48 -0400351#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 default_vmode = VMODE_CHOOSE;
353 }
354 if (default_vmode == VMODE_CHOOSE) {
355 default_vmode = mac_map_monitor_sense(sense);
356 }
357 if (default_vmode <= 0 || default_vmode > VMODE_MAX)
358 default_vmode = VMODE_640_480_60;
Ben Collinsf3f6f9a2006-10-18 08:52:48 -0400359#ifdef CONFIG_NVRAM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (default_cmode == CMODE_NVRAM)
361 default_cmode = nvram_read_byte(NV_CMODE);
Ben Collinsf3f6f9a2006-10-18 08:52:48 -0400362#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (default_cmode < CMODE_8 || default_cmode > CMODE_32)
364 default_cmode = CMODE_8;
365 /*
366 * Reduce the pixel size if we don't have enough VRAM.
367 */
368 while(default_cmode > CMODE_8 &&
369 platinum_vram_reqd(default_vmode, default_cmode) > pinfo->total_vram)
370 default_cmode--;
371
372 printk("platinumfb: Using video mode %d and color mode %d.\n", default_vmode, default_cmode);
373
374 /* Setup default var */
375 if (mac_vmode_to_var(default_vmode, default_cmode, &var) < 0) {
376 /* This shouldn't happen! */
377 printk("mac_vmode_to_var(%d, %d,) failed\n", default_vmode, default_cmode);
378try_again:
379 default_vmode = VMODE_640_480_60;
380 default_cmode = CMODE_8;
381 if (mac_vmode_to_var(default_vmode, default_cmode, &var) < 0) {
382 printk(KERN_ERR "platinumfb: mac_vmode_to_var() failed\n");
383 return -ENXIO;
384 }
385 }
386
387 /* Initialize info structure */
388 platinum_init_info(info, pinfo);
389
390 /* Apply default var */
391 info->var = var;
392 var.activate = FB_ACTIVATE_NOW;
393 rc = fb_set_var(info, &var);
394 if (rc && (default_vmode != VMODE_640_480_60 || default_cmode != CMODE_8))
395 goto try_again;
396
397 /* Register with fbdev layer */
398 rc = register_framebuffer(info);
399 if (rc < 0)
400 return rc;
401
402 printk(KERN_INFO "fb%d: Apple Platinum frame buffer device\n", info->node);
403
404 return 0;
405}
406
407/*
408 * Get the monitor sense value.
409 * Note that this can be called before calibrate_delay,
410 * so we can't use udelay.
411 */
412static int read_platinum_sense(struct fb_info_platinum *info)
413{
414 volatile struct platinum_regs __iomem *platinum_regs = info->platinum_regs;
415 int sense;
416
417 out_be32(&platinum_regs->reg[23].r, 7); /* turn off drivers */
418 __delay(2000);
419 sense = (~in_be32(&platinum_regs->reg[23].r) & 7) << 8;
420
421 /* drive each sense line low in turn and collect the other 2 */
422 out_be32(&platinum_regs->reg[23].r, 3); /* drive A low */
423 __delay(2000);
424 sense |= (~in_be32(&platinum_regs->reg[23].r) & 3) << 4;
425 out_be32(&platinum_regs->reg[23].r, 5); /* drive B low */
426 __delay(2000);
427 sense |= (~in_be32(&platinum_regs->reg[23].r) & 4) << 1;
428 sense |= (~in_be32(&platinum_regs->reg[23].r) & 1) << 2;
429 out_be32(&platinum_regs->reg[23].r, 6); /* drive C low */
430 __delay(2000);
431 sense |= (~in_be32(&platinum_regs->reg[23].r) & 6) >> 1;
432
433 out_be32(&platinum_regs->reg[23].r, 7); /* turn off drivers */
434
435 return sense;
436}
437
438/*
439 * This routine takes a user-supplied var, and picks the best vmode/cmode from it.
440 * It also updates the var structure to the actual mode data obtained
441 */
442static int platinum_var_to_par(struct fb_var_screeninfo *var,
443 struct fb_info_platinum *pinfo,
444 int check_only)
445{
446 int vmode, cmode;
447
448 if (mac_var_to_vmode(var, &vmode, &cmode) != 0) {
449 printk(KERN_ERR "platinum_var_to_par: mac_var_to_vmode unsuccessful.\n");
450 printk(KERN_ERR "platinum_var_to_par: var->xres = %d\n", var->xres);
451 printk(KERN_ERR "platinum_var_to_par: var->yres = %d\n", var->yres);
452 printk(KERN_ERR "platinum_var_to_par: var->xres_virtual = %d\n", var->xres_virtual);
453 printk(KERN_ERR "platinum_var_to_par: var->yres_virtual = %d\n", var->yres_virtual);
454 printk(KERN_ERR "platinum_var_to_par: var->bits_per_pixel = %d\n", var->bits_per_pixel);
455 printk(KERN_ERR "platinum_var_to_par: var->pixclock = %d\n", var->pixclock);
456 printk(KERN_ERR "platinum_var_to_par: var->vmode = %d\n", var->vmode);
457 return -EINVAL;
458 }
459
460 if (!platinum_reg_init[vmode-1]) {
461 printk(KERN_ERR "platinum_var_to_par, vmode %d not valid.\n", vmode);
462 return -EINVAL;
463 }
464
465 if (platinum_vram_reqd(vmode, cmode) > pinfo->total_vram) {
466 printk(KERN_ERR "platinum_var_to_par, not enough ram for vmode %d, cmode %d.\n", vmode, cmode);
467 return -EINVAL;
468 }
469
470 if (mac_vmode_to_var(vmode, cmode, var))
471 return -EINVAL;
472
473 if (check_only)
474 return 0;
475
476 pinfo->vmode = vmode;
477 pinfo->cmode = cmode;
478 pinfo->xres = vmode_attrs[vmode-1].hres;
479 pinfo->yres = vmode_attrs[vmode-1].vres;
480 pinfo->xoffset = 0;
481 pinfo->yoffset = 0;
482 pinfo->vxres = pinfo->xres;
483 pinfo->vyres = pinfo->yres;
484
485 return 0;
486}
487
488
489/*
490 * Parse user speficied options (`video=platinumfb:')
491 */
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100492static int __init platinumfb_setup(char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
494 char *this_opt;
495
496 if (!options || !*options)
497 return 0;
498
499 while ((this_opt = strsep(&options, ",")) != NULL) {
500 if (!strncmp(this_opt, "vmode:", 6)) {
501 int vmode = simple_strtoul(this_opt+6, NULL, 0);
502 if (vmode > 0 && vmode <= VMODE_MAX)
503 default_vmode = vmode;
504 } else if (!strncmp(this_opt, "cmode:", 6)) {
505 int depth = simple_strtoul(this_opt+6, NULL, 0);
506 switch (depth) {
507 case 0:
508 case 8:
509 default_cmode = CMODE_8;
510 break;
511 case 15:
512 case 16:
513 default_cmode = CMODE_16;
514 break;
515 case 24:
516 case 32:
517 default_cmode = CMODE_32;
518 break;
519 }
520 }
521 }
522 return 0;
523}
524
525#ifdef __powerpc__
526#define invalidate_cache(addr) \
527 asm volatile("eieio; dcbf 0,%1" \
528 : "=m" (*(addr)) : "r" (addr) : "memory");
529#else
530#define invalidate_cache(addr)
531#endif
532
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100533static int __devinit platinumfb_probe(struct of_device* odev,
534 const struct of_device_id *match)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
536 struct device_node *dp = odev->node;
537 struct fb_info *info;
538 struct fb_info_platinum *pinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 volatile __u8 *fbuffer;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100540 int bank0, bank1, bank2, bank3, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Benjamin Herrenschmidt4c2a54b2007-09-19 14:50:22 +1000542 dev_info(&odev->dev, "Found Apple Platinum video hardware\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 info = framebuffer_alloc(sizeof(*pinfo), &odev->dev);
Benjamin Herrenschmidt4c2a54b2007-09-19 14:50:22 +1000545 if (info == NULL) {
546 dev_err(&odev->dev, "Failed to allocate fbdev !\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return -ENOMEM;
Benjamin Herrenschmidt4c2a54b2007-09-19 14:50:22 +1000548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 pinfo = info->par;
550
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100551 if (of_address_to_resource(dp, 0, &pinfo->rsrc_reg) ||
552 of_address_to_resource(dp, 1, &pinfo->rsrc_fb)) {
Benjamin Herrenschmidt4c2a54b2007-09-19 14:50:22 +1000553 dev_err(&odev->dev, "Can't get resources\n");
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100554 framebuffer_release(info);
555 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
Benjamin Herrenschmidt4c2a54b2007-09-19 14:50:22 +1000557 dev_dbg(&odev->dev, " registers : 0x%llx...0x%llx\n",
558 (unsigned long long)pinfo->rsrc_reg.start,
559 (unsigned long long)pinfo->rsrc_reg.end);
560 dev_dbg(&odev->dev, " framebuffer: 0x%llx...0x%llx\n",
561 (unsigned long long)pinfo->rsrc_fb.start,
562 (unsigned long long)pinfo->rsrc_fb.end);
563
564 /* Do not try to request register space, they overlap with the
565 * northbridge and that can fail. Only request framebuffer
566 */
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100567 if (!request_mem_region(pinfo->rsrc_fb.start,
Benjamin Herrenschmidt4c2a54b2007-09-19 14:50:22 +1000568 pinfo->rsrc_fb.end - pinfo->rsrc_fb.start + 1,
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100569 "platinumfb framebuffer")) {
Benjamin Herrenschmidt4c2a54b2007-09-19 14:50:22 +1000570 printk(KERN_ERR "platinumfb: Can't request framebuffer !\n");
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100571 framebuffer_release(info);
572 return -ENXIO;
573 }
574
575 /* frame buffer - map only 4MB */
576 pinfo->frame_buffer_phys = pinfo->rsrc_fb.start;
577 pinfo->frame_buffer = __ioremap(pinfo->rsrc_fb.start, 0x400000,
578 _PAGE_WRITETHRU);
579 pinfo->base_frame_buffer = pinfo->frame_buffer;
580
581 /* registers */
582 pinfo->platinum_regs_phys = pinfo->rsrc_reg.start;
583 pinfo->platinum_regs = ioremap(pinfo->rsrc_reg.start, 0x1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 pinfo->cmap_regs_phys = 0xf301b000; /* XXX not in prom? */
586 request_mem_region(pinfo->cmap_regs_phys, 0x1000, "platinumfb cmap");
587 pinfo->cmap_regs = ioremap(pinfo->cmap_regs_phys, 0x1000);
588
589 /* Grok total video ram */
590 out_be32(&pinfo->platinum_regs->reg[16].r, (unsigned)pinfo->frame_buffer_phys);
591 out_be32(&pinfo->platinum_regs->reg[20].r, 0x1011); /* select max vram */
592 out_be32(&pinfo->platinum_regs->reg[24].r, 0); /* switch in vram */
593
594 fbuffer = pinfo->base_frame_buffer;
595 fbuffer[0x100000] = 0x34;
596 fbuffer[0x100008] = 0x0;
597 invalidate_cache(&fbuffer[0x100000]);
598 fbuffer[0x200000] = 0x56;
599 fbuffer[0x200008] = 0x0;
600 invalidate_cache(&fbuffer[0x200000]);
601 fbuffer[0x300000] = 0x78;
602 fbuffer[0x300008] = 0x0;
603 invalidate_cache(&fbuffer[0x300000]);
604 bank0 = 1; /* builtin 1MB vram, always there */
605 bank1 = fbuffer[0x100000] == 0x34;
606 bank2 = fbuffer[0x200000] == 0x56;
607 bank3 = fbuffer[0x300000] == 0x78;
608 pinfo->total_vram = (bank0 + bank1 + bank2 + bank3) * 0x100000;
Benjamin Herrenschmidt4c2a54b2007-09-19 14:50:22 +1000609 printk(KERN_INFO "platinumfb: Total VRAM = %dMB (%d%d%d%d)\n",
610 (unsigned int) (pinfo->total_vram / 1024 / 1024),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 bank3, bank2, bank1, bank0);
612
613 /*
614 * Try to determine whether we have an old or a new DACula.
615 */
616 out_8(&pinfo->cmap_regs->addr, 0x40);
617 pinfo->dactype = in_8(&pinfo->cmap_regs->d2);
618 switch (pinfo->dactype) {
619 case 0x3c:
620 pinfo->clktype = 1;
621 printk(KERN_INFO "platinumfb: DACula type 0x3c\n");
622 break;
623 case 0x84:
624 pinfo->clktype = 0;
625 printk(KERN_INFO "platinumfb: DACula type 0x84\n");
626 break;
627 default:
628 pinfo->clktype = 0;
629 printk(KERN_INFO "platinumfb: Unknown DACula type: %x\n", pinfo->dactype);
630 break;
631 }
632 dev_set_drvdata(&odev->dev, info);
633
634 rc = platinum_init_fb(info);
635 if (rc != 0) {
Amol Lad2b7574d2006-12-08 02:40:07 -0800636 iounmap(pinfo->frame_buffer);
637 iounmap(pinfo->platinum_regs);
638 iounmap(pinfo->cmap_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 dev_set_drvdata(&odev->dev, NULL);
640 framebuffer_release(info);
641 }
642
643 return rc;
644}
645
646static int __devexit platinumfb_remove(struct of_device* odev)
647{
648 struct fb_info *info = dev_get_drvdata(&odev->dev);
649 struct fb_info_platinum *pinfo = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 unregister_framebuffer (info);
652
653 /* Unmap frame buffer and registers */
Benjamin Herrenschmidt4c2a54b2007-09-19 14:50:22 +1000654 iounmap(pinfo->frame_buffer);
655 iounmap(pinfo->platinum_regs);
656 iounmap(pinfo->cmap_regs);
657
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100658 release_mem_region(pinfo->rsrc_fb.start,
659 pinfo->rsrc_fb.end -
660 pinfo->rsrc_fb.start + 1);
Benjamin Herrenschmidt4c2a54b2007-09-19 14:50:22 +1000661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 release_mem_region(pinfo->cmap_regs_phys, 0x1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 framebuffer_release(info);
665
666 return 0;
667}
668
Jeff Mahoney5e655772005-07-06 15:44:41 -0400669static struct of_device_id platinumfb_match[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
671 {
672 .name = "platinum",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 },
674 {},
675};
676
677static struct of_platform_driver platinum_driver =
678{
679 .name = "platinumfb",
680 .match_table = platinumfb_match,
681 .probe = platinumfb_probe,
682 .remove = platinumfb_remove,
683};
684
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100685static int __init platinumfb_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
687#ifndef MODULE
688 char *option = NULL;
689
690 if (fb_get_options("platinumfb", &option))
691 return -ENODEV;
692 platinumfb_setup(option);
693#endif
Benjamin Herrenschmidt7eebde72006-11-11 17:24:59 +1100694 of_register_platform_driver(&platinum_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 return 0;
697}
698
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100699static void __exit platinumfb_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Benjamin Herrenschmidt7eebde72006-11-11 17:24:59 +1100701 of_unregister_platform_driver(&platinum_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
704MODULE_LICENSE("GPL");
705MODULE_DESCRIPTION("framebuffer driver for Apple Platinum video");
706module_init(platinumfb_init);
707
708#ifdef MODULE
709module_exit(platinumfb_exit);
710#endif