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