Home | History | Annotate | Line # | Download | only in eisa
cac_eisa.c revision 1.17
      1 /*	$NetBSD: cac_eisa.c,v 1.17 2007/03/04 06:01:46 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * 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 copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (c) 2000 Jonathan Lemon
     41  * Copyright (c) 1999 by Matthew N. Dodd <winter (at) jurai.net>
     42  * All Rights Reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions, and the following disclaimer,
     49  *    without modification, immediately at the beginning of the file.
     50  * 2. The name of the author may not be used to endorse or promote products
     51  *    derived from this software without specific prior written permission.
     52  *
     53  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     56  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
     57  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     63  * SUCH DAMAGE.
     64  */
     65 
     66 /*
     67  * EISA front-end for cac(4) driver.
     68  */
     69 
     70 #include <sys/cdefs.h>
     71 __KERNEL_RCSID(0, "$NetBSD: cac_eisa.c,v 1.17 2007/03/04 06:01:46 christos Exp $");
     72 
     73 #include <sys/param.h>
     74 #include <sys/systm.h>
     75 #include <sys/device.h>
     76 
     77 #include <machine/bus.h>
     78 #include <machine/intr.h>
     79 
     80 #include <dev/eisa/eisavar.h>
     81 #include <dev/eisa/eisadevs.h>
     82 
     83 #include <dev/ic/cacreg.h>
     84 #include <dev/ic/cacvar.h>
     85 
     86 #define CAC_EISA_SLOT_OFFSET		0x0c88
     87 #define CAC_EISA_IOSIZE			0x0017
     88 #define CAC_EISA_IOCONF			0x38
     89 
     90 static void	cac_eisa_attach(struct device *, struct device *, void *);
     91 static int	cac_eisa_match(struct device *, struct cfdata *, void *);
     92 
     93 static struct	cac_ccb *cac_eisa_l0_completed(struct cac_softc *);
     94 static int	cac_eisa_l0_fifo_full(struct cac_softc *);
     95 static void	cac_eisa_l0_intr_enable(struct cac_softc *, int);
     96 static int	cac_eisa_l0_intr_pending(struct cac_softc *);
     97 static void	cac_eisa_l0_submit(struct cac_softc *, struct cac_ccb *);
     98 
     99 CFATTACH_DECL(cac_eisa, sizeof(struct cac_softc),
    100     cac_eisa_match, cac_eisa_attach, NULL, NULL);
    101 
    102 static const struct cac_linkage cac_eisa_l0 = {
    103 	cac_eisa_l0_completed,
    104 	cac_eisa_l0_fifo_full,
    105 	cac_eisa_l0_intr_enable,
    106 	cac_eisa_l0_intr_pending,
    107 	cac_eisa_l0_submit
    108 };
    109 
    110 static struct cac_eisa_type {
    111 	const char	*ct_prodstr;
    112 	const char	*ct_typestr;
    113 	const struct	cac_linkage *ct_linkage;
    114 } cac_eisa_type[] = {
    115 	{ "CPQ4001",	"IDA",		&cac_eisa_l0 },
    116 	{ "CPQ4002",	"IDA-2",	&cac_eisa_l0 },
    117 	{ "CPQ4010",	"IEAS",		&cac_eisa_l0 },
    118 	{ "CPQ4020",	"SMART",	&cac_eisa_l0 },
    119 	{ "CPQ4030",	"SMART-2/E",	&cac_l0 },
    120 };
    121 
    122 static int
    123 cac_eisa_match(struct device *parent, struct cfdata *match,
    124     void *aux)
    125 {
    126 	struct eisa_attach_args *ea;
    127 	int i;
    128 
    129 	ea = aux;
    130 
    131 	for (i = 0; i < sizeof(cac_eisa_type) / sizeof(cac_eisa_type[0]); i++)
    132 		if (strcmp(ea->ea_idstring, cac_eisa_type[i].ct_prodstr) == 0)
    133 			return (1);
    134 
    135 	return (0);
    136 }
    137 
    138 static void
    139 cac_eisa_attach(struct device *parent, struct device *self, void *aux)
    140 {
    141 	struct eisa_attach_args *ea;
    142 	bus_space_handle_t ioh;
    143 	eisa_chipset_tag_t ec;
    144 	eisa_intr_handle_t ih;
    145 	struct cac_softc *sc;
    146 	bus_space_tag_t iot;
    147 	const char *intrstr;
    148 	int irq, i;
    149 
    150 	ea = aux;
    151 	sc = device_private(self);
    152 	iot = ea->ea_iot;
    153 	ec = ea->ea_ec;
    154 
    155 	if (bus_space_map(iot, EISA_SLOT_ADDR(ea->ea_slot) +
    156 	    CAC_EISA_SLOT_OFFSET, CAC_EISA_IOSIZE, 0, &ioh)) {
    157 		printf("can't map i/o space\n");
    158 		return;
    159 	}
    160 
    161 	sc->sc_iot = iot;
    162 	sc->sc_ioh = ioh;
    163 	sc->sc_dmat = ea->ea_dmat;
    164 
    165 	/*
    166 	 * Map and establish the interrupt.
    167 	 */
    168 	switch (bus_space_read_1(iot, ioh, CAC_EISA_IOCONF) & 0xf0) {
    169 	case 0x20:
    170 		irq = 10;
    171 		break;
    172 	case 0x10:
    173 		irq = 11;
    174 		break;
    175 	case 0x40:
    176 		irq = 14;
    177 		break;
    178 	case 0x80:
    179 		irq = 15;
    180 		break;
    181 	default:
    182 		printf("controller on invalid IRQ\n");
    183 		return;
    184 	}
    185 
    186 	if (eisa_intr_map(ec, irq, &ih)) {
    187 		printf("can't map interrupt (%d)\n", irq);
    188 		return;
    189 	}
    190 
    191 	intrstr = eisa_intr_string(ec, ih);
    192 	if ((sc->sc_ih = eisa_intr_establish(ec, ih, IST_LEVEL, IPL_BIO,
    193 	    cac_intr, sc)) == NULL) {
    194 		printf("can't establish interrupt");
    195 		if (intrstr != NULL)
    196 			printf(" at %s", intrstr);
    197 		printf("\n");
    198 		return;
    199 	}
    200 
    201 	/*
    202 	 * Print board type and attach to the bus-independent code.
    203 	 */
    204 	for (i = 0; i < sizeof(cac_eisa_type) / sizeof(cac_eisa_type[0]); i++)
    205 		if (strcmp(ea->ea_idstring, cac_eisa_type[i].ct_prodstr) == 0)
    206 			break;
    207 
    208 	printf(": Compaq %s\n", cac_eisa_type[i].ct_typestr);
    209 	memcpy(&sc->sc_cl, cac_eisa_type[i].ct_linkage, sizeof(sc->sc_cl));
    210 	cac_init(sc, intrstr, 0);
    211 }
    212 
    213 /*
    214  * Linkage specific to EISA boards.
    215  */
    216 
    217 static int
    218 cac_eisa_l0_fifo_full(struct cac_softc *sc)
    219 {
    220 
    221 	return ((cac_inb(sc, CAC_EISAREG_SYSTEM_DOORBELL) &
    222 	    CAC_EISA_CHANNEL_CLEAR) == 0);
    223 }
    224 
    225 static void
    226 cac_eisa_l0_submit(struct cac_softc *sc, struct cac_ccb *ccb)
    227 {
    228 	u_int16_t size;
    229 
    230 	/*
    231 	 * On these boards, `ccb_hdr.size' is actually for control flags.
    232 	 * Set it to zero and pass the value by means of an I/O port.
    233 	 */
    234 	size = le16toh(ccb->ccb_hdr.size) << 2;
    235 	ccb->ccb_hdr.size = 0;
    236 
    237 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
    238 	    (char *)ccb - (char *)sc->sc_ccbs,
    239 	    sizeof(struct cac_ccb), BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
    240 
    241 	cac_outb(sc, CAC_EISAREG_SYSTEM_DOORBELL, CAC_EISA_CHANNEL_CLEAR);
    242 	cac_outl(sc, CAC_EISAREG_LIST_ADDR, ccb->ccb_paddr);
    243 	cac_outw(sc, CAC_EISAREG_LIST_LEN, size);
    244 	cac_outb(sc, CAC_EISAREG_LOCAL_DOORBELL, CAC_EISA_CHANNEL_BUSY);
    245 }
    246 
    247 static struct cac_ccb *
    248 cac_eisa_l0_completed(struct cac_softc *sc)
    249 {
    250 	struct cac_ccb *ccb;
    251 	u_int32_t off;
    252 	u_int8_t status;
    253 
    254 	if ((cac_inb(sc, CAC_EISAREG_SYSTEM_DOORBELL) &
    255 	    CAC_EISA_CHANNEL_BUSY) == 0)
    256 		return (NULL);
    257 
    258 	cac_outb(sc, CAC_EISAREG_SYSTEM_DOORBELL, CAC_EISA_CHANNEL_BUSY);
    259 	off = cac_inl(sc, CAC_EISAREG_COMPLETE_ADDR);
    260 	status = cac_inb(sc, CAC_EISAREG_LIST_STATUS);
    261 	cac_outb(sc, CAC_EISAREG_LOCAL_DOORBELL, CAC_EISA_CHANNEL_CLEAR);
    262 
    263 	if (off == 0)
    264 		return (NULL);
    265 
    266 	off = (off & ~3) - sc->sc_ccbs_paddr;
    267 	ccb = (struct cac_ccb *)((char *)sc->sc_ccbs + off);
    268 
    269 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, off, sizeof(struct cac_ccb),
    270 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
    271 
    272 	ccb->ccb_req.error = status;
    273 
    274 	if ((off & 3) != 0 && ccb->ccb_req.error == 0)
    275 		ccb->ccb_req.error = CAC_RET_CMD_REJECTED;
    276 
    277 	return (ccb);
    278 }
    279 
    280 static int
    281 cac_eisa_l0_intr_pending(struct cac_softc *sc)
    282 {
    283 
    284 	return (cac_inb(sc, CAC_EISAREG_SYSTEM_DOORBELL) &
    285 	    CAC_EISA_CHANNEL_BUSY);
    286 }
    287 
    288 static void
    289 cac_eisa_l0_intr_enable(struct cac_softc *sc, int state)
    290 {
    291 
    292 	if (state) {
    293 		cac_outb(sc, CAC_EISAREG_SYSTEM_DOORBELL,
    294 		    ~CAC_EISA_CHANNEL_CLEAR);
    295 		cac_outb(sc, CAC_EISAREG_LOCAL_DOORBELL,
    296 		    CAC_EISA_CHANNEL_BUSY);
    297 		cac_outb(sc, CAC_EISAREG_INTR_MASK, CAC_INTR_ENABLE);
    298 		cac_outb(sc, CAC_EISAREG_SYSTEM_MASK, CAC_INTR_ENABLE);
    299 	} else
    300 		cac_outb(sc, CAC_EISAREG_SYSTEM_MASK, CAC_INTR_DISABLE);
    301 }
    302