xref: /openbmc/u-boot/drivers/serial/serial_sh.c (revision 2956532625cf8414ad3efb37598ba34db08d67ec)
1 /*
2  * SuperH SCIF device driver.
3  * Copyright (c) 2007,2008 Nobuhiro Iwamatsu
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19 
20 #include <common.h>
21 #include <asm/io.h>
22 #include <asm/processor.h>
23 
24 DECLARE_GLOBAL_DATA_PTR;
25 
26 #if defined(CONFIG_CONS_SCIF0)
27 # define SCIF_BASE	SCIF0_BASE
28 #elif defined(CONFIG_CONS_SCIF1)
29 # define SCIF_BASE	SCIF1_BASE
30 #elif defined(CONFIG_CONS_SCIF2)
31 # define SCIF_BASE	SCIF2_BASE
32 #elif defined(CONFIG_CONS_SCIF3)
33 # define SCIF_BASE	SCIF3_BASE
34 #elif defined(CONFIG_CONS_SCIF4)
35 # define SCIF_BASE	SCIF4_BASE
36 #elif defined(CONFIG_CONS_SCIF5)
37 # define SCIF_BASE	SCIF5_BASE
38 #else
39 # error "Default SCIF doesn't set....."
40 #endif
41 
42 /* Base register */
43 #define SCSMR	(vu_short *)(SCIF_BASE + 0x0)
44 #define SCBRR	(vu_char  *)(SCIF_BASE + 0x4)
45 #define SCSCR	(vu_short *)(SCIF_BASE + 0x8)
46 #define SCFCR	(vu_short *)(SCIF_BASE + 0x18)
47 #define SCFDR	(vu_short *)(SCIF_BASE + 0x1C)
48 #if defined(CONFIG_CPU_SH7720) || \
49 	(defined(CONFIG_CPU_SH7723) && defined(CONFIG_SCIF_A))
50 # define SCFSR	(vu_short *)(SCIF_BASE + 0x14)	/* SCSSR */
51 # define SCFTDR	(vu_char  *)(SCIF_BASE + 0x20)
52 # define SCFRDR	(vu_char  *)(SCIF_BASE + 0x24)
53 #else
54 # define SCFTDR (vu_char  *)(SCIF_BASE + 0xC)
55 # define SCFSR	(vu_short *)(SCIF_BASE + 0x10)
56 # define SCFRDR (vu_char  *)(SCIF_BASE + 0x14)
57 #endif
58 
59 #if	defined(CONFIG_CPU_SH7780) || \
60 	defined(CONFIG_CPU_SH7785)
61 # define SCRFDR	(vu_short *)(SCIF_BASE + 0x20)
62 # define SCSPTR	(vu_short *)(SCIF_BASE + 0x24)
63 # define SCLSR	(vu_short *)(SCIF_BASE + 0x28)
64 # define SCRER	(vu_short *)(SCIF_BASE + 0x2C)
65 # define LSR_ORER	1
66 # define FIFOLEVEL_MASK	0xFF
67 #elif defined(CONFIG_CPU_SH7763)
68 # if defined(CONFIG_CONS_SCIF2)
69 # define SCSPTR	(vu_short *)(SCIF_BASE + 0x20)
70 # define SCLSR	(vu_short *)(SCIF_BASE + 0x24)
71 # define LSR_ORER	1
72 # define FIFOLEVEL_MASK	0x1F
73 # else
74 # define SCRFDR	(vu_short *)(SCIF_BASE + 0x20)
75 # define SCSPTR	(vu_short *)(SCIF_BASE + 0x24)
76 # define SCLSR	(vu_short *)(SCIF_BASE + 0x28)
77 # define SCRER	(vu_short *)(SCIF_BASE + 0x2C)
78 # define LSR_ORER	1
79 # define FIFOLEVEL_MASK	0xFF
80 # endif
81 #elif defined(CONFIG_CPU_SH7723)
82 # if defined(CONFIG_SCIF_A)
83 # define SCLSR	SCFSR
84 # define LSR_ORER	0x0200
85 # define FIFOLEVEL_MASK	0x3F
86 #else
87 # define SCLSR	(vu_short *)(SCIF_BASE + 0x24)
88 # define LSR_ORER	1
89 # define FIFOLEVEL_MASK	0x1F
90 #endif
91 #elif defined(CONFIG_CPU_SH7750) || \
92 	defined(CONFIG_CPU_SH7751) || \
93 	defined(CONFIG_CPU_SH7722) || \
94 	defined(CONFIG_CPU_SH7203)
95 # define SCSPTR	(vu_short *)(SCIF_BASE + 0x20)
96 # define SCLSR	(vu_short *)(SCIF_BASE + 0x24)
97 # define LSR_ORER	1
98 # define FIFOLEVEL_MASK	0x1F
99 #elif defined(CONFIG_CPU_SH7720)
100 # define SCLSR		SCFSR
101 # define LSR_ORER	0x0200
102 # define FIFOLEVEL_MASK	0x1F
103 #elif defined(CONFIG_CPU_SH7710) || \
104 	defined(CONFIG_CPU_SH7712)
105 # define SCLSR	SCFSR		/* SCSSR */
106 # define LSR_ORER	1
107 # define FIFOLEVEL_MASK	0x1F
108 #endif
109 
110 /* SCBRR register value setting */
111 #if defined(CONFIG_CPU_SH7720)
112 # define SCBRR_VALUE(bps, clk) (((clk * 2) + 16 * bps) / (32 * bps) - 1)
113 #elif defined(CONFIG_CPU_SH7723) && defined(CONFIG_SCIF_A)
114 /* SH7723 SCIFA use bus clock. So clock *2 */
115 # define SCBRR_VALUE(bps, clk) (((clk * 2 * 2) + 16 * bps) / (32 * bps) - 1)
116 #else /* Generic SuperH */
117 # define SCBRR_VALUE(bps, clk) ((clk + 16 * bps) / (32 * bps) - 1)
118 #endif
119 
120 #define SCR_RE		(1 << 4)
121 #define SCR_TE		(1 << 5)
122 #define FCR_RFRST	(1 << 1)	/* RFCL */
123 #define FCR_TFRST	(1 << 2)	/* TFCL */
124 #define FSR_DR		(1 << 0)
125 #define FSR_RDF		(1 << 1)
126 #define FSR_FER		(1 << 3)
127 #define FSR_BRK		(1 << 4)
128 #define FSR_FER		(1 << 3)
129 #define FSR_TEND	(1 << 6)
130 #define FSR_ER		(1 << 7)
131 
132 /*----------------------------------------------------------------------*/
133 
134 void serial_setbrg(void)
135 {
136 	writeb(SCBRR_VALUE(gd->baudrate, CONFIG_SYS_CLK_FREQ), SCBRR);
137 }
138 
139 int serial_init(void)
140 {
141 	writew((SCR_RE | SCR_TE), SCSCR);
142 	writew(0, SCSMR);
143 	writew(0, SCSMR);
144 	writew((FCR_RFRST | FCR_TFRST), SCFCR);
145 	readw(SCFCR);
146 	writew(0, SCFCR);
147 
148 	serial_setbrg();
149 	return 0;
150 }
151 
152 static int serial_rx_fifo_level(void)
153 {
154 #if defined(SCRFDR)
155 	return (readw(SCRFDR) >> 0) & FIFOLEVEL_MASK;
156 #else
157 	return (readw(SCFDR) >> 0) & FIFOLEVEL_MASK;
158 #endif
159 }
160 
161 void serial_raw_putc(const char c)
162 {
163 	unsigned int fsr_bits_to_clear;
164 
165 	while (1) {
166 		if (readw(SCFSR) & FSR_TEND) { /* Tx fifo is empty */
167 			fsr_bits_to_clear = FSR_TEND;
168 			break;
169 		}
170 	}
171 
172 	writeb(c, SCFTDR);
173 	if (fsr_bits_to_clear != 0)
174 		writew(readw(SCFSR) & ~fsr_bits_to_clear, SCFSR);
175 }
176 
177 void serial_putc(const char c)
178 {
179 	if (c == '\n')
180 		serial_raw_putc('\r');
181 	serial_raw_putc(c);
182 }
183 
184 void serial_puts(const char *s)
185 {
186 	char c;
187 	while ((c = *s++) != 0)
188 		serial_putc(c);
189 }
190 
191 int serial_tstc(void)
192 {
193 	return serial_rx_fifo_level() ? 1 : 0;
194 }
195 
196 #define FSR_ERR_CLEAR	0x0063
197 #define RDRF_CLEAR		0x00fc
198 void handle_error(void)
199 {
200 	readw(SCFSR);
201 	writew(FSR_ERR_CLEAR, SCFSR);
202 	readw(SCLSR);
203 	writew(0x00, SCLSR);
204 }
205 
206 int serial_getc_check(void)
207 {
208 	unsigned short status;
209 
210 	status = readw(SCFSR);
211 
212 	if (status & (FSR_FER | FSR_ER | FSR_BRK))
213 		handle_error();
214 	if (readw(SCLSR) & LSR_ORER)
215 		handle_error();
216 	return status & (FSR_DR | FSR_RDF);
217 }
218 
219 int serial_getc(void)
220 {
221 	unsigned short status;
222 	char ch;
223 
224 	while (!serial_getc_check())
225 		;
226 
227 	ch = readb(SCFRDR);
228 	status = readw(SCFSR);
229 
230 	writew(RDRF_CLEAR, SCFSR);
231 
232 	if (status & (FSR_FER | FSR_FER | FSR_ER | FSR_BRK))
233 		handle_error();
234 
235 	if (readw(SCLSR) & LSR_ORER)
236 		handle_error();
237 
238 	return ch;
239 }
240