1/* $NetBSD: wdc_pcmcia.c,v 1.116 2009/05/12 14:42:19 cegger 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 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32#include <sys/cdefs.h> 33__KERNEL_RCSID(0, "$NetBSD: wdc_pcmcia.c,v 1.116 2009/05/12 14:42:19 cegger Exp $"); 34 35#include <sys/param.h> 36#include <sys/device.h> 37#include <sys/kernel.h> 38#include <sys/malloc.h> 39#include <sys/systm.h> 40#include <sys/proc.h> 41 42#include <sys/bus.h> 43#include <sys/intr.h> 44 45#include <dev/pcmcia/pcmciareg.h> 46#include <dev/pcmcia/pcmciavar.h> 47#include <dev/pcmcia/pcmciadevs.h> 48 49#include <dev/ic/wdcreg.h> 50#include <dev/ata/atavar.h> 51#include <dev/ic/wdcvar.h> 52 53#ifndef __BUS_SPACE_HAS_STREAM_METHODS 54#define bus_space_write_multi_stream_2 bus_space_write_multi_2 55#define bus_space_write_multi_stream_4 bus_space_write_multi_4 56#define bus_space_read_multi_stream_2 bus_space_read_multi_2 57#define bus_space_read_multi_stream_4 bus_space_read_multi_4 58#endif /* __BUS_SPACE_HAS_STREAM_METHODS */ 59 60#define WDC_PCMCIA_REG_NPORTS 8 61#define WDC_PCMCIA_AUXREG_OFFSET (WDC_PCMCIA_REG_NPORTS + 6) 62#define WDC_PCMCIA_AUXREG_NPORTS 2 63 64struct wdc_pcmcia_softc { 65 struct wdc_softc sc_wdcdev; 66 struct ata_channel *wdc_chanlist[1]; 67 struct ata_channel ata_channel; 68 struct ata_queue wdc_chqueue; 69 struct wdc_regs wdc_regs; 70 71 struct pcmcia_function *sc_pf; 72 void *sc_ih; 73 74 int sc_state; 75#define WDC_PCMCIA_ATTACHED 3 76}; 77 78#ifndef __BUS_SPACE_HAS_STREAM_METHODS 79#define bus_space_read_region_stream_2 bus_space_read_region_2 80#define bus_space_read_region_stream_4 bus_space_read_region_4 81#define bus_space_write_region_stream_2 bus_space_write_region_2 82#define bus_space_write_region_stream_4 bus_space_write_region_4 83#endif /* __BUS_SPACE_HAS_STREAM_METHODS */ 84 85static int wdc_pcmcia_match(device_t, cfdata_t, void *); 86static int wdc_pcmcia_validate_config_io(struct pcmcia_config_entry *); 87static int wdc_pcmcia_validate_config_memory(struct pcmcia_config_entry *); 88static void wdc_pcmcia_attach(device_t, device_t, void *); 89static int wdc_pcmcia_detach(device_t, int); 90 91CFATTACH_DECL_NEW(wdc_pcmcia, sizeof(struct wdc_pcmcia_softc), 92 wdc_pcmcia_match, wdc_pcmcia_attach, wdc_pcmcia_detach, NULL); 93 94static const struct wdc_pcmcia_product { 95 struct pcmcia_product wdc_product; 96 int wdc_ndrive; 97} wdc_pcmcia_products[] = { 98 { { PCMCIA_VENDOR_DIGITAL, 99 PCMCIA_PRODUCT_DIGITAL_MOBILE_MEDIA_CDROM, 100 {NULL, "Digital Mobile Media CD-ROM", NULL, NULL} }, 2 }, 101 102 { { PCMCIA_VENDOR_IBM, 103 PCMCIA_PRODUCT_IBM_PORTABLE_CDROM, 104 {NULL, "PCMCIA Portable CD-ROM Drive", NULL, NULL} }, 2 }, 105 106 /* The TEAC IDE/Card II is used on the Sony Vaio */ 107 { { PCMCIA_VENDOR_TEAC, 108 PCMCIA_PRODUCT_TEAC_IDECARDII, 109 PCMCIA_CIS_TEAC_IDECARDII }, 2 }, 110 111 /* 112 * A fujitsu rebranded panasonic drive that reports 113 * itself as function "scsi", disk interface 0 114 */ 115 { { PCMCIA_VENDOR_PANASONIC, 116 PCMCIA_PRODUCT_PANASONIC_KXLC005, 117 PCMCIA_CIS_PANASONIC_KXLC005 }, 2 }, 118 119 { { PCMCIA_VENDOR_SANDISK, 120 PCMCIA_PRODUCT_SANDISK_SDCFB, 121 PCMCIA_CIS_SANDISK_SDCFB }, 1 }, 122 123 /* 124 * EXP IDE/ATAPI DVD Card use with some DVD players. 125 * Does not have a vendor ID or product ID. 126 */ 127 { { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID, 128 PCMCIA_CIS_EXP_EXPMULTIMEDIA }, 2 }, 129 130 /* Mobile Dock 2, neither vendor ID nor product ID */ 131 { { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID, 132 {"SHUTTLE TECHNOLOGY LTD.", "PCCARD-IDE/ATAPI Adapter", NULL, NULL} }, 2 }, 133 134 /* Toshiba Portege 3110 CD, neither vendor ID nor product ID */ 135 { { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID, 136 {"FREECOM", "PCCARD-IDE", NULL, NULL} }, 2 }, 137 138 /* Random CD-ROM, (badged AMACOM), neither vendor ID nor product ID */ 139 { { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID, 140 {"PCMCIA", "CD-ROM", NULL, NULL} }, 2 }, 141 142 /* IO DATA CBIDE2, with neither vendor ID nor product ID */ 143 { { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID, 144 PCMCIA_CIS_IODATA_CBIDE2 }, 2 }, 145 146 /* TOSHIBA PA2673U(IODATA_CBIDE2 OEM), */ 147 /* with neither vendor ID nor product ID */ 148 { { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID, 149 PCMCIA_CIS_TOSHIBA_CBIDE2 }, 2 }, 150 151 /* 152 * Novac PCMCIA-IDE Card for HD530P IDE Box, 153 * with neither vendor ID nor product ID 154 */ 155 { { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID, 156 {"PCMCIA", "PnPIDE", NULL, NULL} }, 2 }, 157 158 { { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID, 159 {"PCMCIA", "IDE CARD", NULL, NULL} }, 2 }, 160 161}; 162static const size_t wdc_pcmcia_nproducts = 163 sizeof(wdc_pcmcia_products) / sizeof(wdc_pcmcia_products[0]); 164 165static int wdc_pcmcia_enable(device_t, int); 166static void wdc_pcmcia_datain_memory(struct ata_channel *, int, void *, 167 size_t); 168static void wdc_pcmcia_dataout_memory(struct ata_channel *, int, void *, 169 size_t); 170 171static int 172wdc_pcmcia_match(device_t parent, cfdata_t match, void *aux) 173{ 174 struct pcmcia_attach_args *pa = aux; 175 176 if (pa->pf->function == PCMCIA_FUNCTION_DISK && 177 pa->pf->pf_funce_disk_interface == PCMCIA_TPLFE_DDI_PCCARD_ATA) 178 return (1); 179 if (pcmcia_product_lookup(pa, wdc_pcmcia_products, wdc_pcmcia_nproducts, 180 sizeof(wdc_pcmcia_products[0]), NULL)) 181 return (2); 182 return (0); 183} 184 185static int 186wdc_pcmcia_validate_config_io(struct pcmcia_config_entry *cfe) 187{ 188 if (cfe->iftype != PCMCIA_IFTYPE_IO || 189 cfe->num_iospace < 1 || cfe->num_iospace > 2) 190 return (EINVAL); 191 cfe->num_memspace = 0; 192 return (0); 193} 194 195static int 196wdc_pcmcia_validate_config_memory(struct pcmcia_config_entry *cfe) 197{ 198 if (cfe->iftype != PCMCIA_IFTYPE_MEMORY || 199 cfe->num_memspace > 1 || 200 cfe->memspace[0].length < 2048) 201 return (EINVAL); 202 cfe->num_iospace = 0; 203 return (0); 204} 205 206static void 207wdc_pcmcia_attach(device_t parent, device_t self, void *aux) 208{ 209 struct wdc_pcmcia_softc *sc = device_private(self); 210 struct pcmcia_attach_args *pa = aux; 211 struct pcmcia_config_entry *cfe; 212 struct wdc_regs *wdr; 213 const struct wdc_pcmcia_product *wdcp; 214 bus_size_t offset; 215 int i; 216 int error; 217 218 aprint_naive("\n"); 219 220 sc->sc_wdcdev.sc_atac.atac_dev = self; 221 sc->sc_pf = pa->pf; 222 223 error = pcmcia_function_configure(pa->pf, 224 wdc_pcmcia_validate_config_io); 225 if (error) 226 /*XXXmem16|common*/ 227 error = pcmcia_function_configure(pa->pf, 228 wdc_pcmcia_validate_config_memory); 229 if (error) { 230 aprint_error_dev(self, "configure failed, error=%d\n", error); 231 return; 232 } 233 234 cfe = pa->pf->cfe; 235 sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16; 236 237 sc->sc_wdcdev.regs = wdr = &sc->wdc_regs; 238 239 if (cfe->iftype == PCMCIA_IFTYPE_MEMORY) { 240 wdr->cmd_iot = cfe->memspace[0].handle.memt; 241 wdr->cmd_baseioh = cfe->memspace[0].handle.memh; 242 offset = cfe->memspace[0].offset; 243 wdr->ctl_iot = cfe->memspace[0].handle.memt; 244 if (bus_space_subregion(cfe->memspace[0].handle.memt, 245 cfe->memspace[0].handle.memh, 246 offset + WDC_PCMCIA_AUXREG_OFFSET, WDC_PCMCIA_AUXREG_NPORTS, 247 &wdr->ctl_ioh)) 248 goto fail; 249 } else { 250 wdr->cmd_iot = cfe->iospace[0].handle.iot; 251 wdr->cmd_baseioh = cfe->iospace[0].handle.ioh; 252 offset = 0; 253 if (cfe->num_iospace == 1) { 254 wdr->ctl_iot = cfe->iospace[0].handle.iot; 255 if (bus_space_subregion(cfe->iospace[0].handle.iot, 256 cfe->iospace[0].handle.ioh, 257 WDC_PCMCIA_AUXREG_OFFSET, WDC_PCMCIA_AUXREG_NPORTS, 258 &wdr->ctl_ioh)) 259 goto fail; 260 } else { 261 wdr->ctl_iot = cfe->iospace[1].handle.iot; 262 wdr->ctl_ioh = cfe->iospace[1].handle.ioh; 263 } 264 } 265 266 for (i = 0; i < WDC_PCMCIA_REG_NPORTS; i++) { 267 if (bus_space_subregion(wdr->cmd_iot, 268 wdr->cmd_baseioh, 269 offset + i, i == 0 ? 4 : 1, 270 &wdr->cmd_iohs[i]) != 0) { 271 aprint_error_dev(self, "can't subregion I/O space\n"); 272 goto fail; 273 } 274 } 275 276 if (cfe->iftype == PCMCIA_IFTYPE_MEMORY) { 277 aprint_normal_dev(self, "memory mapped mode\n"); 278 wdr->data32iot = cfe->memspace[0].handle.memt; 279 if (bus_space_subregion(cfe->memspace[0].handle.memt, 280 cfe->memspace[0].handle.memh, offset + 1024, 1024, 281 &wdr->data32ioh)) 282 goto fail; 283 sc->sc_wdcdev.datain_pio = wdc_pcmcia_datain_memory; 284 sc->sc_wdcdev.dataout_pio = wdc_pcmcia_dataout_memory; 285 sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_NOIRQ; 286 } else { 287 aprint_normal_dev(self, "i/o mapped mode\n"); 288 wdr->data32iot = wdr->cmd_iot; 289 wdr->data32ioh = wdr->cmd_iohs[wd_data]; 290 sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA32; 291 } 292 293 sc->sc_wdcdev.sc_atac.atac_pio_cap = 0; 294 sc->wdc_chanlist[0] = &sc->ata_channel; 295 sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanlist; 296 sc->sc_wdcdev.sc_atac.atac_nchannels = 1; 297 sc->ata_channel.ch_channel = 0; 298 sc->ata_channel.ch_atac = &sc->sc_wdcdev.sc_atac; 299 sc->ata_channel.ch_queue = &sc->wdc_chqueue; 300 wdcp = pcmcia_product_lookup(pa, wdc_pcmcia_products, 301 wdc_pcmcia_nproducts, sizeof(wdc_pcmcia_products[0]), NULL); 302 sc->ata_channel.ch_ndrive = wdcp ? wdcp->wdc_ndrive : 2; 303 wdc_init_shadow_regs(&sc->ata_channel); 304 305 error = wdc_pcmcia_enable(self, 1); 306 if (error) 307 goto fail; 308 309 /* We can enable and disable the controller. */ 310 sc->sc_wdcdev.sc_atac.atac_atapi_adapter._generic.adapt_enable = 311 wdc_pcmcia_enable; 312 sc->sc_wdcdev.sc_atac.atac_atapi_adapter._generic.adapt_refcnt = 1; 313 314 /* 315 * Some devices needs some more delay after power up to stabilize 316 * and probe properly, so give them half a second. 317 * See PR 25659 for details. 318 */ 319 config_pending_incr(); 320 tsleep(wdc_pcmcia_attach, PWAIT, "wdcattach", hz / 2); 321 322 wdcattach(&sc->ata_channel); 323 324 if (!pmf_device_register(self, NULL, NULL)) 325 aprint_error_dev(self, "unable to establish power handler\n"); 326 327 config_pending_decr(); 328 ata_delref(&sc->ata_channel); 329 sc->sc_state = WDC_PCMCIA_ATTACHED; 330 return; 331 332fail: 333 pcmcia_function_unconfigure(pa->pf); 334} 335 336static int 337wdc_pcmcia_detach(device_t self, int flags) 338{ 339 struct wdc_pcmcia_softc *sc = device_private(self); 340 int error; 341 342 if (sc->sc_state != WDC_PCMCIA_ATTACHED) 343 return (0); 344 345 pmf_device_deregister(self); 346 347 if ((error = wdcdetach(self, flags)) != 0) 348 return (error); 349 350 pcmcia_function_unconfigure(sc->sc_pf); 351 352 return (0); 353} 354 355static int 356wdc_pcmcia_enable(device_t self, int onoff) 357{ 358 struct wdc_pcmcia_softc *sc = device_private(self); 359 int error; 360 361#if 1 362 /* 363 * XXX temporary kludge: we need to allow enabling while (cold) 364 * for some hpc* ports which attach pcmcia devices too early. 365 * This is problematic because pcmcia code uses tsleep() in 366 * the attach code path, but it seems to work somehow. 367 */ 368 if (doing_shutdown) 369 return (EIO); 370#else 371 if (cold || doing_shutdown) 372 return (EIO); 373#endif 374 375 if (onoff) { 376 /* Establish the interrupt handler. */ 377 sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO, 378 wdcintr, &sc->ata_channel); 379 if (!sc->sc_ih) 380 return (EIO); 381 382 error = pcmcia_function_enable(sc->sc_pf); 383 if (error) { 384 pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih); 385 sc->sc_ih = 0; 386 return (error); 387 } 388 } else { 389 pcmcia_function_disable(sc->sc_pf); 390 pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih); 391 sc->sc_ih = 0; 392 } 393 394 return (0); 395} 396 397static void 398wdc_pcmcia_datain_memory(struct ata_channel *chp, int flags, void *buf, 399 size_t len) 400{ 401 struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp); 402 403 while (len > 0) { 404 size_t n; 405 406 n = min(len, 1024); 407 if ((flags & DRIVE_CAP32) && (n & 3) == 0) 408 bus_space_read_region_stream_4(wdr->data32iot, 409 wdr->data32ioh, 0, buf, n >> 2); 410 else 411 bus_space_read_region_stream_2(wdr->data32iot, 412 wdr->data32ioh, 0, buf, n >> 1); 413 buf = (char *)buf + n; 414 len -= n; 415 } 416} 417 418static void 419wdc_pcmcia_dataout_memory(struct ata_channel *chp, int flags, void *buf, 420 size_t len) 421{ 422 struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp); 423 424 while (len > 0) { 425 size_t n; 426 427 n = min(len, 1024); 428 if ((flags & DRIVE_CAP32) && (n & 3) == 0) 429 bus_space_write_region_stream_4(wdr->data32iot, 430 wdr->data32ioh, 0, buf, n >> 2); 431 else 432 bus_space_write_region_stream_2(wdr->data32iot, 433 wdr->data32ioh, 0, buf, n >> 1); 434 buf = (char *)buf + n; 435 len -= n; 436 } 437} 438