1 /*- 2 * Copyright (c) 2017 Ilya Bakulin. All rights reserved. 3 * Copyright (c) 2018-2019 The FreeBSD Foundation 4 * 5 * Portions of this software were developed by Björn Zeeb 6 * under sponsorship from the FreeBSD Foundation. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * 29 * Portions of this software may have been developed with reference to 30 * the SD Simplified Specification. The following disclaimer may apply: 31 * 32 * The following conditions apply to the release of the simplified 33 * specification ("Simplified Specification") by the SD Card Association and 34 * the SD Group. The Simplified Specification is a subset of the complete SD 35 * Specification which is owned by the SD Card Association and the SD 36 * Group. This Simplified Specification is provided on a non-confidential 37 * basis subject to the disclaimers below. Any implementation of the 38 * Simplified Specification may require a license from the SD Card 39 * Association, SD Group, SD-3C LLC or other third parties. 40 * 41 * Disclaimers: 42 * 43 * The information contained in the Simplified Specification is presented only 44 * as a standard specification for SD Cards and SD Host/Ancillary products and 45 * is provided "AS-IS" without any representations or warranties of any 46 * kind. No responsibility is assumed by the SD Group, SD-3C LLC or the SD 47 * Card Association for any damages, any infringements of patents or other 48 * right of the SD Group, SD-3C LLC, the SD Card Association or any third 49 * parties, which may result from its use. No license is granted by 50 * implication, estoppel or otherwise under any patent or other rights of the 51 * SD Group, SD-3C LLC, the SD Card Association or any third party. Nothing 52 * herein shall be construed as an obligation by the SD Group, the SD-3C LLC 53 * or the SD Card Association to disclose or distribute any technical 54 * information, know-how or other confidential information to any third party. 55 */ 56 /* 57 * Implements the (kernel specific) SDIO parts. 58 * This will hide all cam(4) functionality from the SDIO driver implementations 59 * which will just be newbus/device(9) and hence look like any other driver for, 60 * e.g., PCI. 61 * The sdiob(4) parts effetively "translate" between the two worlds "bridging" 62 * messages from MMCCAM to newbus and back. 63 */ 64 65 #include <sys/param.h> 66 #include <sys/systm.h> 67 #include <sys/types.h> 68 #include <sys/kernel.h> 69 #include <sys/bus.h> 70 #include <sys/endian.h> 71 #include <sys/lock.h> 72 #include <sys/malloc.h> 73 #include <sys/module.h> 74 #include <sys/mutex.h> 75 76 #include <cam/cam.h> 77 #include <cam/cam_ccb.h> 78 #include <cam/cam_queue.h> 79 #include <cam/cam_periph.h> 80 #include <cam/cam_xpt.h> 81 #include <cam/cam_xpt_periph.h> 82 #include <cam/cam_xpt_internal.h> /* for cam_path */ 83 #include <cam/cam_debug.h> 84 85 #include <dev/mmc/mmcreg.h> 86 87 #include <dev/sdio/sdiob.h> 88 #include <dev/sdio/sdio_subr.h> 89 90 #include "sdio_if.h" 91 92 #ifdef DEBUG 93 #define DPRINTF(...) printf(__VA_ARGS__) 94 #define DPRINTFDEV(_dev, ...) device_printf((_dev), __VA_ARGS__) 95 #else 96 #define DPRINTF(...) 97 #define DPRINTFDEV(_dev, ...) 98 #endif 99 100 struct sdiob_softc { 101 uint32_t sdio_state; 102 #define SDIO_STATE_DEAD 0x0001 103 #define SDIO_STATE_INITIALIZING 0x0002 104 #define SDIO_STATE_READY 0x0004 105 uint32_t nb_state; 106 #define NB_STATE_DEAD 0x0001 107 #define NB_STATE_SIM_ADDED 0x0002 108 #define NB_STATE_READY 0x0004 109 110 /* CAM side. */ 111 struct card_info cardinfo; 112 struct cam_periph *periph; 113 union ccb *ccb; 114 struct task discover_task; 115 116 /* Newbus side. */ 117 device_t dev; /* Ourselves. */ 118 device_t child[8]; 119 }; 120 121 /* -------------------------------------------------------------------------- */ 122 /* 123 * SDIO CMD52 and CM53 implementations along with wrapper functions for 124 * read/write and a CAM periph helper function. 125 * These are the backend implementations of the sdio_if.m framework talking 126 * through CAM to sdhci. 127 * Note: these functions are also called during early discovery stage when 128 * we are not a device(9) yet. Hence they cannot always use device_printf() 129 * to log errors and have to call CAM_DEBUG() during these early stages. 130 */ 131 132 static int 133 sdioerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 134 { 135 136 return (cam_periph_error(ccb, cam_flags, sense_flags)); 137 } 138 139 /* CMD52: direct byte access. */ 140 static int 141 sdiob_rw_direct_sc(struct sdiob_softc *sc, uint8_t fn, uint32_t addr, bool wr, 142 uint8_t *val) 143 { 144 uint32_t arg, flags; 145 int error; 146 147 KASSERT((val != NULL), ("%s val passed as NULL\n", __func__)); 148 149 if (sc->ccb == NULL) 150 sc->ccb = xpt_alloc_ccb(); 151 else 152 memset(sc->ccb, 0, sizeof(*sc->ccb)); 153 xpt_setup_ccb(&sc->ccb->ccb_h, sc->periph->path, CAM_PRIORITY_NONE); 154 CAM_DEBUG(sc->ccb->ccb_h.path, CAM_DEBUG_TRACE, 155 ("%s(fn=%d, addr=%#02x, wr=%d, *val=%#02x)\n", __func__, 156 fn, addr, wr, *val)); 157 158 flags = MMC_RSP_R5 | MMC_CMD_AC; 159 arg = SD_IO_RW_FUNC(fn) | SD_IO_RW_ADR(addr); 160 if (wr) 161 arg |= SD_IO_RW_WR | SD_IO_RW_RAW | SD_IO_RW_DAT(*val); 162 163 cam_fill_mmcio(&sc->ccb->mmcio, 164 /*retries*/ 0, 165 /*cbfcnp*/ NULL, 166 /*flags*/ CAM_DIR_NONE, 167 /*mmc_opcode*/ SD_IO_RW_DIRECT, 168 /*mmc_arg*/ arg, 169 /*mmc_flags*/ flags, 170 /*mmc_data*/ 0, 171 /*timeout*/ sc->cardinfo.f[fn].timeout); 172 error = cam_periph_runccb(sc->ccb, sdioerror, CAM_FLAG_NONE, 0, NULL); 173 if (error != 0) { 174 if (sc->dev != NULL) 175 device_printf(sc->dev, 176 "%s: Failed to %s address %#10x error=%d\n", 177 __func__, (wr) ? "write" : "read", addr, error); 178 else 179 CAM_DEBUG(sc->ccb->ccb_h.path, CAM_DEBUG_INFO, 180 ("%s: Failed to %s address: %#10x error=%d\n", 181 __func__, (wr) ? "write" : "read", addr, error)); 182 return (error); 183 } 184 185 /* TODO: Add handling of MMC errors */ 186 /* ccb->mmcio.cmd.error ? */ 187 if (wr == false) 188 *val = sc->ccb->mmcio.cmd.resp[0] & 0xff; 189 190 return (0); 191 } 192 193 static int 194 sdio_rw_direct(device_t dev, uint8_t fn, uint32_t addr, bool wr, 195 uint8_t *val) 196 { 197 struct sdiob_softc *sc; 198 int error; 199 200 sc = device_get_softc(dev); 201 cam_periph_lock(sc->periph); 202 error = sdiob_rw_direct_sc(sc, fn, addr, wr, val); 203 cam_periph_unlock(sc->periph); 204 return (error); 205 } 206 207 static int 208 sdiob_read_direct(device_t dev, uint8_t fn, uint32_t addr, uint8_t *val) 209 { 210 int error; 211 uint8_t v; 212 213 error = sdio_rw_direct(dev, fn, addr, false, &v); 214 /* Be polite and do not touch the value on read error. */ 215 if (error == 0 && val != NULL) 216 *val = v; 217 return (error); 218 } 219 220 static int 221 sdiob_write_direct(device_t dev, uint8_t fn, uint32_t addr, uint8_t val) 222 { 223 224 return (sdio_rw_direct(dev, fn, addr, true, &val)); 225 } 226 227 /* 228 * CMD53: IO_RW_EXTENDED, read and write multiple I/O registers. 229 * Increment false gets FIFO mode (single register address). 230 */ 231 /* 232 * A b_count of 0 means byte mode, b_count > 0 gets block mode. 233 * A b_count of >= 512 would mean infinitive block transfer, which would become 234 * b_count = 0, is not yet supported. 235 * For b_count == 0, blksz is the len of bytes, otherwise it is the amount of 236 * full sized blocks (you must not round the blocks up and leave the last one 237 * partial!) 238 * For byte mode, the maximum of blksz is the functions cur_blksize. 239 * This function should ever only be called by sdio_rw_extended_sc()! 240 */ 241 static int 242 sdiob_rw_extended_cam(struct sdiob_softc *sc, uint8_t fn, uint32_t addr, 243 bool wr, uint8_t *buffer, bool incaddr, uint32_t b_count, uint16_t blksz) 244 { 245 struct mmc_data mmcd; 246 uint32_t arg, cam_flags, flags, len; 247 int error; 248 249 if (sc->ccb == NULL) 250 sc->ccb = xpt_alloc_ccb(); 251 else 252 memset(sc->ccb, 0, sizeof(*sc->ccb)); 253 xpt_setup_ccb(&sc->ccb->ccb_h, sc->periph->path, CAM_PRIORITY_NONE); 254 CAM_DEBUG(sc->ccb->ccb_h.path, CAM_DEBUG_TRACE, 255 ("%s(fn=%d addr=%#0x wr=%d b_count=%u blksz=%u buf=%p incr=%d)\n", 256 __func__, fn, addr, wr, b_count, blksz, buffer, incaddr)); 257 258 KASSERT((b_count <= 511), ("%s: infinitive block transfer not yet " 259 "supported: b_count %u blksz %u, sc %p, fn %u, addr %#10x, %s, " 260 "buffer %p, %s\n", __func__, b_count, blksz, sc, fn, addr, 261 wr ? "wr" : "rd", buffer, incaddr ? "incaddr" : "fifo")); 262 /* Blksz needs to be within bounds for both byte and block mode! */ 263 KASSERT((blksz <= sc->cardinfo.f[fn].cur_blksize), ("%s: blksz " 264 "%u > bur_blksize %u, sc %p, fn %u, addr %#10x, %s, " 265 "buffer %p, %s, b_count %u\n", __func__, blksz, 266 sc->cardinfo.f[fn].cur_blksize, sc, fn, addr, 267 wr ? "wr" : "rd", buffer, incaddr ? "incaddr" : "fifo", 268 b_count)); 269 if (b_count == 0) { 270 /* Byte mode */ 271 len = blksz; 272 if (blksz == 512) 273 blksz = 0; 274 arg = SD_IOE_RW_LEN(blksz); 275 } else { 276 /* Block mode. */ 277 #ifdef __notyet__ 278 if (b_count > 511) { 279 /* Infinitive block transfer. */ 280 b_count = 0; 281 } 282 #endif 283 len = b_count * blksz; 284 arg = SD_IOE_RW_BLK | SD_IOE_RW_LEN(b_count); 285 } 286 287 flags = MMC_RSP_R5 | MMC_CMD_ADTC; 288 arg |= SD_IOE_RW_FUNC(fn) | SD_IOE_RW_ADR(addr); 289 if (incaddr) 290 arg |= SD_IOE_RW_INCR; 291 292 memset(&mmcd, 0, sizeof(mmcd)); 293 mmcd.data = buffer; 294 mmcd.len = len; 295 if (arg & SD_IOE_RW_BLK) { 296 /* XXX both should be known from elsewhere, aren't they? */ 297 mmcd.block_size = blksz; 298 mmcd.block_count = b_count; 299 } 300 301 if (wr) { 302 arg |= SD_IOE_RW_WR; 303 cam_flags = CAM_DIR_OUT; 304 mmcd.flags = MMC_DATA_WRITE; 305 } else { 306 cam_flags = CAM_DIR_IN; 307 mmcd.flags = MMC_DATA_READ; 308 } 309 #ifdef __notyet__ 310 if (b_count == 0) { 311 /* XXX-BZ TODO FIXME. Cancel I/O: CCCR -> ASx */ 312 /* Stop cmd. */ 313 } 314 #endif 315 cam_fill_mmcio(&sc->ccb->mmcio, 316 /*retries*/ 0, 317 /*cbfcnp*/ NULL, 318 /*flags*/ cam_flags, 319 /*mmc_opcode*/ SD_IO_RW_EXTENDED, 320 /*mmc_arg*/ arg, 321 /*mmc_flags*/ flags, 322 /*mmc_data*/ &mmcd, 323 /*timeout*/ sc->cardinfo.f[fn].timeout); 324 if (arg & SD_IOE_RW_BLK) { 325 mmcd.flags |= MMC_DATA_BLOCK_SIZE; 326 if (b_count != 1) 327 sc->ccb->mmcio.cmd.data->flags |= MMC_DATA_MULTI; 328 } 329 330 /* Execute. */ 331 error = cam_periph_runccb(sc->ccb, sdioerror, CAM_FLAG_NONE, 0, NULL); 332 if (error != 0) { 333 if (sc->dev != NULL) 334 device_printf(sc->dev, 335 "%s: Failed to %s address %#10x buffer %p size %u " 336 "%s b_count %u blksz %u error=%d\n", 337 __func__, (wr) ? "write to" : "read from", addr, 338 buffer, len, (incaddr) ? "incr" : "fifo", 339 b_count, blksz, error); 340 else 341 CAM_DEBUG(sc->ccb->ccb_h.path, CAM_DEBUG_INFO, 342 ("%s: Failed to %s address %#10x buffer %p size %u " 343 "%s b_count %u blksz %u error=%d\n", 344 __func__, (wr) ? "write to" : "read from", addr, 345 buffer, len, (incaddr) ? "incr" : "fifo", 346 b_count, blksz, error)); 347 return (error); 348 } 349 350 /* TODO: Add handling of MMC errors */ 351 /* ccb->mmcio.cmd.error ? */ 352 error = sc->ccb->mmcio.cmd.resp[0] & 0xff; 353 if (error != 0) { 354 if (sc->dev != NULL) 355 device_printf(sc->dev, 356 "%s: Failed to %s address %#10x buffer %p size %u " 357 "%s b_count %u blksz %u mmcio resp error=%d\n", 358 __func__, (wr) ? "write to" : "read from", addr, 359 buffer, len, (incaddr) ? "incr" : "fifo", 360 b_count, blksz, error); 361 else 362 CAM_DEBUG(sc->ccb->ccb_h.path, CAM_DEBUG_INFO, 363 ("%s: Failed to %s address %#10x buffer %p size %u " 364 "%s b_count %u blksz %u mmcio resp error=%d\n", 365 __func__, (wr) ? "write to" : "read from", addr, 366 buffer, len, (incaddr) ? "incr" : "fifo", 367 b_count, blksz, error)); 368 } 369 return (error); 370 } 371 372 static int 373 sdiob_rw_extended_sc(struct sdiob_softc *sc, uint8_t fn, uint32_t addr, 374 bool wr, uint32_t size, uint8_t *buffer, bool incaddr) 375 { 376 int error; 377 uint32_t len; 378 uint32_t b_count; 379 380 /* 381 * If block mode is supported and we have at least 4 bytes to write and 382 * the size is at least one block, then start doing blk transfers. 383 */ 384 while (sc->cardinfo.support_multiblk && 385 size > 4 && size >= sc->cardinfo.f[fn].cur_blksize) { 386 b_count = size / sc->cardinfo.f[fn].cur_blksize; 387 KASSERT(b_count >= 1, ("%s: block count too small %u size %u " 388 "cur_blksize %u\n", __func__, b_count, size, 389 sc->cardinfo.f[fn].cur_blksize)); 390 391 #ifdef __notyet__ 392 /* XXX support inifinite transfer with b_count = 0. */ 393 #else 394 if (b_count > 511) 395 b_count = 511; 396 #endif 397 len = b_count * sc->cardinfo.f[fn].cur_blksize; 398 error = sdiob_rw_extended_cam(sc, fn, addr, wr, buffer, incaddr, 399 b_count, sc->cardinfo.f[fn].cur_blksize); 400 if (error != 0) 401 return (error); 402 403 size -= len; 404 buffer += len; 405 if (incaddr) 406 addr += len; 407 } 408 409 while (size > 0) { 410 len = MIN(size, sc->cardinfo.f[fn].cur_blksize); 411 412 error = sdiob_rw_extended_cam(sc, fn, addr, wr, buffer, incaddr, 413 0, len); 414 if (error != 0) 415 return (error); 416 417 /* Prepare for next iteration. */ 418 size -= len; 419 buffer += len; 420 if (incaddr) 421 addr += len; 422 } 423 424 return (0); 425 } 426 427 static int 428 sdiob_rw_extended(device_t dev, uint8_t fn, uint32_t addr, bool wr, 429 uint32_t size, uint8_t *buffer, bool incaddr) 430 { 431 struct sdiob_softc *sc; 432 int error; 433 434 sc = device_get_softc(dev); 435 cam_periph_lock(sc->periph); 436 error = sdiob_rw_extended_sc(sc, fn, addr, wr, size, buffer, incaddr); 437 cam_periph_unlock(sc->periph); 438 return (error); 439 } 440 441 static int 442 sdiob_read_extended(device_t dev, uint8_t fn, uint32_t addr, uint32_t size, 443 uint8_t *buffer, bool incaddr) 444 { 445 446 return (sdiob_rw_extended(dev, fn, addr, false, size, buffer, incaddr)); 447 } 448 449 static int 450 sdiob_write_extended(device_t dev, uint8_t fn, uint32_t addr, uint32_t size, 451 uint8_t *buffer, bool incaddr) 452 { 453 454 return (sdiob_rw_extended(dev, fn, addr, true, size, buffer, incaddr)); 455 } 456 457 /* -------------------------------------------------------------------------- */ 458 /* Bus interface, ivars handling. */ 459 460 static int 461 sdiob_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 462 { 463 struct sdiob_softc *sc; 464 struct sdio_func *f; 465 466 f = device_get_ivars(child); 467 KASSERT(f != NULL, ("%s: dev %p child %p which %d, child ivars NULL\n", 468 __func__, dev, child, which)); 469 470 switch (which) { 471 case SDIOB_IVAR_SUPPORT_MULTIBLK: 472 sc = device_get_softc(dev); 473 KASSERT(sc != NULL, ("%s: dev %p child %p which %d, sc NULL\n", 474 __func__, dev, child, which)); 475 *result = sc->cardinfo.support_multiblk; 476 break; 477 case SDIOB_IVAR_FUNCTION: 478 *result = (uintptr_t)f; 479 break; 480 case SDIOB_IVAR_FUNCNUM: 481 *result = f->fn; 482 break; 483 case SDIOB_IVAR_CLASS: 484 *result = f->class; 485 break; 486 case SDIOB_IVAR_VENDOR: 487 *result = f->vendor; 488 break; 489 case SDIOB_IVAR_DEVICE: 490 *result = f->device; 491 break; 492 case SDIOB_IVAR_DRVDATA: 493 *result = f->drvdata; 494 break; 495 default: 496 return (ENOENT); 497 } 498 return (0); 499 } 500 501 static int 502 sdiob_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 503 { 504 struct sdio_func *f; 505 506 f = device_get_ivars(child); 507 KASSERT(f != NULL, ("%s: dev %p child %p which %d, child ivars NULL\n", 508 __func__, dev, child, which)); 509 510 switch (which) { 511 case SDIOB_IVAR_SUPPORT_MULTIBLK: 512 case SDIOB_IVAR_FUNCTION: 513 case SDIOB_IVAR_FUNCNUM: 514 case SDIOB_IVAR_CLASS: 515 case SDIOB_IVAR_VENDOR: 516 case SDIOB_IVAR_DEVICE: 517 return (EINVAL); /* Disallowed. */ 518 case SDIOB_IVAR_DRVDATA: 519 f->drvdata = value; 520 break; 521 default: 522 return (ENOENT); 523 } 524 525 return (0); 526 } 527 528 /* -------------------------------------------------------------------------- */ 529 /* 530 * Newbus functions for ourselves to probe/attach/detach and become a proper 531 * device(9). Attach will also probe for child devices (another driver 532 * implementing SDIO). 533 */ 534 535 static int 536 sdiob_probe(device_t dev) 537 { 538 539 device_set_desc(dev, "SDIO CAM-Newbus bridge"); 540 return (BUS_PROBE_DEFAULT); 541 } 542 543 static int 544 sdiob_attach(device_t dev) 545 { 546 struct sdiob_softc *sc; 547 int error, i; 548 549 sc = device_get_softc(dev); 550 if (sc == NULL) 551 return (ENXIO); 552 553 /* 554 * Now that we are a dev, create one child device per function, 555 * initialize the backpointer, so we can pass them around and 556 * call CAM operations on the parent, and also set the function 557 * itself as ivars, so that we can query/update them. 558 * Do this before any child gets a chance to attach. 559 */ 560 for (i = 0; i < sc->cardinfo.num_funcs; i++) { 561 sc->child[i] = device_add_child(dev, NULL, -1); 562 if (sc->child[i] == NULL) { 563 device_printf(dev, "%s: failed to add child\n", __func__); 564 return (ENXIO); 565 } 566 sc->cardinfo.f[i].dev = sc->child[i]; 567 568 /* Set the function as ivar to the child device. */ 569 device_set_ivars(sc->child[i], &sc->cardinfo.f[i]); 570 } 571 572 /* 573 * No one will ever attach to F0; we do the above to have a "device" 574 * to talk to in a general way in the code. 575 * Also do the probe/attach in a 2nd loop, so that all devices are 576 * present as we do have drivers consuming more than one device/func 577 * and might play "tricks" in order to do that assuming devices and 578 * ivars are available for all. 579 */ 580 for (i = 1; i < sc->cardinfo.num_funcs; i++) { 581 error = device_probe_and_attach(sc->child[i]); 582 if (error != 0 && bootverbose) 583 device_printf(dev, "%s: device_probe_and_attach(%p %s) " 584 "failed %d for function %d, no child yet\n", 585 __func__, 586 sc->child, device_get_nameunit(sc->child[i]), 587 error, i); 588 } 589 590 sc->nb_state = NB_STATE_READY; 591 592 cam_periph_lock(sc->periph); 593 xpt_announce_periph(sc->periph, NULL); 594 cam_periph_unlock(sc->periph); 595 596 return (0); 597 } 598 599 static int 600 sdiob_detach(device_t dev) 601 { 602 603 /* XXX TODO? */ 604 return (EOPNOTSUPP); 605 } 606 607 /* -------------------------------------------------------------------------- */ 608 /* 609 * driver(9) and device(9) "control plane". 610 * This is what we use when we are making ourselves a device(9) in order to 611 * provide a newbus interface again, as well as the implementation of the 612 * SDIO interface. 613 */ 614 615 static device_method_t sdiob_methods[] = { 616 /* Device interface. */ 617 DEVMETHOD(device_probe, sdiob_probe), 618 DEVMETHOD(device_attach, sdiob_attach), 619 DEVMETHOD(device_detach, sdiob_detach), 620 621 /* Bus interface. */ 622 DEVMETHOD(bus_add_child, bus_generic_add_child), 623 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 624 DEVMETHOD(bus_read_ivar, sdiob_read_ivar), 625 DEVMETHOD(bus_write_ivar, sdiob_write_ivar), 626 627 /* SDIO interface. */ 628 DEVMETHOD(sdio_read_direct, sdiob_read_direct), 629 DEVMETHOD(sdio_write_direct, sdiob_write_direct), 630 DEVMETHOD(sdio_read_extended, sdiob_read_extended), 631 DEVMETHOD(sdio_write_extended, sdiob_write_extended), 632 633 DEVMETHOD_END 634 }; 635 636 static driver_t sdiob_driver = { 637 SDIOB_NAME_S, 638 sdiob_methods, 639 0 640 }; 641 642 /* -------------------------------------------------------------------------- */ 643 /* 644 * CIS related. 645 * Read card and function information and populate the cardinfo structure. 646 */ 647 648 static int 649 sdio_read_direct_sc(struct sdiob_softc *sc, uint8_t fn, uint32_t addr, 650 uint8_t *val) 651 { 652 int error; 653 uint8_t v; 654 655 error = sdiob_rw_direct_sc(sc, fn, addr, false, &v); 656 if (error == 0 && val != NULL) 657 *val = v; 658 return (error); 659 } 660 661 static int 662 sdio_func_read_cis(struct sdiob_softc *sc, uint8_t fn, uint32_t cis_addr) 663 { 664 char cis1_info_buf[256]; 665 char *cis1_info[4]; 666 int start, i, count, ret; 667 uint32_t addr; 668 uint8_t ch, tuple_id, tuple_len, tuple_count, v; 669 670 /* If we encounter any read errors, abort and return. */ 671 #define ERR_OUT(ret) \ 672 if (ret != 0) \ 673 goto err; 674 ret = 0; 675 /* Use to prevent infinite loop in case of parse errors. */ 676 tuple_count = 0; 677 memset(cis1_info_buf, 0, 256); 678 do { 679 addr = cis_addr; 680 ret = sdio_read_direct_sc(sc, 0, addr++, &tuple_id); 681 ERR_OUT(ret); 682 if (tuple_id == SD_IO_CISTPL_END) 683 break; 684 if (tuple_id == 0) { 685 cis_addr++; 686 continue; 687 } 688 ret = sdio_read_direct_sc(sc, 0, addr++, &tuple_len); 689 ERR_OUT(ret); 690 if (tuple_len == 0) { 691 CAM_DEBUG(sc->ccb->ccb_h.path, CAM_DEBUG_PERIPH, 692 ("%s: parse error: 0-length tuple %#02x\n", 693 __func__, tuple_id)); 694 return (EIO); 695 } 696 697 switch (tuple_id) { 698 case SD_IO_CISTPL_VERS_1: 699 addr += 2; 700 for (count = 0, start = 0, i = 0; 701 (count < 4) && ((i + 4) < 256); i++) { 702 ret = sdio_read_direct_sc(sc, 0, addr + i, &ch); 703 ERR_OUT(ret); 704 DPRINTF("%s: count=%d, start=%d, i=%d, got " 705 "(%#02x)\n", __func__, count, start, i, ch); 706 if (ch == 0xff) 707 break; 708 cis1_info_buf[i] = ch; 709 if (ch == 0) { 710 cis1_info[count] = 711 cis1_info_buf + start; 712 start = i + 1; 713 count++; 714 } 715 } 716 DPRINTF("Card info: "); 717 for (i=0; i < 4; i++) 718 if (cis1_info[i]) 719 DPRINTF(" %s", cis1_info[i]); 720 DPRINTF("\n"); 721 break; 722 case SD_IO_CISTPL_MANFID: 723 /* TPLMID_MANF */ 724 ret = sdio_read_direct_sc(sc, 0, addr++, &v); 725 ERR_OUT(ret); 726 sc->cardinfo.f[fn].vendor = v; 727 ret = sdio_read_direct_sc(sc, 0, addr++, &v); 728 ERR_OUT(ret); 729 sc->cardinfo.f[fn].vendor |= (v << 8); 730 /* TPLMID_CARD */ 731 ret = sdio_read_direct_sc(sc, 0, addr++, &v); 732 ERR_OUT(ret); 733 sc->cardinfo.f[fn].device = v; 734 ret = sdio_read_direct_sc(sc, 0, addr, &v); 735 ERR_OUT(ret); 736 sc->cardinfo.f[fn].device |= (v << 8); 737 break; 738 case SD_IO_CISTPL_FUNCID: 739 /* Not sure if we need to parse it? */ 740 break; 741 case SD_IO_CISTPL_FUNCE: 742 if (tuple_len < 4) { 743 printf("%s: FUNCE is too short: %d\n", 744 __func__, tuple_len); 745 break; 746 } 747 /* TPLFE_TYPE (Extended Data) */ 748 ret = sdio_read_direct_sc(sc, 0, addr++, &v); 749 ERR_OUT(ret); 750 if (fn == 0) { 751 if (v != 0x00) 752 break; 753 } else { 754 if (v != 0x01) 755 break; 756 addr += 0x0b; 757 } 758 ret = sdio_read_direct_sc(sc, 0, addr, &v); 759 ERR_OUT(ret); 760 sc->cardinfo.f[fn].max_blksize = v; 761 ret = sdio_read_direct_sc(sc, 0, addr+1, &v); 762 ERR_OUT(ret); 763 sc->cardinfo.f[fn].max_blksize |= (v << 8); 764 break; 765 default: 766 CAM_DEBUG(sc->ccb->ccb_h.path, CAM_DEBUG_PERIPH, 767 ("%s: Skipping fn %d tuple %d ID %#02x " 768 "len %#02x\n", __func__, fn, tuple_count, 769 tuple_id, tuple_len)); 770 } 771 if (tuple_len == 0xff) { 772 /* Also marks the end of a tuple chain (E1 16.2) */ 773 /* The tuple is valid, hence this going at the end. */ 774 break; 775 } 776 cis_addr += 2 + tuple_len; 777 tuple_count++; 778 } while (tuple_count < 20); 779 err: 780 #undef ERR_OUT 781 return (ret); 782 } 783 784 static int 785 sdio_get_common_cis_addr(struct sdiob_softc *sc, uint32_t *addr) 786 { 787 int error; 788 uint32_t a; 789 uint8_t val; 790 791 error = sdio_read_direct_sc(sc, 0, SD_IO_CCCR_CISPTR + 0, &val); 792 if (error != 0) 793 goto err; 794 a = val; 795 error = sdio_read_direct_sc(sc, 0, SD_IO_CCCR_CISPTR + 1, &val); 796 if (error != 0) 797 goto err; 798 a |= (val << 8); 799 error = sdio_read_direct_sc(sc, 0, SD_IO_CCCR_CISPTR + 2, &val); 800 if (error != 0) 801 goto err; 802 a |= (val << 16); 803 804 if (a < SD_IO_CIS_START || a > SD_IO_CIS_START + SD_IO_CIS_SIZE) { 805 err: 806 CAM_DEBUG(sc->ccb->ccb_h.path, CAM_DEBUG_PERIPH, 807 ("%s: bad CIS address: %#04x, error %d\n", __func__, a, 808 error)); 809 } else if (error == 0 && addr != NULL) 810 *addr = a; 811 812 return (error); 813 } 814 815 static int 816 sdiob_get_card_info(struct sdiob_softc *sc) 817 { 818 struct mmc_params *mmcp; 819 uint32_t cis_addr, fbr_addr; 820 int fn, error; 821 uint8_t fn_max, val; 822 823 error = sdio_get_common_cis_addr(sc, &cis_addr); 824 if (error != 0) 825 return (-1); 826 827 memset(&sc->cardinfo, 0, sizeof(sc->cardinfo)); 828 829 /* F0 must always be present. */ 830 fn = 0; 831 error = sdio_func_read_cis(sc, fn, cis_addr); 832 if (error != 0) 833 return (error); 834 sc->cardinfo.num_funcs++; 835 /* Read CCCR Card Capability. */ 836 error = sdio_read_direct_sc(sc, 0, SD_IO_CCCR_CARDCAP, &val); 837 if (error != 0) 838 return (error); 839 sc->cardinfo.support_multiblk = (val & CCCR_CC_SMB) ? true : false; 840 DPRINTF("%s: F%d: Vendor %#04x product %#04x max block size %d bytes " 841 "support_multiblk %s\n", 842 __func__, fn, sc->cardinfo.f[fn].vendor, sc->cardinfo.f[fn].device, 843 sc->cardinfo.f[fn].max_blksize, 844 sc->cardinfo.support_multiblk ? "yes" : "no"); 845 846 /* mmcp->sdio_func_count contains the number of functions w/o F0. */ 847 mmcp = &sc->ccb->ccb_h.path->device->mmc_ident_data; 848 fn_max = MIN(mmcp->sdio_func_count + 1, nitems(sc->cardinfo.f)); 849 for (fn = 1; fn < fn_max; fn++) { 850 fbr_addr = SD_IO_FBR_START * fn + SD_IO_FBR_CIS_OFFSET; 851 852 error = sdio_read_direct_sc(sc, 0, fbr_addr++, &val); 853 if (error != 0) 854 break; 855 cis_addr = val; 856 error = sdio_read_direct_sc(sc, 0, fbr_addr++, &val); 857 if (error != 0) 858 break; 859 cis_addr |= (val << 8); 860 error = sdio_read_direct_sc(sc, 0, fbr_addr++, &val); 861 if (error != 0) 862 break; 863 cis_addr |= (val << 16); 864 865 error = sdio_func_read_cis(sc, fn, cis_addr); 866 if (error != 0) 867 break; 868 869 /* Read the Standard SDIO Function Interface Code. */ 870 fbr_addr = SD_IO_FBR_START * fn; 871 error = sdio_read_direct_sc(sc, 0, fbr_addr++, &val); 872 if (error != 0) 873 break; 874 sc->cardinfo.f[fn].class = (val & 0x0f); 875 if (sc->cardinfo.f[fn].class == 0x0f) { 876 error = sdio_read_direct_sc(sc, 0, fbr_addr, &val); 877 if (error != 0) 878 break; 879 sc->cardinfo.f[fn].class = val; 880 } 881 882 sc->cardinfo.f[fn].fn = fn; 883 sc->cardinfo.f[fn].cur_blksize = sc->cardinfo.f[fn].max_blksize; 884 sc->cardinfo.f[fn].retries = 0; 885 sc->cardinfo.f[fn].timeout = 5000; 886 887 DPRINTF("%s: F%d: Class %d Vendor %#04x product %#04x " 888 "max_blksize %d bytes\n", __func__, fn, 889 sc->cardinfo.f[fn].class, 890 sc->cardinfo.f[fn].vendor, sc->cardinfo.f[fn].device, 891 sc->cardinfo.f[fn].max_blksize); 892 if (sc->cardinfo.f[fn].vendor == 0) { 893 DPRINTF("%s: F%d doesn't exist\n", __func__, fn); 894 break; 895 } 896 sc->cardinfo.num_funcs++; 897 } 898 return (error); 899 } 900 901 /* -------------------------------------------------------------------------- */ 902 /* 903 * CAM periph registration, allocation, and detached from that a discovery 904 * task, which goes off reads cardinfo, and then adds ourselves to our SIM's 905 * device adding the devclass and registering the driver. This keeps the 906 * newbus chain connected though we will talk CAM in the middle (until one 907 * day CAM might be newbusyfied). 908 */ 909 910 static int 911 sdio_newbus_sim_add(struct sdiob_softc *sc) 912 { 913 device_t pdev; 914 devclass_t bus_devclass; 915 int error; 916 917 /* Add ourselves to our parent (SIM) device. */ 918 919 /* Add ourselves to our parent. That way we can become a parent. */ 920 pdev = xpt_path_sim_device(sc->periph->path); 921 KASSERT(pdev != NULL, 922 ("%s: pdev is NULL, sc %p periph %p sim %p\n", 923 __func__, sc, sc->periph, sc->periph->sim)); 924 925 if (sc->dev == NULL) 926 sc->dev = BUS_ADD_CHILD(pdev, 0, SDIOB_NAME_S, -1); 927 if (sc->dev == NULL) 928 return (ENXIO); 929 device_set_softc(sc->dev, sc); 930 931 /* 932 * Don't set description here; devclass_add_driver() -> 933 * device_probe_child() -> device_set_driver() will nuke it again. 934 */ 935 bus_devclass = device_get_devclass(pdev); 936 if (bus_devclass == NULL) { 937 printf("%s: Failed to get devclass from %s.\n", __func__, 938 device_get_nameunit(pdev)); 939 return (ENXIO); 940 } 941 942 bus_topo_lock(); 943 error = devclass_add_driver(bus_devclass, &sdiob_driver, 944 BUS_PASS_DEFAULT, NULL); 945 bus_topo_unlock(); 946 if (error != 0) { 947 printf("%s: Failed to add driver to devclass: %d.\n", 948 __func__, error); 949 return (error); 950 } 951 952 /* Done. */ 953 sc->nb_state = NB_STATE_SIM_ADDED; 954 955 return (0); 956 } 957 958 static void 959 sdiobdiscover(void *context, int pending) 960 { 961 struct cam_periph *periph; 962 struct sdiob_softc *sc; 963 int error; 964 965 KASSERT(context != NULL, ("%s: context is NULL\n", __func__)); 966 periph = (struct cam_periph *)context; 967 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("%s\n", __func__)); 968 969 /* Periph was held for us when this task was enqueued. */ 970 if ((periph->flags & CAM_PERIPH_INVALID) != 0) { 971 cam_periph_release(periph); 972 return; 973 } 974 975 sc = periph->softc; 976 sc->sdio_state = SDIO_STATE_INITIALIZING; 977 978 if (sc->ccb == NULL) 979 sc->ccb = xpt_alloc_ccb(); 980 else 981 memset(sc->ccb, 0, sizeof(*sc->ccb)); 982 xpt_setup_ccb(&sc->ccb->ccb_h, periph->path, CAM_PRIORITY_NONE); 983 984 /* 985 * Read CCCR and FBR of each function, get manufacturer and device IDs, 986 * max block size, and whatever else we deem necessary. 987 */ 988 cam_periph_lock(periph); 989 error = sdiob_get_card_info(sc); 990 if (error == 0) 991 sc->sdio_state = SDIO_STATE_READY; 992 else 993 sc->sdio_state = SDIO_STATE_DEAD; 994 cam_periph_unlock(periph); 995 996 if (error) 997 return; 998 999 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("%s: num_func %d\n", 1000 __func__, sc->cardinfo.num_funcs)); 1001 1002 /* 1003 * Now CAM portion of the driver has been initialized and 1004 * we know VID/PID of all the functions on the card. 1005 * Time to hook into the newbus. 1006 */ 1007 error = sdio_newbus_sim_add(sc); 1008 if (error != 0) 1009 sc->nb_state = NB_STATE_DEAD; 1010 1011 return; 1012 } 1013 1014 /* Called at the end of cam_periph_alloc() for us to finish allocation. */ 1015 static cam_status 1016 sdiobregister(struct cam_periph *periph, void *arg) 1017 { 1018 struct sdiob_softc *sc; 1019 int error; 1020 1021 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("%s: arg %p\n", __func__, arg)); 1022 if (arg == NULL) { 1023 printf("%s: no getdev CCB, can't register device pariph %p\n", 1024 __func__, periph); 1025 return(CAM_REQ_CMP_ERR); 1026 } 1027 if (xpt_path_sim_device(periph->path) == NULL) { 1028 printf("%s: no device_t for sim %p\n", __func__, periph->sim); 1029 return(CAM_REQ_CMP_ERR); 1030 } 1031 1032 sc = (struct sdiob_softc *) malloc(sizeof(*sc), M_DEVBUF, 1033 M_NOWAIT|M_ZERO); 1034 if (sc == NULL) { 1035 printf("%s: unable to allocate sc\n", __func__); 1036 return (CAM_REQ_CMP_ERR); 1037 } 1038 sc->sdio_state = SDIO_STATE_DEAD; 1039 sc->nb_state = NB_STATE_DEAD; 1040 TASK_INIT(&sc->discover_task, 0, sdiobdiscover, periph); 1041 1042 /* Refcount until we are setup. Can't block. */ 1043 error = cam_periph_hold(periph, PRIBIO); 1044 if (error != 0) { 1045 printf("%s: lost periph during registration!\n", __func__); 1046 free(sc, M_DEVBUF); 1047 return(CAM_REQ_CMP_ERR); 1048 } 1049 periph->softc = sc; 1050 sc->periph = periph; 1051 cam_periph_unlock(periph); 1052 1053 error = taskqueue_enqueue(taskqueue_thread, &sc->discover_task); 1054 1055 cam_periph_lock(periph); 1056 /* We will continue to hold a refcount for discover_task. */ 1057 /* cam_periph_unhold(periph); */ 1058 1059 xpt_schedule(periph, CAM_PRIORITY_XPT); 1060 1061 return (CAM_REQ_CMP); 1062 } 1063 1064 static void 1065 sdioboninvalidate(struct cam_periph *periph) 1066 { 1067 1068 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("%s:\n", __func__)); 1069 1070 return; 1071 } 1072 1073 static void 1074 sdiobcleanup(struct cam_periph *periph) 1075 { 1076 1077 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("%s:\n", __func__)); 1078 1079 return; 1080 } 1081 1082 static void 1083 sdiobstart(struct cam_periph *periph, union ccb *ccb) 1084 { 1085 1086 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("%s: ccb %p\n", __func__, ccb)); 1087 1088 return; 1089 } 1090 1091 static void 1092 sdiobasync(void *softc, uint32_t code, struct cam_path *path, void *arg) 1093 { 1094 struct cam_periph *periph; 1095 struct ccb_getdev *cgd; 1096 cam_status status; 1097 1098 periph = (struct cam_periph *)softc; 1099 1100 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("%s(code=%d)\n", __func__, code)); 1101 switch (code) { 1102 case AC_FOUND_DEVICE: 1103 if (arg == NULL) 1104 break; 1105 cgd = (struct ccb_getdev *)arg; 1106 if (cgd->protocol != PROTO_MMCSD) 1107 break; 1108 1109 /* We do not support SD memory (Combo) Cards. */ 1110 if ((path->device->mmc_ident_data.card_features & 1111 CARD_FEATURE_MEMORY)) { 1112 CAM_DEBUG(path, CAM_DEBUG_TRACE, 1113 ("Memory card, not interested\n")); 1114 break; 1115 } 1116 1117 /* 1118 * Allocate a peripheral instance for this device which starts 1119 * the probe process. 1120 */ 1121 status = cam_periph_alloc(sdiobregister, sdioboninvalidate, 1122 sdiobcleanup, sdiobstart, SDIOB_NAME_S, CAM_PERIPH_BIO, path, 1123 sdiobasync, AC_FOUND_DEVICE, cgd); 1124 if (status != CAM_REQ_CMP && status != CAM_REQ_INPROG) 1125 CAM_DEBUG(path, CAM_DEBUG_PERIPH, 1126 ("%s: Unable to attach to new device due to " 1127 "status %#02x\n", __func__, status)); 1128 break; 1129 default: 1130 CAM_DEBUG(path, CAM_DEBUG_PERIPH, 1131 ("%s: cannot handle async code %#02x\n", __func__, code)); 1132 cam_periph_async(periph, code, path, arg); 1133 break; 1134 } 1135 } 1136 1137 static void 1138 sdiobinit(void) 1139 { 1140 cam_status status; 1141 1142 /* 1143 * Register for new device notification. We will be notified for all 1144 * already existing ones. 1145 */ 1146 status = xpt_register_async(AC_FOUND_DEVICE, sdiobasync, NULL, NULL); 1147 if (status != CAM_REQ_CMP) 1148 printf("%s: Failed to attach async callback, statux %#02x", 1149 __func__, status); 1150 } 1151 1152 /* This function will allow unloading the KLD. */ 1153 static int 1154 sdiobdeinit(void) 1155 { 1156 1157 return (EOPNOTSUPP); 1158 } 1159 1160 static struct periph_driver sdiobdriver = 1161 { 1162 .init = sdiobinit, 1163 .driver_name = SDIOB_NAME_S, 1164 .units = TAILQ_HEAD_INITIALIZER(sdiobdriver.units), 1165 .generation = 0, 1166 .flags = 0, 1167 .deinit = sdiobdeinit, 1168 }; 1169 1170 PERIPHDRIVER_DECLARE(SDIOB_NAME, sdiobdriver); 1171 MODULE_VERSION(SDIOB_NAME, 1); 1172