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