early_printk.c 5.4 KB

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