wdenk | ef3386f | 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 | * |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame^] | 5 | * YANU Support: |
| 6 | * Copyright 2010, Renato Andreola <renato.andreola@imagos.it> |
| 7 | * |
wdenk | ef3386f | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 8 | * See file CREDITS for list of people who contributed to this |
| 9 | * project. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License as |
| 13 | * published by the Free Software Foundation; either version 2 of |
| 14 | * the License, or (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 24 | * MA 02111-1307 USA |
| 25 | */ |
| 26 | |
| 27 | |
| 28 | #include <common.h> |
| 29 | #include <watchdog.h> |
Scott McNutt | a5721a3 | 2006-06-08 11:59:57 -0400 | [diff] [blame] | 30 | #include <asm/io.h> |
wdenk | ef3386f | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 31 | #include <nios2-io.h> |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame^] | 32 | #include <nios2-yanu.h> |
wdenk | ef3386f | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 33 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 34 | DECLARE_GLOBAL_DATA_PTR; |
| 35 | |
wdenk | ef3386f | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 36 | /*------------------------------------------------------------------ |
| 37 | * JTAG acts as the serial port |
| 38 | *-----------------------------------------------------------------*/ |
| 39 | #if defined(CONFIG_CONSOLE_JTAG) |
| 40 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 41 | static nios_jtag_t *jtag = (nios_jtag_t *)CONFIG_SYS_NIOS_CONSOLE; |
wdenk | ef3386f | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 42 | |
| 43 | void serial_setbrg( void ){ return; } |
| 44 | int serial_init( void ) { return(0);} |
| 45 | |
| 46 | void serial_putc (char c) |
| 47 | { |
| 48 | unsigned val; |
| 49 | |
Scott McNutt | a5721a3 | 2006-06-08 11:59:57 -0400 | [diff] [blame] | 50 | while (NIOS_JTAG_WSPACE ( readl (&jtag->control)) == 0) |
wdenk | ef3386f | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 51 | WATCHDOG_RESET (); |
Scott McNutt | a5721a3 | 2006-06-08 11:59:57 -0400 | [diff] [blame] | 52 | writel (&jtag->data, (unsigned char)c); |
wdenk | ef3386f | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void serial_puts (const char *s) |
| 56 | { |
| 57 | while (*s != 0) |
| 58 | serial_putc (*s++); |
| 59 | } |
| 60 | |
| 61 | int serial_tstc (void) |
| 62 | { |
Scott McNutt | a5721a3 | 2006-06-08 11:59:57 -0400 | [diff] [blame] | 63 | return ( readl (&jtag->control) & NIOS_JTAG_RRDY); |
wdenk | ef3386f | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | int serial_getc (void) |
| 67 | { |
| 68 | int c; |
| 69 | unsigned val; |
| 70 | |
| 71 | while (1) { |
| 72 | WATCHDOG_RESET (); |
Scott McNutt | a5721a3 | 2006-06-08 11:59:57 -0400 | [diff] [blame] | 73 | val = readl (&jtag->data); |
wdenk | ef3386f | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 74 | if (val & NIOS_JTAG_RVALID) |
| 75 | break; |
| 76 | } |
| 77 | c = val & 0x0ff; |
| 78 | return (c); |
| 79 | } |
| 80 | |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame^] | 81 | #elif defined(CONFIG_CONSOLE_YANU) |
| 82 | /*-----------------------------------------------------------------*/ |
| 83 | /* YANU Imagos serial port */ |
| 84 | /*-----------------------------------------------------------------*/ |
| 85 | |
| 86 | static yanu_uart_t *uart = (yanu_uart_t *)CONFIG_SYS_NIOS_CONSOLE; |
| 87 | |
| 88 | #if defined(CONFIG_SYS_NIOS_FIXEDBAUD) |
| 89 | |
| 90 | /* Everything's already setup for fixed-baud PTF assignment*/ |
| 91 | |
| 92 | void serial_setbrg (void) |
| 93 | { |
| 94 | int n, k; |
| 95 | const unsigned max_uns = 0xFFFFFFFF; |
| 96 | unsigned best_n, best_m, baud; |
| 97 | |
| 98 | /* compute best N and M couple */ |
| 99 | best_n = YANU_MAX_PRESCALER_N; |
| 100 | for (n = YANU_MAX_PRESCALER_N; n >= 0; n--) { |
| 101 | if ((unsigned)CONFIG_SYS_CLK_FREQ / (1 << (n + 4)) >= |
| 102 | (unsigned)CONFIG_BAUDRATE) { |
| 103 | best_n = n; |
| 104 | break; |
| 105 | } |
| 106 | } |
| 107 | for (k = 0;; k++) { |
| 108 | if ((unsigned)CONFIG_BAUDRATE <= (max_uns >> (15+n-k))) |
| 109 | break; |
| 110 | } |
| 111 | best_m = |
| 112 | ((unsigned)CONFIG_BAUDRATE * (1 << (15 + n - k))) / |
| 113 | ((unsigned)CONFIG_SYS_CLK_FREQ >> k); |
| 114 | |
| 115 | baud = best_m + best_n * YANU_BAUDE; |
| 116 | writel(&uart->baud, baud); |
| 117 | |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | #else |
| 122 | |
| 123 | void serial_setbrg (void) |
| 124 | { |
| 125 | int n, k; |
| 126 | const unsigned max_uns = 0xFFFFFFFF; |
| 127 | unsigned best_n, best_m, baud; |
| 128 | |
| 129 | /* compute best N and M couple */ |
| 130 | best_n = YANU_MAX_PRESCALER_N; |
| 131 | for (n = YANU_MAX_PRESCALER_N; n >= 0; n--) { |
| 132 | if ((unsigned)CONFIG_SYS_CLK_FREQ / (1 << (n + 4)) >= |
| 133 | gd->baudrate) { |
| 134 | best_n = n; |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | for (k = 0;; k++) { |
| 139 | if (gd->baudrate <= (max_uns >> (15+n-k))) |
| 140 | break; |
| 141 | } |
| 142 | best_m = |
| 143 | (gd->baudrate * (1 << (15 + n - k))) / |
| 144 | ((unsigned)CONFIG_SYS_CLK_FREQ >> k); |
| 145 | |
| 146 | baud = best_m + best_n * YANU_BAUDE; |
| 147 | writel(&uart->baud, baud); |
| 148 | |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | |
| 153 | #endif /* CONFIG_SYS_NIOS_FIXEDBAUD */ |
| 154 | |
| 155 | int serial_init (void) |
| 156 | { |
| 157 | unsigned action,control; |
| 158 | |
| 159 | /* status register cleanup */ |
| 160 | action = YANU_ACTION_RRRDY | |
| 161 | YANU_ACTION_RTRDY | |
| 162 | YANU_ACTION_ROE | |
| 163 | YANU_ACTION_RBRK | |
| 164 | YANU_ACTION_RFE | |
| 165 | YANU_ACTION_RPE | |
| 166 | YANU_ACTION_RFE | YANU_ACTION_RFIFO_CLEAR | YANU_ACTION_TFIFO_CLEAR; |
| 167 | |
| 168 | writel(&uart->action, action); |
| 169 | |
| 170 | /* control register cleanup */ |
| 171 | /* no interrupts enabled */ |
| 172 | /* one stop bit */ |
| 173 | /* hardware flow control disabled */ |
| 174 | /* 8 bits */ |
| 175 | control = (0x7 << YANU_CONTROL_BITS_POS); |
| 176 | /* enven parity just to be clean */ |
| 177 | control |= YANU_CONTROL_PAREVEN; |
| 178 | /* we set threshold for fifo */ |
| 179 | control |= YANU_CONTROL_RDYDLY * YANU_RXFIFO_DLY; |
| 180 | control |= YANU_CONTROL_TXTHR * YANU_TXFIFO_THR; |
| 181 | |
| 182 | writel(&uart->control, control); |
| 183 | |
| 184 | /* to set baud rate */ |
| 185 | serial_setbrg(); |
| 186 | |
| 187 | return (0); |
| 188 | } |
| 189 | |
| 190 | |
| 191 | /*----------------------------------------------------------------------- |
| 192 | * YANU CONSOLE |
| 193 | *---------------------------------------------------------------------*/ |
| 194 | void serial_putc (char c) |
| 195 | { |
| 196 | int tx_chars; |
| 197 | unsigned status; |
| 198 | |
| 199 | if (c == '\n') |
| 200 | serial_putc ('\r'); |
| 201 | |
| 202 | while (1) { |
| 203 | status = readl(&uart->status); |
| 204 | tx_chars = (status>>YANU_TFIFO_CHARS_POS) |
| 205 | & ((1<<YANU_TFIFO_CHARS_N)-1); |
| 206 | if (tx_chars < YANU_TXFIFO_SIZE-1) |
| 207 | break; |
| 208 | WATCHDOG_RESET (); |
| 209 | } |
| 210 | |
| 211 | writel(&uart->data, (unsigned char)c); |
| 212 | } |
| 213 | |
| 214 | void serial_puts (const char *s) |
| 215 | { |
| 216 | while (*s != 0) { |
| 217 | serial_putc (*s++); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | |
| 222 | int serial_tstc(void) |
| 223 | { |
| 224 | unsigned status ; |
| 225 | |
| 226 | status = readl(&uart->status); |
| 227 | return (((status >> YANU_RFIFO_CHARS_POS) & |
| 228 | ((1 << YANU_RFIFO_CHARS_N) - 1)) > 0); |
| 229 | } |
| 230 | |
| 231 | int serial_getc (void) |
| 232 | { |
| 233 | while (serial_tstc() == 0) |
| 234 | WATCHDOG_RESET (); |
| 235 | |
| 236 | /* first we pull the char */ |
| 237 | writel(&uart->action, YANU_ACTION_RFIFO_PULL); |
| 238 | |
| 239 | return(readl(&uart->data) & YANU_DATA_CHAR_MASK); |
| 240 | } |
| 241 | |
| 242 | #else /*CONFIG_CONSOLE_YANU*/ |
| 243 | |
wdenk | ef3386f | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 244 | /*------------------------------------------------------------------ |
| 245 | * UART the serial port |
| 246 | *-----------------------------------------------------------------*/ |
wdenk | ef3386f | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 247 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 248 | static nios_uart_t *uart = (nios_uart_t *) CONFIG_SYS_NIOS_CONSOLE; |
wdenk | ef3386f | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 249 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 250 | #if defined(CONFIG_SYS_NIOS_FIXEDBAUD) |
wdenk | ef3386f | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 251 | |
| 252 | /* Everything's already setup for fixed-baud PTF |
| 253 | * assignment |
| 254 | */ |
| 255 | void serial_setbrg (void){ return; } |
| 256 | int serial_init (void) { return (0);} |
| 257 | |
| 258 | #else |
| 259 | |
| 260 | void serial_setbrg (void) |
| 261 | { |
wdenk | ef3386f | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 262 | unsigned div; |
| 263 | |
| 264 | div = (CONFIG_SYS_CLK_FREQ/gd->baudrate)-1; |
Scott McNutt | a5721a3 | 2006-06-08 11:59:57 -0400 | [diff] [blame] | 265 | writel (&uart->divisor,div); |
wdenk | ef3386f | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 266 | return; |
| 267 | } |
| 268 | |
| 269 | int serial_init (void) |
| 270 | { |
| 271 | serial_setbrg (); |
| 272 | return (0); |
| 273 | } |
| 274 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 275 | #endif /* CONFIG_SYS_NIOS_FIXEDBAUD */ |
wdenk | ef3386f | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 276 | |
| 277 | |
| 278 | /*----------------------------------------------------------------------- |
| 279 | * UART CONSOLE |
| 280 | *---------------------------------------------------------------------*/ |
| 281 | void serial_putc (char c) |
| 282 | { |
| 283 | if (c == '\n') |
| 284 | serial_putc ('\r'); |
Scott McNutt | a5721a3 | 2006-06-08 11:59:57 -0400 | [diff] [blame] | 285 | while ((readl (&uart->status) & NIOS_UART_TRDY) == 0) |
wdenk | ef3386f | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 286 | WATCHDOG_RESET (); |
Scott McNutt | a5721a3 | 2006-06-08 11:59:57 -0400 | [diff] [blame] | 287 | writel (&uart->txdata,(unsigned char)c); |
wdenk | ef3386f | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | void serial_puts (const char *s) |
| 291 | { |
| 292 | while (*s != 0) { |
| 293 | serial_putc (*s++); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | int serial_tstc (void) |
| 298 | { |
Scott McNutt | a5721a3 | 2006-06-08 11:59:57 -0400 | [diff] [blame] | 299 | return (readl (&uart->status) & NIOS_UART_RRDY); |
wdenk | ef3386f | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | int serial_getc (void) |
| 303 | { |
| 304 | while (serial_tstc () == 0) |
| 305 | WATCHDOG_RESET (); |
Scott McNutt | a5721a3 | 2006-06-08 11:59:57 -0400 | [diff] [blame] | 306 | return (readl (&uart->rxdata) & 0x00ff ); |
wdenk | ef3386f | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | #endif /* CONFIG_JTAG_CONSOLE */ |