source: mainline/kernel/genarch/src/drivers/ega/ega.c@ 712c4ba

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 712c4ba was 712c4ba, checked in by Martin Decky <martin@…>, 14 years ago

avoid spinlocks to be taken in the direct code path to physically outputing kconsole characters to the framebuffer (this fixes ticket #243)
the spinlock in printf() (kernel) was sacrificed, this might lead potentially to scrambled kconsole output
the hack of ignoring spinlocks whose names start with "*" in the livelock detector can be removed

  • Property mode set to 100644
File size: 11.7 KB
Line 
1/*
2 * Copyright (c) 2001-2004 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup genarch_drivers
30 * @{
31 */
32/**
33 * @file
34 * @brief EGA driver.
35 */
36
37#include <genarch/drivers/ega/ega.h>
38#include <putchar.h>
39#include <mm/page.h>
40#include <mm/as.h>
41#include <mm/slab.h>
42#include <synch/mutex.h>
43#include <arch/mm/page.h>
44#include <typedefs.h>
45#include <arch/asm.h>
46#include <memstr.h>
47#include <str.h>
48#include <console/chardev.h>
49#include <console/console.h>
50#include <sysinfo/sysinfo.h>
51#include <ddi/ddi.h>
52
53/*
54 * The EGA driver.
55 * Simple and short. Function for displaying characters and "scrolling".
56 */
57
58#define SPACE 0x20
59#define STYLE 0x1e
60#define INVAL 0x17
61
62#define EMPTY_CHAR ((STYLE << 8) | SPACE)
63
64typedef struct {
65 mutex_t mtx;
66
67 uint32_t cursor;
68 uint8_t *addr;
69 uint8_t *backbuf;
70 ioport8_t *base;
71} ega_instance_t;
72
73static void ega_putchar(outdev_t *, wchar_t, bool);
74static void ega_redraw(outdev_t *);
75
76static outdev_operations_t egadev_ops = {
77 .write = ega_putchar,
78 .redraw = ega_redraw
79};
80
81static uint16_t ega_oem_glyph(const wchar_t ch)
82{
83 if ((ch >= 0x0000) && (ch <= 0x007f))
84 return ch;
85
86 if (ch == 0x00a0)
87 return 255;
88
89 if (ch == 0x00a1)
90 return 173;
91
92 if ((ch >= 0x00a2) && (ch <= 0x00a3))
93 return (ch - 7);
94
95 if (ch == 0x00a5)
96 return 157;
97
98 if (ch == 0x00aa)
99 return 166;
100
101 if (ch == 0x00ab)
102 return 174;
103
104 if (ch == 0x00ac)
105 return 170;
106
107 if (ch == 0x00b0)
108 return 248;
109
110 if (ch == 0x00b1)
111 return 241;
112
113 if (ch == 0x00b2)
114 return 253;
115
116 if (ch == 0x00b5)
117 return 230;
118
119 if (ch == 0x00b7)
120 return 250;
121
122 if (ch == 0x00ba)
123 return 167;
124
125 if (ch == 0x00bb)
126 return 175;
127
128 if (ch == 0x00bc)
129 return 172;
130
131 if (ch == 0x00bd)
132 return 171;
133
134 if (ch == 0x00bf)
135 return 168;
136
137 if ((ch >= 0x00c4) && (ch <= 0x00c5))
138 return (ch - 54);
139
140 if (ch == 0x00c6)
141 return 146;
142
143 if (ch == 0x00c7)
144 return 128;
145
146 if (ch == 0x00c9)
147 return 144;
148
149 if (ch == 0x00d1)
150 return 165;
151
152 if (ch == 0x00d6)
153 return 153;
154
155 if (ch == 0x00dc)
156 return 154;
157
158 if (ch == 0x00df)
159 return 225;
160
161 if (ch == 0x00e0)
162 return 133;
163
164 if (ch == 0x00e1)
165 return 160;
166
167 if (ch == 0x00e2)
168 return 131;
169
170 if (ch == 0x00e4)
171 return 132;
172
173 if (ch == 0x00e5)
174 return 134;
175
176 if (ch == 0x00e6)
177 return 145;
178
179 if (ch == 0x00e7)
180 return 135;
181
182 if (ch == 0x00e8)
183 return 138;
184
185 if (ch == 0x00e9)
186 return 130;
187
188 if ((ch >= 0x00ea) && (ch <= 0x00eb))
189 return (ch - 98);
190
191 if (ch == 0x00ec)
192 return 141;
193
194 if (ch == 0x00ed)
195 return 161;
196
197 if (ch == 0x00ee)
198 return 140;
199
200 if (ch == 0x00ef)
201 return 139;
202
203 if (ch == 0x00f1)
204 return 164;
205
206 if (ch == 0x00f2)
207 return 149;
208
209 if (ch == 0x00f3)
210 return 162;
211
212 if (ch == 0x00f4)
213 return 147;
214
215 if (ch == 0x00f6)
216 return 148;
217
218 if (ch == 0x00f7)
219 return 246;
220
221 if (ch == 0x00f9)
222 return 151;
223
224 if (ch == 0x00fa)
225 return 163;
226
227 if (ch == 0x00fb)
228 return 150;
229
230 if (ch == 0x00fc)
231 return 129;
232
233 if (ch == 0x00ff)
234 return 152;
235
236 if (ch == 0x0192)
237 return 159;
238
239 if (ch == 0x0393)
240 return 226;
241
242 if (ch == 0x0398)
243 return 233;
244
245 if (ch == 0x03a3)
246 return 228;
247
248 if (ch == 0x03a6)
249 return 232;
250
251 if (ch == 0x03a9)
252 return 234;
253
254 if (ch == 0x03b1)
255 return 224;
256
257 if (ch == 0x03b4)
258 return 235;
259
260 if (ch == 0x03b5)
261 return 238;
262
263 if (ch == 0x03c0)
264 return 227;
265
266 if (ch == 0x03c3)
267 return 229;
268
269 if (ch == 0x03c4)
270 return 231;
271
272 if (ch == 0x03c6)
273 return 237;
274
275 if (ch == 0x207f)
276 return 252;
277
278 if (ch == 0x20a7)
279 return 158;
280
281 if (ch == 0x2219)
282 return 249;
283
284 if (ch == 0x221a)
285 return 251;
286
287 if (ch == 0x221e)
288 return 236;
289
290 if (ch == 0x2229)
291 return 239;
292
293 if (ch == 0x2248)
294 return 247;
295
296 if (ch == 0x2261)
297 return 240;
298
299 if (ch == 0x2264)
300 return 243;
301
302 if (ch == 0x2265)
303 return 242;
304
305 if (ch == 0x2310)
306 return 169;
307
308 if ((ch >= 0x2320) && (ch <= 0x2321))
309 return (ch - 8748);
310
311 if (ch == 0x2500)
312 return 196;
313
314 if (ch == 0x2502)
315 return 179;
316
317 if (ch == 0x250c)
318 return 218;
319
320 if (ch == 0x2510)
321 return 191;
322
323 if (ch == 0x2514)
324 return 192;
325
326 if (ch == 0x2518)
327 return 217;
328
329 if (ch == 0x251c)
330 return 195;
331
332 if (ch == 0x2524)
333 return 180;
334
335 if (ch == 0x252c)
336 return 194;
337
338 if (ch == 0x2534)
339 return 193;
340
341 if (ch == 0x253c)
342 return 197;
343
344 if (ch == 0x2550)
345 return 205;
346
347 if (ch == 0x2551)
348 return 186;
349
350 if ((ch >= 0x2552) && (ch <= 0x2553))
351 return (ch - 9341);
352
353 if (ch == 0x2554)
354 return 201;
355
356 if (ch == 0x2555)
357 return 184;
358
359 if (ch == 0x2556)
360 return 183;
361
362 if (ch == 0x2557)
363 return 187;
364
365 if (ch == 0x2558)
366 return 212;
367
368 if (ch == 0x2559)
369 return 211;
370
371 if (ch == 0x255a)
372 return 200;
373
374 if (ch == 0x255b)
375 return 190;
376
377 if (ch == 0x255c)
378 return 189;
379
380 if (ch == 0x255d)
381 return 188;
382
383 if ((ch >= 0x255e) && (ch <= 0x255f))
384 return (ch - 9368);
385
386 if (ch == 0x2560)
387 return 204;
388
389 if ((ch >= 0x2561) && (ch <= 0x2562))
390 return (ch - 9388);
391
392 if (ch == 0x2563)
393 return 185;
394
395 if ((ch >= 0x2564) && (ch <= 0x2565))
396 return (ch - 9363);
397
398 if (ch == 0x2566)
399 return 203;
400
401 if ((ch >= 0x2567) && (ch <= 0x2568))
402 return (ch - 9368);
403
404 if (ch == 0x2569)
405 return 202;
406
407 if (ch == 0x256a)
408 return 216;
409
410 if (ch == 0x256b)
411 return 215;
412
413 if (ch == 0x256c)
414 return 206;
415
416 if (ch == 0x2580)
417 return 223;
418
419 if (ch == 0x2584)
420 return 220;
421
422 if (ch == 0x2588)
423 return 219;
424
425 if (ch == 0x258c)
426 return 221;
427
428 if (ch == 0x2590)
429 return 222;
430
431 if ((ch >= 0x2591) && (ch <= 0x2593))
432 return (ch - 9441);
433
434 return 256;
435}
436
437/*
438 * This function takes care of scrolling.
439 */
440static void ega_check_cursor(ega_instance_t *instance, bool silent)
441{
442 if (instance->cursor < EGA_SCREEN)
443 return;
444
445 memmove((void *) instance->backbuf,
446 (void *) (instance->backbuf + EGA_COLS * 2),
447 (EGA_SCREEN - EGA_COLS) * 2);
448 memsetw(instance->backbuf + (EGA_SCREEN - EGA_COLS) * 2,
449 EGA_COLS, EMPTY_CHAR);
450
451 if (!silent) {
452 memmove((void *) instance->addr,
453 (void *) (instance->addr + EGA_COLS * 2),
454 (EGA_SCREEN - EGA_COLS) * 2);
455 memsetw(instance->addr + (EGA_SCREEN - EGA_COLS) * 2,
456 EGA_COLS, EMPTY_CHAR);
457 }
458
459 instance->cursor = instance->cursor - EGA_COLS;
460}
461
462static void ega_show_cursor(ega_instance_t *instance, bool silent)
463{
464 if (!silent) {
465 pio_write_8(instance->base + EGA_INDEX_REG, 0x0a);
466 uint8_t stat = pio_read_8(instance->base + EGA_DATA_REG);
467 pio_write_8(instance->base + EGA_INDEX_REG, 0x0a);
468 pio_write_8(instance->base + EGA_DATA_REG, stat & (~(1 << 5)));
469 }
470}
471
472static void ega_move_cursor(ega_instance_t *instance, bool silent)
473{
474 if (!silent) {
475 pio_write_8(instance->base + EGA_INDEX_REG, 0x0e);
476 pio_write_8(instance->base + EGA_DATA_REG,
477 (uint8_t) ((instance->cursor >> 8) & 0xff));
478 pio_write_8(instance->base + EGA_INDEX_REG, 0x0f);
479 pio_write_8(instance->base + EGA_DATA_REG,
480 (uint8_t) (instance->cursor & 0xff));
481 }
482}
483
484static void ega_sync_cursor(ega_instance_t *instance, bool silent)
485{
486 if (!silent) {
487 pio_write_8(instance->base + EGA_INDEX_REG, 0x0e);
488 uint8_t hi = pio_read_8(instance->base + EGA_DATA_REG);
489 pio_write_8(instance->base + EGA_INDEX_REG, 0x0f);
490 uint8_t lo = pio_read_8(instance->base + EGA_DATA_REG);
491
492 instance->cursor = (hi << 8) | lo;
493 } else
494 instance->cursor = 0;
495
496 if (instance->cursor >= EGA_SCREEN)
497 instance->cursor = 0;
498
499 if ((instance->cursor % EGA_COLS) != 0)
500 instance->cursor =
501 (instance->cursor + EGA_COLS) - instance->cursor % EGA_COLS;
502
503 memsetw(instance->backbuf + instance->cursor * 2,
504 EGA_SCREEN - instance->cursor, EMPTY_CHAR);
505
506 if (!silent)
507 memsetw(instance->addr + instance->cursor * 2,
508 EGA_SCREEN - instance->cursor, EMPTY_CHAR);
509
510 ega_check_cursor(instance, silent);
511 ega_move_cursor(instance, silent);
512 ega_show_cursor(instance, silent);
513}
514
515static void ega_display_char(ega_instance_t *instance, wchar_t ch, bool silent)
516{
517 uint16_t index = ega_oem_glyph(ch);
518 uint8_t glyph;
519 uint8_t style;
520
521 if ((index >> 8)) {
522 glyph = U_SPECIAL;
523 style = INVAL;
524 } else {
525 glyph = index & 0xff;
526 style = STYLE;
527 }
528
529 instance->backbuf[instance->cursor * 2] = glyph;
530 instance->backbuf[instance->cursor * 2 + 1] = style;
531
532 if (!silent) {
533 instance->addr[instance->cursor * 2] = glyph;
534 instance->addr[instance->cursor * 2 + 1] = style;
535 }
536}
537
538static void ega_putchar(outdev_t *dev, wchar_t ch, bool silent)
539{
540 ega_instance_t *instance = (ega_instance_t *) dev->data;
541
542 mutex_lock(&instance->mtx);
543
544 switch (ch) {
545 case '\n':
546 instance->cursor = (instance->cursor + EGA_COLS)
547 - instance->cursor % EGA_COLS;
548 break;
549 case '\t':
550 instance->cursor = (instance->cursor + 8)
551 - instance->cursor % 8;
552 break;
553 case '\b':
554 if (instance->cursor % EGA_COLS)
555 instance->cursor--;
556 break;
557 default:
558 ega_display_char(instance, ch, silent);
559 instance->cursor++;
560 break;
561 }
562 ega_check_cursor(instance, silent);
563 ega_move_cursor(instance, silent);
564
565 mutex_unlock(&instance->mtx);
566}
567
568static void ega_redraw(outdev_t *dev)
569{
570 ega_instance_t *instance = (ega_instance_t *) dev->data;
571
572 mutex_lock(&instance->mtx);
573
574 memcpy(instance->addr, instance->backbuf, EGA_VRAM_SIZE);
575 ega_move_cursor(instance, silent);
576 ega_show_cursor(instance, silent);
577
578 mutex_unlock(&instance->mtx);
579}
580
581outdev_t *ega_init(ioport8_t *base, uintptr_t addr)
582{
583 outdev_t *egadev = malloc(sizeof(outdev_t), FRAME_ATOMIC);
584 if (!egadev)
585 return NULL;
586
587 ega_instance_t *instance = malloc(sizeof(ega_instance_t), FRAME_ATOMIC);
588 if (!instance) {
589 free(egadev);
590 return NULL;
591 }
592
593 outdev_initialize("egadev", egadev, &egadev_ops);
594 egadev->data = instance;
595
596 mutex_initialize(&instance->mtx, MUTEX_PASSIVE);
597
598 instance->base = base;
599 instance->addr = (uint8_t *) hw_map(addr, EGA_VRAM_SIZE);
600 if (!instance->addr) {
601 LOG("Unable to EGA video memory.");
602 free(instance);
603 free(egadev);
604 return NULL;
605 }
606
607 instance->backbuf = (uint8_t *) malloc(EGA_VRAM_SIZE, 0);
608 if (!instance->backbuf) {
609 LOG("Unable to allocate backbuffer.");
610 free(instance);
611 free(egadev);
612 return NULL;
613 }
614
615 /* Synchronize the back buffer and cursor position. */
616 memcpy(instance->backbuf, instance->addr, EGA_VRAM_SIZE);
617 ega_sync_cursor(instance, silent);
618
619 if (!fb_exported) {
620 /*
621 * This is the necessary evil until the userspace driver is entirely
622 * self-sufficient.
623 */
624 sysinfo_set_item_val("fb", NULL, true);
625 sysinfo_set_item_val("fb.kind", NULL, 2);
626 sysinfo_set_item_val("fb.width", NULL, EGA_COLS);
627 sysinfo_set_item_val("fb.height", NULL, EGA_ROWS);
628 sysinfo_set_item_val("fb.blinking", NULL, true);
629 sysinfo_set_item_val("fb.address.physical", NULL, addr);
630
631 fb_exported = true;
632 }
633
634 return egadev;
635}
636
637/** @}
638 */
Note: See TracBrowser for help on using the repository browser.