early_printk.c 9.7 KB

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