Home | History | Annotate | Line # | Download | only in dev
com_dio.c revision 1.2
      1  1.2  thorpej /*	$NetBSD: com_dio.c,v 1.2 2004/08/28 17:37:00 thorpej Exp $	*/
      2  1.1  tsutsui 
      3  1.1  tsutsui /*-
      4  1.1  tsutsui  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  1.1  tsutsui  * All rights reserved.
      6  1.1  tsutsui  *
      7  1.1  tsutsui  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  tsutsui  * by Charles M. Hannum.
      9  1.1  tsutsui  *
     10  1.1  tsutsui  * Redistribution and use in source and binary forms, with or without
     11  1.1  tsutsui  * modification, are permitted provided that the following conditions
     12  1.1  tsutsui  * are met:
     13  1.1  tsutsui  * 1. Redistributions of source code must retain the above copyright
     14  1.1  tsutsui  *    notice, this list of conditions and the following disclaimer.
     15  1.1  tsutsui  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  tsutsui  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  tsutsui  *    documentation and/or other materials provided with the distribution.
     18  1.1  tsutsui  * 3. All advertising materials mentioning features or use of this software
     19  1.1  tsutsui  *    must display the following acknowledgement:
     20  1.1  tsutsui  *        This product includes software developed by the NetBSD
     21  1.1  tsutsui  *        Foundation, Inc. and its contributors.
     22  1.1  tsutsui  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  tsutsui  *    contributors may be used to endorse or promote products derived
     24  1.1  tsutsui  *    from this software without specific prior written permission.
     25  1.1  tsutsui  *
     26  1.1  tsutsui  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  tsutsui  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  tsutsui  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  tsutsui  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  tsutsui  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  tsutsui  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  tsutsui  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  tsutsui  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  tsutsui  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  tsutsui  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  tsutsui  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  tsutsui  */
     38  1.1  tsutsui 
     39  1.1  tsutsui /*-
     40  1.1  tsutsui  * Copyright (c) 1991 The Regents of the University of California.
     41  1.1  tsutsui  * All rights reserved.
     42  1.1  tsutsui  *
     43  1.1  tsutsui  * Redistribution and use in source and binary forms, with or without
     44  1.1  tsutsui  * modification, are permitted provided that the following conditions
     45  1.1  tsutsui  * are met:
     46  1.1  tsutsui  * 1. Redistributions of source code must retain the above copyright
     47  1.1  tsutsui  *    notice, this list of conditions and the following disclaimer.
     48  1.1  tsutsui  * 2. Redistributions in binary form must reproduce the above copyright
     49  1.1  tsutsui  *    notice, this list of conditions and the following disclaimer in the
     50  1.1  tsutsui  *    documentation and/or other materials provided with the distribution.
     51  1.1  tsutsui  * 3. Neither the name of the University nor the names of its contributors
     52  1.1  tsutsui  *    may be used to endorse or promote products derived from this software
     53  1.1  tsutsui  *    without specific prior written permission.
     54  1.1  tsutsui  *
     55  1.1  tsutsui  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     56  1.1  tsutsui  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     57  1.1  tsutsui  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     58  1.1  tsutsui  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     59  1.1  tsutsui  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     60  1.1  tsutsui  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     61  1.1  tsutsui  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     62  1.1  tsutsui  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     63  1.1  tsutsui  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     64  1.1  tsutsui  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     65  1.1  tsutsui  * SUCH DAMAGE.
     66  1.1  tsutsui  *
     67  1.1  tsutsui  *	@(#)com.c	7.5 (Berkeley) 5/16/91
     68  1.1  tsutsui  */
     69  1.1  tsutsui 
     70  1.1  tsutsui #include <sys/cdefs.h>
     71  1.2  thorpej __KERNEL_RCSID(0, "$NetBSD: com_dio.c,v 1.2 2004/08/28 17:37:00 thorpej Exp $");
     72  1.1  tsutsui 
     73  1.1  tsutsui #include <sys/param.h>
     74  1.1  tsutsui #include <sys/systm.h>
     75  1.1  tsutsui #include <sys/device.h>
     76  1.1  tsutsui #include <sys/termios.h>
     77  1.1  tsutsui #include <sys/ttydefaults.h>
     78  1.1  tsutsui 
     79  1.1  tsutsui #include <machine/bus.h>
     80  1.1  tsutsui 
     81  1.1  tsutsui #include <dev/ic/comreg.h>
     82  1.1  tsutsui #include <dev/ic/comvar.h>
     83  1.1  tsutsui 
     84  1.1  tsutsui #include <hp300/dev/diovar.h>
     85  1.1  tsutsui #include <hp300/dev/diodevs.h>
     86  1.1  tsutsui #include <hp300/dev/com_dioreg.h>
     87  1.1  tsutsui #include <hp300/dev/com_diovar.h>
     88  1.1  tsutsui 
     89  1.1  tsutsui struct com_dio_softc {
     90  1.1  tsutsui 	struct	com_softc sc_com;	/* real "com" softc */
     91  1.1  tsutsui 
     92  1.1  tsutsui 	/* DIO-specific goo. */
     93  1.1  tsutsui 	struct	bus_space_tag sc_tag;	/* device specific bus space tag */
     94  1.1  tsutsui 	void	*sc_ih;			/* interrupt handler */
     95  1.1  tsutsui };
     96  1.1  tsutsui 
     97  1.2  thorpej static int	com_dio_match(struct device *, struct cfdata *, void *);
     98  1.2  thorpej static void	com_dio_attach(struct device *, struct device *, void *);
     99  1.1  tsutsui 
    100  1.1  tsutsui CFATTACH_DECL(com_dio, sizeof(struct com_dio_softc),
    101  1.1  tsutsui     com_dio_match, com_dio_attach, NULL, NULL);
    102  1.1  tsutsui 
    103  1.1  tsutsui static int com_dio_speed = TTYDEF_SPEED;
    104  1.1  tsutsui static struct bus_space_tag comcntag;
    105  1.1  tsutsui 
    106  1.1  tsutsui #define CONMODE	((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
    107  1.1  tsutsui 
    108  1.2  thorpej static int
    109  1.1  tsutsui com_dio_match(struct device *parent, struct cfdata *match, void *aux)
    110  1.1  tsutsui {
    111  1.1  tsutsui 	struct dio_attach_args *da = aux;
    112  1.1  tsutsui 
    113  1.1  tsutsui 	switch (da->da_id) {
    114  1.1  tsutsui 	case DIO_DEVICE_ID_DCA0:
    115  1.1  tsutsui 	case DIO_DEVICE_ID_DCA0REM:
    116  1.1  tsutsui 	case DIO_DEVICE_ID_DCA1:
    117  1.1  tsutsui 	case DIO_DEVICE_ID_DCA1REM:
    118  1.1  tsutsui 		return 1;
    119  1.1  tsutsui 	}
    120  1.1  tsutsui 
    121  1.1  tsutsui 	return 0;
    122  1.1  tsutsui }
    123  1.1  tsutsui 
    124  1.2  thorpej static void
    125  1.1  tsutsui com_dio_attach(struct device *parent, struct device *self, void *aux)
    126  1.1  tsutsui {
    127  1.1  tsutsui 	struct com_dio_softc *dsc = (void *)self;
    128  1.1  tsutsui 	struct com_softc *sc = &dsc->sc_com;
    129  1.1  tsutsui 	bus_space_tag_t iot;
    130  1.1  tsutsui 	bus_space_handle_t iohdca, iohcom;
    131  1.1  tsutsui 	struct dio_attach_args *da = aux;
    132  1.1  tsutsui 	int isconsole;
    133  1.1  tsutsui 
    134  1.1  tsutsui 	isconsole = com_is_console(&comcntag, da->da_addr + DCA_COM_OFFSET,
    135  1.1  tsutsui 	    &iohcom);
    136  1.1  tsutsui 
    137  1.1  tsutsui 	if (isconsole) {
    138  1.1  tsutsui 		iot = &comcntag;
    139  1.1  tsutsui 		bus_space_unmap(iot, iohcom, COM_NPORTS);
    140  1.1  tsutsui 	} else {
    141  1.1  tsutsui 		iot = &dsc->sc_tag;
    142  1.1  tsutsui 		memcpy(iot, da->da_bst, sizeof(struct bus_space_tag));
    143  1.1  tsutsui 		dio_set_bus_space_oddbyte(iot);
    144  1.1  tsutsui 	}
    145  1.1  tsutsui 
    146  1.1  tsutsui 	if (bus_space_map(iot, da->da_addr, DCA_SIZE, 0, &iohdca) ||
    147  1.1  tsutsui 	    bus_space_subregion(iot, iohdca, DCA_COM_OFFSET,
    148  1.1  tsutsui 	    COM_NPORTS << 1, &iohcom)) {
    149  1.1  tsutsui 		printf(": can't map i/o space\n");
    150  1.1  tsutsui 		return;
    151  1.1  tsutsui 	}
    152  1.1  tsutsui 
    153  1.1  tsutsui 	if (!isconsole) {
    154  1.1  tsutsui 		bus_space_write_1(iot, iohdca, DCA_RESET, 0xff);
    155  1.1  tsutsui 		DELAY(1000);
    156  1.1  tsutsui 	}
    157  1.1  tsutsui 
    158  1.1  tsutsui 	sc->sc_iot = iot;
    159  1.1  tsutsui 	sc->sc_ioh = iohcom;
    160  1.1  tsutsui 	sc->sc_iobase = da->da_addr + DCA_COM_OFFSET;
    161  1.1  tsutsui 	sc->sc_frequency = COM_DIO_FREQ;
    162  1.1  tsutsui 
    163  1.1  tsutsui 	com_attach_subr(sc);
    164  1.1  tsutsui 
    165  1.1  tsutsui 	dsc->sc_ih = dio_intr_establish(comintr, sc, da->da_ipl,
    166  1.1  tsutsui 	    ((sc->sc_hwflags & COM_HW_FIFO) != 0) ? IPL_TTY : IPL_TTYNOBUF);
    167  1.1  tsutsui 
    168  1.1  tsutsui 	/* Enable interrupts. */
    169  1.1  tsutsui 	bus_space_write_1(iot, iohdca, DCA_IC, IC_IE);
    170  1.1  tsutsui }
    171  1.1  tsutsui 
    172  1.1  tsutsui int
    173  1.1  tsutsui com_dio_cnattach(bus_space_tag_t bst, bus_addr_t addr, int scode)
    174  1.1  tsutsui {
    175  1.1  tsutsui 	bus_space_tag_t iot = &comcntag;
    176  1.1  tsutsui 	bus_space_handle_t iohdca;
    177  1.1  tsutsui 	u_int8_t id;
    178  1.1  tsutsui 
    179  1.1  tsutsui 	memcpy(iot, bst, sizeof(struct bus_space_tag));
    180  1.1  tsutsui 	dio_set_bus_space_oddbyte(iot);
    181  1.1  tsutsui 
    182  1.1  tsutsui 	if (bus_space_map(iot, addr, DCA_SIZE, 0, &iohdca))
    183  1.1  tsutsui 		return 1;
    184  1.1  tsutsui 	bus_space_write_1(iot, iohdca, DCA_RESET, 0xff);
    185  1.1  tsutsui 	DELAY(100);
    186  1.1  tsutsui 	bus_space_write_1(iot, iohdca, DCA_IC, IC_IE);
    187  1.1  tsutsui 
    188  1.1  tsutsui 	id = bus_space_read_1(iot, iohdca, DCA_ID);
    189  1.1  tsutsui 	bus_space_unmap(iot, iohdca, DCA_SIZE);
    190  1.1  tsutsui 
    191  1.1  tsutsui 	switch (id) {
    192  1.1  tsutsui #ifdef CONSCODE
    193  1.1  tsutsui 	case DCAID0:
    194  1.1  tsutsui 	case DCAID1:
    195  1.1  tsutsui #endif
    196  1.1  tsutsui 	case DCAREMID0:
    197  1.1  tsutsui 	case DCAREMID1:
    198  1.1  tsutsui 		break;
    199  1.1  tsutsui 	default:
    200  1.1  tsutsui 		return 1;
    201  1.1  tsutsui 	}
    202  1.1  tsutsui 
    203  1.1  tsutsui 	comcnattach(iot, addr + DCA_COM_OFFSET, com_dio_speed, COM_DIO_FREQ,
    204  1.1  tsutsui 	    COM_TYPE_NORMAL, CONMODE);
    205  1.1  tsutsui 
    206  1.1  tsutsui 	return 0;
    207  1.1  tsutsui }
    208