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