Merge branch 'upstream'
[pandora-kernel.git] / arch / x86_64 / kernel / early_printk.c
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>
9
10 /* Simple VGA output */
11
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
19
20 #define MAX_YPOS        max_ypos
21 #define MAX_XPOS        max_xpos
22
23 static int max_ypos = 25, max_xpos = 80;
24 static int current_ypos = 1, current_xpos = 0; 
25
26 static void early_vga_write(struct console *con, const char *str, unsigned n)
27 {
28         char c;
29         int  i, k, j;
30
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));
38                                 }
39                         }
40                         for (i = 0; i < MAX_XPOS; i++)
41                                 writew(0x720, VGABASE + 2*(MAX_XPOS*j + i));
42                         current_ypos = MAX_YPOS-1;
43                 }
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++;
54                         }
55                 }
56         }
57 }
58
59 static struct console early_vga_console = {
60         .name =         "earlyvga",
61         .write =        early_vga_write,
62         .flags =        CON_PRINTBUFFER,
63         .index =        -1,
64 };
65
66 /* Serial functions loosely based on a similar package from Klaus P. Gerlicher */ 
67
68 static int early_serial_base = 0x3f8;  /* ttyS0 */
69
70 #define XMTRDY          0x20
71
72 #define DLAB            0x80
73
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        */
85
86 static int early_serial_putc(unsigned char ch) 
87
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;
93
94
95 static void early_serial_write(struct console *con, const char *s, unsigned n)
96 {
97         while (*s && n-- > 0) { 
98                 early_serial_putc(*s); 
99                 if (*s == '\n') 
100                         early_serial_putc('\r'); 
101                 s++; 
102         } 
103
104
105 #define DEFAULT_BAUD 9600
106
107 static __init void early_serial_init(char *s)
108 {
109         unsigned char c; 
110         unsigned divisor;
111         unsigned baud = DEFAULT_BAUD;
112         char *e;
113
114         if (*s == ',')
115                 ++s;
116
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 };
123
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];
130                 }
131                 s += strcspn(s, ",");
132                 if (*s == ',')
133                         s++;
134         }
135
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 */
140
141         if (*s) {
142                 baud = simple_strtoul(s, &e, 0); 
143                 if (baud == 0 || s == e) 
144                         baud = DEFAULT_BAUD;
145         } 
146         
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);
153 }
154
155 static struct console early_serial_console = {
156         .name =         "earlyser",
157         .write =        early_serial_write,
158         .flags =        CON_PRINTBUFFER,
159         .index =        -1,
160 };
161
162 /* Console interface to a host file on AMD's SimNow! */
163
164 static int simnow_fd;
165
166 enum {
167         MAGIC1 = 0xBACCD00A,
168         MAGIC2 = 0xCA110000,
169         XOPEN = 5,
170         XWRITE = 4,
171 };
172
173 static noinline long simnow(long cmd, long a, long b, long c)
174 {
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;
180 }
181
182 void __init simnow_init(char *str)
183 {
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);
189 }
190
191 static void simnow_write(struct console *con, const char *s, unsigned n)
192 {
193         simnow(XWRITE, simnow_fd, (unsigned long)s, n);
194 }
195
196 static struct console simnow_console = {
197         .name =         "simnow",
198         .write =        simnow_write,
199         .flags =        CON_PRINTBUFFER,
200         .index =        -1,
201 };
202
203 /* Direct interface for emergencies */
204 struct console *early_console = &early_vga_console;
205 static int early_console_initialized = 0;
206
207 void early_printk(const char *fmt, ...)
208
209         char buf[512]; 
210         int n; 
211         va_list ap;
212
213         va_start(ap,fmt); 
214         n = vscnprintf(buf,512,fmt,ap);
215         early_console->write(early_console,buf,n);
216         va_end(ap); 
217
218
219 static int __initdata keep_early;
220
221 int __init setup_early_printk(char *opt) 
222 {  
223         char *space;
224         char buf[256]; 
225
226         if (early_console_initialized)
227                 return -1;
228
229         strlcpy(buf,opt,sizeof(buf)); 
230         space = strchr(buf, ' '); 
231         if (space)
232                 *space = 0; 
233
234         if (strstr(buf,"keep"))
235                 keep_early = 1; 
236
237         if (!strncmp(buf, "serial", 6)) { 
238                 early_serial_init(buf + 6);
239                 early_console = &early_serial_console;
240         } else if (!strncmp(buf, "ttyS", 4)) { 
241                 early_serial_init(buf);
242                 early_console = &early_serial_console;          
243         } else if (!strncmp(buf, "vga", 3)
244                    && SCREEN_INFO.orig_video_isVGA == 1) {
245                 max_xpos = SCREEN_INFO.orig_video_cols;
246                 max_ypos = SCREEN_INFO.orig_video_lines;
247                 early_console = &early_vga_console; 
248         } else if (!strncmp(buf, "simnow", 6)) {
249                 simnow_init(buf + 6);
250                 early_console = &simnow_console;
251                 keep_early = 1;
252         }
253         early_console_initialized = 1;
254         register_console(early_console);       
255         return 0;
256 }
257
258 void __init disable_early_printk(void)
259
260         if (!early_console_initialized || !early_console)
261                 return;
262         if (!keep_early) {
263                 printk("disabling early console\n");
264                 unregister_console(early_console);
265                 early_console_initialized = 0;
266         } else { 
267                 printk("keeping early console\n");
268         }
269
270
271 __setup("earlyprintk=", setup_early_printk);