2 * Altera NiosII YANU serial interface by Imagos
3 * please see http://www.opencores.org/project,yanu for
4 * information/downloads
6 * Copyright 2010, Renato Andreola <renato.andreola@imagos.it>
8 * SPDX-License-Identifier: GPL-2.0+
16 DECLARE_GLOBAL_DATA_PTR;
18 /*-----------------------------------------------------------------*/
19 /* YANU Imagos serial port */
20 /*-----------------------------------------------------------------*/
22 #define YANU_MAX_PRESCALER_N ((1 << 4) - 1) /* 15 */
23 #define YANU_MAX_PRESCALER_M ((1 << 11) -1) /* 2047 */
24 #define YANU_FIFO_SIZE (16)
25 #define YANU_RXFIFO_SIZE (YANU_FIFO_SIZE)
26 #define YANU_TXFIFO_SIZE (YANU_FIFO_SIZE)
28 #define YANU_RXFIFO_DLY (10*11)
29 #define YANU_TXFIFO_THR (10)
30 #define YANU_DATA_CHAR_MASK (0xFF)
33 #define YANU_DATA_OFFSET (0) /* data register offset */
35 #define YANU_CONTROL_OFFSET (4) /* control register offset */
36 /* interrupt enable */
37 #define YANU_CONTROL_IE_RRDY (1<<0) /* ie on received character ready */
38 #define YANU_CONTROL_IE_OE (1<<1) /* ie on rx overrun */
39 #define YANU_CONTROL_IE_BRK (1<<2) /* ie on break detect */
40 #define YANU_CONTROL_IE_FE (1<<3) /* ie on framing error */
41 #define YANU_CONTROL_IE_PE (1<<4) /* ie on parity error */
42 #define YANU_CONTROL_IE_TRDY (1<<5) /* ie interrupt on tranmitter ready */
44 #define YANU_CONTROL_BITS_POS (6) /* bits number pos */
45 #define YANU_CONTROL_BITS (1<<YANU_CONTROL_BITS_POS) /* number of rx/tx bits per word. 3 bit unsigned integer */
46 #define YANU_CONTROL_BITS_N (3) /* ... its bit filed length */
47 #define YANU_CONTROL_PARENA (1<<9) /* enable parity bit transmission/reception */
48 #define YANU_CONTROL_PAREVEN (1<<10) /* parity even */
49 #define YANU_CONTROL_STOPS (1<<11) /* number of stop bits */
50 #define YANU_CONTROL_HHENA (1<<12) /* Harware Handshake enable... */
51 #define YANU_CONTROL_FORCEBRK (1<<13) /* if set than txd = active (0) */
53 #define YANU_CONTROL_RDYDLY (1<<14) /* delay from "first" before setting rrdy (in bit) */
54 #define YANU_CONTROL_RDYDLY_N (8) /* ... its bit filed length */
55 #define YANU_CONTROL_TXTHR (1<<22) /* tx interrupt threshold: the trdy set if txfifo_chars<= txthr (chars) */
56 #define YANU_CONTROL_TXTHR_N (4) /* ... its bit field length */
58 #define YANU_BAUD_OFFSET (8) /* baud register offset */
59 #define YANU_BAUDM (1<<0) /* baud mantissa lsb */
60 #define YANU_BAUDM_N (12) /* ...its bit filed length */
61 #define YANU_BAUDE (1<<12) /* baud exponent lsb */
62 #define YANU_BAUDE_N (4) /* ...its bit field length */
64 #define YANU_ACTION_OFFSET (12) /* action register... write only */
65 #define YANU_ACTION_RRRDY (1<<0) /* reset rrdy */
66 #define YANU_ACTION_ROE (1<<1) /* reset oe */
67 #define YANU_ACTION_RBRK (1<<2) /* reset brk */
68 #define YANU_ACTION_RFE (1<<3) /* reset fe */
69 #define YANU_ACTION_RPE (1<<4) /* reset pe */
70 #define YANU_ACTION_SRRDY (1<<5) /* set rrdy */
71 #define YANU_ACTION_SOE (1<<6) /* set oe */
72 #define YANU_ACTION_SBRK (1<<7) /* set brk */
73 #define YANU_ACTION_SFE (1<<8) /* set fe */
74 #define YANU_ACTION_SPE (1<<9) /* set pe */
75 #define YANU_ACTION_RFIFO_PULL (1<<10) /* pull a char from rx fifo we MUST do it before taking a char */
76 #define YANU_ACTION_RFIFO_CLEAR (1<<11) /* clear rx fifo */
77 #define YANU_ACTION_TFIFO_CLEAR (1<<12) /* clear tx fifo */
78 #define YANU_ACTION_RTRDY (1<<13) /* clear trdy */
79 #define YANU_ACTION_STRDY (1<<14) /* set trdy */
81 #define YANU_STATUS_OFFSET (16)
82 #define YANU_STATUS_RRDY (1<<0) /* rxrdy flag */
83 #define YANU_STATUS_TRDY (1<<1) /* txrdy flag */
84 #define YANU_STATUS_OE (1<<2) /* rx overrun error */
85 #define YANU_STATUS_BRK (1<<3) /* rx break detect flag */
86 #define YANU_STATUS_FE (1<<4) /* rx framing error flag */
87 #define YANU_STATUS_PE (1<<5) /* rx parity erro flag */
88 #define YANU_RFIFO_CHARS_POS (6)
89 #define YANU_RFIFO_CHARS (1<<RFIFO_CHAR_POS) /* number of chars into rx fifo */
90 #define YANU_RFIFO_CHARS_N (5) /* ...its bit field length: 32 chars */
91 #define YANU_TFIFO_CHARS_POS (11)
92 #define YANU_TFIFO_CHARS (1<<TFIFO_CHAR_POS) /* number of chars into tx fifo */
93 #define YANU_TFIFO_CHARS_N (5) /* ...its bit field length: 32 chars */
95 typedef volatile struct {
96 volatile unsigned data;
97 volatile unsigned control; /* control register (RW) 32-bit */
98 volatile unsigned baud; /* baud/prescaler register (RW) 32-bit */
99 volatile unsigned action; /* action register (W) 32-bit */
100 volatile unsigned status; /* status register (R) 32-bit */
101 volatile unsigned magic; /* magic register (R) 32-bit */
104 static yanu_uart_t *uart = (yanu_uart_t *)CONFIG_SYS_NIOS_CONSOLE;
106 static void oc_serial_setbrg(void)
109 const unsigned max_uns = 0xFFFFFFFF;
110 unsigned best_n, best_m, baud;
113 #if defined(CONFIG_SYS_NIOS_FIXEDBAUD)
114 /* Everything's already setup for fixed-baud PTF assignment */
115 baudrate = CONFIG_BAUDRATE;
117 baudrate = gd->baudrate;
119 /* compute best N and M couple */
120 best_n = YANU_MAX_PRESCALER_N;
121 for (n = YANU_MAX_PRESCALER_N; n >= 0; n--) {
122 if ((unsigned)CONFIG_SYS_CLK_FREQ / (1 << (n + 4)) >=
129 if (baudrate <= (max_uns >> (15+n-k)))
133 (baudrate * (1 << (15 + n - k))) /
134 ((unsigned)CONFIG_SYS_CLK_FREQ >> k);
136 baud = best_m + best_n * YANU_BAUDE;
137 writel(baud, &uart->baud);
142 static int oc_serial_init(void)
144 unsigned action,control;
146 /* status register cleanup */
147 action = YANU_ACTION_RRRDY |
153 YANU_ACTION_RFE | YANU_ACTION_RFIFO_CLEAR | YANU_ACTION_TFIFO_CLEAR;
155 writel(action, &uart->action);
158 * control register cleanup
159 * no interrupts enabled
161 * hardware flow control disabled
164 control = (0x7 << YANU_CONTROL_BITS_POS);
165 /* enven parity just to be clean */
166 control |= YANU_CONTROL_PAREVEN;
167 /* we set threshold for fifo */
168 control |= YANU_CONTROL_RDYDLY * YANU_RXFIFO_DLY;
169 control |= YANU_CONTROL_TXTHR * YANU_TXFIFO_THR;
171 writel(control, &uart->control);
173 /* to set baud rate */
180 /*-----------------------------------------------------------------------
182 *---------------------------------------------------------------------*/
183 static void oc_serial_putc(char c)
192 status = readl(&uart->status);
193 tx_chars = (status>>YANU_TFIFO_CHARS_POS)
194 & ((1<<YANU_TFIFO_CHARS_N)-1);
195 if (tx_chars < YANU_TXFIFO_SIZE-1)
200 writel((unsigned char)c, &uart->data);
203 static int oc_serial_tstc(void)
207 status = readl(&uart->status);
208 return (((status >> YANU_RFIFO_CHARS_POS) &
209 ((1 << YANU_RFIFO_CHARS_N) - 1)) > 0);
212 static int oc_serial_getc(void)
214 while (serial_tstc() == 0)
217 /* first we pull the char */
218 writel(YANU_ACTION_RFIFO_PULL, &uart->action);
220 return(readl(&uart->data) & YANU_DATA_CHAR_MASK);
223 static struct serial_device oc_serial_drv = {
225 .start = oc_serial_init,
227 .setbrg = oc_serial_setbrg,
228 .putc = oc_serial_putc,
229 .puts = default_serial_puts,
230 .getc = oc_serial_getc,
231 .tstc = oc_serial_tstc,
234 void oc_serial_initialize(void)
236 serial_register(&oc_serial_drv);
239 __weak struct serial_device *default_serial_console(void)
241 return &oc_serial_drv;