serial.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 <nios2.h>
  26. #include <nios2-io.h>
  27. /*------------------------------------------------------------------
  28. * JTAG acts as the serial port
  29. *-----------------------------------------------------------------*/
  30. #if defined(CONFIG_CONSOLE_JTAG)
  31. static nios_jtag_t *jtag =
  32. (nios_jtag_t *)CACHE_BYPASS(CFG_NIOS_CONSOLE);
  33. void serial_setbrg( void ){ return; }
  34. int serial_init( void ) { return(0);}
  35. void serial_putc (char c)
  36. {
  37. unsigned val;
  38. while (NIOS_JTAG_WSPACE (jtag->control) == 0)
  39. WATCHDOG_RESET ();
  40. jtag->data = (unsigned char)c;
  41. }
  42. void serial_puts (const char *s)
  43. {
  44. while (*s != 0)
  45. serial_putc (*s++);
  46. }
  47. int serial_tstc (void)
  48. {
  49. return (jtag->control & NIOS_JTAG_RRDY);
  50. }
  51. int serial_getc (void)
  52. {
  53. int c;
  54. unsigned val;
  55. while (1) {
  56. WATCHDOG_RESET ();
  57. val = jtag->data;
  58. if (val & NIOS_JTAG_RVALID)
  59. break;
  60. }
  61. c = val & 0x0ff;
  62. return (c);
  63. }
  64. /*------------------------------------------------------------------
  65. * UART the serial port
  66. *-----------------------------------------------------------------*/
  67. #else
  68. static nios_uart_t *uart = (nios_uart_t *)
  69. CACHE_BYPASS(CFG_NIOS_CONSOLE);
  70. #if defined(CFG_NIOS_FIXEDBAUD)
  71. /* Everything's already setup for fixed-baud PTF
  72. * assignment
  73. */
  74. void serial_setbrg (void){ return; }
  75. int serial_init (void) { return (0);}
  76. #else
  77. void serial_setbrg (void)
  78. {
  79. DECLARE_GLOBAL_DATA_PTR;
  80. unsigned div;
  81. div = (CONFIG_SYS_CLK_FREQ/gd->baudrate)-1;
  82. uart->divisor = div;
  83. return;
  84. }
  85. int serial_init (void)
  86. {
  87. serial_setbrg ();
  88. return (0);
  89. }
  90. #endif /* CFG_NIOS_FIXEDBAUD */
  91. /*-----------------------------------------------------------------------
  92. * UART CONSOLE
  93. *---------------------------------------------------------------------*/
  94. void serial_putc (char c)
  95. {
  96. if (c == '\n')
  97. serial_putc ('\r');
  98. while ((uart->status & NIOS_UART_TRDY) == 0)
  99. WATCHDOG_RESET ();
  100. uart->txdata = (unsigned char)c;
  101. }
  102. void serial_puts (const char *s)
  103. {
  104. while (*s != 0) {
  105. serial_putc (*s++);
  106. }
  107. }
  108. int serial_tstc (void)
  109. {
  110. return (uart->status & NIOS_UART_RRDY);
  111. }
  112. int serial_getc (void)
  113. {
  114. while (serial_tstc () == 0)
  115. WATCHDOG_RESET ();
  116. return( uart->rxdata & 0x00ff );
  117. }
  118. #endif /* CONFIG_JTAG_CONSOLE */