xref: /linux/drivers/video/fbdev/p9100.c (revision 89755ee1d59311855b4afcf35aebddcb653b3cd5)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* p9100.c: P9100 frame buffer driver
3  *
4  * Copyright (C) 2003, 2006 David S. Miller (davem@davemloft.net)
5  * Copyright 1999 Derrick J Brashear (shadow@dementia.org)
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/delay.h>
15 #include <linux/init.h>
16 #include <linux/fb.h>
17 #include <linux/mm.h>
18 #include <linux/of_device.h>
19 
20 #include <asm/io.h>
21 #include <asm/fbio.h>
22 
23 #include "sbuslib.h"
24 
25 /*
26  * Local functions.
27  */
28 
29 static int p9100_setcolreg(unsigned, unsigned, unsigned, unsigned,
30 			   unsigned, struct fb_info *);
31 static int p9100_blank(int, struct fb_info *);
32 
33 static int p9100_sbusfb_mmap(struct fb_info *info, struct vm_area_struct *vma);
34 static int p9100_sbusfb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg);
35 
36 /*
37  *  Frame buffer operations
38  */
39 
40 static const struct fb_ops p9100_ops = {
41 	.owner			= THIS_MODULE,
42 	FB_DEFAULT_SBUS_OPS(p9100),
43 	.fb_setcolreg		= p9100_setcolreg,
44 	.fb_blank		= p9100_blank,
45 };
46 
47 /* P9100 control registers */
48 #define P9100_SYSCTL_OFF	0x0UL
49 #define P9100_VIDEOCTL_OFF	0x100UL
50 #define P9100_VRAMCTL_OFF 	0x180UL
51 #define P9100_RAMDAC_OFF 	0x200UL
52 #define P9100_VIDEOCOPROC_OFF 	0x400UL
53 
54 /* P9100 command registers */
55 #define P9100_CMD_OFF 0x0UL
56 
57 /* P9100 framebuffer memory */
58 #define P9100_FB_OFF 0x0UL
59 
60 /* 3 bits: 2=8bpp 3=16bpp 5=32bpp 7=24bpp */
61 #define SYS_CONFIG_PIXELSIZE_SHIFT 26
62 
63 #define SCREENPAINT_TIMECTL1_ENABLE_VIDEO 0x20 /* 0 = off, 1 = on */
64 
65 struct p9100_regs {
66 	/* Registers for the system control */
67 	u32 sys_base;
68 	u32 sys_config;
69 	u32 sys_intr;
70 	u32 sys_int_ena;
71 	u32 sys_alt_rd;
72 	u32 sys_alt_wr;
73 	u32 sys_xxx[58];
74 
75 	/* Registers for the video control */
76 	u32 vid_base;
77 	u32 vid_hcnt;
78 	u32 vid_htotal;
79 	u32 vid_hsync_rise;
80 	u32 vid_hblank_rise;
81 	u32 vid_hblank_fall;
82 	u32 vid_hcnt_preload;
83 	u32 vid_vcnt;
84 	u32 vid_vlen;
85 	u32 vid_vsync_rise;
86 	u32 vid_vblank_rise;
87 	u32 vid_vblank_fall;
88 	u32 vid_vcnt_preload;
89 	u32 vid_screenpaint_addr;
90 	u32 vid_screenpaint_timectl1;
91 	u32 vid_screenpaint_qsfcnt;
92 	u32 vid_screenpaint_timectl2;
93 	u32 vid_xxx[15];
94 
95 	/* Registers for the video control */
96 	u32 vram_base;
97 	u32 vram_memcfg;
98 	u32 vram_refresh_pd;
99 	u32 vram_refresh_cnt;
100 	u32 vram_raslo_max;
101 	u32 vram_raslo_cur;
102 	u32 pwrup_cfg;
103 	u32 vram_xxx[25];
104 
105 	/* Registers for IBM RGB528 Palette */
106 	u32 ramdac_cmap_wridx;
107 	u32 ramdac_palette_data;
108 	u32 ramdac_pixel_mask;
109 	u32 ramdac_palette_rdaddr;
110 	u32 ramdac_idx_lo;
111 	u32 ramdac_idx_hi;
112 	u32 ramdac_idx_data;
113 	u32 ramdac_idx_ctl;
114 	u32 ramdac_xxx[1784];
115 };
116 
117 struct p9100_cmd_parameng {
118 	u32 parameng_status;
119 	u32 parameng_bltcmd;
120 	u32 parameng_quadcmd;
121 };
122 
123 struct p9100_par {
124 	spinlock_t		lock;
125 	struct p9100_regs	__iomem *regs;
126 
127 	u32			flags;
128 #define P9100_FLAG_BLANKED	0x00000001
129 
130 	unsigned long		which_io;
131 };
132 
133 /**
134  *      p9100_setcolreg - Optional function. Sets a color register.
135  *      @regno: boolean, 0 copy local, 1 get_user() function
136  *      @red: frame buffer colormap structure
137  *      @green: The green value which can be up to 16 bits wide
138  *      @blue:  The blue value which can be up to 16 bits wide.
139  *      @transp: If supported the alpha value which can be up to 16 bits wide.
140  *      @info: frame buffer info structure
141  */
142 static int p9100_setcolreg(unsigned regno,
143 			   unsigned red, unsigned green, unsigned blue,
144 			   unsigned transp, struct fb_info *info)
145 {
146 	struct p9100_par *par = (struct p9100_par *) info->par;
147 	struct p9100_regs __iomem *regs = par->regs;
148 	unsigned long flags;
149 
150 	if (regno >= 256)
151 		return 1;
152 
153 	red >>= 8;
154 	green >>= 8;
155 	blue >>= 8;
156 
157 	spin_lock_irqsave(&par->lock, flags);
158 
159 	sbus_writel((regno << 16), &regs->ramdac_cmap_wridx);
160 	sbus_writel((red << 16), &regs->ramdac_palette_data);
161 	sbus_writel((green << 16), &regs->ramdac_palette_data);
162 	sbus_writel((blue << 16), &regs->ramdac_palette_data);
163 
164 	spin_unlock_irqrestore(&par->lock, flags);
165 
166 	return 0;
167 }
168 
169 /**
170  *      p9100_blank - Optional function.  Blanks the display.
171  *      @blank: the blank mode we want.
172  *      @info: frame buffer structure that represents a single frame buffer
173  */
174 static int
175 p9100_blank(int blank, struct fb_info *info)
176 {
177 	struct p9100_par *par = (struct p9100_par *) info->par;
178 	struct p9100_regs __iomem *regs = par->regs;
179 	unsigned long flags;
180 	u32 val;
181 
182 	spin_lock_irqsave(&par->lock, flags);
183 
184 	switch (blank) {
185 	case FB_BLANK_UNBLANK: /* Unblanking */
186 		val = sbus_readl(&regs->vid_screenpaint_timectl1);
187 		val |= SCREENPAINT_TIMECTL1_ENABLE_VIDEO;
188 		sbus_writel(val, &regs->vid_screenpaint_timectl1);
189 		par->flags &= ~P9100_FLAG_BLANKED;
190 		break;
191 
192 	case FB_BLANK_NORMAL: /* Normal blanking */
193 	case FB_BLANK_VSYNC_SUSPEND: /* VESA blank (vsync off) */
194 	case FB_BLANK_HSYNC_SUSPEND: /* VESA blank (hsync off) */
195 	case FB_BLANK_POWERDOWN: /* Poweroff */
196 		val = sbus_readl(&regs->vid_screenpaint_timectl1);
197 		val &= ~SCREENPAINT_TIMECTL1_ENABLE_VIDEO;
198 		sbus_writel(val, &regs->vid_screenpaint_timectl1);
199 		par->flags |= P9100_FLAG_BLANKED;
200 		break;
201 	}
202 
203 	spin_unlock_irqrestore(&par->lock, flags);
204 
205 	return 0;
206 }
207 
208 static struct sbus_mmap_map p9100_mmap_map[] = {
209 	{ CG3_MMAP_OFFSET,	0,		SBUS_MMAP_FBSIZE(1) },
210 	{ 0,			0,		0		    }
211 };
212 
213 static int p9100_sbusfb_mmap(struct fb_info *info, struct vm_area_struct *vma)
214 {
215 	struct p9100_par *par = (struct p9100_par *)info->par;
216 
217 	return sbusfb_mmap_helper(p9100_mmap_map,
218 				  info->fix.smem_start, info->fix.smem_len,
219 				  par->which_io, vma);
220 }
221 
222 static int p9100_sbusfb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
223 {
224 	/* Make it look like a cg3. */
225 	return sbusfb_ioctl_helper(cmd, arg, info,
226 				   FBTYPE_SUN3COLOR, 8, info->fix.smem_len);
227 }
228 
229 /*
230  *  Initialisation
231  */
232 
233 static void p9100_init_fix(struct fb_info *info, int linebytes, struct device_node *dp)
234 {
235 	snprintf(info->fix.id, sizeof(info->fix.id), "%pOFn", dp);
236 
237 	info->fix.type = FB_TYPE_PACKED_PIXELS;
238 	info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
239 
240 	info->fix.line_length = linebytes;
241 
242 	info->fix.accel = FB_ACCEL_SUN_CGTHREE;
243 }
244 
245 static int p9100_probe(struct platform_device *op)
246 {
247 	struct device_node *dp = op->dev.of_node;
248 	struct fb_info *info;
249 	struct p9100_par *par;
250 	int linebytes, err;
251 
252 	info = framebuffer_alloc(sizeof(struct p9100_par), &op->dev);
253 
254 	err = -ENOMEM;
255 	if (!info)
256 		goto out_err;
257 	par = info->par;
258 
259 	spin_lock_init(&par->lock);
260 
261 	/* This is the framebuffer and the only resource apps can mmap.  */
262 	info->fix.smem_start = op->resource[2].start;
263 	par->which_io = op->resource[2].flags & IORESOURCE_BITS;
264 
265 	sbusfb_fill_var(&info->var, dp, 8);
266 	info->var.red.length = 8;
267 	info->var.green.length = 8;
268 	info->var.blue.length = 8;
269 
270 	linebytes = of_getintprop_default(dp, "linebytes", info->var.xres);
271 	info->fix.smem_len = PAGE_ALIGN(linebytes * info->var.yres);
272 
273 	par->regs = of_ioremap(&op->resource[0], 0,
274 			       sizeof(struct p9100_regs), "p9100 regs");
275 	if (!par->regs)
276 		goto out_release_fb;
277 
278 	info->fbops = &p9100_ops;
279 	info->screen_base = of_ioremap(&op->resource[2], 0,
280 				       info->fix.smem_len, "p9100 ram");
281 	if (!info->screen_base)
282 		goto out_unmap_regs;
283 
284 	p9100_blank(FB_BLANK_UNBLANK, info);
285 
286 	if (fb_alloc_cmap(&info->cmap, 256, 0))
287 		goto out_unmap_screen;
288 
289 	p9100_init_fix(info, linebytes, dp);
290 
291 	err = register_framebuffer(info);
292 	if (err < 0)
293 		goto out_dealloc_cmap;
294 
295 	fb_set_cmap(&info->cmap, info);
296 
297 	dev_set_drvdata(&op->dev, info);
298 
299 	printk(KERN_INFO "%pOF: p9100 at %lx:%lx\n",
300 	       dp,
301 	       par->which_io, info->fix.smem_start);
302 
303 	return 0;
304 
305 out_dealloc_cmap:
306 	fb_dealloc_cmap(&info->cmap);
307 
308 out_unmap_screen:
309 	of_iounmap(&op->resource[2], info->screen_base, info->fix.smem_len);
310 
311 out_unmap_regs:
312 	of_iounmap(&op->resource[0], par->regs, sizeof(struct p9100_regs));
313 
314 out_release_fb:
315 	framebuffer_release(info);
316 
317 out_err:
318 	return err;
319 }
320 
321 static void p9100_remove(struct platform_device *op)
322 {
323 	struct fb_info *info = dev_get_drvdata(&op->dev);
324 	struct p9100_par *par = info->par;
325 
326 	unregister_framebuffer(info);
327 	fb_dealloc_cmap(&info->cmap);
328 
329 	of_iounmap(&op->resource[0], par->regs, sizeof(struct p9100_regs));
330 	of_iounmap(&op->resource[2], info->screen_base, info->fix.smem_len);
331 
332 	framebuffer_release(info);
333 }
334 
335 static const struct of_device_id p9100_match[] = {
336 	{
337 		.name = "p9100",
338 	},
339 	{},
340 };
341 MODULE_DEVICE_TABLE(of, p9100_match);
342 
343 static struct platform_driver p9100_driver = {
344 	.driver = {
345 		.name = "p9100",
346 		.of_match_table = p9100_match,
347 	},
348 	.probe		= p9100_probe,
349 	.remove_new	= p9100_remove,
350 };
351 
352 static int __init p9100_init(void)
353 {
354 	if (fb_get_options("p9100fb", NULL))
355 		return -ENODEV;
356 
357 	return platform_driver_register(&p9100_driver);
358 }
359 
360 static void __exit p9100_exit(void)
361 {
362 	platform_driver_unregister(&p9100_driver);
363 }
364 
365 module_init(p9100_init);
366 module_exit(p9100_exit);
367 
368 MODULE_DESCRIPTION("framebuffer driver for P9100 chipsets");
369 MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
370 MODULE_VERSION("2.0");
371 MODULE_LICENSE("GPL");
372