Home | History | Annotate | Line # | Download | only in pcmcia
wdc_pcmcia.c revision 1.53
      1 /*	$NetBSD: wdc_pcmcia.c,v 1.53 2003/03/22 20:09:35 matt 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.53 2003/03/22 20:09:35 matt 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_chanlist[1];
     64 	struct channel_softc wdc_channel;
     65 	struct channel_queue wdc_chqueue;
     66 	struct pcmcia_io_handle sc_pioh;
     67 	struct pcmcia_io_handle sc_auxpioh;
     68 	struct pcmcia_mem_handle sc_pmembaseh;
     69 	struct pcmcia_mem_handle sc_pmemh;
     70 	struct pcmcia_mem_handle sc_auxpmemh;
     71 	int sc_memwindow;
     72 	int sc_iowindow;
     73 	int sc_auxiowindow;
     74 	void *sc_ih;
     75 	struct pcmcia_function *sc_pf;
     76 	int sc_flags;
     77 #define	WDC_PCMCIA_ATTACH	0x0001
     78 #define WDC_PCMCIA_MEMMODE	0x0002
     79 };
     80 
     81 static int wdc_pcmcia_match	__P((struct device *, struct cfdata *, void *));
     82 static void wdc_pcmcia_attach	__P((struct device *, struct device *, void *));
     83 static int wdc_pcmcia_detach	__P((struct device *, int));
     84 
     85 CFATTACH_DECL(wdc_pcmcia, sizeof(struct wdc_pcmcia_softc),
     86     wdc_pcmcia_match, wdc_pcmcia_attach, wdc_pcmcia_detach, wdcactivate);
     87 
     88 const struct wdc_pcmcia_product {
     89 	u_int32_t	wpp_vendor;	/* vendor ID */
     90 	u_int32_t	wpp_product;	/* product ID */
     91 	int		wpp_quirk_flag;	/* Quirk flags */
     92 #define WDC_PCMCIA_NO_EXTRA_RESETS	0x02 /* Only reset ctrl once */
     93 	const char	*wpp_cis_info[4];	/* XXX necessary? */
     94 	const char	*wpp_name;	/* product name */
     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 	  PCMCIA_STR_DIGITAL_MOBILE_MEDIA_CDROM },
    101 
    102 	{ PCMCIA_VENDOR_IBM,
    103 	  PCMCIA_PRODUCT_IBM_PORTABLE_CDROM,
    104 	  0, { NULL, "PCMCIA Portable CD-ROM Drive", NULL, NULL },
    105 	  PCMCIA_STR_IBM_PORTABLE_CDROM },
    106 
    107 	/* The TEAC IDE/Card II is used on the Sony Vaio */
    108 	{ PCMCIA_VENDOR_TEAC,
    109 	  PCMCIA_PRODUCT_TEAC_IDECARDII,
    110 	  WDC_PCMCIA_NO_EXTRA_RESETS,
    111 	  PCMCIA_CIS_TEAC_IDECARDII,
    112 	  PCMCIA_STR_TEAC_IDECARDII },
    113 
    114 	/*
    115 	 * A fujitsu rebranded panasonic drive that reports
    116 	 * itself as function "scsi", disk interface 0
    117 	 */
    118 	{ PCMCIA_VENDOR_PANASONIC,
    119 	  PCMCIA_PRODUCT_PANASONIC_KXLC005,
    120 	  0,
    121 	  PCMCIA_CIS_PANASONIC_KXLC005,
    122 	  PCMCIA_STR_PANASONIC_KXLC005 },
    123 
    124 	/*
    125 	 * EXP IDE/ATAPI DVD Card use with some DVD players.
    126 	 * Does not have a vendor ID or product ID.
    127 	 */
    128 	{ -1,
    129 	  -1,
    130 	  0,
    131 	  PCMCIA_CIS_EXP_EXPMULTIMEDIA,
    132 	  PCMCIA_STR_EXP_EXPMULTIMEDIA },
    133 
    134 	/* Mobile Dock 2, neither vendor ID nor product ID */
    135 	{ -1, -1, 0,
    136 	  { "SHUTTLE TECHNOLOGY LTD.", "PCCARD-IDE/ATAPI Adapter", NULL, NULL},
    137 	  "SHUTTLE TECHNOLOGY IDE/ATAPI Adapter"
    138 	},
    139 
    140 	/* Toshiba Portege 3110 CD, neither vendor ID nor product ID */
    141 	{ -1, -1, 0,
    142 	  { "FREECOM", "PCCARD-IDE", NULL, NULL},
    143 	  "FREECOM PCCARD-IDE"
    144 	},
    145 
    146 	/* Random CD-ROM, (badged AMACOM), neither vendor ID nor product ID */
    147 	{ -1, -1, 0,
    148 	  { "PCMCIA", "CD-ROM", NULL, NULL},
    149 	  "PCMCIA CD-ROM"
    150 	},
    151 
    152 	/* IO DATA CBIDE2, with neither vendor ID nor product ID */
    153 	{ -1, -1, 0,
    154 	  PCMCIA_CIS_IODATA_CBIDE2,
    155 	  PCMCIA_STR_IODATA_CBIDE2
    156 	},
    157 
    158 	/*
    159 	 * Novac PCMCIA-IDE Card for HD530P IDE Box,
    160 	 * with neither vendor ID nor product ID
    161 	 */
    162 	{ -1, -1, 0,
    163 	  { "PCMCIA", "PnPIDE", NULL, NULL},
    164 	  "Novac PCCARD-IDE"
    165 	},
    166 
    167 	{ 0, 0, 0, { NULL, NULL, NULL, NULL}, NULL }
    168 };
    169 
    170 const struct wdc_pcmcia_product *
    171 	wdc_pcmcia_lookup __P((struct pcmcia_attach_args *));
    172 
    173 int	wdc_pcmcia_enable __P((struct device *, int));
    174 
    175 const struct wdc_pcmcia_product *
    176 wdc_pcmcia_lookup(pa)
    177 	struct pcmcia_attach_args *pa;
    178 {
    179 	const struct wdc_pcmcia_product *wpp;
    180 	int i, cis_match;
    181 
    182 	for (wpp = wdc_pcmcia_products; wpp->wpp_name != NULL; wpp++)
    183 		if ((wpp->wpp_vendor == -1 ||
    184 		     pa->manufacturer == wpp->wpp_vendor) &&
    185 		    (wpp->wpp_product == -1 ||
    186 		     pa->product == wpp->wpp_product)) {
    187 			cis_match = 1;
    188 			for (i = 0; i < 4; i++) {
    189 				if (!(wpp->wpp_cis_info[i] == NULL ||
    190 				      (pa->card->cis1_info[i] != NULL &&
    191 				       strcmp(pa->card->cis1_info[i],
    192 					      wpp->wpp_cis_info[i]) == 0)))
    193 					cis_match = 0;
    194 			}
    195 			if (cis_match)
    196 				return (wpp);
    197 		}
    198 
    199 	return (NULL);
    200 }
    201 
    202 static int
    203 wdc_pcmcia_match(parent, match, aux)
    204 	struct device *parent;
    205 	struct cfdata *match;
    206 	void *aux;
    207 {
    208 	struct pcmcia_attach_args *pa = aux;
    209 
    210 	if (pa->pf->function == PCMCIA_FUNCTION_DISK &&
    211 	    pa->pf->pf_funce_disk_interface == PCMCIA_TPLFE_DDI_PCCARD_ATA) {
    212 		return 10;
    213 	}
    214 
    215 	if (wdc_pcmcia_lookup(pa) != NULL)
    216 		return (1);
    217 
    218 	return (0);
    219 }
    220 
    221 static void
    222 wdc_pcmcia_attach(parent, self, aux)
    223 	struct device *parent;
    224 	struct device *self;
    225 	void *aux;
    226 {
    227 	struct wdc_pcmcia_softc *sc = (void *)self;
    228 	struct pcmcia_attach_args *pa = aux;
    229 	struct pcmcia_config_entry *cfe;
    230 	const struct wdc_pcmcia_product *wpp;
    231 	bus_size_t offset;
    232 	int quirks;
    233 
    234 	sc->sc_pf = pa->pf;
    235 
    236 	SIMPLEQ_FOREACH(cfe, &pa->pf->cfe_head, cfe_list) {
    237 		if (cfe->num_iospace != 1 && cfe->num_iospace != 2)
    238 			continue;
    239 
    240 		if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
    241 		    cfe->iospace[0].length,
    242 		    cfe->iospace[0].start == 0 ? cfe->iospace[0].length : 0,
    243 		    &sc->sc_pioh))
    244 			continue;
    245 
    246 		if (cfe->num_iospace == 2) {
    247 			if (!pcmcia_io_alloc(pa->pf, cfe->iospace[1].start,
    248 			    cfe->iospace[1].length, 0, &sc->sc_auxpioh))
    249 				break;
    250 		} else /* num_iospace == 1 */ {
    251 			sc->sc_auxpioh.iot = sc->sc_pioh.iot;
    252 			if (!bus_space_subregion(sc->sc_pioh.iot,
    253 			    sc->sc_pioh.ioh, WDC_PCMCIA_AUXREG_OFFSET,
    254 			    WDC_PCMCIA_AUXREG_NPORTS, &sc->sc_auxpioh.ioh))
    255 				break;
    256 		}
    257 		pcmcia_io_free(pa->pf, &sc->sc_pioh);
    258 	}
    259 
    260 	/*
    261 	 * Compact Flash memory mapped mode
    262 	 * CF+ and CompactFlash Spec. Rev 1.4, 6.1.3 Memory Mapped Addressing.
    263 	 * http://www.compactflash.org/cfspc1_4.pdf
    264 	 */
    265 	if (cfe == NULL) {
    266 		SIMPLEQ_FOREACH(cfe, &pa->pf->cfe_head, cfe_list) {
    267 			if (cfe->iftype != PCMCIA_IFTYPE_MEMORY)
    268 				continue;
    269 			if (pcmcia_mem_alloc(pa->pf, cfe->memspace[0].length,
    270 			    &sc->sc_pmembaseh) == 0) {
    271 				sc->sc_flags |= WDC_PCMCIA_MEMMODE;
    272 				break;
    273 			}
    274 		}
    275 	}
    276 
    277 	if (cfe == NULL) {
    278 		printf(": can't handle card info\n");
    279 		goto no_config_entry;
    280 	}
    281 
    282 	/* Enable the card. */
    283 	pcmcia_function_init(pa->pf, cfe);
    284 	if (pcmcia_function_enable(pa->pf)) {
    285 		printf(": function enable failed\n");
    286 		goto enable_failed;
    287 	}
    288 
    289 	wpp = wdc_pcmcia_lookup(pa);
    290 	if (wpp != NULL)
    291 		quirks = wpp->wpp_quirk_flag;
    292 	else
    293 		quirks = 0;
    294 
    295 	if (sc->sc_flags & WDC_PCMCIA_MEMMODE) {
    296 		if (pcmcia_mem_map(pa->pf, PCMCIA_MEM_COMMON, 0,
    297 		    sc->sc_pmembaseh.size, &sc->sc_pmembaseh, &offset,
    298 		    &sc->sc_memwindow)) {
    299 			printf(": can't map memory space\n");
    300 			goto map_failed;
    301 		}
    302 
    303 		sc->sc_pmemh.memt = sc->sc_pmembaseh.memt;
    304 		if (offset == 0) {
    305 			sc->sc_pmemh.memh = sc->sc_pmembaseh.memh;
    306 		} else {
    307 			if (bus_space_subregion(sc->sc_pmemh.memt,
    308 				sc->sc_pmembaseh.memh, offset,
    309 				WDC_PCMCIA_REG_NPORTS, &sc->sc_pmemh.memh))
    310 				goto mapaux_failed;
    311 		}
    312 
    313 		sc->sc_auxpmemh.memt = sc->sc_pmemh.memt;
    314 		if (bus_space_subregion(sc->sc_pmemh.memt,
    315 		    sc->sc_pmemh.memh, WDC_PCMCIA_AUXREG_OFFSET,
    316 		    WDC_PCMCIA_AUXREG_NPORTS, &sc->sc_auxpmemh.memh))
    317 			goto mapaux_failed;
    318 
    319 		printf(" memory mapped mode");
    320 	} else {
    321 		if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, 0,
    322 		    sc->sc_pioh.size, &sc->sc_pioh, &sc->sc_iowindow)) {
    323 			printf(": can't map first I/O space\n");
    324 			goto map_failed;
    325 		}
    326 	}
    327 
    328 	if (cfe->num_iospace <= 1 || sc->sc_flags & WDC_PCMCIA_MEMMODE)
    329 		sc->sc_auxiowindow = -1;
    330 	else if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, 0,
    331 	    sc->sc_auxpioh.size, &sc->sc_auxpioh, &sc->sc_auxiowindow)) {
    332 		printf(": can't map second I/O space\n");
    333 		goto mapaux_failed;
    334 	}
    335 
    336 	if ((wpp != NULL) && (wpp->wpp_name != NULL))
    337 		printf(": %s", wpp->wpp_name);
    338 
    339 	printf("\n");
    340 
    341 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16;
    342 	if (sc->sc_flags & WDC_PCMCIA_MEMMODE) {
    343 		sc->wdc_channel.cmd_iot = sc->sc_pmemh.memt;
    344 		sc->wdc_channel.cmd_ioh = sc->sc_pmemh.memh;
    345 		sc->wdc_channel.ctl_iot = sc->sc_auxpmemh.memt;
    346 		sc->wdc_channel.ctl_ioh = sc->sc_auxpmemh.memh;
    347 	} else {
    348 		sc->wdc_channel.cmd_iot = sc->sc_pioh.iot;
    349 		sc->wdc_channel.cmd_ioh = sc->sc_pioh.ioh;
    350 		sc->wdc_channel.ctl_iot = sc->sc_auxpioh.iot;
    351 		sc->wdc_channel.ctl_ioh = sc->sc_auxpioh.ioh;
    352 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32;
    353 	}
    354 	sc->wdc_channel.data32iot = sc->wdc_channel.cmd_iot;
    355 	sc->wdc_channel.data32ioh = sc->wdc_channel.cmd_ioh;
    356 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_SINGLE_DRIVE;
    357 	sc->sc_wdcdev.PIO_cap = 0;
    358 	sc->wdc_chanlist[0] = &sc->wdc_channel;
    359 	sc->sc_wdcdev.channels = sc->wdc_chanlist;
    360 	sc->sc_wdcdev.nchannels = 1;
    361 	sc->wdc_channel.channel = 0;
    362 	sc->wdc_channel.wdc = &sc->sc_wdcdev;
    363 	sc->wdc_channel.ch_queue = &sc->wdc_chqueue;
    364 	if (quirks & WDC_PCMCIA_NO_EXTRA_RESETS)
    365 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_NO_EXTRA_RESETS;
    366 
    367 	/* We can enable and disable the controller. */
    368 	sc->sc_wdcdev.sc_atapi_adapter._generic.adapt_enable =
    369 	    wdc_pcmcia_enable;
    370 
    371 	sc->sc_flags |= WDC_PCMCIA_ATTACH;
    372 	wdcattach(&sc->wdc_channel);	/* should return an error XXX */
    373 	sc->sc_flags &= ~WDC_PCMCIA_ATTACH;
    374 	return;
    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