blob: 4f41b8e6aa3302ce25e3fd0f56366fbb0e1fa8b9 [file] [log] [blame]
Daniel Hellstromb3309902008-03-28 10:00:33 +01001/* 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
30DECLARE_GLOBAL_DATA_PTR;
31
32/* Force cache miss each time a serial controller reg is read */
33#define CACHE_BYPASS 1
34
35#ifdef CACHE_BYPASS
36#define READ_BYTE(var) SPARC_NOCACHE_READ_BYTE((unsigned int)&(var))
37#define READ_HWORD(var) SPARC_NOCACHE_READ_HWORD((unsigned int)&(var))
38#define READ_WORD(var) SPARC_NOCACHE_READ((unsigned int)&(var))
39#define READ_DWORD(var) SPARC_NOCACHE_READ_DWORD((unsigned int)&(var))
40#endif
41
42int serial_init(void)
43{
44 LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
45 LEON2_Uart_regs *regs;
46 unsigned int tmp;
47
48 /* Init LEON2 UART
49 *
50 * Set scaler / baud rate
51 *
52 * Receiver & transmitter enable
53 */
54#if LEON2_CONSOLE_SELECT == LEON_CONSOLE_UART1
55 regs = (LEON2_Uart_regs *) & leon2->UART_Channel_1;
56#else
57 regs = (LEON2_Uart_regs *) & leon2->UART_Channel_2;
58#endif
59
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020060 regs->UART_Scaler = CONFIG_SYS_LEON2_UART1_SCALER;
Daniel Hellstromb3309902008-03-28 10:00:33 +010061
62 /* Let bit 11 be unchanged (debug bit for GRMON) */
63 tmp = READ_WORD(regs->UART_Control);
64
65 regs->UART_Control = ((tmp & LEON2_UART_CTRL_DBG) |
66 (LEON2_UART1_LOOPBACK_ENABLE << 7) |
67 (LEON2_UART1_FLOWCTRL_ENABLE << 6) |
68 (LEON2_UART1_PARITY_ENABLE << 5) |
69 (LEON2_UART1_ODDPAR_ENABLE << 4) |
70 LEON2_UART_CTRL_RE | LEON2_UART_CTRL_TE);
71
72 return 0;
73}
74
75void serial_putc(const char c)
76{
77 if (c == '\n')
78 serial_putc_raw('\r');
79
80 serial_putc_raw(c);
81}
82
83void serial_putc_raw(const char c)
84{
85 LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
86 LEON2_Uart_regs *regs;
87
88#if LEON2_CONSOLE_SELECT == LEON_CONSOLE_UART1
89 regs = (LEON2_Uart_regs *) & leon2->UART_Channel_1;
90#else
91 regs = (LEON2_Uart_regs *) & leon2->UART_Channel_2;
92#endif
93
94 /* Wait for last character to go. */
95 while (!(READ_WORD(regs->UART_Status) & LEON2_UART_STAT_THE)) ;
96
97 /* Send data */
98 regs->UART_Channel = c;
99
100#ifdef LEON_DEBUG
101 /* Wait for data to be sent */
102 while (!(READ_WORD(regs->UART_Status) & LEON2_UART_STAT_TSE)) ;
103#endif
104}
105
106void serial_puts(const char *s)
107{
108 while (*s) {
109 serial_putc(*s++);
110 }
111}
112
113int serial_getc(void)
114{
115 LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
116 LEON2_Uart_regs *regs;
117
118#if LEON2_CONSOLE_SELECT == LEON_CONSOLE_UART1
119 regs = (LEON2_Uart_regs *) & leon2->UART_Channel_1;
120#else
121 regs = (LEON2_Uart_regs *) & leon2->UART_Channel_2;
122#endif
123
124 /* Wait for a character to arrive. */
125 while (!(READ_WORD(regs->UART_Status) & LEON2_UART_STAT_DR)) ;
126
127 /* read data */
128 return READ_WORD(regs->UART_Channel);
129}
130
131int serial_tstc(void)
132{
133 LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
134 LEON2_Uart_regs *regs;
135
136#if LEON2_CONSOLE_SELECT == LEON_CONSOLE_UART1
137 regs = (LEON2_Uart_regs *) & leon2->UART_Channel_1;
138#else
139 regs = (LEON2_Uart_regs *) & leon2->UART_Channel_2;
140#endif
141
142 return (READ_WORD(regs->UART_Status) & LEON2_UART_STAT_DR);
143}
144
145/* set baud rate for uart */
146void serial_setbrg(void)
147{
148 /* update baud rate settings, read it from gd->baudrate */
149 unsigned int scaler;
150 LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
151 LEON2_Uart_regs *regs;
152
153#if LEON2_CONSOLE_SELECT == LEON_CONSOLE_UART1
154 regs = (LEON2_Uart_regs *) & leon2->UART_Channel_1;
155#else
156 regs = (LEON2_Uart_regs *) & leon2->UART_Channel_2;
157#endif
158
159 if (gd->baudrate > 0) {
160 scaler =
161 (((CONFIG_SYS_CLK_FREQ * 10) / (gd->baudrate * 8)) -
162 5) / 10;
163 regs->UART_Scaler = scaler;
164 }
165}