Home | History | Annotate | Line # | Download | only in pcmcia
wdc_pcmcia.c revision 1.84
      1 /*	$NetBSD: wdc_pcmcia.c,v 1.84 2004/08/10 23:34:32 mycroft Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1998, 2003, 2004 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/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: wdc_pcmcia.c,v 1.84 2004/08/10 23:34:32 mycroft Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/device.h>
     44 #include <sys/malloc.h>
     45 #include <sys/systm.h>
     46 
     47 #include <machine/bus.h>
     48 #include <machine/intr.h>
     49 
     50 #include <dev/pcmcia/pcmciareg.h>
     51 #include <dev/pcmcia/pcmciavar.h>
     52 #include <dev/pcmcia/pcmciadevs.h>
     53 
     54 #include <dev/ic/wdcreg.h>
     55 #include <dev/ata/atavar.h>
     56 #include <dev/ic/wdcvar.h>
     57 
     58 #define WDC_PCMCIA_REG_NPORTS      8
     59 #define WDC_PCMCIA_AUXREG_OFFSET   (WDC_PCMCIA_REG_NPORTS + 6)
     60 #define WDC_PCMCIA_AUXREG_NPORTS   2
     61 
     62 struct wdc_pcmcia_softc {
     63 	struct wdc_softc sc_wdcdev;
     64 	struct wdc_channel *wdc_chanlist[1];
     65 	struct wdc_channel wdc_channel;
     66 	struct ata_queue wdc_chqueue;
     67 
     68 	struct pcmcia_function *sc_pf;
     69 	void *sc_ih;
     70 
     71 	int sc_state;
     72 #define WDC_PCMCIA_ATTACHED	3
     73 };
     74 
     75 static int wdc_pcmcia_match	__P((struct device *, struct cfdata *, void *));
     76 static int wdc_pcmcia_validate_config __P((struct pcmcia_config_entry *));
     77 static void wdc_pcmcia_attach	__P((struct device *, struct device *, void *));
     78 static int wdc_pcmcia_detach	__P((struct device *, int));
     79 
     80 CFATTACH_DECL(wdc_pcmcia, sizeof(struct wdc_pcmcia_softc),
     81     wdc_pcmcia_match, wdc_pcmcia_attach, wdc_pcmcia_detach, wdcactivate);
     82 
     83 const struct pcmcia_product wdc_pcmcia_products[] = {
     84 	{ PCMCIA_VENDOR_DIGITAL,
     85 	  PCMCIA_PRODUCT_DIGITAL_MOBILE_MEDIA_CDROM,
     86 	  {NULL, "Digital Mobile Media CD-ROM", NULL, NULL} },
     87 
     88 	{ PCMCIA_VENDOR_IBM,
     89 	  PCMCIA_PRODUCT_IBM_PORTABLE_CDROM,
     90 	  {NULL, "PCMCIA Portable CD-ROM Drive", NULL, NULL} },
     91 
     92 	/* The TEAC IDE/Card II is used on the Sony Vaio */
     93 	{ PCMCIA_VENDOR_TEAC,
     94 	  PCMCIA_PRODUCT_TEAC_IDECARDII,
     95 	  PCMCIA_CIS_TEAC_IDECARDII },
     96 
     97 	/*
     98 	 * A fujitsu rebranded panasonic drive that reports
     99 	 * itself as function "scsi", disk interface 0
    100 	 */
    101 	{ PCMCIA_VENDOR_PANASONIC,
    102 	  PCMCIA_PRODUCT_PANASONIC_KXLC005,
    103 	  PCMCIA_CIS_PANASONIC_KXLC005 },
    104 
    105 	/*
    106 	 * EXP IDE/ATAPI DVD Card use with some DVD players.
    107 	 * Does not have a vendor ID or product ID.
    108 	 */
    109 	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
    110 	  PCMCIA_CIS_EXP_EXPMULTIMEDIA },
    111 
    112 	/* Mobile Dock 2, neither vendor ID nor product ID */
    113 	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
    114 	  {"SHUTTLE TECHNOLOGY LTD.", "PCCARD-IDE/ATAPI Adapter", NULL, NULL} },
    115 
    116 	/* Toshiba Portege 3110 CD, neither vendor ID nor product ID */
    117 	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
    118 	  {"FREECOM", "PCCARD-IDE", NULL, NULL} },
    119 
    120 	/* Random CD-ROM, (badged AMACOM), neither vendor ID nor product ID */
    121 	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
    122 	  {"PCMCIA", "CD-ROM", NULL, NULL} },
    123 
    124 	/* IO DATA CBIDE2, with neither vendor ID nor product ID */
    125 	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
    126 	  PCMCIA_CIS_IODATA_CBIDE2 },
    127 
    128 	/* TOSHIBA PA2673U(IODATA_CBIDE2 OEM), */
    129 	/*  with neither vendor ID nor product ID */
    130 	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
    131 	  PCMCIA_CIS_TOSHIBA_CBIDE2 },
    132 
    133 	/*
    134 	 * Novac PCMCIA-IDE Card for HD530P IDE Box,
    135 	 * with neither vendor ID nor product ID
    136 	 */
    137 	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
    138 	  {"PCMCIA", "PnPIDE", NULL, NULL} },
    139 };
    140 const size_t wdc_pcmcia_nproducts =
    141     sizeof(wdc_pcmcia_products) / sizeof(wdc_pcmcia_products[0]);
    142 
    143 int	wdc_pcmcia_enable __P((struct device *, int));
    144 
    145 static int
    146 wdc_pcmcia_match(parent, match, aux)
    147 	struct device *parent;
    148 	struct cfdata *match;
    149 	void *aux;
    150 {
    151 	struct pcmcia_attach_args *pa = aux;
    152 
    153 	if (pa->pf->function == PCMCIA_FUNCTION_DISK &&
    154 	    pa->pf->pf_funce_disk_interface == PCMCIA_TPLFE_DDI_PCCARD_ATA)
    155 		return (1);
    156 	if (pcmcia_product_lookup(pa, wdc_pcmcia_products, wdc_pcmcia_nproducts,
    157 	    sizeof(wdc_pcmcia_products[0]), NULL))
    158 		return (2);
    159 	return (0);
    160 }
    161 
    162 static int
    163 wdc_pcmcia_validate_config(cfe)
    164 	struct pcmcia_config_entry *cfe;
    165 {
    166 	switch (cfe->iftype) {
    167 #if 0
    168 	case PCMCIA_IFTYPE_MEMORY:
    169 		if (cfe->num_iospace != 0 ||
    170 		    cfe->num_memspace != 1)
    171 			return (EINVAL);
    172 		break;
    173 #endif
    174 	case PCMCIA_IFTYPE_IO:
    175 		if (cfe->num_iospace < 1 || cfe->num_iospace > 2)
    176 			return (EINVAL);
    177 		cfe->num_memspace = 0;
    178 		break;
    179 	default:
    180 		return (EINVAL);
    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 	bus_size_t offset;
    195 	int i;
    196 	int error;
    197 
    198 	sc->sc_pf = pa->pf;
    199 
    200 	/*XXXmem16|common*/
    201 	error = pcmcia_function_configure(pa->pf, wdc_pcmcia_validate_config);
    202 	if (error) {
    203 		aprint_error("%s: configure failed, error=%d\n", self->dv_xname,
    204 		    error);
    205 		return;
    206 	}
    207 
    208 	cfe = pa->pf->cfe;
    209 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16;
    210 	if (cfe->iftype == PCMCIA_IFTYPE_MEMORY) {
    211 		sc->wdc_channel.cmd_iot = cfe->memspace[0].handle.memt;
    212 		sc->wdc_channel.cmd_baseioh = cfe->memspace[0].handle.memh;
    213 		offset = cfe->memspace[0].offset;
    214 
    215 		sc->wdc_channel.ctl_iot = cfe->memspace[0].handle.memt;
    216 		if (bus_space_subregion(cfe->memspace[0].handle.memt,
    217 		    cfe->memspace[0].handle.memh,
    218 		    WDC_PCMCIA_AUXREG_OFFSET + offset, WDC_PCMCIA_AUXREG_NPORTS,
    219 		    &sc->wdc_channel.ctl_ioh))
    220 			goto fail;
    221 
    222 		aprint_normal("%s: memory mapped mode\n", self->dv_xname);
    223 	} else {
    224 		sc->wdc_channel.cmd_iot = cfe->iospace[0].handle.iot;
    225 		sc->wdc_channel.cmd_baseioh = cfe->iospace[0].handle.ioh;
    226 		offset = 0;
    227 
    228 		if (cfe->num_iospace == 1) {
    229 			sc->wdc_channel.ctl_iot = cfe->iospace[0].handle.iot;
    230 			if (bus_space_subregion(cfe->iospace[0].handle.iot,
    231 			    cfe->iospace[0].handle.ioh,
    232 			    WDC_PCMCIA_AUXREG_OFFSET, WDC_PCMCIA_AUXREG_NPORTS,
    233 			    &sc->wdc_channel.ctl_ioh))
    234 				goto fail;
    235 		} else {
    236 			sc->wdc_channel.ctl_iot = cfe->iospace[1].handle.iot;
    237 			sc->wdc_channel.ctl_ioh = cfe->iospace[1].handle.ioh;
    238 		}
    239 
    240 		aprint_normal("%s: i/o mapped mode\n", self->dv_xname);
    241 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32;
    242 	}
    243 
    244 	error = wdc_pcmcia_enable(self, 1);
    245 	if (error)
    246 		goto fail;
    247 
    248 	for (i = 0; i < WDC_PCMCIA_REG_NPORTS; i++) {
    249 		if (bus_space_subregion(sc->wdc_channel.cmd_iot,
    250 		    sc->wdc_channel.cmd_baseioh,
    251 		    offset + i, i == 0 ? 4 : 1,
    252 		    &sc->wdc_channel.cmd_iohs[i]) != 0) {
    253 			aprint_error("%s: can't subregion I/O space\n",
    254 			    self->dv_xname);
    255 			goto fail2;
    256 		}
    257 	}
    258 	wdc_init_shadow_regs(&sc->wdc_channel);
    259 	sc->wdc_channel.data32iot = sc->wdc_channel.cmd_iot;
    260 	sc->wdc_channel.data32ioh = sc->wdc_channel.cmd_iohs[0];
    261 	sc->sc_wdcdev.PIO_cap = 0;
    262 	sc->wdc_chanlist[0] = &sc->wdc_channel;
    263 	sc->sc_wdcdev.channels = sc->wdc_chanlist;
    264 	sc->sc_wdcdev.nchannels = 1;
    265 	sc->wdc_channel.ch_channel = 0;
    266 	sc->wdc_channel.ch_wdc = &sc->sc_wdcdev;
    267 	sc->wdc_channel.ch_queue = &sc->wdc_chqueue;
    268 
    269 	/* We can enable and disable the controller. */
    270 	sc->sc_wdcdev.sc_atapi_adapter._generic.adapt_enable =
    271 	    wdc_pcmcia_enable;
    272 	sc->sc_wdcdev.sc_atapi_adapter._generic.adapt_refcnt = 1;
    273 
    274 	wdcattach(&sc->wdc_channel);
    275 	wdc_delref(&sc->wdc_channel);
    276 	sc->sc_state = WDC_PCMCIA_ATTACHED;
    277 	return;
    278 
    279 fail2:
    280 	wdc_pcmcia_enable(self, 0);
    281 fail:
    282 	pcmcia_function_unconfigure(pa->pf);
    283 }
    284 
    285 int
    286 wdc_pcmcia_detach(self, flags)
    287 	struct device *self;
    288 	int flags;
    289 {
    290 	struct wdc_pcmcia_softc *sc = (struct wdc_pcmcia_softc *)self;
    291 	int error;
    292 
    293 	if (sc->sc_state != WDC_PCMCIA_ATTACHED)
    294 		return (0);
    295 
    296 	if ((error = wdcdetach(self, flags)) != 0)
    297 		return (error);
    298 
    299 	pcmcia_function_unconfigure(sc->sc_pf);
    300 
    301 	return (0);
    302 }
    303 
    304 int
    305 wdc_pcmcia_enable(self, onoff)
    306 	struct device *self;
    307 	int onoff;
    308 {
    309 	struct wdc_pcmcia_softc *sc = (void *)self;
    310 	int error;
    311 
    312 	if (onoff) {
    313 		/* Establish the interrupt handler. */
    314 		sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
    315 		    wdcintr, &sc->wdc_channel);
    316 		if (!sc->sc_ih)
    317 			return (EIO);
    318 
    319 		error = pcmcia_function_enable(sc->sc_pf);
    320 		if (error) {
    321 			pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    322 			sc->sc_ih = 0;
    323 			return (error);
    324 		}
    325 	} else {
    326 		pcmcia_function_disable(sc->sc_pf);
    327 		pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    328 		sc->sc_ih = 0;
    329 	}
    330 
    331 	return (0);
    332 }
    333