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