8250_early.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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/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. #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
  82. static void serial_putc(struct uart_port *port, int c)
  83. {
  84. unsigned int status;
  85. serial8250_early_out(port, UART_TX, c);
  86. for (;;) {
  87. status = serial8250_early_in(port, UART_LSR);
  88. if ((status & BOTH_EMPTY) == BOTH_EMPTY)
  89. break;
  90. cpu_relax();
  91. }
  92. }
  93. static void early_serial8250_write(struct console *console,
  94. const char *s, unsigned int count)
  95. {
  96. struct earlycon_device *device = console->data;
  97. struct uart_port *port = &device->port;
  98. uart_console_write(port, s, count, serial_putc);
  99. }
  100. static void __init init_port(struct earlycon_device *device)
  101. {
  102. struct uart_port *port = &device->port;
  103. unsigned int divisor;
  104. unsigned char c;
  105. unsigned int ier;
  106. serial8250_early_out(port, UART_LCR, 0x3); /* 8n1 */
  107. ier = serial8250_early_in(port, UART_IER);
  108. serial8250_early_out(port, UART_IER, ier & UART_IER_UUE); /* no interrupt */
  109. serial8250_early_out(port, UART_FCR, 0); /* no fifo */
  110. serial8250_early_out(port, UART_MCR, 0x3); /* DTR + RTS */
  111. if (port->uartclk) {
  112. divisor = DIV_ROUND_CLOSEST(port->uartclk, 16 * device->baud);
  113. c = serial8250_early_in(port, UART_LCR);
  114. serial8250_early_out(port, UART_LCR, c | UART_LCR_DLAB);
  115. serial8250_early_out(port, UART_DLL, divisor & 0xff);
  116. serial8250_early_out(port, UART_DLM, (divisor >> 8) & 0xff);
  117. serial8250_early_out(port, UART_LCR, c & ~UART_LCR_DLAB);
  118. }
  119. }
  120. int __init early_serial8250_setup(struct earlycon_device *device,
  121. const char *options)
  122. {
  123. if (!(device->port.membase || device->port.iobase))
  124. return -ENODEV;
  125. if (!device->baud) {
  126. struct uart_port *port = &device->port;
  127. unsigned int ier;
  128. /* assume the device was initialized, only mask interrupts */
  129. ier = serial8250_early_in(port, UART_IER);
  130. serial8250_early_out(port, UART_IER, ier & UART_IER_UUE);
  131. } else
  132. init_port(device);
  133. device->con->write = early_serial8250_write;
  134. return 0;
  135. }
  136. EARLYCON_DECLARE(uart8250, early_serial8250_setup);
  137. EARLYCON_DECLARE(uart, early_serial8250_setup);
  138. OF_EARLYCON_DECLARE(ns16550, "ns16550", early_serial8250_setup);
  139. OF_EARLYCON_DECLARE(ns16550a, "ns16550a", early_serial8250_setup);
  140. OF_EARLYCON_DECLARE(uart, "nvidia,tegra20-uart", early_serial8250_setup);
  141. OF_EARLYCON_DECLARE(uart, "snps,dw-apb-uart", early_serial8250_setup);
  142. #ifdef CONFIG_SERIAL_8250_OMAP
  143. static int __init early_omap8250_setup(struct earlycon_device *device,
  144. const char *options)
  145. {
  146. struct uart_port *port = &device->port;
  147. if (!(device->port.membase || device->port.iobase))
  148. return -ENODEV;
  149. port->regshift = 2;
  150. device->con->write = early_serial8250_write;
  151. return 0;
  152. }
  153. OF_EARLYCON_DECLARE(omap8250, "ti,omap2-uart", early_omap8250_setup);
  154. OF_EARLYCON_DECLARE(omap8250, "ti,omap3-uart", early_omap8250_setup);
  155. OF_EARLYCON_DECLARE(omap8250, "ti,omap4-uart", early_omap8250_setup);
  156. #endif
  157. #ifdef CONFIG_SERIAL_8250_RT288X
  158. unsigned int au_serial_in(struct uart_port *p, int offset);
  159. void au_serial_out(struct uart_port *p, int offset, int value);
  160. static int __init early_au_setup(struct earlycon_device *dev, const char *opt)
  161. {
  162. dev->port.serial_in = au_serial_in;
  163. dev->port.serial_out = au_serial_out;
  164. dev->port.iotype = UPIO_AU;
  165. dev->con->write = early_serial8250_write;
  166. return 0;
  167. }
  168. OF_EARLYCON_DECLARE(palmchip, "ralink,rt2880-uart", early_au_setup);
  169. #endif