Home | History | Annotate | Line # | Download | only in pcmcia
wdc_pcmcia.c revision 1.85
      1 /*	$NetBSD: wdc_pcmcia.c,v 1.85 2004/08/11 18:06:22 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.85 2004/08/11 18:06:22 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 	/*
    167 	 * NOTE: We can't use pure memory card mode, because we wouldn't get
    168 	 * any interrupts.
    169 	 */
    170 	switch (cfe->iftype) {
    171 	case PCMCIA_IFTYPE_IO:
    172 		if (cfe->num_iospace < 1 || cfe->num_iospace > 2 ||
    173 		    cfe->num_memspace > 1)
    174 			return (EINVAL);
    175 		break;
    176 	default:
    177 		return (EINVAL);
    178 	}
    179 	return (0);
    180 }
    181 
    182 static void
    183 wdc_pcmcia_attach(parent, self, aux)
    184 	struct device *parent;
    185 	struct device *self;
    186 	void *aux;
    187 {
    188 	struct wdc_pcmcia_softc *sc = (void *)self;
    189 	struct pcmcia_attach_args *pa = aux;
    190 	struct pcmcia_config_entry *cfe;
    191 	bus_size_t offset;
    192 	int i;
    193 	int error;
    194 
    195 	sc->sc_pf = pa->pf;
    196 
    197 	/*XXXmem16|common*/
    198 	error = pcmcia_function_configure(pa->pf, wdc_pcmcia_validate_config);
    199 	if (error) {
    200 		aprint_error("%s: configure failed, error=%d\n", self->dv_xname,
    201 		    error);
    202 		return;
    203 	}
    204 
    205 	cfe = pa->pf->cfe;
    206 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16;
    207 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32;
    208 
    209 	sc->wdc_channel.cmd_iot = cfe->iospace[0].handle.iot;
    210 	sc->wdc_channel.cmd_baseioh = cfe->iospace[0].handle.ioh;
    211 	offset = 0;
    212 
    213 	if (cfe->num_iospace == 1) {
    214 		sc->wdc_channel.ctl_iot = cfe->iospace[0].handle.iot;
    215 		if (bus_space_subregion(cfe->iospace[0].handle.iot,
    216 		    cfe->iospace[0].handle.ioh,
    217 		    WDC_PCMCIA_AUXREG_OFFSET, WDC_PCMCIA_AUXREG_NPORTS,
    218 		    &sc->wdc_channel.ctl_ioh))
    219 			goto fail;
    220 	} else {
    221 		sc->wdc_channel.ctl_iot = cfe->iospace[1].handle.iot;
    222 		sc->wdc_channel.ctl_ioh = cfe->iospace[1].handle.ioh;
    223 	}
    224 
    225 	for (i = 0; i < WDC_PCMCIA_REG_NPORTS; i++) {
    226 		if (bus_space_subregion(sc->wdc_channel.cmd_iot,
    227 		    sc->wdc_channel.cmd_baseioh,
    228 		    offset + i, i == 0 ? 4 : 1,
    229 		    &sc->wdc_channel.cmd_iohs[i]) != 0) {
    230 			aprint_error("%s: can't subregion I/O space\n",
    231 			    self->dv_xname);
    232 			goto fail2;
    233 		}
    234 	}
    235 
    236 	if (cfe->num_memspace == 1) {
    237 		sc->wdc_channel.data32iot = cfe->memspace[0].handle.memt;
    238 		if (bus_space_subregion(cfe->memspace[0].handle.memt,
    239 		    cfe->memspace[0].handle.memh,
    240 		    cfe->memspace[0].offset + 1024, 1024,
    241 		    &sc->wdc_channel.data32ioh))
    242 			goto fail;
    243 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA1K;
    244 		aprint_normal("%s: memory mapped mode\n", self->dv_xname);
    245 	} else {
    246 		sc->wdc_channel.data32iot = sc->wdc_channel.cmd_iot;
    247 		sc->wdc_channel.data32ioh = sc->wdc_channel.cmd_iohs[wd_data];
    248 		aprint_normal("%s: i/o mapped mode\n", self->dv_xname);
    249 	}
    250 
    251 	error = wdc_pcmcia_enable(self, 1);
    252 	if (error)
    253 		goto fail;
    254 
    255 	wdc_init_shadow_regs(&sc->wdc_channel);
    256 	sc->sc_wdcdev.PIO_cap = 0;
    257 	sc->wdc_chanlist[0] = &sc->wdc_channel;
    258 	sc->sc_wdcdev.channels = sc->wdc_chanlist;
    259 	sc->sc_wdcdev.nchannels = 1;
    260 	sc->wdc_channel.ch_channel = 0;
    261 	sc->wdc_channel.ch_wdc = &sc->sc_wdcdev;
    262 	sc->wdc_channel.ch_queue = &sc->wdc_chqueue;
    263 
    264 	/* We can enable and disable the controller. */
    265 	sc->sc_wdcdev.sc_atapi_adapter._generic.adapt_enable =
    266 	    wdc_pcmcia_enable;
    267 	sc->sc_wdcdev.sc_atapi_adapter._generic.adapt_refcnt = 1;
    268 
    269 	wdcattach(&sc->wdc_channel);
    270 	wdc_delref(&sc->wdc_channel);
    271 	sc->sc_state = WDC_PCMCIA_ATTACHED;
    272 	return;
    273 
    274 fail2:
    275 	wdc_pcmcia_enable(self, 0);
    276 fail:
    277 	pcmcia_function_unconfigure(pa->pf);
    278 }
    279 
    280 int
    281 wdc_pcmcia_detach(self, flags)
    282 	struct device *self;
    283 	int flags;
    284 {
    285 	struct wdc_pcmcia_softc *sc = (struct wdc_pcmcia_softc *)self;
    286 	int error;
    287 
    288 	if (sc->sc_state != WDC_PCMCIA_ATTACHED)
    289 		return (0);
    290 
    291 	if ((error = wdcdetach(self, flags)) != 0)
    292 		return (error);
    293 
    294 	pcmcia_function_unconfigure(sc->sc_pf);
    295 
    296 	return (0);
    297 }
    298 
    299 int
    300 wdc_pcmcia_enable(self, onoff)
    301 	struct device *self;
    302 	int onoff;
    303 {
    304 	struct wdc_pcmcia_softc *sc = (void *)self;
    305 	int error;
    306 
    307 	if (onoff) {
    308 		/* Establish the interrupt handler. */
    309 		sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
    310 		    wdcintr, &sc->wdc_channel);
    311 		if (!sc->sc_ih)
    312 			return (EIO);
    313 
    314 		error = pcmcia_function_enable(sc->sc_pf);
    315 		if (error) {
    316 			pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    317 			sc->sc_ih = 0;
    318 			return (error);
    319 		}
    320 	} else {
    321 		pcmcia_function_disable(sc->sc_pf);
    322 		pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    323 		sc->sc_ih = 0;
    324 	}
    325 
    326 	return (0);
    327 }
    328