1 /* $OpenBSD: wdc_pcmcia.c,v 1.33 2021/03/07 06:20:09 jsg Exp $ */ 2 /* $NetBSD: wdc_pcmcia.c,v 1.19 1999/02/19 21:49:43 abs Exp $ */ 3 4 /*- 5 * Copyright (c) 1998 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Charles M. Hannum, by Onno van der Linden and by Manuel Bouyer. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/kernel.h> 36 #include <sys/conf.h> 37 #include <sys/stat.h> 38 #include <sys/ioctl.h> 39 #include <sys/buf.h> 40 #include <sys/uio.h> 41 #include <sys/malloc.h> 42 #include <sys/device.h> 43 #include <sys/disklabel.h> 44 #include <sys/disk.h> 45 #include <sys/syslog.h> 46 47 #include <machine/cpu.h> 48 #include <machine/intr.h> 49 #include <machine/bus.h> 50 51 #include <dev/pcmcia/pcmciareg.h> 52 #include <dev/pcmcia/pcmciavar.h> 53 #include <dev/pcmcia/pcmciadevs.h> 54 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 channel_softc *wdc_chanptr; 65 struct channel_softc wdc_channel; 66 struct pcmcia_io_handle sc_pioh; 67 struct pcmcia_io_handle sc_auxpioh; 68 int sc_iowindow; 69 int sc_auxiowindow; 70 void *sc_ih; 71 struct pcmcia_function *sc_pf; 72 int sc_flags; 73 #define WDC_PCMCIA_ATTACH 0x0001 74 }; 75 76 static int wdc_pcmcia_match(struct device *, void *, void *); 77 static void wdc_pcmcia_attach(struct device *, struct device *, void *); 78 int wdc_pcmcia_detach(struct device *, int); 79 int wdc_pcmcia_activate(struct device *, int); 80 81 struct cfattach wdc_pcmcia_ca = { 82 sizeof(struct wdc_pcmcia_softc), wdc_pcmcia_match, wdc_pcmcia_attach, 83 wdc_pcmcia_detach, wdc_pcmcia_activate 84 }; 85 86 struct wdc_pcmcia_product { 87 u_int16_t wpp_vendor; /* vendor ID */ 88 u_int16_t wpp_product; /* product ID */ 89 int wpp_quirk_flag; /* Quirk flags */ 90 #define WDC_PCMCIA_FORCE_16BIT_IO 0x01 /* Don't use PCMCIA_WIDTH_AUTO */ 91 #define WDC_PCMCIA_NO_EXTRA_RESETS 0x02 /* Only reset ctrl once */ 92 const char *wpp_cis_info[4]; /* XXX necessary? */ 93 } wdc_pcmcia_pr[] = { 94 95 { /* PCMCIA_VENDOR_DIGITAL XXX */ 0x0100, 96 PCMCIA_PRODUCT_DIGITAL_MOBILE_MEDIA_CDROM, 97 0, { NULL, "Digital Mobile Media CD-ROM", NULL, NULL }, }, 98 99 { PCMCIA_VENDOR_IBM, PCMCIA_PRODUCT_IBM_PORTABLE_CDROM, 100 0, { NULL, "Portable CD-ROM Drive", NULL, NULL }, }, 101 102 { PCMCIA_VENDOR_HAGIWARASYSCOM, PCMCIA_PRODUCT_INVALID, /* XXX */ 103 WDC_PCMCIA_FORCE_16BIT_IO, { NULL, NULL, NULL, NULL }, }, 104 105 /* The TEAC IDE/Card II is used on the Sony Vaio */ 106 { PCMCIA_VENDOR_TEAC, PCMCIA_PRODUCT_TEAC_IDECARDII, 107 WDC_PCMCIA_NO_EXTRA_RESETS, PCMCIA_CIS_TEAC_IDECARDII }, 108 109 /* 110 * EXP IDE/ATAPI DVD Card use with some DVD players. 111 * Does not have a vendor ID or product ID. 112 */ 113 { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID, 114 0, PCMCIA_CIS_EXP_EXPMULTIMEDIA }, 115 116 /* Mobile Dock 2, which doesn't have vendor ID nor product ID */ 117 { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID, 118 0, PCMCIA_CIS_SHUTTLE_IDE_ATAPI }, 119 120 /* Archos MiniCD */ 121 { PCMCIA_VENDOR_ARCHOS, PCMCIA_PRODUCT_ARCHOS_ARC_ATAPI, 122 0, PCMCIA_CIS_ARCHOS_ARC_ATAPI }, 123 }; 124 125 struct wdc_pcmcia_disk_device_interface_args { 126 int ddi_type; /* interface type */ 127 int ddi_reqfn; /* function we are requesting iftype */ 128 int ddi_curfn; /* function we are currently parsing in CIS */ 129 }; 130 131 int wdc_pcmcia_disk_device_interface_callback(struct pcmcia_tuple *, 132 void *); 133 int wdc_pcmcia_disk_device_interface(struct pcmcia_function *); 134 struct wdc_pcmcia_product * 135 wdc_pcmcia_lookup(struct pcmcia_attach_args *); 136 137 int 138 wdc_pcmcia_disk_device_interface_callback(struct pcmcia_tuple *tuple, void *arg) 139 { 140 struct wdc_pcmcia_disk_device_interface_args *ddi = arg; 141 142 switch (tuple->code) { 143 case PCMCIA_CISTPL_FUNCID: 144 ddi->ddi_curfn++; 145 break; 146 147 case PCMCIA_CISTPL_FUNCE: 148 if (ddi->ddi_reqfn != ddi->ddi_curfn) 149 break; 150 151 /* subcode (disk device interface), data (interface type) */ 152 if (tuple->length < 2) 153 break; 154 155 /* check type */ 156 if (pcmcia_tuple_read_1(tuple, 0) != 157 PCMCIA_TPLFE_TYPE_DISK_DEVICE_INTERFACE) 158 break; 159 160 ddi->ddi_type = pcmcia_tuple_read_1(tuple, 1); 161 return (1); 162 } 163 return (0); 164 } 165 166 int 167 wdc_pcmcia_disk_device_interface(struct pcmcia_function *pf) 168 { 169 struct wdc_pcmcia_disk_device_interface_args ddi; 170 171 ddi.ddi_reqfn = pf->number; 172 ddi.ddi_curfn = -1; 173 if (pcmcia_scan_cis((struct device *)pf->sc, 174 wdc_pcmcia_disk_device_interface_callback, &ddi) > 0) 175 return (ddi.ddi_type); 176 else 177 return (-1); 178 } 179 180 struct wdc_pcmcia_product * 181 wdc_pcmcia_lookup(struct pcmcia_attach_args *pa) 182 { 183 struct wdc_pcmcia_product *wpp; 184 int i, cis_match; 185 186 for (wpp = wdc_pcmcia_pr; 187 wpp < &wdc_pcmcia_pr[nitems(wdc_pcmcia_pr)]; 188 wpp++) 189 if ((wpp->wpp_vendor == PCMCIA_VENDOR_INVALID || 190 pa->manufacturer == wpp->wpp_vendor) && 191 (wpp->wpp_product == PCMCIA_PRODUCT_INVALID || 192 pa->product == wpp->wpp_product)) { 193 cis_match = 1; 194 for (i = 0; i < 4; i++) { 195 if (!(wpp->wpp_cis_info[i] == NULL || 196 (pa->card->cis1_info[i] != NULL && 197 strcmp(pa->card->cis1_info[i], 198 wpp->wpp_cis_info[i]) == 0))) 199 cis_match = 0; 200 } 201 if (cis_match) 202 return (wpp); 203 } 204 205 return (NULL); 206 } 207 208 static int 209 wdc_pcmcia_match(struct device *parent, void *match, void *aux) 210 { 211 struct pcmcia_attach_args *pa = aux; 212 struct pcmcia_softc *sc; 213 int iftype; 214 215 if (wdc_pcmcia_lookup(pa) != NULL) 216 return (1); 217 218 if (pa->pf->function == PCMCIA_FUNCTION_DISK) { 219 sc = pa->pf->sc; 220 221 pcmcia_chip_socket_enable(sc->pct, sc->pch); 222 iftype = wdc_pcmcia_disk_device_interface(pa->pf); 223 pcmcia_chip_socket_disable(sc->pct, sc->pch); 224 225 if (iftype == PCMCIA_TPLFE_DDI_PCCARD_ATA) 226 return (1); 227 } 228 229 return (0); 230 } 231 232 static void 233 wdc_pcmcia_attach(struct device *parent, struct device *self, void *aux) 234 { 235 struct wdc_pcmcia_softc *sc = (void *)self; 236 struct pcmcia_attach_args *pa = aux; 237 struct pcmcia_config_entry *cfe; 238 struct wdc_pcmcia_product *wpp; 239 const char *intrstr; 240 int quirks; 241 242 sc->sc_pf = pa->pf; 243 244 for (cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head); cfe != NULL; 245 cfe = SIMPLEQ_NEXT(cfe, cfe_list)) { 246 if (cfe->num_iospace != 1 && cfe->num_iospace != 2) 247 continue; 248 249 if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start, 250 cfe->iospace[0].length, 251 cfe->iospace[0].start == 0 ? cfe->iospace[0].length : 0, 252 &sc->sc_pioh)) 253 continue; 254 255 if (cfe->num_iospace == 2) { 256 if (!pcmcia_io_alloc(pa->pf, cfe->iospace[1].start, 257 cfe->iospace[1].length, 0, &sc->sc_auxpioh)) 258 break; 259 } else /* num_iospace == 1 */ { 260 sc->sc_auxpioh.iot = sc->sc_pioh.iot; 261 if (!bus_space_subregion(sc->sc_pioh.iot, 262 sc->sc_pioh.ioh, WDC_PCMCIA_AUXREG_OFFSET, 263 WDC_PCMCIA_AUXREG_NPORTS, &sc->sc_auxpioh.ioh)) 264 break; 265 } 266 pcmcia_io_free(pa->pf, &sc->sc_pioh); 267 } 268 269 if (cfe == NULL) { 270 printf(": can't handle card info\n"); 271 goto no_config_entry; 272 } 273 274 /* Enable the card. */ 275 pcmcia_function_init(pa->pf, cfe); 276 if (pcmcia_function_enable(pa->pf)) { 277 printf(": function enable failed\n"); 278 goto enable_failed; 279 } 280 281 /* 282 * XXX DEC Mobile Media CDROM is not yet tested whether it works 283 * XXX with PCMCIA_WIDTH_IO16. HAGIWARA SYS-COM HPC-CF32 doesn't 284 * XXX work with PCMCIA_WIDTH_AUTO. 285 * XXX CANON FC-8M (SANDISK SDCFB 8M) works for both _AUTO and IO16. 286 * XXX So, here is temporary work around. 287 */ 288 wpp = wdc_pcmcia_lookup(pa); 289 if (wpp != NULL) 290 quirks = wpp->wpp_quirk_flag; 291 else 292 quirks = 0; 293 294 if (pcmcia_io_map(pa->pf, quirks & WDC_PCMCIA_FORCE_16BIT_IO ? 295 PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_AUTO, 0, 296 sc->sc_pioh.size, &sc->sc_pioh, &sc->sc_iowindow)) { 297 printf(": can't map first i/o space\n"); 298 goto iomap_failed; 299 } 300 301 /* 302 * Currently, # of iospace is 1 except DIGITAL Mobile Media CD-ROM. 303 * So whether the work around like above is necessary or not 304 * is unknown. XXX. 305 */ 306 if (cfe->num_iospace <= 1) 307 sc->sc_auxiowindow = -1; 308 else if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, 0, 309 sc->sc_auxpioh.size, &sc->sc_auxpioh, &sc->sc_auxiowindow)) { 310 printf(": can't map second i/o space\n"); 311 goto iomapaux_failed; 312 } 313 314 printf(" port 0x%lx/%lu", 315 sc->sc_pioh.addr, (u_long)sc->sc_pioh.size); 316 if (cfe->num_iospace > 1 && sc->sc_auxpioh.size > 0) 317 printf(",0x%lx/%lu", 318 sc->sc_auxpioh.addr, (u_long)sc->sc_auxpioh.size); 319 320 sc->wdc_channel.cmd_iot = sc->sc_pioh.iot; 321 sc->wdc_channel.cmd_ioh = sc->sc_pioh.ioh; 322 sc->wdc_channel.ctl_iot = sc->sc_auxpioh.iot; 323 sc->wdc_channel.ctl_ioh = sc->sc_auxpioh.ioh; 324 sc->wdc_channel.data32iot = sc->wdc_channel.cmd_iot; 325 sc->wdc_channel.data32ioh = sc->wdc_channel.cmd_ioh; 326 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32; 327 sc->sc_wdcdev.PIO_cap = 0; 328 sc->wdc_chanptr = &sc->wdc_channel; 329 sc->sc_wdcdev.channels = &sc->wdc_chanptr; 330 sc->sc_wdcdev.nchannels = 1; 331 sc->wdc_channel.channel = 0; 332 sc->wdc_channel.wdc = &sc->sc_wdcdev; 333 sc->wdc_channel.ch_queue = wdc_alloc_queue(); 334 if (sc->wdc_channel.ch_queue == NULL) { 335 printf("cannot allocate channel queue\n"); 336 goto ch_queue_alloc_failed; 337 } 338 if (quirks & WDC_PCMCIA_NO_EXTRA_RESETS) 339 sc->sc_wdcdev.cap |= WDC_CAPABILITY_NO_EXTRA_RESETS; 340 341 /* Establish the interrupt handler. */ 342 sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO, wdcintr, 343 &sc->wdc_channel, sc->sc_wdcdev.sc_dev.dv_xname); 344 intrstr = pcmcia_intr_string(sc->sc_pf, sc->sc_ih); 345 if (*intrstr) 346 printf(": %s", intrstr); 347 348 printf("\n"); 349 350 sc->sc_flags |= WDC_PCMCIA_ATTACH; 351 wdcattach(&sc->wdc_channel); 352 wdc_print_current_modes(&sc->wdc_channel); 353 sc->sc_flags &= ~WDC_PCMCIA_ATTACH; 354 return; 355 356 ch_queue_alloc_failed: 357 /* Unmap our aux i/o window. */ 358 if (sc->sc_auxiowindow != -1) 359 pcmcia_io_unmap(sc->sc_pf, sc->sc_auxiowindow); 360 361 iomapaux_failed: 362 /* Unmap our i/o window. */ 363 pcmcia_io_unmap(sc->sc_pf, sc->sc_iowindow); 364 365 iomap_failed: 366 /* Disable the function */ 367 pcmcia_function_disable(sc->sc_pf); 368 369 enable_failed: 370 /* Unmap our i/o space. */ 371 pcmcia_io_free(sc->sc_pf, &sc->sc_pioh); 372 if (cfe->num_iospace == 2) 373 pcmcia_io_free(sc->sc_pf, &sc->sc_auxpioh); 374 375 no_config_entry: 376 sc->sc_iowindow = -1; 377 } 378 379 int 380 wdc_pcmcia_detach(struct device *self, int flags) 381 { 382 struct wdc_pcmcia_softc *sc = (struct wdc_pcmcia_softc *)self; 383 int error; 384 385 if (sc->sc_iowindow == -1) 386 /* Nothing to detach */ 387 return (0); 388 389 if ((error = wdcdetach(&sc->wdc_channel, flags)) != 0) 390 return (error); 391 392 if (sc->wdc_channel.ch_queue != NULL) 393 wdc_free_queue(sc->wdc_channel.ch_queue); 394 395 /* Unmap our i/o window and i/o space. */ 396 pcmcia_io_unmap(sc->sc_pf, sc->sc_iowindow); 397 pcmcia_io_free(sc->sc_pf, &sc->sc_pioh); 398 if (sc->sc_auxiowindow != -1) { 399 pcmcia_io_unmap(sc->sc_pf, sc->sc_auxiowindow); 400 pcmcia_io_free(sc->sc_pf, &sc->sc_auxpioh); 401 } 402 403 return (0); 404 } 405 406 int 407 wdc_pcmcia_activate(struct device *self, int act) 408 { 409 struct wdc_pcmcia_softc *sc = (struct wdc_pcmcia_softc *)self; 410 int rv = 0; 411 412 if (sc->sc_iowindow == -1) 413 /* Nothing to activate/deactivate. */ 414 return (0); 415 416 switch (act) { 417 case DVACT_DEACTIVATE: 418 rv = config_activate_children(self, act); 419 if (sc->sc_ih) 420 pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih); 421 sc->sc_ih = NULL; 422 pcmcia_function_disable(sc->sc_pf); 423 break; 424 case DVACT_RESUME: 425 pcmcia_function_enable(sc->sc_pf); 426 sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO, 427 wdcintr, &sc->wdc_channel, sc->sc_wdcdev.sc_dev.dv_xname); 428 wdcreset(&sc->wdc_channel, VERBOSE); 429 rv = config_activate_children(self, act); 430 break; 431 case DVACT_POWERDOWN: 432 rv = config_activate_children(self, act); 433 if (sc->sc_ih) 434 pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih); 435 sc->sc_ih = NULL; 436 pcmcia_function_disable(sc->sc_pf); 437 break; 438 default: 439 rv = config_activate_children(self, act); 440 break; 441 } 442 return (rv); 443 } 444