diff options
Diffstat (limited to 'drivers/mmc/core/core.c')
-rw-r--r-- | drivers/mmc/core/core.c | 461 |
1 files changed, 418 insertions, 43 deletions
diff --git a/drivers/mmc/core/core.c b/drivers/mmc/core/core.c index 91a0a7460ebb..5278ffb20e74 100644 --- a/drivers/mmc/core/core.c +++ b/drivers/mmc/core/core.c | |||
@@ -24,6 +24,8 @@ | |||
24 | #include <linux/regulator/consumer.h> | 24 | #include <linux/regulator/consumer.h> |
25 | #include <linux/pm_runtime.h> | 25 | #include <linux/pm_runtime.h> |
26 | #include <linux/suspend.h> | 26 | #include <linux/suspend.h> |
27 | #include <linux/fault-inject.h> | ||
28 | #include <linux/random.h> | ||
27 | 29 | ||
28 | #include <linux/mmc/card.h> | 30 | #include <linux/mmc/card.h> |
29 | #include <linux/mmc/host.h> | 31 | #include <linux/mmc/host.h> |
@@ -83,6 +85,43 @@ static void mmc_flush_scheduled_work(void) | |||
83 | flush_workqueue(workqueue); | 85 | flush_workqueue(workqueue); |
84 | } | 86 | } |
85 | 87 | ||
88 | #ifdef CONFIG_FAIL_MMC_REQUEST | ||
89 | |||
90 | /* | ||
91 | * Internal function. Inject random data errors. | ||
92 | * If mmc_data is NULL no errors are injected. | ||
93 | */ | ||
94 | static void mmc_should_fail_request(struct mmc_host *host, | ||
95 | struct mmc_request *mrq) | ||
96 | { | ||
97 | struct mmc_command *cmd = mrq->cmd; | ||
98 | struct mmc_data *data = mrq->data; | ||
99 | static const int data_errors[] = { | ||
100 | -ETIMEDOUT, | ||
101 | -EILSEQ, | ||
102 | -EIO, | ||
103 | }; | ||
104 | |||
105 | if (!data) | ||
106 | return; | ||
107 | |||
108 | if (cmd->error || data->error || | ||
109 | !should_fail(&host->fail_mmc_request, data->blksz * data->blocks)) | ||
110 | return; | ||
111 | |||
112 | data->error = data_errors[random32() % ARRAY_SIZE(data_errors)]; | ||
113 | data->bytes_xfered = (random32() % (data->bytes_xfered >> 9)) << 9; | ||
114 | } | ||
115 | |||
116 | #else /* CONFIG_FAIL_MMC_REQUEST */ | ||
117 | |||
118 | static inline void mmc_should_fail_request(struct mmc_host *host, | ||
119 | struct mmc_request *mrq) | ||
120 | { | ||
121 | } | ||
122 | |||
123 | #endif /* CONFIG_FAIL_MMC_REQUEST */ | ||
124 | |||
86 | /** | 125 | /** |
87 | * mmc_request_done - finish processing an MMC request | 126 | * mmc_request_done - finish processing an MMC request |
88 | * @host: MMC host which completed request | 127 | * @host: MMC host which completed request |
@@ -102,13 +141,15 @@ void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq) | |||
102 | } | 141 | } |
103 | 142 | ||
104 | if (err && cmd->retries) { | 143 | if (err && cmd->retries) { |
105 | pr_debug("%s: req failed (CMD%u): %d, retrying...\n", | 144 | /* |
106 | mmc_hostname(host), cmd->opcode, err); | 145 | * Request starter must handle retries - see |
107 | 146 | * mmc_wait_for_req_done(). | |
108 | cmd->retries--; | 147 | */ |
109 | cmd->error = 0; | 148 | if (mrq->done) |
110 | host->ops->request(host, mrq); | 149 | mrq->done(mrq); |
111 | } else { | 150 | } else { |
151 | mmc_should_fail_request(host, mrq); | ||
152 | |||
112 | led_trigger_event(host->led, LED_OFF); | 153 | led_trigger_event(host->led, LED_OFF); |
113 | 154 | ||
114 | pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n", | 155 | pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n", |
@@ -133,7 +174,7 @@ void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq) | |||
133 | if (mrq->done) | 174 | if (mrq->done) |
134 | mrq->done(mrq); | 175 | mrq->done(mrq); |
135 | 176 | ||
136 | mmc_host_clk_gate(host); | 177 | mmc_host_clk_release(host); |
137 | } | 178 | } |
138 | } | 179 | } |
139 | 180 | ||
@@ -192,7 +233,7 @@ mmc_start_request(struct mmc_host *host, struct mmc_request *mrq) | |||
192 | mrq->stop->mrq = mrq; | 233 | mrq->stop->mrq = mrq; |
193 | } | 234 | } |
194 | } | 235 | } |
195 | mmc_host_clk_ungate(host); | 236 | mmc_host_clk_hold(host); |
196 | led_trigger_event(host->led, LED_FULL); | 237 | led_trigger_event(host->led, LED_FULL); |
197 | host->ops->request(host, mrq); | 238 | host->ops->request(host, mrq); |
198 | } | 239 | } |
@@ -212,7 +253,21 @@ static void __mmc_start_req(struct mmc_host *host, struct mmc_request *mrq) | |||
212 | static void mmc_wait_for_req_done(struct mmc_host *host, | 253 | static void mmc_wait_for_req_done(struct mmc_host *host, |
213 | struct mmc_request *mrq) | 254 | struct mmc_request *mrq) |
214 | { | 255 | { |
215 | wait_for_completion(&mrq->completion); | 256 | struct mmc_command *cmd; |
257 | |||
258 | while (1) { | ||
259 | wait_for_completion(&mrq->completion); | ||
260 | |||
261 | cmd = mrq->cmd; | ||
262 | if (!cmd->error || !cmd->retries) | ||
263 | break; | ||
264 | |||
265 | pr_debug("%s: req failed (CMD%u): %d, retrying...\n", | ||
266 | mmc_hostname(host), cmd->opcode, cmd->error); | ||
267 | cmd->retries--; | ||
268 | cmd->error = 0; | ||
269 | host->ops->request(host, mrq); | ||
270 | } | ||
216 | } | 271 | } |
217 | 272 | ||
218 | /** | 273 | /** |
@@ -279,8 +334,14 @@ struct mmc_async_req *mmc_start_req(struct mmc_host *host, | |||
279 | mmc_wait_for_req_done(host, host->areq->mrq); | 334 | mmc_wait_for_req_done(host, host->areq->mrq); |
280 | err = host->areq->err_check(host->card, host->areq); | 335 | err = host->areq->err_check(host->card, host->areq); |
281 | if (err) { | 336 | if (err) { |
337 | /* post process the completed failed request */ | ||
282 | mmc_post_req(host, host->areq->mrq, 0); | 338 | mmc_post_req(host, host->areq->mrq, 0); |
283 | if (areq) | 339 | if (areq) |
340 | /* | ||
341 | * Cancel the new prepared request, because | ||
342 | * it can't run until the failed | ||
343 | * request has been properly handled. | ||
344 | */ | ||
284 | mmc_post_req(host, areq->mrq, -EINVAL); | 345 | mmc_post_req(host, areq->mrq, -EINVAL); |
285 | 346 | ||
286 | host->areq = NULL; | 347 | host->areq = NULL; |
@@ -319,6 +380,63 @@ void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq) | |||
319 | EXPORT_SYMBOL(mmc_wait_for_req); | 380 | EXPORT_SYMBOL(mmc_wait_for_req); |
320 | 381 | ||
321 | /** | 382 | /** |
383 | * mmc_interrupt_hpi - Issue for High priority Interrupt | ||
384 | * @card: the MMC card associated with the HPI transfer | ||
385 | * | ||
386 | * Issued High Priority Interrupt, and check for card status | ||
387 | * util out-of prg-state. | ||
388 | */ | ||
389 | int mmc_interrupt_hpi(struct mmc_card *card) | ||
390 | { | ||
391 | int err; | ||
392 | u32 status; | ||
393 | |||
394 | BUG_ON(!card); | ||
395 | |||
396 | if (!card->ext_csd.hpi_en) { | ||
397 | pr_info("%s: HPI enable bit unset\n", mmc_hostname(card->host)); | ||
398 | return 1; | ||
399 | } | ||
400 | |||
401 | mmc_claim_host(card->host); | ||
402 | err = mmc_send_status(card, &status); | ||
403 | if (err) { | ||
404 | pr_err("%s: Get card status fail\n", mmc_hostname(card->host)); | ||
405 | goto out; | ||
406 | } | ||
407 | |||
408 | /* | ||
409 | * If the card status is in PRG-state, we can send the HPI command. | ||
410 | */ | ||
411 | if (R1_CURRENT_STATE(status) == R1_STATE_PRG) { | ||
412 | do { | ||
413 | /* | ||
414 | * We don't know when the HPI command will finish | ||
415 | * processing, so we need to resend HPI until out | ||
416 | * of prg-state, and keep checking the card status | ||
417 | * with SEND_STATUS. If a timeout error occurs when | ||
418 | * sending the HPI command, we are already out of | ||
419 | * prg-state. | ||
420 | */ | ||
421 | err = mmc_send_hpi_cmd(card, &status); | ||
422 | if (err) | ||
423 | pr_debug("%s: abort HPI (%d error)\n", | ||
424 | mmc_hostname(card->host), err); | ||
425 | |||
426 | err = mmc_send_status(card, &status); | ||
427 | if (err) | ||
428 | break; | ||
429 | } while (R1_CURRENT_STATE(status) == R1_STATE_PRG); | ||
430 | } else | ||
431 | pr_debug("%s: Left prg-state\n", mmc_hostname(card->host)); | ||
432 | |||
433 | out: | ||
434 | mmc_release_host(card->host); | ||
435 | return err; | ||
436 | } | ||
437 | EXPORT_SYMBOL(mmc_interrupt_hpi); | ||
438 | |||
439 | /** | ||
322 | * mmc_wait_for_cmd - start a command and wait for completion | 440 | * mmc_wait_for_cmd - start a command and wait for completion |
323 | * @host: MMC host to start command | 441 | * @host: MMC host to start command |
324 | * @cmd: MMC command to start | 442 | * @cmd: MMC command to start |
@@ -330,7 +448,7 @@ EXPORT_SYMBOL(mmc_wait_for_req); | |||
330 | */ | 448 | */ |
331 | int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries) | 449 | int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries) |
332 | { | 450 | { |
333 | struct mmc_request mrq = {0}; | 451 | struct mmc_request mrq = {NULL}; |
334 | 452 | ||
335 | WARN_ON(!host->claimed); | 453 | WARN_ON(!host->claimed); |
336 | 454 | ||
@@ -728,15 +846,17 @@ static inline void mmc_set_ios(struct mmc_host *host) | |||
728 | */ | 846 | */ |
729 | void mmc_set_chip_select(struct mmc_host *host, int mode) | 847 | void mmc_set_chip_select(struct mmc_host *host, int mode) |
730 | { | 848 | { |
849 | mmc_host_clk_hold(host); | ||
731 | host->ios.chip_select = mode; | 850 | host->ios.chip_select = mode; |
732 | mmc_set_ios(host); | 851 | mmc_set_ios(host); |
852 | mmc_host_clk_release(host); | ||
733 | } | 853 | } |
734 | 854 | ||
735 | /* | 855 | /* |
736 | * Sets the host clock to the highest possible frequency that | 856 | * Sets the host clock to the highest possible frequency that |
737 | * is below "hz". | 857 | * is below "hz". |
738 | */ | 858 | */ |
739 | void mmc_set_clock(struct mmc_host *host, unsigned int hz) | 859 | static void __mmc_set_clock(struct mmc_host *host, unsigned int hz) |
740 | { | 860 | { |
741 | WARN_ON(hz < host->f_min); | 861 | WARN_ON(hz < host->f_min); |
742 | 862 | ||
@@ -747,6 +867,13 @@ void mmc_set_clock(struct mmc_host *host, unsigned int hz) | |||
747 | mmc_set_ios(host); | 867 | mmc_set_ios(host); |
748 | } | 868 | } |
749 | 869 | ||
870 | void mmc_set_clock(struct mmc_host *host, unsigned int hz) | ||
871 | { | ||
872 | mmc_host_clk_hold(host); | ||
873 | __mmc_set_clock(host, hz); | ||
874 | mmc_host_clk_release(host); | ||
875 | } | ||
876 | |||
750 | #ifdef CONFIG_MMC_CLKGATE | 877 | #ifdef CONFIG_MMC_CLKGATE |
751 | /* | 878 | /* |
752 | * This gates the clock by setting it to 0 Hz. | 879 | * This gates the clock by setting it to 0 Hz. |
@@ -779,7 +906,7 @@ void mmc_ungate_clock(struct mmc_host *host) | |||
779 | if (host->clk_old) { | 906 | if (host->clk_old) { |
780 | BUG_ON(host->ios.clock); | 907 | BUG_ON(host->ios.clock); |
781 | /* This call will also set host->clk_gated to false */ | 908 | /* This call will also set host->clk_gated to false */ |
782 | mmc_set_clock(host, host->clk_old); | 909 | __mmc_set_clock(host, host->clk_old); |
783 | } | 910 | } |
784 | } | 911 | } |
785 | 912 | ||
@@ -807,8 +934,10 @@ void mmc_set_ungated(struct mmc_host *host) | |||
807 | */ | 934 | */ |
808 | void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode) | 935 | void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode) |
809 | { | 936 | { |
937 | mmc_host_clk_hold(host); | ||
810 | host->ios.bus_mode = mode; | 938 | host->ios.bus_mode = mode; |
811 | mmc_set_ios(host); | 939 | mmc_set_ios(host); |
940 | mmc_host_clk_release(host); | ||
812 | } | 941 | } |
813 | 942 | ||
814 | /* | 943 | /* |
@@ -816,8 +945,10 @@ void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode) | |||
816 | */ | 945 | */ |
817 | void mmc_set_bus_width(struct mmc_host *host, unsigned int width) | 946 | void mmc_set_bus_width(struct mmc_host *host, unsigned int width) |
818 | { | 947 | { |
948 | mmc_host_clk_hold(host); | ||
819 | host->ios.bus_width = width; | 949 | host->ios.bus_width = width; |
820 | mmc_set_ios(host); | 950 | mmc_set_ios(host); |
951 | mmc_host_clk_release(host); | ||
821 | } | 952 | } |
822 | 953 | ||
823 | /** | 954 | /** |
@@ -1015,8 +1146,10 @@ u32 mmc_select_voltage(struct mmc_host *host, u32 ocr) | |||
1015 | 1146 | ||
1016 | ocr &= 3 << bit; | 1147 | ocr &= 3 << bit; |
1017 | 1148 | ||
1149 | mmc_host_clk_hold(host); | ||
1018 | host->ios.vdd = bit; | 1150 | host->ios.vdd = bit; |
1019 | mmc_set_ios(host); | 1151 | mmc_set_ios(host); |
1152 | mmc_host_clk_release(host); | ||
1020 | } else { | 1153 | } else { |
1021 | pr_warning("%s: host doesn't support card's voltages\n", | 1154 | pr_warning("%s: host doesn't support card's voltages\n", |
1022 | mmc_hostname(host)); | 1155 | mmc_hostname(host)); |
@@ -1063,8 +1196,10 @@ int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage, bool cmd11 | |||
1063 | */ | 1196 | */ |
1064 | void mmc_set_timing(struct mmc_host *host, unsigned int timing) | 1197 | void mmc_set_timing(struct mmc_host *host, unsigned int timing) |
1065 | { | 1198 | { |
1199 | mmc_host_clk_hold(host); | ||
1066 | host->ios.timing = timing; | 1200 | host->ios.timing = timing; |
1067 | mmc_set_ios(host); | 1201 | mmc_set_ios(host); |
1202 | mmc_host_clk_release(host); | ||
1068 | } | 1203 | } |
1069 | 1204 | ||
1070 | /* | 1205 | /* |
@@ -1072,8 +1207,10 @@ void mmc_set_timing(struct mmc_host *host, unsigned int timing) | |||
1072 | */ | 1207 | */ |
1073 | void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type) | 1208 | void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type) |
1074 | { | 1209 | { |
1210 | mmc_host_clk_hold(host); | ||
1075 | host->ios.drv_type = drv_type; | 1211 | host->ios.drv_type = drv_type; |
1076 | mmc_set_ios(host); | 1212 | mmc_set_ios(host); |
1213 | mmc_host_clk_release(host); | ||
1077 | } | 1214 | } |
1078 | 1215 | ||
1079 | /* | 1216 | /* |
@@ -1091,6 +1228,8 @@ static void mmc_power_up(struct mmc_host *host) | |||
1091 | { | 1228 | { |
1092 | int bit; | 1229 | int bit; |
1093 | 1230 | ||
1231 | mmc_host_clk_hold(host); | ||
1232 | |||
1094 | /* If ocr is set, we use it */ | 1233 | /* If ocr is set, we use it */ |
1095 | if (host->ocr) | 1234 | if (host->ocr) |
1096 | bit = ffs(host->ocr) - 1; | 1235 | bit = ffs(host->ocr) - 1; |
@@ -1098,13 +1237,11 @@ static void mmc_power_up(struct mmc_host *host) | |||
1098 | bit = fls(host->ocr_avail) - 1; | 1237 | bit = fls(host->ocr_avail) - 1; |
1099 | 1238 | ||
1100 | host->ios.vdd = bit; | 1239 | host->ios.vdd = bit; |
1101 | if (mmc_host_is_spi(host)) { | 1240 | if (mmc_host_is_spi(host)) |
1102 | host->ios.chip_select = MMC_CS_HIGH; | 1241 | host->ios.chip_select = MMC_CS_HIGH; |
1103 | host->ios.bus_mode = MMC_BUSMODE_PUSHPULL; | 1242 | else |
1104 | } else { | ||
1105 | host->ios.chip_select = MMC_CS_DONTCARE; | 1243 | host->ios.chip_select = MMC_CS_DONTCARE; |
1106 | host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; | 1244 | host->ios.bus_mode = MMC_BUSMODE_PUSHPULL; |
1107 | } | ||
1108 | host->ios.power_mode = MMC_POWER_UP; | 1245 | host->ios.power_mode = MMC_POWER_UP; |
1109 | host->ios.bus_width = MMC_BUS_WIDTH_1; | 1246 | host->ios.bus_width = MMC_BUS_WIDTH_1; |
1110 | host->ios.timing = MMC_TIMING_LEGACY; | 1247 | host->ios.timing = MMC_TIMING_LEGACY; |
@@ -1126,13 +1263,49 @@ static void mmc_power_up(struct mmc_host *host) | |||
1126 | * time required to reach a stable voltage. | 1263 | * time required to reach a stable voltage. |
1127 | */ | 1264 | */ |
1128 | mmc_delay(10); | 1265 | mmc_delay(10); |
1266 | |||
1267 | mmc_host_clk_release(host); | ||
1129 | } | 1268 | } |
1130 | 1269 | ||
1131 | static void mmc_power_off(struct mmc_host *host) | 1270 | void mmc_power_off(struct mmc_host *host) |
1132 | { | 1271 | { |
1272 | struct mmc_card *card; | ||
1273 | unsigned int notify_type; | ||
1274 | unsigned int timeout; | ||
1275 | int err; | ||
1276 | |||
1277 | mmc_host_clk_hold(host); | ||
1278 | |||
1279 | card = host->card; | ||
1133 | host->ios.clock = 0; | 1280 | host->ios.clock = 0; |
1134 | host->ios.vdd = 0; | 1281 | host->ios.vdd = 0; |
1135 | 1282 | ||
1283 | if (card && mmc_card_mmc(card) && | ||
1284 | (card->poweroff_notify_state == MMC_POWERED_ON)) { | ||
1285 | |||
1286 | if (host->power_notify_type == MMC_HOST_PW_NOTIFY_SHORT) { | ||
1287 | notify_type = EXT_CSD_POWER_OFF_SHORT; | ||
1288 | timeout = card->ext_csd.generic_cmd6_time; | ||
1289 | card->poweroff_notify_state = MMC_POWEROFF_SHORT; | ||
1290 | } else { | ||
1291 | notify_type = EXT_CSD_POWER_OFF_LONG; | ||
1292 | timeout = card->ext_csd.power_off_longtime; | ||
1293 | card->poweroff_notify_state = MMC_POWEROFF_LONG; | ||
1294 | } | ||
1295 | |||
1296 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, | ||
1297 | EXT_CSD_POWER_OFF_NOTIFICATION, | ||
1298 | notify_type, timeout); | ||
1299 | |||
1300 | if (err && err != -EBADMSG) | ||
1301 | pr_err("Device failed to respond within %d poweroff " | ||
1302 | "time. Forcefully powering down the device\n", | ||
1303 | timeout); | ||
1304 | |||
1305 | /* Set the card state to no notification after the poweroff */ | ||
1306 | card->poweroff_notify_state = MMC_NO_POWER_NOTIFICATION; | ||
1307 | } | ||
1308 | |||
1136 | /* | 1309 | /* |
1137 | * Reset ocr mask to be the highest possible voltage supported for | 1310 | * Reset ocr mask to be the highest possible voltage supported for |
1138 | * this mmc host. This value will be used at next power up. | 1311 | * this mmc host. This value will be used at next power up. |
@@ -1147,6 +1320,15 @@ static void mmc_power_off(struct mmc_host *host) | |||
1147 | host->ios.bus_width = MMC_BUS_WIDTH_1; | 1320 | host->ios.bus_width = MMC_BUS_WIDTH_1; |
1148 | host->ios.timing = MMC_TIMING_LEGACY; | 1321 | host->ios.timing = MMC_TIMING_LEGACY; |
1149 | mmc_set_ios(host); | 1322 | mmc_set_ios(host); |
1323 | |||
1324 | /* | ||
1325 | * Some configurations, such as the 802.11 SDIO card in the OLPC | ||
1326 | * XO-1.5, require a short delay after poweroff before the card | ||
1327 | * can be successfully turned on again. | ||
1328 | */ | ||
1329 | mmc_delay(1); | ||
1330 | |||
1331 | mmc_host_clk_release(host); | ||
1150 | } | 1332 | } |
1151 | 1333 | ||
1152 | /* | 1334 | /* |
@@ -1214,8 +1396,7 @@ void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops) | |||
1214 | } | 1396 | } |
1215 | 1397 | ||
1216 | /* | 1398 | /* |
1217 | * Remove the current bus handler from a host. Assumes that there are | 1399 | * Remove the current bus handler from a host. |
1218 | * no interesting cards left, so the bus is powered down. | ||
1219 | */ | 1400 | */ |
1220 | void mmc_detach_bus(struct mmc_host *host) | 1401 | void mmc_detach_bus(struct mmc_host *host) |
1221 | { | 1402 | { |
@@ -1232,8 +1413,6 @@ void mmc_detach_bus(struct mmc_host *host) | |||
1232 | 1413 | ||
1233 | spin_unlock_irqrestore(&host->lock, flags); | 1414 | spin_unlock_irqrestore(&host->lock, flags); |
1234 | 1415 | ||
1235 | mmc_power_off(host); | ||
1236 | |||
1237 | mmc_bus_put(host); | 1416 | mmc_bus_put(host); |
1238 | } | 1417 | } |
1239 | 1418 | ||
@@ -1451,9 +1630,9 @@ static int mmc_do_erase(struct mmc_card *card, unsigned int from, | |||
1451 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; | 1630 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
1452 | err = mmc_wait_for_cmd(card->host, &cmd, 0); | 1631 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
1453 | if (err) { | 1632 | if (err) { |
1454 | printk(KERN_ERR "mmc_erase: group start error %d, " | 1633 | pr_err("mmc_erase: group start error %d, " |
1455 | "status %#x\n", err, cmd.resp[0]); | 1634 | "status %#x\n", err, cmd.resp[0]); |
1456 | err = -EINVAL; | 1635 | err = -EIO; |
1457 | goto out; | 1636 | goto out; |
1458 | } | 1637 | } |
1459 | 1638 | ||
@@ -1466,9 +1645,9 @@ static int mmc_do_erase(struct mmc_card *card, unsigned int from, | |||
1466 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; | 1645 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
1467 | err = mmc_wait_for_cmd(card->host, &cmd, 0); | 1646 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
1468 | if (err) { | 1647 | if (err) { |
1469 | printk(KERN_ERR "mmc_erase: group end error %d, status %#x\n", | 1648 | pr_err("mmc_erase: group end error %d, status %#x\n", |
1470 | err, cmd.resp[0]); | 1649 | err, cmd.resp[0]); |
1471 | err = -EINVAL; | 1650 | err = -EIO; |
1472 | goto out; | 1651 | goto out; |
1473 | } | 1652 | } |
1474 | 1653 | ||
@@ -1479,7 +1658,7 @@ static int mmc_do_erase(struct mmc_card *card, unsigned int from, | |||
1479 | cmd.cmd_timeout_ms = mmc_erase_timeout(card, arg, qty); | 1658 | cmd.cmd_timeout_ms = mmc_erase_timeout(card, arg, qty); |
1480 | err = mmc_wait_for_cmd(card->host, &cmd, 0); | 1659 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
1481 | if (err) { | 1660 | if (err) { |
1482 | printk(KERN_ERR "mmc_erase: erase error %d, status %#x\n", | 1661 | pr_err("mmc_erase: erase error %d, status %#x\n", |
1483 | err, cmd.resp[0]); | 1662 | err, cmd.resp[0]); |
1484 | err = -EIO; | 1663 | err = -EIO; |
1485 | goto out; | 1664 | goto out; |
@@ -1496,7 +1675,7 @@ static int mmc_do_erase(struct mmc_card *card, unsigned int from, | |||
1496 | /* Do not retry else we can't see errors */ | 1675 | /* Do not retry else we can't see errors */ |
1497 | err = mmc_wait_for_cmd(card->host, &cmd, 0); | 1676 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
1498 | if (err || (cmd.resp[0] & 0xFDF92000)) { | 1677 | if (err || (cmd.resp[0] & 0xFDF92000)) { |
1499 | printk(KERN_ERR "error %d requesting status %#x\n", | 1678 | pr_err("error %d requesting status %#x\n", |
1500 | err, cmd.resp[0]); | 1679 | err, cmd.resp[0]); |
1501 | err = -EIO; | 1680 | err = -EIO; |
1502 | goto out; | 1681 | goto out; |
@@ -1587,10 +1766,32 @@ int mmc_can_trim(struct mmc_card *card) | |||
1587 | { | 1766 | { |
1588 | if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN) | 1767 | if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN) |
1589 | return 1; | 1768 | return 1; |
1769 | if (mmc_can_discard(card)) | ||
1770 | return 1; | ||
1590 | return 0; | 1771 | return 0; |
1591 | } | 1772 | } |
1592 | EXPORT_SYMBOL(mmc_can_trim); | 1773 | EXPORT_SYMBOL(mmc_can_trim); |
1593 | 1774 | ||
1775 | int mmc_can_discard(struct mmc_card *card) | ||
1776 | { | ||
1777 | /* | ||
1778 | * As there's no way to detect the discard support bit at v4.5 | ||
1779 | * use the s/w feature support filed. | ||
1780 | */ | ||
1781 | if (card->ext_csd.feature_support & MMC_DISCARD_FEATURE) | ||
1782 | return 1; | ||
1783 | return 0; | ||
1784 | } | ||
1785 | EXPORT_SYMBOL(mmc_can_discard); | ||
1786 | |||
1787 | int mmc_can_sanitize(struct mmc_card *card) | ||
1788 | { | ||
1789 | if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_SANITIZE) | ||
1790 | return 1; | ||
1791 | return 0; | ||
1792 | } | ||
1793 | EXPORT_SYMBOL(mmc_can_sanitize); | ||
1794 | |||
1594 | int mmc_can_secure_erase_trim(struct mmc_card *card) | 1795 | int mmc_can_secure_erase_trim(struct mmc_card *card) |
1595 | { | 1796 | { |
1596 | if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN) | 1797 | if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN) |
@@ -1700,6 +1901,94 @@ int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen) | |||
1700 | } | 1901 | } |
1701 | EXPORT_SYMBOL(mmc_set_blocklen); | 1902 | EXPORT_SYMBOL(mmc_set_blocklen); |
1702 | 1903 | ||
1904 | static void mmc_hw_reset_for_init(struct mmc_host *host) | ||
1905 | { | ||
1906 | if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset) | ||
1907 | return; | ||
1908 | mmc_host_clk_hold(host); | ||
1909 | host->ops->hw_reset(host); | ||
1910 | mmc_host_clk_release(host); | ||
1911 | } | ||
1912 | |||
1913 | int mmc_can_reset(struct mmc_card *card) | ||
1914 | { | ||
1915 | u8 rst_n_function; | ||
1916 | |||
1917 | if (!mmc_card_mmc(card)) | ||
1918 | return 0; | ||
1919 | rst_n_function = card->ext_csd.rst_n_function; | ||
1920 | if ((rst_n_function & EXT_CSD_RST_N_EN_MASK) != EXT_CSD_RST_N_ENABLED) | ||
1921 | return 0; | ||
1922 | return 1; | ||
1923 | } | ||
1924 | EXPORT_SYMBOL(mmc_can_reset); | ||
1925 | |||
1926 | static int mmc_do_hw_reset(struct mmc_host *host, int check) | ||
1927 | { | ||
1928 | struct mmc_card *card = host->card; | ||
1929 | |||
1930 | if (!host->bus_ops->power_restore) | ||
1931 | return -EOPNOTSUPP; | ||
1932 | |||
1933 | if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset) | ||
1934 | return -EOPNOTSUPP; | ||
1935 | |||
1936 | if (!card) | ||
1937 | return -EINVAL; | ||
1938 | |||
1939 | if (!mmc_can_reset(card)) | ||
1940 | return -EOPNOTSUPP; | ||
1941 | |||
1942 | mmc_host_clk_hold(host); | ||
1943 | mmc_set_clock(host, host->f_init); | ||
1944 | |||
1945 | host->ops->hw_reset(host); | ||
1946 | |||
1947 | /* If the reset has happened, then a status command will fail */ | ||
1948 | if (check) { | ||
1949 | struct mmc_command cmd = {0}; | ||
1950 | int err; | ||
1951 | |||
1952 | cmd.opcode = MMC_SEND_STATUS; | ||
1953 | if (!mmc_host_is_spi(card->host)) | ||
1954 | cmd.arg = card->rca << 16; | ||
1955 | cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC; | ||
1956 | err = mmc_wait_for_cmd(card->host, &cmd, 0); | ||
1957 | if (!err) { | ||
1958 | mmc_host_clk_release(host); | ||
1959 | return -ENOSYS; | ||
1960 | } | ||
1961 | } | ||
1962 | |||
1963 | host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_DDR); | ||
1964 | if (mmc_host_is_spi(host)) { | ||
1965 | host->ios.chip_select = MMC_CS_HIGH; | ||
1966 | host->ios.bus_mode = MMC_BUSMODE_PUSHPULL; | ||
1967 | } else { | ||
1968 | host->ios.chip_select = MMC_CS_DONTCARE; | ||
1969 | host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; | ||
1970 | } | ||
1971 | host->ios.bus_width = MMC_BUS_WIDTH_1; | ||
1972 | host->ios.timing = MMC_TIMING_LEGACY; | ||
1973 | mmc_set_ios(host); | ||
1974 | |||
1975 | mmc_host_clk_release(host); | ||
1976 | |||
1977 | return host->bus_ops->power_restore(host); | ||
1978 | } | ||
1979 | |||
1980 | int mmc_hw_reset(struct mmc_host *host) | ||
1981 | { | ||
1982 | return mmc_do_hw_reset(host, 0); | ||
1983 | } | ||
1984 | EXPORT_SYMBOL(mmc_hw_reset); | ||
1985 | |||
1986 | int mmc_hw_reset_check(struct mmc_host *host) | ||
1987 | { | ||
1988 | return mmc_do_hw_reset(host, 1); | ||
1989 | } | ||
1990 | EXPORT_SYMBOL(mmc_hw_reset_check); | ||
1991 | |||
1703 | static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq) | 1992 | static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq) |
1704 | { | 1993 | { |
1705 | host->f_init = freq; | 1994 | host->f_init = freq; |
@@ -1711,6 +2000,12 @@ static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq) | |||
1711 | mmc_power_up(host); | 2000 | mmc_power_up(host); |
1712 | 2001 | ||
1713 | /* | 2002 | /* |
2003 | * Some eMMCs (with VCCQ always on) may not be reset after power up, so | ||
2004 | * do a hardware reset if possible. | ||
2005 | */ | ||
2006 | mmc_hw_reset_for_init(host); | ||
2007 | |||
2008 | /* | ||
1714 | * sdio_reset sends CMD52 to reset card. Since we do not know | 2009 | * sdio_reset sends CMD52 to reset card. Since we do not know |
1715 | * if the card is being re-initialized, just send it. CMD52 | 2010 | * if the card is being re-initialized, just send it. CMD52 |
1716 | * should be ignored by SD/eMMC cards. | 2011 | * should be ignored by SD/eMMC cards. |
@@ -1818,6 +2113,7 @@ void mmc_stop_host(struct mmc_host *host) | |||
1818 | 2113 | ||
1819 | mmc_claim_host(host); | 2114 | mmc_claim_host(host); |
1820 | mmc_detach_bus(host); | 2115 | mmc_detach_bus(host); |
2116 | mmc_power_off(host); | ||
1821 | mmc_release_host(host); | 2117 | mmc_release_host(host); |
1822 | mmc_bus_put(host); | 2118 | mmc_bus_put(host); |
1823 | return; | 2119 | return; |
@@ -1919,6 +2215,65 @@ int mmc_card_can_sleep(struct mmc_host *host) | |||
1919 | } | 2215 | } |
1920 | EXPORT_SYMBOL(mmc_card_can_sleep); | 2216 | EXPORT_SYMBOL(mmc_card_can_sleep); |
1921 | 2217 | ||
2218 | /* | ||
2219 | * Flush the cache to the non-volatile storage. | ||
2220 | */ | ||
2221 | int mmc_flush_cache(struct mmc_card *card) | ||
2222 | { | ||
2223 | struct mmc_host *host = card->host; | ||
2224 | int err = 0; | ||
2225 | |||
2226 | if (!(host->caps2 & MMC_CAP2_CACHE_CTRL)) | ||
2227 | return err; | ||
2228 | |||
2229 | if (mmc_card_mmc(card) && | ||
2230 | (card->ext_csd.cache_size > 0) && | ||
2231 | (card->ext_csd.cache_ctrl & 1)) { | ||
2232 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, | ||
2233 | EXT_CSD_FLUSH_CACHE, 1, 0); | ||
2234 | if (err) | ||
2235 | pr_err("%s: cache flush error %d\n", | ||
2236 | mmc_hostname(card->host), err); | ||
2237 | } | ||
2238 | |||
2239 | return err; | ||
2240 | } | ||
2241 | EXPORT_SYMBOL(mmc_flush_cache); | ||
2242 | |||
2243 | /* | ||
2244 | * Turn the cache ON/OFF. | ||
2245 | * Turning the cache OFF shall trigger flushing of the data | ||
2246 | * to the non-volatile storage. | ||
2247 | */ | ||
2248 | int mmc_cache_ctrl(struct mmc_host *host, u8 enable) | ||
2249 | { | ||
2250 | struct mmc_card *card = host->card; | ||
2251 | int err = 0; | ||
2252 | |||
2253 | if (!(host->caps2 & MMC_CAP2_CACHE_CTRL) || | ||
2254 | mmc_card_is_removable(host)) | ||
2255 | return err; | ||
2256 | |||
2257 | if (card && mmc_card_mmc(card) && | ||
2258 | (card->ext_csd.cache_size > 0)) { | ||
2259 | enable = !!enable; | ||
2260 | |||
2261 | if (card->ext_csd.cache_ctrl ^ enable) | ||
2262 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, | ||
2263 | EXT_CSD_CACHE_CTRL, enable, 0); | ||
2264 | if (err) | ||
2265 | pr_err("%s: cache %s error %d\n", | ||
2266 | mmc_hostname(card->host), | ||
2267 | enable ? "on" : "off", | ||
2268 | err); | ||
2269 | else | ||
2270 | card->ext_csd.cache_ctrl = enable; | ||
2271 | } | ||
2272 | |||
2273 | return err; | ||
2274 | } | ||
2275 | EXPORT_SYMBOL(mmc_cache_ctrl); | ||
2276 | |||
1922 | #ifdef CONFIG_PM | 2277 | #ifdef CONFIG_PM |
1923 | 2278 | ||
1924 | /** | 2279 | /** |
@@ -1933,23 +2288,39 @@ int mmc_suspend_host(struct mmc_host *host) | |||
1933 | cancel_delayed_work(&host->disable); | 2288 | cancel_delayed_work(&host->disable); |
1934 | cancel_delayed_work(&host->detect); | 2289 | cancel_delayed_work(&host->detect); |
1935 | mmc_flush_scheduled_work(); | 2290 | mmc_flush_scheduled_work(); |
2291 | err = mmc_cache_ctrl(host, 0); | ||
2292 | if (err) | ||
2293 | goto out; | ||
1936 | 2294 | ||
1937 | mmc_bus_get(host); | 2295 | mmc_bus_get(host); |
1938 | if (host->bus_ops && !host->bus_dead) { | 2296 | if (host->bus_ops && !host->bus_dead) { |
1939 | if (host->bus_ops->suspend) | 2297 | |
1940 | err = host->bus_ops->suspend(host); | 2298 | /* |
1941 | if (err == -ENOSYS || !host->bus_ops->resume) { | 2299 | * A long response time is not acceptable for device drivers |
1942 | /* | 2300 | * when doing suspend. Prevent mmc_claim_host in the suspend |
1943 | * We simply "remove" the card in this case. | 2301 | * sequence, to potentially wait "forever" by trying to |
1944 | * It will be redetected on resume. | 2302 | * pre-claim the host. |
1945 | */ | 2303 | */ |
1946 | if (host->bus_ops->remove) | 2304 | if (mmc_try_claim_host(host)) { |
1947 | host->bus_ops->remove(host); | 2305 | if (host->bus_ops->suspend) |
1948 | mmc_claim_host(host); | 2306 | err = host->bus_ops->suspend(host); |
1949 | mmc_detach_bus(host); | 2307 | if (err == -ENOSYS || !host->bus_ops->resume) { |
1950 | mmc_release_host(host); | 2308 | /* |
1951 | host->pm_flags = 0; | 2309 | * We simply "remove" the card in this case. |
1952 | err = 0; | 2310 | * It will be redetected on resume. |
2311 | */ | ||
2312 | if (host->bus_ops->remove) | ||
2313 | host->bus_ops->remove(host); | ||
2314 | mmc_claim_host(host); | ||
2315 | mmc_detach_bus(host); | ||
2316 | mmc_power_off(host); | ||
2317 | mmc_release_host(host); | ||
2318 | host->pm_flags = 0; | ||
2319 | err = 0; | ||
2320 | } | ||
2321 | mmc_do_release_host(host); | ||
2322 | } else { | ||
2323 | err = -EBUSY; | ||
1953 | } | 2324 | } |
1954 | } | 2325 | } |
1955 | mmc_bus_put(host); | 2326 | mmc_bus_put(host); |
@@ -1957,6 +2328,7 @@ int mmc_suspend_host(struct mmc_host *host) | |||
1957 | if (!err && !mmc_card_keep_power(host)) | 2328 | if (!err && !mmc_card_keep_power(host)) |
1958 | mmc_power_off(host); | 2329 | mmc_power_off(host); |
1959 | 2330 | ||
2331 | out: | ||
1960 | return err; | 2332 | return err; |
1961 | } | 2333 | } |
1962 | 2334 | ||
@@ -1991,7 +2363,7 @@ int mmc_resume_host(struct mmc_host *host) | |||
1991 | BUG_ON(!host->bus_ops->resume); | 2363 | BUG_ON(!host->bus_ops->resume); |
1992 | err = host->bus_ops->resume(host); | 2364 | err = host->bus_ops->resume(host); |
1993 | if (err) { | 2365 | if (err) { |
1994 | printk(KERN_WARNING "%s: error %d during resume " | 2366 | pr_warning("%s: error %d during resume " |
1995 | "(card was removed?)\n", | 2367 | "(card was removed?)\n", |
1996 | mmc_hostname(host), err); | 2368 | mmc_hostname(host), err); |
1997 | err = 0; | 2369 | err = 0; |
@@ -2022,6 +2394,7 @@ int mmc_pm_notify(struct notifier_block *notify_block, | |||
2022 | 2394 | ||
2023 | spin_lock_irqsave(&host->lock, flags); | 2395 | spin_lock_irqsave(&host->lock, flags); |
2024 | host->rescan_disable = 1; | 2396 | host->rescan_disable = 1; |
2397 | host->power_notify_type = MMC_HOST_PW_NOTIFY_SHORT; | ||
2025 | spin_unlock_irqrestore(&host->lock, flags); | 2398 | spin_unlock_irqrestore(&host->lock, flags); |
2026 | cancel_delayed_work_sync(&host->detect); | 2399 | cancel_delayed_work_sync(&host->detect); |
2027 | 2400 | ||
@@ -2034,6 +2407,7 @@ int mmc_pm_notify(struct notifier_block *notify_block, | |||
2034 | host->bus_ops->remove(host); | 2407 | host->bus_ops->remove(host); |
2035 | 2408 | ||
2036 | mmc_detach_bus(host); | 2409 | mmc_detach_bus(host); |
2410 | mmc_power_off(host); | ||
2037 | mmc_release_host(host); | 2411 | mmc_release_host(host); |
2038 | host->pm_flags = 0; | 2412 | host->pm_flags = 0; |
2039 | break; | 2413 | break; |
@@ -2044,6 +2418,7 @@ int mmc_pm_notify(struct notifier_block *notify_block, | |||
2044 | 2418 | ||
2045 | spin_lock_irqsave(&host->lock, flags); | 2419 | spin_lock_irqsave(&host->lock, flags); |
2046 | host->rescan_disable = 0; | 2420 | host->rescan_disable = 0; |
2421 | host->power_notify_type = MMC_HOST_PW_NOTIFY_LONG; | ||
2047 | spin_unlock_irqrestore(&host->lock, flags); | 2422 | spin_unlock_irqrestore(&host->lock, flags); |
2048 | mmc_detect_change(host, 0); | 2423 | mmc_detect_change(host, 0); |
2049 | 2424 | ||