8250_early.c 6.4 KB

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