Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1 | /* |
Pierre Ossman | 70f1048 | 2007-07-11 20:04:50 +0200 | [diff] [blame] | 2 | * linux/drivers/mmc/core/mmc.c |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2003-2004 Russell King, All Rights Reserved. |
| 5 | * Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved. |
| 6 | * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/err.h> |
Hans de Goede | 81f8a7b | 2015-04-01 17:26:23 +0200 | [diff] [blame] | 14 | #include <linux/of.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Paul Gortmaker | 0205a90 | 2011-07-15 12:01:27 -0400 | [diff] [blame] | 16 | #include <linux/stat.h> |
Ulf Hansson | 0cb403a | 2013-10-10 14:20:05 +0200 | [diff] [blame] | 17 | #include <linux/pm_runtime.h> |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 18 | |
| 19 | #include <linux/mmc/host.h> |
| 20 | #include <linux/mmc/card.h> |
| 21 | #include <linux/mmc/mmc.h> |
| 22 | |
| 23 | #include "core.h" |
Ulf Hansson | 4facdde | 2017-01-13 14:14:14 +0100 | [diff] [blame] | 24 | #include "card.h" |
Adrian Hunter | 436f8da | 2015-05-07 13:10:18 +0300 | [diff] [blame] | 25 | #include "host.h" |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 26 | #include "bus.h" |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 27 | #include "mmc_ops.h" |
Shawn Lin | 28fc64a | 2017-02-15 16:35:28 +0800 | [diff] [blame] | 28 | #include "quirks.h" |
Philip Rakity | 4c4cb17 | 2011-05-13 11:17:18 +0530 | [diff] [blame] | 29 | #include "sd_ops.h" |
Ulf Hansson | 52c8212 | 2017-05-08 23:52:04 +0200 | [diff] [blame] | 30 | #include "pwrseq.h" |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 31 | |
Ulf Hansson | fe1b570 | 2016-11-04 18:32:33 +0100 | [diff] [blame] | 32 | #define DEFAULT_CMD6_TIMEOUT_MS 500 |
Ulf Hansson | e3ae340 | 2018-12-10 17:52:38 +0100 | [diff] [blame] | 33 | #define MIN_CACHE_EN_TIMEOUT_MS 1600 |
Ulf Hansson | fe1b570 | 2016-11-04 18:32:33 +0100 | [diff] [blame] | 34 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 35 | static const unsigned int tran_exp[] = { |
| 36 | 10000, 100000, 1000000, 10000000, |
| 37 | 0, 0, 0, 0 |
| 38 | }; |
| 39 | |
| 40 | static const unsigned char tran_mant[] = { |
| 41 | 0, 10, 12, 13, 15, 20, 25, 30, |
| 42 | 35, 40, 45, 50, 55, 60, 70, 80, |
| 43 | }; |
| 44 | |
Shawn Lin | 4406ae2 | 2017-08-02 11:12:42 +0800 | [diff] [blame] | 45 | static const unsigned int taac_exp[] = { |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 46 | 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, |
| 47 | }; |
| 48 | |
Shawn Lin | 4406ae2 | 2017-08-02 11:12:42 +0800 | [diff] [blame] | 49 | static const unsigned int taac_mant[] = { |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 50 | 0, 10, 12, 13, 15, 20, 25, 30, |
| 51 | 35, 40, 45, 50, 55, 60, 70, 80, |
| 52 | }; |
| 53 | |
| 54 | #define UNSTUFF_BITS(resp,start,size) \ |
| 55 | ({ \ |
| 56 | const int __size = size; \ |
| 57 | const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \ |
| 58 | const int __off = 3 - ((start) / 32); \ |
| 59 | const int __shft = (start) & 31; \ |
| 60 | u32 __res; \ |
| 61 | \ |
| 62 | __res = resp[__off] >> __shft; \ |
| 63 | if (__size + __shft > 32) \ |
| 64 | __res |= resp[__off-1] << ((32 - __shft) % 32); \ |
| 65 | __res & __mask; \ |
| 66 | }) |
| 67 | |
| 68 | /* |
| 69 | * Given the decoded CSD structure, decode the raw CID to our CID structure. |
| 70 | */ |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 71 | static int mmc_decode_cid(struct mmc_card *card) |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 72 | { |
| 73 | u32 *resp = card->raw_cid; |
| 74 | |
| 75 | /* |
| 76 | * The selection of the format here is based upon published |
| 77 | * specs from sandisk and from what people have reported. |
| 78 | */ |
| 79 | switch (card->csd.mmca_vsn) { |
| 80 | case 0: /* MMC v1.0 - v1.2 */ |
| 81 | case 1: /* MMC v1.4 */ |
| 82 | card->cid.manfid = UNSTUFF_BITS(resp, 104, 24); |
| 83 | card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); |
| 84 | card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); |
| 85 | card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); |
| 86 | card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); |
| 87 | card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); |
| 88 | card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8); |
| 89 | card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8); |
| 90 | card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4); |
| 91 | card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4); |
| 92 | card->cid.serial = UNSTUFF_BITS(resp, 16, 24); |
| 93 | card->cid.month = UNSTUFF_BITS(resp, 12, 4); |
| 94 | card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997; |
| 95 | break; |
| 96 | |
| 97 | case 2: /* MMC v2.0 - v2.2 */ |
| 98 | case 3: /* MMC v3.1 - v3.3 */ |
| 99 | case 4: /* MMC v4 */ |
| 100 | card->cid.manfid = UNSTUFF_BITS(resp, 120, 8); |
| 101 | card->cid.oemid = UNSTUFF_BITS(resp, 104, 16); |
| 102 | card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); |
| 103 | card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); |
| 104 | card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); |
| 105 | card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); |
| 106 | card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); |
| 107 | card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8); |
Bernie Thompson | 51e7e8b | 2013-02-27 12:19:17 -0800 | [diff] [blame] | 108 | card->cid.prv = UNSTUFF_BITS(resp, 48, 8); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 109 | card->cid.serial = UNSTUFF_BITS(resp, 16, 32); |
| 110 | card->cid.month = UNSTUFF_BITS(resp, 12, 4); |
| 111 | card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997; |
| 112 | break; |
| 113 | |
| 114 | default: |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 115 | pr_err("%s: card has unknown MMCA version %d\n", |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 116 | mmc_hostname(card->host), card->csd.mmca_vsn); |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 117 | return -EINVAL; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 118 | } |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 119 | |
| 120 | return 0; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 121 | } |
| 122 | |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 123 | static void mmc_set_erase_size(struct mmc_card *card) |
| 124 | { |
| 125 | if (card->ext_csd.erase_group_def & 1) |
| 126 | card->erase_size = card->ext_csd.hc_erase_size; |
| 127 | else |
| 128 | card->erase_size = card->csd.erase_size; |
| 129 | |
| 130 | mmc_init_erase(card); |
| 131 | } |
| 132 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 133 | /* |
| 134 | * Given a 128-bit response, decode to our card CSD structure. |
| 135 | */ |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 136 | static int mmc_decode_csd(struct mmc_card *card) |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 137 | { |
| 138 | struct mmc_csd *csd = &card->csd; |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 139 | unsigned int e, m, a, b; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 140 | u32 *resp = card->raw_csd; |
| 141 | |
| 142 | /* |
| 143 | * We only understand CSD structure v1.1 and v1.2. |
| 144 | * v1.2 has extra information in bits 15, 11 and 10. |
Kyungmin Park | 6da24b7 | 2010-08-10 18:01:36 -0700 | [diff] [blame] | 145 | * We also support eMMC v4.4 & v4.41. |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 146 | */ |
Kyungmin Park | 6da24b7 | 2010-08-10 18:01:36 -0700 | [diff] [blame] | 147 | csd->structure = UNSTUFF_BITS(resp, 126, 2); |
| 148 | if (csd->structure == 0) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 149 | pr_err("%s: unrecognised CSD structure version %d\n", |
Kyungmin Park | 6da24b7 | 2010-08-10 18:01:36 -0700 | [diff] [blame] | 150 | mmc_hostname(card->host), csd->structure); |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 151 | return -EINVAL; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4); |
| 155 | m = UNSTUFF_BITS(resp, 115, 4); |
| 156 | e = UNSTUFF_BITS(resp, 112, 3); |
Shawn Lin | 4406ae2 | 2017-08-02 11:12:42 +0800 | [diff] [blame] | 157 | csd->taac_ns = (taac_exp[e] * taac_mant[m] + 9) / 10; |
| 158 | csd->taac_clks = UNSTUFF_BITS(resp, 104, 8) * 100; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 159 | |
| 160 | m = UNSTUFF_BITS(resp, 99, 4); |
| 161 | e = UNSTUFF_BITS(resp, 96, 3); |
| 162 | csd->max_dtr = tran_exp[e] * tran_mant[m]; |
| 163 | csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); |
| 164 | |
| 165 | e = UNSTUFF_BITS(resp, 47, 3); |
| 166 | m = UNSTUFF_BITS(resp, 62, 12); |
| 167 | csd->capacity = (1 + m) << (e + 2); |
| 168 | |
| 169 | csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4); |
| 170 | csd->read_partial = UNSTUFF_BITS(resp, 79, 1); |
| 171 | csd->write_misalign = UNSTUFF_BITS(resp, 78, 1); |
| 172 | csd->read_misalign = UNSTUFF_BITS(resp, 77, 1); |
Sascha Hauer | 3d705d1 | 2014-08-19 10:45:51 +0200 | [diff] [blame] | 173 | csd->dsr_imp = UNSTUFF_BITS(resp, 76, 1); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 174 | csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3); |
| 175 | csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4); |
| 176 | csd->write_partial = UNSTUFF_BITS(resp, 21, 1); |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 177 | |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 178 | if (csd->write_blkbits >= 9) { |
| 179 | a = UNSTUFF_BITS(resp, 42, 5); |
| 180 | b = UNSTUFF_BITS(resp, 37, 5); |
| 181 | csd->erase_size = (a + 1) * (b + 1); |
| 182 | csd->erase_size <<= csd->write_blkbits - 9; |
| 183 | } |
| 184 | |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 185 | return 0; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 186 | } |
| 187 | |
Seungwon Jeon | 96cf5f0 | 2012-04-25 16:17:37 +0900 | [diff] [blame] | 188 | static void mmc_select_card_type(struct mmc_card *card) |
| 189 | { |
| 190 | struct mmc_host *host = card->host; |
Seungwon Jeon | 0a5b643 | 2014-04-23 17:14:58 +0900 | [diff] [blame] | 191 | u8 card_type = card->ext_csd.raw_card_type; |
Lee Jones | 5f1a4dd | 2012-11-14 12:35:51 +0000 | [diff] [blame] | 192 | u32 caps = host->caps, caps2 = host->caps2; |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 193 | unsigned int hs_max_dtr = 0, hs200_max_dtr = 0; |
Seungwon Jeon | 2415c0e | 2014-04-23 17:07:58 +0900 | [diff] [blame] | 194 | unsigned int avail_type = 0; |
Seungwon Jeon | 96cf5f0 | 2012-04-25 16:17:37 +0900 | [diff] [blame] | 195 | |
| 196 | if (caps & MMC_CAP_MMC_HIGHSPEED && |
Seungwon Jeon | 2415c0e | 2014-04-23 17:07:58 +0900 | [diff] [blame] | 197 | card_type & EXT_CSD_CARD_TYPE_HS_26) { |
| 198 | hs_max_dtr = MMC_HIGH_26_MAX_DTR; |
| 199 | avail_type |= EXT_CSD_CARD_TYPE_HS_26; |
| 200 | } |
| 201 | |
| 202 | if (caps & MMC_CAP_MMC_HIGHSPEED && |
| 203 | card_type & EXT_CSD_CARD_TYPE_HS_52) { |
Seungwon Jeon | 96cf5f0 | 2012-04-25 16:17:37 +0900 | [diff] [blame] | 204 | hs_max_dtr = MMC_HIGH_52_MAX_DTR; |
Seungwon Jeon | 2415c0e | 2014-04-23 17:07:58 +0900 | [diff] [blame] | 205 | avail_type |= EXT_CSD_CARD_TYPE_HS_52; |
| 206 | } |
Seungwon Jeon | 96cf5f0 | 2012-04-25 16:17:37 +0900 | [diff] [blame] | 207 | |
Ulf Hansson | 20f921b | 2017-01-25 12:04:17 +0100 | [diff] [blame] | 208 | if (caps & (MMC_CAP_1_8V_DDR | MMC_CAP_3_3V_DDR) && |
Seungwon Jeon | 2415c0e | 2014-04-23 17:07:58 +0900 | [diff] [blame] | 209 | card_type & EXT_CSD_CARD_TYPE_DDR_1_8V) { |
Seungwon Jeon | 96cf5f0 | 2012-04-25 16:17:37 +0900 | [diff] [blame] | 210 | hs_max_dtr = MMC_HIGH_DDR_MAX_DTR; |
Seungwon Jeon | 2415c0e | 2014-04-23 17:07:58 +0900 | [diff] [blame] | 211 | avail_type |= EXT_CSD_CARD_TYPE_DDR_1_8V; |
| 212 | } |
Seungwon Jeon | 96cf5f0 | 2012-04-25 16:17:37 +0900 | [diff] [blame] | 213 | |
Seungwon Jeon | 2415c0e | 2014-04-23 17:07:58 +0900 | [diff] [blame] | 214 | if (caps & MMC_CAP_1_2V_DDR && |
| 215 | card_type & EXT_CSD_CARD_TYPE_DDR_1_2V) { |
| 216 | hs_max_dtr = MMC_HIGH_DDR_MAX_DTR; |
| 217 | avail_type |= EXT_CSD_CARD_TYPE_DDR_1_2V; |
| 218 | } |
| 219 | |
| 220 | if (caps2 & MMC_CAP2_HS200_1_8V_SDR && |
| 221 | card_type & EXT_CSD_CARD_TYPE_HS200_1_8V) { |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 222 | hs200_max_dtr = MMC_HS200_MAX_DTR; |
Seungwon Jeon | 2415c0e | 2014-04-23 17:07:58 +0900 | [diff] [blame] | 223 | avail_type |= EXT_CSD_CARD_TYPE_HS200_1_8V; |
| 224 | } |
| 225 | |
| 226 | if (caps2 & MMC_CAP2_HS200_1_2V_SDR && |
| 227 | card_type & EXT_CSD_CARD_TYPE_HS200_1_2V) { |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 228 | hs200_max_dtr = MMC_HS200_MAX_DTR; |
Seungwon Jeon | 2415c0e | 2014-04-23 17:07:58 +0900 | [diff] [blame] | 229 | avail_type |= EXT_CSD_CARD_TYPE_HS200_1_2V; |
| 230 | } |
Seungwon Jeon | 96cf5f0 | 2012-04-25 16:17:37 +0900 | [diff] [blame] | 231 | |
Seungwon Jeon | 0a5b643 | 2014-04-23 17:14:58 +0900 | [diff] [blame] | 232 | if (caps2 & MMC_CAP2_HS400_1_8V && |
| 233 | card_type & EXT_CSD_CARD_TYPE_HS400_1_8V) { |
| 234 | hs200_max_dtr = MMC_HS200_MAX_DTR; |
| 235 | avail_type |= EXT_CSD_CARD_TYPE_HS400_1_8V; |
| 236 | } |
| 237 | |
| 238 | if (caps2 & MMC_CAP2_HS400_1_2V && |
| 239 | card_type & EXT_CSD_CARD_TYPE_HS400_1_2V) { |
| 240 | hs200_max_dtr = MMC_HS200_MAX_DTR; |
| 241 | avail_type |= EXT_CSD_CARD_TYPE_HS400_1_2V; |
| 242 | } |
| 243 | |
Shawn Lin | 81ac2af | 2016-05-26 09:56:22 +0800 | [diff] [blame] | 244 | if ((caps2 & MMC_CAP2_HS400_ES) && |
| 245 | card->ext_csd.strobe_support && |
| 246 | (avail_type & EXT_CSD_CARD_TYPE_HS400)) |
| 247 | avail_type |= EXT_CSD_CARD_TYPE_HS400ES; |
| 248 | |
Seungwon Jeon | 96cf5f0 | 2012-04-25 16:17:37 +0900 | [diff] [blame] | 249 | card->ext_csd.hs_max_dtr = hs_max_dtr; |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 250 | card->ext_csd.hs200_max_dtr = hs200_max_dtr; |
Seungwon Jeon | 2415c0e | 2014-04-23 17:07:58 +0900 | [diff] [blame] | 251 | card->mmc_avail_type = avail_type; |
Seungwon Jeon | 96cf5f0 | 2012-04-25 16:17:37 +0900 | [diff] [blame] | 252 | } |
| 253 | |
Grégory Soutadé | b4493ee | 2014-09-15 17:47:06 +0200 | [diff] [blame] | 254 | static void mmc_manage_enhanced_area(struct mmc_card *card, u8 *ext_csd) |
| 255 | { |
Grégory Soutadé | 994324b | 2014-09-15 17:47:11 +0200 | [diff] [blame] | 256 | u8 hc_erase_grp_sz, hc_wp_grp_sz; |
| 257 | |
| 258 | /* |
| 259 | * Disable these attributes by default |
| 260 | */ |
| 261 | card->ext_csd.enhanced_area_offset = -EINVAL; |
| 262 | card->ext_csd.enhanced_area_size = -EINVAL; |
Grégory Soutadé | b4493ee | 2014-09-15 17:47:06 +0200 | [diff] [blame] | 263 | |
| 264 | /* |
| 265 | * Enhanced area feature support -- check whether the eMMC |
| 266 | * card has the Enhanced area enabled. If so, export enhanced |
| 267 | * area offset and size to user by adding sysfs interface. |
| 268 | */ |
| 269 | if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) && |
| 270 | (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) { |
Grégory Soutadé | 994324b | 2014-09-15 17:47:11 +0200 | [diff] [blame] | 271 | if (card->ext_csd.partition_setting_completed) { |
| 272 | hc_erase_grp_sz = |
| 273 | ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]; |
| 274 | hc_wp_grp_sz = |
| 275 | ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; |
Grégory Soutadé | b4493ee | 2014-09-15 17:47:06 +0200 | [diff] [blame] | 276 | |
Grégory Soutadé | 994324b | 2014-09-15 17:47:11 +0200 | [diff] [blame] | 277 | /* |
| 278 | * calculate the enhanced data area offset, in bytes |
| 279 | */ |
| 280 | card->ext_csd.enhanced_area_offset = |
Kuninori Morimoto | ded8a5f | 2015-05-11 07:34:53 +0000 | [diff] [blame] | 281 | (((unsigned long long)ext_csd[139]) << 24) + |
| 282 | (((unsigned long long)ext_csd[138]) << 16) + |
| 283 | (((unsigned long long)ext_csd[137]) << 8) + |
| 284 | (((unsigned long long)ext_csd[136])); |
Grégory Soutadé | 994324b | 2014-09-15 17:47:11 +0200 | [diff] [blame] | 285 | if (mmc_card_blockaddr(card)) |
| 286 | card->ext_csd.enhanced_area_offset <<= 9; |
| 287 | /* |
| 288 | * calculate the enhanced data area size, in kilobytes |
| 289 | */ |
| 290 | card->ext_csd.enhanced_area_size = |
| 291 | (ext_csd[142] << 16) + (ext_csd[141] << 8) + |
| 292 | ext_csd[140]; |
| 293 | card->ext_csd.enhanced_area_size *= |
| 294 | (size_t)(hc_erase_grp_sz * hc_wp_grp_sz); |
| 295 | card->ext_csd.enhanced_area_size <<= 9; |
| 296 | } else { |
| 297 | pr_warn("%s: defines enhanced area without partition setting complete\n", |
| 298 | mmc_hostname(card->host)); |
| 299 | } |
Grégory Soutadé | b4493ee | 2014-09-15 17:47:06 +0200 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
Ulf Hansson | 4facdde | 2017-01-13 14:14:14 +0100 | [diff] [blame] | 303 | static void mmc_part_add(struct mmc_card *card, unsigned int size, |
| 304 | unsigned int part_cfg, char *name, int idx, bool ro, |
| 305 | int area_type) |
| 306 | { |
| 307 | card->part[card->nr_parts].size = size; |
| 308 | card->part[card->nr_parts].part_cfg = part_cfg; |
| 309 | sprintf(card->part[card->nr_parts].name, name, idx); |
| 310 | card->part[card->nr_parts].force_ro = ro; |
| 311 | card->part[card->nr_parts].area_type = area_type; |
| 312 | card->nr_parts++; |
| 313 | } |
| 314 | |
Grégory Soutadé | b4493ee | 2014-09-15 17:47:06 +0200 | [diff] [blame] | 315 | static void mmc_manage_gp_partitions(struct mmc_card *card, u8 *ext_csd) |
| 316 | { |
Grégory Soutadé | b4493ee | 2014-09-15 17:47:06 +0200 | [diff] [blame] | 317 | int idx; |
Grégory Soutadé | 994324b | 2014-09-15 17:47:11 +0200 | [diff] [blame] | 318 | u8 hc_erase_grp_sz, hc_wp_grp_sz; |
| 319 | unsigned int part_size; |
Grégory Soutadé | b4493ee | 2014-09-15 17:47:06 +0200 | [diff] [blame] | 320 | |
| 321 | /* |
| 322 | * General purpose partition feature support -- |
| 323 | * If ext_csd has the size of general purpose partitions, |
| 324 | * set size, part_cfg, partition name in mmc_part. |
| 325 | */ |
| 326 | if (ext_csd[EXT_CSD_PARTITION_SUPPORT] & |
| 327 | EXT_CSD_PART_SUPPORT_PART_EN) { |
Grégory Soutadé | 994324b | 2014-09-15 17:47:11 +0200 | [diff] [blame] | 328 | hc_erase_grp_sz = |
| 329 | ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]; |
| 330 | hc_wp_grp_sz = |
| 331 | ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; |
Grégory Soutadé | b4493ee | 2014-09-15 17:47:06 +0200 | [diff] [blame] | 332 | |
| 333 | for (idx = 0; idx < MMC_NUM_GP_PARTITION; idx++) { |
| 334 | if (!ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3] && |
| 335 | !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1] && |
| 336 | !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2]) |
| 337 | continue; |
Grégory Soutadé | 994324b | 2014-09-15 17:47:11 +0200 | [diff] [blame] | 338 | if (card->ext_csd.partition_setting_completed == 0) { |
| 339 | pr_warn("%s: has partition size defined without partition complete\n", |
| 340 | mmc_hostname(card->host)); |
| 341 | break; |
| 342 | } |
Grégory Soutadé | b4493ee | 2014-09-15 17:47:06 +0200 | [diff] [blame] | 343 | part_size = |
| 344 | (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2] |
| 345 | << 16) + |
| 346 | (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1] |
| 347 | << 8) + |
| 348 | ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3]; |
| 349 | part_size *= (size_t)(hc_erase_grp_sz * |
| 350 | hc_wp_grp_sz); |
| 351 | mmc_part_add(card, part_size << 19, |
| 352 | EXT_CSD_PART_CONFIG_ACC_GP0 + idx, |
| 353 | "gp%d", idx, false, |
| 354 | MMC_BLK_DATA_AREA_GP); |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
Adrian Hunter | 1c44711 | 2016-05-05 08:12:28 +0300 | [diff] [blame] | 359 | /* Minimum partition switch timeout in milliseconds */ |
| 360 | #define MMC_MIN_PART_SWITCH_TIME 300 |
| 361 | |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 362 | /* |
| 363 | * Decode extended CSD. |
| 364 | */ |
Ulf Hansson | 076ec38 | 2014-10-20 13:37:24 +0200 | [diff] [blame] | 365 | static int mmc_decode_ext_csd(struct mmc_card *card, u8 *ext_csd) |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 366 | { |
Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 367 | int err = 0, idx; |
| 368 | unsigned int part_size; |
Hans de Goede | 81f8a7b | 2015-04-01 17:26:23 +0200 | [diff] [blame] | 369 | struct device_node *np; |
| 370 | bool broken_hpi = false; |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 371 | |
Kyungmin Park | 6da24b7 | 2010-08-10 18:01:36 -0700 | [diff] [blame] | 372 | /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */ |
Philip Rakity | f39b2dd9 | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 373 | card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE]; |
Kyungmin Park | 6da24b7 | 2010-08-10 18:01:36 -0700 | [diff] [blame] | 374 | if (card->csd.structure == 3) { |
Philip Rakity | f39b2dd9 | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 375 | if (card->ext_csd.raw_ext_csd_structure > 2) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 376 | pr_err("%s: unrecognised EXT_CSD structure " |
Kyungmin Park | 6da24b7 | 2010-08-10 18:01:36 -0700 | [diff] [blame] | 377 | "version %d\n", mmc_hostname(card->host), |
Philip Rakity | f39b2dd9 | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 378 | card->ext_csd.raw_ext_csd_structure); |
Kyungmin Park | 6da24b7 | 2010-08-10 18:01:36 -0700 | [diff] [blame] | 379 | err = -EINVAL; |
| 380 | goto out; |
| 381 | } |
| 382 | } |
| 383 | |
Hans de Goede | 81f8a7b | 2015-04-01 17:26:23 +0200 | [diff] [blame] | 384 | np = mmc_of_find_child_device(card->host, 0); |
| 385 | if (np && of_device_is_compatible(np, "mmc-card")) |
| 386 | broken_hpi = of_property_read_bool(np, "broken-hpi"); |
| 387 | of_node_put(np); |
| 388 | |
Romain Izard | 03a5943 | 2014-06-27 10:51:07 +0200 | [diff] [blame] | 389 | /* |
| 390 | * The EXT_CSD format is meant to be forward compatible. As long |
| 391 | * as CSD_STRUCTURE does not change, all values for EXT_CSD_REV |
| 392 | * are authorized, see JEDEC JESD84-B50 section B.8. |
| 393 | */ |
Jarkko Lavinen | b1ebe38 | 2009-09-22 16:44:34 -0700 | [diff] [blame] | 394 | card->ext_csd.rev = ext_csd[EXT_CSD_REV]; |
Pierre Ossman | d7604d7 | 2007-07-23 00:34:07 +0200 | [diff] [blame] | 395 | |
Pratibhasagar V | 5320226 | 2016-06-09 18:09:31 -0400 | [diff] [blame] | 396 | /* fixup device after ext_csd revision field is updated */ |
| 397 | mmc_fixup_device(card, mmc_ext_csd_fixups); |
| 398 | |
Philip Rakity | f39b2dd9 | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 399 | card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0]; |
| 400 | card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1]; |
| 401 | card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2]; |
| 402 | card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3]; |
Jarkko Lavinen | b1ebe38 | 2009-09-22 16:44:34 -0700 | [diff] [blame] | 403 | if (card->ext_csd.rev >= 2) { |
Pierre Ossman | d7604d7 | 2007-07-23 00:34:07 +0200 | [diff] [blame] | 404 | card->ext_csd.sectors = |
| 405 | ext_csd[EXT_CSD_SEC_CNT + 0] << 0 | |
| 406 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8 | |
| 407 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16 | |
| 408 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24; |
Hanumath Prasad | fc8a098 | 2010-08-10 18:01:45 -0700 | [diff] [blame] | 409 | |
| 410 | /* Cards with density > 2GiB are sector addressed */ |
| 411 | if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512) |
Pierre Ossman | d7604d7 | 2007-07-23 00:34:07 +0200 | [diff] [blame] | 412 | mmc_card_set_blockaddr(card); |
| 413 | } |
Seungwon Jeon | 96cf5f0 | 2012-04-25 16:17:37 +0900 | [diff] [blame] | 414 | |
Shawn Lin | 81ac2af | 2016-05-26 09:56:22 +0800 | [diff] [blame] | 415 | card->ext_csd.strobe_support = ext_csd[EXT_CSD_STROBE_SUPPORT]; |
Philip Rakity | f39b2dd9 | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 416 | card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE]; |
Seungwon Jeon | 96cf5f0 | 2012-04-25 16:17:37 +0900 | [diff] [blame] | 417 | mmc_select_card_type(card); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 418 | |
Philip Rakity | f39b2dd9 | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 419 | card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT]; |
| 420 | card->ext_csd.raw_erase_timeout_mult = |
| 421 | ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]; |
| 422 | card->ext_csd.raw_hc_erase_grp_size = |
| 423 | ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]; |
Jarkko Lavinen | b1ebe38 | 2009-09-22 16:44:34 -0700 | [diff] [blame] | 424 | if (card->ext_csd.rev >= 3) { |
| 425 | u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT]; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 426 | card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG]; |
| 427 | |
| 428 | /* EXT_CSD value is in units of 10ms, but we store in ms */ |
| 429 | card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME]; |
Adrian Hunter | 1c44711 | 2016-05-05 08:12:28 +0300 | [diff] [blame] | 430 | /* Some eMMC set the value too low so set a minimum */ |
| 431 | if (card->ext_csd.part_time && |
| 432 | card->ext_csd.part_time < MMC_MIN_PART_SWITCH_TIME) |
| 433 | card->ext_csd.part_time = MMC_MIN_PART_SWITCH_TIME; |
Jarkko Lavinen | b1ebe38 | 2009-09-22 16:44:34 -0700 | [diff] [blame] | 434 | |
| 435 | /* Sleep / awake timeout in 100ns units */ |
| 436 | if (sa_shift > 0 && sa_shift <= 0x17) |
| 437 | card->ext_csd.sa_timeout = |
| 438 | 1 << ext_csd[EXT_CSD_S_A_TIMEOUT]; |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 439 | card->ext_csd.erase_group_def = |
| 440 | ext_csd[EXT_CSD_ERASE_GROUP_DEF]; |
| 441 | card->ext_csd.hc_erase_timeout = 300 * |
| 442 | ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]; |
| 443 | card->ext_csd.hc_erase_size = |
| 444 | ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10; |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 445 | |
| 446 | card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C]; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 447 | |
| 448 | /* |
| 449 | * There are two boot regions of equal size, defined in |
| 450 | * multiples of 128K. |
| 451 | */ |
Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 452 | if (ext_csd[EXT_CSD_BOOT_MULT] && mmc_boot_partition_access(card->host)) { |
| 453 | for (idx = 0; idx < MMC_NUM_BOOT_PARTITION; idx++) { |
| 454 | part_size = ext_csd[EXT_CSD_BOOT_MULT] << 17; |
| 455 | mmc_part_add(card, part_size, |
| 456 | EXT_CSD_PART_CONFIG_ACC_BOOT0 + idx, |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 457 | "boot%d", idx, true, |
| 458 | MMC_BLK_DATA_AREA_BOOT); |
Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 459 | } |
| 460 | } |
Jarkko Lavinen | b1ebe38 | 2009-09-22 16:44:34 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Philip Rakity | f39b2dd9 | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 463 | card->ext_csd.raw_hc_erase_gap_size = |
Jurgen Heeks | dd13b4e | 2012-02-01 13:30:55 +0100 | [diff] [blame] | 464 | ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; |
Philip Rakity | f39b2dd9 | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 465 | card->ext_csd.raw_sec_trim_mult = |
| 466 | ext_csd[EXT_CSD_SEC_TRIM_MULT]; |
| 467 | card->ext_csd.raw_sec_erase_mult = |
| 468 | ext_csd[EXT_CSD_SEC_ERASE_MULT]; |
| 469 | card->ext_csd.raw_sec_feature_support = |
| 470 | ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]; |
| 471 | card->ext_csd.raw_trim_mult = |
| 472 | ext_csd[EXT_CSD_TRIM_MULT]; |
Philip Rakity | 836dc2f | 2013-04-04 20:18:11 +0100 | [diff] [blame] | 473 | card->ext_csd.raw_partition_support = ext_csd[EXT_CSD_PARTITION_SUPPORT]; |
Adrian Hunter | b097e07 | 2015-02-06 14:12:57 +0200 | [diff] [blame] | 474 | card->ext_csd.raw_driver_strength = ext_csd[EXT_CSD_DRIVER_STRENGTH]; |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 475 | if (card->ext_csd.rev >= 4) { |
Grégory Soutadé | 69803d4 | 2014-09-15 17:47:09 +0200 | [diff] [blame] | 476 | if (ext_csd[EXT_CSD_PARTITION_SETTING_COMPLETED] & |
| 477 | EXT_CSD_PART_SETTING_COMPLETED) |
| 478 | card->ext_csd.partition_setting_completed = 1; |
| 479 | else |
| 480 | card->ext_csd.partition_setting_completed = 0; |
| 481 | |
Grégory Soutadé | b4493ee | 2014-09-15 17:47:06 +0200 | [diff] [blame] | 482 | mmc_manage_enhanced_area(card, ext_csd); |
Chuanxiao Dong | 709de99d | 2011-01-22 04:09:41 +0800 | [diff] [blame] | 483 | |
Grégory Soutadé | b4493ee | 2014-09-15 17:47:06 +0200 | [diff] [blame] | 484 | mmc_manage_gp_partitions(card, ext_csd); |
Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 485 | |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 486 | card->ext_csd.sec_trim_mult = |
| 487 | ext_csd[EXT_CSD_SEC_TRIM_MULT]; |
| 488 | card->ext_csd.sec_erase_mult = |
| 489 | ext_csd[EXT_CSD_SEC_ERASE_MULT]; |
| 490 | card->ext_csd.sec_feature_support = |
| 491 | ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]; |
| 492 | card->ext_csd.trim_timeout = 300 * |
| 493 | ext_csd[EXT_CSD_TRIM_MULT]; |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 494 | |
| 495 | /* |
| 496 | * Note that the call to mmc_part_add above defaults to read |
| 497 | * only. If this default assumption is changed, the call must |
| 498 | * take into account the value of boot_locked below. |
| 499 | */ |
| 500 | card->ext_csd.boot_ro_lock = ext_csd[EXT_CSD_BOOT_WP]; |
| 501 | card->ext_csd.boot_ro_lockable = true; |
Fredrik Soderstedt | 6044371 | 2013-04-23 16:27:07 +0200 | [diff] [blame] | 502 | |
| 503 | /* Save power class values */ |
| 504 | card->ext_csd.raw_pwr_cl_52_195 = |
| 505 | ext_csd[EXT_CSD_PWR_CL_52_195]; |
| 506 | card->ext_csd.raw_pwr_cl_26_195 = |
| 507 | ext_csd[EXT_CSD_PWR_CL_26_195]; |
| 508 | card->ext_csd.raw_pwr_cl_52_360 = |
| 509 | ext_csd[EXT_CSD_PWR_CL_52_360]; |
| 510 | card->ext_csd.raw_pwr_cl_26_360 = |
| 511 | ext_csd[EXT_CSD_PWR_CL_26_360]; |
| 512 | card->ext_csd.raw_pwr_cl_200_195 = |
| 513 | ext_csd[EXT_CSD_PWR_CL_200_195]; |
| 514 | card->ext_csd.raw_pwr_cl_200_360 = |
| 515 | ext_csd[EXT_CSD_PWR_CL_200_360]; |
| 516 | card->ext_csd.raw_pwr_cl_ddr_52_195 = |
| 517 | ext_csd[EXT_CSD_PWR_CL_DDR_52_195]; |
| 518 | card->ext_csd.raw_pwr_cl_ddr_52_360 = |
| 519 | ext_csd[EXT_CSD_PWR_CL_DDR_52_360]; |
Seungwon Jeon | 0a5b643 | 2014-04-23 17:14:58 +0900 | [diff] [blame] | 520 | card->ext_csd.raw_pwr_cl_ddr_200_360 = |
| 521 | ext_csd[EXT_CSD_PWR_CL_DDR_200_360]; |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 522 | } |
| 523 | |
Adrian Hunter | b249951 | 2011-08-29 16:42:11 +0300 | [diff] [blame] | 524 | if (card->ext_csd.rev >= 5) { |
Romain Izard | 7c4f10a | 2013-06-14 14:25:44 +0200 | [diff] [blame] | 525 | /* Adjust production date as per JEDEC JESD84-B451 */ |
| 526 | if (card->cid.year < 2010) |
| 527 | card->cid.year += 16; |
| 528 | |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 529 | /* check whether the eMMC card supports BKOPS */ |
Ulf Hansson | ba9f39a | 2018-12-10 17:52:37 +0100 | [diff] [blame] | 530 | if (ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1) { |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 531 | card->ext_csd.bkops = 1; |
Alexey Skidanov | 0501be6 | 2015-01-29 10:49:43 +0200 | [diff] [blame] | 532 | card->ext_csd.man_bkops_en = |
| 533 | (ext_csd[EXT_CSD_BKOPS_EN] & |
| 534 | EXT_CSD_MANUAL_BKOPS_MASK); |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 535 | card->ext_csd.raw_bkops_status = |
| 536 | ext_csd[EXT_CSD_BKOPS_STATUS]; |
Uri Yanai | 6962c2f | 2017-02-07 18:00:00 +0200 | [diff] [blame] | 537 | if (card->ext_csd.man_bkops_en) |
| 538 | pr_debug("%s: MAN_BKOPS_EN bit is set\n", |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 539 | mmc_hostname(card->host)); |
Uri Yanai | efff8e7 | 2017-02-07 18:00:01 +0200 | [diff] [blame] | 540 | card->ext_csd.auto_bkops_en = |
| 541 | (ext_csd[EXT_CSD_BKOPS_EN] & |
| 542 | EXT_CSD_AUTO_BKOPS_MASK); |
| 543 | if (card->ext_csd.auto_bkops_en) |
| 544 | pr_debug("%s: AUTO_BKOPS_EN bit is set\n", |
| 545 | mmc_hostname(card->host)); |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Jaehoon Chung | eb0d8f1 | 2011-10-18 01:26:42 -0400 | [diff] [blame] | 548 | /* check whether the eMMC card supports HPI */ |
Pratibhasagar V | 5320226 | 2016-06-09 18:09:31 -0400 | [diff] [blame] | 549 | if (!mmc_card_broken_hpi(card) && |
| 550 | !broken_hpi && (ext_csd[EXT_CSD_HPI_FEATURES] & 0x1)) { |
Jaehoon Chung | eb0d8f1 | 2011-10-18 01:26:42 -0400 | [diff] [blame] | 551 | card->ext_csd.hpi = 1; |
| 552 | if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x2) |
| 553 | card->ext_csd.hpi_cmd = MMC_STOP_TRANSMISSION; |
| 554 | else |
| 555 | card->ext_csd.hpi_cmd = MMC_SEND_STATUS; |
| 556 | /* |
| 557 | * Indicate the maximum timeout to close |
| 558 | * a command interrupted by HPI |
| 559 | */ |
| 560 | card->ext_csd.out_of_int_time = |
| 561 | ext_csd[EXT_CSD_OUT_OF_INTERRUPT_TIME] * 10; |
| 562 | } |
| 563 | |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 564 | card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM]; |
Adrian Hunter | b249951 | 2011-08-29 16:42:11 +0300 | [diff] [blame] | 565 | card->ext_csd.rst_n_function = ext_csd[EXT_CSD_RST_N_FUNCTION]; |
Loic Pallardy | 090d25f | 2012-11-17 18:08:24 -0500 | [diff] [blame] | 566 | |
| 567 | /* |
| 568 | * RPMB regions are defined in multiples of 128K. |
| 569 | */ |
| 570 | card->ext_csd.raw_rpmb_size_mult = ext_csd[EXT_CSD_RPMB_MULT]; |
Balaji T K | d0123cc | 2013-01-25 17:00:30 +0530 | [diff] [blame] | 571 | if (ext_csd[EXT_CSD_RPMB_MULT] && mmc_host_cmd23(card->host)) { |
Loic Pallardy | 090d25f | 2012-11-17 18:08:24 -0500 | [diff] [blame] | 572 | mmc_part_add(card, ext_csd[EXT_CSD_RPMB_MULT] << 17, |
| 573 | EXT_CSD_PART_CONFIG_ACC_RPMB, |
| 574 | "rpmb", 0, false, |
| 575 | MMC_BLK_DATA_AREA_RPMB); |
| 576 | } |
Adrian Hunter | b249951 | 2011-08-29 16:42:11 +0300 | [diff] [blame] | 577 | } |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 578 | |
Andrei Warkentin | 5238acb | 2011-09-24 12:12:30 -0400 | [diff] [blame] | 579 | card->ext_csd.raw_erased_mem_count = ext_csd[EXT_CSD_ERASED_MEM_CONT]; |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 580 | if (ext_csd[EXT_CSD_ERASED_MEM_CONT]) |
| 581 | card->erased_byte = 0xFF; |
| 582 | else |
| 583 | card->erased_byte = 0x0; |
| 584 | |
Seungwon Jeon | 336c716 | 2011-10-25 09:43:12 +0900 | [diff] [blame] | 585 | /* eMMC v4.5 or later */ |
Ulf Hansson | fe1b570 | 2016-11-04 18:32:33 +0100 | [diff] [blame] | 586 | card->ext_csd.generic_cmd6_time = DEFAULT_CMD6_TIMEOUT_MS; |
Girish K S | bec8726 | 2011-10-13 12:04:16 +0530 | [diff] [blame] | 587 | if (card->ext_csd.rev >= 6) { |
Seungwon Jeon | 336c716 | 2011-10-25 09:43:12 +0900 | [diff] [blame] | 588 | card->ext_csd.feature_support |= MMC_DISCARD_FEATURE; |
| 589 | |
Seungwon Jeon | b23cf0b | 2011-09-23 14:15:29 +0900 | [diff] [blame] | 590 | card->ext_csd.generic_cmd6_time = 10 * |
| 591 | ext_csd[EXT_CSD_GENERIC_CMD6_TIME]; |
Girish K S | bec8726 | 2011-10-13 12:04:16 +0530 | [diff] [blame] | 592 | card->ext_csd.power_off_longtime = 10 * |
| 593 | ext_csd[EXT_CSD_POWER_OFF_LONG_TIME]; |
Seungwon Jeon | b23cf0b | 2011-09-23 14:15:29 +0900 | [diff] [blame] | 594 | |
Seungwon Jeon | 336c716 | 2011-10-25 09:43:12 +0900 | [diff] [blame] | 595 | card->ext_csd.cache_size = |
| 596 | ext_csd[EXT_CSD_CACHE_SIZE + 0] << 0 | |
| 597 | ext_csd[EXT_CSD_CACHE_SIZE + 1] << 8 | |
| 598 | ext_csd[EXT_CSD_CACHE_SIZE + 2] << 16 | |
| 599 | ext_csd[EXT_CSD_CACHE_SIZE + 3] << 24; |
Saugata Das | 4265900 | 2011-12-21 13:09:17 +0530 | [diff] [blame] | 600 | |
| 601 | if (ext_csd[EXT_CSD_DATA_SECTOR_SIZE] == 1) |
| 602 | card->ext_csd.data_sector_size = 4096; |
| 603 | else |
| 604 | card->ext_csd.data_sector_size = 512; |
| 605 | |
| 606 | if ((ext_csd[EXT_CSD_DATA_TAG_SUPPORT] & 1) && |
| 607 | (ext_csd[EXT_CSD_TAG_UNIT_SIZE] <= 8)) { |
| 608 | card->ext_csd.data_tag_unit_size = |
| 609 | ((unsigned int) 1 << ext_csd[EXT_CSD_TAG_UNIT_SIZE]) * |
| 610 | (card->ext_csd.data_sector_size); |
| 611 | } else { |
| 612 | card->ext_csd.data_tag_unit_size = 0; |
| 613 | } |
Seungwon Jeon | abd9ac1 | 2013-02-06 17:01:43 +0900 | [diff] [blame] | 614 | |
| 615 | card->ext_csd.max_packed_writes = |
| 616 | ext_csd[EXT_CSD_MAX_PACKED_WRITES]; |
| 617 | card->ext_csd.max_packed_reads = |
| 618 | ext_csd[EXT_CSD_MAX_PACKED_READS]; |
Saugata Das | a5075eb | 2012-05-17 16:32:21 +0530 | [diff] [blame] | 619 | } else { |
| 620 | card->ext_csd.data_sector_size = 512; |
Seungwon Jeon | 336c716 | 2011-10-25 09:43:12 +0900 | [diff] [blame] | 621 | } |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 622 | |
Gwendal Grignou | 0f76242 | 2014-10-16 11:27:16 -0700 | [diff] [blame] | 623 | /* eMMC v5 or later */ |
| 624 | if (card->ext_csd.rev >= 7) { |
| 625 | memcpy(card->ext_csd.fwrev, &ext_csd[EXT_CSD_FIRMWARE_VERSION], |
| 626 | MMC_FIRMWARE_LEN); |
| 627 | card->ext_csd.ffu_capable = |
| 628 | (ext_csd[EXT_CSD_SUPPORTED_MODE] & 0x1) && |
| 629 | !(ext_csd[EXT_CSD_FW_CONFIG] & 0x1); |
Jungseung Lee | 46bc5c4 | 2016-12-22 12:37:34 +0900 | [diff] [blame] | 630 | |
| 631 | card->ext_csd.pre_eol_info = ext_csd[EXT_CSD_PRE_EOL_INFO]; |
| 632 | card->ext_csd.device_life_time_est_typ_a = |
| 633 | ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A]; |
| 634 | card->ext_csd.device_life_time_est_typ_b = |
| 635 | ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_B]; |
Gwendal Grignou | 0f76242 | 2014-10-16 11:27:16 -0700 | [diff] [blame] | 636 | } |
Adrian Hunter | 925ff3a | 2016-11-29 12:09:16 +0200 | [diff] [blame] | 637 | |
| 638 | /* eMMC v5.1 or later */ |
| 639 | if (card->ext_csd.rev >= 8) { |
| 640 | card->ext_csd.cmdq_support = ext_csd[EXT_CSD_CMDQ_SUPPORT] & |
| 641 | EXT_CSD_CMDQ_SUPPORTED; |
| 642 | card->ext_csd.cmdq_depth = (ext_csd[EXT_CSD_CMDQ_DEPTH] & |
| 643 | EXT_CSD_CMDQ_DEPTH_MASK) + 1; |
| 644 | /* Exclude inefficiently small queue depths */ |
| 645 | if (card->ext_csd.cmdq_depth <= 2) { |
| 646 | card->ext_csd.cmdq_support = false; |
| 647 | card->ext_csd.cmdq_depth = 0; |
| 648 | } |
| 649 | if (card->ext_csd.cmdq_support) { |
| 650 | pr_debug("%s: Command Queue supported depth %u\n", |
| 651 | mmc_hostname(card->host), |
| 652 | card->ext_csd.cmdq_depth); |
| 653 | } |
| 654 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 655 | out: |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 656 | return err; |
| 657 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 658 | |
Ulf Hansson | 076ec38 | 2014-10-20 13:37:24 +0200 | [diff] [blame] | 659 | static int mmc_read_ext_csd(struct mmc_card *card) |
| 660 | { |
Ulf Hansson | c197787 | 2014-10-20 14:08:16 +0200 | [diff] [blame] | 661 | u8 *ext_csd; |
Ulf Hansson | 076ec38 | 2014-10-20 13:37:24 +0200 | [diff] [blame] | 662 | int err; |
| 663 | |
Ulf Hansson | c197787 | 2014-10-20 14:08:16 +0200 | [diff] [blame] | 664 | if (!mmc_can_ext_csd(card)) |
| 665 | return 0; |
| 666 | |
Ulf Hansson | 076ec38 | 2014-10-20 13:37:24 +0200 | [diff] [blame] | 667 | err = mmc_get_ext_csd(card, &ext_csd); |
Ulf Hansson | c197787 | 2014-10-20 14:08:16 +0200 | [diff] [blame] | 668 | if (err) { |
| 669 | /* If the host or the card can't do the switch, |
| 670 | * fail more gracefully. */ |
| 671 | if ((err != -EINVAL) |
| 672 | && (err != -ENOSYS) |
| 673 | && (err != -EFAULT)) |
| 674 | return err; |
| 675 | |
| 676 | /* |
| 677 | * High capacity cards should have this "magic" size |
| 678 | * stored in their CSD. |
| 679 | */ |
| 680 | if (card->csd.capacity == (4096 * 512)) { |
| 681 | pr_err("%s: unable to read EXT_CSD on a possible high capacity card. Card will be ignored.\n", |
| 682 | mmc_hostname(card->host)); |
| 683 | } else { |
| 684 | pr_warn("%s: unable to read EXT_CSD, performance might suffer\n", |
| 685 | mmc_hostname(card->host)); |
| 686 | err = 0; |
| 687 | } |
| 688 | |
Ulf Hansson | 076ec38 | 2014-10-20 13:37:24 +0200 | [diff] [blame] | 689 | return err; |
Ulf Hansson | c197787 | 2014-10-20 14:08:16 +0200 | [diff] [blame] | 690 | } |
Ulf Hansson | 076ec38 | 2014-10-20 13:37:24 +0200 | [diff] [blame] | 691 | |
| 692 | err = mmc_decode_ext_csd(card, ext_csd); |
| 693 | kfree(ext_csd); |
| 694 | return err; |
| 695 | } |
| 696 | |
Philip Rakity | f39b2dd9 | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 697 | static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width) |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 698 | { |
| 699 | u8 *bw_ext_csd; |
| 700 | int err; |
| 701 | |
Philip Rakity | f39b2dd9 | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 702 | if (bus_width == MMC_BUS_WIDTH_1) |
| 703 | return 0; |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 704 | |
Philip Rakity | f39b2dd9 | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 705 | err = mmc_get_ext_csd(card, &bw_ext_csd); |
Ulf Hansson | c197787 | 2014-10-20 14:08:16 +0200 | [diff] [blame] | 706 | if (err) |
| 707 | return err; |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 708 | |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 709 | /* only compare read only fields */ |
Jurgen Heeks | dd13b4e | 2012-02-01 13:30:55 +0100 | [diff] [blame] | 710 | err = !((card->ext_csd.raw_partition_support == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 711 | bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) && |
Philip Rakity | f39b2dd9 | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 712 | (card->ext_csd.raw_erased_mem_count == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 713 | bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) && |
Philip Rakity | f39b2dd9 | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 714 | (card->ext_csd.rev == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 715 | bw_ext_csd[EXT_CSD_REV]) && |
Philip Rakity | f39b2dd9 | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 716 | (card->ext_csd.raw_ext_csd_structure == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 717 | bw_ext_csd[EXT_CSD_STRUCTURE]) && |
Philip Rakity | f39b2dd9 | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 718 | (card->ext_csd.raw_card_type == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 719 | bw_ext_csd[EXT_CSD_CARD_TYPE]) && |
Philip Rakity | f39b2dd9 | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 720 | (card->ext_csd.raw_s_a_timeout == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 721 | bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) && |
Philip Rakity | f39b2dd9 | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 722 | (card->ext_csd.raw_hc_erase_gap_size == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 723 | bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) && |
Philip Rakity | f39b2dd9 | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 724 | (card->ext_csd.raw_erase_timeout_mult == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 725 | bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) && |
Philip Rakity | f39b2dd9 | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 726 | (card->ext_csd.raw_hc_erase_grp_size == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 727 | bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) && |
Philip Rakity | f39b2dd9 | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 728 | (card->ext_csd.raw_sec_trim_mult == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 729 | bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) && |
Philip Rakity | f39b2dd9 | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 730 | (card->ext_csd.raw_sec_erase_mult == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 731 | bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) && |
Philip Rakity | f39b2dd9 | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 732 | (card->ext_csd.raw_sec_feature_support == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 733 | bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) && |
Philip Rakity | f39b2dd9 | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 734 | (card->ext_csd.raw_trim_mult == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 735 | bw_ext_csd[EXT_CSD_TRIM_MULT]) && |
Philip Rakity | f39b2dd9 | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 736 | (card->ext_csd.raw_sectors[0] == |
| 737 | bw_ext_csd[EXT_CSD_SEC_CNT + 0]) && |
| 738 | (card->ext_csd.raw_sectors[1] == |
| 739 | bw_ext_csd[EXT_CSD_SEC_CNT + 1]) && |
| 740 | (card->ext_csd.raw_sectors[2] == |
| 741 | bw_ext_csd[EXT_CSD_SEC_CNT + 2]) && |
| 742 | (card->ext_csd.raw_sectors[3] == |
Fredrik Soderstedt | 6044371 | 2013-04-23 16:27:07 +0200 | [diff] [blame] | 743 | bw_ext_csd[EXT_CSD_SEC_CNT + 3]) && |
| 744 | (card->ext_csd.raw_pwr_cl_52_195 == |
| 745 | bw_ext_csd[EXT_CSD_PWR_CL_52_195]) && |
| 746 | (card->ext_csd.raw_pwr_cl_26_195 == |
| 747 | bw_ext_csd[EXT_CSD_PWR_CL_26_195]) && |
| 748 | (card->ext_csd.raw_pwr_cl_52_360 == |
| 749 | bw_ext_csd[EXT_CSD_PWR_CL_52_360]) && |
| 750 | (card->ext_csd.raw_pwr_cl_26_360 == |
| 751 | bw_ext_csd[EXT_CSD_PWR_CL_26_360]) && |
| 752 | (card->ext_csd.raw_pwr_cl_200_195 == |
| 753 | bw_ext_csd[EXT_CSD_PWR_CL_200_195]) && |
| 754 | (card->ext_csd.raw_pwr_cl_200_360 == |
| 755 | bw_ext_csd[EXT_CSD_PWR_CL_200_360]) && |
| 756 | (card->ext_csd.raw_pwr_cl_ddr_52_195 == |
| 757 | bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_195]) && |
| 758 | (card->ext_csd.raw_pwr_cl_ddr_52_360 == |
Seungwon Jeon | 0a5b643 | 2014-04-23 17:14:58 +0900 | [diff] [blame] | 759 | bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_360]) && |
| 760 | (card->ext_csd.raw_pwr_cl_ddr_200_360 == |
| 761 | bw_ext_csd[EXT_CSD_PWR_CL_DDR_200_360])); |
| 762 | |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 763 | if (err) |
| 764 | err = -EINVAL; |
| 765 | |
Ulf Hansson | 00b41b5 | 2014-10-16 16:18:51 +0200 | [diff] [blame] | 766 | kfree(bw_ext_csd); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 767 | return err; |
| 768 | } |
| 769 | |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 770 | MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1], |
| 771 | card->raw_cid[2], card->raw_cid[3]); |
| 772 | MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1], |
| 773 | card->raw_csd[2], card->raw_csd[3]); |
| 774 | MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year); |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 775 | MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9); |
| 776 | MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9); |
Gwendal Grignou | 0f76242 | 2014-10-16 11:27:16 -0700 | [diff] [blame] | 777 | MMC_DEV_ATTR(ffu_capable, "%d\n", card->ext_csd.ffu_capable); |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 778 | MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev); |
| 779 | MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid); |
| 780 | MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name); |
| 781 | MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid); |
Bernie Thompson | 51e7e8b | 2013-02-27 12:19:17 -0800 | [diff] [blame] | 782 | MMC_DEV_ATTR(prv, "0x%x\n", card->cid.prv); |
Jin Qian | 04fa054 | 2017-10-12 10:46:59 -0700 | [diff] [blame] | 783 | MMC_DEV_ATTR(rev, "0x%x\n", card->ext_csd.rev); |
Bastian Stender | 80a780a | 2017-11-28 09:24:06 +0100 | [diff] [blame] | 784 | MMC_DEV_ATTR(pre_eol_info, "0x%02x\n", card->ext_csd.pre_eol_info); |
Jungseung Lee | 46bc5c4 | 2016-12-22 12:37:34 +0900 | [diff] [blame] | 785 | MMC_DEV_ATTR(life_time, "0x%02x 0x%02x\n", |
| 786 | card->ext_csd.device_life_time_est_typ_a, |
| 787 | card->ext_csd.device_life_time_est_typ_b); |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 788 | MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial); |
Chuanxiao Dong | 709de99d | 2011-01-22 04:09:41 +0800 | [diff] [blame] | 789 | MMC_DEV_ATTR(enhanced_area_offset, "%llu\n", |
| 790 | card->ext_csd.enhanced_area_offset); |
| 791 | MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size); |
Loic Pallardy | 188cc04 | 2012-08-06 17:12:29 +0200 | [diff] [blame] | 792 | MMC_DEV_ATTR(raw_rpmb_size_mult, "%#x\n", card->ext_csd.raw_rpmb_size_mult); |
| 793 | MMC_DEV_ATTR(rel_sectors, "%#x\n", card->ext_csd.rel_sectors); |
Bastian Stender | c892b0d | 2017-11-28 09:24:07 +0100 | [diff] [blame] | 794 | MMC_DEV_ATTR(ocr, "0x%08x\n", card->ocr); |
Harish Jenny K N | 974e85e | 2018-03-07 10:31:42 +0530 | [diff] [blame] | 795 | MMC_DEV_ATTR(rca, "0x%04x\n", card->rca); |
Adrian Hunter | b658af7 | 2017-03-13 14:36:37 +0200 | [diff] [blame] | 796 | MMC_DEV_ATTR(cmdq_en, "%d\n", card->ext_csd.cmdq_en); |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 797 | |
Gwendal Grignou | 0f76242 | 2014-10-16 11:27:16 -0700 | [diff] [blame] | 798 | static ssize_t mmc_fwrev_show(struct device *dev, |
| 799 | struct device_attribute *attr, |
| 800 | char *buf) |
| 801 | { |
| 802 | struct mmc_card *card = mmc_dev_to_card(dev); |
| 803 | |
| 804 | if (card->ext_csd.rev < 7) { |
| 805 | return sprintf(buf, "0x%x\n", card->cid.fwrev); |
| 806 | } else { |
| 807 | return sprintf(buf, "0x%*phN\n", MMC_FIRMWARE_LEN, |
| 808 | card->ext_csd.fwrev); |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | static DEVICE_ATTR(fwrev, S_IRUGO, mmc_fwrev_show, NULL); |
| 813 | |
Bojan Prtvar | 6825a60 | 2016-07-19 11:16:38 +0200 | [diff] [blame] | 814 | static ssize_t mmc_dsr_show(struct device *dev, |
| 815 | struct device_attribute *attr, |
| 816 | char *buf) |
| 817 | { |
| 818 | struct mmc_card *card = mmc_dev_to_card(dev); |
| 819 | struct mmc_host *host = card->host; |
| 820 | |
| 821 | if (card->csd.dsr_imp && host->dsr_req) |
| 822 | return sprintf(buf, "0x%x\n", host->dsr); |
| 823 | else |
| 824 | /* return default DSR value */ |
| 825 | return sprintf(buf, "0x%x\n", 0x404); |
| 826 | } |
| 827 | |
| 828 | static DEVICE_ATTR(dsr, S_IRUGO, mmc_dsr_show, NULL); |
| 829 | |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 830 | static struct attribute *mmc_std_attrs[] = { |
| 831 | &dev_attr_cid.attr, |
| 832 | &dev_attr_csd.attr, |
| 833 | &dev_attr_date.attr, |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 834 | &dev_attr_erase_size.attr, |
| 835 | &dev_attr_preferred_erase_size.attr, |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 836 | &dev_attr_fwrev.attr, |
Gwendal Grignou | 0f76242 | 2014-10-16 11:27:16 -0700 | [diff] [blame] | 837 | &dev_attr_ffu_capable.attr, |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 838 | &dev_attr_hwrev.attr, |
| 839 | &dev_attr_manfid.attr, |
| 840 | &dev_attr_name.attr, |
| 841 | &dev_attr_oemid.attr, |
Bernie Thompson | 51e7e8b | 2013-02-27 12:19:17 -0800 | [diff] [blame] | 842 | &dev_attr_prv.attr, |
Jin Qian | 04fa054 | 2017-10-12 10:46:59 -0700 | [diff] [blame] | 843 | &dev_attr_rev.attr, |
Jungseung Lee | 46bc5c4 | 2016-12-22 12:37:34 +0900 | [diff] [blame] | 844 | &dev_attr_pre_eol_info.attr, |
| 845 | &dev_attr_life_time.attr, |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 846 | &dev_attr_serial.attr, |
Chuanxiao Dong | 709de99d | 2011-01-22 04:09:41 +0800 | [diff] [blame] | 847 | &dev_attr_enhanced_area_offset.attr, |
| 848 | &dev_attr_enhanced_area_size.attr, |
Loic Pallardy | 188cc04 | 2012-08-06 17:12:29 +0200 | [diff] [blame] | 849 | &dev_attr_raw_rpmb_size_mult.attr, |
| 850 | &dev_attr_rel_sectors.attr, |
Bojan Prtvar | 5fb06af | 2016-07-04 13:56:55 +0200 | [diff] [blame] | 851 | &dev_attr_ocr.attr, |
Harish Jenny K N | 974e85e | 2018-03-07 10:31:42 +0530 | [diff] [blame] | 852 | &dev_attr_rca.attr, |
Bojan Prtvar | 6825a60 | 2016-07-19 11:16:38 +0200 | [diff] [blame] | 853 | &dev_attr_dsr.attr, |
Adrian Hunter | b658af7 | 2017-03-13 14:36:37 +0200 | [diff] [blame] | 854 | &dev_attr_cmdq_en.attr, |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 855 | NULL, |
| 856 | }; |
Axel Lin | d1e5821 | 2014-03-08 15:05:27 +0800 | [diff] [blame] | 857 | ATTRIBUTE_GROUPS(mmc_std); |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 858 | |
| 859 | static struct device_type mmc_type = { |
Axel Lin | d1e5821 | 2014-03-08 15:05:27 +0800 | [diff] [blame] | 860 | .groups = mmc_std_groups, |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 861 | }; |
| 862 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 863 | /* |
Girish K S | b87d8db | 2011-09-23 20:41:47 +0530 | [diff] [blame] | 864 | * Select the PowerClass for the current bus width |
| 865 | * If power class is defined for 4/8 bit bus in the |
| 866 | * extended CSD register, select it by executing the |
| 867 | * mmc_switch command. |
| 868 | */ |
Seungwon Jeon | 2385049 | 2014-04-23 17:08:05 +0900 | [diff] [blame] | 869 | static int __mmc_select_powerclass(struct mmc_card *card, |
| 870 | unsigned int bus_width) |
Girish K S | b87d8db | 2011-09-23 20:41:47 +0530 | [diff] [blame] | 871 | { |
Seungwon Jeon | 2385049 | 2014-04-23 17:08:05 +0900 | [diff] [blame] | 872 | struct mmc_host *host = card->host; |
| 873 | struct mmc_ext_csd *ext_csd = &card->ext_csd; |
Fredrik Soderstedt | 6044371 | 2013-04-23 16:27:07 +0200 | [diff] [blame] | 874 | unsigned int pwrclass_val = 0; |
Seungwon Jeon | 2385049 | 2014-04-23 17:08:05 +0900 | [diff] [blame] | 875 | int err = 0; |
Girish K S | b87d8db | 2011-09-23 20:41:47 +0530 | [diff] [blame] | 876 | |
Girish K S | b87d8db | 2011-09-23 20:41:47 +0530 | [diff] [blame] | 877 | switch (1 << host->ios.vdd) { |
| 878 | case MMC_VDD_165_195: |
Seungwon Jeon | 2385049 | 2014-04-23 17:08:05 +0900 | [diff] [blame] | 879 | if (host->ios.clock <= MMC_HIGH_26_MAX_DTR) |
| 880 | pwrclass_val = ext_csd->raw_pwr_cl_26_195; |
| 881 | else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR) |
Fredrik Soderstedt | 6044371 | 2013-04-23 16:27:07 +0200 | [diff] [blame] | 882 | pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ? |
Seungwon Jeon | 2385049 | 2014-04-23 17:08:05 +0900 | [diff] [blame] | 883 | ext_csd->raw_pwr_cl_52_195 : |
| 884 | ext_csd->raw_pwr_cl_ddr_52_195; |
| 885 | else if (host->ios.clock <= MMC_HS200_MAX_DTR) |
| 886 | pwrclass_val = ext_csd->raw_pwr_cl_200_195; |
Girish K S | b87d8db | 2011-09-23 20:41:47 +0530 | [diff] [blame] | 887 | break; |
Subhash Jadavani | 93fc5a4 | 2012-04-03 12:25:58 +0530 | [diff] [blame] | 888 | case MMC_VDD_27_28: |
| 889 | case MMC_VDD_28_29: |
| 890 | case MMC_VDD_29_30: |
| 891 | case MMC_VDD_30_31: |
| 892 | case MMC_VDD_31_32: |
Girish K S | b87d8db | 2011-09-23 20:41:47 +0530 | [diff] [blame] | 893 | case MMC_VDD_32_33: |
| 894 | case MMC_VDD_33_34: |
| 895 | case MMC_VDD_34_35: |
| 896 | case MMC_VDD_35_36: |
Seungwon Jeon | 2385049 | 2014-04-23 17:08:05 +0900 | [diff] [blame] | 897 | if (host->ios.clock <= MMC_HIGH_26_MAX_DTR) |
| 898 | pwrclass_val = ext_csd->raw_pwr_cl_26_360; |
| 899 | else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR) |
Fredrik Soderstedt | 6044371 | 2013-04-23 16:27:07 +0200 | [diff] [blame] | 900 | pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ? |
Seungwon Jeon | 2385049 | 2014-04-23 17:08:05 +0900 | [diff] [blame] | 901 | ext_csd->raw_pwr_cl_52_360 : |
| 902 | ext_csd->raw_pwr_cl_ddr_52_360; |
| 903 | else if (host->ios.clock <= MMC_HS200_MAX_DTR) |
Seungwon Jeon | 0a5b643 | 2014-04-23 17:14:58 +0900 | [diff] [blame] | 904 | pwrclass_val = (bus_width == EXT_CSD_DDR_BUS_WIDTH_8) ? |
| 905 | ext_csd->raw_pwr_cl_ddr_200_360 : |
| 906 | ext_csd->raw_pwr_cl_200_360; |
Girish K S | b87d8db | 2011-09-23 20:41:47 +0530 | [diff] [blame] | 907 | break; |
| 908 | default: |
Joe Perches | 6606110 | 2014-09-12 14:56:56 -0700 | [diff] [blame] | 909 | pr_warn("%s: Voltage range not supported for power class\n", |
| 910 | mmc_hostname(host)); |
Girish K S | b87d8db | 2011-09-23 20:41:47 +0530 | [diff] [blame] | 911 | return -EINVAL; |
| 912 | } |
| 913 | |
Girish K S | b87d8db | 2011-09-23 20:41:47 +0530 | [diff] [blame] | 914 | if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8)) |
| 915 | pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >> |
| 916 | EXT_CSD_PWR_CL_8BIT_SHIFT; |
| 917 | else |
| 918 | pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >> |
| 919 | EXT_CSD_PWR_CL_4BIT_SHIFT; |
| 920 | |
| 921 | /* If the power class is different from the default value */ |
| 922 | if (pwrclass_val > 0) { |
| 923 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 924 | EXT_CSD_POWER_CLASS, |
| 925 | pwrclass_val, |
Seungwon Jeon | 71fe3eb | 2011-10-18 13:20:57 +0900 | [diff] [blame] | 926 | card->ext_csd.generic_cmd6_time); |
Girish K S | b87d8db | 2011-09-23 20:41:47 +0530 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | return err; |
| 930 | } |
| 931 | |
Seungwon Jeon | 2385049 | 2014-04-23 17:08:05 +0900 | [diff] [blame] | 932 | static int mmc_select_powerclass(struct mmc_card *card) |
| 933 | { |
| 934 | struct mmc_host *host = card->host; |
| 935 | u32 bus_width, ext_csd_bits; |
| 936 | int err, ddr; |
| 937 | |
| 938 | /* Power class selection is supported for versions >= 4.0 */ |
Ulf Hansson | 148bcab | 2014-10-20 11:33:53 +0200 | [diff] [blame] | 939 | if (!mmc_can_ext_csd(card)) |
Seungwon Jeon | 2385049 | 2014-04-23 17:08:05 +0900 | [diff] [blame] | 940 | return 0; |
| 941 | |
| 942 | bus_width = host->ios.bus_width; |
| 943 | /* Power class values are defined only for 4/8 bit bus */ |
| 944 | if (bus_width == MMC_BUS_WIDTH_1) |
| 945 | return 0; |
| 946 | |
| 947 | ddr = card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52; |
| 948 | if (ddr) |
| 949 | ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ? |
| 950 | EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4; |
| 951 | else |
| 952 | ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ? |
| 953 | EXT_CSD_BUS_WIDTH_8 : EXT_CSD_BUS_WIDTH_4; |
| 954 | |
| 955 | err = __mmc_select_powerclass(card, ext_csd_bits); |
| 956 | if (err) |
| 957 | pr_warn("%s: power class selection to bus width %d ddr %d failed\n", |
| 958 | mmc_hostname(host), 1 << bus_width, ddr); |
| 959 | |
| 960 | return err; |
| 961 | } |
| 962 | |
Girish K S | b87d8db | 2011-09-23 20:41:47 +0530 | [diff] [blame] | 963 | /* |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 964 | * Set the bus speed for the selected speed mode. |
Girish K S | a4924c7 | 2012-01-11 14:04:52 -0500 | [diff] [blame] | 965 | */ |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 966 | static void mmc_set_bus_speed(struct mmc_card *card) |
Girish K S | a4924c7 | 2012-01-11 14:04:52 -0500 | [diff] [blame] | 967 | { |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 968 | unsigned int max_dtr = (unsigned int)-1; |
| 969 | |
Seungwon Jeon | 0a5b643 | 2014-04-23 17:14:58 +0900 | [diff] [blame] | 970 | if ((mmc_card_hs200(card) || mmc_card_hs400(card)) && |
| 971 | max_dtr > card->ext_csd.hs200_max_dtr) |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 972 | max_dtr = card->ext_csd.hs200_max_dtr; |
| 973 | else if (mmc_card_hs(card) && max_dtr > card->ext_csd.hs_max_dtr) |
| 974 | max_dtr = card->ext_csd.hs_max_dtr; |
| 975 | else if (max_dtr > card->csd.max_dtr) |
| 976 | max_dtr = card->csd.max_dtr; |
| 977 | |
| 978 | mmc_set_clock(card->host, max_dtr); |
| 979 | } |
| 980 | |
| 981 | /* |
| 982 | * Select the bus width amoung 4-bit and 8-bit(SDR). |
| 983 | * If the bus width is changed successfully, return the selected width value. |
| 984 | * Zero is returned instead of error value if the wide width is not supported. |
| 985 | */ |
| 986 | static int mmc_select_bus_width(struct mmc_card *card) |
| 987 | { |
Girish K S | a4924c7 | 2012-01-11 14:04:52 -0500 | [diff] [blame] | 988 | static unsigned ext_csd_bits[] = { |
Girish K S | a4924c7 | 2012-01-11 14:04:52 -0500 | [diff] [blame] | 989 | EXT_CSD_BUS_WIDTH_8, |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 990 | EXT_CSD_BUS_WIDTH_4, |
Girish K S | a4924c7 | 2012-01-11 14:04:52 -0500 | [diff] [blame] | 991 | }; |
| 992 | static unsigned bus_widths[] = { |
Girish K S | a4924c7 | 2012-01-11 14:04:52 -0500 | [diff] [blame] | 993 | MMC_BUS_WIDTH_8, |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 994 | MMC_BUS_WIDTH_4, |
Girish K S | a4924c7 | 2012-01-11 14:04:52 -0500 | [diff] [blame] | 995 | }; |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 996 | struct mmc_host *host = card->host; |
| 997 | unsigned idx, bus_width = 0; |
| 998 | int err = 0; |
Girish K S | a4924c7 | 2012-01-11 14:04:52 -0500 | [diff] [blame] | 999 | |
Alexandre Belloni | 1c2d26e | 2014-12-17 19:32:06 +0100 | [diff] [blame] | 1000 | if (!mmc_can_ext_csd(card) || |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1001 | !(host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA))) |
| 1002 | return 0; |
Girish K S | a4924c7 | 2012-01-11 14:04:52 -0500 | [diff] [blame] | 1003 | |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1004 | idx = (host->caps & MMC_CAP_8_BIT_DATA) ? 0 : 1; |
Girish K S | a4924c7 | 2012-01-11 14:04:52 -0500 | [diff] [blame] | 1005 | |
| 1006 | /* |
| 1007 | * Unlike SD, MMC cards dont have a configuration register to notify |
| 1008 | * supported bus width. So bus test command should be run to identify |
| 1009 | * the supported bus width or compare the ext csd values of current |
| 1010 | * bus width and ext csd values of 1 bit mode read earlier. |
| 1011 | */ |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1012 | for (; idx < ARRAY_SIZE(bus_widths); idx++) { |
Girish K S | a4924c7 | 2012-01-11 14:04:52 -0500 | [diff] [blame] | 1013 | /* |
| 1014 | * Host is capable of 8bit transfer, then switch |
| 1015 | * the device to work in 8bit transfer mode. If the |
| 1016 | * mmc switch command returns error then switch to |
| 1017 | * 4bit transfer mode. On success set the corresponding |
| 1018 | * bus width on the host. |
| 1019 | */ |
| 1020 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 1021 | EXT_CSD_BUS_WIDTH, |
| 1022 | ext_csd_bits[idx], |
| 1023 | card->ext_csd.generic_cmd6_time); |
| 1024 | if (err) |
| 1025 | continue; |
| 1026 | |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1027 | bus_width = bus_widths[idx]; |
| 1028 | mmc_set_bus_width(host, bus_width); |
Girish K S | a4924c7 | 2012-01-11 14:04:52 -0500 | [diff] [blame] | 1029 | |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1030 | /* |
| 1031 | * If controller can't handle bus width test, |
| 1032 | * compare ext_csd previously read in 1 bit mode |
| 1033 | * against ext_csd at new bus width |
| 1034 | */ |
Girish K S | a4924c7 | 2012-01-11 14:04:52 -0500 | [diff] [blame] | 1035 | if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST)) |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1036 | err = mmc_compare_ext_csds(card, bus_width); |
Girish K S | a4924c7 | 2012-01-11 14:04:52 -0500 | [diff] [blame] | 1037 | else |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1038 | err = mmc_bus_test(card, bus_width); |
| 1039 | |
| 1040 | if (!err) { |
| 1041 | err = bus_width; |
Girish K S | a4924c7 | 2012-01-11 14:04:52 -0500 | [diff] [blame] | 1042 | break; |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1043 | } else { |
| 1044 | pr_warn("%s: switch to bus width %d failed\n", |
Wolfram Sang | ed9feec | 2016-01-29 09:27:50 +0100 | [diff] [blame] | 1045 | mmc_hostname(host), 1 << bus_width); |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1046 | } |
Girish K S | a4924c7 | 2012-01-11 14:04:52 -0500 | [diff] [blame] | 1047 | } |
| 1048 | |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1049 | return err; |
| 1050 | } |
| 1051 | |
| 1052 | /* |
| 1053 | * Switch to the high-speed mode |
| 1054 | */ |
| 1055 | static int mmc_select_hs(struct mmc_card *card) |
| 1056 | { |
| 1057 | int err; |
| 1058 | |
| 1059 | err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 1060 | EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS, |
Ulf Hansson | 53e6065 | 2016-11-09 19:40:29 +0100 | [diff] [blame] | 1061 | card->ext_csd.generic_cmd6_time, MMC_TIMING_MMC_HS, |
| 1062 | true, true, true); |
Jungseung Lee | 67d3596 | 2016-08-24 19:34:09 +0900 | [diff] [blame] | 1063 | if (err) |
| 1064 | pr_warn("%s: switch to high-speed failed, err:%d\n", |
| 1065 | mmc_hostname(card->host), err); |
| 1066 | |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1067 | return err; |
| 1068 | } |
| 1069 | |
| 1070 | /* |
| 1071 | * Activate wide bus and DDR if supported. |
| 1072 | */ |
| 1073 | static int mmc_select_hs_ddr(struct mmc_card *card) |
| 1074 | { |
| 1075 | struct mmc_host *host = card->host; |
| 1076 | u32 bus_width, ext_csd_bits; |
| 1077 | int err = 0; |
| 1078 | |
| 1079 | if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52)) |
| 1080 | return 0; |
| 1081 | |
| 1082 | bus_width = host->ios.bus_width; |
| 1083 | if (bus_width == MMC_BUS_WIDTH_1) |
| 1084 | return 0; |
| 1085 | |
| 1086 | ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ? |
| 1087 | EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4; |
| 1088 | |
Ulf Hansson | e173f89 | 2016-11-09 21:00:26 +0100 | [diff] [blame] | 1089 | err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 1090 | EXT_CSD_BUS_WIDTH, |
| 1091 | ext_csd_bits, |
| 1092 | card->ext_csd.generic_cmd6_time, |
| 1093 | MMC_TIMING_MMC_DDR52, |
| 1094 | true, true, true); |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1095 | if (err) { |
Andrew Gabbasov | 4b75bff | 2014-10-01 07:14:11 -0500 | [diff] [blame] | 1096 | pr_err("%s: switch to bus width %d ddr failed\n", |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1097 | mmc_hostname(host), 1 << bus_width); |
| 1098 | return err; |
| 1099 | } |
| 1100 | |
| 1101 | /* |
| 1102 | * eMMC cards can support 3.3V to 1.2V i/o (vccq) |
| 1103 | * signaling. |
| 1104 | * |
| 1105 | * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq. |
| 1106 | * |
| 1107 | * 1.8V vccq at 3.3V core voltage (vcc) is not required |
| 1108 | * in the JEDEC spec for DDR. |
| 1109 | * |
Chuanxiao.Dong | 312449e | 2014-08-15 11:28:07 +0800 | [diff] [blame] | 1110 | * Even (e)MMC card can support 3.3v to 1.2v vccq, but not all |
| 1111 | * host controller can support this, like some of the SDHCI |
| 1112 | * controller which connect to an eMMC device. Some of these |
| 1113 | * host controller still needs to use 1.8v vccq for supporting |
| 1114 | * DDR mode. |
| 1115 | * |
| 1116 | * So the sequence will be: |
| 1117 | * if (host and device can both support 1.2v IO) |
| 1118 | * use 1.2v IO; |
| 1119 | * else if (host and device can both support 1.8v IO) |
| 1120 | * use 1.8v IO; |
| 1121 | * so if host and device can only support 3.3v IO, this is the |
| 1122 | * last choice. |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1123 | * |
| 1124 | * WARNING: eMMC rules are NOT the same as SD DDR |
| 1125 | */ |
Ulf Hansson | 20f921b | 2017-01-25 12:04:17 +0100 | [diff] [blame] | 1126 | if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_2V) { |
Ulf Hansson | 4e74b6b | 2017-01-25 11:12:34 +0100 | [diff] [blame] | 1127 | err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120); |
Ulf Hansson | 20f921b | 2017-01-25 12:04:17 +0100 | [diff] [blame] | 1128 | if (!err) |
| 1129 | return 0; |
| 1130 | } |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1131 | |
Ulf Hansson | 20f921b | 2017-01-25 12:04:17 +0100 | [diff] [blame] | 1132 | if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_8V && |
| 1133 | host->caps & MMC_CAP_1_8V_DDR) |
Ulf Hansson | 4e74b6b | 2017-01-25 11:12:34 +0100 | [diff] [blame] | 1134 | err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180); |
Chuanxiao.Dong | 312449e | 2014-08-15 11:28:07 +0800 | [diff] [blame] | 1135 | |
| 1136 | /* make sure vccq is 3.3v after switching disaster */ |
| 1137 | if (err) |
Ulf Hansson | 4e74b6b | 2017-01-25 11:12:34 +0100 | [diff] [blame] | 1138 | err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330); |
Chuanxiao.Dong | 312449e | 2014-08-15 11:28:07 +0800 | [diff] [blame] | 1139 | |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1140 | return err; |
| 1141 | } |
| 1142 | |
Seungwon Jeon | 0a5b643 | 2014-04-23 17:14:58 +0900 | [diff] [blame] | 1143 | static int mmc_select_hs400(struct mmc_card *card) |
| 1144 | { |
| 1145 | struct mmc_host *host = card->host; |
Adrian Hunter | 51b12f7 | 2015-10-28 14:25:41 +0200 | [diff] [blame] | 1146 | unsigned int max_dtr; |
Seungwon Jeon | 0a5b643 | 2014-04-23 17:14:58 +0900 | [diff] [blame] | 1147 | int err = 0; |
Adrian Hunter | cc4f414 | 2015-02-06 14:12:58 +0200 | [diff] [blame] | 1148 | u8 val; |
Seungwon Jeon | 0a5b643 | 2014-04-23 17:14:58 +0900 | [diff] [blame] | 1149 | |
| 1150 | /* |
| 1151 | * HS400 mode requires 8-bit bus width |
| 1152 | */ |
| 1153 | if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 && |
| 1154 | host->ios.bus_width == MMC_BUS_WIDTH_8)) |
| 1155 | return 0; |
| 1156 | |
Adrian Hunter | 51b12f7 | 2015-10-28 14:25:41 +0200 | [diff] [blame] | 1157 | /* Switch card to HS mode */ |
Wenkai Du | adb24d4 | 2015-11-26 14:00:44 +0200 | [diff] [blame] | 1158 | val = EXT_CSD_TIMING_HS; |
Seungwon Jeon | 0a5b643 | 2014-04-23 17:14:58 +0900 | [diff] [blame] | 1159 | err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
Adrian Hunter | cc4f414 | 2015-02-06 14:12:58 +0200 | [diff] [blame] | 1160 | EXT_CSD_HS_TIMING, val, |
Ulf Hansson | aa33ce3 | 2016-11-09 17:33:36 +0100 | [diff] [blame] | 1161 | card->ext_csd.generic_cmd6_time, 0, |
Chaotian Jing | 08573ea | 2016-05-19 16:47:41 +0800 | [diff] [blame] | 1162 | true, false, true); |
Seungwon Jeon | 0a5b643 | 2014-04-23 17:14:58 +0900 | [diff] [blame] | 1163 | if (err) { |
Andrew Gabbasov | 4b75bff | 2014-10-01 07:14:11 -0500 | [diff] [blame] | 1164 | pr_err("%s: switch to high-speed from hs200 failed, err:%d\n", |
Seungwon Jeon | 0a5b643 | 2014-04-23 17:14:58 +0900 | [diff] [blame] | 1165 | mmc_hostname(host), err); |
| 1166 | return err; |
| 1167 | } |
| 1168 | |
Adrian Hunter | 51b12f7 | 2015-10-28 14:25:41 +0200 | [diff] [blame] | 1169 | /* Set host controller to HS timing */ |
| 1170 | mmc_set_timing(card->host, MMC_TIMING_MMC_HS); |
| 1171 | |
Simon Horman | ba6c7ac | 2018-06-18 14:57:49 +0200 | [diff] [blame] | 1172 | /* Prepare host to downgrade to HS timing */ |
| 1173 | if (host->ops->hs400_downgrade) |
| 1174 | host->ops->hs400_downgrade(host); |
| 1175 | |
Ziyuan Xu | 649c605 | 2016-05-26 13:50:32 +0800 | [diff] [blame] | 1176 | /* Reduce frequency to HS frequency */ |
| 1177 | max_dtr = card->ext_csd.hs_max_dtr; |
| 1178 | mmc_set_clock(host, max_dtr); |
| 1179 | |
Chaotian Jing | 08573ea | 2016-05-19 16:47:41 +0800 | [diff] [blame] | 1180 | err = mmc_switch_status(card); |
| 1181 | if (err) |
| 1182 | goto out_err; |
Adrian Hunter | d230293 | 2015-10-28 14:25:43 +0200 | [diff] [blame] | 1183 | |
| 1184 | /* Switch card to DDR */ |
Seungwon Jeon | 0a5b643 | 2014-04-23 17:14:58 +0900 | [diff] [blame] | 1185 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 1186 | EXT_CSD_BUS_WIDTH, |
| 1187 | EXT_CSD_DDR_BUS_WIDTH_8, |
| 1188 | card->ext_csd.generic_cmd6_time); |
| 1189 | if (err) { |
Andrew Gabbasov | 4b75bff | 2014-10-01 07:14:11 -0500 | [diff] [blame] | 1190 | pr_err("%s: switch to bus width for hs400 failed, err:%d\n", |
Seungwon Jeon | 0a5b643 | 2014-04-23 17:14:58 +0900 | [diff] [blame] | 1191 | mmc_hostname(host), err); |
| 1192 | return err; |
| 1193 | } |
| 1194 | |
Adrian Hunter | d230293 | 2015-10-28 14:25:43 +0200 | [diff] [blame] | 1195 | /* Switch card to HS400 */ |
Adrian Hunter | cc4f414 | 2015-02-06 14:12:58 +0200 | [diff] [blame] | 1196 | val = EXT_CSD_TIMING_HS400 | |
| 1197 | card->drive_strength << EXT_CSD_DRV_STR_SHIFT; |
Seungwon Jeon | 0a5b643 | 2014-04-23 17:14:58 +0900 | [diff] [blame] | 1198 | err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
Adrian Hunter | cc4f414 | 2015-02-06 14:12:58 +0200 | [diff] [blame] | 1199 | EXT_CSD_HS_TIMING, val, |
Ulf Hansson | aa33ce3 | 2016-11-09 17:33:36 +0100 | [diff] [blame] | 1200 | card->ext_csd.generic_cmd6_time, 0, |
Chaotian Jing | 08573ea | 2016-05-19 16:47:41 +0800 | [diff] [blame] | 1201 | true, false, true); |
Seungwon Jeon | 0a5b643 | 2014-04-23 17:14:58 +0900 | [diff] [blame] | 1202 | if (err) { |
Andrew Gabbasov | 4b75bff | 2014-10-01 07:14:11 -0500 | [diff] [blame] | 1203 | pr_err("%s: switch to hs400 failed, err:%d\n", |
Seungwon Jeon | 0a5b643 | 2014-04-23 17:14:58 +0900 | [diff] [blame] | 1204 | mmc_hostname(host), err); |
| 1205 | return err; |
| 1206 | } |
| 1207 | |
Adrian Hunter | d230293 | 2015-10-28 14:25:43 +0200 | [diff] [blame] | 1208 | /* Set host controller to HS400 timing and frequency */ |
Seungwon Jeon | 0a5b643 | 2014-04-23 17:14:58 +0900 | [diff] [blame] | 1209 | mmc_set_timing(host, MMC_TIMING_MMC_HS400); |
| 1210 | mmc_set_bus_speed(card); |
| 1211 | |
Chaotian Jing | 08573ea | 2016-05-19 16:47:41 +0800 | [diff] [blame] | 1212 | err = mmc_switch_status(card); |
| 1213 | if (err) |
| 1214 | goto out_err; |
Adrian Hunter | d230293 | 2015-10-28 14:25:43 +0200 | [diff] [blame] | 1215 | |
Simon Horman | ba6c7ac | 2018-06-18 14:57:49 +0200 | [diff] [blame] | 1216 | if (host->ops->hs400_complete) |
| 1217 | host->ops->hs400_complete(host); |
| 1218 | |
Seungwon Jeon | 0a5b643 | 2014-04-23 17:14:58 +0900 | [diff] [blame] | 1219 | return 0; |
Adrian Hunter | d230293 | 2015-10-28 14:25:43 +0200 | [diff] [blame] | 1220 | |
| 1221 | out_err: |
| 1222 | pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host), |
| 1223 | __func__, err); |
| 1224 | return err; |
Seungwon Jeon | 0a5b643 | 2014-04-23 17:14:58 +0900 | [diff] [blame] | 1225 | } |
| 1226 | |
Adrian Hunter | 6376f69 | 2015-05-07 13:10:20 +0300 | [diff] [blame] | 1227 | int mmc_hs200_to_hs400(struct mmc_card *card) |
| 1228 | { |
| 1229 | return mmc_select_hs400(card); |
| 1230 | } |
| 1231 | |
Adrian Hunter | 6376f69 | 2015-05-07 13:10:20 +0300 | [diff] [blame] | 1232 | int mmc_hs400_to_hs200(struct mmc_card *card) |
| 1233 | { |
| 1234 | struct mmc_host *host = card->host; |
Adrian Hunter | 6376f69 | 2015-05-07 13:10:20 +0300 | [diff] [blame] | 1235 | unsigned int max_dtr; |
| 1236 | int err; |
Adrian Hunter | cc4f414 | 2015-02-06 14:12:58 +0200 | [diff] [blame] | 1237 | u8 val; |
Adrian Hunter | 6376f69 | 2015-05-07 13:10:20 +0300 | [diff] [blame] | 1238 | |
Adrian Hunter | 6376f69 | 2015-05-07 13:10:20 +0300 | [diff] [blame] | 1239 | /* Reduce frequency to HS */ |
| 1240 | max_dtr = card->ext_csd.hs_max_dtr; |
| 1241 | mmc_set_clock(host, max_dtr); |
| 1242 | |
| 1243 | /* Switch HS400 to HS DDR */ |
Wenkai Du | adb24d4 | 2015-11-26 14:00:44 +0200 | [diff] [blame] | 1244 | val = EXT_CSD_TIMING_HS; |
Adrian Hunter | 6376f69 | 2015-05-07 13:10:20 +0300 | [diff] [blame] | 1245 | err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, |
Ulf Hansson | aa33ce3 | 2016-11-09 17:33:36 +0100 | [diff] [blame] | 1246 | val, card->ext_csd.generic_cmd6_time, 0, |
Chaotian Jing | 08573ea | 2016-05-19 16:47:41 +0800 | [diff] [blame] | 1247 | true, false, true); |
Adrian Hunter | 6376f69 | 2015-05-07 13:10:20 +0300 | [diff] [blame] | 1248 | if (err) |
| 1249 | goto out_err; |
| 1250 | |
| 1251 | mmc_set_timing(host, MMC_TIMING_MMC_DDR52); |
| 1252 | |
Chaotian Jing | 08573ea | 2016-05-19 16:47:41 +0800 | [diff] [blame] | 1253 | err = mmc_switch_status(card); |
| 1254 | if (err) |
| 1255 | goto out_err; |
Adrian Hunter | 6376f69 | 2015-05-07 13:10:20 +0300 | [diff] [blame] | 1256 | |
| 1257 | /* Switch HS DDR to HS */ |
| 1258 | err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH, |
| 1259 | EXT_CSD_BUS_WIDTH_8, card->ext_csd.generic_cmd6_time, |
Ulf Hansson | aa33ce3 | 2016-11-09 17:33:36 +0100 | [diff] [blame] | 1260 | 0, true, false, true); |
Adrian Hunter | 6376f69 | 2015-05-07 13:10:20 +0300 | [diff] [blame] | 1261 | if (err) |
| 1262 | goto out_err; |
| 1263 | |
| 1264 | mmc_set_timing(host, MMC_TIMING_MMC_HS); |
| 1265 | |
Simon Horman | ba6c7ac | 2018-06-18 14:57:49 +0200 | [diff] [blame] | 1266 | if (host->ops->hs400_downgrade) |
| 1267 | host->ops->hs400_downgrade(host); |
| 1268 | |
Chaotian Jing | 08573ea | 2016-05-19 16:47:41 +0800 | [diff] [blame] | 1269 | err = mmc_switch_status(card); |
| 1270 | if (err) |
| 1271 | goto out_err; |
Adrian Hunter | 6376f69 | 2015-05-07 13:10:20 +0300 | [diff] [blame] | 1272 | |
| 1273 | /* Switch HS to HS200 */ |
Adrian Hunter | cc4f414 | 2015-02-06 14:12:58 +0200 | [diff] [blame] | 1274 | val = EXT_CSD_TIMING_HS200 | |
| 1275 | card->drive_strength << EXT_CSD_DRV_STR_SHIFT; |
Adrian Hunter | 6376f69 | 2015-05-07 13:10:20 +0300 | [diff] [blame] | 1276 | err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, |
Ulf Hansson | aa33ce3 | 2016-11-09 17:33:36 +0100 | [diff] [blame] | 1277 | val, card->ext_csd.generic_cmd6_time, 0, |
Chaotian Jing | 08573ea | 2016-05-19 16:47:41 +0800 | [diff] [blame] | 1278 | true, false, true); |
Adrian Hunter | 6376f69 | 2015-05-07 13:10:20 +0300 | [diff] [blame] | 1279 | if (err) |
| 1280 | goto out_err; |
| 1281 | |
| 1282 | mmc_set_timing(host, MMC_TIMING_MMC_HS200); |
| 1283 | |
Adrian Hunter | ef3d232 | 2016-12-02 13:16:35 +0200 | [diff] [blame] | 1284 | /* |
| 1285 | * For HS200, CRC errors are not a reliable way to know the switch |
| 1286 | * failed. If there really is a problem, we would expect tuning will |
| 1287 | * fail and the result ends up the same. |
| 1288 | */ |
| 1289 | err = __mmc_switch_status(card, false); |
Chaotian Jing | 08573ea | 2016-05-19 16:47:41 +0800 | [diff] [blame] | 1290 | if (err) |
| 1291 | goto out_err; |
Adrian Hunter | 6376f69 | 2015-05-07 13:10:20 +0300 | [diff] [blame] | 1292 | |
| 1293 | mmc_set_bus_speed(card); |
| 1294 | |
Ulf Hansson | c8a4e30 | 2018-05-22 16:26:26 +0200 | [diff] [blame] | 1295 | /* Prepare tuning for HS400 mode. */ |
| 1296 | if (host->ops->prepare_hs400_tuning) |
| 1297 | host->ops->prepare_hs400_tuning(host, &host->ios); |
| 1298 | |
Adrian Hunter | 6376f69 | 2015-05-07 13:10:20 +0300 | [diff] [blame] | 1299 | return 0; |
| 1300 | |
| 1301 | out_err: |
| 1302 | pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host), |
| 1303 | __func__, err); |
| 1304 | return err; |
| 1305 | } |
| 1306 | |
Chanho Min | fb45886 | 2017-09-26 09:03:40 +0900 | [diff] [blame] | 1307 | static void mmc_select_driver_type(struct mmc_card *card) |
| 1308 | { |
Wolfram Sang | be17f1c | 2017-11-30 15:49:10 +0100 | [diff] [blame] | 1309 | int card_drv_type, drive_strength, drv_type = 0; |
Wolfram Sang | 6186d06 | 2017-10-15 14:46:14 +0200 | [diff] [blame] | 1310 | int fixed_drv_type = card->host->fixed_drv_type; |
Chanho Min | fb45886 | 2017-09-26 09:03:40 +0900 | [diff] [blame] | 1311 | |
| 1312 | card_drv_type = card->ext_csd.raw_driver_strength | |
| 1313 | mmc_driver_type_mask(0); |
| 1314 | |
Wolfram Sang | 6186d06 | 2017-10-15 14:46:14 +0200 | [diff] [blame] | 1315 | if (fixed_drv_type >= 0) |
| 1316 | drive_strength = card_drv_type & mmc_driver_type_mask(fixed_drv_type) |
| 1317 | ? fixed_drv_type : 0; |
| 1318 | else |
| 1319 | drive_strength = mmc_select_drive_strength(card, |
| 1320 | card->ext_csd.hs200_max_dtr, |
| 1321 | card_drv_type, &drv_type); |
Chanho Min | fb45886 | 2017-09-26 09:03:40 +0900 | [diff] [blame] | 1322 | |
| 1323 | card->drive_strength = drive_strength; |
| 1324 | |
| 1325 | if (drv_type) |
| 1326 | mmc_set_driver_type(card->host, drv_type); |
| 1327 | } |
| 1328 | |
Shawn Lin | 81ac2af | 2016-05-26 09:56:22 +0800 | [diff] [blame] | 1329 | static int mmc_select_hs400es(struct mmc_card *card) |
| 1330 | { |
| 1331 | struct mmc_host *host = card->host; |
Haibo Chen | 92ddd95 | 2017-08-08 18:54:01 +0800 | [diff] [blame] | 1332 | int err = -EINVAL; |
Shawn Lin | 81ac2af | 2016-05-26 09:56:22 +0800 | [diff] [blame] | 1333 | u8 val; |
| 1334 | |
| 1335 | if (!(host->caps & MMC_CAP_8_BIT_DATA)) { |
| 1336 | err = -ENOTSUPP; |
| 1337 | goto out_err; |
| 1338 | } |
| 1339 | |
Shawn Lin | 1720d35 | 2016-09-30 14:18:58 +0800 | [diff] [blame] | 1340 | if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400_1_2V) |
Ulf Hansson | 4e74b6b | 2017-01-25 11:12:34 +0100 | [diff] [blame] | 1341 | err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120); |
Shawn Lin | 1720d35 | 2016-09-30 14:18:58 +0800 | [diff] [blame] | 1342 | |
| 1343 | if (err && card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400_1_8V) |
Ulf Hansson | 4e74b6b | 2017-01-25 11:12:34 +0100 | [diff] [blame] | 1344 | err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180); |
Shawn Lin | 1720d35 | 2016-09-30 14:18:58 +0800 | [diff] [blame] | 1345 | |
| 1346 | /* If fails try again during next card power cycle */ |
| 1347 | if (err) |
| 1348 | goto out_err; |
| 1349 | |
Shawn Lin | 81ac2af | 2016-05-26 09:56:22 +0800 | [diff] [blame] | 1350 | err = mmc_select_bus_width(card); |
Hongjie Fang | 7f38abf | 2018-07-31 10:55:09 +0800 | [diff] [blame] | 1351 | if (err != MMC_BUS_WIDTH_8) { |
| 1352 | pr_err("%s: switch to 8bit bus width failed, err:%d\n", |
| 1353 | mmc_hostname(host), err); |
| 1354 | err = err < 0 ? err : -ENOTSUPP; |
Shawn Lin | 81ac2af | 2016-05-26 09:56:22 +0800 | [diff] [blame] | 1355 | goto out_err; |
Hongjie Fang | 7f38abf | 2018-07-31 10:55:09 +0800 | [diff] [blame] | 1356 | } |
Shawn Lin | 81ac2af | 2016-05-26 09:56:22 +0800 | [diff] [blame] | 1357 | |
| 1358 | /* Switch card to HS mode */ |
Ulf Hansson | 53e6065 | 2016-11-09 19:40:29 +0100 | [diff] [blame] | 1359 | err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 1360 | EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS, |
| 1361 | card->ext_csd.generic_cmd6_time, 0, |
| 1362 | true, false, true); |
| 1363 | if (err) { |
| 1364 | pr_err("%s: switch to hs for hs400es failed, err:%d\n", |
| 1365 | mmc_hostname(host), err); |
| 1366 | goto out_err; |
| 1367 | } |
| 1368 | |
| 1369 | mmc_set_timing(host, MMC_TIMING_MMC_HS); |
| 1370 | err = mmc_switch_status(card); |
Jungseung Lee | 67d3596 | 2016-08-24 19:34:09 +0900 | [diff] [blame] | 1371 | if (err) |
Shawn Lin | 81ac2af | 2016-05-26 09:56:22 +0800 | [diff] [blame] | 1372 | goto out_err; |
Shawn Lin | 81ac2af | 2016-05-26 09:56:22 +0800 | [diff] [blame] | 1373 | |
Shawn Lin | 4f25580 | 2016-09-30 14:18:59 +0800 | [diff] [blame] | 1374 | mmc_set_clock(host, card->ext_csd.hs_max_dtr); |
| 1375 | |
Shawn Lin | 81ac2af | 2016-05-26 09:56:22 +0800 | [diff] [blame] | 1376 | /* Switch card to DDR with strobe bit */ |
| 1377 | val = EXT_CSD_DDR_BUS_WIDTH_8 | EXT_CSD_BUS_WIDTH_STROBE; |
| 1378 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 1379 | EXT_CSD_BUS_WIDTH, |
| 1380 | val, |
| 1381 | card->ext_csd.generic_cmd6_time); |
| 1382 | if (err) { |
| 1383 | pr_err("%s: switch to bus width for hs400es failed, err:%d\n", |
| 1384 | mmc_hostname(host), err); |
| 1385 | goto out_err; |
| 1386 | } |
| 1387 | |
Chanho Min | fb45886 | 2017-09-26 09:03:40 +0900 | [diff] [blame] | 1388 | mmc_select_driver_type(card); |
| 1389 | |
Shawn Lin | 81ac2af | 2016-05-26 09:56:22 +0800 | [diff] [blame] | 1390 | /* Switch card to HS400 */ |
| 1391 | val = EXT_CSD_TIMING_HS400 | |
| 1392 | card->drive_strength << EXT_CSD_DRV_STR_SHIFT; |
| 1393 | err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 1394 | EXT_CSD_HS_TIMING, val, |
Ulf Hansson | aa33ce3 | 2016-11-09 17:33:36 +0100 | [diff] [blame] | 1395 | card->ext_csd.generic_cmd6_time, 0, |
Shawn Lin | 81ac2af | 2016-05-26 09:56:22 +0800 | [diff] [blame] | 1396 | true, false, true); |
| 1397 | if (err) { |
| 1398 | pr_err("%s: switch to hs400es failed, err:%d\n", |
| 1399 | mmc_hostname(host), err); |
| 1400 | goto out_err; |
| 1401 | } |
| 1402 | |
| 1403 | /* Set host controller to HS400 timing and frequency */ |
| 1404 | mmc_set_timing(host, MMC_TIMING_MMC_HS400); |
| 1405 | |
| 1406 | /* Controller enable enhanced strobe function */ |
| 1407 | host->ios.enhanced_strobe = true; |
| 1408 | if (host->ops->hs400_enhanced_strobe) |
| 1409 | host->ops->hs400_enhanced_strobe(host, &host->ios); |
| 1410 | |
| 1411 | err = mmc_switch_status(card); |
| 1412 | if (err) |
| 1413 | goto out_err; |
| 1414 | |
| 1415 | return 0; |
| 1416 | |
| 1417 | out_err: |
| 1418 | pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host), |
| 1419 | __func__, err); |
| 1420 | return err; |
| 1421 | } |
| 1422 | |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1423 | /* |
| 1424 | * For device supporting HS200 mode, the following sequence |
| 1425 | * should be done before executing the tuning process. |
| 1426 | * 1. set the desired bus width(4-bit or 8-bit, 1-bit is not supported) |
| 1427 | * 2. switch to HS200 mode |
| 1428 | * 3. set the clock to > 52Mhz and <=200MHz |
| 1429 | */ |
| 1430 | static int mmc_select_hs200(struct mmc_card *card) |
| 1431 | { |
| 1432 | struct mmc_host *host = card->host; |
Dong Aisheng | e51534c | 2016-04-21 00:51:30 +0800 | [diff] [blame] | 1433 | unsigned int old_timing, old_signal_voltage; |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1434 | int err = -EINVAL; |
Adrian Hunter | cc4f414 | 2015-02-06 14:12:58 +0200 | [diff] [blame] | 1435 | u8 val; |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1436 | |
Dong Aisheng | e51534c | 2016-04-21 00:51:30 +0800 | [diff] [blame] | 1437 | old_signal_voltage = host->ios.signal_voltage; |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1438 | if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_2V) |
Ulf Hansson | 4e74b6b | 2017-01-25 11:12:34 +0100 | [diff] [blame] | 1439 | err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120); |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1440 | |
| 1441 | if (err && card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_8V) |
Ulf Hansson | 4e74b6b | 2017-01-25 11:12:34 +0100 | [diff] [blame] | 1442 | err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180); |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1443 | |
| 1444 | /* If fails try again during next card power cycle */ |
| 1445 | if (err) |
Dong Aisheng | e51534c | 2016-04-21 00:51:30 +0800 | [diff] [blame] | 1446 | return err; |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1447 | |
Adrian Hunter | cc4f414 | 2015-02-06 14:12:58 +0200 | [diff] [blame] | 1448 | mmc_select_driver_type(card); |
| 1449 | |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1450 | /* |
| 1451 | * Set the bus width(4 or 8) with host's support and |
| 1452 | * switch to HS200 mode if bus width is set successfully. |
| 1453 | */ |
| 1454 | err = mmc_select_bus_width(card); |
Peter Griffin | 8b7be8f | 2016-06-08 11:27:52 +0100 | [diff] [blame] | 1455 | if (err > 0) { |
Adrian Hunter | cc4f414 | 2015-02-06 14:12:58 +0200 | [diff] [blame] | 1456 | val = EXT_CSD_TIMING_HS200 | |
| 1457 | card->drive_strength << EXT_CSD_DRV_STR_SHIFT; |
Ulf Hansson | 4509f8477 | 2014-01-08 16:09:33 +0100 | [diff] [blame] | 1458 | err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
Adrian Hunter | cc4f414 | 2015-02-06 14:12:58 +0200 | [diff] [blame] | 1459 | EXT_CSD_HS_TIMING, val, |
Ulf Hansson | aa33ce3 | 2016-11-09 17:33:36 +0100 | [diff] [blame] | 1460 | card->ext_csd.generic_cmd6_time, 0, |
Chaotian Jing | 08573ea | 2016-05-19 16:47:41 +0800 | [diff] [blame] | 1461 | true, false, true); |
Adrian Hunter | 1815e61 | 2015-10-28 14:25:40 +0200 | [diff] [blame] | 1462 | if (err) |
| 1463 | goto err; |
| 1464 | old_timing = host->ios.timing; |
| 1465 | mmc_set_timing(host, MMC_TIMING_MMC_HS200); |
Chaotian Jing | 08573ea | 2016-05-19 16:47:41 +0800 | [diff] [blame] | 1466 | |
Adrian Hunter | ef3d232 | 2016-12-02 13:16:35 +0200 | [diff] [blame] | 1467 | /* |
| 1468 | * For HS200, CRC errors are not a reliable way to know the |
| 1469 | * switch failed. If there really is a problem, we would expect |
| 1470 | * tuning will fail and the result ends up the same. |
| 1471 | */ |
| 1472 | err = __mmc_switch_status(card, false); |
| 1473 | |
Chaotian Jing | 08573ea | 2016-05-19 16:47:41 +0800 | [diff] [blame] | 1474 | /* |
| 1475 | * mmc_select_timing() assumes timing has not changed if |
| 1476 | * it is a switch error. |
| 1477 | */ |
| 1478 | if (err == -EBADMSG) |
| 1479 | mmc_set_timing(host, old_timing); |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1480 | } |
Girish K S | a4924c7 | 2012-01-11 14:04:52 -0500 | [diff] [blame] | 1481 | err: |
Dong Aisheng | e51534c | 2016-04-21 00:51:30 +0800 | [diff] [blame] | 1482 | if (err) { |
| 1483 | /* fall back to the old signal voltage, if fails report error */ |
Ulf Hansson | 4e74b6b | 2017-01-25 11:12:34 +0100 | [diff] [blame] | 1484 | if (mmc_set_signal_voltage(host, old_signal_voltage)) |
Dong Aisheng | e51534c | 2016-04-21 00:51:30 +0800 | [diff] [blame] | 1485 | err = -EIO; |
| 1486 | |
Adrian Hunter | 1815e61 | 2015-10-28 14:25:40 +0200 | [diff] [blame] | 1487 | pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host), |
| 1488 | __func__, err); |
Dong Aisheng | e51534c | 2016-04-21 00:51:30 +0800 | [diff] [blame] | 1489 | } |
Girish K S | a4924c7 | 2012-01-11 14:04:52 -0500 | [diff] [blame] | 1490 | return err; |
| 1491 | } |
| 1492 | |
| 1493 | /* |
Shawn Lin | 81ac2af | 2016-05-26 09:56:22 +0800 | [diff] [blame] | 1494 | * Activate High Speed, HS200 or HS400ES mode if supported. |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1495 | */ |
| 1496 | static int mmc_select_timing(struct mmc_card *card) |
| 1497 | { |
| 1498 | int err = 0; |
| 1499 | |
Ulf Hansson | 148bcab | 2014-10-20 11:33:53 +0200 | [diff] [blame] | 1500 | if (!mmc_can_ext_csd(card)) |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1501 | goto bus_speed; |
| 1502 | |
Shawn Lin | 81ac2af | 2016-05-26 09:56:22 +0800 | [diff] [blame] | 1503 | if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400ES) |
| 1504 | err = mmc_select_hs400es(card); |
| 1505 | else if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200) |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1506 | err = mmc_select_hs200(card); |
| 1507 | else if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS) |
| 1508 | err = mmc_select_hs(card); |
| 1509 | |
| 1510 | if (err && err != -EBADMSG) |
| 1511 | return err; |
| 1512 | |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1513 | bus_speed: |
| 1514 | /* |
| 1515 | * Set the bus speed to the selected bus timing. |
| 1516 | * If timing is not selected, backward compatible is the default. |
| 1517 | */ |
| 1518 | mmc_set_bus_speed(card); |
Dong Aisheng | 0400ed0 | 2016-04-21 00:51:31 +0800 | [diff] [blame] | 1519 | return 0; |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | /* |
| 1523 | * Execute tuning sequence to seek the proper bus operating |
Seungwon Jeon | 0a5b643 | 2014-04-23 17:14:58 +0900 | [diff] [blame] | 1524 | * conditions for HS200 and HS400, which sends CMD21 to the device. |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1525 | */ |
| 1526 | static int mmc_hs200_tuning(struct mmc_card *card) |
| 1527 | { |
| 1528 | struct mmc_host *host = card->host; |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1529 | |
Seungwon Jeon | 0a5b643 | 2014-04-23 17:14:58 +0900 | [diff] [blame] | 1530 | /* |
| 1531 | * Timing should be adjusted to the HS400 target |
| 1532 | * operation frequency for tuning process |
| 1533 | */ |
| 1534 | if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 && |
| 1535 | host->ios.bus_width == MMC_BUS_WIDTH_8) |
| 1536 | if (host->ops->prepare_hs400_tuning) |
| 1537 | host->ops->prepare_hs400_tuning(host, &host->ios); |
| 1538 | |
Adrian Hunter | 63e415c | 2014-12-05 19:40:59 +0200 | [diff] [blame] | 1539 | return mmc_execute_tuning(card); |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1540 | } |
| 1541 | |
| 1542 | /* |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1543 | * Handle the detection and initialisation of a card. |
| 1544 | * |
Deepak Saxena | 8769392 | 2008-06-16 19:20:57 -0700 | [diff] [blame] | 1545 | * In the case of a resume, "oldcard" will contain the card |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1546 | * we're trying to reinitialise. |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1547 | */ |
Pierre Ossman | 8c75dea | 2007-05-19 16:14:43 +0200 | [diff] [blame] | 1548 | static int mmc_init_card(struct mmc_host *host, u32 ocr, |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1549 | struct mmc_card *oldcard) |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1550 | { |
| 1551 | struct mmc_card *card; |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1552 | int err; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1553 | u32 cid[4]; |
Philip Rakity | b676f03 | 2011-02-13 23:13:09 -0800 | [diff] [blame] | 1554 | u32 rocr; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1555 | |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 1556 | WARN_ON(!host->claimed); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1557 | |
Stefan Nilsson XK | 4466903 | 2011-09-15 17:50:38 +0200 | [diff] [blame] | 1558 | /* Set correct bus mode for MMC before attempting init */ |
| 1559 | if (!mmc_host_is_spi(host)) |
| 1560 | mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN); |
| 1561 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1562 | /* |
| 1563 | * Since we're changing the OCR value, we seem to |
| 1564 | * need to tell some cards to go back to the idle |
| 1565 | * state. We wait 1ms to give cards time to |
| 1566 | * respond. |
Balaji T K | c380546 | 2011-09-08 22:08:39 +0530 | [diff] [blame] | 1567 | * mmc_go_idle is needed for eMMC that are asleep |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1568 | */ |
| 1569 | mmc_go_idle(host); |
| 1570 | |
| 1571 | /* The extra bit indicates that we support high capacity */ |
Philip Rakity | b676f03 | 2011-02-13 23:13:09 -0800 | [diff] [blame] | 1572 | err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 1573 | if (err) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1574 | goto err; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1575 | |
| 1576 | /* |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 1577 | * For SPI, enable CRC as appropriate. |
| 1578 | */ |
| 1579 | if (mmc_host_is_spi(host)) { |
| 1580 | err = mmc_spi_set_crc(host, use_spi_crc); |
| 1581 | if (err) |
| 1582 | goto err; |
| 1583 | } |
| 1584 | |
| 1585 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1586 | * Fetch CID from card. |
| 1587 | */ |
Ulf Hansson | a147373 | 2017-06-08 15:27:43 +0200 | [diff] [blame] | 1588 | err = mmc_send_cid(host, cid); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 1589 | if (err) |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1590 | goto err; |
| 1591 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1592 | if (oldcard) { |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 1593 | if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) { |
| 1594 | err = -ENOENT; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1595 | goto err; |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 1596 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1597 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1598 | card = oldcard; |
| 1599 | } else { |
| 1600 | /* |
| 1601 | * Allocate card structure. |
| 1602 | */ |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 1603 | card = mmc_alloc_card(host, &mmc_type); |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 1604 | if (IS_ERR(card)) { |
| 1605 | err = PTR_ERR(card); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1606 | goto err; |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 1607 | } |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1608 | |
Ulf Hansson | 6904115 | 2013-09-13 11:31:33 +0200 | [diff] [blame] | 1609 | card->ocr = ocr; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1610 | card->type = MMC_TYPE_MMC; |
| 1611 | card->rca = 1; |
| 1612 | memcpy(card->raw_cid, cid, sizeof(card->raw_cid)); |
| 1613 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1614 | |
| 1615 | /* |
Doug Anderson | eac8632 | 2014-12-02 15:42:45 -0800 | [diff] [blame] | 1616 | * Call the optional HC's init_card function to handle quirks. |
| 1617 | */ |
| 1618 | if (host->ops->init_card) |
| 1619 | host->ops->init_card(host, card); |
| 1620 | |
| 1621 | /* |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 1622 | * For native busses: set card RCA and quit open drain mode. |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1623 | */ |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 1624 | if (!mmc_host_is_spi(host)) { |
| 1625 | err = mmc_set_relative_addr(card); |
| 1626 | if (err) |
| 1627 | goto free_card; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1628 | |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 1629 | mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL); |
| 1630 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1631 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1632 | if (!oldcard) { |
| 1633 | /* |
| 1634 | * Fetch CSD from card. |
| 1635 | */ |
| 1636 | err = mmc_send_csd(card, card->raw_csd); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 1637 | if (err) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1638 | goto free_card; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1639 | |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 1640 | err = mmc_decode_csd(card); |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 1641 | if (err) |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 1642 | goto free_card; |
| 1643 | err = mmc_decode_cid(card); |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 1644 | if (err) |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 1645 | goto free_card; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1646 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1647 | |
| 1648 | /* |
Sascha Hauer | 3d705d1 | 2014-08-19 10:45:51 +0200 | [diff] [blame] | 1649 | * handling only for cards supporting DSR and hosts requesting |
| 1650 | * DSR configuration |
| 1651 | */ |
| 1652 | if (card->csd.dsr_imp && host->dsr_req) |
| 1653 | mmc_set_dsr(host); |
| 1654 | |
| 1655 | /* |
Pierre Ossman | 89a73cf | 2007-05-01 15:08:30 +0200 | [diff] [blame] | 1656 | * Select card, as all following commands rely on that. |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1657 | */ |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 1658 | if (!mmc_host_is_spi(host)) { |
| 1659 | err = mmc_select_card(card); |
| 1660 | if (err) |
| 1661 | goto free_card; |
| 1662 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1663 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1664 | if (!oldcard) { |
Ulf Hansson | 076ec38 | 2014-10-20 13:37:24 +0200 | [diff] [blame] | 1665 | /* Read extended CSD. */ |
| 1666 | err = mmc_read_ext_csd(card); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 1667 | if (err) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1668 | goto free_card; |
Philip Rakity | b676f03 | 2011-02-13 23:13:09 -0800 | [diff] [blame] | 1669 | |
Masahiro Yamada | 87e8865 | 2016-04-15 20:16:12 +0900 | [diff] [blame] | 1670 | /* |
| 1671 | * If doing byte addressing, check if required to do sector |
Philip Rakity | b676f03 | 2011-02-13 23:13:09 -0800 | [diff] [blame] | 1672 | * addressing. Handle the case of <2GB cards needing sector |
| 1673 | * addressing. See section 8.1 JEDEC Standard JED84-A441; |
| 1674 | * ocr register has bit 30 set for sector addressing. |
| 1675 | */ |
Masahiro Yamada | 87e8865 | 2016-04-15 20:16:12 +0900 | [diff] [blame] | 1676 | if (rocr & BIT(30)) |
Philip Rakity | b676f03 | 2011-02-13 23:13:09 -0800 | [diff] [blame] | 1677 | mmc_card_set_blockaddr(card); |
| 1678 | |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1679 | /* Erase size depends on CSD and Extended CSD */ |
| 1680 | mmc_set_erase_size(card); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1681 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1682 | |
Ulf Hansson | d2a4717 | 2017-06-08 15:23:08 +0200 | [diff] [blame] | 1683 | /* Enable ERASE_GRP_DEF. This bit is lost after a reset or power off. */ |
| 1684 | if (card->ext_csd.rev >= 3) { |
Chuanxiao Dong | 709de99d | 2011-01-22 04:09:41 +0800 | [diff] [blame] | 1685 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
Seungwon Jeon | b23cf0b | 2011-09-23 14:15:29 +0900 | [diff] [blame] | 1686 | EXT_CSD_ERASE_GROUP_DEF, 1, |
| 1687 | card->ext_csd.generic_cmd6_time); |
Chuanxiao Dong | 709de99d | 2011-01-22 04:09:41 +0800 | [diff] [blame] | 1688 | |
| 1689 | if (err && err != -EBADMSG) |
| 1690 | goto free_card; |
| 1691 | |
| 1692 | if (err) { |
| 1693 | err = 0; |
| 1694 | /* |
| 1695 | * Just disable enhanced area off & sz |
| 1696 | * will try to enable ERASE_GROUP_DEF |
| 1697 | * during next time reinit |
| 1698 | */ |
| 1699 | card->ext_csd.enhanced_area_offset = -EINVAL; |
| 1700 | card->ext_csd.enhanced_area_size = -EINVAL; |
| 1701 | } else { |
| 1702 | card->ext_csd.erase_group_def = 1; |
| 1703 | /* |
| 1704 | * enable ERASE_GRP_DEF successfully. |
| 1705 | * This will affect the erase size, so |
| 1706 | * here need to reset erase size |
| 1707 | */ |
| 1708 | mmc_set_erase_size(card); |
| 1709 | } |
| 1710 | } |
| 1711 | |
| 1712 | /* |
Philip Rakity | 41e2a48 | 2011-03-19 14:10:33 -0400 | [diff] [blame] | 1713 | * Ensure eMMC user default partition is enabled |
| 1714 | */ |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1715 | if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) { |
| 1716 | card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK; |
| 1717 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG, |
| 1718 | card->ext_csd.part_config, |
| 1719 | card->ext_csd.part_time); |
| 1720 | if (err && err != -EBADMSG) |
| 1721 | goto free_card; |
Philip Rakity | 41e2a48 | 2011-03-19 14:10:33 -0400 | [diff] [blame] | 1722 | } |
| 1723 | |
| 1724 | /* |
Ulf Hansson | 43235679 | 2013-06-10 17:03:45 +0200 | [diff] [blame] | 1725 | * Enable power_off_notification byte in the ext_csd register |
Girish K S | bec8726 | 2011-10-13 12:04:16 +0530 | [diff] [blame] | 1726 | */ |
Ulf Hansson | 43235679 | 2013-06-10 17:03:45 +0200 | [diff] [blame] | 1727 | if (card->ext_csd.rev >= 6) { |
Girish K S | bec8726 | 2011-10-13 12:04:16 +0530 | [diff] [blame] | 1728 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 1729 | EXT_CSD_POWER_OFF_NOTIFICATION, |
| 1730 | EXT_CSD_POWER_ON, |
| 1731 | card->ext_csd.generic_cmd6_time); |
| 1732 | if (err && err != -EBADMSG) |
| 1733 | goto free_card; |
Girish K S | bec8726 | 2011-10-13 12:04:16 +0530 | [diff] [blame] | 1734 | |
Girish K S | 96a85d5 | 2011-11-04 16:22:47 +0530 | [diff] [blame] | 1735 | /* |
| 1736 | * The err can be -EBADMSG or 0, |
| 1737 | * so check for success and update the flag |
| 1738 | */ |
| 1739 | if (!err) |
Ulf Hansson | e6c0858 | 2012-10-05 12:45:39 -0400 | [diff] [blame] | 1740 | card->ext_csd.power_off_notification = EXT_CSD_POWER_ON; |
Girish K S | 96a85d5 | 2011-11-04 16:22:47 +0530 | [diff] [blame] | 1741 | } |
Girish K S | bec8726 | 2011-10-13 12:04:16 +0530 | [diff] [blame] | 1742 | |
| 1743 | /* |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1744 | * Select timing interface |
Pierre Ossman | 89a73cf | 2007-05-01 15:08:30 +0200 | [diff] [blame] | 1745 | */ |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1746 | err = mmc_select_timing(card); |
| 1747 | if (err) |
| 1748 | goto free_card; |
Girish K S | a4924c7 | 2012-01-11 14:04:52 -0500 | [diff] [blame] | 1749 | |
Girish K S | a4924c7 | 2012-01-11 14:04:52 -0500 | [diff] [blame] | 1750 | if (mmc_card_hs200(card)) { |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1751 | err = mmc_hs200_tuning(card); |
| 1752 | if (err) |
Andrew Gabbasov | 4b75bff | 2014-10-01 07:14:11 -0500 | [diff] [blame] | 1753 | goto free_card; |
Seungwon Jeon | 0a5b643 | 2014-04-23 17:14:58 +0900 | [diff] [blame] | 1754 | |
| 1755 | err = mmc_select_hs400(card); |
| 1756 | if (err) |
Andrew Gabbasov | 4b75bff | 2014-10-01 07:14:11 -0500 | [diff] [blame] | 1757 | goto free_card; |
Guenter Roeck | 773dc11 | 2017-03-01 14:11:47 -0800 | [diff] [blame] | 1758 | } else if (!mmc_card_hs400es(card)) { |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1759 | /* Select the desired bus width optionally */ |
| 1760 | err = mmc_select_bus_width(card); |
Anssi Hannula | 3d4ef32 | 2017-02-13 13:46:41 +0200 | [diff] [blame] | 1761 | if (err > 0 && mmc_card_hs(card)) { |
Seungwon Jeon | 577fb13 | 2014-04-23 17:08:44 +0900 | [diff] [blame] | 1762 | err = mmc_select_hs_ddr(card); |
| 1763 | if (err) |
Andrew Gabbasov | 4b75bff | 2014-10-01 07:14:11 -0500 | [diff] [blame] | 1764 | goto free_card; |
Adrian Hunter | ef0b27d | 2009-09-22 16:44:37 -0700 | [diff] [blame] | 1765 | } |
Pierre Ossman | 89a73cf | 2007-05-01 15:08:30 +0200 | [diff] [blame] | 1766 | } |
| 1767 | |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 1768 | /* |
Seungwon Jeon | 2385049 | 2014-04-23 17:08:05 +0900 | [diff] [blame] | 1769 | * Choose the power class with selected bus interface |
| 1770 | */ |
| 1771 | mmc_select_powerclass(card); |
| 1772 | |
| 1773 | /* |
Subhash Jadavani | 52d0974 | 2012-03-06 17:59:12 +0530 | [diff] [blame] | 1774 | * Enable HPI feature (if supported) |
| 1775 | */ |
| 1776 | if (card->ext_csd.hpi) { |
| 1777 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 1778 | EXT_CSD_HPI_MGMT, 1, |
| 1779 | card->ext_csd.generic_cmd6_time); |
| 1780 | if (err && err != -EBADMSG) |
| 1781 | goto free_card; |
| 1782 | if (err) { |
Joe Perches | 6606110 | 2014-09-12 14:56:56 -0700 | [diff] [blame] | 1783 | pr_warn("%s: Enabling HPI failed\n", |
| 1784 | mmc_hostname(card->host)); |
Ulf Hansson | a0741ba | 2018-12-10 17:52:36 +0100 | [diff] [blame] | 1785 | card->ext_csd.hpi_en = 0; |
Subhash Jadavani | 52d0974 | 2012-03-06 17:59:12 +0530 | [diff] [blame] | 1786 | err = 0; |
Ulf Hansson | a0741ba | 2018-12-10 17:52:36 +0100 | [diff] [blame] | 1787 | } else { |
Subhash Jadavani | 52d0974 | 2012-03-06 17:59:12 +0530 | [diff] [blame] | 1788 | card->ext_csd.hpi_en = 1; |
Ulf Hansson | a0741ba | 2018-12-10 17:52:36 +0100 | [diff] [blame] | 1789 | } |
Subhash Jadavani | 52d0974 | 2012-03-06 17:59:12 +0530 | [diff] [blame] | 1790 | } |
| 1791 | |
| 1792 | /* |
Ulf Hansson | e3ae340 | 2018-12-10 17:52:38 +0100 | [diff] [blame] | 1793 | * If cache size is higher than 0, this indicates the existence of cache |
| 1794 | * and it can be turned on. Note that some eMMCs from Micron has been |
| 1795 | * reported to need ~800 ms timeout, while enabling the cache after |
| 1796 | * sudden power failure tests. Let's extend the timeout to a minimum of |
| 1797 | * DEFAULT_CACHE_EN_TIMEOUT_MS and do it for all cards. |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 1798 | */ |
Ulf Hansson | ba9f39a | 2018-12-10 17:52:37 +0100 | [diff] [blame] | 1799 | if (card->ext_csd.cache_size > 0) { |
Ulf Hansson | e3ae340 | 2018-12-10 17:52:38 +0100 | [diff] [blame] | 1800 | unsigned int timeout_ms = MIN_CACHE_EN_TIMEOUT_MS; |
| 1801 | |
| 1802 | timeout_ms = max(card->ext_csd.generic_cmd6_time, timeout_ms); |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 1803 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
Ulf Hansson | e3ae340 | 2018-12-10 17:52:38 +0100 | [diff] [blame] | 1804 | EXT_CSD_CACHE_CTRL, 1, timeout_ms); |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 1805 | if (err && err != -EBADMSG) |
| 1806 | goto free_card; |
| 1807 | |
| 1808 | /* |
| 1809 | * Only if no error, cache is turned on successfully. |
| 1810 | */ |
Seungwon Jeon | 8bc0678 | 2011-12-09 17:47:17 +0900 | [diff] [blame] | 1811 | if (err) { |
Joe Perches | 6606110 | 2014-09-12 14:56:56 -0700 | [diff] [blame] | 1812 | pr_warn("%s: Cache is supported, but failed to turn on (%d)\n", |
| 1813 | mmc_hostname(card->host), err); |
Seungwon Jeon | 8bc0678 | 2011-12-09 17:47:17 +0900 | [diff] [blame] | 1814 | card->ext_csd.cache_ctrl = 0; |
| 1815 | err = 0; |
| 1816 | } else { |
| 1817 | card->ext_csd.cache_ctrl = 1; |
| 1818 | } |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 1819 | } |
| 1820 | |
Seungwon Jeon | abd9ac1 | 2013-02-06 17:01:43 +0900 | [diff] [blame] | 1821 | /* |
Adrian Hunter | 98d4f78 | 2017-09-22 15:36:53 +0300 | [diff] [blame] | 1822 | * Enable Command Queue if supported. Note that Packed Commands cannot |
| 1823 | * be used with Command Queue. |
| 1824 | */ |
| 1825 | card->ext_csd.cmdq_en = false; |
| 1826 | if (card->ext_csd.cmdq_support && host->caps2 & MMC_CAP2_CQE) { |
| 1827 | err = mmc_cmdq_enable(card); |
| 1828 | if (err && err != -EBADMSG) |
| 1829 | goto free_card; |
| 1830 | if (err) { |
| 1831 | pr_warn("%s: Enabling CMDQ failed\n", |
| 1832 | mmc_hostname(card->host)); |
| 1833 | card->ext_csd.cmdq_support = false; |
| 1834 | card->ext_csd.cmdq_depth = 0; |
| 1835 | err = 0; |
| 1836 | } |
| 1837 | } |
| 1838 | /* |
Adrian Hunter | 9d4579a | 2017-03-13 14:36:38 +0200 | [diff] [blame] | 1839 | * In some cases (e.g. RPMB or mmc_test), the Command Queue must be |
| 1840 | * disabled for a time, so a flag is needed to indicate to re-enable the |
| 1841 | * Command Queue. |
| 1842 | */ |
| 1843 | card->reenable_cmdq = card->ext_csd.cmdq_en; |
| 1844 | |
Adrian Hunter | f690f44 | 2017-09-22 15:36:54 +0300 | [diff] [blame] | 1845 | if (card->ext_csd.cmdq_en && !host->cqe_enabled) { |
| 1846 | err = host->cqe_ops->cqe_enable(host, card); |
| 1847 | if (err) { |
| 1848 | pr_err("%s: Failed to enable CQE, error %d\n", |
| 1849 | mmc_hostname(host), err); |
| 1850 | } else { |
| 1851 | host->cqe_enabled = true; |
| 1852 | pr_info("%s: Command Queue Engine enabled\n", |
| 1853 | mmc_hostname(host)); |
| 1854 | } |
| 1855 | } |
| 1856 | |
Kyle Roeschley | 247cfe5 | 2018-04-13 16:54:57 -0500 | [diff] [blame] | 1857 | if (host->caps2 & MMC_CAP2_AVOID_3_3V && |
| 1858 | host->ios.signal_voltage == MMC_SIGNAL_VOLTAGE_330) { |
| 1859 | pr_err("%s: Host failed to negotiate down from 3.3V\n", |
| 1860 | mmc_hostname(host)); |
| 1861 | err = -EINVAL; |
| 1862 | goto free_card; |
| 1863 | } |
| 1864 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1865 | if (!oldcard) |
| 1866 | host->card = card; |
| 1867 | |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 1868 | return 0; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1869 | |
| 1870 | free_card: |
| 1871 | if (!oldcard) |
| 1872 | mmc_remove_card(card); |
| 1873 | err: |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 1874 | return err; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1875 | } |
| 1876 | |
Ulf Hansson | 07a6821 | 2013-04-19 15:12:11 +0200 | [diff] [blame] | 1877 | static int mmc_can_sleep(struct mmc_card *card) |
| 1878 | { |
| 1879 | return (card && card->ext_csd.rev >= 3); |
| 1880 | } |
| 1881 | |
| 1882 | static int mmc_sleep(struct mmc_host *host) |
| 1883 | { |
Masahiro Yamada | c7836d1 | 2016-12-19 20:51:18 +0900 | [diff] [blame] | 1884 | struct mmc_command cmd = {}; |
Ulf Hansson | 07a6821 | 2013-04-19 15:12:11 +0200 | [diff] [blame] | 1885 | struct mmc_card *card = host->card; |
Ulf Hansson | cb962e0 | 2014-01-14 23:17:36 +0100 | [diff] [blame] | 1886 | unsigned int timeout_ms = DIV_ROUND_UP(card->ext_csd.sa_timeout, 10000); |
Ulf Hansson | 07a6821 | 2013-04-19 15:12:11 +0200 | [diff] [blame] | 1887 | int err; |
| 1888 | |
Adrian Hunter | 436f8da | 2015-05-07 13:10:18 +0300 | [diff] [blame] | 1889 | /* Re-tuning can't be done once the card is deselected */ |
| 1890 | mmc_retune_hold(host); |
| 1891 | |
Ulf Hansson | 07a6821 | 2013-04-19 15:12:11 +0200 | [diff] [blame] | 1892 | err = mmc_deselect_cards(host); |
| 1893 | if (err) |
Adrian Hunter | 436f8da | 2015-05-07 13:10:18 +0300 | [diff] [blame] | 1894 | goto out_release; |
Ulf Hansson | 07a6821 | 2013-04-19 15:12:11 +0200 | [diff] [blame] | 1895 | |
| 1896 | cmd.opcode = MMC_SLEEP_AWAKE; |
| 1897 | cmd.arg = card->rca << 16; |
| 1898 | cmd.arg |= 1 << 15; |
| 1899 | |
Ulf Hansson | cb962e0 | 2014-01-14 23:17:36 +0100 | [diff] [blame] | 1900 | /* |
| 1901 | * If the max_busy_timeout of the host is specified, validate it against |
| 1902 | * the sleep cmd timeout. A failure means we need to prevent the host |
| 1903 | * from doing hw busy detection, which is done by converting to a R1 |
| 1904 | * response instead of a R1B. |
| 1905 | */ |
| 1906 | if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout)) { |
| 1907 | cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; |
| 1908 | } else { |
| 1909 | cmd.flags = MMC_RSP_R1B | MMC_CMD_AC; |
| 1910 | cmd.busy_timeout = timeout_ms; |
| 1911 | } |
| 1912 | |
Ulf Hansson | 07a6821 | 2013-04-19 15:12:11 +0200 | [diff] [blame] | 1913 | err = mmc_wait_for_cmd(host, &cmd, 0); |
| 1914 | if (err) |
Adrian Hunter | 436f8da | 2015-05-07 13:10:18 +0300 | [diff] [blame] | 1915 | goto out_release; |
Ulf Hansson | 07a6821 | 2013-04-19 15:12:11 +0200 | [diff] [blame] | 1916 | |
| 1917 | /* |
| 1918 | * If the host does not wait while the card signals busy, then we will |
| 1919 | * will have to wait the sleep/awake timeout. Note, we cannot use the |
| 1920 | * SEND_STATUS command to poll the status because that command (and most |
| 1921 | * others) is invalid while the card sleeps. |
| 1922 | */ |
Ulf Hansson | cb962e0 | 2014-01-14 23:17:36 +0100 | [diff] [blame] | 1923 | if (!cmd.busy_timeout || !(host->caps & MMC_CAP_WAIT_WHILE_BUSY)) |
| 1924 | mmc_delay(timeout_ms); |
Ulf Hansson | 07a6821 | 2013-04-19 15:12:11 +0200 | [diff] [blame] | 1925 | |
Adrian Hunter | 436f8da | 2015-05-07 13:10:18 +0300 | [diff] [blame] | 1926 | out_release: |
| 1927 | mmc_retune_release(host); |
Ulf Hansson | 07a6821 | 2013-04-19 15:12:11 +0200 | [diff] [blame] | 1928 | return err; |
| 1929 | } |
| 1930 | |
Ulf Hansson | e6c0858 | 2012-10-05 12:45:39 -0400 | [diff] [blame] | 1931 | static int mmc_can_poweroff_notify(const struct mmc_card *card) |
| 1932 | { |
| 1933 | return card && |
| 1934 | mmc_card_mmc(card) && |
| 1935 | (card->ext_csd.power_off_notification == EXT_CSD_POWER_ON); |
| 1936 | } |
| 1937 | |
| 1938 | static int mmc_poweroff_notify(struct mmc_card *card, unsigned int notify_type) |
| 1939 | { |
| 1940 | unsigned int timeout = card->ext_csd.generic_cmd6_time; |
| 1941 | int err; |
| 1942 | |
| 1943 | /* Use EXT_CSD_POWER_OFF_SHORT as default notification type. */ |
| 1944 | if (notify_type == EXT_CSD_POWER_OFF_LONG) |
| 1945 | timeout = card->ext_csd.power_off_longtime; |
| 1946 | |
Ulf Hansson | 878e200 | 2013-09-09 11:57:57 +0200 | [diff] [blame] | 1947 | err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 1948 | EXT_CSD_POWER_OFF_NOTIFICATION, |
Ulf Hansson | aa33ce3 | 2016-11-09 17:33:36 +0100 | [diff] [blame] | 1949 | notify_type, timeout, 0, true, false, false); |
Ulf Hansson | e6c0858 | 2012-10-05 12:45:39 -0400 | [diff] [blame] | 1950 | if (err) |
| 1951 | pr_err("%s: Power Off Notification timed out, %u\n", |
| 1952 | mmc_hostname(card->host), timeout); |
| 1953 | |
| 1954 | /* Disable the power off notification after the switch operation. */ |
| 1955 | card->ext_csd.power_off_notification = EXT_CSD_NO_POWER_NOTIFICATION; |
| 1956 | |
| 1957 | return err; |
| 1958 | } |
| 1959 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1960 | /* |
| 1961 | * Host is being removed. Free up the current card. |
| 1962 | */ |
| 1963 | static void mmc_remove(struct mmc_host *host) |
| 1964 | { |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1965 | mmc_remove_card(host->card); |
| 1966 | host->card = NULL; |
| 1967 | } |
| 1968 | |
| 1969 | /* |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 1970 | * Card detection - card is alive. |
| 1971 | */ |
| 1972 | static int mmc_alive(struct mmc_host *host) |
| 1973 | { |
| 1974 | return mmc_send_status(host->card, NULL); |
| 1975 | } |
| 1976 | |
| 1977 | /* |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1978 | * Card detection callback from host. |
| 1979 | */ |
| 1980 | static void mmc_detect(struct mmc_host *host) |
| 1981 | { |
| 1982 | int err; |
| 1983 | |
Adrian Hunter | 6c0cedd | 2017-09-22 15:36:51 +0300 | [diff] [blame] | 1984 | mmc_get_card(host->card, NULL); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1985 | |
| 1986 | /* |
| 1987 | * Just check if our card has been removed. |
| 1988 | */ |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 1989 | err = _mmc_detect_card_removed(host); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1990 | |
Adrian Hunter | 6c0cedd | 2017-09-22 15:36:51 +0300 | [diff] [blame] | 1991 | mmc_put_card(host->card, NULL); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1992 | |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 1993 | if (err) { |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 1994 | mmc_remove(host); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1995 | |
| 1996 | mmc_claim_host(host); |
| 1997 | mmc_detach_bus(host); |
Ulf Hansson | 7f7e412 | 2011-09-21 14:08:13 -0400 | [diff] [blame] | 1998 | mmc_power_off(host); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1999 | mmc_release_host(host); |
| 2000 | } |
| 2001 | } |
| 2002 | |
Ulf Hansson | 03d071f | 2013-06-10 17:03:43 +0200 | [diff] [blame] | 2003 | static int _mmc_suspend(struct mmc_host *host, bool is_suspend) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 2004 | { |
Balaji T K | c380546 | 2011-09-08 22:08:39 +0530 | [diff] [blame] | 2005 | int err = 0; |
Ulf Hansson | 03d071f | 2013-06-10 17:03:43 +0200 | [diff] [blame] | 2006 | unsigned int notify_type = is_suspend ? EXT_CSD_POWER_OFF_SHORT : |
| 2007 | EXT_CSD_POWER_OFF_LONG; |
Balaji T K | c380546 | 2011-09-08 22:08:39 +0530 | [diff] [blame] | 2008 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 2009 | mmc_claim_host(host); |
Maya Erez | 881d926 | 2013-01-28 16:44:22 -0500 | [diff] [blame] | 2010 | |
Ulf Hansson | 9ec775f | 2013-10-02 17:37:09 +0200 | [diff] [blame] | 2011 | if (mmc_card_suspended(host->card)) |
| 2012 | goto out; |
| 2013 | |
Ulf Hansson | 39b9431 | 2013-05-02 14:02:36 +0200 | [diff] [blame] | 2014 | if (mmc_card_doing_bkops(host->card)) { |
| 2015 | err = mmc_stop_bkops(host->card); |
| 2016 | if (err) |
| 2017 | goto out; |
| 2018 | } |
| 2019 | |
Ulf Hansson | 10e5d96 | 2013-12-16 16:23:22 +0100 | [diff] [blame] | 2020 | err = mmc_flush_cache(host->card); |
Maya Erez | 881d926 | 2013-01-28 16:44:22 -0500 | [diff] [blame] | 2021 | if (err) |
| 2022 | goto out; |
| 2023 | |
Ulf Hansson | 43235679 | 2013-06-10 17:03:45 +0200 | [diff] [blame] | 2024 | if (mmc_can_poweroff_notify(host->card) && |
Ulf Hansson | 53275c2 | 2013-06-10 17:03:46 +0200 | [diff] [blame] | 2025 | ((host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) || !is_suspend)) |
Ulf Hansson | 03d071f | 2013-06-10 17:03:43 +0200 | [diff] [blame] | 2026 | err = mmc_poweroff_notify(host->card, notify_type); |
Ulf Hansson | 07a6821 | 2013-04-19 15:12:11 +0200 | [diff] [blame] | 2027 | else if (mmc_can_sleep(host->card)) |
| 2028 | err = mmc_sleep(host); |
Ulf Hansson | e6c0858 | 2012-10-05 12:45:39 -0400 | [diff] [blame] | 2029 | else if (!mmc_host_is_spi(host)) |
Jaehoon Chung | 85e727e | 2012-05-31 20:31:47 +0900 | [diff] [blame] | 2030 | err = mmc_deselect_cards(host); |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 2031 | |
Ulf Hansson | 9ec775f | 2013-10-02 17:37:09 +0200 | [diff] [blame] | 2032 | if (!err) { |
Ulf Hansson | 7459026 | 2013-06-10 17:03:38 +0200 | [diff] [blame] | 2033 | mmc_power_off(host); |
Ulf Hansson | 9ec775f | 2013-10-02 17:37:09 +0200 | [diff] [blame] | 2034 | mmc_card_set_suspended(host->card); |
| 2035 | } |
Maya Erez | 881d926 | 2013-01-28 16:44:22 -0500 | [diff] [blame] | 2036 | out: |
| 2037 | mmc_release_host(host); |
Balaji T K | c380546 | 2011-09-08 22:08:39 +0530 | [diff] [blame] | 2038 | return err; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 2039 | } |
| 2040 | |
| 2041 | /* |
Ulf Hansson | 0cb403a | 2013-10-10 14:20:05 +0200 | [diff] [blame] | 2042 | * Suspend callback |
Ulf Hansson | 03d071f | 2013-06-10 17:03:43 +0200 | [diff] [blame] | 2043 | */ |
| 2044 | static int mmc_suspend(struct mmc_host *host) |
| 2045 | { |
Ulf Hansson | 0cb403a | 2013-10-10 14:20:05 +0200 | [diff] [blame] | 2046 | int err; |
| 2047 | |
| 2048 | err = _mmc_suspend(host, true); |
| 2049 | if (!err) { |
| 2050 | pm_runtime_disable(&host->card->dev); |
| 2051 | pm_runtime_set_suspended(&host->card->dev); |
| 2052 | } |
| 2053 | |
| 2054 | return err; |
Ulf Hansson | 03d071f | 2013-06-10 17:03:43 +0200 | [diff] [blame] | 2055 | } |
| 2056 | |
| 2057 | /* |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 2058 | * This function tries to determine if the same card is still present |
| 2059 | * and, if so, restore all state to it. |
| 2060 | */ |
Ulf Hansson | 0cb403a | 2013-10-10 14:20:05 +0200 | [diff] [blame] | 2061 | static int _mmc_resume(struct mmc_host *host) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 2062 | { |
Ulf Hansson | 9ec775f | 2013-10-02 17:37:09 +0200 | [diff] [blame] | 2063 | int err = 0; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 2064 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 2065 | mmc_claim_host(host); |
Ulf Hansson | 9ec775f | 2013-10-02 17:37:09 +0200 | [diff] [blame] | 2066 | |
| 2067 | if (!mmc_card_suspended(host->card)) |
| 2068 | goto out; |
| 2069 | |
Ulf Hansson | 6904115 | 2013-09-13 11:31:33 +0200 | [diff] [blame] | 2070 | mmc_power_up(host, host->card->ocr); |
Ulf Hansson | 6904115 | 2013-09-13 11:31:33 +0200 | [diff] [blame] | 2071 | err = mmc_init_card(host, host->card->ocr, host->card); |
Ulf Hansson | 9ec775f | 2013-10-02 17:37:09 +0200 | [diff] [blame] | 2072 | mmc_card_clr_suspended(host->card); |
Pierre Ossman | 2986d0b | 2007-07-22 17:52:06 +0200 | [diff] [blame] | 2073 | |
Ulf Hansson | 9ec775f | 2013-10-02 17:37:09 +0200 | [diff] [blame] | 2074 | out: |
| 2075 | mmc_release_host(host); |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 2076 | return err; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 2077 | } |
| 2078 | |
Ulf Hansson | 9ec775f | 2013-10-02 17:37:09 +0200 | [diff] [blame] | 2079 | /* |
| 2080 | * Shutdown callback |
| 2081 | */ |
| 2082 | static int mmc_shutdown(struct mmc_host *host) |
| 2083 | { |
| 2084 | int err = 0; |
| 2085 | |
| 2086 | /* |
| 2087 | * In a specific case for poweroff notify, we need to resume the card |
| 2088 | * before we can shutdown it properly. |
| 2089 | */ |
| 2090 | if (mmc_can_poweroff_notify(host->card) && |
| 2091 | !(host->caps2 & MMC_CAP2_FULL_PWR_CYCLE)) |
Ulf Hansson | 0cb403a | 2013-10-10 14:20:05 +0200 | [diff] [blame] | 2092 | err = _mmc_resume(host); |
Ulf Hansson | 9ec775f | 2013-10-02 17:37:09 +0200 | [diff] [blame] | 2093 | |
| 2094 | if (!err) |
| 2095 | err = _mmc_suspend(host, false); |
| 2096 | |
| 2097 | return err; |
| 2098 | } |
Ulf Hansson | c4d770d | 2013-05-02 14:02:39 +0200 | [diff] [blame] | 2099 | |
| 2100 | /* |
Ulf Hansson | 0cb403a | 2013-10-10 14:20:05 +0200 | [diff] [blame] | 2101 | * Callback for resume. |
| 2102 | */ |
| 2103 | static int mmc_resume(struct mmc_host *host) |
| 2104 | { |
Ulf Hansson | 0cb403a | 2013-10-10 14:20:05 +0200 | [diff] [blame] | 2105 | pm_runtime_enable(&host->card->dev); |
Ulf Hansson | c29536e | 2015-11-05 16:01:32 +0100 | [diff] [blame] | 2106 | return 0; |
Ulf Hansson | 0cb403a | 2013-10-10 14:20:05 +0200 | [diff] [blame] | 2107 | } |
| 2108 | |
| 2109 | /* |
Ulf Hansson | c4d770d | 2013-05-02 14:02:39 +0200 | [diff] [blame] | 2110 | * Callback for runtime_suspend. |
| 2111 | */ |
| 2112 | static int mmc_runtime_suspend(struct mmc_host *host) |
| 2113 | { |
| 2114 | int err; |
| 2115 | |
| 2116 | if (!(host->caps & MMC_CAP_AGGRESSIVE_PM)) |
| 2117 | return 0; |
| 2118 | |
Ulf Hansson | 0cb403a | 2013-10-10 14:20:05 +0200 | [diff] [blame] | 2119 | err = _mmc_suspend(host, true); |
Ulf Hansson | 0cc81a8 | 2013-10-03 11:24:44 +0200 | [diff] [blame] | 2120 | if (err) |
Masanari Iida | f42cf8d | 2015-02-24 23:11:26 +0900 | [diff] [blame] | 2121 | pr_err("%s: error %d doing aggressive suspend\n", |
Ulf Hansson | c4d770d | 2013-05-02 14:02:39 +0200 | [diff] [blame] | 2122 | mmc_hostname(host), err); |
Ulf Hansson | c4d770d | 2013-05-02 14:02:39 +0200 | [diff] [blame] | 2123 | |
Ulf Hansson | c4d770d | 2013-05-02 14:02:39 +0200 | [diff] [blame] | 2124 | return err; |
| 2125 | } |
| 2126 | |
| 2127 | /* |
| 2128 | * Callback for runtime_resume. |
| 2129 | */ |
| 2130 | static int mmc_runtime_resume(struct mmc_host *host) |
| 2131 | { |
| 2132 | int err; |
| 2133 | |
Ulf Hansson | 0cb403a | 2013-10-10 14:20:05 +0200 | [diff] [blame] | 2134 | err = _mmc_resume(host); |
Adrian Hunter | 520322d | 2015-12-14 15:51:27 +0200 | [diff] [blame] | 2135 | if (err && err != -ENOMEDIUM) |
Ulf Hansson | c29536e | 2015-11-05 16:01:32 +0100 | [diff] [blame] | 2136 | pr_err("%s: error %d doing runtime resume\n", |
Ulf Hansson | c4d770d | 2013-05-02 14:02:39 +0200 | [diff] [blame] | 2137 | mmc_hostname(host), err); |
| 2138 | |
Ulf Hansson | c4d770d | 2013-05-02 14:02:39 +0200 | [diff] [blame] | 2139 | return 0; |
| 2140 | } |
| 2141 | |
Ulf Hansson | 88bed3a | 2017-06-08 15:27:41 +0200 | [diff] [blame] | 2142 | static int mmc_can_reset(struct mmc_card *card) |
Johan Rudholm | f855a37 | 2015-01-12 15:38:05 +0100 | [diff] [blame] | 2143 | { |
| 2144 | u8 rst_n_function; |
| 2145 | |
| 2146 | rst_n_function = card->ext_csd.rst_n_function; |
| 2147 | if ((rst_n_function & EXT_CSD_RST_N_EN_MASK) != EXT_CSD_RST_N_ENABLED) |
| 2148 | return 0; |
| 2149 | return 1; |
| 2150 | } |
Johan Rudholm | f855a37 | 2015-01-12 15:38:05 +0100 | [diff] [blame] | 2151 | |
Ulf Hansson | 3a3db60 | 2018-04-05 13:24:43 +0200 | [diff] [blame] | 2152 | static int _mmc_hw_reset(struct mmc_host *host) |
Johan Rudholm | f855a37 | 2015-01-12 15:38:05 +0100 | [diff] [blame] | 2153 | { |
| 2154 | struct mmc_card *card = host->card; |
Johan Rudholm | f855a37 | 2015-01-12 15:38:05 +0100 | [diff] [blame] | 2155 | |
Adrian Hunter | 437db4c | 2016-05-04 10:38:21 +0300 | [diff] [blame] | 2156 | /* |
| 2157 | * In the case of recovery, we can't expect flushing the cache to work |
| 2158 | * always, but we have a go and ignore errors. |
| 2159 | */ |
| 2160 | mmc_flush_cache(host->card); |
| 2161 | |
Gwendal Grignou | 4e6c717 | 2016-04-01 16:04:22 -0700 | [diff] [blame] | 2162 | if ((host->caps & MMC_CAP_HW_RESET) && host->ops->hw_reset && |
| 2163 | mmc_can_reset(card)) { |
| 2164 | /* If the card accept RST_n signal, send it. */ |
| 2165 | mmc_set_clock(host, host->f_init); |
| 2166 | host->ops->hw_reset(host); |
| 2167 | /* Set initial state and call mmc_set_ios */ |
| 2168 | mmc_set_initial_state(host); |
| 2169 | } else { |
| 2170 | /* Do a brute force power cycle */ |
| 2171 | mmc_power_cycle(host, card->ocr); |
Ulf Hansson | 52c8212 | 2017-05-08 23:52:04 +0200 | [diff] [blame] | 2172 | mmc_pwrseq_reset(host); |
Gwendal Grignou | 4e6c717 | 2016-04-01 16:04:22 -0700 | [diff] [blame] | 2173 | } |
Ulf Hansson | 364549d | 2015-06-01 11:14:57 +0200 | [diff] [blame] | 2174 | return mmc_init_card(host, card->ocr, card); |
Johan Rudholm | f855a37 | 2015-01-12 15:38:05 +0100 | [diff] [blame] | 2175 | } |
| 2176 | |
Adrian Hunter | 9feae24 | 2009-09-22 16:44:32 -0700 | [diff] [blame] | 2177 | static const struct mmc_bus_ops mmc_ops = { |
| 2178 | .remove = mmc_remove, |
| 2179 | .detect = mmc_detect, |
Adrian Hunter | 9feae24 | 2009-09-22 16:44:32 -0700 | [diff] [blame] | 2180 | .suspend = mmc_suspend, |
| 2181 | .resume = mmc_resume, |
Ulf Hansson | c4d770d | 2013-05-02 14:02:39 +0200 | [diff] [blame] | 2182 | .runtime_suspend = mmc_runtime_suspend, |
| 2183 | .runtime_resume = mmc_runtime_resume, |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 2184 | .alive = mmc_alive, |
Ulf Hansson | 486fdbb | 2013-06-10 17:03:44 +0200 | [diff] [blame] | 2185 | .shutdown = mmc_shutdown, |
Ulf Hansson | 3a3db60 | 2018-04-05 13:24:43 +0200 | [diff] [blame] | 2186 | .hw_reset = _mmc_hw_reset, |
Adrian Hunter | 9feae24 | 2009-09-22 16:44:32 -0700 | [diff] [blame] | 2187 | }; |
| 2188 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 2189 | /* |
| 2190 | * Starting point for MMC card init. |
| 2191 | */ |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 2192 | int mmc_attach_mmc(struct mmc_host *host) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 2193 | { |
| 2194 | int err; |
Ulf Hansson | 6904115 | 2013-09-13 11:31:33 +0200 | [diff] [blame] | 2195 | u32 ocr, rocr; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 2196 | |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 2197 | WARN_ON(!host->claimed); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 2198 | |
Stefan Nilsson XK | 4466903 | 2011-09-15 17:50:38 +0200 | [diff] [blame] | 2199 | /* Set correct bus mode for MMC before attempting attach */ |
| 2200 | if (!mmc_host_is_spi(host)) |
| 2201 | mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN); |
| 2202 | |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 2203 | err = mmc_send_op_cond(host, 0, &ocr); |
| 2204 | if (err) |
| 2205 | return err; |
| 2206 | |
Ulf Hansson | 2501c91 | 2013-10-30 01:00:18 +0100 | [diff] [blame] | 2207 | mmc_attach_bus(host, &mmc_ops); |
Takashi Iwai | 8f230f4 | 2010-12-08 10:04:30 +0100 | [diff] [blame] | 2208 | if (host->ocr_avail_mmc) |
| 2209 | host->ocr_avail = host->ocr_avail_mmc; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 2210 | |
| 2211 | /* |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 2212 | * We need to get OCR a different way for SPI. |
| 2213 | */ |
| 2214 | if (mmc_host_is_spi(host)) { |
| 2215 | err = mmc_spi_read_ocr(host, 1, &ocr); |
| 2216 | if (err) |
| 2217 | goto err; |
| 2218 | } |
| 2219 | |
Ulf Hansson | 6904115 | 2013-09-13 11:31:33 +0200 | [diff] [blame] | 2220 | rocr = mmc_select_voltage(host, ocr); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 2221 | |
| 2222 | /* |
| 2223 | * Can we support the voltage of the card? |
| 2224 | */ |
Ulf Hansson | 6904115 | 2013-09-13 11:31:33 +0200 | [diff] [blame] | 2225 | if (!rocr) { |
Pierre Ossman | 109b5be | 2007-07-23 00:12:10 +0200 | [diff] [blame] | 2226 | err = -EINVAL; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 2227 | goto err; |
Pierre Ossman | 109b5be | 2007-07-23 00:12:10 +0200 | [diff] [blame] | 2228 | } |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 2229 | |
| 2230 | /* |
| 2231 | * Detect and init the card. |
| 2232 | */ |
Ulf Hansson | 6904115 | 2013-09-13 11:31:33 +0200 | [diff] [blame] | 2233 | err = mmc_init_card(host, rocr, NULL); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 2234 | if (err) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 2235 | goto err; |
| 2236 | |
| 2237 | mmc_release_host(host); |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 2238 | err = mmc_add_card(host->card); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 2239 | if (err) |
Pierre Ossman | 2986d0b | 2007-07-22 17:52:06 +0200 | [diff] [blame] | 2240 | goto remove_card; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 2241 | |
Sergei Shtylyov | 2860d06 | 2015-10-14 23:53:03 +0300 | [diff] [blame] | 2242 | mmc_claim_host(host); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 2243 | return 0; |
| 2244 | |
Pierre Ossman | 2986d0b | 2007-07-22 17:52:06 +0200 | [diff] [blame] | 2245 | remove_card: |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 2246 | mmc_remove_card(host->card); |
Pierre Ossman | 2986d0b | 2007-07-22 17:52:06 +0200 | [diff] [blame] | 2247 | mmc_claim_host(host); |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 2248 | host->card = NULL; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 2249 | err: |
| 2250 | mmc_detach_bus(host); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 2251 | |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 2252 | pr_err("%s: error %d whilst initialising MMC card\n", |
Pierre Ossman | 109b5be | 2007-07-23 00:12:10 +0200 | [diff] [blame] | 2253 | mmc_hostname(host), err); |
| 2254 | |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 2255 | return err; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 2256 | } |