blob: dfd2a8496e72e10fcec3fcbad750babac54aea8e [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/*
2 * (C) Copyright 2002
3 * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
9#include <pci.h>
10#include <stdio_dev.h>
11#include <i8042.h>
12#include <asm/ptrace.h>
13#include <asm/io.h>
14#include <asm/pci.h>
15
16/* basic textmode I/O from linux kernel */
17static char *vidmem = (char *)0xb8000;
18static int vidport;
19static int lines, cols;
20static int orig_x, orig_y;
21
22static void beep(int dur)
23{
24 int i;
25
26 outb_p(3, 0x61);
27 for (i = 0; i < 10*dur; i++)
28 udelay(1000);
29
30 outb_p(0, 0x61);
31}
32
33static void scroll(void)
34{
35 int i;
36
37 memcpy(vidmem, vidmem + cols * 2, (lines - 1) * cols * 2);
38 for (i = (lines - 1) * cols * 2; i < lines * cols * 2; i += 2)
39 vidmem[i] = ' ';
40}
41
42static void __video_putc(const char c, int *x, int *y)
43{
44 if (c == '\n') {
45 (*x) = 0;
46 if (++(*y) >= lines) {
47 scroll();
48 (*y)--;
49 }
50 } else if (c == '\b') {
51 if ((*x) != 0) {
52 --(*x);
53 vidmem[((*x) + cols * (*y)) * 2] = ' ';
54 }
55 } else if (c == '\r') {
56 (*x) = 0;
57
58 } else if (c == '\a') {
59 beep(3);
60
61 } else if (c == '\t') {
62 __video_putc(' ', x, y);
63 __video_putc(' ', x, y);
64 __video_putc(' ', x, y);
65 __video_putc(' ', x, y);
66 __video_putc(' ', x, y);
67 __video_putc(' ', x, y);
68 __video_putc(' ', x, y);
69 __video_putc(' ', x, y);
70 } else if (c == '\v') {
71 switch ((*x) % 8) {
72 case 0:
73 __video_putc(' ', x, y);
74 case 7:
75 __video_putc(' ', x, y);
76 case 6:
77 __video_putc(' ', x, y);
78 case 5:
79 __video_putc(' ', x, y);
80 case 4:
81 __video_putc(' ', x, y);
82 case 3:
83 __video_putc(' ', x, y);
84 case 2:
85 __video_putc(' ', x, y);
86 case 1:
87 __video_putc(' ', x, y);
88 }
89 } else if (c == '\f') {
90 int i;
91 for (i = 0; i < lines * cols * 2; i += 2)
92 vidmem[i] = 0;
93 (*x) = 0;
94 (*y) = 0;
95 } else {
96 vidmem[((*x) + cols * (*y)) * 2] = c;
97 if (++(*x) >= cols) {
98 (*x) = 0;
99 if (++(*y) >= lines) {
100 scroll();
101 (*y)--;
102 }
103 }
104 }
105}
106
107static void video_putc(const char c)
108{
109 int x, y, pos;
110
111 x = orig_x;
112 y = orig_y;
113
114 __video_putc(c, &x, &y);
115
116 orig_x = x;
117 orig_y = y;
118
119 pos = (x + cols * y) * 2; /* Update cursor position */
120 outb_p(14, vidport);
121 outb_p(0xff & (pos >> 9), vidport+1);
122 outb_p(15, vidport);
123 outb_p(0xff & (pos >> 1), vidport+1);
124}
125
126static void video_puts(const char *s)
127{
128 int x, y, pos;
129 char c;
130
131 x = orig_x;
132 y = orig_y;
133
134 while ((c = *s++) != '\0')
135 __video_putc(c, &x, &y);
136
137 orig_x = x;
138 orig_y = y;
139
140 pos = (x + cols * y) * 2; /* Update cursor position */
141 outb_p(14, vidport);
142 outb_p(0xff & (pos >> 9), vidport+1);
143 outb_p(15, vidport);
144 outb_p(0xff & (pos >> 1), vidport+1);
145}
146
147int video_init(void)
148{
149 u16 pos;
150
151 static struct stdio_dev vga_dev;
152 static struct stdio_dev kbd_dev;
153
154 vidmem = (char *) 0xb8000;
155 vidport = 0x3d4;
156
157 lines = 25;
158 cols = 80;
159
160 outb_p(14, vidport);
161 pos = inb_p(vidport+1);
162 pos <<= 8;
163 outb_p(15, vidport);
164 pos |= inb_p(vidport+1);
165
166 orig_x = pos%cols;
167 orig_y = pos/cols;
168
169#if 0
170 printf("pos %x %d %d\n", pos, orig_x, orig_y);
171#endif
172 if (orig_y > lines)
173 orig_x = orig_y = 0;
174
175 memset(&vga_dev, 0, sizeof(vga_dev));
176 strcpy(vga_dev.name, "vga");
177 vga_dev.ext = 0;
178 vga_dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_SYSTEM;
179 vga_dev.putc = video_putc; /* 'putc' function */
180 vga_dev.puts = video_puts; /* 'puts' function */
181 vga_dev.tstc = NULL; /* 'tstc' function */
182 vga_dev.getc = NULL; /* 'getc' function */
183
184 if (stdio_register(&vga_dev) == 0)
185 return 1;
186
187 if (i8042_kbd_init())
188 return 1;
189
190 memset(&kbd_dev, 0, sizeof(kbd_dev));
191 strcpy(kbd_dev.name, "kbd");
192 kbd_dev.ext = 0;
193 kbd_dev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
194 kbd_dev.putc = NULL; /* 'putc' function */
195 kbd_dev.puts = NULL; /* 'puts' function */
196 kbd_dev.tstc = i8042_tstc; /* 'tstc' function */
197 kbd_dev.getc = i8042_getc; /* 'getc' function */
198
199 if (stdio_register(&kbd_dev) == 0)
200 return 1;
201
202 return 0;
203}
204
205
206int drv_video_init(void)
207{
208 return video_init();
209}