2 * Copyright (c) 2017 Ilya Bakulin. All rights reserved.
3 * Copyright (c) 2018-2019 The FreeBSD Foundation
5 * Portions of this software were developed by Björn Zeeb
6 * under sponsorship from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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.
29 * Portions of this software may have been developed with reference to
30 * the SD Simplified Specification. The following disclaimer may apply:
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.
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.
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,
61 * The sdiob(4) parts effetively "translate" between the two worlds "bridging"
62 * messages from MMCCAM to newbus and back.
65 #include <sys/cdefs.h>
66 __FBSDID("$FreeBSD$");
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/types.h>
73 #include <sys/kernel.h>
75 #include <sys/endian.h>
77 #include <sys/malloc.h>
78 #include <sys/module.h>
79 #include <sys/mutex.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>
90 #include <dev/mmc/mmcreg.h>
92 #include <dev/sdio/sdiob.h>
93 #include <dev/sdio/sdio_subr.h>
98 #define DPRINTF(...) printf(__VA_ARGS__)
99 #define DPRINTFDEV(_dev, ...) device_printf((_dev), __VA_ARGS__)
102 #define DPRINTFDEV(_dev, ...)
107 #define SDIO_STATE_DEAD 0x0001
108 #define SDIO_STATE_INITIALIZING 0x0002
109 #define SDIO_STATE_READY 0x0004
111 #define NB_STATE_DEAD 0x0001
112 #define NB_STATE_SIM_ADDED 0x0002
113 #define NB_STATE_READY 0x0004
115 /* CAM side (including sim_dev). */
116 struct card_info cardinfo;
117 struct cam_periph *periph;
119 struct task discover_task;
122 device_t dev; /* Ourselves. */
126 /* -------------------------------------------------------------------------- */
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.
138 sdioerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
141 return (cam_periph_error(ccb, cam_flags, sense_flags));
144 /* CMD52: direct byte access. */
146 sdiob_rw_direct_sc(struct sdiob_softc *sc, uint8_t fn, uint32_t addr, bool wr,
152 KASSERT((val != NULL), ("%s val passed as NULL\n", __func__));
155 sc->ccb = xpt_alloc_ccb();
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));
163 flags = MMC_RSP_R5 | MMC_CMD_AC;
164 arg = SD_IO_RW_FUNC(fn) | SD_IO_RW_ADR(addr);
166 arg |= SD_IO_RW_WR | SD_IO_RW_RAW | SD_IO_RW_DAT(*val);
168 cam_fill_mmcio(&sc->ccb->mmcio,
171 /*flags*/ CAM_DIR_NONE,
172 /*mmc_opcode*/ SD_IO_RW_DIRECT,
176 /*timeout*/ sc->cardinfo.f[fn].timeout);
177 error = cam_periph_runccb(sc->ccb, sdioerror, CAM_FLAG_NONE, 0, NULL);
180 device_printf(sc->dev,
181 "%s: Failed to %s address %#10x error=%d\n",
182 __func__, (wr) ? "write" : "read", addr, error);
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));
190 /* TODO: Add handling of MMC errors */
191 /* ccb->mmcio.cmd.error ? */
193 *val = sc->ccb->mmcio.cmd.resp[0] & 0xff;
199 sdio_rw_direct(device_t dev, uint8_t fn, uint32_t addr, bool wr,
202 struct sdiob_softc *sc;
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);
213 sdiob_read_direct(device_t dev, uint8_t fn, uint32_t addr, uint8_t *val)
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)
226 sdiob_write_direct(device_t dev, uint8_t fn, uint32_t addr, uint8_t val)
229 return (sdio_rw_direct(dev, fn, addr, true, &val));
233 * CMD53: IO_RW_EXTENDED, read and write multiple I/O registers.
234 * Increment false gets FIFO mode (single register address).
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
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()!
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)
250 struct mmc_data mmcd;
251 uint32_t arg, cam_flags, flags, len;
255 sc->ccb = xpt_alloc_ccb();
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));
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",
279 arg = SD_IOE_RW_LEN(blksz);
284 /* Infinitive block transfer. */
288 len = b_count * blksz;
289 arg = SD_IOE_RW_BLK | SD_IOE_RW_LEN(b_count);
292 flags = MMC_RSP_R5 | MMC_CMD_ADTC;
293 arg |= SD_IOE_RW_FUNC(fn) | SD_IOE_RW_ADR(addr);
295 arg |= SD_IOE_RW_INCR;
297 memset(&mmcd, 0, sizeof(mmcd));
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;
308 cam_flags = CAM_DIR_OUT;
309 mmcd.flags = MMC_DATA_WRITE;
311 cam_flags = CAM_DIR_IN;
312 mmcd.flags = MMC_DATA_READ;
316 /* XXX-BZ TODO FIXME. Cancel I/O: CCCR -> ASx */
320 cam_fill_mmcio(&sc->ccb->mmcio,
324 /*mmc_opcode*/ SD_IO_RW_EXTENDED,
328 /*timeout*/ sc->cardinfo.f[fn].timeout);
329 if (arg & SD_IOE_RW_BLK) {
330 mmcd.flags |= MMC_DATA_BLOCK_SIZE;
332 sc->ccb->mmcio.cmd.data->flags |= MMC_DATA_MULTI;
336 error = cam_periph_runccb(sc->ccb, sdioerror, CAM_FLAG_NONE, 0, 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);
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));
355 /* TODO: Add handling of MMC errors */
356 /* ccb->mmcio.cmd.error ? */
357 error = sc->ccb->mmcio.cmd.resp[0] & 0xff;
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);
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));
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)
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.
389 while (sc->cardinfo.support_multiblk &&
390 size > 4 && size >= sc->cardinfo.f[fn].cur_blksize) {
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));
398 /* XXX support inifinite transfer with b_count = 0. */
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);
416 len = MIN(size, sc->cardinfo.f[fn].cur_blksize);
418 error = sdiob_rw_extended_cam(sc, fn, addr, wr, buffer, incaddr,
423 /* Prepare for next iteration. */
434 sdiob_rw_extended(device_t dev, uint8_t fn, uint32_t addr, bool wr,
435 uint32_t size, uint8_t *buffer, bool incaddr)
437 struct sdiob_softc *sc;
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);
448 sdiob_read_extended(device_t dev, uint8_t fn, uint32_t addr, uint32_t size,
449 uint8_t *buffer, bool incaddr)
452 return (sdiob_rw_extended(dev, fn, addr, false, size, buffer, incaddr));
456 sdiob_write_extended(device_t dev, uint8_t fn, uint32_t addr, uint32_t size,
457 uint8_t *buffer, bool incaddr)
460 return (sdiob_rw_extended(dev, fn, addr, true, size, buffer, incaddr));
463 /* -------------------------------------------------------------------------- */
464 /* Bus interface, ivars handling. */
467 sdiob_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
469 struct sdiob_softc *sc;
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));
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;
483 case SDIOB_IVAR_FUNCTION:
484 *result = (uintptr_t)f;
486 case SDIOB_IVAR_FUNCNUM:
489 case SDIOB_IVAR_CLASS:
492 case SDIOB_IVAR_VENDOR:
495 case SDIOB_IVAR_DEVICE:
498 case SDIOB_IVAR_DRVDATA:
499 *result = f->drvdata;
508 sdiob_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
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));
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:
535 /* -------------------------------------------------------------------------- */
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).
543 sdiob_probe(device_t dev)
546 device_set_desc(dev, "SDIO CAM-Newbus bridge");
547 return (BUS_PROBE_DEFAULT);
551 sdiob_attach(device_t dev)
553 struct sdiob_softc *sc;
556 sc = device_get_softc(dev);
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.
567 for (i = 0; i < sc->cardinfo.num_funcs; i++) {
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__);
574 sc->cardinfo.f[i].dev = sc->child[i];
576 /* Set the function as ivar to the child device. */
577 device_set_ivars(sc->child[i], &sc->cardinfo.f[i]);
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.
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",
594 sc->child, device_get_nameunit(sc->child[i]),
598 sc->nb_state = NB_STATE_READY;
600 cam_periph_lock(sc->periph);
601 xpt_announce_periph(sc->periph, NULL);
602 cam_periph_unlock(sc->periph);
608 sdiob_detach(device_t dev)
616 /* -------------------------------------------------------------------------- */
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
624 static device_method_t sdiob_methods[] = {
626 /* Device interface. */
627 DEVMETHOD(device_probe, sdiob_probe),
628 DEVMETHOD(device_attach, sdiob_attach),
629 DEVMETHOD(device_detach, sdiob_detach),
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),
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),
646 static devclass_t sdiob_devclass;
647 static driver_t sdiob_driver = {
654 /* -------------------------------------------------------------------------- */
657 * Read card and function information and populate the cardinfo structure.
661 sdio_read_direct_sc(struct sdiob_softc *sc, uint8_t fn, uint32_t addr,
667 error = sdiob_rw_direct_sc(sc, fn, addr, false, &v);
668 if (error == 0 && val != NULL)
674 sdio_func_read_cis(struct sdiob_softc *sc, uint8_t fn, uint32_t cis_addr)
676 char cis1_info_buf[256];
678 int start, i, count, ret;
680 uint8_t ch, tuple_id, tuple_len, tuple_count, v;
682 /* If we encounter any read errors, abort and return. */
683 #define ERR_OUT(ret) \
687 /* Use to prevent infinite loop in case of parse errors. */
689 memset(cis1_info_buf, 0, 256);
692 ret = sdio_read_direct_sc(sc, 0, addr++, &tuple_id);
694 if (tuple_id == SD_IO_CISTPL_END)
700 ret = sdio_read_direct_sc(sc, 0, addr++, &tuple_len);
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));
710 case SD_IO_CISTPL_VERS_1:
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);
716 DPRINTF("%s: count=%d, start=%d, i=%d, got "
717 "(%#02x)\n", __func__, count, start, i, ch);
720 cis1_info_buf[i] = ch;
723 cis1_info_buf + start;
728 DPRINTF("Card info: ");
729 for (i=0; i < 4; i++)
731 DPRINTF(" %s", cis1_info[i]);
734 case SD_IO_CISTPL_MANFID:
736 ret = sdio_read_direct_sc(sc, 0, addr++, &v);
738 sc->cardinfo.f[fn].vendor = v;
739 ret = sdio_read_direct_sc(sc, 0, addr++, &v);
741 sc->cardinfo.f[fn].vendor |= (v << 8);
743 ret = sdio_read_direct_sc(sc, 0, addr++, &v);
745 sc->cardinfo.f[fn].device = v;
746 ret = sdio_read_direct_sc(sc, 0, addr, &v);
748 sc->cardinfo.f[fn].device |= (v << 8);
750 case SD_IO_CISTPL_FUNCID:
751 /* Not sure if we need to parse it? */
753 case SD_IO_CISTPL_FUNCE:
755 printf("%s: FUNCE is too short: %d\n",
756 __func__, tuple_len);
759 /* TPLFE_TYPE (Extended Data) */
760 ret = sdio_read_direct_sc(sc, 0, addr++, &v);
770 ret = sdio_read_direct_sc(sc, 0, addr, &v);
772 sc->cardinfo.f[fn].max_blksize = v;
773 ret = sdio_read_direct_sc(sc, 0, addr+1, &v);
775 sc->cardinfo.f[fn].max_blksize |= (v << 8);
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));
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. */
788 cis_addr += 2 + tuple_len;
790 } while (tuple_count < 20);
797 sdio_get_common_cis_addr(struct sdiob_softc *sc, uint32_t *addr)
803 error = sdio_read_direct_sc(sc, 0, SD_IO_CCCR_CISPTR + 0, &val);
807 error = sdio_read_direct_sc(sc, 0, SD_IO_CCCR_CISPTR + 1, &val);
811 error = sdio_read_direct_sc(sc, 0, SD_IO_CCCR_CISPTR + 2, &val);
816 if (a < SD_IO_CIS_START || a > SD_IO_CIS_START + SD_IO_CIS_SIZE) {
818 CAM_DEBUG(sc->ccb->ccb_h.path, CAM_DEBUG_PERIPH,
819 ("%s: bad CIS address: %#04x, error %d\n", __func__, a,
821 } else if (error == 0 && addr != NULL)
828 sdiob_get_card_info(struct sdiob_softc *sc)
830 struct mmc_params *mmcp;
831 uint32_t cis_addr, fbr_addr;
835 error = sdio_get_common_cis_addr(sc, &cis_addr);
839 memset(&sc->cardinfo, 0, sizeof(sc->cardinfo));
841 /* F0 must always be present. */
843 error = sdio_func_read_cis(sc, fn, cis_addr);
846 sc->cardinfo.num_funcs++;
847 /* Read CCCR Card Capability. */
848 error = sdio_read_direct_sc(sc, 0, SD_IO_CCCR_CARDCAP, &val);
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");
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++) {
863 fbr_addr = SD_IO_FBR_START * fn + SD_IO_FBR_CIS_OFFSET;
865 error = sdio_read_direct_sc(sc, 0, fbr_addr++, &val);
869 error = sdio_read_direct_sc(sc, 0, fbr_addr++, &val);
872 cis_addr |= (val << 8);
873 error = sdio_read_direct_sc(sc, 0, fbr_addr++, &val);
876 cis_addr |= (val << 16);
878 error = sdio_func_read_cis(sc, fn, cis_addr);
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);
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);
892 sc->cardinfo.f[fn].class = val;
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;
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);
909 sc->cardinfo.num_funcs++;
915 /* -------------------------------------------------------------------------- */
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).
925 sdio_newbus_sim_add(struct sdiob_softc *sc)
928 devclass_t bus_devclass;
931 /* Add ourselves to our parent (SIM) device. */
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));
938 sc->dev = BUS_ADD_CHILD(sc->periph->sim->sim_dev, 0,
942 device_set_softc(sc->dev, sc);
944 * Don't set description here; devclass_add_driver() ->
945 * device_probe_child() -> device_set_driver() will nuke it again.
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));
959 error = devclass_add_driver(bus_devclass, &sdiob_driver,
960 BUS_PASS_DEFAULT, &sdiob_devclass);
963 printf("%s: Failed to add driver to devclass: %d.\n",
969 sc->nb_state = NB_STATE_SIM_ADDED;
975 sdiobdiscover(void *context, int pending)
977 struct cam_periph *periph;
978 struct sdiob_softc *sc;
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__));
985 /* Periph was held for us when this task was enqueued. */
986 if ((periph->flags & CAM_PERIPH_INVALID) != 0) {
987 cam_periph_release(periph);
992 sc->sdio_state = SDIO_STATE_INITIALIZING;
995 sc->ccb = xpt_alloc_ccb();
997 memset(sc->ccb, 0, sizeof(*sc->ccb));
998 xpt_setup_ccb(&sc->ccb->ccb_h, periph->path, CAM_PRIORITY_NONE);
1001 * Read CCCR and FBR of each function, get manufacturer and device IDs,
1002 * max block size, and whatever else we deem necessary.
1004 cam_periph_lock(periph);
1005 error = sdiob_get_card_info(sc);
1007 sc->sdio_state = SDIO_STATE_READY;
1009 sc->sdio_state = SDIO_STATE_DEAD;
1010 cam_periph_unlock(periph);
1015 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("%s: num_func %d\n",
1016 __func__, sc->cardinfo.num_funcs));
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.
1023 error = sdio_newbus_sim_add(sc);
1025 sc->nb_state = NB_STATE_DEAD;
1030 /* Called at the end of cam_periph_alloc() for us to finish allocation. */
1032 sdiobregister(struct cam_periph *periph, void *arg)
1034 struct sdiob_softc *sc;
1037 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("%s: arg %p\n", __func__, arg));
1039 printf("%s: no getdev CCB, can't register device pariph %p\n",
1041 return(CAM_REQ_CMP_ERR);
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);
1049 sc = (struct sdiob_softc *) malloc(sizeof(*sc), M_DEVBUF,
1052 printf("%s: unable to allocate sc\n", __func__);
1053 return (CAM_REQ_CMP_ERR);
1055 sc->sdio_state = SDIO_STATE_DEAD;
1056 sc->nb_state = NB_STATE_DEAD;
1057 TASK_INIT(&sc->discover_task, 0, sdiobdiscover, periph);
1059 /* Refcount until we are setup. Can't block. */
1060 error = cam_periph_hold(periph, PRIBIO);
1062 printf("%s: lost periph during registration!\n", __func__);
1064 return(CAM_REQ_CMP_ERR);
1067 sc->periph = periph;
1068 cam_periph_unlock(periph);
1070 error = taskqueue_enqueue(taskqueue_thread, &sc->discover_task);
1072 cam_periph_lock(periph);
1073 /* We will continue to hold a refcount for discover_task. */
1074 /* cam_periph_unhold(periph); */
1076 xpt_schedule(periph, CAM_PRIORITY_XPT);
1078 return (CAM_REQ_CMP);
1082 sdioboninvalidate(struct cam_periph *periph)
1085 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("%s:\n", __func__));
1091 sdiobcleanup(struct cam_periph *periph)
1094 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("%s:\n", __func__));
1100 sdiobstart(struct cam_periph *periph, union ccb *ccb)
1103 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("%s: ccb %p\n", __func__, ccb));
1109 sdiobasync(void *softc, uint32_t code, struct cam_path *path, void *arg)
1111 struct cam_periph *periph;
1112 struct ccb_getdev *cgd;
1115 periph = (struct cam_periph *)softc;
1117 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("%s(code=%d)\n", __func__, code));
1119 case AC_FOUND_DEVICE:
1122 cgd = (struct ccb_getdev *)arg;
1123 if (cgd->protocol != PROTO_MMCSD)
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"));
1135 * Allocate a peripheral instance for this device which starts
1136 * the probe process.
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));
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);
1160 * Register for new device notification. We will be notified for all
1161 * already existing ones.
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",
1169 /* This function will allow unloading the KLD. */
1174 return (EOPNOTSUPP);
1177 static struct periph_driver sdiobdriver =
1180 .driver_name = SDIOB_NAME_S,
1181 .units = TAILQ_HEAD_INITIALIZER(sdiobdriver.units),
1184 .deinit = sdiobdeinit,
1187 PERIPHDRIVER_DECLARE(SDIOB_NAME, sdiobdriver);
1188 MODULE_VERSION(SDIOB_NAME, 1);