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