Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1 | /* |
Pierre Ossman | 70f1048 | 2007-07-11 20:04:50 +0200 | [diff] [blame] | 2 | * linux/drivers/mmc/core/mmc.c |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2003-2004 Russell King, All Rights Reserved. |
| 5 | * Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved. |
| 6 | * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/err.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/slab.h> |
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> |
| 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 | |
| 24 | static const unsigned int tran_exp[] = { |
| 25 | 10000, 100000, 1000000, 10000000, |
| 26 | 0, 0, 0, 0 |
| 27 | }; |
| 28 | |
| 29 | static const unsigned char tran_mant[] = { |
| 30 | 0, 10, 12, 13, 15, 20, 25, 30, |
| 31 | 35, 40, 45, 50, 55, 60, 70, 80, |
| 32 | }; |
| 33 | |
| 34 | static const unsigned int tacc_exp[] = { |
| 35 | 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, |
| 36 | }; |
| 37 | |
| 38 | static const unsigned int tacc_mant[] = { |
| 39 | 0, 10, 12, 13, 15, 20, 25, 30, |
| 40 | 35, 40, 45, 50, 55, 60, 70, 80, |
| 41 | }; |
| 42 | |
| 43 | #define UNSTUFF_BITS(resp,start,size) \ |
| 44 | ({ \ |
| 45 | const int __size = size; \ |
| 46 | const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \ |
| 47 | const int __off = 3 - ((start) / 32); \ |
| 48 | const int __shft = (start) & 31; \ |
| 49 | u32 __res; \ |
| 50 | \ |
| 51 | __res = resp[__off] >> __shft; \ |
| 52 | if (__size + __shft > 32) \ |
| 53 | __res |= resp[__off-1] << ((32 - __shft) % 32); \ |
| 54 | __res & __mask; \ |
| 55 | }) |
| 56 | |
| 57 | /* |
| 58 | * Given the decoded CSD structure, decode the raw CID to our CID structure. |
| 59 | */ |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 60 | static int mmc_decode_cid(struct mmc_card *card) |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 61 | { |
| 62 | u32 *resp = card->raw_cid; |
| 63 | |
| 64 | /* |
| 65 | * The selection of the format here is based upon published |
| 66 | * specs from sandisk and from what people have reported. |
| 67 | */ |
| 68 | switch (card->csd.mmca_vsn) { |
| 69 | case 0: /* MMC v1.0 - v1.2 */ |
| 70 | case 1: /* MMC v1.4 */ |
| 71 | card->cid.manfid = UNSTUFF_BITS(resp, 104, 24); |
| 72 | card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); |
| 73 | card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); |
| 74 | card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); |
| 75 | card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); |
| 76 | card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); |
| 77 | card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8); |
| 78 | card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8); |
| 79 | card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4); |
| 80 | card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4); |
| 81 | card->cid.serial = UNSTUFF_BITS(resp, 16, 24); |
| 82 | card->cid.month = UNSTUFF_BITS(resp, 12, 4); |
| 83 | card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997; |
| 84 | break; |
| 85 | |
| 86 | case 2: /* MMC v2.0 - v2.2 */ |
| 87 | case 3: /* MMC v3.1 - v3.3 */ |
| 88 | case 4: /* MMC v4 */ |
| 89 | card->cid.manfid = UNSTUFF_BITS(resp, 120, 8); |
| 90 | card->cid.oemid = UNSTUFF_BITS(resp, 104, 16); |
| 91 | card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); |
| 92 | card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); |
| 93 | card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); |
| 94 | card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); |
| 95 | card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); |
| 96 | card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8); |
| 97 | card->cid.serial = UNSTUFF_BITS(resp, 16, 32); |
| 98 | card->cid.month = UNSTUFF_BITS(resp, 12, 4); |
| 99 | card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997; |
| 100 | break; |
| 101 | |
| 102 | default: |
Pierre Ossman | facba91 | 2007-07-24 21:53:43 +0200 | [diff] [blame] | 103 | printk(KERN_ERR "%s: card has unknown MMCA version %d\n", |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 104 | mmc_hostname(card->host), card->csd.mmca_vsn); |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 105 | return -EINVAL; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 106 | } |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 107 | |
| 108 | return 0; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /* |
| 112 | * Given a 128-bit response, decode to our card CSD structure. |
| 113 | */ |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 114 | static int mmc_decode_csd(struct mmc_card *card) |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 115 | { |
| 116 | struct mmc_csd *csd = &card->csd; |
Kyungmin Park | 6da24b7 | 2010-08-10 18:01:36 -0700 | [diff] [blame] | 117 | unsigned int e, m; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 118 | u32 *resp = card->raw_csd; |
| 119 | |
| 120 | /* |
| 121 | * We only understand CSD structure v1.1 and v1.2. |
| 122 | * v1.2 has extra information in bits 15, 11 and 10. |
Kyungmin Park | 6da24b7 | 2010-08-10 18:01:36 -0700 | [diff] [blame] | 123 | * We also support eMMC v4.4 & v4.41. |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 124 | */ |
Kyungmin Park | 6da24b7 | 2010-08-10 18:01:36 -0700 | [diff] [blame] | 125 | csd->structure = UNSTUFF_BITS(resp, 126, 2); |
| 126 | if (csd->structure == 0) { |
Pierre Ossman | facba91 | 2007-07-24 21:53:43 +0200 | [diff] [blame] | 127 | printk(KERN_ERR "%s: unrecognised CSD structure version %d\n", |
Kyungmin Park | 6da24b7 | 2010-08-10 18:01:36 -0700 | [diff] [blame] | 128 | mmc_hostname(card->host), csd->structure); |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 129 | return -EINVAL; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4); |
| 133 | m = UNSTUFF_BITS(resp, 115, 4); |
| 134 | e = UNSTUFF_BITS(resp, 112, 3); |
| 135 | csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10; |
| 136 | csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100; |
| 137 | |
| 138 | m = UNSTUFF_BITS(resp, 99, 4); |
| 139 | e = UNSTUFF_BITS(resp, 96, 3); |
| 140 | csd->max_dtr = tran_exp[e] * tran_mant[m]; |
| 141 | csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); |
| 142 | |
| 143 | e = UNSTUFF_BITS(resp, 47, 3); |
| 144 | m = UNSTUFF_BITS(resp, 62, 12); |
| 145 | csd->capacity = (1 + m) << (e + 2); |
| 146 | |
| 147 | csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4); |
| 148 | csd->read_partial = UNSTUFF_BITS(resp, 79, 1); |
| 149 | csd->write_misalign = UNSTUFF_BITS(resp, 78, 1); |
| 150 | csd->read_misalign = UNSTUFF_BITS(resp, 77, 1); |
| 151 | csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3); |
| 152 | csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4); |
| 153 | csd->write_partial = UNSTUFF_BITS(resp, 21, 1); |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 154 | |
| 155 | return 0; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /* |
Pierre Ossman | 89a73cf | 2007-05-01 15:08:30 +0200 | [diff] [blame] | 159 | * Read and decode extended CSD. |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 160 | */ |
Pierre Ossman | 89a73cf | 2007-05-01 15:08:30 +0200 | [diff] [blame] | 161 | static int mmc_read_ext_csd(struct mmc_card *card) |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 162 | { |
| 163 | int err; |
| 164 | u8 *ext_csd; |
| 165 | |
| 166 | BUG_ON(!card); |
| 167 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 168 | if (card->csd.mmca_vsn < CSD_SPEC_VER_4) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 169 | return 0; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 170 | |
| 171 | /* |
| 172 | * As the ext_csd is so large and mostly unused, we don't store the |
| 173 | * raw block in mmc_card. |
| 174 | */ |
| 175 | ext_csd = kmalloc(512, GFP_KERNEL); |
| 176 | if (!ext_csd) { |
| 177 | printk(KERN_ERR "%s: could not allocate a buffer to " |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 178 | "receive the ext_csd.\n", mmc_hostname(card->host)); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 179 | return -ENOMEM; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | err = mmc_send_ext_csd(card, ext_csd); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 183 | if (err) { |
Wolfgang Muees | d08ebed | 2009-09-22 16:45:26 -0700 | [diff] [blame] | 184 | /* If the host or the card can't do the switch, |
| 185 | * fail more gracefully. */ |
| 186 | if ((err != -EINVAL) |
| 187 | && (err != -ENOSYS) |
| 188 | && (err != -EFAULT)) |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 189 | goto out; |
| 190 | |
| 191 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 192 | * High capacity cards should have this "magic" size |
| 193 | * stored in their CSD. |
| 194 | */ |
| 195 | if (card->csd.capacity == (4096 * 512)) { |
| 196 | printk(KERN_ERR "%s: unable to read EXT_CSD " |
| 197 | "on a possible high capacity card. " |
| 198 | "Card will be ignored.\n", |
| 199 | mmc_hostname(card->host)); |
| 200 | } else { |
| 201 | printk(KERN_WARNING "%s: unable to read " |
| 202 | "EXT_CSD, performance might " |
| 203 | "suffer.\n", |
| 204 | mmc_hostname(card->host)); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 205 | err = 0; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 206 | } |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 207 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 208 | goto out; |
| 209 | } |
| 210 | |
Kyungmin Park | 6da24b7 | 2010-08-10 18:01:36 -0700 | [diff] [blame] | 211 | /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */ |
| 212 | if (card->csd.structure == 3) { |
| 213 | int ext_csd_struct = ext_csd[EXT_CSD_STRUCTURE]; |
| 214 | if (ext_csd_struct > 2) { |
| 215 | printk(KERN_ERR "%s: unrecognised EXT_CSD structure " |
| 216 | "version %d\n", mmc_hostname(card->host), |
| 217 | ext_csd_struct); |
| 218 | err = -EINVAL; |
| 219 | goto out; |
| 220 | } |
| 221 | } |
| 222 | |
Jarkko Lavinen | b1ebe38 | 2009-09-22 16:44:34 -0700 | [diff] [blame] | 223 | card->ext_csd.rev = ext_csd[EXT_CSD_REV]; |
Adrian Hunter | 11723ab | 2010-01-08 14:43:01 -0800 | [diff] [blame] | 224 | if (card->ext_csd.rev > 5) { |
Kyungmin Park | 6da24b7 | 2010-08-10 18:01:36 -0700 | [diff] [blame] | 225 | printk(KERN_ERR "%s: unrecognised EXT_CSD revision %d\n", |
| 226 | mmc_hostname(card->host), card->ext_csd.rev); |
Florin Malita | 00cedfa | 2007-10-13 12:27:20 -0400 | [diff] [blame] | 227 | err = -EINVAL; |
| 228 | goto out; |
Pierre Ossman | d7604d7 | 2007-07-23 00:34:07 +0200 | [diff] [blame] | 229 | } |
| 230 | |
Jarkko Lavinen | b1ebe38 | 2009-09-22 16:44:34 -0700 | [diff] [blame] | 231 | if (card->ext_csd.rev >= 2) { |
Pierre Ossman | d7604d7 | 2007-07-23 00:34:07 +0200 | [diff] [blame] | 232 | card->ext_csd.sectors = |
| 233 | ext_csd[EXT_CSD_SEC_CNT + 0] << 0 | |
| 234 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8 | |
| 235 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16 | |
| 236 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24; |
Hanumath Prasad | fc8a098 | 2010-08-10 18:01:45 -0700 | [diff] [blame^] | 237 | |
| 238 | /* Cards with density > 2GiB are sector addressed */ |
| 239 | if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512) |
Pierre Ossman | d7604d7 | 2007-07-23 00:34:07 +0200 | [diff] [blame] | 240 | mmc_card_set_blockaddr(card); |
| 241 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 242 | |
Adrian Hunter | 7198f3c | 2010-03-23 13:35:40 -0700 | [diff] [blame] | 243 | switch (ext_csd[EXT_CSD_CARD_TYPE] & EXT_CSD_CARD_TYPE_MASK) { |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 244 | case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26: |
| 245 | card->ext_csd.hs_max_dtr = 52000000; |
| 246 | break; |
| 247 | case EXT_CSD_CARD_TYPE_26: |
| 248 | card->ext_csd.hs_max_dtr = 26000000; |
| 249 | break; |
| 250 | default: |
| 251 | /* MMC v4 spec says this cannot happen */ |
| 252 | printk(KERN_WARNING "%s: card is mmc v4 but doesn't " |
| 253 | "support any high-speed modes.\n", |
| 254 | mmc_hostname(card->host)); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 255 | } |
| 256 | |
Jarkko Lavinen | b1ebe38 | 2009-09-22 16:44:34 -0700 | [diff] [blame] | 257 | if (card->ext_csd.rev >= 3) { |
| 258 | u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT]; |
| 259 | |
| 260 | /* Sleep / awake timeout in 100ns units */ |
| 261 | if (sa_shift > 0 && sa_shift <= 0x17) |
| 262 | card->ext_csd.sa_timeout = |
| 263 | 1 << ext_csd[EXT_CSD_S_A_TIMEOUT]; |
| 264 | } |
| 265 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 266 | out: |
| 267 | kfree(ext_csd); |
| 268 | |
| 269 | return err; |
| 270 | } |
| 271 | |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 272 | MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1], |
| 273 | card->raw_cid[2], card->raw_cid[3]); |
| 274 | MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1], |
| 275 | card->raw_csd[2], card->raw_csd[3]); |
| 276 | MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year); |
| 277 | MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev); |
| 278 | MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev); |
| 279 | MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid); |
| 280 | MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name); |
| 281 | MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid); |
| 282 | MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial); |
| 283 | |
| 284 | static struct attribute *mmc_std_attrs[] = { |
| 285 | &dev_attr_cid.attr, |
| 286 | &dev_attr_csd.attr, |
| 287 | &dev_attr_date.attr, |
| 288 | &dev_attr_fwrev.attr, |
| 289 | &dev_attr_hwrev.attr, |
| 290 | &dev_attr_manfid.attr, |
| 291 | &dev_attr_name.attr, |
| 292 | &dev_attr_oemid.attr, |
| 293 | &dev_attr_serial.attr, |
| 294 | NULL, |
| 295 | }; |
| 296 | |
| 297 | static struct attribute_group mmc_std_attr_group = { |
| 298 | .attrs = mmc_std_attrs, |
| 299 | }; |
| 300 | |
David Brownell | a4dbd67 | 2009-06-24 10:06:31 -0700 | [diff] [blame] | 301 | static const struct attribute_group *mmc_attr_groups[] = { |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 302 | &mmc_std_attr_group, |
| 303 | NULL, |
| 304 | }; |
| 305 | |
| 306 | static struct device_type mmc_type = { |
| 307 | .groups = mmc_attr_groups, |
| 308 | }; |
| 309 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 310 | /* |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 311 | * Handle the detection and initialisation of a card. |
| 312 | * |
Deepak Saxena | 8769392 | 2008-06-16 19:20:57 -0700 | [diff] [blame] | 313 | * In the case of a resume, "oldcard" will contain the card |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 314 | * we're trying to reinitialise. |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 315 | */ |
Pierre Ossman | 8c75dea | 2007-05-19 16:14:43 +0200 | [diff] [blame] | 316 | static int mmc_init_card(struct mmc_host *host, u32 ocr, |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 317 | struct mmc_card *oldcard) |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 318 | { |
| 319 | struct mmc_card *card; |
| 320 | int err; |
| 321 | u32 cid[4]; |
| 322 | unsigned int max_dtr; |
| 323 | |
| 324 | BUG_ON(!host); |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 325 | WARN_ON(!host->claimed); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 326 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 327 | /* |
| 328 | * Since we're changing the OCR value, we seem to |
| 329 | * need to tell some cards to go back to the idle |
| 330 | * state. We wait 1ms to give cards time to |
| 331 | * respond. |
| 332 | */ |
| 333 | mmc_go_idle(host); |
| 334 | |
| 335 | /* The extra bit indicates that we support high capacity */ |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 336 | err = mmc_send_op_cond(host, ocr | (1 << 30), NULL); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 337 | if (err) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 338 | goto err; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 339 | |
| 340 | /* |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 341 | * For SPI, enable CRC as appropriate. |
| 342 | */ |
| 343 | if (mmc_host_is_spi(host)) { |
| 344 | err = mmc_spi_set_crc(host, use_spi_crc); |
| 345 | if (err) |
| 346 | goto err; |
| 347 | } |
| 348 | |
| 349 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 350 | * Fetch CID from card. |
| 351 | */ |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 352 | if (mmc_host_is_spi(host)) |
| 353 | err = mmc_send_cid(host, cid); |
| 354 | else |
| 355 | err = mmc_all_send_cid(host, cid); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 356 | if (err) |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 357 | goto err; |
| 358 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 359 | if (oldcard) { |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 360 | if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) { |
| 361 | err = -ENOENT; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 362 | goto err; |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 363 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 364 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 365 | card = oldcard; |
| 366 | } else { |
| 367 | /* |
| 368 | * Allocate card structure. |
| 369 | */ |
Pierre Ossman | 51ec92e | 2008-03-21 23:54:50 +0100 | [diff] [blame] | 370 | card = mmc_alloc_card(host, &mmc_type); |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 371 | if (IS_ERR(card)) { |
| 372 | err = PTR_ERR(card); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 373 | goto err; |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 374 | } |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 375 | |
| 376 | card->type = MMC_TYPE_MMC; |
| 377 | card->rca = 1; |
| 378 | memcpy(card->raw_cid, cid, sizeof(card->raw_cid)); |
| 379 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 380 | |
| 381 | /* |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 382 | * For native busses: set card RCA and quit open drain mode. |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 383 | */ |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 384 | if (!mmc_host_is_spi(host)) { |
| 385 | err = mmc_set_relative_addr(card); |
| 386 | if (err) |
| 387 | goto free_card; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 388 | |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 389 | mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL); |
| 390 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 391 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 392 | if (!oldcard) { |
| 393 | /* |
| 394 | * Fetch CSD from card. |
| 395 | */ |
| 396 | err = mmc_send_csd(card, card->raw_csd); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 397 | if (err) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 398 | goto free_card; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 399 | |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 400 | err = mmc_decode_csd(card); |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 401 | if (err) |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 402 | goto free_card; |
| 403 | err = mmc_decode_cid(card); |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 404 | if (err) |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 405 | goto free_card; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 406 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 407 | |
| 408 | /* |
Pierre Ossman | 89a73cf | 2007-05-01 15:08:30 +0200 | [diff] [blame] | 409 | * Select card, as all following commands rely on that. |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 410 | */ |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 411 | if (!mmc_host_is_spi(host)) { |
| 412 | err = mmc_select_card(card); |
| 413 | if (err) |
| 414 | goto free_card; |
| 415 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 416 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 417 | if (!oldcard) { |
| 418 | /* |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 419 | * Fetch and process extended CSD. |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 420 | */ |
| 421 | err = mmc_read_ext_csd(card); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 422 | if (err) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 423 | goto free_card; |
| 424 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 425 | |
| 426 | /* |
Pierre Ossman | 89a73cf | 2007-05-01 15:08:30 +0200 | [diff] [blame] | 427 | * Activate high speed (if supported) |
| 428 | */ |
| 429 | if ((card->ext_csd.hs_max_dtr != 0) && |
| 430 | (host->caps & MMC_CAP_MMC_HIGHSPEED)) { |
| 431 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 432 | EXT_CSD_HS_TIMING, 1); |
Adrian Hunter | ef0b27d | 2009-09-22 16:44:37 -0700 | [diff] [blame] | 433 | if (err && err != -EBADMSG) |
Pierre Ossman | 89a73cf | 2007-05-01 15:08:30 +0200 | [diff] [blame] | 434 | goto free_card; |
| 435 | |
Adrian Hunter | ef0b27d | 2009-09-22 16:44:37 -0700 | [diff] [blame] | 436 | if (err) { |
| 437 | printk(KERN_WARNING "%s: switch to highspeed failed\n", |
| 438 | mmc_hostname(card->host)); |
| 439 | err = 0; |
| 440 | } else { |
| 441 | mmc_card_set_highspeed(card); |
| 442 | mmc_set_timing(card->host, MMC_TIMING_MMC_HS); |
| 443 | } |
Pierre Ossman | 89a73cf | 2007-05-01 15:08:30 +0200 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 447 | * Compute bus speed. |
| 448 | */ |
| 449 | max_dtr = (unsigned int)-1; |
| 450 | |
| 451 | if (mmc_card_highspeed(card)) { |
| 452 | if (max_dtr > card->ext_csd.hs_max_dtr) |
| 453 | max_dtr = card->ext_csd.hs_max_dtr; |
| 454 | } else if (max_dtr > card->csd.max_dtr) { |
| 455 | max_dtr = card->csd.max_dtr; |
| 456 | } |
| 457 | |
| 458 | mmc_set_clock(host, max_dtr); |
| 459 | |
Pierre Ossman | 89a73cf | 2007-05-01 15:08:30 +0200 | [diff] [blame] | 460 | /* |
| 461 | * Activate wide bus (if supported). |
| 462 | */ |
| 463 | if ((card->csd.mmca_vsn >= CSD_SPEC_VER_4) && |
Jarkko Lavinen | b30f8af | 2008-11-17 14:35:21 +0200 | [diff] [blame] | 464 | (host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA))) { |
| 465 | unsigned ext_csd_bit, bus_width; |
| 466 | |
| 467 | if (host->caps & MMC_CAP_8_BIT_DATA) { |
| 468 | ext_csd_bit = EXT_CSD_BUS_WIDTH_8; |
| 469 | bus_width = MMC_BUS_WIDTH_8; |
| 470 | } else { |
| 471 | ext_csd_bit = EXT_CSD_BUS_WIDTH_4; |
| 472 | bus_width = MMC_BUS_WIDTH_4; |
| 473 | } |
| 474 | |
Pierre Ossman | 89a73cf | 2007-05-01 15:08:30 +0200 | [diff] [blame] | 475 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
Jarkko Lavinen | b30f8af | 2008-11-17 14:35:21 +0200 | [diff] [blame] | 476 | EXT_CSD_BUS_WIDTH, ext_csd_bit); |
| 477 | |
Adrian Hunter | ef0b27d | 2009-09-22 16:44:37 -0700 | [diff] [blame] | 478 | if (err && err != -EBADMSG) |
Pierre Ossman | 89a73cf | 2007-05-01 15:08:30 +0200 | [diff] [blame] | 479 | goto free_card; |
| 480 | |
Adrian Hunter | ef0b27d | 2009-09-22 16:44:37 -0700 | [diff] [blame] | 481 | if (err) { |
| 482 | printk(KERN_WARNING "%s: switch to bus width %d " |
| 483 | "failed\n", mmc_hostname(card->host), |
| 484 | 1 << bus_width); |
| 485 | err = 0; |
| 486 | } else { |
| 487 | mmc_set_bus_width(card->host, bus_width); |
| 488 | } |
Pierre Ossman | 89a73cf | 2007-05-01 15:08:30 +0200 | [diff] [blame] | 489 | } |
| 490 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 491 | if (!oldcard) |
| 492 | host->card = card; |
| 493 | |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 494 | return 0; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 495 | |
| 496 | free_card: |
| 497 | if (!oldcard) |
| 498 | mmc_remove_card(card); |
| 499 | err: |
| 500 | |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 501 | return err; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | /* |
| 505 | * Host is being removed. Free up the current card. |
| 506 | */ |
| 507 | static void mmc_remove(struct mmc_host *host) |
| 508 | { |
| 509 | BUG_ON(!host); |
| 510 | BUG_ON(!host->card); |
| 511 | |
| 512 | mmc_remove_card(host->card); |
| 513 | host->card = NULL; |
| 514 | } |
| 515 | |
| 516 | /* |
| 517 | * Card detection callback from host. |
| 518 | */ |
| 519 | static void mmc_detect(struct mmc_host *host) |
| 520 | { |
| 521 | int err; |
| 522 | |
| 523 | BUG_ON(!host); |
| 524 | BUG_ON(!host->card); |
| 525 | |
| 526 | mmc_claim_host(host); |
| 527 | |
| 528 | /* |
| 529 | * Just check if our card has been removed. |
| 530 | */ |
| 531 | err = mmc_send_status(host->card, NULL); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 532 | |
| 533 | mmc_release_host(host); |
| 534 | |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 535 | if (err) { |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 536 | mmc_remove(host); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 537 | |
| 538 | mmc_claim_host(host); |
| 539 | mmc_detach_bus(host); |
| 540 | mmc_release_host(host); |
| 541 | } |
| 542 | } |
| 543 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 544 | /* |
| 545 | * Suspend callback from host. |
| 546 | */ |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 547 | static int mmc_suspend(struct mmc_host *host) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 548 | { |
| 549 | BUG_ON(!host); |
| 550 | BUG_ON(!host->card); |
| 551 | |
| 552 | mmc_claim_host(host); |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 553 | if (!mmc_host_is_spi(host)) |
| 554 | mmc_deselect_cards(host); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 555 | host->card->state &= ~MMC_STATE_HIGHSPEED; |
| 556 | mmc_release_host(host); |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 557 | |
| 558 | return 0; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | /* |
| 562 | * Resume callback from host. |
| 563 | * |
| 564 | * This function tries to determine if the same card is still present |
| 565 | * and, if so, restore all state to it. |
| 566 | */ |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 567 | static int mmc_resume(struct mmc_host *host) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 568 | { |
| 569 | int err; |
| 570 | |
| 571 | BUG_ON(!host); |
| 572 | BUG_ON(!host->card); |
| 573 | |
| 574 | mmc_claim_host(host); |
Pierre Ossman | 8c75dea | 2007-05-19 16:14:43 +0200 | [diff] [blame] | 575 | err = mmc_init_card(host, host->ocr, host->card); |
Pierre Ossman | 2986d0b | 2007-07-22 17:52:06 +0200 | [diff] [blame] | 576 | mmc_release_host(host); |
| 577 | |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 578 | return err; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 579 | } |
| 580 | |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 581 | static void mmc_power_restore(struct mmc_host *host) |
| 582 | { |
| 583 | host->card->state &= ~MMC_STATE_HIGHSPEED; |
| 584 | mmc_claim_host(host); |
| 585 | mmc_init_card(host, host->ocr, host->card); |
| 586 | mmc_release_host(host); |
| 587 | } |
| 588 | |
Jarkko Lavinen | b1ebe38 | 2009-09-22 16:44:34 -0700 | [diff] [blame] | 589 | static int mmc_sleep(struct mmc_host *host) |
| 590 | { |
| 591 | struct mmc_card *card = host->card; |
| 592 | int err = -ENOSYS; |
| 593 | |
| 594 | if (card && card->ext_csd.rev >= 3) { |
| 595 | err = mmc_card_sleepawake(host, 1); |
| 596 | if (err < 0) |
| 597 | pr_debug("%s: Error %d while putting card into sleep", |
| 598 | mmc_hostname(host), err); |
| 599 | } |
| 600 | |
| 601 | return err; |
| 602 | } |
| 603 | |
| 604 | static int mmc_awake(struct mmc_host *host) |
| 605 | { |
| 606 | struct mmc_card *card = host->card; |
| 607 | int err = -ENOSYS; |
| 608 | |
| 609 | if (card && card->ext_csd.rev >= 3) { |
| 610 | err = mmc_card_sleepawake(host, 0); |
| 611 | if (err < 0) |
| 612 | pr_debug("%s: Error %d while awaking sleeping card", |
| 613 | mmc_hostname(host), err); |
| 614 | } |
| 615 | |
| 616 | return err; |
| 617 | } |
| 618 | |
Adrian Hunter | 9feae24 | 2009-09-22 16:44:32 -0700 | [diff] [blame] | 619 | static const struct mmc_bus_ops mmc_ops = { |
Jarkko Lavinen | b1ebe38 | 2009-09-22 16:44:34 -0700 | [diff] [blame] | 620 | .awake = mmc_awake, |
| 621 | .sleep = mmc_sleep, |
Adrian Hunter | 9feae24 | 2009-09-22 16:44:32 -0700 | [diff] [blame] | 622 | .remove = mmc_remove, |
| 623 | .detect = mmc_detect, |
| 624 | .suspend = NULL, |
| 625 | .resume = NULL, |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 626 | .power_restore = mmc_power_restore, |
Adrian Hunter | 9feae24 | 2009-09-22 16:44:32 -0700 | [diff] [blame] | 627 | }; |
| 628 | |
| 629 | static const struct mmc_bus_ops mmc_ops_unsafe = { |
Jarkko Lavinen | b1ebe38 | 2009-09-22 16:44:34 -0700 | [diff] [blame] | 630 | .awake = mmc_awake, |
| 631 | .sleep = mmc_sleep, |
Adrian Hunter | 9feae24 | 2009-09-22 16:44:32 -0700 | [diff] [blame] | 632 | .remove = mmc_remove, |
| 633 | .detect = mmc_detect, |
| 634 | .suspend = mmc_suspend, |
| 635 | .resume = mmc_resume, |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 636 | .power_restore = mmc_power_restore, |
Adrian Hunter | 9feae24 | 2009-09-22 16:44:32 -0700 | [diff] [blame] | 637 | }; |
| 638 | |
| 639 | static void mmc_attach_bus_ops(struct mmc_host *host) |
| 640 | { |
| 641 | const struct mmc_bus_ops *bus_ops; |
| 642 | |
Ben Hutchings | bd68e08 | 2009-12-14 18:01:29 -0800 | [diff] [blame] | 643 | if (host->caps & MMC_CAP_NONREMOVABLE || !mmc_assume_removable) |
Adrian Hunter | 9feae24 | 2009-09-22 16:44:32 -0700 | [diff] [blame] | 644 | bus_ops = &mmc_ops_unsafe; |
| 645 | else |
| 646 | bus_ops = &mmc_ops; |
| 647 | mmc_attach_bus(host, bus_ops); |
| 648 | } |
| 649 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 650 | /* |
| 651 | * Starting point for MMC card init. |
| 652 | */ |
| 653 | int mmc_attach_mmc(struct mmc_host *host, u32 ocr) |
| 654 | { |
| 655 | int err; |
| 656 | |
| 657 | BUG_ON(!host); |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 658 | WARN_ON(!host->claimed); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 659 | |
Adrian Hunter | 9feae24 | 2009-09-22 16:44:32 -0700 | [diff] [blame] | 660 | mmc_attach_bus_ops(host); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 661 | |
| 662 | /* |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 663 | * We need to get OCR a different way for SPI. |
| 664 | */ |
| 665 | if (mmc_host_is_spi(host)) { |
| 666 | err = mmc_spi_read_ocr(host, 1, &ocr); |
| 667 | if (err) |
| 668 | goto err; |
| 669 | } |
| 670 | |
| 671 | /* |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 672 | * Sanity check the voltages that the card claims to |
| 673 | * support. |
| 674 | */ |
| 675 | if (ocr & 0x7F) { |
| 676 | printk(KERN_WARNING "%s: card claims to support voltages " |
| 677 | "below the defined range. These will be ignored.\n", |
| 678 | mmc_hostname(host)); |
| 679 | ocr &= ~0x7F; |
| 680 | } |
| 681 | |
| 682 | host->ocr = mmc_select_voltage(host, ocr); |
| 683 | |
| 684 | /* |
| 685 | * Can we support the voltage of the card? |
| 686 | */ |
Pierre Ossman | 109b5be | 2007-07-23 00:12:10 +0200 | [diff] [blame] | 687 | if (!host->ocr) { |
| 688 | err = -EINVAL; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 689 | goto err; |
Pierre Ossman | 109b5be | 2007-07-23 00:12:10 +0200 | [diff] [blame] | 690 | } |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 691 | |
| 692 | /* |
| 693 | * Detect and init the card. |
| 694 | */ |
Pierre Ossman | 8c75dea | 2007-05-19 16:14:43 +0200 | [diff] [blame] | 695 | err = mmc_init_card(host, host->ocr, NULL); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 696 | if (err) |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 697 | goto err; |
| 698 | |
| 699 | mmc_release_host(host); |
| 700 | |
Pierre Ossman | 4101c16 | 2007-05-19 13:39:01 +0200 | [diff] [blame] | 701 | err = mmc_add_card(host->card); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 702 | if (err) |
Pierre Ossman | 2986d0b | 2007-07-22 17:52:06 +0200 | [diff] [blame] | 703 | goto remove_card; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 704 | |
| 705 | return 0; |
| 706 | |
Pierre Ossman | 2986d0b | 2007-07-22 17:52:06 +0200 | [diff] [blame] | 707 | remove_card: |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 708 | mmc_remove_card(host->card); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 709 | host->card = NULL; |
Pierre Ossman | 2986d0b | 2007-07-22 17:52:06 +0200 | [diff] [blame] | 710 | mmc_claim_host(host); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 711 | err: |
| 712 | mmc_detach_bus(host); |
| 713 | mmc_release_host(host); |
| 714 | |
Pierre Ossman | 109b5be | 2007-07-23 00:12:10 +0200 | [diff] [blame] | 715 | printk(KERN_ERR "%s: error %d whilst initialising MMC card\n", |
| 716 | mmc_hostname(host), err); |
| 717 | |
Pierre Ossman | adf66a0 | 2007-07-22 23:08:30 +0200 | [diff] [blame] | 718 | return err; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 719 | } |