early_printk.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. baud = simple_strtoull(s, &e, 0);
  154. if (baud == 0 || s == e)
  155. baud = DEFAULT_BAUD;
  156. }
  157. /* Convert from baud to divisor value */
  158. divisor = 115200 / baud;
  159. /* These will always be IO based ports */
  160. serial_in = io_serial_in;
  161. serial_out = io_serial_out;
  162. /* Set up the HW */
  163. early_serial_hw_init(divisor);
  164. }
  165. #ifdef CONFIG_PCI
  166. static void mem32_serial_out(unsigned long addr, int offset, int value)
  167. {
  168. u32 __iomem *vaddr = (u32 __iomem *)addr;
  169. /* shift implied by pointer type */
  170. writel(value, vaddr + offset);
  171. }
  172. static unsigned int mem32_serial_in(unsigned long addr, int offset)
  173. {
  174. u32 __iomem *vaddr = (u32 __iomem *)addr;
  175. /* shift implied by pointer type */
  176. return readl(vaddr + offset);
  177. }
  178. /*
  179. * early_pci_serial_init()
  180. *
  181. * This function is invoked when the early_printk param starts with "pciserial"
  182. * The rest of the param should be ",B:D.F,baud" where B, D & F describe the
  183. * location of a PCI device that must be a UART device.
  184. */
  185. static __init void early_pci_serial_init(char *s)
  186. {
  187. unsigned divisor;
  188. unsigned long baud = DEFAULT_BAUD;
  189. u8 bus, slot, func;
  190. u32 classcode, bar0;
  191. u16 cmdreg;
  192. char *e;
  193. /*
  194. * First, part the param to get the BDF values
  195. */
  196. if (*s == ',')
  197. ++s;
  198. if (*s == 0)
  199. return;
  200. bus = (u8)simple_strtoul(s, &e, 16);
  201. s = e;
  202. if (*s != ':')
  203. return;
  204. ++s;
  205. slot = (u8)simple_strtoul(s, &e, 16);
  206. s = e;
  207. if (*s != '.')
  208. return;
  209. ++s;
  210. func = (u8)simple_strtoul(s, &e, 16);
  211. s = e;
  212. /* A baud might be following */
  213. if (*s == ',')
  214. s++;
  215. /*
  216. * Second, find the device from the BDF
  217. */
  218. cmdreg = read_pci_config(bus, slot, func, PCI_COMMAND);
  219. classcode = read_pci_config(bus, slot, func, PCI_CLASS_REVISION);
  220. bar0 = read_pci_config(bus, slot, func, PCI_BASE_ADDRESS_0);
  221. /*
  222. * Verify it is a UART type device
  223. */
  224. if (((classcode >> 16 != PCI_CLASS_COMMUNICATION_MODEM) &&
  225. (classcode >> 16 != PCI_CLASS_COMMUNICATION_SERIAL)) ||
  226. (((classcode >> 8) & 0xff) != 0x02)) /* 16550 I/F at BAR0 */
  227. return;
  228. /*
  229. * Determine if it is IO or memory mapped
  230. */
  231. if (bar0 & 0x01) {
  232. /* it is IO mapped */
  233. serial_in = io_serial_in;
  234. serial_out = io_serial_out;
  235. early_serial_base = bar0&0xfffffffc;
  236. write_pci_config(bus, slot, func, PCI_COMMAND,
  237. cmdreg|PCI_COMMAND_IO);
  238. } else {
  239. /* It is memory mapped - assume 32-bit alignment */
  240. serial_in = mem32_serial_in;
  241. serial_out = mem32_serial_out;
  242. /* WARNING! assuming the address is always in the first 4G */
  243. early_serial_base =
  244. (unsigned long)early_ioremap(bar0 & 0xfffffff0, 0x10);
  245. write_pci_config(bus, slot, func, PCI_COMMAND,
  246. cmdreg|PCI_COMMAND_MEMORY);
  247. }
  248. /*
  249. * Lastly, initalize the hardware
  250. */
  251. if (*s) {
  252. if (strcmp(s, "nocfg") == 0)
  253. /* Sometimes, we want to leave the UART alone
  254. * and assume the BIOS has set it up correctly.
  255. * "nocfg" tells us this is the case, and we
  256. * should do no more setup.
  257. */
  258. return;
  259. if (kstrtoul(s, 0, &baud) < 0 || baud == 0)
  260. baud = DEFAULT_BAUD;
  261. }
  262. /* Convert from baud to divisor value */
  263. divisor = 115200 / baud;
  264. /* Set up the HW */
  265. early_serial_hw_init(divisor);
  266. }
  267. #endif
  268. static struct console early_serial_console = {
  269. .name = "earlyser",
  270. .write = early_serial_write,
  271. .flags = CON_PRINTBUFFER,
  272. .index = -1,
  273. };
  274. static void early_console_register(struct console *con, int keep_early)
  275. {
  276. if (con->index != -1) {
  277. printk(KERN_CRIT "ERROR: earlyprintk= %s already used\n",
  278. con->name);
  279. return;
  280. }
  281. early_console = con;
  282. if (keep_early)
  283. early_console->flags &= ~CON_BOOT;
  284. else
  285. early_console->flags |= CON_BOOT;
  286. register_console(early_console);
  287. }
  288. static int __init setup_early_printk(char *buf)
  289. {
  290. int keep;
  291. if (!buf)
  292. return 0;
  293. if (early_console)
  294. return 0;
  295. keep = (strstr(buf, "keep") != NULL);
  296. while (*buf != '\0') {
  297. if (!strncmp(buf, "serial", 6)) {
  298. buf += 6;
  299. early_serial_init(buf);
  300. early_console_register(&early_serial_console, keep);
  301. if (!strncmp(buf, ",ttyS", 5))
  302. buf += 5;
  303. }
  304. if (!strncmp(buf, "ttyS", 4)) {
  305. early_serial_init(buf + 4);
  306. early_console_register(&early_serial_console, keep);
  307. }
  308. #ifdef CONFIG_PCI
  309. if (!strncmp(buf, "pciserial", 9)) {
  310. early_pci_serial_init(buf + 9);
  311. early_console_register(&early_serial_console, keep);
  312. buf += 9; /* Keep from match the above "serial" */
  313. }
  314. #endif
  315. if (!strncmp(buf, "vga", 3) &&
  316. boot_params.screen_info.orig_video_isVGA == 1) {
  317. max_xpos = boot_params.screen_info.orig_video_cols;
  318. max_ypos = boot_params.screen_info.orig_video_lines;
  319. current_ypos = boot_params.screen_info.orig_y;
  320. early_console_register(&early_vga_console, keep);
  321. }
  322. #ifdef CONFIG_EARLY_PRINTK_DBGP
  323. if (!strncmp(buf, "dbgp", 4) && !early_dbgp_init(buf + 4))
  324. early_console_register(&early_dbgp_console, keep);
  325. #endif
  326. #ifdef CONFIG_HVC_XEN
  327. if (!strncmp(buf, "xen", 3))
  328. early_console_register(&xenboot_console, keep);
  329. #endif
  330. #ifdef CONFIG_EARLY_PRINTK_EFI
  331. if (!strncmp(buf, "efi", 3))
  332. early_console_register(&early_efi_console, keep);
  333. #endif
  334. buf++;
  335. }
  336. return 0;
  337. }
  338. early_param("earlyprintk", setup_early_printk);