Home | History | Annotate | Line # | Download | only in pcmcia
wdc_pcmcia.c revision 1.49
      1 /*	$NetBSD: wdc_pcmcia.c,v 1.49 2002/06/01 23:51:02 lukem 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/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: wdc_pcmcia.c,v 1.49 2002/06/01 23:51:02 lukem 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/ata/atavar.h>
     55 #include <dev/ic/wdcvar.h>
     56 
     57 #define WDC_PCMCIA_REG_NPORTS      8
     58 #define WDC_PCMCIA_AUXREG_OFFSET   (WDC_PCMCIA_REG_NPORTS + 6)
     59 #define WDC_PCMCIA_AUXREG_NPORTS   2
     60 
     61 struct wdc_pcmcia_softc {
     62 	struct wdc_softc sc_wdcdev;
     63 	struct channel_softc *wdc_chanptr;
     64 	struct channel_softc wdc_channel;
     65 	struct pcmcia_io_handle sc_pioh;
     66 	struct pcmcia_io_handle sc_auxpioh;
     67 	struct pcmcia_mem_handle sc_pmembaseh;
     68 	struct pcmcia_mem_handle sc_pmemh;
     69 	struct pcmcia_mem_handle sc_auxpmemh;
     70 	int sc_memwindow;
     71 	int sc_iowindow;
     72 	int sc_auxiowindow;
     73 	void *sc_ih;
     74 	struct pcmcia_function *sc_pf;
     75 	int sc_flags;
     76 #define	WDC_PCMCIA_ATTACH	0x0001
     77 #define WDC_PCMCIA_MEMMODE	0x0002
     78 };
     79 
     80 static int wdc_pcmcia_match	__P((struct device *, struct cfdata *, void *));
     81 static void wdc_pcmcia_attach	__P((struct device *, struct device *, void *));
     82 static int wdc_pcmcia_detach	__P((struct device *, int));
     83 
     84 struct cfattach wdc_pcmcia_ca = {
     85 	sizeof(struct wdc_pcmcia_softc), wdc_pcmcia_match, wdc_pcmcia_attach,
     86 	wdc_pcmcia_detach, wdcactivate
     87 };
     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 	/*
    160 	 * Novac PCMCIA-IDE Card for HD530P IDE Box,
    161 	 * with neither vendor ID nor product ID
    162 	 */
    163 	{ -1, -1, 0,
    164 	  { "PCMCIA", "PnPIDE", NULL, NULL},
    165 	  "Novac PCCARD-IDE"
    166 	},
    167 
    168 	{ 0, 0, 0, { NULL, NULL, NULL, NULL}, NULL }
    169 };
    170 
    171 const struct wdc_pcmcia_product *
    172 	wdc_pcmcia_lookup __P((struct pcmcia_attach_args *));
    173 
    174 int	wdc_pcmcia_enable __P((struct device *, int));
    175 
    176 const struct wdc_pcmcia_product *
    177 wdc_pcmcia_lookup(pa)
    178 	struct pcmcia_attach_args *pa;
    179 {
    180 	const struct wdc_pcmcia_product *wpp;
    181 	int i, cis_match;
    182 
    183 	for (wpp = wdc_pcmcia_products; wpp->wpp_name != NULL; wpp++)
    184 		if ((wpp->wpp_vendor == -1 ||
    185 		     pa->manufacturer == wpp->wpp_vendor) &&
    186 		    (wpp->wpp_product == -1 ||
    187 		     pa->product == wpp->wpp_product)) {
    188 			cis_match = 1;
    189 			for (i = 0; i < 4; i++) {
    190 				if (!(wpp->wpp_cis_info[i] == NULL ||
    191 				      (pa->card->cis1_info[i] != NULL &&
    192 				       strcmp(pa->card->cis1_info[i],
    193 					      wpp->wpp_cis_info[i]) == 0)))
    194 					cis_match = 0;
    195 			}
    196 			if (cis_match)
    197 				return (wpp);
    198 		}
    199 
    200 	return (NULL);
    201 }
    202 
    203 static int
    204 wdc_pcmcia_match(parent, match, aux)
    205 	struct device *parent;
    206 	struct cfdata *match;
    207 	void *aux;
    208 {
    209 	struct pcmcia_attach_args *pa = aux;
    210 
    211 	if (pa->pf->function == PCMCIA_FUNCTION_DISK &&
    212 	    pa->pf->pf_funce_disk_interface == PCMCIA_TPLFE_DDI_PCCARD_ATA) {
    213 		return 10;
    214 	}
    215 
    216 	if (wdc_pcmcia_lookup(pa) != NULL)
    217 		return (1);
    218 
    219 	return (0);
    220 }
    221 
    222 static void
    223 wdc_pcmcia_attach(parent, self, aux)
    224 	struct device *parent;
    225 	struct device *self;
    226 	void *aux;
    227 {
    228 	struct wdc_pcmcia_softc *sc = (void *)self;
    229 	struct pcmcia_attach_args *pa = aux;
    230 	struct pcmcia_config_entry *cfe;
    231 	const struct wdc_pcmcia_product *wpp;
    232 	bus_size_t offset;
    233 	int quirks;
    234 
    235 	sc->sc_pf = pa->pf;
    236 
    237 	SIMPLEQ_FOREACH(cfe, &pa->pf->cfe_head, cfe_list) {
    238 		if (cfe->num_iospace != 1 && cfe->num_iospace != 2)
    239 			continue;
    240 
    241 		if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
    242 		    cfe->iospace[0].length,
    243 		    cfe->iospace[0].start == 0 ? cfe->iospace[0].length : 0,
    244 		    &sc->sc_pioh))
    245 			continue;
    246 
    247 		if (cfe->num_iospace == 2) {
    248 			if (!pcmcia_io_alloc(pa->pf, cfe->iospace[1].start,
    249 			    cfe->iospace[1].length, 0, &sc->sc_auxpioh))
    250 				break;
    251 		} else /* num_iospace == 1 */ {
    252 			sc->sc_auxpioh.iot = sc->sc_pioh.iot;
    253 			if (!bus_space_subregion(sc->sc_pioh.iot,
    254 			    sc->sc_pioh.ioh, WDC_PCMCIA_AUXREG_OFFSET,
    255 			    WDC_PCMCIA_AUXREG_NPORTS, &sc->sc_auxpioh.ioh))
    256 				break;
    257 		}
    258 		pcmcia_io_free(pa->pf, &sc->sc_pioh);
    259 	}
    260 
    261 	/*
    262 	 * Compact Flash memory mapped mode
    263 	 * CF+ and CompactFlash Spec. Rev 1.4, 6.1.3 Memory Mapped Addressing.
    264 	 * http://www.compactflash.org/cfspc1_4.pdf
    265 	 */
    266 	if (cfe == NULL) {
    267 		SIMPLEQ_FOREACH(cfe, &pa->pf->cfe_head, cfe_list) {
    268 			if (cfe->iftype != PCMCIA_IFTYPE_MEMORY)
    269 				continue;
    270 			if (pcmcia_mem_alloc(pa->pf, cfe->memspace[0].length,
    271 			    &sc->sc_pmembaseh) == 0) {
    272 				sc->sc_flags |= WDC_PCMCIA_MEMMODE;
    273 				break;
    274 			}
    275 		}
    276 	}
    277 
    278 	if (cfe == NULL) {
    279 		printf(": can't handle card info\n");
    280 		goto no_config_entry;
    281 	}
    282 
    283 	/* Enable the card. */
    284 	pcmcia_function_init(pa->pf, cfe);
    285 	if (pcmcia_function_enable(pa->pf)) {
    286 		printf(": function enable failed\n");
    287 		goto enable_failed;
    288 	}
    289 
    290 	wpp = wdc_pcmcia_lookup(pa);
    291 	if (wpp != NULL)
    292 		quirks = wpp->wpp_quirk_flag;
    293 	else
    294 		quirks = 0;
    295 
    296 	if (sc->sc_flags & WDC_PCMCIA_MEMMODE) {
    297 		if (pcmcia_mem_map(pa->pf, PCMCIA_MEM_COMMON, 0,
    298 		    sc->sc_pmembaseh.size, &sc->sc_pmembaseh, &offset,
    299 		    &sc->sc_memwindow)) {
    300 			printf(": can't map memory space\n");
    301 			goto map_failed;
    302 		}
    303 
    304 		sc->sc_pmemh.memt = sc->sc_pmembaseh.memt;
    305 		if (offset == 0) {
    306 			sc->sc_pmemh.memh = sc->sc_pmembaseh.memh;
    307 		} else {
    308 			if (bus_space_subregion(sc->sc_pmemh.memt,
    309 				sc->sc_pmembaseh.memh, offset,
    310 				WDC_PCMCIA_REG_NPORTS, &sc->sc_pmemh.memh))
    311 				goto mapaux_failed;
    312 		}
    313 
    314 		sc->sc_auxpmemh.memt = sc->sc_pmemh.memt;
    315 		if (bus_space_subregion(sc->sc_pmemh.memt,
    316 		    sc->sc_pmemh.memh, WDC_PCMCIA_AUXREG_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_ioh = 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_ioh = 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 	sc->wdc_channel.data32iot = sc->wdc_channel.cmd_iot;
    356 	sc->wdc_channel.data32ioh = sc->wdc_channel.cmd_ioh;
    357 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_SINGLE_DRIVE;
    358 	sc->sc_wdcdev.PIO_cap = 0;
    359 	sc->wdc_chanptr = &sc->wdc_channel;
    360 	sc->sc_wdcdev.channels = &sc->wdc_chanptr;
    361 	sc->sc_wdcdev.nchannels = 1;
    362 	sc->wdc_channel.channel = 0;
    363 	sc->wdc_channel.wdc = &sc->sc_wdcdev;
    364 	sc->wdc_channel.ch_queue = malloc(sizeof(struct channel_queue),
    365 	    M_DEVBUF, M_NOWAIT);
    366 	if (sc->wdc_channel.ch_queue == NULL) {
    367 		printf("%s: can't allocate memory for command queue\n",
    368 		    sc->sc_wdcdev.sc_dev.dv_xname);
    369 		goto ch_queue_alloc_failed;
    370 	}
    371 	if (quirks & WDC_PCMCIA_NO_EXTRA_RESETS)
    372 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_NO_EXTRA_RESETS;
    373 
    374 	/* We can enable and disable the controller. */
    375 	sc->sc_wdcdev.sc_atapi_adapter._generic.adapt_enable =
    376 	    wdc_pcmcia_enable;
    377 
    378 	sc->sc_flags |= WDC_PCMCIA_ATTACH;
    379 	wdcattach(&sc->wdc_channel);	/* should return an error XXX */
    380 	sc->sc_flags &= ~WDC_PCMCIA_ATTACH;
    381 	return;
    382 
    383  ch_queue_alloc_failed:
    384 	/* Unmap our aux i/o window. */
    385 	if (!(sc->sc_flags & WDC_PCMCIA_MEMMODE) && (sc->sc_auxiowindow != -1))
    386 		pcmcia_io_unmap(sc->sc_pf, sc->sc_auxiowindow);
    387 
    388  mapaux_failed:
    389 	/* Unmap our i/o window. */
    390 	if (sc->sc_flags & WDC_PCMCIA_MEMMODE)
    391 		pcmcia_mem_unmap(sc->sc_pf, sc->sc_memwindow);
    392 	else
    393 		pcmcia_io_unmap(sc->sc_pf, sc->sc_iowindow);
    394 
    395  map_failed:
    396 	/* Disable the function */
    397 	pcmcia_function_disable(sc->sc_pf);
    398 
    399  enable_failed:
    400 	/* Unmap our i/o space. */
    401 	if (sc->sc_flags & WDC_PCMCIA_MEMMODE) {
    402 		pcmcia_mem_free(sc->sc_pf, &sc->sc_pmembaseh);
    403 	} else  {
    404 		pcmcia_io_free(sc->sc_pf, &sc->sc_pioh);
    405 		if (cfe->num_iospace == 2)
    406 		    pcmcia_io_free(sc->sc_pf, &sc->sc_auxpioh);
    407 	}
    408  no_config_entry:
    409 	sc->sc_iowindow = -1;
    410 }
    411 
    412 int
    413 wdc_pcmcia_detach(self, flags)
    414 	struct device *self;
    415 	int flags;
    416 {
    417 	struct wdc_pcmcia_softc *sc = (struct wdc_pcmcia_softc *)self;
    418 	int error;
    419 
    420 	if (sc->sc_iowindow == -1)
    421 		/* Nothing to detach */
    422 		return (0);
    423 
    424 	if ((error = wdcdetach(self, flags)) != 0)
    425 		return (error);
    426 
    427 	if (sc->wdc_channel.ch_queue != NULL)
    428 		free(sc->wdc_channel.ch_queue, M_DEVBUF);
    429 
    430 	/* Unmap our i/o window and i/o space. */
    431 	if (sc->sc_flags & WDC_PCMCIA_MEMMODE) {
    432 		pcmcia_mem_unmap(sc->sc_pf, sc->sc_memwindow);
    433 		pcmcia_mem_free(sc->sc_pf, &sc->sc_pmembaseh);
    434 	} else {
    435 		pcmcia_io_unmap(sc->sc_pf, sc->sc_iowindow);
    436 		pcmcia_io_free(sc->sc_pf, &sc->sc_pioh);
    437 		if (sc->sc_auxiowindow != -1) {
    438 			pcmcia_io_unmap(sc->sc_pf, sc->sc_auxiowindow);
    439 			pcmcia_io_free(sc->sc_pf, &sc->sc_auxpioh);
    440 		}
    441 	}
    442 
    443 	return (0);
    444 }
    445 
    446 int
    447 wdc_pcmcia_enable(self, onoff)
    448 	struct device *self;
    449 	int onoff;
    450 {
    451 	struct wdc_pcmcia_softc *sc = (void *)self;
    452 
    453 	if (onoff) {
    454 		/* See the comment in aic_pcmcia_enable */
    455 		if ((sc->sc_flags & WDC_PCMCIA_ATTACH) == 0) {
    456 			/* Establish the interrupt handler. */
    457 			sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
    458 			    wdcintr, &sc->wdc_channel);
    459 			if (sc->sc_ih == NULL) {
    460 				printf("%s: "
    461 				    "couldn't establish interrupt handler\n",
    462 				    sc->sc_wdcdev.sc_dev.dv_xname);
    463 				return (EIO);
    464 			}
    465 
    466 			if (pcmcia_function_enable(sc->sc_pf)) {
    467 				printf("%s: couldn't enable PCMCIA function\n",
    468 				    sc->sc_wdcdev.sc_dev.dv_xname);
    469 				pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    470 				return (EIO);
    471 			}
    472 			wdcreset(&sc->wdc_channel, VERBOSE);
    473 		}
    474 	} else {
    475 		pcmcia_function_disable(sc->sc_pf);
    476 		if ((sc->sc_flags & WDC_PCMCIA_ATTACH) == 0)
    477 			pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    478 	}
    479 
    480 	return (0);
    481 }
    482