xref: /linux/drivers/video/fbdev/bw2.c (revision 89755ee1d59311855b4afcf35aebddcb653b3cd5)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* bw2.c: BWTWO frame buffer driver
3  *
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) 1996 Miguel de Icaza (miguel@nuclecu.unam.mx)
7  * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
8  *
9  * Driver layout based loosely on tgafb.c, see that file for credits.
10  */
11 
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/string.h>
16 #include <linux/delay.h>
17 #include <linux/init.h>
18 #include <linux/fb.h>
19 #include <linux/mm.h>
20 #include <linux/of_device.h>
21 
22 #include <asm/io.h>
23 #include <asm/fbio.h>
24 
25 #include "sbuslib.h"
26 
27 /*
28  * Local functions.
29  */
30 
31 static int bw2_blank(int, struct fb_info *);
32 
33 static int bw2_sbusfb_mmap(struct fb_info *info, struct vm_area_struct *vma);
34 static int bw2_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 bw2_ops = {
41 	.owner			= THIS_MODULE,
42 	FB_DEFAULT_SBUS_OPS(bw2),
43 	.fb_blank		= bw2_blank,
44 };
45 
46 /* OBio addresses for the bwtwo registers */
47 #define BWTWO_REGISTER_OFFSET 0x400000
48 
49 struct bt_regs {
50 	u32 addr;
51 	u32 color_map;
52 	u32 control;
53 	u32 cursor;
54 };
55 
56 struct bw2_regs {
57 	struct bt_regs	cmap;
58 	u8	control;
59 	u8	status;
60 	u8	cursor_start;
61 	u8	cursor_end;
62 	u8	h_blank_start;
63 	u8	h_blank_end;
64 	u8	h_sync_start;
65 	u8	h_sync_end;
66 	u8	comp_sync_end;
67 	u8	v_blank_start_high;
68 	u8	v_blank_start_low;
69 	u8	v_blank_end;
70 	u8	v_sync_start;
71 	u8	v_sync_end;
72 	u8	xfer_holdoff_start;
73 	u8	xfer_holdoff_end;
74 };
75 
76 /* Status Register Constants */
77 #define BWTWO_SR_RES_MASK	0x70
78 #define BWTWO_SR_1600_1280	0x50
79 #define BWTWO_SR_1152_900_76_A	0x40
80 #define BWTWO_SR_1152_900_76_B	0x60
81 #define BWTWO_SR_ID_MASK	0x0f
82 #define BWTWO_SR_ID_MONO	0x02
83 #define BWTWO_SR_ID_MONO_ECL	0x03
84 #define BWTWO_SR_ID_MSYNC	0x04
85 #define BWTWO_SR_ID_NOCONN	0x0a
86 
87 /* Control Register Constants */
88 #define BWTWO_CTL_ENABLE_INTS   0x80
89 #define BWTWO_CTL_ENABLE_VIDEO  0x40
90 #define BWTWO_CTL_ENABLE_TIMING 0x20
91 #define BWTWO_CTL_ENABLE_CURCMP 0x10
92 #define BWTWO_CTL_XTAL_MASK     0x0C
93 #define BWTWO_CTL_DIVISOR_MASK  0x03
94 
95 /* Status Register Constants */
96 #define BWTWO_STAT_PENDING_INT  0x80
97 #define BWTWO_STAT_MSENSE_MASK  0x70
98 #define BWTWO_STAT_ID_MASK      0x0f
99 
100 struct bw2_par {
101 	spinlock_t		lock;
102 	struct bw2_regs		__iomem *regs;
103 
104 	u32			flags;
105 #define BW2_FLAG_BLANKED	0x00000001
106 
107 	unsigned long		which_io;
108 };
109 
110 /**
111  *      bw2_blank - Optional function.  Blanks the display.
112  *      @blank: the blank mode we want.
113  *      @info: frame buffer structure that represents a single frame buffer
114  */
115 static int
116 bw2_blank(int blank, struct fb_info *info)
117 {
118 	struct bw2_par *par = (struct bw2_par *) info->par;
119 	struct bw2_regs __iomem *regs = par->regs;
120 	unsigned long flags;
121 	u8 val;
122 
123 	spin_lock_irqsave(&par->lock, flags);
124 
125 	switch (blank) {
126 	case FB_BLANK_UNBLANK: /* Unblanking */
127 		val = sbus_readb(&regs->control);
128 		val |= BWTWO_CTL_ENABLE_VIDEO;
129 		sbus_writeb(val, &regs->control);
130 		par->flags &= ~BW2_FLAG_BLANKED;
131 		break;
132 
133 	case FB_BLANK_NORMAL: /* Normal blanking */
134 	case FB_BLANK_VSYNC_SUSPEND: /* VESA blank (vsync off) */
135 	case FB_BLANK_HSYNC_SUSPEND: /* VESA blank (hsync off) */
136 	case FB_BLANK_POWERDOWN: /* Poweroff */
137 		val = sbus_readb(&regs->control);
138 		val &= ~BWTWO_CTL_ENABLE_VIDEO;
139 		sbus_writeb(val, &regs->control);
140 		par->flags |= BW2_FLAG_BLANKED;
141 		break;
142 	}
143 
144 	spin_unlock_irqrestore(&par->lock, flags);
145 
146 	return 0;
147 }
148 
149 static struct sbus_mmap_map bw2_mmap_map[] = {
150 	{
151 		.size = SBUS_MMAP_FBSIZE(1)
152 	},
153 	{ .size = 0 }
154 };
155 
156 static int bw2_sbusfb_mmap(struct fb_info *info, struct vm_area_struct *vma)
157 {
158 	struct bw2_par *par = (struct bw2_par *)info->par;
159 
160 	return sbusfb_mmap_helper(bw2_mmap_map,
161 				  info->fix.smem_start, info->fix.smem_len,
162 				  par->which_io,
163 				  vma);
164 }
165 
166 static int bw2_sbusfb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
167 {
168 	return sbusfb_ioctl_helper(cmd, arg, info,
169 				   FBTYPE_SUN2BW, 1, info->fix.smem_len);
170 }
171 
172 /*
173  *  Initialisation
174  */
175 
176 static void bw2_init_fix(struct fb_info *info, int linebytes)
177 {
178 	strscpy(info->fix.id, "bwtwo", sizeof(info->fix.id));
179 
180 	info->fix.type = FB_TYPE_PACKED_PIXELS;
181 	info->fix.visual = FB_VISUAL_MONO01;
182 
183 	info->fix.line_length = linebytes;
184 
185 	info->fix.accel = FB_ACCEL_SUN_BWTWO;
186 }
187 
188 static u8 bw2regs_1600[] = {
189 	0x14, 0x8b,	0x15, 0x28,	0x16, 0x03,	0x17, 0x13,
190 	0x18, 0x7b,	0x19, 0x05,	0x1a, 0x34,	0x1b, 0x2e,
191 	0x1c, 0x00,	0x1d, 0x0a,	0x1e, 0xff,	0x1f, 0x01,
192 	0x10, 0x21,	0
193 };
194 
195 static u8 bw2regs_ecl[] = {
196 	0x14, 0x65,	0x15, 0x1e,	0x16, 0x04,	0x17, 0x0c,
197 	0x18, 0x5e,	0x19, 0x03,	0x1a, 0xa7,	0x1b, 0x23,
198 	0x1c, 0x00,	0x1d, 0x08,	0x1e, 0xff,	0x1f, 0x01,
199 	0x10, 0x20,	0
200 };
201 
202 static u8 bw2regs_analog[] = {
203 	0x14, 0xbb,	0x15, 0x2b,	0x16, 0x03,	0x17, 0x13,
204 	0x18, 0xb0,	0x19, 0x03,	0x1a, 0xa6,	0x1b, 0x22,
205 	0x1c, 0x01,	0x1d, 0x05,	0x1e, 0xff,	0x1f, 0x01,
206 	0x10, 0x20,	0
207 };
208 
209 static u8 bw2regs_76hz[] = {
210 	0x14, 0xb7,	0x15, 0x27,	0x16, 0x03,	0x17, 0x0f,
211 	0x18, 0xae,	0x19, 0x03,	0x1a, 0xae,	0x1b, 0x2a,
212 	0x1c, 0x01,	0x1d, 0x09,	0x1e, 0xff,	0x1f, 0x01,
213 	0x10, 0x24,	0
214 };
215 
216 static u8 bw2regs_66hz[] = {
217 	0x14, 0xbb,	0x15, 0x2b,	0x16, 0x04,	0x17, 0x14,
218 	0x18, 0xae,	0x19, 0x03,	0x1a, 0xa8,	0x1b, 0x24,
219 	0x1c, 0x01,	0x1d, 0x05,	0x1e, 0xff,	0x1f, 0x01,
220 	0x10, 0x20,	0
221 };
222 
223 static int bw2_do_default_mode(struct bw2_par *par, struct fb_info *info,
224 			       int *linebytes)
225 {
226 	u8 status, mon;
227 	u8 *p;
228 
229 	status = sbus_readb(&par->regs->status);
230 	mon = status & BWTWO_SR_RES_MASK;
231 	switch (status & BWTWO_SR_ID_MASK) {
232 	case BWTWO_SR_ID_MONO_ECL:
233 		if (mon == BWTWO_SR_1600_1280) {
234 			p = bw2regs_1600;
235 			info->var.xres = info->var.xres_virtual = 1600;
236 			info->var.yres = info->var.yres_virtual = 1280;
237 			*linebytes = 1600 / 8;
238 		} else
239 			p = bw2regs_ecl;
240 		break;
241 
242 	case BWTWO_SR_ID_MONO:
243 		p = bw2regs_analog;
244 		break;
245 
246 	case BWTWO_SR_ID_MSYNC:
247 		if (mon == BWTWO_SR_1152_900_76_A ||
248 		    mon == BWTWO_SR_1152_900_76_B)
249 			p = bw2regs_76hz;
250 		else
251 			p = bw2regs_66hz;
252 		break;
253 
254 	case BWTWO_SR_ID_NOCONN:
255 		return 0;
256 
257 	default:
258 		printk(KERN_ERR "bw2: can't handle SR %02x\n",
259 		       status);
260 		return -EINVAL;
261 	}
262 	for ( ; *p; p += 2) {
263 		u8 __iomem *regp = &((u8 __iomem *)par->regs)[p[0]];
264 		sbus_writeb(p[1], regp);
265 	}
266 	return 0;
267 }
268 
269 static int bw2_probe(struct platform_device *op)
270 {
271 	struct device_node *dp = op->dev.of_node;
272 	struct fb_info *info;
273 	struct bw2_par *par;
274 	int linebytes, err;
275 
276 	info = framebuffer_alloc(sizeof(struct bw2_par), &op->dev);
277 
278 	err = -ENOMEM;
279 	if (!info)
280 		goto out_err;
281 	par = info->par;
282 
283 	spin_lock_init(&par->lock);
284 
285 	info->fix.smem_start = op->resource[0].start;
286 	par->which_io = op->resource[0].flags & IORESOURCE_BITS;
287 
288 	sbusfb_fill_var(&info->var, dp, 1);
289 	linebytes = of_getintprop_default(dp, "linebytes",
290 					  info->var.xres);
291 
292 	info->var.red.length = info->var.green.length =
293 		info->var.blue.length = info->var.bits_per_pixel;
294 	info->var.red.offset = info->var.green.offset =
295 		info->var.blue.offset = 0;
296 
297 	par->regs = of_ioremap(&op->resource[0], BWTWO_REGISTER_OFFSET,
298 			       sizeof(struct bw2_regs), "bw2 regs");
299 	if (!par->regs)
300 		goto out_release_fb;
301 
302 	if (!of_property_present(dp, "width")) {
303 		err = bw2_do_default_mode(par, info, &linebytes);
304 		if (err)
305 			goto out_unmap_regs;
306 	}
307 
308 	info->fix.smem_len = PAGE_ALIGN(linebytes * info->var.yres);
309 
310 	info->fbops = &bw2_ops;
311 
312 	info->screen_base = of_ioremap(&op->resource[0], 0,
313 				       info->fix.smem_len, "bw2 ram");
314 	if (!info->screen_base) {
315 		err = -ENOMEM;
316 		goto out_unmap_regs;
317 	}
318 
319 	bw2_blank(FB_BLANK_UNBLANK, info);
320 
321 	bw2_init_fix(info, linebytes);
322 
323 	err = register_framebuffer(info);
324 	if (err < 0)
325 		goto out_unmap_screen;
326 
327 	dev_set_drvdata(&op->dev, info);
328 
329 	printk(KERN_INFO "%pOF: bwtwo at %lx:%lx\n",
330 	       dp, par->which_io, info->fix.smem_start);
331 
332 	return 0;
333 
334 out_unmap_screen:
335 	of_iounmap(&op->resource[0], info->screen_base, info->fix.smem_len);
336 
337 out_unmap_regs:
338 	of_iounmap(&op->resource[0], par->regs, sizeof(struct bw2_regs));
339 
340 out_release_fb:
341 	framebuffer_release(info);
342 
343 out_err:
344 	return err;
345 }
346 
347 static void bw2_remove(struct platform_device *op)
348 {
349 	struct fb_info *info = dev_get_drvdata(&op->dev);
350 	struct bw2_par *par = info->par;
351 
352 	unregister_framebuffer(info);
353 
354 	of_iounmap(&op->resource[0], par->regs, sizeof(struct bw2_regs));
355 	of_iounmap(&op->resource[0], info->screen_base, info->fix.smem_len);
356 
357 	framebuffer_release(info);
358 }
359 
360 static const struct of_device_id bw2_match[] = {
361 	{
362 		.name = "bwtwo",
363 	},
364 	{},
365 };
366 MODULE_DEVICE_TABLE(of, bw2_match);
367 
368 static struct platform_driver bw2_driver = {
369 	.driver = {
370 		.name = "bw2",
371 		.of_match_table = bw2_match,
372 	},
373 	.probe		= bw2_probe,
374 	.remove_new	= bw2_remove,
375 };
376 
377 static int __init bw2_init(void)
378 {
379 	if (fb_get_options("bw2fb", NULL))
380 		return -ENODEV;
381 
382 	return platform_driver_register(&bw2_driver);
383 }
384 
385 static void __exit bw2_exit(void)
386 {
387 	platform_driver_unregister(&bw2_driver);
388 }
389 
390 module_init(bw2_init);
391 module_exit(bw2_exit);
392 
393 MODULE_DESCRIPTION("framebuffer driver for BWTWO chipsets");
394 MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
395 MODULE_VERSION("2.0");
396 MODULE_LICENSE("GPL");
397