Home | History | Annotate | Line # | Download | only in ralink
ralink_com.c revision 1.3.10.1
      1  1.3.10.1  rmind /*	$NetBSD: ralink_com.c,v 1.3.10.1 2014/05/18 17:45:17 rmind 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.3.10.1  rmind __KERNEL_RCSID(0, "$NetBSD: ralink_com.c,v 1.3.10.1 2014/05/18 17:45:17 rmind Exp $");
    134       1.2   matt 
    135       1.2   matt #include <sys/param.h>
    136       1.2   matt #include <sys/bus.h>
    137       1.2   matt #include <sys/device.h>
    138       1.2   matt #include <sys/kernel.h>
    139       1.2   matt #include <sys/systm.h>
    140       1.2   matt #include <sys/tty.h>
    141       1.2   matt #include <sys/termios.h>
    142       1.2   matt #include <sys/ttydefaults.h>
    143       1.2   matt 
    144       1.2   matt #include <dev/cons.h>
    145       1.2   matt #include <dev/ic/comreg.h>
    146       1.2   matt #include <dev/ic/comvar.h>
    147       1.2   matt 
    148       1.2   matt #include <mips/cpuregs.h>
    149       1.2   matt 
    150       1.2   matt #include <mips/ralink/ralink_reg.h>
    151       1.2   matt #include <mips/ralink/ralink_var.h>
    152       1.2   matt 
    153       1.2   matt #include "opt_com.h"
    154       1.2   matt 
    155       1.2   matt struct ralink_com_softc {
    156       1.2   matt     struct com_softc sc_com;
    157       1.2   matt     void *           sc_ih;
    158       1.2   matt };
    159       1.2   matt 
    160       1.2   matt static int ralink_com_match(device_t, cfdata_t , void *);
    161       1.2   matt static void ralink_com_attach(device_t, device_t, void *);
    162       1.2   matt static void ralink_com_initmap(struct com_regs *regsp);
    163       1.2   matt 
    164       1.2   matt CFATTACH_DECL_NEW(ralink_com, sizeof(struct ralink_com_softc),
    165       1.2   matt 	ralink_com_match, ralink_com_attach, NULL, NULL);
    166       1.2   matt 
    167       1.2   matt #define CONMODE	\
    168       1.2   matt 	((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
    169       1.2   matt 
    170       1.2   matt #ifndef COM_REGMAP
    171       1.2   matt #error  COM_REGMAP not defined!
    172       1.2   matt #endif
    173       1.2   matt 
    174       1.2   matt static inline uint32_t
    175       1.2   matt sysctl_read(const u_int offset)
    176       1.2   matt {
    177       1.2   matt 	return *RA_IOREG_VADDR(RA_SYSCTL_BASE, offset);
    178       1.2   matt }
    179       1.2   matt 
    180       1.2   matt static inline void
    181       1.2   matt sysctl_write(const u_int offset, uint32_t val)
    182       1.2   matt {
    183       1.2   matt 	*RA_IOREG_VADDR(RA_SYSCTL_BASE, offset) = val;
    184       1.2   matt }
    185       1.2   matt 
    186       1.2   matt static inline uint32_t
    187       1.2   matt uart_read(const u_int offset)
    188       1.2   matt {
    189       1.2   matt 	return *RA_IOREG_VADDR(RA_UART_LITE_BASE, offset);
    190       1.2   matt }
    191       1.2   matt 
    192       1.2   matt static inline void
    193       1.2   matt uart_write(const u_int offset, const uint32_t val)
    194       1.2   matt {
    195       1.2   matt 	*RA_IOREG_VADDR(RA_UART_LITE_BASE, offset) = val;
    196       1.2   matt }
    197       1.2   matt 
    198       1.3   matt #ifdef RALINK_CONSOLE_EARLY
    199       1.3   matt static int
    200       1.3   matt ralink_cngetc(dev_t dv)
    201       1.3   matt {
    202       1.3   matt         if ((uart_read(RA_UART_LSR) & LSR_RXRDY) == 0)
    203       1.3   matt 		return -1;
    204       1.3   matt 
    205       1.3   matt 	return uart_read(RA_UART_RBR) & 0xff;
    206       1.3   matt }
    207       1.3   matt 
    208       1.3   matt static void
    209       1.3   matt ralink_cnputc(dev_t dv, int c)
    210       1.3   matt {
    211       1.3   matt 	int timo = 150000;
    212       1.3   matt 
    213       1.3   matt         while ((uart_read(RA_UART_LSR) & LSR_TXRDY) == 0 && --timo > 0)
    214       1.3   matt 		;
    215       1.3   matt 
    216       1.3   matt 	uart_write(RA_UART_TBR, c);
    217       1.3   matt 	__asm __volatile("sync");
    218       1.3   matt 
    219       1.3   matt 	timo = 150000;
    220       1.3   matt         while ((uart_read(RA_UART_LSR) & LSR_TSRE) == 0 && --timo > 0)
    221       1.3   matt 		;
    222       1.3   matt }
    223       1.3   matt 
    224       1.3   matt static struct consdev ralink_earlycons = {
    225       1.3   matt 	.cn_putc = ralink_cnputc,
    226       1.3   matt 	.cn_getc = ralink_cngetc,
    227       1.3   matt 	.cn_pollc = nullcnpollc,
    228       1.3   matt };
    229       1.3   matt 
    230       1.3   matt void
    231       1.3   matt ralink_console_early(void)
    232       1.3   matt {
    233       1.3   matt 	cn_tab = &ralink_earlycons;
    234       1.3   matt }
    235  1.3.10.1  rmind #endif /* RALINK_CONSOLE_EARLY */
    236       1.3   matt 
    237       1.2   matt 
    238       1.2   matt int
    239       1.2   matt ralink_com_match(device_t parent, cfdata_t cf, void *aux)
    240       1.2   matt {
    241       1.2   matt 	if (cn_tab == NULL || cn_tab->cn_pri < CN_NORMAL) {
    242       1.2   matt 		printf("NULL console set, don't install ourselves "
    243       1.2   matt 			"(of course this shouldn't print)");
    244       1.2   matt 		return 0;
    245       1.2   matt 	}
    246       1.2   matt 
    247       1.2   matt 	/*
    248       1.2   matt 	 * If we got this far, assume we want to install it as the console.
    249       1.2   matt 	 * No need to probe. Future possibilities include checking to see if it
    250       1.2   matt 	 * is console or KGDB but now it is our only console method if we aren't
    251       1.2   matt 	 * forcing a null console
    252       1.2   matt 	 */
    253       1.2   matt 	return 1;
    254       1.2   matt }
    255       1.2   matt 
    256       1.2   matt void
    257       1.2   matt ralink_com_attach(device_t parent, device_t self, void *aux)
    258       1.2   matt {
    259       1.2   matt 	const struct mainbus_attach_args *ma = aux;
    260       1.2   matt 	struct ralink_com_softc * const rtsc = device_private(self);
    261       1.2   matt 	struct com_softc * const sc = &rtsc->sc_com;
    262       1.2   matt 	bus_space_handle_t ioh;
    263       1.2   matt 	int error;
    264       1.2   matt 
    265       1.2   matt 	if ((error = bus_space_map(ma->ma_memt, RA_UART_LITE_BASE,
    266       1.2   matt 	    0x1000, 0, &ioh)) != 0) {
    267       1.2   matt 		aprint_error(": can't map registers, error=%d\n", error);
    268       1.2   matt 		return;
    269       1.2   matt 	}
    270       1.2   matt 
    271       1.2   matt 	COM_INIT_REGS(sc->sc_regs, ma->ma_memt, ioh, RA_UART_LITE_BASE);
    272       1.2   matt 	sc->sc_dev = self;
    273       1.2   matt 	sc->sc_frequency = RA_UART_FREQ;
    274       1.2   matt 	sc->sc_regs.cr_nports = 0x1000;
    275       1.2   matt 	sc->sc_type = COM_TYPE_AU1x00;
    276       1.2   matt 	sc->enabled = 1;
    277       1.2   matt 
    278       1.2   matt 	ralink_com_initmap(&sc->sc_regs);
    279       1.2   matt 
    280       1.2   matt 	rtsc->sc_ih = ra_intr_establish(RA_IRQ_UARTL, comintr, sc, 1);
    281       1.2   matt 
    282       1.2   matt 	com_attach_subr(sc);
    283       1.2   matt }
    284       1.2   matt 
    285       1.2   matt static void
    286       1.2   matt ralink_com_initmap(struct com_regs *regsp)
    287       1.2   matt {
    288       1.2   matt 	regsp->cr_map[COM_REG_RXDATA] = RA_UART_RBR;
    289       1.2   matt 	regsp->cr_map[COM_REG_TXDATA] = RA_UART_TBR;
    290       1.2   matt 	regsp->cr_map[COM_REG_DLBL]   = RA_UART_DLL;
    291       1.2   matt 	regsp->cr_map[COM_REG_IER]    = RA_UART_IER;
    292       1.2   matt 	regsp->cr_map[COM_REG_IIR]    = RA_UART_IIR;
    293       1.2   matt 	regsp->cr_map[COM_REG_FIFO]   = RA_UART_FCR;
    294       1.2   matt 	regsp->cr_map[COM_REG_LCR]    = RA_UART_LCR;
    295       1.2   matt 	regsp->cr_map[COM_REG_MCR]    = RA_UART_MCR;
    296       1.2   matt 	regsp->cr_map[COM_REG_LSR]    = RA_UART_LSR;
    297       1.2   matt 	regsp->cr_map[COM_REG_MSR]    = RA_UART_MSR;
    298       1.2   matt }
    299       1.2   matt 
    300       1.2   matt void
    301       1.2   matt ralink_com_early(int silent)
    302       1.2   matt {
    303       1.2   matt 	struct com_regs regs;
    304       1.2   matt 	uint32_t r;
    305       1.2   matt 	int error;
    306       1.2   matt 
    307       1.2   matt 	/* reset */
    308       1.2   matt 	r = sysctl_read(RA_SYSCTL_RST);
    309       1.2   matt 	r |= RST_UARTL;
    310       1.2   matt 	sysctl_write(RA_SYSCTL_RST, r);
    311       1.2   matt 	r ^= RST_UARTL;
    312       1.2   matt 	sysctl_write(RA_SYSCTL_RST, r);
    313       1.2   matt 
    314       1.2   matt 	if (silent) {
    315       1.2   matt 		/*
    316       1.2   matt 		 * put us in PIO mode,
    317       1.2   matt 		 * effectively tri-stating the UARTL block
    318       1.2   matt 		 */
    319       1.2   matt 		r = sysctl_read(RA_SYSCTL_GPIOMODE);
    320       1.2   matt 		r |= GPIOMODE_UARTL;
    321       1.2   matt 		sysctl_write(RA_SYSCTL_GPIOMODE, r);
    322       1.2   matt 	} else {
    323       1.2   matt 		/* make sure we are in UART mode */
    324       1.2   matt 		r = sysctl_read(RA_SYSCTL_GPIOMODE);
    325       1.2   matt 		r &= ~GPIOMODE_UARTL;
    326       1.2   matt 		sysctl_write(RA_SYSCTL_GPIOMODE, r);
    327       1.2   matt 	}
    328       1.2   matt 
    329       1.2   matt 	uart_write(RA_UART_IER, 0);		/* disable interrupts */
    330       1.2   matt 	uart_write(RA_UART_FCR, 0);		/* disable fifos */
    331       1.2   matt 
    332       1.2   matt 	/* set baud rate */
    333       1.2   matt 	uart_write(RA_UART_LCR,
    334       1.2   matt 		UART_LCR_WLS0 | UART_LCR_WLS1 | UART_LCR_DLAB);
    335       1.2   matt 	uart_write(RA_UART_DLL,
    336       1.2   matt 		(RA_UART_FREQ / RA_SERIAL_CLKDIV / RA_BAUDRATE)
    337       1.2   matt 			& 0xffff);
    338       1.2   matt 	uart_write(RA_UART_LCR,
    339       1.2   matt 		UART_LCR_WLS0 | UART_LCR_WLS1);
    340       1.2   matt 
    341       1.2   matt 	regs.cr_iot = &ra_bus_memt;
    342       1.2   matt 	regs.cr_iobase = RA_UART_LITE_BASE;
    343       1.2   matt 	regs.cr_nports = 0x1000;
    344       1.2   matt 	ralink_com_initmap(&regs);
    345       1.2   matt 
    346       1.2   matt 	if ((error = bus_space_map(regs.cr_iot, regs.cr_iobase, regs.cr_nports,
    347       1.2   matt 	    0, &regs.cr_ioh)) != 0) {
    348       1.2   matt 		return;
    349       1.2   matt 	}
    350       1.2   matt 
    351       1.2   matt 	/* Ralink UART has a 16-bit rate latch (like the AU1x00) */
    352       1.2   matt 	comcnattach1(&regs, RA_BAUDRATE, RA_UART_FREQ,
    353       1.2   matt 		COM_TYPE_AU1x00, CONMODE);
    354       1.2   matt }
    355