8250_early.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. * or
  23. * console=uart8250,io,0x3f8,9600n8
  24. * console=uart8250,mmio,0xff5e0000,115200n8
  25. */
  26. #include <linux/tty.h>
  27. #include <linux/init.h>
  28. #include <linux/console.h>
  29. #include <linux/serial_core.h>
  30. #include <linux/serial_reg.h>
  31. #include <linux/serial.h>
  32. #include <linux/serial_8250.h>
  33. #include <asm/io.h>
  34. #include <asm/serial.h>
  35. #ifdef CONFIG_FIX_EARLYCON_MEM
  36. #include <asm/pgtable.h>
  37. #include <asm/fixmap.h>
  38. #endif
  39. struct early_serial8250_device {
  40. struct uart_port port;
  41. char options[16]; /* e.g., 115200n8 */
  42. unsigned int baud;
  43. };
  44. static struct early_serial8250_device early_device;
  45. static unsigned int __init serial_in(struct uart_port *port, int offset)
  46. {
  47. if (port->iotype == UPIO_MEM)
  48. return readb(port->membase + offset);
  49. else
  50. return inb(port->iobase + offset);
  51. }
  52. static void __init serial_out(struct uart_port *port, int offset, int value)
  53. {
  54. if (port->iotype == UPIO_MEM)
  55. writeb(value, port->membase + offset);
  56. else
  57. outb(value, port->iobase + offset);
  58. }
  59. #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
  60. static void __init wait_for_xmitr(struct uart_port *port)
  61. {
  62. unsigned int status;
  63. for (;;) {
  64. status = serial_in(port, UART_LSR);
  65. if ((status & BOTH_EMPTY) == BOTH_EMPTY)
  66. return;
  67. cpu_relax();
  68. }
  69. }
  70. static void __init serial_putc(struct uart_port *port, int c)
  71. {
  72. wait_for_xmitr(port);
  73. serial_out(port, UART_TX, c);
  74. }
  75. static void __init early_serial8250_write(struct console *console,
  76. const char *s, unsigned int count)
  77. {
  78. struct uart_port *port = &early_device.port;
  79. unsigned int ier;
  80. /* Save the IER and disable interrupts */
  81. ier = serial_in(port, UART_IER);
  82. serial_out(port, UART_IER, 0);
  83. uart_console_write(port, s, count, serial_putc);
  84. /* Wait for transmitter to become empty and restore the IER */
  85. wait_for_xmitr(port);
  86. serial_out(port, UART_IER, ier);
  87. }
  88. static unsigned int __init probe_baud(struct uart_port *port)
  89. {
  90. unsigned char lcr, dll, dlm;
  91. unsigned int quot;
  92. lcr = serial_in(port, UART_LCR);
  93. serial_out(port, UART_LCR, lcr | UART_LCR_DLAB);
  94. dll = serial_in(port, UART_DLL);
  95. dlm = serial_in(port, UART_DLM);
  96. serial_out(port, UART_LCR, lcr);
  97. quot = (dlm << 8) | dll;
  98. return (port->uartclk / 16) / quot;
  99. }
  100. static void __init init_port(struct early_serial8250_device *device)
  101. {
  102. struct uart_port *port = &device->port;
  103. unsigned int divisor;
  104. unsigned char c;
  105. serial_out(port, UART_LCR, 0x3); /* 8n1 */
  106. serial_out(port, UART_IER, 0); /* no interrupt */
  107. serial_out(port, UART_FCR, 0); /* no fifo */
  108. serial_out(port, UART_MCR, 0x3); /* DTR + RTS */
  109. divisor = port->uartclk / (16 * device->baud);
  110. c = serial_in(port, UART_LCR);
  111. serial_out(port, UART_LCR, c | UART_LCR_DLAB);
  112. serial_out(port, UART_DLL, divisor & 0xff);
  113. serial_out(port, UART_DLM, (divisor >> 8) & 0xff);
  114. serial_out(port, UART_LCR, c & ~UART_LCR_DLAB);
  115. }
  116. static int __init parse_options(struct early_serial8250_device *device,
  117. char *options)
  118. {
  119. struct uart_port *port = &device->port;
  120. int mmio, length;
  121. if (!options)
  122. return -ENODEV;
  123. port->uartclk = BASE_BAUD * 16;
  124. if (!strncmp(options, "mmio,", 5)) {
  125. port->iotype = UPIO_MEM;
  126. port->mapbase = simple_strtoul(options + 5, &options, 0);
  127. #ifdef CONFIG_FIX_EARLYCON_MEM
  128. set_fixmap_nocache(FIX_EARLYCON_MEM_BASE,
  129. port->mapbase & PAGE_MASK);
  130. port->membase =
  131. (void __iomem *)__fix_to_virt(FIX_EARLYCON_MEM_BASE);
  132. port->membase += port->mapbase & ~PAGE_MASK;
  133. #else
  134. port->membase = ioremap_nocache(port->mapbase, 64);
  135. if (!port->membase) {
  136. printk(KERN_ERR "%s: Couldn't ioremap 0x%llx\n",
  137. __func__,
  138. (unsigned long long)port->mapbase);
  139. return -ENOMEM;
  140. }
  141. #endif
  142. mmio = 1;
  143. } else if (!strncmp(options, "io,", 3)) {
  144. port->iotype = UPIO_PORT;
  145. port->iobase = simple_strtoul(options + 3, &options, 0);
  146. mmio = 0;
  147. } else
  148. return -EINVAL;
  149. options = strchr(options, ',');
  150. if (options) {
  151. options++;
  152. device->baud = simple_strtoul(options, NULL, 0);
  153. length = min(strcspn(options, " "), sizeof(device->options));
  154. strncpy(device->options, options, length);
  155. } else {
  156. device->baud = probe_baud(port);
  157. snprintf(device->options, sizeof(device->options), "%u",
  158. device->baud);
  159. }
  160. printk(KERN_INFO "Early serial console at %s 0x%llx (options '%s')\n",
  161. mmio ? "MMIO" : "I/O port",
  162. mmio ? (unsigned long long) port->mapbase
  163. : (unsigned long long) port->iobase,
  164. device->options);
  165. return 0;
  166. }
  167. static struct console early_serial8250_console __initdata = {
  168. .name = "uart",
  169. .write = early_serial8250_write,
  170. .flags = CON_PRINTBUFFER | CON_BOOT,
  171. .index = -1,
  172. };
  173. static int __init early_serial8250_setup(char *options)
  174. {
  175. struct early_serial8250_device *device = &early_device;
  176. int err;
  177. if (device->port.membase || device->port.iobase)
  178. return 0;
  179. err = parse_options(device, options);
  180. if (err < 0)
  181. return err;
  182. init_port(device);
  183. return 0;
  184. }
  185. int __init setup_early_serial8250_console(char *cmdline)
  186. {
  187. char *options;
  188. int err;
  189. options = strstr(cmdline, "uart8250,");
  190. if (!options) {
  191. options = strstr(cmdline, "uart,");
  192. if (!options)
  193. return 0;
  194. }
  195. options = strchr(cmdline, ',') + 1;
  196. err = early_serial8250_setup(options);
  197. if (err < 0)
  198. return err;
  199. register_console(&early_serial8250_console);
  200. return 0;
  201. }
  202. int serial8250_find_port_for_earlycon(void)
  203. {
  204. struct early_serial8250_device *device = &early_device;
  205. struct uart_port *port = &device->port;
  206. int line;
  207. int ret;
  208. if (!device->port.membase && !device->port.iobase)
  209. return -ENODEV;
  210. line = serial8250_find_port(port);
  211. if (line < 0)
  212. return -ENODEV;
  213. ret = update_console_cmdline("uart", 8250,
  214. "ttyS", line, device->options);
  215. if (ret < 0)
  216. ret = update_console_cmdline("uart", 0,
  217. "ttyS", line, device->options);
  218. return ret;
  219. }
  220. early_param("earlycon", setup_early_serial8250_console);