early_printk.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 base_addr;
  23. #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
  24. static void early_printk_uartlite_putc(char c)
  25. {
  26. /*
  27. * Limit how many times we'll spin waiting for TX FIFO status.
  28. * This will prevent lockups if the base address is incorrectly
  29. * set, or any other issue on the UARTLITE.
  30. * This limit is pretty arbitrary, unless we are at about 10 baud
  31. * we'll never timeout on a working UART.
  32. */
  33. unsigned retries = 1000000;
  34. /* read status bit - 0x8 offset */
  35. while (--retries && (in_be32(base_addr + 8) & (1 << 3)))
  36. ;
  37. /* Only attempt the iowrite if we didn't timeout */
  38. /* write to TX_FIFO - 0x4 offset */
  39. if (retries)
  40. out_be32(base_addr + 4, c & 0xff);
  41. }
  42. static void early_printk_uartlite_write(struct console *unused,
  43. const char *s, unsigned n)
  44. {
  45. while (*s && n-- > 0) {
  46. if (*s == '\n')
  47. early_printk_uartlite_putc('\r');
  48. early_printk_uartlite_putc(*s);
  49. s++;
  50. }
  51. }
  52. static struct console early_serial_uartlite_console = {
  53. .name = "earlyser",
  54. .write = early_printk_uartlite_write,
  55. .flags = CON_PRINTBUFFER | CON_BOOT,
  56. .index = -1,
  57. };
  58. #endif /* CONFIG_SERIAL_UARTLITE_CONSOLE */
  59. #ifdef CONFIG_SERIAL_8250_CONSOLE
  60. static void early_printk_uart16550_putc(char c)
  61. {
  62. /*
  63. * Limit how many times we'll spin waiting for TX FIFO status.
  64. * This will prevent lockups if the base address is incorrectly
  65. * set, or any other issue on the UARTLITE.
  66. * This limit is pretty arbitrary, unless we are at about 10 baud
  67. * we'll never timeout on a working UART.
  68. */
  69. #define UART_LSR_TEMT 0x40 /* Transmitter empty */
  70. #define UART_LSR_THRE 0x20 /* Transmit-hold-register empty */
  71. #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
  72. unsigned retries = 10000;
  73. while (--retries &&
  74. !((in_be32(base_addr + 0x14) & BOTH_EMPTY) == BOTH_EMPTY))
  75. ;
  76. if (retries)
  77. out_be32(base_addr, c & 0xff);
  78. }
  79. static void early_printk_uart16550_write(struct console *unused,
  80. const char *s, unsigned n)
  81. {
  82. while (*s && n-- > 0) {
  83. if (*s == '\n')
  84. early_printk_uart16550_putc('\r');
  85. early_printk_uart16550_putc(*s);
  86. s++;
  87. }
  88. }
  89. static struct console early_serial_uart16550_console = {
  90. .name = "earlyser",
  91. .write = early_printk_uart16550_write,
  92. .flags = CON_PRINTBUFFER | CON_BOOT,
  93. .index = -1,
  94. };
  95. #endif /* CONFIG_SERIAL_8250_CONSOLE */
  96. int __init setup_early_printk(char *opt)
  97. {
  98. int version = 0;
  99. if (early_console)
  100. return 1;
  101. base_addr = of_early_console(&version);
  102. if (base_addr) {
  103. #ifdef CONFIG_MMU
  104. early_console_reg_tlb_alloc(base_addr);
  105. #endif
  106. switch (version) {
  107. #ifdef CONFIG_SERIAL_UARTLITE_CONSOLE
  108. case UARTLITE:
  109. pr_info("Early console on uartlite at 0x%08x\n",
  110. base_addr);
  111. early_console = &early_serial_uartlite_console;
  112. break;
  113. #endif
  114. #ifdef CONFIG_SERIAL_8250_CONSOLE
  115. case UART16550:
  116. pr_info("Early console on uart16650 at 0x%08x\n",
  117. base_addr);
  118. early_console = &early_serial_uart16550_console;
  119. break;
  120. #endif
  121. default:
  122. pr_info("Unsupported early console %d\n",
  123. version);
  124. return 1;
  125. }
  126. register_console(early_console);
  127. return 0;
  128. }
  129. return 1;
  130. }
  131. /* Remap early console to virtual address and do not allocate one TLB
  132. * only for early console because of performance degression */
  133. void __init remap_early_printk(void)
  134. {
  135. if (!early_console)
  136. return;
  137. pr_info("early_printk_console remapping from 0x%x to ", base_addr);
  138. base_addr = (u32) ioremap(base_addr, PAGE_SIZE);
  139. pr_cont("0x%x\n", base_addr);
  140. #ifdef CONFIG_MMU
  141. /*
  142. * Early console is on the top of skipped TLB entries
  143. * decrease tlb_skip size ensure that hardcoded TLB entry will be
  144. * used by generic algorithm
  145. * FIXME check if early console mapping is on the top by rereading
  146. * TLB entry and compare baseaddr
  147. * mts rtlbx, (tlb_skip - 1)
  148. * nop
  149. * mfs rX, rtlblo
  150. * nop
  151. * cmp rX, orig_base_addr
  152. */
  153. tlb_skip -= 1;
  154. #endif
  155. }
  156. void __init disable_early_printk(void)
  157. {
  158. if (!early_console)
  159. return;
  160. pr_warn("disabling early console\n");
  161. unregister_console(early_console);
  162. early_console = NULL;
  163. }