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