Home | History | Annotate | Line # | Download | only in pcmcia
wdc_pcmcia.c revision 1.72
      1 /*	$NetBSD: wdc_pcmcia.c,v 1.72 2004/08/10 02:56:42 mycroft Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1998, 2003 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/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: wdc_pcmcia.c,v 1.72 2004/08/10 02:56:42 mycroft Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/device.h>
     44 #include <sys/malloc.h>
     45 #include <sys/systm.h>
     46 
     47 #include <machine/bus.h>
     48 #include <machine/intr.h>
     49 
     50 #include <dev/pcmcia/pcmciareg.h>
     51 #include <dev/pcmcia/pcmciavar.h>
     52 #include <dev/pcmcia/pcmciadevs.h>
     53 
     54 #include <dev/ic/wdcreg.h>
     55 #include <dev/ata/atavar.h>
     56 #include <dev/ic/wdcvar.h>
     57 
     58 #define WDC_PCMCIA_REG_NPORTS      8
     59 #define WDC_PCMCIA_AUXREG_OFFSET   (WDC_PCMCIA_REG_NPORTS + 6)
     60 #define WDC_PCMCIA_AUXREG_NPORTS   2
     61 
     62 struct wdc_pcmcia_softc {
     63 	struct wdc_softc sc_wdcdev;
     64 	struct wdc_channel *wdc_chanlist[1];
     65 	struct wdc_channel wdc_channel;
     66 	struct ata_queue wdc_chqueue;
     67 	struct pcmcia_io_handle sc_pioh;
     68 	struct pcmcia_io_handle sc_auxpioh;
     69 	struct pcmcia_mem_handle sc_pmemh;
     70 	struct pcmcia_mem_handle sc_auxpmemh;
     71 	void *sc_ih;
     72 	struct pcmcia_function *sc_pf;
     73 	int sc_flags;
     74 #define WDC_PCMCIA_ATTACH	0x0001
     75 #define WDC_PCMCIA_ATTACHED	0x0002
     76 };
     77 
     78 static int wdc_pcmcia_match	__P((struct device *, struct cfdata *, void *));
     79 static int wdc_pcmcia_validate_config __P((struct pcmcia_config_entry *));
     80 static void wdc_pcmcia_attach	__P((struct device *, struct device *, void *));
     81 static int wdc_pcmcia_detach	__P((struct device *, int));
     82 
     83 CFATTACH_DECL(wdc_pcmcia, sizeof(struct wdc_pcmcia_softc),
     84     wdc_pcmcia_match, wdc_pcmcia_attach, wdc_pcmcia_detach, wdcactivate);
     85 
     86 const struct wdc_pcmcia_product {
     87 	u_int32_t	wpp_vendor;	/* vendor ID */
     88 	u_int32_t	wpp_product;	/* product ID */
     89 	int		wpp_quirk_flag;	/* Quirk flags */
     90 #define WDC_PCMCIA_NO_EXTRA_RESETS	0x02 /* Only reset ctrl once */
     91 	const char	*wpp_cis_info[4];	/* XXX necessary? */
     92 } wdc_pcmcia_products[] = {
     93 
     94 	{ /* PCMCIA_VENDOR_DIGITAL XXX */ 0x0100,
     95 	  PCMCIA_PRODUCT_DIGITAL_MOBILE_MEDIA_CDROM,
     96 	  0, {NULL, "Digital Mobile Media CD-ROM", NULL, NULL} },
     97 
     98 	{ PCMCIA_VENDOR_IBM,
     99 	  PCMCIA_PRODUCT_IBM_PORTABLE_CDROM,
    100 	  0, {NULL, "PCMCIA Portable CD-ROM Drive", NULL, NULL} },
    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 
    108 	/*
    109 	 * A fujitsu rebranded panasonic drive that reports
    110 	 * itself as function "scsi", disk interface 0
    111 	 */
    112 	{ PCMCIA_VENDOR_PANASONIC,
    113 	  PCMCIA_PRODUCT_PANASONIC_KXLC005,
    114 	  0,
    115 	  PCMCIA_CIS_PANASONIC_KXLC005 },
    116 
    117 	/*
    118 	 * EXP IDE/ATAPI DVD Card use with some DVD players.
    119 	 * Does not have a vendor ID or product ID.
    120 	 */
    121 	{ -1, -1, 0,
    122 	  PCMCIA_CIS_EXP_EXPMULTIMEDIA },
    123 
    124 	/* Mobile Dock 2, neither vendor ID nor product ID */
    125 	{ -1, -1, 0,
    126 	  {"SHUTTLE TECHNOLOGY LTD.", "PCCARD-IDE/ATAPI Adapter", NULL, NULL} },
    127 
    128 	/* Toshiba Portege 3110 CD, neither vendor ID nor product ID */
    129 	{ -1, -1, 0,
    130 	  {"FREECOM", "PCCARD-IDE", NULL, NULL} },
    131 
    132 	/* Random CD-ROM, (badged AMACOM), neither vendor ID nor product ID */
    133 	{ -1, -1, 0,
    134 	  {"PCMCIA", "CD-ROM", NULL, NULL} },
    135 
    136 	/* IO DATA CBIDE2, with neither vendor ID nor product ID */
    137 	{ -1, -1, 0,
    138 	  PCMCIA_CIS_IODATA_CBIDE2 },
    139 
    140 	/* TOSHIBA PA2673U(IODATA_CBIDE2 OEM), */
    141 	/*  with neither vendor ID nor product ID */
    142 	{ -1, -1, 0,
    143 	  PCMCIA_CIS_TOSHIBA_CBIDE2 },
    144 
    145 	/*
    146 	 * Novac PCMCIA-IDE Card for HD530P IDE Box,
    147 	 * with neither vendor ID nor product ID
    148 	 */
    149 	{ -1, -1, 0,
    150 	  {"PCMCIA", "PnPIDE", 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((struct device *, int));
    157 
    158 const struct wdc_pcmcia_product *
    159 wdc_pcmcia_lookup(pa)
    160 	struct pcmcia_attach_args *pa;
    161 {
    162 	const struct wdc_pcmcia_product *wpp;
    163 	int i, cis_match;
    164 	int n;
    165 
    166 	for (wpp = wdc_pcmcia_products,
    167 	    n = sizeof(wdc_pcmcia_products) / sizeof(wdc_pcmcia_products[0]);
    168 	    n; wpp++, n--) {
    169 		if ((wpp->wpp_vendor == -1 ||
    170 		     pa->manufacturer == wpp->wpp_vendor) &&
    171 		    (wpp->wpp_product == -1 ||
    172 		     pa->product == wpp->wpp_product)) {
    173 			cis_match = 1;
    174 			for (i = 0; i < 4; i++) {
    175 				if (!(wpp->wpp_cis_info[i] == NULL ||
    176 				      (pa->card->cis1_info[i] != NULL &&
    177 				       strcmp(pa->card->cis1_info[i],
    178 					      wpp->wpp_cis_info[i]) == 0)))
    179 					cis_match = 0;
    180 			}
    181 			if (cis_match)
    182 				return (wpp);
    183 		}
    184 	}
    185 
    186 	return (NULL);
    187 }
    188 
    189 static int
    190 wdc_pcmcia_match(parent, match, aux)
    191 	struct device *parent;
    192 	struct cfdata *match;
    193 	void *aux;
    194 {
    195 	struct pcmcia_attach_args *pa = aux;
    196 
    197 	if (pa->pf->function == PCMCIA_FUNCTION_DISK &&
    198 	    pa->pf->pf_funce_disk_interface == PCMCIA_TPLFE_DDI_PCCARD_ATA) {
    199 		return 10;
    200 	}
    201 
    202 	if (wdc_pcmcia_lookup(pa) != NULL)
    203 		return (1);
    204 
    205 	return (0);
    206 }
    207 
    208 static int
    209 wdc_pcmcia_validate_config(cfe)
    210 	struct pcmcia_config_entry *cfe;
    211 {
    212 	switch (cfe->iftype) {
    213 #if 0
    214 	case PCMCIA_IFTYPE_MEMORY:
    215 		if (cfe->num_iospace != 0 ||
    216 		    cfe->num_memspace != 1)
    217 			return (EINVAL);
    218 		break;
    219 #endif
    220 	case PCMCIA_IFTYPE_IO:
    221 		if (cfe->num_iospace < 1 || cfe->num_iospace > 2)
    222 			return (EINVAL);
    223 		cfe->num_memspace = 0;
    224 		break;
    225 	default:
    226 		return (EINVAL);
    227 	}
    228 	return (0);
    229 }
    230 
    231 static void
    232 wdc_pcmcia_attach(parent, self, aux)
    233 	struct device *parent;
    234 	struct device *self;
    235 	void *aux;
    236 {
    237 	struct wdc_pcmcia_softc *sc = (void *)self;
    238 	struct pcmcia_attach_args *pa = aux;
    239 	struct pcmcia_config_entry *cfe;
    240 	const struct wdc_pcmcia_product *wpp;
    241 	bus_size_t offset;
    242 	int quirks, i;
    243 	int error;
    244 
    245 	aprint_normal("\n");
    246 	sc->sc_pf = pa->pf;
    247 
    248 	/*XXXmem16|common*/
    249 	error = pcmcia_function_configure(pa->pf, wdc_pcmcia_validate_config);
    250 	if (error) {
    251 		aprint_error("%s: configure failed, error=%d\n", self->dv_xname,
    252 		    error);
    253 		return;
    254 	}
    255 
    256 	cfe = pa->pf->cfe;
    257 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16;
    258 	if (cfe->iftype == PCMCIA_IFTYPE_MEMORY) {
    259 		sc->sc_pmemh.memt = cfe->memspace[0].handle.memt;
    260 		sc->sc_pmemh.memh = cfe->memspace[0].handle.memh;
    261 		offset = cfe->memspace[0].offset;
    262 
    263 		sc->sc_auxpmemh.memt = sc->sc_pmemh.memt;
    264 		if (bus_space_subregion(sc->sc_pmemh.memt,
    265 		    sc->sc_pmemh.memh, WDC_PCMCIA_AUXREG_OFFSET + offset,
    266 		    WDC_PCMCIA_AUXREG_NPORTS, &sc->sc_auxpmemh.memh))
    267 			goto fail;
    268 
    269 		aprint_normal("%s: memory mapped mode\n", self->dv_xname);
    270 		sc->wdc_channel.cmd_iot = sc->sc_pmemh.memt;
    271 		sc->wdc_channel.cmd_baseioh = sc->sc_pmemh.memh;
    272 		sc->wdc_channel.ctl_iot = sc->sc_auxpmemh.memt;
    273 		sc->wdc_channel.ctl_ioh = sc->sc_auxpmemh.memh;
    274 	} else {
    275 		sc->sc_pioh.iot = cfe->iospace[0].handle.iot;
    276 		sc->sc_pioh.ioh = cfe->iospace[0].handle.ioh;
    277 		offset = 0;
    278 
    279 		if (cfe->num_iospace == 1) {
    280 			sc->sc_auxpioh.iot = sc->sc_pioh.iot;
    281 			if (bus_space_subregion(sc->sc_pioh.iot,
    282 			    sc->sc_pioh.ioh, WDC_PCMCIA_AUXREG_OFFSET,
    283 			    WDC_PCMCIA_AUXREG_NPORTS, &sc->sc_auxpioh.ioh))
    284 				goto fail;
    285 		} else {
    286 			sc->sc_auxpioh.iot = cfe->iospace[1].handle.iot;
    287 			sc->sc_auxpioh.ioh = cfe->iospace[1].handle.ioh;
    288 		}
    289 
    290 		aprint_normal("%s: i/o mapped mode\n", self->dv_xname);
    291 		sc->wdc_channel.cmd_iot = sc->sc_pioh.iot;
    292 		sc->wdc_channel.cmd_baseioh = sc->sc_pioh.ioh;
    293 		sc->wdc_channel.ctl_iot = sc->sc_auxpioh.iot;
    294 		sc->wdc_channel.ctl_ioh = sc->sc_auxpioh.ioh;
    295 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32;
    296 	}
    297 
    298 	if (wdc_pcmcia_enable(self, 1)) {
    299 		aprint_error("%s: enable failed\n", self->dv_xname);
    300 		goto fail;
    301 	}
    302 
    303 	wpp = wdc_pcmcia_lookup(pa);
    304 	if (wpp != NULL)
    305 		quirks = wpp->wpp_quirk_flag;
    306 	else
    307 		quirks = 0;
    308 
    309 	for (i = 0; i < WDC_PCMCIA_REG_NPORTS; i++) {
    310 		if (bus_space_subregion(sc->wdc_channel.cmd_iot,
    311 		    sc->wdc_channel.cmd_baseioh,
    312 		    offset + i, i == 0 ? 4 : 1,
    313 		    &sc->wdc_channel.cmd_iohs[i]) != 0) {
    314 			aprint_error("%s: can't subregion I/O space\n",
    315 			    self->dv_xname);
    316 			goto fail2;
    317 		}
    318 	}
    319 	wdc_init_shadow_regs(&sc->wdc_channel);
    320 	sc->wdc_channel.data32iot = sc->wdc_channel.cmd_iot;
    321 	sc->wdc_channel.data32ioh = sc->wdc_channel.cmd_iohs[0];
    322 	sc->sc_wdcdev.PIO_cap = 0;
    323 	sc->wdc_chanlist[0] = &sc->wdc_channel;
    324 	sc->sc_wdcdev.channels = sc->wdc_chanlist;
    325 	sc->sc_wdcdev.nchannels = 1;
    326 	sc->wdc_channel.ch_channel = 0;
    327 	sc->wdc_channel.ch_wdc = &sc->sc_wdcdev;
    328 	sc->wdc_channel.ch_queue = &sc->wdc_chqueue;
    329 #if 0
    330 	if (quirks & WDC_PCMCIA_NO_EXTRA_RESETS)
    331 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_NO_EXTRA_RESETS;
    332 #endif
    333 
    334 	/* We can enable and disable the controller. */
    335 	sc->sc_wdcdev.sc_atapi_adapter._generic.adapt_enable =
    336 	    wdc_pcmcia_enable;
    337 
    338 	sc->sc_flags |= WDC_PCMCIA_ATTACH;
    339 	wdcattach(&sc->wdc_channel);
    340 	sc->sc_flags |= WDC_PCMCIA_ATTACHED;
    341 	return;
    342 
    343 fail2:
    344 	wdc_pcmcia_enable(self, 0);
    345 fail:
    346 	pcmcia_function_unconfigure(pa->pf);
    347 }
    348 
    349 int
    350 wdc_pcmcia_detach(self, flags)
    351 	struct device *self;
    352 	int flags;
    353 {
    354 	struct wdc_pcmcia_softc *sc = (struct wdc_pcmcia_softc *)self;
    355 	int error;
    356 
    357 	if ((sc->sc_flags & WDC_PCMCIA_ATTACHED) == 0)
    358 		return (0);
    359 
    360 	if ((error = wdcdetach(self, flags)) != 0)
    361 		return (error);
    362 
    363 	pcmcia_function_unconfigure(sc->sc_pf);
    364 
    365 	return (0);
    366 }
    367 
    368 int
    369 wdc_pcmcia_enable(self, onoff)
    370 	struct device *self;
    371 	int onoff;
    372 {
    373 	struct wdc_pcmcia_softc *sc = (void *)self;
    374 
    375 	if (onoff) {
    376 		/*
    377 		 * If the WDC_PCMCIA_ATTACH flag is set, we've already
    378 		 * enabled the card in the attach routine, so don't
    379 		 * re-enable it here (to save power cycle time).  Clear
    380 		 * the flag, though, so that the next disable/enable
    381 		 * will do the right thing.
    382 		 */
    383 		if (sc->sc_flags & WDC_PCMCIA_ATTACH) {
    384 			sc->sc_flags &= ~WDC_PCMCIA_ATTACH;
    385 		} else {
    386 			/* Establish the interrupt handler. */
    387 			sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
    388 			    wdcintr, &sc->wdc_channel);
    389 			if (sc->sc_ih == NULL) {
    390 				printf("%s: couldn't establish interrupt handler\n",
    391 				    sc->sc_wdcdev.sc_dev.dv_xname);
    392 				return (EIO);
    393 			}
    394 
    395 			if (pcmcia_function_enable(sc->sc_pf)) {
    396 				printf("%s: couldn't enable PCMCIA function\n",
    397 				    sc->sc_wdcdev.sc_dev.dv_xname);
    398 				pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    399 				return (EIO);
    400 			}
    401 		}
    402 	} else {
    403 		pcmcia_function_disable(sc->sc_pf);
    404 		pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    405 		sc->sc_ih = 0;
    406 	}
    407 
    408 	return (0);
    409 }
    410