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