Home | History | Annotate | Line # | Download | only in rmi
rmixl_com.c revision 1.5
      1  1.5  dyoung /* $Id: rmixl_com.c,v 1.5 2011/07/01 19:01:30 dyoung Exp $ */
      2  1.2    matt /*-
      3  1.2    matt  * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
      4  1.2    matt  * Copyright (c) 2006 Garrett D'Amore.
      5  1.2    matt  * All rights reserved.
      6  1.2    matt  *
      7  1.2    matt  * Portions of this code were written by Garrett D'Amore for the
      8  1.2    matt  * Champaign-Urbana Community Wireless Network Project.
      9  1.2    matt  *
     10  1.2    matt  * Redistribution and use in source and binary forms, with or
     11  1.2    matt  * without modification, are permitted provided that the following
     12  1.2    matt  * conditions are met:
     13  1.2    matt  * 1. Redistributions of source code must retain the above copyright
     14  1.2    matt  *    notice, this list of conditions and the following disclaimer.
     15  1.2    matt  * 2. Redistributions in binary form must reproduce the above
     16  1.2    matt  *    copyright notice, this list of conditions and the following
     17  1.2    matt  *    disclaimer in the documentation and/or other materials provided
     18  1.2    matt  *    with the distribution.
     19  1.2    matt  * 3. All advertising materials mentioning features or use of this
     20  1.2    matt  *    software must display the following acknowledgements:
     21  1.2    matt  *      This product includes software developed by the Urbana-Champaign
     22  1.2    matt  *      Independent Media Center.
     23  1.2    matt  *	This product includes software developed by Garrett D'Amore.
     24  1.2    matt  * 4. Urbana-Champaign Independent Media Center's name and Garrett
     25  1.2    matt  *    D'Amore's name may not be used to endorse or promote products
     26  1.2    matt  *    derived from this software without specific prior written permission.
     27  1.2    matt  *
     28  1.2    matt  * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
     29  1.2    matt  * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
     30  1.2    matt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     31  1.2    matt  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     32  1.2    matt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
     33  1.2    matt  * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
     34  1.2    matt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     35  1.2    matt  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     36  1.2    matt  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     37  1.2    matt  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     38  1.2    matt  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     39  1.2    matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     40  1.2    matt  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     41  1.2    matt  */
     42  1.2    matt 
     43  1.2    matt /*-
     44  1.2    matt  * Copyright (c) 1998 The NetBSD Foundation, Inc.
     45  1.2    matt  * All rights reserved.
     46  1.2    matt  *
     47  1.2    matt  * This code is derived from software contributed to The NetBSD Foundation
     48  1.2    matt  * by Charles M. Hannum.
     49  1.2    matt  *
     50  1.2    matt  * Redistribution and use in source and binary forms, with or without
     51  1.2    matt  * modification, are permitted provided that the following conditions
     52  1.2    matt  * are met:
     53  1.2    matt  * 1. Redistributions of source code must retain the above copyright
     54  1.2    matt  *    notice, this list of conditions and the following disclaimer.
     55  1.2    matt  * 2. Redistributions in binary form must reproduce the above copyright
     56  1.2    matt  *    notice, this list of conditions and the following disclaimer in the
     57  1.2    matt  *    documentation and/or other materials provided with the distribution.
     58  1.2    matt  *
     59  1.2    matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     60  1.2    matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     61  1.2    matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     62  1.2    matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     63  1.2    matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     64  1.2    matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     65  1.2    matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     66  1.2    matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     67  1.2    matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     68  1.2    matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     69  1.2    matt  * POSSIBILITY OF SUCH DAMAGE.
     70  1.2    matt  */
     71  1.2    matt 
     72  1.2    matt /*-
     73  1.2    matt  * Copyright (c) 1991 The Regents of the University of California.
     74  1.2    matt  * All rights reserved.
     75  1.2    matt  *
     76  1.2    matt  * Redistribution and use in source and binary forms, with or without
     77  1.2    matt  * modification, are permitted provided that the following conditions
     78  1.2    matt  * are met:
     79  1.2    matt  * 1. Redistributions of source code must retain the above copyright
     80  1.2    matt  *    notice, this list of conditions and the following disclaimer.
     81  1.2    matt  * 2. Redistributions in binary form must reproduce the above copyright
     82  1.2    matt  *    notice, this list of conditions and the following disclaimer in the
     83  1.2    matt  *    documentation and/or other materials provided with the distribution.
     84  1.2    matt  * 3. Neither the name of the University nor the names of its contributors
     85  1.2    matt  *    may be used to endorse or promote products derived from this software
     86  1.2    matt  *    without specific prior written permission.
     87  1.2    matt  *
     88  1.2    matt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     89  1.2    matt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     90  1.2    matt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     91  1.2    matt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     92  1.2    matt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     93  1.2    matt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     94  1.2    matt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     95  1.2    matt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     96  1.2    matt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     97  1.2    matt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     98  1.2    matt  * SUCH DAMAGE.
     99  1.2    matt  *
    100  1.2    matt  *	@(#)com.c	7.5 (Berkeley) 5/16/91
    101  1.2    matt  */
    102  1.2    matt 
    103  1.2    matt #include <sys/cdefs.h>
    104  1.5  dyoung __KERNEL_RCSID(0, "$NetBSD: rmixl_com.c,v 1.5 2011/07/01 19:01:30 dyoung Exp $");
    105  1.2    matt 
    106  1.2    matt #include <sys/param.h>
    107  1.2    matt #include <sys/systm.h>
    108  1.2    matt #include <sys/device.h>
    109  1.2    matt #include <sys/kernel.h>
    110  1.2    matt #include <sys/termios.h>
    111  1.2    matt #include <sys/ttydefaults.h>
    112  1.2    matt #include <sys/types.h>
    113  1.2    matt 
    114  1.5  dyoung #include <sys/bus.h>
    115  1.2    matt 
    116  1.2    matt #include <dev/cons.h>
    117  1.2    matt #include <dev/ic/comreg.h>
    118  1.2    matt #include <dev/ic/comvar.h>
    119  1.2    matt 
    120  1.2    matt #include <mips/cpuregs.h>
    121  1.2    matt 
    122  1.3    matt #include <mips/rmi/rmixlvar.h>
    123  1.3    matt #include <mips/rmi/rmixl_intr.h>
    124  1.2    matt #include <mips/rmi/rmixl_obiovar.h>
    125  1.2    matt #include <mips/rmi/rmixl_comvar.h>
    126  1.2    matt 
    127  1.2    matt #include "opt_com.h"
    128  1.2    matt 
    129  1.3    matt /* span of UART regs in bytes */
    130  1.3    matt #define RMIXL_IO_DEV_UART_SIZE	(COM_NPORTS * sizeof(uint32_t))
    131  1.3    matt 
    132  1.3    matt #define RMIXL_COM_INIT_REGS(regs, bst, ioh, addr) 		\
    133  1.3    matt 	do {							\
    134  1.3    matt 		memset(&regs, 0, sizeof(regs));			\
    135  1.3    matt 		COM_INIT_REGS(regs, bst, ioh, addr);		\
    136  1.3    matt 		regs.cr_nports = RMIXL_IO_DEV_UART_SIZE;	\
    137  1.3    matt 		rmixl_com_initmap(&regs);			\
    138  1.3    matt 	} while (0)
    139  1.3    matt 
    140  1.3    matt 
    141  1.2    matt struct rmixl_com_softc {
    142  1.2    matt 	struct com_softc sc_com;
    143  1.2    matt };
    144  1.2    matt 
    145  1.2    matt static void rmixl_com_initmap(struct com_regs *);
    146  1.2    matt static int rmixl_com_match(device_t, cfdata_t , void *);
    147  1.2    matt static void rmixl_com_attach(device_t, device_t, void *);
    148  1.2    matt 
    149  1.3    matt CFATTACH_DECL_NEW(com_rmixl, sizeof(struct rmixl_com_softc),
    150  1.2    matt     rmixl_com_match, rmixl_com_attach, NULL, NULL);
    151  1.2    matt 
    152  1.2    matt #ifndef	COM_REGMAP
    153  1.2    matt #error	COM_REGMAP not defined!
    154  1.2    matt #endif
    155  1.2    matt 
    156  1.3    matt volatile int32_t *com0addr = (int32_t *)
    157  1.2    matt 	MIPS_PHYS_TO_KSEG1(RMIXL_IO_DEV_PBASE + RMIXL_IO_DEV_UART_1);
    158  1.2    matt 
    159  1.3    matt extern int comcnfreq;
    160  1.3    matt extern int comcnspeed;
    161  1.3    matt 
    162  1.2    matt void
    163  1.2    matt rmixl_putchar_init(uint64_t io_pbase)
    164  1.2    matt {
    165  1.2    matt 	int rate;
    166  1.2    matt 	extern int comspeed(long, long, int);
    167  1.2    matt 
    168  1.2    matt 	com0addr = (uint32_t *)
    169  1.2    matt 		MIPS_PHYS_TO_KSEG1(io_pbase + RMIXL_IO_DEV_UART_1);
    170  1.2    matt 
    171  1.3    matt 	if (comcnfreq != -1) {
    172  1.3    matt 		rate = comspeed(comcnspeed, comcnfreq, COM_TYPE_NORMAL);
    173  1.2    matt 		if (rate < 0)
    174  1.2    matt 			return;					/* XXX */
    175  1.2    matt 
    176  1.2    matt 		com0addr[com_ier] = 0;
    177  1.2    matt 		com0addr[com_lctl] = htobe32(LCR_DLAB);
    178  1.2    matt 		com0addr[com_dlbl] = htobe32(rate & 0xff);
    179  1.2    matt 		com0addr[com_dlbh] = htobe32(rate >> 8);
    180  1.2    matt 		com0addr[com_lctl] = htobe32(LCR_8BITS);	/* XXX */
    181  1.2    matt 		com0addr[com_mcr]  = htobe32(MCR_DTR|MCR_RTS);
    182  1.2    matt 		com0addr[com_fifo] = htobe32(
    183  1.2    matt 			FIFO_ENABLE|FIFO_RCV_RST|FIFO_XMT_RST|FIFO_TRIGGER_1);
    184  1.2    matt 	}
    185  1.2    matt }
    186  1.2    matt 
    187  1.2    matt 
    188  1.2    matt void
    189  1.2    matt rmixl_putchar(char c)
    190  1.2    matt {
    191  1.2    matt 	int timo = 150000;
    192  1.2    matt 
    193  1.2    matt 	while ((be32toh(com0addr[com_lsr]) & LSR_TXRDY) == 0)
    194  1.2    matt 		if (--timo == 0)
    195  1.2    matt 			break;
    196  1.2    matt 
    197  1.2    matt 	com0addr[com_data] = htobe32((uint32_t)c);
    198  1.2    matt 
    199  1.2    matt 	while ((be32toh(com0addr[com_lsr]) & LSR_TSRE) == 0)
    200  1.2    matt 		if (--timo == 0)
    201  1.2    matt 			break;
    202  1.2    matt }
    203  1.2    matt 
    204  1.2    matt void
    205  1.2    matt rmixl_puts(const char *restrict s)
    206  1.2    matt {
    207  1.2    matt 	char c;
    208  1.2    matt 
    209  1.2    matt 	while ((c = *s++) != 0)
    210  1.2    matt 		rmixl_putchar(c);
    211  1.2    matt }
    212  1.2    matt 
    213  1.2    matt static char hexc[] = "0123456789abcdef";
    214  1.2    matt 
    215  1.2    matt #define RMIXL_PUTHEX 						\
    216  1.2    matt 	u_int shift = sizeof(val) * 8;				\
    217  1.2    matt 	rmixl_putchar('0');					\
    218  1.2    matt 	rmixl_putchar('x');					\
    219  1.2    matt 	do {							\
    220  1.2    matt 		shift -= 4;					\
    221  1.2    matt 		rmixl_putchar(hexc[(val >> shift) & 0xf]);	\
    222  1.2    matt 	} while(shift != 0)
    223  1.2    matt 
    224  1.2    matt void
    225  1.2    matt rmixl_puthex32(uint32_t val)
    226  1.2    matt {
    227  1.2    matt 	RMIXL_PUTHEX;
    228  1.2    matt }
    229  1.2    matt 
    230  1.2    matt void
    231  1.2    matt rmixl_puthex64(uint64_t val)
    232  1.2    matt {
    233  1.2    matt 	RMIXL_PUTHEX;
    234  1.2    matt }
    235  1.2    matt 
    236  1.2    matt int
    237  1.2    matt rmixl_com_match(device_t parent, cfdata_t cf, void *aux)
    238  1.2    matt {
    239  1.2    matt 	struct obio_attach_args *obio = aux;
    240  1.2    matt 	bus_space_tag_t bst;
    241  1.3    matt 	bus_space_handle_t ioh;
    242  1.2    matt 	bus_addr_t addr;
    243  1.2    matt 	bus_size_t size;
    244  1.2    matt 	struct com_regs	regs;
    245  1.2    matt 	int rv;
    246  1.2    matt 
    247  1.3    matt 	bst = obio->obio_eb_bst;
    248  1.2    matt 	addr = obio->obio_addr;
    249  1.3    matt 	size = (bus_size_t)RMIXL_IO_DEV_UART_SIZE;
    250  1.2    matt 
    251  1.2    matt 	if (com_is_console(bst, addr, &regs.cr_ioh))
    252  1.2    matt 		return 1;
    253  1.2    matt 
    254  1.3    matt 	if (bus_space_map(bst, addr, size, 0, &ioh))
    255  1.2    matt 		return 0;		/* FAIL */
    256  1.2    matt 
    257  1.3    matt 	RMIXL_COM_INIT_REGS(regs, bst, ioh, addr);
    258  1.2    matt 
    259  1.2    matt 	rv = com_probe_subr(&regs);
    260  1.2    matt 
    261  1.3    matt 	bus_space_unmap(bst, ioh, size);
    262  1.2    matt 
    263  1.2    matt 	return rv;
    264  1.2    matt }
    265  1.2    matt 
    266  1.2    matt void
    267  1.2    matt rmixl_com_attach(device_t parent, device_t self, void *aux)
    268  1.2    matt {
    269  1.2    matt 	struct rmixl_com_softc *rsc = device_private(self);
    270  1.2    matt 	struct com_softc *sc = &rsc->sc_com;
    271  1.2    matt 	struct obio_attach_args *obio = aux;
    272  1.2    matt 	bus_space_tag_t bst;
    273  1.2    matt 	bus_space_handle_t ioh;
    274  1.2    matt 	bus_addr_t addr;
    275  1.2    matt 	bus_size_t size;
    276  1.2    matt 
    277  1.2    matt 	sc->sc_dev = self;
    278  1.3    matt 	sc->sc_frequency = comcnfreq;
    279  1.2    matt 
    280  1.3    matt 	bst = obio->obio_eb_bst;
    281  1.2    matt 	addr = obio->obio_addr;
    282  1.3    matt 	size = (bus_size_t)RMIXL_IO_DEV_UART_SIZE;
    283  1.2    matt 
    284  1.2    matt 	if (!com_is_console(bst, addr, &ioh)
    285  1.2    matt 	&&  bus_space_map(bst, addr, size, 0, &ioh) != 0) {
    286  1.2    matt 		aprint_error(": can't map registers\n");
    287  1.2    matt 		return;
    288  1.2    matt 	}
    289  1.2    matt 
    290  1.3    matt 	RMIXL_COM_INIT_REGS(sc->sc_regs, bst, ioh, addr);
    291  1.2    matt 
    292  1.2    matt 	com_attach_subr(sc);
    293  1.2    matt 
    294  1.3    matt 	rmixl_intr_establish(obio->obio_intr, obio->obio_tmsk,
    295  1.3    matt 		IPL_VM, RMIXL_TRIG_LEVEL, RMIXL_POLR_HIGH,
    296  1.3    matt 		comintr, sc, true);
    297  1.2    matt 
    298  1.2    matt }
    299  1.2    matt 
    300  1.2    matt void
    301  1.2    matt rmixl_com_initmap(struct com_regs *regsp)
    302  1.2    matt {
    303  1.2    matt 	/*
    304  1.2    matt 	 * map the 4 byte register stride
    305  1.2    matt 	 */
    306  1.4   cliff 	for (int i = 0; i < __arraycount(regsp->cr_map); i++)
    307  1.2    matt 		regsp->cr_map[i] = com_std_map[i] * 4;
    308  1.2    matt }
    309  1.2    matt 
    310  1.2    matt void
    311  1.2    matt rmixl_com_cnattach(bus_addr_t addr, int speed, int freq,
    312  1.2    matt 	int type, tcflag_t mode)
    313  1.2    matt {
    314  1.2    matt 	bus_space_tag_t bst;
    315  1.2    matt 	struct com_regs	regs;
    316  1.2    matt 
    317  1.3    matt 	bst = (bus_space_tag_t)&rmixl_configuration.rc_obio_eb_memt;
    318  1.2    matt 
    319  1.3    matt 	RMIXL_COM_INIT_REGS(regs, bst, 0, addr);
    320  1.2    matt 
    321  1.2    matt 	comcnattach1(&regs, speed, freq, type, mode);
    322  1.2    matt }
    323