Home | History | Annotate | Line # | Download | only in pcmcia
wdc_pcmcia.c revision 1.36
      1 /*	$NetBSD: wdc_pcmcia.c,v 1.36 2000/12/18 17:21:11 abs 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, neither 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, neither vendor ID nor product ID */
    124 	{ -1, -1, 0,
    125 	  { "FREECOM", "PCCARD-IDE", NULL, NULL},
    126 	  "FREECOM PCCARD-IDE"
    127 	},
    128 
    129 	/* Random CD-ROM, (badged AMACOM), neither vendor ID nor product ID */
    130 	{ -1, -1, 0,
    131 	  { "PCMCIA", "CD-ROM", NULL, NULL},
    132 	  "PCMCIA CD-ROM"
    133 	},
    134 
    135 	{ 0, 0, 0, { NULL, NULL, NULL, NULL}, NULL }
    136 };
    137 
    138 struct wdc_pcmcia_product *
    139 	wdc_pcmcia_lookup __P((struct pcmcia_attach_args *));
    140 
    141 int	wdc_pcmcia_enable __P((void *, int));
    142 
    143 
    144 
    145 struct wdc_pcmcia_product *
    146 wdc_pcmcia_lookup(pa)
    147 	struct pcmcia_attach_args *pa;
    148 {
    149 	struct wdc_pcmcia_product *wpp;
    150 	int i, cis_match;
    151 
    152 	for (wpp = wdc_pcmcia_products; wpp->wpp_name != NULL; wpp++)
    153 		if ((wpp->wpp_vendor == -1 ||
    154 		     pa->manufacturer == wpp->wpp_vendor) &&
    155 		    (wpp->wpp_product == -1 ||
    156 		     pa->product == wpp->wpp_product)) {
    157 			cis_match = 1;
    158 			for (i = 0; i < 4; i++) {
    159 				if (!(wpp->wpp_cis_info[i] == NULL ||
    160 				      (pa->card->cis1_info[i] != NULL &&
    161 				       strcmp(pa->card->cis1_info[i],
    162 					      wpp->wpp_cis_info[i]) == 0)))
    163 					cis_match = 0;
    164 			}
    165 			if (cis_match)
    166 				return (wpp);
    167 		}
    168 
    169 	return (NULL);
    170 }
    171 
    172 static int
    173 wdc_pcmcia_match(parent, match, aux)
    174 	struct device *parent;
    175 	struct cfdata *match;
    176 	void *aux;
    177 {
    178 	struct pcmcia_attach_args *pa = aux;
    179 
    180 	if (pa->pf->function == PCMCIA_FUNCTION_DISK &&
    181 	    pa->pf->pf_funce_disk_interface == PCMCIA_TPLFE_DDI_PCCARD_ATA) {
    182 		return 10;
    183 	}
    184 
    185 	if (wdc_pcmcia_lookup(pa) != NULL)
    186 		return (1);
    187 
    188 	return (0);
    189 }
    190 
    191 static void
    192 wdc_pcmcia_attach(parent, self, aux)
    193 	struct device *parent;
    194 	struct device *self;
    195 	void *aux;
    196 {
    197 	struct wdc_pcmcia_softc *sc = (void *)self;
    198 	struct pcmcia_attach_args *pa = aux;
    199 	struct pcmcia_config_entry *cfe;
    200 	struct wdc_pcmcia_product *wpp;
    201 	int quirks;
    202 
    203 	sc->sc_pf = pa->pf;
    204 
    205 	for (cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head); cfe != NULL;
    206 	    cfe = SIMPLEQ_NEXT(cfe, cfe_list)) {
    207 		if (cfe->num_iospace != 1 && cfe->num_iospace != 2)
    208 			continue;
    209 
    210 		if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
    211 		    cfe->iospace[0].length,
    212 		    cfe->iospace[0].start == 0 ? cfe->iospace[0].length : 0,
    213 		    &sc->sc_pioh))
    214 			continue;
    215 
    216 		if (cfe->num_iospace == 2) {
    217 			if (!pcmcia_io_alloc(pa->pf, cfe->iospace[1].start,
    218 			    cfe->iospace[1].length, 0, &sc->sc_auxpioh))
    219 				break;
    220 		} else /* num_iospace == 1 */ {
    221 			sc->sc_auxpioh.iot = sc->sc_pioh.iot;
    222 			if (!bus_space_subregion(sc->sc_pioh.iot,
    223 			    sc->sc_pioh.ioh, WDC_PCMCIA_AUXREG_OFFSET,
    224 			    WDC_PCMCIA_AUXREG_NPORTS, &sc->sc_auxpioh.ioh))
    225 				break;
    226 		}
    227 		pcmcia_io_free(pa->pf, &sc->sc_pioh);
    228 	}
    229 
    230 	if (cfe == NULL) {
    231 		printf(": can't handle card info\n");
    232 		goto no_config_entry;
    233 	}
    234 
    235 	/* Enable the card. */
    236 	pcmcia_function_init(pa->pf, cfe);
    237 	if (pcmcia_function_enable(pa->pf)) {
    238 		printf(": function enable failed\n");
    239 		goto enable_failed;
    240 	}
    241 
    242 	wpp = wdc_pcmcia_lookup(pa);
    243 	if (wpp != NULL)
    244 		quirks = wpp->wpp_quirk_flag;
    245 	else
    246 		quirks = 0;
    247 
    248 	if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, 0,
    249 	    sc->sc_pioh.size, &sc->sc_pioh, &sc->sc_iowindow)) {
    250 		printf(": can't map first I/O space\n");
    251 		goto iomap_failed;
    252 	}
    253 
    254 	if (cfe->num_iospace <= 1)
    255 		sc->sc_auxiowindow = -1;
    256 	else if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, 0,
    257 	    sc->sc_auxpioh.size, &sc->sc_auxpioh, &sc->sc_auxiowindow)) {
    258 		printf(": can't map second I/O space\n");
    259 		goto iomapaux_failed;
    260 	}
    261 
    262 	if ((wpp != NULL) && (wpp->wpp_name != NULL))
    263 		printf(": %s", wpp->wpp_name);
    264 
    265 	printf("\n");
    266 
    267 	sc->wdc_channel.cmd_iot = sc->sc_pioh.iot;
    268 	sc->wdc_channel.cmd_ioh = sc->sc_pioh.ioh;
    269 	sc->wdc_channel.ctl_iot = sc->sc_auxpioh.iot;
    270 	sc->wdc_channel.ctl_ioh = sc->sc_auxpioh.ioh;
    271 	sc->wdc_channel.data32iot = sc->wdc_channel.cmd_iot;
    272 	sc->wdc_channel.data32ioh = sc->wdc_channel.cmd_ioh;
    273 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32;
    274 	sc->sc_wdcdev.PIO_cap = 0;
    275 	sc->wdc_chanptr = &sc->wdc_channel;
    276 	sc->sc_wdcdev.channels = &sc->wdc_chanptr;
    277 	sc->sc_wdcdev.nchannels = 1;
    278 	sc->wdc_channel.channel = 0;
    279 	sc->wdc_channel.wdc = &sc->sc_wdcdev;
    280 	sc->wdc_channel.ch_queue = malloc(sizeof(struct channel_queue),
    281 	    M_DEVBUF, M_NOWAIT);
    282 	if (sc->wdc_channel.ch_queue == NULL) {
    283 		printf("%s: can't allocate memory for command queue\n",
    284 		    sc->sc_wdcdev.sc_dev.dv_xname);
    285 		goto ch_queue_alloc_failed;
    286 	}
    287 	if (quirks & WDC_PCMCIA_NO_EXTRA_RESETS)
    288 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_NO_EXTRA_RESETS;
    289 
    290 	/* We can enable and disable the controller. */
    291 	sc->sc_wdcdev.sc_atapi_adapter._generic.scsipi_enable =
    292 	    wdc_pcmcia_enable;
    293 
    294 	sc->sc_flags |= WDC_PCMCIA_ATTACH;
    295 	wdcattach(&sc->wdc_channel);	/* should return an error XXX */
    296 	sc->sc_flags &= ~WDC_PCMCIA_ATTACH;
    297 	return;
    298 
    299  ch_queue_alloc_failed:
    300 	/* Unmap our aux i/o window. */
    301 	if (sc->sc_auxiowindow != -1)
    302 		pcmcia_io_unmap(sc->sc_pf, sc->sc_auxiowindow);
    303 
    304  iomapaux_failed:
    305 	/* Unmap our i/o window. */
    306 	pcmcia_io_unmap(sc->sc_pf, sc->sc_iowindow);
    307 
    308  iomap_failed:
    309 	/* Disable the function */
    310 	pcmcia_function_disable(sc->sc_pf);
    311 
    312  enable_failed:
    313 	/* Unmap our i/o space. */
    314 	pcmcia_io_free(sc->sc_pf, &sc->sc_pioh);
    315 	if (cfe->num_iospace == 2)
    316 		pcmcia_io_free(sc->sc_pf, &sc->sc_auxpioh);
    317 
    318  no_config_entry:
    319 	sc->sc_iowindow = -1;
    320 }
    321 
    322 int
    323 wdc_pcmcia_detach(self, flags)
    324 	struct device *self;
    325 	int flags;
    326 {
    327 	struct wdc_pcmcia_softc *sc = (struct wdc_pcmcia_softc *)self;
    328 	int error;
    329 
    330 	if (sc->sc_iowindow == -1)
    331 		/* Nothing to detach */
    332 		return (0);
    333 
    334 	if ((error = wdcdetach(self, flags)) != 0)
    335 		return (error);
    336 
    337 	if (sc->wdc_channel.ch_queue != NULL)
    338 		free(sc->wdc_channel.ch_queue, M_DEVBUF);
    339 
    340 	/* Unmap our i/o window and i/o space. */
    341 	pcmcia_io_unmap(sc->sc_pf, sc->sc_iowindow);
    342 	pcmcia_io_free(sc->sc_pf, &sc->sc_pioh);
    343 	if (sc->sc_auxiowindow != -1) {
    344 		pcmcia_io_unmap(sc->sc_pf, sc->sc_auxiowindow);
    345 		pcmcia_io_free(sc->sc_pf, &sc->sc_auxpioh);
    346 	}
    347 
    348 	return (0);
    349 }
    350 
    351 int
    352 wdc_pcmcia_enable(arg, onoff)
    353 	void *arg;
    354 	int onoff;
    355 {
    356 	struct wdc_pcmcia_softc *sc = arg;
    357 
    358 	if (onoff) {
    359 		/* See the comment in aic_pcmcia_enable */
    360 		if ((sc->sc_flags & WDC_PCMCIA_ATTACH) == 0) {
    361 			/* Establish the interrupt handler. */
    362 			sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
    363 			    wdcintr, &sc->wdc_channel);
    364 			if (sc->sc_ih == NULL) {
    365 				printf("%s: "
    366 				    "couldn't establish interrupt handler\n",
    367 				    sc->sc_wdcdev.sc_dev.dv_xname);
    368 				return (EIO);
    369 			}
    370 
    371 			if (pcmcia_function_enable(sc->sc_pf)) {
    372 				printf("%s: couldn't enable PCMCIA function\n",
    373 				    sc->sc_wdcdev.sc_dev.dv_xname);
    374 				pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    375 				return (EIO);
    376 			}
    377 			wdcreset(&sc->wdc_channel, VERBOSE);
    378 		}
    379 	} else {
    380 		pcmcia_function_disable(sc->sc_pf);
    381 		if ((sc->sc_flags & WDC_PCMCIA_ATTACH) == 0)
    382 			pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    383 	}
    384 
    385 	return (0);
    386 }
    387