altera_uart.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * (C) Copyright 2004, Psyent Corporation <www.psyent.com>
  3. * Scott McNutt <smcnutt@psyent.com>
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <watchdog.h>
  25. #include <asm/io.h>
  26. #include <nios2-io.h>
  27. #include <linux/compiler.h>
  28. #include <serial.h>
  29. DECLARE_GLOBAL_DATA_PTR;
  30. /*------------------------------------------------------------------
  31. * UART the serial port
  32. *-----------------------------------------------------------------*/
  33. static nios_uart_t *uart = (nios_uart_t *) CONFIG_SYS_NIOS_CONSOLE;
  34. #if defined(CONFIG_SYS_NIOS_FIXEDBAUD)
  35. /*
  36. * Everything's already setup for fixed-baud PTF
  37. * assignment
  38. */
  39. static void altera_serial_setbrg(void)
  40. {
  41. }
  42. static int altera_serial_init(void)
  43. {
  44. return 0;
  45. }
  46. #else
  47. static void altera_serial_setbrg(void)
  48. {
  49. unsigned div;
  50. div = (CONFIG_SYS_CLK_FREQ/gd->baudrate)-1;
  51. writel (div, &uart->divisor);
  52. }
  53. static int altera_serial_init(void)
  54. {
  55. serial_setbrg();
  56. return 0;
  57. }
  58. #endif /* CONFIG_SYS_NIOS_FIXEDBAUD */
  59. /*-----------------------------------------------------------------------
  60. * UART CONSOLE
  61. *---------------------------------------------------------------------*/
  62. static void altera_serial_putc(char c)
  63. {
  64. if (c == '\n')
  65. serial_putc ('\r');
  66. while ((readl (&uart->status) & NIOS_UART_TRDY) == 0)
  67. WATCHDOG_RESET ();
  68. writel ((unsigned char)c, &uart->txdata);
  69. }
  70. static void altera_serial_puts(const char *s)
  71. {
  72. while (*s != 0) {
  73. serial_putc (*s++);
  74. }
  75. }
  76. static int altera_serial_tstc(void)
  77. {
  78. return (readl (&uart->status) & NIOS_UART_RRDY);
  79. }
  80. static int altera_serial_getc(void)
  81. {
  82. while (serial_tstc () == 0)
  83. WATCHDOG_RESET ();
  84. return (readl (&uart->rxdata) & 0x00ff );
  85. }
  86. #ifdef CONFIG_SERIAL_MULTI
  87. static struct serial_device altera_serial_drv = {
  88. .name = "altera_serial",
  89. .start = altera_serial_init,
  90. .stop = NULL,
  91. .setbrg = altera_serial_setbrg,
  92. .putc = altera_serial_putc,
  93. .puts = altera_serial_puts,
  94. .getc = altera_serial_getc,
  95. .tstc = altera_serial_tstc,
  96. };
  97. void altera_serial_initialize(void)
  98. {
  99. serial_register(&altera_serial_drv);
  100. }
  101. __weak struct serial_device *default_serial_console(void)
  102. {
  103. return &altera_serial_drv;
  104. }
  105. #else
  106. int serial_init(void)
  107. {
  108. return altera_serial_init();
  109. }
  110. void serial_setbrg(void)
  111. {
  112. altera_serial_setbrg();
  113. }
  114. void serial_putc(const char c)
  115. {
  116. altera_serial_putc(c);
  117. }
  118. void serial_puts(const char *s)
  119. {
  120. altera_serial_puts(s);
  121. }
  122. int serial_getc(void)
  123. {
  124. return altera_serial_getc();
  125. }
  126. int serial_tstc(void)
  127. {
  128. return altera_serial_tstc();
  129. }
  130. #endif