2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2013,2014 Ilya Bakulin <ilya@bakulin.de>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer,
12 * without modification, immediately at the beginning of the file.
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 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
34 #include <sys/endian.h>
35 #include <sys/systm.h>
36 #include <sys/types.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
41 #include <sys/fcntl.h>
42 #include <sys/interrupt.h>
46 #include <sys/mutex.h>
47 #include <sys/sysctl.h>
48 #include <sys/condvar.h>
51 #include <cam/cam_ccb.h>
52 #include <cam/cam_queue.h>
53 #include <cam/cam_periph.h>
54 #include <cam/cam_sim.h>
55 #include <cam/cam_xpt.h>
56 #include <cam/cam_xpt_sim.h>
57 #include <cam/cam_xpt_periph.h>
58 #include <cam/cam_xpt_internal.h>
59 #include <cam/cam_debug.h>
61 #include <cam/mmc/mmc.h>
62 #include <cam/mmc/mmc_bus.h>
64 #include <machine/stdarg.h> /* for xpt_print below */
65 #include <machine/_inttypes.h> /* for PRIu64 */
68 FEATURE(mmccam, "CAM-based MMC/SD/SDIO stack");
70 static struct cam_ed * mmc_alloc_device(struct cam_eb *bus,
71 struct cam_et *target, lun_id_t lun_id);
72 static void mmc_dev_async(u_int32_t async_code, struct cam_eb *bus,
73 struct cam_et *target, struct cam_ed *device, void *async_arg);
74 static void mmc_action(union ccb *start_ccb);
75 static void mmc_dev_advinfo(union ccb *start_ccb);
76 static void mmc_announce_periph(struct cam_periph *periph);
77 static void mmc_scan_lun(struct cam_periph *periph,
78 struct cam_path *path, cam_flags flags, union ccb *ccb);
80 /* mmcprobe methods */
81 static cam_status mmcprobe_register(struct cam_periph *periph, void *arg);
82 static void mmcprobe_start(struct cam_periph *periph, union ccb *start_ccb);
83 static void mmcprobe_cleanup(struct cam_periph *periph);
84 static void mmcprobe_done(struct cam_periph *periph, union ccb *done_ccb);
86 static void mmc_proto_announce(struct cam_ed *device);
87 static void mmc_proto_denounce(struct cam_ed *device);
88 static void mmc_proto_debug_out(union ccb *ccb);
97 PROBE_SEND_APP_OP_COND,
100 PROBE_SEND_RELATIVE_ADDR,
101 PROBE_MMC_SET_RELATIVE_ADDR,
107 static char *probe_action_text[] = {
111 "PROBE_SEND_IF_COND",
114 "PROBE_SEND_APP_OP_COND",
117 "PROBE_SEND_RELATIVE_ADDR",
118 "PROBE_MMC_SET_RELATIVE_ADDR",
124 #define PROBE_SET_ACTION(softc, newaction) \
127 text = probe_action_text; \
128 CAM_DEBUG((softc)->periph->path, CAM_DEBUG_PROBE, \
129 ("Probe %s to %s\n", text[(softc)->action], \
130 text[(newaction)])); \
131 (softc)->action = (newaction); \
134 static struct xpt_xport_ops mmc_xport_ops = {
135 .alloc_device = mmc_alloc_device,
136 .action = mmc_action,
137 .async = mmc_dev_async,
138 .announce = mmc_announce_periph,
141 #define MMC_XPT_XPORT(x, X) \
142 static struct xpt_xport mmc_xport_ ## x = { \
143 .xport = XPORT_ ## X, \
145 .ops = &mmc_xport_ops, \
147 CAM_XPT_XPORT(mmc_xport_ ## x);
149 MMC_XPT_XPORT(mmc, MMCSD);
151 static struct xpt_proto_ops mmc_proto_ops = {
152 .announce = mmc_proto_announce,
153 .denounce = mmc_proto_denounce,
154 .debug_out = mmc_proto_debug_out,
157 static struct xpt_proto mmc_proto = {
158 .proto = PROTO_MMCSD,
160 .ops = &mmc_proto_ops,
162 CAM_XPT_PROTO(mmc_proto);
169 #define PROBE_FLAG_ACMD_SENT 0x1 /* CMD55 is sent, card expects ACMD */
170 uint8_t acmd41_count; /* how many times ACMD41 has been issued */
171 struct cam_periph *periph;
174 /* XPort functions -- an interface to CAM at periph side */
176 static struct cam_ed *
177 mmc_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
179 struct cam_ed *device;
181 printf("mmc_alloc_device()\n");
182 device = xpt_alloc_device(bus, target, lun_id);
186 device->quirk = NULL;
189 bzero(&device->inq_data, sizeof(device->inq_data));
190 device->inq_flags = 0;
191 device->queue_flags = 0;
192 device->serial_num = NULL;
193 device->serial_num_len = 0;
198 mmc_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
199 struct cam_ed *device, void *async_arg)
202 printf("mmc_dev_async(async_code=0x%x, path_id=%d, target_id=%x, lun_id=%" SCNx64 "\n",
208 * We only need to handle events for real devices.
210 if (target->target_id == CAM_TARGET_WILDCARD
211 || device->lun_id == CAM_LUN_WILDCARD)
214 if (async_code == AC_LOST_DEVICE) {
215 if ((device->flags & CAM_DEV_UNCONFIGURED) == 0) {
216 printf("AC_LOST_DEVICE -> set to unconfigured\n");
217 device->flags |= CAM_DEV_UNCONFIGURED;
218 xpt_release_device(device);
220 printf("AC_LOST_DEVICE on unconfigured device\n");
222 } else if (async_code == AC_FOUND_DEVICE) {
223 printf("Got AC_FOUND_DEVICE -- whatever...\n");
224 } else if (async_code == AC_PATH_REGISTERED) {
225 printf("Got AC_PATH_REGISTERED -- whatever...\n");
226 } else if (async_code == AC_PATH_DEREGISTERED ) {
227 printf("Got AC_PATH_DEREGISTERED -- whatever...\n");
228 } else if (async_code == AC_UNIT_ATTENTION) {
229 printf("Got interrupt generated by the card and ignored it\n");
231 panic("Unknown async code\n");
234 /* Taken from nvme_scan_lun, thanks to bsdimp@ */
236 mmc_scan_lun(struct cam_periph *periph, struct cam_path *path,
237 cam_flags flags, union ccb *request_ccb)
239 struct ccb_pathinq cpi;
241 struct cam_periph *old_periph;
244 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("mmc_scan_lun\n"));
246 xpt_path_inq(&cpi, path);
248 if (cpi.ccb_h.status != CAM_REQ_CMP) {
249 if (request_ccb != NULL) {
250 request_ccb->ccb_h.status = cpi.ccb_h.status;
251 xpt_done(request_ccb);
256 if (xpt_path_lun_id(path) == CAM_LUN_WILDCARD) {
257 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("mmd_scan_lun ignoring bus\n"));
258 request_ccb->ccb_h.status = CAM_REQ_CMP; /* XXX signal error ? */
259 xpt_done(request_ccb);
263 lock = (xpt_path_owned(path) == 0);
267 if ((old_periph = cam_periph_find(path, "mmcprobe")) != NULL) {
268 if ((old_periph->flags & CAM_PERIPH_INVALID) == 0) {
269 // mmcprobe_softc *softc;
270 // softc = (mmcprobe_softc *)old_periph->softc;
271 // Not sure if we need request ccb queue for mmc
272 // TAILQ_INSERT_TAIL(&softc->request_ccbs,
273 // &request_ccb->ccb_h, periph_links.tqe);
274 // softc->restart = 1;
275 CAM_DEBUG(path, CAM_DEBUG_INFO,
276 ("Got scan request, but mmcprobe already exists\n"));
277 request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
278 xpt_done(request_ccb);
280 request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
281 xpt_done(request_ccb);
284 xpt_print(path, " Set up the mmcprobe device...\n");
286 status = cam_periph_alloc(mmcprobe_register, NULL,
293 if (status != CAM_REQ_CMP) {
294 xpt_print(path, "xpt_scan_lun: cam_alloc_periph "
295 "returned an error, can't continue probe\n");
297 request_ccb->ccb_h.status = status;
298 xpt_done(request_ccb);
302 xpt_path_unlock(path);
306 mmc_action(union ccb *start_ccb)
308 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
309 ("mmc_action! func_code=%x, action %s\n", start_ccb->ccb_h.func_code,
310 xpt_action_name(start_ccb->ccb_h.func_code)));
311 switch (start_ccb->ccb_h.func_code) {
318 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
319 ("XPT_SCAN_{BUS,TGT,LUN}\n"));
320 mmc_scan_lun(start_ccb->ccb_h.path->periph,
321 start_ccb->ccb_h.path, start_ccb->crcn.flags,
325 case XPT_DEV_ADVINFO:
327 mmc_dev_advinfo(start_ccb);
332 xpt_action_default(start_ccb);
338 mmc_dev_advinfo(union ccb *start_ccb)
340 struct cam_ed *device;
341 struct ccb_dev_advinfo *cdai;
344 xpt_path_assert(start_ccb->ccb_h.path, MA_OWNED);
345 start_ccb->ccb_h.status = CAM_REQ_INVALID;
346 device = start_ccb->ccb_h.path->device;
347 cdai = &start_ccb->cdai;
348 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
349 ("%s: request %x\n", __func__, cdai->buftype));
351 /* We don't support writing any data */
352 if (cdai->flags & CDAI_FLAG_STORE)
353 panic("Attempt to store data?!");
355 switch(cdai->buftype) {
356 case CDAI_TYPE_SCSI_DEVID:
357 cdai->provsiz = device->device_id_len;
358 if (device->device_id_len == 0)
360 amt = MIN(cdai->provsiz, cdai->bufsiz);
361 memcpy(cdai->buf, device->device_id, amt);
363 case CDAI_TYPE_SERIAL_NUM:
364 cdai->provsiz = device->serial_num_len;
365 if (device->serial_num_len == 0)
367 amt = MIN(cdai->provsiz, cdai->bufsiz);
368 memcpy(cdai->buf, device->serial_num, amt);
370 case CDAI_TYPE_PHYS_PATH: /* pass(4) wants this */
373 case CDAI_TYPE_MMC_PARAMS:
374 cdai->provsiz = sizeof(struct mmc_params);
375 amt = MIN(cdai->provsiz, cdai->bufsiz);
376 memcpy(cdai->buf, &device->mmc_ident_data, amt);
379 panic("Unknown buftype");
382 start_ccb->ccb_h.status = CAM_REQ_CMP;
386 mmc_announce_periph(struct cam_periph *periph)
388 struct ccb_pathinq cpi;
389 struct ccb_trans_settings cts;
390 struct cam_path *path = periph->path;
392 cam_periph_assert(periph, MA_OWNED);
394 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
395 ("mmc_announce_periph: called\n"));
397 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NORMAL);
398 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
399 cts.type = CTS_TYPE_CURRENT_SETTINGS;
400 xpt_action((union ccb*)&cts);
401 if ((cts.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
403 xpt_path_inq(&cpi, periph->path);
404 printf("XPT info: CLK %04X, ...\n", cts.proto_specific.mmc.ios.clock);
407 /* This func is called per attached device :-( */
409 mmc_print_ident(struct mmc_params *ident_data)
411 printf("Relative addr: %08x\n", ident_data->card_rca);
412 printf("Card features: <");
413 if (ident_data->card_features & CARD_FEATURE_MMC)
415 if (ident_data->card_features & CARD_FEATURE_MEMORY)
417 if (ident_data->card_features & CARD_FEATURE_SDHC)
418 printf("High-Capacity ");
419 if (ident_data->card_features & CARD_FEATURE_SD20)
420 printf("SD2.0-Conditions ");
421 if (ident_data->card_features & CARD_FEATURE_SDIO)
425 if (ident_data->card_features & CARD_FEATURE_MEMORY)
426 printf("Card memory OCR: %08x\n", ident_data->card_ocr);
428 if (ident_data->card_features & CARD_FEATURE_SDIO) {
429 printf("Card IO OCR: %08x\n", ident_data->io_ocr);
430 printf("Number of funcitions: %u\n", ident_data->sdio_func_count);
435 mmc_proto_announce(struct cam_ed *device)
437 mmc_print_ident(&device->mmc_ident_data);
441 mmc_proto_denounce(struct cam_ed *device)
443 mmc_print_ident(&device->mmc_ident_data);
447 mmc_proto_debug_out(union ccb *ccb)
449 if (ccb->ccb_h.func_code != XPT_MMC_IO)
452 CAM_DEBUG(ccb->ccb_h.path,
453 CAM_DEBUG_CDB,("mmc_proto_debug_out\n"));
456 static periph_init_t probe_periph_init;
458 static struct periph_driver probe_driver =
460 probe_periph_init, "mmcprobe",
461 TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0,
465 PERIPHDRIVER_DECLARE(mmcprobe, probe_driver);
467 #define CARD_ID_FREQUENCY 400000 /* Spec requires 400kHz max during ID phase. */
475 mmcprobe_register(struct cam_periph *periph, void *arg)
477 mmcprobe_softc *softc;
478 union ccb *request_ccb; /* CCB representing the probe request */
481 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("mmcprobe_register\n"));
483 request_ccb = (union ccb *)arg;
484 if (request_ccb == NULL) {
485 printf("mmcprobe_register: no probe CCB, "
486 "can't register device\n");
487 return(CAM_REQ_CMP_ERR);
490 softc = (mmcprobe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_NOWAIT);
493 printf("proberegister: Unable to probe new device. "
494 "Unable to allocate softc\n");
495 return(CAM_REQ_CMP_ERR);
499 softc->acmd41_count = 0;
500 periph->softc = softc;
501 softc->periph = periph;
502 softc->action = PROBE_INVALID;
504 status = cam_periph_acquire(periph);
506 memset(&periph->path->device->mmc_ident_data, 0, sizeof(struct mmc_params));
508 printf("proberegister: cam_periph_acquire failed (status=%d)\n",
510 return (CAM_REQ_CMP_ERR);
512 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe started\n"));
514 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED)
515 PROBE_SET_ACTION(softc, PROBE_RESET);
517 PROBE_SET_ACTION(softc, PROBE_IDENTIFY);
519 /* This will kick the ball */
520 xpt_schedule(periph, CAM_PRIORITY_XPT);
526 mmc_highest_voltage(uint32_t ocr)
530 for (i = MMC_OCR_MAX_VOLTAGE_SHIFT;
531 i >= MMC_OCR_MIN_VOLTAGE_SHIFT; i--)
538 init_standard_ccb(union ccb *ccb, uint32_t cmd)
540 ccb->ccb_h.func_code = cmd;
541 ccb->ccb_h.flags = CAM_DIR_OUT;
542 ccb->ccb_h.retry_count = 0;
543 ccb->ccb_h.timeout = 15 * 1000;
544 ccb->ccb_h.cbfcnp = mmcprobe_done;
548 mmcprobe_start(struct cam_periph *periph, union ccb *start_ccb)
550 mmcprobe_softc *softc;
551 struct cam_path *path;
552 struct ccb_mmcio *mmcio;
553 struct mtx *p_mtx = cam_periph_mtx(periph);
554 struct ccb_trans_settings_mmc *cts;
556 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("mmcprobe_start\n"));
557 softc = (mmcprobe_softc *)periph->softc;
558 path = start_ccb->ccb_h.path;
559 mmcio = &start_ccb->mmcio;
560 cts = &start_ccb->cts.proto_specific.mmc;
561 struct mmc_params *mmcp = &path->device->mmc_ident_data;
563 memset(&mmcio->cmd, 0, sizeof(struct mmc_command));
565 if (softc->restart) {
567 if (path->device->flags & CAM_DEV_UNCONFIGURED)
568 softc->action = PROBE_RESET;
570 softc->action = PROBE_IDENTIFY;
574 /* Here is the place where the identify fun begins */
575 switch (softc->action) {
579 xpt_path_inq(&start_ccb->cpi, periph->path);
580 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("Start with PROBE_RESET\n"));
581 init_standard_ccb(start_ccb, XPT_GET_TRAN_SETTINGS);
582 xpt_action(start_ccb);
583 if (cts->ios.power_mode != power_off) {
584 init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
585 cts->ios.power_mode = power_off;
586 cts->ios_valid = MMC_PM;
587 xpt_action(start_ccb);
588 mtx_sleep(periph, p_mtx, 0, "mmcios", 100);
591 /* Get the host OCR */
592 init_standard_ccb(start_ccb, XPT_GET_TRAN_SETTINGS);
593 xpt_action(start_ccb);
595 uint32_t hv = mmc_highest_voltage(cts->host_ocr);
596 init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
598 cts->ios.bus_mode = opendrain;
599 cts->ios.chip_select = cs_dontcare;
600 cts->ios.power_mode = power_up;
601 cts->ios.bus_width = bus_width_1;
603 cts->ios_valid = MMC_VDD | MMC_PM | MMC_BM |
604 MMC_CS | MMC_BW | MMC_CLK;
605 xpt_action(start_ccb);
606 mtx_sleep(periph, p_mtx, 0, "mmcios", 100);
608 init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
609 cts->ios.power_mode = power_on;
610 cts->ios.clock = CARD_ID_FREQUENCY;
611 cts->ios.timing = bus_timing_normal;
612 cts->ios_valid = MMC_PM | MMC_CLK | MMC_BT;
613 xpt_action(start_ccb);
614 mtx_sleep(periph, p_mtx, 0, "mmcios", 100);
615 /* End for mmc_power_on */
617 /* Begin mmc_idle_cards() */
618 init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
619 cts->ios.chip_select = cs_high;
620 cts->ios_valid = MMC_CS;
621 xpt_action(start_ccb);
622 mtx_sleep(periph, p_mtx, 0, "mmcios", 1);
624 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("Send first XPT_MMC_IO\n"));
625 init_standard_ccb(start_ccb, XPT_MMC_IO);
626 mmcio->cmd.opcode = MMC_GO_IDLE_STATE; /* CMD 0 */
628 mmcio->cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
629 mmcio->cmd.data = NULL;
630 mmcio->stop.opcode = 0;
632 /* XXX Reset I/O portion as well */
634 case PROBE_SDIO_RESET:
635 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE,
636 ("Start with PROBE_SDIO_RESET\n"));
637 uint32_t mmc_arg = SD_IO_RW_ADR(SD_IO_CCCR_CTL)
638 | SD_IO_RW_DAT(CCCR_CTL_RES) | SD_IO_RW_WR | SD_IO_RW_RAW;
639 cam_fill_mmcio(&start_ccb->mmcio,
641 /*cbfcnp*/ mmcprobe_done,
642 /*flags*/ CAM_DIR_NONE,
643 /*mmc_opcode*/ SD_IO_RW_DIRECT,
645 /*mmc_flags*/ MMC_RSP_R5 | MMC_CMD_AC,
649 case PROBE_SEND_IF_COND:
650 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE,
651 ("Start with PROBE_SEND_IF_COND\n"));
652 init_standard_ccb(start_ccb, XPT_MMC_IO);
653 mmcio->cmd.opcode = SD_SEND_IF_COND; /* CMD 8 */
654 mmcio->cmd.arg = (1 << 8) + 0xAA;
655 mmcio->cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
656 mmcio->stop.opcode = 0;
659 case PROBE_SDIO_INIT:
660 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE,
661 ("Start with PROBE_SDIO_INIT\n"));
662 init_standard_ccb(start_ccb, XPT_MMC_IO);
663 mmcio->cmd.opcode = IO_SEND_OP_COND; /* CMD 5 */
664 mmcio->cmd.arg = mmcp->io_ocr;
665 mmcio->cmd.flags = MMC_RSP_R4;
666 mmcio->stop.opcode = 0;
670 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE,
671 ("Start with PROBE_MMC_INIT\n"));
672 init_standard_ccb(start_ccb, XPT_MMC_IO);
673 mmcio->cmd.opcode = MMC_SEND_OP_COND; /* CMD 1 */
674 mmcio->cmd.arg = MMC_OCR_CCS | mmcp->card_ocr; /* CCS + ocr */;
675 mmcio->cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
676 mmcio->stop.opcode = 0;
679 case PROBE_SEND_APP_OP_COND:
680 init_standard_ccb(start_ccb, XPT_MMC_IO);
681 if (softc->flags & PROBE_FLAG_ACMD_SENT) {
682 mmcio->cmd.opcode = ACMD_SD_SEND_OP_COND; /* CMD 41 */
684 * We set CCS bit because we do support SDHC cards.
685 * XXX: Don't set CCS if no response to CMD8.
687 uint32_t cmd_arg = MMC_OCR_CCS | mmcp->card_ocr; /* CCS + ocr */
688 if (softc->acmd41_count < 10 && mmcp->card_ocr != 0 )
689 cmd_arg |= MMC_OCR_S18R;
690 mmcio->cmd.arg = cmd_arg;
691 mmcio->cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
692 softc->acmd41_count++;
694 mmcio->cmd.opcode = MMC_APP_CMD; /* CMD 55 */
695 mmcio->cmd.arg = 0; /* rca << 16 */
696 mmcio->cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
698 mmcio->stop.opcode = 0;
701 case PROBE_GET_CID: /* XXX move to mmc_da */
702 init_standard_ccb(start_ccb, XPT_MMC_IO);
703 mmcio->cmd.opcode = MMC_ALL_SEND_CID;
705 mmcio->cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
706 mmcio->stop.opcode = 0;
708 case PROBE_SEND_RELATIVE_ADDR:
709 init_standard_ccb(start_ccb, XPT_MMC_IO);
710 mmcio->cmd.opcode = SD_SEND_RELATIVE_ADDR;
712 mmcio->cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
713 mmcio->stop.opcode = 0;
715 case PROBE_MMC_SET_RELATIVE_ADDR:
716 init_standard_ccb(start_ccb, XPT_MMC_IO);
717 mmcio->cmd.opcode = MMC_SET_RELATIVE_ADDR;
718 mmcio->cmd.arg = MMC_PROPOSED_RCA << 16;
719 mmcio->cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
720 mmcio->stop.opcode = 0;
722 case PROBE_SELECT_CARD:
723 init_standard_ccb(start_ccb, XPT_MMC_IO);
724 mmcio->cmd.opcode = MMC_SELECT_CARD;
725 mmcio->cmd.arg = (uint32_t)path->device->mmc_ident_data.card_rca << 16;
726 mmcio->cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
727 mmcio->stop.opcode = 0;
729 case PROBE_GET_CSD: /* XXX move to mmc_da */
730 init_standard_ccb(start_ccb, XPT_MMC_IO);
731 mmcio->cmd.opcode = MMC_SEND_CSD;
732 mmcio->cmd.arg = (uint32_t)path->device->mmc_ident_data.card_rca << 16;
733 mmcio->cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
734 mmcio->stop.opcode = 0;
737 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("Start with PROBE_DONE\n"));
738 init_standard_ccb(start_ccb, XPT_SET_TRAN_SETTINGS);
739 cts->ios.bus_mode = pushpull;
740 cts->ios_valid = MMC_BM;
741 xpt_action(start_ccb);
748 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("probestart: invalid action state 0x%x\n", softc->action));
749 panic("default: case in mmc_probe_start()");
752 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
753 xpt_action(start_ccb);
756 static void mmcprobe_cleanup(struct cam_periph *periph)
758 free(periph->softc, M_CAMXPT);
762 mmcprobe_done(struct cam_periph *periph, union ccb *done_ccb)
764 mmcprobe_softc *softc;
765 struct cam_path *path;
768 struct ccb_mmcio *mmcio;
771 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("mmcprobe_done\n"));
772 softc = (mmcprobe_softc *)periph->softc;
773 path = done_ccb->ccb_h.path;
774 priority = done_ccb->ccb_h.pinfo.priority;
776 switch (softc->action) {
781 printf("Starting completion of PROBE_RESET\n");
782 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE, ("done with PROBE_RESET\n"));
783 mmcio = &done_ccb->mmcio;
784 err = mmcio->cmd.error;
786 if (err != MMC_ERR_NONE) {
787 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
788 ("GO_IDLE_STATE failed with error %d\n",
791 /* There was a device there, but now it's gone... */
792 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
793 xpt_async(AC_LOST_DEVICE, path, NULL);
795 PROBE_SET_ACTION(softc, PROBE_INVALID);
798 path->device->protocol = PROTO_MMCSD;
799 PROBE_SET_ACTION(softc, PROBE_SEND_IF_COND);
802 case PROBE_SEND_IF_COND:
804 mmcio = &done_ccb->mmcio;
805 err = mmcio->cmd.error;
806 struct mmc_params *mmcp = &path->device->mmc_ident_data;
808 if (err != MMC_ERR_NONE || mmcio->cmd.resp[0] != 0x1AA) {
809 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
810 ("IF_COND: error %d, pattern %08x\n",
811 err, mmcio->cmd.resp[0]));
813 mmcp->card_features |= CARD_FEATURE_SD20;
814 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
815 ("SD 2.0 interface conditions: OK\n"));
818 PROBE_SET_ACTION(softc, PROBE_SDIO_RESET);
821 case PROBE_SDIO_RESET:
823 mmcio = &done_ccb->mmcio;
824 err = mmcio->cmd.error;
826 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
827 ("SDIO_RESET: error %d, CCCR CTL register: %08x\n",
828 err, mmcio->cmd.resp[0]));
829 PROBE_SET_ACTION(softc, PROBE_SDIO_INIT);
832 case PROBE_SDIO_INIT:
834 mmcio = &done_ccb->mmcio;
835 err = mmcio->cmd.error;
836 struct mmc_params *mmcp = &path->device->mmc_ident_data;
838 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
839 ("SDIO_INIT: error %d, %08x %08x %08x %08x\n",
840 err, mmcio->cmd.resp[0],
843 mmcio->cmd.resp[3]));
846 * Error here means that this card is not SDIO,
847 * so proceed with memory init as if nothing has happened
849 if (err != MMC_ERR_NONE) {
850 PROBE_SET_ACTION(softc, PROBE_SEND_APP_OP_COND);
853 mmcp->card_features |= CARD_FEATURE_SDIO;
854 uint32_t ioifcond = mmcio->cmd.resp[0];
855 uint32_t io_ocr = ioifcond & R4_IO_OCR_MASK;
857 mmcp->sdio_func_count = R4_IO_NUM_FUNCTIONS(ioifcond);
858 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
859 ("SDIO card: %d functions\n", mmcp->sdio_func_count));
861 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
862 ("SDIO OCR invalid?!\n"));
866 if (io_ocr != 0 && mmcp->io_ocr == 0) {
867 mmcp->io_ocr = io_ocr;
868 break; /* Retry, this time with non-0 OCR */
870 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
871 ("SDIO OCR: %08x\n", mmcp->io_ocr));
873 if (ioifcond & R4_IO_MEM_PRESENT) {
874 /* Combo card -- proceed to memory initialization */
875 PROBE_SET_ACTION(softc, PROBE_SEND_APP_OP_COND);
877 /* No memory portion -- get RCA and select card */
878 PROBE_SET_ACTION(softc, PROBE_SEND_RELATIVE_ADDR);
884 mmcio = &done_ccb->mmcio;
885 err = mmcio->cmd.error;
886 struct mmc_params *mmcp = &path->device->mmc_ident_data;
888 if (err != MMC_ERR_NONE) {
889 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
890 ("MMC_INIT: error %d, resp %08x\n",
891 err, mmcio->cmd.resp[0]));
892 PROBE_SET_ACTION(softc, PROBE_INVALID);
895 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
896 ("MMC card, OCR %08x\n", mmcio->cmd.resp[0]));
898 if (mmcp->card_ocr == 0) {
899 /* We haven't sent the OCR to the card yet -- do it */
900 mmcp->card_ocr = mmcio->cmd.resp[0];
901 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
902 ("-> sending OCR to card\n"));
906 if (!(mmcio->cmd.resp[0] & MMC_OCR_CARD_BUSY)) {
907 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
908 ("Card is still powering up\n"));
912 mmcp->card_features |= CARD_FEATURE_MMC | CARD_FEATURE_MEMORY;
913 PROBE_SET_ACTION(softc, PROBE_GET_CID);
916 case PROBE_SEND_APP_OP_COND:
918 mmcio = &done_ccb->mmcio;
919 err = mmcio->cmd.error;
921 if (err != MMC_ERR_NONE) {
922 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
923 ("APP_OP_COND: error %d, resp %08x\n",
924 err, mmcio->cmd.resp[0]));
925 PROBE_SET_ACTION(softc, PROBE_MMC_INIT);
929 if (!(softc->flags & PROBE_FLAG_ACMD_SENT)) {
930 /* Don't change the state */
931 softc->flags |= PROBE_FLAG_ACMD_SENT;
935 softc->flags &= ~PROBE_FLAG_ACMD_SENT;
936 if ((mmcio->cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
937 (mmcio->cmd.arg & MMC_OCR_VOLTAGE) == 0) {
938 struct mmc_params *mmcp = &path->device->mmc_ident_data;
939 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
940 ("Card OCR: %08x\n", mmcio->cmd.resp[0]));
941 if (mmcp->card_ocr == 0) {
942 mmcp->card_ocr = mmcio->cmd.resp[0];
943 /* Now when we know OCR that we want -- send it to card */
944 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
945 ("-> sending OCR to card\n"));
947 /* We already know the OCR and despite of that we
948 * are processing the answer to ACMD41 -> move on
950 PROBE_SET_ACTION(softc, PROBE_GET_CID);
952 /* Getting an answer to ACMD41 means the card has memory */
953 mmcp->card_features |= CARD_FEATURE_MEMORY;
955 /* Standard capacity vs High Capacity memory card */
956 if (mmcio->cmd.resp[0] & MMC_OCR_CCS) {
957 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
959 mmcp->card_features |= CARD_FEATURE_SDHC;
962 /* Whether the card supports 1.8V signaling */
963 if (mmcio->cmd.resp[0] & MMC_OCR_S18A) {
964 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
965 ("Card supports 1.8V signaling\n"));
966 mmcp->card_features |= CARD_FEATURE_18V;
969 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
970 ("Card not ready: %08x\n", mmcio->cmd.resp[0]));
971 /* Send CMD55+ACMD41 once again */
972 PROBE_SET_ACTION(softc, PROBE_SEND_APP_OP_COND);
977 case PROBE_GET_CID: /* XXX move to mmc_da */
979 mmcio = &done_ccb->mmcio;
980 err = mmcio->cmd.error;
982 if (err != MMC_ERR_NONE) {
983 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
984 ("PROBE_GET_CID: error %d\n", err));
985 PROBE_SET_ACTION(softc, PROBE_INVALID);
989 struct mmc_params *mmcp = &path->device->mmc_ident_data;
990 memcpy(mmcp->card_cid, mmcio->cmd.resp, 4 * sizeof(uint32_t));
991 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
992 ("CID %08x%08x%08x%08x\n",
997 if (mmcp->card_features & CARD_FEATURE_MMC)
998 PROBE_SET_ACTION(softc, PROBE_MMC_SET_RELATIVE_ADDR);
1000 PROBE_SET_ACTION(softc, PROBE_SEND_RELATIVE_ADDR);
1003 case PROBE_SEND_RELATIVE_ADDR: {
1004 mmcio = &done_ccb->mmcio;
1005 err = mmcio->cmd.error;
1006 struct mmc_params *mmcp = &path->device->mmc_ident_data;
1007 uint16_t rca = mmcio->cmd.resp[0] >> 16;
1008 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1009 ("Card published RCA: %u\n", rca));
1010 path->device->mmc_ident_data.card_rca = rca;
1011 if (err != MMC_ERR_NONE) {
1012 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1013 ("PROBE_SEND_RELATIVE_ADDR: error %d\n", err));
1014 PROBE_SET_ACTION(softc, PROBE_INVALID);
1018 /* If memory is present, get CSD, otherwise select card */
1019 if (mmcp->card_features & CARD_FEATURE_MEMORY)
1020 PROBE_SET_ACTION(softc, PROBE_GET_CSD);
1022 PROBE_SET_ACTION(softc, PROBE_SELECT_CARD);
1025 case PROBE_MMC_SET_RELATIVE_ADDR:
1026 mmcio = &done_ccb->mmcio;
1027 err = mmcio->cmd.error;
1028 if (err != MMC_ERR_NONE) {
1029 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1030 ("PROBE_MMC_SET_RELATIVE_ADDR: error %d\n", err));
1031 PROBE_SET_ACTION(softc, PROBE_INVALID);
1034 path->device->mmc_ident_data.card_rca = MMC_PROPOSED_RCA;
1035 PROBE_SET_ACTION(softc, PROBE_GET_CSD);
1037 case PROBE_GET_CSD: {
1038 mmcio = &done_ccb->mmcio;
1039 err = mmcio->cmd.error;
1041 if (err != MMC_ERR_NONE) {
1042 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1043 ("PROBE_GET_CSD: error %d\n", err));
1044 PROBE_SET_ACTION(softc, PROBE_INVALID);
1048 struct mmc_params *mmcp = &path->device->mmc_ident_data;
1049 memcpy(mmcp->card_csd, mmcio->cmd.resp, 4 * sizeof(uint32_t));
1050 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1051 ("CSD %08x%08x%08x%08x\n",
1055 mmcp->card_csd[3]));
1056 PROBE_SET_ACTION(softc, PROBE_SELECT_CARD);
1059 case PROBE_SELECT_CARD: {
1060 mmcio = &done_ccb->mmcio;
1061 err = mmcio->cmd.error;
1062 if (err != MMC_ERR_NONE) {
1063 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1064 ("PROBE_SEND_RELATIVE_ADDR: error %d\n", err));
1065 PROBE_SET_ACTION(softc, PROBE_INVALID);
1069 PROBE_SET_ACTION(softc, PROBE_DONE);
1073 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1074 ("mmc_probedone: invalid action state 0x%x\n", softc->action));
1075 panic("default: case in mmc_probe_done()");
1078 if (softc->action == PROBE_INVALID &&
1079 (path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
1080 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_PROBE,
1081 ("mmc_probedone: Should send AC_LOST_DEVICE but won't for now\n"));
1082 //xpt_async(AC_LOST_DEVICE, path, NULL);
1085 if (softc->action != PROBE_INVALID)
1086 xpt_schedule(periph, priority);
1087 /* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
1088 int frozen = cam_release_devq(path, 0, 0, 0, FALSE);
1089 printf("mmc_probedone: remaining freezecnt %d\n", frozen);
1091 if (softc->action == PROBE_DONE) {
1092 /* Notify the system that the device is found! */
1093 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
1094 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1095 xpt_acquire_device(path->device);
1096 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1097 xpt_action(done_ccb);
1098 xpt_async(AC_FOUND_DEVICE, path, done_ccb);
1101 xpt_release_ccb(done_ccb);
1102 if (softc->action == PROBE_DONE || softc->action == PROBE_INVALID) {
1103 cam_periph_invalidate(periph);
1104 cam_periph_release_locked(periph);