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 =
140 container_of(info, 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;
145
146 if ((err = valkyrie_var_to_par(&info->var, par, info)))
147 return err;
148
149 valkyrie_par_to_fix(par, &info->fix);
150
151
152 out_8(&valkyrie_regs->status.r, 0);
153 udelay(100);
154
155
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);
161
162
163 out_8(&valkyrie_regs->mode.r, init->mode);
164
165 return 0;
166}
167
168static inline int valkyrie_par_to_var(struct fb_par_valkyrie *par,
169 struct fb_var_screeninfo *var)
170{
171 return mac_vmode_to_var(par->vmode, par->cmode, var);
172}
173
174static int
175valkyriefb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
176{
177 int err;
178 struct fb_par_valkyrie par;
179
180 if ((err = valkyrie_var_to_par(var, &par, info)))
181 return err;
182 valkyrie_par_to_var(&par, var);
183 return 0;
184}
185
186
187
188
189
190
191
192
193
194
195
196static int valkyriefb_blank(int blank_mode, struct fb_info *info)
197{
198 struct fb_info_valkyrie *p =
199 container_of(info, 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:
208 out_8(&p->valkyrie_regs->mode.r, init->mode);
209 break;
210 case FB_BLANK_NORMAL:
211 return 1;
212 case FB_BLANK_VSYNC_SUSPEND:
213 case FB_BLANK_HSYNC_SUSPEND:
214
215
216
217
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
228static 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 =
232 container_of(info, struct fb_info_valkyrie, info);
233 volatile struct cmap_regs __iomem *cmap_regs = p->cmap_regs;
234 struct fb_par_valkyrie *par = info->par;
235
236 if (regno > 255)
237 return 1;
238 red >>= 8;
239 green >>= 8;
240 blue >>= 8;
241
242
243 out_8(&p->cmap_regs->addr, regno);
244 udelay(1);
245
246 out_8(&cmap_regs->lut, red);
247 out_8(&cmap_regs->lut, green);
248 out_8(&cmap_regs->lut, blue);
249
250 if (regno < 16 && par->cmode == CMODE_16)
251 ((u32 *)info->pseudo_palette)[regno] =
252 (regno << 10) | (regno << 5) | regno;
253
254 return 0;
255}
256
257static inline int valkyrie_vram_reqd(int video_mode, int color_mode)
258{
259 int pitch;
260 struct valkyrie_regvals *init = valkyrie_reg_init[video_mode-1];
261
262 if ((pitch = init->pitch[color_mode]) == 0)
263 pitch = 2 * init->pitch[0];
264 return init->vres * pitch;
265}
266
267static void set_valkyrie_clock(unsigned char *params)
268{
269#ifdef CONFIG_ADB_CUDA
270 struct adb_request req;
271 int i;
272
273 for (i = 0; i < 3; ++i) {
274 cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_GET_SET_IIC,
275 0x50, i + 1, params[i]);
276 while (!req.complete)
277 cuda_poll();
278 }
279#endif
280}
281
282static void __init valkyrie_choose_mode(struct fb_info_valkyrie *p)
283{
284 p->sense = read_valkyrie_sense(p);
285 printk(KERN_INFO "Monitor sense value = 0x%x\n", p->sense);
286
287
288#if !defined(CONFIG_MAC) && defined(CONFIG_NVRAM)
289 if (default_vmode == VMODE_NVRAM) {
290 default_vmode = nvram_read_byte(NV_VMODE);
291 if (default_vmode <= 0
292 || default_vmode > VMODE_MAX
293 || !valkyrie_reg_init[default_vmode - 1])
294 default_vmode = VMODE_CHOOSE;
295 }
296#endif
297 if (default_vmode == VMODE_CHOOSE)
298 default_vmode = mac_map_monitor_sense(p->sense);
299 if (!valkyrie_reg_init[default_vmode - 1])
300 default_vmode = VMODE_640_480_67;
301#if !defined(CONFIG_MAC) && defined(CONFIG_NVRAM)
302 if (default_cmode == CMODE_NVRAM)
303 default_cmode = nvram_read_byte(NV_CMODE);
304#endif
305
306
307
308
309 if (default_cmode < CMODE_8 || default_cmode > CMODE_16
310 || valkyrie_reg_init[default_vmode-1]->pitch[default_cmode] == 0
311 || valkyrie_vram_reqd(default_vmode, default_cmode) > p->total_vram)
312 default_cmode = CMODE_8;
313
314 printk(KERN_INFO "using video mode %d and color mode %d.\n",
315 default_vmode, default_cmode);
316}
317
318int __init valkyriefb_init(void)
319{
320 struct fb_info_valkyrie *p;
321 unsigned long frame_buffer_phys, cmap_regs_phys, flags;
322 int err;
323 char *option = NULL;
324
325 if (fb_get_options("valkyriefb", &option))
326 return -ENODEV;
327 valkyriefb_setup(option);
328
329#ifdef CONFIG_MAC
330 if (!MACH_IS_MAC)
331 return -ENODEV;
332 if (!(mac_bi_data.id == MAC_MODEL_Q630
333
334 || mac_bi_data.id == MAC_MODEL_P588))
335 return -ENODEV;
336
337
338 frame_buffer_phys = 0xf9000000;
339 cmap_regs_phys = 0x50f24000;
340 flags = IOMAP_NOCACHE_SER;
341#else
342 {
343 struct device_node *dp;
344 struct resource r;
345
346 dp = of_find_node_by_name(NULL, "valkyrie");
347 if (dp == 0)
348 return 0;
349
350 if (of_address_to_resource(dp, 0, &r)) {
351 printk(KERN_ERR "can't find address for valkyrie\n");
352 return 0;
353 }
354
355 frame_buffer_phys = r.start;
356 cmap_regs_phys = r.start + 0x304000;
357 flags = _PAGE_WRITETHRU;
358 }
359#endif
360
361 p = kzalloc(sizeof(*p), GFP_ATOMIC);
362 if (p == 0)
363 return -ENOMEM;
364
365
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 err = valkyrie_init_info(&p->info, p);
387 if (err < 0)
388 goto out_free;
389 valkyrie_init_fix(&p->info.fix, p);
390 if (valkyriefb_set_par(&p->info))
391
392 printk(KERN_ERR "valkyriefb: can't set default video mode\n");
393
394 if ((err = register_framebuffer(&p->info)) != 0)
395 goto out_cmap_free;
396
397 fb_info(&p->info, "valkyrie frame buffer device\n");
398 return 0;
399
400 out_cmap_free:
401 fb_dealloc_cmap(&p->info.cmap);
402 out_free:
403 if (p->frame_buffer)
404 iounmap(p->frame_buffer);
405 if (p->cmap_regs)
406 iounmap(p->cmap_regs);
407 if (p->valkyrie_regs)
408 iounmap(p->valkyrie_regs);
409 kfree(p);
410 return err;
411}
412
413
414
415
416static int read_valkyrie_sense(struct fb_info_valkyrie *p)
417{
418 int sense, in;
419
420 out_8(&p->valkyrie_regs->msense.r, 0);
421 __delay(20000);
422 sense = ((in = in_8(&p->valkyrie_regs->msense.r)) & 0x70) << 4;
423
424 out_8(&p->valkyrie_regs->msense.r, 4);
425 __delay(20000);
426 sense |= ((in = in_8(&p->valkyrie_regs->msense.r)) & 0x30);
427 out_8(&p->valkyrie_regs->msense.r, 2);
428 __delay(20000);
429 sense |= ((in = in_8(&p->valkyrie_regs->msense.r)) & 0x40) >> 3;
430 sense |= (in & 0x10) >> 2;
431 out_8(&p->valkyrie_regs->msense.r, 1);
432 __delay(20000);
433 sense |= ((in = in_8(&p->valkyrie_regs->msense.r)) & 0x60) >> 5;
434
435 out_8(&p->valkyrie_regs->msense.r, 7);
436
437 return sense;
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
463
464
465
466static int valkyrie_var_to_par(struct fb_var_screeninfo *var,
467 struct fb_par_valkyrie *par, const struct fb_info *fb_info)
468{
469 int vmode, cmode;
470 struct valkyrie_regvals *init;
471 struct fb_info_valkyrie *p =
472 container_of(fb_info, struct fb_info_valkyrie, info);
473
474 if (mac_var_to_vmode(var, &vmode, &cmode) != 0) {
475 printk(KERN_ERR "valkyriefb: can't do %dx%dx%d.\n",
476 var->xres, var->yres, var->bits_per_pixel);
477 return -EINVAL;
478 }
479
480
481 if (vmode < 1 || vmode > VMODE_MAX || !valkyrie_reg_init[vmode-1]) {
482 printk(KERN_ERR "valkyriefb: vmode %d not valid.\n", vmode);
483 return -EINVAL;
484 }
485
486 if (cmode != CMODE_8 && cmode != CMODE_16) {
487 printk(KERN_ERR "valkyriefb: cmode %d not valid.\n", cmode);
488 return -EINVAL;
489 }
490
491 if (var->xres_virtual > var->xres || var->yres_virtual > var->yres
492 || var->xoffset != 0 || var->yoffset != 0) {
493 return -EINVAL;
494 }
495
496 init = valkyrie_reg_init[vmode-1];
497 if (init->pitch[cmode] == 0) {
498 printk(KERN_ERR "valkyriefb: vmode %d does not support "
499 "cmode %d.\n", vmode, cmode);
500 return -EINVAL;
501 }
502
503 if (valkyrie_vram_reqd(vmode, cmode) > p->total_vram) {
504 printk(KERN_ERR "valkyriefb: not enough ram for vmode %d, "
505 "cmode %d.\n", vmode, cmode);
506 return -EINVAL;
507 }
508
509 par->vmode = vmode;
510 par->cmode = cmode;
511 par->init = init;
512 par->xres = var->xres;
513 par->yres = var->yres;
514 par->vxres = par->xres;
515 par->vyres = par->yres;
516
517 return 0;
518}
519
520static void valkyrie_init_fix(struct fb_fix_screeninfo *fix, struct fb_info_valkyrie *p)
521{
522 memset(fix, 0, sizeof(*fix));
523 strcpy(fix->id, "valkyrie");
524 fix->mmio_start = p->valkyrie_regs_phys;
525 fix->mmio_len = sizeof(struct valkyrie_regs);
526 fix->type = FB_TYPE_PACKED_PIXELS;
527 fix->smem_start = p->frame_buffer_phys + 0x1000;
528 fix->smem_len = p->total_vram;
529
530 fix->type_aux = 0;
531 fix->ywrapstep = 0;
532 fix->ypanstep = 0;
533 fix->xpanstep = 0;
534
535}
536
537
538static void valkyrie_par_to_fix(struct fb_par_valkyrie *par,
539 struct fb_fix_screeninfo *fix)
540{
541 fix->smem_len = valkyrie_vram_reqd(par->vmode, par->cmode);
542 fix->visual = (par->cmode == CMODE_8) ?
543 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_DIRECTCOLOR;
544 fix->line_length = par->vxres << par->cmode;
545
546}
547
548static int __init valkyrie_init_info(struct fb_info *info,
549 struct fb_info_valkyrie *p)
550{
551 info->fbops = &valkyriefb_ops;
552 info->screen_base = p->frame_buffer + 0x1000;
553 info->flags = FBINFO_DEFAULT;
554 info->pseudo_palette = p->pseudo_palette;
555 info->par = &p->par;
556 return fb_alloc_cmap(&info->cmap, 256, 0);
557}
558
559
560
561
562
563int __init valkyriefb_setup(char *options)
564{
565 char *this_opt;
566
567 if (!options || !*options)
568 return 0;
569
570 while ((this_opt = strsep(&options, ",")) != NULL) {
571 if (!strncmp(this_opt, "vmode:", 6)) {
572 int vmode = simple_strtoul(this_opt+6, NULL, 0);
573 if (vmode > 0 && vmode <= VMODE_MAX)
574 default_vmode = vmode;
575 }
576 else if (!strncmp(this_opt, "cmode:", 6)) {
577 int depth = simple_strtoul(this_opt+6, NULL, 0);
578 switch (depth) {
579 case 8:
580 default_cmode = CMODE_8;
581 break;
582 case 15:
583 case 16:
584 default_cmode = CMODE_16;
585 break;
586 }
587 }
588 }
589 return 0;
590}
591
592module_init(valkyriefb_init);
593MODULE_LICENSE("GPL");
594