wdenk | ef3386f | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 1 | /* |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame] | 2 | * Copyright 2010, Renato Andreola <renato.andreola@imagos.it> |
| 3 | * |
wdenk | ef3386f | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 4 | * See file CREDITS for list of people who contributed to this |
| 5 | * project. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License as |
| 9 | * published by the Free Software Foundation; either version 2 of |
| 10 | * the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 20 | * MA 02111-1307 USA |
| 21 | */ |
| 22 | |
wdenk | ef3386f | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 23 | #include <common.h> |
| 24 | #include <watchdog.h> |
Scott McNutt | a5721a3 | 2006-06-08 11:59:57 -0400 | [diff] [blame] | 25 | #include <asm/io.h> |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame] | 26 | #include <nios2-yanu.h> |
wdenk | ef3386f | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 27 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 28 | DECLARE_GLOBAL_DATA_PTR; |
| 29 | |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame] | 30 | /*-----------------------------------------------------------------*/ |
| 31 | /* YANU Imagos serial port */ |
| 32 | /*-----------------------------------------------------------------*/ |
| 33 | |
| 34 | static yanu_uart_t *uart = (yanu_uart_t *)CONFIG_SYS_NIOS_CONSOLE; |
| 35 | |
| 36 | #if defined(CONFIG_SYS_NIOS_FIXEDBAUD) |
| 37 | |
| 38 | /* Everything's already setup for fixed-baud PTF assignment*/ |
| 39 | |
Marek Vasut | fcb9ef8 | 2012-09-13 16:52:38 +0200 | [diff] [blame] | 40 | static void oc_serial_setbrg(void) |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame] | 41 | { |
| 42 | int n, k; |
| 43 | const unsigned max_uns = 0xFFFFFFFF; |
| 44 | unsigned best_n, best_m, baud; |
| 45 | |
| 46 | /* compute best N and M couple */ |
| 47 | best_n = YANU_MAX_PRESCALER_N; |
| 48 | for (n = YANU_MAX_PRESCALER_N; n >= 0; n--) { |
| 49 | if ((unsigned)CONFIG_SYS_CLK_FREQ / (1 << (n + 4)) >= |
| 50 | (unsigned)CONFIG_BAUDRATE) { |
| 51 | best_n = n; |
| 52 | break; |
| 53 | } |
| 54 | } |
| 55 | for (k = 0;; k++) { |
| 56 | if ((unsigned)CONFIG_BAUDRATE <= (max_uns >> (15+n-k))) |
| 57 | break; |
| 58 | } |
| 59 | best_m = |
| 60 | ((unsigned)CONFIG_BAUDRATE * (1 << (15 + n - k))) / |
| 61 | ((unsigned)CONFIG_SYS_CLK_FREQ >> k); |
| 62 | |
| 63 | baud = best_m + best_n * YANU_BAUDE; |
Scott McNutt | 0fd72d3 | 2010-03-21 21:24:43 -0400 | [diff] [blame] | 64 | writel(baud, &uart->baud); |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame] | 65 | |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | #else |
| 70 | |
Marek Vasut | fcb9ef8 | 2012-09-13 16:52:38 +0200 | [diff] [blame] | 71 | static void oc_serial_setbrg(void) |
Wolfgang Denk | be2034d | 2010-05-26 23:51:22 +0200 | [diff] [blame] | 72 | { |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame] | 73 | int n, k; |
| 74 | const unsigned max_uns = 0xFFFFFFFF; |
| 75 | unsigned best_n, best_m, baud; |
| 76 | |
| 77 | /* compute best N and M couple */ |
| 78 | best_n = YANU_MAX_PRESCALER_N; |
| 79 | for (n = YANU_MAX_PRESCALER_N; n >= 0; n--) { |
| 80 | if ((unsigned)CONFIG_SYS_CLK_FREQ / (1 << (n + 4)) >= |
| 81 | gd->baudrate) { |
| 82 | best_n = n; |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | for (k = 0;; k++) { |
| 87 | if (gd->baudrate <= (max_uns >> (15+n-k))) |
| 88 | break; |
| 89 | } |
| 90 | best_m = |
| 91 | (gd->baudrate * (1 << (15 + n - k))) / |
| 92 | ((unsigned)CONFIG_SYS_CLK_FREQ >> k); |
| 93 | |
| 94 | baud = best_m + best_n * YANU_BAUDE; |
Scott McNutt | 0fd72d3 | 2010-03-21 21:24:43 -0400 | [diff] [blame] | 95 | writel(baud, &uart->baud); |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame] | 96 | |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | |
| 101 | #endif /* CONFIG_SYS_NIOS_FIXEDBAUD */ |
| 102 | |
Marek Vasut | fcb9ef8 | 2012-09-13 16:52:38 +0200 | [diff] [blame] | 103 | static int oc_serial_init(void) |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame] | 104 | { |
| 105 | unsigned action,control; |
| 106 | |
| 107 | /* status register cleanup */ |
| 108 | action = YANU_ACTION_RRRDY | |
| 109 | YANU_ACTION_RTRDY | |
| 110 | YANU_ACTION_ROE | |
| 111 | YANU_ACTION_RBRK | |
| 112 | YANU_ACTION_RFE | |
| 113 | YANU_ACTION_RPE | |
| 114 | YANU_ACTION_RFE | YANU_ACTION_RFIFO_CLEAR | YANU_ACTION_TFIFO_CLEAR; |
| 115 | |
Scott McNutt | 0fd72d3 | 2010-03-21 21:24:43 -0400 | [diff] [blame] | 116 | writel(action, &uart->action); |
Wolfgang Denk | be2034d | 2010-05-26 23:51:22 +0200 | [diff] [blame] | 117 | |
| 118 | /* |
| 119 | * control register cleanup |
| 120 | * no interrupts enabled |
| 121 | * one stop bit |
| 122 | * hardware flow control disabled |
| 123 | * 8 bits |
| 124 | */ |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame] | 125 | control = (0x7 << YANU_CONTROL_BITS_POS); |
| 126 | /* enven parity just to be clean */ |
| 127 | control |= YANU_CONTROL_PAREVEN; |
| 128 | /* we set threshold for fifo */ |
| 129 | control |= YANU_CONTROL_RDYDLY * YANU_RXFIFO_DLY; |
| 130 | control |= YANU_CONTROL_TXTHR * YANU_TXFIFO_THR; |
| 131 | |
Scott McNutt | 0fd72d3 | 2010-03-21 21:24:43 -0400 | [diff] [blame] | 132 | writel(control, &uart->control); |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame] | 133 | |
| 134 | /* to set baud rate */ |
| 135 | serial_setbrg(); |
| 136 | |
| 137 | return (0); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | /*----------------------------------------------------------------------- |
| 142 | * YANU CONSOLE |
| 143 | *---------------------------------------------------------------------*/ |
Marek Vasut | fcb9ef8 | 2012-09-13 16:52:38 +0200 | [diff] [blame] | 144 | static void oc_serial_putc(char c) |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame] | 145 | { |
| 146 | int tx_chars; |
| 147 | unsigned status; |
| 148 | |
| 149 | if (c == '\n') |
| 150 | serial_putc ('\r'); |
Wolfgang Denk | be2034d | 2010-05-26 23:51:22 +0200 | [diff] [blame] | 151 | |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame] | 152 | while (1) { |
| 153 | status = readl(&uart->status); |
| 154 | tx_chars = (status>>YANU_TFIFO_CHARS_POS) |
| 155 | & ((1<<YANU_TFIFO_CHARS_N)-1); |
| 156 | if (tx_chars < YANU_TXFIFO_SIZE-1) |
| 157 | break; |
| 158 | WATCHDOG_RESET (); |
| 159 | } |
| 160 | |
Scott McNutt | 0fd72d3 | 2010-03-21 21:24:43 -0400 | [diff] [blame] | 161 | writel((unsigned char)c, &uart->data); |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame] | 162 | } |
| 163 | |
Marek Vasut | fcb9ef8 | 2012-09-13 16:52:38 +0200 | [diff] [blame] | 164 | static int oc_serial_tstc(void) |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame] | 165 | { |
| 166 | unsigned status ; |
| 167 | |
| 168 | status = readl(&uart->status); |
| 169 | return (((status >> YANU_RFIFO_CHARS_POS) & |
| 170 | ((1 << YANU_RFIFO_CHARS_N) - 1)) > 0); |
Wolfgang Denk | be2034d | 2010-05-26 23:51:22 +0200 | [diff] [blame] | 171 | } |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame] | 172 | |
Marek Vasut | fcb9ef8 | 2012-09-13 16:52:38 +0200 | [diff] [blame] | 173 | statoc int oc_serial_getc(void) |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame] | 174 | { |
| 175 | while (serial_tstc() == 0) |
| 176 | WATCHDOG_RESET (); |
Wolfgang Denk | be2034d | 2010-05-26 23:51:22 +0200 | [diff] [blame] | 177 | |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame] | 178 | /* first we pull the char */ |
Scott McNutt | 0fd72d3 | 2010-03-21 21:24:43 -0400 | [diff] [blame] | 179 | writel(YANU_ACTION_RFIFO_PULL, &uart->action); |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame] | 180 | |
| 181 | return(readl(&uart->data) & YANU_DATA_CHAR_MASK); |
| 182 | } |
Marek Vasut | fcb9ef8 | 2012-09-13 16:52:38 +0200 | [diff] [blame] | 183 | |
Marek Vasut | fcb9ef8 | 2012-09-13 16:52:38 +0200 | [diff] [blame] | 184 | static struct serial_device oc_serial_drv = { |
| 185 | .name = "oc_serial", |
| 186 | .start = oc_serial_init, |
| 187 | .stop = NULL, |
| 188 | .setbrg = oc_serial_setbrg, |
| 189 | .putc = oc_serial_putc, |
Marek Vasut | d9c6449 | 2012-10-06 14:07:02 +0000 | [diff] [blame] | 190 | .puts = default_serial_puts, |
Marek Vasut | fcb9ef8 | 2012-09-13 16:52:38 +0200 | [diff] [blame] | 191 | .getc = oc_serial_getc, |
| 192 | .tstc = oc_serial_tstc, |
| 193 | }; |
| 194 | |
| 195 | void oc_serial_initialize(void) |
| 196 | { |
| 197 | serial_register(&oc_serial_drv); |
| 198 | } |
| 199 | |
| 200 | __weak struct serial_device *default_serial_console(void) |
| 201 | { |
| 202 | return &oc_serial_drv; |
| 203 | } |