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> |
| 14 | |
| 15 | #include <linux/mmc/host.h> |
| 16 | #include <linux/mmc/card.h> |
| 17 | #include <linux/mmc/mmc.h> |
Pierre Ossman | 3373c0a | 2007-05-31 22:25:11 +0200 | [diff] [blame] | 18 | #include <linux/mmc/sd.h> |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 19 | |
| 20 | #include "core.h" |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 21 | #include "bus.h" |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 22 | #include "mmc_ops.h" |
| 23 | #include "sd_ops.h" |
| 24 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 25 | static const unsigned int tran_exp[] = { |
| 26 | 10000, 100000, 1000000, 10000000, |
| 27 | 0, 0, 0, 0 |
| 28 | }; |
| 29 | |
| 30 | static const unsigned char tran_mant[] = { |
| 31 | 0, 10, 12, 13, 15, 20, 25, 30, |
| 32 | 35, 40, 45, 50, 55, 60, 70, 80, |
| 33 | }; |
| 34 | |
| 35 | static const unsigned int tacc_exp[] = { |
| 36 | 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, |
| 37 | }; |
| 38 | |
| 39 | static const unsigned int tacc_mant[] = { |
| 40 | 0, 10, 12, 13, 15, 20, 25, 30, |
| 41 | 35, 40, 45, 50, 55, 60, 70, 80, |
| 42 | }; |
| 43 | |
| 44 | #define UNSTUFF_BITS(resp,start,size) \ |
| 45 | ({ \ |
| 46 | const int __size = size; \ |
| 47 | const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \ |
| 48 | const int __off = 3 - ((start) / 32); \ |
| 49 | const int __shft = (start) & 31; \ |
| 50 | u32 __res; \ |
| 51 | \ |
| 52 | __res = resp[__off] >> __shft; \ |
| 53 | if (__size + __shft > 32) \ |
| 54 | __res |= resp[__off-1] << ((32 - __shft) % 32); \ |
| 55 | __res & __mask; \ |
| 56 | }) |
| 57 | |
| 58 | /* |
| 59 | * Given the decoded CSD structure, decode the raw CID to our CID structure. |
| 60 | */ |
| 61 | static void mmc_decode_cid(struct mmc_card *card) |
| 62 | { |
| 63 | u32 *resp = card->raw_cid; |
| 64 | |
| 65 | memset(&card->cid, 0, sizeof(struct mmc_cid)); |
| 66 | |
| 67 | /* |
| 68 | * SD doesn't currently have a version field so we will |
| 69 | * have to assume we can parse this. |
| 70 | */ |
| 71 | card->cid.manfid = UNSTUFF_BITS(resp, 120, 8); |
| 72 | card->cid.oemid = UNSTUFF_BITS(resp, 104, 16); |
| 73 | card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); |
| 74 | card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); |
| 75 | card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); |
| 76 | card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); |
| 77 | card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); |
| 78 | card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4); |
| 79 | card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4); |
| 80 | card->cid.serial = UNSTUFF_BITS(resp, 24, 32); |
| 81 | card->cid.year = UNSTUFF_BITS(resp, 12, 8); |
| 82 | card->cid.month = UNSTUFF_BITS(resp, 8, 4); |
| 83 | |
| 84 | card->cid.year += 2000; /* SD cards year offset */ |
| 85 | } |
| 86 | |
| 87 | /* |
| 88 | * Given a 128-bit response, decode to our card CSD structure. |
| 89 | */ |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 90 | static int mmc_decode_csd(struct mmc_card *card) |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 91 | { |
| 92 | struct mmc_csd *csd = &card->csd; |
| 93 | unsigned int e, m, csd_struct; |
| 94 | u32 *resp = card->raw_csd; |
| 95 | |
| 96 | csd_struct = UNSTUFF_BITS(resp, 126, 2); |
| 97 | |
| 98 | switch (csd_struct) { |
| 99 | case 0: |
| 100 | m = UNSTUFF_BITS(resp, 115, 4); |
| 101 | e = UNSTUFF_BITS(resp, 112, 3); |
| 102 | csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10; |
| 103 | csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100; |
| 104 | |
| 105 | m = UNSTUFF_BITS(resp, 99, 4); |
| 106 | e = UNSTUFF_BITS(resp, 96, 3); |
| 107 | csd->max_dtr = tran_exp[e] * tran_mant[m]; |
| 108 | csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); |
| 109 | |
| 110 | e = UNSTUFF_BITS(resp, 47, 3); |
| 111 | m = UNSTUFF_BITS(resp, 62, 12); |
| 112 | csd->capacity = (1 + m) << (e + 2); |
| 113 | |
| 114 | csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4); |
| 115 | csd->read_partial = UNSTUFF_BITS(resp, 79, 1); |
| 116 | csd->write_misalign = UNSTUFF_BITS(resp, 78, 1); |
| 117 | csd->read_misalign = UNSTUFF_BITS(resp, 77, 1); |
| 118 | csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3); |
| 119 | csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4); |
| 120 | csd->write_partial = UNSTUFF_BITS(resp, 21, 1); |
| 121 | break; |
| 122 | case 1: |
| 123 | /* |
| 124 | * This is a block-addressed SDHC card. Most |
| 125 | * interesting fields are unused and have fixed |
| 126 | * values. To avoid getting tripped by buggy cards, |
| 127 | * we assume those fixed values ourselves. |
| 128 | */ |
| 129 | mmc_card_set_blockaddr(card); |
| 130 | |
| 131 | csd->tacc_ns = 0; /* Unused */ |
| 132 | csd->tacc_clks = 0; /* Unused */ |
| 133 | |
| 134 | m = UNSTUFF_BITS(resp, 99, 4); |
| 135 | e = UNSTUFF_BITS(resp, 96, 3); |
| 136 | csd->max_dtr = tran_exp[e] * tran_mant[m]; |
| 137 | csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); |
| 138 | |
| 139 | m = UNSTUFF_BITS(resp, 48, 22); |
| 140 | csd->capacity = (1 + m) << 10; |
| 141 | |
| 142 | csd->read_blkbits = 9; |
| 143 | csd->read_partial = 0; |
| 144 | csd->write_misalign = 0; |
| 145 | csd->read_misalign = 0; |
| 146 | csd->r2w_factor = 4; /* Unused */ |
| 147 | csd->write_blkbits = 9; |
| 148 | csd->write_partial = 0; |
| 149 | break; |
| 150 | default: |
Pierre Ossman | facba91 | 2007-07-24 21:53:43 +0200 | [diff] [blame] | 151 | printk(KERN_ERR "%s: unrecognised CSD structure version %d\n", |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 152 | mmc_hostname(card->host), csd_struct); |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 153 | return -EINVAL; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 154 | } |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 155 | |
| 156 | return 0; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /* |
| 160 | * Given a 64-bit response, decode to our card SCR structure. |
| 161 | */ |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 162 | static int mmc_decode_scr(struct mmc_card *card) |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 163 | { |
| 164 | struct sd_scr *scr = &card->scr; |
| 165 | unsigned int scr_struct; |
| 166 | u32 resp[4]; |
| 167 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 168 | resp[3] = card->raw_scr[1]; |
| 169 | resp[2] = card->raw_scr[0]; |
| 170 | |
| 171 | scr_struct = UNSTUFF_BITS(resp, 60, 4); |
| 172 | if (scr_struct != 0) { |
Pierre Ossman | facba91 | 2007-07-24 21:53:43 +0200 | [diff] [blame] | 173 | printk(KERN_ERR "%s: unrecognised SCR structure version %d\n", |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 174 | mmc_hostname(card->host), scr_struct); |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 175 | return -EINVAL; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4); |
| 179 | scr->bus_widths = UNSTUFF_BITS(resp, 48, 4); |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 180 | |
| 181 | return 0; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /* |
Pierre Ossman | 1addfcd | 2007-05-01 14:46:08 +0200 | [diff] [blame] | 185 | * Fetches and decodes switch information |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 186 | */ |
Pierre Ossman | 1addfcd | 2007-05-01 14:46:08 +0200 | [diff] [blame] | 187 | static int mmc_read_switch(struct mmc_card *card) |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 188 | { |
| 189 | int err; |
| 190 | u8 *status; |
| 191 | |
Pierre Ossman | 3373c0a | 2007-05-31 22:25:11 +0200 | [diff] [blame] | 192 | if (card->scr.sda_vsn < SCR_SPEC_VER_1) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 193 | return 0; |
Pierre Ossman | 3373c0a | 2007-05-31 22:25:11 +0200 | [diff] [blame] | 194 | |
| 195 | if (!(card->csd.cmdclass & CCC_SWITCH)) { |
| 196 | printk(KERN_WARNING "%s: card lacks mandatory switch " |
| 197 | "function, performance might suffer.\n", |
| 198 | mmc_hostname(card->host)); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 199 | return 0; |
Pierre Ossman | 3373c0a | 2007-05-31 22:25:11 +0200 | [diff] [blame] | 200 | } |
| 201 | |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 202 | err = -EIO; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 203 | |
| 204 | status = kmalloc(64, GFP_KERNEL); |
| 205 | if (!status) { |
Pierre Ossman | facba91 | 2007-07-24 21:53:43 +0200 | [diff] [blame] | 206 | printk(KERN_ERR "%s: could not allocate a buffer for " |
| 207 | "switch capabilities.\n", mmc_hostname(card->host)); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 208 | return -ENOMEM; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | err = mmc_sd_switch(card, 0, 0, 1, status); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 212 | if (err) { |
Wolfgang Muees | d08ebed | 2009-09-22 16:45:26 -0700 | [diff] [blame] | 213 | /* If the host or the card can't do the switch, |
| 214 | * fail more gracefully. */ |
| 215 | if ((err != -EINVAL) |
| 216 | && (err != -ENOSYS) |
| 217 | && (err != -EFAULT)) |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 218 | goto out; |
| 219 | |
Pierre Ossman | 3373c0a | 2007-05-31 22:25:11 +0200 | [diff] [blame] | 220 | printk(KERN_WARNING "%s: problem reading switch " |
| 221 | "capabilities, performance might suffer.\n", |
| 222 | mmc_hostname(card->host)); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 223 | err = 0; |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 224 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 225 | goto out; |
| 226 | } |
| 227 | |
| 228 | if (status[13] & 0x02) |
| 229 | card->sw_caps.hs_max_dtr = 50000000; |
| 230 | |
Pierre Ossman | 1addfcd | 2007-05-01 14:46:08 +0200 | [diff] [blame] | 231 | out: |
| 232 | kfree(status); |
| 233 | |
| 234 | return err; |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | * Test if the card supports high-speed mode and, if so, switch to it. |
| 239 | */ |
| 240 | static int mmc_switch_hs(struct mmc_card *card) |
| 241 | { |
| 242 | int err; |
| 243 | u8 *status; |
| 244 | |
Pierre Ossman | 3373c0a | 2007-05-31 22:25:11 +0200 | [diff] [blame] | 245 | if (card->scr.sda_vsn < SCR_SPEC_VER_1) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 246 | return 0; |
Pierre Ossman | 3373c0a | 2007-05-31 22:25:11 +0200 | [diff] [blame] | 247 | |
| 248 | if (!(card->csd.cmdclass & CCC_SWITCH)) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 249 | return 0; |
Pierre Ossman | 3373c0a | 2007-05-31 22:25:11 +0200 | [diff] [blame] | 250 | |
Pierre Ossman | 1addfcd | 2007-05-01 14:46:08 +0200 | [diff] [blame] | 251 | if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED)) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 252 | return 0; |
Pierre Ossman | 1addfcd | 2007-05-01 14:46:08 +0200 | [diff] [blame] | 253 | |
| 254 | if (card->sw_caps.hs_max_dtr == 0) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 255 | return 0; |
Pierre Ossman | 1addfcd | 2007-05-01 14:46:08 +0200 | [diff] [blame] | 256 | |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 257 | err = -EIO; |
Pierre Ossman | 1addfcd | 2007-05-01 14:46:08 +0200 | [diff] [blame] | 258 | |
| 259 | status = kmalloc(64, GFP_KERNEL); |
| 260 | if (!status) { |
Pierre Ossman | facba91 | 2007-07-24 21:53:43 +0200 | [diff] [blame] | 261 | printk(KERN_ERR "%s: could not allocate a buffer for " |
| 262 | "switch capabilities.\n", mmc_hostname(card->host)); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 263 | return -ENOMEM; |
Pierre Ossman | 1addfcd | 2007-05-01 14:46:08 +0200 | [diff] [blame] | 264 | } |
| 265 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 266 | err = mmc_sd_switch(card, 1, 0, 1, status); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 267 | if (err) |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 268 | goto out; |
| 269 | |
| 270 | if ((status[16] & 0xF) != 1) { |
| 271 | printk(KERN_WARNING "%s: Problem switching card " |
| 272 | "into high-speed mode!\n", |
| 273 | mmc_hostname(card->host)); |
| 274 | } else { |
| 275 | mmc_card_set_highspeed(card); |
| 276 | mmc_set_timing(card->host, MMC_TIMING_SD_HS); |
| 277 | } |
| 278 | |
| 279 | out: |
| 280 | kfree(status); |
| 281 | |
| 282 | return err; |
| 283 | } |
| 284 | |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 285 | MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1], |
| 286 | card->raw_cid[2], card->raw_cid[3]); |
| 287 | MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1], |
| 288 | card->raw_csd[2], card->raw_csd[3]); |
| 289 | MMC_DEV_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]); |
| 290 | MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year); |
| 291 | MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev); |
| 292 | MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev); |
| 293 | MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid); |
| 294 | MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name); |
| 295 | MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid); |
| 296 | MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial); |
| 297 | |
| 298 | |
| 299 | static struct attribute *sd_std_attrs[] = { |
| 300 | &dev_attr_cid.attr, |
| 301 | &dev_attr_csd.attr, |
| 302 | &dev_attr_scr.attr, |
| 303 | &dev_attr_date.attr, |
| 304 | &dev_attr_fwrev.attr, |
| 305 | &dev_attr_hwrev.attr, |
| 306 | &dev_attr_manfid.attr, |
| 307 | &dev_attr_name.attr, |
| 308 | &dev_attr_oemid.attr, |
| 309 | &dev_attr_serial.attr, |
| 310 | NULL, |
| 311 | }; |
| 312 | |
| 313 | static struct attribute_group sd_std_attr_group = { |
| 314 | .attrs = sd_std_attrs, |
| 315 | }; |
| 316 | |
David Brownell | a4dbd67 | 2009-06-24 10:06:31 -0700 | [diff] [blame] | 317 | static const struct attribute_group *sd_attr_groups[] = { |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 318 | &sd_std_attr_group, |
| 319 | NULL, |
| 320 | }; |
| 321 | |
| 322 | static struct device_type sd_type = { |
| 323 | .groups = sd_attr_groups, |
| 324 | }; |
| 325 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 326 | /* |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 327 | * Handle the detection and initialisation of a card. |
| 328 | * |
Deepak Saxena | 8769392 | 2008-06-16 19:20:57 -0700 | [diff] [blame] | 329 | * In the case of a resume, "oldcard" will contain the card |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 330 | * we're trying to reinitialise. |
| 331 | */ |
| 332 | static int mmc_sd_init_card(struct mmc_host *host, u32 ocr, |
| 333 | struct mmc_card *oldcard) |
| 334 | { |
| 335 | struct mmc_card *card; |
| 336 | int err; |
| 337 | u32 cid[4]; |
| 338 | unsigned int max_dtr; |
| 339 | |
| 340 | BUG_ON(!host); |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 341 | WARN_ON(!host->claimed); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 342 | |
| 343 | /* |
| 344 | * Since we're changing the OCR value, we seem to |
| 345 | * need to tell some cards to go back to the idle |
| 346 | * state. We wait 1ms to give cards time to |
| 347 | * respond. |
| 348 | */ |
| 349 | mmc_go_idle(host); |
| 350 | |
| 351 | /* |
| 352 | * If SD_SEND_IF_COND indicates an SD 2.0 |
| 353 | * compliant card and we should set bit 30 |
| 354 | * of the ocr to indicate that we can handle |
| 355 | * block-addressed SDHC cards. |
| 356 | */ |
| 357 | err = mmc_send_if_cond(host, ocr); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 358 | if (!err) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 359 | ocr |= 1 << 30; |
| 360 | |
| 361 | err = mmc_send_app_op_cond(host, ocr, NULL); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 362 | if (err) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 363 | goto err; |
| 364 | |
| 365 | /* |
| 366 | * Fetch CID from card. |
| 367 | */ |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 368 | if (mmc_host_is_spi(host)) |
| 369 | err = mmc_send_cid(host, cid); |
| 370 | else |
| 371 | err = mmc_all_send_cid(host, cid); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 372 | if (err) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 373 | goto err; |
| 374 | |
| 375 | if (oldcard) { |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 376 | if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) { |
| 377 | err = -ENOENT; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 378 | goto err; |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 379 | } |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 380 | |
| 381 | card = oldcard; |
| 382 | } else { |
| 383 | /* |
| 384 | * Allocate card structure. |
| 385 | */ |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 386 | card = mmc_alloc_card(host, &sd_type); |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 387 | if (IS_ERR(card)) { |
| 388 | err = PTR_ERR(card); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 389 | goto err; |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 390 | } |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 391 | |
| 392 | card->type = MMC_TYPE_SD; |
| 393 | memcpy(card->raw_cid, cid, sizeof(card->raw_cid)); |
| 394 | } |
| 395 | |
| 396 | /* |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 397 | * For native busses: get card RCA and quit open drain mode. |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 398 | */ |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 399 | if (!mmc_host_is_spi(host)) { |
| 400 | err = mmc_send_relative_addr(host, &card->rca); |
| 401 | if (err) |
| 402 | goto free_card; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 403 | |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 404 | mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL); |
| 405 | } |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 406 | |
| 407 | if (!oldcard) { |
| 408 | /* |
| 409 | * Fetch CSD from card. |
| 410 | */ |
| 411 | err = mmc_send_csd(card, card->raw_csd); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 412 | if (err) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 413 | goto free_card; |
| 414 | |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 415 | err = mmc_decode_csd(card); |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 416 | if (err) |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 417 | goto free_card; |
| 418 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 419 | mmc_decode_cid(card); |
| 420 | } |
| 421 | |
| 422 | /* |
| 423 | * Select card, as all following commands rely on that. |
| 424 | */ |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 425 | if (!mmc_host_is_spi(host)) { |
| 426 | err = mmc_select_card(card); |
| 427 | if (err) |
| 428 | goto free_card; |
| 429 | } |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 430 | |
| 431 | if (!oldcard) { |
| 432 | /* |
| 433 | * Fetch SCR from card. |
| 434 | */ |
| 435 | err = mmc_app_send_scr(card, card->raw_scr); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 436 | if (err) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 437 | goto free_card; |
| 438 | |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 439 | err = mmc_decode_scr(card); |
| 440 | if (err < 0) |
| 441 | goto free_card; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 442 | |
| 443 | /* |
| 444 | * Fetch switch information from card. |
| 445 | */ |
| 446 | err = mmc_read_switch(card); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 447 | if (err) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 448 | goto free_card; |
| 449 | } |
| 450 | |
| 451 | /* |
Wolfgang Muees | 9d9f25c | 2009-04-07 14:48:16 +0100 | [diff] [blame] | 452 | * For SPI, enable CRC as appropriate. |
| 453 | * This CRC enable is located AFTER the reading of the |
| 454 | * card registers because some SDHC cards are not able |
| 455 | * to provide valid CRCs for non-512-byte blocks. |
| 456 | */ |
| 457 | if (mmc_host_is_spi(host)) { |
| 458 | err = mmc_spi_set_crc(host, use_spi_crc); |
| 459 | if (err) |
| 460 | goto free_card; |
| 461 | } |
| 462 | |
| 463 | /* |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 464 | * Attempt to change to high-speed (if supported) |
| 465 | */ |
| 466 | err = mmc_switch_hs(card); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 467 | if (err) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 468 | goto free_card; |
| 469 | |
| 470 | /* |
| 471 | * Compute bus speed. |
| 472 | */ |
| 473 | max_dtr = (unsigned int)-1; |
| 474 | |
| 475 | if (mmc_card_highspeed(card)) { |
| 476 | if (max_dtr > card->sw_caps.hs_max_dtr) |
| 477 | max_dtr = card->sw_caps.hs_max_dtr; |
| 478 | } else if (max_dtr > card->csd.max_dtr) { |
| 479 | max_dtr = card->csd.max_dtr; |
| 480 | } |
| 481 | |
| 482 | mmc_set_clock(host, max_dtr); |
| 483 | |
| 484 | /* |
| 485 | * Switch to wider bus (if supported). |
| 486 | */ |
Pierre Ossman | 7165129 | 2007-06-06 20:23:25 +0200 | [diff] [blame] | 487 | if ((host->caps & MMC_CAP_4_BIT_DATA) && |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 488 | (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) { |
| 489 | err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 490 | if (err) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 491 | goto free_card; |
| 492 | |
| 493 | mmc_set_bus_width(host, MMC_BUS_WIDTH_4); |
| 494 | } |
| 495 | |
Pierre Ossman | c3bff2e | 2007-06-13 19:06:03 +0200 | [diff] [blame] | 496 | /* |
| 497 | * Check if read-only switch is active. |
| 498 | */ |
| 499 | if (!oldcard) { |
Anton Vorontsov | 08f80bb | 2008-06-17 18:17:39 +0400 | [diff] [blame] | 500 | if (!host->ops->get_ro || host->ops->get_ro(host) < 0) { |
Pierre Ossman | c3bff2e | 2007-06-13 19:06:03 +0200 | [diff] [blame] | 501 | printk(KERN_WARNING "%s: host does not " |
| 502 | "support reading read-only " |
| 503 | "switch. assuming write-enable.\n", |
| 504 | mmc_hostname(host)); |
| 505 | } else { |
Anton Vorontsov | 08f80bb | 2008-06-17 18:17:39 +0400 | [diff] [blame] | 506 | if (host->ops->get_ro(host) > 0) |
Pierre Ossman | c3bff2e | 2007-06-13 19:06:03 +0200 | [diff] [blame] | 507 | mmc_card_set_readonly(card); |
| 508 | } |
| 509 | } |
| 510 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 511 | if (!oldcard) |
| 512 | host->card = card; |
| 513 | |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 514 | return 0; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 515 | |
| 516 | free_card: |
| 517 | if (!oldcard) |
| 518 | mmc_remove_card(card); |
| 519 | err: |
| 520 | |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 521 | return err; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 525 | * Host is being removed. Free up the current card. |
| 526 | */ |
| 527 | static void mmc_sd_remove(struct mmc_host *host) |
| 528 | { |
| 529 | BUG_ON(!host); |
| 530 | BUG_ON(!host->card); |
| 531 | |
| 532 | mmc_remove_card(host->card); |
| 533 | host->card = NULL; |
| 534 | } |
| 535 | |
| 536 | /* |
| 537 | * Card detection callback from host. |
| 538 | */ |
| 539 | static void mmc_sd_detect(struct mmc_host *host) |
| 540 | { |
| 541 | int err; |
| 542 | |
| 543 | BUG_ON(!host); |
| 544 | BUG_ON(!host->card); |
| 545 | |
| 546 | mmc_claim_host(host); |
| 547 | |
| 548 | /* |
| 549 | * Just check if our card has been removed. |
| 550 | */ |
| 551 | err = mmc_send_status(host->card, NULL); |
| 552 | |
| 553 | mmc_release_host(host); |
| 554 | |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 555 | if (err) { |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 556 | mmc_sd_remove(host); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 557 | |
| 558 | mmc_claim_host(host); |
| 559 | mmc_detach_bus(host); |
| 560 | mmc_release_host(host); |
| 561 | } |
| 562 | } |
| 563 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 564 | /* |
| 565 | * Suspend callback from host. |
| 566 | */ |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 567 | static int mmc_sd_suspend(struct mmc_host *host) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 568 | { |
| 569 | BUG_ON(!host); |
| 570 | BUG_ON(!host->card); |
| 571 | |
| 572 | mmc_claim_host(host); |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 573 | if (!mmc_host_is_spi(host)) |
| 574 | mmc_deselect_cards(host); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 575 | host->card->state &= ~MMC_STATE_HIGHSPEED; |
| 576 | mmc_release_host(host); |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 577 | |
| 578 | return 0; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | /* |
| 582 | * Resume callback from host. |
| 583 | * |
| 584 | * This function tries to determine if the same card is still present |
| 585 | * and, if so, restore all state to it. |
| 586 | */ |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 587 | static int mmc_sd_resume(struct mmc_host *host) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 588 | { |
| 589 | int err; |
| 590 | |
| 591 | BUG_ON(!host); |
| 592 | BUG_ON(!host->card); |
| 593 | |
| 594 | mmc_claim_host(host); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 595 | err = mmc_sd_init_card(host, host->ocr, host->card); |
Pierre Ossman | 2986d0b | 2007-07-22 17:52:06 +0200 | [diff] [blame] | 596 | mmc_release_host(host); |
| 597 | |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 598 | return err; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 599 | } |
| 600 | |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 601 | static void mmc_sd_power_restore(struct mmc_host *host) |
| 602 | { |
| 603 | host->card->state &= ~MMC_STATE_HIGHSPEED; |
| 604 | mmc_claim_host(host); |
| 605 | mmc_sd_init_card(host, host->ocr, host->card); |
| 606 | mmc_release_host(host); |
| 607 | } |
| 608 | |
Adrian Hunter | 9feae24 | 2009-09-22 16:44:32 -0700 | [diff] [blame] | 609 | static const struct mmc_bus_ops mmc_sd_ops = { |
| 610 | .remove = mmc_sd_remove, |
| 611 | .detect = mmc_sd_detect, |
| 612 | .suspend = NULL, |
| 613 | .resume = NULL, |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 614 | .power_restore = mmc_sd_power_restore, |
Adrian Hunter | 9feae24 | 2009-09-22 16:44:32 -0700 | [diff] [blame] | 615 | }; |
| 616 | |
| 617 | static const struct mmc_bus_ops mmc_sd_ops_unsafe = { |
| 618 | .remove = mmc_sd_remove, |
| 619 | .detect = mmc_sd_detect, |
| 620 | .suspend = mmc_sd_suspend, |
| 621 | .resume = mmc_sd_resume, |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 622 | .power_restore = mmc_sd_power_restore, |
Adrian Hunter | 9feae24 | 2009-09-22 16:44:32 -0700 | [diff] [blame] | 623 | }; |
| 624 | |
| 625 | static void mmc_sd_attach_bus_ops(struct mmc_host *host) |
| 626 | { |
| 627 | const struct mmc_bus_ops *bus_ops; |
| 628 | |
Ben Hutchings | bd68e08 | 2009-12-14 18:01:29 -0800 | [diff] [blame] | 629 | if (host->caps & MMC_CAP_NONREMOVABLE || !mmc_assume_removable) |
Adrian Hunter | 9feae24 | 2009-09-22 16:44:32 -0700 | [diff] [blame] | 630 | bus_ops = &mmc_sd_ops_unsafe; |
| 631 | else |
| 632 | bus_ops = &mmc_sd_ops; |
| 633 | mmc_attach_bus(host, bus_ops); |
| 634 | } |
| 635 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 636 | /* |
| 637 | * Starting point for SD card init. |
| 638 | */ |
| 639 | int mmc_attach_sd(struct mmc_host *host, u32 ocr) |
| 640 | { |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 641 | int err; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 642 | |
| 643 | BUG_ON(!host); |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 644 | WARN_ON(!host->claimed); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 645 | |
Adrian Hunter | 9feae24 | 2009-09-22 16:44:32 -0700 | [diff] [blame] | 646 | mmc_sd_attach_bus_ops(host); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 647 | |
Philip Langdale | 55556da | 2007-03-16 19:39:00 -0700 | [diff] [blame] | 648 | /* |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 649 | * We need to get OCR a different way for SPI. |
| 650 | */ |
| 651 | if (mmc_host_is_spi(host)) { |
| 652 | mmc_go_idle(host); |
| 653 | |
| 654 | err = mmc_spi_read_ocr(host, 0, &ocr); |
| 655 | if (err) |
| 656 | goto err; |
| 657 | } |
| 658 | |
| 659 | /* |
Philip Langdale | 55556da | 2007-03-16 19:39:00 -0700 | [diff] [blame] | 660 | * Sanity check the voltages that the card claims to |
| 661 | * support. |
| 662 | */ |
| 663 | if (ocr & 0x7F) { |
| 664 | printk(KERN_WARNING "%s: card claims to support voltages " |
| 665 | "below the defined range. These will be ignored.\n", |
| 666 | mmc_hostname(host)); |
| 667 | ocr &= ~0x7F; |
| 668 | } |
| 669 | |
| 670 | if (ocr & MMC_VDD_165_195) { |
| 671 | printk(KERN_WARNING "%s: SD card claims to support the " |
| 672 | "incompletely defined 'low voltage range'. This " |
| 673 | "will be ignored.\n", mmc_hostname(host)); |
| 674 | ocr &= ~MMC_VDD_165_195; |
| 675 | } |
| 676 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 677 | host->ocr = mmc_select_voltage(host, ocr); |
| 678 | |
| 679 | /* |
| 680 | * Can we support the voltage(s) of the card(s)? |
| 681 | */ |
Pierre Ossman | 109b5be | 2007-07-23 00:12:10 +0200 | [diff] [blame] | 682 | if (!host->ocr) { |
| 683 | err = -EINVAL; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 684 | goto err; |
Pierre Ossman | 109b5be | 2007-07-23 00:12:10 +0200 | [diff] [blame] | 685 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 686 | |
| 687 | /* |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 688 | * Detect and init the card. |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 689 | */ |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 690 | err = mmc_sd_init_card(host, host->ocr, NULL); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 691 | if (err) |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 692 | goto err; |
| 693 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 694 | mmc_release_host(host); |
| 695 | |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 696 | err = mmc_add_card(host->card); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 697 | if (err) |
Pierre Ossman | 2986d0b | 2007-07-22 17:52:06 +0200 | [diff] [blame] | 698 | goto remove_card; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 699 | |
| 700 | return 0; |
| 701 | |
Pierre Ossman | 2986d0b | 2007-07-22 17:52:06 +0200 | [diff] [blame] | 702 | remove_card: |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 703 | mmc_remove_card(host->card); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 704 | host->card = NULL; |
Pierre Ossman | 2986d0b | 2007-07-22 17:52:06 +0200 | [diff] [blame] | 705 | mmc_claim_host(host); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 706 | err: |
| 707 | mmc_detach_bus(host); |
| 708 | mmc_release_host(host); |
| 709 | |
Pierre Ossman | 109b5be | 2007-07-23 00:12:10 +0200 | [diff] [blame] | 710 | printk(KERN_ERR "%s: error %d whilst initialising SD card\n", |
| 711 | mmc_hostname(host), err); |
| 712 | |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 713 | return err; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 714 | } |
| 715 | |