1 // SPDX-License-Identifier: GPL-2.0-only
2 /* cg14.c: CGFOURTEEN frame buffer driver
4 * Copyright (C) 2003, 2006 David S. Miller (davem@davemloft.net)
5 * Copyright (C) 1996,1998 Jakub Jelinek (jj@ultra.linux.cz)
6 * Copyright (C) 1995 Miguel de Icaza (miguel@nuclecu.unam.mx)
8 * Driver layout based loosely on tgafb.c, see that file for credits.
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/string.h>
15 #include <linux/delay.h>
16 #include <linux/init.h>
19 #include <linux/uaccess.h>
21 #include <linux/platform_device.h>
32 static int cg14_setcolreg(unsigned, unsigned, unsigned, unsigned,
33 unsigned, struct fb_info *);
35 static int cg14_mmap(struct fb_info *, struct vm_area_struct *);
36 static int cg14_ioctl(struct fb_info *, unsigned int, unsigned long);
37 static int cg14_pan_display(struct fb_var_screeninfo *, struct fb_info *);
40 * Frame buffer operations
43 static const struct fb_ops cg14_ops = {
45 .fb_setcolreg = cg14_setcolreg,
46 .fb_pan_display = cg14_pan_display,
47 .fb_fillrect = cfb_fillrect,
48 .fb_copyarea = cfb_copyarea,
49 .fb_imageblit = cfb_imageblit,
51 .fb_ioctl = cg14_ioctl,
53 .fb_compat_ioctl = sbusfb_compat_ioctl,
57 #define CG14_MCR_INTENABLE_SHIFT 7
58 #define CG14_MCR_INTENABLE_MASK 0x80
59 #define CG14_MCR_VIDENABLE_SHIFT 6
60 #define CG14_MCR_VIDENABLE_MASK 0x40
61 #define CG14_MCR_PIXMODE_SHIFT 4
62 #define CG14_MCR_PIXMODE_MASK 0x30
63 #define CG14_MCR_TMR_SHIFT 2
64 #define CG14_MCR_TMR_MASK 0x0c
65 #define CG14_MCR_TMENABLE_SHIFT 1
66 #define CG14_MCR_TMENABLE_MASK 0x02
67 #define CG14_MCR_RESET_SHIFT 0
68 #define CG14_MCR_RESET_MASK 0x01
69 #define CG14_REV_REVISION_SHIFT 4
70 #define CG14_REV_REVISION_MASK 0xf0
71 #define CG14_REV_IMPL_SHIFT 0
72 #define CG14_REV_IMPL_MASK 0x0f
73 #define CG14_VBR_FRAMEBASE_SHIFT 12
74 #define CG14_VBR_FRAMEBASE_MASK 0x00fff000
75 #define CG14_VMCR1_SETUP_SHIFT 0
76 #define CG14_VMCR1_SETUP_MASK 0x000001ff
77 #define CG14_VMCR1_VCONFIG_SHIFT 9
78 #define CG14_VMCR1_VCONFIG_MASK 0x00000e00
79 #define CG14_VMCR2_REFRESH_SHIFT 0
80 #define CG14_VMCR2_REFRESH_MASK 0x00000001
81 #define CG14_VMCR2_TESTROWCNT_SHIFT 1
82 #define CG14_VMCR2_TESTROWCNT_MASK 0x00000002
83 #define CG14_VMCR2_FBCONFIG_SHIFT 2
84 #define CG14_VMCR2_FBCONFIG_MASK 0x0000000c
85 #define CG14_VCR_REFRESHREQ_SHIFT 0
86 #define CG14_VCR_REFRESHREQ_MASK 0x000003ff
87 #define CG14_VCR1_REFRESHENA_SHIFT 10
88 #define CG14_VCR1_REFRESHENA_MASK 0x00000400
89 #define CG14_VCA_CAD_SHIFT 0
90 #define CG14_VCA_CAD_MASK 0x000003ff
91 #define CG14_VCA_VERS_SHIFT 10
92 #define CG14_VCA_VERS_MASK 0x00000c00
93 #define CG14_VCA_RAMSPEED_SHIFT 12
94 #define CG14_VCA_RAMSPEED_MASK 0x00001000
95 #define CG14_VCA_8MB_SHIFT 13
96 #define CG14_VCA_8MB_MASK 0x00002000
98 #define CG14_MCR_PIXMODE_8 0
99 #define CG14_MCR_PIXMODE_16 2
100 #define CG14_MCR_PIXMODE_32 3
103 u8 mcr; /* Master Control Reg */
104 u8 ppr; /* Packed Pixel Reg */
105 u8 tms[2]; /* Test Mode Status Regs */
106 u8 msr; /* Master Status Reg */
107 u8 fsr; /* Fault Status Reg */
108 u8 rev; /* Revision & Impl */
109 u8 ccr; /* Clock Control Reg */
110 u32 tmr; /* Test Mode Read Back */
111 u8 mod; /* Monitor Operation Data Reg */
112 u8 acr; /* Aux Control */
114 u16 hct; /* Hor Counter */
115 u16 vct; /* Vert Counter */
116 u16 hbs; /* Hor Blank Start */
117 u16 hbc; /* Hor Blank Clear */
118 u16 hss; /* Hor Sync Start */
119 u16 hsc; /* Hor Sync Clear */
120 u16 csc; /* Composite Sync Clear */
121 u16 vbs; /* Vert Blank Start */
122 u16 vbc; /* Vert Blank Clear */
123 u16 vss; /* Vert Sync Start */
124 u16 vsc; /* Vert Sync Clear */
127 u16 fsa; /* Fault Status Address */
128 u16 adr; /* Address Registers */
130 u8 pcg[0x100]; /* Pixel Clock Generator */
131 u32 vbr; /* Frame Base Row */
132 u32 vmcr; /* VBC Master Control */
133 u32 vcr; /* VBC refresh */
134 u32 vca; /* VBC Config */
137 #define CG14_CCR_ENABLE 0x04
138 #define CG14_CCR_SELECT 0x02 /* HW/Full screen */
141 u32 cpl0[32]; /* Enable plane 0 */
142 u32 cpl1[32]; /* Color selection plane */
143 u8 ccr; /* Cursor Control Reg */
145 u16 cursx; /* Cursor x,y position */
146 u16 cursy; /* Cursor x,y position */
150 u32 cpl0i[32]; /* Enable plane 0 autoinc */
151 u32 cpl1i[32]; /* Color selection autoinc */
155 u8 addr; /* Address Register */
157 u8 glut; /* Gamma table */
159 u8 select; /* Register Select */
161 u8 mode; /* Mode Register */
169 u8 x_xlutd_inc [256];
172 /* Color look up table (clut) */
173 /* Each one of these arrays hold the color lookup table (for 256
174 * colors) for each MDI page (I assume then there should be 4 MDI
175 * pages, I still wonder what they are. I have seen NeXTStep split
176 * the screen in four parts, while operating in 24 bits mode. Each
177 * integer holds 4 values: alpha value (transparency channel, thanks
178 * go to John Stone (johns@umr.edu) from OpenBSD), red, green and blue
180 * I currently use the clut instead of the Xlut
184 u32 c_clutd [256]; /* i wonder what the 'd' is for */
185 u32 c_clut_inc [256];
186 u32 c_clutd_inc [256];
189 #define CG14_MMAP_ENTRIES 16
193 struct cg14_regs __iomem *regs;
194 struct cg14_clut __iomem *clut;
195 struct cg14_cursor __iomem *cursor;
198 #define CG14_FLAG_BLANKED 0x00000001
200 unsigned long iospace;
202 struct sbus_mmap_map mmap_map[CG14_MMAP_ENTRIES];
208 static void __cg14_reset(struct cg14_par *par)
210 struct cg14_regs __iomem *regs = par->regs;
213 val = sbus_readb(®s->mcr);
214 val &= ~(CG14_MCR_PIXMODE_MASK);
215 sbus_writeb(val, ®s->mcr);
218 static int cg14_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
220 struct cg14_par *par = (struct cg14_par *) info->par;
223 /* We just use this to catch switches out of
226 spin_lock_irqsave(&par->lock, flags);
228 spin_unlock_irqrestore(&par->lock, flags);
230 if (var->xoffset || var->yoffset || var->vmode)
236 * cg14_setcolreg - Optional function. Sets a color register.
237 * @regno: boolean, 0 copy local, 1 get_user() function
238 * @red: frame buffer colormap structure
239 * @green: The green value which can be up to 16 bits wide
240 * @blue: The blue value which can be up to 16 bits wide.
241 * @transp: If supported the alpha value which can be up to 16 bits wide.
242 * @info: frame buffer info structure
244 static int cg14_setcolreg(unsigned regno,
245 unsigned red, unsigned green, unsigned blue,
246 unsigned transp, struct fb_info *info)
248 struct cg14_par *par = (struct cg14_par *) info->par;
249 struct cg14_clut __iomem *clut = par->clut;
259 val = (red | (green << 8) | (blue << 16));
261 spin_lock_irqsave(&par->lock, flags);
262 sbus_writel(val, &clut->c_clut[regno]);
263 spin_unlock_irqrestore(&par->lock, flags);
268 static int cg14_mmap(struct fb_info *info, struct vm_area_struct *vma)
270 struct cg14_par *par = (struct cg14_par *) info->par;
272 return sbusfb_mmap_helper(par->mmap_map,
273 info->fix.smem_start, info->fix.smem_len,
277 static int cg14_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
279 struct cg14_par *par = (struct cg14_par *) info->par;
280 struct cg14_regs __iomem *regs = par->regs;
281 struct mdi_cfginfo kmdi, __user *mdii;
283 int cur_mode, mode, ret = 0;
287 spin_lock_irqsave(&par->lock, flags);
289 spin_unlock_irqrestore(&par->lock, flags);
292 case MDI_GET_CFGINFO:
293 memset(&kmdi, 0, sizeof(kmdi));
295 spin_lock_irqsave(&par->lock, flags);
296 kmdi.mdi_type = FBTYPE_MDICOLOR;
297 kmdi.mdi_height = info->var.yres;
298 kmdi.mdi_width = info->var.xres;
299 kmdi.mdi_mode = par->mode;
300 kmdi.mdi_pixfreq = 72; /* FIXME */
301 kmdi.mdi_size = par->ramsize;
302 spin_unlock_irqrestore(&par->lock, flags);
304 mdii = (struct mdi_cfginfo __user *) arg;
305 if (copy_to_user(mdii, &kmdi, sizeof(kmdi)))
309 case MDI_SET_PIXELMODE:
310 if (get_user(mode, (int __user *) arg)) {
315 spin_lock_irqsave(&par->lock, flags);
316 cur_mode = sbus_readb(®s->mcr);
317 cur_mode &= ~CG14_MCR_PIXMODE_MASK;
320 cur_mode |= (CG14_MCR_PIXMODE_32 <<
321 CG14_MCR_PIXMODE_SHIFT);
325 cur_mode |= (CG14_MCR_PIXMODE_16 <<
326 CG14_MCR_PIXMODE_SHIFT);
337 sbus_writeb(cur_mode, ®s->mcr);
340 spin_unlock_irqrestore(&par->lock, flags);
344 ret = sbusfb_ioctl_helper(cmd, arg, info,
357 static void cg14_init_fix(struct fb_info *info, int linebytes,
358 struct device_node *dp)
360 snprintf(info->fix.id, sizeof(info->fix.id), "%pOFn", dp);
362 info->fix.type = FB_TYPE_PACKED_PIXELS;
363 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
365 info->fix.line_length = linebytes;
367 info->fix.accel = FB_ACCEL_SUN_CG14;
370 static struct sbus_mmap_map __cg14_mmap_map[CG14_MMAP_ENTRIES] = {
397 .voff = CG3_MMAP_OFFSET - 0x7000,
402 .voff = CG3_MMAP_OFFSET,
404 .size = SBUS_MMAP_FBSIZE(1)
407 .voff = MDI_CURSOR_MAP,
412 .voff = MDI_CHUNKY_BGR_MAP,
417 .voff = MDI_PLANAR_X16_MAP,
422 .voff = MDI_PLANAR_C16_MAP,
427 .voff = MDI_PLANAR_X32_MAP,
432 .voff = MDI_PLANAR_B32_MAP,
437 .voff = MDI_PLANAR_G32_MAP,
442 .voff = MDI_PLANAR_R32_MAP,
449 static void cg14_unmap_regs(struct platform_device *op, struct fb_info *info,
450 struct cg14_par *par)
453 of_iounmap(&op->resource[0],
454 par->regs, sizeof(struct cg14_regs));
456 of_iounmap(&op->resource[0],
457 par->clut, sizeof(struct cg14_clut));
459 of_iounmap(&op->resource[0],
460 par->cursor, sizeof(struct cg14_cursor));
461 if (info->screen_base)
462 of_iounmap(&op->resource[1],
463 info->screen_base, info->fix.smem_len);
466 static int cg14_probe(struct platform_device *op)
468 struct device_node *dp = op->dev.of_node;
469 struct fb_info *info;
470 struct cg14_par *par;
471 int is_8mb, linebytes, i, err;
473 info = framebuffer_alloc(sizeof(struct cg14_par), &op->dev);
480 spin_lock_init(&par->lock);
482 sbusfb_fill_var(&info->var, dp, 8);
483 info->var.red.length = 8;
484 info->var.green.length = 8;
485 info->var.blue.length = 8;
487 linebytes = of_getintprop_default(dp, "linebytes",
489 info->fix.smem_len = PAGE_ALIGN(linebytes * info->var.yres);
491 if (of_node_name_eq(dp->parent, "sbus") ||
492 of_node_name_eq(dp->parent, "sbi")) {
493 info->fix.smem_start = op->resource[0].start;
494 par->iospace = op->resource[0].flags & IORESOURCE_BITS;
496 info->fix.smem_start = op->resource[1].start;
497 par->iospace = op->resource[0].flags & IORESOURCE_BITS;
500 par->regs = of_ioremap(&op->resource[0], 0,
501 sizeof(struct cg14_regs), "cg14 regs");
502 par->clut = of_ioremap(&op->resource[0], CG14_CLUT1,
503 sizeof(struct cg14_clut), "cg14 clut");
504 par->cursor = of_ioremap(&op->resource[0], CG14_CURSORREGS,
505 sizeof(struct cg14_cursor), "cg14 cursor");
507 info->screen_base = of_ioremap(&op->resource[1], 0,
508 info->fix.smem_len, "cg14 ram");
510 if (!par->regs || !par->clut || !par->cursor || !info->screen_base)
513 is_8mb = (resource_size(&op->resource[1]) == (8 * 1024 * 1024));
515 BUILD_BUG_ON(sizeof(par->mmap_map) != sizeof(__cg14_mmap_map));
517 memcpy(&par->mmap_map, &__cg14_mmap_map, sizeof(par->mmap_map));
519 for (i = 0; i < CG14_MMAP_ENTRIES; i++) {
520 struct sbus_mmap_map *map = &par->mmap_map[i];
524 if (map->poff & 0x80000000)
525 map->poff = (map->poff & 0x7fffffff) +
526 (op->resource[0].start -
527 op->resource[1].start);
529 map->size >= 0x100000 &&
530 map->size <= 0x400000)
534 par->mode = MDI_8_PIX;
535 par->ramsize = (is_8mb ? 0x800000 : 0x400000);
537 info->flags = FBINFO_HWACCEL_YPAN;
538 info->fbops = &cg14_ops;
542 if (fb_alloc_cmap(&info->cmap, 256, 0))
545 fb_set_cmap(&info->cmap, info);
547 cg14_init_fix(info, linebytes, dp);
549 err = register_framebuffer(info);
551 goto out_dealloc_cmap;
553 dev_set_drvdata(&op->dev, info);
555 printk(KERN_INFO "%pOF: cgfourteen at %lx:%lx, %dMB\n",
557 par->iospace, info->fix.smem_start,
563 fb_dealloc_cmap(&info->cmap);
566 cg14_unmap_regs(op, info, par);
567 framebuffer_release(info);
573 static void cg14_remove(struct platform_device *op)
575 struct fb_info *info = dev_get_drvdata(&op->dev);
576 struct cg14_par *par = info->par;
578 unregister_framebuffer(info);
579 fb_dealloc_cmap(&info->cmap);
581 cg14_unmap_regs(op, info, par);
583 framebuffer_release(info);
586 static const struct of_device_id cg14_match[] = {
588 .name = "cgfourteen",
592 MODULE_DEVICE_TABLE(of, cg14_match);
594 static struct platform_driver cg14_driver = {
597 .of_match_table = cg14_match,
600 .remove_new = cg14_remove,
603 static int __init cg14_init(void)
605 if (fb_get_options("cg14fb", NULL))
608 return platform_driver_register(&cg14_driver);
611 static void __exit cg14_exit(void)
613 platform_driver_unregister(&cg14_driver);
616 module_init(cg14_init);
617 module_exit(cg14_exit);
619 MODULE_DESCRIPTION("framebuffer driver for CGfourteen chipsets");
620 MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
621 MODULE_VERSION("2.0");
622 MODULE_LICENSE("GPL");