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