blob: a110244c9ef76ebbff89128cf553e5ba843d89dc [file] [log] [blame]
Daniel Hellstromb552dbe2008-03-26 22:51:29 +01001/* GRLIB APBUART Serial controller driver
2 *
3 * (C) Copyright 2007
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 <ambapp.h>
Marek Vasut2c91a962012-09-13 12:25:48 +020030#include <serial.h>
31#include <linux/compiler.h>
Daniel Hellstromb552dbe2008-03-26 22:51:29 +010032
33DECLARE_GLOBAL_DATA_PTR;
34
35/* Force cache miss each time a serial controller reg is read */
36#define CACHE_BYPASS 1
37
38#ifdef CACHE_BYPASS
39#define READ_BYTE(var) SPARC_NOCACHE_READ_BYTE((unsigned int)&(var))
40#define READ_HWORD(var) SPARC_NOCACHE_READ_HWORD((unsigned int)&(var))
41#define READ_WORD(var) SPARC_NOCACHE_READ((unsigned int)&(var))
42#define READ_DWORD(var) SPARC_NOCACHE_READ_DWORD((unsigned int)&(var))
43#endif
44
45ambapp_dev_apbuart *leon3_apbuart = NULL;
46
Marek Vasut2c91a962012-09-13 12:25:48 +020047static int leon3_serial_init(void)
Daniel Hellstromb552dbe2008-03-26 22:51:29 +010048{
49 ambapp_apbdev apbdev;
50 unsigned int tmp;
51
52 /* find UART */
53 if (ambapp_apb_first(VENDOR_GAISLER, GAISLER_APBUART, &apbdev) == 1) {
54
55 leon3_apbuart = (ambapp_dev_apbuart *) apbdev.address;
56
57 /* found apbuart, let's init...
58 *
59 * Set scaler / baud rate
60 *
61 * Receiver & transmitter enable
62 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020063 leon3_apbuart->scaler = CONFIG_SYS_GRLIB_APBUART_SCALER;
Daniel Hellstromb552dbe2008-03-26 22:51:29 +010064
65 /* Let bit 11 be unchanged (debug bit for GRMON) */
66 tmp = READ_WORD(leon3_apbuart->ctrl);
67
68 leon3_apbuart->ctrl = ((tmp & LEON_REG_UART_CTRL_DBG) |
69 LEON_REG_UART_CTRL_RE |
70 LEON_REG_UART_CTRL_TE);
71
72 return 0;
73 }
74 return -1; /* didn't find hardware */
75}
76
Marek Vasut2c91a962012-09-13 12:25:48 +020077static void leon3_serial_putc_raw(const char c)
Daniel Hellstromb552dbe2008-03-26 22:51:29 +010078{
79 if (!leon3_apbuart)
80 return;
81
82 /* Wait for last character to go. */
83 while (!(READ_WORD(leon3_apbuart->status) & LEON_REG_UART_STATUS_THE)) ;
84
85 /* Send data */
86 leon3_apbuart->data = c;
87
88#ifdef LEON_DEBUG
89 /* Wait for data to be sent */
90 while (!(READ_WORD(leon3_apbuart->status) & LEON_REG_UART_STATUS_TSE)) ;
91#endif
92}
93
Marek Vasut2c91a962012-09-13 12:25:48 +020094static void leon3_serial_putc(const char c)
95{
96 if (c == '\n')
97 leon3_serial_putc_raw('\r');
98
99 leon3_serial_putc_raw(c);
100}
101
102static void leon3_serial_puts(const char *s)
Daniel Hellstromb552dbe2008-03-26 22:51:29 +0100103{
104 while (*s) {
105 serial_putc(*s++);
106 }
107}
108
Marek Vasut2c91a962012-09-13 12:25:48 +0200109static int leon3_serial_getc(void)
Daniel Hellstromb552dbe2008-03-26 22:51:29 +0100110{
111 if (!leon3_apbuart)
112 return 0;
113
114 /* Wait for a character to arrive. */
115 while (!(READ_WORD(leon3_apbuart->status) & LEON_REG_UART_STATUS_DR)) ;
116
117 /* read data */
118 return READ_WORD(leon3_apbuart->data);
119}
120
Marek Vasut2c91a962012-09-13 12:25:48 +0200121static int leon3_serial_tstc(void)
Daniel Hellstromb552dbe2008-03-26 22:51:29 +0100122{
123 if (leon3_apbuart)
124 return (READ_WORD(leon3_apbuart->status) &
125 LEON_REG_UART_STATUS_DR);
126 return 0;
127}
128
129/* set baud rate for uart */
Marek Vasut2c91a962012-09-13 12:25:48 +0200130static void leon3_serial_setbrg(void)
Daniel Hellstromb552dbe2008-03-26 22:51:29 +0100131{
132 /* update baud rate settings, read it from gd->baudrate */
133 unsigned int scaler;
134 if (leon3_apbuart && (gd->baudrate > 0)) {
135 scaler =
136 (((CONFIG_SYS_CLK_FREQ * 10) / (gd->baudrate * 8)) -
137 5) / 10;
138 leon3_apbuart->scaler = scaler;
139 }
140 return;
141}
Marek Vasut2c91a962012-09-13 12:25:48 +0200142
143#ifdef CONFIG_SERIAL_MULTI
144static struct serial_device leon3_serial_drv = {
145 .name = "leon3_serial",
146 .start = leon3_serial_init,
147 .stop = NULL,
148 .setbrg = leon3_serial_setbrg,
149 .putc = leon3_serial_putc,
150 .puts = leon3_serial_puts,
151 .getc = leon3_serial_getc,
152 .tstc = leon3_serial_tstc,
153};
154
155void leon3_serial_initialize(void)
156{
157 serial_register(&leon3_serial_drv);
158}
159
160__weak struct serial_device *default_serial_console(void)
161{
162 return &leon3_serial_drv;
163}
164#else
165int serial_init(void)
166{
167 return leon3_serial_init();
168}
169
170void serial_setbrg(void)
171{
172 leon3_serial_setbrg();
173}
174
175void serial_putc(const char c)
176{
177 leon3_serial_putc(c);
178}
179
180void serial_puts(const char *s)
181{
182 leon3_serial_puts(s);
183}
184
185int serial_getc(void)
186{
187 return leon3_serial_getc();
188}
189
190int serial_tstc(void)
191{
192 return leon3_serial_tstc();
193}
194#endif