]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/serial/opencores_yanu.c
f18f7f444e119b5692759ce06c9e7ed1b38f581f
[karo-tx-uboot.git] / drivers / serial / opencores_yanu.c
1 /*
2  * Copyright 2010, Renato Andreola <renato.andreola@imagos.it>
3  *
4  * See file CREDITS for list of people who contributed to this
5  * project.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of
10  * the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20  * MA 02111-1307 USA
21  */
22
23 #include <common.h>
24 #include <watchdog.h>
25 #include <asm/io.h>
26 #include <nios2-yanu.h>
27
28 DECLARE_GLOBAL_DATA_PTR;
29
30 /*-----------------------------------------------------------------*/
31 /* YANU Imagos serial port */
32 /*-----------------------------------------------------------------*/
33
34 static yanu_uart_t *uart = (yanu_uart_t *)CONFIG_SYS_NIOS_CONSOLE;
35
36 #if defined(CONFIG_SYS_NIOS_FIXEDBAUD)
37
38 /* Everything's already setup for fixed-baud PTF assignment*/
39
40 void serial_setbrg (void)
41 {
42         int n, k;
43         const unsigned max_uns = 0xFFFFFFFF;
44         unsigned best_n, best_m, baud;
45
46         /* compute best N and M couple */
47         best_n = YANU_MAX_PRESCALER_N;
48         for (n = YANU_MAX_PRESCALER_N; n >= 0; n--) {
49                 if ((unsigned)CONFIG_SYS_CLK_FREQ / (1 << (n + 4)) >=
50                     (unsigned)CONFIG_BAUDRATE) {
51                         best_n = n;
52                         break;
53                 }
54         }
55         for (k = 0;; k++) {
56                 if ((unsigned)CONFIG_BAUDRATE <= (max_uns >> (15+n-k)))
57                         break;
58         }
59         best_m =
60             ((unsigned)CONFIG_BAUDRATE * (1 << (15 + n - k))) /
61             ((unsigned)CONFIG_SYS_CLK_FREQ >> k);
62
63         baud = best_m + best_n * YANU_BAUDE;
64         writel(baud, &uart->baud);
65
66         return;
67 }
68
69 #else
70
71 void serial_setbrg (void)
72 {       
73         int n, k;
74         const unsigned max_uns = 0xFFFFFFFF;
75         unsigned best_n, best_m, baud;
76
77         /* compute best N and M couple */
78         best_n = YANU_MAX_PRESCALER_N;
79         for (n = YANU_MAX_PRESCALER_N; n >= 0; n--) {
80                 if ((unsigned)CONFIG_SYS_CLK_FREQ / (1 << (n + 4)) >=
81                     gd->baudrate) {
82                         best_n = n;
83                         break;
84                 }
85         }
86         for (k = 0;; k++) {
87                 if (gd->baudrate <= (max_uns >> (15+n-k)))
88                         break;
89         }
90         best_m =
91             (gd->baudrate * (1 << (15 + n - k))) /
92             ((unsigned)CONFIG_SYS_CLK_FREQ >> k);
93
94         baud = best_m + best_n * YANU_BAUDE;
95         writel(baud, &uart->baud);
96
97         return;
98 }
99
100
101 #endif /* CONFIG_SYS_NIOS_FIXEDBAUD */
102
103 int serial_init (void)
104 {
105         unsigned action,control;
106
107         /* status register cleanup */
108         action =  YANU_ACTION_RRRDY     |
109                 YANU_ACTION_RTRDY       |
110                 YANU_ACTION_ROE         |
111                 YANU_ACTION_RBRK        |
112                 YANU_ACTION_RFE         |
113                 YANU_ACTION_RPE         |
114             YANU_ACTION_RFE | YANU_ACTION_RFIFO_CLEAR | YANU_ACTION_TFIFO_CLEAR;
115
116         writel(action, &uart->action);
117         
118         /*  control register cleanup */
119         /* no interrupts enabled */
120         /* one stop bit */
121         /* hardware flow control disabled */
122         /* 8 bits */
123         control = (0x7 << YANU_CONTROL_BITS_POS);
124         /* enven parity just to be clean */
125         control |= YANU_CONTROL_PAREVEN;
126         /* we set threshold for fifo */
127         control |= YANU_CONTROL_RDYDLY * YANU_RXFIFO_DLY;
128         control |= YANU_CONTROL_TXTHR *  YANU_TXFIFO_THR;
129
130         writel(control, &uart->control);
131
132         /* to set baud rate */
133         serial_setbrg();
134
135         return (0);
136 }
137
138
139 /*-----------------------------------------------------------------------
140  * YANU CONSOLE
141  *---------------------------------------------------------------------*/
142 void serial_putc (char c)
143 {
144         int tx_chars;
145         unsigned status;
146
147         if (c == '\n')
148                 serial_putc ('\r');
149         
150         while (1) {
151                 status = readl(&uart->status);
152                 tx_chars = (status>>YANU_TFIFO_CHARS_POS)
153                         & ((1<<YANU_TFIFO_CHARS_N)-1);
154                 if (tx_chars < YANU_TXFIFO_SIZE-1)
155                         break;
156                 WATCHDOG_RESET ();
157         }
158
159         writel((unsigned char)c, &uart->data);
160 }
161
162 void serial_puts (const char *s)
163 {
164         while (*s != 0) {
165                 serial_putc (*s++);
166         }
167 }
168
169
170 int serial_tstc(void)
171 {
172         unsigned status ;
173
174         status = readl(&uart->status);
175         return (((status >> YANU_RFIFO_CHARS_POS) &
176                  ((1 << YANU_RFIFO_CHARS_N) - 1)) > 0);
177 }       
178
179 int serial_getc (void)
180 {
181         while (serial_tstc() == 0)
182                 WATCHDOG_RESET ();
183         
184         /* first we pull the char */
185         writel(YANU_ACTION_RFIFO_PULL, &uart->action);
186
187         return(readl(&uart->data) & YANU_DATA_CHAR_MASK);
188 }