early_printk.c 6.4 KB

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