early_printk.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Early printk support for Microblaze.
  3. *
  4. * Copyright (C) 2007-2009 Michal Simek <monstr@monstr.eu>
  5. * Copyright (C) 2007-2009 PetaLogix
  6. * Copyright (C) 2003-2006 Yasushi SHOJI <yashi@atmark-techno.com>
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License. See the file "COPYING" in the main directory of this archive
  10. * for more details.
  11. */
  12. #include <linux/console.h>
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/string.h>
  16. #include <linux/tty.h>
  17. #include <linux/io.h>
  18. #include <asm/processor.h>
  19. #include <linux/fcntl.h>
  20. #include <asm/setup.h>
  21. #include <asm/prom.h>
  22. static u32 early_console_initialized;
  23. static u32 base_addr;
  24. #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
  25. static void early_printk_uartlite_putc(char c)
  26. {
  27. /*
  28. * Limit how many times we'll spin waiting for TX FIFO status.
  29. * This will prevent lockups if the base address is incorrectly
  30. * set, or any other issue on the UARTLITE.
  31. * This limit is pretty arbitrary, unless we are at about 10 baud
  32. * we'll never timeout on a working UART.
  33. */
  34. unsigned retries = 10000;
  35. /* read status bit - 0x8 offset */
  36. while (--retries && (in_be32(base_addr + 8) & (1 << 3)))
  37. ;
  38. /* Only attempt the iowrite if we didn't timeout */
  39. /* write to TX_FIFO - 0x4 offset */
  40. if (retries)
  41. out_be32(base_addr + 4, c & 0xff);
  42. }
  43. static void early_printk_uartlite_write(struct console *unused,
  44. const char *s, unsigned n)
  45. {
  46. while (*s && n-- > 0) {
  47. early_printk_uartlite_putc(*s);
  48. if (*s == '\n')
  49. early_printk_uartlite_putc('\r');
  50. s++;
  51. }
  52. }
  53. static struct console early_serial_uartlite_console = {
  54. .name = "earlyser",
  55. .write = early_printk_uartlite_write,
  56. .flags = CON_PRINTBUFFER,
  57. .index = -1,
  58. };
  59. #endif /* CONFIG_SERIAL_UARTLITE_CONSOLE */
  60. #ifdef CONFIG_SERIAL_8250_CONSOLE
  61. static void early_printk_uart16550_putc(char c)
  62. {
  63. /*
  64. * Limit how many times we'll spin waiting for TX FIFO status.
  65. * This will prevent lockups if the base address is incorrectly
  66. * set, or any other issue on the UARTLITE.
  67. * This limit is pretty arbitrary, unless we are at about 10 baud
  68. * we'll never timeout on a working UART.
  69. */
  70. #define UART_LSR_TEMT 0x40 /* Transmitter empty */
  71. #define UART_LSR_THRE 0x20 /* Transmit-hold-register empty */
  72. #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
  73. unsigned retries = 10000;
  74. while (--retries &&
  75. !((in_be32(base_addr + 0x14) & BOTH_EMPTY) == BOTH_EMPTY))
  76. ;
  77. if (retries)
  78. out_be32(base_addr, c & 0xff);
  79. }
  80. static void early_printk_uart16550_write(struct console *unused,
  81. const char *s, unsigned n)
  82. {
  83. while (*s && n-- > 0) {
  84. early_printk_uart16550_putc(*s);
  85. if (*s == '\n')
  86. early_printk_uart16550_putc('\r');
  87. s++;
  88. }
  89. }
  90. static struct console early_serial_uart16550_console = {
  91. .name = "earlyser",
  92. .write = early_printk_uart16550_write,
  93. .flags = CON_PRINTBUFFER,
  94. .index = -1,
  95. };
  96. #endif /* CONFIG_SERIAL_8250_CONSOLE */
  97. static struct console *early_console;
  98. void early_printk(const char *fmt, ...)
  99. {
  100. char buf[512];
  101. int n;
  102. va_list ap;
  103. if (early_console_initialized) {
  104. va_start(ap, fmt);
  105. n = vscnprintf(buf, 512, fmt, ap);
  106. early_console->write(early_console, buf, n);
  107. va_end(ap);
  108. }
  109. }
  110. int __init setup_early_printk(char *opt)
  111. {
  112. if (early_console_initialized)
  113. return 1;
  114. #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
  115. base_addr = early_uartlite_console();
  116. if (base_addr) {
  117. early_console_initialized = 1;
  118. #ifdef CONFIG_MMU
  119. early_console_reg_tlb_alloc(base_addr);
  120. #endif
  121. early_console = &early_serial_uartlite_console;
  122. early_printk("early_printk_console is enabled at 0x%08x\n",
  123. base_addr);
  124. /* register_console(early_console); */
  125. return 0;
  126. }
  127. #endif /* CONFIG_SERIAL_UARTLITE_CONSOLE */
  128. #ifdef CONFIG_SERIAL_8250_CONSOLE
  129. base_addr = early_uart16550_console();
  130. base_addr &= ~3; /* clear register offset */
  131. if (base_addr) {
  132. early_console_initialized = 1;
  133. #ifdef CONFIG_MMU
  134. early_console_reg_tlb_alloc(base_addr);
  135. #endif
  136. early_console = &early_serial_uart16550_console;
  137. early_printk("early_printk_console is enabled at 0x%08x\n",
  138. base_addr);
  139. /* register_console(early_console); */
  140. return 0;
  141. }
  142. #endif /* CONFIG_SERIAL_8250_CONSOLE */
  143. return 1;
  144. }
  145. void __init disable_early_printk(void)
  146. {
  147. if (!early_console_initialized || !early_console)
  148. return;
  149. printk(KERN_WARNING "disabling early console\n");
  150. unregister_console(early_console);
  151. early_console_initialized = 0;
  152. }