Home | History | Annotate | Line # | Download | only in pcmcia
wdc_pcmcia.c revision 1.42
      1 /*	$NetBSD: wdc_pcmcia.c,v 1.42 2001/04/25 17:53:37 bouyer 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/param.h>
     40 #include <sys/device.h>
     41 #include <sys/malloc.h>
     42 #include <sys/systm.h>
     43 
     44 #include <machine/bus.h>
     45 #include <machine/intr.h>
     46 
     47 #include <dev/pcmcia/pcmciareg.h>
     48 #include <dev/pcmcia/pcmciavar.h>
     49 #include <dev/pcmcia/pcmciadevs.h>
     50 
     51 #include <dev/ata/atavar.h>
     52 #include <dev/ic/wdcvar.h>
     53 
     54 #define WDC_PCMCIA_REG_NPORTS      8
     55 #define WDC_PCMCIA_AUXREG_OFFSET   (WDC_PCMCIA_REG_NPORTS + 6)
     56 #define WDC_PCMCIA_AUXREG_NPORTS   2
     57 
     58 struct wdc_pcmcia_softc {
     59 	struct wdc_softc sc_wdcdev;
     60 	struct channel_softc *wdc_chanptr;
     61 	struct channel_softc wdc_channel;
     62 	struct pcmcia_io_handle sc_pioh;
     63 	struct pcmcia_io_handle sc_auxpioh;
     64 	struct pcmcia_mem_handle sc_pmembaseh;
     65 	struct pcmcia_mem_handle sc_pmemh;
     66 	struct pcmcia_mem_handle sc_auxpmemh;
     67 	int sc_memwindow;
     68 	int sc_iowindow;
     69 	int sc_auxiowindow;
     70 	void *sc_ih;
     71 	struct pcmcia_function *sc_pf;
     72 	int sc_flags;
     73 #define	WDC_PCMCIA_ATTACH	0x0001
     74 #define WDC_PCMCIA_MEMMODE	0x0002
     75 };
     76 
     77 static int wdc_pcmcia_match	__P((struct device *, struct cfdata *, void *));
     78 static void wdc_pcmcia_attach	__P((struct device *, struct device *, void *));
     79 static int wdc_pcmcia_detach	__P((struct device *, int));
     80 
     81 struct cfattach wdc_pcmcia_ca = {
     82 	sizeof(struct wdc_pcmcia_softc), wdc_pcmcia_match, wdc_pcmcia_attach,
     83 	wdc_pcmcia_detach, wdcactivate
     84 };
     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 	const char	*wpp_name;	/* product name */
     93 } wdc_pcmcia_products[] = {
     94 
     95 	{ /* PCMCIA_VENDOR_DIGITAL XXX */ 0x0100,
     96 	  PCMCIA_PRODUCT_DIGITAL_MOBILE_MEDIA_CDROM,
     97 	  0, { NULL, "Digital Mobile Media CD-ROM", NULL, NULL },
     98 	  PCMCIA_STR_DIGITAL_MOBILE_MEDIA_CDROM },
     99 
    100 	{ PCMCIA_VENDOR_IBM,
    101 	  PCMCIA_PRODUCT_IBM_PORTABLE_CDROM,
    102 	  0, { NULL, "PCMCIA Portable CD-ROM Drive", NULL, NULL },
    103 	  PCMCIA_STR_IBM_PORTABLE_CDROM },
    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 	  PCMCIA_STR_TEAC_IDECARDII },
    111 
    112 	/*
    113 	 * EXP IDE/ATAPI DVD Card use with some DVD players.
    114 	 * Does not have a vendor ID or product ID.
    115 	 */
    116 	{ -1,
    117 	  -1,
    118 	  0,
    119 	  PCMCIA_CIS_EXP_EXPMULTIMEDIA,
    120 	  PCMCIA_STR_EXP_EXPMULTIMEDIA },
    121 
    122 	/* Mobile Dock 2, neither vendor ID nor product ID */
    123 	{ -1, -1, 0,
    124 	  { "SHUTTLE TECHNOLOGY LTD.", "PCCARD-IDE/ATAPI Adapter", NULL, NULL},
    125 	  "SHUTTLE TECHNOLOGY IDE/ATAPI Adapter"
    126 	},
    127 
    128 	/* Toshiba Portege 3110 CD, neither vendor ID nor product ID */
    129 	{ -1, -1, 0,
    130 	  { "FREECOM", "PCCARD-IDE", NULL, NULL},
    131 	  "FREECOM PCCARD-IDE"
    132 	},
    133 
    134 	/* Random CD-ROM, (badged AMACOM), neither vendor ID nor product ID */
    135 	{ -1, -1, 0,
    136 	  { "PCMCIA", "CD-ROM", NULL, NULL},
    137 	  "PCMCIA CD-ROM"
    138 	},
    139 
    140 	/* IO DATA CBIDE2, with neither vendor ID nor product ID */
    141 	{ -1, -1, 0,
    142 	  PCMCIA_CIS_IODATA_CBIDE2,
    143 	  PCMCIA_STR_IODATA_CBIDE2
    144 	},
    145 
    146 	/*
    147 	 * Novac PCMCIA-IDE Card for HD530P IDE Box,
    148 	 * with neither vendor ID nor product ID
    149 	 */
    150 	{ -1, -1, 0,
    151 	  { "PCMCIA", "PnPIDE", NULL, NULL},
    152 	  "Novac PCCARD-IDE"
    153 	},
    154 
    155 	{ 0, 0, 0, { NULL, NULL, NULL, NULL}, NULL }
    156 };
    157 
    158 const struct wdc_pcmcia_product *
    159 	wdc_pcmcia_lookup __P((struct pcmcia_attach_args *));
    160 
    161 int	wdc_pcmcia_enable __P((struct device *, int));
    162 
    163 const struct wdc_pcmcia_product *
    164 wdc_pcmcia_lookup(pa)
    165 	struct pcmcia_attach_args *pa;
    166 {
    167 	const struct wdc_pcmcia_product *wpp;
    168 	int i, cis_match;
    169 
    170 	for (wpp = wdc_pcmcia_products; wpp->wpp_name != NULL; wpp++)
    171 		if ((wpp->wpp_vendor == -1 ||
    172 		     pa->manufacturer == wpp->wpp_vendor) &&
    173 		    (wpp->wpp_product == -1 ||
    174 		     pa->product == wpp->wpp_product)) {
    175 			cis_match = 1;
    176 			for (i = 0; i < 4; i++) {
    177 				if (!(wpp->wpp_cis_info[i] == NULL ||
    178 				      (pa->card->cis1_info[i] != NULL &&
    179 				       strcmp(pa->card->cis1_info[i],
    180 					      wpp->wpp_cis_info[i]) == 0)))
    181 					cis_match = 0;
    182 			}
    183 			if (cis_match)
    184 				return (wpp);
    185 		}
    186 
    187 	return (NULL);
    188 }
    189 
    190 static int
    191 wdc_pcmcia_match(parent, match, aux)
    192 	struct device *parent;
    193 	struct cfdata *match;
    194 	void *aux;
    195 {
    196 	struct pcmcia_attach_args *pa = aux;
    197 
    198 	if (pa->pf->function == PCMCIA_FUNCTION_DISK &&
    199 	    pa->pf->pf_funce_disk_interface == PCMCIA_TPLFE_DDI_PCCARD_ATA) {
    200 		return 10;
    201 	}
    202 
    203 	if (wdc_pcmcia_lookup(pa) != NULL)
    204 		return (1);
    205 
    206 	return (0);
    207 }
    208 
    209 static void
    210 wdc_pcmcia_attach(parent, self, aux)
    211 	struct device *parent;
    212 	struct device *self;
    213 	void *aux;
    214 {
    215 	struct wdc_pcmcia_softc *sc = (void *)self;
    216 	struct pcmcia_attach_args *pa = aux;
    217 	struct pcmcia_config_entry *cfe;
    218 	const struct wdc_pcmcia_product *wpp;
    219 	bus_addr_t offset;
    220 	int quirks;
    221 
    222 	sc->sc_pf = pa->pf;
    223 
    224 	for (cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head); cfe != NULL;
    225 	    cfe = SIMPLEQ_NEXT(cfe, cfe_list)) {
    226 		if (cfe->num_iospace != 1 && cfe->num_iospace != 2)
    227 			continue;
    228 
    229 		if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
    230 		    cfe->iospace[0].length,
    231 		    cfe->iospace[0].start == 0 ? cfe->iospace[0].length : 0,
    232 		    &sc->sc_pioh))
    233 			continue;
    234 
    235 		if (cfe->num_iospace == 2) {
    236 			if (!pcmcia_io_alloc(pa->pf, cfe->iospace[1].start,
    237 			    cfe->iospace[1].length, 0, &sc->sc_auxpioh))
    238 				break;
    239 		} else /* num_iospace == 1 */ {
    240 			sc->sc_auxpioh.iot = sc->sc_pioh.iot;
    241 			if (!bus_space_subregion(sc->sc_pioh.iot,
    242 			    sc->sc_pioh.ioh, WDC_PCMCIA_AUXREG_OFFSET,
    243 			    WDC_PCMCIA_AUXREG_NPORTS, &sc->sc_auxpioh.ioh))
    244 				break;
    245 		}
    246 		pcmcia_io_free(pa->pf, &sc->sc_pioh);
    247 	}
    248 
    249 	/*
    250 	 * Compact Falsh memory mapped mode
    251 	 * CF+ and CompactFlash Spec. Rev 1.4, 6.1.3 Memory Mapped Addressing.
    252 	 * http://www.compactflash.org/cfspc1_4.pdf
    253 	 */
    254 	if (cfe == NULL) {
    255 		SIMPLEQ_FOREACH(cfe, &pa->pf->cfe_head, cfe_list) {
    256 			if (cfe->iftype != PCMCIA_IFTYPE_MEMORY)
    257 				continue;
    258 			if (pcmcia_mem_alloc(pa->pf, cfe->memspace[0].length,
    259 			    &sc->sc_pmembaseh) == 0) {
    260 				sc->sc_flags |= WDC_PCMCIA_MEMMODE;
    261 				break;
    262 			}
    263 		}
    264 	}
    265 
    266 	if (cfe == NULL) {
    267 		printf(": can't handle card info\n");
    268 		goto no_config_entry;
    269 	}
    270 
    271 	/* Enable the card. */
    272 	pcmcia_function_init(pa->pf, cfe);
    273 	if (pcmcia_function_enable(pa->pf)) {
    274 		printf(": function enable failed\n");
    275 		goto enable_failed;
    276 	}
    277 
    278 	wpp = wdc_pcmcia_lookup(pa);
    279 	if (wpp != NULL)
    280 		quirks = wpp->wpp_quirk_flag;
    281 	else
    282 		quirks = 0;
    283 
    284 	if (sc->sc_flags & WDC_PCMCIA_MEMMODE) {
    285 		if (pcmcia_mem_map(pa->pf, PCMCIA_MEM_COMMON, 0,
    286 		    sc->sc_pmembaseh.size, &sc->sc_pmembaseh, &offset,
    287 		    &sc->sc_memwindow)) {
    288 			printf(": can't map memory space\n");
    289 			goto map_failed;
    290 		}
    291 
    292 		sc->sc_pmemh.memt = sc->sc_pmembaseh.memt;
    293 		if (offset == 0) {
    294 			sc->sc_pmemh.memh = sc->sc_pmembaseh.memh;
    295 		} else {
    296 			if (bus_space_subregion(sc->sc_pmemh.memt,
    297 				sc->sc_pmembaseh.memh, offset,
    298 				WDC_PCMCIA_REG_NPORTS, &sc->sc_pmemh.memh))
    299 				goto mapaux_failed;
    300 		}
    301 
    302 		sc->sc_auxpmemh.memt = sc->sc_pmemh.memt;
    303 		if (bus_space_subregion(sc->sc_pmemh.memt,
    304 		    sc->sc_pmemh.memh, WDC_PCMCIA_AUXREG_OFFSET,
    305 		    WDC_PCMCIA_AUXREG_NPORTS, &sc->sc_auxpmemh.memh))
    306 			goto mapaux_failed;
    307 
    308 		printf(" memory mapped mode");
    309 	} else {
    310 		if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, 0,
    311 		    sc->sc_pioh.size, &sc->sc_pioh, &sc->sc_iowindow)) {
    312 			printf(": can't map first I/O space\n");
    313 			goto map_failed;
    314 		}
    315 	}
    316 
    317 	if (cfe->num_iospace <= 1 || sc->sc_flags & WDC_PCMCIA_MEMMODE)
    318 		sc->sc_auxiowindow = -1;
    319 	else if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, 0,
    320 	    sc->sc_auxpioh.size, &sc->sc_auxpioh, &sc->sc_auxiowindow)) {
    321 		printf(": can't map second I/O space\n");
    322 		goto mapaux_failed;
    323 	}
    324 
    325 	if ((wpp != NULL) && (wpp->wpp_name != NULL))
    326 		printf(": %s", wpp->wpp_name);
    327 
    328 	printf("\n");
    329 
    330 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16;
    331 	if (sc->sc_flags & WDC_PCMCIA_MEMMODE) {
    332 		sc->wdc_channel.cmd_iot = sc->sc_pmemh.memt;
    333 		sc->wdc_channel.cmd_ioh = sc->sc_pmemh.memh;
    334 		sc->wdc_channel.ctl_iot = sc->sc_auxpmemh.memt;
    335 		sc->wdc_channel.ctl_ioh = sc->sc_auxpmemh.memh;
    336 	} else {
    337 		sc->wdc_channel.cmd_iot = sc->sc_pioh.iot;
    338 		sc->wdc_channel.cmd_ioh = sc->sc_pioh.ioh;
    339 		sc->wdc_channel.ctl_iot = sc->sc_auxpioh.iot;
    340 		sc->wdc_channel.ctl_ioh = sc->sc_auxpioh.ioh;
    341 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32;
    342 	}
    343 	sc->wdc_channel.data32iot = sc->wdc_channel.cmd_iot;
    344 	sc->wdc_channel.data32ioh = sc->wdc_channel.cmd_ioh;
    345 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_SINGLE_DRIVE;
    346 	sc->sc_wdcdev.PIO_cap = 0;
    347 	sc->wdc_chanptr = &sc->wdc_channel;
    348 	sc->sc_wdcdev.channels = &sc->wdc_chanptr;
    349 	sc->sc_wdcdev.nchannels = 1;
    350 	sc->wdc_channel.channel = 0;
    351 	sc->wdc_channel.wdc = &sc->sc_wdcdev;
    352 	sc->wdc_channel.ch_queue = malloc(sizeof(struct channel_queue),
    353 	    M_DEVBUF, M_NOWAIT);
    354 	if (sc->wdc_channel.ch_queue == NULL) {
    355 		printf("%s: can't allocate memory for command queue\n",
    356 		    sc->sc_wdcdev.sc_dev.dv_xname);
    357 		goto ch_queue_alloc_failed;
    358 	}
    359 	if (quirks & WDC_PCMCIA_NO_EXTRA_RESETS)
    360 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_NO_EXTRA_RESETS;
    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);	/* should return an error XXX */
    368 	sc->sc_flags &= ~WDC_PCMCIA_ATTACH;
    369 	return;
    370 
    371  ch_queue_alloc_failed:
    372 	/* Unmap our aux i/o window. */
    373 	if (!(sc->sc_flags & WDC_PCMCIA_MEMMODE) && (sc->sc_auxiowindow != -1))
    374 		pcmcia_io_unmap(sc->sc_pf, sc->sc_auxiowindow);
    375 
    376  mapaux_failed:
    377 	/* Unmap our i/o window. */
    378 	if (sc->sc_flags & WDC_PCMCIA_MEMMODE)
    379 		pcmcia_mem_unmap(sc->sc_pf, sc->sc_memwindow);
    380 	else
    381 		pcmcia_io_unmap(sc->sc_pf, sc->sc_iowindow);
    382 
    383  map_failed:
    384 	/* Disable the function */
    385 	pcmcia_function_disable(sc->sc_pf);
    386 
    387  enable_failed:
    388 	/* Unmap our i/o space. */
    389 	if (sc->sc_flags & WDC_PCMCIA_MEMMODE) {
    390 		pcmcia_mem_free(sc->sc_pf, &sc->sc_pmembaseh);
    391 	} else  {
    392 		pcmcia_io_free(sc->sc_pf, &sc->sc_pioh);
    393 		if (cfe->num_iospace == 2)
    394 		    pcmcia_io_free(sc->sc_pf, &sc->sc_auxpioh);
    395 	}
    396  no_config_entry:
    397 	sc->sc_iowindow = -1;
    398 }
    399 
    400 int
    401 wdc_pcmcia_detach(self, flags)
    402 	struct device *self;
    403 	int flags;
    404 {
    405 	struct wdc_pcmcia_softc *sc = (struct wdc_pcmcia_softc *)self;
    406 	int error;
    407 
    408 	if (sc->sc_iowindow == -1)
    409 		/* Nothing to detach */
    410 		return (0);
    411 
    412 	if ((error = wdcdetach(self, flags)) != 0)
    413 		return (error);
    414 
    415 	if (sc->wdc_channel.ch_queue != NULL)
    416 		free(sc->wdc_channel.ch_queue, M_DEVBUF);
    417 
    418 	/* Unmap our i/o window and i/o space. */
    419 	if (sc->sc_flags & WDC_PCMCIA_MEMMODE) {
    420 		pcmcia_mem_unmap(sc->sc_pf, sc->sc_memwindow);
    421 		pcmcia_mem_free(sc->sc_pf, &sc->sc_pmembaseh);
    422 	} else {
    423 		pcmcia_io_unmap(sc->sc_pf, sc->sc_iowindow);
    424 		pcmcia_io_free(sc->sc_pf, &sc->sc_pioh);
    425 		if (sc->sc_auxiowindow != -1) {
    426 			pcmcia_io_unmap(sc->sc_pf, sc->sc_auxiowindow);
    427 			pcmcia_io_free(sc->sc_pf, &sc->sc_auxpioh);
    428 		}
    429 	}
    430 
    431 	return (0);
    432 }
    433 
    434 int
    435 wdc_pcmcia_enable(self, onoff)
    436 	struct device *self;
    437 	int onoff;
    438 {
    439 	struct wdc_pcmcia_softc *sc = (void *)self;
    440 
    441 	if (onoff) {
    442 		/* See the comment in aic_pcmcia_enable */
    443 		if ((sc->sc_flags & WDC_PCMCIA_ATTACH) == 0) {
    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: "
    449 				    "couldn't establish interrupt handler\n",
    450 				    sc->sc_wdcdev.sc_dev.dv_xname);
    451 				return (EIO);
    452 			}
    453 
    454 			if (pcmcia_function_enable(sc->sc_pf)) {
    455 				printf("%s: couldn't enable PCMCIA function\n",
    456 				    sc->sc_wdcdev.sc_dev.dv_xname);
    457 				pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    458 				return (EIO);
    459 			}
    460 			wdcreset(&sc->wdc_channel, VERBOSE);
    461 		}
    462 	} else {
    463 		pcmcia_function_disable(sc->sc_pf);
    464 		if ((sc->sc_flags & WDC_PCMCIA_ATTACH) == 0)
    465 			pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    466 	}
    467 
    468 	return (0);
    469 }
    470