Home | History | Annotate | Line # | Download | only in pcmcia
esp_pcmcia.c revision 1.20
      1 /*	$NetBSD: esp_pcmcia.c,v 1.20 2004/08/10 16:04:16 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.20 2004/08/10 16:04:16 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 		goto fail;
    226 
    227 	sc->sc_adapter.adapt_minphys = minphys;
    228 	sc->sc_adapter.adapt_request = ncr53c9x_scsipi_request;
    229 	sc->sc_adapter.adapt_enable = esp_pcmcia_enable;
    230 
    231 	esc->sc_state = ESP_PCMCIA_ATTACH1;
    232 	ncr53c9x_attach(sc);
    233 	if (esc->sc_state == ESP_PCMCIA_ATTACH1)
    234 		esp_pcmcia_enable(self, 0);
    235 	esc->sc_state = ESP_PCMCIA_ATTACHED;
    236 	return;
    237 
    238 fail:
    239 	pcmcia_function_unconfigure(pf);
    240 }
    241 
    242 void
    243 esp_pcmcia_init(esc)
    244 	struct esp_pcmcia_softc *esc;
    245 {
    246 	struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
    247 
    248 	/* id 7, clock 40M, parity ON, sync OFF, fast ON, slow ON */
    249 
    250 	sc->sc_glue = &esp_pcmcia_glue;
    251 
    252 #ifdef ESP_PCMCIA_POLL
    253 	callout_init(&esc->sc_poll_ch);
    254 #endif
    255 
    256 	sc->sc_rev = NCR_VARIANT_ESP406;
    257 	sc->sc_id = 7;
    258 	sc->sc_freq = 40;
    259 	/* try -PARENB -SLOW */
    260 	sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB | NCRCFG1_SLOW;
    261 	/* try +FE */
    262 	sc->sc_cfg2 = NCRCFG2_SCSI2;
    263 	/* try -IDM -FSCSI -FCLK */
    264 	sc->sc_cfg3 = NCRESPCFG3_CDB | NCRESPCFG3_FCLK | NCRESPCFG3_IDM |
    265 	    NCRESPCFG3_FSCSI;
    266 	sc->sc_cfg4 = NCRCFG4_ACTNEG;
    267 	/* try +INTP */
    268 	sc->sc_cfg5 = NCRCFG5_CRS1 | NCRCFG5_AADDR | NCRCFG5_PTRINC;
    269 	sc->sc_minsync = 0;
    270 	sc->sc_maxxfer = 64 * 1024;
    271 }
    272 
    273 int
    274 esp_pcmcia_detach(self, flags)
    275 	struct device *self;
    276 	int flags;
    277 {
    278 	struct esp_pcmcia_softc *sc = (void *)self;
    279 	int error;
    280 
    281 	if (sc->sc_state != ESP_PCMCIA_ATTACHED)
    282 		return (0);
    283 
    284 	error = ncr53c9x_detach(&sc->sc_ncr53c9x, flags);
    285 	if (error)
    286 		return (error);
    287 
    288 	pcmcia_function_unconfigure(sc->sc_pf);
    289 
    290 	return (0);
    291 }
    292 
    293 int
    294 esp_pcmcia_enable(arg, onoff)
    295 	struct device *arg;
    296 	int onoff;
    297 {
    298 	struct esp_pcmcia_softc *sc = (void *)arg;
    299 	int error;
    300 
    301 	if (onoff) {
    302 		/*
    303 		 * If attach is in progress, we already have the device
    304 		 * powered up.
    305 		 */
    306 		if (sc->sc_state == ESP_PCMCIA_ATTACH1) {
    307 			sc->sc_state = ESP_PCMCIA_ATTACH2;
    308 		} else {
    309 #ifdef ESP_PCMCIA_POLL
    310 			callout_reset(&sc->sc_poll_ch, 1, esp_pcmcia_poll, sc);
    311 #else
    312 			/* Establish the interrupt handler. */
    313 			sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
    314 			    ncr53c9x_intr, &sc->sc_ncr53c9x);
    315 			if (!sc->sc_ih)
    316 				return (EIO);
    317 #endif
    318 
    319 			error = pcmcia_function_enable(sc->sc_pf);
    320 			if (error) {
    321 				pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    322 				sc->sc_ih = 0;
    323 				return (error);
    324 			}
    325 
    326 			/* Initialize only chip.  */
    327 			ncr53c9x_init(&sc->sc_ncr53c9x, 0);
    328 		}
    329 	} else {
    330 		pcmcia_function_disable(sc->sc_pf);
    331 #ifdef ESP_PCMCIA_POLL
    332 		callout_stop(&sc->sc_poll_ch);
    333 #else
    334 		pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    335 		sc->sc_ih = 0;
    336 #endif
    337 	}
    338 
    339 	return (0);
    340 }
    341 
    342 #ifdef ESP_PCMCIA_POLL
    343 void
    344 esp_pcmcia_poll(arg)
    345 	void *arg;
    346 {
    347 	struct esp_pcmcia_softc *esc = arg;
    348 
    349 	(void) ncr53c9x_intr(&esc->sc_ncr53c9x);
    350 	callout_reset(&esc->sc_poll_ch, 1, esp_pcmcia_poll, esc);
    351 }
    352 #endif
    353 
    354 /*
    355  * Glue functions.
    356  */
    357 u_char
    358 esp_pcmcia_read_reg(sc, reg)
    359 	struct ncr53c9x_softc *sc;
    360 	int reg;
    361 {
    362 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
    363 	u_char v;
    364 
    365 	v = bus_space_read_1(esc->sc_iot, esc->sc_ioh, reg);
    366 	return v;
    367 }
    368 
    369 void
    370 esp_pcmcia_write_reg(sc, reg, val)
    371 	struct ncr53c9x_softc *sc;
    372 	int reg;
    373 	u_char val;
    374 {
    375 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
    376 	u_char v = val;
    377 
    378 	if (reg == NCR_CMD && v == (NCRCMD_TRANS|NCRCMD_DMA))
    379 		v = NCRCMD_TRANS;
    380 	bus_space_write_1(esc->sc_iot, esc->sc_ioh, reg, v);
    381 }
    382 
    383 int
    384 esp_pcmcia_dma_isintr(sc)
    385 	struct ncr53c9x_softc *sc;
    386 {
    387 
    388 	return NCR_READ_REG(sc, NCR_STAT) & NCRSTAT_INT;
    389 }
    390 
    391 void
    392 esp_pcmcia_dma_reset(sc)
    393 	struct ncr53c9x_softc *sc;
    394 {
    395 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
    396 
    397 	esc->sc_active = 0;
    398 	esc->sc_tc = 0;
    399 }
    400 
    401 int
    402 esp_pcmcia_dma_intr(sc)
    403 	struct ncr53c9x_softc *sc;
    404 {
    405 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
    406 	u_char	*p;
    407 	u_int	espphase, espstat, espintr;
    408 	int	cnt;
    409 
    410 	if (esc->sc_active == 0) {
    411 		printf("%s: dma_intr--inactive DMA\n", sc->sc_dev.dv_xname);
    412 		return -1;
    413 	}
    414 
    415 	if ((sc->sc_espintr & NCRINTR_BS) == 0) {
    416 		esc->sc_active = 0;
    417 		return 0;
    418 	}
    419 
    420 	cnt = *esc->sc_pdmalen;
    421 	if (*esc->sc_pdmalen == 0) {
    422 		printf("%s: data interrupt, but no count left\n",
    423 		    sc->sc_dev.dv_xname);
    424 	}
    425 
    426 	p = *esc->sc_dmaaddr;
    427 	espphase = sc->sc_phase;
    428 	espstat = (u_int) sc->sc_espstat;
    429 	espintr = (u_int) sc->sc_espintr;
    430 	do {
    431 		if (esc->sc_datain) {
    432 			*p++ = NCR_READ_REG(sc, NCR_FIFO);
    433 			cnt--;
    434 			if (espphase == DATA_IN_PHASE)
    435 				NCR_WRITE_REG(sc, NCR_CMD, NCRCMD_TRANS);
    436 			else
    437 				esc->sc_active = 0;
    438 		} else {
    439 			if (espphase == DATA_OUT_PHASE ||
    440 			    espphase == MESSAGE_OUT_PHASE) {
    441 				NCR_WRITE_REG(sc, NCR_FIFO, *p++);
    442 				cnt--;
    443 				NCR_WRITE_REG(sc, NCR_CMD, NCRCMD_TRANS);
    444 			} else
    445 				esc->sc_active = 0;
    446 		}
    447 
    448 		if (esc->sc_active) {
    449 			while (!(NCR_READ_REG(sc, NCR_STAT) & NCRSTAT_INT));
    450 			espstat = NCR_READ_REG(sc, NCR_STAT);
    451 			espintr = NCR_READ_REG(sc, NCR_INTR);
    452 			espphase = (espintr & NCRINTR_DIS)
    453 				    ? /* Disconnected */ BUSFREE_PHASE
    454 				    : espstat & PHASE_MASK;
    455 		}
    456 	} while (esc->sc_active && espintr);
    457 	sc->sc_phase = espphase;
    458 	sc->sc_espstat = (u_char) espstat;
    459 	sc->sc_espintr = (u_char) espintr;
    460 	*esc->sc_dmaaddr = p;
    461 	*esc->sc_pdmalen = cnt;
    462 
    463 	if (*esc->sc_pdmalen == 0)
    464 		esc->sc_tc = NCRSTAT_TC;
    465 	sc->sc_espstat |= esc->sc_tc;
    466 	return 0;
    467 }
    468 
    469 int
    470 esp_pcmcia_dma_setup(sc, addr, len, datain, dmasize)
    471 	struct ncr53c9x_softc *sc;
    472 	caddr_t *addr;
    473 	size_t *len;
    474 	int datain;
    475 	size_t *dmasize;
    476 {
    477 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
    478 
    479 	esc->sc_dmaaddr = addr;
    480 	esc->sc_pdmalen = len;
    481 	esc->sc_datain = datain;
    482 	esc->sc_dmasize = *dmasize;
    483 	esc->sc_tc = 0;
    484 
    485 	return 0;
    486 }
    487 
    488 void
    489 esp_pcmcia_dma_go(sc)
    490 	struct ncr53c9x_softc *sc;
    491 {
    492 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
    493 
    494 	esc->sc_active = 1;
    495 }
    496 
    497 void
    498 esp_pcmcia_dma_stop(sc)
    499 	struct ncr53c9x_softc *sc;
    500 {
    501 }
    502 
    503 int
    504 esp_pcmcia_dma_isactive(sc)
    505 	struct ncr53c9x_softc *sc;
    506 {
    507 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
    508 
    509 	return (esc->sc_active);
    510 }
    511