console.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (C) 2008-2010 Thomas Chou <thomas@wytron.com.tw>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. */
  18. #include <linux/io.h>
  19. #if (defined(CONFIG_SERIAL_ALTERA_JTAGUART_CONSOLE) && defined(JTAG_UART_BASE))\
  20. || (defined(CONFIG_SERIAL_ALTERA_UART_CONSOLE) && defined(UART0_BASE))
  21. static void *my_ioremap(unsigned long physaddr)
  22. {
  23. return (void *)(physaddr | CONFIG_NIOS2_IO_REGION_BASE);
  24. }
  25. #endif
  26. #if defined(CONFIG_SERIAL_ALTERA_JTAGUART_CONSOLE) && defined(JTAG_UART_BASE)
  27. #define ALTERA_JTAGUART_SIZE 8
  28. #define ALTERA_JTAGUART_DATA_REG 0
  29. #define ALTERA_JTAGUART_CONTROL_REG 4
  30. #define ALTERA_JTAGUART_CONTROL_AC_MSK (0x00000400)
  31. #define ALTERA_JTAGUART_CONTROL_WSPACE_MSK (0xFFFF0000)
  32. static void *uartbase;
  33. #if defined(CONFIG_SERIAL_ALTERA_JTAGUART_CONSOLE_BYPASS)
  34. static void jtag_putc(int ch)
  35. {
  36. if (readl(uartbase + ALTERA_JTAGUART_CONTROL_REG) &
  37. ALTERA_JTAGUART_CONTROL_WSPACE_MSK)
  38. writeb(ch, uartbase + ALTERA_JTAGUART_DATA_REG);
  39. }
  40. #else
  41. static void jtag_putc(int ch)
  42. {
  43. while ((readl(uartbase + ALTERA_JTAGUART_CONTROL_REG) &
  44. ALTERA_JTAGUART_CONTROL_WSPACE_MSK) == 0)
  45. ;
  46. writeb(ch, uartbase + ALTERA_JTAGUART_DATA_REG);
  47. }
  48. #endif
  49. static int putchar(int ch)
  50. {
  51. jtag_putc(ch);
  52. return ch;
  53. }
  54. static void console_init(void)
  55. {
  56. uartbase = my_ioremap((unsigned long) JTAG_UART_BASE);
  57. writel(ALTERA_JTAGUART_CONTROL_AC_MSK,
  58. uartbase + ALTERA_JTAGUART_CONTROL_REG);
  59. }
  60. #elif defined(CONFIG_SERIAL_ALTERA_UART_CONSOLE) && defined(UART0_BASE)
  61. #define ALTERA_UART_SIZE 32
  62. #define ALTERA_UART_TXDATA_REG 4
  63. #define ALTERA_UART_STATUS_REG 8
  64. #define ALTERA_UART_DIVISOR_REG 16
  65. #define ALTERA_UART_STATUS_TRDY_MSK (0x40)
  66. static unsigned uartbase;
  67. static void uart_putc(int ch)
  68. {
  69. int i;
  70. for (i = 0; (i < 0x10000); i++) {
  71. if (readw(uartbase + ALTERA_UART_STATUS_REG) &
  72. ALTERA_UART_STATUS_TRDY_MSK)
  73. break;
  74. }
  75. writeb(ch, uartbase + ALTERA_UART_TXDATA_REG);
  76. }
  77. static int putchar(int ch)
  78. {
  79. uart_putc(ch);
  80. if (ch == '\n')
  81. uart_putc('\r');
  82. return ch;
  83. }
  84. static void console_init(void)
  85. {
  86. unsigned int baud, baudclk;
  87. uartbase = (unsigned long) my_ioremap((unsigned long) UART0_BASE);
  88. baud = CONFIG_SERIAL_ALTERA_UART_BAUDRATE;
  89. baudclk = UART0_FREQ / baud;
  90. writew(baudclk, uartbase + ALTERA_UART_DIVISOR_REG);
  91. }
  92. #else
  93. static int putchar(int ch)
  94. {
  95. return ch;
  96. }
  97. static void console_init(void)
  98. {
  99. }
  100. #endif
  101. static int puts(const char *s)
  102. {
  103. while (*s)
  104. putchar(*s++);
  105. return 0;
  106. }