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