Home | History | Annotate | Line # | Download | only in pcmcia
wdc_pcmcia.c revision 1.94
      1 /*	$NetBSD: wdc_pcmcia.c,v 1.94 2004/08/19 23:36:26 thorpej 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.94 2004/08/19 23:36:26 thorpej 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 ata_channel *wdc_chanlist[1];
     65 	struct ata_channel ata_channel;
     66 	struct ata_queue wdc_chqueue;
     67 	struct wdc_regs wdc_regs;
     68 
     69 	struct pcmcia_function *sc_pf;
     70 	void *sc_ih;
     71 
     72 	int sc_state;
     73 #define WDC_PCMCIA_ATTACHED	3
     74 };
     75 
     76 static int wdc_pcmcia_match(struct device *, struct cfdata *, void *);
     77 static int wdc_pcmcia_validate_config_io(struct pcmcia_config_entry *);
     78 static int wdc_pcmcia_validate_config_memory(struct pcmcia_config_entry *);
     79 static void wdc_pcmcia_attach(struct device *, struct device *, void *);
     80 static int wdc_pcmcia_detach(struct device *, int);
     81 
     82 CFATTACH_DECL(wdc_pcmcia, sizeof(struct wdc_pcmcia_softc),
     83     wdc_pcmcia_match, wdc_pcmcia_attach, wdc_pcmcia_detach, wdcactivate);
     84 
     85 static const struct pcmcia_product wdc_pcmcia_products[] = {
     86 	{ PCMCIA_VENDOR_DIGITAL,
     87 	  PCMCIA_PRODUCT_DIGITAL_MOBILE_MEDIA_CDROM,
     88 	  {NULL, "Digital Mobile Media CD-ROM", NULL, NULL} },
     89 
     90 	{ PCMCIA_VENDOR_IBM,
     91 	  PCMCIA_PRODUCT_IBM_PORTABLE_CDROM,
     92 	  {NULL, "PCMCIA Portable CD-ROM Drive", NULL, NULL} },
     93 
     94 	/* The TEAC IDE/Card II is used on the Sony Vaio */
     95 	{ PCMCIA_VENDOR_TEAC,
     96 	  PCMCIA_PRODUCT_TEAC_IDECARDII,
     97 	  PCMCIA_CIS_TEAC_IDECARDII },
     98 
     99 	/*
    100 	 * A fujitsu rebranded panasonic drive that reports
    101 	 * itself as function "scsi", disk interface 0
    102 	 */
    103 	{ PCMCIA_VENDOR_PANASONIC,
    104 	  PCMCIA_PRODUCT_PANASONIC_KXLC005,
    105 	  PCMCIA_CIS_PANASONIC_KXLC005 },
    106 
    107 	/*
    108 	 * EXP IDE/ATAPI DVD Card use with some DVD players.
    109 	 * Does not have a vendor ID or product ID.
    110 	 */
    111 	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
    112 	  PCMCIA_CIS_EXP_EXPMULTIMEDIA },
    113 
    114 	/* Mobile Dock 2, neither vendor ID nor product ID */
    115 	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
    116 	  {"SHUTTLE TECHNOLOGY LTD.", "PCCARD-IDE/ATAPI Adapter", NULL, NULL} },
    117 
    118 	/* Toshiba Portege 3110 CD, neither vendor ID nor product ID */
    119 	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
    120 	  {"FREECOM", "PCCARD-IDE", NULL, NULL} },
    121 
    122 	/* Random CD-ROM, (badged AMACOM), neither vendor ID nor product ID */
    123 	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
    124 	  {"PCMCIA", "CD-ROM", NULL, NULL} },
    125 
    126 	/* IO DATA CBIDE2, with neither vendor ID nor product ID */
    127 	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
    128 	  PCMCIA_CIS_IODATA_CBIDE2 },
    129 
    130 	/* TOSHIBA PA2673U(IODATA_CBIDE2 OEM), */
    131 	/*  with neither vendor ID nor product ID */
    132 	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
    133 	  PCMCIA_CIS_TOSHIBA_CBIDE2 },
    134 
    135 	/*
    136 	 * Novac PCMCIA-IDE Card for HD530P IDE Box,
    137 	 * with neither vendor ID nor product ID
    138 	 */
    139 	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
    140 	  {"PCMCIA", "PnPIDE", NULL, NULL} },
    141 };
    142 static const size_t wdc_pcmcia_nproducts =
    143     sizeof(wdc_pcmcia_products) / sizeof(wdc_pcmcia_products[0]);
    144 
    145 static int	wdc_pcmcia_enable(struct device *, int);
    146 static void	wdc_pcmcia_datain_memory(struct ata_channel *, int, void *,
    147 					 size_t);
    148 static void	wdc_pcmcia_dataout_memory(struct ata_channel *, int, void *,
    149 					  size_t);
    150 
    151 static int
    152 wdc_pcmcia_match(struct device *parent, struct cfdata *match, void *aux)
    153 {
    154 	struct pcmcia_attach_args *pa = aux;
    155 
    156 	if (pa->pf->function == PCMCIA_FUNCTION_DISK &&
    157 	    pa->pf->pf_funce_disk_interface == PCMCIA_TPLFE_DDI_PCCARD_ATA)
    158 		return (1);
    159 	if (pcmcia_product_lookup(pa, wdc_pcmcia_products, wdc_pcmcia_nproducts,
    160 	    sizeof(wdc_pcmcia_products[0]), NULL))
    161 		return (2);
    162 	return (0);
    163 }
    164 
    165 static int
    166 wdc_pcmcia_validate_config_io(struct pcmcia_config_entry *cfe)
    167 {
    168 	if (cfe->iftype != PCMCIA_IFTYPE_IO ||
    169 	    cfe->num_iospace < 1 || cfe->num_iospace > 2)
    170 		return (EINVAL);
    171 	cfe->num_memspace = 0;
    172 	return (0);
    173 }
    174 
    175 static int
    176 wdc_pcmcia_validate_config_memory(struct pcmcia_config_entry *cfe)
    177 {
    178 	if (cfe->iftype != PCMCIA_IFTYPE_MEMORY ||
    179 	    cfe->num_memspace > 1 ||
    180 	    cfe->memspace[0].length < 2048)
    181 		return (EINVAL);
    182 	cfe->num_iospace = 0;
    183 	return (0);
    184 }
    185 
    186 static void
    187 wdc_pcmcia_attach(struct device *parent, struct device *self, void *aux)
    188 {
    189 	struct wdc_pcmcia_softc *sc = (void *)self;
    190 	struct pcmcia_attach_args *pa = aux;
    191 	struct pcmcia_config_entry *cfe;
    192 	struct wdc_regs *wdr;
    193 	bus_size_t offset;
    194 	int i;
    195 	int error;
    196 
    197 	sc->sc_pf = pa->pf;
    198 
    199 	error = pcmcia_function_configure(pa->pf,
    200 	    wdc_pcmcia_validate_config_io);
    201 	if (error)
    202 		/*XXXmem16|common*/
    203 		error = pcmcia_function_configure(pa->pf,
    204 		    wdc_pcmcia_validate_config_memory);
    205 	if (error) {
    206 		aprint_error("%s: configure failed, error=%d\n", self->dv_xname,
    207 		    error);
    208 		return;
    209 	}
    210 
    211 	cfe = pa->pf->cfe;
    212 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16;
    213 
    214 	sc->sc_wdcdev.regs = wdr = &sc->wdc_regs;
    215 
    216 	if (cfe->iftype == PCMCIA_IFTYPE_MEMORY) {
    217 		wdr->cmd_iot = cfe->memspace[0].handle.memt;
    218 		wdr->cmd_baseioh = cfe->memspace[0].handle.memh;
    219 		offset = cfe->memspace[0].offset;
    220 		wdr->ctl_iot = cfe->memspace[0].handle.memt;
    221 		if (bus_space_subregion(cfe->memspace[0].handle.memt,
    222 		    cfe->memspace[0].handle.memh,
    223 		    offset + WDC_PCMCIA_AUXREG_OFFSET, WDC_PCMCIA_AUXREG_NPORTS,
    224 		    &wdr->ctl_ioh))
    225 			goto fail;
    226 	} else {
    227 		wdr->cmd_iot = cfe->iospace[0].handle.iot;
    228 		wdr->cmd_baseioh = cfe->iospace[0].handle.ioh;
    229 		offset = 0;
    230 		if (cfe->num_iospace == 1) {
    231 			wdr->ctl_iot = cfe->iospace[0].handle.iot;
    232 			if (bus_space_subregion(cfe->iospace[0].handle.iot,
    233 			    cfe->iospace[0].handle.ioh,
    234 			    WDC_PCMCIA_AUXREG_OFFSET, WDC_PCMCIA_AUXREG_NPORTS,
    235 			    &wdr->ctl_ioh))
    236 				goto fail;
    237 		} else {
    238 			wdr->ctl_iot = cfe->iospace[1].handle.iot;
    239 			wdr->ctl_ioh = cfe->iospace[1].handle.ioh;
    240 		}
    241 	}
    242 
    243 	for (i = 0; i < WDC_PCMCIA_REG_NPORTS; i++) {
    244 		if (bus_space_subregion(wdr->cmd_iot,
    245 		    wdr->cmd_baseioh,
    246 		    offset + i, i == 0 ? 4 : 1,
    247 		    &wdr->cmd_iohs[i]) != 0) {
    248 			aprint_error("%s: can't subregion I/O space\n",
    249 			    self->dv_xname);
    250 			goto fail2;
    251 		}
    252 	}
    253 
    254 	if (cfe->iftype == PCMCIA_IFTYPE_MEMORY) {
    255 		aprint_normal("%s: memory mapped mode\n", self->dv_xname);
    256 		wdr->data32iot = cfe->memspace[0].handle.memt;
    257 		if (bus_space_subregion(cfe->memspace[0].handle.memt,
    258 		    cfe->memspace[0].handle.memh, offset + 1024, 1024,
    259 		    &wdr->data32ioh))
    260 			goto fail;
    261 		sc->sc_wdcdev.datain_pio = wdc_pcmcia_datain_memory;
    262 		sc->sc_wdcdev.dataout_pio = wdc_pcmcia_dataout_memory;
    263 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_NOIRQ;
    264 	} else {
    265 		aprint_normal("%s: i/o mapped mode\n", self->dv_xname);
    266 		wdr->data32iot = wdr->cmd_iot;
    267 		wdr->data32ioh = wdr->cmd_iohs[wd_data];
    268 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32;
    269 	}
    270 
    271 	sc->sc_wdcdev.PIO_cap = 0;
    272 	sc->wdc_chanlist[0] = &sc->ata_channel;
    273 	sc->sc_wdcdev.channels = sc->wdc_chanlist;
    274 	sc->sc_wdcdev.nchannels = 1;
    275 	sc->ata_channel.ch_channel = 0;
    276 	sc->ata_channel.ch_wdc = &sc->sc_wdcdev;
    277 	sc->ata_channel.ch_queue = &sc->wdc_chqueue;
    278 	wdc_init_shadow_regs(&sc->ata_channel);
    279 
    280 	error = wdc_pcmcia_enable(self, 1);
    281 	if (error)
    282 		goto fail;
    283 
    284 	/* We can enable and disable the controller. */
    285 	sc->sc_wdcdev.sc_atapi_adapter._generic.adapt_enable =
    286 	    wdc_pcmcia_enable;
    287 	sc->sc_wdcdev.sc_atapi_adapter._generic.adapt_refcnt = 1;
    288 
    289 	wdcattach(&sc->ata_channel);
    290 	ata_delref(&sc->ata_channel);
    291 	sc->sc_state = WDC_PCMCIA_ATTACHED;
    292 	return;
    293 
    294 fail2:
    295 	wdc_pcmcia_enable(self, 0);
    296 fail:
    297 	pcmcia_function_unconfigure(pa->pf);
    298 }
    299 
    300 static int
    301 wdc_pcmcia_detach(struct device *self, int flags)
    302 {
    303 	struct wdc_pcmcia_softc *sc = (struct wdc_pcmcia_softc *)self;
    304 	int error;
    305 
    306 	if (sc->sc_state != WDC_PCMCIA_ATTACHED)
    307 		return (0);
    308 
    309 	if ((error = wdcdetach(self, flags)) != 0)
    310 		return (error);
    311 
    312 	pcmcia_function_unconfigure(sc->sc_pf);
    313 
    314 	return (0);
    315 }
    316 
    317 static int
    318 wdc_pcmcia_enable(struct device *self, 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->ata_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 
    345 static void
    346 wdc_pcmcia_datain_memory(struct ata_channel *chp, int flags, void *buf,
    347     size_t len)
    348 {
    349 	struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp);
    350 
    351 	while (len > 0) {
    352 		size_t n;
    353 
    354 		n = min(len, 1024);
    355 		if ((flags & DRIVE_CAP32) && (n & 3) == 0)
    356 			bus_space_read_region_stream_4(wdr->data32iot,
    357 			    wdr->data32ioh, 0, buf, n >> 2);
    358 		else
    359 			bus_space_read_region_stream_2(wdr->data32iot,
    360 			    wdr->data32ioh, 0, buf, n >> 1);
    361 		buf = (char *)buf + n;
    362 		len -= n;
    363 	}
    364 }
    365 
    366 static void
    367 wdc_pcmcia_dataout_memory(struct ata_channel *chp, int flags, void *buf,
    368     size_t len)
    369 {
    370 	struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp);
    371 
    372 	while (len > 0) {
    373 		size_t n;
    374 
    375 		n = min(len, 1024);
    376 		if ((flags & DRIVE_CAP32) && (n & 3) == 0)
    377 			bus_space_write_region_stream_4(wdr->data32iot,
    378 			    wdr->data32ioh, 0, buf, n >> 2);
    379 		else
    380 			bus_space_write_region_stream_2(wdr->data32iot,
    381 			    wdr->data32ioh, 0, buf, n >> 1);
    382 		buf = (char *)buf + n;
    383 		len -= n;
    384 	}
    385 }
    386