blob: 8de2eca2ac72c1ef32b6f2eb26c286fa2710fb47 [file] [log] [blame]
wdenkef3386f2004-10-10 21:27:30 +00001/*
Renato Andreola9f64dcb2010-03-16 16:01:29 -04002 * Copyright 2010, Renato Andreola <renato.andreola@imagos.it>
3 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
wdenkef3386f2004-10-10 21:27:30 +00005 */
6
wdenkef3386f2004-10-10 21:27:30 +00007#include <common.h>
8#include <watchdog.h>
Scott McNutta5721a32006-06-08 11:59:57 -04009#include <asm/io.h>
Renato Andreola9f64dcb2010-03-16 16:01:29 -040010#include <nios2-yanu.h>
wdenkef3386f2004-10-10 21:27:30 +000011
Wolfgang Denk6405a152006-03-31 18:32:53 +020012DECLARE_GLOBAL_DATA_PTR;
13
Renato Andreola9f64dcb2010-03-16 16:01:29 -040014/*-----------------------------------------------------------------*/
15/* YANU Imagos serial port */
16/*-----------------------------------------------------------------*/
17
18static yanu_uart_t *uart = (yanu_uart_t *)CONFIG_SYS_NIOS_CONSOLE;
19
20#if defined(CONFIG_SYS_NIOS_FIXEDBAUD)
21
22/* Everything's already setup for fixed-baud PTF assignment*/
23
Marek Vasutfcb9ef82012-09-13 16:52:38 +020024static void oc_serial_setbrg(void)
Renato Andreola9f64dcb2010-03-16 16:01:29 -040025{
26 int n, k;
27 const unsigned max_uns = 0xFFFFFFFF;
28 unsigned best_n, best_m, baud;
29
30 /* compute best N and M couple */
31 best_n = YANU_MAX_PRESCALER_N;
32 for (n = YANU_MAX_PRESCALER_N; n >= 0; n--) {
33 if ((unsigned)CONFIG_SYS_CLK_FREQ / (1 << (n + 4)) >=
34 (unsigned)CONFIG_BAUDRATE) {
35 best_n = n;
36 break;
37 }
38 }
39 for (k = 0;; k++) {
40 if ((unsigned)CONFIG_BAUDRATE <= (max_uns >> (15+n-k)))
41 break;
42 }
43 best_m =
44 ((unsigned)CONFIG_BAUDRATE * (1 << (15 + n - k))) /
45 ((unsigned)CONFIG_SYS_CLK_FREQ >> k);
46
47 baud = best_m + best_n * YANU_BAUDE;
Scott McNutt0fd72d32010-03-21 21:24:43 -040048 writel(baud, &uart->baud);
Renato Andreola9f64dcb2010-03-16 16:01:29 -040049
50 return;
51}
52
53#else
54
Marek Vasutfcb9ef82012-09-13 16:52:38 +020055static void oc_serial_setbrg(void)
Wolfgang Denkbe2034d2010-05-26 23:51:22 +020056{
Renato Andreola9f64dcb2010-03-16 16:01:29 -040057 int n, k;
58 const unsigned max_uns = 0xFFFFFFFF;
59 unsigned best_n, best_m, baud;
60
61 /* compute best N and M couple */
62 best_n = YANU_MAX_PRESCALER_N;
63 for (n = YANU_MAX_PRESCALER_N; n >= 0; n--) {
64 if ((unsigned)CONFIG_SYS_CLK_FREQ / (1 << (n + 4)) >=
65 gd->baudrate) {
66 best_n = n;
67 break;
68 }
69 }
70 for (k = 0;; k++) {
71 if (gd->baudrate <= (max_uns >> (15+n-k)))
72 break;
73 }
74 best_m =
75 (gd->baudrate * (1 << (15 + n - k))) /
76 ((unsigned)CONFIG_SYS_CLK_FREQ >> k);
77
78 baud = best_m + best_n * YANU_BAUDE;
Scott McNutt0fd72d32010-03-21 21:24:43 -040079 writel(baud, &uart->baud);
Renato Andreola9f64dcb2010-03-16 16:01:29 -040080
81 return;
82}
83
84
85#endif /* CONFIG_SYS_NIOS_FIXEDBAUD */
86
Marek Vasutfcb9ef82012-09-13 16:52:38 +020087static int oc_serial_init(void)
Renato Andreola9f64dcb2010-03-16 16:01:29 -040088{
89 unsigned action,control;
90
91 /* status register cleanup */
92 action = YANU_ACTION_RRRDY |
93 YANU_ACTION_RTRDY |
94 YANU_ACTION_ROE |
95 YANU_ACTION_RBRK |
96 YANU_ACTION_RFE |
97 YANU_ACTION_RPE |
98 YANU_ACTION_RFE | YANU_ACTION_RFIFO_CLEAR | YANU_ACTION_TFIFO_CLEAR;
99
Scott McNutt0fd72d32010-03-21 21:24:43 -0400100 writel(action, &uart->action);
Wolfgang Denkbe2034d2010-05-26 23:51:22 +0200101
102 /*
103 * control register cleanup
104 * no interrupts enabled
105 * one stop bit
106 * hardware flow control disabled
107 * 8 bits
108 */
Renato Andreola9f64dcb2010-03-16 16:01:29 -0400109 control = (0x7 << YANU_CONTROL_BITS_POS);
110 /* enven parity just to be clean */
111 control |= YANU_CONTROL_PAREVEN;
112 /* we set threshold for fifo */
113 control |= YANU_CONTROL_RDYDLY * YANU_RXFIFO_DLY;
114 control |= YANU_CONTROL_TXTHR * YANU_TXFIFO_THR;
115
Scott McNutt0fd72d32010-03-21 21:24:43 -0400116 writel(control, &uart->control);
Renato Andreola9f64dcb2010-03-16 16:01:29 -0400117
118 /* to set baud rate */
119 serial_setbrg();
120
121 return (0);
122}
123
124
125/*-----------------------------------------------------------------------
126 * YANU CONSOLE
127 *---------------------------------------------------------------------*/
Marek Vasutfcb9ef82012-09-13 16:52:38 +0200128static void oc_serial_putc(char c)
Renato Andreola9f64dcb2010-03-16 16:01:29 -0400129{
130 int tx_chars;
131 unsigned status;
132
133 if (c == '\n')
134 serial_putc ('\r');
Wolfgang Denkbe2034d2010-05-26 23:51:22 +0200135
Renato Andreola9f64dcb2010-03-16 16:01:29 -0400136 while (1) {
137 status = readl(&uart->status);
138 tx_chars = (status>>YANU_TFIFO_CHARS_POS)
139 & ((1<<YANU_TFIFO_CHARS_N)-1);
140 if (tx_chars < YANU_TXFIFO_SIZE-1)
141 break;
142 WATCHDOG_RESET ();
143 }
144
Scott McNutt0fd72d32010-03-21 21:24:43 -0400145 writel((unsigned char)c, &uart->data);
Renato Andreola9f64dcb2010-03-16 16:01:29 -0400146}
147
Marek Vasutfcb9ef82012-09-13 16:52:38 +0200148static int oc_serial_tstc(void)
Renato Andreola9f64dcb2010-03-16 16:01:29 -0400149{
150 unsigned status ;
151
152 status = readl(&uart->status);
153 return (((status >> YANU_RFIFO_CHARS_POS) &
154 ((1 << YANU_RFIFO_CHARS_N) - 1)) > 0);
Wolfgang Denkbe2034d2010-05-26 23:51:22 +0200155}
Renato Andreola9f64dcb2010-03-16 16:01:29 -0400156
Marek Vasutfcb9ef82012-09-13 16:52:38 +0200157statoc int oc_serial_getc(void)
Renato Andreola9f64dcb2010-03-16 16:01:29 -0400158{
159 while (serial_tstc() == 0)
160 WATCHDOG_RESET ();
Wolfgang Denkbe2034d2010-05-26 23:51:22 +0200161
Renato Andreola9f64dcb2010-03-16 16:01:29 -0400162 /* first we pull the char */
Scott McNutt0fd72d32010-03-21 21:24:43 -0400163 writel(YANU_ACTION_RFIFO_PULL, &uart->action);
Renato Andreola9f64dcb2010-03-16 16:01:29 -0400164
165 return(readl(&uart->data) & YANU_DATA_CHAR_MASK);
166}
Marek Vasutfcb9ef82012-09-13 16:52:38 +0200167
Marek Vasutfcb9ef82012-09-13 16:52:38 +0200168static struct serial_device oc_serial_drv = {
169 .name = "oc_serial",
170 .start = oc_serial_init,
171 .stop = NULL,
172 .setbrg = oc_serial_setbrg,
173 .putc = oc_serial_putc,
Marek Vasutd9c64492012-10-06 14:07:02 +0000174 .puts = default_serial_puts,
Marek Vasutfcb9ef82012-09-13 16:52:38 +0200175 .getc = oc_serial_getc,
176 .tstc = oc_serial_tstc,
177};
178
179void oc_serial_initialize(void)
180{
181 serial_register(&oc_serial_drv);
182}
183
184__weak struct serial_device *default_serial_console(void)
185{
186 return &oc_serial_drv;
187}