early_printk.c 6.1 KB

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