Home | History | Annotate | Line # | Download | only in pcmcia
wdc_pcmcia.c revision 1.31
      1 /*	$NetBSD: wdc_pcmcia.c,v 1.31 2000/02/21 03:55:22 enami Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1998 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, by Onno van der Linden and by Manuel Bouyer.
      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/param.h>
     40 #include <sys/device.h>
     41 #include <sys/malloc.h>
     42 #include <sys/systm.h>
     43 
     44 #include <vm/vm.h>
     45 
     46 #include <machine/bus.h>
     47 #include <machine/intr.h>
     48 
     49 #include <dev/pcmcia/pcmciareg.h>
     50 #include <dev/pcmcia/pcmciavar.h>
     51 #include <dev/pcmcia/pcmciadevs.h>
     52 
     53 #include <dev/ata/atavar.h>
     54 #include <dev/ic/wdcvar.h>
     55 
     56 #define WDC_PCMCIA_REG_NPORTS      8
     57 #define WDC_PCMCIA_AUXREG_OFFSET   (WDC_PCMCIA_REG_NPORTS + 6)
     58 #define WDC_PCMCIA_AUXREG_NPORTS   2
     59 
     60 struct wdc_pcmcia_softc {
     61 	struct wdc_softc sc_wdcdev;
     62 	struct channel_softc *wdc_chanptr;
     63 	struct channel_softc wdc_channel;
     64 	struct pcmcia_io_handle sc_pioh;
     65 	struct pcmcia_io_handle sc_auxpioh;
     66 	int sc_iowindow;
     67 	int sc_auxiowindow;
     68 	void *sc_ih;
     69 	struct pcmcia_function *sc_pf;
     70 	int sc_flags;
     71 #define	WDC_PCMCIA_ATTACH	0x0001
     72 };
     73 
     74 static int wdc_pcmcia_match	__P((struct device *, struct cfdata *, void *));
     75 static void wdc_pcmcia_attach	__P((struct device *, struct device *, void *));
     76 static int wdc_pcmcia_detach	__P((struct device *, int));
     77 
     78 struct cfattach wdc_pcmcia_ca = {
     79 	sizeof(struct wdc_pcmcia_softc), wdc_pcmcia_match, wdc_pcmcia_attach,
     80 	wdc_pcmcia_detach, wdcactivate
     81 };
     82 
     83 struct wdc_pcmcia_product {
     84 	u_int32_t	wpp_vendor;	/* vendor ID */
     85 	u_int32_t	wpp_product;	/* product ID */
     86 	int		wpp_quirk_flag;	/* Quirk flags */
     87 #define WDC_PCMCIA_NO_EXTRA_RESETS	0x02 /* Only reset ctrl once */
     88 	const char	*wpp_cis_info[4];	/* XXX necessary? */
     89 	const char	*wpp_name;	/* product name */
     90 } wdc_pcmcia_products[] = {
     91 
     92 	{ /* PCMCIA_VENDOR_DIGITAL XXX */ 0x0100,
     93 	  PCMCIA_PRODUCT_DIGITAL_MOBILE_MEDIA_CDROM,
     94 	  0, { NULL, "Digital Mobile Media CD-ROM", NULL, NULL },
     95 	  PCMCIA_STR_DIGITAL_MOBILE_MEDIA_CDROM },
     96 
     97 	{ PCMCIA_VENDOR_IBM,
     98 	  PCMCIA_PRODUCT_IBM_PORTABLE_CDROM,
     99 	  0, { NULL, "PCMCIA Portable CD-ROM Drive", NULL, NULL },
    100 	  PCMCIA_STR_IBM_PORTABLE_CDROM },
    101 
    102 	/* The TEAC IDE/Card II is used on the Sony Vaio */
    103 	{ PCMCIA_VENDOR_TEAC,
    104 	  PCMCIA_PRODUCT_TEAC_IDECARDII,
    105 	  WDC_PCMCIA_NO_EXTRA_RESETS,
    106 	  PCMCIA_CIS_TEAC_IDECARDII,
    107 	  PCMCIA_STR_TEAC_IDECARDII },
    108 
    109 	/*
    110 	 * EXP IDE/ATAPI DVD Card use with some DVD players.
    111 	 * Does not have a vendor ID or product ID.
    112 	 */
    113 	{ -1,
    114 	  -1,
    115 	  0,
    116 	  PCMCIA_CIS_EXP_EXPMULTIMEDIA,
    117 	  PCMCIA_STR_EXP_EXPMULTIMEDIA },
    118 
    119 	/* Mobile Dock 2, which doesn't have vendor ID nor product ID */
    120 	{ -1, -1, 0,
    121 	  { "SHUTTLE TECHNOLOGY LTD.", "PCCARD-IDE/ATAPI Adapter", NULL, NULL},
    122 	  "SHUTTLE TECHNOLOGY IDE/ATAPI Adapter"
    123 	},
    124 
    125 	{ 0, 0, 0, { NULL, NULL, NULL, NULL}, NULL }
    126 };
    127 
    128 struct wdc_pcmcia_disk_device_interface_args {
    129 	int ddi_type;		/* interface type */
    130 	int ddi_reqfn;		/* function we are requesting iftype */
    131 	int ddi_curfn;		/* function we are currently parsing in CIS */
    132 };
    133 
    134 int	wdc_pcmcia_disk_device_interface_callback __P((struct pcmcia_tuple *,
    135 	    void *));
    136 int	wdc_pcmcia_disk_device_interface __P((struct pcmcia_function *));
    137 struct wdc_pcmcia_product *
    138 	wdc_pcmcia_lookup __P((struct pcmcia_attach_args *));
    139 
    140 int	wdc_pcmcia_enable __P((void *, int));
    141 
    142 int
    143 wdc_pcmcia_disk_device_interface_callback(tuple, arg)
    144 	struct pcmcia_tuple *tuple;
    145 	void *arg;
    146 {
    147 	struct wdc_pcmcia_disk_device_interface_args *ddi = arg;
    148 
    149 	switch (tuple->code) {
    150 	case PCMCIA_CISTPL_FUNCID:
    151 		ddi->ddi_curfn++;
    152 		break;
    153 
    154 	case PCMCIA_CISTPL_FUNCE:
    155 		if (ddi->ddi_reqfn != ddi->ddi_curfn)
    156 			break;
    157 
    158 		/* subcode (disk device interface), data (interface type) */
    159 		if (tuple->length < 2)
    160 			break;
    161 
    162 		/* check type */
    163 		if (pcmcia_tuple_read_1(tuple, 0) !=
    164 		    PCMCIA_TPLFE_TYPE_DISK_DEVICE_INTERFACE)
    165 			break;
    166 
    167 		ddi->ddi_type = pcmcia_tuple_read_1(tuple, 1);
    168 		return (1);
    169 	}
    170 	return (0);
    171 }
    172 
    173 int
    174 wdc_pcmcia_disk_device_interface(pf)
    175 	struct pcmcia_function *pf;
    176 {
    177 	struct wdc_pcmcia_disk_device_interface_args ddi;
    178 
    179 	ddi.ddi_reqfn = pf->number;
    180 	ddi.ddi_curfn = -1;
    181 	if (pcmcia_scan_cis((struct device *)pf->sc,
    182 	    wdc_pcmcia_disk_device_interface_callback, &ddi) > 0)
    183 		return (ddi.ddi_type);
    184 	else
    185 		return (-1);
    186 }
    187 
    188 struct wdc_pcmcia_product *
    189 wdc_pcmcia_lookup(pa)
    190 	struct pcmcia_attach_args *pa;
    191 {
    192 	struct wdc_pcmcia_product *wpp;
    193 	int i, cis_match;
    194 
    195 	for (wpp = wdc_pcmcia_products; wpp->wpp_name != NULL; wpp++)
    196 		if ((wpp->wpp_vendor == -1 ||
    197 		     pa->manufacturer == wpp->wpp_vendor) &&
    198 		    (wpp->wpp_product == -1 ||
    199 		     pa->product == wpp->wpp_product)) {
    200 			cis_match = 1;
    201 			for (i = 0; i < 4; i++) {
    202 				if (!(wpp->wpp_cis_info[i] == NULL ||
    203 				      (pa->card->cis1_info[i] != NULL &&
    204 				       strcmp(pa->card->cis1_info[i],
    205 					      wpp->wpp_cis_info[i]) == 0)))
    206 					cis_match = 0;
    207 			}
    208 			if (cis_match)
    209 				return (wpp);
    210 		}
    211 
    212 	return (NULL);
    213 }
    214 
    215 static int
    216 wdc_pcmcia_match(parent, match, aux)
    217 	struct device *parent;
    218 	struct cfdata *match;
    219 	void *aux;
    220 {
    221 	struct pcmcia_attach_args *pa = aux;
    222 	struct pcmcia_softc *sc;
    223 	int iftype;
    224 
    225 	if (wdc_pcmcia_lookup(pa) != NULL)
    226 		return (1);
    227 
    228 	if (pa->pf->function == PCMCIA_FUNCTION_DISK) {
    229 		sc = pa->pf->sc;
    230 
    231 		pcmcia_chip_socket_enable(sc->pct, sc->pch);
    232 		iftype = wdc_pcmcia_disk_device_interface(pa->pf);
    233 		pcmcia_chip_socket_disable(sc->pct, sc->pch);
    234 
    235 		if (iftype == PCMCIA_TPLFE_DDI_PCCARD_ATA)
    236 			return (1);
    237 	}
    238 
    239 	return (0);
    240 }
    241 
    242 static void
    243 wdc_pcmcia_attach(parent, self, aux)
    244 	struct device *parent;
    245 	struct device *self;
    246 	void *aux;
    247 {
    248 	struct wdc_pcmcia_softc *sc = (void *)self;
    249 	struct pcmcia_attach_args *pa = aux;
    250 	struct pcmcia_config_entry *cfe;
    251 	struct wdc_pcmcia_product *wpp;
    252 	int quirks;
    253 
    254 	sc->sc_pf = pa->pf;
    255 
    256 	for (cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head); cfe != NULL;
    257 	    cfe = SIMPLEQ_NEXT(cfe, cfe_list)) {
    258 		if (cfe->num_iospace != 1 && cfe->num_iospace != 2)
    259 			continue;
    260 
    261 		if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
    262 		    cfe->iospace[0].length,
    263 		    cfe->iospace[0].start == 0 ? cfe->iospace[0].length : 0,
    264 		    &sc->sc_pioh))
    265 			continue;
    266 
    267 		if (cfe->num_iospace == 2) {
    268 			if (!pcmcia_io_alloc(pa->pf, cfe->iospace[1].start,
    269 			    cfe->iospace[1].length, 0, &sc->sc_auxpioh))
    270 				break;
    271 		} else /* num_iospace == 1 */ {
    272 			sc->sc_auxpioh.iot = sc->sc_pioh.iot;
    273 			if (!bus_space_subregion(sc->sc_pioh.iot,
    274 			    sc->sc_pioh.ioh, WDC_PCMCIA_AUXREG_OFFSET,
    275 			    WDC_PCMCIA_AUXREG_NPORTS, &sc->sc_auxpioh.ioh))
    276 				break;
    277 		}
    278 		pcmcia_io_free(pa->pf, &sc->sc_pioh);
    279 	}
    280 
    281 	if (cfe == NULL) {
    282 		printf(": can't handle card info\n");
    283 		goto no_config_entry;
    284 	}
    285 
    286 	/* Enable the card. */
    287 	pcmcia_function_init(pa->pf, cfe);
    288 	if (pcmcia_function_enable(pa->pf)) {
    289 		printf(": function enable failed\n");
    290 		goto enable_failed;
    291 	}
    292 
    293 	wpp = wdc_pcmcia_lookup(pa);
    294 	if (wpp != NULL)
    295 		quirks = wpp->wpp_quirk_flag;
    296 	else
    297 		quirks = 0;
    298 
    299 	if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, 0,
    300 	    sc->sc_pioh.size, &sc->sc_pioh, &sc->sc_iowindow)) {
    301 		printf(": can't map first I/O space\n");
    302 		goto iomap_failed;
    303 	}
    304 
    305 	if (cfe->num_iospace <= 1)
    306 		sc->sc_auxiowindow = -1;
    307 	else if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, 0,
    308 	    sc->sc_auxpioh.size, &sc->sc_auxpioh, &sc->sc_auxiowindow)) {
    309 		printf(": can't map second I/O space\n");
    310 		goto iomapaux_failed;
    311 	}
    312 
    313 	if ((wpp != NULL) && (wpp->wpp_name != NULL))
    314 		printf(": %s", wpp->wpp_name);
    315 
    316 	printf("\n");
    317 
    318 	sc->wdc_channel.cmd_iot = sc->sc_pioh.iot;
    319 	sc->wdc_channel.cmd_ioh = sc->sc_pioh.ioh;
    320 	sc->wdc_channel.ctl_iot = sc->sc_auxpioh.iot;
    321 	sc->wdc_channel.ctl_ioh = sc->sc_auxpioh.ioh;
    322 	sc->wdc_channel.data32iot = sc->wdc_channel.cmd_iot;
    323 	sc->wdc_channel.data32ioh = sc->wdc_channel.cmd_ioh;
    324 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32;
    325 	sc->sc_wdcdev.PIO_cap = 0;
    326 	sc->wdc_chanptr = &sc->wdc_channel;
    327 	sc->sc_wdcdev.channels = &sc->wdc_chanptr;
    328 	sc->sc_wdcdev.nchannels = 1;
    329 	sc->wdc_channel.channel = 0;
    330 	sc->wdc_channel.wdc = &sc->sc_wdcdev;
    331 	sc->wdc_channel.ch_queue = malloc(sizeof(struct channel_queue),
    332 	    M_DEVBUF, M_NOWAIT);
    333 	if (sc->wdc_channel.ch_queue == NULL) {
    334 		printf("%s: can't allocate memory for command queue\n",
    335 		    sc->sc_wdcdev.sc_dev.dv_xname);
    336 		goto ch_queue_alloc_failed;
    337 	}
    338 	if (quirks & WDC_PCMCIA_NO_EXTRA_RESETS)
    339 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_NO_EXTRA_RESETS;
    340 
    341 	/* We can enable and disable the controller. */
    342 	sc->sc_wdcdev.sc_atapi_adapter.scsipi_enable = wdc_pcmcia_enable;
    343 
    344 	sc->sc_flags |= WDC_PCMCIA_ATTACH;
    345 	wdcattach(&sc->wdc_channel);	/* should return an error XXX */
    346 	sc->sc_flags &= ~WDC_PCMCIA_ATTACH;
    347 	return;
    348 
    349  ch_queue_alloc_failed:
    350 	/* Unmap our aux i/o window. */
    351 	if (sc->sc_auxiowindow != -1)
    352 		pcmcia_io_unmap(sc->sc_pf, sc->sc_auxiowindow);
    353 
    354  iomapaux_failed:
    355 	/* Unmap our i/o window. */
    356 	pcmcia_io_unmap(sc->sc_pf, sc->sc_iowindow);
    357 
    358  iomap_failed:
    359 	/* Disable the function */
    360 	pcmcia_function_disable(sc->sc_pf);
    361 
    362  enable_failed:
    363 	/* Unmap our i/o space. */
    364 	pcmcia_io_free(sc->sc_pf, &sc->sc_pioh);
    365 	if (cfe->num_iospace == 2)
    366 		pcmcia_io_free(sc->sc_pf, &sc->sc_auxpioh);
    367 
    368  no_config_entry:
    369 	sc->sc_iowindow = -1;
    370 }
    371 
    372 int
    373 wdc_pcmcia_detach(self, flags)
    374 	struct device *self;
    375 	int flags;
    376 {
    377 	struct wdc_pcmcia_softc *sc = (struct wdc_pcmcia_softc *)self;
    378 	int error;
    379 
    380 	if (sc->sc_iowindow == -1)
    381 		/* Nothing to detach */
    382 		return (0);
    383 
    384 	if ((error = wdcdetach(self, flags)) != 0)
    385 		return (error);
    386 
    387 	if (sc->wdc_channel.ch_queue != NULL)
    388 		free(sc->wdc_channel.ch_queue, M_DEVBUF);
    389 
    390 	/* Unmap our i/o window and i/o space. */
    391 	pcmcia_io_unmap(sc->sc_pf, sc->sc_iowindow);
    392 	pcmcia_io_free(sc->sc_pf, &sc->sc_pioh);
    393 	if (sc->sc_auxiowindow != -1) {
    394 		pcmcia_io_unmap(sc->sc_pf, sc->sc_auxiowindow);
    395 		pcmcia_io_free(sc->sc_pf, &sc->sc_auxpioh);
    396 	}
    397 
    398 	return (0);
    399 }
    400 
    401 int
    402 wdc_pcmcia_enable(arg, onoff)
    403 	void *arg;
    404 	int onoff;
    405 {
    406 	struct wdc_pcmcia_softc *sc = arg;
    407 
    408 	if (onoff) {
    409 		/* See the comment in aic_pcmcia_enable */
    410 		if ((sc->sc_flags & WDC_PCMCIA_ATTACH) == 0) {
    411 			/* Establish the interrupt handler. */
    412 			sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
    413 			    wdcintr, &sc->wdc_channel);
    414 			if (sc->sc_ih == NULL) {
    415 				printf("%s: "
    416 				    "couldn't establish interrupt handler\n",
    417 				    sc->sc_wdcdev.sc_dev.dv_xname);
    418 				return (EIO);
    419 			}
    420 
    421 			if (pcmcia_function_enable(sc->sc_pf)) {
    422 				printf("%s: couldn't enable PCMCIA function\n",
    423 				    sc->sc_wdcdev.sc_dev.dv_xname);
    424 				pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    425 				return (EIO);
    426 			}
    427 			wdcreset(&sc->wdc_channel, VERBOSE);
    428 		}
    429 	} else {
    430 		pcmcia_function_disable(sc->sc_pf);
    431 		if ((sc->sc_flags & WDC_PCMCIA_ATTACH) == 0)
    432 			pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    433 	}
    434 
    435 	return (0);
    436 }
    437