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