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> |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 16 | |
| 17 | #include <linux/mmc/host.h> |
| 18 | #include <linux/mmc/card.h> |
| 19 | #include <linux/mmc/mmc.h> |
Asutosh Das | bbc8478 | 2013-02-11 15:31:35 +0530 | [diff] [blame] | 20 | #include <linux/pm_runtime.h> |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 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); |
| 100 | card->cid.serial = UNSTUFF_BITS(resp, 16, 32); |
| 101 | card->cid.month = UNSTUFF_BITS(resp, 12, 4); |
| 102 | card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997; |
| 103 | break; |
| 104 | |
| 105 | default: |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 106 | pr_err("%s: card has unknown MMCA version %d\n", |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 107 | mmc_hostname(card->host), card->csd.mmca_vsn); |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 108 | return -EINVAL; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 109 | } |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 110 | |
| 111 | return 0; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 114 | static void mmc_set_erase_size(struct mmc_card *card) |
| 115 | { |
| 116 | if (card->ext_csd.erase_group_def & 1) |
| 117 | card->erase_size = card->ext_csd.hc_erase_size; |
| 118 | else |
| 119 | card->erase_size = card->csd.erase_size; |
| 120 | |
| 121 | mmc_init_erase(card); |
| 122 | } |
| 123 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 124 | /* |
| 125 | * Given a 128-bit response, decode to our card CSD structure. |
| 126 | */ |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 127 | static int mmc_decode_csd(struct mmc_card *card) |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 128 | { |
| 129 | struct mmc_csd *csd = &card->csd; |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 130 | unsigned int e, m, a, b; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 131 | u32 *resp = card->raw_csd; |
| 132 | |
| 133 | /* |
| 134 | * We only understand CSD structure v1.1 and v1.2. |
| 135 | * v1.2 has extra information in bits 15, 11 and 10. |
Kyungmin Park | 6da24b7 | 2010-08-10 18:01:36 -0700 | [diff] [blame] | 136 | * We also support eMMC v4.4 & v4.41. |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 137 | */ |
Kyungmin Park | 6da24b7 | 2010-08-10 18:01:36 -0700 | [diff] [blame] | 138 | csd->structure = UNSTUFF_BITS(resp, 126, 2); |
| 139 | if (csd->structure == 0) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 140 | pr_err("%s: unrecognised CSD structure version %d\n", |
Kyungmin Park | 6da24b7 | 2010-08-10 18:01:36 -0700 | [diff] [blame] | 141 | mmc_hostname(card->host), csd->structure); |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 142 | return -EINVAL; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4); |
| 146 | m = UNSTUFF_BITS(resp, 115, 4); |
| 147 | e = UNSTUFF_BITS(resp, 112, 3); |
| 148 | csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10; |
| 149 | csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100; |
| 150 | |
| 151 | m = UNSTUFF_BITS(resp, 99, 4); |
| 152 | e = UNSTUFF_BITS(resp, 96, 3); |
| 153 | csd->max_dtr = tran_exp[e] * tran_mant[m]; |
| 154 | csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); |
| 155 | |
| 156 | e = UNSTUFF_BITS(resp, 47, 3); |
| 157 | m = UNSTUFF_BITS(resp, 62, 12); |
| 158 | csd->capacity = (1 + m) << (e + 2); |
| 159 | |
| 160 | csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4); |
| 161 | csd->read_partial = UNSTUFF_BITS(resp, 79, 1); |
| 162 | csd->write_misalign = UNSTUFF_BITS(resp, 78, 1); |
| 163 | csd->read_misalign = UNSTUFF_BITS(resp, 77, 1); |
| 164 | csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3); |
| 165 | csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4); |
| 166 | csd->write_partial = UNSTUFF_BITS(resp, 21, 1); |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 167 | |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 168 | if (csd->write_blkbits >= 9) { |
| 169 | a = UNSTUFF_BITS(resp, 42, 5); |
| 170 | b = UNSTUFF_BITS(resp, 37, 5); |
| 171 | csd->erase_size = (a + 1) * (b + 1); |
| 172 | csd->erase_size <<= csd->write_blkbits - 9; |
| 173 | } |
| 174 | |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 175 | return 0; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /* |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 179 | * Read extended CSD. |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 180 | */ |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 181 | static int mmc_get_ext_csd(struct mmc_card *card, u8 **new_ext_csd) |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 182 | { |
| 183 | int err; |
| 184 | u8 *ext_csd; |
| 185 | |
| 186 | BUG_ON(!card); |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 187 | BUG_ON(!new_ext_csd); |
| 188 | |
| 189 | *new_ext_csd = NULL; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 190 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 191 | if (card->csd.mmca_vsn < CSD_SPEC_VER_4) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 192 | return 0; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 193 | |
| 194 | /* |
| 195 | * As the ext_csd is so large and mostly unused, we don't store the |
| 196 | * raw block in mmc_card. |
| 197 | */ |
| 198 | ext_csd = kmalloc(512, GFP_KERNEL); |
| 199 | if (!ext_csd) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 200 | pr_err("%s: could not allocate a buffer to " |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 201 | "receive the ext_csd.\n", mmc_hostname(card->host)); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 202 | return -ENOMEM; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | err = mmc_send_ext_csd(card, ext_csd); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 206 | if (err) { |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 207 | kfree(ext_csd); |
| 208 | *new_ext_csd = NULL; |
| 209 | |
Wolfgang Muees | d08ebed | 2009-09-22 16:45:26 -0700 | [diff] [blame] | 210 | /* If the host or the card can't do the switch, |
| 211 | * fail more gracefully. */ |
| 212 | if ((err != -EINVAL) |
| 213 | && (err != -ENOSYS) |
| 214 | && (err != -EFAULT)) |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 215 | return err; |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 216 | |
| 217 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 218 | * High capacity cards should have this "magic" size |
| 219 | * stored in their CSD. |
| 220 | */ |
| 221 | if (card->csd.capacity == (4096 * 512)) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 222 | pr_err("%s: unable to read EXT_CSD " |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 223 | "on a possible high capacity card. " |
| 224 | "Card will be ignored.\n", |
| 225 | mmc_hostname(card->host)); |
| 226 | } else { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 227 | pr_warning("%s: unable to read " |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 228 | "EXT_CSD, performance might " |
| 229 | "suffer.\n", |
| 230 | mmc_hostname(card->host)); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 231 | err = 0; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 232 | } |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 233 | } else |
| 234 | *new_ext_csd = ext_csd; |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 235 | |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 236 | return err; |
| 237 | } |
| 238 | |
Seungwon Jeon | fe201cd | 2012-04-25 16:17:37 +0900 | [diff] [blame] | 239 | static void mmc_select_card_type(struct mmc_card *card) |
| 240 | { |
| 241 | struct mmc_host *host = card->host; |
| 242 | u8 card_type = card->ext_csd.raw_card_type & EXT_CSD_CARD_TYPE_MASK; |
| 243 | unsigned int caps = host->caps, caps2 = host->caps2; |
| 244 | unsigned int hs_max_dtr = 0; |
| 245 | |
| 246 | if (card_type & EXT_CSD_CARD_TYPE_26) |
| 247 | hs_max_dtr = MMC_HIGH_26_MAX_DTR; |
| 248 | |
| 249 | if (caps & MMC_CAP_MMC_HIGHSPEED && |
| 250 | card_type & EXT_CSD_CARD_TYPE_52) |
| 251 | hs_max_dtr = MMC_HIGH_52_MAX_DTR; |
| 252 | |
| 253 | if ((caps & MMC_CAP_1_8V_DDR && |
| 254 | card_type & EXT_CSD_CARD_TYPE_DDR_1_8V) || |
| 255 | (caps & MMC_CAP_1_2V_DDR && |
| 256 | card_type & EXT_CSD_CARD_TYPE_DDR_1_2V)) |
| 257 | hs_max_dtr = MMC_HIGH_DDR_MAX_DTR; |
| 258 | |
| 259 | if ((caps2 & MMC_CAP2_HS200_1_8V_SDR && |
| 260 | card_type & EXT_CSD_CARD_TYPE_SDR_1_8V) || |
| 261 | (caps2 & MMC_CAP2_HS200_1_2V_SDR && |
| 262 | card_type & EXT_CSD_CARD_TYPE_SDR_1_2V)) |
| 263 | hs_max_dtr = MMC_HS200_MAX_DTR; |
| 264 | |
| 265 | card->ext_csd.hs_max_dtr = hs_max_dtr; |
| 266 | card->ext_csd.card_type = card_type; |
| 267 | } |
| 268 | |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 269 | /* |
| 270 | * Decode extended CSD. |
| 271 | */ |
| 272 | static int mmc_read_ext_csd(struct mmc_card *card, u8 *ext_csd) |
| 273 | { |
Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 274 | int err = 0, idx; |
| 275 | unsigned int part_size; |
| 276 | u8 hc_erase_grp_sz = 0, hc_wp_grp_sz = 0; |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 277 | |
| 278 | BUG_ON(!card); |
| 279 | |
| 280 | if (!ext_csd) |
| 281 | return 0; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 282 | |
Kyungmin Park | 6da24b7 | 2010-08-10 18:01:36 -0700 | [diff] [blame] | 283 | /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */ |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 284 | card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE]; |
Kyungmin Park | 6da24b7 | 2010-08-10 18:01:36 -0700 | [diff] [blame] | 285 | if (card->csd.structure == 3) { |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 286 | if (card->ext_csd.raw_ext_csd_structure > 2) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 287 | pr_err("%s: unrecognised EXT_CSD structure " |
Kyungmin Park | 6da24b7 | 2010-08-10 18:01:36 -0700 | [diff] [blame] | 288 | "version %d\n", mmc_hostname(card->host), |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 289 | card->ext_csd.raw_ext_csd_structure); |
Kyungmin Park | 6da24b7 | 2010-08-10 18:01:36 -0700 | [diff] [blame] | 290 | err = -EINVAL; |
| 291 | goto out; |
| 292 | } |
| 293 | } |
| 294 | |
Jarkko Lavinen | b1ebe38 | 2009-09-22 16:44:34 -0700 | [diff] [blame] | 295 | card->ext_csd.rev = ext_csd[EXT_CSD_REV]; |
Subhash Jadavani | 7ca4a78 | 2013-02-01 11:09:58 +0530 | [diff] [blame] | 296 | if (card->ext_csd.rev > 7) { |
Stephen Boyd | 3f113aa | 2013-02-11 14:30:14 -0800 | [diff] [blame] | 297 | pr_err("%s: unrecognised EXT_CSD revision %d\n", |
Kyungmin Park | 6da24b7 | 2010-08-10 18:01:36 -0700 | [diff] [blame] | 298 | mmc_hostname(card->host), card->ext_csd.rev); |
Florin Malita | 00cedfa | 2007-10-13 12:27:20 -0400 | [diff] [blame] | 299 | err = -EINVAL; |
| 300 | goto out; |
Pierre Ossman | d7604d7 | 2007-07-23 00:34:07 +0200 | [diff] [blame] | 301 | } |
| 302 | |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 303 | card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0]; |
| 304 | card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1]; |
| 305 | card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2]; |
| 306 | 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] | 307 | if (card->ext_csd.rev >= 2) { |
Pierre Ossman | d7604d7 | 2007-07-23 00:34:07 +0200 | [diff] [blame] | 308 | card->ext_csd.sectors = |
| 309 | ext_csd[EXT_CSD_SEC_CNT + 0] << 0 | |
| 310 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8 | |
| 311 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16 | |
| 312 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24; |
Hanumath Prasad | fc8a098 | 2010-08-10 18:01:45 -0700 | [diff] [blame] | 313 | |
| 314 | /* Cards with density > 2GiB are sector addressed */ |
| 315 | if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512) |
Pierre Ossman | d7604d7 | 2007-07-23 00:34:07 +0200 | [diff] [blame] | 316 | mmc_card_set_blockaddr(card); |
| 317 | } |
Seungwon Jeon | fe201cd | 2012-04-25 16:17:37 +0900 | [diff] [blame] | 318 | |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 319 | card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE]; |
Seungwon Jeon | fe201cd | 2012-04-25 16:17:37 +0900 | [diff] [blame] | 320 | mmc_select_card_type(card); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 321 | |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 322 | card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT]; |
| 323 | card->ext_csd.raw_erase_timeout_mult = |
| 324 | ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]; |
| 325 | card->ext_csd.raw_hc_erase_grp_size = |
| 326 | ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]; |
Jarkko Lavinen | b1ebe38 | 2009-09-22 16:44:34 -0700 | [diff] [blame] | 327 | if (card->ext_csd.rev >= 3) { |
| 328 | u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT]; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 329 | card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG]; |
| 330 | |
| 331 | /* EXT_CSD value is in units of 10ms, but we store in ms */ |
| 332 | 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] | 333 | |
| 334 | /* Sleep / awake timeout in 100ns units */ |
| 335 | if (sa_shift > 0 && sa_shift <= 0x17) |
| 336 | card->ext_csd.sa_timeout = |
| 337 | 1 << ext_csd[EXT_CSD_S_A_TIMEOUT]; |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 338 | card->ext_csd.erase_group_def = |
| 339 | ext_csd[EXT_CSD_ERASE_GROUP_DEF]; |
| 340 | card->ext_csd.hc_erase_timeout = 300 * |
| 341 | ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]; |
| 342 | card->ext_csd.hc_erase_size = |
| 343 | ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10; |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 344 | |
| 345 | 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] | 346 | |
| 347 | /* |
| 348 | * There are two boot regions of equal size, defined in |
| 349 | * multiples of 128K. |
| 350 | */ |
Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 351 | if (ext_csd[EXT_CSD_BOOT_MULT] && mmc_boot_partition_access(card->host)) { |
| 352 | for (idx = 0; idx < MMC_NUM_BOOT_PARTITION; idx++) { |
| 353 | part_size = ext_csd[EXT_CSD_BOOT_MULT] << 17; |
| 354 | mmc_part_add(card, part_size, |
| 355 | EXT_CSD_PART_CONFIG_ACC_BOOT0 + idx, |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 356 | "boot%d", idx, true, |
| 357 | MMC_BLK_DATA_AREA_BOOT); |
Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 358 | } |
| 359 | } |
Jarkko Lavinen | b1ebe38 | 2009-09-22 16:44:34 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 362 | card->ext_csd.raw_hc_erase_gap_size = |
Jurgen Heeks | dd13b4e | 2012-02-01 13:30:55 +0100 | [diff] [blame] | 363 | ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 364 | card->ext_csd.raw_sec_trim_mult = |
| 365 | ext_csd[EXT_CSD_SEC_TRIM_MULT]; |
| 366 | card->ext_csd.raw_sec_erase_mult = |
| 367 | ext_csd[EXT_CSD_SEC_ERASE_MULT]; |
| 368 | card->ext_csd.raw_sec_feature_support = |
| 369 | ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]; |
| 370 | card->ext_csd.raw_trim_mult = |
| 371 | ext_csd[EXT_CSD_TRIM_MULT]; |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 372 | if (card->ext_csd.rev >= 4) { |
Chuanxiao Dong | 709de99 | 2011-01-22 04:09:41 +0800 | [diff] [blame] | 373 | /* |
| 374 | * Enhanced area feature support -- check whether the eMMC |
| 375 | * card has the Enhanced area enabled. If so, export enhanced |
| 376 | * area offset and size to user by adding sysfs interface. |
| 377 | */ |
Andrei Warkentin | 5238acb | 2011-09-24 12:12:30 -0400 | [diff] [blame] | 378 | card->ext_csd.raw_partition_support = ext_csd[EXT_CSD_PARTITION_SUPPORT]; |
Chuanxiao Dong | 709de99 | 2011-01-22 04:09:41 +0800 | [diff] [blame] | 379 | if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) && |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 380 | (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) { |
Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 381 | hc_erase_grp_sz = |
Chuanxiao Dong | 709de99 | 2011-01-22 04:09:41 +0800 | [diff] [blame] | 382 | ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]; |
Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 383 | hc_wp_grp_sz = |
Chuanxiao Dong | 709de99 | 2011-01-22 04:09:41 +0800 | [diff] [blame] | 384 | ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; |
| 385 | |
| 386 | card->ext_csd.enhanced_area_en = 1; |
| 387 | /* |
| 388 | * calculate the enhanced data area offset, in bytes |
| 389 | */ |
| 390 | card->ext_csd.enhanced_area_offset = |
| 391 | (ext_csd[139] << 24) + (ext_csd[138] << 16) + |
| 392 | (ext_csd[137] << 8) + ext_csd[136]; |
| 393 | if (mmc_card_blockaddr(card)) |
| 394 | card->ext_csd.enhanced_area_offset <<= 9; |
| 395 | /* |
| 396 | * calculate the enhanced data area size, in kilobytes |
| 397 | */ |
| 398 | card->ext_csd.enhanced_area_size = |
| 399 | (ext_csd[142] << 16) + (ext_csd[141] << 8) + |
| 400 | ext_csd[140]; |
| 401 | card->ext_csd.enhanced_area_size *= |
| 402 | (size_t)(hc_erase_grp_sz * hc_wp_grp_sz); |
| 403 | card->ext_csd.enhanced_area_size <<= 9; |
| 404 | } else { |
| 405 | /* |
| 406 | * If the enhanced area is not enabled, disable these |
| 407 | * device attributes. |
| 408 | */ |
| 409 | card->ext_csd.enhanced_area_offset = -EINVAL; |
| 410 | card->ext_csd.enhanced_area_size = -EINVAL; |
| 411 | } |
Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 412 | |
| 413 | /* |
| 414 | * General purpose partition feature support -- |
| 415 | * If ext_csd has the size of general purpose partitions, |
| 416 | * set size, part_cfg, partition name in mmc_part. |
| 417 | */ |
| 418 | if (ext_csd[EXT_CSD_PARTITION_SUPPORT] & |
| 419 | EXT_CSD_PART_SUPPORT_PART_EN) { |
| 420 | if (card->ext_csd.enhanced_area_en != 1) { |
| 421 | hc_erase_grp_sz = |
| 422 | ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]; |
| 423 | hc_wp_grp_sz = |
| 424 | ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; |
| 425 | |
| 426 | card->ext_csd.enhanced_area_en = 1; |
| 427 | } |
| 428 | |
| 429 | for (idx = 0; idx < MMC_NUM_GP_PARTITION; idx++) { |
| 430 | if (!ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3] && |
| 431 | !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1] && |
| 432 | !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2]) |
| 433 | continue; |
| 434 | part_size = |
| 435 | (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2] |
| 436 | << 16) + |
| 437 | (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1] |
| 438 | << 8) + |
| 439 | ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3]; |
| 440 | part_size *= (size_t)(hc_erase_grp_sz * |
| 441 | hc_wp_grp_sz); |
| 442 | mmc_part_add(card, part_size << 19, |
| 443 | EXT_CSD_PART_CONFIG_ACC_GP0 + idx, |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 444 | "gp%d", idx, false, |
| 445 | MMC_BLK_DATA_AREA_GP); |
Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 446 | } |
| 447 | } |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 448 | card->ext_csd.sec_trim_mult = |
| 449 | ext_csd[EXT_CSD_SEC_TRIM_MULT]; |
| 450 | card->ext_csd.sec_erase_mult = |
| 451 | ext_csd[EXT_CSD_SEC_ERASE_MULT]; |
| 452 | card->ext_csd.sec_feature_support = |
| 453 | ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]; |
| 454 | card->ext_csd.trim_timeout = 300 * |
| 455 | ext_csd[EXT_CSD_TRIM_MULT]; |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 456 | |
| 457 | /* |
| 458 | * Note that the call to mmc_part_add above defaults to read |
| 459 | * only. If this default assumption is changed, the call must |
| 460 | * take into account the value of boot_locked below. |
| 461 | */ |
| 462 | card->ext_csd.boot_ro_lock = ext_csd[EXT_CSD_BOOT_WP]; |
| 463 | card->ext_csd.boot_ro_lockable = true; |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Adrian Hunter | b249951 | 2011-08-29 16:42:11 +0300 | [diff] [blame] | 466 | if (card->ext_csd.rev >= 5) { |
Jaehoon Chung | c1a56a1 | 2012-09-24 14:44:45 +0200 | [diff] [blame] | 467 | /* check whether the eMMC card supports BKOPS */ |
| 468 | if (ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1) { |
| 469 | card->ext_csd.bkops = 1; |
| 470 | card->ext_csd.bkops_en = ext_csd[EXT_CSD_BKOPS_EN]; |
| 471 | card->ext_csd.raw_bkops_status = |
| 472 | ext_csd[EXT_CSD_BKOPS_STATUS]; |
Maya Erez | e966c1c | 2012-09-29 06:30:47 +0200 | [diff] [blame] | 473 | if (!card->ext_csd.bkops_en && |
| 474 | card->host->caps2 & MMC_CAP2_INIT_BKOPS) { |
| 475 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 476 | EXT_CSD_BKOPS_EN, 1, 0); |
| 477 | if (err) |
| 478 | pr_warn("%s: Enabling BKOPS failed\n", |
| 479 | mmc_hostname(card->host)); |
| 480 | else |
| 481 | card->ext_csd.bkops_en = 1; |
| 482 | } |
Jaehoon Chung | c1a56a1 | 2012-09-24 14:44:45 +0200 | [diff] [blame] | 483 | } |
| 484 | |
Yaniv Gardi | b58afd6 | 2012-11-06 21:06:19 +0200 | [diff] [blame] | 485 | pr_info("%s: BKOPS_EN bit = %d\n", |
| 486 | mmc_hostname(card->host), card->ext_csd.bkops_en); |
| 487 | |
Jaehoon Chung | eb0d8f1 | 2011-10-18 01:26:42 -0400 | [diff] [blame] | 488 | /* check whether the eMMC card supports HPI */ |
| 489 | if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x1) { |
| 490 | card->ext_csd.hpi = 1; |
| 491 | if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x2) |
| 492 | card->ext_csd.hpi_cmd = MMC_STOP_TRANSMISSION; |
| 493 | else |
| 494 | card->ext_csd.hpi_cmd = MMC_SEND_STATUS; |
| 495 | /* |
| 496 | * Indicate the maximum timeout to close |
| 497 | * a command interrupted by HPI |
| 498 | */ |
| 499 | card->ext_csd.out_of_int_time = |
| 500 | ext_csd[EXT_CSD_OUT_OF_INTERRUPT_TIME] * 10; |
| 501 | } |
| 502 | |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 503 | card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM]; |
Adrian Hunter | b249951 | 2011-08-29 16:42:11 +0300 | [diff] [blame] | 504 | card->ext_csd.rst_n_function = ext_csd[EXT_CSD_RST_N_FUNCTION]; |
Loic Pallardy | e12f9ba | 2012-08-06 15:12:27 +0000 | [diff] [blame] | 505 | |
| 506 | /* |
| 507 | * RPMB regions are defined in multiples of 128K. |
| 508 | */ |
| 509 | card->ext_csd.raw_rpmb_size_mult = ext_csd[EXT_CSD_RPMB_MULT]; |
| 510 | if (ext_csd[EXT_CSD_RPMB_MULT]) { |
| 511 | mmc_part_add(card, ext_csd[EXT_CSD_RPMB_MULT] << 17, |
| 512 | EXT_CSD_PART_CONFIG_ACC_RPMB, |
| 513 | "rpmb", 0, false, |
| 514 | MMC_BLK_DATA_AREA_RPMB); |
| 515 | } |
Adrian Hunter | b249951 | 2011-08-29 16:42:11 +0300 | [diff] [blame] | 516 | } |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 517 | |
Andrei Warkentin | 5238acb | 2011-09-24 12:12:30 -0400 | [diff] [blame] | 518 | 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] | 519 | if (ext_csd[EXT_CSD_ERASED_MEM_CONT]) |
| 520 | card->erased_byte = 0xFF; |
| 521 | else |
| 522 | card->erased_byte = 0x0; |
| 523 | |
Seungwon Jeon | 336c716 | 2011-10-25 09:43:12 +0900 | [diff] [blame] | 524 | /* eMMC v4.5 or later */ |
Girish K S | bec8726 | 2011-10-13 12:04:16 +0530 | [diff] [blame] | 525 | if (card->ext_csd.rev >= 6) { |
Seungwon Jeon | 336c716 | 2011-10-25 09:43:12 +0900 | [diff] [blame] | 526 | card->ext_csd.feature_support |= MMC_DISCARD_FEATURE; |
| 527 | |
Seungwon Jeon | b23cf0b | 2011-09-23 14:15:29 +0900 | [diff] [blame] | 528 | card->ext_csd.generic_cmd6_time = 10 * |
| 529 | ext_csd[EXT_CSD_GENERIC_CMD6_TIME]; |
Girish K S | bec8726 | 2011-10-13 12:04:16 +0530 | [diff] [blame] | 530 | card->ext_csd.power_off_longtime = 10 * |
| 531 | ext_csd[EXT_CSD_POWER_OFF_LONG_TIME]; |
Seungwon Jeon | b23cf0b | 2011-09-23 14:15:29 +0900 | [diff] [blame] | 532 | |
Seungwon Jeon | 336c716 | 2011-10-25 09:43:12 +0900 | [diff] [blame] | 533 | card->ext_csd.cache_size = |
| 534 | ext_csd[EXT_CSD_CACHE_SIZE + 0] << 0 | |
| 535 | ext_csd[EXT_CSD_CACHE_SIZE + 1] << 8 | |
| 536 | ext_csd[EXT_CSD_CACHE_SIZE + 2] << 16 | |
| 537 | ext_csd[EXT_CSD_CACHE_SIZE + 3] << 24; |
Saugata Das | 4265900 | 2011-12-21 13:09:17 +0530 | [diff] [blame] | 538 | |
| 539 | if (ext_csd[EXT_CSD_DATA_SECTOR_SIZE] == 1) |
| 540 | card->ext_csd.data_sector_size = 4096; |
| 541 | else |
| 542 | card->ext_csd.data_sector_size = 512; |
| 543 | |
| 544 | if ((ext_csd[EXT_CSD_DATA_TAG_SUPPORT] & 1) && |
| 545 | (ext_csd[EXT_CSD_TAG_UNIT_SIZE] <= 8)) { |
| 546 | card->ext_csd.data_tag_unit_size = |
| 547 | ((unsigned int) 1 << ext_csd[EXT_CSD_TAG_UNIT_SIZE]) * |
| 548 | (card->ext_csd.data_sector_size); |
| 549 | } else { |
| 550 | card->ext_csd.data_tag_unit_size = 0; |
| 551 | } |
Seungwon Jeon | c70e966 | 2012-09-27 16:12:47 +0200 | [diff] [blame] | 552 | |
| 553 | card->ext_csd.max_packed_writes = |
| 554 | ext_csd[EXT_CSD_MAX_PACKED_WRITES]; |
| 555 | card->ext_csd.max_packed_reads = |
| 556 | ext_csd[EXT_CSD_MAX_PACKED_READS]; |
Seungwon Jeon | 336c716 | 2011-10-25 09:43:12 +0900 | [diff] [blame] | 557 | } |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 558 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 559 | out: |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 560 | return err; |
| 561 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 562 | |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 563 | static inline void mmc_free_ext_csd(u8 *ext_csd) |
| 564 | { |
| 565 | kfree(ext_csd); |
| 566 | } |
| 567 | |
| 568 | |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 569 | 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] | 570 | { |
| 571 | u8 *bw_ext_csd; |
| 572 | int err; |
| 573 | |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 574 | if (bus_width == MMC_BUS_WIDTH_1) |
| 575 | return 0; |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 576 | |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 577 | err = mmc_get_ext_csd(card, &bw_ext_csd); |
| 578 | |
| 579 | if (err || bw_ext_csd == NULL) { |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 580 | if (bus_width != MMC_BUS_WIDTH_1) |
| 581 | err = -EINVAL; |
| 582 | goto out; |
| 583 | } |
| 584 | |
| 585 | if (bus_width == MMC_BUS_WIDTH_1) |
| 586 | goto out; |
| 587 | |
| 588 | /* only compare read only fields */ |
Jurgen Heeks | dd13b4e | 2012-02-01 13:30:55 +0100 | [diff] [blame] | 589 | err = !((card->ext_csd.raw_partition_support == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 590 | bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) && |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 591 | (card->ext_csd.raw_erased_mem_count == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 592 | bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) && |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 593 | (card->ext_csd.rev == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 594 | bw_ext_csd[EXT_CSD_REV]) && |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 595 | (card->ext_csd.raw_ext_csd_structure == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 596 | bw_ext_csd[EXT_CSD_STRUCTURE]) && |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 597 | (card->ext_csd.raw_card_type == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 598 | bw_ext_csd[EXT_CSD_CARD_TYPE]) && |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 599 | (card->ext_csd.raw_s_a_timeout == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 600 | bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) && |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 601 | (card->ext_csd.raw_hc_erase_gap_size == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 602 | bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) && |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 603 | (card->ext_csd.raw_erase_timeout_mult == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 604 | bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) && |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 605 | (card->ext_csd.raw_hc_erase_grp_size == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 606 | bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) && |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 607 | (card->ext_csd.raw_sec_trim_mult == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 608 | bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) && |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 609 | (card->ext_csd.raw_sec_erase_mult == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 610 | bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) && |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 611 | (card->ext_csd.raw_sec_feature_support == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 612 | bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) && |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 613 | (card->ext_csd.raw_trim_mult == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 614 | bw_ext_csd[EXT_CSD_TRIM_MULT]) && |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 615 | (card->ext_csd.raw_sectors[0] == |
| 616 | bw_ext_csd[EXT_CSD_SEC_CNT + 0]) && |
| 617 | (card->ext_csd.raw_sectors[1] == |
| 618 | bw_ext_csd[EXT_CSD_SEC_CNT + 1]) && |
| 619 | (card->ext_csd.raw_sectors[2] == |
| 620 | bw_ext_csd[EXT_CSD_SEC_CNT + 2]) && |
| 621 | (card->ext_csd.raw_sectors[3] == |
| 622 | bw_ext_csd[EXT_CSD_SEC_CNT + 3])); |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 623 | if (err) |
| 624 | err = -EINVAL; |
| 625 | |
| 626 | out: |
| 627 | mmc_free_ext_csd(bw_ext_csd); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 628 | return err; |
| 629 | } |
| 630 | |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 631 | MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1], |
| 632 | card->raw_cid[2], card->raw_cid[3]); |
| 633 | MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1], |
| 634 | card->raw_csd[2], card->raw_csd[3]); |
| 635 | 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] | 636 | MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9); |
| 637 | MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9); |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 638 | MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev); |
| 639 | MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev); |
| 640 | MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid); |
| 641 | MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name); |
| 642 | MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid); |
| 643 | MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial); |
Chuanxiao Dong | 709de99 | 2011-01-22 04:09:41 +0800 | [diff] [blame] | 644 | MMC_DEV_ATTR(enhanced_area_offset, "%llu\n", |
| 645 | card->ext_csd.enhanced_area_offset); |
| 646 | MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size); |
Loic Pallardy | 762a87b | 2012-08-06 15:12:29 +0000 | [diff] [blame] | 647 | MMC_DEV_ATTR(raw_rpmb_size_mult, "%#x\n", card->ext_csd.raw_rpmb_size_mult); |
| 648 | MMC_DEV_ATTR(rel_sectors, "%#x\n", card->ext_csd.rel_sectors); |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 649 | |
| 650 | static struct attribute *mmc_std_attrs[] = { |
| 651 | &dev_attr_cid.attr, |
| 652 | &dev_attr_csd.attr, |
| 653 | &dev_attr_date.attr, |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 654 | &dev_attr_erase_size.attr, |
| 655 | &dev_attr_preferred_erase_size.attr, |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 656 | &dev_attr_fwrev.attr, |
| 657 | &dev_attr_hwrev.attr, |
| 658 | &dev_attr_manfid.attr, |
| 659 | &dev_attr_name.attr, |
| 660 | &dev_attr_oemid.attr, |
| 661 | &dev_attr_serial.attr, |
Chuanxiao Dong | 709de99 | 2011-01-22 04:09:41 +0800 | [diff] [blame] | 662 | &dev_attr_enhanced_area_offset.attr, |
| 663 | &dev_attr_enhanced_area_size.attr, |
Loic Pallardy | 762a87b | 2012-08-06 15:12:29 +0000 | [diff] [blame] | 664 | &dev_attr_raw_rpmb_size_mult.attr, |
| 665 | &dev_attr_rel_sectors.attr, |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 666 | NULL, |
| 667 | }; |
| 668 | |
| 669 | static struct attribute_group mmc_std_attr_group = { |
| 670 | .attrs = mmc_std_attrs, |
| 671 | }; |
| 672 | |
David Brownell | a4dbd67 | 2009-06-24 10:06:31 -0700 | [diff] [blame] | 673 | static const struct attribute_group *mmc_attr_groups[] = { |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 674 | &mmc_std_attr_group, |
| 675 | NULL, |
| 676 | }; |
| 677 | |
| 678 | static struct device_type mmc_type = { |
| 679 | .groups = mmc_attr_groups, |
| 680 | }; |
| 681 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 682 | /* |
Girish K S | b87d8db | 2011-09-23 20:41:47 +0530 | [diff] [blame] | 683 | * Select the PowerClass for the current bus width |
| 684 | * If power class is defined for 4/8 bit bus in the |
| 685 | * extended CSD register, select it by executing the |
| 686 | * mmc_switch command. |
| 687 | */ |
| 688 | static int mmc_select_powerclass(struct mmc_card *card, |
| 689 | unsigned int bus_width, u8 *ext_csd) |
| 690 | { |
| 691 | int err = 0; |
| 692 | unsigned int pwrclass_val; |
| 693 | unsigned int index = 0; |
| 694 | struct mmc_host *host; |
| 695 | |
| 696 | BUG_ON(!card); |
| 697 | |
| 698 | host = card->host; |
| 699 | BUG_ON(!host); |
| 700 | |
| 701 | if (ext_csd == NULL) |
| 702 | return 0; |
| 703 | |
| 704 | /* Power class selection is supported for versions >= 4.0 */ |
| 705 | if (card->csd.mmca_vsn < CSD_SPEC_VER_4) |
| 706 | return 0; |
| 707 | |
| 708 | /* Power class values are defined only for 4/8 bit bus */ |
| 709 | if (bus_width == EXT_CSD_BUS_WIDTH_1) |
| 710 | return 0; |
| 711 | |
| 712 | switch (1 << host->ios.vdd) { |
| 713 | case MMC_VDD_165_195: |
| 714 | if (host->ios.clock <= 26000000) |
| 715 | index = EXT_CSD_PWR_CL_26_195; |
| 716 | else if (host->ios.clock <= 52000000) |
| 717 | index = (bus_width <= EXT_CSD_BUS_WIDTH_8) ? |
| 718 | EXT_CSD_PWR_CL_52_195 : |
| 719 | EXT_CSD_PWR_CL_DDR_52_195; |
| 720 | else if (host->ios.clock <= 200000000) |
| 721 | index = EXT_CSD_PWR_CL_200_195; |
| 722 | break; |
Subhash Jadavani | 93fc5a4 | 2012-04-03 12:25:58 +0530 | [diff] [blame] | 723 | case MMC_VDD_27_28: |
| 724 | case MMC_VDD_28_29: |
| 725 | case MMC_VDD_29_30: |
| 726 | case MMC_VDD_30_31: |
| 727 | case MMC_VDD_31_32: |
Girish K S | b87d8db | 2011-09-23 20:41:47 +0530 | [diff] [blame] | 728 | case MMC_VDD_32_33: |
| 729 | case MMC_VDD_33_34: |
| 730 | case MMC_VDD_34_35: |
| 731 | case MMC_VDD_35_36: |
| 732 | if (host->ios.clock <= 26000000) |
| 733 | index = EXT_CSD_PWR_CL_26_360; |
| 734 | else if (host->ios.clock <= 52000000) |
| 735 | index = (bus_width <= EXT_CSD_BUS_WIDTH_8) ? |
| 736 | EXT_CSD_PWR_CL_52_360 : |
| 737 | EXT_CSD_PWR_CL_DDR_52_360; |
| 738 | else if (host->ios.clock <= 200000000) |
| 739 | index = EXT_CSD_PWR_CL_200_360; |
| 740 | break; |
| 741 | default: |
| 742 | pr_warning("%s: Voltage range not supported " |
| 743 | "for power class.\n", mmc_hostname(host)); |
| 744 | return -EINVAL; |
| 745 | } |
| 746 | |
| 747 | pwrclass_val = ext_csd[index]; |
| 748 | |
| 749 | if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8)) |
| 750 | pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >> |
| 751 | EXT_CSD_PWR_CL_8BIT_SHIFT; |
| 752 | else |
| 753 | pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >> |
| 754 | EXT_CSD_PWR_CL_4BIT_SHIFT; |
| 755 | |
| 756 | /* If the power class is different from the default value */ |
| 757 | if (pwrclass_val > 0) { |
| 758 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 759 | EXT_CSD_POWER_CLASS, |
| 760 | pwrclass_val, |
Seungwon Jeon | 71fe3eb | 2011-10-18 13:20:57 +0900 | [diff] [blame] | 761 | card->ext_csd.generic_cmd6_time); |
Girish K S | b87d8db | 2011-09-23 20:41:47 +0530 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | return err; |
| 765 | } |
| 766 | |
| 767 | /* |
Girish K S | a4924c7 | 2012-01-11 14:04:52 -0500 | [diff] [blame] | 768 | * Selects the desired buswidth and switch to the HS200 mode |
| 769 | * if bus width set without error |
| 770 | */ |
| 771 | static int mmc_select_hs200(struct mmc_card *card) |
| 772 | { |
| 773 | int idx, err = 0; |
| 774 | struct mmc_host *host; |
| 775 | static unsigned ext_csd_bits[] = { |
| 776 | EXT_CSD_BUS_WIDTH_4, |
| 777 | EXT_CSD_BUS_WIDTH_8, |
| 778 | }; |
| 779 | static unsigned bus_widths[] = { |
| 780 | MMC_BUS_WIDTH_4, |
| 781 | MMC_BUS_WIDTH_8, |
| 782 | }; |
| 783 | |
| 784 | BUG_ON(!card); |
| 785 | |
| 786 | host = card->host; |
| 787 | |
| 788 | if (card->ext_csd.card_type & EXT_CSD_CARD_TYPE_SDR_1_2V && |
| 789 | host->caps2 & MMC_CAP2_HS200_1_2V_SDR) |
| 790 | if (mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120, 0)) |
| 791 | err = mmc_set_signal_voltage(host, |
| 792 | MMC_SIGNAL_VOLTAGE_180, 0); |
| 793 | |
| 794 | /* If fails try again during next card power cycle */ |
| 795 | if (err) |
| 796 | goto err; |
| 797 | |
| 798 | idx = (host->caps & MMC_CAP_8_BIT_DATA) ? 1 : 0; |
| 799 | |
| 800 | /* |
| 801 | * Unlike SD, MMC cards dont have a configuration register to notify |
| 802 | * supported bus width. So bus test command should be run to identify |
| 803 | * the supported bus width or compare the ext csd values of current |
| 804 | * bus width and ext csd values of 1 bit mode read earlier. |
| 805 | */ |
| 806 | for (; idx >= 0; idx--) { |
| 807 | |
| 808 | /* |
| 809 | * Host is capable of 8bit transfer, then switch |
| 810 | * the device to work in 8bit transfer mode. If the |
| 811 | * mmc switch command returns error then switch to |
| 812 | * 4bit transfer mode. On success set the corresponding |
| 813 | * bus width on the host. |
| 814 | */ |
| 815 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 816 | EXT_CSD_BUS_WIDTH, |
| 817 | ext_csd_bits[idx], |
| 818 | card->ext_csd.generic_cmd6_time); |
| 819 | if (err) |
| 820 | continue; |
| 821 | |
| 822 | mmc_set_bus_width(card->host, bus_widths[idx]); |
| 823 | |
| 824 | if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST)) |
| 825 | err = mmc_compare_ext_csds(card, bus_widths[idx]); |
| 826 | else |
| 827 | err = mmc_bus_test(card, bus_widths[idx]); |
| 828 | if (!err) |
| 829 | break; |
| 830 | } |
| 831 | |
| 832 | /* switch to HS200 mode if bus width set successfully */ |
| 833 | if (!err) |
| 834 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 835 | EXT_CSD_HS_TIMING, 2, 0); |
| 836 | err: |
| 837 | return err; |
| 838 | } |
| 839 | |
Sujit Reddy Thumma | 1cd61b7 | 2012-08-06 11:23:21 +0530 | [diff] [blame] | 840 | /** |
| 841 | * mmc_change_bus_speed() - Change MMC card bus frequency at runtime |
| 842 | * @host: pointer to mmc host structure |
| 843 | * @freq: pointer to desired frequency to be set |
| 844 | * |
| 845 | * Change the MMC card bus frequency at runtime after the card is |
| 846 | * initialized. Callers are expected to make sure of the card's |
| 847 | * state (DATA/RCV/TRANSFER) beforing changing the frequency at runtime. |
| 848 | * |
| 849 | * If the frequency to change is greater than max. supported by card, |
| 850 | * *freq is changed to max. supported by card and if it is less than min. |
| 851 | * supported by host, *freq is changed to min. supported by host. |
| 852 | */ |
| 853 | static int mmc_change_bus_speed(struct mmc_host *host, unsigned long *freq) |
| 854 | { |
| 855 | int err = 0; |
| 856 | struct mmc_card *card; |
| 857 | |
| 858 | mmc_claim_host(host); |
| 859 | /* |
| 860 | * Assign card pointer after claiming host to avoid race |
| 861 | * conditions that may arise during removal of the card. |
| 862 | */ |
| 863 | card = host->card; |
| 864 | |
| 865 | if (!card || !freq) { |
| 866 | err = -EINVAL; |
| 867 | goto out; |
| 868 | } |
| 869 | |
| 870 | if (mmc_card_highspeed(card) || mmc_card_hs200(card) |
| 871 | || mmc_card_ddr_mode(card)) { |
| 872 | if (*freq > card->ext_csd.hs_max_dtr) |
| 873 | *freq = card->ext_csd.hs_max_dtr; |
| 874 | } else if (*freq > card->csd.max_dtr) { |
| 875 | *freq = card->csd.max_dtr; |
| 876 | } |
| 877 | |
| 878 | if (*freq < host->f_min) |
| 879 | *freq = host->f_min; |
| 880 | |
| 881 | mmc_set_clock(host, (unsigned int) (*freq)); |
| 882 | |
| 883 | if (mmc_card_hs200(card) && card->host->ops->execute_tuning) { |
| 884 | /* |
| 885 | * We try to probe host driver for tuning for any |
| 886 | * frequency, it is host driver responsibility to |
| 887 | * perform actual tuning only when required. |
| 888 | */ |
| 889 | mmc_host_clk_hold(card->host); |
| 890 | err = card->host->ops->execute_tuning(card->host, |
| 891 | MMC_SEND_TUNING_BLOCK_HS200); |
| 892 | mmc_host_clk_release(card->host); |
| 893 | |
| 894 | if (err) |
| 895 | pr_warn("%s: %s: tuning execution failed %d\n", |
| 896 | mmc_hostname(card->host), __func__, err); |
| 897 | } |
| 898 | out: |
| 899 | mmc_release_host(host); |
| 900 | return err; |
| 901 | } |
| 902 | |
Girish K S | a4924c7 | 2012-01-11 14:04:52 -0500 | [diff] [blame] | 903 | /* |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 904 | * Handle the detection and initialisation of a card. |
| 905 | * |
Deepak Saxena | 8769392 | 2008-06-16 19:20:57 -0700 | [diff] [blame] | 906 | * In the case of a resume, "oldcard" will contain the card |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 907 | * we're trying to reinitialise. |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 908 | */ |
Pierre Ossman | 8c75dea | 2007-05-19 16:14:43 +0200 | [diff] [blame] | 909 | static int mmc_init_card(struct mmc_host *host, u32 ocr, |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 910 | struct mmc_card *oldcard) |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 911 | { |
| 912 | struct mmc_card *card; |
Philip Rakity | 25d5c69 | 2010-11-07 16:22:28 -0500 | [diff] [blame] | 913 | int err, ddr = 0; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 914 | u32 cid[4]; |
| 915 | unsigned int max_dtr; |
Philip Rakity | b676f03 | 2011-02-13 23:13:09 -0800 | [diff] [blame] | 916 | u32 rocr; |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 917 | u8 *ext_csd = NULL; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 918 | |
| 919 | BUG_ON(!host); |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 920 | WARN_ON(!host->claimed); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 921 | |
Stefan Nilsson XK | 4466903 | 2011-09-15 17:50:38 +0200 | [diff] [blame] | 922 | /* Set correct bus mode for MMC before attempting init */ |
| 923 | if (!mmc_host_is_spi(host)) |
| 924 | mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN); |
| 925 | |
Ulf Hansson | e774747 | 2012-03-01 13:18:05 +0100 | [diff] [blame] | 926 | /* Initialization should be done at 3.3 V I/O voltage. */ |
| 927 | mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330, 0); |
| 928 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 929 | /* |
| 930 | * Since we're changing the OCR value, we seem to |
| 931 | * need to tell some cards to go back to the idle |
| 932 | * state. We wait 1ms to give cards time to |
| 933 | * respond. |
Balaji T K | c380546 | 2011-09-08 22:08:39 +0530 | [diff] [blame] | 934 | * mmc_go_idle is needed for eMMC that are asleep |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 935 | */ |
| 936 | mmc_go_idle(host); |
| 937 | |
| 938 | /* The extra bit indicates that we support high capacity */ |
Philip Rakity | b676f03 | 2011-02-13 23:13:09 -0800 | [diff] [blame] | 939 | err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 940 | if (err) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 941 | goto err; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 942 | |
| 943 | /* |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 944 | * For SPI, enable CRC as appropriate. |
| 945 | */ |
| 946 | if (mmc_host_is_spi(host)) { |
| 947 | err = mmc_spi_set_crc(host, use_spi_crc); |
| 948 | if (err) |
| 949 | goto err; |
| 950 | } |
| 951 | |
| 952 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 953 | * Fetch CID from card. |
| 954 | */ |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 955 | if (mmc_host_is_spi(host)) |
| 956 | err = mmc_send_cid(host, cid); |
| 957 | else |
| 958 | err = mmc_all_send_cid(host, cid); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 959 | if (err) |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 960 | goto err; |
| 961 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 962 | if (oldcard) { |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 963 | if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) { |
| 964 | err = -ENOENT; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 965 | goto err; |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 966 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 967 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 968 | card = oldcard; |
| 969 | } else { |
| 970 | /* |
| 971 | * Allocate card structure. |
| 972 | */ |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 973 | card = mmc_alloc_card(host, &mmc_type); |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 974 | if (IS_ERR(card)) { |
| 975 | err = PTR_ERR(card); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 976 | goto err; |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 977 | } |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 978 | |
| 979 | card->type = MMC_TYPE_MMC; |
| 980 | card->rca = 1; |
| 981 | memcpy(card->raw_cid, cid, sizeof(card->raw_cid)); |
| 982 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 983 | |
| 984 | /* |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 985 | * For native busses: set card RCA and quit open drain mode. |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 986 | */ |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 987 | if (!mmc_host_is_spi(host)) { |
| 988 | err = mmc_set_relative_addr(card); |
| 989 | if (err) |
| 990 | goto free_card; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 991 | |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 992 | mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL); |
| 993 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 994 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 995 | if (!oldcard) { |
| 996 | /* |
| 997 | * Fetch CSD from card. |
| 998 | */ |
| 999 | err = mmc_send_csd(card, card->raw_csd); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 1000 | if (err) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1001 | goto free_card; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1002 | |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 1003 | err = mmc_decode_csd(card); |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 1004 | if (err) |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 1005 | goto free_card; |
| 1006 | err = mmc_decode_cid(card); |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 1007 | if (err) |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 1008 | goto free_card; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1009 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1010 | |
| 1011 | /* |
Pierre Ossman | 89a73cf | 2007-05-01 15:08:30 +0200 | [diff] [blame] | 1012 | * Select card, as all following commands rely on that. |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1013 | */ |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 1014 | if (!mmc_host_is_spi(host)) { |
| 1015 | err = mmc_select_card(card); |
| 1016 | if (err) |
| 1017 | goto free_card; |
| 1018 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1019 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1020 | if (!oldcard) { |
| 1021 | /* |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 1022 | * Fetch and process extended CSD. |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1023 | */ |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 1024 | |
| 1025 | err = mmc_get_ext_csd(card, &ext_csd); |
| 1026 | if (err) |
| 1027 | goto free_card; |
| 1028 | err = mmc_read_ext_csd(card, ext_csd); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 1029 | if (err) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1030 | goto free_card; |
Philip Rakity | b676f03 | 2011-02-13 23:13:09 -0800 | [diff] [blame] | 1031 | |
| 1032 | /* If doing byte addressing, check if required to do sector |
| 1033 | * addressing. Handle the case of <2GB cards needing sector |
| 1034 | * addressing. See section 8.1 JEDEC Standard JED84-A441; |
| 1035 | * ocr register has bit 30 set for sector addressing. |
| 1036 | */ |
| 1037 | if (!(mmc_card_blockaddr(card)) && (rocr & (1<<30))) |
| 1038 | mmc_card_set_blockaddr(card); |
| 1039 | |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1040 | /* Erase size depends on CSD and Extended CSD */ |
| 1041 | mmc_set_erase_size(card); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1042 | |
| 1043 | if (card->ext_csd.sectors && (rocr & MMC_CARD_SECTOR_ADDR)) |
| 1044 | mmc_card_set_blockaddr(card); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1045 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1046 | |
| 1047 | /* |
Chuanxiao Dong | 709de99 | 2011-01-22 04:09:41 +0800 | [diff] [blame] | 1048 | * 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] | 1049 | * bit. This bit will be lost every time after a reset or power off. |
Chuanxiao Dong | 709de99 | 2011-01-22 04:09:41 +0800 | [diff] [blame] | 1050 | */ |
Adrian Hunter | 83bb24a | 2012-03-13 05:19:13 -0400 | [diff] [blame] | 1051 | if (card->ext_csd.enhanced_area_en || |
| 1052 | (card->ext_csd.rev >= 3 && (host->caps2 & MMC_CAP2_HC_ERASE_SZ))) { |
Chuanxiao Dong | 709de99 | 2011-01-22 04:09:41 +0800 | [diff] [blame] | 1053 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
Seungwon Jeon | b23cf0b | 2011-09-23 14:15:29 +0900 | [diff] [blame] | 1054 | EXT_CSD_ERASE_GROUP_DEF, 1, |
| 1055 | card->ext_csd.generic_cmd6_time); |
Chuanxiao Dong | 709de99 | 2011-01-22 04:09:41 +0800 | [diff] [blame] | 1056 | |
| 1057 | if (err && err != -EBADMSG) |
| 1058 | goto free_card; |
| 1059 | |
| 1060 | if (err) { |
| 1061 | err = 0; |
| 1062 | /* |
| 1063 | * Just disable enhanced area off & sz |
| 1064 | * will try to enable ERASE_GROUP_DEF |
| 1065 | * during next time reinit |
| 1066 | */ |
| 1067 | card->ext_csd.enhanced_area_offset = -EINVAL; |
| 1068 | card->ext_csd.enhanced_area_size = -EINVAL; |
| 1069 | } else { |
| 1070 | card->ext_csd.erase_group_def = 1; |
| 1071 | /* |
| 1072 | * enable ERASE_GRP_DEF successfully. |
| 1073 | * This will affect the erase size, so |
| 1074 | * here need to reset erase size |
| 1075 | */ |
| 1076 | mmc_set_erase_size(card); |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | /* |
Philip Rakity | 41e2a48 | 2011-03-19 14:10:33 -0400 | [diff] [blame] | 1081 | * Ensure eMMC user default partition is enabled |
| 1082 | */ |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1083 | if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) { |
| 1084 | card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK; |
| 1085 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG, |
| 1086 | card->ext_csd.part_config, |
| 1087 | card->ext_csd.part_time); |
| 1088 | if (err && err != -EBADMSG) |
| 1089 | goto free_card; |
Oluwafemi Adeyemi | 151b477 | 2013-01-03 11:32:53 -0800 | [diff] [blame] | 1090 | card->part_curr = card->ext_csd.part_config & |
| 1091 | EXT_CSD_PART_CONFIG_ACC_MASK; |
Philip Rakity | 41e2a48 | 2011-03-19 14:10:33 -0400 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | /* |
Girish K S | bec8726 | 2011-10-13 12:04:16 +0530 | [diff] [blame] | 1095 | * If the host supports the power_off_notify capability then |
| 1096 | * set the notification byte in the ext_csd register of device |
| 1097 | */ |
| 1098 | if ((host->caps2 & MMC_CAP2_POWEROFF_NOTIFY) && |
Girish K S | a80f162 | 2011-11-15 11:55:46 +0530 | [diff] [blame] | 1099 | (card->ext_csd.rev >= 6)) { |
Girish K S | bec8726 | 2011-10-13 12:04:16 +0530 | [diff] [blame] | 1100 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 1101 | EXT_CSD_POWER_OFF_NOTIFICATION, |
| 1102 | EXT_CSD_POWER_ON, |
| 1103 | card->ext_csd.generic_cmd6_time); |
| 1104 | if (err && err != -EBADMSG) |
| 1105 | goto free_card; |
Girish K S | bec8726 | 2011-10-13 12:04:16 +0530 | [diff] [blame] | 1106 | |
Girish K S | 96a85d5 | 2011-11-04 16:22:47 +0530 | [diff] [blame] | 1107 | /* |
| 1108 | * The err can be -EBADMSG or 0, |
| 1109 | * so check for success and update the flag |
| 1110 | */ |
| 1111 | if (!err) |
Maya Erez | cec02a4 | 2012-10-19 20:18:17 +0200 | [diff] [blame] | 1112 | card->ext_csd.power_off_notification = EXT_CSD_POWER_ON; |
Girish K S | 96a85d5 | 2011-11-04 16:22:47 +0530 | [diff] [blame] | 1113 | } |
Girish K S | bec8726 | 2011-10-13 12:04:16 +0530 | [diff] [blame] | 1114 | |
| 1115 | /* |
Pierre Ossman | 89a73cf | 2007-05-01 15:08:30 +0200 | [diff] [blame] | 1116 | * Activate high speed (if supported) |
| 1117 | */ |
Girish K S | a4924c7 | 2012-01-11 14:04:52 -0500 | [diff] [blame] | 1118 | if (card->ext_csd.hs_max_dtr != 0) { |
| 1119 | err = 0; |
| 1120 | if (card->ext_csd.hs_max_dtr > 52000000 && |
| 1121 | host->caps2 & MMC_CAP2_HS200) |
| 1122 | err = mmc_select_hs200(card); |
| 1123 | else if (host->caps & MMC_CAP_MMC_HIGHSPEED) |
| 1124 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
Girish K S | 7488e92 | 2012-01-19 08:56:19 +0530 | [diff] [blame] | 1125 | EXT_CSD_HS_TIMING, 1, |
| 1126 | card->ext_csd.generic_cmd6_time); |
Girish K S | a4924c7 | 2012-01-11 14:04:52 -0500 | [diff] [blame] | 1127 | |
Adrian Hunter | ef0b27d | 2009-09-22 16:44:37 -0700 | [diff] [blame] | 1128 | if (err && err != -EBADMSG) |
Pierre Ossman | 89a73cf | 2007-05-01 15:08:30 +0200 | [diff] [blame] | 1129 | goto free_card; |
| 1130 | |
Adrian Hunter | ef0b27d | 2009-09-22 16:44:37 -0700 | [diff] [blame] | 1131 | if (err) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 1132 | pr_warning("%s: switch to highspeed failed\n", |
Adrian Hunter | ef0b27d | 2009-09-22 16:44:37 -0700 | [diff] [blame] | 1133 | mmc_hostname(card->host)); |
| 1134 | err = 0; |
| 1135 | } else { |
Girish K S | a4924c7 | 2012-01-11 14:04:52 -0500 | [diff] [blame] | 1136 | if (card->ext_csd.hs_max_dtr > 52000000 && |
| 1137 | host->caps2 & MMC_CAP2_HS200) { |
| 1138 | mmc_card_set_hs200(card); |
| 1139 | mmc_set_timing(card->host, |
| 1140 | MMC_TIMING_MMC_HS200); |
| 1141 | } else { |
| 1142 | mmc_card_set_highspeed(card); |
| 1143 | mmc_set_timing(card->host, MMC_TIMING_MMC_HS); |
| 1144 | } |
Adrian Hunter | ef0b27d | 2009-09-22 16:44:37 -0700 | [diff] [blame] | 1145 | } |
Pierre Ossman | 89a73cf | 2007-05-01 15:08:30 +0200 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1149 | * Compute bus speed. |
| 1150 | */ |
| 1151 | max_dtr = (unsigned int)-1; |
| 1152 | |
Girish K S | a4924c7 | 2012-01-11 14:04:52 -0500 | [diff] [blame] | 1153 | if (mmc_card_highspeed(card) || mmc_card_hs200(card)) { |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1154 | if (max_dtr > card->ext_csd.hs_max_dtr) |
| 1155 | max_dtr = card->ext_csd.hs_max_dtr; |
| 1156 | } else if (max_dtr > card->csd.max_dtr) { |
| 1157 | max_dtr = card->csd.max_dtr; |
| 1158 | } |
| 1159 | |
| 1160 | mmc_set_clock(host, max_dtr); |
| 1161 | |
Pierre Ossman | 89a73cf | 2007-05-01 15:08:30 +0200 | [diff] [blame] | 1162 | /* |
Adrian Hunter | 0f8d8ea | 2010-08-24 13:20:26 +0300 | [diff] [blame] | 1163 | * Indicate DDR mode (if supported). |
Hanumath Prasad | dfc13e8 | 2010-09-30 17:37:23 -0400 | [diff] [blame] | 1164 | */ |
| 1165 | if (mmc_card_highspeed(card)) { |
| 1166 | if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_8V) |
Philip Rakity | 4c4cb17 | 2011-05-13 11:17:18 +0530 | [diff] [blame] | 1167 | && ((host->caps & (MMC_CAP_1_8V_DDR | |
| 1168 | MMC_CAP_UHS_DDR50)) |
| 1169 | == (MMC_CAP_1_8V_DDR | MMC_CAP_UHS_DDR50))) |
Adrian Hunter | 49e3b5a | 2010-10-11 12:43:50 +0300 | [diff] [blame] | 1170 | ddr = MMC_1_8V_DDR_MODE; |
Hanumath Prasad | dfc13e8 | 2010-09-30 17:37:23 -0400 | [diff] [blame] | 1171 | else if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_2V) |
Philip Rakity | 4c4cb17 | 2011-05-13 11:17:18 +0530 | [diff] [blame] | 1172 | && ((host->caps & (MMC_CAP_1_2V_DDR | |
| 1173 | MMC_CAP_UHS_DDR50)) |
| 1174 | == (MMC_CAP_1_2V_DDR | MMC_CAP_UHS_DDR50))) |
Adrian Hunter | 49e3b5a | 2010-10-11 12:43:50 +0300 | [diff] [blame] | 1175 | ddr = MMC_1_2V_DDR_MODE; |
Hanumath Prasad | dfc13e8 | 2010-09-30 17:37:23 -0400 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | /* |
Girish K S | a4924c7 | 2012-01-11 14:04:52 -0500 | [diff] [blame] | 1179 | * Indicate HS200 SDR mode (if supported). |
| 1180 | */ |
| 1181 | if (mmc_card_hs200(card)) { |
| 1182 | u32 ext_csd_bits; |
| 1183 | u32 bus_width = card->host->ios.bus_width; |
| 1184 | |
| 1185 | /* |
| 1186 | * For devices supporting HS200 mode, the bus width has |
| 1187 | * to be set before executing the tuning function. If |
| 1188 | * set before tuning, then device will respond with CRC |
| 1189 | * errors for responses on CMD line. So for HS200 the |
| 1190 | * sequence will be |
| 1191 | * 1. set bus width 4bit / 8 bit (1 bit not supported) |
| 1192 | * 2. switch to HS200 mode |
| 1193 | * 3. set the clock to > 52Mhz <=200MHz and |
| 1194 | * 4. execute tuning for HS200 |
| 1195 | */ |
| 1196 | if ((host->caps2 & MMC_CAP2_HS200) && |
Subhash Jadavani | 52d0974 | 2012-03-06 17:59:12 +0530 | [diff] [blame] | 1197 | card->host->ops->execute_tuning) { |
| 1198 | mmc_host_clk_hold(card->host); |
Girish K S | a4924c7 | 2012-01-11 14:04:52 -0500 | [diff] [blame] | 1199 | err = card->host->ops->execute_tuning(card->host, |
| 1200 | MMC_SEND_TUNING_BLOCK_HS200); |
Subhash Jadavani | 52d0974 | 2012-03-06 17:59:12 +0530 | [diff] [blame] | 1201 | mmc_host_clk_release(card->host); |
| 1202 | } |
Girish K S | a4924c7 | 2012-01-11 14:04:52 -0500 | [diff] [blame] | 1203 | if (err) { |
| 1204 | pr_warning("%s: tuning execution failed\n", |
| 1205 | mmc_hostname(card->host)); |
| 1206 | goto err; |
| 1207 | } |
| 1208 | |
| 1209 | ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ? |
| 1210 | EXT_CSD_BUS_WIDTH_8 : EXT_CSD_BUS_WIDTH_4; |
| 1211 | err = mmc_select_powerclass(card, ext_csd_bits, ext_csd); |
Subhash Jadavani | 93fc5a4 | 2012-04-03 12:25:58 +0530 | [diff] [blame] | 1212 | if (err) |
| 1213 | pr_warning("%s: power class selection to bus width %d" |
| 1214 | " failed\n", mmc_hostname(card->host), |
| 1215 | 1 << bus_width); |
Girish K S | a4924c7 | 2012-01-11 14:04:52 -0500 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | /* |
Adrian Hunter | 0f8d8ea | 2010-08-24 13:20:26 +0300 | [diff] [blame] | 1219 | * Activate wide bus and DDR (if supported). |
Pierre Ossman | 89a73cf | 2007-05-01 15:08:30 +0200 | [diff] [blame] | 1220 | */ |
Girish K S | a4924c7 | 2012-01-11 14:04:52 -0500 | [diff] [blame] | 1221 | if (!mmc_card_hs200(card) && |
Girish K S | 7488e92 | 2012-01-19 08:56:19 +0530 | [diff] [blame] | 1222 | (card->csd.mmca_vsn >= CSD_SPEC_VER_4) && |
Jarkko Lavinen | b30f8af | 2008-11-17 14:35:21 +0200 | [diff] [blame] | 1223 | (host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA))) { |
Aries Lee | 22113ef | 2010-12-15 08:14:24 +0100 | [diff] [blame] | 1224 | static unsigned ext_csd_bits[][2] = { |
| 1225 | { EXT_CSD_BUS_WIDTH_8, EXT_CSD_DDR_BUS_WIDTH_8 }, |
| 1226 | { EXT_CSD_BUS_WIDTH_4, EXT_CSD_DDR_BUS_WIDTH_4 }, |
| 1227 | { EXT_CSD_BUS_WIDTH_1, EXT_CSD_BUS_WIDTH_1 }, |
| 1228 | }; |
| 1229 | static unsigned bus_widths[] = { |
| 1230 | MMC_BUS_WIDTH_8, |
| 1231 | MMC_BUS_WIDTH_4, |
| 1232 | MMC_BUS_WIDTH_1 |
| 1233 | }; |
| 1234 | unsigned idx, bus_width = 0; |
Jarkko Lavinen | b30f8af | 2008-11-17 14:35:21 +0200 | [diff] [blame] | 1235 | |
Aries Lee | 22113ef | 2010-12-15 08:14:24 +0100 | [diff] [blame] | 1236 | if (host->caps & MMC_CAP_8_BIT_DATA) |
| 1237 | idx = 0; |
| 1238 | else |
| 1239 | idx = 1; |
| 1240 | for (; idx < ARRAY_SIZE(bus_widths); idx++) { |
| 1241 | bus_width = bus_widths[idx]; |
| 1242 | if (bus_width == MMC_BUS_WIDTH_1) |
| 1243 | ddr = 0; /* no DDR for 1-bit width */ |
Girish K S | b87d8db | 2011-09-23 20:41:47 +0530 | [diff] [blame] | 1244 | err = mmc_select_powerclass(card, ext_csd_bits[idx][0], |
| 1245 | ext_csd); |
| 1246 | if (err) |
Subhash Jadavani | 93fc5a4 | 2012-04-03 12:25:58 +0530 | [diff] [blame] | 1247 | pr_warning("%s: power class selection to " |
| 1248 | "bus width %d failed\n", |
| 1249 | mmc_hostname(card->host), |
| 1250 | 1 << bus_width); |
Girish K S | b87d8db | 2011-09-23 20:41:47 +0530 | [diff] [blame] | 1251 | |
Aries Lee | 22113ef | 2010-12-15 08:14:24 +0100 | [diff] [blame] | 1252 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 1253 | EXT_CSD_BUS_WIDTH, |
Andrei Warkentin | d3a8d95 | 2011-04-11 16:13:43 -0500 | [diff] [blame] | 1254 | ext_csd_bits[idx][0], |
Seungwon Jeon | b23cf0b | 2011-09-23 14:15:29 +0900 | [diff] [blame] | 1255 | card->ext_csd.generic_cmd6_time); |
Aries Lee | 22113ef | 2010-12-15 08:14:24 +0100 | [diff] [blame] | 1256 | if (!err) { |
Philip Rakity | 4c4cb17 | 2011-05-13 11:17:18 +0530 | [diff] [blame] | 1257 | mmc_set_bus_width(card->host, bus_width); |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 1258 | |
Aries Lee | 22113ef | 2010-12-15 08:14:24 +0100 | [diff] [blame] | 1259 | /* |
| 1260 | * If controller can't handle bus width test, |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 1261 | * compare ext_csd previously read in 1 bit mode |
| 1262 | * against ext_csd at new bus width |
Aries Lee | 22113ef | 2010-12-15 08:14:24 +0100 | [diff] [blame] | 1263 | */ |
| 1264 | if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST)) |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 1265 | err = mmc_compare_ext_csds(card, |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 1266 | bus_width); |
| 1267 | else |
| 1268 | err = mmc_bus_test(card, bus_width); |
Aries Lee | 22113ef | 2010-12-15 08:14:24 +0100 | [diff] [blame] | 1269 | if (!err) |
| 1270 | break; |
| 1271 | } |
Jarkko Lavinen | b30f8af | 2008-11-17 14:35:21 +0200 | [diff] [blame] | 1272 | } |
| 1273 | |
Aries Lee | 22113ef | 2010-12-15 08:14:24 +0100 | [diff] [blame] | 1274 | if (!err && ddr) { |
Girish K S | b87d8db | 2011-09-23 20:41:47 +0530 | [diff] [blame] | 1275 | err = mmc_select_powerclass(card, ext_csd_bits[idx][1], |
| 1276 | ext_csd); |
| 1277 | if (err) |
Subhash Jadavani | 93fc5a4 | 2012-04-03 12:25:58 +0530 | [diff] [blame] | 1278 | pr_warning("%s: power class selection to " |
| 1279 | "bus width %d ddr %d failed\n", |
| 1280 | mmc_hostname(card->host), |
| 1281 | 1 << bus_width, ddr); |
Girish K S | b87d8db | 2011-09-23 20:41:47 +0530 | [diff] [blame] | 1282 | |
Aries Lee | 22113ef | 2010-12-15 08:14:24 +0100 | [diff] [blame] | 1283 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
Andrei Warkentin | d3a8d95 | 2011-04-11 16:13:43 -0500 | [diff] [blame] | 1284 | EXT_CSD_BUS_WIDTH, |
| 1285 | ext_csd_bits[idx][1], |
Seungwon Jeon | b23cf0b | 2011-09-23 14:15:29 +0900 | [diff] [blame] | 1286 | card->ext_csd.generic_cmd6_time); |
Aries Lee | 22113ef | 2010-12-15 08:14:24 +0100 | [diff] [blame] | 1287 | } |
Adrian Hunter | ef0b27d | 2009-09-22 16:44:37 -0700 | [diff] [blame] | 1288 | if (err) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 1289 | pr_warning("%s: switch to bus width %d ddr %d " |
Aries Lee | 22113ef | 2010-12-15 08:14:24 +0100 | [diff] [blame] | 1290 | "failed\n", mmc_hostname(card->host), |
| 1291 | 1 << bus_width, ddr); |
| 1292 | goto free_card; |
| 1293 | } else if (ddr) { |
Philip Rakity | 4c4cb17 | 2011-05-13 11:17:18 +0530 | [diff] [blame] | 1294 | /* |
| 1295 | * eMMC cards can support 3.3V to 1.2V i/o (vccq) |
| 1296 | * signaling. |
| 1297 | * |
| 1298 | * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq. |
| 1299 | * |
| 1300 | * 1.8V vccq at 3.3V core voltage (vcc) is not required |
| 1301 | * in the JEDEC spec for DDR. |
| 1302 | * |
| 1303 | * Do not force change in vccq since we are obviously |
| 1304 | * working and no change to vccq is needed. |
| 1305 | * |
| 1306 | * WARNING: eMMC rules are NOT the same as SD DDR |
| 1307 | */ |
Girish K S | 913047e | 2011-12-15 17:27:42 +0530 | [diff] [blame] | 1308 | if (ddr == MMC_1_2V_DDR_MODE) { |
Philip Rakity | 4c4cb17 | 2011-05-13 11:17:18 +0530 | [diff] [blame] | 1309 | err = mmc_set_signal_voltage(host, |
| 1310 | MMC_SIGNAL_VOLTAGE_120, 0); |
| 1311 | if (err) |
| 1312 | goto err; |
| 1313 | } |
Aries Lee | 22113ef | 2010-12-15 08:14:24 +0100 | [diff] [blame] | 1314 | mmc_card_set_ddr_mode(card); |
Philip Rakity | 4c4cb17 | 2011-05-13 11:17:18 +0530 | [diff] [blame] | 1315 | mmc_set_timing(card->host, MMC_TIMING_UHS_DDR50); |
| 1316 | mmc_set_bus_width(card->host, bus_width); |
Adrian Hunter | ef0b27d | 2009-09-22 16:44:37 -0700 | [diff] [blame] | 1317 | } |
Pierre Ossman | 89a73cf | 2007-05-01 15:08:30 +0200 | [diff] [blame] | 1318 | } |
| 1319 | |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 1320 | /* |
Subhash Jadavani | 52d0974 | 2012-03-06 17:59:12 +0530 | [diff] [blame] | 1321 | * Enable HPI feature (if supported) |
| 1322 | */ |
| 1323 | if (card->ext_csd.hpi) { |
| 1324 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 1325 | EXT_CSD_HPI_MGMT, 1, |
| 1326 | card->ext_csd.generic_cmd6_time); |
| 1327 | if (err && err != -EBADMSG) |
| 1328 | goto free_card; |
| 1329 | if (err) { |
| 1330 | pr_warning("%s: Enabling HPI failed\n", |
| 1331 | mmc_hostname(card->host)); |
| 1332 | err = 0; |
| 1333 | } else |
| 1334 | card->ext_csd.hpi_en = 1; |
| 1335 | } |
| 1336 | |
| 1337 | /* |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 1338 | * If cache size is higher than 0, this indicates |
| 1339 | * the existence of cache and it can be turned on. |
| 1340 | */ |
| 1341 | if ((host->caps2 & MMC_CAP2_CACHE_CTRL) && |
| 1342 | card->ext_csd.cache_size > 0) { |
| 1343 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
Seungwon Jeon | 8bc0678 | 2011-12-09 17:47:17 +0900 | [diff] [blame] | 1344 | EXT_CSD_CACHE_CTRL, 1, |
| 1345 | card->ext_csd.generic_cmd6_time); |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 1346 | if (err && err != -EBADMSG) |
| 1347 | goto free_card; |
| 1348 | |
| 1349 | /* |
| 1350 | * Only if no error, cache is turned on successfully. |
| 1351 | */ |
Seungwon Jeon | 8bc0678 | 2011-12-09 17:47:17 +0900 | [diff] [blame] | 1352 | if (err) { |
| 1353 | pr_warning("%s: Cache is supported, " |
| 1354 | "but failed to turn on (%d)\n", |
| 1355 | mmc_hostname(card->host), err); |
| 1356 | card->ext_csd.cache_ctrl = 0; |
| 1357 | err = 0; |
| 1358 | } else { |
| 1359 | card->ext_csd.cache_ctrl = 1; |
| 1360 | } |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 1361 | } |
| 1362 | |
Seungwon Jeon | c70e966 | 2012-09-27 16:12:47 +0200 | [diff] [blame] | 1363 | if ((host->caps2 & MMC_CAP2_PACKED_WR && |
| 1364 | card->ext_csd.max_packed_writes > 0) || |
| 1365 | (host->caps2 & MMC_CAP2_PACKED_RD && |
| 1366 | card->ext_csd.max_packed_reads > 0)) { |
| 1367 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 1368 | EXT_CSD_EXP_EVENTS_CTRL, |
| 1369 | EXT_CSD_PACKED_EVENT_EN, |
| 1370 | card->ext_csd.generic_cmd6_time); |
| 1371 | if (err && err != -EBADMSG) |
| 1372 | goto free_card; |
| 1373 | if (err) { |
| 1374 | pr_warning("%s: Enabling packed event failed\n", |
| 1375 | mmc_hostname(card->host)); |
| 1376 | card->ext_csd.packed_event_en = 0; |
| 1377 | err = 0; |
| 1378 | } else { |
| 1379 | card->ext_csd.packed_event_en = 1; |
| 1380 | } |
Tatyana Brokhman | 464fbe1 | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 1381 | |
| 1382 | } |
| 1383 | |
| 1384 | if (!oldcard) { |
| 1385 | if ((host->caps2 & MMC_CAP2_PACKED_CMD) && |
| 1386 | (card->ext_csd.max_packed_writes > 0)) { |
| 1387 | /* |
| 1388 | * We would like to keep the statistics in an index |
| 1389 | * that equals the num of packed requests |
| 1390 | * (1 to max_packed_writes) |
| 1391 | */ |
| 1392 | card->wr_pack_stats.packing_events = kzalloc( |
| 1393 | (card->ext_csd.max_packed_writes + 1) * |
| 1394 | sizeof(*card->wr_pack_stats.packing_events), |
| 1395 | GFP_KERNEL); |
| 1396 | if (!card->wr_pack_stats.packing_events) |
| 1397 | goto free_card; |
| 1398 | } |
Maya Erez | 5f36069 | 2012-10-10 03:47:54 +0200 | [diff] [blame] | 1399 | |
| 1400 | if (card->ext_csd.bkops_en) { |
| 1401 | INIT_DELAYED_WORK(&card->bkops_info.dw, |
| 1402 | mmc_start_idle_time_bkops); |
| 1403 | INIT_WORK(&card->bkops_info.poll_for_completion, |
| 1404 | mmc_bkops_completion_polling); |
| 1405 | |
| 1406 | /* |
| 1407 | * Calculate the time to start the BKOPs checking. |
Maya Erez | 70b0022 | 2012-12-27 15:40:17 +0200 | [diff] [blame] | 1408 | * The host controller can set this time in order to |
| 1409 | * prevent a race condition before starting BKOPs |
| 1410 | * and going into suspend. |
| 1411 | * If the host controller didn't set this time, |
Maya Erez | 5f36069 | 2012-10-10 03:47:54 +0200 | [diff] [blame] | 1412 | * a default value is used. |
| 1413 | */ |
| 1414 | card->bkops_info.delay_ms = MMC_IDLE_BKOPS_TIME_MS; |
Maya Erez | 70b0022 | 2012-12-27 15:40:17 +0200 | [diff] [blame] | 1415 | if (card->bkops_info.host_delay_ms) |
| 1416 | card->bkops_info.delay_ms = |
| 1417 | card->bkops_info.host_delay_ms; |
Maya Erez | 5f36069 | 2012-10-10 03:47:54 +0200 | [diff] [blame] | 1418 | } |
Seungwon Jeon | c70e966 | 2012-09-27 16:12:47 +0200 | [diff] [blame] | 1419 | } |
| 1420 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1421 | if (!oldcard) |
| 1422 | host->card = card; |
| 1423 | |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 1424 | mmc_free_ext_csd(ext_csd); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 1425 | return 0; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1426 | |
| 1427 | free_card: |
| 1428 | if (!oldcard) |
| 1429 | mmc_remove_card(card); |
| 1430 | err: |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 1431 | mmc_free_ext_csd(ext_csd); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1432 | |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 1433 | return err; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1434 | } |
| 1435 | |
Maya Erez | cec02a4 | 2012-10-19 20:18:17 +0200 | [diff] [blame] | 1436 | static int mmc_can_poweroff_notify(const struct mmc_card *card) |
| 1437 | { |
| 1438 | return card && |
| 1439 | mmc_card_mmc(card) && |
| 1440 | (card->ext_csd.power_off_notification == EXT_CSD_POWER_ON); |
| 1441 | } |
| 1442 | |
| 1443 | static int mmc_poweroff_notify(struct mmc_card *card, unsigned int notify_type) |
| 1444 | { |
| 1445 | unsigned int timeout = card->ext_csd.generic_cmd6_time; |
| 1446 | int err; |
| 1447 | |
| 1448 | /* Use EXT_CSD_POWER_OFF_SHORT as default notification type. */ |
| 1449 | if (notify_type == EXT_CSD_POWER_OFF_LONG) |
| 1450 | timeout = card->ext_csd.power_off_longtime; |
| 1451 | |
| 1452 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 1453 | EXT_CSD_POWER_OFF_NOTIFICATION, |
| 1454 | notify_type, timeout); |
| 1455 | if (err) |
| 1456 | pr_err("%s: Power Off Notification timed out, %u\n", |
| 1457 | mmc_hostname(card->host), timeout); |
| 1458 | |
| 1459 | /* Disable the power off notification after the switch operation. */ |
| 1460 | card->ext_csd.power_off_notification = EXT_CSD_NO_POWER_NOTIFICATION; |
| 1461 | |
| 1462 | return err; |
| 1463 | } |
| 1464 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1465 | /* |
| 1466 | * Host is being removed. Free up the current card. |
| 1467 | */ |
| 1468 | static void mmc_remove(struct mmc_host *host) |
| 1469 | { |
| 1470 | BUG_ON(!host); |
| 1471 | BUG_ON(!host->card); |
| 1472 | |
| 1473 | mmc_remove_card(host->card); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1474 | |
| 1475 | mmc_claim_host(host); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1476 | host->card = NULL; |
Sujit Reddy Thumma | 51d57b3 | 2012-10-11 17:17:03 +0530 | [diff] [blame] | 1477 | mmc_exit_clk_scaling(host); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1478 | mmc_release_host(host); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1479 | } |
| 1480 | |
| 1481 | /* |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 1482 | * Card detection - card is alive. |
| 1483 | */ |
| 1484 | static int mmc_alive(struct mmc_host *host) |
| 1485 | { |
| 1486 | return mmc_send_status(host->card, NULL); |
| 1487 | } |
| 1488 | |
| 1489 | /* |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1490 | * Card detection callback from host. |
| 1491 | */ |
| 1492 | static void mmc_detect(struct mmc_host *host) |
| 1493 | { |
| 1494 | int err; |
| 1495 | |
| 1496 | BUG_ON(!host); |
| 1497 | BUG_ON(!host->card); |
| 1498 | |
Asutosh Das | bbc8478 | 2013-02-11 15:31:35 +0530 | [diff] [blame] | 1499 | mmc_rpm_hold(host, &host->card->dev); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1500 | mmc_claim_host(host); |
| 1501 | |
| 1502 | /* |
| 1503 | * Just check if our card has been removed. |
| 1504 | */ |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 1505 | err = _mmc_detect_card_removed(host); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1506 | |
| 1507 | mmc_release_host(host); |
| 1508 | |
Asutosh Das | bbc8478 | 2013-02-11 15:31:35 +0530 | [diff] [blame] | 1509 | /* |
| 1510 | * if detect fails, the device would be removed anyway; |
| 1511 | * the rpm framework would mark the device state suspended. |
| 1512 | */ |
| 1513 | if (!err) |
| 1514 | mmc_rpm_release(host, &host->card->dev); |
| 1515 | |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 1516 | if (err) { |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 1517 | mmc_remove(host); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1518 | |
| 1519 | mmc_claim_host(host); |
| 1520 | mmc_detach_bus(host); |
Ulf Hansson | 7f7e412 | 2011-09-21 14:08:13 -0400 | [diff] [blame] | 1521 | mmc_power_off(host); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1522 | mmc_release_host(host); |
| 1523 | } |
| 1524 | } |
| 1525 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1526 | /* |
| 1527 | * Suspend callback from host. |
| 1528 | */ |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 1529 | static int mmc_suspend(struct mmc_host *host) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1530 | { |
Balaji T K | c380546 | 2011-09-08 22:08:39 +0530 | [diff] [blame] | 1531 | int err = 0; |
| 1532 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1533 | BUG_ON(!host); |
| 1534 | BUG_ON(!host->card); |
| 1535 | |
Sujit Reddy Thumma | 51d57b3 | 2012-10-11 17:17:03 +0530 | [diff] [blame] | 1536 | /* |
| 1537 | * Disable clock scaling before suspend and enable it after resume so |
| 1538 | * as to avoid clock scaling decisions kicking in during this window. |
| 1539 | */ |
| 1540 | mmc_disable_clk_scaling(host); |
| 1541 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1542 | mmc_claim_host(host); |
Maya Erez | 67a5138 | 2012-12-31 16:17:58 +0200 | [diff] [blame] | 1543 | |
| 1544 | err = mmc_cache_ctrl(host, 0); |
| 1545 | if (err) |
| 1546 | goto out; |
| 1547 | |
Maya Erez | cec02a4 | 2012-10-19 20:18:17 +0200 | [diff] [blame] | 1548 | if (mmc_can_poweroff_notify(host->card)) |
| 1549 | err = mmc_poweroff_notify(host->card, EXT_CSD_POWER_OFF_SHORT); |
| 1550 | else if (mmc_card_can_sleep(host)) |
Tatyana Brokhman | 8b458cf | 2012-10-16 08:26:18 +0200 | [diff] [blame] | 1551 | err = mmc_card_sleep(host); |
Maya Erez | cec02a4 | 2012-10-19 20:18:17 +0200 | [diff] [blame] | 1552 | else if (!mmc_host_is_spi(host)) |
Tatyana Brokhman | 8b458cf | 2012-10-16 08:26:18 +0200 | [diff] [blame] | 1553 | mmc_deselect_cards(host); |
| 1554 | host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_200); |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 1555 | |
Maya Erez | 67a5138 | 2012-12-31 16:17:58 +0200 | [diff] [blame] | 1556 | out: |
| 1557 | mmc_release_host(host); |
Balaji T K | c380546 | 2011-09-08 22:08:39 +0530 | [diff] [blame] | 1558 | return err; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1559 | } |
| 1560 | |
| 1561 | /* |
| 1562 | * Resume callback from host. |
| 1563 | * |
| 1564 | * This function tries to determine if the same card is still present |
| 1565 | * and, if so, restore all state to it. |
| 1566 | */ |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 1567 | static int mmc_resume(struct mmc_host *host) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1568 | { |
| 1569 | int err; |
| 1570 | |
| 1571 | BUG_ON(!host); |
| 1572 | BUG_ON(!host->card); |
| 1573 | |
| 1574 | mmc_claim_host(host); |
Krishna Konda | 07d1f38 | 2012-08-11 16:52:41 -0700 | [diff] [blame] | 1575 | err = mmc_init_card(host, host->ocr, host->card); |
Pierre Ossman | 2986d0b | 2007-07-22 17:52:06 +0200 | [diff] [blame] | 1576 | mmc_release_host(host); |
| 1577 | |
Sujit Reddy Thumma | 51d57b3 | 2012-10-11 17:17:03 +0530 | [diff] [blame] | 1578 | /* |
| 1579 | * We have done full initialization of the card, |
| 1580 | * reset the clk scale stats and current frequency. |
| 1581 | */ |
| 1582 | if (mmc_can_scale_clk(host)) |
| 1583 | mmc_init_clk_scaling(host); |
| 1584 | |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 1585 | return err; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1586 | } |
| 1587 | |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 1588 | static int mmc_power_restore(struct mmc_host *host) |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1589 | { |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 1590 | int ret; |
| 1591 | |
Sujit Reddy Thumma | 51d57b3 | 2012-10-11 17:17:03 +0530 | [diff] [blame] | 1592 | /* Disable clk scaling to avoid switching frequencies intermittently */ |
| 1593 | mmc_disable_clk_scaling(host); |
| 1594 | |
Girish K S | 3e73c36 | 2012-01-31 15:44:03 +0530 | [diff] [blame] | 1595 | host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_200); |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1596 | mmc_claim_host(host); |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 1597 | ret = mmc_init_card(host, host->ocr, host->card); |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1598 | mmc_release_host(host); |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 1599 | |
Sujit Reddy Thumma | 51d57b3 | 2012-10-11 17:17:03 +0530 | [diff] [blame] | 1600 | if (mmc_can_scale_clk(host)) |
| 1601 | mmc_init_clk_scaling(host); |
| 1602 | |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 1603 | return ret; |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1604 | } |
| 1605 | |
Jarkko Lavinen | b1ebe38 | 2009-09-22 16:44:34 -0700 | [diff] [blame] | 1606 | static int mmc_sleep(struct mmc_host *host) |
| 1607 | { |
| 1608 | struct mmc_card *card = host->card; |
| 1609 | int err = -ENOSYS; |
| 1610 | |
| 1611 | if (card && card->ext_csd.rev >= 3) { |
| 1612 | err = mmc_card_sleepawake(host, 1); |
| 1613 | if (err < 0) |
Sujit Reddy Thumma | f43ab43 | 2012-06-19 14:18:27 +0530 | [diff] [blame] | 1614 | pr_warn("%s: Error %d while putting card into sleep", |
Jarkko Lavinen | b1ebe38 | 2009-09-22 16:44:34 -0700 | [diff] [blame] | 1615 | mmc_hostname(host), err); |
| 1616 | } |
| 1617 | |
| 1618 | return err; |
| 1619 | } |
| 1620 | |
| 1621 | static int mmc_awake(struct mmc_host *host) |
| 1622 | { |
| 1623 | struct mmc_card *card = host->card; |
| 1624 | int err = -ENOSYS; |
| 1625 | |
| 1626 | if (card && card->ext_csd.rev >= 3) { |
| 1627 | err = mmc_card_sleepawake(host, 0); |
| 1628 | if (err < 0) |
| 1629 | pr_debug("%s: Error %d while awaking sleeping card", |
| 1630 | mmc_hostname(host), err); |
| 1631 | } |
| 1632 | |
| 1633 | return err; |
| 1634 | } |
| 1635 | |
Adrian Hunter | 9feae24 | 2009-09-22 16:44:32 -0700 | [diff] [blame] | 1636 | static const struct mmc_bus_ops mmc_ops = { |
Jarkko Lavinen | b1ebe38 | 2009-09-22 16:44:34 -0700 | [diff] [blame] | 1637 | .awake = mmc_awake, |
| 1638 | .sleep = mmc_sleep, |
Adrian Hunter | 9feae24 | 2009-09-22 16:44:32 -0700 | [diff] [blame] | 1639 | .remove = mmc_remove, |
| 1640 | .detect = mmc_detect, |
| 1641 | .suspend = NULL, |
| 1642 | .resume = NULL, |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1643 | .power_restore = mmc_power_restore, |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 1644 | .alive = mmc_alive, |
Sujit Reddy Thumma | 1cd61b7 | 2012-08-06 11:23:21 +0530 | [diff] [blame] | 1645 | .change_bus_speed = mmc_change_bus_speed, |
Adrian Hunter | 9feae24 | 2009-09-22 16:44:32 -0700 | [diff] [blame] | 1646 | }; |
| 1647 | |
| 1648 | static const struct mmc_bus_ops mmc_ops_unsafe = { |
Jarkko Lavinen | b1ebe38 | 2009-09-22 16:44:34 -0700 | [diff] [blame] | 1649 | .awake = mmc_awake, |
| 1650 | .sleep = mmc_sleep, |
Adrian Hunter | 9feae24 | 2009-09-22 16:44:32 -0700 | [diff] [blame] | 1651 | .remove = mmc_remove, |
| 1652 | .detect = mmc_detect, |
| 1653 | .suspend = mmc_suspend, |
| 1654 | .resume = mmc_resume, |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1655 | .power_restore = mmc_power_restore, |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 1656 | .alive = mmc_alive, |
Sujit Reddy Thumma | 1cd61b7 | 2012-08-06 11:23:21 +0530 | [diff] [blame] | 1657 | .change_bus_speed = mmc_change_bus_speed, |
Adrian Hunter | 9feae24 | 2009-09-22 16:44:32 -0700 | [diff] [blame] | 1658 | }; |
| 1659 | |
| 1660 | static void mmc_attach_bus_ops(struct mmc_host *host) |
| 1661 | { |
| 1662 | const struct mmc_bus_ops *bus_ops; |
| 1663 | |
Matt Fleming | 71d7d3d | 2010-09-27 09:42:19 +0100 | [diff] [blame] | 1664 | if (!mmc_card_is_removable(host)) |
Adrian Hunter | 9feae24 | 2009-09-22 16:44:32 -0700 | [diff] [blame] | 1665 | bus_ops = &mmc_ops_unsafe; |
| 1666 | else |
| 1667 | bus_ops = &mmc_ops; |
| 1668 | mmc_attach_bus(host, bus_ops); |
| 1669 | } |
| 1670 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1671 | /* |
| 1672 | * Starting point for MMC card init. |
| 1673 | */ |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 1674 | int mmc_attach_mmc(struct mmc_host *host) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1675 | { |
| 1676 | int err; |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 1677 | u32 ocr; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1678 | |
| 1679 | BUG_ON(!host); |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 1680 | WARN_ON(!host->claimed); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1681 | |
Stefan Nilsson XK | 4466903 | 2011-09-15 17:50:38 +0200 | [diff] [blame] | 1682 | /* Set correct bus mode for MMC before attempting attach */ |
| 1683 | if (!mmc_host_is_spi(host)) |
| 1684 | mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN); |
| 1685 | |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 1686 | err = mmc_send_op_cond(host, 0, &ocr); |
| 1687 | if (err) |
| 1688 | return err; |
| 1689 | |
Adrian Hunter | 9feae24 | 2009-09-22 16:44:32 -0700 | [diff] [blame] | 1690 | mmc_attach_bus_ops(host); |
Takashi Iwai | 8f230f4 | 2010-12-08 10:04:30 +0100 | [diff] [blame] | 1691 | if (host->ocr_avail_mmc) |
| 1692 | host->ocr_avail = host->ocr_avail_mmc; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1693 | |
| 1694 | /* |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 1695 | * We need to get OCR a different way for SPI. |
| 1696 | */ |
| 1697 | if (mmc_host_is_spi(host)) { |
| 1698 | err = mmc_spi_read_ocr(host, 1, &ocr); |
| 1699 | if (err) |
| 1700 | goto err; |
| 1701 | } |
| 1702 | |
| 1703 | /* |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1704 | * Sanity check the voltages that the card claims to |
| 1705 | * support. |
| 1706 | */ |
| 1707 | if (ocr & 0x7F) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 1708 | pr_warning("%s: card claims to support voltages " |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1709 | "below the defined range. These will be ignored.\n", |
| 1710 | mmc_hostname(host)); |
| 1711 | ocr &= ~0x7F; |
| 1712 | } |
| 1713 | |
| 1714 | host->ocr = mmc_select_voltage(host, ocr); |
| 1715 | |
| 1716 | /* |
| 1717 | * Can we support the voltage of the card? |
| 1718 | */ |
Pierre Ossman | 109b5be | 2007-07-23 00:12:10 +0200 | [diff] [blame] | 1719 | if (!host->ocr) { |
| 1720 | err = -EINVAL; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1721 | goto err; |
Pierre Ossman | 109b5be | 2007-07-23 00:12:10 +0200 | [diff] [blame] | 1722 | } |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1723 | |
| 1724 | /* |
| 1725 | * Detect and init the card. |
| 1726 | */ |
Pierre Ossman | 8c75dea | 2007-05-19 16:14:43 +0200 | [diff] [blame] | 1727 | err = mmc_init_card(host, host->ocr, NULL); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 1728 | if (err) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1729 | goto err; |
| 1730 | |
| 1731 | mmc_release_host(host); |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 1732 | err = mmc_add_card(host->card); |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 1733 | mmc_claim_host(host); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1734 | if (err) |
Pierre Ossman | 2986d0b | 2007-07-22 17:52:06 +0200 | [diff] [blame] | 1735 | goto remove_card; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1736 | |
Sujit Reddy Thumma | 1ed78ab | 2013-04-25 12:39:40 +0530 | [diff] [blame] | 1737 | mmc_init_clk_scaling(host); |
Sujit Reddy Thumma | 51d57b3 | 2012-10-11 17:17:03 +0530 | [diff] [blame] | 1738 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1739 | return 0; |
| 1740 | |
Pierre Ossman | 2986d0b | 2007-07-22 17:52:06 +0200 | [diff] [blame] | 1741 | remove_card: |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 1742 | mmc_release_host(host); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1743 | mmc_remove_card(host->card); |
Pierre Ossman | 2986d0b | 2007-07-22 17:52:06 +0200 | [diff] [blame] | 1744 | mmc_claim_host(host); |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 1745 | host->card = NULL; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1746 | err: |
| 1747 | mmc_detach_bus(host); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1748 | |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 1749 | pr_err("%s: error %d whilst initialising MMC card\n", |
Pierre Ossman | 109b5be | 2007-07-23 00:12:10 +0200 | [diff] [blame] | 1750 | mmc_hostname(host), err); |
| 1751 | |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 1752 | return err; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1753 | } |