Home | History | Annotate | Line # | Download | only in pcmcia
wdc_pcmcia.c revision 1.86
      1 /*	$NetBSD: wdc_pcmcia.c,v 1.86 2004/08/11 18:41:46 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.86 2004/08/11 18:41:46 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 void	wdc_pcmcia_datain_memory __P((struct wdc_channel *, int, void *,
    145 	    size_t));
    146 void	wdc_pcmcia_dataout_memory __P((struct wdc_channel *, int, void *,
    147 	    size_t));
    148 
    149 static int
    150 wdc_pcmcia_match(parent, match, aux)
    151 	struct device *parent;
    152 	struct cfdata *match;
    153 	void *aux;
    154 {
    155 	struct pcmcia_attach_args *pa = aux;
    156 
    157 	if (pa->pf->function == PCMCIA_FUNCTION_DISK &&
    158 	    pa->pf->pf_funce_disk_interface == PCMCIA_TPLFE_DDI_PCCARD_ATA)
    159 		return (1);
    160 	if (pcmcia_product_lookup(pa, wdc_pcmcia_products, wdc_pcmcia_nproducts,
    161 	    sizeof(wdc_pcmcia_products[0]), NULL))
    162 		return (2);
    163 	return (0);
    164 }
    165 
    166 static int
    167 wdc_pcmcia_validate_config(cfe)
    168 	struct pcmcia_config_entry *cfe;
    169 {
    170 	/*
    171 	 * NOTE: We can't use pure memory card mode, because we wouldn't get
    172 	 * any interrupts.
    173 	 */
    174 	switch (cfe->iftype) {
    175 	case PCMCIA_IFTYPE_IO:
    176 		if (cfe->num_iospace < 1 || cfe->num_iospace > 2 ||
    177 		    cfe->num_memspace > 1)
    178 			return (EINVAL);
    179 		break;
    180 	default:
    181 		return (EINVAL);
    182 	}
    183 	return (0);
    184 }
    185 
    186 static void
    187 wdc_pcmcia_attach(parent, self, aux)
    188 	struct device *parent;
    189 	struct device *self;
    190 	void *aux;
    191 {
    192 	struct wdc_pcmcia_softc *sc = (void *)self;
    193 	struct pcmcia_attach_args *pa = aux;
    194 	struct pcmcia_config_entry *cfe;
    195 	bus_size_t offset;
    196 	int i;
    197 	int error;
    198 
    199 	sc->sc_pf = pa->pf;
    200 
    201 	/*XXXmem16|common*/
    202 	error = pcmcia_function_configure(pa->pf, wdc_pcmcia_validate_config);
    203 	if (error) {
    204 		aprint_error("%s: configure failed, error=%d\n", self->dv_xname,
    205 		    error);
    206 		return;
    207 	}
    208 
    209 	cfe = pa->pf->cfe;
    210 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16;
    211 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32;
    212 
    213 	sc->wdc_channel.cmd_iot = cfe->iospace[0].handle.iot;
    214 	sc->wdc_channel.cmd_baseioh = cfe->iospace[0].handle.ioh;
    215 	offset = 0;
    216 
    217 	if (cfe->num_iospace == 1) {
    218 		sc->wdc_channel.ctl_iot = cfe->iospace[0].handle.iot;
    219 		if (bus_space_subregion(cfe->iospace[0].handle.iot,
    220 		    cfe->iospace[0].handle.ioh,
    221 		    WDC_PCMCIA_AUXREG_OFFSET, WDC_PCMCIA_AUXREG_NPORTS,
    222 		    &sc->wdc_channel.ctl_ioh))
    223 			goto fail;
    224 	} else {
    225 		sc->wdc_channel.ctl_iot = cfe->iospace[1].handle.iot;
    226 		sc->wdc_channel.ctl_ioh = cfe->iospace[1].handle.ioh;
    227 	}
    228 
    229 	for (i = 0; i < WDC_PCMCIA_REG_NPORTS; i++) {
    230 		if (bus_space_subregion(sc->wdc_channel.cmd_iot,
    231 		    sc->wdc_channel.cmd_baseioh,
    232 		    offset + i, i == 0 ? 4 : 1,
    233 		    &sc->wdc_channel.cmd_iohs[i]) != 0) {
    234 			aprint_error("%s: can't subregion I/O space\n",
    235 			    self->dv_xname);
    236 			goto fail2;
    237 		}
    238 	}
    239 
    240 	if (cfe->num_memspace == 1) {
    241 		sc->wdc_channel.data32iot = cfe->memspace[0].handle.memt;
    242 		if (bus_space_subregion(cfe->memspace[0].handle.memt,
    243 		    cfe->memspace[0].handle.memh,
    244 		    cfe->memspace[0].offset + 1024, 1024,
    245 		    &sc->wdc_channel.data32ioh))
    246 			goto fail;
    247 		aprint_normal("%s: memory mapped mode\n", self->dv_xname);
    248 #if 0
    249 		sc->wdc_channel.datain_pio = wdc_pcmcia_datain_memory;
    250 		sc->wdc_channel.dataout_pio = wdc_pcmcia_dataout_memory;
    251 #endif
    252 	} else {
    253 		sc->wdc_channel.data32iot = sc->wdc_channel.cmd_iot;
    254 		sc->wdc_channel.data32ioh = sc->wdc_channel.cmd_iohs[wd_data];
    255 		aprint_normal("%s: i/o mapped mode\n", self->dv_xname);
    256 	}
    257 
    258 	error = wdc_pcmcia_enable(self, 1);
    259 	if (error)
    260 		goto fail;
    261 
    262 	wdc_init_shadow_regs(&sc->wdc_channel);
    263 	sc->sc_wdcdev.PIO_cap = 0;
    264 	sc->wdc_chanlist[0] = &sc->wdc_channel;
    265 	sc->sc_wdcdev.channels = sc->wdc_chanlist;
    266 	sc->sc_wdcdev.nchannels = 1;
    267 	sc->wdc_channel.ch_channel = 0;
    268 	sc->wdc_channel.ch_wdc = &sc->sc_wdcdev;
    269 	sc->wdc_channel.ch_queue = &sc->wdc_chqueue;
    270 
    271 	/* We can enable and disable the controller. */
    272 	sc->sc_wdcdev.sc_atapi_adapter._generic.adapt_enable =
    273 	    wdc_pcmcia_enable;
    274 	sc->sc_wdcdev.sc_atapi_adapter._generic.adapt_refcnt = 1;
    275 
    276 	wdcattach(&sc->wdc_channel);
    277 	wdc_delref(&sc->wdc_channel);
    278 	sc->sc_state = WDC_PCMCIA_ATTACHED;
    279 	return;
    280 
    281 fail2:
    282 	wdc_pcmcia_enable(self, 0);
    283 fail:
    284 	pcmcia_function_unconfigure(pa->pf);
    285 }
    286 
    287 int
    288 wdc_pcmcia_detach(self, flags)
    289 	struct device *self;
    290 	int flags;
    291 {
    292 	struct wdc_pcmcia_softc *sc = (struct wdc_pcmcia_softc *)self;
    293 	int error;
    294 
    295 	if (sc->sc_state != WDC_PCMCIA_ATTACHED)
    296 		return (0);
    297 
    298 	if ((error = wdcdetach(self, flags)) != 0)
    299 		return (error);
    300 
    301 	pcmcia_function_unconfigure(sc->sc_pf);
    302 
    303 	return (0);
    304 }
    305 
    306 int
    307 wdc_pcmcia_enable(self, onoff)
    308 	struct device *self;
    309 	int onoff;
    310 {
    311 	struct wdc_pcmcia_softc *sc = (void *)self;
    312 	int error;
    313 
    314 	if (onoff) {
    315 		/* Establish the interrupt handler. */
    316 		sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
    317 		    wdcintr, &sc->wdc_channel);
    318 		if (!sc->sc_ih)
    319 			return (EIO);
    320 
    321 		error = pcmcia_function_enable(sc->sc_pf);
    322 		if (error) {
    323 			pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    324 			sc->sc_ih = 0;
    325 			return (error);
    326 		}
    327 	} else {
    328 		pcmcia_function_disable(sc->sc_pf);
    329 		pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    330 		sc->sc_ih = 0;
    331 	}
    332 
    333 	return (0);
    334 }
    335 
    336 void
    337 wdc_pcmcia_datain_memory(chp, flags, buf, len)
    338 	struct wdc_channel *chp;
    339 	int flags;
    340 	void *buf;
    341 	size_t len;
    342 {
    343 
    344 	while (len > 0) {
    345 		size_t n;
    346 
    347 		n = min(len, 1024);
    348 		if (flags & DRIVE_NOSTREAM) {
    349 			if ((flags & DRIVE_CAP32) && (n & 3) == 0)
    350 				bus_space_read_region_4(
    351 				    chp->data32iot, chp->data32ioh,
    352 				    0, buf, n >> 2);
    353 			else
    354 				bus_space_read_region_2(
    355 				    chp->data32iot, chp->data32ioh,
    356 				    0, buf, n >> 1);
    357 		} else {
    358 			if ((flags & DRIVE_CAP32) && (n & 3) == 0)
    359 				bus_space_read_region_stream_4(
    360 				    chp->data32iot, chp->data32ioh,
    361 				    0, buf, n >> 2);
    362 			else
    363 				bus_space_read_region_stream_2(
    364 				    chp->data32iot, chp->data32ioh,
    365 				    0, buf, n >> 1);
    366 		}
    367 		buf = (char *)buf + n;
    368 		len -= n;
    369 	}
    370 }
    371 
    372 void
    373 wdc_pcmcia_dataout_memory(chp, flags, buf, len)
    374 	struct wdc_channel *chp;
    375 	int flags;
    376 	void *buf;
    377 	size_t len;
    378 {
    379 
    380 	while (len > 0) {
    381 		size_t n;
    382 
    383 		n = min(len, 1024);
    384 		if (flags & DRIVE_NOSTREAM) {
    385 			if ((flags & DRIVE_CAP32) && (n & 3) == 0)
    386 				bus_space_write_region_4(
    387 				    chp->data32iot, chp->data32ioh,
    388 				    0, buf, n >> 2);
    389 			else
    390 				bus_space_write_region_2(
    391 				    chp->data32iot, chp->data32ioh,
    392 				    0, buf, n >> 1);
    393 		} else {
    394 			if ((flags & DRIVE_CAP32) && (n & 3) == 0)
    395 				bus_space_write_region_stream_4(
    396 				    chp->data32iot, chp->data32ioh,
    397 				    0, buf, n >> 2);
    398 			else
    399 				bus_space_write_region_stream_2(
    400 				    chp->data32iot, chp->data32ioh,
    401 				    0, buf, n >> 1);
    402 		}
    403 		buf = (char *)buf + n;
    404 		len -= n;
    405 	}
    406 }
    407