early_printk.c 9.6 KB

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