early_printk.c 5.2 KB

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