]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - arch/sparc/cpu/leon2/serial.c
serial: sparc: Implement CONFIG_SERIAL_MULTI into leon2 serial driver
[karo-tx-uboot.git] / arch / sparc / cpu / leon2 / serial.c
1 /* GRLIB APBUART Serial controller driver
2  *
3  * (C) Copyright 2008
4  * Daniel Hellstrom, Gaisler Research, daniel@gaisler.com.
5  *
6  * See file CREDITS for list of people who contributed to this
7  * project.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of
12  * the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22  * MA 02111-1307 USA
23  *
24  */
25
26 #include <common.h>
27 #include <asm/processor.h>
28 #include <asm/leon.h>
29 #include <serial.h>
30 #include <linux/compiler.h>
31
32 DECLARE_GLOBAL_DATA_PTR;
33
34 /* Force cache miss each time a serial controller reg is read */
35 #define CACHE_BYPASS 1
36
37 #ifdef CACHE_BYPASS
38 #define READ_BYTE(var)  SPARC_NOCACHE_READ_BYTE((unsigned int)&(var))
39 #define READ_HWORD(var) SPARC_NOCACHE_READ_HWORD((unsigned int)&(var))
40 #define READ_WORD(var)  SPARC_NOCACHE_READ((unsigned int)&(var))
41 #define READ_DWORD(var) SPARC_NOCACHE_READ_DWORD((unsigned int)&(var))
42 #endif
43
44 static int leon2_serial_init(void)
45 {
46         LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
47         LEON2_Uart_regs *regs;
48         unsigned int tmp;
49
50         /* Init LEON2 UART
51          *
52          * Set scaler / baud rate
53          *
54          * Receiver & transmitter enable
55          */
56 #if LEON2_CONSOLE_SELECT == LEON_CONSOLE_UART1
57         regs = (LEON2_Uart_regs *) & leon2->UART_Channel_1;
58 #else
59         regs = (LEON2_Uart_regs *) & leon2->UART_Channel_2;
60 #endif
61
62         regs->UART_Scaler = CONFIG_SYS_LEON2_UART1_SCALER;
63
64         /* Let bit 11 be unchanged (debug bit for GRMON) */
65         tmp = READ_WORD(regs->UART_Control);
66
67         regs->UART_Control = ((tmp & LEON2_UART_CTRL_DBG) |
68                               (LEON2_UART1_LOOPBACK_ENABLE << 7) |
69                               (LEON2_UART1_FLOWCTRL_ENABLE << 6) |
70                               (LEON2_UART1_PARITY_ENABLE << 5) |
71                               (LEON2_UART1_ODDPAR_ENABLE << 4) |
72                               LEON2_UART_CTRL_RE | LEON2_UART_CTRL_TE);
73
74         return 0;
75 }
76
77 static void leon2_serial_putc_raw(const char c)
78 {
79         LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
80         LEON2_Uart_regs *regs;
81
82 #if LEON2_CONSOLE_SELECT == LEON_CONSOLE_UART1
83         regs = (LEON2_Uart_regs *) & leon2->UART_Channel_1;
84 #else
85         regs = (LEON2_Uart_regs *) & leon2->UART_Channel_2;
86 #endif
87
88         /* Wait for last character to go. */
89         while (!(READ_WORD(regs->UART_Status) & LEON2_UART_STAT_THE)) ;
90
91         /* Send data */
92         regs->UART_Channel = c;
93
94 #ifdef LEON_DEBUG
95         /* Wait for data to be sent */
96         while (!(READ_WORD(regs->UART_Status) & LEON2_UART_STAT_TSE)) ;
97 #endif
98 }
99
100 static void leon2_serial_putc(const char c)
101 {
102         if (c == '\n')
103                 leon2_serial_putc_raw('\r');
104
105         leon2_serial_putc_raw(c);
106 }
107
108 static void leon2_serial_puts(const char *s)
109 {
110         while (*s) {
111                 serial_putc(*s++);
112         }
113 }
114
115 static int leon2_serial_getc(void)
116 {
117         LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
118         LEON2_Uart_regs *regs;
119
120 #if LEON2_CONSOLE_SELECT == LEON_CONSOLE_UART1
121         regs = (LEON2_Uart_regs *) & leon2->UART_Channel_1;
122 #else
123         regs = (LEON2_Uart_regs *) & leon2->UART_Channel_2;
124 #endif
125
126         /* Wait for a character to arrive. */
127         while (!(READ_WORD(regs->UART_Status) & LEON2_UART_STAT_DR)) ;
128
129         /* read data */
130         return READ_WORD(regs->UART_Channel);
131 }
132
133 static int leon2_serial_tstc(void)
134 {
135         LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
136         LEON2_Uart_regs *regs;
137
138 #if LEON2_CONSOLE_SELECT == LEON_CONSOLE_UART1
139         regs = (LEON2_Uart_regs *) & leon2->UART_Channel_1;
140 #else
141         regs = (LEON2_Uart_regs *) & leon2->UART_Channel_2;
142 #endif
143
144         return (READ_WORD(regs->UART_Status) & LEON2_UART_STAT_DR);
145 }
146
147 /* set baud rate for uart */
148 static void leon2_serial_setbrg(void)
149 {
150         /* update baud rate settings, read it from gd->baudrate */
151         unsigned int scaler;
152         LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
153         LEON2_Uart_regs *regs;
154
155 #if LEON2_CONSOLE_SELECT == LEON_CONSOLE_UART1
156         regs = (LEON2_Uart_regs *) & leon2->UART_Channel_1;
157 #else
158         regs = (LEON2_Uart_regs *) & leon2->UART_Channel_2;
159 #endif
160
161         if (gd->baudrate > 0) {
162                 scaler =
163                     (((CONFIG_SYS_CLK_FREQ * 10) / (gd->baudrate * 8)) -
164                      5) / 10;
165                 regs->UART_Scaler = scaler;
166         }
167 }
168
169 #ifdef CONFIG_SERIAL_MULTI
170 static struct serial_device leon2_serial_drv = {
171         .name   = "leon2_serial",
172         .start  = leon2_serial_init,
173         .stop   = NULL,
174         .setbrg = leon2_serial_setbrg,
175         .putc   = leon2_serial_putc,
176         .puts   = leon2_serial_puts,
177         .getc   = leon2_serial_getc,
178         .tstc   = leon2_serial_tstc,
179 };
180
181 void leon2_serial_initialize(void)
182 {
183         serial_register(&leon2_serial_drv);
184 }
185
186 __weak struct serial_device *default_serial_console(void)
187 {
188         return &leon2_serial_drv;
189 }
190 #else
191 int serial_init(void)
192 {
193         return leon2_serial_init();
194 }
195
196 void serial_setbrg(void)
197 {
198         leon2_serial_setbrg();
199 }
200
201 void serial_putc(const char c)
202 {
203         leon2_serial_putc(c);
204 }
205
206 void serial_puts(const char *s)
207 {
208         leon2_serial_puts(s);
209 }
210
211 int serial_getc(void)
212 {
213         return leon2_serial_getc();
214 }
215
216 int serial_tstc(void)
217 {
218         return leon2_serial_tstc();
219 }
220 #endif