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