Home | History | Annotate | Line # | Download | only in pcmcia
wdc_pcmcia.c revision 1.16
      1 /*	$NetBSD: wdc_pcmcia.c,v 1.16 1998/11/21 15:41:42 drochner 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/isa/isavar.h>
     66 #include <dev/ata/atavar.h>
     67 #include <dev/ic/wdcvar.h>
     68 
     69 #define WDC_PCMCIA_REG_NPORTS      8
     70 #define WDC_PCMCIA_AUXREG_OFFSET   (WDC_PCMCIA_REG_NPORTS + 6)
     71 #define WDC_PCMCIA_AUXREG_NPORTS   2
     72 
     73 struct wdc_pcmcia_softc {
     74 	struct wdc_softc sc_wdcdev;
     75 	struct channel_softc *wdc_chanptr;
     76 	struct channel_softc wdc_channel;
     77 	struct pcmcia_io_handle sc_pioh;
     78 	struct pcmcia_io_handle sc_auxpioh;
     79 	int sc_iowindow;
     80 	int sc_auxiowindow;
     81 	void *sc_ih;
     82 	struct pcmcia_function *sc_pf;
     83 };
     84 
     85 static int wdc_pcmcia_match	__P((struct device *, struct cfdata *, void *));
     86 static void wdc_pcmcia_attach	__P((struct device *, struct device *, void *));
     87 
     88 struct cfattach wdc_pcmcia_ca = {
     89 	sizeof(struct wdc_pcmcia_softc), wdc_pcmcia_match, wdc_pcmcia_attach
     90 };
     91 
     92 struct wdc_pcmcia_product {
     93 	u_int32_t	wpp_vendor;	/* vendor ID */
     94 	u_int32_t	wpp_product;	/* product ID */
     95 	int		wpp_quirk_flag;	/* Quirk flags */
     96 #define WDC_PCMCIA_FORCE_16BIT_IO	0x01 /* Don't use PCMCIA_WIDTH_AUTO */
     97 #define WDC_PCMCIA_NO_EXTRA_RESETS	0x02 /* Only reset ctrl once */
     98 	const char	*wpp_cis_info[4];	/* XXX necessary? */
     99 	const char	*wpp_name;	/* product name */
    100 } wdc_pcmcia_products[] = {
    101 
    102 	{ /* PCMCIA_VENDOR_DIGITAL XXX */ 0x0100,
    103 	  PCMCIA_PRODUCT_DIGITAL_MOBILE_MEDIA_CDROM,
    104 	  0, { NULL, "Digital Mobile Media CD-ROM", NULL, NULL },
    105 	  PCMCIA_STR_DIGITAL_MOBILE_MEDIA_CDROM },
    106 
    107 	{ PCMCIA_VENDOR_HAGIWARASYSCOM,
    108 	  -1,			/* XXX */
    109 	  WDC_PCMCIA_FORCE_16BIT_IO,
    110 	  { NULL, NULL, NULL, NULL },
    111 	  "Hagiwara SYS-COM CompactFlash Card" },
    112 
    113 	/* The TEAC IDE/Card II is used on the Sony Vaio */
    114 	{ PCMCIA_VENDOR_TEAC,
    115 	  PCMCIA_PRODUCT_TEAC_IDECARDII,
    116 	  WDC_PCMCIA_NO_EXTRA_RESETS,
    117 	  PCMCIA_CIS_TEAC_IDECARDII,
    118 	  PCMCIA_STR_TEAC_IDECARDII },
    119 
    120 	{ 0, 0, 0, { NULL, NULL, NULL, NULL}, NULL }
    121 };
    122 
    123 struct wdc_pcmcia_disk_device_interface_args {
    124 	int ddi_type;		/* interface type */
    125 	int ddi_reqfn;		/* function we are requesting iftype */
    126 	int ddi_curfn;		/* function we are currently parsing in CIS */
    127 };
    128 
    129 int	wdc_pcmcia_disk_device_interface_callback __P((struct pcmcia_tuple *,
    130 	    void *));
    131 int	wdc_pcmcia_disk_device_interface __P((struct pcmcia_function *));
    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 int
    138 wdc_pcmcia_disk_device_interface_callback(tuple, arg)
    139 	struct pcmcia_tuple *tuple;
    140 	void *arg;
    141 {
    142 	struct wdc_pcmcia_disk_device_interface_args *ddi = arg;
    143 
    144 	switch (tuple->code) {
    145 	case PCMCIA_CISTPL_FUNCID:
    146 		ddi->ddi_curfn++;
    147 		break;
    148 
    149 	case PCMCIA_CISTPL_FUNCE:
    150 		if (ddi->ddi_reqfn != ddi->ddi_curfn)
    151 			break;
    152 
    153 		/* subcode (disk device interface), data (interface type) */
    154 		if (tuple->length < 2)
    155 			break;
    156 
    157 		/* check type */
    158 		if (pcmcia_tuple_read_1(tuple, 0) !=
    159 		    PCMCIA_TPLFE_TYPE_DISK_DEVICE_INTERFACE)
    160 			break;
    161 
    162 		ddi->ddi_type = pcmcia_tuple_read_1(tuple, 1);
    163 		return (1);
    164 	}
    165 	return (0);
    166 }
    167 
    168 int
    169 wdc_pcmcia_disk_device_interface(pf)
    170 	struct pcmcia_function *pf;
    171 {
    172 	struct wdc_pcmcia_disk_device_interface_args ddi;
    173 
    174 	ddi.ddi_reqfn = pf->number;
    175 	ddi.ddi_curfn = -1;
    176 	if (pcmcia_scan_cis((struct device *)pf->sc,
    177 	    wdc_pcmcia_disk_device_interface_callback, &ddi) > 0)
    178 		return (ddi.ddi_type);
    179 	else
    180 		return (-1);
    181 }
    182 
    183 struct wdc_pcmcia_product *
    184 wdc_pcmcia_lookup(pa)
    185 	struct pcmcia_attach_args *pa;
    186 {
    187 	struct wdc_pcmcia_product *wpp;
    188 	int i, cis_match;
    189 
    190 	for (wpp = wdc_pcmcia_products; wpp->wpp_name != NULL; wpp++)
    191 		if ((wpp->wpp_vendor == -1 ||
    192 		     pa->manufacturer == wpp->wpp_vendor) &&
    193 		    (wpp->wpp_product == -1 ||
    194 		     pa->product == wpp->wpp_product)) {
    195 			cis_match = 1;
    196 			for (i = 0; i < 4; i++) {
    197 				if (!(wpp->wpp_cis_info[i] == NULL ||
    198 				      (pa->card->cis1_info[i] != NULL &&
    199 				       strcmp(pa->card->cis1_info[i],
    200 					      wpp->wpp_cis_info[i]) == 0)))
    201 					cis_match = 0;
    202 			}
    203 			if (cis_match)
    204 				return (wpp);
    205 		}
    206 
    207 	return (NULL);
    208 }
    209 
    210 static int
    211 wdc_pcmcia_match(parent, match, aux)
    212 	struct device *parent;
    213 	struct cfdata *match;
    214 	void *aux;
    215 {
    216 	struct pcmcia_attach_args *pa = aux;
    217 	struct pcmcia_softc *sc;
    218 	int iftype;
    219 
    220 	if (wdc_pcmcia_lookup(pa) != NULL)
    221 		return (1);
    222 
    223 	if (pa->pf->function == PCMCIA_FUNCTION_DISK) {
    224 		sc = pa->pf->sc;
    225 
    226 		pcmcia_chip_socket_enable(sc->pct, sc->pch);
    227 		iftype = wdc_pcmcia_disk_device_interface(pa->pf);
    228 		pcmcia_chip_socket_disable(sc->pct, sc->pch);
    229 
    230 		if (iftype == PCMCIA_TPLFE_DDI_PCCARD_ATA)
    231 			return (1);
    232 	}
    233 
    234 	return (0);
    235 }
    236 
    237 static void
    238 wdc_pcmcia_attach(parent, self, aux)
    239 	struct device *parent;
    240 	struct device *self;
    241 	void *aux;
    242 {
    243 	struct wdc_pcmcia_softc *sc = (void *)self;
    244 	struct pcmcia_attach_args *pa = aux;
    245 	struct pcmcia_config_entry *cfe;
    246 	struct wdc_pcmcia_product *wpp;
    247 	int quirks;
    248 
    249 	sc->sc_pf = pa->pf;
    250 
    251 	for (cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head); cfe != NULL;
    252 	    cfe = SIMPLEQ_NEXT(cfe, cfe_list)) {
    253 		if (cfe->num_iospace != 1 && cfe->num_iospace != 2)
    254 			continue;
    255 
    256 		if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
    257 		    cfe->iospace[0].length,
    258 		    cfe->iospace[0].start == 0 ? cfe->iospace[0].length : 0,
    259 		    &sc->sc_pioh))
    260 			continue;
    261 
    262 		if (cfe->num_iospace == 2) {
    263 			if (!pcmcia_io_alloc(pa->pf, cfe->iospace[1].start,
    264 			    cfe->iospace[1].length, 0, &sc->sc_auxpioh))
    265 				break;
    266 		} else /* num_iospace == 1 */ {
    267 			sc->sc_auxpioh.iot = sc->sc_pioh.iot;
    268 			if (!bus_space_subregion(sc->sc_pioh.iot,
    269 			    sc->sc_pioh.ioh, WDC_PCMCIA_AUXREG_OFFSET,
    270 			    WDC_PCMCIA_AUXREG_NPORTS, &sc->sc_auxpioh.ioh))
    271 				break;
    272 		}
    273 		pcmcia_io_free(pa->pf, &sc->sc_pioh);
    274 	}
    275 
    276 	if (cfe == NULL) {
    277 		printf(": can't handle card info\n");
    278 		return;
    279 	}
    280 
    281 	/* Enable the card. */
    282 	pcmcia_function_init(pa->pf, cfe);
    283 	if (pcmcia_function_enable(pa->pf)) {
    284 		printf(": function enable failed\n");
    285 		return;
    286 	}
    287 
    288 	/*
    289 	 * XXX  DEC Mobile Media CDROM is not yet tested whether it works
    290 	 * XXX  with PCMCIA_WIDTH_IO16.  HAGIWARA SYS-COM HPC-CF32 doesn't
    291 	 * XXX  work with PCMCIA_WIDTH_AUTO.
    292 	 * XXX  CANON FC-8M (SANDISK SDCFB 8M) works for both _AUTO and IO16.
    293 	 * XXX  So, here is temporary work around.
    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 (pcmcia_io_map(pa->pf, quirks & WDC_PCMCIA_FORCE_16BIT_IO ?
    302 	    PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_AUTO, 0,
    303 	    sc->sc_pioh.size, &sc->sc_pioh, &sc->sc_iowindow)) {
    304 		printf(": can't map first I/O space\n");
    305 		return;
    306 	}
    307 
    308 	/*
    309 	 * Currently, # of iospace is 1 except DIGITAL Mobile Media CD-ROM.
    310 	 * So whether the work around like above is necessary or not
    311 	 * is unknown.  XXX.
    312 	 */
    313 	if (cfe->num_iospace > 1 &&
    314 	    pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, 0,
    315 	    sc->sc_auxpioh.size, &sc->sc_auxpioh, &sc->sc_auxiowindow)) {
    316 		printf(": can't map second I/O space\n");
    317 		return;
    318 	}
    319 
    320 	printf("\n");
    321 
    322 	sc->wdc_channel.cmd_iot = sc->sc_pioh.iot;
    323 	sc->wdc_channel.cmd_ioh = sc->sc_pioh.ioh;
    324 	sc->wdc_channel.ctl_iot = sc->sc_auxpioh.iot;
    325 	sc->wdc_channel.ctl_ioh = sc->sc_auxpioh.ioh;
    326 	sc->wdc_channel.data32iot = sc->wdc_channel.cmd_iot;
    327 	sc->wdc_channel.data32ioh = sc->wdc_channel.cmd_ioh;
    328 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32;
    329 	sc->sc_wdcdev.pio_mode = 0;
    330 	sc->wdc_chanptr = &sc->wdc_channel;
    331 	sc->sc_wdcdev.channels = &sc->wdc_chanptr;
    332 	sc->sc_wdcdev.nchannels = 1;
    333 	sc->wdc_channel.channel = 0;
    334 	sc->wdc_channel.wdc = &sc->sc_wdcdev;
    335 	sc->wdc_channel.ch_queue = malloc(sizeof(struct channel_queue),
    336 	    M_DEVBUF, M_NOWAIT);
    337 	if (sc->wdc_channel.ch_queue == NULL) {
    338 		printf("%s: can't allocate memory for command queue",
    339 		    sc->sc_wdcdev.sc_dev.dv_xname);
    340 		return;
    341 	}
    342 	if (quirks & WDC_PCMCIA_NO_EXTRA_RESETS)
    343 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_NO_EXTRA_RESETS;
    344 
    345 	/* We can enable and disable the controller. */
    346 	sc->sc_wdcdev.sc_atapi_adapter.scsipi_enable = wdc_pcmcia_enable;
    347 
    348 	/*
    349 	 * Disable the pcmcia function now; wdcattach() will enable
    350 	 * us again as it adds references to probe for children.
    351 	 */
    352 	pcmcia_function_disable(pa->pf);
    353 
    354 	wdcattach(&sc->wdc_channel);
    355 }
    356 
    357 int
    358 wdc_pcmcia_enable(arg, onoff)
    359 	void *arg;
    360 	int onoff;
    361 {
    362 	struct wdc_pcmcia_softc *sc = arg;
    363 
    364 	if (onoff) {
    365 		/* Establish the interrupt handler. */
    366 		sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO, wdcintr,
    367 		    &sc->wdc_channel);
    368 		if (sc->sc_ih == NULL) {
    369 			printf("%s: couldn't establish interrupt handler\n",
    370 			    sc->sc_wdcdev.sc_dev.dv_xname);
    371 			return (EIO);
    372 		}
    373 
    374 		if (pcmcia_function_enable(sc->sc_pf)) {
    375 			printf("%s: couldn't enable PCMCIA function\n",
    376 			    sc->sc_wdcdev.sc_dev.dv_xname);
    377 			pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    378 			return (EIO);
    379 		}
    380 	} else {
    381 		pcmcia_function_disable(sc->sc_pf);
    382 		pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    383 	}
    384 
    385 	return (0);
    386 }
    387