[PARISC] stifb: Fix framebuffer console at 32bpp
[linux-2.6] / drivers / video / p9100.c
1 /* p9100.c: P9100 frame buffer driver
2  *
3  * Copyright (C) 2003 David S. Miller (davem@redhat.com)
4  * Copyright 1999 Derrick J Brashear (shadow@dementia.org)
5  *
6  * Driver layout based loosely on tgafb.c, see that file for credits.
7  */
8
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/errno.h>
12 #include <linux/string.h>
13 #include <linux/slab.h>
14 #include <linux/delay.h>
15 #include <linux/init.h>
16 #include <linux/fb.h>
17 #include <linux/mm.h>
18
19 #include <asm/io.h>
20 #include <asm/sbus.h>
21 #include <asm/oplib.h>
22 #include <asm/fbio.h>
23
24 #include "sbuslib.h"
25
26 /*
27  * Local functions.
28  */
29
30 static int p9100_setcolreg(unsigned, unsigned, unsigned, unsigned,
31                            unsigned, struct fb_info *);
32 static int p9100_blank(int, struct fb_info *);
33
34 static int p9100_mmap(struct fb_info *, struct file *, struct vm_area_struct *);
35 static int p9100_ioctl(struct inode *, struct file *, unsigned int,
36                        unsigned long, struct fb_info *);
37
38 /*
39  *  Frame buffer operations
40  */
41
42 static struct fb_ops p9100_ops = {
43         .owner                  = THIS_MODULE,
44         .fb_setcolreg           = p9100_setcolreg,
45         .fb_blank               = p9100_blank,
46         .fb_fillrect            = cfb_fillrect,
47         .fb_copyarea            = cfb_copyarea,
48         .fb_imageblit           = cfb_imageblit,
49         .fb_mmap                = p9100_mmap,
50         .fb_ioctl               = p9100_ioctl,
51 #ifdef CONFIG_COMPAT
52         .fb_compat_ioctl        = sbusfb_compat_ioctl,
53 #endif
54 };
55
56 /* P9100 control registers */
57 #define P9100_SYSCTL_OFF        0x0UL
58 #define P9100_VIDEOCTL_OFF      0x100UL
59 #define P9100_VRAMCTL_OFF       0x180UL
60 #define P9100_RAMDAC_OFF        0x200UL
61 #define P9100_VIDEOCOPROC_OFF   0x400UL
62
63 /* P9100 command registers */
64 #define P9100_CMD_OFF 0x0UL
65
66 /* P9100 framebuffer memory */
67 #define P9100_FB_OFF 0x0UL
68
69 /* 3 bits: 2=8bpp 3=16bpp 5=32bpp 7=24bpp */
70 #define SYS_CONFIG_PIXELSIZE_SHIFT 26 
71
72 #define SCREENPAINT_TIMECTL1_ENABLE_VIDEO 0x20 /* 0 = off, 1 = on */
73
74 struct p9100_regs {
75         /* Registers for the system control */
76         volatile u32 sys_base;
77         volatile u32 sys_config;
78         volatile u32 sys_intr;
79         volatile u32 sys_int_ena;
80         volatile u32 sys_alt_rd;
81         volatile u32 sys_alt_wr;
82         volatile u32 sys_xxx[58];
83
84         /* Registers for the video control */
85         volatile u32 vid_base;
86         volatile u32 vid_hcnt;
87         volatile u32 vid_htotal;
88         volatile u32 vid_hsync_rise;
89         volatile u32 vid_hblank_rise;
90         volatile u32 vid_hblank_fall;
91         volatile u32 vid_hcnt_preload;
92         volatile u32 vid_vcnt;
93         volatile u32 vid_vlen;
94         volatile u32 vid_vsync_rise;
95         volatile u32 vid_vblank_rise;
96         volatile u32 vid_vblank_fall;
97         volatile u32 vid_vcnt_preload;
98         volatile u32 vid_screenpaint_addr;
99         volatile u32 vid_screenpaint_timectl1;
100         volatile u32 vid_screenpaint_qsfcnt;
101         volatile u32 vid_screenpaint_timectl2;
102         volatile u32 vid_xxx[15];
103
104         /* Registers for the video control */
105         volatile u32 vram_base;
106         volatile u32 vram_memcfg;
107         volatile u32 vram_refresh_pd;
108         volatile u32 vram_refresh_cnt;
109         volatile u32 vram_raslo_max;
110         volatile u32 vram_raslo_cur;
111         volatile u32 pwrup_cfg;
112         volatile u32 vram_xxx[25];
113
114         /* Registers for IBM RGB528 Palette */
115         volatile u32 ramdac_cmap_wridx; 
116         volatile u32 ramdac_palette_data;
117         volatile u32 ramdac_pixel_mask;
118         volatile u32 ramdac_palette_rdaddr;
119         volatile u32 ramdac_idx_lo;
120         volatile u32 ramdac_idx_hi;
121         volatile u32 ramdac_idx_data;
122         volatile u32 ramdac_idx_ctl;
123         volatile u32 ramdac_xxx[1784];
124 };
125
126 struct p9100_cmd_parameng {
127         volatile u32 parameng_status;
128         volatile u32 parameng_bltcmd;
129         volatile u32 parameng_quadcmd;
130 };
131
132 struct p9100_par {
133         spinlock_t              lock;
134         struct p9100_regs       __iomem *regs;
135
136         u32                     flags;
137 #define P9100_FLAG_BLANKED      0x00000001
138
139         unsigned long           physbase;
140         unsigned long           fbsize;
141
142         struct sbus_dev         *sdev;
143 };
144
145 /**
146  *      p9100_setcolreg - Optional function. Sets a color register.
147  *      @regno: boolean, 0 copy local, 1 get_user() function
148  *      @red: frame buffer colormap structure
149  *      @green: The green value which can be up to 16 bits wide
150  *      @blue:  The blue value which can be up to 16 bits wide.
151  *      @transp: If supported the alpha value which can be up to 16 bits wide.
152  *      @info: frame buffer info structure
153  */
154 static int p9100_setcolreg(unsigned regno,
155                            unsigned red, unsigned green, unsigned blue,
156                            unsigned transp, struct fb_info *info)
157 {
158         struct p9100_par *par = (struct p9100_par *) info->par;
159         struct p9100_regs __iomem *regs = par->regs;
160         unsigned long flags;
161
162         if (regno >= 256)
163                 return 1;
164
165         red >>= 8;
166         green >>= 8;
167         blue >>= 8;
168
169         spin_lock_irqsave(&par->lock, flags);
170
171         sbus_writel((regno << 16), &regs->ramdac_cmap_wridx);
172         sbus_writel((red << 16), &regs->ramdac_palette_data);
173         sbus_writel((green << 16), &regs->ramdac_palette_data);
174         sbus_writel((blue << 16), &regs->ramdac_palette_data);
175
176         spin_unlock_irqrestore(&par->lock, flags);
177
178         return 0;
179 }
180
181 /**
182  *      p9100_blank - Optional function.  Blanks the display.
183  *      @blank_mode: the blank mode we want.
184  *      @info: frame buffer structure that represents a single frame buffer
185  */
186 static int
187 p9100_blank(int blank, struct fb_info *info)
188 {
189         struct p9100_par *par = (struct p9100_par *) info->par;
190         struct p9100_regs __iomem *regs = par->regs;
191         unsigned long flags;
192         u32 val;
193
194         spin_lock_irqsave(&par->lock, flags);
195
196         switch (blank) {
197         case FB_BLANK_UNBLANK: /* Unblanking */
198                 val = sbus_readl(&regs->vid_screenpaint_timectl1);
199                 val |= SCREENPAINT_TIMECTL1_ENABLE_VIDEO;
200                 sbus_writel(val, &regs->vid_screenpaint_timectl1);
201                 par->flags &= ~P9100_FLAG_BLANKED;
202                 break;
203
204         case FB_BLANK_NORMAL: /* Normal blanking */
205         case FB_BLANK_VSYNC_SUSPEND: /* VESA blank (vsync off) */
206         case FB_BLANK_HSYNC_SUSPEND: /* VESA blank (hsync off) */
207         case FB_BLANK_POWERDOWN: /* Poweroff */
208                 val = sbus_readl(&regs->vid_screenpaint_timectl1);
209                 val &= ~SCREENPAINT_TIMECTL1_ENABLE_VIDEO;
210                 sbus_writel(val, &regs->vid_screenpaint_timectl1);
211                 par->flags |= P9100_FLAG_BLANKED;
212                 break;
213         }
214
215         spin_unlock_irqrestore(&par->lock, flags);
216
217         return 0;
218 }
219
220 static struct sbus_mmap_map p9100_mmap_map[] = {
221         { CG3_MMAP_OFFSET,      0,              SBUS_MMAP_FBSIZE(1) },
222         { 0,                    0,              0                   }
223 };
224
225 static int p9100_mmap(struct fb_info *info, struct file *file, struct vm_area_struct *vma)
226 {
227         struct p9100_par *par = (struct p9100_par *)info->par;
228
229         return sbusfb_mmap_helper(p9100_mmap_map,
230                                   par->physbase, par->fbsize,
231                                   par->sdev->reg_addrs[0].which_io,
232                                   vma);
233 }
234
235 static int p9100_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
236                        unsigned long arg, struct fb_info *info)
237 {
238         struct p9100_par *par = (struct p9100_par *) info->par;
239
240         /* Make it look like a cg3. */
241         return sbusfb_ioctl_helper(cmd, arg, info,
242                                    FBTYPE_SUN3COLOR, 8, par->fbsize);
243 }
244
245 /*
246  *  Initialisation
247  */
248
249 static void
250 p9100_init_fix(struct fb_info *info, int linebytes)
251 {
252         struct p9100_par *par = (struct p9100_par *)info->par;
253
254         strlcpy(info->fix.id, par->sdev->prom_name, sizeof(info->fix.id));
255
256         info->fix.type = FB_TYPE_PACKED_PIXELS;
257         info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
258
259         info->fix.line_length = linebytes;
260
261         info->fix.accel = FB_ACCEL_SUN_CGTHREE;
262 }
263
264 struct all_info {
265         struct fb_info info;
266         struct p9100_par par;
267         struct list_head list;
268 };
269 static LIST_HEAD(p9100_list);
270
271 static void p9100_init_one(struct sbus_dev *sdev)
272 {
273         struct all_info *all;
274         int linebytes;
275
276         all = kmalloc(sizeof(*all), GFP_KERNEL);
277         if (!all) {
278                 printk(KERN_ERR "p9100: Cannot allocate memory.\n");
279                 return;
280         }
281         memset(all, 0, sizeof(*all));
282
283         INIT_LIST_HEAD(&all->list);
284
285         spin_lock_init(&all->par.lock);
286         all->par.sdev = sdev;
287
288         /* This is the framebuffer and the only resource apps can mmap.  */
289         all->par.physbase = sdev->reg_addrs[2].phys_addr;
290
291         sbusfb_fill_var(&all->info.var, sdev->prom_node, 8);
292         all->info.var.red.length = 8;
293         all->info.var.green.length = 8;
294         all->info.var.blue.length = 8;
295
296         linebytes = prom_getintdefault(sdev->prom_node, "linebytes",
297                                        all->info.var.xres);
298         all->par.fbsize = PAGE_ALIGN(linebytes * all->info.var.yres);
299
300         all->par.regs = sbus_ioremap(&sdev->resource[0], 0,
301                              sizeof(struct p9100_regs), "p9100 regs");
302
303         all->info.flags = FBINFO_DEFAULT;
304         all->info.fbops = &p9100_ops;
305 #ifdef CONFIG_SPARC32
306         all->info.screen_base = (char __iomem *)
307                 prom_getintdefault(sdev->prom_node, "address", 0);
308 #endif
309         if (!all->info.screen_base)
310                 all->info.screen_base = sbus_ioremap(&sdev->resource[2], 0,
311                                      all->par.fbsize, "p9100 ram");
312         all->info.par = &all->par;
313
314         p9100_blank(0, &all->info);
315
316         if (fb_alloc_cmap(&all->info.cmap, 256, 0)) {
317                 printk(KERN_ERR "p9100: Could not allocate color map.\n");
318                 kfree(all);
319                 return;
320         }
321
322         p9100_init_fix(&all->info, linebytes);
323
324         if (register_framebuffer(&all->info) < 0) {
325                 printk(KERN_ERR "p9100: Could not register framebuffer.\n");
326                 fb_dealloc_cmap(&all->info.cmap);
327                 kfree(all);
328                 return;
329         }
330         fb_set_cmap(&all->info.cmap, &all->info);
331
332         list_add(&all->list, &p9100_list);
333
334         printk("p9100: %s at %lx:%lx\n",
335                sdev->prom_name,
336                (long) sdev->reg_addrs[0].which_io,
337                (long) sdev->reg_addrs[0].phys_addr);
338 }
339
340 int __init p9100_init(void)
341 {
342         struct sbus_bus *sbus;
343         struct sbus_dev *sdev;
344
345         if (fb_get_options("p9100fb", NULL))
346                 return -ENODEV;
347
348         for_all_sbusdev(sdev, sbus) {
349                 if (!strcmp(sdev->prom_name, "p9100"))
350                         p9100_init_one(sdev);
351         }
352
353         return 0;
354 }
355
356 void __exit p9100_exit(void)
357 {
358         struct list_head *pos, *tmp;
359
360         list_for_each_safe(pos, tmp, &p9100_list) {
361                 struct all_info *all = list_entry(pos, typeof(*all), list);
362
363                 unregister_framebuffer(&all->info);
364                 fb_dealloc_cmap(&all->info.cmap);
365                 kfree(all);
366         }
367 }
368
369 int __init
370 p9100_setup(char *arg)
371 {
372         /* No cmdline options yet... */
373         return 0;
374 }
375
376 module_init(p9100_init);
377
378 #ifdef MODULE
379 module_exit(p9100_exit);
380 #endif
381
382 MODULE_DESCRIPTION("framebuffer driver for P9100 chipsets");
383 MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
384 MODULE_LICENSE("GPL");