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