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