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