serial.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. DECLARE_GLOBAL_DATA_PTR;
  28. /*------------------------------------------------------------------
  29. * JTAG acts as the serial port
  30. *-----------------------------------------------------------------*/
  31. #if defined(CONFIG_CONSOLE_JTAG)
  32. static nios_jtag_t *jtag = (nios_jtag_t *)CONFIG_SYS_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 ( readl (&jtag->control)) == 0)
  39. WATCHDOG_RESET ();
  40. writel (&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 ( readl (&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 = readl (&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 *) CONFIG_SYS_NIOS_CONSOLE;
  69. #if defined(CONFIG_SYS_NIOS_FIXEDBAUD)
  70. /* Everything's already setup for fixed-baud PTF
  71. * assignment
  72. */
  73. void serial_setbrg (void){ return; }
  74. int serial_init (void) { return (0);}
  75. #else
  76. void serial_setbrg (void)
  77. {
  78. unsigned div;
  79. div = (CONFIG_SYS_CLK_FREQ/gd->baudrate)-1;
  80. writel (&uart->divisor,div);
  81. return;
  82. }
  83. int serial_init (void)
  84. {
  85. serial_setbrg ();
  86. return (0);
  87. }
  88. #endif /* CONFIG_SYS_NIOS_FIXEDBAUD */
  89. /*-----------------------------------------------------------------------
  90. * UART CONSOLE
  91. *---------------------------------------------------------------------*/
  92. void serial_putc (char c)
  93. {
  94. if (c == '\n')
  95. serial_putc ('\r');
  96. while ((readl (&uart->status) & NIOS_UART_TRDY) == 0)
  97. WATCHDOG_RESET ();
  98. writel (&uart->txdata,(unsigned char)c);
  99. }
  100. void serial_puts (const char *s)
  101. {
  102. while (*s != 0) {
  103. serial_putc (*s++);
  104. }
  105. }
  106. int serial_tstc (void)
  107. {
  108. return (readl (&uart->status) & NIOS_UART_RRDY);
  109. }
  110. int serial_getc (void)
  111. {
  112. while (serial_tstc () == 0)
  113. WATCHDOG_RESET ();
  114. return (readl (&uart->rxdata) & 0x00ff );
  115. }
  116. #endif /* CONFIG_JTAG_CONSOLE */