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