early_printk.c 6.1 KB

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