Home | History | Annotate | Line # | Download | only in pcmcia
wdc_pcmcia.c revision 1.28
      1 /*	$NetBSD: wdc_pcmcia.c,v 1.28 2000/02/01 06:48:15 enami 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/systm.h>
     41 #include <sys/kernel.h>
     42 #include <sys/conf.h>
     43 #include <sys/file.h>
     44 #include <sys/stat.h>
     45 #include <sys/ioctl.h>
     46 #include <sys/buf.h>
     47 #include <sys/uio.h>
     48 #include <sys/malloc.h>
     49 #include <sys/device.h>
     50 #include <sys/disklabel.h>
     51 #include <sys/disk.h>
     52 #include <sys/syslog.h>
     53 #include <sys/proc.h>
     54 
     55 #include <vm/vm.h>
     56 
     57 #include <machine/cpu.h>
     58 #include <machine/intr.h>
     59 #include <machine/bus.h>
     60 
     61 #include <dev/pcmcia/pcmciareg.h>
     62 #include <dev/pcmcia/pcmciavar.h>
     63 #include <dev/pcmcia/pcmciadevs.h>
     64 
     65 #include <dev/ata/atavar.h>
     66 #include <dev/ic/wdcvar.h>
     67 
     68 #define WDC_PCMCIA_REG_NPORTS      8
     69 #define WDC_PCMCIA_AUXREG_OFFSET   (WDC_PCMCIA_REG_NPORTS + 6)
     70 #define WDC_PCMCIA_AUXREG_NPORTS   2
     71 
     72 struct wdc_pcmcia_softc {
     73 	struct wdc_softc sc_wdcdev;
     74 	struct channel_softc *wdc_chanptr;
     75 	struct channel_softc wdc_channel;
     76 	struct pcmcia_io_handle sc_pioh;
     77 	struct pcmcia_io_handle sc_auxpioh;
     78 	int sc_iowindow;
     79 	int sc_auxiowindow;
     80 	void *sc_ih;
     81 	struct pcmcia_function *sc_pf;
     82 	int sc_flags;
     83 #define	WDC_PCMCIA_ATTACH	0x0001
     84 };
     85 
     86 static int wdc_pcmcia_match	__P((struct device *, struct cfdata *, void *));
     87 static void wdc_pcmcia_attach	__P((struct device *, struct device *, void *));
     88 static int wdc_pcmcia_detach	__P((struct device *, int));
     89 
     90 struct cfattach wdc_pcmcia_ca = {
     91 	sizeof(struct wdc_pcmcia_softc), wdc_pcmcia_match, wdc_pcmcia_attach,
     92 	wdc_pcmcia_detach, wdcactivate
     93 };
     94 
     95 struct wdc_pcmcia_product {
     96 	u_int32_t	wpp_vendor;	/* vendor ID */
     97 	u_int32_t	wpp_product;	/* product ID */
     98 	int		wpp_quirk_flag;	/* Quirk flags */
     99 #define WDC_PCMCIA_NO_EXTRA_RESETS	0x02 /* Only reset ctrl once */
    100 	const char	*wpp_cis_info[4];	/* XXX necessary? */
    101 	const char	*wpp_name;	/* product name */
    102 } wdc_pcmcia_products[] = {
    103 
    104 	{ /* PCMCIA_VENDOR_DIGITAL XXX */ 0x0100,
    105 	  PCMCIA_PRODUCT_DIGITAL_MOBILE_MEDIA_CDROM,
    106 	  0, { NULL, "Digital Mobile Media CD-ROM", NULL, NULL },
    107 	  PCMCIA_STR_DIGITAL_MOBILE_MEDIA_CDROM },
    108 
    109 	{ PCMCIA_VENDOR_IBM,
    110 	  PCMCIA_PRODUCT_IBM_PORTABLE_CDROM,
    111 	  0, { NULL, "PCMCIA Portable CD-ROM Drive", NULL, NULL },
    112 	  PCMCIA_STR_IBM_PORTABLE_CDROM },
    113 
    114 	/* The TEAC IDE/Card II is used on the Sony Vaio */
    115 	{ PCMCIA_VENDOR_TEAC,
    116 	  PCMCIA_PRODUCT_TEAC_IDECARDII,
    117 	  WDC_PCMCIA_NO_EXTRA_RESETS,
    118 	  PCMCIA_CIS_TEAC_IDECARDII,
    119 	  PCMCIA_STR_TEAC_IDECARDII },
    120 
    121 	/*
    122 	 * EXP IDE/ATAPI DVD Card use with some DVD players.
    123 	 * Does not have a vendor ID or product ID.
    124 	 */
    125 	{ -1,
    126 	  -1,
    127 	  0,
    128 	  PCMCIA_CIS_EXP_EXPMULTIMEDIA,
    129 	  PCMCIA_STR_EXP_EXPMULTIMEDIA },
    130 
    131 	/* Mobile Dock 2, which doesn't have vendor ID nor product ID */
    132 	{ -1, -1, 0,
    133 	  { "SHUTTLE TECHNOLOGY LTD.", "PCCARD-IDE/ATAPI Adapter", NULL, NULL},
    134 	  "SHUTTLE TECHNOLOGY IDE/ATAPI Adapter"
    135 	},
    136 
    137 	{ 0, 0, 0, { NULL, NULL, NULL, NULL}, NULL }
    138 };
    139 
    140 struct wdc_pcmcia_disk_device_interface_args {
    141 	int ddi_type;		/* interface type */
    142 	int ddi_reqfn;		/* function we are requesting iftype */
    143 	int ddi_curfn;		/* function we are currently parsing in CIS */
    144 };
    145 
    146 int	wdc_pcmcia_disk_device_interface_callback __P((struct pcmcia_tuple *,
    147 	    void *));
    148 int	wdc_pcmcia_disk_device_interface __P((struct pcmcia_function *));
    149 struct wdc_pcmcia_product *
    150 	wdc_pcmcia_lookup __P((struct pcmcia_attach_args *));
    151 
    152 int	wdc_pcmcia_enable __P((void *, int));
    153 
    154 int
    155 wdc_pcmcia_disk_device_interface_callback(tuple, arg)
    156 	struct pcmcia_tuple *tuple;
    157 	void *arg;
    158 {
    159 	struct wdc_pcmcia_disk_device_interface_args *ddi = arg;
    160 
    161 	switch (tuple->code) {
    162 	case PCMCIA_CISTPL_FUNCID:
    163 		ddi->ddi_curfn++;
    164 		break;
    165 
    166 	case PCMCIA_CISTPL_FUNCE:
    167 		if (ddi->ddi_reqfn != ddi->ddi_curfn)
    168 			break;
    169 
    170 		/* subcode (disk device interface), data (interface type) */
    171 		if (tuple->length < 2)
    172 			break;
    173 
    174 		/* check type */
    175 		if (pcmcia_tuple_read_1(tuple, 0) !=
    176 		    PCMCIA_TPLFE_TYPE_DISK_DEVICE_INTERFACE)
    177 			break;
    178 
    179 		ddi->ddi_type = pcmcia_tuple_read_1(tuple, 1);
    180 		return (1);
    181 	}
    182 	return (0);
    183 }
    184 
    185 int
    186 wdc_pcmcia_disk_device_interface(pf)
    187 	struct pcmcia_function *pf;
    188 {
    189 	struct wdc_pcmcia_disk_device_interface_args ddi;
    190 
    191 	ddi.ddi_reqfn = pf->number;
    192 	ddi.ddi_curfn = -1;
    193 	if (pcmcia_scan_cis((struct device *)pf->sc,
    194 	    wdc_pcmcia_disk_device_interface_callback, &ddi) > 0)
    195 		return (ddi.ddi_type);
    196 	else
    197 		return (-1);
    198 }
    199 
    200 struct wdc_pcmcia_product *
    201 wdc_pcmcia_lookup(pa)
    202 	struct pcmcia_attach_args *pa;
    203 {
    204 	struct wdc_pcmcia_product *wpp;
    205 	int i, cis_match;
    206 
    207 	for (wpp = wdc_pcmcia_products; wpp->wpp_name != NULL; wpp++)
    208 		if ((wpp->wpp_vendor == -1 ||
    209 		     pa->manufacturer == wpp->wpp_vendor) &&
    210 		    (wpp->wpp_product == -1 ||
    211 		     pa->product == wpp->wpp_product)) {
    212 			cis_match = 1;
    213 			for (i = 0; i < 4; i++) {
    214 				if (!(wpp->wpp_cis_info[i] == NULL ||
    215 				      (pa->card->cis1_info[i] != NULL &&
    216 				       strcmp(pa->card->cis1_info[i],
    217 					      wpp->wpp_cis_info[i]) == 0)))
    218 					cis_match = 0;
    219 			}
    220 			if (cis_match)
    221 				return (wpp);
    222 		}
    223 
    224 	return (NULL);
    225 }
    226 
    227 static int
    228 wdc_pcmcia_match(parent, match, aux)
    229 	struct device *parent;
    230 	struct cfdata *match;
    231 	void *aux;
    232 {
    233 	struct pcmcia_attach_args *pa = aux;
    234 	struct pcmcia_softc *sc;
    235 	int iftype;
    236 
    237 	if (wdc_pcmcia_lookup(pa) != NULL)
    238 		return (1);
    239 
    240 	if (pa->pf->function == PCMCIA_FUNCTION_DISK) {
    241 		sc = pa->pf->sc;
    242 
    243 		pcmcia_chip_socket_enable(sc->pct, sc->pch);
    244 		iftype = wdc_pcmcia_disk_device_interface(pa->pf);
    245 		pcmcia_chip_socket_disable(sc->pct, sc->pch);
    246 
    247 		if (iftype == PCMCIA_TPLFE_DDI_PCCARD_ATA)
    248 			return (1);
    249 	}
    250 
    251 	return (0);
    252 }
    253 
    254 static void
    255 wdc_pcmcia_attach(parent, self, aux)
    256 	struct device *parent;
    257 	struct device *self;
    258 	void *aux;
    259 {
    260 	struct wdc_pcmcia_softc *sc = (void *)self;
    261 	struct pcmcia_attach_args *pa = aux;
    262 	struct pcmcia_config_entry *cfe;
    263 	struct wdc_pcmcia_product *wpp;
    264 	int quirks;
    265 
    266 	sc->sc_pf = pa->pf;
    267 
    268 	for (cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head); cfe != NULL;
    269 	    cfe = SIMPLEQ_NEXT(cfe, cfe_list)) {
    270 		if (cfe->num_iospace != 1 && cfe->num_iospace != 2)
    271 			continue;
    272 
    273 		if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
    274 		    cfe->iospace[0].length,
    275 		    cfe->iospace[0].start == 0 ? cfe->iospace[0].length : 0,
    276 		    &sc->sc_pioh))
    277 			continue;
    278 
    279 		if (cfe->num_iospace == 2) {
    280 			if (!pcmcia_io_alloc(pa->pf, cfe->iospace[1].start,
    281 			    cfe->iospace[1].length, 0, &sc->sc_auxpioh))
    282 				break;
    283 		} else /* num_iospace == 1 */ {
    284 			sc->sc_auxpioh.iot = sc->sc_pioh.iot;
    285 			if (!bus_space_subregion(sc->sc_pioh.iot,
    286 			    sc->sc_pioh.ioh, WDC_PCMCIA_AUXREG_OFFSET,
    287 			    WDC_PCMCIA_AUXREG_NPORTS, &sc->sc_auxpioh.ioh))
    288 				break;
    289 		}
    290 		pcmcia_io_free(pa->pf, &sc->sc_pioh);
    291 	}
    292 
    293 	if (cfe == NULL) {
    294 		printf(": can't handle card info\n");
    295 		return;
    296 	}
    297 
    298 	/* Enable the card. */
    299 	pcmcia_function_init(pa->pf, cfe);
    300 	if (pcmcia_function_enable(pa->pf)) {
    301 		printf(": function enable failed\n");
    302 		return;
    303 	}
    304 
    305 	wpp = wdc_pcmcia_lookup(pa);
    306 	if (wpp != NULL)
    307 		quirks = wpp->wpp_quirk_flag;
    308 	else
    309 		quirks = 0;
    310 
    311 	if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, 0,
    312 	    sc->sc_pioh.size, &sc->sc_pioh, &sc->sc_iowindow)) {
    313 		printf(": can't map first I/O space\n");
    314 		return;
    315 	}
    316 
    317 	if (cfe->num_iospace <= 1)
    318 		sc->sc_auxiowindow = -1;
    319 	else if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, 0,
    320 	    sc->sc_auxpioh.size, &sc->sc_auxpioh, &sc->sc_auxiowindow)) {
    321 		printf(": can't map second I/O space\n");
    322 		return;
    323 	}
    324 
    325 	if ((wpp != NULL) && (wpp->wpp_name != NULL))
    326 		printf(": %s", wpp->wpp_name);
    327 
    328 	printf("\n");
    329 
    330 	sc->wdc_channel.cmd_iot = sc->sc_pioh.iot;
    331 	sc->wdc_channel.cmd_ioh = sc->sc_pioh.ioh;
    332 	sc->wdc_channel.ctl_iot = sc->sc_auxpioh.iot;
    333 	sc->wdc_channel.ctl_ioh = sc->sc_auxpioh.ioh;
    334 	sc->wdc_channel.data32iot = sc->wdc_channel.cmd_iot;
    335 	sc->wdc_channel.data32ioh = sc->wdc_channel.cmd_ioh;
    336 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32;
    337 	sc->sc_wdcdev.PIO_cap = 0;
    338 	sc->wdc_chanptr = &sc->wdc_channel;
    339 	sc->sc_wdcdev.channels = &sc->wdc_chanptr;
    340 	sc->sc_wdcdev.nchannels = 1;
    341 	sc->wdc_channel.channel = 0;
    342 	sc->wdc_channel.wdc = &sc->sc_wdcdev;
    343 	sc->wdc_channel.ch_queue = malloc(sizeof(struct channel_queue),
    344 	    M_DEVBUF, M_NOWAIT);
    345 	if (sc->wdc_channel.ch_queue == NULL) {
    346 		printf("%s: can't allocate memory for command queue\n",
    347 		    sc->sc_wdcdev.sc_dev.dv_xname);
    348 		return;
    349 	}
    350 	if (quirks & WDC_PCMCIA_NO_EXTRA_RESETS)
    351 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_NO_EXTRA_RESETS;
    352 
    353 	/* We can enable and disable the controller. */
    354 	sc->sc_wdcdev.sc_atapi_adapter.scsipi_enable = wdc_pcmcia_enable;
    355 
    356 	sc->sc_flags |= WDC_PCMCIA_ATTACH;
    357 	wdcattach(&sc->wdc_channel);
    358 	sc->sc_flags &= ~WDC_PCMCIA_ATTACH;
    359 }
    360 
    361 int
    362 wdc_pcmcia_detach(self, flags)
    363 	struct device *self;
    364 	int flags;
    365 {
    366 	struct wdc_pcmcia_softc *sc = (struct wdc_pcmcia_softc *)self;
    367 	int error;
    368 
    369 	if ((error = wdcdetach(self, flags)) != 0)
    370 		return (error);
    371 
    372 	if (sc->wdc_channel.ch_queue != NULL)
    373 		free(sc->wdc_channel.ch_queue, M_DEVBUF);
    374 
    375 	/* Unmap our i/o window and i/o space. */
    376 	pcmcia_io_unmap(sc->sc_pf, sc->sc_iowindow);
    377 	pcmcia_io_free(sc->sc_pf, &sc->sc_pioh);
    378 	if (sc->sc_auxiowindow != -1) {
    379 		pcmcia_io_unmap(sc->sc_pf, sc->sc_auxiowindow);
    380 		pcmcia_io_free(sc->sc_pf, &sc->sc_auxpioh);
    381 	}
    382 
    383 	return (0);
    384 }
    385 
    386 int
    387 wdc_pcmcia_enable(arg, onoff)
    388 	void *arg;
    389 	int onoff;
    390 {
    391 	struct wdc_pcmcia_softc *sc = arg;
    392 
    393 	if (onoff) {
    394 		/* Establish the interrupt handler. */
    395 		sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO, wdcintr,
    396 		    &sc->wdc_channel);
    397 		if (sc->sc_ih == NULL) {
    398 			printf("%s: couldn't establish interrupt handler\n",
    399 			    sc->sc_wdcdev.sc_dev.dv_xname);
    400 			return (EIO);
    401 		}
    402 
    403 		/* See the comment in aic_pcmcia_enable */
    404 		if ((sc->sc_flags & WDC_PCMCIA_ATTACH) == 0) {
    405 			if (pcmcia_function_enable(sc->sc_pf)) {
    406 				printf("%s: couldn't enable PCMCIA function\n",
    407 				    sc->sc_wdcdev.sc_dev.dv_xname);
    408 				pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    409 				return (EIO);
    410 			}
    411 			wdcreset(&sc->wdc_channel, VERBOSE);
    412 		}
    413 	} else {
    414 		pcmcia_function_disable(sc->sc_pf);
    415 		pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    416 	}
    417 
    418 	return (0);
    419 }
    420