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