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