blob: 376d4a171ec7bd4fb4d0ca6698d6fdb6522cfa72 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* leo.c: LEO frame buffer driver
2 *
3 * Copyright (C) 2003 David S. Miller (davem@redhat.com)
4 * Copyright (C) 1996-1999 Jakub Jelinek (jj@ultra.linux.cz)
5 * Copyright (C) 1997 Michal Rehacek (Michal.Rehacek@st.mff.cuni.cz)
6 *
7 * Driver layout based loosely on tgafb.c, see that file for credits.
8 */
9
10#include <linux/module.h>
11#include <linux/kernel.h>
12#include <linux/errno.h>
13#include <linux/string.h>
14#include <linux/slab.h>
15#include <linux/delay.h>
16#include <linux/init.h>
17#include <linux/fb.h>
18#include <linux/mm.h>
19
20#include <asm/io.h>
21#include <asm/sbus.h>
22#include <asm/oplib.h>
23#include <asm/fbio.h>
24
25#include "sbuslib.h"
26
27/*
28 * Local functions.
29 */
30
31static int leo_setcolreg(unsigned, unsigned, unsigned, unsigned,
32 unsigned, struct fb_info *);
33static int leo_blank(int, struct fb_info *);
34
35static int leo_mmap(struct fb_info *, struct file *, struct vm_area_struct *);
36static int leo_ioctl(struct inode *, struct file *, unsigned int,
37 unsigned long, struct fb_info *);
38static int leo_pan_display(struct fb_var_screeninfo *, struct fb_info *);
39
40/*
41 * Frame buffer operations
42 */
43
44static struct fb_ops leo_ops = {
45 .owner = THIS_MODULE,
46 .fb_setcolreg = leo_setcolreg,
47 .fb_blank = leo_blank,
48 .fb_pan_display = leo_pan_display,
49 .fb_fillrect = cfb_fillrect,
50 .fb_copyarea = cfb_copyarea,
51 .fb_imageblit = cfb_imageblit,
52 .fb_mmap = leo_mmap,
53 .fb_ioctl = leo_ioctl,
Christoph Hellwig9ffb83b2005-11-12 12:11:12 -080054#ifdef CONFIG_COMPAT
55 .fb_compat_ioctl = sbusfb_compat_ioctl,
56#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070057};
58
59#define LEO_OFF_LC_SS0_KRN 0x00200000UL
60#define LEO_OFF_LC_SS0_USR 0x00201000UL
61#define LEO_OFF_LC_SS1_KRN 0x01200000UL
62#define LEO_OFF_LC_SS1_USR 0x01201000UL
63#define LEO_OFF_LD_SS0 0x00400000UL
64#define LEO_OFF_LD_SS1 0x01400000UL
65#define LEO_OFF_LD_GBL 0x00401000UL
66#define LEO_OFF_LX_KRN 0x00600000UL
67#define LEO_OFF_LX_CURSOR 0x00601000UL
68#define LEO_OFF_SS0 0x00800000UL
69#define LEO_OFF_SS1 0x01800000UL
70#define LEO_OFF_UNK 0x00602000UL
71#define LEO_OFF_UNK2 0x00000000UL
72
73#define LEO_CUR_ENABLE 0x00000080
74#define LEO_CUR_UPDATE 0x00000030
75#define LEO_CUR_PROGRESS 0x00000006
76#define LEO_CUR_UPDATECMAP 0x00000003
77
78#define LEO_CUR_TYPE_MASK 0x00000000
79#define LEO_CUR_TYPE_IMAGE 0x00000020
80#define LEO_CUR_TYPE_CMAP 0x00000050
81
82struct leo_cursor {
83 u8 xxx0[16];
84 volatile u32 cur_type;
85 volatile u32 cur_misc;
86 volatile u32 cur_cursxy;
87 volatile u32 cur_data;
88};
89
90#define LEO_KRN_TYPE_CLUT0 0x00001000
91#define LEO_KRN_TYPE_CLUT1 0x00001001
92#define LEO_KRN_TYPE_CLUT2 0x00001002
93#define LEO_KRN_TYPE_WID 0x00001003
94#define LEO_KRN_TYPE_UNK 0x00001006
95#define LEO_KRN_TYPE_VIDEO 0x00002003
96#define LEO_KRN_TYPE_CLUTDATA 0x00004000
97#define LEO_KRN_CSR_ENABLE 0x00000008
98#define LEO_KRN_CSR_PROGRESS 0x00000004
99#define LEO_KRN_CSR_UNK 0x00000002
100#define LEO_KRN_CSR_UNK2 0x00000001
101
102struct leo_lx_krn {
103 volatile u32 krn_type;
104 volatile u32 krn_csr;
105 volatile u32 krn_value;
106};
107
108struct leo_lc_ss0_krn {
109 volatile u32 misc;
110 u8 xxx0[0x800-4];
111 volatile u32 rev;
112};
113
114struct leo_lc_ss0_usr {
115 volatile u32 csr;
116 volatile u32 addrspace;
117 volatile u32 fontmsk;
118 volatile u32 fontt;
119 volatile u32 extent;
120 volatile u32 src;
121 u32 dst;
122 volatile u32 copy;
123 volatile u32 fill;
124};
125
126struct leo_lc_ss1_krn {
127 u8 unknown;
128};
129
130struct leo_lc_ss1_usr {
131 u8 unknown;
132};
133
134struct leo_ld {
135 u8 xxx0[0xe00];
136 volatile u32 csr;
137 volatile u32 wid;
138 volatile u32 wmask;
139 volatile u32 widclip;
140 volatile u32 vclipmin;
141 volatile u32 vclipmax;
142 volatile u32 pickmin; /* SS1 only */
143 volatile u32 pickmax; /* SS1 only */
144 volatile u32 fg;
145 volatile u32 bg;
146 volatile u32 src; /* Copy/Scroll (SS0 only) */
147 volatile u32 dst; /* Copy/Scroll/Fill (SS0 only) */
148 volatile u32 extent; /* Copy/Scroll/Fill size (SS0 only) */
149 u32 xxx1[3];
150 volatile u32 setsem; /* SS1 only */
151 volatile u32 clrsem; /* SS1 only */
152 volatile u32 clrpick; /* SS1 only */
153 volatile u32 clrdat; /* SS1 only */
154 volatile u32 alpha; /* SS1 only */
155 u8 xxx2[0x2c];
156 volatile u32 winbg;
157 volatile u32 planemask;
158 volatile u32 rop;
159 volatile u32 z;
160 volatile u32 dczf; /* SS1 only */
161 volatile u32 dczb; /* SS1 only */
162 volatile u32 dcs; /* SS1 only */
163 volatile u32 dczs; /* SS1 only */
164 volatile u32 pickfb; /* SS1 only */
165 volatile u32 pickbb; /* SS1 only */
166 volatile u32 dcfc; /* SS1 only */
167 volatile u32 forcecol; /* SS1 only */
168 volatile u32 door[8]; /* SS1 only */
169 volatile u32 pick[5]; /* SS1 only */
170};
171
172#define LEO_SS1_MISC_ENABLE 0x00000001
173#define LEO_SS1_MISC_STEREO 0x00000002
174struct leo_ld_ss1 {
175 u8 xxx0[0xef4];
176 volatile u32 ss1_misc;
177};
178
179struct leo_ld_gbl {
180 u8 unknown;
181};
182
183struct leo_par {
184 spinlock_t lock;
185 struct leo_lx_krn __iomem *lx_krn;
186 struct leo_lc_ss0_usr __iomem *lc_ss0_usr;
187 struct leo_ld_ss0 __iomem *ld_ss0;
188 struct leo_ld_ss1 __iomem *ld_ss1;
189 struct leo_cursor __iomem *cursor;
190 u32 extent;
191 u32 clut_data[256];
192
193 u32 flags;
194#define LEO_FLAG_BLANKED 0x00000001
195
196 unsigned long physbase;
197 unsigned long fbsize;
198
199 struct sbus_dev *sdev;
200 struct list_head list;
201};
202
203static void leo_wait(struct leo_lx_krn __iomem *lx_krn)
204{
205 int i;
206
207 for (i = 0;
208 (sbus_readl(&lx_krn->krn_csr) & LEO_KRN_CSR_PROGRESS) && i < 300000;
209 i++)
210 udelay (1); /* Busy wait at most 0.3 sec */
211 return;
212}
213
214/**
215 * leo_setcolreg - Optional function. Sets a color register.
216 * @regno: boolean, 0 copy local, 1 get_user() function
217 * @red: frame buffer colormap structure
218 * @green: The green value which can be up to 16 bits wide
219 * @blue: The blue value which can be up to 16 bits wide.
220 * @transp: If supported the alpha value which can be up to 16 bits wide.
221 * @info: frame buffer info structure
222 */
223static int leo_setcolreg(unsigned regno,
224 unsigned red, unsigned green, unsigned blue,
225 unsigned transp, struct fb_info *info)
226{
227 struct leo_par *par = (struct leo_par *) info->par;
228 struct leo_lx_krn __iomem *lx_krn = par->lx_krn;
229 unsigned long flags;
230 u32 val;
231 int i;
232
233 if (regno >= 256)
234 return 1;
235
236 red >>= 8;
237 green >>= 8;
238 blue >>= 8;
239
240 par->clut_data[regno] = red | (green << 8) | (blue << 16);
241
242 spin_lock_irqsave(&par->lock, flags);
243
244 leo_wait(lx_krn);
245
246 sbus_writel(LEO_KRN_TYPE_CLUTDATA, &lx_krn->krn_type);
247 for (i = 0; i < 256; i++)
248 sbus_writel(par->clut_data[i], &lx_krn->krn_value);
249 sbus_writel(LEO_KRN_TYPE_CLUT0, &lx_krn->krn_type);
250
251 val = sbus_readl(&lx_krn->krn_csr);
252 val |= (LEO_KRN_CSR_UNK | LEO_KRN_CSR_UNK2);
253 sbus_writel(val, &lx_krn->krn_csr);
254
255 spin_unlock_irqrestore(&par->lock, flags);
256
257 return 0;
258}
259
260/**
261 * leo_blank - Optional function. Blanks the display.
262 * @blank_mode: the blank mode we want.
263 * @info: frame buffer structure that represents a single frame buffer
264 */
265static int leo_blank(int blank, struct fb_info *info)
266{
267 struct leo_par *par = (struct leo_par *) info->par;
268 struct leo_lx_krn __iomem *lx_krn = par->lx_krn;
269 unsigned long flags;
270 u32 val;
271
272 spin_lock_irqsave(&par->lock, flags);
273
274 switch (blank) {
275 case FB_BLANK_UNBLANK: /* Unblanking */
276 val = sbus_readl(&lx_krn->krn_csr);
277 val |= LEO_KRN_CSR_ENABLE;
278 sbus_writel(val, &lx_krn->krn_csr);
279 par->flags &= ~LEO_FLAG_BLANKED;
280 break;
281
282 case FB_BLANK_NORMAL: /* Normal blanking */
283 case FB_BLANK_VSYNC_SUSPEND: /* VESA blank (vsync off) */
284 case FB_BLANK_HSYNC_SUSPEND: /* VESA blank (hsync off) */
285 case FB_BLANK_POWERDOWN: /* Poweroff */
286 val = sbus_readl(&lx_krn->krn_csr);
287 val &= ~LEO_KRN_CSR_ENABLE;
288 sbus_writel(val, &lx_krn->krn_csr);
289 par->flags |= LEO_FLAG_BLANKED;
290 break;
291 }
292
293 spin_unlock_irqrestore(&par->lock, flags);
294
295 return 0;
296}
297
298static struct sbus_mmap_map leo_mmap_map[] = {
299 {
300 .voff = LEO_SS0_MAP,
301 .poff = LEO_OFF_SS0,
302 .size = 0x800000
303 },
304 {
305 .voff = LEO_LC_SS0_USR_MAP,
306 .poff = LEO_OFF_LC_SS0_USR,
307 .size = 0x1000
308 },
309 {
310 .voff = LEO_LD_SS0_MAP,
311 .poff = LEO_OFF_LD_SS0,
312 .size = 0x1000
313 },
314 {
315 .voff = LEO_LX_CURSOR_MAP,
316 .poff = LEO_OFF_LX_CURSOR,
317 .size = 0x1000
318 },
319 {
320 .voff = LEO_SS1_MAP,
321 .poff = LEO_OFF_SS1,
322 .size = 0x800000
323 },
324 {
325 .voff = LEO_LC_SS1_USR_MAP,
326 .poff = LEO_OFF_LC_SS1_USR,
327 .size = 0x1000
328 },
329 {
330 .voff = LEO_LD_SS1_MAP,
331 .poff = LEO_OFF_LD_SS1,
332 .size = 0x1000
333 },
334 {
335 .voff = LEO_UNK_MAP,
336 .poff = LEO_OFF_UNK,
337 .size = 0x1000
338 },
339 {
340 .voff = LEO_LX_KRN_MAP,
341 .poff = LEO_OFF_LX_KRN,
342 .size = 0x1000
343 },
344 {
345 .voff = LEO_LC_SS0_KRN_MAP,
346 .poff = LEO_OFF_LC_SS0_KRN,
347 .size = 0x1000
348 },
349 {
350 .voff = LEO_LC_SS1_KRN_MAP,
351 .poff = LEO_OFF_LC_SS1_KRN,
352 .size = 0x1000
353 },
354 {
355 .voff = LEO_LD_GBL_MAP,
356 .poff = LEO_OFF_LD_GBL,
357 .size = 0x1000
358 },
359 {
360 .voff = LEO_UNK2_MAP,
361 .poff = LEO_OFF_UNK2,
362 .size = 0x100000
363 },
364 { .size = 0 }
365};
366
367static int leo_mmap(struct fb_info *info, struct file *file, struct vm_area_struct *vma)
368{
369 struct leo_par *par = (struct leo_par *)info->par;
370
371 return sbusfb_mmap_helper(leo_mmap_map,
372 par->physbase, par->fbsize,
373 par->sdev->reg_addrs[0].which_io,
374 vma);
375}
376
377static int leo_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
378 unsigned long arg, struct fb_info *info)
379{
380 struct leo_par *par = (struct leo_par *) info->par;
381
382 return sbusfb_ioctl_helper(cmd, arg, info,
383 FBTYPE_SUNLEO, 32, par->fbsize);
384}
385
386/*
387 * Initialisation
388 */
389
390static void
391leo_init_fix(struct fb_info *info)
392{
393 struct leo_par *par = (struct leo_par *)info->par;
394
395 strlcpy(info->fix.id, par->sdev->prom_name, sizeof(info->fix.id));
396
397 info->fix.type = FB_TYPE_PACKED_PIXELS;
398 info->fix.visual = FB_VISUAL_TRUECOLOR;
399
400 info->fix.line_length = 8192;
401
402 info->fix.accel = FB_ACCEL_SUN_LEO;
403}
404
405static void leo_wid_put(struct fb_info *info, struct fb_wid_list *wl)
406{
407 struct leo_par *par = (struct leo_par *) info->par;
408 struct leo_lx_krn __iomem *lx_krn = par->lx_krn;
409 struct fb_wid_item *wi;
410 unsigned long flags;
411 u32 val;
412 int i, j;
413
414 spin_lock_irqsave(&par->lock, flags);
415
416 leo_wait(lx_krn);
417
418 for (i = 0, wi = wl->wl_list; i < wl->wl_count; i++, wi++) {
419 switch(wi->wi_type) {
420 case FB_WID_DBL_8:
421 j = (wi->wi_index & 0xf) + 0x40;
422 break;
423
424 case FB_WID_DBL_24:
425 j = wi->wi_index & 0x3f;
426 break;
427
428 default:
429 continue;
430 };
431 sbus_writel(0x5800 + j, &lx_krn->krn_type);
432 sbus_writel(wi->wi_values[0], &lx_krn->krn_value);
433 }
434 sbus_writel(LEO_KRN_TYPE_WID, &lx_krn->krn_type);
435
436 val = sbus_readl(&lx_krn->krn_csr);
437 val |= (LEO_KRN_CSR_UNK | LEO_KRN_CSR_UNK2);
438 sbus_writel(val, &lx_krn->krn_csr);
439
440 spin_unlock_irqrestore(&par->lock, flags);
441}
442
443static void leo_init_wids(struct fb_info *info)
444{
445 struct fb_wid_item wi;
446 struct fb_wid_list wl;
447
448 wl.wl_count = 1;
449 wl.wl_list = &wi;
450 wi.wi_type = FB_WID_DBL_8;
451 wi.wi_index = 0;
452 wi.wi_values [0] = 0x2c0;
453 leo_wid_put(info, &wl);
454 wi.wi_index = 1;
455 wi.wi_values [0] = 0x30;
456 leo_wid_put(info, &wl);
457 wi.wi_index = 2;
458 wi.wi_values [0] = 0x20;
459 leo_wid_put(info, &wl);
460 wi.wi_type = FB_WID_DBL_24;
461 wi.wi_index = 1;
462 wi.wi_values [0] = 0x30;
463 leo_wid_put(info, &wl);
464
465}
466
467static void leo_switch_from_graph(struct fb_info *info)
468{
469 struct leo_par *par = (struct leo_par *) info->par;
470 struct leo_ld __iomem *ss = (struct leo_ld __iomem *) par->ld_ss0;
471 unsigned long flags;
472 u32 val;
473
474 spin_lock_irqsave(&par->lock, flags);
475
476 par->extent = ((info->var.xres - 1) |
477 ((info->var.yres - 1) << 16));
478
479 sbus_writel(0xffffffff, &ss->wid);
480 sbus_writel(0xffff, &ss->wmask);
481 sbus_writel(0, &ss->vclipmin);
482 sbus_writel(par->extent, &ss->vclipmax);
483 sbus_writel(0, &ss->fg);
484 sbus_writel(0xff000000, &ss->planemask);
485 sbus_writel(0x310850, &ss->rop);
486 sbus_writel(0, &ss->widclip);
487 sbus_writel((info->var.xres-1) | ((info->var.yres-1) << 11),
488 &par->lc_ss0_usr->extent);
489 sbus_writel(4, &par->lc_ss0_usr->addrspace);
490 sbus_writel(0x80000000, &par->lc_ss0_usr->fill);
491 sbus_writel(0, &par->lc_ss0_usr->fontt);
492 do {
493 val = sbus_readl(&par->lc_ss0_usr->csr);
494 } while (val & 0x20000000);
495
496 spin_unlock_irqrestore(&par->lock, flags);
497}
498
499static int leo_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
500{
501 /* We just use this to catch switches out of
502 * graphics mode.
503 */
504 leo_switch_from_graph(info);
505
506 if (var->xoffset || var->yoffset || var->vmode)
507 return -EINVAL;
508 return 0;
509}
510
511static void leo_init_hw(struct fb_info *info)
512{
513 struct leo_par *par = (struct leo_par *) info->par;
514 u32 val;
515
516 val = sbus_readl(&par->ld_ss1->ss1_misc);
517 val |= LEO_SS1_MISC_ENABLE;
518 sbus_writel(val, &par->ld_ss1->ss1_misc);
519
520 leo_switch_from_graph(info);
521}
522
523static void leo_fixup_var_rgb(struct fb_var_screeninfo *var)
524{
525 var->red.offset = 0;
526 var->red.length = 8;
527 var->green.offset = 8;
528 var->green.length = 8;
529 var->blue.offset = 16;
530 var->blue.length = 8;
531 var->transp.offset = 0;
532 var->transp.length = 0;
533}
534
535struct all_info {
536 struct fb_info info;
537 struct leo_par par;
538 struct list_head list;
539};
540static LIST_HEAD(leo_list);
541
542static void leo_init_one(struct sbus_dev *sdev)
543{
544 struct all_info *all;
545 int linebytes;
546
547 all = kmalloc(sizeof(*all), GFP_KERNEL);
548 if (!all) {
549 printk(KERN_ERR "leo: Cannot allocate memory.\n");
550 return;
551 }
552 memset(all, 0, sizeof(*all));
553
554 INIT_LIST_HEAD(&all->list);
555
556 spin_lock_init(&all->par.lock);
557 all->par.sdev = sdev;
558
559 all->par.physbase = sdev->reg_addrs[0].phys_addr;
560
561 sbusfb_fill_var(&all->info.var, sdev->prom_node, 32);
562 leo_fixup_var_rgb(&all->info.var);
563
564 linebytes = prom_getintdefault(sdev->prom_node, "linebytes",
565 all->info.var.xres);
566 all->par.fbsize = PAGE_ALIGN(linebytes * all->info.var.yres);
567
568#ifdef CONFIG_SPARC32
569 all->info.screen_base = (char __iomem *)
570 prom_getintdefault(sdev->prom_node, "address", 0);
571#endif
572 if (!all->info.screen_base)
573 all->info.screen_base =
574 sbus_ioremap(&sdev->resource[0], LEO_OFF_SS0,
575 0x800000, "leo ram");
576
577 all->par.lc_ss0_usr =
578 sbus_ioremap(&sdev->resource[0], LEO_OFF_LC_SS0_USR,
579 0x1000, "leolc ss0usr");
580 all->par.ld_ss0 =
581 sbus_ioremap(&sdev->resource[0], LEO_OFF_LD_SS0,
582 0x1000, "leold ss0");
583 all->par.ld_ss1 =
584 sbus_ioremap(&sdev->resource[0], LEO_OFF_LD_SS1,
585 0x1000, "leold ss1");
586 all->par.lx_krn =
587 sbus_ioremap(&sdev->resource[0], LEO_OFF_LX_KRN,
588 0x1000, "leolx krn");
589 all->par.cursor =
590 sbus_ioremap(&sdev->resource[0], LEO_OFF_LX_CURSOR,
591 sizeof(struct leo_cursor), "leolx cursor");
592
593 all->info.flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
594 all->info.fbops = &leo_ops;
595 all->info.par = &all->par;
596
597 leo_init_wids(&all->info);
598 leo_init_hw(&all->info);
599
600 leo_blank(0, &all->info);
601
602 if (fb_alloc_cmap(&all->info.cmap, 256, 0)) {
603 printk(KERN_ERR "leo: Could not allocate color map.\n");
604 kfree(all);
605 return;
606 }
607
608 leo_init_fix(&all->info);
609
610 if (register_framebuffer(&all->info) < 0) {
611 printk(KERN_ERR "leo: Could not register framebuffer.\n");
612 fb_dealloc_cmap(&all->info.cmap);
613 kfree(all);
614 return;
615 }
616
617 list_add(&all->list, &leo_list);
618
619 printk("leo: %s at %lx:%lx\n",
620 sdev->prom_name,
621 (long) sdev->reg_addrs[0].which_io,
622 (long) sdev->reg_addrs[0].phys_addr);
623}
624
625int __init leo_init(void)
626{
627 struct sbus_bus *sbus;
628 struct sbus_dev *sdev;
629
630 if (fb_get_options("leofb", NULL))
631 return -ENODEV;
632
633 for_all_sbusdev(sdev, sbus) {
634 if (!strcmp(sdev->prom_name, "leo"))
635 leo_init_one(sdev);
636 }
637
638 return 0;
639}
640
641void __exit leo_exit(void)
642{
643 struct list_head *pos, *tmp;
644
645 list_for_each_safe(pos, tmp, &leo_list) {
646 struct all_info *all = list_entry(pos, typeof(*all), list);
647
648 unregister_framebuffer(&all->info);
649 fb_dealloc_cmap(&all->info.cmap);
650 kfree(all);
651 }
652}
653
654int __init
655leo_setup(char *arg)
656{
657 /* No cmdline options yet... */
658 return 0;
659}
660
661module_init(leo_init);
662#ifdef MODULE
663module_exit(leo_exit);
664#endif
665
666MODULE_DESCRIPTION("framebuffer driver for LEO chipsets");
667MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
668MODULE_LICENSE("GPL");