early_printk.c 6.1 KB

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