early_printk.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Early printk for Nios2.
  3. *
  4. * Copyright (C) 2015, Altera Corporation
  5. * Copyright (C) 2010, Tobias Klauser <tklauser@distanz.ch>
  6. * Copyright (C) 2009, Wind River Systems Inc
  7. * Implemented by fredrik.markstrom@gmail.com and ivarholmqvist@gmail.com
  8. *
  9. * This file is subject to the terms and conditions of the GNU General Public
  10. * License. See the file "COPYING" in the main directory of this archive
  11. * for more details.
  12. */
  13. #include <linux/console.h>
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/io.h>
  17. #include <asm/prom.h>
  18. static unsigned long base_addr;
  19. #if defined(CONFIG_SERIAL_ALTERA_JTAGUART_CONSOLE)
  20. #define ALTERA_JTAGUART_DATA_REG 0
  21. #define ALTERA_JTAGUART_CONTROL_REG 4
  22. #define ALTERA_JTAGUART_CONTROL_WSPACE_MSK 0xFFFF0000
  23. #define ALTERA_JTAGUART_CONTROL_AC_MSK 0x00000400
  24. #define JUART_GET_CR() \
  25. __builtin_ldwio((void *)(base_addr + ALTERA_JTAGUART_CONTROL_REG))
  26. #define JUART_SET_CR(v) \
  27. __builtin_stwio((void *)(base_addr + ALTERA_JTAGUART_CONTROL_REG), v)
  28. #define JUART_SET_TX(v) \
  29. __builtin_stwio((void *)(base_addr + ALTERA_JTAGUART_DATA_REG), v)
  30. static void early_console_write(struct console *con, const char *s, unsigned n)
  31. {
  32. unsigned long status;
  33. while (n-- && *s) {
  34. while (((status = JUART_GET_CR())
  35. & ALTERA_JTAGUART_CONTROL_WSPACE_MSK) == 0) {
  36. #if defined(CONFIG_SERIAL_ALTERA_JTAGUART_CONSOLE_BYPASS)
  37. if ((status & ALTERA_JTAGUART_CONTROL_AC_MSK) == 0)
  38. return; /* no connection activity */
  39. #endif
  40. }
  41. JUART_SET_TX(*s);
  42. s++;
  43. }
  44. }
  45. #elif defined(CONFIG_SERIAL_ALTERA_UART_CONSOLE)
  46. #define ALTERA_UART_TXDATA_REG 4
  47. #define ALTERA_UART_STATUS_REG 8
  48. #define ALTERA_UART_STATUS_TRDY 0x0040
  49. #define UART_GET_SR() \
  50. __builtin_ldwio((void *)(base_addr + ALTERA_UART_STATUS_REG))
  51. #define UART_SET_TX(v) \
  52. __builtin_stwio((void *)(base_addr + ALTERA_UART_TXDATA_REG), v)
  53. static void early_console_putc(char c)
  54. {
  55. while (!(UART_GET_SR() & ALTERA_UART_STATUS_TRDY))
  56. ;
  57. UART_SET_TX(c);
  58. }
  59. static void early_console_write(struct console *con, const char *s, unsigned n)
  60. {
  61. while (n-- && *s) {
  62. early_console_putc(*s);
  63. if (*s == '\n')
  64. early_console_putc('\r');
  65. s++;
  66. }
  67. }
  68. #else
  69. # error Neither SERIAL_ALTERA_JTAGUART_CONSOLE nor SERIAL_ALTERA_UART_CONSOLE \
  70. selected
  71. #endif
  72. static struct console early_console_prom = {
  73. .name = "early",
  74. .write = early_console_write,
  75. .flags = CON_PRINTBUFFER | CON_BOOT,
  76. .index = -1
  77. };
  78. void __init setup_early_printk(void)
  79. {
  80. #if defined(CONFIG_SERIAL_ALTERA_JTAGUART_CONSOLE) || \
  81. defined(CONFIG_SERIAL_ALTERA_UART_CONSOLE)
  82. base_addr = of_early_console();
  83. #else
  84. base_addr = 0;
  85. #endif
  86. if (!base_addr)
  87. return;
  88. #if defined(CONFIG_SERIAL_ALTERA_JTAGUART_CONSOLE_BYPASS)
  89. /* Clear activity bit so BYPASS doesn't stall if we've used JTAG for
  90. * downloading the kernel. This might cause early data to be lost even
  91. * if the JTAG terminal is running.
  92. */
  93. JUART_SET_CR(JUART_GET_CR() | ALTERA_JTAGUART_CONTROL_AC_MSK);
  94. #endif
  95. early_console = &early_console_prom;
  96. register_console(early_console);
  97. pr_info("early_console initialized at 0x%08lx\n", base_addr);
  98. }