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> |
Scott McNutt | c2ced00 | 2006-06-08 11:59:57 -0400 | [diff] [blame] | 27 | #include <asm/io.h> |
wdenk | 5c952cf | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 28 | #include <nios2-io.h> |
| 29 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 30 | DECLARE_GLOBAL_DATA_PTR; |
| 31 | |
wdenk | 5c952cf | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 32 | /*------------------------------------------------------------------ |
| 33 | * JTAG acts as the serial port |
| 34 | *-----------------------------------------------------------------*/ |
| 35 | #if defined(CONFIG_CONSOLE_JTAG) |
| 36 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 37 | static nios_jtag_t *jtag = (nios_jtag_t *)CONFIG_SYS_NIOS_CONSOLE; |
wdenk | 5c952cf | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 38 | |
| 39 | void serial_setbrg( void ){ return; } |
| 40 | int serial_init( void ) { return(0);} |
| 41 | |
| 42 | void serial_putc (char c) |
| 43 | { |
| 44 | unsigned val; |
| 45 | |
Scott McNutt | c2ced00 | 2006-06-08 11:59:57 -0400 | [diff] [blame] | 46 | while (NIOS_JTAG_WSPACE ( readl (&jtag->control)) == 0) |
wdenk | 5c952cf | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 47 | WATCHDOG_RESET (); |
Scott McNutt | c2ced00 | 2006-06-08 11:59:57 -0400 | [diff] [blame] | 48 | writel (&jtag->data, (unsigned char)c); |
wdenk | 5c952cf | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void serial_puts (const char *s) |
| 52 | { |
| 53 | while (*s != 0) |
| 54 | serial_putc (*s++); |
| 55 | } |
| 56 | |
| 57 | int serial_tstc (void) |
| 58 | { |
Scott McNutt | c2ced00 | 2006-06-08 11:59:57 -0400 | [diff] [blame] | 59 | return ( readl (&jtag->control) & NIOS_JTAG_RRDY); |
wdenk | 5c952cf | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | int serial_getc (void) |
| 63 | { |
| 64 | int c; |
| 65 | unsigned val; |
| 66 | |
| 67 | while (1) { |
| 68 | WATCHDOG_RESET (); |
Scott McNutt | c2ced00 | 2006-06-08 11:59:57 -0400 | [diff] [blame] | 69 | val = readl (&jtag->data); |
wdenk | 5c952cf | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 70 | if (val & NIOS_JTAG_RVALID) |
| 71 | break; |
| 72 | } |
| 73 | c = val & 0x0ff; |
| 74 | return (c); |
| 75 | } |
| 76 | |
| 77 | /*------------------------------------------------------------------ |
| 78 | * UART the serial port |
| 79 | *-----------------------------------------------------------------*/ |
| 80 | #else |
| 81 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 82 | static nios_uart_t *uart = (nios_uart_t *) CONFIG_SYS_NIOS_CONSOLE; |
wdenk | 5c952cf | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 83 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 84 | #if defined(CONFIG_SYS_NIOS_FIXEDBAUD) |
wdenk | 5c952cf | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 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 | { |
wdenk | 5c952cf | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 96 | unsigned div; |
| 97 | |
| 98 | div = (CONFIG_SYS_CLK_FREQ/gd->baudrate)-1; |
Scott McNutt | c2ced00 | 2006-06-08 11:59:57 -0400 | [diff] [blame] | 99 | writel (&uart->divisor,div); |
wdenk | 5c952cf | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 100 | return; |
| 101 | } |
| 102 | |
| 103 | int serial_init (void) |
| 104 | { |
| 105 | serial_setbrg (); |
| 106 | return (0); |
| 107 | } |
| 108 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 109 | #endif /* CONFIG_SYS_NIOS_FIXEDBAUD */ |
wdenk | 5c952cf | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 110 | |
| 111 | |
| 112 | /*----------------------------------------------------------------------- |
| 113 | * UART CONSOLE |
| 114 | *---------------------------------------------------------------------*/ |
| 115 | void serial_putc (char c) |
| 116 | { |
| 117 | if (c == '\n') |
| 118 | serial_putc ('\r'); |
Scott McNutt | c2ced00 | 2006-06-08 11:59:57 -0400 | [diff] [blame] | 119 | while ((readl (&uart->status) & NIOS_UART_TRDY) == 0) |
wdenk | 5c952cf | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 120 | WATCHDOG_RESET (); |
Scott McNutt | c2ced00 | 2006-06-08 11:59:57 -0400 | [diff] [blame] | 121 | writel (&uart->txdata,(unsigned char)c); |
wdenk | 5c952cf | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void serial_puts (const char *s) |
| 125 | { |
| 126 | while (*s != 0) { |
| 127 | serial_putc (*s++); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | int serial_tstc (void) |
| 132 | { |
Scott McNutt | c2ced00 | 2006-06-08 11:59:57 -0400 | [diff] [blame] | 133 | return (readl (&uart->status) & NIOS_UART_RRDY); |
wdenk | 5c952cf | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | int serial_getc (void) |
| 137 | { |
| 138 | while (serial_tstc () == 0) |
| 139 | WATCHDOG_RESET (); |
Scott McNutt | c2ced00 | 2006-06-08 11:59:57 -0400 | [diff] [blame] | 140 | return (readl (&uart->rxdata) & 0x00ff ); |
wdenk | 5c952cf | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | #endif /* CONFIG_JTAG_CONSOLE */ |