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 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | ef3386f | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
wdenk | ef3386f | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 7 | #include <common.h> |
| 8 | #include <watchdog.h> |
Scott McNutt | a5721a3 | 2006-06-08 11:59:57 -0400 | [diff] [blame] | 9 | #include <asm/io.h> |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame] | 10 | #include <nios2-yanu.h> |
Axel Lin | 0bdf000 | 2014-01-16 15:01:49 +0800 | [diff] [blame^] | 11 | #include <serial.h> |
wdenk | ef3386f | 2004-10-10 21:27:30 +0000 | [diff] [blame] | 12 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 13 | DECLARE_GLOBAL_DATA_PTR; |
| 14 | |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame] | 15 | /*-----------------------------------------------------------------*/ |
| 16 | /* YANU Imagos serial port */ |
| 17 | /*-----------------------------------------------------------------*/ |
| 18 | |
| 19 | static yanu_uart_t *uart = (yanu_uart_t *)CONFIG_SYS_NIOS_CONSOLE; |
| 20 | |
| 21 | #if defined(CONFIG_SYS_NIOS_FIXEDBAUD) |
| 22 | |
| 23 | /* Everything's already setup for fixed-baud PTF assignment*/ |
| 24 | |
Marek Vasut | fcb9ef8 | 2012-09-13 16:52:38 +0200 | [diff] [blame] | 25 | static void oc_serial_setbrg(void) |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame] | 26 | { |
| 27 | int n, k; |
| 28 | const unsigned max_uns = 0xFFFFFFFF; |
| 29 | unsigned best_n, best_m, baud; |
| 30 | |
| 31 | /* compute best N and M couple */ |
| 32 | best_n = YANU_MAX_PRESCALER_N; |
| 33 | for (n = YANU_MAX_PRESCALER_N; n >= 0; n--) { |
| 34 | if ((unsigned)CONFIG_SYS_CLK_FREQ / (1 << (n + 4)) >= |
| 35 | (unsigned)CONFIG_BAUDRATE) { |
| 36 | best_n = n; |
| 37 | break; |
| 38 | } |
| 39 | } |
| 40 | for (k = 0;; k++) { |
| 41 | if ((unsigned)CONFIG_BAUDRATE <= (max_uns >> (15+n-k))) |
| 42 | break; |
| 43 | } |
| 44 | best_m = |
| 45 | ((unsigned)CONFIG_BAUDRATE * (1 << (15 + n - k))) / |
| 46 | ((unsigned)CONFIG_SYS_CLK_FREQ >> k); |
| 47 | |
| 48 | baud = best_m + best_n * YANU_BAUDE; |
Scott McNutt | 0fd72d3 | 2010-03-21 21:24:43 -0400 | [diff] [blame] | 49 | writel(baud, &uart->baud); |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame] | 50 | |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | #else |
| 55 | |
Marek Vasut | fcb9ef8 | 2012-09-13 16:52:38 +0200 | [diff] [blame] | 56 | static void oc_serial_setbrg(void) |
Wolfgang Denk | be2034d | 2010-05-26 23:51:22 +0200 | [diff] [blame] | 57 | { |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame] | 58 | int n, k; |
| 59 | const unsigned max_uns = 0xFFFFFFFF; |
| 60 | unsigned best_n, best_m, baud; |
| 61 | |
| 62 | /* compute best N and M couple */ |
| 63 | best_n = YANU_MAX_PRESCALER_N; |
| 64 | for (n = YANU_MAX_PRESCALER_N; n >= 0; n--) { |
| 65 | if ((unsigned)CONFIG_SYS_CLK_FREQ / (1 << (n + 4)) >= |
| 66 | gd->baudrate) { |
| 67 | best_n = n; |
| 68 | break; |
| 69 | } |
| 70 | } |
| 71 | for (k = 0;; k++) { |
| 72 | if (gd->baudrate <= (max_uns >> (15+n-k))) |
| 73 | break; |
| 74 | } |
| 75 | best_m = |
| 76 | (gd->baudrate * (1 << (15 + n - k))) / |
| 77 | ((unsigned)CONFIG_SYS_CLK_FREQ >> k); |
| 78 | |
| 79 | baud = best_m + best_n * YANU_BAUDE; |
Scott McNutt | 0fd72d3 | 2010-03-21 21:24:43 -0400 | [diff] [blame] | 80 | writel(baud, &uart->baud); |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame] | 81 | |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | #endif /* CONFIG_SYS_NIOS_FIXEDBAUD */ |
| 87 | |
Marek Vasut | fcb9ef8 | 2012-09-13 16:52:38 +0200 | [diff] [blame] | 88 | static int oc_serial_init(void) |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame] | 89 | { |
| 90 | unsigned action,control; |
| 91 | |
| 92 | /* status register cleanup */ |
| 93 | action = YANU_ACTION_RRRDY | |
| 94 | YANU_ACTION_RTRDY | |
| 95 | YANU_ACTION_ROE | |
| 96 | YANU_ACTION_RBRK | |
| 97 | YANU_ACTION_RFE | |
| 98 | YANU_ACTION_RPE | |
| 99 | YANU_ACTION_RFE | YANU_ACTION_RFIFO_CLEAR | YANU_ACTION_TFIFO_CLEAR; |
| 100 | |
Scott McNutt | 0fd72d3 | 2010-03-21 21:24:43 -0400 | [diff] [blame] | 101 | writel(action, &uart->action); |
Wolfgang Denk | be2034d | 2010-05-26 23:51:22 +0200 | [diff] [blame] | 102 | |
| 103 | /* |
| 104 | * control register cleanup |
| 105 | * no interrupts enabled |
| 106 | * one stop bit |
| 107 | * hardware flow control disabled |
| 108 | * 8 bits |
| 109 | */ |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame] | 110 | control = (0x7 << YANU_CONTROL_BITS_POS); |
| 111 | /* enven parity just to be clean */ |
| 112 | control |= YANU_CONTROL_PAREVEN; |
| 113 | /* we set threshold for fifo */ |
| 114 | control |= YANU_CONTROL_RDYDLY * YANU_RXFIFO_DLY; |
| 115 | control |= YANU_CONTROL_TXTHR * YANU_TXFIFO_THR; |
| 116 | |
Scott McNutt | 0fd72d3 | 2010-03-21 21:24:43 -0400 | [diff] [blame] | 117 | writel(control, &uart->control); |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame] | 118 | |
| 119 | /* to set baud rate */ |
| 120 | serial_setbrg(); |
| 121 | |
| 122 | return (0); |
| 123 | } |
| 124 | |
| 125 | |
| 126 | /*----------------------------------------------------------------------- |
| 127 | * YANU CONSOLE |
| 128 | *---------------------------------------------------------------------*/ |
Marek Vasut | fcb9ef8 | 2012-09-13 16:52:38 +0200 | [diff] [blame] | 129 | static void oc_serial_putc(char c) |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame] | 130 | { |
| 131 | int tx_chars; |
| 132 | unsigned status; |
| 133 | |
| 134 | if (c == '\n') |
| 135 | serial_putc ('\r'); |
Wolfgang Denk | be2034d | 2010-05-26 23:51:22 +0200 | [diff] [blame] | 136 | |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame] | 137 | while (1) { |
| 138 | status = readl(&uart->status); |
| 139 | tx_chars = (status>>YANU_TFIFO_CHARS_POS) |
| 140 | & ((1<<YANU_TFIFO_CHARS_N)-1); |
| 141 | if (tx_chars < YANU_TXFIFO_SIZE-1) |
| 142 | break; |
| 143 | WATCHDOG_RESET (); |
| 144 | } |
| 145 | |
Scott McNutt | 0fd72d3 | 2010-03-21 21:24:43 -0400 | [diff] [blame] | 146 | writel((unsigned char)c, &uart->data); |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame] | 147 | } |
| 148 | |
Marek Vasut | fcb9ef8 | 2012-09-13 16:52:38 +0200 | [diff] [blame] | 149 | static int oc_serial_tstc(void) |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame] | 150 | { |
| 151 | unsigned status ; |
| 152 | |
| 153 | status = readl(&uart->status); |
| 154 | return (((status >> YANU_RFIFO_CHARS_POS) & |
| 155 | ((1 << YANU_RFIFO_CHARS_N) - 1)) > 0); |
Wolfgang Denk | be2034d | 2010-05-26 23:51:22 +0200 | [diff] [blame] | 156 | } |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame] | 157 | |
Axel Lin | 0bdf000 | 2014-01-16 15:01:49 +0800 | [diff] [blame^] | 158 | static int oc_serial_getc(void) |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame] | 159 | { |
| 160 | while (serial_tstc() == 0) |
| 161 | WATCHDOG_RESET (); |
Wolfgang Denk | be2034d | 2010-05-26 23:51:22 +0200 | [diff] [blame] | 162 | |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame] | 163 | /* first we pull the char */ |
Scott McNutt | 0fd72d3 | 2010-03-21 21:24:43 -0400 | [diff] [blame] | 164 | writel(YANU_ACTION_RFIFO_PULL, &uart->action); |
Renato Andreola | 9f64dcb | 2010-03-16 16:01:29 -0400 | [diff] [blame] | 165 | |
| 166 | return(readl(&uart->data) & YANU_DATA_CHAR_MASK); |
| 167 | } |
Marek Vasut | fcb9ef8 | 2012-09-13 16:52:38 +0200 | [diff] [blame] | 168 | |
Marek Vasut | fcb9ef8 | 2012-09-13 16:52:38 +0200 | [diff] [blame] | 169 | static struct serial_device oc_serial_drv = { |
| 170 | .name = "oc_serial", |
| 171 | .start = oc_serial_init, |
| 172 | .stop = NULL, |
| 173 | .setbrg = oc_serial_setbrg, |
| 174 | .putc = oc_serial_putc, |
Marek Vasut | d9c6449 | 2012-10-06 14:07:02 +0000 | [diff] [blame] | 175 | .puts = default_serial_puts, |
Marek Vasut | fcb9ef8 | 2012-09-13 16:52:38 +0200 | [diff] [blame] | 176 | .getc = oc_serial_getc, |
| 177 | .tstc = oc_serial_tstc, |
| 178 | }; |
| 179 | |
| 180 | void oc_serial_initialize(void) |
| 181 | { |
| 182 | serial_register(&oc_serial_drv); |
| 183 | } |
| 184 | |
| 185 | __weak struct serial_device *default_serial_console(void) |
| 186 | { |
| 187 | return &oc_serial_drv; |
| 188 | } |