1 /*- 2 * Copyright (c) 2006 Bernd Walter. All rights reserved. 3 * Copyright (c) 2006 M. Warner Losh <imp@FreeBSD.org> 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 * 25 * Portions of this software may have been developed with reference to 26 * the SD Simplified Specification. The following disclaimer may apply: 27 * 28 * The following conditions apply to the release of the simplified 29 * specification ("Simplified Specification") by the SD Card Association and 30 * the SD Group. The Simplified Specification is a subset of the complete SD 31 * Specification which is owned by the SD Card Association and the SD 32 * Group. This Simplified Specification is provided on a non-confidential 33 * basis subject to the disclaimers below. Any implementation of the 34 * Simplified Specification may require a license from the SD Card 35 * Association, SD Group, SD-3C LLC or other third parties. 36 * 37 * Disclaimers: 38 * 39 * The information contained in the Simplified Specification is presented only 40 * as a standard specification for SD Cards and SD Host/Ancillary products and 41 * is provided "AS-IS" without any representations or warranties of any 42 * kind. No responsibility is assumed by the SD Group, SD-3C LLC or the SD 43 * Card Association for any damages, any infringements of patents or other 44 * right of the SD Group, SD-3C LLC, the SD Card Association or any third 45 * parties, which may result from its use. No license is granted by 46 * implication, estoppel or otherwise under any patent or other rights of the 47 * SD Group, SD-3C LLC, the SD Card Association or any third party. Nothing 48 * herein shall be construed as an obligation by the SD Group, the SD-3C LLC 49 * or the SD Card Association to disclose or distribute any technical 50 * information, know-how or other confidential information to any third party. 51 */ 52 53 #include <sys/cdefs.h> 54 __FBSDID("$FreeBSD$"); 55 56 #include <sys/param.h> 57 #include <sys/systm.h> 58 #include <sys/kernel.h> 59 #include <sys/lock.h> 60 #include <sys/mutex.h> 61 #include <sys/time.h> 62 63 #include <dev/mmc/bridge.h> 64 #include <dev/mmc/mmc_private.h> 65 #include <dev/mmc/mmc_subr.h> 66 #include <dev/mmc/mmcreg.h> 67 #include <dev/mmc/mmcbrvar.h> 68 69 #include "mmcbus_if.h" 70 71 #define CMD_RETRIES 3 72 #define LOG_PPS 5 /* Log no more than 5 errors per second. */ 73 74 int 75 mmc_wait_for_cmd(device_t busdev, device_t dev, struct mmc_command *cmd, 76 int retries) 77 { 78 struct mmc_request mreq; 79 struct mmc_softc *sc; 80 int err; 81 82 do { 83 memset(&mreq, 0, sizeof(mreq)); 84 memset(cmd->resp, 0, sizeof(cmd->resp)); 85 cmd->retries = 0; /* Retries done here, not in hardware. */ 86 cmd->mrq = &mreq; 87 if (cmd->data != NULL) 88 cmd->data->mrq = &mreq; 89 mreq.cmd = cmd; 90 if (MMCBUS_WAIT_FOR_REQUEST(busdev, dev, &mreq) != 0) 91 err = MMC_ERR_FAILED; 92 else 93 err = cmd->error; 94 } while (err != MMC_ERR_NONE && retries-- > 0); 95 96 if (err != MMC_ERR_NONE && busdev == dev) { 97 sc = device_get_softc(busdev); 98 if (sc->squelched == 0 && ppsratecheck(&sc->log_time, 99 &sc->log_count, LOG_PPS)) { 100 device_printf(sc->dev, "CMD%d failed, RESULT: %d\n", 101 cmd->opcode, err); 102 } 103 } 104 105 return (err); 106 } 107 108 int 109 mmc_wait_for_app_cmd(device_t busdev, device_t dev, uint16_t rca, 110 struct mmc_command *cmd, int retries) 111 { 112 struct mmc_command appcmd; 113 struct mmc_softc *sc; 114 int err; 115 116 sc = device_get_softc(busdev); 117 118 /* Squelch error reporting at lower levels, we report below. */ 119 sc->squelched++; 120 do { 121 memset(&appcmd, 0, sizeof(appcmd)); 122 appcmd.opcode = MMC_APP_CMD; 123 appcmd.arg = (uint32_t)rca << 16; 124 appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 125 if (mmc_wait_for_cmd(busdev, dev, &appcmd, 0) != 0) 126 err = MMC_ERR_FAILED; 127 else 128 err = appcmd.error; 129 if (err == MMC_ERR_NONE) { 130 if (!(appcmd.resp[0] & R1_APP_CMD)) 131 err = MMC_ERR_FAILED; 132 else if (mmc_wait_for_cmd(busdev, dev, cmd, 0) != 0) 133 err = MMC_ERR_FAILED; 134 else 135 err = cmd->error; 136 } 137 } while (err != MMC_ERR_NONE && retries-- > 0); 138 sc->squelched--; 139 140 if (err != MMC_ERR_NONE && busdev == dev) { 141 if (sc->squelched == 0 && ppsratecheck(&sc->log_time, 142 &sc->log_count, LOG_PPS)) { 143 device_printf(sc->dev, "ACMD%d failed, RESULT: %d\n", 144 cmd->opcode, err); 145 } 146 } 147 148 return (err); 149 } 150 151 int 152 mmc_switch(device_t busdev, device_t dev, uint16_t rca, uint8_t set, 153 uint8_t index, uint8_t value, u_int timeout, bool status) 154 { 155 struct mmc_command cmd; 156 struct mmc_softc *sc; 157 int err; 158 159 KASSERT(timeout != 0, ("%s: no timeout", __func__)); 160 161 sc = device_get_softc(busdev); 162 163 memset(&cmd, 0, sizeof(cmd)); 164 cmd.opcode = MMC_SWITCH_FUNC; 165 cmd.arg = (MMC_SWITCH_FUNC_WR << 24) | (index << 16) | (value << 8) | 166 set; 167 /* 168 * If the hardware supports busy detection but the switch timeout 169 * exceeds the maximum host timeout, use a R1 instead of a R1B 170 * response in order to keep the hardware from timing out. 171 */ 172 if (mmcbr_get_caps(busdev) & MMC_CAP_WAIT_WHILE_BUSY && 173 timeout > mmcbr_get_max_busy_timeout(busdev)) 174 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 175 else 176 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC; 177 /* 178 * Pause re-tuning so it won't interfere with the busy state and also 179 * so that the result of CMD13 will always refer to switching rather 180 * than to a tuning command that may have snuck in between. 181 */ 182 sc->retune_paused++; 183 err = mmc_wait_for_cmd(busdev, dev, &cmd, CMD_RETRIES); 184 if (err != MMC_ERR_NONE || status == false) 185 goto out; 186 err = mmc_switch_status(busdev, dev, rca, timeout); 187 out: 188 sc->retune_paused--; 189 return (err); 190 } 191 192 int 193 mmc_switch_status(device_t busdev, device_t dev, uint16_t rca, u_int timeout) 194 { 195 struct timeval cur, end; 196 int err; 197 uint32_t status; 198 199 KASSERT(timeout != 0, ("%s: no timeout", __func__)); 200 201 /* 202 * Note that when using a R1B response in mmc_switch(), bridges of 203 * type MMC_CAP_WAIT_WHILE_BUSY will issue mmc_send_status() only 204 * once and then exit the loop. 205 */ 206 end.tv_sec = end.tv_usec = 0; 207 for (;;) { 208 err = mmc_send_status(busdev, dev, rca, &status); 209 if (err != MMC_ERR_NONE) 210 break; 211 if (R1_CURRENT_STATE(status) == R1_STATE_TRAN) 212 break; 213 getmicrouptime(&cur); 214 if (end.tv_sec == 0 && end.tv_usec == 0) { 215 end.tv_usec = timeout; 216 timevaladd(&end, &cur); 217 } 218 if (timevalcmp(&cur, &end, >)) { 219 err = MMC_ERR_TIMEOUT; 220 break; 221 } 222 } 223 if (err == MMC_ERR_NONE && (status & R1_SWITCH_ERROR) != 0) 224 return (MMC_ERR_FAILED); 225 return (err); 226 } 227 228 int 229 mmc_send_ext_csd(device_t busdev, device_t dev, uint8_t *rawextcsd) 230 { 231 struct mmc_command cmd; 232 struct mmc_data data; 233 int err; 234 235 memset(&cmd, 0, sizeof(cmd)); 236 memset(&data, 0, sizeof(data)); 237 238 memset(rawextcsd, 0, MMC_EXTCSD_SIZE); 239 cmd.opcode = MMC_SEND_EXT_CSD; 240 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 241 cmd.data = &data; 242 243 data.data = rawextcsd; 244 data.len = MMC_EXTCSD_SIZE; 245 data.flags = MMC_DATA_READ; 246 247 err = mmc_wait_for_cmd(busdev, dev, &cmd, CMD_RETRIES); 248 return (err); 249 } 250 251 int 252 mmc_send_status(device_t busdev, device_t dev, uint16_t rca, uint32_t *status) 253 { 254 struct mmc_command cmd; 255 int err; 256 257 memset(&cmd, 0, sizeof(cmd)); 258 cmd.opcode = MMC_SEND_STATUS; 259 cmd.arg = (uint32_t)rca << 16; 260 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 261 err = mmc_wait_for_cmd(busdev, dev, &cmd, CMD_RETRIES); 262 *status = cmd.resp[0]; 263 return (err); 264 } 265