xref: /netbsd-src/sys/arch/mips/ralink/ralink_com.c (revision a5847cc334d9a7029f6352b847e9e8d71a0f9e0c)
1 /*	$NetBSD: ralink_com.c,v 1.2 2011/07/28 15:38:49 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.2 2011/07/28 15:38:49 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 
199 int
200 ralink_com_match(device_t parent, cfdata_t cf, void *aux)
201 {
202 	if (cn_tab == NULL || cn_tab->cn_pri < CN_NORMAL) {
203 		printf("NULL console set, don't install ourselves "
204 			"(of course this shouldn't print)");
205 		return 0;
206 	}
207 
208 	/*
209 	 * If we got this far, assume we want to install it as the console.
210 	 * No need to probe. Future possibilities include checking to see if it
211 	 * is console or KGDB but now it is our only console method if we aren't
212 	 * forcing a null console
213 	 */
214 	return 1;
215 }
216 
217 void
218 ralink_com_attach(device_t parent, device_t self, void *aux)
219 {
220 	const struct mainbus_attach_args *ma = aux;
221 	struct ralink_com_softc * const rtsc = device_private(self);
222 	struct com_softc * const sc = &rtsc->sc_com;
223 	bus_space_handle_t ioh;
224 	int error;
225 
226 	if ((error = bus_space_map(ma->ma_memt, RA_UART_LITE_BASE,
227 	    0x1000, 0, &ioh)) != 0) {
228 		aprint_error(": can't map registers, error=%d\n", error);
229 		return;
230 	}
231 
232 	COM_INIT_REGS(sc->sc_regs, ma->ma_memt, ioh, RA_UART_LITE_BASE);
233 	sc->sc_dev = self;
234 	sc->sc_frequency = RA_UART_FREQ;
235 	sc->sc_regs.cr_nports = 0x1000;
236 	sc->sc_type = COM_TYPE_AU1x00;
237 	sc->enabled = 1;
238 
239 	ralink_com_initmap(&sc->sc_regs);
240 
241 	rtsc->sc_ih = ra_intr_establish(RA_IRQ_UARTL, comintr, sc, 1);
242 
243 	com_attach_subr(sc);
244 }
245 
246 static void
247 ralink_com_initmap(struct com_regs *regsp)
248 {
249 	regsp->cr_map[COM_REG_RXDATA] = RA_UART_RBR;
250 	regsp->cr_map[COM_REG_TXDATA] = RA_UART_TBR;
251 	regsp->cr_map[COM_REG_DLBL]   = RA_UART_DLL;
252 	regsp->cr_map[COM_REG_IER]    = RA_UART_IER;
253 	regsp->cr_map[COM_REG_IIR]    = RA_UART_IIR;
254 	regsp->cr_map[COM_REG_FIFO]   = RA_UART_FCR;
255 	regsp->cr_map[COM_REG_LCR]    = RA_UART_LCR;
256 	regsp->cr_map[COM_REG_MCR]    = RA_UART_MCR;
257 	regsp->cr_map[COM_REG_LSR]    = RA_UART_LSR;
258 	regsp->cr_map[COM_REG_MSR]    = RA_UART_MSR;
259 }
260 
261 void
262 ralink_com_early(int silent)
263 {
264 	struct com_regs regs;
265 	uint32_t r;
266 	int error;
267 
268 	/* reset */
269 	r = sysctl_read(RA_SYSCTL_RST);
270 	r |= RST_UARTL;
271 	sysctl_write(RA_SYSCTL_RST, r);
272 	r ^= RST_UARTL;
273 	sysctl_write(RA_SYSCTL_RST, r);
274 
275 	if (silent) {
276 		/*
277 		 * put us in PIO mode,
278 		 * effectively tri-stating the UARTL block
279 		 */
280 		r = sysctl_read(RA_SYSCTL_GPIOMODE);
281 		r |= GPIOMODE_UARTL;
282 		sysctl_write(RA_SYSCTL_GPIOMODE, r);
283 	} else {
284 		/* make sure we are in UART mode */
285 		r = sysctl_read(RA_SYSCTL_GPIOMODE);
286 		r &= ~GPIOMODE_UARTL;
287 		sysctl_write(RA_SYSCTL_GPIOMODE, r);
288 	}
289 
290 	uart_write(RA_UART_IER, 0);		/* disable interrupts */
291 	uart_write(RA_UART_FCR, 0);		/* disable fifos */
292 
293 	/* set baud rate */
294 	uart_write(RA_UART_LCR,
295 		UART_LCR_WLS0 | UART_LCR_WLS1 | UART_LCR_DLAB);
296 	uart_write(RA_UART_DLL,
297 		(RA_UART_FREQ / RA_SERIAL_CLKDIV / RA_BAUDRATE)
298 			& 0xffff);
299 	uart_write(RA_UART_LCR,
300 		UART_LCR_WLS0 | UART_LCR_WLS1);
301 
302 	regs.cr_iot = &ra_bus_memt;
303 	regs.cr_iobase = RA_UART_LITE_BASE;
304 	regs.cr_nports = 0x1000;
305 	ralink_com_initmap(&regs);
306 
307 	if ((error = bus_space_map(regs.cr_iot, regs.cr_iobase, regs.cr_nports,
308 	    0, &regs.cr_ioh)) != 0) {
309 		return;
310 	}
311 
312 	/* Ralink UART has a 16-bit rate latch (like the AU1x00) */
313 	comcnattach1(&regs, RA_BAUDRATE, RA_UART_FREQ,
314 		COM_TYPE_AU1x00, CONMODE);
315 }
316