Home | History | Annotate | Line # | Download | only in pcmcia
esp_pcmcia.c revision 1.18
      1 /*	$NetBSD: esp_pcmcia.c,v 1.18 2004/08/10 07:04:19 mycroft Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000, 2004 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum.
      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 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: esp_pcmcia.c,v 1.18 2004/08/10 07:04:19 mycroft Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/device.h>
     45 #include <sys/buf.h>
     46 
     47 #include <machine/bus.h>
     48 #include <machine/intr.h>
     49 
     50 #include <dev/scsipi/scsi_all.h>
     51 #include <dev/scsipi/scsipi_all.h>
     52 #include <dev/scsipi/scsiconf.h>
     53 
     54 #include <dev/pcmcia/pcmciareg.h>
     55 #include <dev/pcmcia/pcmciavar.h>
     56 #include <dev/pcmcia/pcmciadevs.h>
     57 
     58 #include <dev/ic/ncr53c9xreg.h>
     59 #include <dev/ic/ncr53c9xvar.h>
     60 
     61 struct esp_pcmcia_softc {
     62 	struct ncr53c9x_softc	sc_ncr53c9x;	/* glue to MI code */
     63 
     64 	int		sc_active;		/* Pseudo-DMA state vars */
     65 	int		sc_tc;
     66 	int		sc_datain;
     67 	size_t		sc_dmasize;
     68 	size_t		sc_dmatrans;
     69 	char		**sc_dmaaddr;
     70 	size_t		*sc_pdmalen;
     71 
     72 	/* PCMCIA-specific goo. */
     73 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
     74 	void *sc_ih;				/* interrupt handler */
     75 #ifdef ESP_PCMCIA_POLL
     76 	struct callout sc_poll_ch;
     77 #endif
     78 	bus_space_tag_t	sc_iot;
     79 	bus_space_handle_t sc_ioh;
     80 
     81 	int sc_state;
     82 #define	ESP_PCMCIA_ATTACH1	1
     83 #define	ESP_PCMCIA_ATTACH2	2
     84 #define ESP_PCMCIA_ATTACHED	3
     85 };
     86 
     87 int	esp_pcmcia_match __P((struct device *, struct cfdata *, void *));
     88 int	esp_pcmcia_validate_config __P((struct pcmcia_config_entry *));
     89 void	esp_pcmcia_attach __P((struct device *, struct device *, void *));
     90 void	esp_pcmcia_init __P((struct esp_pcmcia_softc *));
     91 int	esp_pcmcia_detach __P((struct device *, int));
     92 int	esp_pcmcia_enable __P((struct device *, int));
     93 
     94 CFATTACH_DECL(esp_pcmcia, sizeof(struct esp_pcmcia_softc),
     95     esp_pcmcia_match, esp_pcmcia_attach, esp_pcmcia_detach, NULL);
     96 
     97 /*
     98  * Functions and the switch for the MI code.
     99  */
    100 #ifdef ESP_PCMCIA_POLL
    101 void	esp_pcmcia_poll __P((void *));
    102 #endif
    103 u_char	esp_pcmcia_read_reg __P((struct ncr53c9x_softc *, int));
    104 void	esp_pcmcia_write_reg __P((struct ncr53c9x_softc *, int, u_char));
    105 int	esp_pcmcia_dma_isintr __P((struct ncr53c9x_softc *));
    106 void	esp_pcmcia_dma_reset __P((struct ncr53c9x_softc *));
    107 int	esp_pcmcia_dma_intr __P((struct ncr53c9x_softc *));
    108 int	esp_pcmcia_dma_setup __P((struct ncr53c9x_softc *, caddr_t *,
    109 	    size_t *, int, size_t *));
    110 void	esp_pcmcia_dma_go __P((struct ncr53c9x_softc *));
    111 void	esp_pcmcia_dma_stop __P((struct ncr53c9x_softc *));
    112 int	esp_pcmcia_dma_isactive __P((struct ncr53c9x_softc *));
    113 
    114 static const struct esp_pcmcia_product *
    115     esp_pcmcia_lookup __P((struct pcmcia_attach_args *));
    116 
    117 struct ncr53c9x_glue esp_pcmcia_glue = {
    118 	esp_pcmcia_read_reg,
    119 	esp_pcmcia_write_reg,
    120 	esp_pcmcia_dma_isintr,
    121 	esp_pcmcia_dma_reset,
    122 	esp_pcmcia_dma_intr,
    123 	esp_pcmcia_dma_setup,
    124 	esp_pcmcia_dma_go,
    125 	esp_pcmcia_dma_stop,
    126 	esp_pcmcia_dma_isactive,
    127 	NULL,			/* gl_clear_latched_intr */
    128 };
    129 
    130 const struct esp_pcmcia_product {
    131 	const char	*epp_name;		/* product name */
    132 	u_int32_t	epp_vendor;		/* vendor ID */
    133 	u_int32_t	epp_product;		/* product ID */
    134 	const char	*epp_cisinfo[4];	/* CIS information */
    135 } esp_pcmcia_products[] = {
    136 	{ "",	PCMCIA_VENDOR_PANASONIC,
    137 	  PCMCIA_PRODUCT_PANASONIC_KXLC002,	PCMCIA_CIS_PANASONIC_KXLC002 },
    138 
    139 	{ "",	PCMCIA_VENDOR_RATOC,
    140 	  PCMCIA_PRODUCT_RATOC_REX_9530,	PCMCIA_CIS_RATOC_REX_9530 },
    141 
    142 	{ NULL }
    143 };
    144 
    145 static const struct esp_pcmcia_product *
    146 esp_pcmcia_lookup(pa)
    147 	struct pcmcia_attach_args *pa;
    148 {
    149 	const struct esp_pcmcia_product *epp;
    150 
    151 	for (epp = esp_pcmcia_products; epp->epp_name != NULL; epp++) {
    152 		/* match by CIS information */
    153 		if (pa->card->cis1_info[0] != NULL &&
    154 		    epp->epp_cisinfo[0] != NULL &&
    155 		    strcmp(pa->card->cis1_info[0], epp->epp_cisinfo[0]) == 0 &&
    156 		    pa->card->cis1_info[1] != NULL &&
    157 		    epp->epp_cisinfo[1] != NULL &&
    158 		    strcmp(pa->card->cis1_info[1], epp->epp_cisinfo[1]) == 0)
    159 			return (epp);
    160 
    161 		/* match by vendor/product id */
    162 		if (pa->manufacturer != PCMCIA_VENDOR_INVALID &&
    163 		    pa->manufacturer == epp->epp_vendor &&
    164 		    pa->product != PCMCIA_PRODUCT_INVALID &&
    165 		    pa->product == epp->epp_product)
    166 			return (epp);
    167 	}
    168 
    169 	return (NULL);
    170 }
    171 
    172 int
    173 esp_pcmcia_match(parent, match, aux)
    174 	struct device *parent;
    175 	struct cfdata *match;
    176 	void *aux;
    177 {
    178 	struct pcmcia_attach_args *pa = aux;
    179 
    180 	if (esp_pcmcia_lookup(pa) != NULL)
    181 		return (1);
    182 	return (0);
    183 }
    184 
    185 int
    186 esp_pcmcia_validate_config(cfe)
    187 	struct pcmcia_config_entry *cfe;
    188 {
    189 	if (cfe->iftype != PCMCIA_IFTYPE_IO ||
    190 	    cfe->num_memspace != 0 ||
    191 	    cfe->num_iospace != 1)
    192 		return (EINVAL);
    193 	return (0);
    194 }
    195 
    196 void
    197 esp_pcmcia_attach(parent, self, aux)
    198 	struct device *parent, *self;
    199 	void *aux;
    200 {
    201 	struct esp_pcmcia_softc *esc = (void *)self;
    202 	struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
    203 	struct pcmcia_attach_args *pa = aux;
    204 	struct pcmcia_config_entry *cfe;
    205 	struct pcmcia_function *pf = pa->pf;
    206 	int error;
    207 
    208 	aprint_normal("\n");
    209 	esc->sc_pf = pf;
    210 
    211 	error = pcmcia_function_configure(pf, esp_pcmcia_validate_config);
    212 	if (error) {
    213 		aprint_error("%s: configure failed, error=%d\n", self->dv_xname,
    214 		    error);
    215 		return;
    216 	}
    217 
    218 	cfe = pf->cfe;
    219 	esc->sc_iot = cfe->iospace[0].handle.iot;
    220 	esc->sc_ioh = cfe->iospace[0].handle.ioh;
    221 	esp_pcmcia_init(esc);
    222 
    223 	error = esp_pcmcia_enable(self, 1);
    224 	if (error) {
    225 		aprint_error("%s: enable failed, error=%d\n", self->dv_xname,
    226 		    error);
    227 		goto fail;
    228 	}
    229 
    230 	sc->sc_adapter.adapt_minphys = minphys;
    231 	sc->sc_adapter.adapt_request = ncr53c9x_scsipi_request;
    232 	sc->sc_adapter.adapt_enable = esp_pcmcia_enable;
    233 
    234 	esc->sc_state = ESP_PCMCIA_ATTACH1;
    235 	ncr53c9x_attach(sc);
    236 	if (esc->sc_state == ESP_PCMCIA_ATTACH1)
    237 		esp_pcmcia_enable(self, 0);
    238 	esc->sc_state = ESP_PCMCIA_ATTACHED;
    239 	return;
    240 
    241 fail:
    242 	pcmcia_function_unconfigure(pf);
    243 }
    244 
    245 void
    246 esp_pcmcia_init(esc)
    247 	struct esp_pcmcia_softc *esc;
    248 {
    249 	struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
    250 
    251 	/* id 7, clock 40M, parity ON, sync OFF, fast ON, slow ON */
    252 
    253 	sc->sc_glue = &esp_pcmcia_glue;
    254 
    255 #ifdef ESP_PCMCIA_POLL
    256 	callout_init(&esc->sc_poll_ch);
    257 #endif
    258 
    259 	sc->sc_rev = NCR_VARIANT_ESP406;
    260 	sc->sc_id = 7;
    261 	sc->sc_freq = 40;
    262 	/* try -PARENB -SLOW */
    263 	sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB | NCRCFG1_SLOW;
    264 	/* try +FE */
    265 	sc->sc_cfg2 = NCRCFG2_SCSI2;
    266 	/* try -IDM -FSCSI -FCLK */
    267 	sc->sc_cfg3 = NCRESPCFG3_CDB | NCRESPCFG3_FCLK | NCRESPCFG3_IDM |
    268 	    NCRESPCFG3_FSCSI;
    269 	sc->sc_cfg4 = NCRCFG4_ACTNEG;
    270 	/* try +INTP */
    271 	sc->sc_cfg5 = NCRCFG5_CRS1 | NCRCFG5_AADDR | NCRCFG5_PTRINC;
    272 	sc->sc_minsync = 0;
    273 	sc->sc_maxxfer = 64 * 1024;
    274 }
    275 
    276 int
    277 esp_pcmcia_detach(self, flags)
    278 	struct device *self;
    279 	int flags;
    280 {
    281 	struct esp_pcmcia_softc *sc = (void *)self;
    282 	int error;
    283 
    284 	if (sc->sc_state != ESP_PCMCIA_ATTACHED)
    285 		return (0);
    286 
    287 	error = ncr53c9x_detach(&sc->sc_ncr53c9x, flags);
    288 	if (error)
    289 		return (error);
    290 
    291 	pcmcia_function_unconfigure(sc->sc_pf);
    292 
    293 	return (0);
    294 }
    295 
    296 int
    297 esp_pcmcia_enable(arg, onoff)
    298 	struct device *arg;
    299 	int onoff;
    300 {
    301 	struct esp_pcmcia_softc *sc = (void *)arg;
    302 
    303 	if (onoff) {
    304 		/*
    305 		 * If attach is in progress, we already have the device
    306 		 * powered up.
    307 		 */
    308 		if (sc->sc_state == ESP_PCMCIA_ATTACH1) {
    309 			sc->sc_state = ESP_PCMCIA_ATTACH2;
    310 		} else {
    311 #ifdef ESP_PCMCIA_POLL
    312 			callout_reset(&sc->sc_poll_ch, 1, esp_pcmcia_poll, sc);
    313 #else
    314 			/* Establish the interrupt handler. */
    315 			sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
    316 			    ncr53c9x_intr, &sc->sc_ncr53c9x);
    317 			if (sc->sc_ih == NULL) {
    318 				printf("%s: couldn't establish interrupt handler\n",
    319 				    sc->sc_ncr53c9x.sc_dev.dv_xname);
    320 				return (EIO);
    321 			}
    322 #endif
    323 
    324 			if (pcmcia_function_enable(sc->sc_pf)) {
    325 				printf("%s: couldn't enable PCMCIA function\n",
    326 				    sc->sc_ncr53c9x.sc_dev.dv_xname);
    327 				pcmcia_intr_disestablish(sc->sc_pf,
    328 				    sc->sc_ih);
    329 				return (EIO);
    330 			}
    331 
    332 			/* Initialize only chip.  */
    333 			ncr53c9x_init(&sc->sc_ncr53c9x, 0);
    334 		}
    335 	} else {
    336 		pcmcia_function_disable(sc->sc_pf);
    337 #ifdef ESP_PCMCIA_POLL
    338 		callout_stop(&sc->sc_poll_ch);
    339 #else
    340 		pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    341 		sc->sc_ih = 0;
    342 #endif
    343 	}
    344 
    345 	return (0);
    346 }
    347 
    348 #ifdef ESP_PCMCIA_POLL
    349 void
    350 esp_pcmcia_poll(arg)
    351 	void *arg;
    352 {
    353 	struct esp_pcmcia_softc *esc = arg;
    354 
    355 	(void) ncr53c9x_intr(&esc->sc_ncr53c9x);
    356 	callout_reset(&esc->sc_poll_ch, 1, esp_pcmcia_poll, esc);
    357 }
    358 #endif
    359 
    360 /*
    361  * Glue functions.
    362  */
    363 u_char
    364 esp_pcmcia_read_reg(sc, reg)
    365 	struct ncr53c9x_softc *sc;
    366 	int reg;
    367 {
    368 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
    369 	u_char v;
    370 
    371 	v = bus_space_read_1(esc->sc_iot, esc->sc_ioh, reg);
    372 	return v;
    373 }
    374 
    375 void
    376 esp_pcmcia_write_reg(sc, reg, val)
    377 	struct ncr53c9x_softc *sc;
    378 	int reg;
    379 	u_char val;
    380 {
    381 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
    382 	u_char v = val;
    383 
    384 	if (reg == NCR_CMD && v == (NCRCMD_TRANS|NCRCMD_DMA))
    385 		v = NCRCMD_TRANS;
    386 	bus_space_write_1(esc->sc_iot, esc->sc_ioh, reg, v);
    387 }
    388 
    389 int
    390 esp_pcmcia_dma_isintr(sc)
    391 	struct ncr53c9x_softc *sc;
    392 {
    393 
    394 	return NCR_READ_REG(sc, NCR_STAT) & NCRSTAT_INT;
    395 }
    396 
    397 void
    398 esp_pcmcia_dma_reset(sc)
    399 	struct ncr53c9x_softc *sc;
    400 {
    401 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
    402 
    403 	esc->sc_active = 0;
    404 	esc->sc_tc = 0;
    405 }
    406 
    407 int
    408 esp_pcmcia_dma_intr(sc)
    409 	struct ncr53c9x_softc *sc;
    410 {
    411 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
    412 	u_char	*p;
    413 	u_int	espphase, espstat, espintr;
    414 	int	cnt;
    415 
    416 	if (esc->sc_active == 0) {
    417 		printf("%s: dma_intr--inactive DMA\n", sc->sc_dev.dv_xname);
    418 		return -1;
    419 	}
    420 
    421 	if ((sc->sc_espintr & NCRINTR_BS) == 0) {
    422 		esc->sc_active = 0;
    423 		return 0;
    424 	}
    425 
    426 	cnt = *esc->sc_pdmalen;
    427 	if (*esc->sc_pdmalen == 0) {
    428 		printf("%s: data interrupt, but no count left\n",
    429 		    sc->sc_dev.dv_xname);
    430 	}
    431 
    432 	p = *esc->sc_dmaaddr;
    433 	espphase = sc->sc_phase;
    434 	espstat = (u_int) sc->sc_espstat;
    435 	espintr = (u_int) sc->sc_espintr;
    436 	do {
    437 		if (esc->sc_datain) {
    438 			*p++ = NCR_READ_REG(sc, NCR_FIFO);
    439 			cnt--;
    440 			if (espphase == DATA_IN_PHASE)
    441 				NCR_WRITE_REG(sc, NCR_CMD, NCRCMD_TRANS);
    442 			else
    443 				esc->sc_active = 0;
    444 		} else {
    445 			if (espphase == DATA_OUT_PHASE ||
    446 			    espphase == MESSAGE_OUT_PHASE) {
    447 				NCR_WRITE_REG(sc, NCR_FIFO, *p++);
    448 				cnt--;
    449 				NCR_WRITE_REG(sc, NCR_CMD, NCRCMD_TRANS);
    450 			} else
    451 				esc->sc_active = 0;
    452 		}
    453 
    454 		if (esc->sc_active) {
    455 			while (!(NCR_READ_REG(sc, NCR_STAT) & NCRSTAT_INT));
    456 			espstat = NCR_READ_REG(sc, NCR_STAT);
    457 			espintr = NCR_READ_REG(sc, NCR_INTR);
    458 			espphase = (espintr & NCRINTR_DIS)
    459 				    ? /* Disconnected */ BUSFREE_PHASE
    460 				    : espstat & PHASE_MASK;
    461 		}
    462 	} while (esc->sc_active && espintr);
    463 	sc->sc_phase = espphase;
    464 	sc->sc_espstat = (u_char) espstat;
    465 	sc->sc_espintr = (u_char) espintr;
    466 	*esc->sc_dmaaddr = p;
    467 	*esc->sc_pdmalen = cnt;
    468 
    469 	if (*esc->sc_pdmalen == 0)
    470 		esc->sc_tc = NCRSTAT_TC;
    471 	sc->sc_espstat |= esc->sc_tc;
    472 	return 0;
    473 }
    474 
    475 int
    476 esp_pcmcia_dma_setup(sc, addr, len, datain, dmasize)
    477 	struct ncr53c9x_softc *sc;
    478 	caddr_t *addr;
    479 	size_t *len;
    480 	int datain;
    481 	size_t *dmasize;
    482 {
    483 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
    484 
    485 	esc->sc_dmaaddr = addr;
    486 	esc->sc_pdmalen = len;
    487 	esc->sc_datain = datain;
    488 	esc->sc_dmasize = *dmasize;
    489 	esc->sc_tc = 0;
    490 
    491 	return 0;
    492 }
    493 
    494 void
    495 esp_pcmcia_dma_go(sc)
    496 	struct ncr53c9x_softc *sc;
    497 {
    498 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
    499 
    500 	esc->sc_active = 1;
    501 }
    502 
    503 void
    504 esp_pcmcia_dma_stop(sc)
    505 	struct ncr53c9x_softc *sc;
    506 {
    507 }
    508 
    509 int
    510 esp_pcmcia_dma_isactive(sc)
    511 	struct ncr53c9x_softc *sc;
    512 {
    513 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
    514 
    515 	return (esc->sc_active);
    516 }
    517