[PATCH] x86-64: i386/x86-64: Fix time going twice as fast problem on ATI Xpress chipsets
[linux-2.6/mini2440.git] / arch / x86_64 / kernel / early_printk.c
blob9cd968dd0f5a4bd3b9d4b83525f94ee5386fec14
1 #include <linux/console.h>
2 #include <linux/kernel.h>
3 #include <linux/init.h>
4 #include <linux/string.h>
5 #include <linux/tty.h>
6 #include <asm/io.h>
7 #include <asm/processor.h>
8 #include <asm/fcntl.h>
10 /* Simple VGA output */
12 #ifdef __i386__
13 #include <asm/setup.h>
14 #define VGABASE (__ISA_IO_base + 0xb8000)
15 #else
16 #include <asm/bootsetup.h>
17 #define VGABASE ((void __iomem *)0xffffffff800b8000UL)
18 #endif
20 #define MAX_YPOS max_ypos
21 #define MAX_XPOS max_xpos
23 static int max_ypos = 25, max_xpos = 80;
24 static int current_ypos = 1, current_xpos = 0;
26 static void early_vga_write(struct console *con, const char *str, unsigned n)
28 char c;
29 int i, k, j;
31 while ((c = *str++) != '\0' && n-- > 0) {
32 if (current_ypos >= MAX_YPOS) {
33 /* scroll 1 line up */
34 for (k = 1, j = 0; k < MAX_YPOS; k++, j++) {
35 for (i = 0; i < MAX_XPOS; i++) {
36 writew(readw(VGABASE + 2*(MAX_XPOS*k + i)),
37 VGABASE + 2*(MAX_XPOS*j + i));
40 for (i = 0; i < MAX_XPOS; i++)
41 writew(0x720, VGABASE + 2*(MAX_XPOS*j + i));
42 current_ypos = MAX_YPOS-1;
44 if (c == '\n') {
45 current_xpos = 0;
46 current_ypos++;
47 } else if (c != '\r') {
48 writew(((0x7 << 8) | (unsigned short) c),
49 VGABASE + 2*(MAX_XPOS*current_ypos +
50 current_xpos++));
51 if (current_xpos >= MAX_XPOS) {
52 current_xpos = 0;
53 current_ypos++;
59 static struct console early_vga_console = {
60 .name = "earlyvga",
61 .write = early_vga_write,
62 .flags = CON_PRINTBUFFER,
63 .index = -1,
66 /* Serial functions loosely based on a similar package from Klaus P. Gerlicher */
68 static int early_serial_base = 0x3f8; /* ttyS0 */
70 #define XMTRDY 0x20
72 #define DLAB 0x80
74 #define TXR 0 /* Transmit register (WRITE) */
75 #define RXR 0 /* Receive register (READ) */
76 #define IER 1 /* Interrupt Enable */
77 #define IIR 2 /* Interrupt ID */
78 #define FCR 2 /* FIFO control */
79 #define LCR 3 /* Line control */
80 #define MCR 4 /* Modem control */
81 #define LSR 5 /* Line Status */
82 #define MSR 6 /* Modem Status */
83 #define DLL 0 /* Divisor Latch Low */
84 #define DLH 1 /* Divisor latch High */
86 static int early_serial_putc(unsigned char ch)
88 unsigned timeout = 0xffff;
89 while ((inb(early_serial_base + LSR) & XMTRDY) == 0 && --timeout)
90 cpu_relax();
91 outb(ch, early_serial_base + TXR);
92 return timeout ? 0 : -1;
95 static void early_serial_write(struct console *con, const char *s, unsigned n)
97 while (*s && n-- > 0) {
98 early_serial_putc(*s);
99 if (*s == '\n')
100 early_serial_putc('\r');
101 s++;
105 #define DEFAULT_BAUD 9600
107 static __init void early_serial_init(char *s)
109 unsigned char c;
110 unsigned divisor;
111 unsigned baud = DEFAULT_BAUD;
112 char *e;
114 if (*s == ',')
115 ++s;
117 if (*s) {
118 unsigned port;
119 if (!strncmp(s,"0x",2)) {
120 early_serial_base = simple_strtoul(s, &e, 16);
121 } else {
122 static int bases[] = { 0x3f8, 0x2f8 };
124 if (!strncmp(s,"ttyS",4))
125 s += 4;
126 port = simple_strtoul(s, &e, 10);
127 if (port > 1 || s == e)
128 port = 0;
129 early_serial_base = bases[port];
131 s += strcspn(s, ",");
132 if (*s == ',')
133 s++;
136 outb(0x3, early_serial_base + LCR); /* 8n1 */
137 outb(0, early_serial_base + IER); /* no interrupt */
138 outb(0, early_serial_base + FCR); /* no fifo */
139 outb(0x3, early_serial_base + MCR); /* DTR + RTS */
141 if (*s) {
142 baud = simple_strtoul(s, &e, 0);
143 if (baud == 0 || s == e)
144 baud = DEFAULT_BAUD;
147 divisor = 115200 / baud;
148 c = inb(early_serial_base + LCR);
149 outb(c | DLAB, early_serial_base + LCR);
150 outb(divisor & 0xff, early_serial_base + DLL);
151 outb((divisor >> 8) & 0xff, early_serial_base + DLH);
152 outb(c & ~DLAB, early_serial_base + LCR);
155 static struct console early_serial_console = {
156 .name = "earlyser",
157 .write = early_serial_write,
158 .flags = CON_PRINTBUFFER,
159 .index = -1,
162 /* Console interface to a host file on AMD's SimNow! */
164 static int simnow_fd;
166 enum {
167 MAGIC1 = 0xBACCD00A,
168 MAGIC2 = 0xCA110000,
169 XOPEN = 5,
170 XWRITE = 4,
173 static noinline long simnow(long cmd, long a, long b, long c)
175 long ret;
176 asm volatile("cpuid" :
177 "=a" (ret) :
178 "b" (a), "c" (b), "d" (c), "0" (MAGIC1), "D" (cmd + MAGIC2));
179 return ret;
182 void __init simnow_init(char *str)
184 char *fn = "klog";
185 if (*str == '=')
186 fn = ++str;
187 /* error ignored */
188 simnow_fd = simnow(XOPEN, (unsigned long)fn, O_WRONLY|O_APPEND|O_CREAT, 0644);
191 static void simnow_write(struct console *con, const char *s, unsigned n)
193 simnow(XWRITE, simnow_fd, (unsigned long)s, n);
196 static struct console simnow_console = {
197 .name = "simnow",
198 .write = simnow_write,
199 .flags = CON_PRINTBUFFER,
200 .index = -1,
203 /* Direct interface for emergencies */
204 struct console *early_console = &early_vga_console;
205 static int early_console_initialized = 0;
207 void early_printk(const char *fmt, ...)
209 char buf[512];
210 int n;
211 va_list ap;
213 va_start(ap,fmt);
214 n = vscnprintf(buf,512,fmt,ap);
215 early_console->write(early_console,buf,n);
216 va_end(ap);
219 static int keep_early;
221 int __init setup_early_printk(char *opt)
223 char *space;
224 char buf[256];
226 if (early_console_initialized)
227 return -1;
229 opt = strchr(opt, '=') + 1;
231 strlcpy(buf,opt,sizeof(buf));
232 space = strchr(buf, ' ');
233 if (space)
234 *space = 0;
236 if (strstr(buf,"keep"))
237 keep_early = 1;
239 if (!strncmp(buf, "serial", 6)) {
240 early_serial_init(buf + 6);
241 early_console = &early_serial_console;
242 } else if (!strncmp(buf, "ttyS", 4)) {
243 early_serial_init(buf);
244 early_console = &early_serial_console;
245 } else if (!strncmp(buf, "vga", 3)
246 && SCREEN_INFO.orig_video_isVGA == 1) {
247 max_xpos = SCREEN_INFO.orig_video_cols;
248 max_ypos = SCREEN_INFO.orig_video_lines;
249 early_console = &early_vga_console;
250 } else if (!strncmp(buf, "simnow", 6)) {
251 simnow_init(buf + 6);
252 early_console = &simnow_console;
253 keep_early = 1;
255 early_console_initialized = 1;
256 register_console(early_console);
257 return 0;
260 void __init disable_early_printk(void)
262 if (!early_console_initialized || !early_console)
263 return;
264 if (!keep_early) {
265 printk("disabling early console\n");
266 unregister_console(early_console);
267 early_console_initialized = 0;
268 } else {
269 printk("keeping early console\n");
273 __setup("earlyprintk=", setup_early_printk);