1 1.9 thorpej /* $NetBSD: ralink_com.c,v 1.9 2019/01/12 15:44:08 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.9 thorpej __KERNEL_RCSID(0, "$NetBSD: ralink_com.c,v 1.9 2019/01/12 15:44:08 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 struct ralink_com_softc { 155 1.5 ryo struct com_softc sc_com; 156 1.5 ryo void *sc_ih; 157 1.5 ryo bus_addr_t sc_addr; 158 1.5 ryo int sc_irq; 159 1.2 matt }; 160 1.2 matt 161 1.2 matt static int ralink_com_match(device_t, cfdata_t , void *); 162 1.2 matt static void ralink_com_attach(device_t, device_t, void *); 163 1.6 thorpej static void ralink_com_init_regs(struct com_regs *regsp, bus_space_tag_t, 164 1.6 thorpej bus_space_handle_t, bus_addr_t); 165 1.2 matt 166 1.2 matt CFATTACH_DECL_NEW(ralink_com, sizeof(struct ralink_com_softc), 167 1.2 matt ralink_com_match, ralink_com_attach, NULL, NULL); 168 1.2 matt 169 1.2 matt #define CONMODE \ 170 1.2 matt ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */ 171 1.2 matt 172 1.5 ryo #ifndef RALINK_CONADDR 173 1.5 ryo #define RALINK_CONADDR RA_UART_LITE_BASE /* default console is UART_LITE */ 174 1.5 ryo #endif 175 1.5 ryo 176 1.5 ryo /* address/irq/rst/gpiomode mappings */ 177 1.5 ryo static struct { 178 1.5 ryo bus_addr_t addr; 179 1.5 ryo int irq; 180 1.5 ryo uint32_t rst; 181 1.5 ryo uint32_t gpiomode; 182 1.5 ryo } ralink_uart_maps[] = { 183 1.5 ryo #ifdef MT7628 184 1.5 ryo { RA_UART_LITE_BASE, RA_IRQ_UARTL, RST_UART0_7628, GPIO1MODE_UART0 }, 185 1.5 ryo { RA_UART1_BASE, RA_IRQ_UART1, RST_UART1_7628, GPIO1MODE_UART1 }, 186 1.5 ryo { RA_UART2_BASE, RA_IRQ_UART2, RST_UART2_7628, GPIO1MODE_UART2 }, 187 1.5 ryo #else 188 1.5 ryo { RA_UART_BASE, RA_IRQ_UARTF, RST_UART, GPIOMODE_UARTF0 }, 189 1.5 ryo { RA_UART_LITE_BASE, RA_IRQ_UARTL, RST_UARTL, GPIOMODE_UARTL } 190 1.5 ryo #endif 191 1.5 ryo }; 192 1.5 ryo 193 1.5 ryo static inline int 194 1.5 ryo ra_uart2irq(bus_addr_t addr) 195 1.5 ryo { 196 1.5 ryo int i; 197 1.5 ryo for (i = 0; __arraycount(ralink_uart_maps); i++) 198 1.5 ryo if (ralink_uart_maps[i].addr == addr) 199 1.5 ryo return ralink_uart_maps[i].irq; 200 1.5 ryo return -1; 201 1.5 ryo } 202 1.5 ryo 203 1.5 ryo static inline uint32_t 204 1.5 ryo ra_uart2rst(bus_addr_t addr) 205 1.5 ryo { 206 1.5 ryo int i; 207 1.5 ryo for (i = 0; __arraycount(ralink_uart_maps); i++) 208 1.5 ryo if (ralink_uart_maps[i].addr == addr) 209 1.5 ryo return ralink_uart_maps[i].rst; 210 1.5 ryo return 0; 211 1.5 ryo } 212 1.5 ryo 213 1.5 ryo static inline uint32_t 214 1.5 ryo ra_uart2gpiomode(bus_addr_t addr) 215 1.5 ryo { 216 1.5 ryo int i; 217 1.5 ryo for (i = 0; __arraycount(ralink_uart_maps); i++) 218 1.5 ryo if (ralink_uart_maps[i].addr == addr) 219 1.5 ryo return ralink_uart_maps[i].gpiomode; 220 1.5 ryo return 0; 221 1.5 ryo } 222 1.5 ryo 223 1.2 matt static inline uint32_t 224 1.2 matt sysctl_read(const u_int offset) 225 1.2 matt { 226 1.2 matt return *RA_IOREG_VADDR(RA_SYSCTL_BASE, offset); 227 1.2 matt } 228 1.2 matt 229 1.2 matt static inline void 230 1.2 matt sysctl_write(const u_int offset, uint32_t val) 231 1.2 matt { 232 1.2 matt *RA_IOREG_VADDR(RA_SYSCTL_BASE, offset) = val; 233 1.2 matt } 234 1.2 matt 235 1.2 matt static inline uint32_t 236 1.2 matt uart_read(const u_int offset) 237 1.2 matt { 238 1.5 ryo return *RA_IOREG_VADDR(RALINK_CONADDR, offset); 239 1.2 matt } 240 1.2 matt 241 1.2 matt static inline void 242 1.2 matt uart_write(const u_int offset, const uint32_t val) 243 1.2 matt { 244 1.5 ryo *RA_IOREG_VADDR(RALINK_CONADDR, offset) = val; 245 1.2 matt } 246 1.2 matt 247 1.3 matt #ifdef RALINK_CONSOLE_EARLY 248 1.3 matt static int 249 1.3 matt ralink_cngetc(dev_t dv) 250 1.3 matt { 251 1.5 ryo if ((uart_read(RA_UART_LSR) & LSR_RXRDY) == 0) 252 1.3 matt return -1; 253 1.3 matt 254 1.3 matt return uart_read(RA_UART_RBR) & 0xff; 255 1.3 matt } 256 1.3 matt 257 1.3 matt static void 258 1.3 matt ralink_cnputc(dev_t dv, int c) 259 1.3 matt { 260 1.3 matt int timo = 150000; 261 1.3 matt 262 1.5 ryo while ((uart_read(RA_UART_LSR) & LSR_TXRDY) == 0 && --timo > 0) 263 1.3 matt ; 264 1.3 matt 265 1.3 matt uart_write(RA_UART_TBR, c); 266 1.3 matt __asm __volatile("sync"); 267 1.3 matt 268 1.3 matt timo = 150000; 269 1.5 ryo while ((uart_read(RA_UART_LSR) & LSR_TSRE) == 0 && --timo > 0) 270 1.3 matt ; 271 1.3 matt } 272 1.3 matt 273 1.3 matt static struct consdev ralink_earlycons = { 274 1.3 matt .cn_putc = ralink_cnputc, 275 1.3 matt .cn_getc = ralink_cngetc, 276 1.3 matt .cn_pollc = nullcnpollc, 277 1.3 matt }; 278 1.3 matt 279 1.3 matt void 280 1.3 matt ralink_console_early(void) 281 1.3 matt { 282 1.3 matt cn_tab = &ralink_earlycons; 283 1.3 matt } 284 1.4 matt #endif /* RALINK_CONSOLE_EARLY */ 285 1.3 matt 286 1.2 matt 287 1.2 matt int 288 1.2 matt ralink_com_match(device_t parent, cfdata_t cf, void *aux) 289 1.2 matt { 290 1.5 ryo const struct mainbus_attach_args *ma; 291 1.5 ryo bus_addr_t addr; 292 1.5 ryo 293 1.5 ryo ma = aux; 294 1.5 ryo addr = ma->ma_addr; 295 1.5 ryo if (addr == MAINBUSCF_ADDR_DEFAULT) 296 1.5 ryo addr = RA_UART_LITE_BASE; 297 1.5 ryo 298 1.5 ryo if (ra_uart2irq(addr) < 0) 299 1.5 ryo return 0; 300 1.5 ryo 301 1.2 matt if (cn_tab == NULL || cn_tab->cn_pri < CN_NORMAL) { 302 1.2 matt printf("NULL console set, don't install ourselves " 303 1.5 ryo "(of course this shouldn't print)"); 304 1.2 matt return 0; 305 1.2 matt } 306 1.2 matt 307 1.2 matt /* 308 1.2 matt * If we got this far, assume we want to install it as the console. 309 1.5 ryo * No need to probe. Future possibilities include checking to see if it 310 1.2 matt * is console or KGDB but now it is our only console method if we aren't 311 1.2 matt * forcing a null console 312 1.2 matt */ 313 1.2 matt return 1; 314 1.2 matt } 315 1.2 matt 316 1.2 matt void 317 1.2 matt ralink_com_attach(device_t parent, device_t self, void *aux) 318 1.2 matt { 319 1.2 matt const struct mainbus_attach_args *ma = aux; 320 1.2 matt struct ralink_com_softc * const rtsc = device_private(self); 321 1.2 matt struct com_softc * const sc = &rtsc->sc_com; 322 1.2 matt bus_space_handle_t ioh; 323 1.2 matt int error; 324 1.2 matt 325 1.5 ryo /* opt addr and irq */ 326 1.5 ryo rtsc->sc_addr = ma->ma_addr; 327 1.5 ryo if (rtsc->sc_addr == MAINBUSCF_ADDR_DEFAULT) 328 1.5 ryo rtsc->sc_addr = RA_UART_LITE_BASE; 329 1.5 ryo rtsc->sc_irq = ra_uart2irq(rtsc->sc_addr); 330 1.5 ryo 331 1.5 ryo if ((error = bus_space_map(ma->ma_memt, rtsc->sc_addr, 332 1.5 ryo RA_UART_SIZE, 0, &ioh)) != 0) { 333 1.2 matt aprint_error(": can't map registers, error=%d\n", error); 334 1.2 matt return; 335 1.2 matt } 336 1.2 matt 337 1.2 matt sc->sc_dev = self; 338 1.2 matt sc->sc_frequency = RA_UART_FREQ; 339 1.5 ryo #if defined(MT7628) 340 1.5 ryo sc->sc_type = COM_TYPE_NORMAL; 341 1.5 ryo #else 342 1.2 matt sc->sc_type = COM_TYPE_AU1x00; 343 1.5 ryo #endif 344 1.2 matt sc->enabled = 1; 345 1.2 matt 346 1.5 ryo /* reset hardware if not a console */ 347 1.5 ryo if (rtsc->sc_addr != RALINK_CONADDR) { 348 1.5 ryo uint32_t r; 349 1.5 ryo 350 1.5 ryo /* reset */ 351 1.5 ryo r = sysctl_read(RA_SYSCTL_RST); 352 1.5 ryo r |= ra_uart2rst(rtsc->sc_addr); 353 1.5 ryo sysctl_write(RA_SYSCTL_RST, r); 354 1.5 ryo r ^= ra_uart2rst(rtsc->sc_addr); 355 1.5 ryo sysctl_write(RA_SYSCTL_RST, r); 356 1.5 ryo 357 1.5 ryo /* make sure we are in UART mode */ 358 1.5 ryo r = sysctl_read(RA_SYSCTL_GPIOMODE); 359 1.5 ryo r &= ra_uart2gpiomode(rtsc->sc_addr); 360 1.5 ryo r |= __SHIFTIN(0, ra_uart2gpiomode(rtsc->sc_addr)); 361 1.5 ryo sysctl_write(RA_SYSCTL_GPIOMODE, r); 362 1.5 ryo } 363 1.5 ryo 364 1.6 thorpej ralink_com_init_regs(&sc->sc_regs, ma->ma_memt, ioh, rtsc->sc_addr); 365 1.2 matt 366 1.5 ryo rtsc->sc_ih = ra_intr_establish(rtsc->sc_irq, comintr, sc, 1); 367 1.2 matt com_attach_subr(sc); 368 1.2 matt } 369 1.2 matt 370 1.9 thorpej static const bus_size_t ralink_com_regmap[COM_REGMAP_NENTRIES] = { 371 1.8 thorpej [COM_REG_RXDATA] = RA_UART_RBR, 372 1.8 thorpej [COM_REG_TXDATA] = RA_UART_TBR, 373 1.8 thorpej [COM_REG_DLBL] = RA_UART_DLL, 374 1.8 thorpej #if defined(MT7628) 375 1.8 thorpej [COM_REG_DLBH] = RA_UART_DLM, 376 1.8 thorpej #endif 377 1.8 thorpej [COM_REG_IER] = RA_UART_IER, 378 1.8 thorpej [COM_REG_IIR] = RA_UART_IIR, 379 1.8 thorpej [COM_REG_FIFO] = RA_UART_FCR, 380 1.8 thorpej [COM_REG_TCR] = RA_UART_FCR, 381 1.8 thorpej [COM_REG_LCR] = RA_UART_LCR, 382 1.8 thorpej [COM_REG_MCR] = RA_UART_MCR, 383 1.8 thorpej [COM_REG_LSR] = RA_UART_LSR, 384 1.8 thorpej [COM_REG_MSR] = RA_UART_MSR, 385 1.8 thorpej }; 386 1.8 thorpej 387 1.2 matt static void 388 1.6 thorpej ralink_com_init_regs(struct com_regs *regsp, bus_space_tag_t st, 389 1.6 thorpej bus_space_handle_t sh, bus_addr_t addr) 390 1.2 matt { 391 1.6 thorpej 392 1.6 thorpej com_init_regs(regsp, st, sh, addr); 393 1.6 thorpej 394 1.8 thorpej memcpy(regsp->cr_map, ralink_com_regmap, sizeof(regsp->cr_map)); 395 1.6 thorpej regsp->cr_nports = 32; 396 1.2 matt } 397 1.2 matt 398 1.2 matt void 399 1.5 ryo ralink_com_early(int silent) 400 1.2 matt { 401 1.2 matt struct com_regs regs; 402 1.6 thorpej bus_space_handle_t bsh; 403 1.2 matt uint32_t r; 404 1.2 matt int error; 405 1.2 matt 406 1.2 matt /* reset */ 407 1.2 matt r = sysctl_read(RA_SYSCTL_RST); 408 1.5 ryo r |= ra_uart2rst(RALINK_CONADDR); 409 1.2 matt sysctl_write(RA_SYSCTL_RST, r); 410 1.5 ryo r ^= ra_uart2rst(RALINK_CONADDR); 411 1.2 matt sysctl_write(RA_SYSCTL_RST, r); 412 1.2 matt 413 1.2 matt if (silent) { 414 1.2 matt /* 415 1.2 matt * put us in PIO mode, 416 1.2 matt * effectively tri-stating the UARTL block 417 1.2 matt */ 418 1.2 matt r = sysctl_read(RA_SYSCTL_GPIOMODE); 419 1.5 ryo r &= ra_uart2gpiomode(RALINK_CONADDR); 420 1.5 ryo r |= __SHIFTIN(1, ra_uart2gpiomode(RALINK_CONADDR)); 421 1.2 matt sysctl_write(RA_SYSCTL_GPIOMODE, r); 422 1.2 matt } else { 423 1.2 matt /* make sure we are in UART mode */ 424 1.2 matt r = sysctl_read(RA_SYSCTL_GPIOMODE); 425 1.5 ryo r &= ra_uart2gpiomode(RALINK_CONADDR); 426 1.5 ryo r |= __SHIFTIN(0, ra_uart2gpiomode(RALINK_CONADDR)); 427 1.2 matt sysctl_write(RA_SYSCTL_GPIOMODE, r); 428 1.2 matt } 429 1.2 matt 430 1.2 matt uart_write(RA_UART_IER, 0); /* disable interrupts */ 431 1.2 matt uart_write(RA_UART_FCR, 0); /* disable fifos */ 432 1.2 matt 433 1.2 matt /* set baud rate */ 434 1.2 matt uart_write(RA_UART_LCR, 435 1.5 ryo UART_LCR_WLS0 | UART_LCR_WLS1 | UART_LCR_DLAB); 436 1.2 matt uart_write(RA_UART_DLL, 437 1.5 ryo (RA_UART_FREQ / RA_SERIAL_CLKDIV / RA_BAUDRATE) & 0xffff); 438 1.5 ryo #if defined(MT7628) 439 1.5 ryo uart_write(RA_UART_DLM, 440 1.5 ryo ((RA_UART_FREQ / RA_SERIAL_CLKDIV / RA_BAUDRATE) & 0xffff) >> 8); 441 1.5 ryo #endif 442 1.5 ryo uart_write(RA_UART_LCR, UART_LCR_WLS0 | UART_LCR_WLS1); 443 1.2 matt 444 1.6 thorpej if ((error = bus_space_map(&ra_bus_memt, RALINK_CONADDR, 445 1.6 thorpej 32, 0, &bsh)) != 0) { 446 1.2 matt return; 447 1.2 matt } 448 1.6 thorpej ralink_com_init_regs(®s, &ra_bus_memt, bsh, RALINK_CONADDR); 449 1.2 matt 450 1.5 ryo #if defined(MT7628) 451 1.5 ryo comcnattach1(®s, RA_BAUDRATE, RA_UART_FREQ, 452 1.5 ryo COM_TYPE_NORMAL, CONMODE); 453 1.5 ryo #else 454 1.2 matt /* Ralink UART has a 16-bit rate latch (like the AU1x00) */ 455 1.2 matt comcnattach1(®s, RA_BAUDRATE, RA_UART_FREQ, 456 1.5 ryo COM_TYPE_AU1x00, CONMODE); 457 1.5 ryo #endif 458 1.2 matt } 459