2 * Copyright (c) 2013,2014 Ilya Bakulin <ilya@bakulin.de>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/endian.h>
33 #include <sys/systm.h>
34 #include <sys/types.h>
35 #include <sys/malloc.h>
36 #include <sys/kernel.h>
39 #include <sys/fcntl.h>
40 #include <sys/interrupt.h>
44 #include <sys/mutex.h>
45 #include <sys/sysctl.h>
46 #include <sys/condvar.h>
49 #include <cam/cam_ccb.h>
50 #include <cam/cam_queue.h>
51 #include <cam/cam_periph.h>
52 #include <cam/cam_sim.h>
53 #include <cam/cam_xpt.h>
54 #include <cam/cam_xpt_sim.h>
55 #include <cam/cam_xpt_periph.h>
56 #include <cam/cam_xpt_internal.h>
57 #include <cam/cam_debug.h>
59 #include <cam/mmc/mmc.h>
60 #include <cam/mmc/mmc_bus.h>
62 #include <machine/stdarg.h> /* for xpt_print below */
63 #include <machine/_inttypes.h> /* for PRIu64 */
66 FEATURE(mmccam, "CAM-based MMC/SD/SDIO stack");
68 static struct cam_ed * mmc_alloc_device(struct cam_eb *bus,
69 struct cam_et *target, lun_id_t lun_id);
70 static void mmc_dev_async(u_int32_t async_code, struct cam_eb *bus,
71 struct cam_et *target, struct cam_ed *device, void *async_arg);
72 static void mmc_action(union ccb *start_ccb);
73 static void mmc_dev_advinfo(union ccb *start_ccb);
74 static void mmc_announce_periph(struct cam_periph *periph);
75 static void mmc_scan_lun(struct cam_periph *periph,
76 struct cam_path *path, cam_flags flags, union ccb *ccb);
78 /* mmcprobe methods */
79 static cam_status mmcprobe_register(struct cam_periph *periph, void *arg);
80 static void mmcprobe_start(struct cam_periph *periph, union ccb *start_ccb);
81 static void mmcprobe_cleanup(struct cam_periph *periph);
82 static void mmcprobe_done(struct cam_periph *periph, union ccb *done_ccb);
84 static void mmc_proto_announce(struct cam_ed *device);
85 static void mmc_proto_denounce(struct cam_ed *device);
86 static void mmc_proto_debug_out(union ccb *ccb);
95 PROBE_SEND_APP_OP_COND,
98 PROBE_SEND_RELATIVE_ADDR,
104 static char *probe_action_text[] = {
108 "PROBE_SEND_IF_COND",
111 "PROBE_SEND_APP_OP_COND",
114 "PROBE_SEND_RELATIVE_ADDR",
120 #define PROBE_SET_ACTION(softc, newaction) \
123 text = probe_action_text; \
124 CAM_DEBUG((softc)->periph->path, CAM_DEBUG_PROBE, \
125 ("Probe %s to %s\n", text[(softc)->action], \
126 text[(newaction)])); \
127 (softc)->action = (newaction); \
130 static struct xpt_xport_ops mmc_xport_ops = {
131 .alloc_device = mmc_alloc_device,
132 .action = mmc_action,
133 .async = mmc_dev_async,
134 .announce = mmc_announce_periph,
137 #define MMC_XPT_XPORT(x, X) \
138 static struct xpt_xport mmc_xport_ ## x = { \
139 .xport = XPORT_ ## X, \
141 .ops = &mmc_xport_ops, \
143 CAM_XPT_XPORT(mmc_xport_ ## x);
145 MMC_XPT_XPORT(mmc, MMCSD);
147 static struct xpt_proto_ops mmc_proto_ops = {
148 .announce = mmc_proto_announce,
149 .denounce = mmc_proto_denounce,
150 .debug_out = mmc_proto_debug_out,
153 static struct xpt_proto mmc_proto = {
154 .proto = PROTO_MMCSD,
156 .ops = &mmc_proto_ops,
158 CAM_XPT_PROTO(mmc_proto);
165 #define PROBE_FLAG_ACMD_SENT 0x1 /* CMD55 is sent, card expects ACMD */
166 uint8_t acmd41_count; /* how many times ACMD41 has been issued */
167 struct cam_periph *periph;
170 /* XPort functions -- an interface to CAM at periph side */
172 static struct cam_ed *
173 mmc_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
175 struct cam_ed *device;
177 printf("mmc_alloc_device()\n");
178 device = xpt_alloc_device(bus, target, lun_id);
182 device->quirk = NULL;
185 bzero(&device->inq_data, sizeof(device->inq_data));
186 device->inq_flags = 0;
187 device->queue_flags = 0;
188 device->serial_num = NULL;
189 device->serial_num_len = 0;
194 mmc_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
195 struct cam_ed *device, void *async_arg)
198 printf("mmc_dev_async(async_code=0x%x, path_id=%d, target_id=%x, lun_id=%" SCNx64 "\n",
204 * We only need to handle events for real devices.
206 if (target->target_id == CAM_TARGET_WILDCARD
207 || device->lun_id == CAM_LUN_WILDCARD)
210 if (async_code == AC_LOST_DEVICE) {
211 if ((device->flags & CAM_DEV_UNCONFIGURED) == 0) {
212 printf("AC_LOST_DEVICE -> set to unconfigured\n");
213 device->flags |= CAM_DEV_UNCONFIGURED;
214 xpt_release_device(device);
216 printf("AC_LOST_DEVICE on unconfigured device\n");
218 } else if (async_code == AC_FOUND_DEVICE) {
219 printf("Got AC_FOUND_DEVICE -- whatever...\n");
220 } else if (async_code == AC_PATH_REGISTERED) {
221 printf("Got AC_PATH_REGISTERED -- whatever...\n");
222 } else if (async_code == AC_PATH_DEREGISTERED ) {
223 printf("Got AC_PATH_DEREGISTERED -- whatever...\n");
224 } else if (async_code == AC_UNIT_ATTENTION) {
225 printf("Got interrupt generated by the card and ignored it\n");
227 panic("Unknown async code\n");
230 /* Taken from nvme_scan_lun, thanks to bsdimp@ */
232 mmc_scan_lun(struct cam_periph *periph, struct cam_path *path,
233 cam_flags flags, union ccb *request_ccb)
235 struct ccb_pathinq cpi;
237 struct cam_periph *old_periph;
240 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("mmc_scan_lun\n"));
242 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
243 cpi.ccb_h.func_code = XPT_PATH_INQ;
244 xpt_action((union ccb *)&cpi);
246 if (cpi.ccb_h.status != CAM_REQ_CMP) {
247 if (request_ccb != NULL) {
248 request_ccb->ccb_h.status = cpi.ccb_h.status;
249 xpt_done(request_ccb);
254 if (xpt_path_lun_id(path) == CAM_LUN_WILDCARD) {
255 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("mmd_scan_lun ignoring bus\n"));
256 request_ccb->ccb_h.status = CAM_REQ_CMP; /* XXX signal error ? */
257 xpt_done(request_ccb);
261 lock = (xpt_path_owned(path) == 0);
265 if ((old_periph = cam_periph_find(path, "mmcprobe")) != NULL) {
266 if ((old_periph->flags & CAM_PERIPH_INVALID) == 0) {
267 // mmcprobe_softc *softc;
268 // softc = (mmcprobe_softc *)old_periph->softc;
269 // Not sure if we need request ccb queue for mmc
270 // TAILQ_INSERT_TAIL(&softc->request_ccbs,
271 // &request_ccb->ccb_h, periph_links.tqe);
272 // softc->restart = 1;
273 CAM_DEBUG(path, CAM_DEBUG_INFO,
274 ("Got scan request, but mmcprobe already exists\n"));
275 request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
276 xpt_done(request_ccb);
278 request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
279 xpt_done(request_ccb);
282 xpt_print(path, " Set up the mmcprobe device...\n");
284 status = cam_periph_alloc(mmcprobe_register, NULL,
291 if (status != CAM_REQ_CMP) {
292 xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
293 "returned an error, can't continue probe\n");
295 request_ccb->ccb_h.status = status;
296 xpt_done(request_ccb);
300 xpt_path_unlock(path);
304 mmc_action(union ccb *start_ccb)
306 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
307 ("mmc_action! func_code=%x, action %s\n", start_ccb->ccb_h.func_code,
308 xpt_action_name(start_ccb->ccb_h.func_code)));
309 switch (start_ccb->ccb_h.func_code) {
316 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
317 ("XPT_SCAN_{BUS,TGT,LUN}\n"));
318 mmc_scan_lun(start_ccb->ccb_h.path->periph,
319 start_ccb->ccb_h.path, start_ccb->crcn.flags,
323 case XPT_DEV_ADVINFO:
325 mmc_dev_advinfo(start_ccb);
330 xpt_action_default(start_ccb);
336 mmc_dev_advinfo(union ccb *start_ccb)
338 struct cam_ed *device;
339 struct ccb_dev_advinfo *cdai;
342 start_ccb->ccb_h.status = CAM_REQ_INVALID;
343 device = start_ccb->ccb_h.path->device;
344 cdai = &start_ccb->cdai;
345 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
346 ("%s: request %x\n", __func__, cdai->buftype));
348 /* We don't support writing any data */
349 if (cdai->flags & CDAI_FLAG_STORE)
350 panic("Attempt to store data?!");
352 switch(cdai->buftype) {
353 case CDAI_TYPE_SCSI_DEVID:
354 cdai->provsiz = device->device_id_len;
355 if (device->device_id_len == 0)
357 amt = MIN(cdai->provsiz, cdai->bufsiz);
358 memcpy(cdai->buf, device->device_id, amt);
360 case CDAI_TYPE_SERIAL_NUM:
361 cdai->provsiz = device->serial_num_len;
362 if (device->serial_num_len == 0)
364 amt = MIN(cdai->provsiz, cdai->bufsiz);
365 memcpy(cdai->buf, device->serial_num, amt);
367 case CDAI_TYPE_PHYS_PATH: /* pass(4) wants this */
371 panic("Unknown buftype");
374 start_ccb->ccb_h.status = CAM_REQ_CMP;
378 mmc_announce_periph(struct cam_periph *periph)
380 struct ccb_pathinq cpi;
381 struct ccb_trans_settings cts;
382 struct cam_path *path = periph->path;
384 cam_periph_assert(periph, MA_OWNED);
386 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
387 ("mmc_announce_periph: called\n"));
389 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
390 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
391 cts.type = CTS_TYPE_CURRENT_SETTINGS;
392 xpt_action((union ccb*)&cts);
393 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
395 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
396 cpi.ccb_h.func_code = XPT_PATH_INQ;
397 xpt_action((union ccb *)&cpi);
398 printf("XPT info: CLK %04X, ...\n", cts.proto_specific.mmc.ios.clock);
401 /* This func is called per attached device :-( */
403 mmc_print_ident(struct mmc_params *ident_data)
405 printf("Relative addr: %08x\n", ident_data->card_rca);
406 printf("Card features: <");
407 if (ident_data->card_features & CARD_FEATURE_MMC)
409 if (ident_data->card_features & CARD_FEATURE_MEMORY)
411 if (ident_data->card_features & CARD_FEATURE_SDHC)
412 printf("High-Capacity ");
413 if (ident_data->card_features & CARD_FEATURE_SD20)
414 printf("SD2.0-Conditions ");
415 if (ident_data->card_features & CARD_FEATURE_SDIO)
419 if (ident_data->card_features & CARD_FEATURE_MEMORY)
420 printf("Card memory OCR: %08x\n", ident_data->card_ocr);
422 if (ident_data->card_features & CARD_FEATURE_SDIO) {
423 printf("Card IO OCR: %08x\n", ident_data->io_ocr);
424 printf("Number of funcitions: %u\n", ident_data->sdio_func_count);
429 mmc_proto_announce(struct cam_ed *device)
431 mmc_print_ident(&device->mmc_ident_data);
435 mmc_proto_denounce(struct cam_ed *device)
437 mmc_print_ident(&device->mmc_ident_data);
441 mmc_proto_debug_out(union ccb *ccb)
443 if (ccb->ccb_h.func_code != XPT_MMC_IO)
446 CAM_DEBUG(ccb->ccb_h.path,
447 CAM_DEBUG_CDB,("mmc_proto_debug_out\n"));
450 static periph_init_t probe_periph_init;
452 static struct periph_driver probe_driver =
454 probe_periph_init, "mmcprobe",
455 TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0,
459 PERIPHDRIVER_DECLARE(mmcprobe, probe_driver);
461 #define CARD_ID_FREQUENCY 400000 /* Spec requires 400kHz max during ID phase. */
469 mmcprobe_register(struct cam_periph *periph, void *arg)
471 union ccb *request_ccb; /* CCB representing the probe request */
473 mmcprobe_softc *softc;
475 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("mmcprobe_register\n"));
477 request_ccb = (union ccb *)arg;
478 if (request_ccb == NULL) {
479 printf("mmcprobe_register: no probe CCB, "
480 "can't register device\n");
481 return(CAM_REQ_CMP_ERR);
484 softc = (mmcprobe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_NOWAIT);
487 printf("proberegister: Unable to probe new device. "
488 "Unable to allocate softc\n");
489 return(CAM_REQ_CMP_ERR);
493 softc->acmd41_count = 0;
494 periph->softc = softc;
495 softc->periph = periph;
496 softc->action = PROBE_INVALID;
498 status = cam_periph_acquire(periph);
500 memset(&periph->path->device->mmc_ident_data, 0, sizeof(struct mmc_params));
501 if (status != CAM_REQ_CMP) {
502 printf("proberegister: cam_periph_acquire failed (status=%d)\n",
506 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe started\n"));
508 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED)
509 PROBE_SET_ACTION(softc, PROBE_RESET);
511 PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
513 /* This will kick the ball */
514 xpt_schedule(periph, CAM_PRIORITY_XPT);
520 mmc_highest_voltage(uint32_t ocr)
524 for (i = MMC_OCR_MAX_VOLTAGE_SHIFT;
525 i >= MMC_OCR_MIN_VOLTAGE_SHIFT; i--)
532 init_standard_ccb(union ccb *ccb, uint32_t cmd)
534 ccb->ccb_h.func_code = cmd;
535 ccb->ccb_h.flags = CAM_DIR_OUT;
536 ccb->ccb_h.retry_count = 0;
537 ccb->ccb_h.timeout = 15 * 1000;
538 ccb->ccb_h.cbfcnp = mmcprobe_done;
542 mmcprobe_start(struct cam_periph *periph, union ccb *start_ccb)
544 mmcprobe_softc *softc;
545 struct cam_path *path;
546 struct ccb_mmcio *mmcio;
547 struct mtx *p_mtx = cam_periph_mtx(periph);
548 struct ccb_trans_settings_mmc *cts;
550 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("mmcprobe_start\n"));
551 softc = (mmcprobe_softc *)periph->softc;
552 path = start_ccb->ccb_h.path;
553 mmcio = &start_ccb->mmcio;
554 cts = &start_ccb->cts.proto_specific.mmc;
555 struct mmc_params *mmcp = &path->device->mmc_ident_data;
557 memset(&mmcio->cmd, 0, sizeof(struct mmc_command));
559 if (softc->restart) {
561 if (path->device->flags & CAM_DEV_UNCONFIGURED)
562 softc->action = PROBE_RESET;
564 softc->action = PROBE_IDENTIFY;
568 /* Here is the place where the identify fun begins */
569 switch (softc->action) {
573 init_standard_ccb(start_ccb, XPT_PATH_INQ);
574 xpt_action(start_ccb);
576 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("Start with PROBE_RESET\n"));
577 init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
578 cts->ios.power_mode = power_off;
579 cts->ios_valid = MMC_PM;
580 xpt_action(start_ccb);
581 mtx_sleep(periph, p_mtx, 0, "mmcios", 100);
584 /* Get the host OCR */
585 init_standard_ccb(start_ccb, XPT_GET_TRAN_SETTINGS);
586 xpt_action(start_ccb);
588 uint32_t hv = mmc_highest_voltage(cts->host_ocr);
589 init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
591 cts->ios.bus_mode = opendrain;
592 cts->ios.chip_select = cs_dontcare;
593 cts->ios.power_mode = power_up;
594 cts->ios.bus_width = bus_width_1;
596 cts->ios_valid = MMC_VDD | MMC_PM | MMC_BM |
597 MMC_CS | MMC_BW | MMC_CLK;
598 xpt_action(start_ccb);
599 mtx_sleep(periph, p_mtx, 0, "mmcios", 100);
601 init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
602 cts->ios.power_mode = power_on;
603 cts->ios.clock = CARD_ID_FREQUENCY;
604 cts->ios.timing = bus_timing_normal;
605 cts->ios_valid = MMC_PM | MMC_CLK | MMC_BT;
606 xpt_action(start_ccb);
607 mtx_sleep(periph, p_mtx, 0, "mmcios", 100);
608 /* End for mmc_power_on */
610 /* Begin mmc_idle_cards() */
611 init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
612 cts->ios.chip_select = cs_high;
613 cts->ios_valid = MMC_CS;
614 xpt_action(start_ccb);
615 mtx_sleep(periph, p_mtx, 0, "mmcios", 1);
617 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("Send first XPT_MMC_IO\n"));
618 init_standard_ccb(start_ccb, XPT_MMC_IO);
619 mmcio->cmd.opcode = MMC_GO_IDLE_STATE; /* CMD 0 */
621 mmcio->cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
622 mmcio->cmd.data = NULL;
623 mmcio->stop.opcode = 0;
625 /* XXX Reset I/O portion as well */
627 case PROBE_SDIO_RESET:
628 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE,
629 ("Start with PROBE_SDIO_RESET\n"));
630 uint32_t mmc_arg = SD_IO_RW_ADR(SD_IO_CCCR_CTL)
631 | SD_IO_RW_DAT(CCCR_CTL_RES) | SD_IO_RW_WR | SD_IO_RW_RAW;
632 cam_fill_mmcio(&start_ccb->mmcio,
634 /*cbfcnp*/ mmcprobe_done,
635 /*flags*/ CAM_DIR_NONE,
636 /*mmc_opcode*/ SD_IO_RW_DIRECT,
638 /*mmc_flags*/ MMC_RSP_R5 | MMC_CMD_AC,
642 case PROBE_SEND_IF_COND:
643 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE,
644 ("Start with PROBE_SEND_IF_COND\n"));
645 init_standard_ccb(start_ccb, XPT_MMC_IO);
646 mmcio->cmd.opcode = SD_SEND_IF_COND; /* CMD 8 */
647 mmcio->cmd.arg = (1 << 8) + 0xAA;
648 mmcio->cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
649 mmcio->stop.opcode = 0;
652 case PROBE_SDIO_INIT:
653 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE,
654 ("Start with PROBE_SDIO_INIT\n"));
655 init_standard_ccb(start_ccb, XPT_MMC_IO);
656 mmcio->cmd.opcode = IO_SEND_OP_COND; /* CMD 5 */
657 mmcio->cmd.arg = mmcp->io_ocr;
658 mmcio->cmd.flags = MMC_RSP_R4;
659 mmcio->stop.opcode = 0;
663 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE,
664 ("Start with PROBE_MMC_INIT\n"));
665 init_standard_ccb(start_ccb, XPT_MMC_IO);
666 mmcio->cmd.opcode = MMC_SEND_OP_COND; /* CMD 1 */
667 mmcio->cmd.arg = MMC_OCR_CCS | mmcp->card_ocr; /* CCS + ocr */;
668 mmcio->cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
669 mmcio->stop.opcode = 0;
672 case PROBE_SEND_APP_OP_COND:
673 init_standard_ccb(start_ccb, XPT_MMC_IO);
674 if (softc->flags & PROBE_FLAG_ACMD_SENT) {
675 mmcio->cmd.opcode = ACMD_SD_SEND_OP_COND; /* CMD 41 */
677 * We set CCS bit because we do support SDHC cards.
678 * XXX: Don't set CCS if no response to CMD8.
680 uint32_t cmd_arg = MMC_OCR_CCS | mmcp->card_ocr; /* CCS + ocr */
681 if (softc->acmd41_count < 10 && mmcp->card_ocr != 0 )
682 cmd_arg |= MMC_OCR_S18R;
683 mmcio->cmd.arg = cmd_arg;
684 mmcio->cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
685 softc->acmd41_count++;
687 mmcio->cmd.opcode = MMC_APP_CMD; /* CMD 55 */
688 mmcio->cmd.arg = 0; /* rca << 16 */
689 mmcio->cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
691 mmcio->stop.opcode = 0;
694 case PROBE_GET_CID: /* XXX move to mmc_da */
695 init_standard_ccb(start_ccb, XPT_MMC_IO);
696 mmcio->cmd.opcode = MMC_ALL_SEND_CID;
698 mmcio->cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
699 mmcio->stop.opcode = 0;
702 case PROBE_SEND_RELATIVE_ADDR:
703 init_standard_ccb(start_ccb, XPT_MMC_IO);
704 mmcio->cmd.opcode = SD_SEND_RELATIVE_ADDR;
706 mmcio->cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
707 mmcio->stop.opcode = 0;
709 case PROBE_SELECT_CARD:
710 init_standard_ccb(start_ccb, XPT_MMC_IO);
711 mmcio->cmd.opcode = MMC_SELECT_CARD;
712 mmcio->cmd.arg = (uint32_t)path->device->mmc_ident_data.card_rca << 16;
713 mmcio->cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
714 mmcio->stop.opcode = 0;
716 case PROBE_GET_CSD: /* XXX move to mmc_da */
717 init_standard_ccb(start_ccb, XPT_MMC_IO);
718 mmcio->cmd.opcode = MMC_SEND_CSD;
719 mmcio->cmd.arg = (uint32_t)path->device->mmc_ident_data.card_rca << 16;
720 mmcio->cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
721 mmcio->stop.opcode = 0;
724 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("Start with PROBE_DONE\n"));
725 init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
726 cts->ios.bus_mode = pushpull;
727 cts->ios_valid = MMC_BM;
728 xpt_action(start_ccb);
735 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("probestart: invalid action state 0x%x\n", softc->action));
736 panic("default: case in mmc_probe_start()");
739 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
740 xpt_action(start_ccb);
743 static void mmcprobe_cleanup(struct cam_periph *periph)
745 free(periph->softc, M_CAMXPT);
749 mmcprobe_done(struct cam_periph *periph, union ccb *done_ccb)
751 mmcprobe_softc *softc;
752 struct cam_path *path;
755 struct ccb_mmcio *mmcio;
758 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("mmcprobe_done\n"));
759 softc = (mmcprobe_softc *)periph->softc;
760 path = done_ccb->ccb_h.path;
761 priority = done_ccb->ccb_h.pinfo.priority;
763 switch (softc->action) {
768 printf("Starting completion of PROBE_RESET\n");
769 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("done with PROBE_RESET\n"));
770 mmcio = &done_ccb->mmcio;
771 err = mmcio->cmd.error;
773 if (err != MMC_ERR_NONE) {
774 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
775 ("GO_IDLE_STATE failed with error %d\n",
778 /* There was a device there, but now it's gone... */
779 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
780 xpt_async(AC_LOST_DEVICE, path, NULL);
782 PROBE_SET_ACTION(softc, PROBE_INVALID);
785 path->device->protocol = PROTO_MMCSD;
786 PROBE_SET_ACTION(softc, PROBE_SEND_IF_COND);
789 case PROBE_SEND_IF_COND:
791 mmcio = &done_ccb->mmcio;
792 err = mmcio->cmd.error;
793 struct mmc_params *mmcp = &path->device->mmc_ident_data;
795 if (err != MMC_ERR_NONE || mmcio->cmd.resp[0] != 0x1AA) {
796 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
797 ("IF_COND: error %d, pattern %08x\n",
798 err, mmcio->cmd.resp[0]));
800 mmcp->card_features |= CARD_FEATURE_SD20;
801 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
802 ("SD 2.0 interface conditions: OK\n"));
805 PROBE_SET_ACTION(softc, PROBE_SDIO_RESET);
808 case PROBE_SDIO_RESET:
810 mmcio = &done_ccb->mmcio;
811 err = mmcio->cmd.error;
813 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
814 ("SDIO_RESET: error %d, CCCR CTL register: %08x\n",
815 err, mmcio->cmd.resp[0]));
816 PROBE_SET_ACTION(softc, PROBE_SDIO_INIT);
819 case PROBE_SDIO_INIT:
821 mmcio = &done_ccb->mmcio;
822 err = mmcio->cmd.error;
823 struct mmc_params *mmcp = &path->device->mmc_ident_data;
825 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
826 ("SDIO_INIT: error %d, %08x %08x %08x %08x\n",
827 err, mmcio->cmd.resp[0],
830 mmcio->cmd.resp[3]));
833 * Error here means that this card is not SDIO,
834 * so proceed with memory init as if nothing has happened
836 if (err != MMC_ERR_NONE) {
837 PROBE_SET_ACTION(softc, PROBE_SEND_APP_OP_COND);
840 mmcp->card_features |= CARD_FEATURE_SDIO;
841 uint32_t ioifcond = mmcio->cmd.resp[0];
842 uint32_t io_ocr = ioifcond & R4_IO_OCR_MASK;
844 mmcp->sdio_func_count = R4_IO_NUM_FUNCTIONS(ioifcond);
845 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
846 ("SDIO card: %d functions\n", mmcp->sdio_func_count));
848 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
849 ("SDIO OCR invalid?!\n"));
853 if (io_ocr != 0 && mmcp->io_ocr == 0) {
854 mmcp->io_ocr = io_ocr;
855 break; /* Retry, this time with non-0 OCR */
857 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
858 ("SDIO OCR: %08x\n", mmcp->io_ocr));
860 if (ioifcond & R4_IO_MEM_PRESENT) {
861 /* Combo card -- proceed to memory initialization */
862 PROBE_SET_ACTION(softc, PROBE_SEND_APP_OP_COND);
864 /* No memory portion -- get RCA and select card */
865 PROBE_SET_ACTION(softc, PROBE_SEND_RELATIVE_ADDR);
871 mmcio = &done_ccb->mmcio;
872 err = mmcio->cmd.error;
873 struct mmc_params *mmcp = &path->device->mmc_ident_data;
875 if (err != MMC_ERR_NONE) {
876 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
877 ("MMC_INIT: error %d, resp %08x\n",
878 err, mmcio->cmd.resp[0]));
879 PROBE_SET_ACTION(softc, PROBE_INVALID);
882 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
883 ("MMC card, OCR %08x\n", mmcio->cmd.resp[0]));
885 if (mmcp->card_ocr == 0) {
886 /* We haven't sent the OCR to the card yet -- do it */
887 mmcp->card_ocr = mmcio->cmd.resp[0];
888 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
889 ("-> sending OCR to card\n"));
893 if (!(mmcio->cmd.resp[0] & MMC_OCR_CARD_BUSY)) {
894 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
895 ("Card is still powering up\n"));
899 mmcp->card_features |= CARD_FEATURE_MMC | CARD_FEATURE_MEMORY;
900 PROBE_SET_ACTION(softc, PROBE_GET_CID);
903 case PROBE_SEND_APP_OP_COND:
905 mmcio = &done_ccb->mmcio;
906 err = mmcio->cmd.error;
908 if (err != MMC_ERR_NONE) {
909 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
910 ("APP_OP_COND: error %d, resp %08x\n",
911 err, mmcio->cmd.resp[0]));
912 PROBE_SET_ACTION(softc, PROBE_MMC_INIT);
916 if (!(softc->flags & PROBE_FLAG_ACMD_SENT)) {
917 /* Don't change the state */
918 softc->flags |= PROBE_FLAG_ACMD_SENT;
922 softc->flags &= ~PROBE_FLAG_ACMD_SENT;
923 if ((mmcio->cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
924 (mmcio->cmd.arg & MMC_OCR_VOLTAGE) == 0) {
925 struct mmc_params *mmcp = &path->device->mmc_ident_data;
926 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
927 ("Card OCR: %08x\n", mmcio->cmd.resp[0]));
928 if (mmcp->card_ocr == 0) {
929 mmcp->card_ocr = mmcio->cmd.resp[0];
930 /* Now when we know OCR that we want -- send it to card */
931 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
932 ("-> sending OCR to card\n"));
934 /* We already know the OCR and despite of that we
935 * are processing the answer to ACMD41 -> move on
937 PROBE_SET_ACTION(softc, PROBE_GET_CID);
939 /* Getting an answer to ACMD41 means the card has memory */
940 mmcp->card_features |= CARD_FEATURE_MEMORY;
942 /* Standard capacity vs High Capacity memory card */
943 if (mmcio->cmd.resp[0] & MMC_OCR_CCS) {
944 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
946 mmcp->card_features |= CARD_FEATURE_SDHC;
949 /* Whether the card supports 1.8V signaling */
950 if (mmcio->cmd.resp[0] & MMC_OCR_S18A) {
951 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
952 ("Card supports 1.8V signaling\n"));
953 mmcp->card_features |= CARD_FEATURE_18V;
956 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
957 ("Card not ready: %08x\n", mmcio->cmd.resp[0]));
958 /* Send CMD55+ACMD41 once again */
959 PROBE_SET_ACTION(softc, PROBE_SEND_APP_OP_COND);
964 case PROBE_GET_CID: /* XXX move to mmc_da */
966 mmcio = &done_ccb->mmcio;
967 err = mmcio->cmd.error;
969 if (err != MMC_ERR_NONE) {
970 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
971 ("PROBE_GET_CID: error %d\n", err));
972 PROBE_SET_ACTION(softc, PROBE_INVALID);
976 struct mmc_params *mmcp = &path->device->mmc_ident_data;
977 memcpy(mmcp->card_cid, mmcio->cmd.resp, 4 * sizeof(uint32_t));
978 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
979 ("CID %08x%08x%08x%08x\n",
984 PROBE_SET_ACTION(softc, PROBE_SEND_RELATIVE_ADDR);
987 case PROBE_SEND_RELATIVE_ADDR: {
988 mmcio = &done_ccb->mmcio;
989 err = mmcio->cmd.error;
990 struct mmc_params *mmcp = &path->device->mmc_ident_data;
991 uint16_t rca = mmcio->cmd.resp[0] >> 16;
992 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
993 ("Card published RCA: %u\n", rca));
994 path->device->mmc_ident_data.card_rca = rca;
995 if (err != MMC_ERR_NONE) {
996 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
997 ("PROBE_SEND_RELATIVE_ADDR: error %d\n", err));
998 PROBE_SET_ACTION(softc, PROBE_INVALID);
1002 /* If memory is present, get CSD, otherwise select card */
1003 if (mmcp->card_features & CARD_FEATURE_MEMORY)
1004 PROBE_SET_ACTION(softc, PROBE_GET_CSD);
1006 PROBE_SET_ACTION(softc, PROBE_SELECT_CARD);
1009 case PROBE_GET_CSD: {
1010 mmcio = &done_ccb->mmcio;
1011 err = mmcio->cmd.error;
1013 if (err != MMC_ERR_NONE) {
1014 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1015 ("PROBE_GET_CSD: error %d\n", err));
1016 PROBE_SET_ACTION(softc, PROBE_INVALID);
1020 struct mmc_params *mmcp = &path->device->mmc_ident_data;
1021 memcpy(mmcp->card_csd, mmcio->cmd.resp, 4 * sizeof(uint32_t));
1022 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1023 ("CSD %08x%08x%08x%08x\n",
1027 mmcp->card_csd[3]));
1028 PROBE_SET_ACTION(softc, PROBE_SELECT_CARD);
1031 case PROBE_SELECT_CARD: {
1032 mmcio = &done_ccb->mmcio;
1033 err = mmcio->cmd.error;
1034 if (err != MMC_ERR_NONE) {
1035 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1036 ("PROBE_SEND_RELATIVE_ADDR: error %d\n", err));
1037 PROBE_SET_ACTION(softc, PROBE_INVALID);
1041 PROBE_SET_ACTION(softc, PROBE_DONE);
1045 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1046 ("mmc_probedone: invalid action state 0x%x\n", softc->action));
1047 panic("default: case in mmc_probe_done()");
1050 if (softc->action == PROBE_INVALID &&
1051 (path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
1052 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1053 ("mmc_probedone: Should send AC_LOST_DEVICE but won't for now\n"));
1054 //xpt_async(AC_LOST_DEVICE, path, NULL);
1057 xpt_release_ccb(done_ccb);
1058 if (softc->action != PROBE_INVALID)
1059 xpt_schedule(periph, priority);
1060 /* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
1061 int frozen = cam_release_devq(path, 0, 0, 0, FALSE);
1062 printf("mmc_probedone: remaining freezecnt %d\n", frozen);
1064 if (softc->action == PROBE_DONE) {
1065 /* Notify the system that the device is found! */
1066 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1067 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1068 xpt_acquire_device(path->device);
1069 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1070 xpt_action(done_ccb);
1071 xpt_async(AC_FOUND_DEVICE, path, done_ccb);
1074 if (softc->action == PROBE_DONE || softc->action == PROBE_INVALID) {
1075 cam_periph_invalidate(periph);
1076 cam_periph_release_locked(periph);