blob: 6f42050b7ccc6d829d6b58ece04b1177e73cbe80 [file] [log] [blame]
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/core/sd.c
Pierre Ossman7ea239d2006-12-31 00:11:32 +01003 *
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 Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Paul Gortmaker0205a902011-07-15 12:01:27 -040015#include <linux/stat.h>
Ulf Hansson0cb403a2013-10-10 14:20:05 +020016#include <linux/pm_runtime.h>
Pierre Ossman7ea239d2006-12-31 00:11:32 +010017
18#include <linux/mmc/host.h>
19#include <linux/mmc/card.h>
20#include <linux/mmc/mmc.h>
Pierre Ossman3373c0a2007-05-31 22:25:11 +020021#include <linux/mmc/sd.h>
Pierre Ossman7ea239d2006-12-31 00:11:32 +010022
23#include "core.h"
Pierre Ossman4101c162007-05-19 13:39:01 +020024#include "bus.h"
Pierre Ossman7ea239d2006-12-31 00:11:32 +010025#include "mmc_ops.h"
Mark Brownce101492011-02-10 10:58:37 +000026#include "sd.h"
Pierre Ossman7ea239d2006-12-31 00:11:32 +010027#include "sd_ops.h"
28
Pierre Ossman7ea239d2006-12-31 00:11:32 +010029static const unsigned int tran_exp[] = {
30 10000, 100000, 1000000, 10000000,
31 0, 0, 0, 0
32};
33
34static const unsigned char tran_mant[] = {
35 0, 10, 12, 13, 15, 20, 25, 30,
36 35, 40, 45, 50, 55, 60, 70, 80,
37};
38
39static const unsigned int tacc_exp[] = {
40 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
41};
42
43static const unsigned int tacc_mant[] = {
44 0, 10, 12, 13, 15, 20, 25, 30,
45 35, 40, 45, 50, 55, 60, 70, 80,
46};
47
48#define UNSTUFF_BITS(resp,start,size) \
49 ({ \
50 const int __size = size; \
51 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
52 const int __off = 3 - ((start) / 32); \
53 const int __shft = (start) & 31; \
54 u32 __res; \
55 \
56 __res = resp[__off] >> __shft; \
57 if (__size + __shft > 32) \
58 __res |= resp[__off-1] << ((32 - __shft) % 32); \
59 __res & __mask; \
60 })
61
62/*
63 * Given the decoded CSD structure, decode the raw CID to our CID structure.
64 */
Michal Miroslaw71578a12010-08-10 18:01:40 -070065void mmc_decode_cid(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +010066{
67 u32 *resp = card->raw_cid;
68
69 memset(&card->cid, 0, sizeof(struct mmc_cid));
70
71 /*
72 * SD doesn't currently have a version field so we will
73 * have to assume we can parse this.
74 */
75 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
76 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
77 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
78 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
79 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
80 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
81 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
82 card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
83 card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
84 card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
85 card->cid.year = UNSTUFF_BITS(resp, 12, 8);
86 card->cid.month = UNSTUFF_BITS(resp, 8, 4);
87
88 card->cid.year += 2000; /* SD cards year offset */
89}
90
91/*
92 * Given a 128-bit response, decode to our card CSD structure.
93 */
Pierre Ossmanbd766312007-05-01 16:11:57 +020094static int mmc_decode_csd(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +010095{
96 struct mmc_csd *csd = &card->csd;
97 unsigned int e, m, csd_struct;
98 u32 *resp = card->raw_csd;
99
100 csd_struct = UNSTUFF_BITS(resp, 126, 2);
101
102 switch (csd_struct) {
103 case 0:
104 m = UNSTUFF_BITS(resp, 115, 4);
105 e = UNSTUFF_BITS(resp, 112, 3);
106 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
107 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
108
109 m = UNSTUFF_BITS(resp, 99, 4);
110 e = UNSTUFF_BITS(resp, 96, 3);
111 csd->max_dtr = tran_exp[e] * tran_mant[m];
112 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
113
114 e = UNSTUFF_BITS(resp, 47, 3);
115 m = UNSTUFF_BITS(resp, 62, 12);
116 csd->capacity = (1 + m) << (e + 2);
117
118 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
119 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
120 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
121 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
122 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
123 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
124 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700125
126 if (UNSTUFF_BITS(resp, 46, 1)) {
127 csd->erase_size = 1;
128 } else if (csd->write_blkbits >= 9) {
129 csd->erase_size = UNSTUFF_BITS(resp, 39, 7) + 1;
130 csd->erase_size <<= csd->write_blkbits - 9;
131 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100132 break;
133 case 1:
134 /*
Arindam Nath3a303512011-05-05 12:19:03 +0530135 * This is a block-addressed SDHC or SDXC card. Most
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100136 * interesting fields are unused and have fixed
137 * values. To avoid getting tripped by buggy cards,
138 * we assume those fixed values ourselves.
139 */
140 mmc_card_set_blockaddr(card);
141
142 csd->tacc_ns = 0; /* Unused */
143 csd->tacc_clks = 0; /* Unused */
144
145 m = UNSTUFF_BITS(resp, 99, 4);
146 e = UNSTUFF_BITS(resp, 96, 3);
147 csd->max_dtr = tran_exp[e] * tran_mant[m];
148 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
Arindam Nath3a303512011-05-05 12:19:03 +0530149 csd->c_size = UNSTUFF_BITS(resp, 48, 22);
150
151 /* SDXC cards have a minimum C_SIZE of 0x00FFFF */
152 if (csd->c_size >= 0xFFFF)
153 mmc_card_set_ext_capacity(card);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100154
155 m = UNSTUFF_BITS(resp, 48, 22);
156 csd->capacity = (1 + m) << 10;
157
158 csd->read_blkbits = 9;
159 csd->read_partial = 0;
160 csd->write_misalign = 0;
161 csd->read_misalign = 0;
162 csd->r2w_factor = 4; /* Unused */
163 csd->write_blkbits = 9;
164 csd->write_partial = 0;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700165 csd->erase_size = 1;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100166 break;
167 default:
Girish K Sa3c76eb2011-10-11 11:44:09 +0530168 pr_err("%s: unrecognised CSD structure version %d\n",
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100169 mmc_hostname(card->host), csd_struct);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200170 return -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100171 }
Pierre Ossmanbd766312007-05-01 16:11:57 +0200172
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700173 card->erase_size = csd->erase_size;
174
Pierre Ossmanbd766312007-05-01 16:11:57 +0200175 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100176}
177
178/*
179 * Given a 64-bit response, decode to our card SCR structure.
180 */
Pierre Ossmanbd766312007-05-01 16:11:57 +0200181static int mmc_decode_scr(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100182{
183 struct sd_scr *scr = &card->scr;
184 unsigned int scr_struct;
185 u32 resp[4];
186
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100187 resp[3] = card->raw_scr[1];
188 resp[2] = card->raw_scr[0];
189
190 scr_struct = UNSTUFF_BITS(resp, 60, 4);
191 if (scr_struct != 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530192 pr_err("%s: unrecognised SCR structure version %d\n",
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100193 mmc_hostname(card->host), scr_struct);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200194 return -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100195 }
196
197 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
198 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
Arindam Nath013909c2011-05-05 12:18:58 +0530199 if (scr->sda_vsn == SCR_SPEC_VER_2)
200 /* Check if Physical Layer Spec v3.0 is supported */
201 scr->sda_spec3 = UNSTUFF_BITS(resp, 47, 1);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200202
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700203 if (UNSTUFF_BITS(resp, 55, 1))
204 card->erased_byte = 0xFF;
205 else
206 card->erased_byte = 0x0;
207
Andrei Warkentinf0d89972011-05-23 15:06:38 -0500208 if (scr->sda_spec3)
209 scr->cmds = UNSTUFF_BITS(resp, 32, 2);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200210 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100211}
212
213/*
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700214 * Fetch and process SD Status register.
215 */
216static int mmc_read_ssr(struct mmc_card *card)
217{
218 unsigned int au, es, et, eo;
Jaehoon Chung77776fd2013-07-18 13:34:41 +0900219 int err, i, max_au;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700220 u32 *ssr;
221
222 if (!(card->csd.cmdclass & CCC_APP_SPEC)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530223 pr_warning("%s: card lacks mandatory SD Status "
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700224 "function.\n", mmc_hostname(card->host));
225 return 0;
226 }
227
228 ssr = kmalloc(64, GFP_KERNEL);
229 if (!ssr)
230 return -ENOMEM;
231
232 err = mmc_app_sd_status(card, ssr);
233 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530234 pr_warning("%s: problem reading SD Status "
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700235 "register.\n", mmc_hostname(card->host));
236 err = 0;
237 goto out;
238 }
239
240 for (i = 0; i < 16; i++)
241 ssr[i] = be32_to_cpu(ssr[i]);
242
Jaehoon Chung77776fd2013-07-18 13:34:41 +0900243 /* SD3.0 increases max AU size to 64MB (0xF) from 4MB (0x9) */
244 max_au = card->scr.sda_spec3 ? 0xF : 0x9;
245
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700246 /*
247 * UNSTUFF_BITS only works with four u32s so we have to offset the
248 * bitfield positions accordingly.
249 */
250 au = UNSTUFF_BITS(ssr, 428 - 384, 4);
Jaehoon Chung77776fd2013-07-18 13:34:41 +0900251 if (au > 0 && au <= max_au) {
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700252 card->ssr.au = 1 << (au + 4);
253 es = UNSTUFF_BITS(ssr, 408 - 384, 16);
254 et = UNSTUFF_BITS(ssr, 402 - 384, 6);
255 eo = UNSTUFF_BITS(ssr, 400 - 384, 2);
256 if (es && et) {
257 card->ssr.erase_timeout = (et * 1000) / es;
258 card->ssr.erase_offset = eo * 1000;
259 }
260 } else {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530261 pr_warning("%s: SD Status: Invalid Allocation Unit "
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700262 "size.\n", mmc_hostname(card->host));
263 }
264out:
265 kfree(ssr);
266 return err;
267}
268
269/*
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200270 * Fetches and decodes switch information
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100271 */
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200272static int mmc_read_switch(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100273{
274 int err;
275 u8 *status;
276
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200277 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
Pierre Ossman17b04292007-07-22 22:18:46 +0200278 return 0;
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200279
280 if (!(card->csd.cmdclass & CCC_SWITCH)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530281 pr_warning("%s: card lacks mandatory switch "
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200282 "function, performance might suffer.\n",
283 mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200284 return 0;
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200285 }
286
Pierre Ossman17b04292007-07-22 22:18:46 +0200287 err = -EIO;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100288
289 status = kmalloc(64, GFP_KERNEL);
290 if (!status) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530291 pr_err("%s: could not allocate a buffer for "
Arindam Nath013909c2011-05-05 12:18:58 +0530292 "switch capabilities.\n",
293 mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200294 return -ENOMEM;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100295 }
296
Aaron Lua39ca6ae2012-07-03 14:16:11 +0800297 /*
298 * Find out the card's support bits with a mode 0 operation.
299 * The argument does not matter, as the support bits do not
300 * change with the arguments.
301 */
302 err = mmc_sd_switch(card, 0, 0, 0, status);
Pierre Ossman17b04292007-07-22 22:18:46 +0200303 if (err) {
Arindam Nath013909c2011-05-05 12:18:58 +0530304 /*
305 * If the host or the card can't do the switch,
306 * fail more gracefully.
307 */
308 if (err != -EINVAL && err != -ENOSYS && err != -EFAULT)
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200309 goto out;
310
Girish K Sa3c76eb2011-10-11 11:44:09 +0530311 pr_warning("%s: problem reading Bus Speed modes.\n",
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200312 mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200313 err = 0;
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200314
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100315 goto out;
316 }
317
Qiang Liufffe5d52011-11-08 08:43:08 -0500318 if (status[13] & SD_MODE_HIGH_SPEED)
319 card->sw_caps.hs_max_dtr = HIGH_SPEED_MAX_DTR;
Subhash Jadavanif2815f62011-08-10 11:16:01 +0530320
Arindam Nath013909c2011-05-05 12:18:58 +0530321 if (card->scr.sda_spec3) {
322 card->sw_caps.sd3_bus_mode = status[13];
Aaron Lua39ca6ae2012-07-03 14:16:11 +0800323 /* Driver Strengths supported by the card */
Arindam Nath013909c2011-05-05 12:18:58 +0530324 card->sw_caps.sd3_drv_type = status[9];
Arindam Nath013909c2011-05-05 12:18:58 +0530325 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100326
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200327out:
328 kfree(status);
329
330 return err;
331}
332
333/*
334 * Test if the card supports high-speed mode and, if so, switch to it.
335 */
Michal Miroslaw71578a12010-08-10 18:01:40 -0700336int mmc_sd_switch_hs(struct mmc_card *card)
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200337{
338 int err;
339 u8 *status;
340
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200341 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
Pierre Ossman17b04292007-07-22 22:18:46 +0200342 return 0;
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200343
344 if (!(card->csd.cmdclass & CCC_SWITCH))
Pierre Ossman17b04292007-07-22 22:18:46 +0200345 return 0;
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200346
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200347 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
Pierre Ossman17b04292007-07-22 22:18:46 +0200348 return 0;
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200349
350 if (card->sw_caps.hs_max_dtr == 0)
Pierre Ossman17b04292007-07-22 22:18:46 +0200351 return 0;
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200352
Pierre Ossman17b04292007-07-22 22:18:46 +0200353 err = -EIO;
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200354
355 status = kmalloc(64, GFP_KERNEL);
356 if (!status) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530357 pr_err("%s: could not allocate a buffer for "
Pierre Ossmanfacba912007-07-24 21:53:43 +0200358 "switch capabilities.\n", mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200359 return -ENOMEM;
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200360 }
361
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100362 err = mmc_sd_switch(card, 1, 0, 1, status);
Pierre Ossman17b04292007-07-22 22:18:46 +0200363 if (err)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100364 goto out;
365
366 if ((status[16] & 0xF) != 1) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530367 pr_warning("%s: Problem switching card "
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100368 "into high-speed mode!\n",
369 mmc_hostname(card->host));
Michal Miroslaw71578a12010-08-10 18:01:40 -0700370 err = 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100371 } else {
Michal Miroslaw71578a12010-08-10 18:01:40 -0700372 err = 1;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100373 }
374
375out:
376 kfree(status);
377
378 return err;
379}
380
Arindam Nathd6d50a12011-05-05 12:18:59 +0530381static int sd_select_driver_type(struct mmc_card *card, u8 *status)
382{
Philip Rakityca8e99b2011-07-06 08:51:32 -0700383 int host_drv_type = SD_DRIVER_TYPE_B;
384 int card_drv_type = SD_DRIVER_TYPE_B;
385 int drive_strength;
Arindam Nathd6d50a12011-05-05 12:18:59 +0530386 int err;
387
388 /*
389 * If the host doesn't support any of the Driver Types A,C or D,
Philip Rakityca8e99b2011-07-06 08:51:32 -0700390 * or there is no board specific handler then default Driver
391 * Type B is used.
Arindam Nathd6d50a12011-05-05 12:18:59 +0530392 */
393 if (!(card->host->caps & (MMC_CAP_DRIVER_TYPE_A | MMC_CAP_DRIVER_TYPE_C
394 | MMC_CAP_DRIVER_TYPE_D)))
395 return 0;
396
Philip Rakityca8e99b2011-07-06 08:51:32 -0700397 if (!card->host->ops->select_drive_strength)
398 return 0;
Arindam Nathd6d50a12011-05-05 12:18:59 +0530399
Philip Rakityca8e99b2011-07-06 08:51:32 -0700400 if (card->host->caps & MMC_CAP_DRIVER_TYPE_A)
401 host_drv_type |= SD_DRIVER_TYPE_A;
402
403 if (card->host->caps & MMC_CAP_DRIVER_TYPE_C)
404 host_drv_type |= SD_DRIVER_TYPE_C;
405
406 if (card->host->caps & MMC_CAP_DRIVER_TYPE_D)
407 host_drv_type |= SD_DRIVER_TYPE_D;
408
409 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_A)
410 card_drv_type |= SD_DRIVER_TYPE_A;
411
412 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_C)
413 card_drv_type |= SD_DRIVER_TYPE_C;
414
415 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_D)
416 card_drv_type |= SD_DRIVER_TYPE_D;
417
418 /*
419 * The drive strength that the hardware can support
420 * depends on the board design. Pass the appropriate
421 * information and let the hardware specific code
422 * return what is possible given the options
423 */
Sujit Reddy Thumma2c4967f742012-02-04 16:14:50 -0500424 mmc_host_clk_hold(card->host);
Philip Rakityca8e99b2011-07-06 08:51:32 -0700425 drive_strength = card->host->ops->select_drive_strength(
426 card->sw_caps.uhs_max_dtr,
427 host_drv_type, card_drv_type);
Sujit Reddy Thumma2c4967f742012-02-04 16:14:50 -0500428 mmc_host_clk_release(card->host);
Philip Rakityca8e99b2011-07-06 08:51:32 -0700429
430 err = mmc_sd_switch(card, 1, 2, drive_strength, status);
Arindam Nathd6d50a12011-05-05 12:18:59 +0530431 if (err)
432 return err;
433
Philip Rakityca8e99b2011-07-06 08:51:32 -0700434 if ((status[15] & 0xF) != drive_strength) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530435 pr_warning("%s: Problem setting drive strength!\n",
Arindam Nathd6d50a12011-05-05 12:18:59 +0530436 mmc_hostname(card->host));
437 return 0;
438 }
439
Philip Rakityca8e99b2011-07-06 08:51:32 -0700440 mmc_set_driver_type(card->host, drive_strength);
Arindam Nathd6d50a12011-05-05 12:18:59 +0530441
442 return 0;
443}
444
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530445static void sd_update_bus_speed_mode(struct mmc_card *card)
Arindam Nath49c468f2011-05-05 12:19:01 +0530446{
Arindam Nath49c468f2011-05-05 12:19:01 +0530447 /*
448 * If the host doesn't support any of the UHS-I modes, fallback on
449 * default speed.
450 */
Johan Rudholm3f8a7fa2013-01-28 15:08:24 +0100451 if (!mmc_host_uhs(card->host)) {
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530452 card->sd_bus_speed = 0;
453 return;
454 }
Arindam Nath49c468f2011-05-05 12:19:01 +0530455
456 if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
457 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530458 card->sd_bus_speed = UHS_SDR104_BUS_SPEED;
Arindam Nath49c468f2011-05-05 12:19:01 +0530459 } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
460 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530461 card->sd_bus_speed = UHS_DDR50_BUS_SPEED;
Arindam Nath49c468f2011-05-05 12:19:01 +0530462 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
463 MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
464 SD_MODE_UHS_SDR50)) {
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530465 card->sd_bus_speed = UHS_SDR50_BUS_SPEED;
Arindam Nath49c468f2011-05-05 12:19:01 +0530466 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
467 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
468 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530469 card->sd_bus_speed = UHS_SDR25_BUS_SPEED;
Arindam Nath49c468f2011-05-05 12:19:01 +0530470 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
471 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
472 MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
473 SD_MODE_UHS_SDR12)) {
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530474 card->sd_bus_speed = UHS_SDR12_BUS_SPEED;
475 }
476}
477
478static int sd_set_bus_speed_mode(struct mmc_card *card, u8 *status)
479{
480 int err;
481 unsigned int timing = 0;
482
483 switch (card->sd_bus_speed) {
484 case UHS_SDR104_BUS_SPEED:
485 timing = MMC_TIMING_UHS_SDR104;
486 card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
487 break;
488 case UHS_DDR50_BUS_SPEED:
489 timing = MMC_TIMING_UHS_DDR50;
490 card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
491 break;
492 case UHS_SDR50_BUS_SPEED:
493 timing = MMC_TIMING_UHS_SDR50;
494 card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
495 break;
496 case UHS_SDR25_BUS_SPEED:
497 timing = MMC_TIMING_UHS_SDR25;
498 card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
499 break;
500 case UHS_SDR12_BUS_SPEED:
501 timing = MMC_TIMING_UHS_SDR12;
502 card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
503 break;
504 default:
505 return 0;
Arindam Nath49c468f2011-05-05 12:19:01 +0530506 }
507
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530508 err = mmc_sd_switch(card, 1, 0, card->sd_bus_speed, status);
Arindam Nath49c468f2011-05-05 12:19:01 +0530509 if (err)
510 return err;
511
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530512 if ((status[16] & 0xF) != card->sd_bus_speed)
Girish K Sa3c76eb2011-10-11 11:44:09 +0530513 pr_warning("%s: Problem setting bus speed mode!\n",
Arindam Nath49c468f2011-05-05 12:19:01 +0530514 mmc_hostname(card->host));
515 else {
516 mmc_set_timing(card->host, timing);
517 mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
518 }
519
520 return 0;
521}
522
Aaron Lu55c46652012-07-04 13:31:48 +0800523/* Get host's max current setting at its current voltage */
524static u32 sd_get_host_max_current(struct mmc_host *host)
525{
526 u32 voltage, max_current;
527
528 voltage = 1 << host->ios.vdd;
529 switch (voltage) {
530 case MMC_VDD_165_195:
531 max_current = host->max_current_180;
532 break;
533 case MMC_VDD_29_30:
534 case MMC_VDD_30_31:
535 max_current = host->max_current_300;
536 break;
537 case MMC_VDD_32_33:
538 case MMC_VDD_33_34:
539 max_current = host->max_current_330;
540 break;
541 default:
542 max_current = 0;
543 }
544
545 return max_current;
546}
547
Arindam Nath5371c922011-05-05 12:19:02 +0530548static int sd_set_current_limit(struct mmc_card *card, u8 *status)
549{
Philip Rakity0aa67702012-05-27 18:36:33 -0700550 int current_limit = SD_SET_CURRENT_NO_CHANGE;
Arindam Nath5371c922011-05-05 12:19:02 +0530551 int err;
Aaron Lu55c46652012-07-04 13:31:48 +0800552 u32 max_current;
Arindam Nath5371c922011-05-05 12:19:02 +0530553
554 /*
555 * Current limit switch is only defined for SDR50, SDR104, and DDR50
Philip Rakity0aa67702012-05-27 18:36:33 -0700556 * bus speed modes. For other bus speed modes, we do not change the
557 * current limit.
Aaron Lu55c46652012-07-04 13:31:48 +0800558 */
559 if ((card->sd_bus_speed != UHS_SDR50_BUS_SPEED) &&
560 (card->sd_bus_speed != UHS_SDR104_BUS_SPEED) &&
561 (card->sd_bus_speed != UHS_DDR50_BUS_SPEED))
562 return 0;
563
564 /*
565 * Host has different current capabilities when operating at
566 * different voltages, so find out its max current first.
567 */
568 max_current = sd_get_host_max_current(card->host);
569
570 /*
Aaron Lua39ca6ae2012-07-03 14:16:11 +0800571 * We only check host's capability here, if we set a limit that is
572 * higher than the card's maximum current, the card will be using its
573 * maximum current, e.g. if the card's maximum current is 300ma, and
574 * when we set current limit to 200ma, the card will draw 200ma, and
575 * when we set current limit to 400/600/800ma, the card will draw its
576 * maximum 300ma from the host.
Arindam Nath5371c922011-05-05 12:19:02 +0530577 */
Aaron Lu55c46652012-07-04 13:31:48 +0800578 if (max_current >= 800)
579 current_limit = SD_SET_CURRENT_LIMIT_800;
580 else if (max_current >= 600)
581 current_limit = SD_SET_CURRENT_LIMIT_600;
582 else if (max_current >= 400)
583 current_limit = SD_SET_CURRENT_LIMIT_400;
584 else if (max_current >= 200)
585 current_limit = SD_SET_CURRENT_LIMIT_200;
Arindam Nath5371c922011-05-05 12:19:02 +0530586
Philip Rakity0aa67702012-05-27 18:36:33 -0700587 if (current_limit != SD_SET_CURRENT_NO_CHANGE) {
588 err = mmc_sd_switch(card, 1, 3, current_limit, status);
589 if (err)
590 return err;
Arindam Nath5371c922011-05-05 12:19:02 +0530591
Philip Rakity0aa67702012-05-27 18:36:33 -0700592 if (((status[15] >> 4) & 0x0F) != current_limit)
593 pr_warning("%s: Problem setting current limit!\n",
594 mmc_hostname(card->host));
Arindam Nath5371c922011-05-05 12:19:02 +0530595
Philip Rakity0aa67702012-05-27 18:36:33 -0700596 }
Aaron Lua39ca6ae2012-07-03 14:16:11 +0800597
Arindam Nath5371c922011-05-05 12:19:02 +0530598 return 0;
599}
600
Arindam Nathd6d50a12011-05-05 12:18:59 +0530601/*
602 * UHS-I specific initialization procedure
603 */
604static int mmc_sd_init_uhs_card(struct mmc_card *card)
605{
606 int err;
607 u8 *status;
608
609 if (!card->scr.sda_spec3)
610 return 0;
611
612 if (!(card->csd.cmdclass & CCC_SWITCH))
613 return 0;
614
615 status = kmalloc(64, GFP_KERNEL);
616 if (!status) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530617 pr_err("%s: could not allocate a buffer for "
Arindam Nathd6d50a12011-05-05 12:18:59 +0530618 "switch capabilities.\n", mmc_hostname(card->host));
619 return -ENOMEM;
620 }
621
622 /* Set 4-bit bus width */
623 if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
624 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
625 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
626 if (err)
627 goto out;
628
629 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
630 }
631
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530632 /*
633 * Select the bus speed mode depending on host
634 * and card capability.
635 */
636 sd_update_bus_speed_mode(card);
637
Arindam Nathd6d50a12011-05-05 12:18:59 +0530638 /* Set the driver strength for the card */
639 err = sd_select_driver_type(card, status);
Arindam Nath49c468f2011-05-05 12:19:01 +0530640 if (err)
641 goto out;
642
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530643 /* Set current limit for the card */
644 err = sd_set_current_limit(card, status);
Arindam Nath5371c922011-05-05 12:19:02 +0530645 if (err)
646 goto out;
647
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530648 /* Set bus speed mode of the card */
649 err = sd_set_bus_speed_mode(card, status);
Arindam Nathb513ea22011-05-05 12:19:04 +0530650 if (err)
651 goto out;
652
Fredrik Soderstedt810e08e2013-04-17 13:50:53 +0200653 /*
654 * SPI mode doesn't define CMD19 and tuning is only valid for SDR50 and
655 * SDR104 mode SD-cards. Note that tuning is mandatory for SDR104.
656 */
657 if (!mmc_host_is_spi(card->host) && card->host->ops->execute_tuning &&
658 (card->sd_bus_speed == UHS_SDR50_BUS_SPEED ||
659 card->sd_bus_speed == UHS_SDR104_BUS_SPEED)) {
Sujit Reddy Thumma2c4967f742012-02-04 16:14:50 -0500660 mmc_host_clk_hold(card->host);
Girish K Sa4924c72012-01-11 14:04:52 -0500661 err = card->host->ops->execute_tuning(card->host,
662 MMC_SEND_TUNING_BLOCK);
Sujit Reddy Thumma2c4967f742012-02-04 16:14:50 -0500663 mmc_host_clk_release(card->host);
664 }
Arindam Nathd6d50a12011-05-05 12:18:59 +0530665
666out:
667 kfree(status);
668
669 return err;
670}
671
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100672MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
673 card->raw_cid[2], card->raw_cid[3]);
674MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
675 card->raw_csd[2], card->raw_csd[3]);
676MMC_DEV_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]);
677MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700678MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
679MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100680MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
681MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
682MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
683MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
684MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
685MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
686
687
688static struct attribute *sd_std_attrs[] = {
689 &dev_attr_cid.attr,
690 &dev_attr_csd.attr,
691 &dev_attr_scr.attr,
692 &dev_attr_date.attr,
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700693 &dev_attr_erase_size.attr,
694 &dev_attr_preferred_erase_size.attr,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100695 &dev_attr_fwrev.attr,
696 &dev_attr_hwrev.attr,
697 &dev_attr_manfid.attr,
698 &dev_attr_name.attr,
699 &dev_attr_oemid.attr,
700 &dev_attr_serial.attr,
701 NULL,
702};
703
704static struct attribute_group sd_std_attr_group = {
705 .attrs = sd_std_attrs,
706};
707
David Brownella4dbd672009-06-24 10:06:31 -0700708static const struct attribute_group *sd_attr_groups[] = {
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100709 &sd_std_attr_group,
710 NULL,
711};
712
Michal Miroslaw71578a12010-08-10 18:01:40 -0700713struct device_type sd_type = {
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100714 .groups = sd_attr_groups,
715};
716
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100717/*
Michal Miroslaw71578a12010-08-10 18:01:40 -0700718 * Fetch CID from card.
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200719 */
Arindam Nathd6d50a12011-05-05 12:18:59 +0530720int mmc_sd_get_cid(struct mmc_host *host, u32 ocr, u32 *cid, u32 *rocr)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200721{
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200722 int err;
Aaron Lu55c46652012-07-04 13:31:48 +0800723 u32 max_current;
Johan Rudholm0797e5f2013-01-28 15:08:28 +0100724 int retries = 10;
Ulf Hansson69041152013-09-13 11:31:33 +0200725 u32 pocr = ocr;
Johan Rudholm0797e5f2013-01-28 15:08:28 +0100726
727try_again:
728 if (!retries) {
729 ocr &= ~SD_OCR_S18R;
730 pr_warning("%s: Skipping voltage switch\n",
731 mmc_hostname(host));
732 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200733
734 /*
735 * Since we're changing the OCR value, we seem to
736 * need to tell some cards to go back to the idle
737 * state. We wait 1ms to give cards time to
738 * respond.
739 */
740 mmc_go_idle(host);
741
742 /*
743 * If SD_SEND_IF_COND indicates an SD 2.0
744 * compliant card and we should set bit 30
745 * of the ocr to indicate that we can handle
746 * block-addressed SDHC cards.
747 */
748 err = mmc_send_if_cond(host, ocr);
Pierre Ossman17b04292007-07-22 22:18:46 +0200749 if (!err)
Arindam Nathf2119df2011-05-05 12:18:57 +0530750 ocr |= SD_OCR_CCS;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200751
Arindam Nathf2119df2011-05-05 12:18:57 +0530752 /*
753 * If the host supports one of UHS-I modes, request the card
Johan Rudholm0797e5f2013-01-28 15:08:28 +0100754 * to switch to 1.8V signaling level. If the card has failed
755 * repeatedly to switch however, skip this.
Arindam Nathf2119df2011-05-05 12:18:57 +0530756 */
Johan Rudholm0797e5f2013-01-28 15:08:28 +0100757 if (retries && mmc_host_uhs(host))
Arindam Nathf2119df2011-05-05 12:18:57 +0530758 ocr |= SD_OCR_S18R;
759
Aaron Lu55c46652012-07-04 13:31:48 +0800760 /*
761 * If the host can supply more than 150mA at current voltage,
762 * XPC should be set to 1.
763 */
764 max_current = sd_get_host_max_current(host);
765 if (max_current > 150)
Arindam Nathf2119df2011-05-05 12:18:57 +0530766 ocr |= SD_OCR_XPC;
767
Arindam Nathd6d50a12011-05-05 12:18:59 +0530768 err = mmc_send_app_op_cond(host, ocr, rocr);
Pierre Ossman17b04292007-07-22 22:18:46 +0200769 if (err)
Michal Miroslaw71578a12010-08-10 18:01:40 -0700770 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200771
Arindam Nathf2119df2011-05-05 12:18:57 +0530772 /*
773 * In case CCS and S18A in the response is set, start Signal Voltage
774 * Switch procedure. SPI mode doesn't support CMD11.
775 */
Arindam Nathd6d50a12011-05-05 12:18:59 +0530776 if (!mmc_host_is_spi(host) && rocr &&
777 ((*rocr & 0x41000000) == 0x41000000)) {
Ulf Hansson0f791fd2013-09-12 15:36:34 +0200778 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180,
Ulf Hansson69041152013-09-13 11:31:33 +0200779 pocr);
Johan Rudholm0797e5f2013-01-28 15:08:28 +0100780 if (err == -EAGAIN) {
781 retries--;
782 goto try_again;
783 } else if (err) {
784 retries = 0;
Arindam Nathf2119df2011-05-05 12:18:57 +0530785 goto try_again;
786 }
787 }
788
David Brownellaf517152007-08-08 09:11:32 -0700789 if (mmc_host_is_spi(host))
790 err = mmc_send_cid(host, cid);
791 else
792 err = mmc_all_send_cid(host, cid);
Michal Miroslaw71578a12010-08-10 18:01:40 -0700793
794 return err;
795}
796
797int mmc_sd_get_csd(struct mmc_host *host, struct mmc_card *card)
798{
799 int err;
800
801 /*
802 * Fetch CSD from card.
803 */
804 err = mmc_send_csd(card, card->raw_csd);
Pierre Ossman17b04292007-07-22 22:18:46 +0200805 if (err)
Michal Miroslaw71578a12010-08-10 18:01:40 -0700806 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200807
Michal Miroslaw71578a12010-08-10 18:01:40 -0700808 err = mmc_decode_csd(card);
809 if (err)
810 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200811
Michal Miroslaw71578a12010-08-10 18:01:40 -0700812 return 0;
813}
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200814
Michal Miroslaw71578a12010-08-10 18:01:40 -0700815int mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card,
816 bool reinit)
817{
818 int err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200819
Michal Miroslaw71578a12010-08-10 18:01:40 -0700820 if (!reinit) {
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200821 /*
822 * Fetch SCR from card.
823 */
824 err = mmc_app_send_scr(card, card->raw_scr);
Pierre Ossman17b04292007-07-22 22:18:46 +0200825 if (err)
Michal Miroslaw71578a12010-08-10 18:01:40 -0700826 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200827
Pierre Ossmanbd766312007-05-01 16:11:57 +0200828 err = mmc_decode_scr(card);
Michal Miroslaw71578a12010-08-10 18:01:40 -0700829 if (err)
830 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200831
832 /*
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700833 * Fetch and process SD Status register.
834 */
835 err = mmc_read_ssr(card);
836 if (err)
837 return err;
838
839 /* Erase init depends on CSD and SSR */
840 mmc_init_erase(card);
841
842 /*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200843 * Fetch switch information from card.
844 */
845 err = mmc_read_switch(card);
Pierre Ossman17b04292007-07-22 22:18:46 +0200846 if (err)
Michal Miroslaw71578a12010-08-10 18:01:40 -0700847 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200848 }
849
850 /*
Wolfgang Muees9d9f25c2009-04-07 14:48:16 +0100851 * For SPI, enable CRC as appropriate.
852 * This CRC enable is located AFTER the reading of the
853 * card registers because some SDHC cards are not able
854 * to provide valid CRCs for non-512-byte blocks.
855 */
856 if (mmc_host_is_spi(host)) {
857 err = mmc_spi_set_crc(host, use_spi_crc);
858 if (err)
Michal Miroslaw71578a12010-08-10 18:01:40 -0700859 return err;
Wolfgang Muees9d9f25c2009-04-07 14:48:16 +0100860 }
861
862 /*
Michal Miroslaw71578a12010-08-10 18:01:40 -0700863 * Check if read-only switch is active.
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200864 */
Michal Miroslaw71578a12010-08-10 18:01:40 -0700865 if (!reinit) {
866 int ro = -1;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200867
Sujit Reddy Thumma2c4967f742012-02-04 16:14:50 -0500868 if (host->ops->get_ro) {
869 mmc_host_clk_hold(card->host);
Michal Miroslaw71578a12010-08-10 18:01:40 -0700870 ro = host->ops->get_ro(host);
Sujit Reddy Thumma2c4967f742012-02-04 16:14:50 -0500871 mmc_host_clk_release(card->host);
872 }
Michal Miroslaw71578a12010-08-10 18:01:40 -0700873
874 if (ro < 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530875 pr_warning("%s: host does not "
Michal Miroslaw71578a12010-08-10 18:01:40 -0700876 "support reading read-only "
877 "switch. assuming write-enable.\n",
878 mmc_hostname(host));
879 } else if (ro > 0) {
880 mmc_card_set_readonly(card);
881 }
882 }
883
884 return 0;
885}
886
887unsigned mmc_sd_get_max_clock(struct mmc_card *card)
888{
889 unsigned max_dtr = (unsigned int)-1;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200890
891 if (mmc_card_highspeed(card)) {
892 if (max_dtr > card->sw_caps.hs_max_dtr)
893 max_dtr = card->sw_caps.hs_max_dtr;
894 } else if (max_dtr > card->csd.max_dtr) {
895 max_dtr = card->csd.max_dtr;
896 }
897
Michal Miroslaw71578a12010-08-10 18:01:40 -0700898 return max_dtr;
899}
900
901void mmc_sd_go_highspeed(struct mmc_card *card)
902{
903 mmc_card_set_highspeed(card);
904 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
905}
906
907/*
908 * Handle the detection and initialisation of a card.
909 *
910 * In the case of a resume, "oldcard" will contain the card
911 * we're trying to reinitialise.
912 */
913static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
914 struct mmc_card *oldcard)
915{
916 struct mmc_card *card;
917 int err;
918 u32 cid[4];
Arindam Nathd6d50a12011-05-05 12:18:59 +0530919 u32 rocr = 0;
Michal Miroslaw71578a12010-08-10 18:01:40 -0700920
921 BUG_ON(!host);
922 WARN_ON(!host->claimed);
923
Arindam Nathd6d50a12011-05-05 12:18:59 +0530924 err = mmc_sd_get_cid(host, ocr, cid, &rocr);
Michal Miroslaw71578a12010-08-10 18:01:40 -0700925 if (err)
926 return err;
927
928 if (oldcard) {
929 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0)
930 return -ENOENT;
931
932 card = oldcard;
933 } else {
934 /*
935 * Allocate card structure.
936 */
937 card = mmc_alloc_card(host, &sd_type);
938 if (IS_ERR(card))
939 return PTR_ERR(card);
940
Ulf Hansson69041152013-09-13 11:31:33 +0200941 card->ocr = ocr;
Michal Miroslaw71578a12010-08-10 18:01:40 -0700942 card->type = MMC_TYPE_SD;
943 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
944 }
945
946 /*
947 * For native busses: get card RCA and quit open drain mode.
948 */
949 if (!mmc_host_is_spi(host)) {
950 err = mmc_send_relative_addr(host, &card->rca);
951 if (err)
Wei WANG7fca9672013-07-17 14:21:10 +0800952 goto free_card;
Michal Miroslaw71578a12010-08-10 18:01:40 -0700953 }
954
955 if (!oldcard) {
956 err = mmc_sd_get_csd(host, card);
957 if (err)
Wei WANG7fca9672013-07-17 14:21:10 +0800958 goto free_card;
Michal Miroslaw71578a12010-08-10 18:01:40 -0700959
960 mmc_decode_cid(card);
961 }
962
963 /*
964 * Select card, as all following commands rely on that.
965 */
966 if (!mmc_host_is_spi(host)) {
967 err = mmc_select_card(card);
968 if (err)
Wei WANG7fca9672013-07-17 14:21:10 +0800969 goto free_card;
Michal Miroslaw71578a12010-08-10 18:01:40 -0700970 }
971
972 err = mmc_sd_setup_card(host, card, oldcard != NULL);
973 if (err)
974 goto free_card;
975
Arindam Nathd6d50a12011-05-05 12:18:59 +0530976 /* Initialization sequence for UHS-I cards */
977 if (rocr & SD_ROCR_S18A) {
978 err = mmc_sd_init_uhs_card(card);
Pierre Ossman17b04292007-07-22 22:18:46 +0200979 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200980 goto free_card;
Arindam Nath3a303512011-05-05 12:19:03 +0530981
982 /* Card is an ultra-high-speed card */
Philip Rakitya303c532011-11-14 19:14:38 -0800983 mmc_card_set_uhs(card);
Arindam Nathd6d50a12011-05-05 12:18:59 +0530984 } else {
985 /*
986 * Attempt to change to high-speed (if supported)
987 */
988 err = mmc_sd_switch_hs(card);
989 if (err > 0)
990 mmc_sd_go_highspeed(card);
991 else if (err)
992 goto free_card;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200993
Arindam Nathd6d50a12011-05-05 12:18:59 +0530994 /*
995 * Set bus speed.
996 */
997 mmc_set_clock(host, mmc_sd_get_max_clock(card));
998
999 /*
1000 * Switch to wider bus (if supported).
1001 */
1002 if ((host->caps & MMC_CAP_4_BIT_DATA) &&
1003 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
1004 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
1005 if (err)
1006 goto free_card;
1007
1008 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
1009 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001010 }
1011
Michal Miroslaw71578a12010-08-10 18:01:40 -07001012 host->card = card;
Pierre Ossman17b04292007-07-22 22:18:46 +02001013 return 0;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001014
1015free_card:
1016 if (!oldcard)
1017 mmc_remove_card(card);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001018
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001019 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001020}
1021
1022/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001023 * Host is being removed. Free up the current card.
1024 */
1025static void mmc_sd_remove(struct mmc_host *host)
1026{
1027 BUG_ON(!host);
1028 BUG_ON(!host->card);
1029
1030 mmc_remove_card(host->card);
1031 host->card = NULL;
1032}
1033
1034/*
Adrian Hunterd3049502011-11-28 16:22:00 +02001035 * Card detection - card is alive.
1036 */
1037static int mmc_sd_alive(struct mmc_host *host)
1038{
1039 return mmc_send_status(host->card, NULL);
1040}
1041
1042/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001043 * Card detection callback from host.
1044 */
1045static void mmc_sd_detect(struct mmc_host *host)
1046{
1047 int err;
1048
1049 BUG_ON(!host);
1050 BUG_ON(!host->card);
1051
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001052 mmc_get_card(host->card);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001053
1054 /*
1055 * Just check if our card has been removed.
1056 */
Adrian Hunterd3049502011-11-28 16:22:00 +02001057 err = _mmc_detect_card_removed(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001058
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001059 mmc_put_card(host->card);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001060
Pierre Ossman17b04292007-07-22 22:18:46 +02001061 if (err) {
Pierre Ossman4101c162007-05-19 13:39:01 +02001062 mmc_sd_remove(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001063
1064 mmc_claim_host(host);
1065 mmc_detach_bus(host);
Ulf Hansson7f7e4122011-09-21 14:08:13 -04001066 mmc_power_off(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001067 mmc_release_host(host);
1068 }
1069}
1070
Ulf Hansson0cb403a2013-10-10 14:20:05 +02001071static int _mmc_sd_suspend(struct mmc_host *host)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001072{
Jaehoon Chung85e727e2012-05-31 20:31:47 +09001073 int err = 0;
1074
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001075 BUG_ON(!host);
1076 BUG_ON(!host->card);
1077
1078 mmc_claim_host(host);
Ulf Hansson9ec775f2013-10-02 17:37:09 +02001079
1080 if (mmc_card_suspended(host->card))
1081 goto out;
1082
David Brownellaf517152007-08-08 09:11:32 -07001083 if (!mmc_host_is_spi(host))
Jaehoon Chung85e727e2012-05-31 20:31:47 +09001084 err = mmc_deselect_cards(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001085 host->card->state &= ~MMC_STATE_HIGHSPEED;
Ulf Hansson9ec775f2013-10-02 17:37:09 +02001086 if (!err) {
Ulf Hansson74590262013-06-10 17:03:38 +02001087 mmc_power_off(host);
Ulf Hansson9ec775f2013-10-02 17:37:09 +02001088 mmc_card_set_suspended(host->card);
1089 }
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001090
Ulf Hansson9ec775f2013-10-02 17:37:09 +02001091out:
1092 mmc_release_host(host);
Jaehoon Chung85e727e2012-05-31 20:31:47 +09001093 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001094}
1095
1096/*
Ulf Hansson0cb403a2013-10-10 14:20:05 +02001097 * Callback for suspend
1098 */
1099static int mmc_sd_suspend(struct mmc_host *host)
1100{
1101 int err;
1102
1103 err = _mmc_sd_suspend(host);
1104 if (!err) {
1105 pm_runtime_disable(&host->card->dev);
1106 pm_runtime_set_suspended(&host->card->dev);
1107 }
1108
1109 return err;
1110}
1111
1112/*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001113 * This function tries to determine if the same card is still present
1114 * and, if so, restore all state to it.
1115 */
Ulf Hansson0cb403a2013-10-10 14:20:05 +02001116static int _mmc_sd_resume(struct mmc_host *host)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001117{
Ulf Hansson9ec775f2013-10-02 17:37:09 +02001118 int err = 0;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001119
1120 BUG_ON(!host);
1121 BUG_ON(!host->card);
1122
1123 mmc_claim_host(host);
Ulf Hansson9ec775f2013-10-02 17:37:09 +02001124
1125 if (!mmc_card_suspended(host->card))
1126 goto out;
1127
Ulf Hansson69041152013-09-13 11:31:33 +02001128 mmc_power_up(host, host->card->ocr);
Ulf Hansson69041152013-09-13 11:31:33 +02001129 err = mmc_sd_init_card(host, host->card->ocr, host->card);
Ulf Hansson9ec775f2013-10-02 17:37:09 +02001130 mmc_card_clr_suspended(host->card);
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001131
Ulf Hansson9ec775f2013-10-02 17:37:09 +02001132out:
1133 mmc_release_host(host);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001134 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001135}
1136
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02001137/*
Ulf Hansson0cb403a2013-10-10 14:20:05 +02001138 * Callback for resume
1139 */
1140static int mmc_sd_resume(struct mmc_host *host)
1141{
Ulf Hansson4d223782013-10-10 17:22:23 +02001142 int err = 0;
Ulf Hansson0cb403a2013-10-10 14:20:05 +02001143
Ulf Hansson4d223782013-10-10 17:22:23 +02001144 if (!(host->caps & MMC_CAP_RUNTIME_RESUME)) {
1145 err = _mmc_sd_resume(host);
1146 pm_runtime_set_active(&host->card->dev);
1147 pm_runtime_mark_last_busy(&host->card->dev);
1148 }
Ulf Hansson0cb403a2013-10-10 14:20:05 +02001149 pm_runtime_enable(&host->card->dev);
1150
1151 return err;
1152}
1153
1154/*
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02001155 * Callback for runtime_suspend.
1156 */
1157static int mmc_sd_runtime_suspend(struct mmc_host *host)
1158{
1159 int err;
1160
1161 if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
1162 return 0;
1163
Ulf Hansson0cb403a2013-10-10 14:20:05 +02001164 err = _mmc_sd_suspend(host);
Ulf Hansson0cc81a82013-10-03 11:24:44 +02001165 if (err)
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02001166 pr_err("%s: error %d doing aggessive suspend\n",
1167 mmc_hostname(host), err);
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02001168
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02001169 return err;
1170}
1171
1172/*
1173 * Callback for runtime_resume.
1174 */
1175static int mmc_sd_runtime_resume(struct mmc_host *host)
1176{
1177 int err;
1178
Ulf Hansson4d223782013-10-10 17:22:23 +02001179 if (!(host->caps & (MMC_CAP_AGGRESSIVE_PM | MMC_CAP_RUNTIME_RESUME)))
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02001180 return 0;
1181
Ulf Hansson0cb403a2013-10-10 14:20:05 +02001182 err = _mmc_sd_resume(host);
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02001183 if (err)
1184 pr_err("%s: error %d doing aggessive resume\n",
1185 mmc_hostname(host), err);
1186
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02001187 return 0;
1188}
1189
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001190static int mmc_sd_power_restore(struct mmc_host *host)
Adrian Huntereae1aee2009-09-22 16:44:33 -07001191{
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001192 int ret;
1193
Adrian Huntereae1aee2009-09-22 16:44:33 -07001194 host->card->state &= ~MMC_STATE_HIGHSPEED;
1195 mmc_claim_host(host);
Ulf Hansson69041152013-09-13 11:31:33 +02001196 ret = mmc_sd_init_card(host, host->card->ocr, host->card);
Adrian Huntereae1aee2009-09-22 16:44:33 -07001197 mmc_release_host(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001198
1199 return ret;
Adrian Huntereae1aee2009-09-22 16:44:33 -07001200}
1201
Adrian Hunter9feae242009-09-22 16:44:32 -07001202static const struct mmc_bus_ops mmc_sd_ops = {
1203 .remove = mmc_sd_remove,
1204 .detect = mmc_sd_detect,
1205 .suspend = NULL,
1206 .resume = NULL,
Adrian Huntereae1aee2009-09-22 16:44:33 -07001207 .power_restore = mmc_sd_power_restore,
Adrian Hunterd3049502011-11-28 16:22:00 +02001208 .alive = mmc_sd_alive,
Ulf Hansson5992e782013-06-10 17:03:42 +02001209 .shutdown = mmc_sd_suspend,
Adrian Hunter9feae242009-09-22 16:44:32 -07001210};
1211
1212static const struct mmc_bus_ops mmc_sd_ops_unsafe = {
1213 .remove = mmc_sd_remove,
1214 .detect = mmc_sd_detect,
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02001215 .runtime_suspend = mmc_sd_runtime_suspend,
1216 .runtime_resume = mmc_sd_runtime_resume,
Adrian Hunter9feae242009-09-22 16:44:32 -07001217 .suspend = mmc_sd_suspend,
1218 .resume = mmc_sd_resume,
Adrian Huntereae1aee2009-09-22 16:44:33 -07001219 .power_restore = mmc_sd_power_restore,
Adrian Hunterd3049502011-11-28 16:22:00 +02001220 .alive = mmc_sd_alive,
Ulf Hansson5992e782013-06-10 17:03:42 +02001221 .shutdown = mmc_sd_suspend,
Adrian Hunter9feae242009-09-22 16:44:32 -07001222};
1223
1224static void mmc_sd_attach_bus_ops(struct mmc_host *host)
1225{
1226 const struct mmc_bus_ops *bus_ops;
1227
Matt Fleming71d7d3d2010-09-27 09:42:19 +01001228 if (!mmc_card_is_removable(host))
Adrian Hunter9feae242009-09-22 16:44:32 -07001229 bus_ops = &mmc_sd_ops_unsafe;
1230 else
1231 bus_ops = &mmc_sd_ops;
1232 mmc_attach_bus(host, bus_ops);
1233}
1234
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001235/*
1236 * Starting point for SD card init.
1237 */
Andy Ross807e8e42011-01-03 10:36:56 -08001238int mmc_attach_sd(struct mmc_host *host)
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001239{
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001240 int err;
Ulf Hansson69041152013-09-13 11:31:33 +02001241 u32 ocr, rocr;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001242
1243 BUG_ON(!host);
Pierre Ossmand84075c82007-08-09 13:23:56 +02001244 WARN_ON(!host->claimed);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001245
Andy Ross807e8e42011-01-03 10:36:56 -08001246 err = mmc_send_app_op_cond(host, 0, &ocr);
1247 if (err)
1248 return err;
1249
Adrian Hunter9feae242009-09-22 16:44:32 -07001250 mmc_sd_attach_bus_ops(host);
Takashi Iwai8f230f42010-12-08 10:04:30 +01001251 if (host->ocr_avail_sd)
1252 host->ocr_avail = host->ocr_avail_sd;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001253
Philip Langdale55556da2007-03-16 19:39:00 -07001254 /*
David Brownellaf517152007-08-08 09:11:32 -07001255 * We need to get OCR a different way for SPI.
1256 */
1257 if (mmc_host_is_spi(host)) {
1258 mmc_go_idle(host);
1259
1260 err = mmc_spi_read_ocr(host, 0, &ocr);
1261 if (err)
1262 goto err;
1263 }
1264
Ulf Hansson69041152013-09-13 11:31:33 +02001265 rocr = mmc_select_voltage(host, ocr);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001266
1267 /*
1268 * Can we support the voltage(s) of the card(s)?
1269 */
Ulf Hansson69041152013-09-13 11:31:33 +02001270 if (!rocr) {
Pierre Ossman109b5be2007-07-23 00:12:10 +02001271 err = -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001272 goto err;
Pierre Ossman109b5be2007-07-23 00:12:10 +02001273 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001274
1275 /*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001276 * Detect and init the card.
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001277 */
Ulf Hansson69041152013-09-13 11:31:33 +02001278 err = mmc_sd_init_card(host, rocr, NULL);
Pierre Ossman17b04292007-07-22 22:18:46 +02001279 if (err)
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001280 goto err;
1281
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001282 mmc_release_host(host);
Pierre Ossman4101c162007-05-19 13:39:01 +02001283 err = mmc_add_card(host->card);
Andy Ross807e8e42011-01-03 10:36:56 -08001284 mmc_claim_host(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001285 if (err)
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001286 goto remove_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001287
1288 return 0;
1289
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001290remove_card:
Andy Ross807e8e42011-01-03 10:36:56 -08001291 mmc_release_host(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001292 mmc_remove_card(host->card);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001293 host->card = NULL;
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001294 mmc_claim_host(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001295err:
1296 mmc_detach_bus(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001297
Girish K Sa3c76eb2011-10-11 11:44:09 +05301298 pr_err("%s: error %d whilst initialising SD card\n",
Pierre Ossman109b5be2007-07-23 00:12:10 +02001299 mmc_hostname(host), err);
1300
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001301 return err;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001302}
1303