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 | |
| 238 | /* |
| 239 | * Decode extended CSD. |
| 240 | */ |
| 241 | static int mmc_read_ext_csd(struct mmc_card *card, u8 *ext_csd) |
| 242 | { |
Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 243 | int err = 0, idx; |
| 244 | unsigned int part_size; |
| 245 | u8 hc_erase_grp_sz = 0, hc_wp_grp_sz = 0; |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 246 | |
| 247 | BUG_ON(!card); |
| 248 | |
| 249 | if (!ext_csd) |
| 250 | return 0; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 251 | |
Kyungmin Park | 6da24b7 | 2010-08-10 18:01:36 -0700 | [diff] [blame] | 252 | /* 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] | 253 | card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE]; |
Kyungmin Park | 6da24b7 | 2010-08-10 18:01:36 -0700 | [diff] [blame] | 254 | if (card->csd.structure == 3) { |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 255 | if (card->ext_csd.raw_ext_csd_structure > 2) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 256 | pr_err("%s: unrecognised EXT_CSD structure " |
Kyungmin Park | 6da24b7 | 2010-08-10 18:01:36 -0700 | [diff] [blame] | 257 | "version %d\n", mmc_hostname(card->host), |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 258 | card->ext_csd.raw_ext_csd_structure); |
Kyungmin Park | 6da24b7 | 2010-08-10 18:01:36 -0700 | [diff] [blame] | 259 | err = -EINVAL; |
| 260 | goto out; |
| 261 | } |
| 262 | } |
| 263 | |
Jarkko Lavinen | b1ebe38 | 2009-09-22 16:44:34 -0700 | [diff] [blame] | 264 | card->ext_csd.rev = ext_csd[EXT_CSD_REV]; |
Kyungmin Park | 38ca285 | 2011-07-26 17:12:37 +0900 | [diff] [blame] | 265 | if (card->ext_csd.rev > 6) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 266 | pr_err("%s: unrecognised EXT_CSD revision %d\n", |
Kyungmin Park | 6da24b7 | 2010-08-10 18:01:36 -0700 | [diff] [blame] | 267 | mmc_hostname(card->host), card->ext_csd.rev); |
Florin Malita | 00cedfa | 2007-10-13 12:27:20 -0400 | [diff] [blame] | 268 | err = -EINVAL; |
| 269 | goto out; |
Pierre Ossman | d7604d7 | 2007-07-23 00:34:07 +0200 | [diff] [blame] | 270 | } |
| 271 | |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 272 | card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0]; |
| 273 | card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1]; |
| 274 | card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2]; |
| 275 | 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] | 276 | if (card->ext_csd.rev >= 2) { |
Pierre Ossman | d7604d7 | 2007-07-23 00:34:07 +0200 | [diff] [blame] | 277 | card->ext_csd.sectors = |
| 278 | ext_csd[EXT_CSD_SEC_CNT + 0] << 0 | |
| 279 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8 | |
| 280 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16 | |
| 281 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24; |
Hanumath Prasad | fc8a098 | 2010-08-10 18:01:45 -0700 | [diff] [blame] | 282 | |
| 283 | /* Cards with density > 2GiB are sector addressed */ |
| 284 | if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512) |
Pierre Ossman | d7604d7 | 2007-07-23 00:34:07 +0200 | [diff] [blame] | 285 | mmc_card_set_blockaddr(card); |
| 286 | } |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 287 | card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE]; |
Adrian Hunter | 7198f3c | 2010-03-23 13:35:40 -0700 | [diff] [blame] | 288 | switch (ext_csd[EXT_CSD_CARD_TYPE] & EXT_CSD_CARD_TYPE_MASK) { |
Hanumath Prasad | dfc13e8 | 2010-09-30 17:37:23 -0400 | [diff] [blame] | 289 | case EXT_CSD_CARD_TYPE_DDR_52 | EXT_CSD_CARD_TYPE_52 | |
| 290 | EXT_CSD_CARD_TYPE_26: |
| 291 | card->ext_csd.hs_max_dtr = 52000000; |
| 292 | card->ext_csd.card_type = EXT_CSD_CARD_TYPE_DDR_52; |
| 293 | break; |
| 294 | case EXT_CSD_CARD_TYPE_DDR_1_2V | EXT_CSD_CARD_TYPE_52 | |
| 295 | EXT_CSD_CARD_TYPE_26: |
| 296 | card->ext_csd.hs_max_dtr = 52000000; |
| 297 | card->ext_csd.card_type = EXT_CSD_CARD_TYPE_DDR_1_2V; |
| 298 | break; |
| 299 | case EXT_CSD_CARD_TYPE_DDR_1_8V | EXT_CSD_CARD_TYPE_52 | |
| 300 | EXT_CSD_CARD_TYPE_26: |
| 301 | card->ext_csd.hs_max_dtr = 52000000; |
| 302 | card->ext_csd.card_type = EXT_CSD_CARD_TYPE_DDR_1_8V; |
| 303 | break; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 304 | case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26: |
| 305 | card->ext_csd.hs_max_dtr = 52000000; |
| 306 | break; |
| 307 | case EXT_CSD_CARD_TYPE_26: |
| 308 | card->ext_csd.hs_max_dtr = 26000000; |
| 309 | break; |
| 310 | default: |
| 311 | /* MMC v4 spec says this cannot happen */ |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 312 | pr_warning("%s: card is mmc v4 but doesn't " |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 313 | "support any high-speed modes.\n", |
| 314 | mmc_hostname(card->host)); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 315 | } |
| 316 | |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 317 | card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT]; |
| 318 | card->ext_csd.raw_erase_timeout_mult = |
| 319 | ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]; |
| 320 | card->ext_csd.raw_hc_erase_grp_size = |
| 321 | ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]; |
Jarkko Lavinen | b1ebe38 | 2009-09-22 16:44:34 -0700 | [diff] [blame] | 322 | if (card->ext_csd.rev >= 3) { |
| 323 | u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT]; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 324 | card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG]; |
| 325 | |
| 326 | /* EXT_CSD value is in units of 10ms, but we store in ms */ |
| 327 | 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] | 328 | |
| 329 | /* Sleep / awake timeout in 100ns units */ |
| 330 | if (sa_shift > 0 && sa_shift <= 0x17) |
| 331 | card->ext_csd.sa_timeout = |
| 332 | 1 << ext_csd[EXT_CSD_S_A_TIMEOUT]; |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 333 | card->ext_csd.erase_group_def = |
| 334 | ext_csd[EXT_CSD_ERASE_GROUP_DEF]; |
| 335 | card->ext_csd.hc_erase_timeout = 300 * |
| 336 | ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]; |
| 337 | card->ext_csd.hc_erase_size = |
| 338 | ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10; |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 339 | |
| 340 | 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] | 341 | |
| 342 | /* |
| 343 | * There are two boot regions of equal size, defined in |
| 344 | * multiples of 128K. |
| 345 | */ |
Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 346 | if (ext_csd[EXT_CSD_BOOT_MULT] && mmc_boot_partition_access(card->host)) { |
| 347 | for (idx = 0; idx < MMC_NUM_BOOT_PARTITION; idx++) { |
| 348 | part_size = ext_csd[EXT_CSD_BOOT_MULT] << 17; |
| 349 | mmc_part_add(card, part_size, |
| 350 | EXT_CSD_PART_CONFIG_ACC_BOOT0 + idx, |
| 351 | "boot%d", idx, true); |
| 352 | } |
| 353 | } |
Jarkko Lavinen | b1ebe38 | 2009-09-22 16:44:34 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 356 | card->ext_csd.raw_hc_erase_gap_size = |
| 357 | ext_csd[EXT_CSD_PARTITION_ATTRIBUTE]; |
| 358 | card->ext_csd.raw_sec_trim_mult = |
| 359 | ext_csd[EXT_CSD_SEC_TRIM_MULT]; |
| 360 | card->ext_csd.raw_sec_erase_mult = |
| 361 | ext_csd[EXT_CSD_SEC_ERASE_MULT]; |
| 362 | card->ext_csd.raw_sec_feature_support = |
| 363 | ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]; |
| 364 | card->ext_csd.raw_trim_mult = |
| 365 | ext_csd[EXT_CSD_TRIM_MULT]; |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 366 | if (card->ext_csd.rev >= 4) { |
Chuanxiao Dong | 709de99 | 2011-01-22 04:09:41 +0800 | [diff] [blame] | 367 | /* |
| 368 | * Enhanced area feature support -- check whether the eMMC |
| 369 | * card has the Enhanced area enabled. If so, export enhanced |
| 370 | * area offset and size to user by adding sysfs interface. |
| 371 | */ |
Andrei Warkentin | 5238acb | 2011-09-24 12:12:30 -0400 | [diff] [blame] | 372 | card->ext_csd.raw_partition_support = ext_csd[EXT_CSD_PARTITION_SUPPORT]; |
Chuanxiao Dong | 709de99 | 2011-01-22 04:09:41 +0800 | [diff] [blame] | 373 | if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) && |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 374 | (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) { |
Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 375 | hc_erase_grp_sz = |
Chuanxiao Dong | 709de99 | 2011-01-22 04:09:41 +0800 | [diff] [blame] | 376 | ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]; |
Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 377 | hc_wp_grp_sz = |
Chuanxiao Dong | 709de99 | 2011-01-22 04:09:41 +0800 | [diff] [blame] | 378 | ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; |
| 379 | |
| 380 | card->ext_csd.enhanced_area_en = 1; |
| 381 | /* |
| 382 | * calculate the enhanced data area offset, in bytes |
| 383 | */ |
| 384 | card->ext_csd.enhanced_area_offset = |
| 385 | (ext_csd[139] << 24) + (ext_csd[138] << 16) + |
| 386 | (ext_csd[137] << 8) + ext_csd[136]; |
| 387 | if (mmc_card_blockaddr(card)) |
| 388 | card->ext_csd.enhanced_area_offset <<= 9; |
| 389 | /* |
| 390 | * calculate the enhanced data area size, in kilobytes |
| 391 | */ |
| 392 | card->ext_csd.enhanced_area_size = |
| 393 | (ext_csd[142] << 16) + (ext_csd[141] << 8) + |
| 394 | ext_csd[140]; |
| 395 | card->ext_csd.enhanced_area_size *= |
| 396 | (size_t)(hc_erase_grp_sz * hc_wp_grp_sz); |
| 397 | card->ext_csd.enhanced_area_size <<= 9; |
| 398 | } else { |
| 399 | /* |
| 400 | * If the enhanced area is not enabled, disable these |
| 401 | * device attributes. |
| 402 | */ |
| 403 | card->ext_csd.enhanced_area_offset = -EINVAL; |
| 404 | card->ext_csd.enhanced_area_size = -EINVAL; |
| 405 | } |
Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 406 | |
| 407 | /* |
| 408 | * General purpose partition feature support -- |
| 409 | * If ext_csd has the size of general purpose partitions, |
| 410 | * set size, part_cfg, partition name in mmc_part. |
| 411 | */ |
| 412 | if (ext_csd[EXT_CSD_PARTITION_SUPPORT] & |
| 413 | EXT_CSD_PART_SUPPORT_PART_EN) { |
| 414 | if (card->ext_csd.enhanced_area_en != 1) { |
| 415 | hc_erase_grp_sz = |
| 416 | ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]; |
| 417 | hc_wp_grp_sz = |
| 418 | ext_csd[EXT_CSD_HC_WP_GRP_SIZE]; |
| 419 | |
| 420 | card->ext_csd.enhanced_area_en = 1; |
| 421 | } |
| 422 | |
| 423 | for (idx = 0; idx < MMC_NUM_GP_PARTITION; idx++) { |
| 424 | if (!ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3] && |
| 425 | !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1] && |
| 426 | !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2]) |
| 427 | continue; |
| 428 | part_size = |
| 429 | (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2] |
| 430 | << 16) + |
| 431 | (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1] |
| 432 | << 8) + |
| 433 | ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3]; |
| 434 | part_size *= (size_t)(hc_erase_grp_sz * |
| 435 | hc_wp_grp_sz); |
| 436 | mmc_part_add(card, part_size << 19, |
| 437 | EXT_CSD_PART_CONFIG_ACC_GP0 + idx, |
| 438 | "gp%d", idx, false); |
| 439 | } |
| 440 | } |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 441 | card->ext_csd.sec_trim_mult = |
| 442 | ext_csd[EXT_CSD_SEC_TRIM_MULT]; |
| 443 | card->ext_csd.sec_erase_mult = |
| 444 | ext_csd[EXT_CSD_SEC_ERASE_MULT]; |
| 445 | card->ext_csd.sec_feature_support = |
| 446 | ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]; |
| 447 | card->ext_csd.trim_timeout = 300 * |
| 448 | ext_csd[EXT_CSD_TRIM_MULT]; |
| 449 | } |
| 450 | |
Adrian Hunter | b249951 | 2011-08-29 16:42:11 +0300 | [diff] [blame] | 451 | if (card->ext_csd.rev >= 5) { |
Jaehoon Chung | eb0d8f1 | 2011-10-18 01:26:42 -0400 | [diff] [blame] | 452 | /* check whether the eMMC card supports HPI */ |
| 453 | if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x1) { |
| 454 | card->ext_csd.hpi = 1; |
| 455 | if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x2) |
| 456 | card->ext_csd.hpi_cmd = MMC_STOP_TRANSMISSION; |
| 457 | else |
| 458 | card->ext_csd.hpi_cmd = MMC_SEND_STATUS; |
| 459 | /* |
| 460 | * Indicate the maximum timeout to close |
| 461 | * a command interrupted by HPI |
| 462 | */ |
| 463 | card->ext_csd.out_of_int_time = |
| 464 | ext_csd[EXT_CSD_OUT_OF_INTERRUPT_TIME] * 10; |
| 465 | } |
| 466 | |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 467 | card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM]; |
Adrian Hunter | b249951 | 2011-08-29 16:42:11 +0300 | [diff] [blame] | 468 | card->ext_csd.rst_n_function = ext_csd[EXT_CSD_RST_N_FUNCTION]; |
| 469 | } |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 470 | |
Andrei Warkentin | 5238acb | 2011-09-24 12:12:30 -0400 | [diff] [blame] | 471 | 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] | 472 | if (ext_csd[EXT_CSD_ERASED_MEM_CONT]) |
| 473 | card->erased_byte = 0xFF; |
| 474 | else |
| 475 | card->erased_byte = 0x0; |
| 476 | |
Seungwon Jeon | 336c716 | 2011-10-25 09:43:12 +0900 | [diff] [blame] | 477 | /* eMMC v4.5 or later */ |
Girish K S | bec8726 | 2011-10-13 12:04:16 +0530 | [diff] [blame] | 478 | if (card->ext_csd.rev >= 6) { |
Seungwon Jeon | 336c716 | 2011-10-25 09:43:12 +0900 | [diff] [blame] | 479 | card->ext_csd.feature_support |= MMC_DISCARD_FEATURE; |
| 480 | |
Seungwon Jeon | b23cf0b | 2011-09-23 14:15:29 +0900 | [diff] [blame] | 481 | card->ext_csd.generic_cmd6_time = 10 * |
| 482 | ext_csd[EXT_CSD_GENERIC_CMD6_TIME]; |
Girish K S | bec8726 | 2011-10-13 12:04:16 +0530 | [diff] [blame] | 483 | card->ext_csd.power_off_longtime = 10 * |
| 484 | ext_csd[EXT_CSD_POWER_OFF_LONG_TIME]; |
Seungwon Jeon | b23cf0b | 2011-09-23 14:15:29 +0900 | [diff] [blame] | 485 | |
Seungwon Jeon | 336c716 | 2011-10-25 09:43:12 +0900 | [diff] [blame] | 486 | card->ext_csd.cache_size = |
| 487 | ext_csd[EXT_CSD_CACHE_SIZE + 0] << 0 | |
| 488 | ext_csd[EXT_CSD_CACHE_SIZE + 1] << 8 | |
| 489 | ext_csd[EXT_CSD_CACHE_SIZE + 2] << 16 | |
| 490 | ext_csd[EXT_CSD_CACHE_SIZE + 3] << 24; |
| 491 | } |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 492 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 493 | out: |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 494 | return err; |
| 495 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 496 | |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 497 | static inline void mmc_free_ext_csd(u8 *ext_csd) |
| 498 | { |
| 499 | kfree(ext_csd); |
| 500 | } |
| 501 | |
| 502 | |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 503 | 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] | 504 | { |
| 505 | u8 *bw_ext_csd; |
| 506 | int err; |
| 507 | |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 508 | if (bus_width == MMC_BUS_WIDTH_1) |
| 509 | return 0; |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 510 | |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 511 | err = mmc_get_ext_csd(card, &bw_ext_csd); |
| 512 | |
| 513 | if (err || bw_ext_csd == NULL) { |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 514 | if (bus_width != MMC_BUS_WIDTH_1) |
| 515 | err = -EINVAL; |
| 516 | goto out; |
| 517 | } |
| 518 | |
| 519 | if (bus_width == MMC_BUS_WIDTH_1) |
| 520 | goto out; |
| 521 | |
| 522 | /* only compare read only fields */ |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 523 | err = (!(card->ext_csd.raw_partition_support == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 524 | bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) && |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 525 | (card->ext_csd.raw_erased_mem_count == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 526 | bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) && |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 527 | (card->ext_csd.rev == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 528 | bw_ext_csd[EXT_CSD_REV]) && |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 529 | (card->ext_csd.raw_ext_csd_structure == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 530 | bw_ext_csd[EXT_CSD_STRUCTURE]) && |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 531 | (card->ext_csd.raw_card_type == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 532 | bw_ext_csd[EXT_CSD_CARD_TYPE]) && |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 533 | (card->ext_csd.raw_s_a_timeout == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 534 | bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) && |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 535 | (card->ext_csd.raw_hc_erase_gap_size == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 536 | bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) && |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 537 | (card->ext_csd.raw_erase_timeout_mult == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 538 | bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) && |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 539 | (card->ext_csd.raw_hc_erase_grp_size == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 540 | bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) && |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 541 | (card->ext_csd.raw_sec_trim_mult == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 542 | bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) && |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 543 | (card->ext_csd.raw_sec_erase_mult == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 544 | bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) && |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 545 | (card->ext_csd.raw_sec_feature_support == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 546 | bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) && |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 547 | (card->ext_csd.raw_trim_mult == |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 548 | bw_ext_csd[EXT_CSD_TRIM_MULT]) && |
Philip Rakity | f39b2dd | 2011-07-07 09:04:55 -0700 | [diff] [blame] | 549 | (card->ext_csd.raw_sectors[0] == |
| 550 | bw_ext_csd[EXT_CSD_SEC_CNT + 0]) && |
| 551 | (card->ext_csd.raw_sectors[1] == |
| 552 | bw_ext_csd[EXT_CSD_SEC_CNT + 1]) && |
| 553 | (card->ext_csd.raw_sectors[2] == |
| 554 | bw_ext_csd[EXT_CSD_SEC_CNT + 2]) && |
| 555 | (card->ext_csd.raw_sectors[3] == |
| 556 | bw_ext_csd[EXT_CSD_SEC_CNT + 3])); |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 557 | if (err) |
| 558 | err = -EINVAL; |
| 559 | |
| 560 | out: |
| 561 | mmc_free_ext_csd(bw_ext_csd); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 562 | return err; |
| 563 | } |
| 564 | |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 565 | MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1], |
| 566 | card->raw_cid[2], card->raw_cid[3]); |
| 567 | MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1], |
| 568 | card->raw_csd[2], card->raw_csd[3]); |
| 569 | 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] | 570 | MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9); |
| 571 | MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9); |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 572 | MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev); |
| 573 | MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev); |
| 574 | MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid); |
| 575 | MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name); |
| 576 | MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid); |
| 577 | MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial); |
Chuanxiao Dong | 709de99 | 2011-01-22 04:09:41 +0800 | [diff] [blame] | 578 | MMC_DEV_ATTR(enhanced_area_offset, "%llu\n", |
| 579 | card->ext_csd.enhanced_area_offset); |
| 580 | 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] | 581 | |
| 582 | static struct attribute *mmc_std_attrs[] = { |
| 583 | &dev_attr_cid.attr, |
| 584 | &dev_attr_csd.attr, |
| 585 | &dev_attr_date.attr, |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 586 | &dev_attr_erase_size.attr, |
| 587 | &dev_attr_preferred_erase_size.attr, |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 588 | &dev_attr_fwrev.attr, |
| 589 | &dev_attr_hwrev.attr, |
| 590 | &dev_attr_manfid.attr, |
| 591 | &dev_attr_name.attr, |
| 592 | &dev_attr_oemid.attr, |
| 593 | &dev_attr_serial.attr, |
Chuanxiao Dong | 709de99 | 2011-01-22 04:09:41 +0800 | [diff] [blame] | 594 | &dev_attr_enhanced_area_offset.attr, |
| 595 | &dev_attr_enhanced_area_size.attr, |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 596 | NULL, |
| 597 | }; |
| 598 | |
| 599 | static struct attribute_group mmc_std_attr_group = { |
| 600 | .attrs = mmc_std_attrs, |
| 601 | }; |
| 602 | |
David Brownell | a4dbd67 | 2009-06-24 10:06:31 -0700 | [diff] [blame] | 603 | static const struct attribute_group *mmc_attr_groups[] = { |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 604 | &mmc_std_attr_group, |
| 605 | NULL, |
| 606 | }; |
| 607 | |
| 608 | static struct device_type mmc_type = { |
| 609 | .groups = mmc_attr_groups, |
| 610 | }; |
| 611 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 612 | /* |
Girish K S | b87d8db | 2011-09-23 20:41:47 +0530 | [diff] [blame] | 613 | * Select the PowerClass for the current bus width |
| 614 | * If power class is defined for 4/8 bit bus in the |
| 615 | * extended CSD register, select it by executing the |
| 616 | * mmc_switch command. |
| 617 | */ |
| 618 | static int mmc_select_powerclass(struct mmc_card *card, |
| 619 | unsigned int bus_width, u8 *ext_csd) |
| 620 | { |
| 621 | int err = 0; |
| 622 | unsigned int pwrclass_val; |
| 623 | unsigned int index = 0; |
| 624 | struct mmc_host *host; |
| 625 | |
| 626 | BUG_ON(!card); |
| 627 | |
| 628 | host = card->host; |
| 629 | BUG_ON(!host); |
| 630 | |
| 631 | if (ext_csd == NULL) |
| 632 | return 0; |
| 633 | |
| 634 | /* Power class selection is supported for versions >= 4.0 */ |
| 635 | if (card->csd.mmca_vsn < CSD_SPEC_VER_4) |
| 636 | return 0; |
| 637 | |
| 638 | /* Power class values are defined only for 4/8 bit bus */ |
| 639 | if (bus_width == EXT_CSD_BUS_WIDTH_1) |
| 640 | return 0; |
| 641 | |
| 642 | switch (1 << host->ios.vdd) { |
| 643 | case MMC_VDD_165_195: |
| 644 | if (host->ios.clock <= 26000000) |
| 645 | index = EXT_CSD_PWR_CL_26_195; |
| 646 | else if (host->ios.clock <= 52000000) |
| 647 | index = (bus_width <= EXT_CSD_BUS_WIDTH_8) ? |
| 648 | EXT_CSD_PWR_CL_52_195 : |
| 649 | EXT_CSD_PWR_CL_DDR_52_195; |
| 650 | else if (host->ios.clock <= 200000000) |
| 651 | index = EXT_CSD_PWR_CL_200_195; |
| 652 | break; |
| 653 | case MMC_VDD_32_33: |
| 654 | case MMC_VDD_33_34: |
| 655 | case MMC_VDD_34_35: |
| 656 | case MMC_VDD_35_36: |
| 657 | if (host->ios.clock <= 26000000) |
| 658 | index = EXT_CSD_PWR_CL_26_360; |
| 659 | else if (host->ios.clock <= 52000000) |
| 660 | index = (bus_width <= EXT_CSD_BUS_WIDTH_8) ? |
| 661 | EXT_CSD_PWR_CL_52_360 : |
| 662 | EXT_CSD_PWR_CL_DDR_52_360; |
| 663 | else if (host->ios.clock <= 200000000) |
| 664 | index = EXT_CSD_PWR_CL_200_360; |
| 665 | break; |
| 666 | default: |
| 667 | pr_warning("%s: Voltage range not supported " |
| 668 | "for power class.\n", mmc_hostname(host)); |
| 669 | return -EINVAL; |
| 670 | } |
| 671 | |
| 672 | pwrclass_val = ext_csd[index]; |
| 673 | |
| 674 | if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8)) |
| 675 | pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >> |
| 676 | EXT_CSD_PWR_CL_8BIT_SHIFT; |
| 677 | else |
| 678 | pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >> |
| 679 | EXT_CSD_PWR_CL_4BIT_SHIFT; |
| 680 | |
| 681 | /* If the power class is different from the default value */ |
| 682 | if (pwrclass_val > 0) { |
| 683 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 684 | EXT_CSD_POWER_CLASS, |
| 685 | pwrclass_val, |
Seungwon Jeon | 71fe3eb | 2011-10-18 13:20:57 +0900 | [diff] [blame] | 686 | card->ext_csd.generic_cmd6_time); |
Girish K S | b87d8db | 2011-09-23 20:41:47 +0530 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | return err; |
| 690 | } |
| 691 | |
| 692 | /* |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 693 | * Handle the detection and initialisation of a card. |
| 694 | * |
Deepak Saxena | 8769392 | 2008-06-16 19:20:57 -0700 | [diff] [blame] | 695 | * In the case of a resume, "oldcard" will contain the card |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 696 | * we're trying to reinitialise. |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 697 | */ |
Pierre Ossman | 8c75dea | 2007-05-19 16:14:43 +0200 | [diff] [blame] | 698 | static int mmc_init_card(struct mmc_host *host, u32 ocr, |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 699 | struct mmc_card *oldcard) |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 700 | { |
| 701 | struct mmc_card *card; |
Philip Rakity | 25d5c69 | 2010-11-07 16:22:28 -0500 | [diff] [blame] | 702 | int err, ddr = 0; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 703 | u32 cid[4]; |
| 704 | unsigned int max_dtr; |
Philip Rakity | b676f03 | 2011-02-13 23:13:09 -0800 | [diff] [blame] | 705 | u32 rocr; |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 706 | u8 *ext_csd = NULL; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 707 | |
| 708 | BUG_ON(!host); |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 709 | WARN_ON(!host->claimed); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 710 | |
Stefan Nilsson XK | 4466903 | 2011-09-15 17:50:38 +0200 | [diff] [blame] | 711 | /* Set correct bus mode for MMC before attempting init */ |
| 712 | if (!mmc_host_is_spi(host)) |
| 713 | mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN); |
| 714 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 715 | /* |
| 716 | * Since we're changing the OCR value, we seem to |
| 717 | * need to tell some cards to go back to the idle |
| 718 | * state. We wait 1ms to give cards time to |
| 719 | * respond. |
Balaji T K | c380546 | 2011-09-08 22:08:39 +0530 | [diff] [blame] | 720 | * mmc_go_idle is needed for eMMC that are asleep |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 721 | */ |
| 722 | mmc_go_idle(host); |
| 723 | |
| 724 | /* The extra bit indicates that we support high capacity */ |
Philip Rakity | b676f03 | 2011-02-13 23:13:09 -0800 | [diff] [blame] | 725 | err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 726 | if (err) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 727 | goto err; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 728 | |
| 729 | /* |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 730 | * For SPI, enable CRC as appropriate. |
| 731 | */ |
| 732 | if (mmc_host_is_spi(host)) { |
| 733 | err = mmc_spi_set_crc(host, use_spi_crc); |
| 734 | if (err) |
| 735 | goto err; |
| 736 | } |
| 737 | |
| 738 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 739 | * Fetch CID from card. |
| 740 | */ |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 741 | if (mmc_host_is_spi(host)) |
| 742 | err = mmc_send_cid(host, cid); |
| 743 | else |
| 744 | err = mmc_all_send_cid(host, cid); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 745 | if (err) |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 746 | goto err; |
| 747 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 748 | if (oldcard) { |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 749 | if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) { |
| 750 | err = -ENOENT; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 751 | goto err; |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 752 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 753 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 754 | card = oldcard; |
| 755 | } else { |
| 756 | /* |
| 757 | * Allocate card structure. |
| 758 | */ |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 759 | card = mmc_alloc_card(host, &mmc_type); |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 760 | if (IS_ERR(card)) { |
| 761 | err = PTR_ERR(card); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 762 | goto err; |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 763 | } |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 764 | |
| 765 | card->type = MMC_TYPE_MMC; |
| 766 | card->rca = 1; |
| 767 | memcpy(card->raw_cid, cid, sizeof(card->raw_cid)); |
| 768 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 769 | |
| 770 | /* |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 771 | * For native busses: set card RCA and quit open drain mode. |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 772 | */ |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 773 | if (!mmc_host_is_spi(host)) { |
| 774 | err = mmc_set_relative_addr(card); |
| 775 | if (err) |
| 776 | goto free_card; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 777 | |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 778 | mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL); |
| 779 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 780 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 781 | if (!oldcard) { |
| 782 | /* |
| 783 | * Fetch CSD from card. |
| 784 | */ |
| 785 | err = mmc_send_csd(card, card->raw_csd); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 786 | if (err) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 787 | goto free_card; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 788 | |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 789 | err = mmc_decode_csd(card); |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 790 | if (err) |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 791 | goto free_card; |
| 792 | err = mmc_decode_cid(card); |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 793 | if (err) |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 794 | goto free_card; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 795 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 796 | |
| 797 | /* |
Pierre Ossman | 89a73cf | 2007-05-01 15:08:30 +0200 | [diff] [blame] | 798 | * Select card, as all following commands rely on that. |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 799 | */ |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 800 | if (!mmc_host_is_spi(host)) { |
| 801 | err = mmc_select_card(card); |
| 802 | if (err) |
| 803 | goto free_card; |
| 804 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 805 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 806 | if (!oldcard) { |
| 807 | /* |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 808 | * Fetch and process extended CSD. |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 809 | */ |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 810 | |
| 811 | err = mmc_get_ext_csd(card, &ext_csd); |
| 812 | if (err) |
| 813 | goto free_card; |
| 814 | err = mmc_read_ext_csd(card, ext_csd); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 815 | if (err) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 816 | goto free_card; |
Philip Rakity | b676f03 | 2011-02-13 23:13:09 -0800 | [diff] [blame] | 817 | |
| 818 | /* If doing byte addressing, check if required to do sector |
| 819 | * addressing. Handle the case of <2GB cards needing sector |
| 820 | * addressing. See section 8.1 JEDEC Standard JED84-A441; |
| 821 | * ocr register has bit 30 set for sector addressing. |
| 822 | */ |
| 823 | if (!(mmc_card_blockaddr(card)) && (rocr & (1<<30))) |
| 824 | mmc_card_set_blockaddr(card); |
| 825 | |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 826 | /* Erase size depends on CSD and Extended CSD */ |
| 827 | mmc_set_erase_size(card); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 828 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 829 | |
| 830 | /* |
Chuanxiao Dong | 709de99 | 2011-01-22 04:09:41 +0800 | [diff] [blame] | 831 | * 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] | 832 | * 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] | 833 | */ |
| 834 | if (card->ext_csd.enhanced_area_en) { |
| 835 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
Seungwon Jeon | b23cf0b | 2011-09-23 14:15:29 +0900 | [diff] [blame] | 836 | EXT_CSD_ERASE_GROUP_DEF, 1, |
| 837 | card->ext_csd.generic_cmd6_time); |
Chuanxiao Dong | 709de99 | 2011-01-22 04:09:41 +0800 | [diff] [blame] | 838 | |
| 839 | if (err && err != -EBADMSG) |
| 840 | goto free_card; |
| 841 | |
| 842 | if (err) { |
| 843 | err = 0; |
| 844 | /* |
| 845 | * Just disable enhanced area off & sz |
| 846 | * will try to enable ERASE_GROUP_DEF |
| 847 | * during next time reinit |
| 848 | */ |
| 849 | card->ext_csd.enhanced_area_offset = -EINVAL; |
| 850 | card->ext_csd.enhanced_area_size = -EINVAL; |
| 851 | } else { |
| 852 | card->ext_csd.erase_group_def = 1; |
| 853 | /* |
| 854 | * enable ERASE_GRP_DEF successfully. |
| 855 | * This will affect the erase size, so |
| 856 | * here need to reset erase size |
| 857 | */ |
| 858 | mmc_set_erase_size(card); |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | /* |
Philip Rakity | 41e2a48 | 2011-03-19 14:10:33 -0400 | [diff] [blame] | 863 | * Ensure eMMC user default partition is enabled |
| 864 | */ |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 865 | if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) { |
| 866 | card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK; |
| 867 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG, |
| 868 | card->ext_csd.part_config, |
| 869 | card->ext_csd.part_time); |
| 870 | if (err && err != -EBADMSG) |
| 871 | goto free_card; |
Philip Rakity | 41e2a48 | 2011-03-19 14:10:33 -0400 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | /* |
Girish K S | bec8726 | 2011-10-13 12:04:16 +0530 | [diff] [blame] | 875 | * If the host supports the power_off_notify capability then |
| 876 | * set the notification byte in the ext_csd register of device |
| 877 | */ |
| 878 | if ((host->caps2 & MMC_CAP2_POWEROFF_NOTIFY) && |
Girish K S | a80f162 | 2011-11-15 11:55:46 +0530 | [diff] [blame] | 879 | (card->ext_csd.rev >= 6)) { |
Girish K S | bec8726 | 2011-10-13 12:04:16 +0530 | [diff] [blame] | 880 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 881 | EXT_CSD_POWER_OFF_NOTIFICATION, |
| 882 | EXT_CSD_POWER_ON, |
| 883 | card->ext_csd.generic_cmd6_time); |
| 884 | if (err && err != -EBADMSG) |
| 885 | goto free_card; |
Girish K S | bec8726 | 2011-10-13 12:04:16 +0530 | [diff] [blame] | 886 | |
Girish K S | 96a85d5 | 2011-11-04 16:22:47 +0530 | [diff] [blame] | 887 | /* |
| 888 | * The err can be -EBADMSG or 0, |
| 889 | * so check for success and update the flag |
| 890 | */ |
| 891 | if (!err) |
| 892 | card->poweroff_notify_state = MMC_POWERED_ON; |
| 893 | } |
Girish K S | bec8726 | 2011-10-13 12:04:16 +0530 | [diff] [blame] | 894 | |
| 895 | /* |
Pierre Ossman | 89a73cf | 2007-05-01 15:08:30 +0200 | [diff] [blame] | 896 | * Activate high speed (if supported) |
| 897 | */ |
| 898 | if ((card->ext_csd.hs_max_dtr != 0) && |
| 899 | (host->caps & MMC_CAP_MMC_HIGHSPEED)) { |
| 900 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
Seungwon Jeon | b23cf0b | 2011-09-23 14:15:29 +0900 | [diff] [blame] | 901 | EXT_CSD_HS_TIMING, 1, |
| 902 | card->ext_csd.generic_cmd6_time); |
Adrian Hunter | ef0b27d | 2009-09-22 16:44:37 -0700 | [diff] [blame] | 903 | if (err && err != -EBADMSG) |
Pierre Ossman | 89a73cf | 2007-05-01 15:08:30 +0200 | [diff] [blame] | 904 | goto free_card; |
| 905 | |
Adrian Hunter | ef0b27d | 2009-09-22 16:44:37 -0700 | [diff] [blame] | 906 | if (err) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 907 | pr_warning("%s: switch to highspeed failed\n", |
Adrian Hunter | ef0b27d | 2009-09-22 16:44:37 -0700 | [diff] [blame] | 908 | mmc_hostname(card->host)); |
| 909 | err = 0; |
| 910 | } else { |
| 911 | mmc_card_set_highspeed(card); |
| 912 | mmc_set_timing(card->host, MMC_TIMING_MMC_HS); |
| 913 | } |
Pierre Ossman | 89a73cf | 2007-05-01 15:08:30 +0200 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | /* |
Jaehoon Chung | eb0d8f1 | 2011-10-18 01:26:42 -0400 | [diff] [blame] | 917 | * Enable HPI feature (if supported) |
| 918 | */ |
| 919 | if (card->ext_csd.hpi) { |
| 920 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 921 | EXT_CSD_HPI_MGMT, 1, 0); |
| 922 | if (err && err != -EBADMSG) |
| 923 | goto free_card; |
| 924 | if (err) { |
| 925 | pr_warning("%s: Enabling HPI failed\n", |
| 926 | mmc_hostname(card->host)); |
| 927 | err = 0; |
| 928 | } else |
| 929 | card->ext_csd.hpi_en = 1; |
| 930 | } |
| 931 | |
| 932 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 933 | * Compute bus speed. |
| 934 | */ |
| 935 | max_dtr = (unsigned int)-1; |
| 936 | |
| 937 | if (mmc_card_highspeed(card)) { |
| 938 | if (max_dtr > card->ext_csd.hs_max_dtr) |
| 939 | max_dtr = card->ext_csd.hs_max_dtr; |
| 940 | } else if (max_dtr > card->csd.max_dtr) { |
| 941 | max_dtr = card->csd.max_dtr; |
| 942 | } |
| 943 | |
| 944 | mmc_set_clock(host, max_dtr); |
| 945 | |
Pierre Ossman | 89a73cf | 2007-05-01 15:08:30 +0200 | [diff] [blame] | 946 | /* |
Adrian Hunter | 0f8d8ea | 2010-08-24 13:20:26 +0300 | [diff] [blame] | 947 | * Indicate DDR mode (if supported). |
Hanumath Prasad | dfc13e8 | 2010-09-30 17:37:23 -0400 | [diff] [blame] | 948 | */ |
| 949 | if (mmc_card_highspeed(card)) { |
| 950 | 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] | 951 | && ((host->caps & (MMC_CAP_1_8V_DDR | |
| 952 | MMC_CAP_UHS_DDR50)) |
| 953 | == (MMC_CAP_1_8V_DDR | MMC_CAP_UHS_DDR50))) |
Adrian Hunter | 49e3b5a | 2010-10-11 12:43:50 +0300 | [diff] [blame] | 954 | ddr = MMC_1_8V_DDR_MODE; |
Hanumath Prasad | dfc13e8 | 2010-09-30 17:37:23 -0400 | [diff] [blame] | 955 | 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] | 956 | && ((host->caps & (MMC_CAP_1_2V_DDR | |
| 957 | MMC_CAP_UHS_DDR50)) |
| 958 | == (MMC_CAP_1_2V_DDR | MMC_CAP_UHS_DDR50))) |
Adrian Hunter | 49e3b5a | 2010-10-11 12:43:50 +0300 | [diff] [blame] | 959 | ddr = MMC_1_2V_DDR_MODE; |
Hanumath Prasad | dfc13e8 | 2010-09-30 17:37:23 -0400 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | /* |
Adrian Hunter | 0f8d8ea | 2010-08-24 13:20:26 +0300 | [diff] [blame] | 963 | * Activate wide bus and DDR (if supported). |
Pierre Ossman | 89a73cf | 2007-05-01 15:08:30 +0200 | [diff] [blame] | 964 | */ |
| 965 | if ((card->csd.mmca_vsn >= CSD_SPEC_VER_4) && |
Jarkko Lavinen | b30f8af | 2008-11-17 14:35:21 +0200 | [diff] [blame] | 966 | (host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA))) { |
Aries Lee | 22113ef | 2010-12-15 08:14:24 +0100 | [diff] [blame] | 967 | static unsigned ext_csd_bits[][2] = { |
| 968 | { EXT_CSD_BUS_WIDTH_8, EXT_CSD_DDR_BUS_WIDTH_8 }, |
| 969 | { EXT_CSD_BUS_WIDTH_4, EXT_CSD_DDR_BUS_WIDTH_4 }, |
| 970 | { EXT_CSD_BUS_WIDTH_1, EXT_CSD_BUS_WIDTH_1 }, |
| 971 | }; |
| 972 | static unsigned bus_widths[] = { |
| 973 | MMC_BUS_WIDTH_8, |
| 974 | MMC_BUS_WIDTH_4, |
| 975 | MMC_BUS_WIDTH_1 |
| 976 | }; |
| 977 | unsigned idx, bus_width = 0; |
Jarkko Lavinen | b30f8af | 2008-11-17 14:35:21 +0200 | [diff] [blame] | 978 | |
Aries Lee | 22113ef | 2010-12-15 08:14:24 +0100 | [diff] [blame] | 979 | if (host->caps & MMC_CAP_8_BIT_DATA) |
| 980 | idx = 0; |
| 981 | else |
| 982 | idx = 1; |
| 983 | for (; idx < ARRAY_SIZE(bus_widths); idx++) { |
| 984 | bus_width = bus_widths[idx]; |
| 985 | if (bus_width == MMC_BUS_WIDTH_1) |
| 986 | ddr = 0; /* no DDR for 1-bit width */ |
Girish K S | b87d8db | 2011-09-23 20:41:47 +0530 | [diff] [blame] | 987 | err = mmc_select_powerclass(card, ext_csd_bits[idx][0], |
| 988 | ext_csd); |
| 989 | if (err) |
| 990 | pr_err("%s: power class selection to " |
| 991 | "bus width %d failed\n", |
| 992 | mmc_hostname(card->host), |
| 993 | 1 << bus_width); |
| 994 | |
Aries Lee | 22113ef | 2010-12-15 08:14:24 +0100 | [diff] [blame] | 995 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 996 | EXT_CSD_BUS_WIDTH, |
Andrei Warkentin | d3a8d95 | 2011-04-11 16:13:43 -0500 | [diff] [blame] | 997 | ext_csd_bits[idx][0], |
Seungwon Jeon | b23cf0b | 2011-09-23 14:15:29 +0900 | [diff] [blame] | 998 | card->ext_csd.generic_cmd6_time); |
Aries Lee | 22113ef | 2010-12-15 08:14:24 +0100 | [diff] [blame] | 999 | if (!err) { |
Philip Rakity | 4c4cb17 | 2011-05-13 11:17:18 +0530 | [diff] [blame] | 1000 | mmc_set_bus_width(card->host, bus_width); |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 1001 | |
Aries Lee | 22113ef | 2010-12-15 08:14:24 +0100 | [diff] [blame] | 1002 | /* |
| 1003 | * If controller can't handle bus width test, |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 1004 | * compare ext_csd previously read in 1 bit mode |
| 1005 | * against ext_csd at new bus width |
Aries Lee | 22113ef | 2010-12-15 08:14:24 +0100 | [diff] [blame] | 1006 | */ |
| 1007 | if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST)) |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 1008 | err = mmc_compare_ext_csds(card, |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 1009 | bus_width); |
| 1010 | else |
| 1011 | err = mmc_bus_test(card, bus_width); |
Aries Lee | 22113ef | 2010-12-15 08:14:24 +0100 | [diff] [blame] | 1012 | if (!err) |
| 1013 | break; |
| 1014 | } |
Jarkko Lavinen | b30f8af | 2008-11-17 14:35:21 +0200 | [diff] [blame] | 1015 | } |
| 1016 | |
Aries Lee | 22113ef | 2010-12-15 08:14:24 +0100 | [diff] [blame] | 1017 | if (!err && ddr) { |
Girish K S | b87d8db | 2011-09-23 20:41:47 +0530 | [diff] [blame] | 1018 | err = mmc_select_powerclass(card, ext_csd_bits[idx][1], |
| 1019 | ext_csd); |
| 1020 | if (err) |
| 1021 | pr_err("%s: power class selection to " |
| 1022 | "bus width %d ddr %d failed\n", |
| 1023 | mmc_hostname(card->host), |
| 1024 | 1 << bus_width, ddr); |
| 1025 | |
Aries Lee | 22113ef | 2010-12-15 08:14:24 +0100 | [diff] [blame] | 1026 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
Andrei Warkentin | d3a8d95 | 2011-04-11 16:13:43 -0500 | [diff] [blame] | 1027 | EXT_CSD_BUS_WIDTH, |
| 1028 | ext_csd_bits[idx][1], |
Seungwon Jeon | b23cf0b | 2011-09-23 14:15:29 +0900 | [diff] [blame] | 1029 | card->ext_csd.generic_cmd6_time); |
Aries Lee | 22113ef | 2010-12-15 08:14:24 +0100 | [diff] [blame] | 1030 | } |
Adrian Hunter | ef0b27d | 2009-09-22 16:44:37 -0700 | [diff] [blame] | 1031 | if (err) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 1032 | pr_warning("%s: switch to bus width %d ddr %d " |
Aries Lee | 22113ef | 2010-12-15 08:14:24 +0100 | [diff] [blame] | 1033 | "failed\n", mmc_hostname(card->host), |
| 1034 | 1 << bus_width, ddr); |
| 1035 | goto free_card; |
| 1036 | } else if (ddr) { |
Philip Rakity | 4c4cb17 | 2011-05-13 11:17:18 +0530 | [diff] [blame] | 1037 | /* |
| 1038 | * eMMC cards can support 3.3V to 1.2V i/o (vccq) |
| 1039 | * signaling. |
| 1040 | * |
| 1041 | * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq. |
| 1042 | * |
| 1043 | * 1.8V vccq at 3.3V core voltage (vcc) is not required |
| 1044 | * in the JEDEC spec for DDR. |
| 1045 | * |
| 1046 | * Do not force change in vccq since we are obviously |
| 1047 | * working and no change to vccq is needed. |
| 1048 | * |
| 1049 | * WARNING: eMMC rules are NOT the same as SD DDR |
| 1050 | */ |
| 1051 | if (ddr == EXT_CSD_CARD_TYPE_DDR_1_2V) { |
| 1052 | err = mmc_set_signal_voltage(host, |
| 1053 | MMC_SIGNAL_VOLTAGE_120, 0); |
| 1054 | if (err) |
| 1055 | goto err; |
| 1056 | } |
Aries Lee | 22113ef | 2010-12-15 08:14:24 +0100 | [diff] [blame] | 1057 | mmc_card_set_ddr_mode(card); |
Philip Rakity | 4c4cb17 | 2011-05-13 11:17:18 +0530 | [diff] [blame] | 1058 | mmc_set_timing(card->host, MMC_TIMING_UHS_DDR50); |
| 1059 | mmc_set_bus_width(card->host, bus_width); |
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 | |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 1063 | /* |
| 1064 | * If cache size is higher than 0, this indicates |
| 1065 | * the existence of cache and it can be turned on. |
| 1066 | */ |
| 1067 | if ((host->caps2 & MMC_CAP2_CACHE_CTRL) && |
| 1068 | card->ext_csd.cache_size > 0) { |
| 1069 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 1070 | EXT_CSD_CACHE_CTRL, 1, 0); |
| 1071 | if (err && err != -EBADMSG) |
| 1072 | goto free_card; |
| 1073 | |
| 1074 | /* |
| 1075 | * Only if no error, cache is turned on successfully. |
| 1076 | */ |
| 1077 | card->ext_csd.cache_ctrl = err ? 0 : 1; |
| 1078 | } |
| 1079 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1080 | if (!oldcard) |
| 1081 | host->card = card; |
| 1082 | |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 1083 | mmc_free_ext_csd(ext_csd); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 1084 | return 0; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1085 | |
| 1086 | free_card: |
| 1087 | if (!oldcard) |
| 1088 | mmc_remove_card(card); |
| 1089 | err: |
Philip Rakity | 08ee80c | 2011-05-24 18:14:58 -0700 | [diff] [blame] | 1090 | mmc_free_ext_csd(ext_csd); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1091 | |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 1092 | return err; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | /* |
| 1096 | * Host is being removed. Free up the current card. |
| 1097 | */ |
| 1098 | static void mmc_remove(struct mmc_host *host) |
| 1099 | { |
| 1100 | BUG_ON(!host); |
| 1101 | BUG_ON(!host->card); |
| 1102 | |
| 1103 | mmc_remove_card(host->card); |
| 1104 | host->card = NULL; |
| 1105 | } |
| 1106 | |
| 1107 | /* |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame^] | 1108 | * Card detection - card is alive. |
| 1109 | */ |
| 1110 | static int mmc_alive(struct mmc_host *host) |
| 1111 | { |
| 1112 | return mmc_send_status(host->card, NULL); |
| 1113 | } |
| 1114 | |
| 1115 | /* |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1116 | * Card detection callback from host. |
| 1117 | */ |
| 1118 | static void mmc_detect(struct mmc_host *host) |
| 1119 | { |
| 1120 | int err; |
| 1121 | |
| 1122 | BUG_ON(!host); |
| 1123 | BUG_ON(!host->card); |
| 1124 | |
| 1125 | mmc_claim_host(host); |
| 1126 | |
| 1127 | /* |
| 1128 | * Just check if our card has been removed. |
| 1129 | */ |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame^] | 1130 | err = _mmc_detect_card_removed(host); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1131 | |
| 1132 | mmc_release_host(host); |
| 1133 | |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 1134 | if (err) { |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 1135 | mmc_remove(host); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1136 | |
| 1137 | mmc_claim_host(host); |
| 1138 | mmc_detach_bus(host); |
Ulf Hansson | 7f7e412 | 2011-09-21 14:08:13 -0400 | [diff] [blame] | 1139 | mmc_power_off(host); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1140 | mmc_release_host(host); |
| 1141 | } |
| 1142 | } |
| 1143 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1144 | /* |
| 1145 | * Suspend callback from host. |
| 1146 | */ |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 1147 | static int mmc_suspend(struct mmc_host *host) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1148 | { |
Balaji T K | c380546 | 2011-09-08 22:08:39 +0530 | [diff] [blame] | 1149 | int err = 0; |
| 1150 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1151 | BUG_ON(!host); |
| 1152 | BUG_ON(!host->card); |
| 1153 | |
| 1154 | mmc_claim_host(host); |
Balaji T K | c380546 | 2011-09-08 22:08:39 +0530 | [diff] [blame] | 1155 | if (mmc_card_can_sleep(host)) |
| 1156 | err = mmc_card_sleep(host); |
| 1157 | else if (!mmc_host_is_spi(host)) |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 1158 | mmc_deselect_cards(host); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1159 | host->card->state &= ~MMC_STATE_HIGHSPEED; |
| 1160 | mmc_release_host(host); |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 1161 | |
Balaji T K | c380546 | 2011-09-08 22:08:39 +0530 | [diff] [blame] | 1162 | return err; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | /* |
| 1166 | * Resume callback from host. |
| 1167 | * |
| 1168 | * This function tries to determine if the same card is still present |
| 1169 | * and, if so, restore all state to it. |
| 1170 | */ |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 1171 | static int mmc_resume(struct mmc_host *host) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1172 | { |
| 1173 | int err; |
| 1174 | |
| 1175 | BUG_ON(!host); |
| 1176 | BUG_ON(!host->card); |
| 1177 | |
| 1178 | mmc_claim_host(host); |
Pierre Ossman | 8c75dea | 2007-05-19 16:14:43 +0200 | [diff] [blame] | 1179 | err = mmc_init_card(host, host->ocr, host->card); |
Pierre Ossman | 2986d0b | 2007-07-22 17:52:06 +0200 | [diff] [blame] | 1180 | mmc_release_host(host); |
| 1181 | |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 1182 | return err; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1183 | } |
| 1184 | |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 1185 | static int mmc_power_restore(struct mmc_host *host) |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1186 | { |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 1187 | int ret; |
| 1188 | |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1189 | host->card->state &= ~MMC_STATE_HIGHSPEED; |
| 1190 | mmc_claim_host(host); |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 1191 | ret = mmc_init_card(host, host->ocr, host->card); |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1192 | mmc_release_host(host); |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 1193 | |
| 1194 | return ret; |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1195 | } |
| 1196 | |
Jarkko Lavinen | b1ebe38 | 2009-09-22 16:44:34 -0700 | [diff] [blame] | 1197 | static int mmc_sleep(struct mmc_host *host) |
| 1198 | { |
| 1199 | struct mmc_card *card = host->card; |
| 1200 | int err = -ENOSYS; |
| 1201 | |
| 1202 | if (card && card->ext_csd.rev >= 3) { |
| 1203 | err = mmc_card_sleepawake(host, 1); |
| 1204 | if (err < 0) |
| 1205 | pr_debug("%s: Error %d while putting card into sleep", |
| 1206 | mmc_hostname(host), err); |
| 1207 | } |
| 1208 | |
| 1209 | return err; |
| 1210 | } |
| 1211 | |
| 1212 | static int mmc_awake(struct mmc_host *host) |
| 1213 | { |
| 1214 | struct mmc_card *card = host->card; |
| 1215 | int err = -ENOSYS; |
| 1216 | |
| 1217 | if (card && card->ext_csd.rev >= 3) { |
| 1218 | err = mmc_card_sleepawake(host, 0); |
| 1219 | if (err < 0) |
| 1220 | pr_debug("%s: Error %d while awaking sleeping card", |
| 1221 | mmc_hostname(host), err); |
| 1222 | } |
| 1223 | |
| 1224 | return err; |
| 1225 | } |
| 1226 | |
Adrian Hunter | 9feae24 | 2009-09-22 16:44:32 -0700 | [diff] [blame] | 1227 | static const struct mmc_bus_ops mmc_ops = { |
Jarkko Lavinen | b1ebe38 | 2009-09-22 16:44:34 -0700 | [diff] [blame] | 1228 | .awake = mmc_awake, |
| 1229 | .sleep = mmc_sleep, |
Adrian Hunter | 9feae24 | 2009-09-22 16:44:32 -0700 | [diff] [blame] | 1230 | .remove = mmc_remove, |
| 1231 | .detect = mmc_detect, |
| 1232 | .suspend = NULL, |
| 1233 | .resume = NULL, |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1234 | .power_restore = mmc_power_restore, |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame^] | 1235 | .alive = mmc_alive, |
Adrian Hunter | 9feae24 | 2009-09-22 16:44:32 -0700 | [diff] [blame] | 1236 | }; |
| 1237 | |
| 1238 | static const struct mmc_bus_ops mmc_ops_unsafe = { |
Jarkko Lavinen | b1ebe38 | 2009-09-22 16:44:34 -0700 | [diff] [blame] | 1239 | .awake = mmc_awake, |
| 1240 | .sleep = mmc_sleep, |
Adrian Hunter | 9feae24 | 2009-09-22 16:44:32 -0700 | [diff] [blame] | 1241 | .remove = mmc_remove, |
| 1242 | .detect = mmc_detect, |
| 1243 | .suspend = mmc_suspend, |
| 1244 | .resume = mmc_resume, |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1245 | .power_restore = mmc_power_restore, |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame^] | 1246 | .alive = mmc_alive, |
Adrian Hunter | 9feae24 | 2009-09-22 16:44:32 -0700 | [diff] [blame] | 1247 | }; |
| 1248 | |
| 1249 | static void mmc_attach_bus_ops(struct mmc_host *host) |
| 1250 | { |
| 1251 | const struct mmc_bus_ops *bus_ops; |
| 1252 | |
Matt Fleming | 71d7d3d | 2010-09-27 09:42:19 +0100 | [diff] [blame] | 1253 | if (!mmc_card_is_removable(host)) |
Adrian Hunter | 9feae24 | 2009-09-22 16:44:32 -0700 | [diff] [blame] | 1254 | bus_ops = &mmc_ops_unsafe; |
| 1255 | else |
| 1256 | bus_ops = &mmc_ops; |
| 1257 | mmc_attach_bus(host, bus_ops); |
| 1258 | } |
| 1259 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1260 | /* |
| 1261 | * Starting point for MMC card init. |
| 1262 | */ |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 1263 | int mmc_attach_mmc(struct mmc_host *host) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1264 | { |
| 1265 | int err; |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 1266 | u32 ocr; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1267 | |
| 1268 | BUG_ON(!host); |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 1269 | WARN_ON(!host->claimed); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1270 | |
Stefan Nilsson XK | 4466903 | 2011-09-15 17:50:38 +0200 | [diff] [blame] | 1271 | /* Set correct bus mode for MMC before attempting attach */ |
| 1272 | if (!mmc_host_is_spi(host)) |
| 1273 | mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN); |
| 1274 | |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 1275 | err = mmc_send_op_cond(host, 0, &ocr); |
| 1276 | if (err) |
| 1277 | return err; |
| 1278 | |
Adrian Hunter | 9feae24 | 2009-09-22 16:44:32 -0700 | [diff] [blame] | 1279 | mmc_attach_bus_ops(host); |
Takashi Iwai | 8f230f4 | 2010-12-08 10:04:30 +0100 | [diff] [blame] | 1280 | if (host->ocr_avail_mmc) |
| 1281 | host->ocr_avail = host->ocr_avail_mmc; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1282 | |
| 1283 | /* |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 1284 | * We need to get OCR a different way for SPI. |
| 1285 | */ |
| 1286 | if (mmc_host_is_spi(host)) { |
| 1287 | err = mmc_spi_read_ocr(host, 1, &ocr); |
| 1288 | if (err) |
| 1289 | goto err; |
| 1290 | } |
| 1291 | |
| 1292 | /* |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1293 | * Sanity check the voltages that the card claims to |
| 1294 | * support. |
| 1295 | */ |
| 1296 | if (ocr & 0x7F) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 1297 | pr_warning("%s: card claims to support voltages " |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1298 | "below the defined range. These will be ignored.\n", |
| 1299 | mmc_hostname(host)); |
| 1300 | ocr &= ~0x7F; |
| 1301 | } |
| 1302 | |
| 1303 | host->ocr = mmc_select_voltage(host, ocr); |
| 1304 | |
| 1305 | /* |
| 1306 | * Can we support the voltage of the card? |
| 1307 | */ |
Pierre Ossman | 109b5be | 2007-07-23 00:12:10 +0200 | [diff] [blame] | 1308 | if (!host->ocr) { |
| 1309 | err = -EINVAL; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1310 | goto err; |
Pierre Ossman | 109b5be | 2007-07-23 00:12:10 +0200 | [diff] [blame] | 1311 | } |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1312 | |
| 1313 | /* |
| 1314 | * Detect and init the card. |
| 1315 | */ |
Pierre Ossman | 8c75dea | 2007-05-19 16:14:43 +0200 | [diff] [blame] | 1316 | err = mmc_init_card(host, host->ocr, NULL); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 1317 | if (err) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1318 | goto err; |
| 1319 | |
| 1320 | mmc_release_host(host); |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 1321 | err = mmc_add_card(host->card); |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 1322 | mmc_claim_host(host); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1323 | if (err) |
Pierre Ossman | 2986d0b | 2007-07-22 17:52:06 +0200 | [diff] [blame] | 1324 | goto remove_card; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1325 | |
| 1326 | return 0; |
| 1327 | |
Pierre Ossman | 2986d0b | 2007-07-22 17:52:06 +0200 | [diff] [blame] | 1328 | remove_card: |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 1329 | mmc_release_host(host); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1330 | mmc_remove_card(host->card); |
Pierre Ossman | 2986d0b | 2007-07-22 17:52:06 +0200 | [diff] [blame] | 1331 | mmc_claim_host(host); |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 1332 | host->card = NULL; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1333 | err: |
| 1334 | mmc_detach_bus(host); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1335 | |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 1336 | pr_err("%s: error %d whilst initialising MMC card\n", |
Pierre Ossman | 109b5be | 2007-07-23 00:12:10 +0200 | [diff] [blame] | 1337 | mmc_hostname(host), err); |
| 1338 | |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 1339 | return err; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1340 | } |