8250_early.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Early serial console for 8250/16550 devices
  3. *
  4. * (c) Copyright 2004 Hewlett-Packard Development Company, L.P.
  5. * Bjorn Helgaas <bjorn.helgaas@hp.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * Based on the 8250.c serial driver, Copyright (C) 2001 Russell King,
  12. * and on early_printk.c by Andi Kleen.
  13. *
  14. * This is for use before the serial driver has initialized, in
  15. * particular, before the UARTs have been discovered and named.
  16. * Instead of specifying the console device as, e.g., "ttyS0",
  17. * we locate the device directly by its MMIO or I/O port address.
  18. *
  19. * The user can specify the device directly, e.g.,
  20. * earlycon=uart8250,io,0x3f8,9600n8
  21. * earlycon=uart8250,mmio,0xff5e0000,115200n8
  22. * earlycon=uart8250,mmio32,0xff5e0000,115200n8
  23. * or
  24. * console=uart8250,io,0x3f8,9600n8
  25. * console=uart8250,mmio,0xff5e0000,115200n8
  26. * console=uart8250,mmio32,0xff5e0000,115200n8
  27. */
  28. #include <linux/tty.h>
  29. #include <linux/init.h>
  30. #include <linux/console.h>
  31. #include <linux/serial_core.h>
  32. #include <linux/serial_reg.h>
  33. #include <linux/serial.h>
  34. #include <linux/serial_8250.h>
  35. #include <asm/io.h>
  36. #include <asm/serial.h>
  37. #ifdef CONFIG_FIX_EARLYCON_MEM
  38. #include <asm/pgtable.h>
  39. #include <asm/fixmap.h>
  40. #endif
  41. struct early_serial8250_device {
  42. struct uart_port port;
  43. char options[16]; /* e.g., 115200n8 */
  44. unsigned int baud;
  45. };
  46. static struct early_serial8250_device early_device;
  47. static unsigned int __init serial_in(struct uart_port *port, int offset)
  48. {
  49. switch (port->iotype) {
  50. case UPIO_MEM:
  51. return readb(port->membase + offset);
  52. case UPIO_MEM32:
  53. return readl(port->membase + (offset << 2));
  54. case UPIO_PORT:
  55. return inb(port->iobase + offset);
  56. default:
  57. return 0;
  58. }
  59. }
  60. static void __init serial_out(struct uart_port *port, int offset, int value)
  61. {
  62. switch (port->iotype) {
  63. case UPIO_MEM:
  64. writeb(value, port->membase + offset);
  65. break;
  66. case UPIO_MEM32:
  67. writel(value, port->membase + (offset << 2));
  68. break;
  69. case UPIO_PORT:
  70. outb(value, port->iobase + offset);
  71. break;
  72. }
  73. }
  74. #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
  75. static void __init wait_for_xmitr(struct uart_port *port)
  76. {
  77. unsigned int status;
  78. for (;;) {
  79. status = serial_in(port, UART_LSR);
  80. if ((status & BOTH_EMPTY) == BOTH_EMPTY)
  81. return;
  82. cpu_relax();
  83. }
  84. }
  85. static void __init serial_putc(struct uart_port *port, int c)
  86. {
  87. wait_for_xmitr(port);
  88. serial_out(port, UART_TX, c);
  89. }
  90. static void __init early_serial8250_write(struct console *console,
  91. const char *s, unsigned int count)
  92. {
  93. struct uart_port *port = &early_device.port;
  94. unsigned int ier;
  95. /* Save the IER and disable interrupts */
  96. ier = serial_in(port, UART_IER);
  97. serial_out(port, UART_IER, 0);
  98. uart_console_write(port, s, count, serial_putc);
  99. /* Wait for transmitter to become empty and restore the IER */
  100. wait_for_xmitr(port);
  101. serial_out(port, UART_IER, ier);
  102. }
  103. static unsigned int __init probe_baud(struct uart_port *port)
  104. {
  105. unsigned char lcr, dll, dlm;
  106. unsigned int quot;
  107. lcr = serial_in(port, UART_LCR);
  108. serial_out(port, UART_LCR, lcr | UART_LCR_DLAB);
  109. dll = serial_in(port, UART_DLL);
  110. dlm = serial_in(port, UART_DLM);
  111. serial_out(port, UART_LCR, lcr);
  112. quot = (dlm << 8) | dll;
  113. return (port->uartclk / 16) / quot;
  114. }
  115. static void __init init_port(struct early_serial8250_device *device)
  116. {
  117. struct uart_port *port = &device->port;
  118. unsigned int divisor;
  119. unsigned char c;
  120. serial_out(port, UART_LCR, 0x3); /* 8n1 */
  121. serial_out(port, UART_IER, 0); /* no interrupt */
  122. serial_out(port, UART_FCR, 0); /* no fifo */
  123. serial_out(port, UART_MCR, 0x3); /* DTR + RTS */
  124. divisor = port->uartclk / (16 * device->baud);
  125. c = serial_in(port, UART_LCR);
  126. serial_out(port, UART_LCR, c | UART_LCR_DLAB);
  127. serial_out(port, UART_DLL, divisor & 0xff);
  128. serial_out(port, UART_DLM, (divisor >> 8) & 0xff);
  129. serial_out(port, UART_LCR, c & ~UART_LCR_DLAB);
  130. }
  131. static int __init parse_options(struct early_serial8250_device *device,
  132. char *options)
  133. {
  134. struct uart_port *port = &device->port;
  135. int mmio, mmio32, length;
  136. if (!options)
  137. return -ENODEV;
  138. port->uartclk = BASE_BAUD * 16;
  139. mmio = !strncmp(options, "mmio,", 5);
  140. mmio32 = !strncmp(options, "mmio32,", 7);
  141. if (mmio || mmio32) {
  142. port->iotype = (mmio ? UPIO_MEM : UPIO_MEM32);
  143. port->mapbase = simple_strtoul(options + (mmio ? 5 : 7),
  144. &options, 0);
  145. if (mmio32)
  146. port->regshift = 2;
  147. #ifdef CONFIG_FIX_EARLYCON_MEM
  148. set_fixmap_nocache(FIX_EARLYCON_MEM_BASE,
  149. port->mapbase & PAGE_MASK);
  150. port->membase =
  151. (void __iomem *)__fix_to_virt(FIX_EARLYCON_MEM_BASE);
  152. port->membase += port->mapbase & ~PAGE_MASK;
  153. #else
  154. port->membase = ioremap_nocache(port->mapbase, 64);
  155. if (!port->membase) {
  156. printk(KERN_ERR "%s: Couldn't ioremap 0x%llx\n",
  157. __func__,
  158. (unsigned long long) port->mapbase);
  159. return -ENOMEM;
  160. }
  161. #endif
  162. } else if (!strncmp(options, "io,", 3)) {
  163. port->iotype = UPIO_PORT;
  164. port->iobase = simple_strtoul(options + 3, &options, 0);
  165. mmio = 0;
  166. } else
  167. return -EINVAL;
  168. options = strchr(options, ',');
  169. if (options) {
  170. options++;
  171. device->baud = simple_strtoul(options, NULL, 0);
  172. length = min(strcspn(options, " "), sizeof(device->options));
  173. strncpy(device->options, options, length);
  174. } else {
  175. device->baud = probe_baud(port);
  176. snprintf(device->options, sizeof(device->options), "%u",
  177. device->baud);
  178. }
  179. if (mmio || mmio32)
  180. printk(KERN_INFO
  181. "Early serial console at MMIO%s 0x%llx (options '%s')\n",
  182. mmio32 ? "32" : "",
  183. (unsigned long long)port->mapbase,
  184. device->options);
  185. else
  186. printk(KERN_INFO
  187. "Early serial console at I/O port 0x%lx (options '%s')\n",
  188. port->iobase,
  189. device->options);
  190. return 0;
  191. }
  192. static struct console early_serial8250_console __initdata = {
  193. .name = "uart",
  194. .write = early_serial8250_write,
  195. .flags = CON_PRINTBUFFER | CON_BOOT,
  196. .index = -1,
  197. };
  198. static int __init early_serial8250_setup(char *options)
  199. {
  200. struct early_serial8250_device *device = &early_device;
  201. int err;
  202. if (device->port.membase || device->port.iobase)
  203. return 0;
  204. err = parse_options(device, options);
  205. if (err < 0)
  206. return err;
  207. init_port(device);
  208. return 0;
  209. }
  210. int __init setup_early_serial8250_console(char *cmdline)
  211. {
  212. char *options;
  213. int err;
  214. options = strstr(cmdline, "uart8250,");
  215. if (!options) {
  216. options = strstr(cmdline, "uart,");
  217. if (!options)
  218. return 0;
  219. }
  220. options = strchr(cmdline, ',') + 1;
  221. err = early_serial8250_setup(options);
  222. if (err < 0)
  223. return err;
  224. register_console(&early_serial8250_console);
  225. return 0;
  226. }
  227. int serial8250_find_port_for_earlycon(void)
  228. {
  229. struct early_serial8250_device *device = &early_device;
  230. struct uart_port *port = &device->port;
  231. int line;
  232. int ret;
  233. if (!device->port.membase && !device->port.iobase)
  234. return -ENODEV;
  235. line = serial8250_find_port(port);
  236. if (line < 0)
  237. return -ENODEV;
  238. ret = update_console_cmdline("uart", 8250,
  239. "ttyS", line, device->options);
  240. if (ret < 0)
  241. ret = update_console_cmdline("uart", 0,
  242. "ttyS", line, device->options);
  243. return ret;
  244. }
  245. early_param("earlycon", setup_early_serial8250_console);