Home | History | Annotate | Line # | Download | only in pcmcia
wdc_pcmcia.c revision 1.35
      1 /*	$NetBSD: wdc_pcmcia.c,v 1.35 2000/10/25 09:32:11 haya 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 	int sc_iowindow;
     65 	int sc_auxiowindow;
     66 	void *sc_ih;
     67 	struct pcmcia_function *sc_pf;
     68 	int sc_flags;
     69 #define	WDC_PCMCIA_ATTACH	0x0001
     70 };
     71 
     72 static int wdc_pcmcia_match	__P((struct device *, struct cfdata *, void *));
     73 static void wdc_pcmcia_attach	__P((struct device *, struct device *, void *));
     74 static int wdc_pcmcia_detach	__P((struct device *, int));
     75 
     76 struct cfattach wdc_pcmcia_ca = {
     77 	sizeof(struct wdc_pcmcia_softc), wdc_pcmcia_match, wdc_pcmcia_attach,
     78 	wdc_pcmcia_detach, wdcactivate
     79 };
     80 
     81 struct wdc_pcmcia_product {
     82 	u_int32_t	wpp_vendor;	/* vendor ID */
     83 	u_int32_t	wpp_product;	/* product ID */
     84 	int		wpp_quirk_flag;	/* Quirk flags */
     85 #define WDC_PCMCIA_NO_EXTRA_RESETS	0x02 /* Only reset ctrl once */
     86 	const char	*wpp_cis_info[4];	/* XXX necessary? */
     87 	const char	*wpp_name;	/* product name */
     88 } wdc_pcmcia_products[] = {
     89 
     90 	{ /* PCMCIA_VENDOR_DIGITAL XXX */ 0x0100,
     91 	  PCMCIA_PRODUCT_DIGITAL_MOBILE_MEDIA_CDROM,
     92 	  0, { NULL, "Digital Mobile Media CD-ROM", NULL, NULL },
     93 	  PCMCIA_STR_DIGITAL_MOBILE_MEDIA_CDROM },
     94 
     95 	{ PCMCIA_VENDOR_IBM,
     96 	  PCMCIA_PRODUCT_IBM_PORTABLE_CDROM,
     97 	  0, { NULL, "PCMCIA Portable CD-ROM Drive", NULL, NULL },
     98 	  PCMCIA_STR_IBM_PORTABLE_CDROM },
     99 
    100 	/* The TEAC IDE/Card II is used on the Sony Vaio */
    101 	{ PCMCIA_VENDOR_TEAC,
    102 	  PCMCIA_PRODUCT_TEAC_IDECARDII,
    103 	  WDC_PCMCIA_NO_EXTRA_RESETS,
    104 	  PCMCIA_CIS_TEAC_IDECARDII,
    105 	  PCMCIA_STR_TEAC_IDECARDII },
    106 
    107 	/*
    108 	 * EXP IDE/ATAPI DVD Card use with some DVD players.
    109 	 * Does not have a vendor ID or product ID.
    110 	 */
    111 	{ -1,
    112 	  -1,
    113 	  0,
    114 	  PCMCIA_CIS_EXP_EXPMULTIMEDIA,
    115 	  PCMCIA_STR_EXP_EXPMULTIMEDIA },
    116 
    117 	/* Mobile Dock 2, which doesn't have vendor ID nor product ID */
    118 	{ -1, -1, 0,
    119 	  { "SHUTTLE TECHNOLOGY LTD.", "PCCARD-IDE/ATAPI Adapter", NULL, NULL},
    120 	  "SHUTTLE TECHNOLOGY IDE/ATAPI Adapter"
    121 	},
    122 
    123 	/* Toshiba Portege 3110 CD, with neither vendor ID nor product ID */
    124 	{ -1, -1, 0,
    125 	  { "FREECOM", "PCCARD-IDE", NULL, NULL},
    126 	  "FREECOM PCCARD-IDE"
    127 	},
    128 
    129 	{ 0, 0, 0, { NULL, NULL, NULL, NULL}, NULL }
    130 };
    131 
    132 struct wdc_pcmcia_product *
    133 	wdc_pcmcia_lookup __P((struct pcmcia_attach_args *));
    134 
    135 int	wdc_pcmcia_enable __P((void *, int));
    136 
    137 
    138 
    139 struct wdc_pcmcia_product *
    140 wdc_pcmcia_lookup(pa)
    141 	struct pcmcia_attach_args *pa;
    142 {
    143 	struct wdc_pcmcia_product *wpp;
    144 	int i, cis_match;
    145 
    146 	for (wpp = wdc_pcmcia_products; wpp->wpp_name != NULL; wpp++)
    147 		if ((wpp->wpp_vendor == -1 ||
    148 		     pa->manufacturer == wpp->wpp_vendor) &&
    149 		    (wpp->wpp_product == -1 ||
    150 		     pa->product == wpp->wpp_product)) {
    151 			cis_match = 1;
    152 			for (i = 0; i < 4; i++) {
    153 				if (!(wpp->wpp_cis_info[i] == NULL ||
    154 				      (pa->card->cis1_info[i] != NULL &&
    155 				       strcmp(pa->card->cis1_info[i],
    156 					      wpp->wpp_cis_info[i]) == 0)))
    157 					cis_match = 0;
    158 			}
    159 			if (cis_match)
    160 				return (wpp);
    161 		}
    162 
    163 	return (NULL);
    164 }
    165 
    166 static int
    167 wdc_pcmcia_match(parent, match, aux)
    168 	struct device *parent;
    169 	struct cfdata *match;
    170 	void *aux;
    171 {
    172 	struct pcmcia_attach_args *pa = aux;
    173 
    174 	if (pa->pf->function == PCMCIA_FUNCTION_DISK &&
    175 	    pa->pf->pf_funce_disk_interface == PCMCIA_TPLFE_DDI_PCCARD_ATA) {
    176 		return 10;
    177 	}
    178 
    179 	if (wdc_pcmcia_lookup(pa) != NULL)
    180 		return (1);
    181 
    182 	return (0);
    183 }
    184 
    185 static void
    186 wdc_pcmcia_attach(parent, self, aux)
    187 	struct device *parent;
    188 	struct device *self;
    189 	void *aux;
    190 {
    191 	struct wdc_pcmcia_softc *sc = (void *)self;
    192 	struct pcmcia_attach_args *pa = aux;
    193 	struct pcmcia_config_entry *cfe;
    194 	struct wdc_pcmcia_product *wpp;
    195 	int quirks;
    196 
    197 	sc->sc_pf = pa->pf;
    198 
    199 	for (cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head); cfe != NULL;
    200 	    cfe = SIMPLEQ_NEXT(cfe, cfe_list)) {
    201 		if (cfe->num_iospace != 1 && cfe->num_iospace != 2)
    202 			continue;
    203 
    204 		if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
    205 		    cfe->iospace[0].length,
    206 		    cfe->iospace[0].start == 0 ? cfe->iospace[0].length : 0,
    207 		    &sc->sc_pioh))
    208 			continue;
    209 
    210 		if (cfe->num_iospace == 2) {
    211 			if (!pcmcia_io_alloc(pa->pf, cfe->iospace[1].start,
    212 			    cfe->iospace[1].length, 0, &sc->sc_auxpioh))
    213 				break;
    214 		} else /* num_iospace == 1 */ {
    215 			sc->sc_auxpioh.iot = sc->sc_pioh.iot;
    216 			if (!bus_space_subregion(sc->sc_pioh.iot,
    217 			    sc->sc_pioh.ioh, WDC_PCMCIA_AUXREG_OFFSET,
    218 			    WDC_PCMCIA_AUXREG_NPORTS, &sc->sc_auxpioh.ioh))
    219 				break;
    220 		}
    221 		pcmcia_io_free(pa->pf, &sc->sc_pioh);
    222 	}
    223 
    224 	if (cfe == NULL) {
    225 		printf(": can't handle card info\n");
    226 		goto no_config_entry;
    227 	}
    228 
    229 	/* Enable the card. */
    230 	pcmcia_function_init(pa->pf, cfe);
    231 	if (pcmcia_function_enable(pa->pf)) {
    232 		printf(": function enable failed\n");
    233 		goto enable_failed;
    234 	}
    235 
    236 	wpp = wdc_pcmcia_lookup(pa);
    237 	if (wpp != NULL)
    238 		quirks = wpp->wpp_quirk_flag;
    239 	else
    240 		quirks = 0;
    241 
    242 	if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, 0,
    243 	    sc->sc_pioh.size, &sc->sc_pioh, &sc->sc_iowindow)) {
    244 		printf(": can't map first I/O space\n");
    245 		goto iomap_failed;
    246 	}
    247 
    248 	if (cfe->num_iospace <= 1)
    249 		sc->sc_auxiowindow = -1;
    250 	else if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, 0,
    251 	    sc->sc_auxpioh.size, &sc->sc_auxpioh, &sc->sc_auxiowindow)) {
    252 		printf(": can't map second I/O space\n");
    253 		goto iomapaux_failed;
    254 	}
    255 
    256 	if ((wpp != NULL) && (wpp->wpp_name != NULL))
    257 		printf(": %s", wpp->wpp_name);
    258 
    259 	printf("\n");
    260 
    261 	sc->wdc_channel.cmd_iot = sc->sc_pioh.iot;
    262 	sc->wdc_channel.cmd_ioh = sc->sc_pioh.ioh;
    263 	sc->wdc_channel.ctl_iot = sc->sc_auxpioh.iot;
    264 	sc->wdc_channel.ctl_ioh = sc->sc_auxpioh.ioh;
    265 	sc->wdc_channel.data32iot = sc->wdc_channel.cmd_iot;
    266 	sc->wdc_channel.data32ioh = sc->wdc_channel.cmd_ioh;
    267 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32;
    268 	sc->sc_wdcdev.PIO_cap = 0;
    269 	sc->wdc_chanptr = &sc->wdc_channel;
    270 	sc->sc_wdcdev.channels = &sc->wdc_chanptr;
    271 	sc->sc_wdcdev.nchannels = 1;
    272 	sc->wdc_channel.channel = 0;
    273 	sc->wdc_channel.wdc = &sc->sc_wdcdev;
    274 	sc->wdc_channel.ch_queue = malloc(sizeof(struct channel_queue),
    275 	    M_DEVBUF, M_NOWAIT);
    276 	if (sc->wdc_channel.ch_queue == NULL) {
    277 		printf("%s: can't allocate memory for command queue\n",
    278 		    sc->sc_wdcdev.sc_dev.dv_xname);
    279 		goto ch_queue_alloc_failed;
    280 	}
    281 	if (quirks & WDC_PCMCIA_NO_EXTRA_RESETS)
    282 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_NO_EXTRA_RESETS;
    283 
    284 	/* We can enable and disable the controller. */
    285 	sc->sc_wdcdev.sc_atapi_adapter._generic.scsipi_enable =
    286 	    wdc_pcmcia_enable;
    287 
    288 	sc->sc_flags |= WDC_PCMCIA_ATTACH;
    289 	wdcattach(&sc->wdc_channel);	/* should return an error XXX */
    290 	sc->sc_flags &= ~WDC_PCMCIA_ATTACH;
    291 	return;
    292 
    293  ch_queue_alloc_failed:
    294 	/* Unmap our aux i/o window. */
    295 	if (sc->sc_auxiowindow != -1)
    296 		pcmcia_io_unmap(sc->sc_pf, sc->sc_auxiowindow);
    297 
    298  iomapaux_failed:
    299 	/* Unmap our i/o window. */
    300 	pcmcia_io_unmap(sc->sc_pf, sc->sc_iowindow);
    301 
    302  iomap_failed:
    303 	/* Disable the function */
    304 	pcmcia_function_disable(sc->sc_pf);
    305 
    306  enable_failed:
    307 	/* Unmap our i/o space. */
    308 	pcmcia_io_free(sc->sc_pf, &sc->sc_pioh);
    309 	if (cfe->num_iospace == 2)
    310 		pcmcia_io_free(sc->sc_pf, &sc->sc_auxpioh);
    311 
    312  no_config_entry:
    313 	sc->sc_iowindow = -1;
    314 }
    315 
    316 int
    317 wdc_pcmcia_detach(self, flags)
    318 	struct device *self;
    319 	int flags;
    320 {
    321 	struct wdc_pcmcia_softc *sc = (struct wdc_pcmcia_softc *)self;
    322 	int error;
    323 
    324 	if (sc->sc_iowindow == -1)
    325 		/* Nothing to detach */
    326 		return (0);
    327 
    328 	if ((error = wdcdetach(self, flags)) != 0)
    329 		return (error);
    330 
    331 	if (sc->wdc_channel.ch_queue != NULL)
    332 		free(sc->wdc_channel.ch_queue, M_DEVBUF);
    333 
    334 	/* Unmap our i/o window and i/o space. */
    335 	pcmcia_io_unmap(sc->sc_pf, sc->sc_iowindow);
    336 	pcmcia_io_free(sc->sc_pf, &sc->sc_pioh);
    337 	if (sc->sc_auxiowindow != -1) {
    338 		pcmcia_io_unmap(sc->sc_pf, sc->sc_auxiowindow);
    339 		pcmcia_io_free(sc->sc_pf, &sc->sc_auxpioh);
    340 	}
    341 
    342 	return (0);
    343 }
    344 
    345 int
    346 wdc_pcmcia_enable(arg, onoff)
    347 	void *arg;
    348 	int onoff;
    349 {
    350 	struct wdc_pcmcia_softc *sc = arg;
    351 
    352 	if (onoff) {
    353 		/* See the comment in aic_pcmcia_enable */
    354 		if ((sc->sc_flags & WDC_PCMCIA_ATTACH) == 0) {
    355 			/* Establish the interrupt handler. */
    356 			sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
    357 			    wdcintr, &sc->wdc_channel);
    358 			if (sc->sc_ih == NULL) {
    359 				printf("%s: "
    360 				    "couldn't establish interrupt handler\n",
    361 				    sc->sc_wdcdev.sc_dev.dv_xname);
    362 				return (EIO);
    363 			}
    364 
    365 			if (pcmcia_function_enable(sc->sc_pf)) {
    366 				printf("%s: couldn't enable PCMCIA function\n",
    367 				    sc->sc_wdcdev.sc_dev.dv_xname);
    368 				pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    369 				return (EIO);
    370 			}
    371 			wdcreset(&sc->wdc_channel, VERBOSE);
    372 		}
    373 	} else {
    374 		pcmcia_function_disable(sc->sc_pf);
    375 		if ((sc->sc_flags & WDC_PCMCIA_ATTACH) == 0)
    376 			pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    377 	}
    378 
    379 	return (0);
    380 }
    381