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