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