1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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/nvram.h>
55#include <linux/adb.h>
56#include <linux/cuda.h>
57#include <asm/io.h>
58#ifdef CONFIG_MAC
59#include <asm/macintosh.h>
60#else
61#include <asm/prom.h>
62#endif
63#include <asm/pgtable.h>
64
65#include "macmodes.h"
66#include "valkyriefb.h"
67
68#ifdef CONFIG_MAC
69
70
71static int default_vmode = VMODE_CHOOSE;
72static int default_cmode = CMODE_8;
73#else
74static int default_vmode = VMODE_NVRAM;
75static int default_cmode = CMODE_NVRAM;
76#endif
77
78struct fb_par_valkyrie {
79 int vmode, cmode;
80 int xres, yres;
81 int vxres, vyres;
82 struct valkyrie_regvals *init;
83};
84
85struct fb_info_valkyrie {
86 struct fb_info info;
87 struct fb_par_valkyrie par;
88 struct cmap_regs __iomem *cmap_regs;
89 unsigned long cmap_regs_phys;
90
91 struct valkyrie_regs __iomem *valkyrie_regs;
92 unsigned long valkyrie_regs_phys;
93
94 __u8 __iomem *frame_buffer;
95 unsigned long frame_buffer_phys;
96
97 int sense;
98 unsigned long total_vram;
99
100 u32 pseudo_palette[16];
101};
102
103
104
105
106int valkyriefb_init(void);
107int valkyriefb_setup(char*);
108
109static int valkyriefb_check_var(struct fb_var_screeninfo *var,
110 struct fb_info *info);
111static int valkyriefb_set_par(struct fb_info *info);
112static int valkyriefb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
113 u_int transp, struct fb_info *info);
114static int valkyriefb_blank(int blank_mode, struct fb_info *info);
115
116static int read_valkyrie_sense(struct fb_info_valkyrie *p);
117static void set_valkyrie_clock(unsigned char *params);
118static int valkyrie_var_to_par(struct fb_var_screeninfo *var,
119 struct fb_par_valkyrie *par, const struct fb_info *fb_info);
120
121static int valkyrie_init_info(struct fb_info *info, struct fb_info_valkyrie *p);
122static void valkyrie_par_to_fix(struct fb_par_valkyrie *par, struct fb_fix_screeninfo *fix);
123static void valkyrie_init_fix(struct fb_fix_screeninfo *fix, struct fb_info_valkyrie *p);
124
125static struct fb_ops valkyriefb_ops = {
126 .owner = THIS_MODULE,
127 .fb_check_var = valkyriefb_check_var,
128 .fb_set_par = valkyriefb_set_par,
129 .fb_setcolreg = valkyriefb_setcolreg,
130 .fb_blank = valkyriefb_blank,
131 .fb_fillrect = cfb_fillrect,
132 .fb_copyarea = cfb_copyarea,
133 .fb_imageblit = cfb_imageblit,
134};
135
136
137static int valkyriefb_set_par(struct fb_info *info)
138{
139 struct fb_info_valkyrie *p = (struct fb_info_valkyrie *) info;
140 volatile struct valkyrie_regs __iomem *valkyrie_regs = p->valkyrie_regs;
141 struct fb_par_valkyrie *par = info->par;
142 struct valkyrie_regvals *init;
143 int err;
144
145 if ((err = valkyrie_var_to_par(&info->var, par, info)))
146 return err;
147
148 valkyrie_par_to_fix(par, &info->fix);
149
150
151 out_8(&valkyrie_regs->status.r, 0);
152 udelay(100);
153
154
155 init = par->init;
156 out_8(&valkyrie_regs->mode.r, init->mode | 0x80);
157 out_8(&valkyrie_regs->depth.r, par->cmode + 3);
158 set_valkyrie_clock(init->clock_params);
159 udelay(100);
160
161
162 out_8(&valkyrie_regs->mode.r, init->mode);
163
164 return 0;
165}
166
167static inline int valkyrie_par_to_var(struct fb_par_valkyrie *par,
168 struct fb_var_screeninfo *var)
169{
170 return mac_vmode_to_var(par->vmode, par->cmode, var);
171}
172
173static int
174valkyriefb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
175{
176 int err;
177 struct fb_par_valkyrie par;
178
179 if ((err = valkyrie_var_to_par(var, &par, info)))
180 return err;
181 valkyrie_par_to_var(&par, var);
182 return 0;
183}
184
185
186
187
188
189
190
191
192
193
194
195static int valkyriefb_blank(int blank_mode, struct fb_info *info)
196{
197 struct fb_info_valkyrie *p = (struct fb_info_valkyrie *) info;
198 struct fb_par_valkyrie *par = info->par;
199 struct valkyrie_regvals *init = par->init;
200
201 if (init == NULL)
202 return 1;
203
204 switch (blank_mode) {
205 case FB_BLANK_UNBLANK:
206 out_8(&p->valkyrie_regs->mode.r, init->mode);
207 break;
208 case FB_BLANK_NORMAL:
209 return 1;
210 case FB_BLANK_VSYNC_SUSPEND:
211 case FB_BLANK_HSYNC_SUSPEND:
212
213
214
215
216
217 out_8(&p->valkyrie_regs->mode.r, init->mode | 0x40);
218 break;
219 case FB_BLANK_POWERDOWN:
220 out_8(&p->valkyrie_regs->mode.r, 0x66);
221 break;
222 }
223 return 0;
224}
225
226static int valkyriefb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
227 u_int transp, struct fb_info *info)
228{
229 struct fb_info_valkyrie *p = (struct fb_info_valkyrie *) info;
230 volatile struct cmap_regs __iomem *cmap_regs = p->cmap_regs;
231 struct fb_par_valkyrie *par = info->par;
232
233 if (regno > 255)
234 return 1;
235 red >>= 8;
236 green >>= 8;
237 blue >>= 8;
238
239
240 out_8(&p->cmap_regs->addr, regno);
241 udelay(1);
242
243 out_8(&cmap_regs->lut, red);
244 out_8(&cmap_regs->lut, green);
245 out_8(&cmap_regs->lut, blue);
246
247 if (regno < 16 && par->cmode == CMODE_16)
248 ((u32 *)info->pseudo_palette)[regno] =
249 (regno << 10) | (regno << 5) | regno;
250
251 return 0;
252}
253
254static inline int valkyrie_vram_reqd(int video_mode, int color_mode)
255{
256 int pitch;
257 struct valkyrie_regvals *init = valkyrie_reg_init[video_mode-1];
258
259 if ((pitch = init->pitch[color_mode]) == 0)
260 pitch = 2 * init->pitch[0];
261 return init->vres * pitch;
262}
263
264static void set_valkyrie_clock(unsigned char *params)
265{
266 struct adb_request req;
267 int i;
268
269#ifdef CONFIG_ADB_CUDA
270 for (i = 0; i < 3; ++i) {
271 cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_GET_SET_IIC,
272 0x50, i + 1, params[i]);
273 while (!req.complete)
274 cuda_poll();
275 }
276#endif
277}
278
279static void __init valkyrie_choose_mode(struct fb_info_valkyrie *p)
280{
281 p->sense = read_valkyrie_sense(p);
282 printk(KERN_INFO "Monitor sense value = 0x%x\n", p->sense);
283
284
285#if !defined(CONFIG_MAC) && defined(CONFIG_NVRAM)
286 if (default_vmode == VMODE_NVRAM) {
287 default_vmode = nvram_read_byte(NV_VMODE);
288 if (default_vmode <= 0
289 || default_vmode > VMODE_MAX
290 || !valkyrie_reg_init[default_vmode - 1])
291 default_vmode = VMODE_CHOOSE;
292 }
293#endif
294 if (default_vmode == VMODE_CHOOSE)
295 default_vmode = mac_map_monitor_sense(p->sense);
296 if (!valkyrie_reg_init[default_vmode - 1])
297 default_vmode = VMODE_640_480_67;
298#if !defined(CONFIG_MAC) && defined(CONFIG_NVRAM)
299 if (default_cmode == CMODE_NVRAM)
300 default_cmode = nvram_read_byte(NV_CMODE);
301#endif
302
303
304
305
306 if (default_cmode < CMODE_8 || default_cmode > CMODE_16
307 || valkyrie_reg_init[default_vmode-1]->pitch[default_cmode] == 0
308 || valkyrie_vram_reqd(default_vmode, default_cmode) > p->total_vram)
309 default_cmode = CMODE_8;
310
311 printk(KERN_INFO "using video mode %d and color mode %d.\n",
312 default_vmode, default_cmode);
313}
314
315int __init valkyriefb_init(void)
316{
317 struct fb_info_valkyrie *p;
318 unsigned long frame_buffer_phys, cmap_regs_phys, flags;
319 int err;
320 char *option = NULL;
321
322 if (fb_get_options("valkyriefb", &option))
323 return -ENODEV;
324 valkyriefb_setup(option);
325
326#ifdef CONFIG_MAC
327 if (!MACH_IS_MAC)
328 return -ENODEV;
329 if (!(mac_bi_data.id == MAC_MODEL_Q630
330
331 || mac_bi_data.id == MAC_MODEL_P588))
332 return -ENODEV;
333
334
335 frame_buffer_phys = 0xf9000000;
336 cmap_regs_phys = 0x50f24000;
337 flags = IOMAP_NOCACHE_SER;
338#else
339 {
340 struct device_node *dp;
341 struct resource r;
342
343 dp = of_find_node_by_name(NULL, "valkyrie");
344 if (dp == 0)
345 return 0;
346
347 if (of_address_to_resource(dp, 0, &r)) {
348 printk(KERN_ERR "can't find address for valkyrie\n");
349 return 0;
350 }
351
352 frame_buffer_phys = r.start;
353 cmap_regs_phys = r.start + 0x304000;
354 flags = _PAGE_WRITETHRU;
355 }
356#endif
357
358 p = kzalloc(sizeof(*p), GFP_ATOMIC);
359 if (p == 0)
360 return -ENOMEM;
361
362
363 if (!request_mem_region(frame_buffer_phys, 0x100000, "valkyriefb")) {
364 kfree(p);
365 return 0;
366 }
367 p->total_vram = 0x100000;
368 p->frame_buffer_phys = frame_buffer_phys;
369 p->frame_buffer = __ioremap(frame_buffer_phys, p->total_vram, flags);
370 p->cmap_regs_phys = cmap_regs_phys;
371 p->cmap_regs = ioremap(p->cmap_regs_phys, 0x1000);
372 p->valkyrie_regs_phys = cmap_regs_phys+0x6000;
373 p->valkyrie_regs = ioremap(p->valkyrie_regs_phys, 0x1000);
374 err = -ENOMEM;
375 if (p->frame_buffer == NULL || p->cmap_regs == NULL
376 || p->valkyrie_regs == NULL) {
377 printk(KERN_ERR "valkyriefb: couldn't map resources\n");
378 goto out_free;
379 }
380
381 valkyrie_choose_mode(p);
382 mac_vmode_to_var(default_vmode, default_cmode, &p->info.var);
383 err = valkyrie_init_info(&p->info, p);
384 if (err < 0)
385 goto out_free;
386 valkyrie_init_fix(&p->info.fix, p);
387 if (valkyriefb_set_par(&p->info))
388
389 printk(KERN_ERR "valkyriefb: can't set default video mode\n");
390
391 if ((err = register_framebuffer(&p->info)) != 0)
392 goto out_cmap_free;
393
394 fb_info(&p->info, "valkyrie frame buffer device\n");
395 return 0;
396
397 out_cmap_free:
398 fb_dealloc_cmap(&p->info.cmap);
399 out_free:
400 if (p->frame_buffer)
401 iounmap(p->frame_buffer);
402 if (p->cmap_regs)
403 iounmap(p->cmap_regs);
404 if (p->valkyrie_regs)
405 iounmap(p->valkyrie_regs);
406 kfree(p);
407 return err;
408}
409
410
411
412
413static int read_valkyrie_sense(struct fb_info_valkyrie *p)
414{
415 int sense, in;
416
417 out_8(&p->valkyrie_regs->msense.r, 0);
418 __delay(20000);
419 sense = ((in = in_8(&p->valkyrie_regs->msense.r)) & 0x70) << 4;
420
421 out_8(&p->valkyrie_regs->msense.r, 4);
422 __delay(20000);
423 sense |= ((in = in_8(&p->valkyrie_regs->msense.r)) & 0x30);
424 out_8(&p->valkyrie_regs->msense.r, 2);
425 __delay(20000);
426 sense |= ((in = in_8(&p->valkyrie_regs->msense.r)) & 0x40) >> 3;
427 sense |= (in & 0x10) >> 2;
428 out_8(&p->valkyrie_regs->msense.r, 1);
429 __delay(20000);
430 sense |= ((in = in_8(&p->valkyrie_regs->msense.r)) & 0x60) >> 5;
431
432 out_8(&p->valkyrie_regs->msense.r, 7);
433
434 return sense;
435}
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463static int valkyrie_var_to_par(struct fb_var_screeninfo *var,
464 struct fb_par_valkyrie *par, const struct fb_info *fb_info)
465{
466 int vmode, cmode;
467 struct valkyrie_regvals *init;
468 struct fb_info_valkyrie *p = (struct fb_info_valkyrie *) fb_info;
469
470 if (mac_var_to_vmode(var, &vmode, &cmode) != 0) {
471 printk(KERN_ERR "valkyriefb: can't do %dx%dx%d.\n",
472 var->xres, var->yres, var->bits_per_pixel);
473 return -EINVAL;
474 }
475
476
477 if (vmode < 1 || vmode > VMODE_MAX || !valkyrie_reg_init[vmode-1]) {
478 printk(KERN_ERR "valkyriefb: vmode %d not valid.\n", vmode);
479 return -EINVAL;
480 }
481
482 if (cmode != CMODE_8 && cmode != CMODE_16) {
483 printk(KERN_ERR "valkyriefb: cmode %d not valid.\n", cmode);
484 return -EINVAL;
485 }
486
487 if (var->xres_virtual > var->xres || var->yres_virtual > var->yres
488 || var->xoffset != 0 || var->yoffset != 0) {
489 return -EINVAL;
490 }
491
492 init = valkyrie_reg_init[vmode-1];
493 if (init->pitch[cmode] == 0) {
494 printk(KERN_ERR "valkyriefb: vmode %d does not support "
495 "cmode %d.\n", vmode, cmode);
496 return -EINVAL;
497 }
498
499 if (valkyrie_vram_reqd(vmode, cmode) > p->total_vram) {
500 printk(KERN_ERR "valkyriefb: not enough ram for vmode %d, "
501 "cmode %d.\n", vmode, cmode);
502 return -EINVAL;
503 }
504
505 par->vmode = vmode;
506 par->cmode = cmode;
507 par->init = init;
508 par->xres = var->xres;
509 par->yres = var->yres;
510 par->vxres = par->xres;
511 par->vyres = par->yres;
512
513 return 0;
514}
515
516static void valkyrie_init_fix(struct fb_fix_screeninfo *fix, struct fb_info_valkyrie *p)
517{
518 memset(fix, 0, sizeof(*fix));
519 strcpy(fix->id, "valkyrie");
520 fix->mmio_start = p->valkyrie_regs_phys;
521 fix->mmio_len = sizeof(struct valkyrie_regs);
522 fix->type = FB_TYPE_PACKED_PIXELS;
523 fix->smem_start = p->frame_buffer_phys + 0x1000;
524 fix->smem_len = p->total_vram;
525
526 fix->type_aux = 0;
527 fix->ywrapstep = 0;
528 fix->ypanstep = 0;
529 fix->xpanstep = 0;
530
531}
532
533
534static void valkyrie_par_to_fix(struct fb_par_valkyrie *par,
535 struct fb_fix_screeninfo *fix)
536{
537 fix->smem_len = valkyrie_vram_reqd(par->vmode, par->cmode);
538 fix->visual = (par->cmode == CMODE_8) ?
539 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_DIRECTCOLOR;
540 fix->line_length = par->vxres << par->cmode;
541
542}
543
544static int __init valkyrie_init_info(struct fb_info *info,
545 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 info->par = &p->par;
552 return fb_alloc_cmap(&info->cmap, 256, 0);
553}
554
555
556
557
558
559int __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
588module_init(valkyriefb_init);
589MODULE_LICENSE("GPL");
590