8250_early.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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/of.h>
  32. #include <linux/of_device.h>
  33. #include <linux/serial_reg.h>
  34. #include <linux/serial.h>
  35. #include <linux/serial_8250.h>
  36. #include <asm/io.h>
  37. #include <asm/serial.h>
  38. static unsigned int __init serial8250_early_in(struct uart_port *port, int offset)
  39. {
  40. int reg_offset = offset;
  41. offset <<= port->regshift;
  42. switch (port->iotype) {
  43. case UPIO_MEM:
  44. return readb(port->membase + offset);
  45. case UPIO_MEM16:
  46. return readw(port->membase + offset);
  47. case UPIO_MEM32:
  48. return readl(port->membase + offset);
  49. case UPIO_MEM32BE:
  50. return ioread32be(port->membase + offset);
  51. case UPIO_PORT:
  52. return inb(port->iobase + offset);
  53. case UPIO_AU:
  54. return port->serial_in(port, reg_offset);
  55. default:
  56. return 0;
  57. }
  58. }
  59. static void __init serial8250_early_out(struct uart_port *port, int offset, int value)
  60. {
  61. int reg_offset = offset;
  62. offset <<= port->regshift;
  63. switch (port->iotype) {
  64. case UPIO_MEM:
  65. writeb(value, port->membase + offset);
  66. break;
  67. case UPIO_MEM16:
  68. writew(value, port->membase + offset);
  69. break;
  70. case UPIO_MEM32:
  71. writel(value, port->membase + offset);
  72. break;
  73. case UPIO_MEM32BE:
  74. iowrite32be(value, port->membase + offset);
  75. break;
  76. case UPIO_PORT:
  77. outb(value, port->iobase + offset);
  78. break;
  79. case UPIO_AU:
  80. port->serial_out(port, reg_offset, value);
  81. break;
  82. }
  83. }
  84. #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
  85. static void __init serial_putc(struct uart_port *port, int c)
  86. {
  87. unsigned int status;
  88. serial8250_early_out(port, UART_TX, c);
  89. for (;;) {
  90. status = serial8250_early_in(port, UART_LSR);
  91. if ((status & BOTH_EMPTY) == BOTH_EMPTY)
  92. break;
  93. cpu_relax();
  94. }
  95. }
  96. static void __init early_serial8250_write(struct console *console,
  97. const char *s, unsigned int count)
  98. {
  99. struct earlycon_device *device = console->data;
  100. struct uart_port *port = &device->port;
  101. uart_console_write(port, s, count, serial_putc);
  102. }
  103. static void __init init_port(struct earlycon_device *device)
  104. {
  105. struct uart_port *port = &device->port;
  106. unsigned int divisor;
  107. unsigned char c;
  108. unsigned int ier;
  109. serial8250_early_out(port, UART_LCR, 0x3); /* 8n1 */
  110. ier = serial8250_early_in(port, UART_IER);
  111. serial8250_early_out(port, UART_IER, ier & UART_IER_UUE); /* no interrupt */
  112. serial8250_early_out(port, UART_FCR, 0); /* no fifo */
  113. serial8250_early_out(port, UART_MCR, 0x3); /* DTR + RTS */
  114. divisor = DIV_ROUND_CLOSEST(port->uartclk, 16 * device->baud);
  115. c = serial8250_early_in(port, UART_LCR);
  116. serial8250_early_out(port, UART_LCR, c | UART_LCR_DLAB);
  117. serial8250_early_out(port, UART_DLL, divisor & 0xff);
  118. serial8250_early_out(port, UART_DLM, (divisor >> 8) & 0xff);
  119. serial8250_early_out(port, UART_LCR, c & ~UART_LCR_DLAB);
  120. }
  121. int __init early_serial8250_setup(struct earlycon_device *device,
  122. const char *options)
  123. {
  124. if (!(device->port.membase || device->port.iobase))
  125. return -ENODEV;
  126. if (!device->baud) {
  127. struct uart_port *port = &device->port;
  128. unsigned int ier;
  129. /* assume the device was initialized, only mask interrupts */
  130. ier = serial8250_early_in(port, UART_IER);
  131. serial8250_early_out(port, UART_IER, ier & UART_IER_UUE);
  132. } else
  133. init_port(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. OF_EARLYCON_DECLARE(ns16550, "ns16550", early_serial8250_setup);
  140. OF_EARLYCON_DECLARE(ns16550a, "ns16550a", early_serial8250_setup);
  141. OF_EARLYCON_DECLARE(uart, "nvidia,tegra20-uart", early_serial8250_setup);
  142. OF_EARLYCON_DECLARE(uart, "snps,dw-apb-uart", early_serial8250_setup);
  143. #ifdef CONFIG_SERIAL_8250_OMAP
  144. static int __init early_omap8250_setup(struct earlycon_device *device,
  145. const char *options)
  146. {
  147. struct uart_port *port = &device->port;
  148. if (!(device->port.membase || device->port.iobase))
  149. return -ENODEV;
  150. port->regshift = 2;
  151. device->con->write = early_serial8250_write;
  152. return 0;
  153. }
  154. OF_EARLYCON_DECLARE(omap8250, "ti,omap2-uart", early_omap8250_setup);
  155. OF_EARLYCON_DECLARE(omap8250, "ti,omap3-uart", early_omap8250_setup);
  156. OF_EARLYCON_DECLARE(omap8250, "ti,omap4-uart", early_omap8250_setup);
  157. #endif
  158. #ifdef CONFIG_SERIAL_8250_RT288X
  159. unsigned int au_serial_in(struct uart_port *p, int offset);
  160. void au_serial_out(struct uart_port *p, int offset, int value);
  161. static int __init early_au_setup(struct earlycon_device *dev, const char *opt)
  162. {
  163. dev->port.serial_in = au_serial_in;
  164. dev->port.serial_out = au_serial_out;
  165. dev->port.iotype = UPIO_AU;
  166. dev->con->write = early_serial8250_write;
  167. return 0;
  168. }
  169. OF_EARLYCON_DECLARE(palmchip, "ralink,rt2880-uart", early_au_setup);
  170. #endif