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 static void oc_serial_setbrg(void)
24 const unsigned max_uns = 0xFFFFFFFF;
25 unsigned best_n, best_m, baud;
28 #if defined(CONFIG_SYS_NIOS_FIXEDBAUD)
29 /* Everything's already setup for fixed-baud PTF assignment */
30 baudrate = CONFIG_BAUDRATE;
32 baudrate = gd->baudrate;
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)) >=
44 if (baudrate <= (max_uns >> (15+n-k)))
48 (baudrate * (1 << (15 + n - k))) /
49 ((unsigned)CONFIG_SYS_CLK_FREQ >> k);
51 baud = best_m + best_n * YANU_BAUDE;
52 writel(baud, &uart->baud);
57 static int oc_serial_init(void)
59 unsigned action,control;
61 /* status register cleanup */
62 action = YANU_ACTION_RRRDY |
68 YANU_ACTION_RFE | YANU_ACTION_RFIFO_CLEAR | YANU_ACTION_TFIFO_CLEAR;
70 writel(action, &uart->action);
73 * control register cleanup
74 * no interrupts enabled
76 * hardware flow control disabled
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;
86 writel(control, &uart->control);
88 /* to set baud rate */
95 /*-----------------------------------------------------------------------
97 *---------------------------------------------------------------------*/
98 static void oc_serial_putc(char c)
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)
115 writel((unsigned char)c, &uart->data);
118 static int oc_serial_tstc(void)
122 status = readl(&uart->status);
123 return (((status >> YANU_RFIFO_CHARS_POS) &
124 ((1 << YANU_RFIFO_CHARS_N) - 1)) > 0);
127 static int oc_serial_getc(void)
129 while (serial_tstc() == 0)
132 /* first we pull the char */
133 writel(YANU_ACTION_RFIFO_PULL, &uart->action);
135 return(readl(&uart->data) & YANU_DATA_CHAR_MASK);
138 static struct serial_device oc_serial_drv = {
140 .start = oc_serial_init,
142 .setbrg = oc_serial_setbrg,
143 .putc = oc_serial_putc,
144 .puts = default_serial_puts,
145 .getc = oc_serial_getc,
146 .tstc = oc_serial_tstc,
149 void oc_serial_initialize(void)
151 serial_register(&oc_serial_drv);
154 __weak struct serial_device *default_serial_console(void)
156 return &oc_serial_drv;