8250_early.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. * earlycon=uart8250,mmio32,0xff5e0000,115200n8
  23. * or
  24. * console=uart8250,io,0x3f8,9600n8
  25. * console=uart8250,mmio,0xff5e0000,115200n8
  26. * console=uart8250,mmio32,0xff5e0000,115200n8
  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 <linux/serial_8250.h>
  35. #include <asm/io.h>
  36. #include <asm/serial.h>
  37. static struct earlycon_device *early_device;
  38. unsigned int __weak __init serial8250_early_in(struct uart_port *port, int offset)
  39. {
  40. switch (port->iotype) {
  41. case UPIO_MEM:
  42. return readb(port->membase + offset);
  43. case UPIO_MEM32:
  44. return readl(port->membase + (offset << 2));
  45. case UPIO_PORT:
  46. return inb(port->iobase + offset);
  47. default:
  48. return 0;
  49. }
  50. }
  51. void __weak __init serial8250_early_out(struct uart_port *port, int offset, int value)
  52. {
  53. switch (port->iotype) {
  54. case UPIO_MEM:
  55. writeb(value, port->membase + offset);
  56. break;
  57. case UPIO_MEM32:
  58. writel(value, port->membase + (offset << 2));
  59. break;
  60. case UPIO_PORT:
  61. outb(value, port->iobase + offset);
  62. break;
  63. }
  64. }
  65. #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
  66. static void __init wait_for_xmitr(struct uart_port *port)
  67. {
  68. unsigned int status;
  69. for (;;) {
  70. status = serial8250_early_in(port, UART_LSR);
  71. if ((status & BOTH_EMPTY) == BOTH_EMPTY)
  72. return;
  73. cpu_relax();
  74. }
  75. }
  76. static void __init serial_putc(struct uart_port *port, int c)
  77. {
  78. wait_for_xmitr(port);
  79. serial8250_early_out(port, UART_TX, c);
  80. }
  81. static void __init early_serial8250_write(struct console *console,
  82. const char *s, unsigned int count)
  83. {
  84. struct uart_port *port = &early_device->port;
  85. unsigned int ier;
  86. /* Save the IER and disable interrupts */
  87. ier = serial8250_early_in(port, UART_IER);
  88. serial8250_early_out(port, UART_IER, 0);
  89. uart_console_write(port, s, count, serial_putc);
  90. /* Wait for transmitter to become empty and restore the IER */
  91. wait_for_xmitr(port);
  92. serial8250_early_out(port, UART_IER, ier);
  93. }
  94. static unsigned int __init probe_baud(struct uart_port *port)
  95. {
  96. unsigned char lcr, dll, dlm;
  97. unsigned int quot;
  98. lcr = serial8250_early_in(port, UART_LCR);
  99. serial8250_early_out(port, UART_LCR, lcr | UART_LCR_DLAB);
  100. dll = serial8250_early_in(port, UART_DLL);
  101. dlm = serial8250_early_in(port, UART_DLM);
  102. serial8250_early_out(port, UART_LCR, lcr);
  103. quot = (dlm << 8) | dll;
  104. return (port->uartclk / 16) / quot;
  105. }
  106. static void __init init_port(struct earlycon_device *device)
  107. {
  108. struct uart_port *port = &device->port;
  109. unsigned int divisor;
  110. unsigned char c;
  111. serial8250_early_out(port, UART_LCR, 0x3); /* 8n1 */
  112. serial8250_early_out(port, UART_IER, 0); /* no interrupt */
  113. serial8250_early_out(port, UART_FCR, 0); /* no fifo */
  114. serial8250_early_out(port, UART_MCR, 0x3); /* DTR + RTS */
  115. divisor = DIV_ROUND_CLOSEST(port->uartclk, 16 * device->baud);
  116. c = serial8250_early_in(port, UART_LCR);
  117. serial8250_early_out(port, UART_LCR, c | UART_LCR_DLAB);
  118. serial8250_early_out(port, UART_DLL, divisor & 0xff);
  119. serial8250_early_out(port, UART_DLM, (divisor >> 8) & 0xff);
  120. serial8250_early_out(port, UART_LCR, c & ~UART_LCR_DLAB);
  121. }
  122. static int __init early_serial8250_setup(struct earlycon_device *device,
  123. const char *options)
  124. {
  125. if (!(device->port.membase || device->port.iobase))
  126. return 0;
  127. if (!device->baud) {
  128. device->baud = probe_baud(&device->port);
  129. snprintf(device->options, sizeof(device->options), "%u",
  130. device->baud);
  131. }
  132. init_port(device);
  133. early_device = device;
  134. device->con->write = early_serial8250_write;
  135. return 0;
  136. }
  137. EARLYCON_DECLARE(uart8250, early_serial8250_setup);
  138. EARLYCON_DECLARE(uart, early_serial8250_setup);
  139. int __init setup_early_serial8250_console(char *cmdline)
  140. {
  141. char match[] = "uart8250";
  142. if (cmdline && cmdline[4] == ',')
  143. match[4] = '\0';
  144. return setup_earlycon(cmdline, match, early_serial8250_setup);
  145. }
  146. int serial8250_find_port_for_earlycon(void)
  147. {
  148. struct earlycon_device *device = early_device;
  149. struct uart_port *port = device ? &device->port : NULL;
  150. int line;
  151. int ret;
  152. if (!port || (!port->membase && !port->iobase))
  153. return -ENODEV;
  154. line = serial8250_find_port(port);
  155. if (line < 0)
  156. return -ENODEV;
  157. ret = update_console_cmdline("uart", 8250,
  158. "ttyS", line, device->options);
  159. if (ret < 0)
  160. ret = update_console_cmdline("uart", 0,
  161. "ttyS", line, device->options);
  162. return ret;
  163. }