2 * Copyright 2010, Renato Andreola <renato.andreola@imagos.it>
4 * SPDX-License-Identifier: GPL-2.0+
10 #include <nios2-yanu.h>
13 DECLARE_GLOBAL_DATA_PTR;
15 /*-----------------------------------------------------------------*/
16 /* YANU Imagos serial port */
17 /*-----------------------------------------------------------------*/
19 static yanu_uart_t *uart = (yanu_uart_t *)CONFIG_SYS_NIOS_CONSOLE;
21 #if defined(CONFIG_SYS_NIOS_FIXEDBAUD)
23 /* Everything's already setup for fixed-baud PTF assignment*/
25 static void oc_serial_setbrg(void)
28 const unsigned max_uns = 0xFFFFFFFF;
29 unsigned best_n, best_m, baud;
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) {
41 if ((unsigned)CONFIG_BAUDRATE <= (max_uns >> (15+n-k)))
45 ((unsigned)CONFIG_BAUDRATE * (1 << (15 + n - k))) /
46 ((unsigned)CONFIG_SYS_CLK_FREQ >> k);
48 baud = best_m + best_n * YANU_BAUDE;
49 writel(baud, &uart->baud);
56 static void oc_serial_setbrg(void)
59 const unsigned max_uns = 0xFFFFFFFF;
60 unsigned best_n, best_m, baud;
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)) >=
72 if (gd->baudrate <= (max_uns >> (15+n-k)))
76 (gd->baudrate * (1 << (15 + n - k))) /
77 ((unsigned)CONFIG_SYS_CLK_FREQ >> k);
79 baud = best_m + best_n * YANU_BAUDE;
80 writel(baud, &uart->baud);
86 #endif /* CONFIG_SYS_NIOS_FIXEDBAUD */
88 static int oc_serial_init(void)
90 unsigned action,control;
92 /* status register cleanup */
93 action = YANU_ACTION_RRRDY |
99 YANU_ACTION_RFE | YANU_ACTION_RFIFO_CLEAR | YANU_ACTION_TFIFO_CLEAR;
101 writel(action, &uart->action);
104 * control register cleanup
105 * no interrupts enabled
107 * hardware flow control disabled
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;
117 writel(control, &uart->control);
119 /* to set baud rate */
126 /*-----------------------------------------------------------------------
128 *---------------------------------------------------------------------*/
129 static void oc_serial_putc(char c)
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)
146 writel((unsigned char)c, &uart->data);
149 static int oc_serial_tstc(void)
153 status = readl(&uart->status);
154 return (((status >> YANU_RFIFO_CHARS_POS) &
155 ((1 << YANU_RFIFO_CHARS_N) - 1)) > 0);
158 static int oc_serial_getc(void)
160 while (serial_tstc() == 0)
163 /* first we pull the char */
164 writel(YANU_ACTION_RFIFO_PULL, &uart->action);
166 return(readl(&uart->data) & YANU_DATA_CHAR_MASK);
169 static struct serial_device oc_serial_drv = {
171 .start = oc_serial_init,
173 .setbrg = oc_serial_setbrg,
174 .putc = oc_serial_putc,
175 .puts = default_serial_puts,
176 .getc = oc_serial_getc,
177 .tstc = oc_serial_tstc,
180 void oc_serial_initialize(void)
182 serial_register(&oc_serial_drv);
185 __weak struct serial_device *default_serial_console(void)
187 return &oc_serial_drv;