Home | History | Annotate | Line # | Download | only in pcmcia
esp_pcmcia.c revision 1.21
      1 /*	$NetBSD: esp_pcmcia.c,v 1.21 2004/08/10 18:39:08 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.21 2004/08/10 18:39:08 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 struct ncr53c9x_glue esp_pcmcia_glue = {
    115 	esp_pcmcia_read_reg,
    116 	esp_pcmcia_write_reg,
    117 	esp_pcmcia_dma_isintr,
    118 	esp_pcmcia_dma_reset,
    119 	esp_pcmcia_dma_intr,
    120 	esp_pcmcia_dma_setup,
    121 	esp_pcmcia_dma_go,
    122 	esp_pcmcia_dma_stop,
    123 	esp_pcmcia_dma_isactive,
    124 	NULL,			/* gl_clear_latched_intr */
    125 };
    126 
    127 const struct pcmcia_product esp_pcmcia_products[] = {
    128 	{ PCMCIA_VENDOR_PANASONIC, PCMCIA_PRODUCT_PANASONIC_KXLC002,
    129 	  PCMCIA_CIS_PANASONIC_KXLC002 },
    130 
    131 	{ PCMCIA_VENDOR_RATOC, PCMCIA_PRODUCT_RATOC_REX_9530,
    132 	  PCMCIA_CIS_RATOC_REX_9530 },
    133 };
    134 const size_t esp_pcmcia_nproducts =
    135     sizeof(esp_pcmcia_products) / sizeof(esp_pcmcia_products[0]);
    136 
    137 int
    138 esp_pcmcia_match(parent, match, aux)
    139 	struct device *parent;
    140 	struct cfdata *match;
    141 	void *aux;
    142 {
    143 	struct pcmcia_attach_args *pa = aux;
    144 
    145 	if (pcmcia_product_lookup(pa, esp_pcmcia_products, esp_pcmcia_nproducts,
    146 	    sizeof(esp_pcmcia_products[0]), NULL))
    147 		return (1);
    148 	return (0);
    149 }
    150 
    151 int
    152 esp_pcmcia_validate_config(cfe)
    153 	struct pcmcia_config_entry *cfe;
    154 {
    155 	if (cfe->iftype != PCMCIA_IFTYPE_IO ||
    156 	    cfe->num_memspace != 0 ||
    157 	    cfe->num_iospace != 1)
    158 		return (EINVAL);
    159 	return (0);
    160 }
    161 
    162 void
    163 esp_pcmcia_attach(parent, self, aux)
    164 	struct device *parent, *self;
    165 	void *aux;
    166 {
    167 	struct esp_pcmcia_softc *esc = (void *)self;
    168 	struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
    169 	struct pcmcia_attach_args *pa = aux;
    170 	struct pcmcia_config_entry *cfe;
    171 	struct pcmcia_function *pf = pa->pf;
    172 	int error;
    173 
    174 	aprint_normal("\n");
    175 	esc->sc_pf = pf;
    176 
    177 	error = pcmcia_function_configure(pf, esp_pcmcia_validate_config);
    178 	if (error) {
    179 		aprint_error("%s: configure failed, error=%d\n", self->dv_xname,
    180 		    error);
    181 		return;
    182 	}
    183 
    184 	cfe = pf->cfe;
    185 	esc->sc_iot = cfe->iospace[0].handle.iot;
    186 	esc->sc_ioh = cfe->iospace[0].handle.ioh;
    187 	esp_pcmcia_init(esc);
    188 
    189 	error = esp_pcmcia_enable(self, 1);
    190 	if (error)
    191 		goto fail;
    192 
    193 	sc->sc_adapter.adapt_minphys = minphys;
    194 	sc->sc_adapter.adapt_request = ncr53c9x_scsipi_request;
    195 	sc->sc_adapter.adapt_enable = esp_pcmcia_enable;
    196 
    197 	esc->sc_state = ESP_PCMCIA_ATTACH1;
    198 	ncr53c9x_attach(sc);
    199 	if (esc->sc_state == ESP_PCMCIA_ATTACH1)
    200 		esp_pcmcia_enable(self, 0);
    201 	esc->sc_state = ESP_PCMCIA_ATTACHED;
    202 	return;
    203 
    204 fail:
    205 	pcmcia_function_unconfigure(pf);
    206 }
    207 
    208 void
    209 esp_pcmcia_init(esc)
    210 	struct esp_pcmcia_softc *esc;
    211 {
    212 	struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
    213 
    214 	/* id 7, clock 40M, parity ON, sync OFF, fast ON, slow ON */
    215 
    216 	sc->sc_glue = &esp_pcmcia_glue;
    217 
    218 #ifdef ESP_PCMCIA_POLL
    219 	callout_init(&esc->sc_poll_ch);
    220 #endif
    221 
    222 	sc->sc_rev = NCR_VARIANT_ESP406;
    223 	sc->sc_id = 7;
    224 	sc->sc_freq = 40;
    225 	/* try -PARENB -SLOW */
    226 	sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB | NCRCFG1_SLOW;
    227 	/* try +FE */
    228 	sc->sc_cfg2 = NCRCFG2_SCSI2;
    229 	/* try -IDM -FSCSI -FCLK */
    230 	sc->sc_cfg3 = NCRESPCFG3_CDB | NCRESPCFG3_FCLK | NCRESPCFG3_IDM |
    231 	    NCRESPCFG3_FSCSI;
    232 	sc->sc_cfg4 = NCRCFG4_ACTNEG;
    233 	/* try +INTP */
    234 	sc->sc_cfg5 = NCRCFG5_CRS1 | NCRCFG5_AADDR | NCRCFG5_PTRINC;
    235 	sc->sc_minsync = 0;
    236 	sc->sc_maxxfer = 64 * 1024;
    237 }
    238 
    239 int
    240 esp_pcmcia_detach(self, flags)
    241 	struct device *self;
    242 	int flags;
    243 {
    244 	struct esp_pcmcia_softc *sc = (void *)self;
    245 	int error;
    246 
    247 	if (sc->sc_state != ESP_PCMCIA_ATTACHED)
    248 		return (0);
    249 
    250 	error = ncr53c9x_detach(&sc->sc_ncr53c9x, flags);
    251 	if (error)
    252 		return (error);
    253 
    254 	pcmcia_function_unconfigure(sc->sc_pf);
    255 
    256 	return (0);
    257 }
    258 
    259 int
    260 esp_pcmcia_enable(arg, onoff)
    261 	struct device *arg;
    262 	int onoff;
    263 {
    264 	struct esp_pcmcia_softc *sc = (void *)arg;
    265 	int error;
    266 
    267 	if (onoff) {
    268 		/*
    269 		 * If attach is in progress, we already have the device
    270 		 * powered up.
    271 		 */
    272 		if (sc->sc_state == ESP_PCMCIA_ATTACH1) {
    273 			sc->sc_state = ESP_PCMCIA_ATTACH2;
    274 		} else {
    275 #ifdef ESP_PCMCIA_POLL
    276 			callout_reset(&sc->sc_poll_ch, 1, esp_pcmcia_poll, sc);
    277 #else
    278 			/* Establish the interrupt handler. */
    279 			sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
    280 			    ncr53c9x_intr, &sc->sc_ncr53c9x);
    281 			if (!sc->sc_ih)
    282 				return (EIO);
    283 #endif
    284 
    285 			error = pcmcia_function_enable(sc->sc_pf);
    286 			if (error) {
    287 				pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    288 				sc->sc_ih = 0;
    289 				return (error);
    290 			}
    291 
    292 			/* Initialize only chip.  */
    293 			ncr53c9x_init(&sc->sc_ncr53c9x, 0);
    294 		}
    295 	} else {
    296 		pcmcia_function_disable(sc->sc_pf);
    297 #ifdef ESP_PCMCIA_POLL
    298 		callout_stop(&sc->sc_poll_ch);
    299 #else
    300 		pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    301 		sc->sc_ih = 0;
    302 #endif
    303 	}
    304 
    305 	return (0);
    306 }
    307 
    308 #ifdef ESP_PCMCIA_POLL
    309 void
    310 esp_pcmcia_poll(arg)
    311 	void *arg;
    312 {
    313 	struct esp_pcmcia_softc *esc = arg;
    314 
    315 	(void) ncr53c9x_intr(&esc->sc_ncr53c9x);
    316 	callout_reset(&esc->sc_poll_ch, 1, esp_pcmcia_poll, esc);
    317 }
    318 #endif
    319 
    320 /*
    321  * Glue functions.
    322  */
    323 u_char
    324 esp_pcmcia_read_reg(sc, reg)
    325 	struct ncr53c9x_softc *sc;
    326 	int reg;
    327 {
    328 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
    329 	u_char v;
    330 
    331 	v = bus_space_read_1(esc->sc_iot, esc->sc_ioh, reg);
    332 	return v;
    333 }
    334 
    335 void
    336 esp_pcmcia_write_reg(sc, reg, val)
    337 	struct ncr53c9x_softc *sc;
    338 	int reg;
    339 	u_char val;
    340 {
    341 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
    342 	u_char v = val;
    343 
    344 	if (reg == NCR_CMD && v == (NCRCMD_TRANS|NCRCMD_DMA))
    345 		v = NCRCMD_TRANS;
    346 	bus_space_write_1(esc->sc_iot, esc->sc_ioh, reg, v);
    347 }
    348 
    349 int
    350 esp_pcmcia_dma_isintr(sc)
    351 	struct ncr53c9x_softc *sc;
    352 {
    353 
    354 	return NCR_READ_REG(sc, NCR_STAT) & NCRSTAT_INT;
    355 }
    356 
    357 void
    358 esp_pcmcia_dma_reset(sc)
    359 	struct ncr53c9x_softc *sc;
    360 {
    361 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
    362 
    363 	esc->sc_active = 0;
    364 	esc->sc_tc = 0;
    365 }
    366 
    367 int
    368 esp_pcmcia_dma_intr(sc)
    369 	struct ncr53c9x_softc *sc;
    370 {
    371 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
    372 	u_char	*p;
    373 	u_int	espphase, espstat, espintr;
    374 	int	cnt;
    375 
    376 	if (esc->sc_active == 0) {
    377 		printf("%s: dma_intr--inactive DMA\n", sc->sc_dev.dv_xname);
    378 		return -1;
    379 	}
    380 
    381 	if ((sc->sc_espintr & NCRINTR_BS) == 0) {
    382 		esc->sc_active = 0;
    383 		return 0;
    384 	}
    385 
    386 	cnt = *esc->sc_pdmalen;
    387 	if (*esc->sc_pdmalen == 0) {
    388 		printf("%s: data interrupt, but no count left\n",
    389 		    sc->sc_dev.dv_xname);
    390 	}
    391 
    392 	p = *esc->sc_dmaaddr;
    393 	espphase = sc->sc_phase;
    394 	espstat = (u_int) sc->sc_espstat;
    395 	espintr = (u_int) sc->sc_espintr;
    396 	do {
    397 		if (esc->sc_datain) {
    398 			*p++ = NCR_READ_REG(sc, NCR_FIFO);
    399 			cnt--;
    400 			if (espphase == DATA_IN_PHASE)
    401 				NCR_WRITE_REG(sc, NCR_CMD, NCRCMD_TRANS);
    402 			else
    403 				esc->sc_active = 0;
    404 		} else {
    405 			if (espphase == DATA_OUT_PHASE ||
    406 			    espphase == MESSAGE_OUT_PHASE) {
    407 				NCR_WRITE_REG(sc, NCR_FIFO, *p++);
    408 				cnt--;
    409 				NCR_WRITE_REG(sc, NCR_CMD, NCRCMD_TRANS);
    410 			} else
    411 				esc->sc_active = 0;
    412 		}
    413 
    414 		if (esc->sc_active) {
    415 			while (!(NCR_READ_REG(sc, NCR_STAT) & NCRSTAT_INT));
    416 			espstat = NCR_READ_REG(sc, NCR_STAT);
    417 			espintr = NCR_READ_REG(sc, NCR_INTR);
    418 			espphase = (espintr & NCRINTR_DIS)
    419 				    ? /* Disconnected */ BUSFREE_PHASE
    420 				    : espstat & PHASE_MASK;
    421 		}
    422 	} while (esc->sc_active && espintr);
    423 	sc->sc_phase = espphase;
    424 	sc->sc_espstat = (u_char) espstat;
    425 	sc->sc_espintr = (u_char) espintr;
    426 	*esc->sc_dmaaddr = p;
    427 	*esc->sc_pdmalen = cnt;
    428 
    429 	if (*esc->sc_pdmalen == 0)
    430 		esc->sc_tc = NCRSTAT_TC;
    431 	sc->sc_espstat |= esc->sc_tc;
    432 	return 0;
    433 }
    434 
    435 int
    436 esp_pcmcia_dma_setup(sc, addr, len, datain, dmasize)
    437 	struct ncr53c9x_softc *sc;
    438 	caddr_t *addr;
    439 	size_t *len;
    440 	int datain;
    441 	size_t *dmasize;
    442 {
    443 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
    444 
    445 	esc->sc_dmaaddr = addr;
    446 	esc->sc_pdmalen = len;
    447 	esc->sc_datain = datain;
    448 	esc->sc_dmasize = *dmasize;
    449 	esc->sc_tc = 0;
    450 
    451 	return 0;
    452 }
    453 
    454 void
    455 esp_pcmcia_dma_go(sc)
    456 	struct ncr53c9x_softc *sc;
    457 {
    458 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
    459 
    460 	esc->sc_active = 1;
    461 }
    462 
    463 void
    464 esp_pcmcia_dma_stop(sc)
    465 	struct ncr53c9x_softc *sc;
    466 {
    467 }
    468 
    469 int
    470 esp_pcmcia_dma_isactive(sc)
    471 	struct ncr53c9x_softc *sc;
    472 {
    473 	struct esp_pcmcia_softc *esc = (struct esp_pcmcia_softc *)sc;
    474 
    475 	return (esc->sc_active);
    476 }
    477