Home | History | Annotate | Line # | Download | only in pcmcia
wdc_pcmcia.c revision 1.41
      1 /*	$NetBSD: wdc_pcmcia.c,v 1.41 2001/03/08 15:20:57 uch 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((void *, int));
    162 
    163 
    164 
    165 const struct wdc_pcmcia_product *
    166 wdc_pcmcia_lookup(pa)
    167 	struct pcmcia_attach_args *pa;
    168 {
    169 	const struct wdc_pcmcia_product *wpp;
    170 	int i, cis_match;
    171 
    172 	for (wpp = wdc_pcmcia_products; wpp->wpp_name != NULL; wpp++)
    173 		if ((wpp->wpp_vendor == -1 ||
    174 		     pa->manufacturer == wpp->wpp_vendor) &&
    175 		    (wpp->wpp_product == -1 ||
    176 		     pa->product == wpp->wpp_product)) {
    177 			cis_match = 1;
    178 			for (i = 0; i < 4; i++) {
    179 				if (!(wpp->wpp_cis_info[i] == NULL ||
    180 				      (pa->card->cis1_info[i] != NULL &&
    181 				       strcmp(pa->card->cis1_info[i],
    182 					      wpp->wpp_cis_info[i]) == 0)))
    183 					cis_match = 0;
    184 			}
    185 			if (cis_match)
    186 				return (wpp);
    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_addr_t offset;
    222 	int quirks;
    223 
    224 	sc->sc_pf = pa->pf;
    225 
    226 	for (cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head); cfe != NULL;
    227 	    cfe = SIMPLEQ_NEXT(cfe, cfe_list)) {
    228 		if (cfe->num_iospace != 1 && cfe->num_iospace != 2)
    229 			continue;
    230 
    231 		if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
    232 		    cfe->iospace[0].length,
    233 		    cfe->iospace[0].start == 0 ? cfe->iospace[0].length : 0,
    234 		    &sc->sc_pioh))
    235 			continue;
    236 
    237 		if (cfe->num_iospace == 2) {
    238 			if (!pcmcia_io_alloc(pa->pf, cfe->iospace[1].start,
    239 			    cfe->iospace[1].length, 0, &sc->sc_auxpioh))
    240 				break;
    241 		} else /* num_iospace == 1 */ {
    242 			sc->sc_auxpioh.iot = sc->sc_pioh.iot;
    243 			if (!bus_space_subregion(sc->sc_pioh.iot,
    244 			    sc->sc_pioh.ioh, WDC_PCMCIA_AUXREG_OFFSET,
    245 			    WDC_PCMCIA_AUXREG_NPORTS, &sc->sc_auxpioh.ioh))
    246 				break;
    247 		}
    248 		pcmcia_io_free(pa->pf, &sc->sc_pioh);
    249 	}
    250 
    251 	/*
    252 	 * Compact Falsh memory mapped mode
    253 	 * CF+ and CompactFlash Spec. Rev 1.4, 6.1.3 Memory Mapped Addressing.
    254 	 * http://www.compactflash.org/cfspc1_4.pdf
    255 	 */
    256 	if (cfe == NULL) {
    257 		SIMPLEQ_FOREACH(cfe, &pa->pf->cfe_head, cfe_list) {
    258 			if (cfe->iftype != PCMCIA_IFTYPE_MEMORY)
    259 				continue;
    260 			if (pcmcia_mem_alloc(pa->pf, cfe->memspace[0].length,
    261 			    &sc->sc_pmembaseh) == 0) {
    262 				sc->sc_flags |= WDC_PCMCIA_MEMMODE;
    263 				break;
    264 			}
    265 		}
    266 	}
    267 
    268 	if (cfe == NULL) {
    269 		printf(": can't handle card info\n");
    270 		goto no_config_entry;
    271 	}
    272 
    273 	/* Enable the card. */
    274 	pcmcia_function_init(pa->pf, cfe);
    275 	if (pcmcia_function_enable(pa->pf)) {
    276 		printf(": function enable failed\n");
    277 		goto enable_failed;
    278 	}
    279 
    280 	wpp = wdc_pcmcia_lookup(pa);
    281 	if (wpp != NULL)
    282 		quirks = wpp->wpp_quirk_flag;
    283 	else
    284 		quirks = 0;
    285 
    286 	if (sc->sc_flags & WDC_PCMCIA_MEMMODE) {
    287 		if (pcmcia_mem_map(pa->pf, PCMCIA_MEM_COMMON, 0,
    288 		    sc->sc_pmembaseh.size, &sc->sc_pmembaseh, &offset,
    289 		    &sc->sc_memwindow)) {
    290 			printf(": can't map memory space\n");
    291 			goto map_failed;
    292 		}
    293 
    294 		sc->sc_pmemh.memt = sc->sc_pmembaseh.memt;
    295 		if (offset == 0) {
    296 			sc->sc_pmemh.memh = sc->sc_pmembaseh.memh;
    297 		} else {
    298 			if (bus_space_subregion(sc->sc_pmemh.memt,
    299 				sc->sc_pmembaseh.memh, offset,
    300 				WDC_PCMCIA_REG_NPORTS, &sc->sc_pmemh.memh))
    301 				goto mapaux_failed;
    302 		}
    303 
    304 		sc->sc_auxpmemh.memt = sc->sc_pmemh.memt;
    305 		if (bus_space_subregion(sc->sc_pmemh.memt,
    306 		    sc->sc_pmemh.memh, WDC_PCMCIA_AUXREG_OFFSET,
    307 		    WDC_PCMCIA_AUXREG_NPORTS, &sc->sc_auxpmemh.memh))
    308 			goto mapaux_failed;
    309 
    310 		printf(" memory mapped mode");
    311 	} else {
    312 		if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, 0,
    313 		    sc->sc_pioh.size, &sc->sc_pioh, &sc->sc_iowindow)) {
    314 			printf(": can't map first I/O space\n");
    315 			goto map_failed;
    316 		}
    317 	}
    318 
    319 	if (cfe->num_iospace <= 1 || sc->sc_flags & WDC_PCMCIA_MEMMODE)
    320 		sc->sc_auxiowindow = -1;
    321 	else if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, 0,
    322 	    sc->sc_auxpioh.size, &sc->sc_auxpioh, &sc->sc_auxiowindow)) {
    323 		printf(": can't map second I/O space\n");
    324 		goto mapaux_failed;
    325 	}
    326 
    327 	if ((wpp != NULL) && (wpp->wpp_name != NULL))
    328 		printf(": %s", wpp->wpp_name);
    329 
    330 	printf("\n");
    331 
    332 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16;
    333 	if (sc->sc_flags & WDC_PCMCIA_MEMMODE) {
    334 		sc->wdc_channel.cmd_iot = sc->sc_pmemh.memt;
    335 		sc->wdc_channel.cmd_ioh = sc->sc_pmemh.memh;
    336 		sc->wdc_channel.ctl_iot = sc->sc_auxpmemh.memt;
    337 		sc->wdc_channel.ctl_ioh = sc->sc_auxpmemh.memh;
    338 	} else {
    339 		sc->wdc_channel.cmd_iot = sc->sc_pioh.iot;
    340 		sc->wdc_channel.cmd_ioh = sc->sc_pioh.ioh;
    341 		sc->wdc_channel.ctl_iot = sc->sc_auxpioh.iot;
    342 		sc->wdc_channel.ctl_ioh = sc->sc_auxpioh.ioh;
    343 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32;
    344 	}
    345 	sc->wdc_channel.data32iot = sc->wdc_channel.cmd_iot;
    346 	sc->wdc_channel.data32ioh = sc->wdc_channel.cmd_ioh;
    347 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_SINGLE_DRIVE;
    348 	sc->sc_wdcdev.PIO_cap = 0;
    349 	sc->wdc_chanptr = &sc->wdc_channel;
    350 	sc->sc_wdcdev.channels = &sc->wdc_chanptr;
    351 	sc->sc_wdcdev.nchannels = 1;
    352 	sc->wdc_channel.channel = 0;
    353 	sc->wdc_channel.wdc = &sc->sc_wdcdev;
    354 	sc->wdc_channel.ch_queue = malloc(sizeof(struct channel_queue),
    355 	    M_DEVBUF, M_NOWAIT);
    356 	if (sc->wdc_channel.ch_queue == NULL) {
    357 		printf("%s: can't allocate memory for command queue\n",
    358 		    sc->sc_wdcdev.sc_dev.dv_xname);
    359 		goto ch_queue_alloc_failed;
    360 	}
    361 	if (quirks & WDC_PCMCIA_NO_EXTRA_RESETS)
    362 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_NO_EXTRA_RESETS;
    363 
    364 	/* We can enable and disable the controller. */
    365 	sc->sc_wdcdev.sc_atapi_adapter._generic.scsipi_enable =
    366 	    wdc_pcmcia_enable;
    367 
    368 	sc->sc_flags |= WDC_PCMCIA_ATTACH;
    369 	wdcattach(&sc->wdc_channel);	/* should return an error XXX */
    370 	sc->sc_flags &= ~WDC_PCMCIA_ATTACH;
    371 	return;
    372 
    373  ch_queue_alloc_failed:
    374 	/* Unmap our aux i/o window. */
    375 	if (!(sc->sc_flags & WDC_PCMCIA_MEMMODE) && (sc->sc_auxiowindow != -1))
    376 		pcmcia_io_unmap(sc->sc_pf, sc->sc_auxiowindow);
    377 
    378  mapaux_failed:
    379 	/* Unmap our i/o window. */
    380 	if (sc->sc_flags & WDC_PCMCIA_MEMMODE)
    381 		pcmcia_mem_unmap(sc->sc_pf, sc->sc_memwindow);
    382 	else
    383 		pcmcia_io_unmap(sc->sc_pf, sc->sc_iowindow);
    384 
    385  map_failed:
    386 	/* Disable the function */
    387 	pcmcia_function_disable(sc->sc_pf);
    388 
    389  enable_failed:
    390 	/* Unmap our i/o space. */
    391 	if (sc->sc_flags & WDC_PCMCIA_MEMMODE) {
    392 		pcmcia_mem_free(sc->sc_pf, &sc->sc_pmembaseh);
    393 	} else  {
    394 		pcmcia_io_free(sc->sc_pf, &sc->sc_pioh);
    395 		if (cfe->num_iospace == 2)
    396 		    pcmcia_io_free(sc->sc_pf, &sc->sc_auxpioh);
    397 	}
    398  no_config_entry:
    399 	sc->sc_iowindow = -1;
    400 }
    401 
    402 int
    403 wdc_pcmcia_detach(self, flags)
    404 	struct device *self;
    405 	int flags;
    406 {
    407 	struct wdc_pcmcia_softc *sc = (struct wdc_pcmcia_softc *)self;
    408 	int error;
    409 
    410 	if (sc->sc_iowindow == -1)
    411 		/* Nothing to detach */
    412 		return (0);
    413 
    414 	if ((error = wdcdetach(self, flags)) != 0)
    415 		return (error);
    416 
    417 	if (sc->wdc_channel.ch_queue != NULL)
    418 		free(sc->wdc_channel.ch_queue, M_DEVBUF);
    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(arg, onoff)
    438 	void *arg;
    439 	int onoff;
    440 {
    441 	struct wdc_pcmcia_softc *sc = arg;
    442 
    443 	if (onoff) {
    444 		/* See the comment in aic_pcmcia_enable */
    445 		if ((sc->sc_flags & WDC_PCMCIA_ATTACH) == 0) {
    446 			/* Establish the interrupt handler. */
    447 			sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
    448 			    wdcintr, &sc->wdc_channel);
    449 			if (sc->sc_ih == NULL) {
    450 				printf("%s: "
    451 				    "couldn't establish interrupt handler\n",
    452 				    sc->sc_wdcdev.sc_dev.dv_xname);
    453 				return (EIO);
    454 			}
    455 
    456 			if (pcmcia_function_enable(sc->sc_pf)) {
    457 				printf("%s: couldn't enable PCMCIA function\n",
    458 				    sc->sc_wdcdev.sc_dev.dv_xname);
    459 				pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    460 				return (EIO);
    461 			}
    462 			wdcreset(&sc->wdc_channel, VERBOSE);
    463 		}
    464 	} else {
    465 		pcmcia_function_disable(sc->sc_pf);
    466 		if ((sc->sc_flags & WDC_PCMCIA_ATTACH) == 0)
    467 			pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    468 	}
    469 
    470 	return (0);
    471 }
    472