early_printk.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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/intel-mid.h>
  18. #include <asm/pgtable.h>
  19. #include <linux/usb/ehci_def.h>
  20. #include <linux/efi.h>
  21. #include <asm/efi.h>
  22. #include <asm/pci_x86.h>
  23. /* Simple VGA output */
  24. #define VGABASE (__ISA_IO_base + 0xb8000)
  25. static int max_ypos = 25, max_xpos = 80;
  26. static int current_ypos = 25, current_xpos;
  27. static void early_vga_write(struct console *con, const char *str, unsigned n)
  28. {
  29. char c;
  30. int i, k, j;
  31. while ((c = *str++) != '\0' && n-- > 0) {
  32. if (current_ypos >= max_ypos) {
  33. /* scroll 1 line up */
  34. for (k = 1, j = 0; k < max_ypos; k++, j++) {
  35. for (i = 0; i < max_xpos; i++) {
  36. writew(readw(VGABASE+2*(max_xpos*k+i)),
  37. VGABASE + 2*(max_xpos*j + i));
  38. }
  39. }
  40. for (i = 0; i < max_xpos; i++)
  41. writew(0x720, VGABASE + 2*(max_xpos*j + i));
  42. current_ypos = max_ypos-1;
  43. }
  44. #ifdef CONFIG_KGDB_KDB
  45. if (c == '\b') {
  46. if (current_xpos > 0)
  47. current_xpos--;
  48. } else if (c == '\r') {
  49. current_xpos = 0;
  50. } else
  51. #endif
  52. if (c == '\n') {
  53. current_xpos = 0;
  54. current_ypos++;
  55. } else if (c != '\r') {
  56. writew(((0x7 << 8) | (unsigned short) c),
  57. VGABASE + 2*(max_xpos*current_ypos +
  58. current_xpos++));
  59. if (current_xpos >= max_xpos) {
  60. current_xpos = 0;
  61. current_ypos++;
  62. }
  63. }
  64. }
  65. }
  66. static struct console early_vga_console = {
  67. .name = "earlyvga",
  68. .write = early_vga_write,
  69. .flags = CON_PRINTBUFFER,
  70. .index = -1,
  71. };
  72. /* Serial functions loosely based on a similar package from Klaus P. Gerlicher */
  73. static unsigned long early_serial_base = 0x3f8; /* ttyS0 */
  74. #define XMTRDY 0x20
  75. #define DLAB 0x80
  76. #define TXR 0 /* Transmit register (WRITE) */
  77. #define RXR 0 /* Receive register (READ) */
  78. #define IER 1 /* Interrupt Enable */
  79. #define IIR 2 /* Interrupt ID */
  80. #define FCR 2 /* FIFO control */
  81. #define LCR 3 /* Line control */
  82. #define MCR 4 /* Modem control */
  83. #define LSR 5 /* Line Status */
  84. #define MSR 6 /* Modem Status */
  85. #define DLL 0 /* Divisor Latch Low */
  86. #define DLH 1 /* Divisor latch High */
  87. static unsigned int io_serial_in(unsigned long addr, int offset)
  88. {
  89. return inb(addr + offset);
  90. }
  91. static void io_serial_out(unsigned long addr, int offset, int value)
  92. {
  93. outb(value, addr + offset);
  94. }
  95. static unsigned int (*serial_in)(unsigned long addr, int offset) = io_serial_in;
  96. static void (*serial_out)(unsigned long addr, int offset, int value) = io_serial_out;
  97. static int early_serial_putc(unsigned char ch)
  98. {
  99. unsigned timeout = 0xffff;
  100. while ((serial_in(early_serial_base, LSR) & XMTRDY) == 0 && --timeout)
  101. cpu_relax();
  102. serial_out(early_serial_base, TXR, ch);
  103. return timeout ? 0 : -1;
  104. }
  105. static void early_serial_write(struct console *con, const char *s, unsigned n)
  106. {
  107. while (*s && n-- > 0) {
  108. if (*s == '\n')
  109. early_serial_putc('\r');
  110. early_serial_putc(*s);
  111. s++;
  112. }
  113. }
  114. static __init void early_serial_hw_init(unsigned divisor)
  115. {
  116. unsigned char c;
  117. serial_out(early_serial_base, LCR, 0x3); /* 8n1 */
  118. serial_out(early_serial_base, IER, 0); /* no interrupt */
  119. serial_out(early_serial_base, FCR, 0); /* no fifo */
  120. serial_out(early_serial_base, MCR, 0x3); /* DTR + RTS */
  121. c = serial_in(early_serial_base, LCR);
  122. serial_out(early_serial_base, LCR, c | DLAB);
  123. serial_out(early_serial_base, DLL, divisor & 0xff);
  124. serial_out(early_serial_base, DLH, (divisor >> 8) & 0xff);
  125. serial_out(early_serial_base, LCR, c & ~DLAB);
  126. }
  127. #define DEFAULT_BAUD 9600
  128. static __init void early_serial_init(char *s)
  129. {
  130. unsigned divisor;
  131. unsigned long baud = DEFAULT_BAUD;
  132. char *e;
  133. if (*s == ',')
  134. ++s;
  135. if (*s) {
  136. unsigned port;
  137. if (!strncmp(s, "0x", 2)) {
  138. early_serial_base = simple_strtoul(s, &e, 16);
  139. } else {
  140. static const int __initconst bases[] = { 0x3f8, 0x2f8 };
  141. if (!strncmp(s, "ttyS", 4))
  142. s += 4;
  143. port = simple_strtoul(s, &e, 10);
  144. if (port > 1 || s == e)
  145. port = 0;
  146. early_serial_base = bases[port];
  147. }
  148. s += strcspn(s, ",");
  149. if (*s == ',')
  150. s++;
  151. }
  152. if (*s) {
  153. if (kstrtoul(s, 0, &baud) < 0 || baud == 0)
  154. baud = DEFAULT_BAUD;
  155. }
  156. /* Convert from baud to divisor value */
  157. divisor = 115200 / baud;
  158. /* These will always be IO based ports */
  159. serial_in = io_serial_in;
  160. serial_out = io_serial_out;
  161. /* Set up the HW */
  162. early_serial_hw_init(divisor);
  163. }
  164. #ifdef CONFIG_PCI
  165. static void mem32_serial_out(unsigned long addr, int offset, int value)
  166. {
  167. u32 *vaddr = (u32 *)addr;
  168. /* shift implied by pointer type */
  169. writel(value, vaddr + offset);
  170. }
  171. static unsigned int mem32_serial_in(unsigned long addr, int offset)
  172. {
  173. u32 *vaddr = (u32 *)addr;
  174. /* shift implied by pointer type */
  175. return readl(vaddr + offset);
  176. }
  177. /*
  178. * early_pci_serial_init()
  179. *
  180. * This function is invoked when the early_printk param starts with "pciserial"
  181. * The rest of the param should be ",B:D.F,baud" where B, D & F describe the
  182. * location of a PCI device that must be a UART device.
  183. */
  184. static __init void early_pci_serial_init(char *s)
  185. {
  186. unsigned divisor;
  187. unsigned long baud = DEFAULT_BAUD;
  188. u8 bus, slot, func;
  189. u32 classcode, bar0;
  190. u16 cmdreg;
  191. char *e;
  192. /*
  193. * First, part the param to get the BDF values
  194. */
  195. if (*s == ',')
  196. ++s;
  197. if (*s == 0)
  198. return;
  199. bus = (u8)simple_strtoul(s, &e, 16);
  200. s = e;
  201. if (*s != ':')
  202. return;
  203. ++s;
  204. slot = (u8)simple_strtoul(s, &e, 16);
  205. s = e;
  206. if (*s != '.')
  207. return;
  208. ++s;
  209. func = (u8)simple_strtoul(s, &e, 16);
  210. s = e;
  211. /* A baud might be following */
  212. if (*s == ',')
  213. s++;
  214. /*
  215. * Second, find the device from the BDF
  216. */
  217. cmdreg = read_pci_config(bus, slot, func, PCI_COMMAND);
  218. classcode = read_pci_config(bus, slot, func, PCI_CLASS_REVISION);
  219. bar0 = read_pci_config(bus, slot, func, PCI_BASE_ADDRESS_0);
  220. /*
  221. * Verify it is a UART type device
  222. */
  223. if (((classcode >> 16 != PCI_CLASS_COMMUNICATION_MODEM) &&
  224. (classcode >> 16 != PCI_CLASS_COMMUNICATION_SERIAL)) ||
  225. (((classcode >> 8) & 0xff) != 0x02)) /* 16550 I/F at BAR0 */
  226. return;
  227. /*
  228. * Determine if it is IO or memory mapped
  229. */
  230. if (bar0 & 0x01) {
  231. /* it is IO mapped */
  232. serial_in = io_serial_in;
  233. serial_out = io_serial_out;
  234. early_serial_base = bar0&0xfffffffc;
  235. write_pci_config(bus, slot, func, PCI_COMMAND,
  236. cmdreg|PCI_COMMAND_IO);
  237. } else {
  238. /* It is memory mapped - assume 32-bit alignment */
  239. serial_in = mem32_serial_in;
  240. serial_out = mem32_serial_out;
  241. /* WARNING! assuming the address is always in the first 4G */
  242. early_serial_base =
  243. (unsigned long)early_ioremap(bar0 & 0xfffffff0, 0x10);
  244. write_pci_config(bus, slot, func, PCI_COMMAND,
  245. cmdreg|PCI_COMMAND_MEMORY);
  246. }
  247. /*
  248. * Lastly, initalize the hardware
  249. */
  250. if (*s) {
  251. if (strcmp(s, "nocfg") == 0)
  252. /* Sometimes, we want to leave the UART alone
  253. * and assume the BIOS has set it up correctly.
  254. * "nocfg" tells us this is the case, and we
  255. * should do no more setup.
  256. */
  257. return;
  258. if (kstrtoul(s, 0, &baud) < 0 || baud == 0)
  259. baud = DEFAULT_BAUD;
  260. }
  261. /* Convert from baud to divisor value */
  262. divisor = 115200 / baud;
  263. /* Set up the HW */
  264. early_serial_hw_init(divisor);
  265. }
  266. #endif
  267. static struct console early_serial_console = {
  268. .name = "earlyser",
  269. .write = early_serial_write,
  270. .flags = CON_PRINTBUFFER,
  271. .index = -1,
  272. };
  273. static inline void early_console_register(struct console *con, int keep_early)
  274. {
  275. if (con->index != -1) {
  276. printk(KERN_CRIT "ERROR: earlyprintk= %s already used\n",
  277. con->name);
  278. return;
  279. }
  280. early_console = con;
  281. if (keep_early)
  282. early_console->flags &= ~CON_BOOT;
  283. else
  284. early_console->flags |= CON_BOOT;
  285. register_console(early_console);
  286. }
  287. static int __init setup_early_printk(char *buf)
  288. {
  289. int keep;
  290. if (!buf)
  291. return 0;
  292. if (early_console)
  293. return 0;
  294. keep = (strstr(buf, "keep") != NULL);
  295. while (*buf != '\0') {
  296. if (!strncmp(buf, "serial", 6)) {
  297. buf += 6;
  298. early_serial_init(buf);
  299. early_console_register(&early_serial_console, keep);
  300. if (!strncmp(buf, ",ttyS", 5))
  301. buf += 5;
  302. }
  303. if (!strncmp(buf, "ttyS", 4)) {
  304. early_serial_init(buf + 4);
  305. early_console_register(&early_serial_console, keep);
  306. }
  307. #ifdef CONFIG_PCI
  308. if (!strncmp(buf, "pciserial", 9)) {
  309. early_pci_serial_init(buf + 9);
  310. early_console_register(&early_serial_console, keep);
  311. buf += 9; /* Keep from match the above "serial" */
  312. }
  313. #endif
  314. if (!strncmp(buf, "vga", 3) &&
  315. boot_params.screen_info.orig_video_isVGA == 1) {
  316. max_xpos = boot_params.screen_info.orig_video_cols;
  317. max_ypos = boot_params.screen_info.orig_video_lines;
  318. current_ypos = boot_params.screen_info.orig_y;
  319. early_console_register(&early_vga_console, keep);
  320. }
  321. #ifdef CONFIG_EARLY_PRINTK_DBGP
  322. if (!strncmp(buf, "dbgp", 4) && !early_dbgp_init(buf + 4))
  323. early_console_register(&early_dbgp_console, keep);
  324. #endif
  325. #ifdef CONFIG_HVC_XEN
  326. if (!strncmp(buf, "xen", 3))
  327. early_console_register(&xenboot_console, keep);
  328. #endif
  329. #ifdef CONFIG_EARLY_PRINTK_EFI
  330. if (!strncmp(buf, "efi", 3))
  331. early_console_register(&early_efi_console, keep);
  332. #endif
  333. buf++;
  334. }
  335. return 0;
  336. }
  337. early_param("earlyprintk", setup_early_printk);