Home | History | Annotate | Line # | Download | only in pcmcia
wdc_pcmcia.c revision 1.26
      1 /*	$NetBSD: wdc_pcmcia.c,v 1.26 2000/01/08 07:34:49 augustss 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_FORCE_16BIT_IO	0x01 /* Don't use PCMCIA_WIDTH_AUTO */
    100 #define WDC_PCMCIA_NO_EXTRA_RESETS	0x02 /* Only reset ctrl once */
    101 	const char	*wpp_cis_info[4];	/* XXX necessary? */
    102 	const char	*wpp_name;	/* product name */
    103 } wdc_pcmcia_products[] = {
    104 
    105 	{ /* PCMCIA_VENDOR_DIGITAL XXX */ 0x0100,
    106 	  PCMCIA_PRODUCT_DIGITAL_MOBILE_MEDIA_CDROM,
    107 	  0, { NULL, "Digital Mobile Media CD-ROM", NULL, NULL },
    108 	  PCMCIA_STR_DIGITAL_MOBILE_MEDIA_CDROM },
    109 
    110 	{ PCMCIA_VENDOR_IBM,
    111 	  PCMCIA_PRODUCT_IBM_PORTABLE_CDROM,
    112 	  0, { NULL, "PCMCIA Portable CD-ROM Drive", NULL, NULL },
    113 	  PCMCIA_STR_IBM_PORTABLE_CDROM },
    114 
    115 	{ PCMCIA_VENDOR_HAGIWARASYSCOM,
    116 	  -1,			/* XXX */
    117 	  WDC_PCMCIA_FORCE_16BIT_IO,
    118 	  { NULL, NULL, NULL, NULL },
    119 	  "Hagiwara SYS-COM CompactFlash Card" },
    120 
    121 	/* The TEAC IDE/Card II is used on the Sony Vaio */
    122 	{ PCMCIA_VENDOR_TEAC,
    123 	  PCMCIA_PRODUCT_TEAC_IDECARDII,
    124 	  WDC_PCMCIA_NO_EXTRA_RESETS,
    125 	  PCMCIA_CIS_TEAC_IDECARDII,
    126 	  PCMCIA_STR_TEAC_IDECARDII },
    127 
    128 	/* EXP IDE/ATAPI DVD Card use with some DVD players.  Does not have a vendor ID or product ID */
    129 	{ -1,
    130 	  -1,
    131 	  0,
    132 	  PCMCIA_CIS_EXP_EXPMULTIMEDIA,
    133 	  PCMCIA_STR_EXP_EXPMULTIMEDIA },
    134 
    135 	/* Mobile Dock 2, which doesn't have vendor ID nor product ID */
    136 	{ -1, -1, 0,
    137 	  { "SHUTTLE TECHNOLOGY LTD.", "PCCARD-IDE/ATAPI Adapter", NULL, NULL},
    138 	  "SHUTTLE TECHNOLOGY IDE/ATAPI Adapter"
    139 	},
    140 
    141 	{ 0, 0, 0, { NULL, NULL, NULL, NULL}, NULL }
    142 };
    143 
    144 struct wdc_pcmcia_disk_device_interface_args {
    145 	int ddi_type;		/* interface type */
    146 	int ddi_reqfn;		/* function we are requesting iftype */
    147 	int ddi_curfn;		/* function we are currently parsing in CIS */
    148 };
    149 
    150 int	wdc_pcmcia_disk_device_interface_callback __P((struct pcmcia_tuple *,
    151 	    void *));
    152 int	wdc_pcmcia_disk_device_interface __P((struct pcmcia_function *));
    153 struct wdc_pcmcia_product *
    154 	wdc_pcmcia_lookup __P((struct pcmcia_attach_args *));
    155 
    156 int	wdc_pcmcia_enable __P((void *, int));
    157 
    158 int
    159 wdc_pcmcia_disk_device_interface_callback(tuple, arg)
    160 	struct pcmcia_tuple *tuple;
    161 	void *arg;
    162 {
    163 	struct wdc_pcmcia_disk_device_interface_args *ddi = arg;
    164 
    165 	switch (tuple->code) {
    166 	case PCMCIA_CISTPL_FUNCID:
    167 		ddi->ddi_curfn++;
    168 		break;
    169 
    170 	case PCMCIA_CISTPL_FUNCE:
    171 		if (ddi->ddi_reqfn != ddi->ddi_curfn)
    172 			break;
    173 
    174 		/* subcode (disk device interface), data (interface type) */
    175 		if (tuple->length < 2)
    176 			break;
    177 
    178 		/* check type */
    179 		if (pcmcia_tuple_read_1(tuple, 0) !=
    180 		    PCMCIA_TPLFE_TYPE_DISK_DEVICE_INTERFACE)
    181 			break;
    182 
    183 		ddi->ddi_type = pcmcia_tuple_read_1(tuple, 1);
    184 		return (1);
    185 	}
    186 	return (0);
    187 }
    188 
    189 int
    190 wdc_pcmcia_disk_device_interface(pf)
    191 	struct pcmcia_function *pf;
    192 {
    193 	struct wdc_pcmcia_disk_device_interface_args ddi;
    194 
    195 	ddi.ddi_reqfn = pf->number;
    196 	ddi.ddi_curfn = -1;
    197 	if (pcmcia_scan_cis((struct device *)pf->sc,
    198 	    wdc_pcmcia_disk_device_interface_callback, &ddi) > 0)
    199 		return (ddi.ddi_type);
    200 	else
    201 		return (-1);
    202 }
    203 
    204 struct wdc_pcmcia_product *
    205 wdc_pcmcia_lookup(pa)
    206 	struct pcmcia_attach_args *pa;
    207 {
    208 	struct wdc_pcmcia_product *wpp;
    209 	int i, cis_match;
    210 
    211 	for (wpp = wdc_pcmcia_products; wpp->wpp_name != NULL; wpp++)
    212 		if ((wpp->wpp_vendor == -1 ||
    213 		     pa->manufacturer == wpp->wpp_vendor) &&
    214 		    (wpp->wpp_product == -1 ||
    215 		     pa->product == wpp->wpp_product)) {
    216 			cis_match = 1;
    217 			for (i = 0; i < 4; i++) {
    218 				if (!(wpp->wpp_cis_info[i] == NULL ||
    219 				      (pa->card->cis1_info[i] != NULL &&
    220 				       strcmp(pa->card->cis1_info[i],
    221 					      wpp->wpp_cis_info[i]) == 0)))
    222 					cis_match = 0;
    223 			}
    224 			if (cis_match)
    225 				return (wpp);
    226 		}
    227 
    228 	return (NULL);
    229 }
    230 
    231 static int
    232 wdc_pcmcia_match(parent, match, aux)
    233 	struct device *parent;
    234 	struct cfdata *match;
    235 	void *aux;
    236 {
    237 	struct pcmcia_attach_args *pa = aux;
    238 	struct pcmcia_softc *sc;
    239 	int iftype;
    240 
    241 	if (wdc_pcmcia_lookup(pa) != NULL)
    242 		return (1);
    243 
    244 	if (pa->pf->function == PCMCIA_FUNCTION_DISK) {
    245 		sc = pa->pf->sc;
    246 
    247 		pcmcia_chip_socket_enable(sc->pct, sc->pch);
    248 		iftype = wdc_pcmcia_disk_device_interface(pa->pf);
    249 		pcmcia_chip_socket_disable(sc->pct, sc->pch);
    250 
    251 		if (iftype == PCMCIA_TPLFE_DDI_PCCARD_ATA)
    252 			return (1);
    253 	}
    254 
    255 	return (0);
    256 }
    257 
    258 static void
    259 wdc_pcmcia_attach(parent, self, aux)
    260 	struct device *parent;
    261 	struct device *self;
    262 	void *aux;
    263 {
    264 	struct wdc_pcmcia_softc *sc = (void *)self;
    265 	struct pcmcia_attach_args *pa = aux;
    266 	struct pcmcia_config_entry *cfe;
    267 	struct wdc_pcmcia_product *wpp;
    268 	int quirks;
    269 
    270 	sc->sc_pf = pa->pf;
    271 
    272 	for (cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head); cfe != NULL;
    273 	    cfe = SIMPLEQ_NEXT(cfe, cfe_list)) {
    274 		if (cfe->num_iospace != 1 && cfe->num_iospace != 2)
    275 			continue;
    276 
    277 		if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
    278 		    cfe->iospace[0].length,
    279 		    cfe->iospace[0].start == 0 ? cfe->iospace[0].length : 0,
    280 		    &sc->sc_pioh))
    281 			continue;
    282 
    283 		if (cfe->num_iospace == 2) {
    284 			if (!pcmcia_io_alloc(pa->pf, cfe->iospace[1].start,
    285 			    cfe->iospace[1].length, 0, &sc->sc_auxpioh))
    286 				break;
    287 		} else /* num_iospace == 1 */ {
    288 			sc->sc_auxpioh.iot = sc->sc_pioh.iot;
    289 			if (!bus_space_subregion(sc->sc_pioh.iot,
    290 			    sc->sc_pioh.ioh, WDC_PCMCIA_AUXREG_OFFSET,
    291 			    WDC_PCMCIA_AUXREG_NPORTS, &sc->sc_auxpioh.ioh))
    292 				break;
    293 		}
    294 		pcmcia_io_free(pa->pf, &sc->sc_pioh);
    295 	}
    296 
    297 	if (cfe == NULL) {
    298 		printf(": can't handle card info\n");
    299 		return;
    300 	}
    301 
    302 	/* Enable the card. */
    303 	pcmcia_function_init(pa->pf, cfe);
    304 	if (pcmcia_function_enable(pa->pf)) {
    305 		printf(": function enable failed\n");
    306 		return;
    307 	}
    308 
    309 	/*
    310 	 * XXX  DEC Mobile Media CDROM is not yet tested whether it works
    311 	 * XXX  with PCMCIA_WIDTH_IO16.  HAGIWARA SYS-COM HPC-CF32 doesn't
    312 	 * XXX  work with PCMCIA_WIDTH_AUTO.
    313 	 * XXX  CANON FC-8M (SANDISK SDCFB 8M) works for both _AUTO and IO16.
    314 	 * XXX  So, here is temporary work around.
    315 	 */
    316 	wpp = wdc_pcmcia_lookup(pa);
    317 	if (wpp != NULL)
    318 		quirks = wpp->wpp_quirk_flag;
    319 	else
    320 		quirks = 0;
    321 
    322 	if (pcmcia_io_map(pa->pf, quirks & WDC_PCMCIA_FORCE_16BIT_IO ?
    323 	    PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_AUTO, 0,
    324 	    sc->sc_pioh.size, &sc->sc_pioh, &sc->sc_iowindow)) {
    325 		printf(": can't map first I/O space\n");
    326 		return;
    327 	}
    328 
    329 	/*
    330 	 * Currently, # of iospace is 1 except DIGITAL Mobile Media CD-ROM.
    331 	 * So whether the work around like above is necessary or not
    332 	 * is unknown.  XXX.
    333 	 */
    334 	if (cfe->num_iospace <= 1)
    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 			return;
    340 	}
    341 
    342 	if ((wpp != NULL) && (wpp->wpp_name != NULL))
    343 		printf(": %s", wpp->wpp_name);
    344 
    345 	printf("\n");
    346 
    347 	sc->wdc_channel.cmd_iot = sc->sc_pioh.iot;
    348 	sc->wdc_channel.cmd_ioh = sc->sc_pioh.ioh;
    349 	sc->wdc_channel.ctl_iot = sc->sc_auxpioh.iot;
    350 	sc->wdc_channel.ctl_ioh = sc->sc_auxpioh.ioh;
    351 	sc->wdc_channel.data32iot = sc->wdc_channel.cmd_iot;
    352 	sc->wdc_channel.data32ioh = sc->wdc_channel.cmd_ioh;
    353 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32;
    354 	sc->sc_wdcdev.PIO_cap = 0;
    355 	sc->wdc_chanptr = &sc->wdc_channel;
    356 	sc->sc_wdcdev.channels = &sc->wdc_chanptr;
    357 	sc->sc_wdcdev.nchannels = 1;
    358 	sc->wdc_channel.channel = 0;
    359 	sc->wdc_channel.wdc = &sc->sc_wdcdev;
    360 	sc->wdc_channel.ch_queue = malloc(sizeof(struct channel_queue),
    361 	    M_DEVBUF, M_NOWAIT);
    362 	if (sc->wdc_channel.ch_queue == NULL) {
    363 		printf("%s: can't allocate memory for command queue\n",
    364 		    sc->sc_wdcdev.sc_dev.dv_xname);
    365 		return;
    366 	}
    367 	if (quirks & WDC_PCMCIA_NO_EXTRA_RESETS)
    368 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_NO_EXTRA_RESETS;
    369 
    370 	/* We can enable and disable the controller. */
    371 	sc->sc_wdcdev.sc_atapi_adapter.scsipi_enable = wdc_pcmcia_enable;
    372 
    373 	sc->sc_flags |= WDC_PCMCIA_ATTACH;
    374 	wdcattach(&sc->wdc_channel);
    375 	sc->sc_flags &= ~WDC_PCMCIA_ATTACH;
    376 }
    377 
    378 int
    379 wdc_pcmcia_detach(self, flags)
    380 	struct device *self;
    381 	int flags;
    382 {
    383 	struct wdc_pcmcia_softc *sc = (struct wdc_pcmcia_softc *)self;
    384 	int error;
    385 
    386 	if ((error = wdcdetach(self, flags)) != 0)
    387 		return (error);
    388 
    389 	if (sc->wdc_channel.ch_queue != NULL)
    390 		free(sc->wdc_channel.ch_queue, M_DEVBUF);
    391 
    392 	/* Unmap our i/o window and i/o space. */
    393 	pcmcia_io_unmap(sc->sc_pf, sc->sc_iowindow);
    394 	pcmcia_io_free(sc->sc_pf, &sc->sc_pioh);
    395 	if (sc->sc_auxiowindow != -1) {
    396 		pcmcia_io_unmap(sc->sc_pf, sc->sc_auxiowindow);
    397 		pcmcia_io_free(sc->sc_pf, &sc->sc_auxpioh);
    398 	}
    399 
    400 	return (0);
    401 }
    402 
    403 int
    404 wdc_pcmcia_enable(arg, onoff)
    405 	void *arg;
    406 	int onoff;
    407 {
    408 	struct wdc_pcmcia_softc *sc = arg;
    409 
    410 	if (onoff) {
    411 		/* Establish the interrupt handler. */
    412 		sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO, wdcintr,
    413 		    &sc->wdc_channel);
    414 		if (sc->sc_ih == NULL) {
    415 			printf("%s: couldn't establish interrupt handler\n",
    416 			    sc->sc_wdcdev.sc_dev.dv_xname);
    417 			return (EIO);
    418 		}
    419 
    420 		if ((sc->sc_flags & WDC_PCMCIA_ATTACH) == 0) {
    421 			if (pcmcia_function_enable(sc->sc_pf)) {
    422 				printf("%s: couldn't enable PCMCIA function\n",
    423 				    sc->sc_wdcdev.sc_dev.dv_xname);
    424 				pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    425 				return (EIO);
    426 			}
    427 			wdcreset(&sc->wdc_channel, VERBOSE);
    428 		}
    429 	} else {
    430 		pcmcia_function_disable(sc->sc_pf);
    431 		pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    432 	}
    433 
    434 	return (0);
    435 }
    436