1/*
2 *	linux/drivers/video/pmag-ba-fb.c
3 *
4 *	PMAG-BA TURBOchannel Color Frame Buffer (CFB) card support,
5 *	derived from:
6 *	"HP300 Topcat framebuffer support (derived from macfb of all things)
7 *	Phil Blundell <philb@gnu.org> 1998", the original code can be
8 *	found in the file hpfb.c in the same directory.
9 *
10 *	Based on digital document:
11 * 	"PMAG-BA TURBOchannel Color Frame Buffer
12 *	 Functional Specification", Revision 1.2, August 27, 1990
13 *
14 *	DECstation related code Copyright (C) 1999, 2000, 2001 by
15 *	Michael Engel <engel@unix-ag.org>,
16 *	Karsten Merker <merker@linuxtag.org> and
17 *	Harald Koerfgen.
18 *	Copyright (c) 2005, 2006  Maciej W. Rozycki
19 *	Copyright (c) 2005  James Simmons
20 *
21 *	This file is subject to the terms and conditions of the GNU General
22 *	Public License.  See the file COPYING in the main directory of this
23 *	archive for more details.
24 */
25
26#include <linux/compiler.h>
27#include <linux/errno.h>
28#include <linux/fb.h>
29#include <linux/init.h>
30#include <linux/kernel.h>
31#include <linux/module.h>
32#include <linux/tc.h>
33#include <linux/types.h>
34
35#include <asm/io.h>
36
37#include <video/pmag-ba-fb.h>
38
39
40struct pmagbafb_par {
41	volatile void __iomem *mmio;
42	volatile u32 __iomem *dac;
43};
44
45
46static const struct fb_var_screeninfo pmagbafb_defined = {
47	.xres		= 1024,
48	.yres		= 864,
49	.xres_virtual	= 1024,
50	.yres_virtual	= 864,
51	.bits_per_pixel	= 8,
52	.red.length	= 8,
53	.green.length	= 8,
54	.blue.length	= 8,
55	.activate	= FB_ACTIVATE_NOW,
56	.height		= -1,
57	.width		= -1,
58	.accel_flags	= FB_ACCEL_NONE,
59	.pixclock	= 14452,
60	.left_margin	= 116,
61	.right_margin	= 12,
62	.upper_margin	= 34,
63	.lower_margin	= 0,
64	.hsync_len	= 128,
65	.vsync_len	= 3,
66	.sync		= FB_SYNC_ON_GREEN,
67	.vmode		= FB_VMODE_NONINTERLACED,
68};
69
70static const struct fb_fix_screeninfo pmagbafb_fix = {
71	.id		= "PMAG-BA",
72	.smem_len	= (1024 * 1024),
73	.type		= FB_TYPE_PACKED_PIXELS,
74	.visual		= FB_VISUAL_PSEUDOCOLOR,
75	.line_length	= 1024,
76	.mmio_len	= PMAG_BA_SIZE - PMAG_BA_BT459,
77};
78
79
80static inline void dac_write(struct pmagbafb_par *par, unsigned int reg, u8 v)
81{
82	writeb(v, par->dac + reg / 4);
83}
84
85static inline u8 dac_read(struct pmagbafb_par *par, unsigned int reg)
86{
87	return readb(par->dac + reg / 4);
88}
89
90
91/*
92 * Set the palette.
93 */
94static int pmagbafb_setcolreg(unsigned int regno, unsigned int red,
95			      unsigned int green, unsigned int blue,
96			      unsigned int transp, struct fb_info *info)
97{
98	struct pmagbafb_par *par = info->par;
99
100	if (regno >= info->cmap.len)
101		return 1;
102
103	red   >>= 8;	/* The cmap fields are 16 bits    */
104	green >>= 8;	/* wide, but the hardware colormap */
105	blue  >>= 8;	/* registers are only 8 bits wide */
106
107	mb();
108	dac_write(par, BT459_ADDR_LO, regno);
109	dac_write(par, BT459_ADDR_HI, 0x00);
110	wmb();
111	dac_write(par, BT459_CMAP, red);
112	wmb();
113	dac_write(par, BT459_CMAP, green);
114	wmb();
115	dac_write(par, BT459_CMAP, blue);
116
117	return 0;
118}
119
120static const struct fb_ops pmagbafb_ops = {
121	.owner		= THIS_MODULE,
122	FB_DEFAULT_IOMEM_OPS,
123	.fb_setcolreg	= pmagbafb_setcolreg,
124};
125
126
127/*
128 * Turn the hardware cursor off.
129 */
130static void pmagbafb_erase_cursor(struct fb_info *info)
131{
132	struct pmagbafb_par *par = info->par;
133
134	mb();
135	dac_write(par, BT459_ADDR_LO, 0x00);
136	dac_write(par, BT459_ADDR_HI, 0x03);
137	wmb();
138	dac_write(par, BT459_DATA, 0x00);
139}
140
141
142static int pmagbafb_probe(struct device *dev)
143{
144	struct tc_dev *tdev = to_tc_dev(dev);
145	resource_size_t start, len;
146	struct fb_info *info;
147	struct pmagbafb_par *par;
148	int err;
149
150	info = framebuffer_alloc(sizeof(struct pmagbafb_par), dev);
151	if (!info)
152		return -ENOMEM;
153
154	par = info->par;
155	dev_set_drvdata(dev, info);
156
157	if (fb_alloc_cmap(&info->cmap, 256, 0) < 0) {
158		printk(KERN_ERR "%s: Cannot allocate color map\n",
159		       dev_name(dev));
160		err = -ENOMEM;
161		goto err_alloc;
162	}
163
164	info->fbops = &pmagbafb_ops;
165	info->fix = pmagbafb_fix;
166	info->var = pmagbafb_defined;
167
168	/* Request the I/O MEM resource.  */
169	start = tdev->resource.start;
170	len = tdev->resource.end - start + 1;
171	if (!request_mem_region(start, len, dev_name(dev))) {
172		printk(KERN_ERR "%s: Cannot reserve FB region\n",
173		       dev_name(dev));
174		err = -EBUSY;
175		goto err_cmap;
176	}
177
178	/* MMIO mapping setup.  */
179	info->fix.mmio_start = start;
180	par->mmio = ioremap(info->fix.mmio_start, info->fix.mmio_len);
181	if (!par->mmio) {
182		printk(KERN_ERR "%s: Cannot map MMIO\n", dev_name(dev));
183		err = -ENOMEM;
184		goto err_resource;
185	}
186	par->dac = par->mmio + PMAG_BA_BT459;
187
188	/* Frame buffer mapping setup.  */
189	info->fix.smem_start = start + PMAG_BA_FBMEM;
190	info->screen_base = ioremap(info->fix.smem_start,
191					    info->fix.smem_len);
192	if (!info->screen_base) {
193		printk(KERN_ERR "%s: Cannot map FB\n", dev_name(dev));
194		err = -ENOMEM;
195		goto err_mmio_map;
196	}
197	info->screen_size = info->fix.smem_len;
198
199	pmagbafb_erase_cursor(info);
200
201	err = register_framebuffer(info);
202	if (err < 0) {
203		printk(KERN_ERR "%s: Cannot register framebuffer\n",
204		       dev_name(dev));
205		goto err_smem_map;
206	}
207
208	get_device(dev);
209
210	fb_info(info, "%s frame buffer device at %s\n",
211		info->fix.id, dev_name(dev));
212
213	return 0;
214
215
216err_smem_map:
217	iounmap(info->screen_base);
218
219err_mmio_map:
220	iounmap(par->mmio);
221
222err_resource:
223	release_mem_region(start, len);
224
225err_cmap:
226	fb_dealloc_cmap(&info->cmap);
227
228err_alloc:
229	framebuffer_release(info);
230	return err;
231}
232
233static int pmagbafb_remove(struct device *dev)
234{
235	struct tc_dev *tdev = to_tc_dev(dev);
236	struct fb_info *info = dev_get_drvdata(dev);
237	struct pmagbafb_par *par = info->par;
238	resource_size_t start, len;
239
240	put_device(dev);
241	unregister_framebuffer(info);
242	iounmap(info->screen_base);
243	iounmap(par->mmio);
244	start = tdev->resource.start;
245	len = tdev->resource.end - start + 1;
246	release_mem_region(start, len);
247	fb_dealloc_cmap(&info->cmap);
248	framebuffer_release(info);
249	return 0;
250}
251
252
253/*
254 * Initialize the framebuffer.
255 */
256static const struct tc_device_id pmagbafb_tc_table[] = {
257	{ "DEC     ", "PMAG-BA " },
258	{ }
259};
260MODULE_DEVICE_TABLE(tc, pmagbafb_tc_table);
261
262static struct tc_driver pmagbafb_driver = {
263	.id_table	= pmagbafb_tc_table,
264	.driver		= {
265		.name	= "pmagbafb",
266		.bus	= &tc_bus_type,
267		.probe	= pmagbafb_probe,
268		.remove	= pmagbafb_remove,
269	},
270};
271
272static int __init pmagbafb_init(void)
273{
274#ifndef MODULE
275	if (fb_get_options("pmagbafb", NULL))
276		return -ENXIO;
277#endif
278	return tc_register_driver(&pmagbafb_driver);
279}
280
281static void __exit pmagbafb_exit(void)
282{
283	tc_unregister_driver(&pmagbafb_driver);
284}
285
286
287module_init(pmagbafb_init);
288module_exit(pmagbafb_exit);
289
290MODULE_LICENSE("GPL");
291