Home | History | Annotate | Line # | Download | only in pcmcia
esp_pcmcia.c revision 1.19
      1 /*	$NetBSD: esp_pcmcia.c,v 1.19 2004/08/10 15:29:56 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.19 2004/08/10 15:29:56 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 				return (error);
    323 			}
    324 
    325 			/* Initialize only chip.  */
    326 			ncr53c9x_init(&sc->sc_ncr53c9x, 0);
    327 		}
    328 	} else {
    329 		pcmcia_function_disable(sc->sc_pf);
    330 #ifdef ESP_PCMCIA_POLL
    331 		callout_stop(&sc->sc_poll_ch);
    332 #else
    333 		pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    334 		sc->sc_ih = 0;
    335 #endif
    336 	}
    337 
    338 	return (0);
    339 }
    340 
    341 #ifdef ESP_PCMCIA_POLL
    342 void
    343 esp_pcmcia_poll(arg)
    344 	void *arg;
    345 {
    346 	struct esp_pcmcia_softc *esc = arg;
    347 
    348 	(void) ncr53c9x_intr(&esc->sc_ncr53c9x);
    349 	callout_reset(&esc->sc_poll_ch, 1, esp_pcmcia_poll, esc);
    350 }
    351 #endif
    352 
    353 /*
    354  * Glue functions.
    355  */
    356 u_char
    357 esp_pcmcia_read_reg(sc, reg)
    358 	struct ncr53c9x_softc *sc;
    359 	int reg;
    360 {
    361 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
    362 	u_char v;
    363 
    364 	v = bus_space_read_1(esc->sc_iot, esc->sc_ioh, reg);
    365 	return v;
    366 }
    367 
    368 void
    369 esp_pcmcia_write_reg(sc, reg, val)
    370 	struct ncr53c9x_softc *sc;
    371 	int reg;
    372 	u_char val;
    373 {
    374 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
    375 	u_char v = val;
    376 
    377 	if (reg == NCR_CMD && v == (NCRCMD_TRANS|NCRCMD_DMA))
    378 		v = NCRCMD_TRANS;
    379 	bus_space_write_1(esc->sc_iot, esc->sc_ioh, reg, v);
    380 }
    381 
    382 int
    383 esp_pcmcia_dma_isintr(sc)
    384 	struct ncr53c9x_softc *sc;
    385 {
    386 
    387 	return NCR_READ_REG(sc, NCR_STAT) & NCRSTAT_INT;
    388 }
    389 
    390 void
    391 esp_pcmcia_dma_reset(sc)
    392 	struct ncr53c9x_softc *sc;
    393 {
    394 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
    395 
    396 	esc->sc_active = 0;
    397 	esc->sc_tc = 0;
    398 }
    399 
    400 int
    401 esp_pcmcia_dma_intr(sc)
    402 	struct ncr53c9x_softc *sc;
    403 {
    404 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
    405 	u_char	*p;
    406 	u_int	espphase, espstat, espintr;
    407 	int	cnt;
    408 
    409 	if (esc->sc_active == 0) {
    410 		printf("%s: dma_intr--inactive DMA\n", sc->sc_dev.dv_xname);
    411 		return -1;
    412 	}
    413 
    414 	if ((sc->sc_espintr & NCRINTR_BS) == 0) {
    415 		esc->sc_active = 0;
    416 		return 0;
    417 	}
    418 
    419 	cnt = *esc->sc_pdmalen;
    420 	if (*esc->sc_pdmalen == 0) {
    421 		printf("%s: data interrupt, but no count left\n",
    422 		    sc->sc_dev.dv_xname);
    423 	}
    424 
    425 	p = *esc->sc_dmaaddr;
    426 	espphase = sc->sc_phase;
    427 	espstat = (u_int) sc->sc_espstat;
    428 	espintr = (u_int) sc->sc_espintr;
    429 	do {
    430 		if (esc->sc_datain) {
    431 			*p++ = NCR_READ_REG(sc, NCR_FIFO);
    432 			cnt--;
    433 			if (espphase == DATA_IN_PHASE)
    434 				NCR_WRITE_REG(sc, NCR_CMD, NCRCMD_TRANS);
    435 			else
    436 				esc->sc_active = 0;
    437 		} else {
    438 			if (espphase == DATA_OUT_PHASE ||
    439 			    espphase == MESSAGE_OUT_PHASE) {
    440 				NCR_WRITE_REG(sc, NCR_FIFO, *p++);
    441 				cnt--;
    442 				NCR_WRITE_REG(sc, NCR_CMD, NCRCMD_TRANS);
    443 			} else
    444 				esc->sc_active = 0;
    445 		}
    446 
    447 		if (esc->sc_active) {
    448 			while (!(NCR_READ_REG(sc, NCR_STAT) & NCRSTAT_INT));
    449 			espstat = NCR_READ_REG(sc, NCR_STAT);
    450 			espintr = NCR_READ_REG(sc, NCR_INTR);
    451 			espphase = (espintr & NCRINTR_DIS)
    452 				    ? /* Disconnected */ BUSFREE_PHASE
    453 				    : espstat & PHASE_MASK;
    454 		}
    455 	} while (esc->sc_active && espintr);
    456 	sc->sc_phase = espphase;
    457 	sc->sc_espstat = (u_char) espstat;
    458 	sc->sc_espintr = (u_char) espintr;
    459 	*esc->sc_dmaaddr = p;
    460 	*esc->sc_pdmalen = cnt;
    461 
    462 	if (*esc->sc_pdmalen == 0)
    463 		esc->sc_tc = NCRSTAT_TC;
    464 	sc->sc_espstat |= esc->sc_tc;
    465 	return 0;
    466 }
    467 
    468 int
    469 esp_pcmcia_dma_setup(sc, addr, len, datain, dmasize)
    470 	struct ncr53c9x_softc *sc;
    471 	caddr_t *addr;
    472 	size_t *len;
    473 	int datain;
    474 	size_t *dmasize;
    475 {
    476 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
    477 
    478 	esc->sc_dmaaddr = addr;
    479 	esc->sc_pdmalen = len;
    480 	esc->sc_datain = datain;
    481 	esc->sc_dmasize = *dmasize;
    482 	esc->sc_tc = 0;
    483 
    484 	return 0;
    485 }
    486 
    487 void
    488 esp_pcmcia_dma_go(sc)
    489 	struct ncr53c9x_softc *sc;
    490 {
    491 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
    492 
    493 	esc->sc_active = 1;
    494 }
    495 
    496 void
    497 esp_pcmcia_dma_stop(sc)
    498 	struct ncr53c9x_softc *sc;
    499 {
    500 }
    501 
    502 int
    503 esp_pcmcia_dma_isactive(sc)
    504 	struct ncr53c9x_softc *sc;
    505 {
    506 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
    507 
    508 	return (esc->sc_active);
    509 }
    510