Home | History | Annotate | Line # | Download | only in pcmcia
wdc_pcmcia.c revision 1.69
      1 /*	$NetBSD: wdc_pcmcia.c,v 1.69 2004/07/07 06:43:22 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.69 2004/07/07 06:43:22 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_pmembaseh;
     70 	struct pcmcia_mem_handle sc_pmemh;
     71 	struct pcmcia_mem_handle sc_auxpmemh;
     72 	int sc_memwindow;
     73 	int sc_iowindow;
     74 	int sc_auxiowindow;
     75 	void *sc_ih;
     76 	struct pcmcia_function *sc_pf;
     77 	int sc_flags;
     78 #define WDC_PCMCIA_ATTACH	0x0001
     79 #define WDC_PCMCIA_MEMMODE	0x0002
     80 };
     81 
     82 static int wdc_pcmcia_match	__P((struct device *, struct cfdata *, void *));
     83 static void wdc_pcmcia_attach	__P((struct device *, struct device *, void *));
     84 static int wdc_pcmcia_detach	__P((struct device *, int));
     85 
     86 CFATTACH_DECL(wdc_pcmcia, sizeof(struct wdc_pcmcia_softc),
     87     wdc_pcmcia_match, wdc_pcmcia_attach, wdc_pcmcia_detach, wdcactivate);
     88 
     89 const struct wdc_pcmcia_product {
     90 	u_int32_t	wpp_vendor;	/* vendor ID */
     91 	u_int32_t	wpp_product;	/* product ID */
     92 	int		wpp_quirk_flag;	/* Quirk flags */
     93 #define WDC_PCMCIA_NO_EXTRA_RESETS	0x02 /* Only reset ctrl once */
     94 	const char	*wpp_cis_info[4];	/* XXX necessary? */
     95 } wdc_pcmcia_products[] = {
     96 
     97 	{ /* PCMCIA_VENDOR_DIGITAL XXX */ 0x0100,
     98 	  PCMCIA_PRODUCT_DIGITAL_MOBILE_MEDIA_CDROM,
     99 	  0, {NULL, "Digital Mobile Media CD-ROM", NULL, NULL} },
    100 
    101 	{ PCMCIA_VENDOR_IBM,
    102 	  PCMCIA_PRODUCT_IBM_PORTABLE_CDROM,
    103 	  0, {NULL, "PCMCIA Portable CD-ROM Drive", NULL, NULL} },
    104 
    105 	/* The TEAC IDE/Card II is used on the Sony Vaio */
    106 	{ PCMCIA_VENDOR_TEAC,
    107 	  PCMCIA_PRODUCT_TEAC_IDECARDII,
    108 	  WDC_PCMCIA_NO_EXTRA_RESETS,
    109 	  PCMCIA_CIS_TEAC_IDECARDII },
    110 
    111 	/*
    112 	 * A fujitsu rebranded panasonic drive that reports
    113 	 * itself as function "scsi", disk interface 0
    114 	 */
    115 	{ PCMCIA_VENDOR_PANASONIC,
    116 	  PCMCIA_PRODUCT_PANASONIC_KXLC005,
    117 	  0,
    118 	  PCMCIA_CIS_PANASONIC_KXLC005 },
    119 
    120 	/*
    121 	 * EXP IDE/ATAPI DVD Card use with some DVD players.
    122 	 * Does not have a vendor ID or product ID.
    123 	 */
    124 	{ -1, -1, 0,
    125 	  PCMCIA_CIS_EXP_EXPMULTIMEDIA },
    126 
    127 	/* Mobile Dock 2, neither vendor ID nor product ID */
    128 	{ -1, -1, 0,
    129 	  {"SHUTTLE TECHNOLOGY LTD.", "PCCARD-IDE/ATAPI Adapter", NULL, NULL} },
    130 
    131 	/* Toshiba Portege 3110 CD, neither vendor ID nor product ID */
    132 	{ -1, -1, 0,
    133 	  {"FREECOM", "PCCARD-IDE", NULL, NULL} },
    134 
    135 	/* Random CD-ROM, (badged AMACOM), neither vendor ID nor product ID */
    136 	{ -1, -1, 0,
    137 	  {"PCMCIA", "CD-ROM", NULL, NULL} },
    138 
    139 	/* IO DATA CBIDE2, with neither vendor ID nor product ID */
    140 	{ -1, -1, 0,
    141 	  PCMCIA_CIS_IODATA_CBIDE2 },
    142 
    143 	/* TOSHIBA PA2673U(IODATA_CBIDE2 OEM), */
    144 	/*  with neither vendor ID nor product ID */
    145 	{ -1, -1, 0,
    146 	  PCMCIA_CIS_TOSHIBA_CBIDE2 },
    147 
    148 	/*
    149 	 * Novac PCMCIA-IDE Card for HD530P IDE Box,
    150 	 * with neither vendor ID nor product ID
    151 	 */
    152 	{ -1, -1, 0,
    153 	  {"PCMCIA", "PnPIDE", NULL, NULL} },
    154 };
    155 
    156 const struct wdc_pcmcia_product *
    157 	wdc_pcmcia_lookup __P((struct pcmcia_attach_args *));
    158 
    159 int	wdc_pcmcia_enable __P((struct device *, int));
    160 
    161 const struct wdc_pcmcia_product *
    162 wdc_pcmcia_lookup(pa)
    163 	struct pcmcia_attach_args *pa;
    164 {
    165 	const struct wdc_pcmcia_product *wpp;
    166 	int i, cis_match;
    167 	int n;
    168 
    169 	for (wpp = wdc_pcmcia_products,
    170 	    n = sizeof(wdc_pcmcia_products) / sizeof(wdc_pcmcia_products[0]);
    171 	    n; wpp++, n--) {
    172 		if ((wpp->wpp_vendor == -1 ||
    173 		     pa->manufacturer == wpp->wpp_vendor) &&
    174 		    (wpp->wpp_product == -1 ||
    175 		     pa->product == wpp->wpp_product)) {
    176 			cis_match = 1;
    177 			for (i = 0; i < 4; i++) {
    178 				if (!(wpp->wpp_cis_info[i] == NULL ||
    179 				      (pa->card->cis1_info[i] != NULL &&
    180 				       strcmp(pa->card->cis1_info[i],
    181 					      wpp->wpp_cis_info[i]) == 0)))
    182 					cis_match = 0;
    183 			}
    184 			if (cis_match)
    185 				return (wpp);
    186 		}
    187 	}
    188 
    189 	return (NULL);
    190 }
    191 
    192 static int
    193 wdc_pcmcia_match(parent, match, aux)
    194 	struct device *parent;
    195 	struct cfdata *match;
    196 	void *aux;
    197 {
    198 	struct pcmcia_attach_args *pa = aux;
    199 
    200 	if (pa->pf->function == PCMCIA_FUNCTION_DISK &&
    201 	    pa->pf->pf_funce_disk_interface == PCMCIA_TPLFE_DDI_PCCARD_ATA) {
    202 		return 10;
    203 	}
    204 
    205 	if (wdc_pcmcia_lookup(pa) != NULL)
    206 		return (1);
    207 
    208 	return (0);
    209 }
    210 
    211 static void
    212 wdc_pcmcia_attach(parent, self, aux)
    213 	struct device *parent;
    214 	struct device *self;
    215 	void *aux;
    216 {
    217 	struct wdc_pcmcia_softc *sc = (void *)self;
    218 	struct pcmcia_attach_args *pa = aux;
    219 	struct pcmcia_config_entry *cfe;
    220 	const struct wdc_pcmcia_product *wpp;
    221 	bus_size_t offset = 0;
    222 	int quirks, i;
    223 
    224 	aprint_normal("\n");
    225 
    226 	sc->sc_pf = pa->pf;
    227 
    228 	SIMPLEQ_FOREACH(cfe, &pa->pf->cfe_head, cfe_list) {
    229 		if (cfe->num_iospace != 1 && cfe->num_iospace != 2)
    230 			continue;
    231 
    232 		if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
    233 		    cfe->iospace[0].length,
    234 		    cfe->iospace[0].start == 0 ? cfe->iospace[0].length : 0,
    235 		    &sc->sc_pioh))
    236 			continue;
    237 
    238 		if (cfe->num_iospace == 2) {
    239 			if (!pcmcia_io_alloc(pa->pf, cfe->iospace[1].start,
    240 			    cfe->iospace[1].length, 0, &sc->sc_auxpioh))
    241 				break;
    242 		} else /* num_iospace == 1 */ {
    243 			sc->sc_auxpioh.iot = sc->sc_pioh.iot;
    244 			if (!bus_space_subregion(sc->sc_pioh.iot,
    245 			    sc->sc_pioh.ioh, WDC_PCMCIA_AUXREG_OFFSET,
    246 			    WDC_PCMCIA_AUXREG_NPORTS, &sc->sc_auxpioh.ioh))
    247 				break;
    248 		}
    249 		pcmcia_io_free(pa->pf, &sc->sc_pioh);
    250 	}
    251 
    252 	/*
    253 	 * Compact Flash memory mapped mode
    254 	 * CF+ and CompactFlash Spec. Rev 1.4, 6.1.3 Memory Mapped Addressing.
    255 	 * http://www.compactflash.org/cfspc1_4.pdf
    256 	 */
    257 	if (cfe == NULL) {
    258 		SIMPLEQ_FOREACH(cfe, &pa->pf->cfe_head, cfe_list) {
    259 			if (cfe->iftype != PCMCIA_IFTYPE_MEMORY)
    260 				continue;
    261 			if (pcmcia_mem_alloc(pa->pf, cfe->memspace[0].length,
    262 			    &sc->sc_pmembaseh) == 0) {
    263 				sc->sc_flags |= WDC_PCMCIA_MEMMODE;
    264 				break;
    265 			}
    266 		}
    267 	}
    268 
    269 	if (cfe == NULL) {
    270 		aprint_error("%s: can't handle card info\n", self->dv_xname);
    271 		goto no_config_entry;
    272 	}
    273 
    274 	/* Enable the card. */
    275 	pcmcia_function_init(pa->pf, cfe);
    276 	if (pcmcia_function_enable(pa->pf)) {
    277 		aprint_error("%s: function enable failed\n", self->dv_xname);
    278 		goto enable_failed;
    279 	}
    280 
    281 	wpp = wdc_pcmcia_lookup(pa);
    282 	if (wpp != NULL)
    283 		quirks = wpp->wpp_quirk_flag;
    284 	else
    285 		quirks = 0;
    286 
    287 	if (sc->sc_flags & WDC_PCMCIA_MEMMODE) {
    288 		if (pcmcia_mem_map(pa->pf, PCMCIA_MEM_COMMON, 0,
    289 		    sc->sc_pmembaseh.size, &sc->sc_pmembaseh, &offset,
    290 		    &sc->sc_memwindow)) {
    291 			aprint_error("%s: can't map memory space\n",
    292 			    self->dv_xname);
    293 			goto map_failed;
    294 		}
    295 
    296 		sc->sc_pmemh.memt = sc->sc_pmembaseh.memt;
    297 		sc->sc_pmemh.memh = sc->sc_pmembaseh.memh;
    298 
    299 		sc->sc_auxpmemh.memt = sc->sc_pmemh.memt;
    300 		if (bus_space_subregion(sc->sc_pmemh.memt,
    301 		    sc->sc_pmembaseh.memh, WDC_PCMCIA_AUXREG_OFFSET + offset,
    302 		    WDC_PCMCIA_AUXREG_NPORTS, &sc->sc_auxpmemh.memh))
    303 			goto mapaux_failed;
    304 
    305 		aprint_normal("%s: memory mapped mode\n", self->dv_xname);
    306 	} else {
    307 		if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, 0,
    308 		    sc->sc_pioh.size, &sc->sc_pioh, &sc->sc_iowindow)) {
    309 			aprint_error("%s: can't map first I/O space\n",
    310 			     self->dv_xname);
    311 			goto map_failed;
    312 		}
    313 	}
    314 
    315 	if (cfe->num_iospace <= 1 || sc->sc_flags & WDC_PCMCIA_MEMMODE)
    316 		sc->sc_auxiowindow = -1;
    317 	else if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, 0,
    318 	    sc->sc_auxpioh.size, &sc->sc_auxpioh, &sc->sc_auxiowindow)) {
    319 		aprint_error("%s: can't map second I/O space\n",
    320 		    self->dv_xname);
    321 		goto mapaux_failed;
    322 	}
    323 
    324 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16;
    325 	if (sc->sc_flags & WDC_PCMCIA_MEMMODE) {
    326 		sc->wdc_channel.cmd_iot = sc->sc_pmemh.memt;
    327 		sc->wdc_channel.cmd_baseioh = sc->sc_pmemh.memh;
    328 		sc->wdc_channel.ctl_iot = sc->sc_auxpmemh.memt;
    329 		sc->wdc_channel.ctl_ioh = sc->sc_auxpmemh.memh;
    330 	} else {
    331 		sc->wdc_channel.cmd_iot = sc->sc_pioh.iot;
    332 		sc->wdc_channel.cmd_baseioh = sc->sc_pioh.ioh;
    333 		sc->wdc_channel.ctl_iot = sc->sc_auxpioh.iot;
    334 		sc->wdc_channel.ctl_ioh = sc->sc_auxpioh.ioh;
    335 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32;
    336 	}
    337 	for (i = 0; i < WDC_PCMCIA_REG_NPORTS; i++) {
    338 		if (bus_space_subregion(sc->wdc_channel.cmd_iot,
    339 		    sc->wdc_channel.cmd_baseioh,
    340 		    offset + i, i == 0 ? 4 : 1,
    341 		    &sc->wdc_channel.cmd_iohs[i]) != 0) {
    342 			aprint_error("%s: can't subregion I/O space\n",
    343 			    self->dv_xname);
    344 			goto mapaux_failed;
    345 		}
    346 	}
    347 	wdc_init_shadow_regs(&sc->wdc_channel);
    348 	sc->wdc_channel.data32iot = sc->wdc_channel.cmd_iot;
    349 	sc->wdc_channel.data32ioh = sc->wdc_channel.cmd_iohs[0];
    350 	sc->sc_wdcdev.PIO_cap = 0;
    351 	sc->wdc_chanlist[0] = &sc->wdc_channel;
    352 	sc->sc_wdcdev.channels = sc->wdc_chanlist;
    353 	sc->sc_wdcdev.nchannels = 1;
    354 	sc->wdc_channel.ch_channel = 0;
    355 	sc->wdc_channel.ch_wdc = &sc->sc_wdcdev;
    356 	sc->wdc_channel.ch_queue = &sc->wdc_chqueue;
    357 #if 0
    358 	if (quirks & WDC_PCMCIA_NO_EXTRA_RESETS)
    359 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_NO_EXTRA_RESETS;
    360 #endif
    361 
    362 	/* We can enable and disable the controller. */
    363 	sc->sc_wdcdev.sc_atapi_adapter._generic.adapt_enable =
    364 	    wdc_pcmcia_enable;
    365 
    366 	sc->sc_flags |= WDC_PCMCIA_ATTACH;
    367 	wdcattach(&sc->wdc_channel);
    368 
    369 	return;
    370 
    371  mapaux_failed:
    372 	/* Unmap our i/o window. */
    373 	if (sc->sc_flags & WDC_PCMCIA_MEMMODE)
    374 		pcmcia_mem_unmap(sc->sc_pf, sc->sc_memwindow);
    375 	else
    376 		pcmcia_io_unmap(sc->sc_pf, sc->sc_iowindow);
    377 
    378  map_failed:
    379 	/* Disable the function */
    380 	pcmcia_function_disable(sc->sc_pf);
    381 
    382  enable_failed:
    383 	/* Unmap our i/o space. */
    384 	if (sc->sc_flags & WDC_PCMCIA_MEMMODE) {
    385 		pcmcia_mem_free(sc->sc_pf, &sc->sc_pmembaseh);
    386 	} else  {
    387 		pcmcia_io_free(sc->sc_pf, &sc->sc_pioh);
    388 		if (cfe->num_iospace == 2)
    389 		    pcmcia_io_free(sc->sc_pf, &sc->sc_auxpioh);
    390 	}
    391  no_config_entry:
    392 	sc->sc_iowindow = -1;
    393 }
    394 
    395 int
    396 wdc_pcmcia_detach(self, flags)
    397 	struct device *self;
    398 	int flags;
    399 {
    400 	struct wdc_pcmcia_softc *sc = (struct wdc_pcmcia_softc *)self;
    401 	int error;
    402 
    403 	if (sc->sc_iowindow == -1)
    404 		/* Nothing to detach */
    405 		return (0);
    406 
    407 	/*
    408 	 * If the WDC_PCMCIA_ATTACH flag is still set, then we didn't get
    409 	 * a chance * enable/disable the card in the wdc/atabus layer, so
    410 	 * we still need to disable the function here.
    411 	 */
    412 	if (sc->sc_flags & WDC_PCMCIA_ATTACH) {
    413 		sc->sc_flags &= ~WDC_PCMCIA_ATTACH;
    414 		pcmcia_function_disable(sc->sc_pf);
    415 	}
    416 
    417 	if ((error = wdcdetach(self, flags)) != 0)
    418 		return (error);
    419 
    420 	/* Unmap our i/o window and i/o space. */
    421 	if (sc->sc_flags & WDC_PCMCIA_MEMMODE) {
    422 		pcmcia_mem_unmap(sc->sc_pf, sc->sc_memwindow);
    423 		pcmcia_mem_free(sc->sc_pf, &sc->sc_pmembaseh);
    424 	} else {
    425 		pcmcia_io_unmap(sc->sc_pf, sc->sc_iowindow);
    426 		pcmcia_io_free(sc->sc_pf, &sc->sc_pioh);
    427 		if (sc->sc_auxiowindow != -1) {
    428 			pcmcia_io_unmap(sc->sc_pf, sc->sc_auxiowindow);
    429 			pcmcia_io_free(sc->sc_pf, &sc->sc_auxpioh);
    430 		}
    431 	}
    432 
    433 	return (0);
    434 }
    435 
    436 int
    437 wdc_pcmcia_enable(self, onoff)
    438 	struct device *self;
    439 	int onoff;
    440 {
    441 	struct wdc_pcmcia_softc *sc = (void *)self;
    442 
    443 	if (onoff) {
    444 		/* Establish the interrupt handler. */
    445 		sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
    446 		    wdcintr, &sc->wdc_channel);
    447 		if (sc->sc_ih == NULL) {
    448 			printf("%s: couldn't establish interrupt handler\n",
    449 			    sc->sc_wdcdev.sc_dev.dv_xname);
    450 			return (EIO);
    451 		}
    452 
    453 		/*
    454 		 * If the WDC_PCMCIA_ATTACH flag is set, we've already
    455 		 * enabled the card in the attach routine, so don't
    456 		 * re-enable it here (to save power cycle time).  Clear
    457 		 * the flag, though, so that the next disable/enable
    458 		 * will do the right thing.
    459 		 */
    460 		if (sc->sc_flags & WDC_PCMCIA_ATTACH) {
    461 			sc->sc_flags &= ~WDC_PCMCIA_ATTACH;
    462 		} else {
    463 			if (pcmcia_function_enable(sc->sc_pf)) {
    464 				printf("%s: couldn't enable PCMCIA function\n",
    465 				    sc->sc_wdcdev.sc_dev.dv_xname);
    466 				pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    467 				return (EIO);
    468 			}
    469 		}
    470 	} else {
    471 		pcmcia_function_disable(sc->sc_pf);
    472 		pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    473 	}
    474 
    475 	return (0);
    476 }
    477