early_printk.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. static void early_printk_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 = 10000;
  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_write(struct console *unused,
  43. const char *s, unsigned n)
  44. {
  45. while (*s && n-- > 0) {
  46. early_printk_putc(*s);
  47. if (*s == '\n')
  48. early_printk_putc('\r');
  49. s++;
  50. }
  51. }
  52. static struct console early_serial_console = {
  53. .name = "earlyser",
  54. .write = early_printk_write,
  55. .flags = CON_PRINTBUFFER,
  56. .index = -1,
  57. };
  58. static struct console *early_console = &early_serial_console;
  59. void early_printk(const char *fmt, ...)
  60. {
  61. char buf[512];
  62. int n;
  63. va_list ap;
  64. if (early_console_initialized) {
  65. va_start(ap, fmt);
  66. n = vscnprintf(buf, 512, fmt, ap);
  67. early_console->write(early_console, buf, n);
  68. va_end(ap);
  69. }
  70. }
  71. int __init setup_early_printk(char *opt)
  72. {
  73. if (early_console_initialized)
  74. return 1;
  75. base_addr = early_uartlite_console();
  76. if (base_addr) {
  77. early_console_initialized = 1;
  78. early_printk("early_printk_console is enabled at 0x%08x\n",
  79. base_addr);
  80. /* register_console(early_console); */
  81. return 0;
  82. } else
  83. return 1;
  84. }
  85. void __init disable_early_printk(void)
  86. {
  87. if (!early_console_initialized || !early_console)
  88. return;
  89. printk(KERN_WARNING "disabling early console\n");
  90. unregister_console(early_console);
  91. early_console_initialized = 0;
  92. }