Merge branch 'upstream-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/linvil...
[pandora-kernel.git] / drivers / video / valkyriefb.c
1 /*
2  *  valkyriefb.c -- frame buffer device for the PowerMac 'valkyrie' display
3  *
4  *  Created 8 August 1998 by 
5  *  Martin Costabel <costabel@wanadoo.fr> and Kevin Schoedel
6  *
7  *  Vmode-switching changes and vmode 15/17 modifications created 29 August
8  *  1998 by Barry K. Nathan <barryn@pobox.com>.
9  *
10  *  Ported to m68k Macintosh by David Huggins-Daines <dhd@debian.org>
11  *
12  *  Derived directly from:
13  *
14  *   controlfb.c -- frame buffer device for the PowerMac 'control' display
15  *   Copyright (C) 1998 Dan Jacobowitz <dan@debian.org>
16  *
17  *   pmc-valkyrie.c -- Console support for PowerMac "valkyrie" display adaptor.
18  *   Copyright (C) 1997 Paul Mackerras.
19  *
20  *  and indirectly:
21  *
22  *  Frame buffer structure from:
23  *    drivers/video/chipsfb.c -- frame buffer device for
24  *    Chips & Technologies 65550 chip.
25  *
26  *    Copyright (C) 1998 Paul Mackerras
27  *
28  *    This file is derived from the Powermac "chips" driver:
29  *    Copyright (C) 1997 Fabio Riccardi.
30  *    And from the frame buffer device for Open Firmware-initialized devices:
31  *    Copyright (C) 1997 Geert Uytterhoeven.
32  *
33  *  Hardware information from:
34  *    control.c: Console support for PowerMac "control" display adaptor.
35  *    Copyright (C) 1996 Paul Mackerras
36  *
37  *  This file is subject to the terms and conditions of the GNU General Public
38  *  License. See the file COPYING in the main directory of this archive for
39  *  more details.
40  */
41
42 #include <linux/module.h>
43 #include <linux/kernel.h>
44 #include <linux/errno.h>
45 #include <linux/string.h>
46 #include <linux/mm.h>
47 #include <linux/slab.h>
48 #include <linux/vmalloc.h>
49 #include <linux/delay.h>
50 #include <linux/interrupt.h>
51 #include <linux/fb.h>
52 #include <linux/selection.h>
53 #include <linux/init.h>
54 #include <linux/pci.h>
55 #include <linux/nvram.h>
56 #include <linux/adb.h>
57 #include <linux/cuda.h>
58 #include <asm/io.h>
59 #ifdef CONFIG_MAC
60 #include <asm/bootinfo.h>
61 #include <asm/macintosh.h>
62 #else
63 #include <asm/prom.h>
64 #endif
65 #include <asm/pgtable.h>
66
67 #include "macmodes.h"
68 #include "valkyriefb.h"
69
70 #ifdef CONFIG_MAC
71 /* We don't yet have functions to read the PRAM... perhaps we can
72    adapt them from the PPC code? */
73 static int default_vmode = VMODE_640_480_67;
74 static int default_cmode = CMODE_8;
75 #else
76 static int default_vmode = VMODE_NVRAM;
77 static int default_cmode = CMODE_NVRAM;
78 #endif
79
80 struct fb_par_valkyrie {
81         int     vmode, cmode;
82         int     xres, yres;
83         int     vxres, vyres;
84         struct valkyrie_regvals *init;
85 };
86
87 struct fb_info_valkyrie {
88         struct fb_info          info;
89         struct fb_par_valkyrie  par;
90         struct cmap_regs        __iomem *cmap_regs;
91         unsigned long           cmap_regs_phys;
92         
93         struct valkyrie_regs    __iomem *valkyrie_regs;
94         unsigned long           valkyrie_regs_phys;
95         
96         __u8                    __iomem *frame_buffer;
97         unsigned long           frame_buffer_phys;
98         
99         int                     sense;
100         unsigned long           total_vram;
101
102         u32                     pseudo_palette[16];
103 };
104
105 /*
106  * Exported functions
107  */
108 int valkyriefb_init(void);
109 int valkyriefb_setup(char*);
110
111 static int valkyriefb_check_var(struct fb_var_screeninfo *var,
112                                 struct fb_info *info);
113 static int valkyriefb_set_par(struct fb_info *info);
114 static int valkyriefb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
115                              u_int transp, struct fb_info *info);
116 static int valkyriefb_blank(int blank_mode, struct fb_info *info);
117
118 static int read_valkyrie_sense(struct fb_info_valkyrie *p);
119 static void set_valkyrie_clock(unsigned char *params);
120 static int valkyrie_var_to_par(struct fb_var_screeninfo *var,
121         struct fb_par_valkyrie *par, const struct fb_info *fb_info);
122
123 static void valkyrie_init_info(struct fb_info *info, struct fb_info_valkyrie *p);
124 static void valkyrie_par_to_fix(struct fb_par_valkyrie *par, struct fb_fix_screeninfo *fix);
125 static void valkyrie_init_fix(struct fb_fix_screeninfo *fix, struct fb_info_valkyrie *p);
126
127 static struct fb_ops valkyriefb_ops = {
128         .owner =        THIS_MODULE,
129         .fb_check_var = valkyriefb_check_var,
130         .fb_set_par =   valkyriefb_set_par,
131         .fb_setcolreg = valkyriefb_setcolreg,
132         .fb_blank =     valkyriefb_blank,
133         .fb_fillrect    = cfb_fillrect,
134         .fb_copyarea    = cfb_copyarea,
135         .fb_imageblit   = cfb_imageblit,
136 };
137
138 /* Sets the video mode according to info->var */
139 static int valkyriefb_set_par(struct fb_info *info)
140 {
141         struct fb_info_valkyrie *p = (struct fb_info_valkyrie *) info;
142         volatile struct valkyrie_regs __iomem *valkyrie_regs = p->valkyrie_regs;
143         struct fb_par_valkyrie *par = info->par;
144         struct valkyrie_regvals *init;
145         int err;
146
147         if ((err = valkyrie_var_to_par(&info->var, par, info)))
148                 return err;
149
150         valkyrie_par_to_fix(par, &info->fix);
151
152         /* Reset the valkyrie */
153         out_8(&valkyrie_regs->status.r, 0);
154         udelay(100);
155
156         /* Initialize display timing registers */
157         init = par->init;
158         out_8(&valkyrie_regs->mode.r, init->mode | 0x80);
159         out_8(&valkyrie_regs->depth.r, par->cmode + 3);
160         set_valkyrie_clock(init->clock_params);
161         udelay(100);
162
163         /* Turn on display */
164         out_8(&valkyrie_regs->mode.r, init->mode);
165
166         return 0;
167 }
168
169 static inline int valkyrie_par_to_var(struct fb_par_valkyrie *par,
170                                       struct fb_var_screeninfo *var)
171 {
172         return mac_vmode_to_var(par->vmode, par->cmode, var);
173 }
174
175 static int
176 valkyriefb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
177 {
178         int err;
179         struct fb_par_valkyrie par;
180
181         if ((err = valkyrie_var_to_par(var, &par, info)))
182                 return err;
183         valkyrie_par_to_var(&par, var);
184         return 0;
185 }
186
187 /*
188  *  Blank the screen if blank_mode != 0, else unblank. If blank_mode == NULL
189  *  then the caller blanks by setting the CLUT (Color Look Up Table) to all
190  *  black. Return 0 if blanking succeeded, != 0 if un-/blanking failed due
191  *  to e.g. a video mode which doesn't support it. Implements VESA suspend
192  *  and powerdown modes on hardware that supports disabling hsync/vsync:
193  *    blank_mode == 2: suspend vsync
194  *    blank_mode == 3: suspend hsync
195  *    blank_mode == 4: powerdown
196  */
197 static int valkyriefb_blank(int blank_mode, struct fb_info *info)
198 {
199         struct fb_info_valkyrie *p = (struct fb_info_valkyrie *) info;
200         struct fb_par_valkyrie *par = info->par;
201         struct valkyrie_regvals *init = par->init;
202
203         if (init == NULL)
204                 return 1;
205
206         switch (blank_mode) {
207         case FB_BLANK_UNBLANK:                  /* unblank */
208                 out_8(&p->valkyrie_regs->mode.r, init->mode);
209                 break;
210         case FB_BLANK_NORMAL:
211                 return 1;       /* get caller to set CLUT to all black */
212         case FB_BLANK_VSYNC_SUSPEND:
213         case FB_BLANK_HSYNC_SUSPEND:
214                 /*
215                  * [kps] Value extracted from MacOS. I don't know
216                  * whether this bit disables hsync or vsync, or
217                  * whether the hardware can do the other as well.
218                  */
219                 out_8(&p->valkyrie_regs->mode.r, init->mode | 0x40);
220                 break;
221         case FB_BLANK_POWERDOWN:
222                 out_8(&p->valkyrie_regs->mode.r, 0x66);
223                 break;
224         }
225         return 0;
226 }
227
228 static int valkyriefb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
229                              u_int transp, struct fb_info *info)
230 {
231         struct fb_info_valkyrie *p = (struct fb_info_valkyrie *) info;
232         volatile struct cmap_regs __iomem *cmap_regs = p->cmap_regs;
233         struct fb_par_valkyrie *par = info->par;
234
235         if (regno > 255)
236                 return 1;
237         red >>= 8;
238         green >>= 8;
239         blue >>= 8;
240
241         /* tell clut which address to fill */
242         out_8(&p->cmap_regs->addr, regno);
243         udelay(1);
244         /* send one color channel at a time */
245         out_8(&cmap_regs->lut, red);
246         out_8(&cmap_regs->lut, green);
247         out_8(&cmap_regs->lut, blue);
248
249         if (regno < 16 && par->cmode == CMODE_16)
250                 ((u32 *)info->pseudo_palette)[regno] =
251                         (regno << 10) | (regno << 5) | regno;
252
253         return 0;
254 }
255
256 static inline int valkyrie_vram_reqd(int video_mode, int color_mode)
257 {
258         int pitch;
259         struct valkyrie_regvals *init = valkyrie_reg_init[video_mode-1];
260         
261         if ((pitch = init->pitch[color_mode]) == 0)
262                 pitch = 2 * init->pitch[0];
263         return init->vres * pitch;
264 }
265
266 static void set_valkyrie_clock(unsigned char *params)
267 {
268         struct adb_request req;
269         int i;
270
271 #ifdef CONFIG_ADB_CUDA
272         for (i = 0; i < 3; ++i) {
273                 cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_GET_SET_IIC,
274                              0x50, i + 1, params[i]);
275                 while (!req.complete)
276                         cuda_poll();
277         }
278 #endif
279 }
280
281 static void __init valkyrie_choose_mode(struct fb_info_valkyrie *p)
282 {
283         p->sense = read_valkyrie_sense(p);
284         printk(KERN_INFO "Monitor sense value = 0x%x\n", p->sense);
285
286         /* Try to pick a video mode out of NVRAM if we have one. */
287 #if !defined(CONFIG_MAC) && defined(CONFIG_NVRAM)
288         if (default_vmode == VMODE_NVRAM) {
289                 default_vmode = nvram_read_byte(NV_VMODE);
290                 if (default_vmode <= 0
291                  || default_vmode > VMODE_MAX
292                  || !valkyrie_reg_init[default_vmode - 1])
293                         default_vmode = VMODE_CHOOSE;
294         }
295 #endif
296         if (default_vmode == VMODE_CHOOSE)
297                 default_vmode = mac_map_monitor_sense(p->sense);
298         if (!valkyrie_reg_init[default_vmode - 1])
299                 default_vmode = VMODE_640_480_67;
300 #if !defined(CONFIG_MAC) && defined(CONFIG_NVRAM)
301         if (default_cmode == CMODE_NVRAM)
302                 default_cmode = nvram_read_byte(NV_CMODE);
303 #endif
304
305         /*
306          * Reduce the pixel size if we don't have enough VRAM or bandwidth.
307          */
308         if (default_cmode < CMODE_8 || default_cmode > CMODE_16
309             || valkyrie_reg_init[default_vmode-1]->pitch[default_cmode] == 0
310             || valkyrie_vram_reqd(default_vmode, default_cmode) > p->total_vram)
311                 default_cmode = CMODE_8;
312
313         printk(KERN_INFO "using video mode %d and color mode %d.\n",
314                default_vmode, default_cmode);
315 }
316
317 int __init valkyriefb_init(void)
318 {
319         struct fb_info_valkyrie *p;
320         unsigned long frame_buffer_phys, cmap_regs_phys, flags;
321         int err;
322         char *option = NULL;
323
324         if (fb_get_options("valkyriefb", &option))
325                 return -ENODEV;
326         valkyriefb_setup(option);
327
328 #ifdef CONFIG_MAC
329         if (!MACH_IS_MAC)
330                 return 0;
331         if (!(mac_bi_data.id == MAC_MODEL_Q630
332               /* I'm not sure about this one */
333             || mac_bi_data.id == MAC_MODEL_P588))
334                 return 0;
335
336         /* Hardcoded addresses... welcome to 68k Macintosh country :-) */
337         frame_buffer_phys = 0xf9000000;
338         cmap_regs_phys = 0x50f24000;
339         flags = IOMAP_NOCACHE_SER; /* IOMAP_WRITETHROUGH?? */
340 #else /* ppc (!CONFIG_MAC) */
341         {
342                 struct device_node *dp;
343                 struct resource r;
344
345                 dp = of_find_node_by_name(NULL, "valkyrie");
346                 if (dp == 0)
347                         return 0;
348
349                 if (of_address_to_resource(dp, 0, &r)) {
350                         printk(KERN_ERR "can't find address for valkyrie\n");
351                         return 0;
352                 }
353
354                 frame_buffer_phys = r.start;
355                 cmap_regs_phys = r.start + 0x304000;
356                 flags = _PAGE_WRITETHRU;
357         }
358 #endif /* ppc (!CONFIG_MAC) */
359
360         p = kmalloc(sizeof(*p), GFP_ATOMIC);
361         if (p == 0)
362                 return -ENOMEM;
363         memset(p, 0, sizeof(*p));
364
365         /* Map in frame buffer and registers */
366         if (!request_mem_region(frame_buffer_phys, 0x100000, "valkyriefb")) {
367                 kfree(p);
368                 return 0;
369         }
370         p->total_vram = 0x100000;
371         p->frame_buffer_phys = frame_buffer_phys;
372         p->frame_buffer = __ioremap(frame_buffer_phys, p->total_vram, flags);
373         p->cmap_regs_phys = cmap_regs_phys;
374         p->cmap_regs = ioremap(p->cmap_regs_phys, 0x1000);
375         p->valkyrie_regs_phys = cmap_regs_phys+0x6000;
376         p->valkyrie_regs = ioremap(p->valkyrie_regs_phys, 0x1000);
377         err = -ENOMEM;
378         if (p->frame_buffer == NULL || p->cmap_regs == NULL
379             || p->valkyrie_regs == NULL) {
380                 printk(KERN_ERR "valkyriefb: couldn't map resources\n");
381                 goto out_free;
382         }
383
384         valkyrie_choose_mode(p);
385         mac_vmode_to_var(default_vmode, default_cmode, &p->info.var);
386         valkyrie_init_info(&p->info, p);
387         valkyrie_init_fix(&p->info.fix, p);
388         if (valkyriefb_set_par(&p->info))
389                 /* "can't happen" */
390                 printk(KERN_ERR "valkyriefb: can't set default video mode\n");
391
392         if ((err = register_framebuffer(&p->info)) != 0)
393                 goto out_free;
394
395         printk(KERN_INFO "fb%d: valkyrie frame buffer device\n", p->info.node);
396         return 0;
397
398  out_free:
399         if (p->frame_buffer)
400                 iounmap(p->frame_buffer);
401         if (p->cmap_regs)
402                 iounmap(p->cmap_regs);
403         if (p->valkyrie_regs)
404                 iounmap(p->valkyrie_regs);
405         kfree(p);
406         return err;
407 }
408
409 /*
410  * Get the monitor sense value.
411  */
412 static int read_valkyrie_sense(struct fb_info_valkyrie *p)
413 {
414         int sense, in;
415
416         out_8(&p->valkyrie_regs->msense.r, 0);   /* release all lines */
417         __delay(20000);
418         sense = ((in = in_8(&p->valkyrie_regs->msense.r)) & 0x70) << 4;
419         /* drive each sense line low in turn and collect the other 2 */
420         out_8(&p->valkyrie_regs->msense.r, 4);   /* drive A low */
421         __delay(20000);
422         sense |= ((in = in_8(&p->valkyrie_regs->msense.r)) & 0x30);
423         out_8(&p->valkyrie_regs->msense.r, 2);   /* drive B low */
424         __delay(20000);
425         sense |= ((in = in_8(&p->valkyrie_regs->msense.r)) & 0x40) >> 3;
426         sense |= (in & 0x10) >> 2;
427         out_8(&p->valkyrie_regs->msense.r, 1);   /* drive C low */
428         __delay(20000);
429         sense |= ((in = in_8(&p->valkyrie_regs->msense.r)) & 0x60) >> 5;
430
431         out_8(&p->valkyrie_regs->msense.r, 7);
432
433         return sense;
434 }
435
436 /*
437  * This routine takes a user-supplied var,
438  * and picks the best vmode/cmode from it.
439  */
440
441 /* [bkn] I did a major overhaul of this function.
442  *
443  * Much of the old code was "swiped by jonh from atyfb.c". Because
444  * macmodes has mac_var_to_vmode, I felt that it would be better to
445  * rework this function to use that, instead of reinventing the wheel to
446  * add support for vmode 17. This was reinforced by the fact that
447  * the previously swiped atyfb.c code is no longer there.
448  *
449  * So, I swiped and adapted platinum_var_to_par (from platinumfb.c), replacing
450  * most, but not all, of the old code in the process. One side benefit of
451  * swiping the platinumfb code is that we now have more comprehensible error
452  * messages when a vmode/cmode switch fails. (Most of the error messages are
453  * platinumfb.c, but I added two of my own, and I also changed some commas
454  * into colons to make the messages more consistent with other Linux error
455  * messages.) In addition, I think the new code *might* fix some vmode-
456  * switching oddities, but I'm not sure.
457  *
458  * There may be some more opportunities for cleanup in here, but this is a
459  * good start...
460  */
461
462 static int valkyrie_var_to_par(struct fb_var_screeninfo *var,
463         struct fb_par_valkyrie *par, const struct fb_info *fb_info)
464 {
465         int vmode, cmode;
466         struct valkyrie_regvals *init;
467         struct fb_info_valkyrie *p = (struct fb_info_valkyrie *) fb_info;
468
469         if (mac_var_to_vmode(var, &vmode, &cmode) != 0) {
470                 printk(KERN_ERR "valkyriefb: can't do %dx%dx%d.\n",
471                        var->xres, var->yres, var->bits_per_pixel);
472                 return -EINVAL;
473         }
474
475         /* Check if we know about the wanted video mode */
476         if (vmode < 1 || vmode > VMODE_MAX || !valkyrie_reg_init[vmode-1]) {
477                 printk(KERN_ERR "valkyriefb: vmode %d not valid.\n", vmode);
478                 return -EINVAL;
479         }
480         
481         if (cmode != CMODE_8 && cmode != CMODE_16) {
482                 printk(KERN_ERR "valkyriefb: cmode %d not valid.\n", cmode);
483                 return -EINVAL;
484         }
485
486         if (var->xres_virtual > var->xres || var->yres_virtual > var->yres
487             || var->xoffset != 0 || var->yoffset != 0) {
488                 return -EINVAL;
489         }
490
491         init = valkyrie_reg_init[vmode-1];
492         if (init->pitch[cmode] == 0) {
493                 printk(KERN_ERR "valkyriefb: vmode %d does not support "
494                        "cmode %d.\n", vmode, cmode);
495                 return -EINVAL;
496         }
497
498         if (valkyrie_vram_reqd(vmode, cmode) > p->total_vram) {
499                 printk(KERN_ERR "valkyriefb: not enough ram for vmode %d, "
500                        "cmode %d.\n", vmode, cmode);
501                 return -EINVAL;
502         }
503
504         par->vmode = vmode;
505         par->cmode = cmode;
506         par->init = init;
507         par->xres = var->xres;
508         par->yres = var->yres;
509         par->vxres = par->xres;
510         par->vyres = par->yres;
511
512         return 0;
513 }
514
515 static void valkyrie_init_fix(struct fb_fix_screeninfo *fix, struct fb_info_valkyrie *p)
516 {
517         memset(fix, 0, sizeof(*fix));
518         strcpy(fix->id, "valkyrie");
519         fix->mmio_start = p->valkyrie_regs_phys;
520         fix->mmio_len = sizeof(struct valkyrie_regs);
521         fix->type = FB_TYPE_PACKED_PIXELS;
522         fix->smem_start = p->frame_buffer_phys + 0x1000;
523         fix->smem_len = p->total_vram;
524
525         fix->type_aux = 0;
526         fix->ywrapstep = 0;
527         fix->ypanstep = 0;
528         fix->xpanstep = 0;
529         
530 }
531
532 /* Fix must already be inited above */
533 static void valkyrie_par_to_fix(struct fb_par_valkyrie *par,
534         struct fb_fix_screeninfo *fix)
535 {
536         fix->smem_len = valkyrie_vram_reqd(par->vmode, par->cmode);
537         fix->visual = (par->cmode == CMODE_8) ?
538                 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_DIRECTCOLOR;
539         fix->line_length = par->vxres << par->cmode;
540                 /* ywrapstep, xpanstep, ypanstep */
541 }
542
543 static void __init valkyrie_init_info(struct fb_info *info, struct fb_info_valkyrie *p)
544 {
545         info->fbops = &valkyriefb_ops;
546         info->screen_base = p->frame_buffer + 0x1000;
547         info->flags = FBINFO_DEFAULT;
548         info->pseudo_palette = p->pseudo_palette;
549         fb_alloc_cmap(&info->cmap, 256, 0);
550         info->par = &p->par;
551 }
552
553
554 /*
555  * Parse user speficied options (`video=valkyriefb:')
556  */
557 int __init valkyriefb_setup(char *options)
558 {
559         char *this_opt;
560
561         if (!options || !*options)
562                 return 0;
563
564         while ((this_opt = strsep(&options, ",")) != NULL) {
565                 if (!strncmp(this_opt, "vmode:", 6)) {
566                         int vmode = simple_strtoul(this_opt+6, NULL, 0);
567                         if (vmode > 0 && vmode <= VMODE_MAX)
568                                 default_vmode = vmode;
569                 }
570                 else if (!strncmp(this_opt, "cmode:", 6)) {
571                         int depth = simple_strtoul(this_opt+6, NULL, 0);
572                         switch (depth) {
573                         case 8:
574                                 default_cmode = CMODE_8;
575                                 break;
576                         case 15:
577                         case 16:
578                                 default_cmode = CMODE_16;
579                                 break;
580                         }
581                 }
582         }
583         return 0;
584 }
585
586 module_init(valkyriefb_init);
587 MODULE_LICENSE("GPL");