8250_early.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 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, const char *s, unsigned int count)
  76. {
  77. struct uart_port *port = &early_device.port;
  78. unsigned int ier;
  79. /* Save the IER and disable interrupts */
  80. ier = serial_in(port, UART_IER);
  81. serial_out(port, UART_IER, 0);
  82. uart_console_write(port, s, count, putc);
  83. /* Wait for transmitter to become empty and restore the IER */
  84. wait_for_xmitr(port);
  85. serial_out(port, UART_IER, ier);
  86. }
  87. static unsigned int __init probe_baud(struct uart_port *port)
  88. {
  89. unsigned char lcr, dll, dlm;
  90. unsigned int quot;
  91. lcr = serial_in(port, UART_LCR);
  92. serial_out(port, UART_LCR, lcr | UART_LCR_DLAB);
  93. dll = serial_in(port, UART_DLL);
  94. dlm = serial_in(port, UART_DLM);
  95. serial_out(port, UART_LCR, lcr);
  96. quot = (dlm << 8) | dll;
  97. return (port->uartclk / 16) / quot;
  98. }
  99. static void __init init_port(struct early_serial8250_device *device)
  100. {
  101. struct uart_port *port = &device->port;
  102. unsigned int divisor;
  103. unsigned char c;
  104. serial_out(port, UART_LCR, 0x3); /* 8n1 */
  105. serial_out(port, UART_IER, 0); /* no interrupt */
  106. serial_out(port, UART_FCR, 0); /* no fifo */
  107. serial_out(port, UART_MCR, 0x3); /* DTR + RTS */
  108. divisor = port->uartclk / (16 * device->baud);
  109. c = serial_in(port, UART_LCR);
  110. serial_out(port, UART_LCR, c | UART_LCR_DLAB);
  111. serial_out(port, UART_DLL, divisor & 0xff);
  112. serial_out(port, UART_DLM, (divisor >> 8) & 0xff);
  113. serial_out(port, UART_LCR, c & ~UART_LCR_DLAB);
  114. }
  115. static int __init parse_options(struct early_serial8250_device *device, char *options)
  116. {
  117. struct uart_port *port = &device->port;
  118. int mmio, length;
  119. if (!options)
  120. return -ENODEV;
  121. port->uartclk = BASE_BAUD * 16;
  122. if (!strncmp(options, "mmio,", 5)) {
  123. port->iotype = UPIO_MEM;
  124. port->mapbase = simple_strtoul(options + 5, &options, 0);
  125. #ifdef CONFIG_FIX_EARLYCON_MEM
  126. set_fixmap_nocache(FIX_EARLYCON_MEM_BASE, port->mapbase & PAGE_MASK);
  127. port->membase = (void __iomem *)__fix_to_virt(FIX_EARLYCON_MEM_BASE);
  128. port->membase += port->mapbase & ~PAGE_MASK;
  129. #else
  130. port->membase = ioremap(port->mapbase, 64);
  131. if (!port->membase) {
  132. printk(KERN_ERR "%s: Couldn't ioremap 0x%lx\n",
  133. __FUNCTION__, port->mapbase);
  134. return -ENOMEM;
  135. }
  136. #endif
  137. mmio = 1;
  138. } else if (!strncmp(options, "io,", 3)) {
  139. port->iotype = UPIO_PORT;
  140. port->iobase = simple_strtoul(options + 3, &options, 0);
  141. mmio = 0;
  142. } else
  143. return -EINVAL;
  144. if ((options = strchr(options, ','))) {
  145. options++;
  146. device->baud = simple_strtoul(options, NULL, 0);
  147. length = min(strcspn(options, " "), sizeof(device->options));
  148. strncpy(device->options, options, length);
  149. } else {
  150. device->baud = probe_baud(port);
  151. snprintf(device->options, sizeof(device->options), "%u",
  152. device->baud);
  153. }
  154. printk(KERN_INFO "Early serial console at %s 0x%lx (options '%s')\n",
  155. mmio ? "MMIO" : "I/O port",
  156. mmio ? port->mapbase : (unsigned long) port->iobase,
  157. device->options);
  158. return 0;
  159. }
  160. static struct console early_serial8250_console __initdata = {
  161. .name = "uart",
  162. .write = early_serial8250_write,
  163. .flags = CON_PRINTBUFFER | CON_BOOT,
  164. .index = -1,
  165. };
  166. static int __init early_serial8250_setup(char *options)
  167. {
  168. struct early_serial8250_device *device = &early_device;
  169. int err;
  170. if (device->port.membase || device->port.iobase)
  171. return 0;
  172. if ((err = parse_options(device, options)) < 0)
  173. return err;
  174. init_port(device);
  175. return 0;
  176. }
  177. int __init setup_early_serial8250_console(char *cmdline)
  178. {
  179. char *options;
  180. int err;
  181. options = strstr(cmdline, "uart8250,");
  182. if (!options) {
  183. options = strstr(cmdline, "uart,");
  184. if (!options)
  185. return 0;
  186. }
  187. options = strchr(cmdline, ',') + 1;
  188. if ((err = early_serial8250_setup(options)) < 0)
  189. return err;
  190. register_console(&early_serial8250_console);
  191. return 0;
  192. }
  193. int __init serial8250_find_port_for_earlycon(void)
  194. {
  195. struct early_serial8250_device *device = &early_device;
  196. struct uart_port *port = &device->port;
  197. int line;
  198. int ret;
  199. if (!device->port.membase && !device->port.iobase)
  200. return -ENODEV;
  201. line = serial8250_find_port(port);
  202. if (line < 0)
  203. return -ENODEV;
  204. ret = update_console_cmdline("uart", 8250,
  205. "ttyS", line, device->options);
  206. if (ret < 0)
  207. ret = update_console_cmdline("uart", 0,
  208. "ttyS", line, device->options);
  209. return ret;
  210. }
  211. early_param("earlycon", setup_early_serial8250_console);