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