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