early_printk.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 = 1000000;
  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. if (*s == '\n')
  48. early_printk_uartlite_putc('\r');
  49. early_printk_uartlite_putc(*s);
  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 | CON_BOOT,
  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. if (*s == '\n')
  85. early_printk_uart16550_putc('\r');
  86. early_printk_uart16550_putc(*s);
  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 | CON_BOOT,
  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. int version = 0;
  113. if (early_console_initialized)
  114. return 1;
  115. base_addr = of_early_console(&version);
  116. if (base_addr) {
  117. #ifdef CONFIG_MMU
  118. early_console_reg_tlb_alloc(base_addr);
  119. #endif
  120. switch (version) {
  121. #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
  122. case UARTLITE:
  123. printk(KERN_INFO "Early console on uartlite "
  124. "at 0x%08x\n", base_addr);
  125. early_console = &early_serial_uartlite_console;
  126. break;
  127. #endif
  128. #ifdef CONFIG_SERIAL_8250_CONSOLE
  129. case UART16550:
  130. printk(KERN_INFO "Early console on uart16650 "
  131. "at 0x%08x\n", base_addr);
  132. early_console = &early_serial_uart16550_console;
  133. break;
  134. #endif
  135. default:
  136. printk(KERN_INFO "Unsupported early console %d\n",
  137. version);
  138. return 1;
  139. }
  140. register_console(early_console);
  141. early_console_initialized = 1;
  142. return 0;
  143. }
  144. return 1;
  145. }
  146. /* Remap early console to virtual address and do not allocate one TLB
  147. * only for early console because of performance degression */
  148. void __init remap_early_printk(void)
  149. {
  150. if (!early_console_initialized || !early_console)
  151. return;
  152. printk(KERN_INFO "early_printk_console remapping from 0x%x to ",
  153. base_addr);
  154. base_addr = (u32) ioremap(base_addr, PAGE_SIZE);
  155. printk(KERN_CONT "0x%x\n", base_addr);
  156. #ifdef CONFIG_MMU
  157. /*
  158. * Early console is on the top of skipped TLB entries
  159. * decrease tlb_skip size ensure that hardcoded TLB entry will be
  160. * used by generic algorithm
  161. * FIXME check if early console mapping is on the top by rereading
  162. * TLB entry and compare baseaddr
  163. * mts rtlbx, (tlb_skip - 1)
  164. * nop
  165. * mfs rX, rtlblo
  166. * nop
  167. * cmp rX, orig_base_addr
  168. */
  169. tlb_skip -= 1;
  170. #endif
  171. }
  172. void __init disable_early_printk(void)
  173. {
  174. if (!early_console_initialized || !early_console)
  175. return;
  176. printk(KERN_WARNING "disabling early console\n");
  177. unregister_console(early_console);
  178. early_console_initialized = 0;
  179. }