Coarsly sort out 32-bit-only, 64-bit-only and ``portable'' MIPS lib/
[linux-2.6/linux-mips.git] / drivers / video / leo.c
blob12917dab5046f3ffea430f272eb07f901a8d0280
1 /* leo.c: LEO frame buffer driver
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)
7 * Driver layout based loosely on tgafb.c, see that file for credits.
8 */
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>
20 #include <asm/io.h>
21 #include <asm/sbus.h>
22 #include <asm/oplib.h>
23 #include <asm/fbio.h>
25 #include "sbuslib.h"
28 * Local functions.
31 static int leo_setcolreg(unsigned, unsigned, unsigned, unsigned,
32 unsigned, struct fb_info *);
33 static int leo_blank(int, struct fb_info *);
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 *);
40 * Frame buffer operations
43 static struct fb_ops leo_ops = {
44 .owner = THIS_MODULE,
45 .fb_setcolreg = leo_setcolreg,
46 .fb_blank = leo_blank,
47 .fb_fillrect = cfb_fillrect,
48 .fb_copyarea = cfb_copyarea,
49 .fb_imageblit = cfb_imageblit,
50 .fb_mmap = leo_mmap,
51 .fb_ioctl = leo_ioctl,
52 .fb_cursor = soft_cursor,
55 #define LEO_OFF_LC_SS0_KRN 0x00200000UL
56 #define LEO_OFF_LC_SS0_USR 0x00201000UL
57 #define LEO_OFF_LC_SS1_KRN 0x01200000UL
58 #define LEO_OFF_LC_SS1_USR 0x01201000UL
59 #define LEO_OFF_LD_SS0 0x00400000UL
60 #define LEO_OFF_LD_SS1 0x01400000UL
61 #define LEO_OFF_LD_GBL 0x00401000UL
62 #define LEO_OFF_LX_KRN 0x00600000UL
63 #define LEO_OFF_LX_CURSOR 0x00601000UL
64 #define LEO_OFF_SS0 0x00800000UL
65 #define LEO_OFF_SS1 0x01800000UL
66 #define LEO_OFF_UNK 0x00602000UL
67 #define LEO_OFF_UNK2 0x00000000UL
69 #define LEO_CUR_ENABLE 0x00000080
70 #define LEO_CUR_UPDATE 0x00000030
71 #define LEO_CUR_PROGRESS 0x00000006
72 #define LEO_CUR_UPDATECMAP 0x00000003
74 #define LEO_CUR_TYPE_MASK 0x00000000
75 #define LEO_CUR_TYPE_IMAGE 0x00000020
76 #define LEO_CUR_TYPE_CMAP 0x00000050
78 struct leo_cursor {
79 u8 xxx0[16];
80 volatile u32 cur_type;
81 volatile u32 cur_misc;
82 volatile u32 cur_cursxy;
83 volatile u32 cur_data;
86 #define LEO_KRN_TYPE_CLUT0 0x00001000
87 #define LEO_KRN_TYPE_CLUT1 0x00001001
88 #define LEO_KRN_TYPE_CLUT2 0x00001002
89 #define LEO_KRN_TYPE_WID 0x00001003
90 #define LEO_KRN_TYPE_UNK 0x00001006
91 #define LEO_KRN_TYPE_VIDEO 0x00002003
92 #define LEO_KRN_TYPE_CLUTDATA 0x00004000
93 #define LEO_KRN_CSR_ENABLE 0x00000008
94 #define LEO_KRN_CSR_PROGRESS 0x00000004
95 #define LEO_KRN_CSR_UNK 0x00000002
96 #define LEO_KRN_CSR_UNK2 0x00000001
98 struct leo_lx_krn {
99 volatile u32 krn_type;
100 volatile u32 krn_csr;
101 volatile u32 krn_value;
104 struct leo_lc_ss0_krn {
105 volatile u32 misc;
106 u8 xxx0[0x800-4];
107 volatile u32 rev;
110 struct leo_lc_ss0_usr {
111 volatile u32 csr;
112 volatile u32 addrspace;
113 volatile u32 fontmsk;
114 volatile u32 fontt;
115 volatile u32 extent;
116 volatile u32 src;
117 u32 dst;
118 volatile u32 copy;
119 volatile u32 fill;
122 struct leo_lc_ss1_krn {
123 u8 unknown;
126 struct leo_lc_ss1_usr {
127 u8 unknown;
130 struct leo_ld {
131 u8 xxx0[0xe00];
132 volatile u32 csr;
133 volatile u32 wid;
134 volatile u32 wmask;
135 volatile u32 widclip;
136 volatile u32 vclipmin;
137 volatile u32 vclipmax;
138 volatile u32 pickmin; /* SS1 only */
139 volatile u32 pickmax; /* SS1 only */
140 volatile u32 fg;
141 volatile u32 bg;
142 volatile u32 src; /* Copy/Scroll (SS0 only) */
143 volatile u32 dst; /* Copy/Scroll/Fill (SS0 only) */
144 volatile u32 extent; /* Copy/Scroll/Fill size (SS0 only) */
145 u32 xxx1[3];
146 volatile u32 setsem; /* SS1 only */
147 volatile u32 clrsem; /* SS1 only */
148 volatile u32 clrpick; /* SS1 only */
149 volatile u32 clrdat; /* SS1 only */
150 volatile u32 alpha; /* SS1 only */
151 u8 xxx2[0x2c];
152 volatile u32 winbg;
153 volatile u32 planemask;
154 volatile u32 rop;
155 volatile u32 z;
156 volatile u32 dczf; /* SS1 only */
157 volatile u32 dczb; /* SS1 only */
158 volatile u32 dcs; /* SS1 only */
159 volatile u32 dczs; /* SS1 only */
160 volatile u32 pickfb; /* SS1 only */
161 volatile u32 pickbb; /* SS1 only */
162 volatile u32 dcfc; /* SS1 only */
163 volatile u32 forcecol; /* SS1 only */
164 volatile u32 door[8]; /* SS1 only */
165 volatile u32 pick[5]; /* SS1 only */
168 #define LEO_SS1_MISC_ENABLE 0x00000001
169 #define LEO_SS1_MISC_STEREO 0x00000002
170 struct leo_ld_ss1 {
171 u8 xxx0[0xef4];
172 volatile u32 ss1_misc;
175 struct leo_ld_gbl {
176 u8 unknown;
179 struct leo_par {
180 spinlock_t lock;
181 struct leo_lx_krn *lx_krn;
182 struct leo_lc_ss0_usr *lc_ss0_usr;
183 struct leo_ld_ss0 *ld_ss0;
184 struct leo_ld_ss1 *ld_ss1;
185 struct leo_cursor *cursor;
186 u32 extent;
187 u32 clut_data[256];
189 u32 flags;
190 #define LEO_FLAG_BLANKED 0x00000001
192 unsigned long physbase;
193 unsigned long fbsize;
195 struct sbus_dev *sdev;
196 struct list_head list;
199 static void leo_wait(struct leo_lx_krn *lx_krn)
201 int i;
203 for (i = 0;
204 (sbus_readl(&lx_krn->krn_csr) & LEO_KRN_CSR_PROGRESS) && i < 300000;
205 i++)
206 udelay (1); /* Busy wait at most 0.3 sec */
207 return;
211 * leo_setcolreg - Optional function. Sets a color register.
212 * @regno: boolean, 0 copy local, 1 get_user() function
213 * @red: frame buffer colormap structure
214 * @green: The green value which can be up to 16 bits wide
215 * @blue: The blue value which can be up to 16 bits wide.
216 * @transp: If supported the alpha value which can be up to 16 bits wide.
217 * @info: frame buffer info structure
219 static int leo_setcolreg(unsigned regno,
220 unsigned red, unsigned green, unsigned blue,
221 unsigned transp, struct fb_info *info)
223 struct leo_par *par = (struct leo_par *) info->par;
224 struct leo_lx_krn *lx_krn = par->lx_krn;
225 unsigned long flags;
226 u32 val;
227 int i;
229 if (regno >= 256)
230 return 1;
232 red >>= 8;
233 green >>= 8;
234 blue >>= 8;
236 par->clut_data[regno] = red | (green << 8) | (blue << 16);
238 spin_lock_irqsave(&par->lock, flags);
240 leo_wait(lx_krn);
242 sbus_writel(LEO_KRN_TYPE_CLUTDATA, &lx_krn->krn_type);
243 for (i = 0; i < 256; i++)
244 sbus_writel(par->clut_data[i], &lx_krn->krn_value);
245 sbus_writel(LEO_KRN_TYPE_CLUT0, &lx_krn->krn_type);
247 val = sbus_readl(&lx_krn->krn_csr);
248 val |= (LEO_KRN_CSR_UNK | LEO_KRN_CSR_UNK2);
249 sbus_writel(val, &lx_krn->krn_csr);
251 spin_unlock_irqrestore(&par->lock, flags);
253 return 0;
257 * leo_blank - Optional function. Blanks the display.
258 * @blank_mode: the blank mode we want.
259 * @info: frame buffer structure that represents a single frame buffer
261 static int leo_blank(int blank, struct fb_info *info)
263 struct leo_par *par = (struct leo_par *) info->par;
264 struct leo_lx_krn *lx_krn = par->lx_krn;
265 unsigned long flags;
266 u32 val;
268 spin_lock_irqsave(&par->lock, flags);
270 switch (blank) {
271 case 0: /* Unblanking */
272 val = sbus_readl(&lx_krn->krn_csr);
273 val |= LEO_KRN_CSR_ENABLE;
274 sbus_writel(val, &lx_krn->krn_csr);
275 par->flags &= ~LEO_FLAG_BLANKED;
276 break;
278 case 1: /* Normal blanking */
279 case 2: /* VESA blank (vsync off) */
280 case 3: /* VESA blank (hsync off) */
281 case 4: /* Poweroff */
282 val = sbus_readl(&lx_krn->krn_csr);
283 val &= ~LEO_KRN_CSR_ENABLE;
284 sbus_writel(val, &lx_krn->krn_csr);
285 par->flags |= LEO_FLAG_BLANKED;
286 break;
289 spin_unlock_irqrestore(&par->lock, flags);
291 return 0;
294 static struct sbus_mmap_map leo_mmap_map[] = {
295 { LEO_SS0_MAP, LEO_OFF_SS0, 0x800000 },
296 { LEO_LC_SS0_USR_MAP, LEO_OFF_LC_SS0_USR, 0x1000 },
297 { LEO_LD_SS0_MAP, LEO_OFF_LD_SS0, 0x1000 },
298 { LEO_LX_CURSOR_MAP, LEO_OFF_LX_CURSOR, 0x1000 },
299 { LEO_SS1_MAP, LEO_OFF_SS1, 0x800000 },
300 { LEO_LC_SS1_USR_MAP, LEO_OFF_LC_SS1_USR, 0x1000 },
301 { LEO_LD_SS1_MAP, LEO_OFF_LD_SS1, 0x1000 },
302 { LEO_UNK_MAP, LEO_OFF_UNK, 0x1000 },
303 { LEO_LX_KRN_MAP, LEO_OFF_LX_KRN, 0x1000 },
304 { LEO_LC_SS0_KRN_MAP, LEO_OFF_LC_SS0_KRN, 0x1000 },
305 { LEO_LC_SS1_KRN_MAP, LEO_OFF_LC_SS1_KRN, 0x1000 },
306 { LEO_LD_GBL_MAP, LEO_OFF_LD_GBL, 0x1000 },
307 { LEO_UNK2_MAP, LEO_OFF_UNK2, 0x100000 },
308 { 0, 0, 0 }
311 static int leo_mmap(struct fb_info *info, struct file *file, struct vm_area_struct *vma)
313 struct leo_par *par = (struct leo_par *)info->par;
315 return sbusfb_mmap_helper(leo_mmap_map,
316 par->physbase, par->fbsize,
317 par->sdev->reg_addrs[0].which_io,
318 vma);
321 static int leo_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
322 unsigned long arg, struct fb_info *info)
324 struct leo_par *par = (struct leo_par *) info->par;
326 return sbusfb_ioctl_helper(cmd, arg, info,
327 FBTYPE_SUNLEO, 32, par->fbsize);
331 * Initialisation
334 static void
335 leo_init_fix(struct fb_info *info)
337 struct leo_par *par = (struct leo_par *)info->par;
339 strlcpy(info->fix.id, par->sdev->prom_name, sizeof(info->fix.id));
341 info->fix.type = FB_TYPE_PACKED_PIXELS;
342 info->fix.visual = FB_VISUAL_TRUECOLOR;
344 info->fix.line_length = 8192;
346 info->fix.accel = FB_ACCEL_SUN_LEO;
349 static void leo_wid_put(struct fb_info *info, struct fb_wid_list *wl)
351 struct leo_par *par = (struct leo_par *) info->par;
352 struct leo_lx_krn *lx_krn = par->lx_krn;
353 struct fb_wid_item *wi;
354 unsigned long flags;
355 u32 val;
356 int i, j;
358 spin_lock_irqsave(&par->lock, flags);
360 leo_wait(lx_krn);
362 for (i = 0, wi = wl->wl_list; i < wl->wl_count; i++, wi++) {
363 switch(wi->wi_type) {
364 case FB_WID_DBL_8:
365 j = (wi->wi_index & 0xf) + 0x40;
366 break;
368 case FB_WID_DBL_24:
369 j = wi->wi_index & 0x3f;
370 break;
372 default:
373 continue;
375 sbus_writel(0x5800 + j, &lx_krn->krn_type);
376 sbus_writel(wi->wi_values[0], &lx_krn->krn_value);
378 sbus_writel(LEO_KRN_TYPE_WID, &lx_krn->krn_type);
380 val = sbus_readl(&lx_krn->krn_csr);
381 val |= (LEO_KRN_CSR_UNK | LEO_KRN_CSR_UNK2);
382 sbus_writel(val, &lx_krn->krn_csr);
384 spin_unlock_irqrestore(&par->lock, flags);
387 static void leo_init_wids(struct fb_info *info)
389 struct fb_wid_item wi;
390 struct fb_wid_list wl;
392 wl.wl_count = 1;
393 wl.wl_list = &wi;
394 wi.wi_type = FB_WID_DBL_8;
395 wi.wi_index = 0;
396 wi.wi_values [0] = 0x2c0;
397 leo_wid_put(info, &wl);
398 wi.wi_index = 1;
399 wi.wi_values [0] = 0x30;
400 leo_wid_put(info, &wl);
401 wi.wi_index = 2;
402 wi.wi_values [0] = 0x20;
403 leo_wid_put(info, &wl);
404 wi.wi_type = FB_WID_DBL_24;
405 wi.wi_index = 1;
406 wi.wi_values [0] = 0x30;
407 leo_wid_put(info, &wl);
411 static void leo_init_hw(struct fb_info *info)
413 struct leo_par *par = (struct leo_par *) info->par;
414 struct leo_ld *ss = (struct leo_ld *) par->ld_ss0;
415 u32 val;
417 val = sbus_readl(&par->ld_ss1->ss1_misc);
418 val |= LEO_SS1_MISC_ENABLE;
419 sbus_writel(val, &par->ld_ss1->ss1_misc);
421 par->extent = ((info->var.xres - 1) |
422 ((info->var.yres - 1) << 16));
424 sbus_writel(0xffffffff, &ss->wid);
425 sbus_writel(0xffff, &ss->wmask);
426 sbus_writel(0, &ss->vclipmin);
427 sbus_writel(par->extent, &ss->vclipmax);
428 sbus_writel(0, &ss->fg);
429 sbus_writel(0xff000000, &ss->planemask);
430 sbus_writel(0x310850, &ss->rop);
431 sbus_writel(0, &ss->widclip);
432 sbus_writel((info->var.xres-1) | ((info->var.yres-1) << 11),
433 &par->lc_ss0_usr->extent);
434 sbus_writel(4, &par->lc_ss0_usr->addrspace);
435 sbus_writel(0x80000000, &par->lc_ss0_usr->fill);
436 sbus_writel(0, &par->lc_ss0_usr->fontt);
437 do {
438 val = sbus_readl(&par->lc_ss0_usr->csr);
439 } while (val & 0x20000000);
442 static void leo_fixup_var_rgb(struct fb_var_screeninfo *var)
444 var->red.offset = 0;
445 var->red.length = 8;
446 var->green.offset = 8;
447 var->green.length = 8;
448 var->blue.offset = 16;
449 var->blue.length = 8;
450 var->transp.offset = 0;
451 var->transp.length = 0;
454 struct all_info {
455 struct fb_info info;
456 struct leo_par par;
457 struct list_head list;
459 static LIST_HEAD(leo_list);
461 static void leo_init_one(struct sbus_dev *sdev)
463 struct all_info *all;
464 int linebytes;
466 all = kmalloc(sizeof(*all), GFP_KERNEL);
467 if (!all) {
468 printk(KERN_ERR "leo: Cannot allocate memory.\n");
469 return;
471 memset(all, 0, sizeof(*all));
473 INIT_LIST_HEAD(&all->list);
475 spin_lock_init(&all->par.lock);
476 all->par.sdev = sdev;
478 all->par.physbase = sdev->reg_addrs[0].phys_addr;
480 sbusfb_fill_var(&all->info.var, sdev->prom_node, 32);
481 leo_fixup_var_rgb(&all->info.var);
483 linebytes = prom_getintdefault(sdev->prom_node, "linebytes",
484 all->info.var.xres);
485 all->par.fbsize = PAGE_ALIGN(linebytes * all->info.var.yres);
487 #ifdef CONFIG_SPARC32
488 all->info.screen_base = (char *)
489 prom_getintdefault(sdev->prom_node, "address", 0);
490 #endif
491 if (!all->info.screen_base)
492 all->info.screen_base = (char *)
493 sbus_ioremap(&sdev->resource[0], LEO_OFF_SS0,
494 0x800000, "leo ram");
496 all->par.lc_ss0_usr = (struct leo_lc_ss0_usr *)
497 sbus_ioremap(&sdev->resource[0], LEO_OFF_LC_SS0_USR,
498 0x1000, "leolc ss0usr");
499 all->par.ld_ss0 = (struct leo_ld_ss0 *)
500 sbus_ioremap(&sdev->resource[0], LEO_OFF_LD_SS0,
501 0x1000, "leold ss0");
502 all->par.ld_ss1 = (struct leo_ld_ss1 *)
503 sbus_ioremap(&sdev->resource[0], LEO_OFF_LD_SS1,
504 0x1000, "leold ss1");
505 all->par.lx_krn = (struct leo_lx_krn *)
506 sbus_ioremap(&sdev->resource[0], LEO_OFF_LX_KRN,
507 0x1000, "leolx krn");
508 all->par.cursor = (struct leo_cursor *)
509 sbus_ioremap(&sdev->resource[0], LEO_OFF_LX_CURSOR,
510 sizeof(struct leo_cursor), "leolx cursor");
512 all->info.flags = FBINFO_FLAG_DEFAULT;
513 all->info.fbops = &leo_ops;
514 all->info.currcon = -1;
515 all->info.par = &all->par;
517 leo_init_wids(&all->info);
518 leo_init_hw(&all->info);
520 leo_blank(0, &all->info);
522 if (fb_alloc_cmap(&all->info.cmap, 256, 0)) {
523 printk(KERN_ERR "leo: Could not allocate color map.\n");
524 kfree(all);
525 return;
528 leo_init_fix(&all->info);
530 if (register_framebuffer(&all->info) < 0) {
531 printk(KERN_ERR "leo: Could not register framebuffer.\n");
532 fb_dealloc_cmap(&all->info.cmap);
533 kfree(all);
534 return;
537 list_add(&all->list, &leo_list);
539 printk("leo: %s at %lx:%lx\n",
540 sdev->prom_name,
541 (long) sdev->reg_addrs[0].which_io,
542 (long) sdev->reg_addrs[0].phys_addr);
545 int __init leo_init(void)
547 struct sbus_bus *sbus;
548 struct sbus_dev *sdev;
550 for_all_sbusdev(sdev, sbus) {
551 if (!strcmp(sdev->prom_name, "leo"))
552 leo_init_one(sdev);
555 return 0;
558 void __exit leo_exit(void)
560 struct list_head *pos, *tmp;
562 list_for_each_safe(pos, tmp, &leo_list) {
563 struct all_info *all = list_entry(pos, typeof(*all), list);
565 unregister_framebuffer(&all->info);
566 fb_dealloc_cmap(&all->info.cmap);
567 kfree(all);
571 int __init
572 leo_setup(char *arg)
574 /* No cmdline options yet... */
575 return 0;
578 #ifdef MODULE
579 module_init(leo_init);
580 module_exit(leo_exit);
581 #endif
583 MODULE_DESCRIPTION("framebuffer driver for LEO chipsets");
584 MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
585 MODULE_LICENSE("GPL");