2 * Copyright 2010, Renato Andreola <renato.andreola@imagos.it>
4 * SPDX-License-Identifier: GPL-2.0+
10 #include <nios2-yanu.h>
12 DECLARE_GLOBAL_DATA_PTR;
14 /*-----------------------------------------------------------------*/
15 /* YANU Imagos serial port */
16 /*-----------------------------------------------------------------*/
18 static yanu_uart_t *uart = (yanu_uart_t *)CONFIG_SYS_NIOS_CONSOLE;
20 #if defined(CONFIG_SYS_NIOS_FIXEDBAUD)
22 /* Everything's already setup for fixed-baud PTF assignment*/
24 static void oc_serial_setbrg(void)
27 const unsigned max_uns = 0xFFFFFFFF;
28 unsigned best_n, best_m, baud;
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) {
40 if ((unsigned)CONFIG_BAUDRATE <= (max_uns >> (15+n-k)))
44 ((unsigned)CONFIG_BAUDRATE * (1 << (15 + n - k))) /
45 ((unsigned)CONFIG_SYS_CLK_FREQ >> k);
47 baud = best_m + best_n * YANU_BAUDE;
48 writel(baud, &uart->baud);
55 static void oc_serial_setbrg(void)
58 const unsigned max_uns = 0xFFFFFFFF;
59 unsigned best_n, best_m, baud;
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)) >=
71 if (gd->baudrate <= (max_uns >> (15+n-k)))
75 (gd->baudrate * (1 << (15 + n - k))) /
76 ((unsigned)CONFIG_SYS_CLK_FREQ >> k);
78 baud = best_m + best_n * YANU_BAUDE;
79 writel(baud, &uart->baud);
85 #endif /* CONFIG_SYS_NIOS_FIXEDBAUD */
87 static int oc_serial_init(void)
89 unsigned action,control;
91 /* status register cleanup */
92 action = YANU_ACTION_RRRDY |
98 YANU_ACTION_RFE | YANU_ACTION_RFIFO_CLEAR | YANU_ACTION_TFIFO_CLEAR;
100 writel(action, &uart->action);
103 * control register cleanup
104 * no interrupts enabled
106 * hardware flow control disabled
109 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;
116 writel(control, &uart->control);
118 /* to set baud rate */
125 /*-----------------------------------------------------------------------
127 *---------------------------------------------------------------------*/
128 static void oc_serial_putc(char c)
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)
145 writel((unsigned char)c, &uart->data);
148 static int oc_serial_tstc(void)
152 status = readl(&uart->status);
153 return (((status >> YANU_RFIFO_CHARS_POS) &
154 ((1 << YANU_RFIFO_CHARS_N) - 1)) > 0);
157 statoc int oc_serial_getc(void)
159 while (serial_tstc() == 0)
162 /* first we pull the char */
163 writel(YANU_ACTION_RFIFO_PULL, &uart->action);
165 return(readl(&uart->data) & YANU_DATA_CHAR_MASK);
168 static struct serial_device oc_serial_drv = {
170 .start = oc_serial_init,
172 .setbrg = oc_serial_setbrg,
173 .putc = oc_serial_putc,
174 .puts = default_serial_puts,
175 .getc = oc_serial_getc,
176 .tstc = oc_serial_tstc,
179 void oc_serial_initialize(void)
181 serial_register(&oc_serial_drv);
184 __weak struct serial_device *default_serial_console(void)
186 return &oc_serial_drv;