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