1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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/delay.h>
40#include <linux/fb.h>
41#include <linux/init.h>
42#include <linux/ioport.h>
43#include <linux/platform_device.h>
44#include <asm/io.h>
45#include <asm/vga.h>
46
47#if 0
48#define DPRINTK(args...) printk(KERN_DEBUG __FILE__": " ##args)
49#else
50#define DPRINTK(args...)
51#endif
52
53#if 0
54#define CHKINFO(ret) if (info != &fb_info) { printk(KERN_DEBUG __FILE__": This should never happen, line:%d \n", __LINE__); return ret; }
55#else
56#define CHKINFO(ret)
57#endif
58
59
60
61static void __iomem *hga_vram;
62static unsigned long hga_vram_len;
63
64#define HGA_ROWADDR(row) ((row%4)*8192 + (row>>2)*90)
65#define HGA_TXT 0
66#define HGA_GFX 1
67
68static inline u8 __iomem * rowaddr(struct fb_info *info, u_int row)
69{
70 return info->screen_base + HGA_ROWADDR(row);
71}
72
73static int hga_mode = -1;
74
75static enum { TYPE_HERC, TYPE_HERCPLUS, TYPE_HERCCOLOR } hga_type;
76static char *hga_type_name;
77
78#define HGA_INDEX_PORT 0x3b4
79#define HGA_VALUE_PORT 0x3b5
80#define HGA_MODE_PORT 0x3b8
81#define HGA_STATUS_PORT 0x3ba
82#define HGA_GFX_PORT 0x3bf
83
84
85
86#define HGA_CURSOR_BLINKING 0x00
87#define HGA_CURSOR_OFF 0x20
88#define HGA_CURSOR_SLOWBLINK 0x60
89
90#define HGA_MODE_GRAPHICS 0x02
91#define HGA_MODE_VIDEO_EN 0x08
92#define HGA_MODE_BLINK_EN 0x20
93#define HGA_MODE_GFX_PAGE1 0x80
94
95#define HGA_STATUS_HSYNC 0x01
96#define HGA_STATUS_VSYNC 0x80
97#define HGA_STATUS_VIDEO 0x08
98
99#define HGA_CONFIG_COL132 0x08
100#define HGA_GFX_MODE_EN 0x01
101#define HGA_GFX_PAGE_EN 0x02
102
103
104
105static DEFINE_SPINLOCK(hga_reg_lock);
106
107
108
109static struct fb_var_screeninfo hga_default_var = {
110 .xres = 720,
111 .yres = 348,
112 .xres_virtual = 720,
113 .yres_virtual = 348,
114 .bits_per_pixel = 1,
115 .red = {0, 1, 0},
116 .green = {0, 1, 0},
117 .blue = {0, 1, 0},
118 .transp = {0, 0, 0},
119 .height = -1,
120 .width = -1,
121};
122
123static struct fb_fix_screeninfo hga_fix = {
124 .id = "HGA",
125 .type = FB_TYPE_PACKED_PIXELS,
126 .visual = FB_VISUAL_MONO10,
127 .xpanstep = 8,
128 .ypanstep = 8,
129 .line_length = 90,
130 .accel = FB_ACCEL_NONE
131};
132
133
134static int release_io_port = 0;
135static int release_io_ports = 0;
136static bool nologo = 0;
137
138
139
140
141
142
143
144static void write_hga_b(unsigned int val, unsigned char reg)
145{
146 outb_p(reg, HGA_INDEX_PORT);
147 outb_p(val, HGA_VALUE_PORT);
148}
149
150static void write_hga_w(unsigned int val, unsigned char reg)
151{
152 outb_p(reg, HGA_INDEX_PORT); outb_p(val >> 8, HGA_VALUE_PORT);
153 outb_p(reg+1, HGA_INDEX_PORT); outb_p(val & 0xff, HGA_VALUE_PORT);
154}
155
156static int test_hga_b(unsigned char val, unsigned char reg)
157{
158 outb_p(reg, HGA_INDEX_PORT);
159 outb (val, HGA_VALUE_PORT);
160 udelay(20); val = (inb_p(HGA_VALUE_PORT) == val);
161 return val;
162}
163
164static void hga_clear_screen(void)
165{
166 unsigned char fillchar = 0xbf;
167 unsigned long flags;
168
169 spin_lock_irqsave(&hga_reg_lock, flags);
170 if (hga_mode == HGA_TXT)
171 fillchar = ' ';
172 else if (hga_mode == HGA_GFX)
173 fillchar = 0x00;
174 spin_unlock_irqrestore(&hga_reg_lock, flags);
175 if (fillchar != 0xbf)
176 memset_io(hga_vram, fillchar, hga_vram_len);
177}
178
179static void hga_txt_mode(void)
180{
181 unsigned long flags;
182
183 spin_lock_irqsave(&hga_reg_lock, flags);
184 outb_p(HGA_MODE_VIDEO_EN | HGA_MODE_BLINK_EN, HGA_MODE_PORT);
185 outb_p(0x00, HGA_GFX_PORT);
186 outb_p(0x00, HGA_STATUS_PORT);
187
188 write_hga_b(0x61, 0x00);
189 write_hga_b(0x50, 0x01);
190 write_hga_b(0x52, 0x02);
191 write_hga_b(0x0f, 0x03);
192
193 write_hga_b(0x19, 0x04);
194 write_hga_b(0x06, 0x05);
195 write_hga_b(0x19, 0x06);
196 write_hga_b(0x19, 0x07);
197
198 write_hga_b(0x02, 0x08);
199 write_hga_b(0x0d, 0x09);
200 write_hga_b(0x0c, 0x0a);
201 write_hga_b(0x0d, 0x0b);
202
203 write_hga_w(0x0000, 0x0c);
204 write_hga_w(0x0000, 0x0e);
205
206 hga_mode = HGA_TXT;
207 spin_unlock_irqrestore(&hga_reg_lock, flags);
208}
209
210static void hga_gfx_mode(void)
211{
212 unsigned long flags;
213
214 spin_lock_irqsave(&hga_reg_lock, flags);
215 outb_p(0x00, HGA_STATUS_PORT);
216 outb_p(HGA_GFX_MODE_EN, HGA_GFX_PORT);
217 outb_p(HGA_MODE_VIDEO_EN | HGA_MODE_GRAPHICS, HGA_MODE_PORT);
218
219 write_hga_b(0x35, 0x00);
220 write_hga_b(0x2d, 0x01);
221 write_hga_b(0x2e, 0x02);
222 write_hga_b(0x07, 0x03);
223
224 write_hga_b(0x5b, 0x04);
225 write_hga_b(0x02, 0x05);
226 write_hga_b(0x57, 0x06);
227 write_hga_b(0x57, 0x07);
228
229 write_hga_b(0x02, 0x08);
230 write_hga_b(0x03, 0x09);
231 write_hga_b(0x00, 0x0a);
232 write_hga_b(0x00, 0x0b);
233
234 write_hga_w(0x0000, 0x0c);
235 write_hga_w(0x0000, 0x0e);
236
237 hga_mode = HGA_GFX;
238 spin_unlock_irqrestore(&hga_reg_lock, flags);
239}
240
241static void hga_show_logo(struct fb_info *info)
242{
243
244
245
246
247
248
249
250
251
252}
253
254static void hga_pan(unsigned int xoffset, unsigned int yoffset)
255{
256 unsigned int base;
257 unsigned long flags;
258
259 base = (yoffset / 8) * 90 + xoffset;
260 spin_lock_irqsave(&hga_reg_lock, flags);
261 write_hga_w(base, 0x0c);
262 spin_unlock_irqrestore(&hga_reg_lock, flags);
263 DPRINTK("hga_pan: base:%d\n", base);
264}
265
266static void hga_blank(int blank_mode)
267{
268 unsigned long flags;
269
270 spin_lock_irqsave(&hga_reg_lock, flags);
271 if (blank_mode) {
272 outb_p(0x00, HGA_MODE_PORT);
273 } else {
274 outb_p(HGA_MODE_VIDEO_EN | HGA_MODE_GRAPHICS, HGA_MODE_PORT);
275 }
276 spin_unlock_irqrestore(&hga_reg_lock, flags);
277}
278
279static int hga_card_detect(void)
280{
281 int count = 0;
282 void __iomem *p, *q;
283 unsigned short p_save, q_save;
284
285 hga_vram_len = 0x08000;
286
287 hga_vram = ioremap(0xb0000, hga_vram_len);
288
289 if (request_region(0x3b0, 12, "hgafb"))
290 release_io_ports = 1;
291 if (request_region(0x3bf, 1, "hgafb"))
292 release_io_port = 1;
293
294
295
296 p = hga_vram;
297 q = hga_vram + 0x01000;
298
299 p_save = readw(p); q_save = readw(q);
300
301 writew(0xaa55, p); if (readw(p) == 0xaa55) count++;
302 writew(0x55aa, p); if (readw(p) == 0x55aa) count++;
303 writew(p_save, p);
304
305 if (count != 2)
306 goto error;
307
308
309
310
311
312 if (!test_hga_b(0x66, 0x0f))
313 goto error;
314
315 if (!test_hga_b(0x99, 0x0f))
316 goto error;
317
318
319
320
321
322
323 p_save = q_save = inb_p(HGA_STATUS_PORT) & HGA_STATUS_VSYNC;
324
325 for (count=0; count < 50000 && p_save == q_save; count++) {
326 q_save = inb(HGA_STATUS_PORT) & HGA_STATUS_VSYNC;
327 udelay(2);
328 }
329
330 if (p_save == q_save)
331 goto error;
332
333 switch (inb_p(HGA_STATUS_PORT) & 0x70) {
334 case 0x10:
335 hga_type = TYPE_HERCPLUS;
336 hga_type_name = "HerculesPlus";
337 break;
338 case 0x50:
339 hga_type = TYPE_HERCCOLOR;
340 hga_type_name = "HerculesColor";
341 break;
342 default:
343 hga_type = TYPE_HERC;
344 hga_type_name = "Hercules";
345 break;
346 }
347 return 1;
348error:
349 if (release_io_ports)
350 release_region(0x3b0, 12);
351 if (release_io_port)
352 release_region(0x3bf, 1);
353 return 0;
354}
355
356
357
358
359
360
361
362static int hgafb_open(struct fb_info *info, int init)
363{
364 hga_gfx_mode();
365 hga_clear_screen();
366 if (!nologo) hga_show_logo(info);
367 return 0;
368}
369
370
371
372
373
374
375
376static int hgafb_release(struct fb_info *info, int init)
377{
378 hga_txt_mode();
379 hga_clear_screen();
380 return 0;
381}
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397static int hgafb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
398 u_int transp, struct fb_info *info)
399{
400 if (regno > 1)
401 return 1;
402 return 0;
403}
404
405
406
407
408
409
410
411
412
413
414
415
416static int hgafb_pan_display(struct fb_var_screeninfo *var,
417 struct fb_info *info)
418{
419 if (var->vmode & FB_VMODE_YWRAP) {
420 if (var->yoffset >= info->var.yres_virtual ||
421 var->xoffset)
422 return -EINVAL;
423 } else {
424 if (var->xoffset + info->var.xres > info->var.xres_virtual
425 || var->yoffset + info->var.yres > info->var.yres_virtual
426 || var->yoffset % 8)
427 return -EINVAL;
428 }
429
430 hga_pan(var->xoffset, var->yoffset);
431 return 0;
432}
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447static int hgafb_blank(int blank_mode, struct fb_info *info)
448{
449 hga_blank(blank_mode);
450 return 0;
451}
452
453
454
455
456static void hgafb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
457{
458 u_int rows, y;
459 u8 __iomem *dest;
460
461 y = rect->dy;
462
463 for (rows = rect->height; rows--; y++) {
464 dest = rowaddr(info, y) + (rect->dx >> 3);
465 switch (rect->rop) {
466 case ROP_COPY:
467 memset_io(dest, rect->color, (rect->width >> 3));
468 break;
469 case ROP_XOR:
470 fb_writeb(~(fb_readb(dest)), dest);
471 break;
472 }
473 }
474}
475
476static void hgafb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
477{
478 u_int rows, y1, y2;
479 u8 __iomem *src;
480 u8 __iomem *dest;
481
482 if (area->dy <= area->sy) {
483 y1 = area->sy;
484 y2 = area->dy;
485
486 for (rows = area->height; rows--; ) {
487 src = rowaddr(info, y1) + (area->sx >> 3);
488 dest = rowaddr(info, y2) + (area->dx >> 3);
489 memmove(dest, src, (area->width >> 3));
490 y1++;
491 y2++;
492 }
493 } else {
494 y1 = area->sy + area->height - 1;
495 y2 = area->dy + area->height - 1;
496
497 for (rows = area->height; rows--;) {
498 src = rowaddr(info, y1) + (area->sx >> 3);
499 dest = rowaddr(info, y2) + (area->dx >> 3);
500 memmove(dest, src, (area->width >> 3));
501 y1--;
502 y2--;
503 }
504 }
505}
506
507static void hgafb_imageblit(struct fb_info *info, const struct fb_image *image)
508{
509 u8 __iomem *dest;
510 u8 *cdat = (u8 *) image->data;
511 u_int rows, y = image->dy;
512 u_int x;
513 u8 d;
514
515 for (rows = image->height; rows--; y++) {
516 for (x = 0; x < image->width; x+= 8) {
517 d = *cdat++;
518 dest = rowaddr(info, y) + ((image->dx + x)>> 3);
519 fb_writeb(d, dest);
520 }
521 }
522}
523
524static struct fb_ops hgafb_ops = {
525 .owner = THIS_MODULE,
526 .fb_open = hgafb_open,
527 .fb_release = hgafb_release,
528 .fb_setcolreg = hgafb_setcolreg,
529 .fb_pan_display = hgafb_pan_display,
530 .fb_blank = hgafb_blank,
531 .fb_fillrect = hgafb_fillrect,
532 .fb_copyarea = hgafb_copyarea,
533 .fb_imageblit = hgafb_imageblit,
534};
535
536
537
538
539
540
541
542
543
544
545
546
547
548static int hgafb_probe(struct platform_device *pdev)
549{
550 struct fb_info *info;
551
552 if (! hga_card_detect()) {
553 printk(KERN_INFO "hgafb: HGA card not detected.\n");
554 if (hga_vram)
555 iounmap(hga_vram);
556 return -EINVAL;
557 }
558
559 printk(KERN_INFO "hgafb: %s with %ldK of memory detected.\n",
560 hga_type_name, hga_vram_len/1024);
561
562 info = framebuffer_alloc(0, &pdev->dev);
563 if (!info) {
564 iounmap(hga_vram);
565 return -ENOMEM;
566 }
567
568 hga_fix.smem_start = (unsigned long)hga_vram;
569 hga_fix.smem_len = hga_vram_len;
570
571 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
572 info->var = hga_default_var;
573 info->fix = hga_fix;
574 info->monspecs.hfmin = 0;
575 info->monspecs.hfmax = 0;
576 info->monspecs.vfmin = 10000;
577 info->monspecs.vfmax = 10000;
578 info->monspecs.dpms = 0;
579 info->fbops = &hgafb_ops;
580 info->screen_base = hga_vram;
581
582 if (register_framebuffer(info) < 0) {
583 framebuffer_release(info);
584 iounmap(hga_vram);
585 return -EINVAL;
586 }
587
588 fb_info(info, "%s frame buffer device\n", info->fix.id);
589 platform_set_drvdata(pdev, info);
590 return 0;
591}
592
593static int hgafb_remove(struct platform_device *pdev)
594{
595 struct fb_info *info = platform_get_drvdata(pdev);
596
597 hga_txt_mode();
598 hga_clear_screen();
599
600 if (info) {
601 unregister_framebuffer(info);
602 framebuffer_release(info);
603 }
604
605 iounmap(hga_vram);
606
607 if (release_io_ports)
608 release_region(0x3b0, 12);
609
610 if (release_io_port)
611 release_region(0x3bf, 1);
612
613 return 0;
614}
615
616static struct platform_driver hgafb_driver = {
617 .probe = hgafb_probe,
618 .remove = hgafb_remove,
619 .driver = {
620 .name = "hgafb",
621 },
622};
623
624static struct platform_device *hgafb_device;
625
626static int __init hgafb_init(void)
627{
628 int ret;
629
630 if (fb_get_options("hgafb", NULL))
631 return -ENODEV;
632
633 ret = platform_driver_register(&hgafb_driver);
634
635 if (!ret) {
636 hgafb_device = platform_device_register_simple("hgafb", 0, NULL, 0);
637
638 if (IS_ERR(hgafb_device)) {
639 platform_driver_unregister(&hgafb_driver);
640 ret = PTR_ERR(hgafb_device);
641 }
642 }
643
644 return ret;
645}
646
647static void __exit hgafb_exit(void)
648{
649 platform_device_unregister(hgafb_device);
650 platform_driver_unregister(&hgafb_driver);
651}
652
653
654
655
656
657
658
659MODULE_AUTHOR("Ferenc Bakonyi (fero@drama.obuda.kando.hu)");
660MODULE_DESCRIPTION("FBDev driver for Hercules Graphics Adaptor");
661MODULE_LICENSE("GPL");
662
663module_param(nologo, bool, 0);
664MODULE_PARM_DESC(nologo, "Disables startup logo if != 0 (default=0)");
665module_init(hgafb_init);
666module_exit(hgafb_exit);
667