1/*
2 *	linux/drivers/video/pmag-aa-fb.c
3 *	Copyright 2002 Karsten Merker <merker@debian.org>
4 *
5 *	PMAG-AA TurboChannel framebuffer card support ... derived from
6 *	pmag-ba-fb.c, which is Copyright (C) 1999, 2000, 2001 by
7 *	Michael Engel <engel@unix-ag.org>, Karsten Merker <merker@debian.org>
8 *	and Harald Koerfgen <hkoerfg@web.de>, which itself is derived from
9 *	"HP300 Topcat framebuffer support (derived from macfb of all things)
10 *	Phil Blundell <philb@gnu.org> 1998"
11 *
12 *	This file is subject to the terms and conditions of the GNU General
13 *	Public License.  See the file COPYING in the main directory of this
14 *	archive for more details.
15 *
16 *	2002-09-28  Karsten Merker <merker@linuxtag.org>
17 *		Version 0.01: First try to get a PMAG-AA running.
18 *
19 *	2003-02-24  Thiemo Seufer  <seufer@csv.ica.uni-stuttgart.de>
20 *		Version 0.02: Major code cleanup.
21 *
22 *	2003-09-21  Thiemo Seufer  <seufer@csv.ica.uni-stuttgart.de>
23 *		Hardware cursor support.
24 */
25#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/errno.h>
28#include <linux/string.h>
29#include <linux/timer.h>
30#include <linux/mm.h>
31#include <linux/delay.h>
32#include <linux/init.h>
33#include <linux/fb.h>
34#include <linux/console.h>
35
36#include <asm/bootinfo.h>
37#include <asm/dec/machtype.h>
38#include <asm/dec/tc.h>
39
40#include <video/fbcon.h>
41#include <video/fbcon-cfb8.h>
42
43#include "bt455.h"
44#include "bt431.h"
45
46/* Version information */
47#define DRIVER_VERSION "0.02"
48#define DRIVER_AUTHOR "Karsten Merker <merker@linuxtag.org>"
49#define DRIVER_DESCRIPTION "PMAG-AA Framebuffer Driver"
50
51/* Prototypes */
52static int aafb_set_var(struct fb_var_screeninfo *var, int con,
53			struct fb_info *info);
54
55/*
56 * Bt455 RAM DAC register base offset (rel. to TC slot base address).
57 */
58#define PMAG_AA_BT455_OFFSET		0x100000
59
60/*
61 * Bt431 cursor generator offset (rel. to TC slot base address).
62 */
63#define PMAG_AA_BT431_OFFSET		0x180000
64
65/*
66 * Begin of PMAG-AA framebuffer memory relative to TC slot address,
67 * resolution is 1280x1024x1 (8 bits deep, but only LSB is used).
68 */
69#define PMAG_AA_ONBOARD_FBMEM_OFFSET	0x200000
70
71struct aafb_cursor {
72	struct timer_list timer;
73	int enable;
74	int on;
75	int vbl_cnt;
76	int blink_rate;
77	u16 x, y, width, height;
78};
79
80#define CURSOR_TIMER_FREQ	(HZ / 50)
81#define CURSOR_BLINK_RATE	(20)
82#define CURSOR_DRAW_DELAY	(2)
83
84struct aafb_info {
85	struct fb_info info;
86	struct display disp;
87	struct aafb_cursor cursor;
88	struct bt455_regs *bt455;
89	struct bt431_regs *bt431;
90	unsigned long fb_start;
91	unsigned long fb_size;
92	unsigned long fb_line_length;
93};
94
95/*
96 * Max 3 TURBOchannel slots -> max 3 PMAG-AA.
97 */
98static struct aafb_info my_fb_info[3];
99
100static struct aafb_par {
101} current_par;
102
103static int currcon = -1;
104
105static void aafb_set_cursor(struct aafb_info *info, int on)
106{
107	struct aafb_cursor *c = &info->cursor;
108
109	if (on) {
110		bt431_position_cursor(info->bt431, c->x, c->y);
111		bt431_enable_cursor(info->bt431);
112	} else
113		bt431_erase_cursor(info->bt431);
114}
115
116static void aafbcon_cursor(struct display *disp, int mode, int x, int y)
117{
118	struct aafb_info *info = (struct aafb_info *)disp->fb_info;
119	struct aafb_cursor *c = &info->cursor;
120
121	x *= fontwidth(disp);
122	y *= fontheight(disp);
123
124	if (c->x == x && c->y == y && (mode == CM_ERASE) == !c->enable)
125		return;
126
127	c->enable = 0;
128	if (c->on)
129		aafb_set_cursor(info, 0);
130	c->x = x - disp->var.xoffset;
131	c->y = y - disp->var.yoffset;
132
133	switch (mode) {
134		case CM_ERASE:
135			c->on = 0;
136			break;
137		case CM_DRAW:
138		case CM_MOVE:
139			if (c->on)
140				aafb_set_cursor(info, c->on);
141			else
142				c->vbl_cnt = CURSOR_DRAW_DELAY;
143			c->enable = 1;
144			break;
145	}
146}
147
148static int aafbcon_set_font(struct display *disp, int width, int height)
149{
150	struct aafb_info *info = (struct aafb_info *)disp->fb_info;
151	struct aafb_cursor *c = &info->cursor;
152	u8 fgc = ~attr_bgcol_ec(disp, disp->conp, &info->info);
153
154	if (width > 64 || height > 64 || width < 0 || height < 0)
155		return -EINVAL;
156
157	c->height = height;
158	c->width = width;
159
160	bt431_set_font(info->bt431, fgc, width, height);
161
162	return 1;
163}
164
165static void aafb_cursor_timer_handler(unsigned long data)
166{
167	struct aafb_info *info = (struct aafb_info *)data;
168	struct aafb_cursor *c = &info->cursor;
169
170	if (!c->enable)
171		goto out;
172
173	if (c->vbl_cnt && --c->vbl_cnt == 0) {
174		c->on ^= 1;
175		aafb_set_cursor(info, c->on);
176		c->vbl_cnt = c->blink_rate;
177	}
178
179out:
180	c->timer.expires = jiffies + CURSOR_TIMER_FREQ;
181	add_timer(&c->timer);
182}
183
184static void __init aafb_cursor_init(struct aafb_info *info)
185{
186	struct aafb_cursor *c = &info->cursor;
187
188	c->enable = 1;
189	c->on = 1;
190	c->x = c->y = 0;
191	c->width = c->height = 0;
192	c->vbl_cnt = CURSOR_DRAW_DELAY;
193	c->blink_rate = CURSOR_BLINK_RATE;
194
195	init_timer(&c->timer);
196	c->timer.data = (unsigned long)info;
197	c->timer.function = aafb_cursor_timer_handler;
198	mod_timer(&c->timer, jiffies + CURSOR_TIMER_FREQ);
199}
200
201static void __exit aafb_cursor_exit(struct aafb_info *info)
202{
203	struct aafb_cursor *c = &info->cursor;
204
205	del_timer_sync(&c->timer);
206}
207
208static struct display_switch aafb_switch8 = {
209	.setup = fbcon_cfb8_setup,
210	.bmove = fbcon_cfb8_bmove,
211	.clear = fbcon_cfb8_clear,
212	.putc = fbcon_cfb8_putc,
213	.putcs = fbcon_cfb8_putcs,
214	.revc = fbcon_cfb8_revc,
215	.cursor = aafbcon_cursor,
216	.set_font = aafbcon_set_font,
217	.clear_margins = fbcon_cfb8_clear_margins,
218	.fontwidthmask = FONTWIDTH(4)|FONTWIDTH(8)|FONTWIDTH(12)|FONTWIDTH(16)
219};
220
221static void aafb_get_par(struct aafb_par *par)
222{
223	*par = current_par;
224}
225
226static int aafb_get_fix(struct fb_fix_screeninfo *fix, int con,
227			struct fb_info *info)
228{
229	struct aafb_info *ip = (struct aafb_info *)info;
230
231	memset(fix, 0, sizeof(struct fb_fix_screeninfo));
232	strcpy(fix->id, "PMAG-AA");
233	fix->smem_start = ip->fb_start;
234	fix->smem_len = ip->fb_size;
235	fix->type = FB_TYPE_PACKED_PIXELS;
236	fix->ypanstep = 1;
237	fix->ywrapstep = 1;
238	fix->visual = FB_VISUAL_MONO10;
239	fix->line_length = 1280;
240	fix->accel = FB_ACCEL_NONE;
241
242	return 0;
243}
244
245static void aafb_set_disp(struct display *disp, int con,
246			  struct aafb_info *info)
247{
248	struct fb_fix_screeninfo fix;
249
250	disp->fb_info = &info->info;
251	aafb_set_var(&disp->var, con, &info->info);
252	if (disp->conp && disp->conp->vc_sw && disp->conp->vc_sw->con_cursor)
253		disp->conp->vc_sw->con_cursor(disp->conp, CM_ERASE);
254	disp->dispsw = &aafb_switch8;
255	disp->dispsw_data = 0;
256
257	aafb_get_fix(&fix, con, &info->info);
258	disp->screen_base = (u8 *) fix.smem_start;
259	disp->visual = fix.visual;
260	disp->type = fix.type;
261	disp->type_aux = fix.type_aux;
262	disp->ypanstep = fix.ypanstep;
263	disp->ywrapstep = fix.ywrapstep;
264	disp->line_length = fix.line_length;
265	disp->next_line = 2048;
266	disp->can_soft_blank = 1;
267	disp->inverse = 0;
268	disp->scrollmode = SCROLL_YREDRAW;
269
270	aafbcon_set_font(disp, fontwidth(disp), fontheight(disp));
271}
272
273static int aafb_get_cmap(struct fb_cmap *cmap, int kspc, int con,
274			 struct fb_info *info)
275{
276	static u16 color[2] = {0x0000, 0x000f};
277	static struct fb_cmap aafb_cmap = {0, 2, color, color, color, NULL};
278
279	fb_copy_cmap(&aafb_cmap, cmap, kspc ? 0 : 2);
280	return 0;
281}
282
283static int aafb_set_cmap(struct fb_cmap *cmap, int kspc, int con,
284			 struct fb_info *info)
285{
286	u16 color[2] = {0x0000, 0x000f};
287
288	if (cmap->start == 0
289	    && cmap->len == 2
290	    && memcmp(cmap->red, color, sizeof(color)) == 0
291	    && memcmp(cmap->green, color, sizeof(color)) == 0
292	    && memcmp(cmap->blue, color, sizeof(color)) == 0
293	    && cmap->transp == NULL)
294		return 0;
295	else
296		return -EINVAL;
297}
298
299static int aafb_ioctl(struct fb_info *info, u32 cmd, unsigned long arg)
300{
301	/* TODO: Not yet implemented */
302	return -ENOIOCTLCMD;
303}
304
305static int aafb_switch(int con, struct fb_info *info)
306{
307	struct aafb_info *ip = (struct aafb_info *)info;
308	struct display *old = (currcon < 0) ? &ip->disp : (fb_display + currcon);
309	struct display *new = (con < 0) ? &ip->disp : (fb_display + con);
310
311	if (old->conp && old->conp->vc_sw && old->conp->vc_sw->con_cursor)
312		old->conp->vc_sw->con_cursor(old->conp, CM_ERASE);
313
314	/* Set the current console. */
315	currcon = con;
316	aafb_set_disp(new, con, ip);
317
318	return 0;
319}
320
321static void aafb_encode_var(struct fb_var_screeninfo *var,
322			    struct aafb_par *par)
323{
324	var->xres = 1280;
325	var->yres = 1024;
326	var->xres_virtual = 2048;
327	var->yres_virtual = 1024;
328	var->xoffset = 0;
329	var->yoffset = 0;
330	var->bits_per_pixel = 8;
331	var->grayscale = 1;
332	var->red.offset = 0;
333	var->red.length = 0;
334	var->red.msb_right = 0;
335	var->green.offset = 0;
336	var->green.length = 1;
337	var->green.msb_right = 0;
338	var->blue.offset = 0;
339	var->blue.length = 0;
340	var->blue.msb_right = 0;
341	var->transp.offset = 0;
342	var->transp.length = 0;
343	var->transp.msb_right = 0;
344	var->nonstd = 0;
345	var->activate &= ~FB_ACTIVATE_MASK & FB_ACTIVATE_NOW;
346	var->accel_flags = 0;
347	var->sync = FB_SYNC_ON_GREEN;
348	var->vmode &= ~FB_VMODE_MASK & FB_VMODE_NONINTERLACED;
349}
350
351static int aafb_get_var(struct fb_var_screeninfo *var, int con,
352			struct fb_info *info)
353{
354	if (con < 0) {
355		struct aafb_par par;
356
357		memset(var, 0, sizeof(struct fb_var_screeninfo));
358		aafb_get_par(&par);
359		aafb_encode_var(var, &par);
360	} else
361		*var = info->var;
362
363	return 0;
364}
365
366static int aafb_set_var(struct fb_var_screeninfo *var, int con,
367			struct fb_info *info)
368{
369	struct aafb_par par;
370
371	aafb_get_par(&par);
372	aafb_encode_var(var, &par);
373	info->var = *var;
374
375	return 0;
376}
377
378static int aafb_update_var(int con, struct fb_info *info)
379{
380	struct aafb_info *ip = (struct aafb_info *)info;
381	struct display *disp = (con < 0) ? &ip->disp : (fb_display + con);
382
383	if (con == currcon)
384		aafbcon_cursor(disp, CM_ERASE, ip->cursor.x, ip->cursor.y);
385
386	return 0;
387}
388
389/* 0 unblanks, any other blanks. */
390
391static void aafb_blank(int blank, struct fb_info *info)
392{
393	struct aafb_info *ip = (struct aafb_info *)info;
394	u8 val = blank ? 0x00 : 0x0f;
395
396	bt455_write_cmap_entry(ip->bt455, 1, val, val, val);
397	aafbcon_cursor(&ip->disp, CM_ERASE, ip->cursor.x, ip->cursor.y);
398}
399
400static struct fb_ops aafb_ops = {
401	.owner = THIS_MODULE,
402	.fb_get_fix = aafb_get_fix,
403	.fb_get_var = aafb_get_var,
404	.fb_set_var = aafb_set_var,
405	.fb_get_cmap = aafb_get_cmap,
406	.fb_set_cmap = aafb_set_cmap,
407	.fb_ioctl = aafb_ioctl
408};
409
410static int __init init_one(int slot)
411{
412	unsigned long base_addr = CKSEG1ADDR(get_tc_base_addr(slot));
413	struct aafb_info *ip = &my_fb_info[slot];
414
415	memset(ip, 0, sizeof(struct aafb_info));
416
417	/*
418	 * Framebuffer display memory base address and friends.
419	 */
420	ip->bt455 = (struct bt455_regs *) (base_addr + PMAG_AA_BT455_OFFSET);
421	ip->bt431 = (struct bt431_regs *) (base_addr + PMAG_AA_BT431_OFFSET);
422	ip->fb_start = base_addr + PMAG_AA_ONBOARD_FBMEM_OFFSET;
423	ip->fb_size = 2048 * 1024; /* fb_fix_screeninfo.smem_length
424				      seems to be physical */
425	ip->fb_line_length = 2048;
426
427	/*
428	 * Let there be consoles..
429	 */
430	strcpy(ip->info.modename, "PMAG-AA");
431	ip->info.node = -1;
432	ip->info.flags = FBINFO_FLAG_DEFAULT;
433	ip->info.fbops = &aafb_ops;
434	ip->info.disp = &ip->disp;
435	ip->info.changevar = NULL;
436	ip->info.switch_con = &aafb_switch;
437	ip->info.updatevar = &aafb_update_var;
438	ip->info.blank = &aafb_blank;
439
440	aafb_set_disp(&ip->disp, currcon, ip);
441
442	/*
443	 * Configure the RAM DACs.
444	 */
445	bt455_erase_cursor(ip->bt455);
446
447	/* Init colormap. */
448	bt455_write_cmap_entry(ip->bt455, 0, 0x00, 0x00, 0x00);
449	bt455_write_cmap_entry(ip->bt455, 1, 0x0f, 0x0f, 0x0f);
450
451	/* Init hardware cursor. */
452	bt431_init_cursor(ip->bt431);
453	aafb_cursor_init(ip);
454
455	/* Clear the screen. */
456	memset ((void *)ip->fb_start, 0, ip->fb_size);
457
458	if (register_framebuffer(&ip->info) < 0)
459		return -EINVAL;
460
461	printk(KERN_INFO "fb%d: %s frame buffer in TC slot %d\n",
462	       GET_FB_IDX(ip->info.node), ip->info.modename, slot);
463
464	return 0;
465}
466
467static int __exit exit_one(int slot)
468{
469	struct aafb_info *ip = &my_fb_info[slot];
470
471	if (unregister_framebuffer(&ip->info) < 0)
472		return -EINVAL;
473
474	return 0;
475}
476
477/*
478 * Initialise the framebuffer.
479 */
480int __init pmagaafb_init(void)
481{
482	int sid;
483	int found = 0;
484
485	while ((sid = search_tc_card("PMAG-AA")) >= 0) {
486		found = 1;
487		claim_tc_card(sid);
488		init_one(sid);
489	}
490
491	return found ? 0 : -ENXIO;
492}
493
494static void __exit pmagaafb_exit(void)
495{
496	int sid;
497
498	while ((sid = search_tc_card("PMAG-AA")) >= 0) {
499		exit_one(sid);
500		release_tc_card(sid);
501	}
502}
503
504MODULE_AUTHOR(DRIVER_AUTHOR);
505MODULE_DESCRIPTION(DRIVER_DESCRIPTION);
506MODULE_LICENSE("GPL");
507#ifdef MODULE
508module_init(pmagaafb_init);
509module_exit(pmagaafb_exit);
510#endif
511