8250_early.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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%llx\n",
  133. __FUNCTION__,
  134. (unsigned long long)port->mapbase);
  135. return -ENOMEM;
  136. }
  137. #endif
  138. mmio = 1;
  139. } else if (!strncmp(options, "io,", 3)) {
  140. port->iotype = UPIO_PORT;
  141. port->iobase = simple_strtoul(options + 3, &options, 0);
  142. mmio = 0;
  143. } else
  144. return -EINVAL;
  145. if ((options = strchr(options, ','))) {
  146. options++;
  147. device->baud = simple_strtoul(options, NULL, 0);
  148. length = min(strcspn(options, " "), sizeof(device->options));
  149. strncpy(device->options, options, length);
  150. } else {
  151. device->baud = probe_baud(port);
  152. snprintf(device->options, sizeof(device->options), "%u",
  153. device->baud);
  154. }
  155. printk(KERN_INFO "Early serial console at %s 0x%llx (options '%s')\n",
  156. mmio ? "MMIO" : "I/O port",
  157. mmio ? (unsigned long long) port->mapbase
  158. : (unsigned long long) port->iobase,
  159. device->options);
  160. return 0;
  161. }
  162. static struct console early_serial8250_console __initdata = {
  163. .name = "uart",
  164. .write = early_serial8250_write,
  165. .flags = CON_PRINTBUFFER | CON_BOOT,
  166. .index = -1,
  167. };
  168. static int __init early_serial8250_setup(char *options)
  169. {
  170. struct early_serial8250_device *device = &early_device;
  171. int err;
  172. if (device->port.membase || device->port.iobase)
  173. return 0;
  174. if ((err = parse_options(device, options)) < 0)
  175. return err;
  176. init_port(device);
  177. return 0;
  178. }
  179. int __init setup_early_serial8250_console(char *cmdline)
  180. {
  181. char *options;
  182. int err;
  183. options = strstr(cmdline, "uart8250,");
  184. if (!options) {
  185. options = strstr(cmdline, "uart,");
  186. if (!options)
  187. return 0;
  188. }
  189. options = strchr(cmdline, ',') + 1;
  190. if ((err = early_serial8250_setup(options)) < 0)
  191. return err;
  192. register_console(&early_serial8250_console);
  193. return 0;
  194. }
  195. int serial8250_find_port_for_earlycon(void)
  196. {
  197. struct early_serial8250_device *device = &early_device;
  198. struct uart_port *port = &device->port;
  199. int line;
  200. int ret;
  201. if (!device->port.membase && !device->port.iobase)
  202. return -ENODEV;
  203. line = serial8250_find_port(port);
  204. if (line < 0)
  205. return -ENODEV;
  206. ret = update_console_cmdline("uart", 8250,
  207. "ttyS", line, device->options);
  208. if (ret < 0)
  209. ret = update_console_cmdline("uart", 0,
  210. "ttyS", line, device->options);
  211. return ret;
  212. }
  213. early_param("earlycon", setup_early_serial8250_console);