ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.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 <asm/io.h>
6 #include <asm/processor.h>
7
8 /* Simple VGA output */
9
10 #ifdef __i386__
11 #define VGABASE         __pa(__PAGE_OFFSET + 0xb8000UL)
12 #else
13 #define VGABASE         0xffffffff800b8000UL
14 #endif
15
16 #define MAX_YPOS        25
17 #define MAX_XPOS        80
18
19 static int current_ypos = 1, current_xpos = 0; 
20
21 static void early_vga_write(struct console *con, const char *str, unsigned n)
22 {
23         char c;
24         int  i, k, j;
25
26         while ((c = *str++) != '\0' && n-- > 0) {
27                 if (current_ypos >= MAX_YPOS) {
28                         /* scroll 1 line up */
29                         for (k = 1, j = 0; k < MAX_YPOS; k++, j++) {
30                                 for (i = 0; i < MAX_XPOS; i++) {
31                                         writew(readw(VGABASE + 2*(MAX_XPOS*k + i)),
32                                                VGABASE + 2*(MAX_XPOS*j + i));
33                                 }
34                         }
35                         for (i = 0; i < MAX_XPOS; i++)
36                                 writew(0x720, VGABASE + 2*(MAX_XPOS*j + i));
37                         current_ypos = MAX_YPOS-1;
38                 }
39                 if (c == '\n') {
40                         current_xpos = 0;
41                         current_ypos++;
42                 } else if (c != '\r')  {
43                         writew(((0x7 << 8) | (unsigned short) c),
44                                VGABASE + 2*(MAX_XPOS*current_ypos +
45                                                 current_xpos++));
46                         if (current_xpos >= MAX_XPOS) {
47                                 current_xpos = 0;
48                                 current_ypos++;
49                         }
50                 }
51         }
52 }
53
54 static struct console early_vga_console = {
55         .name =         "earlyvga",
56         .write =        early_vga_write,
57         .flags =        CON_PRINTBUFFER,
58         .index =        -1,
59 };
60
61 /* Serial functions loosely based on a similar package from Klaus P. Gerlicher */ 
62
63 int early_serial_base = 0x3f8;  /* ttyS0 */ 
64
65 #define XMTRDY          0x20
66
67 #define DLAB            0x80
68
69 #define TXR             0       /*  Transmit register (WRITE) */
70 #define RXR             0       /*  Receive register  (READ)  */
71 #define IER             1       /*  Interrupt Enable          */
72 #define IIR             2       /*  Interrupt ID              */
73 #define FCR             2       /*  FIFO control              */
74 #define LCR             3       /*  Line control              */
75 #define MCR             4       /*  Modem control             */
76 #define LSR             5       /*  Line Status               */
77 #define MSR             6       /*  Modem Status              */
78 #define DLL             0       /*  Divisor Latch Low         */
79 #define DLH             1       /*  Divisor latch High        */
80
81 static int early_serial_putc(unsigned char ch) 
82
83         unsigned timeout = 0xffff; 
84         while ((inb(early_serial_base + LSR) & XMTRDY) == 0 && --timeout) 
85                 cpu_relax();
86         outb(ch, early_serial_base + TXR);
87         return timeout ? 0 : -1;
88
89
90 static void early_serial_write(struct console *con, const char *s, unsigned n)
91 {
92         while (*s && n-- > 0) { 
93                 early_serial_putc(*s); 
94                 if (*s == '\n') 
95                         early_serial_putc('\r'); 
96                 s++; 
97         } 
98
99
100 #define DEFAULT_BAUD 9600
101
102 static __init void early_serial_init(char *opt)
103 {
104         unsigned char c; 
105         unsigned divisor;
106         unsigned baud = DEFAULT_BAUD;
107         char *s, *e;
108
109         if (*opt == ',') 
110                 ++opt;
111
112         s = strsep(&opt, ","); 
113         if (s != NULL) { 
114                 unsigned port; 
115                 if (!strncmp(s,"0x",2)) {
116                         early_serial_base = simple_strtoul(s, &e, 16);
117                 } else {
118                         static int bases[] = { 0x3f8, 0x2f8 };
119
120                         if (!strncmp(s,"ttyS",4))
121                                 s += 4;
122                         port = simple_strtoul(s, &e, 10);
123                         if (port > 1 || s == e)
124                                 port = 0;
125                         early_serial_base = bases[port];
126                 }
127         }
128
129         outb(0x3, early_serial_base + LCR);     /* 8n1 */
130         outb(0, early_serial_base + IER);       /* no interrupt */
131         outb(0, early_serial_base + FCR);       /* no fifo */
132         outb(0x3, early_serial_base + MCR);     /* DTR + RTS */
133
134         s = strsep(&opt, ","); 
135         if (s != NULL) { 
136                 baud = simple_strtoul(s, &e, 0); 
137                 if (baud == 0 || s == e) 
138                         baud = DEFAULT_BAUD;
139         } 
140         
141         divisor = 115200 / baud; 
142         c = inb(early_serial_base + LCR); 
143         outb(c | DLAB, early_serial_base + LCR); 
144         outb(divisor & 0xff, early_serial_base + DLL); 
145         outb((divisor >> 8) & 0xff, early_serial_base + DLH); 
146         outb(c & ~DLAB, early_serial_base + LCR);
147 }
148
149 static struct console early_serial_console = {
150         .name =         "earlyser",
151         .write =        early_serial_write,
152         .flags =        CON_PRINTBUFFER,
153         .index =        -1,
154 };
155
156 /* Direct interface for emergencies */
157 struct console *early_console = &early_vga_console;
158 static int early_console_initialized = 0;
159
160 void early_printk(const char *fmt, ...)
161
162         char buf[512]; 
163         int n; 
164         va_list ap;
165
166         va_start(ap,fmt); 
167         n = vscnprintf(buf,512,fmt,ap);
168         early_console->write(early_console,buf,n);
169         va_end(ap); 
170
171
172 static int keep_early; 
173
174 int __init setup_early_printk(char *opt) 
175 {  
176         char *space;
177         char buf[256]; 
178
179         if (early_console_initialized)
180                 return -1;
181
182         opt = strchr(opt, '=') + 1;
183
184         strlcpy(buf,opt,sizeof(buf)); 
185         space = strchr(buf, ' '); 
186         if (space)
187                 *space = 0; 
188
189         if (strstr(buf,"keep"))
190                 keep_early = 1; 
191
192         if (!strncmp(buf, "serial", 6)) { 
193                 early_serial_init(buf + 6);
194                 early_console = &early_serial_console;
195         } else if (!strncmp(buf, "ttyS", 4)) { 
196                 early_serial_init(buf);
197                 early_console = &early_serial_console;          
198         } else if (!strncmp(buf, "vga", 3)) {
199                 early_console = &early_vga_console; 
200         } else {
201                 early_console = NULL;           
202                 return -1; 
203         }
204         early_console_initialized = 1;
205         register_console(early_console);       
206         return 0;
207 }
208
209 void __init disable_early_printk(void)
210
211         if (!early_console_initialized || !early_console)
212                 return;
213         if (!keep_early) {
214                 printk("disabling early console\n");
215                 unregister_console(early_console);
216                 early_console_initialized = 0;
217         } else { 
218                 printk("keeping early console\n");
219         }
220
221
222 __setup("earlyprintk=", setup_early_printk);