8250_early.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Early serial console for 8250/16550 devices
  3. *
  4. * (c) Copyright 2004 Hewlett-Packard Development Company, L.P.
  5. * Bjorn Helgaas <[email protected]>
  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. offset <<= port->regshift;
  41. switch (port->iotype) {
  42. case UPIO_MEM:
  43. return readb(port->membase + offset);
  44. case UPIO_MEM16:
  45. return readw(port->membase + offset);
  46. case UPIO_MEM32:
  47. return readl(port->membase + offset);
  48. case UPIO_MEM32BE:
  49. return ioread32be(port->membase + offset);
  50. case UPIO_PORT:
  51. return inb(port->iobase + offset);
  52. default:
  53. return 0;
  54. }
  55. }
  56. static void __init serial8250_early_out(struct uart_port *port, int offset, int value)
  57. {
  58. offset <<= port->regshift;
  59. switch (port->iotype) {
  60. case UPIO_MEM:
  61. writeb(value, port->membase + offset);
  62. break;
  63. case UPIO_MEM16:
  64. writew(value, port->membase + offset);
  65. break;
  66. case UPIO_MEM32:
  67. writel(value, port->membase + offset);
  68. break;
  69. case UPIO_MEM32BE:
  70. iowrite32be(value, port->membase + offset);
  71. break;
  72. case UPIO_PORT:
  73. outb(value, port->iobase + offset);
  74. break;
  75. }
  76. }
  77. #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
  78. static void __init serial_putc(struct uart_port *port, int c)
  79. {
  80. unsigned int status;
  81. serial8250_early_out(port, UART_TX, c);
  82. for (;;) {
  83. status = serial8250_early_in(port, UART_LSR);
  84. if ((status & BOTH_EMPTY) == BOTH_EMPTY)
  85. break;
  86. cpu_relax();
  87. }
  88. }
  89. static void __init early_serial8250_write(struct console *console,
  90. const char *s, unsigned int count)
  91. {
  92. struct earlycon_device *device = console->data;
  93. struct uart_port *port = &device->port;
  94. uart_console_write(port, s, count, serial_putc);
  95. }
  96. static void __init init_port(struct earlycon_device *device)
  97. {
  98. struct uart_port *port = &device->port;
  99. unsigned int divisor;
  100. unsigned char c;
  101. unsigned int ier;
  102. serial8250_early_out(port, UART_LCR, 0x3); /* 8n1 */
  103. ier = serial8250_early_in(port, UART_IER);
  104. serial8250_early_out(port, UART_IER, ier & UART_IER_UUE); /* no interrupt */
  105. serial8250_early_out(port, UART_FCR, 0); /* no fifo */
  106. serial8250_early_out(port, UART_MCR, 0x3); /* DTR + RTS */
  107. divisor = DIV_ROUND_CLOSEST(port->uartclk, 16 * device->baud);
  108. c = serial8250_early_in(port, UART_LCR);
  109. serial8250_early_out(port, UART_LCR, c | UART_LCR_DLAB);
  110. serial8250_early_out(port, UART_DLL, divisor & 0xff);
  111. serial8250_early_out(port, UART_DLM, (divisor >> 8) & 0xff);
  112. serial8250_early_out(port, UART_LCR, c & ~UART_LCR_DLAB);
  113. }
  114. int __init early_serial8250_setup(struct earlycon_device *device,
  115. const char *options)
  116. {
  117. if (!(device->port.membase || device->port.iobase))
  118. return -ENODEV;
  119. if (!device->baud) {
  120. struct uart_port *port = &device->port;
  121. unsigned int ier;
  122. /* assume the device was initialized, only mask interrupts */
  123. ier = serial8250_early_in(port, UART_IER);
  124. serial8250_early_out(port, UART_IER, ier & UART_IER_UUE);
  125. } else
  126. init_port(device);
  127. device->con->write = early_serial8250_write;
  128. return 0;
  129. }
  130. EARLYCON_DECLARE(uart8250, early_serial8250_setup);
  131. EARLYCON_DECLARE(uart, early_serial8250_setup);
  132. OF_EARLYCON_DECLARE(ns16550, "ns16550", early_serial8250_setup);
  133. OF_EARLYCON_DECLARE(ns16550a, "ns16550a", early_serial8250_setup);
  134. OF_EARLYCON_DECLARE(uart, "nvidia,tegra20-uart", early_serial8250_setup);
  135. OF_EARLYCON_DECLARE(uart, "snps,dw-apb-uart", early_serial8250_setup);
  136. #ifdef CONFIG_SERIAL_8250_OMAP
  137. static int __init early_omap8250_setup(struct earlycon_device *device,
  138. const char *options)
  139. {
  140. struct uart_port *port = &device->port;
  141. if (!(device->port.membase || device->port.iobase))
  142. return -ENODEV;
  143. port->regshift = 2;
  144. device->con->write = early_serial8250_write;
  145. return 0;
  146. }
  147. OF_EARLYCON_DECLARE(omap8250, "ti,omap2-uart", early_omap8250_setup);
  148. OF_EARLYCON_DECLARE(omap8250, "ti,omap3-uart", early_omap8250_setup);
  149. OF_EARLYCON_DECLARE(omap8250, "ti,omap4-uart", early_omap8250_setup);
  150. #endif