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