xref: /netbsd-src/sys/arch/mips/ralink/ralink_com.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: ralink_com.c,v 1.4 2014/04/30 00:51:26 matt Exp $	*/
2 /*-
3  * Copyright (c) 2011 CradlePoint Technology, Inc.
4  * All rights reserved.
5  *
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY CRADLEPOINT TECHNOLOGY, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*-
30  * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
31  * Copyright (c) 2006 Garrett D'Amore.
32  * All rights reserved.
33  *
34  * Portions of this code were written by Garrett D'Amore for the
35  * Champaign-Urbana Community Wireless Network Project.
36  *
37  * Redistribution and use in source and binary forms, with or
38  * without modification, are permitted provided that the following
39  * conditions are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above
43  *    copyright notice, this list of conditions and the following
44  *    disclaimer in the documentation and/or other materials provided
45  *    with the distribution.
46  * 3. All advertising materials mentioning features or use of this
47  *    software must display the following acknowledgements:
48  *      This product includes software developed by the Urbana-Champaign
49  *      Independent Media Center.
50  *    This product includes software developed by Garrett D'Amore.
51  * 4. Urbana-Champaign Independent Media Center's name and Garrett
52  *    D'Amore's name may not be used to endorse or promote products
53  *    derived from this software without specific prior written permission.
54  *
55  * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
56  * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
57  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
58  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59  * ARE DISCLAIMED.  IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
60  * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
61  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
62  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
63  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
64  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
65  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
66  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
67  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
68  */
69 
70 /*-
71  * Copyright (c) 1998 The NetBSD Foundation, Inc.
72  * All rights reserved.
73  *
74  * This code is derived from software contributed to The NetBSD Foundation
75  * by Charles M. Hannum.
76  *
77  * Redistribution and use in source and binary forms, with or without
78  * modification, are permitted provided that the following conditions
79  * are met:
80  * 1. Redistributions of source code must retain the above copyright
81  *    notice, this list of conditions and the following disclaimer.
82  * 2. Redistributions in binary form must reproduce the above copyright
83  *    notice, this list of conditions and the following disclaimer in the
84  *    documentation and/or other materials provided with the distribution.
85  *
86  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
87  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
88  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
89  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
90  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
91  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
92  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
93  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
94  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
95  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
96  * POSSIBILITY OF SUCH DAMAGE.
97  */
98 
99 /*-
100  * Copyright (c) 1991 The Regents of the University of California.
101  * All rights reserved.
102  *
103  * Redistribution and use in source and binary forms, with or without
104  * modification, are permitted provided that the following conditions
105  * are met:
106  * 1. Redistributions of source code must retain the above copyright
107  *    notice, this list of conditions and the following disclaimer.
108  * 2. Redistributions in binary form must reproduce the above copyright
109  *    notice, this list of conditions and the following disclaimer in the
110  *    documentation and/or other materials provided with the distribution.
111  * 3. Neither the name of the University nor the names of its contributors
112  *    may be used to endorse or promote products derived from this software
113  *    without specific prior written permission.
114  *
115  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
116  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
117  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
118  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
119  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
120  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
121  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
122  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
123  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
124  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
125  * SUCH DAMAGE.
126  *
127  *    @(#)com.c    7.5 (Berkeley) 5/16/91
128  */
129 
130 /* ralink_com.c -- Ralink 3052 uart console driver */
131 
132 #include <sys/cdefs.h>
133 __KERNEL_RCSID(0, "$NetBSD: ralink_com.c,v 1.4 2014/04/30 00:51:26 matt Exp $");
134 
135 #include <sys/param.h>
136 #include <sys/bus.h>
137 #include <sys/device.h>
138 #include <sys/kernel.h>
139 #include <sys/systm.h>
140 #include <sys/tty.h>
141 #include <sys/termios.h>
142 #include <sys/ttydefaults.h>
143 
144 #include <dev/cons.h>
145 #include <dev/ic/comreg.h>
146 #include <dev/ic/comvar.h>
147 
148 #include <mips/cpuregs.h>
149 
150 #include <mips/ralink/ralink_reg.h>
151 #include <mips/ralink/ralink_var.h>
152 
153 #include "opt_com.h"
154 
155 struct ralink_com_softc {
156     struct com_softc sc_com;
157     void *           sc_ih;
158 };
159 
160 static int ralink_com_match(device_t, cfdata_t , void *);
161 static void ralink_com_attach(device_t, device_t, void *);
162 static void ralink_com_initmap(struct com_regs *regsp);
163 
164 CFATTACH_DECL_NEW(ralink_com, sizeof(struct ralink_com_softc),
165 	ralink_com_match, ralink_com_attach, NULL, NULL);
166 
167 #define CONMODE	\
168 	((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
169 
170 #ifndef COM_REGMAP
171 #error  COM_REGMAP not defined!
172 #endif
173 
174 static inline uint32_t
175 sysctl_read(const u_int offset)
176 {
177 	return *RA_IOREG_VADDR(RA_SYSCTL_BASE, offset);
178 }
179 
180 static inline void
181 sysctl_write(const u_int offset, uint32_t val)
182 {
183 	*RA_IOREG_VADDR(RA_SYSCTL_BASE, offset) = val;
184 }
185 
186 static inline uint32_t
187 uart_read(const u_int offset)
188 {
189 	return *RA_IOREG_VADDR(RA_UART_LITE_BASE, offset);
190 }
191 
192 static inline void
193 uart_write(const u_int offset, const uint32_t val)
194 {
195 	*RA_IOREG_VADDR(RA_UART_LITE_BASE, offset) = val;
196 }
197 
198 #ifdef RALINK_CONSOLE_EARLY
199 static int
200 ralink_cngetc(dev_t dv)
201 {
202         if ((uart_read(RA_UART_LSR) & LSR_RXRDY) == 0)
203 		return -1;
204 
205 	return uart_read(RA_UART_RBR) & 0xff;
206 }
207 
208 static void
209 ralink_cnputc(dev_t dv, int c)
210 {
211 	int timo = 150000;
212 
213         while ((uart_read(RA_UART_LSR) & LSR_TXRDY) == 0 && --timo > 0)
214 		;
215 
216 	uart_write(RA_UART_TBR, c);
217 	__asm __volatile("sync");
218 
219 	timo = 150000;
220         while ((uart_read(RA_UART_LSR) & LSR_TSRE) == 0 && --timo > 0)
221 		;
222 }
223 
224 static struct consdev ralink_earlycons = {
225 	.cn_putc = ralink_cnputc,
226 	.cn_getc = ralink_cngetc,
227 	.cn_pollc = nullcnpollc,
228 };
229 
230 void
231 ralink_console_early(void)
232 {
233 	cn_tab = &ralink_earlycons;
234 }
235 #endif /* RALINK_CONSOLE_EARLY */
236 
237 
238 int
239 ralink_com_match(device_t parent, cfdata_t cf, void *aux)
240 {
241 	if (cn_tab == NULL || cn_tab->cn_pri < CN_NORMAL) {
242 		printf("NULL console set, don't install ourselves "
243 			"(of course this shouldn't print)");
244 		return 0;
245 	}
246 
247 	/*
248 	 * If we got this far, assume we want to install it as the console.
249 	 * No need to probe. Future possibilities include checking to see if it
250 	 * is console or KGDB but now it is our only console method if we aren't
251 	 * forcing a null console
252 	 */
253 	return 1;
254 }
255 
256 void
257 ralink_com_attach(device_t parent, device_t self, void *aux)
258 {
259 	const struct mainbus_attach_args *ma = aux;
260 	struct ralink_com_softc * const rtsc = device_private(self);
261 	struct com_softc * const sc = &rtsc->sc_com;
262 	bus_space_handle_t ioh;
263 	int error;
264 
265 	if ((error = bus_space_map(ma->ma_memt, RA_UART_LITE_BASE,
266 	    0x1000, 0, &ioh)) != 0) {
267 		aprint_error(": can't map registers, error=%d\n", error);
268 		return;
269 	}
270 
271 	COM_INIT_REGS(sc->sc_regs, ma->ma_memt, ioh, RA_UART_LITE_BASE);
272 	sc->sc_dev = self;
273 	sc->sc_frequency = RA_UART_FREQ;
274 	sc->sc_regs.cr_nports = 0x1000;
275 	sc->sc_type = COM_TYPE_AU1x00;
276 	sc->enabled = 1;
277 
278 	ralink_com_initmap(&sc->sc_regs);
279 
280 	rtsc->sc_ih = ra_intr_establish(RA_IRQ_UARTL, comintr, sc, 1);
281 
282 	com_attach_subr(sc);
283 }
284 
285 static void
286 ralink_com_initmap(struct com_regs *regsp)
287 {
288 	regsp->cr_map[COM_REG_RXDATA] = RA_UART_RBR;
289 	regsp->cr_map[COM_REG_TXDATA] = RA_UART_TBR;
290 	regsp->cr_map[COM_REG_DLBL]   = RA_UART_DLL;
291 	regsp->cr_map[COM_REG_IER]    = RA_UART_IER;
292 	regsp->cr_map[COM_REG_IIR]    = RA_UART_IIR;
293 	regsp->cr_map[COM_REG_FIFO]   = RA_UART_FCR;
294 	regsp->cr_map[COM_REG_LCR]    = RA_UART_LCR;
295 	regsp->cr_map[COM_REG_MCR]    = RA_UART_MCR;
296 	regsp->cr_map[COM_REG_LSR]    = RA_UART_LSR;
297 	regsp->cr_map[COM_REG_MSR]    = RA_UART_MSR;
298 }
299 
300 void
301 ralink_com_early(int silent)
302 {
303 	struct com_regs regs;
304 	uint32_t r;
305 	int error;
306 
307 	/* reset */
308 	r = sysctl_read(RA_SYSCTL_RST);
309 	r |= RST_UARTL;
310 	sysctl_write(RA_SYSCTL_RST, r);
311 	r ^= RST_UARTL;
312 	sysctl_write(RA_SYSCTL_RST, r);
313 
314 	if (silent) {
315 		/*
316 		 * put us in PIO mode,
317 		 * effectively tri-stating the UARTL block
318 		 */
319 		r = sysctl_read(RA_SYSCTL_GPIOMODE);
320 		r |= GPIOMODE_UARTL;
321 		sysctl_write(RA_SYSCTL_GPIOMODE, r);
322 	} else {
323 		/* make sure we are in UART mode */
324 		r = sysctl_read(RA_SYSCTL_GPIOMODE);
325 		r &= ~GPIOMODE_UARTL;
326 		sysctl_write(RA_SYSCTL_GPIOMODE, r);
327 	}
328 
329 	uart_write(RA_UART_IER, 0);		/* disable interrupts */
330 	uart_write(RA_UART_FCR, 0);		/* disable fifos */
331 
332 	/* set baud rate */
333 	uart_write(RA_UART_LCR,
334 		UART_LCR_WLS0 | UART_LCR_WLS1 | UART_LCR_DLAB);
335 	uart_write(RA_UART_DLL,
336 		(RA_UART_FREQ / RA_SERIAL_CLKDIV / RA_BAUDRATE)
337 			& 0xffff);
338 	uart_write(RA_UART_LCR,
339 		UART_LCR_WLS0 | UART_LCR_WLS1);
340 
341 	regs.cr_iot = &ra_bus_memt;
342 	regs.cr_iobase = RA_UART_LITE_BASE;
343 	regs.cr_nports = 0x1000;
344 	ralink_com_initmap(&regs);
345 
346 	if ((error = bus_space_map(regs.cr_iot, regs.cr_iobase, regs.cr_nports,
347 	    0, &regs.cr_ioh)) != 0) {
348 		return;
349 	}
350 
351 	/* Ralink UART has a 16-bit rate latch (like the AU1x00) */
352 	comcnattach1(&regs, RA_BAUDRATE, RA_UART_FREQ,
353 		COM_TYPE_AU1x00, CONMODE);
354 }
355