8250_early.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Early serial console for 8250/16550 devices
  4. *
  5. * (c) Copyright 2004 Hewlett-Packard Development Company, L.P.
  6. * Bjorn Helgaas <[email protected]>
  7. *
  8. * Based on the 8250.c serial driver, Copyright (C) 2001 Russell King,
  9. * and on early_printk.c by Andi Kleen.
  10. *
  11. * This is for use before the serial driver has initialized, in
  12. * particular, before the UARTs have been discovered and named.
  13. * Instead of specifying the console device as, e.g., "ttyS0",
  14. * we locate the device directly by its MMIO or I/O port address.
  15. *
  16. * The user can specify the device directly, e.g.,
  17. * earlycon=uart8250,io,0x3f8,9600n8
  18. * earlycon=uart8250,mmio,0xff5e0000,115200n8
  19. * earlycon=uart8250,mmio32,0xff5e0000,115200n8
  20. * or
  21. * console=uart8250,io,0x3f8,9600n8
  22. * console=uart8250,mmio,0xff5e0000,115200n8
  23. * console=uart8250,mmio32,0xff5e0000,115200n8
  24. */
  25. #include <linux/tty.h>
  26. #include <linux/init.h>
  27. #include <linux/console.h>
  28. #include <linux/of.h>
  29. #include <linux/of_device.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. static unsigned int serial8250_early_in(struct uart_port *port, int offset)
  36. {
  37. int reg_offset = offset;
  38. offset <<= port->regshift;
  39. switch (port->iotype) {
  40. case UPIO_MEM:
  41. return readb(port->membase + offset);
  42. case UPIO_MEM16:
  43. return readw(port->membase + offset);
  44. case UPIO_MEM32:
  45. return readl(port->membase + offset);
  46. case UPIO_MEM32BE:
  47. return ioread32be(port->membase + offset);
  48. case UPIO_PORT:
  49. return inb(port->iobase + offset);
  50. case UPIO_AU:
  51. return port->serial_in(port, reg_offset);
  52. default:
  53. return 0;
  54. }
  55. }
  56. static void serial8250_early_out(struct uart_port *port, int offset, int value)
  57. {
  58. int reg_offset = offset;
  59. offset <<= port->regshift;
  60. switch (port->iotype) {
  61. case UPIO_MEM:
  62. writeb(value, port->membase + offset);
  63. break;
  64. case UPIO_MEM16:
  65. writew(value, port->membase + offset);
  66. break;
  67. case UPIO_MEM32:
  68. writel(value, port->membase + offset);
  69. break;
  70. case UPIO_MEM32BE:
  71. iowrite32be(value, port->membase + offset);
  72. break;
  73. case UPIO_PORT:
  74. outb(value, port->iobase + offset);
  75. break;
  76. case UPIO_AU:
  77. port->serial_out(port, reg_offset, value);
  78. break;
  79. }
  80. }
  81. static void serial_putc(struct uart_port *port, unsigned char c)
  82. {
  83. unsigned int status;
  84. serial8250_early_out(port, UART_TX, c);
  85. for (;;) {
  86. status = serial8250_early_in(port, UART_LSR);
  87. if (uart_lsr_tx_empty(status))
  88. break;
  89. cpu_relax();
  90. }
  91. }
  92. static void early_serial8250_write(struct console *console,
  93. const char *s, unsigned int count)
  94. {
  95. struct earlycon_device *device = console->data;
  96. struct uart_port *port = &device->port;
  97. uart_console_write(port, s, count, serial_putc);
  98. }
  99. #ifdef CONFIG_CONSOLE_POLL
  100. static int early_serial8250_read(struct console *console,
  101. char *s, unsigned int count)
  102. {
  103. struct earlycon_device *device = console->data;
  104. struct uart_port *port = &device->port;
  105. unsigned int status;
  106. int num_read = 0;
  107. while (num_read < count) {
  108. status = serial8250_early_in(port, UART_LSR);
  109. if (!(status & UART_LSR_DR))
  110. break;
  111. s[num_read++] = serial8250_early_in(port, UART_RX);
  112. }
  113. return num_read;
  114. }
  115. #else
  116. #define early_serial8250_read NULL
  117. #endif
  118. static void __init init_port(struct earlycon_device *device)
  119. {
  120. struct uart_port *port = &device->port;
  121. unsigned int divisor;
  122. unsigned char c;
  123. unsigned int ier;
  124. serial8250_early_out(port, UART_LCR, 0x3); /* 8n1 */
  125. ier = serial8250_early_in(port, UART_IER);
  126. serial8250_early_out(port, UART_IER, ier & UART_IER_UUE); /* no interrupt */
  127. serial8250_early_out(port, UART_FCR, 0); /* no fifo */
  128. serial8250_early_out(port, UART_MCR, 0x3); /* DTR + RTS */
  129. if (port->uartclk) {
  130. divisor = DIV_ROUND_CLOSEST(port->uartclk, 16 * device->baud);
  131. c = serial8250_early_in(port, UART_LCR);
  132. serial8250_early_out(port, UART_LCR, c | UART_LCR_DLAB);
  133. serial8250_early_out(port, UART_DLL, divisor & 0xff);
  134. serial8250_early_out(port, UART_DLM, (divisor >> 8) & 0xff);
  135. serial8250_early_out(port, UART_LCR, c & ~UART_LCR_DLAB);
  136. }
  137. }
  138. int __init early_serial8250_setup(struct earlycon_device *device,
  139. const char *options)
  140. {
  141. if (!(device->port.membase || device->port.iobase))
  142. return -ENODEV;
  143. if (!device->baud) {
  144. struct uart_port *port = &device->port;
  145. unsigned int ier;
  146. /* assume the device was initialized, only mask interrupts */
  147. ier = serial8250_early_in(port, UART_IER);
  148. serial8250_early_out(port, UART_IER, ier & UART_IER_UUE);
  149. } else
  150. init_port(device);
  151. device->con->write = early_serial8250_write;
  152. device->con->read = early_serial8250_read;
  153. return 0;
  154. }
  155. EARLYCON_DECLARE(uart8250, early_serial8250_setup);
  156. EARLYCON_DECLARE(uart, early_serial8250_setup);
  157. OF_EARLYCON_DECLARE(ns16550, "ns16550", early_serial8250_setup);
  158. OF_EARLYCON_DECLARE(ns16550a, "ns16550a", early_serial8250_setup);
  159. OF_EARLYCON_DECLARE(uart, "nvidia,tegra20-uart", early_serial8250_setup);
  160. OF_EARLYCON_DECLARE(uart, "snps,dw-apb-uart", early_serial8250_setup);
  161. #ifdef CONFIG_SERIAL_8250_OMAP
  162. static int __init early_omap8250_setup(struct earlycon_device *device,
  163. const char *options)
  164. {
  165. struct uart_port *port = &device->port;
  166. if (!(device->port.membase || device->port.iobase))
  167. return -ENODEV;
  168. port->regshift = 2;
  169. device->con->write = early_serial8250_write;
  170. return 0;
  171. }
  172. OF_EARLYCON_DECLARE(omap8250, "ti,omap2-uart", early_omap8250_setup);
  173. OF_EARLYCON_DECLARE(omap8250, "ti,omap3-uart", early_omap8250_setup);
  174. OF_EARLYCON_DECLARE(omap8250, "ti,omap4-uart", early_omap8250_setup);
  175. OF_EARLYCON_DECLARE(omap8250, "ti,am654-uart", early_omap8250_setup);
  176. #endif
  177. #ifdef CONFIG_SERIAL_8250_RT288X
  178. static int __init early_au_setup(struct earlycon_device *dev, const char *opt)
  179. {
  180. dev->port.serial_in = au_serial_in;
  181. dev->port.serial_out = au_serial_out;
  182. dev->port.iotype = UPIO_AU;
  183. dev->con->write = early_serial8250_write;
  184. return 0;
  185. }
  186. OF_EARLYCON_DECLARE(palmchip, "ralink,rt2880-uart", early_au_setup);
  187. #endif