1/*
2 * linux/drivers/video/hgafb.c -- Hercules graphics adaptor frame buffer device
3 *
4 *      Created 25 Nov 1999 by Ferenc Bakonyi (fero@drama.obuda.kando.hu)
5 *      Based on skeletonfb.c by Geert Uytterhoeven and
6 *               mdacon.c by Andrew Apted
7 *
8 * History:
9 *
10 * - Revision 0.1.8 (23 Oct 2002): Ported to new framebuffer api.
11 *
12 * - Revision 0.1.7 (23 Jan 2001): fix crash resulting from MDA only cards
13 *				   being detected as Hercules.	 (Paul G.)
14 * - Revision 0.1.6 (17 Aug 2000): new style structs
15 *                                 documentation
16 * - Revision 0.1.5 (13 Mar 2000): spinlocks instead of saveflags();cli();etc
17 *                                 minor fixes
18 * - Revision 0.1.4 (24 Jan 2000): fixed a bug in hga_card_detect() for
19 *                                  HGA-only systems
20 * - Revision 0.1.3 (22 Jan 2000): modified for the new fb_info structure
21 *                                 screen is cleared after rmmod
22 *                                 virtual resolutions
23 *                                 module parameter 'nologo={0|1}'
24 *                                 the most important: boot logo :)
25 * - Revision 0.1.0  (6 Dec 1999): faster scrolling and minor fixes
26 * - First release  (25 Nov 1999)
27 *
28 * This file is subject to the terms and conditions of the GNU General Public
29 * License.  See the file COPYING in the main directory of this archive
30 * for more details.
31 */
32
33#include <linux/module.h>
34#include <linux/kernel.h>
35#include <linux/errno.h>
36#include <linux/spinlock.h>
37#include <linux/string.h>
38#include <linux/mm.h>
39#include <linux/slab.h>
40#include <linux/delay.h>
41#include <linux/fb.h>
42#include <linux/init.h>
43#include <linux/ioport.h>
44#include <linux/platform_device.h>
45#include <asm/io.h>
46#include <asm/vga.h>
47
48#define DPRINTK(args...)
49
50#define CHKINFO(ret)
51
52/* Description of the hardware layout */
53
54static void __iomem *hga_vram;			/* Base of video memory */
55static unsigned long hga_vram_len;		/* Size of video memory */
56
57#define HGA_ROWADDR(row) ((row%4)*8192 + (row>>2)*90)
58#define HGA_TXT			0
59#define HGA_GFX			1
60
61static inline u8 __iomem * rowaddr(struct fb_info *info, u_int row)
62{
63	return info->screen_base + HGA_ROWADDR(row);
64}
65
66static int hga_mode = -1;			/* 0 = txt, 1 = gfx mode */
67
68static enum { TYPE_HERC, TYPE_HERCPLUS, TYPE_HERCCOLOR } hga_type;
69static char *hga_type_name;
70
71#define HGA_INDEX_PORT		0x3b4		/* Register select port */
72#define HGA_VALUE_PORT		0x3b5		/* Register value port */
73#define HGA_MODE_PORT		0x3b8		/* Mode control port */
74#define HGA_STATUS_PORT		0x3ba		/* Status and Config port */
75#define HGA_GFX_PORT		0x3bf		/* Graphics control port */
76
77/* HGA register values */
78
79#define HGA_CURSOR_BLINKING	0x00
80#define HGA_CURSOR_OFF		0x20
81#define HGA_CURSOR_SLOWBLINK	0x60
82
83#define HGA_MODE_GRAPHICS	0x02
84#define HGA_MODE_VIDEO_EN	0x08
85#define HGA_MODE_BLINK_EN	0x20
86#define HGA_MODE_GFX_PAGE1	0x80
87
88#define HGA_STATUS_HSYNC	0x01
89#define HGA_STATUS_VSYNC	0x80
90#define HGA_STATUS_VIDEO	0x08
91
92#define HGA_CONFIG_COL132	0x08
93#define HGA_GFX_MODE_EN		0x01
94#define HGA_GFX_PAGE_EN		0x02
95
96/* Global locks */
97
98static DEFINE_SPINLOCK(hga_reg_lock);
99
100/* Framebuffer driver structures */
101
102static struct fb_var_screeninfo __initdata hga_default_var = {
103	.xres		= 720,
104	.yres 		= 348,
105	.xres_virtual 	= 720,
106	.yres_virtual	= 348,
107	.bits_per_pixel = 1,
108	.red 		= {0, 1, 0},
109	.green 		= {0, 1, 0},
110	.blue 		= {0, 1, 0},
111	.transp 	= {0, 0, 0},
112	.height 	= -1,
113	.width 		= -1,
114};
115
116static struct fb_fix_screeninfo __initdata hga_fix = {
117	.id 		= "HGA",
118	.type 		= FB_TYPE_PACKED_PIXELS,	/* (not sure) */
119	.visual 	= FB_VISUAL_MONO10,
120	.xpanstep 	= 8,
121	.ypanstep 	= 8,
122	.line_length 	= 90,
123	.accel 		= FB_ACCEL_NONE
124};
125
126/* Don't assume that tty1 will be the initial current console. */
127static int release_io_port = 0;
128static int release_io_ports = 0;
129static int nologo = 0;
130
131/* -------------------------------------------------------------------------
132 *
133 * Low level hardware functions
134 *
135 * ------------------------------------------------------------------------- */
136
137static void write_hga_b(unsigned int val, unsigned char reg)
138{
139	outb_p(reg, HGA_INDEX_PORT);
140	outb_p(val, HGA_VALUE_PORT);
141}
142
143static void write_hga_w(unsigned int val, unsigned char reg)
144{
145	outb_p(reg,   HGA_INDEX_PORT); outb_p(val >> 8,   HGA_VALUE_PORT);
146	outb_p(reg+1, HGA_INDEX_PORT); outb_p(val & 0xff, HGA_VALUE_PORT);
147}
148
149static int test_hga_b(unsigned char val, unsigned char reg)
150{
151	outb_p(reg, HGA_INDEX_PORT);
152	outb  (val, HGA_VALUE_PORT);
153	udelay(20); val = (inb_p(HGA_VALUE_PORT) == val);
154	return val;
155}
156
157static void hga_clear_screen(void)
158{
159	unsigned char fillchar = 0xbf; /* magic */
160	unsigned long flags;
161
162	spin_lock_irqsave(&hga_reg_lock, flags);
163	if (hga_mode == HGA_TXT)
164		fillchar = ' ';
165	else if (hga_mode == HGA_GFX)
166		fillchar = 0x00;
167	spin_unlock_irqrestore(&hga_reg_lock, flags);
168	if (fillchar != 0xbf)
169		memset_io(hga_vram, fillchar, hga_vram_len);
170}
171
172static void hga_txt_mode(void)
173{
174	unsigned long flags;
175
176	spin_lock_irqsave(&hga_reg_lock, flags);
177	outb_p(HGA_MODE_VIDEO_EN | HGA_MODE_BLINK_EN, HGA_MODE_PORT);
178	outb_p(0x00, HGA_GFX_PORT);
179	outb_p(0x00, HGA_STATUS_PORT);
180
181	write_hga_b(0x61, 0x00);	/* horizontal total */
182	write_hga_b(0x50, 0x01);	/* horizontal displayed */
183	write_hga_b(0x52, 0x02);	/* horizontal sync pos */
184	write_hga_b(0x0f, 0x03);	/* horizontal sync width */
185
186	write_hga_b(0x19, 0x04);	/* vertical total */
187	write_hga_b(0x06, 0x05);	/* vertical total adjust */
188	write_hga_b(0x19, 0x06);	/* vertical displayed */
189	write_hga_b(0x19, 0x07);	/* vertical sync pos */
190
191	write_hga_b(0x02, 0x08);	/* interlace mode */
192	write_hga_b(0x0d, 0x09);	/* maximum scanline */
193	write_hga_b(0x0c, 0x0a);	/* cursor start */
194	write_hga_b(0x0d, 0x0b);	/* cursor end */
195
196	write_hga_w(0x0000, 0x0c);	/* start address */
197	write_hga_w(0x0000, 0x0e);	/* cursor location */
198
199	hga_mode = HGA_TXT;
200	spin_unlock_irqrestore(&hga_reg_lock, flags);
201}
202
203static void hga_gfx_mode(void)
204{
205	unsigned long flags;
206
207	spin_lock_irqsave(&hga_reg_lock, flags);
208	outb_p(0x00, HGA_STATUS_PORT);
209	outb_p(HGA_GFX_MODE_EN, HGA_GFX_PORT);
210	outb_p(HGA_MODE_VIDEO_EN | HGA_MODE_GRAPHICS, HGA_MODE_PORT);
211
212	write_hga_b(0x35, 0x00);	/* horizontal total */
213	write_hga_b(0x2d, 0x01);	/* horizontal displayed */
214	write_hga_b(0x2e, 0x02);	/* horizontal sync pos */
215	write_hga_b(0x07, 0x03);	/* horizontal sync width */
216
217	write_hga_b(0x5b, 0x04);	/* vertical total */
218	write_hga_b(0x02, 0x05);	/* vertical total adjust */
219	write_hga_b(0x57, 0x06);	/* vertical displayed */
220	write_hga_b(0x57, 0x07);	/* vertical sync pos */
221
222	write_hga_b(0x02, 0x08);	/* interlace mode */
223	write_hga_b(0x03, 0x09);	/* maximum scanline */
224	write_hga_b(0x00, 0x0a);	/* cursor start */
225	write_hga_b(0x00, 0x0b);	/* cursor end */
226
227	write_hga_w(0x0000, 0x0c);	/* start address */
228	write_hga_w(0x0000, 0x0e);	/* cursor location */
229
230	hga_mode = HGA_GFX;
231	spin_unlock_irqrestore(&hga_reg_lock, flags);
232}
233
234static void hga_show_logo(struct fb_info *info)
235{
236/*
237	void __iomem *dest = hga_vram;
238	char *logo = linux_logo_bw;
239	int x, y;
240
241	for (y = 134; y < 134 + 80 ; y++) * this needs some cleanup *
242		for (x = 0; x < 10 ; x++)
243			writeb(~*(logo++),(dest + HGA_ROWADDR(y) + x + 40));
244*/
245}
246
247static void hga_pan(unsigned int xoffset, unsigned int yoffset)
248{
249	unsigned int base;
250	unsigned long flags;
251
252	base = (yoffset / 8) * 90 + xoffset;
253	spin_lock_irqsave(&hga_reg_lock, flags);
254	write_hga_w(base, 0x0c);	/* start address */
255	spin_unlock_irqrestore(&hga_reg_lock, flags);
256	DPRINTK("hga_pan: base:%d\n", base);
257}
258
259static void hga_blank(int blank_mode)
260{
261	unsigned long flags;
262
263	spin_lock_irqsave(&hga_reg_lock, flags);
264	if (blank_mode) {
265		outb_p(0x00, HGA_MODE_PORT);	/* disable video */
266	} else {
267		outb_p(HGA_MODE_VIDEO_EN | HGA_MODE_GRAPHICS, HGA_MODE_PORT);
268	}
269	spin_unlock_irqrestore(&hga_reg_lock, flags);
270}
271
272static int __init hga_card_detect(void)
273{
274	int count=0;
275	void __iomem *p, *q;
276	unsigned short p_save, q_save;
277
278	hga_vram_len  = 0x08000;
279
280	hga_vram = ioremap(0xb0000, hga_vram_len);
281
282	if (request_region(0x3b0, 12, "hgafb"))
283		release_io_ports = 1;
284	if (request_region(0x3bf, 1, "hgafb"))
285		release_io_port = 1;
286
287	/* do a memory check */
288
289	p = hga_vram;
290	q = hga_vram + 0x01000;
291
292	p_save = readw(p); q_save = readw(q);
293
294	writew(0xaa55, p); if (readw(p) == 0xaa55) count++;
295	writew(0x55aa, p); if (readw(p) == 0x55aa) count++;
296	writew(p_save, p);
297
298	if (count != 2) {
299		return 0;
300	}
301
302	/* Ok, there is definitely a card registering at the correct
303	 * memory location, so now we do an I/O port test.
304	 */
305
306	if (!test_hga_b(0x66, 0x0f)) {	    /* cursor low register */
307		return 0;
308	}
309	if (!test_hga_b(0x99, 0x0f)) {     /* cursor low register */
310		return 0;
311	}
312
313	/* See if the card is a Hercules, by checking whether the vsync
314	 * bit of the status register is changing.  This test lasts for
315	 * approximately 1/10th of a second.
316	 */
317
318	p_save = q_save = inb_p(HGA_STATUS_PORT) & HGA_STATUS_VSYNC;
319
320	for (count=0; count < 50000 && p_save == q_save; count++) {
321		q_save = inb(HGA_STATUS_PORT) & HGA_STATUS_VSYNC;
322		udelay(2);
323	}
324
325	if (p_save == q_save)
326		return 0;
327
328	switch (inb_p(HGA_STATUS_PORT) & 0x70) {
329		case 0x10:
330			hga_type = TYPE_HERCPLUS;
331			hga_type_name = "HerculesPlus";
332			break;
333		case 0x50:
334			hga_type = TYPE_HERCCOLOR;
335			hga_type_name = "HerculesColor";
336			break;
337		default:
338			hga_type = TYPE_HERC;
339			hga_type_name = "Hercules";
340			break;
341	}
342	return 1;
343}
344
345/**
346 *	hgafb_open - open the framebuffer device
347 *	@info:pointer to fb_info object containing info for current hga board
348 *	@int:open by console system or userland.
349 */
350
351static int hgafb_open(struct fb_info *info, int init)
352{
353	hga_gfx_mode();
354	hga_clear_screen();
355	if (!nologo) hga_show_logo(info);
356	return 0;
357}
358
359/**
360 *	hgafb_open - open the framebuffer device
361 *	@info:pointer to fb_info object containing info for current hga board
362 *	@int:open by console system or userland.
363 */
364
365static int hgafb_release(struct fb_info *info, int init)
366{
367	hga_txt_mode();
368	hga_clear_screen();
369	return 0;
370}
371
372/**
373 *	hgafb_setcolreg - set color registers
374 *	@regno:register index to set
375 *	@red:red value, unused
376 *	@green:green value, unused
377 *	@blue:blue value, unused
378 *	@transp:transparency value, unused
379 *	@info:unused
380 *
381 *	This callback function is used to set the color registers of a HGA
382 *	board. Since we have only two fixed colors only @regno is checked.
383 *	A zero is returned on success and 1 for failure.
384 */
385
386static int hgafb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
387			   u_int transp, struct fb_info *info)
388{
389	if (regno > 1)
390		return 1;
391	return 0;
392}
393
394/**
395 *	hga_pan_display - pan or wrap the display
396 *	@var:contains new xoffset, yoffset and vmode values
397 *	@info:pointer to fb_info object containing info for current hga board
398 *
399 *	This function looks only at xoffset, yoffset and the %FB_VMODE_YWRAP
400 *	flag in @var. If input parameters are correct it calls hga_pan() to
401 *	program the hardware. @info->var is updated to the new values.
402 *	A zero is returned on success and %-EINVAL for failure.
403 */
404
405static int hgafb_pan_display(struct fb_var_screeninfo *var,
406			     struct fb_info *info)
407{
408	if (var->vmode & FB_VMODE_YWRAP) {
409		if (var->yoffset < 0 ||
410		    var->yoffset >= info->var.yres_virtual ||
411		    var->xoffset)
412			return -EINVAL;
413	} else {
414		if (var->xoffset + var->xres > info->var.xres_virtual
415		 || var->yoffset + var->yres > info->var.yres_virtual
416		 || var->yoffset % 8)
417			return -EINVAL;
418	}
419
420	hga_pan(var->xoffset, var->yoffset);
421	return 0;
422}
423
424/**
425 *	hgafb_blank - (un)blank the screen
426 *	@blank_mode:blanking method to use
427 *	@info:unused
428 *
429 *	Blank the screen if blank_mode != 0, else unblank.
430 *	Implements VESA suspend and powerdown modes on hardware that supports
431 *	disabling hsync/vsync:
432 *		@blank_mode == 2 means suspend vsync,
433 *		@blank_mode == 3 means suspend hsync,
434 *		@blank_mode == 4 means powerdown.
435 */
436
437static int hgafb_blank(int blank_mode, struct fb_info *info)
438{
439	hga_blank(blank_mode);
440	return 0;
441}
442
443/*
444 * Accel functions
445 */
446#ifdef CONFIG_FB_HGA_ACCEL
447static void hgafb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
448{
449	u_int rows, y;
450	u8 __iomem *dest;
451
452	y = rect->dy;
453
454	for (rows = rect->height; rows--; y++) {
455		dest = rowaddr(info, y) + (rect->dx >> 3);
456		switch (rect->rop) {
457		case ROP_COPY:
458			//fb_memset(dest, rect->color, (rect->width >> 3));
459			break;
460		case ROP_XOR:
461			fb_writeb(~(fb_readb(dest)), dest);
462			break;
463		}
464	}
465}
466
467static void hgafb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
468{
469	u_int rows, y1, y2;
470	u8 __iomem *src;
471	u8 __iomem *dest;
472
473	if (area->dy <= area->sy) {
474		y1 = area->sy;
475		y2 = area->dy;
476
477		for (rows = area->height; rows--; ) {
478			src = rowaddr(info, y1) + (area->sx >> 3);
479			dest = rowaddr(info, y2) + (area->dx >> 3);
480			//fb_memmove(dest, src, (area->width >> 3));
481			y1++;
482			y2++;
483		}
484	} else {
485		y1 = area->sy + area->height - 1;
486		y2 = area->dy + area->height - 1;
487
488		for (rows = area->height; rows--;) {
489			src = rowaddr(info, y1) + (area->sx >> 3);
490			dest = rowaddr(info, y2) + (area->dx >> 3);
491			//fb_memmove(dest, src, (area->width >> 3));
492			y1--;
493			y2--;
494		}
495	}
496}
497
498static void hgafb_imageblit(struct fb_info *info, const struct fb_image *image)
499{
500	u8 __iomem *dest;
501	u8 *cdat = (u8 *) image->data;
502	u_int rows, y = image->dy;
503	u8 d;
504
505	for (rows = image->height; rows--; y++) {
506		d = *cdat++;
507		dest = rowaddr(info, y) + (image->dx >> 3);
508		fb_writeb(d, dest);
509	}
510}
511#else /* !CONFIG_FB_HGA_ACCEL */
512#define hgafb_fillrect cfb_fillrect
513#define hgafb_copyarea cfb_copyarea
514#define hgafb_imageblit cfb_imageblit
515#endif /* CONFIG_FB_HGA_ACCEL */
516
517
518static struct fb_ops hgafb_ops = {
519	.owner		= THIS_MODULE,
520	.fb_open	= hgafb_open,
521	.fb_release	= hgafb_release,
522	.fb_setcolreg	= hgafb_setcolreg,
523	.fb_pan_display	= hgafb_pan_display,
524	.fb_blank	= hgafb_blank,
525	.fb_fillrect	= hgafb_fillrect,
526	.fb_copyarea	= hgafb_copyarea,
527	.fb_imageblit	= hgafb_imageblit,
528};
529
530/* ------------------------------------------------------------------------- *
531 *
532 * Functions in fb_info
533 *
534 * ------------------------------------------------------------------------- */
535
536/* ------------------------------------------------------------------------- */
537
538	/*
539	 *  Initialization
540	 */
541
542static int __init hgafb_probe(struct device *device)
543{
544	struct fb_info *info;
545
546	if (! hga_card_detect()) {
547		printk(KERN_INFO "hgafb: HGA card not detected.\n");
548		if (hga_vram)
549			iounmap(hga_vram);
550		return -EINVAL;
551	}
552
553	printk(KERN_INFO "hgafb: %s with %ldK of memory detected.\n",
554		hga_type_name, hga_vram_len/1024);
555
556	info = framebuffer_alloc(0, NULL);
557	if (!info) {
558		iounmap(hga_vram);
559		return -ENOMEM;
560	}
561
562	hga_fix.smem_start = (unsigned long)hga_vram;
563	hga_fix.smem_len = hga_vram_len;
564
565	info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
566	info->var = hga_default_var;
567	info->fix = hga_fix;
568	info->monspecs.hfmin = 0;
569	info->monspecs.hfmax = 0;
570	info->monspecs.vfmin = 10000;
571	info->monspecs.vfmax = 10000;
572	info->monspecs.dpms = 0;
573	info->fbops = &hgafb_ops;
574	info->screen_base = hga_vram;
575
576        if (register_framebuffer(info) < 0) {
577		framebuffer_release(info);
578		iounmap(hga_vram);
579		return -EINVAL;
580	}
581
582        printk(KERN_INFO "fb%d: %s frame buffer device\n",
583               info->node, info->fix.id);
584	dev_set_drvdata(device, info);
585	return 0;
586}
587
588static int hgafb_remove(struct device *device)
589{
590	struct fb_info *info = dev_get_drvdata(device);
591
592	hga_txt_mode();
593	hga_clear_screen();
594
595	if (info) {
596		unregister_framebuffer(info);
597		framebuffer_release(info);
598	}
599
600	iounmap(hga_vram);
601
602	if (release_io_ports)
603		release_region(0x3b0, 12);
604
605	if (release_io_port)
606		release_region(0x3bf, 1);
607
608	return 0;
609}
610
611static struct device_driver hgafb_driver = {
612	.name = "hgafb",
613	.bus  = &platform_bus_type,
614	.probe = hgafb_probe,
615	.remove = hgafb_remove,
616};
617
618static struct platform_device hgafb_device = {
619	.name = "hgafb",
620};
621
622static int __init hgafb_init(void)
623{
624	int ret;
625
626	if (fb_get_options("hgafb", NULL))
627		return -ENODEV;
628
629	ret = driver_register(&hgafb_driver);
630
631	if (!ret) {
632		ret = platform_device_register(&hgafb_device);
633		if (ret)
634			driver_unregister(&hgafb_driver);
635	}
636
637	return ret;
638}
639
640static void __exit hgafb_exit(void)
641{
642	platform_device_unregister(&hgafb_device);
643	driver_unregister(&hgafb_driver);
644}
645
646/* -------------------------------------------------------------------------
647 *
648 *  Modularization
649 *
650 * ------------------------------------------------------------------------- */
651
652MODULE_AUTHOR("Ferenc Bakonyi (fero@drama.obuda.kando.hu)");
653MODULE_DESCRIPTION("FBDev driver for Hercules Graphics Adaptor");
654MODULE_LICENSE("GPL");
655
656module_param(nologo, bool, 0);
657MODULE_PARM_DESC(nologo, "Disables startup logo if != 0 (default=0)");
658module_init(hgafb_init);
659module_exit(hgafb_exit);
660