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