8250_early.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. * console=uart,io,0x3f8,9600n8
  21. * console=uart,mmio,0xff5e0000,115200n8
  22. * or platform code can call early_uart_console_init() to set
  23. * the early UART device.
  24. *
  25. * After the normal serial driver starts, we try to locate the
  26. * matching ttyS device and start a console there.
  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 <asm/io.h>
  35. #include <asm/serial.h>
  36. struct early_uart_device {
  37. struct uart_port port;
  38. char options[16]; /* e.g., 115200n8 */
  39. unsigned int baud;
  40. };
  41. static struct early_uart_device early_device __initdata;
  42. static int early_uart_registered __initdata;
  43. static unsigned int __init serial_in(struct uart_port *port, int offset)
  44. {
  45. if (port->iotype == UPIO_MEM)
  46. return readb(port->membase + offset);
  47. else
  48. return inb(port->iobase + offset);
  49. }
  50. static void __init serial_out(struct uart_port *port, int offset, int value)
  51. {
  52. if (port->iotype == UPIO_MEM)
  53. writeb(value, port->membase + offset);
  54. else
  55. outb(value, port->iobase + offset);
  56. }
  57. #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
  58. static void __init wait_for_xmitr(struct uart_port *port)
  59. {
  60. unsigned int status;
  61. for (;;) {
  62. status = serial_in(port, UART_LSR);
  63. if ((status & BOTH_EMPTY) == BOTH_EMPTY)
  64. return;
  65. cpu_relax();
  66. }
  67. }
  68. static void __init putc(struct uart_port *port, unsigned char c)
  69. {
  70. wait_for_xmitr(port);
  71. serial_out(port, UART_TX, c);
  72. }
  73. static void __init early_uart_write(struct console *console, const char *s, unsigned int count)
  74. {
  75. struct uart_port *port = &early_device.port;
  76. unsigned int ier;
  77. /* Save the IER and disable interrupts */
  78. ier = serial_in(port, UART_IER);
  79. serial_out(port, UART_IER, 0);
  80. while (*s && count-- > 0) {
  81. putc(port, *s);
  82. if (*s == '\n')
  83. putc(port, '\r');
  84. s++;
  85. }
  86. /* Wait for transmitter to become empty and restore the IER */
  87. wait_for_xmitr(port);
  88. serial_out(port, UART_IER, ier);
  89. }
  90. static unsigned int __init probe_baud(struct uart_port *port)
  91. {
  92. unsigned char lcr, dll, dlm;
  93. unsigned int quot;
  94. lcr = serial_in(port, UART_LCR);
  95. serial_out(port, UART_LCR, lcr | UART_LCR_DLAB);
  96. dll = serial_in(port, UART_DLL);
  97. dlm = serial_in(port, UART_DLM);
  98. serial_out(port, UART_LCR, lcr);
  99. quot = (dlm << 8) | dll;
  100. return (port->uartclk / 16) / quot;
  101. }
  102. static void __init init_port(struct early_uart_device *device)
  103. {
  104. struct uart_port *port = &device->port;
  105. unsigned int divisor;
  106. unsigned char c;
  107. serial_out(port, UART_LCR, 0x3); /* 8n1 */
  108. serial_out(port, UART_IER, 0); /* no interrupt */
  109. serial_out(port, UART_FCR, 0); /* no fifo */
  110. serial_out(port, UART_MCR, 0x3); /* DTR + RTS */
  111. divisor = port->uartclk / (16 * device->baud);
  112. c = serial_in(port, UART_LCR);
  113. serial_out(port, UART_LCR, c | UART_LCR_DLAB);
  114. serial_out(port, UART_DLL, divisor & 0xff);
  115. serial_out(port, UART_DLM, (divisor >> 8) & 0xff);
  116. serial_out(port, UART_LCR, c & ~UART_LCR_DLAB);
  117. }
  118. static int __init parse_options(struct early_uart_device *device, char *options)
  119. {
  120. struct uart_port *port = &device->port;
  121. int mapsize = 64;
  122. int mmio, length;
  123. if (!options)
  124. return -ENODEV;
  125. port->uartclk = BASE_BAUD * 16;
  126. if (!strncmp(options, "mmio,", 5)) {
  127. port->iotype = UPIO_MEM;
  128. port->mapbase = simple_strtoul(options + 5, &options, 0);
  129. port->membase = ioremap(port->mapbase, mapsize);
  130. if (!port->membase) {
  131. printk(KERN_ERR "%s: Couldn't ioremap 0x%lx\n",
  132. __FUNCTION__, port->mapbase);
  133. return -ENOMEM;
  134. }
  135. mmio = 1;
  136. } else if (!strncmp(options, "io,", 3)) {
  137. port->iotype = UPIO_PORT;
  138. port->iobase = simple_strtoul(options + 3, &options, 0);
  139. mmio = 0;
  140. } else
  141. return -EINVAL;
  142. if ((options = strchr(options, ','))) {
  143. options++;
  144. device->baud = simple_strtoul(options, NULL, 0);
  145. length = min(strcspn(options, " "), sizeof(device->options));
  146. strncpy(device->options, options, length);
  147. } else {
  148. device->baud = probe_baud(port);
  149. snprintf(device->options, sizeof(device->options), "%u",
  150. device->baud);
  151. }
  152. printk(KERN_INFO "Early serial console at %s 0x%lx (options '%s')\n",
  153. mmio ? "MMIO" : "I/O port",
  154. mmio ? port->mapbase : (unsigned long) port->iobase,
  155. device->options);
  156. return 0;
  157. }
  158. static int __init early_uart_setup(struct console *console, char *options)
  159. {
  160. struct early_uart_device *device = &early_device;
  161. int err;
  162. if (device->port.membase || device->port.iobase)
  163. return 0;
  164. if ((err = parse_options(device, options)) < 0)
  165. return err;
  166. init_port(device);
  167. return 0;
  168. }
  169. static struct console early_uart_console __initdata = {
  170. .name = "uart",
  171. .write = early_uart_write,
  172. .setup = early_uart_setup,
  173. .flags = CON_PRINTBUFFER,
  174. .index = -1,
  175. };
  176. static int __init early_uart_console_init(void)
  177. {
  178. if (!early_uart_registered) {
  179. register_console(&early_uart_console);
  180. early_uart_registered = 1;
  181. }
  182. return 0;
  183. }
  184. console_initcall(early_uart_console_init);
  185. int __init early_serial_console_init(char *cmdline)
  186. {
  187. char *options;
  188. int err;
  189. options = strstr(cmdline, "console=uart,");
  190. if (!options)
  191. return -ENODEV;
  192. options = strchr(cmdline, ',') + 1;
  193. if ((err = early_uart_setup(NULL, options)) < 0)
  194. return err;
  195. return early_uart_console_init();
  196. }
  197. static int __init early_uart_console_switch(void)
  198. {
  199. struct early_uart_device *device = &early_device;
  200. struct uart_port *port = &device->port;
  201. int mmio, line;
  202. if (!(early_uart_console.flags & CON_ENABLED))
  203. return 0;
  204. /* Try to start the normal driver on a matching line. */
  205. mmio = (port->iotype == UPIO_MEM);
  206. line = serial8250_start_console(port, device->options);
  207. if (line < 0)
  208. printk("No ttyS device at %s 0x%lx for console\n",
  209. mmio ? "MMIO" : "I/O port",
  210. mmio ? port->mapbase :
  211. (unsigned long) port->iobase);
  212. unregister_console(&early_uart_console);
  213. if (mmio)
  214. iounmap(port->membase);
  215. return 0;
  216. }
  217. late_initcall(early_uart_console_switch);