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