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