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