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