Home | History | Annotate | Line # | Download | only in pcmcia
wdc_pcmcia.c revision 1.40
      1 /*	$NetBSD: wdc_pcmcia.c,v 1.40 2001/02/21 21:39:56 jdolecek 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 const 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, neither 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, neither vendor ID nor product ID */
    124 	{ -1, -1, 0,
    125 	  { "FREECOM", "PCCARD-IDE", NULL, NULL},
    126 	  "FREECOM PCCARD-IDE"
    127 	},
    128 
    129 	/* Random CD-ROM, (badged AMACOM), neither vendor ID nor product ID */
    130 	{ -1, -1, 0,
    131 	  { "PCMCIA", "CD-ROM", NULL, NULL},
    132 	  "PCMCIA CD-ROM"
    133 	},
    134 
    135 	/* IO DATA CBIDE2, with neither vendor ID nor product ID */
    136 	{ -1, -1, 0,
    137 	  PCMCIA_CIS_IODATA_CBIDE2,
    138 	  PCMCIA_STR_IODATA_CBIDE2
    139 	},
    140 
    141 	/*
    142 	 * Novac PCMCIA-IDE Card for HD530P IDE Box,
    143 	 * with neither vendor ID nor product ID
    144 	 */
    145 	{ -1, -1, 0,
    146 	  { "PCMCIA", "PnPIDE", NULL, NULL},
    147 	  "Novac PCCARD-IDE"
    148 	},
    149 
    150 	{ 0, 0, 0, { NULL, NULL, NULL, NULL}, NULL }
    151 };
    152 
    153 const struct wdc_pcmcia_product *
    154 	wdc_pcmcia_lookup __P((struct pcmcia_attach_args *));
    155 
    156 int	wdc_pcmcia_enable __P((void *, int));
    157 
    158 
    159 
    160 const struct wdc_pcmcia_product *
    161 wdc_pcmcia_lookup(pa)
    162 	struct pcmcia_attach_args *pa;
    163 {
    164 	const struct wdc_pcmcia_product *wpp;
    165 	int i, cis_match;
    166 
    167 	for (wpp = wdc_pcmcia_products; wpp->wpp_name != NULL; wpp++)
    168 		if ((wpp->wpp_vendor == -1 ||
    169 		     pa->manufacturer == wpp->wpp_vendor) &&
    170 		    (wpp->wpp_product == -1 ||
    171 		     pa->product == wpp->wpp_product)) {
    172 			cis_match = 1;
    173 			for (i = 0; i < 4; i++) {
    174 				if (!(wpp->wpp_cis_info[i] == NULL ||
    175 				      (pa->card->cis1_info[i] != NULL &&
    176 				       strcmp(pa->card->cis1_info[i],
    177 					      wpp->wpp_cis_info[i]) == 0)))
    178 					cis_match = 0;
    179 			}
    180 			if (cis_match)
    181 				return (wpp);
    182 		}
    183 
    184 	return (NULL);
    185 }
    186 
    187 static int
    188 wdc_pcmcia_match(parent, match, aux)
    189 	struct device *parent;
    190 	struct cfdata *match;
    191 	void *aux;
    192 {
    193 	struct pcmcia_attach_args *pa = aux;
    194 
    195 	if (pa->pf->function == PCMCIA_FUNCTION_DISK &&
    196 	    pa->pf->pf_funce_disk_interface == PCMCIA_TPLFE_DDI_PCCARD_ATA) {
    197 		return 10;
    198 	}
    199 
    200 	if (wdc_pcmcia_lookup(pa) != NULL)
    201 		return (1);
    202 
    203 	return (0);
    204 }
    205 
    206 static void
    207 wdc_pcmcia_attach(parent, self, aux)
    208 	struct device *parent;
    209 	struct device *self;
    210 	void *aux;
    211 {
    212 	struct wdc_pcmcia_softc *sc = (void *)self;
    213 	struct pcmcia_attach_args *pa = aux;
    214 	struct pcmcia_config_entry *cfe;
    215 	const struct wdc_pcmcia_product *wpp;
    216 	int quirks;
    217 
    218 	sc->sc_pf = pa->pf;
    219 
    220 	for (cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head); cfe != NULL;
    221 	    cfe = SIMPLEQ_NEXT(cfe, cfe_list)) {
    222 		if (cfe->num_iospace != 1 && cfe->num_iospace != 2)
    223 			continue;
    224 
    225 		if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
    226 		    cfe->iospace[0].length,
    227 		    cfe->iospace[0].start == 0 ? cfe->iospace[0].length : 0,
    228 		    &sc->sc_pioh))
    229 			continue;
    230 
    231 		if (cfe->num_iospace == 2) {
    232 			if (!pcmcia_io_alloc(pa->pf, cfe->iospace[1].start,
    233 			    cfe->iospace[1].length, 0, &sc->sc_auxpioh))
    234 				break;
    235 		} else /* num_iospace == 1 */ {
    236 			sc->sc_auxpioh.iot = sc->sc_pioh.iot;
    237 			if (!bus_space_subregion(sc->sc_pioh.iot,
    238 			    sc->sc_pioh.ioh, WDC_PCMCIA_AUXREG_OFFSET,
    239 			    WDC_PCMCIA_AUXREG_NPORTS, &sc->sc_auxpioh.ioh))
    240 				break;
    241 		}
    242 		pcmcia_io_free(pa->pf, &sc->sc_pioh);
    243 	}
    244 
    245 	if (cfe == NULL) {
    246 		printf(": can't handle card info\n");
    247 		goto no_config_entry;
    248 	}
    249 
    250 	/* Enable the card. */
    251 	pcmcia_function_init(pa->pf, cfe);
    252 	if (pcmcia_function_enable(pa->pf)) {
    253 		printf(": function enable failed\n");
    254 		goto enable_failed;
    255 	}
    256 
    257 	wpp = wdc_pcmcia_lookup(pa);
    258 	if (wpp != NULL)
    259 		quirks = wpp->wpp_quirk_flag;
    260 	else
    261 		quirks = 0;
    262 
    263 	if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, 0,
    264 	    sc->sc_pioh.size, &sc->sc_pioh, &sc->sc_iowindow)) {
    265 		printf(": can't map first I/O space\n");
    266 		goto iomap_failed;
    267 	}
    268 
    269 	if (cfe->num_iospace <= 1)
    270 		sc->sc_auxiowindow = -1;
    271 	else if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, 0,
    272 	    sc->sc_auxpioh.size, &sc->sc_auxpioh, &sc->sc_auxiowindow)) {
    273 		printf(": can't map second I/O space\n");
    274 		goto iomapaux_failed;
    275 	}
    276 
    277 	if ((wpp != NULL) && (wpp->wpp_name != NULL))
    278 		printf(": %s", wpp->wpp_name);
    279 
    280 	printf("\n");
    281 
    282 	sc->wdc_channel.cmd_iot = sc->sc_pioh.iot;
    283 	sc->wdc_channel.cmd_ioh = sc->sc_pioh.ioh;
    284 	sc->wdc_channel.ctl_iot = sc->sc_auxpioh.iot;
    285 	sc->wdc_channel.ctl_ioh = sc->sc_auxpioh.ioh;
    286 	sc->wdc_channel.data32iot = sc->wdc_channel.cmd_iot;
    287 	sc->wdc_channel.data32ioh = sc->wdc_channel.cmd_ioh;
    288 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32;
    289 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_SINGLE_DRIVE;
    290 	sc->sc_wdcdev.PIO_cap = 0;
    291 	sc->wdc_chanptr = &sc->wdc_channel;
    292 	sc->sc_wdcdev.channels = &sc->wdc_chanptr;
    293 	sc->sc_wdcdev.nchannels = 1;
    294 	sc->wdc_channel.channel = 0;
    295 	sc->wdc_channel.wdc = &sc->sc_wdcdev;
    296 	sc->wdc_channel.ch_queue = malloc(sizeof(struct channel_queue),
    297 	    M_DEVBUF, M_NOWAIT);
    298 	if (sc->wdc_channel.ch_queue == NULL) {
    299 		printf("%s: can't allocate memory for command queue\n",
    300 		    sc->sc_wdcdev.sc_dev.dv_xname);
    301 		goto ch_queue_alloc_failed;
    302 	}
    303 	if (quirks & WDC_PCMCIA_NO_EXTRA_RESETS)
    304 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_NO_EXTRA_RESETS;
    305 
    306 	/* We can enable and disable the controller. */
    307 	sc->sc_wdcdev.sc_atapi_adapter._generic.scsipi_enable =
    308 	    wdc_pcmcia_enable;
    309 
    310 	sc->sc_flags |= WDC_PCMCIA_ATTACH;
    311 	wdcattach(&sc->wdc_channel);	/* should return an error XXX */
    312 	sc->sc_flags &= ~WDC_PCMCIA_ATTACH;
    313 	return;
    314 
    315  ch_queue_alloc_failed:
    316 	/* Unmap our aux i/o window. */
    317 	if (sc->sc_auxiowindow != -1)
    318 		pcmcia_io_unmap(sc->sc_pf, sc->sc_auxiowindow);
    319 
    320  iomapaux_failed:
    321 	/* Unmap our i/o window. */
    322 	pcmcia_io_unmap(sc->sc_pf, sc->sc_iowindow);
    323 
    324  iomap_failed:
    325 	/* Disable the function */
    326 	pcmcia_function_disable(sc->sc_pf);
    327 
    328  enable_failed:
    329 	/* Unmap our i/o space. */
    330 	pcmcia_io_free(sc->sc_pf, &sc->sc_pioh);
    331 	if (cfe->num_iospace == 2)
    332 		pcmcia_io_free(sc->sc_pf, &sc->sc_auxpioh);
    333 
    334  no_config_entry:
    335 	sc->sc_iowindow = -1;
    336 }
    337 
    338 int
    339 wdc_pcmcia_detach(self, flags)
    340 	struct device *self;
    341 	int flags;
    342 {
    343 	struct wdc_pcmcia_softc *sc = (struct wdc_pcmcia_softc *)self;
    344 	int error;
    345 
    346 	if (sc->sc_iowindow == -1)
    347 		/* Nothing to detach */
    348 		return (0);
    349 
    350 	if ((error = wdcdetach(self, flags)) != 0)
    351 		return (error);
    352 
    353 	if (sc->wdc_channel.ch_queue != NULL)
    354 		free(sc->wdc_channel.ch_queue, M_DEVBUF);
    355 
    356 	/* Unmap our i/o window and i/o space. */
    357 	pcmcia_io_unmap(sc->sc_pf, sc->sc_iowindow);
    358 	pcmcia_io_free(sc->sc_pf, &sc->sc_pioh);
    359 	if (sc->sc_auxiowindow != -1) {
    360 		pcmcia_io_unmap(sc->sc_pf, sc->sc_auxiowindow);
    361 		pcmcia_io_free(sc->sc_pf, &sc->sc_auxpioh);
    362 	}
    363 
    364 	return (0);
    365 }
    366 
    367 int
    368 wdc_pcmcia_enable(arg, onoff)
    369 	void *arg;
    370 	int onoff;
    371 {
    372 	struct wdc_pcmcia_softc *sc = arg;
    373 
    374 	if (onoff) {
    375 		/* See the comment in aic_pcmcia_enable */
    376 		if ((sc->sc_flags & WDC_PCMCIA_ATTACH) == 0) {
    377 			/* Establish the interrupt handler. */
    378 			sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
    379 			    wdcintr, &sc->wdc_channel);
    380 			if (sc->sc_ih == NULL) {
    381 				printf("%s: "
    382 				    "couldn't establish interrupt handler\n",
    383 				    sc->sc_wdcdev.sc_dev.dv_xname);
    384 				return (EIO);
    385 			}
    386 
    387 			if (pcmcia_function_enable(sc->sc_pf)) {
    388 				printf("%s: couldn't enable PCMCIA function\n",
    389 				    sc->sc_wdcdev.sc_dev.dv_xname);
    390 				pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    391 				return (EIO);
    392 			}
    393 			wdcreset(&sc->wdc_channel, VERBOSE);
    394 		}
    395 	} else {
    396 		pcmcia_function_disable(sc->sc_pf);
    397 		if ((sc->sc_flags & WDC_PCMCIA_ATTACH) == 0)
    398 			pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    399 	}
    400 
    401 	return (0);
    402 }
    403