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