8250_early.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 <bjorn.helgaas@hp.com>
  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/serial_reg.h>
  30. #include <linux/serial.h>
  31. #include <linux/serial_8250.h>
  32. #include <asm/io.h>
  33. #include <asm/serial.h>
  34. static unsigned int serial8250_early_in(struct uart_port *port, int offset)
  35. {
  36. offset <<= port->regshift;
  37. switch (port->iotype) {
  38. case UPIO_MEM:
  39. return readb(port->membase + offset);
  40. case UPIO_MEM16:
  41. return readw(port->membase + offset);
  42. case UPIO_MEM32:
  43. return readl(port->membase + offset);
  44. case UPIO_MEM32BE:
  45. return ioread32be(port->membase + offset);
  46. #ifdef CONFIG_HAS_IOPORT
  47. case UPIO_PORT:
  48. return inb(port->iobase + offset);
  49. #endif
  50. default:
  51. return 0;
  52. }
  53. }
  54. static void serial8250_early_out(struct uart_port *port, int offset, int value)
  55. {
  56. offset <<= port->regshift;
  57. switch (port->iotype) {
  58. case UPIO_MEM:
  59. writeb(value, port->membase + offset);
  60. break;
  61. case UPIO_MEM16:
  62. writew(value, port->membase + offset);
  63. break;
  64. case UPIO_MEM32:
  65. writel(value, port->membase + offset);
  66. break;
  67. case UPIO_MEM32BE:
  68. iowrite32be(value, port->membase + offset);
  69. break;
  70. #ifdef CONFIG_HAS_IOPORT
  71. case UPIO_PORT:
  72. outb(value, port->iobase + offset);
  73. break;
  74. #endif
  75. default:
  76. break;
  77. }
  78. }
  79. static void serial_putc(struct uart_port *port, unsigned char c)
  80. {
  81. unsigned int status;
  82. serial8250_early_out(port, UART_TX, c);
  83. for (;;) {
  84. status = serial8250_early_in(port, UART_LSR);
  85. if (uart_lsr_tx_empty(status))
  86. break;
  87. cpu_relax();
  88. }
  89. }
  90. static void early_serial8250_write(struct console *console,
  91. const char *s, unsigned int count)
  92. {
  93. struct earlycon_device *device = console->data;
  94. struct uart_port *port = &device->port;
  95. uart_console_write(port, s, count, serial_putc);
  96. }
  97. #ifdef CONFIG_CONSOLE_POLL
  98. static int early_serial8250_read(struct console *console,
  99. char *s, unsigned int count)
  100. {
  101. struct earlycon_device *device = console->data;
  102. struct uart_port *port = &device->port;
  103. unsigned int status;
  104. int num_read = 0;
  105. while (num_read < count) {
  106. status = serial8250_early_in(port, UART_LSR);
  107. if (!(status & UART_LSR_DR))
  108. break;
  109. s[num_read++] = serial8250_early_in(port, UART_RX);
  110. }
  111. return num_read;
  112. }
  113. #else
  114. #define early_serial8250_read NULL
  115. #endif
  116. static void __init init_port(struct earlycon_device *device)
  117. {
  118. struct uart_port *port = &device->port;
  119. unsigned int divisor;
  120. unsigned char c;
  121. unsigned int ier;
  122. serial8250_early_out(port, UART_LCR, UART_LCR_WLEN8); /* 8n1 */
  123. ier = serial8250_early_in(port, UART_IER);
  124. serial8250_early_out(port, UART_IER, ier & UART_IER_UUE); /* no interrupt */
  125. serial8250_early_out(port, UART_FCR, 0); /* no fifo */
  126. serial8250_early_out(port, UART_MCR, UART_MCR_DTR | UART_MCR_RTS);
  127. if (port->uartclk) {
  128. divisor = DIV_ROUND_CLOSEST(port->uartclk, 16 * device->baud);
  129. c = serial8250_early_in(port, UART_LCR);
  130. serial8250_early_out(port, UART_LCR, c | UART_LCR_DLAB);
  131. serial8250_early_out(port, UART_DLL, divisor & 0xff);
  132. serial8250_early_out(port, UART_DLM, (divisor >> 8) & 0xff);
  133. serial8250_early_out(port, UART_LCR, c & ~UART_LCR_DLAB);
  134. }
  135. }
  136. int __init early_serial8250_setup(struct earlycon_device *device,
  137. const char *options)
  138. {
  139. if (!(device->port.membase || device->port.iobase))
  140. return -ENODEV;
  141. if (!device->baud) {
  142. struct uart_port *port = &device->port;
  143. unsigned int ier;
  144. /* assume the device was initialized, only mask interrupts */
  145. ier = serial8250_early_in(port, UART_IER);
  146. serial8250_early_out(port, UART_IER, ier & UART_IER_UUE);
  147. } else
  148. init_port(device);
  149. device->con->write = early_serial8250_write;
  150. device->con->read = early_serial8250_read;
  151. return 0;
  152. }
  153. EARLYCON_DECLARE(uart8250, early_serial8250_setup);
  154. EARLYCON_DECLARE(uart, early_serial8250_setup);
  155. OF_EARLYCON_DECLARE(ns16550, "ns16550", early_serial8250_setup);
  156. OF_EARLYCON_DECLARE(ns16550a, "ns16550a", early_serial8250_setup);
  157. OF_EARLYCON_DECLARE(uart, "nvidia,tegra20-uart", early_serial8250_setup);
  158. OF_EARLYCON_DECLARE(uart, "snps,dw-apb-uart", early_serial8250_setup);
  159. static int __init early_serial8250_rs2_setup(struct earlycon_device *device,
  160. const char *options)
  161. {
  162. device->port.regshift = 2;
  163. return early_serial8250_setup(device, options);
  164. }
  165. OF_EARLYCON_DECLARE(uart, "intel,xscale-uart", early_serial8250_rs2_setup);
  166. OF_EARLYCON_DECLARE(uart, "mrvl,mmp-uart", early_serial8250_rs2_setup);
  167. OF_EARLYCON_DECLARE(uart, "mrvl,pxa-uart", early_serial8250_rs2_setup);
  168. #ifdef CONFIG_SERIAL_8250_OMAP
  169. static int __init early_omap8250_setup(struct earlycon_device *device,
  170. const char *options)
  171. {
  172. struct uart_port *port = &device->port;
  173. if (!(device->port.membase || device->port.iobase))
  174. return -ENODEV;
  175. port->regshift = 2;
  176. device->con->write = early_serial8250_write;
  177. return 0;
  178. }
  179. OF_EARLYCON_DECLARE(omap8250, "ti,omap2-uart", early_omap8250_setup);
  180. OF_EARLYCON_DECLARE(omap8250, "ti,omap3-uart", early_omap8250_setup);
  181. OF_EARLYCON_DECLARE(omap8250, "ti,omap4-uart", early_omap8250_setup);
  182. OF_EARLYCON_DECLARE(omap8250, "ti,am654-uart", early_omap8250_setup);
  183. #endif