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/sd.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 | * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved. |
| 6 | * Copyright (C) 2005-2007 Pierre Ossman, 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> |
Pierre Ossman | 3373c0a | 2007-05-31 22:25:11 +0200 | [diff] [blame] | 20 | #include <linux/mmc/sd.h> |
Asutosh Das | bbc8478 | 2013-02-11 15:31:35 +0530 | [diff] [blame] | 21 | #include <linux/pm_runtime.h> |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 22 | |
| 23 | #include "core.h" |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 24 | #include "bus.h" |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 25 | #include "mmc_ops.h" |
Mark Brown | ce10149 | 2011-02-10 10:58:37 +0000 | [diff] [blame] | 26 | #include "sd.h" |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 27 | #include "sd_ops.h" |
| 28 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 29 | static const unsigned int tran_exp[] = { |
| 30 | 10000, 100000, 1000000, 10000000, |
| 31 | 0, 0, 0, 0 |
| 32 | }; |
| 33 | |
| 34 | static const unsigned char tran_mant[] = { |
| 35 | 0, 10, 12, 13, 15, 20, 25, 30, |
| 36 | 35, 40, 45, 50, 55, 60, 70, 80, |
| 37 | }; |
| 38 | |
| 39 | static const unsigned int tacc_exp[] = { |
| 40 | 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, |
| 41 | }; |
| 42 | |
| 43 | static const unsigned int tacc_mant[] = { |
| 44 | 0, 10, 12, 13, 15, 20, 25, 30, |
| 45 | 35, 40, 45, 50, 55, 60, 70, 80, |
| 46 | }; |
| 47 | |
| 48 | #define UNSTUFF_BITS(resp,start,size) \ |
| 49 | ({ \ |
| 50 | const int __size = size; \ |
| 51 | const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \ |
| 52 | const int __off = 3 - ((start) / 32); \ |
| 53 | const int __shft = (start) & 31; \ |
| 54 | u32 __res; \ |
| 55 | \ |
| 56 | __res = resp[__off] >> __shft; \ |
| 57 | if (__size + __shft > 32) \ |
| 58 | __res |= resp[__off-1] << ((32 - __shft) % 32); \ |
| 59 | __res & __mask; \ |
| 60 | }) |
| 61 | |
| 62 | /* |
| 63 | * Given the decoded CSD structure, decode the raw CID to our CID structure. |
| 64 | */ |
Michal Miroslaw | 71578a1 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 65 | void mmc_decode_cid(struct mmc_card *card) |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 66 | { |
| 67 | u32 *resp = card->raw_cid; |
| 68 | |
| 69 | memset(&card->cid, 0, sizeof(struct mmc_cid)); |
| 70 | |
| 71 | /* |
| 72 | * SD doesn't currently have a version field so we will |
| 73 | * have to assume we can parse this. |
| 74 | */ |
| 75 | card->cid.manfid = UNSTUFF_BITS(resp, 120, 8); |
| 76 | card->cid.oemid = UNSTUFF_BITS(resp, 104, 16); |
| 77 | card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); |
| 78 | card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); |
| 79 | card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); |
| 80 | card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); |
| 81 | card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); |
| 82 | card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4); |
| 83 | card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4); |
| 84 | card->cid.serial = UNSTUFF_BITS(resp, 24, 32); |
| 85 | card->cid.year = UNSTUFF_BITS(resp, 12, 8); |
| 86 | card->cid.month = UNSTUFF_BITS(resp, 8, 4); |
| 87 | |
| 88 | card->cid.year += 2000; /* SD cards year offset */ |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * Given a 128-bit response, decode to our card CSD structure. |
| 93 | */ |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 94 | static int mmc_decode_csd(struct mmc_card *card) |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 95 | { |
| 96 | struct mmc_csd *csd = &card->csd; |
| 97 | unsigned int e, m, csd_struct; |
| 98 | u32 *resp = card->raw_csd; |
| 99 | |
| 100 | csd_struct = UNSTUFF_BITS(resp, 126, 2); |
| 101 | |
| 102 | switch (csd_struct) { |
| 103 | case 0: |
| 104 | m = UNSTUFF_BITS(resp, 115, 4); |
| 105 | e = UNSTUFF_BITS(resp, 112, 3); |
| 106 | csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10; |
| 107 | csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100; |
| 108 | |
| 109 | m = UNSTUFF_BITS(resp, 99, 4); |
| 110 | e = UNSTUFF_BITS(resp, 96, 3); |
| 111 | csd->max_dtr = tran_exp[e] * tran_mant[m]; |
| 112 | csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); |
| 113 | |
| 114 | e = UNSTUFF_BITS(resp, 47, 3); |
| 115 | m = UNSTUFF_BITS(resp, 62, 12); |
| 116 | csd->capacity = (1 + m) << (e + 2); |
| 117 | |
| 118 | csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4); |
| 119 | csd->read_partial = UNSTUFF_BITS(resp, 79, 1); |
| 120 | csd->write_misalign = UNSTUFF_BITS(resp, 78, 1); |
| 121 | csd->read_misalign = UNSTUFF_BITS(resp, 77, 1); |
| 122 | csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3); |
| 123 | csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4); |
| 124 | csd->write_partial = UNSTUFF_BITS(resp, 21, 1); |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 125 | |
| 126 | if (UNSTUFF_BITS(resp, 46, 1)) { |
| 127 | csd->erase_size = 1; |
| 128 | } else if (csd->write_blkbits >= 9) { |
| 129 | csd->erase_size = UNSTUFF_BITS(resp, 39, 7) + 1; |
| 130 | csd->erase_size <<= csd->write_blkbits - 9; |
| 131 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 132 | break; |
| 133 | case 1: |
| 134 | /* |
Arindam Nath | 3a30351 | 2011-05-05 12:19:03 +0530 | [diff] [blame] | 135 | * This is a block-addressed SDHC or SDXC card. Most |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 136 | * interesting fields are unused and have fixed |
| 137 | * values. To avoid getting tripped by buggy cards, |
| 138 | * we assume those fixed values ourselves. |
| 139 | */ |
| 140 | mmc_card_set_blockaddr(card); |
| 141 | |
| 142 | csd->tacc_ns = 0; /* Unused */ |
| 143 | csd->tacc_clks = 0; /* Unused */ |
| 144 | |
| 145 | m = UNSTUFF_BITS(resp, 99, 4); |
| 146 | e = UNSTUFF_BITS(resp, 96, 3); |
| 147 | csd->max_dtr = tran_exp[e] * tran_mant[m]; |
| 148 | csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); |
Arindam Nath | 3a30351 | 2011-05-05 12:19:03 +0530 | [diff] [blame] | 149 | csd->c_size = UNSTUFF_BITS(resp, 48, 22); |
| 150 | |
| 151 | /* SDXC cards have a minimum C_SIZE of 0x00FFFF */ |
| 152 | if (csd->c_size >= 0xFFFF) |
| 153 | mmc_card_set_ext_capacity(card); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 154 | |
| 155 | m = UNSTUFF_BITS(resp, 48, 22); |
| 156 | csd->capacity = (1 + m) << 10; |
| 157 | |
| 158 | csd->read_blkbits = 9; |
| 159 | csd->read_partial = 0; |
| 160 | csd->write_misalign = 0; |
| 161 | csd->read_misalign = 0; |
| 162 | csd->r2w_factor = 4; /* Unused */ |
| 163 | csd->write_blkbits = 9; |
| 164 | csd->write_partial = 0; |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 165 | csd->erase_size = 1; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 166 | break; |
| 167 | default: |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 168 | pr_err("%s: unrecognised CSD structure version %d\n", |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 169 | mmc_hostname(card->host), csd_struct); |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 170 | return -EINVAL; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 171 | } |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 172 | |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 173 | card->erase_size = csd->erase_size; |
| 174 | |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 175 | return 0; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /* |
| 179 | * Given a 64-bit response, decode to our card SCR structure. |
| 180 | */ |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 181 | static int mmc_decode_scr(struct mmc_card *card) |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 182 | { |
| 183 | struct sd_scr *scr = &card->scr; |
| 184 | unsigned int scr_struct; |
| 185 | u32 resp[4]; |
| 186 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 187 | resp[3] = card->raw_scr[1]; |
| 188 | resp[2] = card->raw_scr[0]; |
| 189 | |
| 190 | scr_struct = UNSTUFF_BITS(resp, 60, 4); |
| 191 | if (scr_struct != 0) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 192 | pr_err("%s: unrecognised SCR structure version %d\n", |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 193 | mmc_hostname(card->host), scr_struct); |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 194 | return -EINVAL; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4); |
| 198 | scr->bus_widths = UNSTUFF_BITS(resp, 48, 4); |
Arindam Nath | 013909c | 2011-05-05 12:18:58 +0530 | [diff] [blame] | 199 | if (scr->sda_vsn == SCR_SPEC_VER_2) |
| 200 | /* Check if Physical Layer Spec v3.0 is supported */ |
| 201 | scr->sda_spec3 = UNSTUFF_BITS(resp, 47, 1); |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 202 | |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 203 | if (UNSTUFF_BITS(resp, 55, 1)) |
| 204 | card->erased_byte = 0xFF; |
| 205 | else |
| 206 | card->erased_byte = 0x0; |
| 207 | |
Andrei Warkentin | f0d8997 | 2011-05-23 15:06:38 -0500 | [diff] [blame] | 208 | if (scr->sda_spec3) |
| 209 | scr->cmds = UNSTUFF_BITS(resp, 32, 2); |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 210 | return 0; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /* |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 214 | * Fetch and process SD Status register. |
| 215 | */ |
| 216 | static int mmc_read_ssr(struct mmc_card *card) |
| 217 | { |
| 218 | unsigned int au, es, et, eo; |
| 219 | int err, i; |
| 220 | u32 *ssr; |
| 221 | |
| 222 | if (!(card->csd.cmdclass & CCC_APP_SPEC)) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 223 | pr_warning("%s: card lacks mandatory SD Status " |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 224 | "function.\n", mmc_hostname(card->host)); |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | ssr = kmalloc(64, GFP_KERNEL); |
| 229 | if (!ssr) |
| 230 | return -ENOMEM; |
| 231 | |
| 232 | err = mmc_app_sd_status(card, ssr); |
| 233 | if (err) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 234 | pr_warning("%s: problem reading SD Status " |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 235 | "register.\n", mmc_hostname(card->host)); |
| 236 | err = 0; |
| 237 | goto out; |
| 238 | } |
| 239 | |
| 240 | for (i = 0; i < 16; i++) |
| 241 | ssr[i] = be32_to_cpu(ssr[i]); |
| 242 | |
| 243 | /* |
| 244 | * UNSTUFF_BITS only works with four u32s so we have to offset the |
| 245 | * bitfield positions accordingly. |
| 246 | */ |
| 247 | au = UNSTUFF_BITS(ssr, 428 - 384, 4); |
| 248 | if (au > 0 || au <= 9) { |
| 249 | card->ssr.au = 1 << (au + 4); |
| 250 | es = UNSTUFF_BITS(ssr, 408 - 384, 16); |
| 251 | et = UNSTUFF_BITS(ssr, 402 - 384, 6); |
| 252 | eo = UNSTUFF_BITS(ssr, 400 - 384, 2); |
| 253 | if (es && et) { |
| 254 | card->ssr.erase_timeout = (et * 1000) / es; |
| 255 | card->ssr.erase_offset = eo * 1000; |
| 256 | } |
| 257 | } else { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 258 | pr_warning("%s: SD Status: Invalid Allocation Unit " |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 259 | "size.\n", mmc_hostname(card->host)); |
| 260 | } |
| 261 | out: |
| 262 | kfree(ssr); |
| 263 | return err; |
| 264 | } |
| 265 | |
| 266 | /* |
Pierre Ossman | 1addfcd | 2007-05-01 14:46:08 +0200 | [diff] [blame] | 267 | * Fetches and decodes switch information |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 268 | */ |
Pierre Ossman | 1addfcd | 2007-05-01 14:46:08 +0200 | [diff] [blame] | 269 | static int mmc_read_switch(struct mmc_card *card) |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 270 | { |
| 271 | int err; |
| 272 | u8 *status; |
| 273 | |
Pierre Ossman | 3373c0a | 2007-05-31 22:25:11 +0200 | [diff] [blame] | 274 | if (card->scr.sda_vsn < SCR_SPEC_VER_1) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 275 | return 0; |
Pierre Ossman | 3373c0a | 2007-05-31 22:25:11 +0200 | [diff] [blame] | 276 | |
| 277 | if (!(card->csd.cmdclass & CCC_SWITCH)) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 278 | pr_warning("%s: card lacks mandatory switch " |
Pierre Ossman | 3373c0a | 2007-05-31 22:25:11 +0200 | [diff] [blame] | 279 | "function, performance might suffer.\n", |
| 280 | mmc_hostname(card->host)); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 281 | return 0; |
Pierre Ossman | 3373c0a | 2007-05-31 22:25:11 +0200 | [diff] [blame] | 282 | } |
| 283 | |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 284 | err = -EIO; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 285 | |
| 286 | status = kmalloc(64, GFP_KERNEL); |
| 287 | if (!status) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 288 | pr_err("%s: could not allocate a buffer for " |
Arindam Nath | 013909c | 2011-05-05 12:18:58 +0530 | [diff] [blame] | 289 | "switch capabilities.\n", |
| 290 | mmc_hostname(card->host)); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 291 | return -ENOMEM; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 292 | } |
| 293 | |
Arindam Nath | 013909c | 2011-05-05 12:18:58 +0530 | [diff] [blame] | 294 | /* Find out the supported Bus Speed Modes. */ |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 295 | err = mmc_sd_switch(card, 0, 0, 1, status); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 296 | if (err) { |
Arindam Nath | 013909c | 2011-05-05 12:18:58 +0530 | [diff] [blame] | 297 | /* |
| 298 | * If the host or the card can't do the switch, |
| 299 | * fail more gracefully. |
| 300 | */ |
| 301 | if (err != -EINVAL && err != -ENOSYS && err != -EFAULT) |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 302 | goto out; |
| 303 | |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 304 | pr_warning("%s: problem reading Bus Speed modes.\n", |
Pierre Ossman | 3373c0a | 2007-05-31 22:25:11 +0200 | [diff] [blame] | 305 | mmc_hostname(card->host)); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 306 | err = 0; |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 307 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 308 | goto out; |
| 309 | } |
| 310 | |
Qiang Liu | fffe5d5 | 2011-11-08 08:43:08 -0500 | [diff] [blame] | 311 | if (status[13] & SD_MODE_HIGH_SPEED) |
| 312 | card->sw_caps.hs_max_dtr = HIGH_SPEED_MAX_DTR; |
Subhash Jadavani | f2815f6 | 2011-08-10 11:16:01 +0530 | [diff] [blame] | 313 | |
Arindam Nath | 013909c | 2011-05-05 12:18:58 +0530 | [diff] [blame] | 314 | if (card->scr.sda_spec3) { |
| 315 | card->sw_caps.sd3_bus_mode = status[13]; |
| 316 | |
| 317 | /* Find out Driver Strengths supported by the card */ |
| 318 | err = mmc_sd_switch(card, 0, 2, 1, status); |
| 319 | if (err) { |
| 320 | /* |
| 321 | * If the host or the card can't do the switch, |
| 322 | * fail more gracefully. |
| 323 | */ |
| 324 | if (err != -EINVAL && err != -ENOSYS && err != -EFAULT) |
| 325 | goto out; |
| 326 | |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 327 | pr_warning("%s: problem reading " |
Arindam Nath | 013909c | 2011-05-05 12:18:58 +0530 | [diff] [blame] | 328 | "Driver Strength.\n", |
| 329 | mmc_hostname(card->host)); |
| 330 | err = 0; |
| 331 | |
| 332 | goto out; |
| 333 | } |
| 334 | |
| 335 | card->sw_caps.sd3_drv_type = status[9]; |
| 336 | |
| 337 | /* Find out Current Limits supported by the card */ |
| 338 | err = mmc_sd_switch(card, 0, 3, 1, status); |
| 339 | if (err) { |
| 340 | /* |
| 341 | * If the host or the card can't do the switch, |
| 342 | * fail more gracefully. |
| 343 | */ |
| 344 | if (err != -EINVAL && err != -ENOSYS && err != -EFAULT) |
| 345 | goto out; |
| 346 | |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 347 | pr_warning("%s: problem reading " |
Arindam Nath | 013909c | 2011-05-05 12:18:58 +0530 | [diff] [blame] | 348 | "Current Limit.\n", |
| 349 | mmc_hostname(card->host)); |
| 350 | err = 0; |
| 351 | |
| 352 | goto out; |
| 353 | } |
| 354 | |
| 355 | card->sw_caps.sd3_curr_limit = status[7]; |
Arindam Nath | 013909c | 2011-05-05 12:18:58 +0530 | [diff] [blame] | 356 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 357 | |
Pierre Ossman | 1addfcd | 2007-05-01 14:46:08 +0200 | [diff] [blame] | 358 | out: |
| 359 | kfree(status); |
| 360 | |
| 361 | return err; |
| 362 | } |
| 363 | |
| 364 | /* |
| 365 | * Test if the card supports high-speed mode and, if so, switch to it. |
| 366 | */ |
Michal Miroslaw | 71578a1 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 367 | int mmc_sd_switch_hs(struct mmc_card *card) |
Pierre Ossman | 1addfcd | 2007-05-01 14:46:08 +0200 | [diff] [blame] | 368 | { |
| 369 | int err; |
| 370 | u8 *status; |
| 371 | |
Pierre Ossman | 3373c0a | 2007-05-31 22:25:11 +0200 | [diff] [blame] | 372 | if (card->scr.sda_vsn < SCR_SPEC_VER_1) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 373 | return 0; |
Pierre Ossman | 3373c0a | 2007-05-31 22:25:11 +0200 | [diff] [blame] | 374 | |
| 375 | if (!(card->csd.cmdclass & CCC_SWITCH)) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 376 | return 0; |
Pierre Ossman | 3373c0a | 2007-05-31 22:25:11 +0200 | [diff] [blame] | 377 | |
Pierre Ossman | 1addfcd | 2007-05-01 14:46:08 +0200 | [diff] [blame] | 378 | if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED)) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 379 | return 0; |
Pierre Ossman | 1addfcd | 2007-05-01 14:46:08 +0200 | [diff] [blame] | 380 | |
| 381 | if (card->sw_caps.hs_max_dtr == 0) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 382 | return 0; |
Pierre Ossman | 1addfcd | 2007-05-01 14:46:08 +0200 | [diff] [blame] | 383 | |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 384 | err = -EIO; |
Pierre Ossman | 1addfcd | 2007-05-01 14:46:08 +0200 | [diff] [blame] | 385 | |
| 386 | status = kmalloc(64, GFP_KERNEL); |
| 387 | if (!status) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 388 | pr_err("%s: could not allocate a buffer for " |
Pierre Ossman | facba91 | 2007-07-24 21:53:43 +0200 | [diff] [blame] | 389 | "switch capabilities.\n", mmc_hostname(card->host)); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 390 | return -ENOMEM; |
Pierre Ossman | 1addfcd | 2007-05-01 14:46:08 +0200 | [diff] [blame] | 391 | } |
| 392 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 393 | err = mmc_sd_switch(card, 1, 0, 1, status); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 394 | if (err) |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 395 | goto out; |
| 396 | |
| 397 | if ((status[16] & 0xF) != 1) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 398 | pr_warning("%s: Problem switching card " |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 399 | "into high-speed mode!\n", |
| 400 | mmc_hostname(card->host)); |
Michal Miroslaw | 71578a1 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 401 | err = 0; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 402 | } else { |
Michal Miroslaw | 71578a1 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 403 | err = 1; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | out: |
| 407 | kfree(status); |
| 408 | |
| 409 | return err; |
| 410 | } |
| 411 | |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 412 | static int sd_select_driver_type(struct mmc_card *card, u8 *status) |
| 413 | { |
Philip Rakity | ca8e99b | 2011-07-06 08:51:32 -0700 | [diff] [blame] | 414 | int host_drv_type = SD_DRIVER_TYPE_B; |
| 415 | int card_drv_type = SD_DRIVER_TYPE_B; |
| 416 | int drive_strength; |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 417 | int err; |
| 418 | |
| 419 | /* |
| 420 | * If the host doesn't support any of the Driver Types A,C or D, |
Philip Rakity | ca8e99b | 2011-07-06 08:51:32 -0700 | [diff] [blame] | 421 | * or there is no board specific handler then default Driver |
| 422 | * Type B is used. |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 423 | */ |
| 424 | if (!(card->host->caps & (MMC_CAP_DRIVER_TYPE_A | MMC_CAP_DRIVER_TYPE_C |
| 425 | | MMC_CAP_DRIVER_TYPE_D))) |
| 426 | return 0; |
| 427 | |
Philip Rakity | ca8e99b | 2011-07-06 08:51:32 -0700 | [diff] [blame] | 428 | if (!card->host->ops->select_drive_strength) |
| 429 | return 0; |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 430 | |
Philip Rakity | ca8e99b | 2011-07-06 08:51:32 -0700 | [diff] [blame] | 431 | if (card->host->caps & MMC_CAP_DRIVER_TYPE_A) |
| 432 | host_drv_type |= SD_DRIVER_TYPE_A; |
| 433 | |
| 434 | if (card->host->caps & MMC_CAP_DRIVER_TYPE_C) |
| 435 | host_drv_type |= SD_DRIVER_TYPE_C; |
| 436 | |
| 437 | if (card->host->caps & MMC_CAP_DRIVER_TYPE_D) |
| 438 | host_drv_type |= SD_DRIVER_TYPE_D; |
| 439 | |
| 440 | if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_A) |
| 441 | card_drv_type |= SD_DRIVER_TYPE_A; |
| 442 | |
| 443 | if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_C) |
| 444 | card_drv_type |= SD_DRIVER_TYPE_C; |
| 445 | |
| 446 | if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_D) |
| 447 | card_drv_type |= SD_DRIVER_TYPE_D; |
| 448 | |
| 449 | /* |
| 450 | * The drive strength that the hardware can support |
| 451 | * depends on the board design. Pass the appropriate |
| 452 | * information and let the hardware specific code |
| 453 | * return what is possible given the options |
| 454 | */ |
Sujit Reddy Thumma | 2c4967f74 | 2012-02-04 16:14:50 -0500 | [diff] [blame] | 455 | mmc_host_clk_hold(card->host); |
Philip Rakity | ca8e99b | 2011-07-06 08:51:32 -0700 | [diff] [blame] | 456 | drive_strength = card->host->ops->select_drive_strength( |
| 457 | card->sw_caps.uhs_max_dtr, |
| 458 | host_drv_type, card_drv_type); |
Sujit Reddy Thumma | 2c4967f74 | 2012-02-04 16:14:50 -0500 | [diff] [blame] | 459 | mmc_host_clk_release(card->host); |
Philip Rakity | ca8e99b | 2011-07-06 08:51:32 -0700 | [diff] [blame] | 460 | |
| 461 | err = mmc_sd_switch(card, 1, 2, drive_strength, status); |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 462 | if (err) |
| 463 | return err; |
| 464 | |
Philip Rakity | ca8e99b | 2011-07-06 08:51:32 -0700 | [diff] [blame] | 465 | if ((status[15] & 0xF) != drive_strength) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 466 | pr_warning("%s: Problem setting drive strength!\n", |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 467 | mmc_hostname(card->host)); |
| 468 | return 0; |
| 469 | } |
| 470 | |
Philip Rakity | ca8e99b | 2011-07-06 08:51:32 -0700 | [diff] [blame] | 471 | mmc_set_driver_type(card->host, drive_strength); |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 472 | |
| 473 | return 0; |
| 474 | } |
| 475 | |
Subhash Jadavani | 93c712f | 2011-08-09 12:19:31 +0530 | [diff] [blame] | 476 | static void sd_update_bus_speed_mode(struct mmc_card *card) |
Arindam Nath | 49c468f | 2011-05-05 12:19:01 +0530 | [diff] [blame] | 477 | { |
Arindam Nath | 49c468f | 2011-05-05 12:19:01 +0530 | [diff] [blame] | 478 | /* |
| 479 | * If the host doesn't support any of the UHS-I modes, fallback on |
| 480 | * default speed. |
| 481 | */ |
| 482 | if (!(card->host->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | |
Subhash Jadavani | 93c712f | 2011-08-09 12:19:31 +0530 | [diff] [blame] | 483 | MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_DDR50))) { |
| 484 | card->sd_bus_speed = 0; |
| 485 | return; |
| 486 | } |
Arindam Nath | 49c468f | 2011-05-05 12:19:01 +0530 | [diff] [blame] | 487 | |
| 488 | if ((card->host->caps & MMC_CAP_UHS_SDR104) && |
| 489 | (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) { |
Subhash Jadavani | 93c712f | 2011-08-09 12:19:31 +0530 | [diff] [blame] | 490 | card->sd_bus_speed = UHS_SDR104_BUS_SPEED; |
Arindam Nath | 49c468f | 2011-05-05 12:19:01 +0530 | [diff] [blame] | 491 | } else if ((card->host->caps & MMC_CAP_UHS_DDR50) && |
| 492 | (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) { |
Subhash Jadavani | 93c712f | 2011-08-09 12:19:31 +0530 | [diff] [blame] | 493 | card->sd_bus_speed = UHS_DDR50_BUS_SPEED; |
Arindam Nath | 49c468f | 2011-05-05 12:19:01 +0530 | [diff] [blame] | 494 | } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 | |
| 495 | MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode & |
| 496 | SD_MODE_UHS_SDR50)) { |
Subhash Jadavani | 93c712f | 2011-08-09 12:19:31 +0530 | [diff] [blame] | 497 | card->sd_bus_speed = UHS_SDR50_BUS_SPEED; |
Arindam Nath | 49c468f | 2011-05-05 12:19:01 +0530 | [diff] [blame] | 498 | } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 | |
| 499 | MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) && |
| 500 | (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) { |
Subhash Jadavani | 93c712f | 2011-08-09 12:19:31 +0530 | [diff] [blame] | 501 | card->sd_bus_speed = UHS_SDR25_BUS_SPEED; |
Arindam Nath | 49c468f | 2011-05-05 12:19:01 +0530 | [diff] [blame] | 502 | } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 | |
| 503 | MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 | |
| 504 | MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode & |
| 505 | SD_MODE_UHS_SDR12)) { |
Subhash Jadavani | 93c712f | 2011-08-09 12:19:31 +0530 | [diff] [blame] | 506 | card->sd_bus_speed = UHS_SDR12_BUS_SPEED; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | static int sd_set_bus_speed_mode(struct mmc_card *card, u8 *status) |
| 511 | { |
| 512 | int err; |
| 513 | unsigned int timing = 0; |
| 514 | |
| 515 | switch (card->sd_bus_speed) { |
| 516 | case UHS_SDR104_BUS_SPEED: |
| 517 | timing = MMC_TIMING_UHS_SDR104; |
| 518 | card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR; |
| 519 | break; |
| 520 | case UHS_DDR50_BUS_SPEED: |
| 521 | timing = MMC_TIMING_UHS_DDR50; |
| 522 | card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR; |
| 523 | break; |
| 524 | case UHS_SDR50_BUS_SPEED: |
| 525 | timing = MMC_TIMING_UHS_SDR50; |
| 526 | card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR; |
| 527 | break; |
| 528 | case UHS_SDR25_BUS_SPEED: |
| 529 | timing = MMC_TIMING_UHS_SDR25; |
| 530 | card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR; |
| 531 | break; |
| 532 | case UHS_SDR12_BUS_SPEED: |
| 533 | timing = MMC_TIMING_UHS_SDR12; |
| 534 | card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR; |
| 535 | break; |
| 536 | default: |
| 537 | return 0; |
Arindam Nath | 49c468f | 2011-05-05 12:19:01 +0530 | [diff] [blame] | 538 | } |
| 539 | |
Subhash Jadavani | 93c712f | 2011-08-09 12:19:31 +0530 | [diff] [blame] | 540 | err = mmc_sd_switch(card, 1, 0, card->sd_bus_speed, status); |
Arindam Nath | 49c468f | 2011-05-05 12:19:01 +0530 | [diff] [blame] | 541 | if (err) |
| 542 | return err; |
| 543 | |
Subhash Jadavani | 93c712f | 2011-08-09 12:19:31 +0530 | [diff] [blame] | 544 | if ((status[16] & 0xF) != card->sd_bus_speed) |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 545 | pr_warning("%s: Problem setting bus speed mode!\n", |
Arindam Nath | 49c468f | 2011-05-05 12:19:01 +0530 | [diff] [blame] | 546 | mmc_hostname(card->host)); |
| 547 | else { |
| 548 | mmc_set_timing(card->host, timing); |
| 549 | mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr); |
| 550 | } |
| 551 | |
| 552 | return 0; |
| 553 | } |
| 554 | |
Arindam Nath | 5371c92 | 2011-05-05 12:19:02 +0530 | [diff] [blame] | 555 | static int sd_set_current_limit(struct mmc_card *card, u8 *status) |
| 556 | { |
| 557 | int current_limit = 0; |
| 558 | int err; |
| 559 | |
| 560 | /* |
| 561 | * Current limit switch is only defined for SDR50, SDR104, and DDR50 |
| 562 | * bus speed modes. For other bus speed modes, we set the default |
| 563 | * current limit of 200mA. |
| 564 | */ |
| 565 | if ((card->sd_bus_speed == UHS_SDR50_BUS_SPEED) || |
| 566 | (card->sd_bus_speed == UHS_SDR104_BUS_SPEED) || |
| 567 | (card->sd_bus_speed == UHS_DDR50_BUS_SPEED)) { |
| 568 | if (card->host->caps & MMC_CAP_MAX_CURRENT_800) { |
| 569 | if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_800) |
| 570 | current_limit = SD_SET_CURRENT_LIMIT_800; |
| 571 | else if (card->sw_caps.sd3_curr_limit & |
| 572 | SD_MAX_CURRENT_600) |
| 573 | current_limit = SD_SET_CURRENT_LIMIT_600; |
| 574 | else if (card->sw_caps.sd3_curr_limit & |
| 575 | SD_MAX_CURRENT_400) |
| 576 | current_limit = SD_SET_CURRENT_LIMIT_400; |
| 577 | else if (card->sw_caps.sd3_curr_limit & |
| 578 | SD_MAX_CURRENT_200) |
| 579 | current_limit = SD_SET_CURRENT_LIMIT_200; |
| 580 | } else if (card->host->caps & MMC_CAP_MAX_CURRENT_600) { |
| 581 | if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_600) |
| 582 | current_limit = SD_SET_CURRENT_LIMIT_600; |
| 583 | else if (card->sw_caps.sd3_curr_limit & |
| 584 | SD_MAX_CURRENT_400) |
| 585 | current_limit = SD_SET_CURRENT_LIMIT_400; |
| 586 | else if (card->sw_caps.sd3_curr_limit & |
| 587 | SD_MAX_CURRENT_200) |
| 588 | current_limit = SD_SET_CURRENT_LIMIT_200; |
| 589 | } else if (card->host->caps & MMC_CAP_MAX_CURRENT_400) { |
| 590 | if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_400) |
| 591 | current_limit = SD_SET_CURRENT_LIMIT_400; |
| 592 | else if (card->sw_caps.sd3_curr_limit & |
| 593 | SD_MAX_CURRENT_200) |
| 594 | current_limit = SD_SET_CURRENT_LIMIT_200; |
| 595 | } else if (card->host->caps & MMC_CAP_MAX_CURRENT_200) { |
| 596 | if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_200) |
| 597 | current_limit = SD_SET_CURRENT_LIMIT_200; |
| 598 | } |
| 599 | } else |
| 600 | current_limit = SD_SET_CURRENT_LIMIT_200; |
| 601 | |
| 602 | err = mmc_sd_switch(card, 1, 3, current_limit, status); |
| 603 | if (err) |
| 604 | return err; |
| 605 | |
| 606 | if (((status[15] >> 4) & 0x0F) != current_limit) |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 607 | pr_warning("%s: Problem setting current limit!\n", |
Arindam Nath | 5371c92 | 2011-05-05 12:19:02 +0530 | [diff] [blame] | 608 | mmc_hostname(card->host)); |
| 609 | |
| 610 | return 0; |
| 611 | } |
| 612 | |
Sujit Reddy Thumma | 1cd61b7 | 2012-08-06 11:23:21 +0530 | [diff] [blame] | 613 | /** |
| 614 | * mmc_sd_change_bus_speed() - Change SD card bus frequency at runtime |
| 615 | * @host: pointer to mmc host structure |
| 616 | * @freq: pointer to desired frequency to be set |
| 617 | * |
| 618 | * Change the SD card bus frequency at runtime after the card is |
| 619 | * initialized. Callers are expected to make sure of the card's |
| 620 | * state (DATA/RCV/TRANSFER) beforing changing the frequency at runtime. |
| 621 | * |
| 622 | * If the frequency to change is greater than max. supported by card, |
| 623 | * *freq is changed to max. supported by card and if it is less than min. |
| 624 | * supported by host, *freq is changed to min. supported by host. |
| 625 | */ |
| 626 | static int mmc_sd_change_bus_speed(struct mmc_host *host, unsigned long *freq) |
| 627 | { |
| 628 | int err = 0; |
| 629 | struct mmc_card *card; |
| 630 | |
| 631 | mmc_claim_host(host); |
| 632 | /* |
| 633 | * Assign card pointer after claiming host to avoid race |
| 634 | * conditions that may arise during removal of the card. |
| 635 | */ |
| 636 | card = host->card; |
| 637 | |
| 638 | /* sanity checks */ |
| 639 | if (!card || !freq) { |
| 640 | err = -EINVAL; |
| 641 | goto out; |
| 642 | } |
| 643 | |
| 644 | if (mmc_card_uhs(card)) { |
| 645 | if (*freq > card->sw_caps.uhs_max_dtr) |
| 646 | *freq = card->sw_caps.uhs_max_dtr; |
| 647 | } else { |
| 648 | if (*freq > mmc_sd_get_max_clock(card)) |
| 649 | *freq = mmc_sd_get_max_clock(card); |
| 650 | } |
| 651 | |
| 652 | if (*freq < host->f_min) |
| 653 | *freq = host->f_min; |
| 654 | |
| 655 | mmc_set_clock(host, (unsigned int) (*freq)); |
| 656 | |
| 657 | if (!mmc_host_is_spi(card->host) && mmc_sd_card_uhs(card) |
| 658 | && card->host->ops->execute_tuning) { |
| 659 | /* |
| 660 | * We try to probe host driver for tuning for any |
| 661 | * frequency, it is host driver responsibility to |
| 662 | * perform actual tuning only when required. |
| 663 | */ |
| 664 | mmc_host_clk_hold(card->host); |
| 665 | err = card->host->ops->execute_tuning(card->host, |
| 666 | MMC_SEND_TUNING_BLOCK); |
| 667 | mmc_host_clk_release(card->host); |
| 668 | |
| 669 | if (err) |
| 670 | pr_warn("%s: %s: tuning execution failed %d\n", |
| 671 | mmc_hostname(card->host), __func__, err); |
| 672 | } |
| 673 | |
| 674 | out: |
| 675 | mmc_release_host(host); |
| 676 | return err; |
| 677 | } |
| 678 | |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 679 | /* |
| 680 | * UHS-I specific initialization procedure |
| 681 | */ |
| 682 | static int mmc_sd_init_uhs_card(struct mmc_card *card) |
| 683 | { |
| 684 | int err; |
| 685 | u8 *status; |
| 686 | |
| 687 | if (!card->scr.sda_spec3) |
| 688 | return 0; |
| 689 | |
| 690 | if (!(card->csd.cmdclass & CCC_SWITCH)) |
| 691 | return 0; |
| 692 | |
| 693 | status = kmalloc(64, GFP_KERNEL); |
| 694 | if (!status) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 695 | pr_err("%s: could not allocate a buffer for " |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 696 | "switch capabilities.\n", mmc_hostname(card->host)); |
| 697 | return -ENOMEM; |
| 698 | } |
| 699 | |
| 700 | /* Set 4-bit bus width */ |
| 701 | if ((card->host->caps & MMC_CAP_4_BIT_DATA) && |
| 702 | (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) { |
| 703 | err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4); |
| 704 | if (err) |
| 705 | goto out; |
| 706 | |
| 707 | mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4); |
| 708 | } |
| 709 | |
Subhash Jadavani | 93c712f | 2011-08-09 12:19:31 +0530 | [diff] [blame] | 710 | /* |
| 711 | * Select the bus speed mode depending on host |
| 712 | * and card capability. |
| 713 | */ |
| 714 | sd_update_bus_speed_mode(card); |
| 715 | |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 716 | /* Set the driver strength for the card */ |
| 717 | err = sd_select_driver_type(card, status); |
Arindam Nath | 49c468f | 2011-05-05 12:19:01 +0530 | [diff] [blame] | 718 | if (err) |
| 719 | goto out; |
| 720 | |
Subhash Jadavani | 93c712f | 2011-08-09 12:19:31 +0530 | [diff] [blame] | 721 | /* Set current limit for the card */ |
| 722 | err = sd_set_current_limit(card, status); |
Arindam Nath | 5371c92 | 2011-05-05 12:19:02 +0530 | [diff] [blame] | 723 | if (err) |
| 724 | goto out; |
| 725 | |
Subhash Jadavani | 93c712f | 2011-08-09 12:19:31 +0530 | [diff] [blame] | 726 | /* Set bus speed mode of the card */ |
| 727 | err = sd_set_bus_speed_mode(card, status); |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 728 | if (err) |
| 729 | goto out; |
| 730 | |
| 731 | /* SPI mode doesn't define CMD19 */ |
Sujit Reddy Thumma | 2c4967f74 | 2012-02-04 16:14:50 -0500 | [diff] [blame] | 732 | if (!mmc_host_is_spi(card->host) && card->host->ops->execute_tuning) { |
| 733 | mmc_host_clk_hold(card->host); |
Girish K S | a4924c7 | 2012-01-11 14:04:52 -0500 | [diff] [blame] | 734 | err = card->host->ops->execute_tuning(card->host, |
| 735 | MMC_SEND_TUNING_BLOCK); |
Sujit Reddy Thumma | 2c4967f74 | 2012-02-04 16:14:50 -0500 | [diff] [blame] | 736 | mmc_host_clk_release(card->host); |
| 737 | } |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 738 | |
| 739 | out: |
| 740 | kfree(status); |
| 741 | |
| 742 | return err; |
| 743 | } |
| 744 | |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 745 | MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1], |
| 746 | card->raw_cid[2], card->raw_cid[3]); |
| 747 | MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1], |
| 748 | card->raw_csd[2], card->raw_csd[3]); |
| 749 | MMC_DEV_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]); |
| 750 | 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] | 751 | MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9); |
| 752 | MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9); |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 753 | MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev); |
| 754 | MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev); |
| 755 | MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid); |
| 756 | MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name); |
| 757 | MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid); |
| 758 | MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial); |
| 759 | |
| 760 | |
| 761 | static struct attribute *sd_std_attrs[] = { |
| 762 | &dev_attr_cid.attr, |
| 763 | &dev_attr_csd.attr, |
| 764 | &dev_attr_scr.attr, |
| 765 | &dev_attr_date.attr, |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 766 | &dev_attr_erase_size.attr, |
| 767 | &dev_attr_preferred_erase_size.attr, |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 768 | &dev_attr_fwrev.attr, |
| 769 | &dev_attr_hwrev.attr, |
| 770 | &dev_attr_manfid.attr, |
| 771 | &dev_attr_name.attr, |
| 772 | &dev_attr_oemid.attr, |
| 773 | &dev_attr_serial.attr, |
| 774 | NULL, |
| 775 | }; |
| 776 | |
| 777 | static struct attribute_group sd_std_attr_group = { |
| 778 | .attrs = sd_std_attrs, |
| 779 | }; |
| 780 | |
David Brownell | a4dbd67 | 2009-06-24 10:06:31 -0700 | [diff] [blame] | 781 | static const struct attribute_group *sd_attr_groups[] = { |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 782 | &sd_std_attr_group, |
| 783 | NULL, |
| 784 | }; |
| 785 | |
Michal Miroslaw | 71578a1 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 786 | struct device_type sd_type = { |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 787 | .groups = sd_attr_groups, |
| 788 | }; |
| 789 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 790 | /* |
Michal Miroslaw | 71578a1 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 791 | * Fetch CID from card. |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 792 | */ |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 793 | int mmc_sd_get_cid(struct mmc_host *host, u32 ocr, u32 *cid, u32 *rocr) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 794 | { |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 795 | int err; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 796 | |
| 797 | /* |
| 798 | * Since we're changing the OCR value, we seem to |
| 799 | * need to tell some cards to go back to the idle |
| 800 | * state. We wait 1ms to give cards time to |
| 801 | * respond. |
| 802 | */ |
| 803 | mmc_go_idle(host); |
| 804 | |
| 805 | /* |
| 806 | * If SD_SEND_IF_COND indicates an SD 2.0 |
| 807 | * compliant card and we should set bit 30 |
| 808 | * of the ocr to indicate that we can handle |
| 809 | * block-addressed SDHC cards. |
| 810 | */ |
| 811 | err = mmc_send_if_cond(host, ocr); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 812 | if (!err) |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 813 | ocr |= SD_OCR_CCS; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 814 | |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 815 | /* |
| 816 | * If the host supports one of UHS-I modes, request the card |
| 817 | * to switch to 1.8V signaling level. |
| 818 | */ |
| 819 | if (host->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | |
| 820 | MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_DDR50)) |
| 821 | ocr |= SD_OCR_S18R; |
| 822 | |
| 823 | /* If the host can supply more than 150mA, XPC should be set to 1. */ |
| 824 | if (host->caps & (MMC_CAP_SET_XPC_330 | MMC_CAP_SET_XPC_300 | |
| 825 | MMC_CAP_SET_XPC_180)) |
| 826 | ocr |= SD_OCR_XPC; |
| 827 | |
| 828 | try_again: |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 829 | err = mmc_send_app_op_cond(host, ocr, rocr); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 830 | if (err) |
Michal Miroslaw | 71578a1 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 831 | return err; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 832 | |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 833 | /* |
| 834 | * In case CCS and S18A in the response is set, start Signal Voltage |
| 835 | * Switch procedure. SPI mode doesn't support CMD11. |
| 836 | */ |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 837 | if (!mmc_host_is_spi(host) && rocr && |
| 838 | ((*rocr & 0x41000000) == 0x41000000)) { |
Philip Rakity | 261bbd4 | 2011-05-13 11:17:17 +0530 | [diff] [blame] | 839 | err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180, true); |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 840 | if (err) { |
| 841 | ocr &= ~SD_OCR_S18R; |
| 842 | goto try_again; |
| 843 | } |
| 844 | } |
| 845 | |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 846 | if (mmc_host_is_spi(host)) |
| 847 | err = mmc_send_cid(host, cid); |
| 848 | else |
| 849 | err = mmc_all_send_cid(host, cid); |
Michal Miroslaw | 71578a1 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 850 | |
| 851 | return err; |
| 852 | } |
| 853 | |
| 854 | int mmc_sd_get_csd(struct mmc_host *host, struct mmc_card *card) |
| 855 | { |
| 856 | int err; |
| 857 | |
| 858 | /* |
| 859 | * Fetch CSD from card. |
| 860 | */ |
| 861 | err = mmc_send_csd(card, card->raw_csd); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 862 | if (err) |
Michal Miroslaw | 71578a1 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 863 | return err; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 864 | |
Michal Miroslaw | 71578a1 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 865 | err = mmc_decode_csd(card); |
| 866 | if (err) |
| 867 | return err; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 868 | |
Michal Miroslaw | 71578a1 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 869 | return 0; |
| 870 | } |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 871 | |
Michal Miroslaw | 71578a1 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 872 | int mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card, |
| 873 | bool reinit) |
| 874 | { |
| 875 | int err; |
San Mehat | 8ed5a66 | 2008-12-04 11:18:00 -0800 | [diff] [blame] | 876 | #ifdef CONFIG_MMC_PARANOID_SD_INIT |
| 877 | int retries; |
| 878 | #endif |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 879 | |
Michal Miroslaw | 71578a1 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 880 | if (!reinit) { |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 881 | /* |
| 882 | * Fetch SCR from card. |
| 883 | */ |
| 884 | err = mmc_app_send_scr(card, card->raw_scr); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 885 | if (err) |
Michal Miroslaw | 71578a1 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 886 | return err; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 887 | |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 888 | err = mmc_decode_scr(card); |
Michal Miroslaw | 71578a1 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 889 | if (err) |
| 890 | return err; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 891 | |
| 892 | /* |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 893 | * Fetch and process SD Status register. |
| 894 | */ |
| 895 | err = mmc_read_ssr(card); |
| 896 | if (err) |
| 897 | return err; |
| 898 | |
| 899 | /* Erase init depends on CSD and SSR */ |
| 900 | mmc_init_erase(card); |
| 901 | |
| 902 | /* |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 903 | * Fetch switch information from card. |
| 904 | */ |
San Mehat | c80f546 | 2008-11-11 09:35:36 -0800 | [diff] [blame] | 905 | #ifdef CONFIG_MMC_PARANOID_SD_INIT |
| 906 | for (retries = 1; retries <= 3; retries++) { |
| 907 | err = mmc_read_switch(card); |
| 908 | if (!err) { |
| 909 | if (retries > 1) { |
| 910 | printk(KERN_WARNING |
| 911 | "%s: recovered\n", |
| 912 | mmc_hostname(host)); |
| 913 | } |
| 914 | break; |
| 915 | } else { |
| 916 | printk(KERN_WARNING |
| 917 | "%s: read switch failed (attempt %d)\n", |
| 918 | mmc_hostname(host), retries); |
| 919 | } |
| 920 | } |
| 921 | #else |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 922 | err = mmc_read_switch(card); |
San Mehat | c80f546 | 2008-11-11 09:35:36 -0800 | [diff] [blame] | 923 | #endif |
| 924 | |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 925 | if (err) |
Michal Miroslaw | 71578a1 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 926 | return err; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | /* |
Wolfgang Muees | 9d9f25c | 2009-04-07 14:48:16 +0100 | [diff] [blame] | 930 | * For SPI, enable CRC as appropriate. |
| 931 | * This CRC enable is located AFTER the reading of the |
| 932 | * card registers because some SDHC cards are not able |
| 933 | * to provide valid CRCs for non-512-byte blocks. |
| 934 | */ |
| 935 | if (mmc_host_is_spi(host)) { |
| 936 | err = mmc_spi_set_crc(host, use_spi_crc); |
| 937 | if (err) |
Michal Miroslaw | 71578a1 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 938 | return err; |
Wolfgang Muees | 9d9f25c | 2009-04-07 14:48:16 +0100 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | /* |
Michal Miroslaw | 71578a1 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 942 | * Check if read-only switch is active. |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 943 | */ |
Michal Miroslaw | 71578a1 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 944 | if (!reinit) { |
| 945 | int ro = -1; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 946 | |
Sujit Reddy Thumma | 2c4967f74 | 2012-02-04 16:14:50 -0500 | [diff] [blame] | 947 | if (host->ops->get_ro) { |
Stephen Boyd | 3f113aa | 2013-02-11 14:30:14 -0800 | [diff] [blame] | 948 | mmc_host_clk_hold(card->host); |
Michal Miroslaw | 71578a1 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 949 | ro = host->ops->get_ro(host); |
Stephen Boyd | 3f113aa | 2013-02-11 14:30:14 -0800 | [diff] [blame] | 950 | mmc_host_clk_release(card->host); |
Sujit Reddy Thumma | 2c4967f74 | 2012-02-04 16:14:50 -0500 | [diff] [blame] | 951 | } |
Michal Miroslaw | 71578a1 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 952 | |
| 953 | if (ro < 0) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 954 | pr_warning("%s: host does not " |
Michal Miroslaw | 71578a1 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 955 | "support reading read-only " |
| 956 | "switch. assuming write-enable.\n", |
| 957 | mmc_hostname(host)); |
| 958 | } else if (ro > 0) { |
| 959 | mmc_card_set_readonly(card); |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | return 0; |
| 964 | } |
| 965 | |
| 966 | unsigned mmc_sd_get_max_clock(struct mmc_card *card) |
| 967 | { |
| 968 | unsigned max_dtr = (unsigned int)-1; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 969 | |
| 970 | if (mmc_card_highspeed(card)) { |
| 971 | if (max_dtr > card->sw_caps.hs_max_dtr) |
| 972 | max_dtr = card->sw_caps.hs_max_dtr; |
| 973 | } else if (max_dtr > card->csd.max_dtr) { |
| 974 | max_dtr = card->csd.max_dtr; |
| 975 | } |
| 976 | |
Michal Miroslaw | 71578a1 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 977 | return max_dtr; |
| 978 | } |
| 979 | |
| 980 | void mmc_sd_go_highspeed(struct mmc_card *card) |
| 981 | { |
| 982 | mmc_card_set_highspeed(card); |
| 983 | mmc_set_timing(card->host, MMC_TIMING_SD_HS); |
| 984 | } |
| 985 | |
| 986 | /* |
| 987 | * Handle the detection and initialisation of a card. |
| 988 | * |
| 989 | * In the case of a resume, "oldcard" will contain the card |
| 990 | * we're trying to reinitialise. |
| 991 | */ |
| 992 | static int mmc_sd_init_card(struct mmc_host *host, u32 ocr, |
| 993 | struct mmc_card *oldcard) |
| 994 | { |
| 995 | struct mmc_card *card; |
| 996 | int err; |
| 997 | u32 cid[4]; |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 998 | u32 rocr = 0; |
Michal Miroslaw | 71578a1 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 999 | |
| 1000 | BUG_ON(!host); |
| 1001 | WARN_ON(!host->claimed); |
| 1002 | |
Ulf Hansson | e774747 | 2012-03-01 13:18:05 +0100 | [diff] [blame] | 1003 | /* The initialization should be done at 3.3 V I/O voltage. */ |
| 1004 | mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330, 0); |
| 1005 | |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 1006 | err = mmc_sd_get_cid(host, ocr, cid, &rocr); |
Michal Miroslaw | 71578a1 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 1007 | if (err) |
| 1008 | return err; |
| 1009 | |
| 1010 | if (oldcard) { |
| 1011 | if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) |
| 1012 | return -ENOENT; |
| 1013 | |
| 1014 | card = oldcard; |
| 1015 | } else { |
| 1016 | /* |
| 1017 | * Allocate card structure. |
| 1018 | */ |
| 1019 | card = mmc_alloc_card(host, &sd_type); |
| 1020 | if (IS_ERR(card)) |
| 1021 | return PTR_ERR(card); |
| 1022 | |
| 1023 | card->type = MMC_TYPE_SD; |
| 1024 | memcpy(card->raw_cid, cid, sizeof(card->raw_cid)); |
| 1025 | } |
| 1026 | |
| 1027 | /* |
| 1028 | * For native busses: get card RCA and quit open drain mode. |
| 1029 | */ |
| 1030 | if (!mmc_host_is_spi(host)) { |
| 1031 | err = mmc_send_relative_addr(host, &card->rca); |
| 1032 | if (err) |
| 1033 | return err; |
Michal Miroslaw | 71578a1 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | if (!oldcard) { |
| 1037 | err = mmc_sd_get_csd(host, card); |
| 1038 | if (err) |
| 1039 | return err; |
| 1040 | |
| 1041 | mmc_decode_cid(card); |
| 1042 | } |
| 1043 | |
| 1044 | /* |
| 1045 | * Select card, as all following commands rely on that. |
| 1046 | */ |
| 1047 | if (!mmc_host_is_spi(host)) { |
| 1048 | err = mmc_select_card(card); |
| 1049 | if (err) |
| 1050 | return err; |
| 1051 | } |
| 1052 | |
| 1053 | err = mmc_sd_setup_card(host, card, oldcard != NULL); |
| 1054 | if (err) |
| 1055 | goto free_card; |
| 1056 | |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 1057 | /* Initialization sequence for UHS-I cards */ |
| 1058 | if (rocr & SD_ROCR_S18A) { |
| 1059 | err = mmc_sd_init_uhs_card(card); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 1060 | if (err) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1061 | goto free_card; |
Arindam Nath | 3a30351 | 2011-05-05 12:19:03 +0530 | [diff] [blame] | 1062 | |
| 1063 | /* Card is an ultra-high-speed card */ |
Philip Rakity | a303c53 | 2011-11-14 19:14:38 -0800 | [diff] [blame] | 1064 | mmc_card_set_uhs(card); |
Arindam Nath | 4d55c5a | 2011-05-05 12:19:05 +0530 | [diff] [blame] | 1065 | |
| 1066 | /* |
| 1067 | * Since initialization is now complete, enable preset |
| 1068 | * value registers for UHS-I cards. |
| 1069 | */ |
Sujit Reddy Thumma | 2c4967f74 | 2012-02-04 16:14:50 -0500 | [diff] [blame] | 1070 | if (host->ops->enable_preset_value) { |
Stephen Boyd | 3f113aa | 2013-02-11 14:30:14 -0800 | [diff] [blame] | 1071 | mmc_host_clk_hold(card->host); |
Arindam Nath | 4d55c5a | 2011-05-05 12:19:05 +0530 | [diff] [blame] | 1072 | host->ops->enable_preset_value(host, true); |
Stephen Boyd | 3f113aa | 2013-02-11 14:30:14 -0800 | [diff] [blame] | 1073 | mmc_host_clk_release(card->host); |
Sujit Reddy Thumma | 2c4967f74 | 2012-02-04 16:14:50 -0500 | [diff] [blame] | 1074 | } |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 1075 | } else { |
| 1076 | /* |
| 1077 | * Attempt to change to high-speed (if supported) |
| 1078 | */ |
| 1079 | err = mmc_sd_switch_hs(card); |
| 1080 | if (err > 0) |
| 1081 | mmc_sd_go_highspeed(card); |
| 1082 | else if (err) |
| 1083 | goto free_card; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1084 | |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 1085 | /* |
| 1086 | * Set bus speed. |
| 1087 | */ |
| 1088 | mmc_set_clock(host, mmc_sd_get_max_clock(card)); |
| 1089 | |
| 1090 | /* |
| 1091 | * Switch to wider bus (if supported). |
| 1092 | */ |
| 1093 | if ((host->caps & MMC_CAP_4_BIT_DATA) && |
| 1094 | (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) { |
| 1095 | err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4); |
| 1096 | if (err) |
| 1097 | goto free_card; |
| 1098 | |
| 1099 | mmc_set_bus_width(host, MMC_BUS_WIDTH_4); |
| 1100 | } |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1101 | } |
| 1102 | |
Michal Miroslaw | 71578a1 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 1103 | host->card = card; |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 1104 | return 0; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1105 | |
| 1106 | free_card: |
| 1107 | if (!oldcard) |
| 1108 | mmc_remove_card(card); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1109 | |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 1110 | return err; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1114 | * Host is being removed. Free up the current card. |
| 1115 | */ |
| 1116 | static void mmc_sd_remove(struct mmc_host *host) |
| 1117 | { |
| 1118 | BUG_ON(!host); |
| 1119 | BUG_ON(!host->card); |
| 1120 | |
| 1121 | mmc_remove_card(host->card); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1122 | |
| 1123 | mmc_claim_host(host); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1124 | host->card = NULL; |
Sujit Reddy Thumma | 51d57b3 | 2012-10-11 17:17:03 +0530 | [diff] [blame] | 1125 | mmc_exit_clk_scaling(host); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1126 | mmc_release_host(host); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | /* |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 1130 | * Card detection - card is alive. |
| 1131 | */ |
| 1132 | static int mmc_sd_alive(struct mmc_host *host) |
| 1133 | { |
| 1134 | return mmc_send_status(host->card, NULL); |
| 1135 | } |
| 1136 | |
| 1137 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1138 | * Card detection callback from host. |
| 1139 | */ |
| 1140 | static void mmc_sd_detect(struct mmc_host *host) |
| 1141 | { |
San Mehat | 8ed5a66 | 2008-12-04 11:18:00 -0800 | [diff] [blame] | 1142 | int err = 0; |
| 1143 | #ifdef CONFIG_MMC_PARANOID_SD_INIT |
| 1144 | int retries = 5; |
| 1145 | #endif |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1146 | |
| 1147 | BUG_ON(!host); |
| 1148 | BUG_ON(!host->card); |
Asutosh Das | bbc8478 | 2013-02-11 15:31:35 +0530 | [diff] [blame] | 1149 | |
| 1150 | mmc_rpm_hold(host, &host->card->dev); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1151 | mmc_claim_host(host); |
| 1152 | |
| 1153 | /* |
| 1154 | * Just check if our card has been removed. |
| 1155 | */ |
San Mehat | 8ed5a66 | 2008-12-04 11:18:00 -0800 | [diff] [blame] | 1156 | #ifdef CONFIG_MMC_PARANOID_SD_INIT |
| 1157 | while(retries) { |
| 1158 | err = mmc_send_status(host->card, NULL); |
| 1159 | if (err) { |
| 1160 | retries--; |
| 1161 | udelay(5); |
| 1162 | continue; |
| 1163 | } |
| 1164 | break; |
| 1165 | } |
| 1166 | if (!retries) { |
| 1167 | printk(KERN_ERR "%s(%s): Unable to re-detect card (%d)\n", |
| 1168 | __func__, mmc_hostname(host), err); |
| 1169 | } |
| 1170 | #else |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 1171 | err = _mmc_detect_card_removed(host); |
San Mehat | 8ed5a66 | 2008-12-04 11:18:00 -0800 | [diff] [blame] | 1172 | #endif |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1173 | mmc_release_host(host); |
| 1174 | |
Asutosh Das | bbc8478 | 2013-02-11 15:31:35 +0530 | [diff] [blame] | 1175 | /* |
| 1176 | * if detect fails, the device would be removed anyway; |
| 1177 | * the rpm framework would mark the device state suspended. |
| 1178 | */ |
| 1179 | if (!err) |
| 1180 | mmc_rpm_release(host, &host->card->dev); |
| 1181 | |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 1182 | if (err) { |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 1183 | mmc_sd_remove(host); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1184 | |
| 1185 | mmc_claim_host(host); |
| 1186 | mmc_detach_bus(host); |
Ulf Hansson | 7f7e412 | 2011-09-21 14:08:13 -0400 | [diff] [blame] | 1187 | mmc_power_off(host); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1188 | mmc_release_host(host); |
| 1189 | } |
| 1190 | } |
| 1191 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1192 | /* |
| 1193 | * Suspend callback from host. |
| 1194 | */ |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 1195 | static int mmc_sd_suspend(struct mmc_host *host) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1196 | { |
| 1197 | BUG_ON(!host); |
| 1198 | BUG_ON(!host->card); |
| 1199 | |
Sujit Reddy Thumma | 51d57b3 | 2012-10-11 17:17:03 +0530 | [diff] [blame] | 1200 | /* |
| 1201 | * Disable clock scaling before suspend and enable it after resume so |
| 1202 | * as to avoid clock scaling decisions kicking in during this window. |
| 1203 | */ |
| 1204 | mmc_disable_clk_scaling(host); |
| 1205 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1206 | mmc_claim_host(host); |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 1207 | if (!mmc_host_is_spi(host)) |
| 1208 | mmc_deselect_cards(host); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1209 | host->card->state &= ~MMC_STATE_HIGHSPEED; |
| 1210 | mmc_release_host(host); |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 1211 | |
| 1212 | return 0; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | /* |
| 1216 | * Resume callback from host. |
| 1217 | * |
| 1218 | * This function tries to determine if the same card is still present |
| 1219 | * and, if so, restore all state to it. |
| 1220 | */ |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 1221 | static int mmc_sd_resume(struct mmc_host *host) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1222 | { |
| 1223 | int err; |
San Mehat | 05063bf | 2008-12-01 08:52:34 -0800 | [diff] [blame] | 1224 | #ifdef CONFIG_MMC_PARANOID_SD_INIT |
| 1225 | int retries; |
| 1226 | #endif |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1227 | |
| 1228 | BUG_ON(!host); |
| 1229 | BUG_ON(!host->card); |
| 1230 | |
| 1231 | mmc_claim_host(host); |
San Mehat | 05063bf | 2008-12-01 08:52:34 -0800 | [diff] [blame] | 1232 | #ifdef CONFIG_MMC_PARANOID_SD_INIT |
| 1233 | retries = 5; |
| 1234 | while (retries) { |
| 1235 | err = mmc_sd_init_card(host, host->ocr, host->card); |
| 1236 | |
| 1237 | if (err) { |
| 1238 | printk(KERN_ERR "%s: Re-init card rc = %d (retries = %d)\n", |
| 1239 | mmc_hostname(host), err, retries); |
San Mehat | 05063bf | 2008-12-01 08:52:34 -0800 | [diff] [blame] | 1240 | retries--; |
Subhash Jadavani | 4dd5c44 | 2012-03-13 00:33:00 +0530 | [diff] [blame] | 1241 | mmc_power_off(host); |
| 1242 | usleep_range(5000, 5500); |
| 1243 | mmc_power_up(host); |
| 1244 | mmc_select_voltage(host, host->ocr); |
San Mehat | 05063bf | 2008-12-01 08:52:34 -0800 | [diff] [blame] | 1245 | continue; |
| 1246 | } |
| 1247 | break; |
| 1248 | } |
| 1249 | #else |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1250 | err = mmc_sd_init_card(host, host->ocr, host->card); |
San Mehat | 05063bf | 2008-12-01 08:52:34 -0800 | [diff] [blame] | 1251 | #endif |
Pierre Ossman | 2986d0b | 2007-07-22 17:52:06 +0200 | [diff] [blame] | 1252 | mmc_release_host(host); |
| 1253 | |
Sujit Reddy Thumma | 51d57b3 | 2012-10-11 17:17:03 +0530 | [diff] [blame] | 1254 | /* |
| 1255 | * We have done full initialization of the card, |
| 1256 | * reset the clk scale stats and current frequency. |
| 1257 | */ |
| 1258 | if (mmc_can_scale_clk(host)) |
| 1259 | mmc_init_clk_scaling(host); |
| 1260 | |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 1261 | return err; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1262 | } |
| 1263 | |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 1264 | static int mmc_sd_power_restore(struct mmc_host *host) |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1265 | { |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 1266 | int ret; |
| 1267 | |
Sujit Reddy Thumma | 51d57b3 | 2012-10-11 17:17:03 +0530 | [diff] [blame] | 1268 | /* Disable clk scaling to avoid switching frequencies intermittently */ |
| 1269 | mmc_disable_clk_scaling(host); |
| 1270 | |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1271 | host->card->state &= ~MMC_STATE_HIGHSPEED; |
| 1272 | mmc_claim_host(host); |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 1273 | ret = mmc_sd_init_card(host, host->ocr, host->card); |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1274 | mmc_release_host(host); |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 1275 | |
Sujit Reddy Thumma | 51d57b3 | 2012-10-11 17:17:03 +0530 | [diff] [blame] | 1276 | if (mmc_can_scale_clk(host)) |
| 1277 | mmc_init_clk_scaling(host); |
| 1278 | |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 1279 | return ret; |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1280 | } |
| 1281 | |
Adrian Hunter | 9feae24 | 2009-09-22 16:44:32 -0700 | [diff] [blame] | 1282 | static const struct mmc_bus_ops mmc_sd_ops = { |
| 1283 | .remove = mmc_sd_remove, |
| 1284 | .detect = mmc_sd_detect, |
| 1285 | .suspend = NULL, |
| 1286 | .resume = NULL, |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1287 | .power_restore = mmc_sd_power_restore, |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 1288 | .alive = mmc_sd_alive, |
Sujit Reddy Thumma | 1cd61b7 | 2012-08-06 11:23:21 +0530 | [diff] [blame] | 1289 | .change_bus_speed = mmc_sd_change_bus_speed, |
Adrian Hunter | 9feae24 | 2009-09-22 16:44:32 -0700 | [diff] [blame] | 1290 | }; |
| 1291 | |
| 1292 | static const struct mmc_bus_ops mmc_sd_ops_unsafe = { |
| 1293 | .remove = mmc_sd_remove, |
| 1294 | .detect = mmc_sd_detect, |
| 1295 | .suspend = mmc_sd_suspend, |
| 1296 | .resume = mmc_sd_resume, |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1297 | .power_restore = mmc_sd_power_restore, |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 1298 | .alive = mmc_sd_alive, |
Sujit Reddy Thumma | 1cd61b7 | 2012-08-06 11:23:21 +0530 | [diff] [blame] | 1299 | .change_bus_speed = mmc_sd_change_bus_speed, |
Adrian Hunter | 9feae24 | 2009-09-22 16:44:32 -0700 | [diff] [blame] | 1300 | }; |
| 1301 | |
| 1302 | static void mmc_sd_attach_bus_ops(struct mmc_host *host) |
| 1303 | { |
| 1304 | const struct mmc_bus_ops *bus_ops; |
| 1305 | |
Matt Fleming | 71d7d3d | 2010-09-27 09:42:19 +0100 | [diff] [blame] | 1306 | if (!mmc_card_is_removable(host)) |
Adrian Hunter | 9feae24 | 2009-09-22 16:44:32 -0700 | [diff] [blame] | 1307 | bus_ops = &mmc_sd_ops_unsafe; |
| 1308 | else |
| 1309 | bus_ops = &mmc_sd_ops; |
| 1310 | mmc_attach_bus(host, bus_ops); |
| 1311 | } |
| 1312 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1313 | /* |
| 1314 | * Starting point for SD card init. |
| 1315 | */ |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 1316 | int mmc_attach_sd(struct mmc_host *host) |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1317 | { |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1318 | int err; |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 1319 | u32 ocr; |
San Mehat | 8ed5a66 | 2008-12-04 11:18:00 -0800 | [diff] [blame] | 1320 | #ifdef CONFIG_MMC_PARANOID_SD_INIT |
| 1321 | int retries; |
| 1322 | #endif |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1323 | |
| 1324 | BUG_ON(!host); |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 1325 | WARN_ON(!host->claimed); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1326 | |
Arindam Nath | 4d55c5a | 2011-05-05 12:19:05 +0530 | [diff] [blame] | 1327 | /* Disable preset value enable if already set since last time */ |
Sujit Reddy Thumma | 2c4967f74 | 2012-02-04 16:14:50 -0500 | [diff] [blame] | 1328 | if (host->ops->enable_preset_value) { |
| 1329 | mmc_host_clk_hold(host); |
Arindam Nath | 4d55c5a | 2011-05-05 12:19:05 +0530 | [diff] [blame] | 1330 | host->ops->enable_preset_value(host, false); |
Sujit Reddy Thumma | 2c4967f74 | 2012-02-04 16:14:50 -0500 | [diff] [blame] | 1331 | mmc_host_clk_release(host); |
| 1332 | } |
Arindam Nath | 4d55c5a | 2011-05-05 12:19:05 +0530 | [diff] [blame] | 1333 | |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 1334 | err = mmc_send_app_op_cond(host, 0, &ocr); |
| 1335 | if (err) |
| 1336 | return err; |
| 1337 | |
Adrian Hunter | 9feae24 | 2009-09-22 16:44:32 -0700 | [diff] [blame] | 1338 | mmc_sd_attach_bus_ops(host); |
Takashi Iwai | 8f230f4 | 2010-12-08 10:04:30 +0100 | [diff] [blame] | 1339 | if (host->ocr_avail_sd) |
| 1340 | host->ocr_avail = host->ocr_avail_sd; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1341 | |
Philip Langdale | 55556da | 2007-03-16 19:39:00 -0700 | [diff] [blame] | 1342 | /* |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 1343 | * We need to get OCR a different way for SPI. |
| 1344 | */ |
| 1345 | if (mmc_host_is_spi(host)) { |
| 1346 | mmc_go_idle(host); |
| 1347 | |
| 1348 | err = mmc_spi_read_ocr(host, 0, &ocr); |
| 1349 | if (err) |
| 1350 | goto err; |
| 1351 | } |
| 1352 | |
| 1353 | /* |
Philip Langdale | 55556da | 2007-03-16 19:39:00 -0700 | [diff] [blame] | 1354 | * Sanity check the voltages that the card claims to |
| 1355 | * support. |
| 1356 | */ |
| 1357 | if (ocr & 0x7F) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 1358 | pr_warning("%s: card claims to support voltages " |
Philip Langdale | 55556da | 2007-03-16 19:39:00 -0700 | [diff] [blame] | 1359 | "below the defined range. These will be ignored.\n", |
| 1360 | mmc_hostname(host)); |
| 1361 | ocr &= ~0x7F; |
| 1362 | } |
| 1363 | |
Takashi Iwai | 8f230f4 | 2010-12-08 10:04:30 +0100 | [diff] [blame] | 1364 | if ((ocr & MMC_VDD_165_195) && |
| 1365 | !(host->ocr_avail_sd & MMC_VDD_165_195)) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 1366 | pr_warning("%s: SD card claims to support the " |
Philip Langdale | 55556da | 2007-03-16 19:39:00 -0700 | [diff] [blame] | 1367 | "incompletely defined 'low voltage range'. This " |
| 1368 | "will be ignored.\n", mmc_hostname(host)); |
| 1369 | ocr &= ~MMC_VDD_165_195; |
| 1370 | } |
| 1371 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1372 | host->ocr = mmc_select_voltage(host, ocr); |
| 1373 | |
| 1374 | /* |
| 1375 | * Can we support the voltage(s) of the card(s)? |
| 1376 | */ |
Pierre Ossman | 109b5be | 2007-07-23 00:12:10 +0200 | [diff] [blame] | 1377 | if (!host->ocr) { |
| 1378 | err = -EINVAL; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1379 | goto err; |
Pierre Ossman | 109b5be | 2007-07-23 00:12:10 +0200 | [diff] [blame] | 1380 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1381 | |
| 1382 | /* |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1383 | * Detect and init the card. |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1384 | */ |
San Mehat | 8ed5a66 | 2008-12-04 11:18:00 -0800 | [diff] [blame] | 1385 | #ifdef CONFIG_MMC_PARANOID_SD_INIT |
| 1386 | retries = 5; |
| 1387 | while (retries) { |
| 1388 | err = mmc_sd_init_card(host, host->ocr, NULL); |
| 1389 | if (err) { |
| 1390 | retries--; |
Subhash Jadavani | 4dd5c44 | 2012-03-13 00:33:00 +0530 | [diff] [blame] | 1391 | mmc_power_off(host); |
| 1392 | usleep_range(5000, 5500); |
| 1393 | mmc_power_up(host); |
| 1394 | mmc_select_voltage(host, host->ocr); |
San Mehat | 8ed5a66 | 2008-12-04 11:18:00 -0800 | [diff] [blame] | 1395 | continue; |
| 1396 | } |
| 1397 | break; |
| 1398 | } |
| 1399 | |
| 1400 | if (!retries) { |
| 1401 | printk(KERN_ERR "%s: mmc_sd_init_card() failure (err = %d)\n", |
| 1402 | mmc_hostname(host), err); |
| 1403 | goto err; |
| 1404 | } |
| 1405 | #else |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1406 | err = mmc_sd_init_card(host, host->ocr, NULL); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 1407 | if (err) |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1408 | goto err; |
San Mehat | 8ed5a66 | 2008-12-04 11:18:00 -0800 | [diff] [blame] | 1409 | #endif |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1410 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1411 | mmc_release_host(host); |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 1412 | err = mmc_add_card(host->card); |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 1413 | mmc_claim_host(host); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1414 | if (err) |
Pierre Ossman | 2986d0b | 2007-07-22 17:52:06 +0200 | [diff] [blame] | 1415 | goto remove_card; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1416 | |
Sujit Reddy Thumma | 51d57b3 | 2012-10-11 17:17:03 +0530 | [diff] [blame] | 1417 | /* Initialize clock scaling only for high frequency modes */ |
| 1418 | if (mmc_card_uhs(host->card)) |
| 1419 | mmc_init_clk_scaling(host); |
| 1420 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1421 | return 0; |
| 1422 | |
Pierre Ossman | 2986d0b | 2007-07-22 17:52:06 +0200 | [diff] [blame] | 1423 | remove_card: |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 1424 | mmc_release_host(host); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1425 | mmc_remove_card(host->card); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1426 | host->card = NULL; |
Pierre Ossman | 2986d0b | 2007-07-22 17:52:06 +0200 | [diff] [blame] | 1427 | mmc_claim_host(host); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1428 | err: |
| 1429 | mmc_detach_bus(host); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1430 | |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 1431 | pr_err("%s: error %d whilst initialising SD card\n", |
Pierre Ossman | 109b5be | 2007-07-23 00:12:10 +0200 | [diff] [blame] | 1432 | mmc_hostname(host), err); |
| 1433 | |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 1434 | return err; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1435 | } |
| 1436 | |