blob: deb90c2ff6b423e63352283c8dfdbb935bfb4e27 [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>
Wolfram Sang9288cac2013-11-26 02:16:25 +010014#include <linux/sizes.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Paul Gortmaker0205a902011-07-15 12:01:27 -040016#include <linux/stat.h>
Ulf Hansson0cb403a2013-10-10 14:20:05 +020017#include <linux/pm_runtime.h>
Pierre Ossman7ea239d2006-12-31 00:11:32 +010018
19#include <linux/mmc/host.h>
20#include <linux/mmc/card.h>
21#include <linux/mmc/mmc.h>
Pierre Ossman3373c0a2007-05-31 22:25:11 +020022#include <linux/mmc/sd.h>
Pierre Ossman7ea239d2006-12-31 00:11:32 +010023
24#include "core.h"
Pierre Ossman4101c162007-05-19 13:39:01 +020025#include "bus.h"
Pierre Ossman7ea239d2006-12-31 00:11:32 +010026#include "mmc_ops.h"
Mark Brownce101492011-02-10 10:58:37 +000027#include "sd.h"
Pierre Ossman7ea239d2006-12-31 00:11:32 +010028#include "sd_ops.h"
29
Pierre Ossman7ea239d2006-12-31 00:11:32 +010030static const unsigned int tran_exp[] = {
31 10000, 100000, 1000000, 10000000,
32 0, 0, 0, 0
33};
34
35static const unsigned char tran_mant[] = {
36 0, 10, 12, 13, 15, 20, 25, 30,
37 35, 40, 45, 50, 55, 60, 70, 80,
38};
39
40static const unsigned int tacc_exp[] = {
41 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
42};
43
44static const unsigned int tacc_mant[] = {
45 0, 10, 12, 13, 15, 20, 25, 30,
46 35, 40, 45, 50, 55, 60, 70, 80,
47};
48
Wolfram Sang9288cac2013-11-26 02:16:25 +010049static const unsigned int sd_au_size[] = {
50 0, SZ_16K / 512, SZ_32K / 512, SZ_64K / 512,
51 SZ_128K / 512, SZ_256K / 512, SZ_512K / 512, SZ_1M / 512,
52 SZ_2M / 512, SZ_4M / 512, SZ_8M / 512, (SZ_8M + SZ_4M) / 512,
53 SZ_16M / 512, (SZ_16M + SZ_8M) / 512, SZ_32M / 512, SZ_64M / 512,
54};
55
Pierre Ossman7ea239d2006-12-31 00:11:32 +010056#define UNSTUFF_BITS(resp,start,size) \
57 ({ \
58 const int __size = size; \
59 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
60 const int __off = 3 - ((start) / 32); \
61 const int __shft = (start) & 31; \
62 u32 __res; \
63 \
64 __res = resp[__off] >> __shft; \
65 if (__size + __shft > 32) \
66 __res |= resp[__off-1] << ((32 - __shft) % 32); \
67 __res & __mask; \
68 })
69
70/*
71 * Given the decoded CSD structure, decode the raw CID to our CID structure.
72 */
Michal Miroslaw71578a12010-08-10 18:01:40 -070073void mmc_decode_cid(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +010074{
75 u32 *resp = card->raw_cid;
76
Pierre Ossman7ea239d2006-12-31 00:11:32 +010077 /*
78 * SD doesn't currently have a version field so we will
79 * have to assume we can parse this.
80 */
81 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
82 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
83 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
84 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
85 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
86 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
87 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
88 card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
89 card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
90 card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
91 card->cid.year = UNSTUFF_BITS(resp, 12, 8);
92 card->cid.month = UNSTUFF_BITS(resp, 8, 4);
93
94 card->cid.year += 2000; /* SD cards year offset */
95}
96
97/*
98 * Given a 128-bit response, decode to our card CSD structure.
99 */
Pierre Ossmanbd766312007-05-01 16:11:57 +0200100static int mmc_decode_csd(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100101{
102 struct mmc_csd *csd = &card->csd;
103 unsigned int e, m, csd_struct;
104 u32 *resp = card->raw_csd;
105
106 csd_struct = UNSTUFF_BITS(resp, 126, 2);
107
108 switch (csd_struct) {
109 case 0:
110 m = UNSTUFF_BITS(resp, 115, 4);
111 e = UNSTUFF_BITS(resp, 112, 3);
112 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
113 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
114
115 m = UNSTUFF_BITS(resp, 99, 4);
116 e = UNSTUFF_BITS(resp, 96, 3);
117 csd->max_dtr = tran_exp[e] * tran_mant[m];
118 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
119
120 e = UNSTUFF_BITS(resp, 47, 3);
121 m = UNSTUFF_BITS(resp, 62, 12);
122 csd->capacity = (1 + m) << (e + 2);
123
124 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
125 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
126 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
127 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
Sascha Hauer3d705d12014-08-19 10:45:51 +0200128 csd->dsr_imp = UNSTUFF_BITS(resp, 76, 1);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100129 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
130 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
131 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700132
133 if (UNSTUFF_BITS(resp, 46, 1)) {
134 csd->erase_size = 1;
135 } else if (csd->write_blkbits >= 9) {
136 csd->erase_size = UNSTUFF_BITS(resp, 39, 7) + 1;
137 csd->erase_size <<= csd->write_blkbits - 9;
138 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100139 break;
140 case 1:
141 /*
Arindam Nath3a303512011-05-05 12:19:03 +0530142 * This is a block-addressed SDHC or SDXC card. Most
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100143 * interesting fields are unused and have fixed
144 * values. To avoid getting tripped by buggy cards,
145 * we assume those fixed values ourselves.
146 */
147 mmc_card_set_blockaddr(card);
148
149 csd->tacc_ns = 0; /* Unused */
150 csd->tacc_clks = 0; /* Unused */
151
152 m = UNSTUFF_BITS(resp, 99, 4);
153 e = UNSTUFF_BITS(resp, 96, 3);
154 csd->max_dtr = tran_exp[e] * tran_mant[m];
155 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
Arindam Nath3a303512011-05-05 12:19:03 +0530156 csd->c_size = UNSTUFF_BITS(resp, 48, 22);
157
158 /* SDXC cards have a minimum C_SIZE of 0x00FFFF */
159 if (csd->c_size >= 0xFFFF)
160 mmc_card_set_ext_capacity(card);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100161
162 m = UNSTUFF_BITS(resp, 48, 22);
163 csd->capacity = (1 + m) << 10;
164
165 csd->read_blkbits = 9;
166 csd->read_partial = 0;
167 csd->write_misalign = 0;
168 csd->read_misalign = 0;
169 csd->r2w_factor = 4; /* Unused */
170 csd->write_blkbits = 9;
171 csd->write_partial = 0;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700172 csd->erase_size = 1;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100173 break;
174 default:
Girish K Sa3c76eb2011-10-11 11:44:09 +0530175 pr_err("%s: unrecognised CSD structure version %d\n",
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100176 mmc_hostname(card->host), csd_struct);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200177 return -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100178 }
Pierre Ossmanbd766312007-05-01 16:11:57 +0200179
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700180 card->erase_size = csd->erase_size;
181
Pierre Ossmanbd766312007-05-01 16:11:57 +0200182 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100183}
184
185/*
186 * Given a 64-bit response, decode to our card SCR structure.
187 */
Pierre Ossmanbd766312007-05-01 16:11:57 +0200188static int mmc_decode_scr(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100189{
190 struct sd_scr *scr = &card->scr;
191 unsigned int scr_struct;
192 u32 resp[4];
193
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100194 resp[3] = card->raw_scr[1];
195 resp[2] = card->raw_scr[0];
196
197 scr_struct = UNSTUFF_BITS(resp, 60, 4);
198 if (scr_struct != 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530199 pr_err("%s: unrecognised SCR structure version %d\n",
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100200 mmc_hostname(card->host), scr_struct);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200201 return -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100202 }
203
204 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
205 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
Arindam Nath013909c2011-05-05 12:18:58 +0530206 if (scr->sda_vsn == SCR_SPEC_VER_2)
207 /* Check if Physical Layer Spec v3.0 is supported */
208 scr->sda_spec3 = UNSTUFF_BITS(resp, 47, 1);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200209
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700210 if (UNSTUFF_BITS(resp, 55, 1))
211 card->erased_byte = 0xFF;
212 else
213 card->erased_byte = 0x0;
214
Andrei Warkentinf0d89972011-05-23 15:06:38 -0500215 if (scr->sda_spec3)
216 scr->cmds = UNSTUFF_BITS(resp, 32, 2);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200217 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100218}
219
220/*
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700221 * Fetch and process SD Status register.
222 */
223static int mmc_read_ssr(struct mmc_card *card)
224{
225 unsigned int au, es, et, eo;
Uri Yanai5275a652016-08-14 11:46:36 +0300226 int i;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700227
228 if (!(card->csd.cmdclass & CCC_APP_SPEC)) {
Joe Perches66061102014-09-12 14:56:56 -0700229 pr_warn("%s: card lacks mandatory SD Status function\n",
230 mmc_hostname(card->host));
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700231 return 0;
232 }
233
Uri Yanai5275a652016-08-14 11:46:36 +0300234 if (mmc_app_sd_status(card, card->raw_ssr)) {
Joe Perches66061102014-09-12 14:56:56 -0700235 pr_warn("%s: problem reading SD Status register\n",
236 mmc_hostname(card->host));
Uri Yanai5275a652016-08-14 11:46:36 +0300237 return 0;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700238 }
239
240 for (i = 0; i < 16; i++)
Uri Yanai5275a652016-08-14 11:46:36 +0300241 card->raw_ssr[i] = be32_to_cpu(card->raw_ssr[i]);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700242
243 /*
244 * UNSTUFF_BITS only works with four u32s so we have to offset the
245 * bitfield positions accordingly.
246 */
Uri Yanai5275a652016-08-14 11:46:36 +0300247 au = UNSTUFF_BITS(card->raw_ssr, 428 - 384, 4);
Wolfram Sang9288cac2013-11-26 02:16:25 +0100248 if (au) {
249 if (au <= 9 || card->scr.sda_spec3) {
250 card->ssr.au = sd_au_size[au];
Uri Yanai5275a652016-08-14 11:46:36 +0300251 es = UNSTUFF_BITS(card->raw_ssr, 408 - 384, 16);
252 et = UNSTUFF_BITS(card->raw_ssr, 402 - 384, 6);
Wolfram Sang9288cac2013-11-26 02:16:25 +0100253 if (es && et) {
Uri Yanai5275a652016-08-14 11:46:36 +0300254 eo = UNSTUFF_BITS(card->raw_ssr, 400 - 384, 2);
Wolfram Sang9288cac2013-11-26 02:16:25 +0100255 card->ssr.erase_timeout = (et * 1000) / es;
256 card->ssr.erase_offset = eo * 1000;
257 }
258 } else {
Joe Perches66061102014-09-12 14:56:56 -0700259 pr_warn("%s: SD Status: Invalid Allocation Unit size\n",
260 mmc_hostname(card->host));
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700261 }
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700262 }
Uri Yanai5275a652016-08-14 11:46:36 +0300263
264 return 0;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700265}
266
267/*
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200268 * Fetches and decodes switch information
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100269 */
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200270static int mmc_read_switch(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100271{
272 int err;
273 u8 *status;
274
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200275 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
Pierre Ossman17b04292007-07-22 22:18:46 +0200276 return 0;
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200277
278 if (!(card->csd.cmdclass & CCC_SWITCH)) {
Joe Perches66061102014-09-12 14:56:56 -0700279 pr_warn("%s: card lacks mandatory switch function, performance might suffer\n",
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200280 mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200281 return 0;
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200282 }
283
Pierre Ossman17b04292007-07-22 22:18:46 +0200284 err = -EIO;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100285
286 status = kmalloc(64, GFP_KERNEL);
287 if (!status) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530288 pr_err("%s: could not allocate a buffer for "
Arindam Nath013909c2011-05-05 12:18:58 +0530289 "switch capabilities.\n",
290 mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200291 return -ENOMEM;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100292 }
293
Aaron Lua39ca6ae2012-07-03 14:16:11 +0800294 /*
295 * Find out the card's support bits with a mode 0 operation.
296 * The argument does not matter, as the support bits do not
297 * change with the arguments.
298 */
299 err = mmc_sd_switch(card, 0, 0, 0, status);
Pierre Ossman17b04292007-07-22 22:18:46 +0200300 if (err) {
Arindam Nath013909c2011-05-05 12:18:58 +0530301 /*
302 * If the host or the card can't do the switch,
303 * fail more gracefully.
304 */
305 if (err != -EINVAL && err != -ENOSYS && err != -EFAULT)
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200306 goto out;
307
Joe Perches66061102014-09-12 14:56:56 -0700308 pr_warn("%s: problem reading Bus Speed modes\n",
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200309 mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200310 err = 0;
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200311
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100312 goto out;
313 }
314
Qiang Liufffe5d52011-11-08 08:43:08 -0500315 if (status[13] & SD_MODE_HIGH_SPEED)
316 card->sw_caps.hs_max_dtr = HIGH_SPEED_MAX_DTR;
Subhash Jadavanif2815f62011-08-10 11:16:01 +0530317
Arindam Nath013909c2011-05-05 12:18:58 +0530318 if (card->scr.sda_spec3) {
319 card->sw_caps.sd3_bus_mode = status[13];
Aaron Lua39ca6ae2012-07-03 14:16:11 +0800320 /* Driver Strengths supported by the card */
Arindam Nath013909c2011-05-05 12:18:58 +0530321 card->sw_caps.sd3_drv_type = status[9];
Russell Kingd9812782016-01-02 10:06:29 +0000322 card->sw_caps.sd3_curr_limit = status[7] | status[6] << 8;
Arindam Nath013909c2011-05-05 12:18:58 +0530323 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100324
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200325out:
326 kfree(status);
327
328 return err;
329}
330
331/*
332 * Test if the card supports high-speed mode and, if so, switch to it.
333 */
Michal Miroslaw71578a12010-08-10 18:01:40 -0700334int mmc_sd_switch_hs(struct mmc_card *card)
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200335{
336 int err;
337 u8 *status;
338
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200339 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
Pierre Ossman17b04292007-07-22 22:18:46 +0200340 return 0;
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200341
342 if (!(card->csd.cmdclass & CCC_SWITCH))
Pierre Ossman17b04292007-07-22 22:18:46 +0200343 return 0;
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200344
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200345 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
Pierre Ossman17b04292007-07-22 22:18:46 +0200346 return 0;
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200347
348 if (card->sw_caps.hs_max_dtr == 0)
Pierre Ossman17b04292007-07-22 22:18:46 +0200349 return 0;
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200350
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200351 status = kmalloc(64, GFP_KERNEL);
352 if (!status) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530353 pr_err("%s: could not allocate a buffer for "
Pierre Ossmanfacba912007-07-24 21:53:43 +0200354 "switch capabilities.\n", mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200355 return -ENOMEM;
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200356 }
357
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100358 err = mmc_sd_switch(card, 1, 0, 1, status);
Pierre Ossman17b04292007-07-22 22:18:46 +0200359 if (err)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100360 goto out;
361
362 if ((status[16] & 0xF) != 1) {
Joe Perches66061102014-09-12 14:56:56 -0700363 pr_warn("%s: Problem switching card into high-speed mode!\n",
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100364 mmc_hostname(card->host));
Michal Miroslaw71578a12010-08-10 18:01:40 -0700365 err = 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100366 } else {
Michal Miroslaw71578a12010-08-10 18:01:40 -0700367 err = 1;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100368 }
369
370out:
371 kfree(status);
372
373 return err;
374}
375
Arindam Nathd6d50a12011-05-05 12:18:59 +0530376static int sd_select_driver_type(struct mmc_card *card, u8 *status)
377{
Adrian Hunterfa021ce2015-02-06 14:12:53 +0200378 int card_drv_type, drive_strength, drv_type;
Arindam Nathd6d50a12011-05-05 12:18:59 +0530379 int err;
380
Adrian Hunter3853a042015-02-06 14:12:56 +0200381 card->drive_strength = 0;
382
Adrian Hunterfa021ce2015-02-06 14:12:53 +0200383 card_drv_type = card->sw_caps.sd3_drv_type | SD_DRIVER_TYPE_B;
Philip Rakityca8e99b2011-07-06 08:51:32 -0700384
Adrian Huntere23350b2015-02-06 14:12:55 +0200385 drive_strength = mmc_select_drive_strength(card,
386 card->sw_caps.uhs_max_dtr,
387 card_drv_type, &drv_type);
Philip Rakityca8e99b2011-07-06 08:51:32 -0700388
Adrian Hunterb4f30a12015-02-06 14:12:52 +0200389 if (drive_strength) {
390 err = mmc_sd_switch(card, 1, 2, drive_strength, status);
391 if (err)
392 return err;
393 if ((status[15] & 0xF) != drive_strength) {
394 pr_warn("%s: Problem setting drive strength!\n",
395 mmc_hostname(card->host));
396 return 0;
397 }
Adrian Hunter3853a042015-02-06 14:12:56 +0200398 card->drive_strength = drive_strength;
Arindam Nathd6d50a12011-05-05 12:18:59 +0530399 }
400
Adrian Hunterb4f30a12015-02-06 14:12:52 +0200401 if (drv_type)
402 mmc_set_driver_type(card->host, drv_type);
Arindam Nathd6d50a12011-05-05 12:18:59 +0530403
404 return 0;
405}
406
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530407static void sd_update_bus_speed_mode(struct mmc_card *card)
Arindam Nath49c468f2011-05-05 12:19:01 +0530408{
Arindam Nath49c468f2011-05-05 12:19:01 +0530409 /*
410 * If the host doesn't support any of the UHS-I modes, fallback on
411 * default speed.
412 */
Johan Rudholm3f8a7fa2013-01-28 15:08:24 +0100413 if (!mmc_host_uhs(card->host)) {
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530414 card->sd_bus_speed = 0;
415 return;
416 }
Arindam Nath49c468f2011-05-05 12:19:01 +0530417
418 if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
419 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530420 card->sd_bus_speed = UHS_SDR104_BUS_SPEED;
Arindam Nath49c468f2011-05-05 12:19:01 +0530421 } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
422 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530423 card->sd_bus_speed = UHS_DDR50_BUS_SPEED;
Arindam Nath49c468f2011-05-05 12:19:01 +0530424 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
425 MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
426 SD_MODE_UHS_SDR50)) {
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530427 card->sd_bus_speed = UHS_SDR50_BUS_SPEED;
Arindam Nath49c468f2011-05-05 12:19:01 +0530428 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
429 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
430 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530431 card->sd_bus_speed = UHS_SDR25_BUS_SPEED;
Arindam Nath49c468f2011-05-05 12:19:01 +0530432 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
433 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
434 MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
435 SD_MODE_UHS_SDR12)) {
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530436 card->sd_bus_speed = UHS_SDR12_BUS_SPEED;
437 }
438}
439
440static int sd_set_bus_speed_mode(struct mmc_card *card, u8 *status)
441{
442 int err;
443 unsigned int timing = 0;
444
445 switch (card->sd_bus_speed) {
446 case UHS_SDR104_BUS_SPEED:
447 timing = MMC_TIMING_UHS_SDR104;
448 card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
449 break;
450 case UHS_DDR50_BUS_SPEED:
451 timing = MMC_TIMING_UHS_DDR50;
452 card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
453 break;
454 case UHS_SDR50_BUS_SPEED:
455 timing = MMC_TIMING_UHS_SDR50;
456 card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
457 break;
458 case UHS_SDR25_BUS_SPEED:
459 timing = MMC_TIMING_UHS_SDR25;
460 card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
461 break;
462 case UHS_SDR12_BUS_SPEED:
463 timing = MMC_TIMING_UHS_SDR12;
464 card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
465 break;
466 default:
467 return 0;
Arindam Nath49c468f2011-05-05 12:19:01 +0530468 }
469
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530470 err = mmc_sd_switch(card, 1, 0, card->sd_bus_speed, status);
Arindam Nath49c468f2011-05-05 12:19:01 +0530471 if (err)
472 return err;
473
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530474 if ((status[16] & 0xF) != card->sd_bus_speed)
Joe Perches66061102014-09-12 14:56:56 -0700475 pr_warn("%s: Problem setting bus speed mode!\n",
Arindam Nath49c468f2011-05-05 12:19:01 +0530476 mmc_hostname(card->host));
477 else {
478 mmc_set_timing(card->host, timing);
479 mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
480 }
481
482 return 0;
483}
484
Aaron Lu55c46652012-07-04 13:31:48 +0800485/* Get host's max current setting at its current voltage */
486static u32 sd_get_host_max_current(struct mmc_host *host)
487{
488 u32 voltage, max_current;
489
490 voltage = 1 << host->ios.vdd;
491 switch (voltage) {
492 case MMC_VDD_165_195:
493 max_current = host->max_current_180;
494 break;
495 case MMC_VDD_29_30:
496 case MMC_VDD_30_31:
497 max_current = host->max_current_300;
498 break;
499 case MMC_VDD_32_33:
500 case MMC_VDD_33_34:
501 max_current = host->max_current_330;
502 break;
503 default:
504 max_current = 0;
505 }
506
507 return max_current;
508}
509
Arindam Nath5371c922011-05-05 12:19:02 +0530510static int sd_set_current_limit(struct mmc_card *card, u8 *status)
511{
Philip Rakity0aa67702012-05-27 18:36:33 -0700512 int current_limit = SD_SET_CURRENT_NO_CHANGE;
Arindam Nath5371c922011-05-05 12:19:02 +0530513 int err;
Aaron Lu55c46652012-07-04 13:31:48 +0800514 u32 max_current;
Arindam Nath5371c922011-05-05 12:19:02 +0530515
516 /*
517 * Current limit switch is only defined for SDR50, SDR104, and DDR50
Philip Rakity0aa67702012-05-27 18:36:33 -0700518 * bus speed modes. For other bus speed modes, we do not change the
519 * current limit.
Aaron Lu55c46652012-07-04 13:31:48 +0800520 */
521 if ((card->sd_bus_speed != UHS_SDR50_BUS_SPEED) &&
522 (card->sd_bus_speed != UHS_SDR104_BUS_SPEED) &&
523 (card->sd_bus_speed != UHS_DDR50_BUS_SPEED))
524 return 0;
525
526 /*
527 * Host has different current capabilities when operating at
528 * different voltages, so find out its max current first.
529 */
530 max_current = sd_get_host_max_current(card->host);
531
532 /*
Aaron Lua39ca6ae2012-07-03 14:16:11 +0800533 * We only check host's capability here, if we set a limit that is
534 * higher than the card's maximum current, the card will be using its
535 * maximum current, e.g. if the card's maximum current is 300ma, and
536 * when we set current limit to 200ma, the card will draw 200ma, and
537 * when we set current limit to 400/600/800ma, the card will draw its
538 * maximum 300ma from the host.
Russell Kingd9812782016-01-02 10:06:29 +0000539 *
540 * The above is incorrect: if we try to set a current limit that is
541 * not supported by the card, the card can rightfully error out the
542 * attempt, and remain at the default current limit. This results
543 * in a 300mA card being limited to 200mA even though the host
544 * supports 800mA. Failures seen with SanDisk 8GB UHS cards with
545 * an iMX6 host. --rmk
Arindam Nath5371c922011-05-05 12:19:02 +0530546 */
Russell Kingd9812782016-01-02 10:06:29 +0000547 if (max_current >= 800 &&
548 card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_800)
Aaron Lu55c46652012-07-04 13:31:48 +0800549 current_limit = SD_SET_CURRENT_LIMIT_800;
Russell Kingd9812782016-01-02 10:06:29 +0000550 else if (max_current >= 600 &&
551 card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_600)
Aaron Lu55c46652012-07-04 13:31:48 +0800552 current_limit = SD_SET_CURRENT_LIMIT_600;
Russell Kingd9812782016-01-02 10:06:29 +0000553 else if (max_current >= 400 &&
554 card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_400)
Aaron Lu55c46652012-07-04 13:31:48 +0800555 current_limit = SD_SET_CURRENT_LIMIT_400;
Russell Kingd9812782016-01-02 10:06:29 +0000556 else if (max_current >= 200 &&
557 card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_200)
Aaron Lu55c46652012-07-04 13:31:48 +0800558 current_limit = SD_SET_CURRENT_LIMIT_200;
Arindam Nath5371c922011-05-05 12:19:02 +0530559
Philip Rakity0aa67702012-05-27 18:36:33 -0700560 if (current_limit != SD_SET_CURRENT_NO_CHANGE) {
561 err = mmc_sd_switch(card, 1, 3, current_limit, status);
562 if (err)
563 return err;
Arindam Nath5371c922011-05-05 12:19:02 +0530564
Philip Rakity0aa67702012-05-27 18:36:33 -0700565 if (((status[15] >> 4) & 0x0F) != current_limit)
Joe Perches66061102014-09-12 14:56:56 -0700566 pr_warn("%s: Problem setting current limit!\n",
Philip Rakity0aa67702012-05-27 18:36:33 -0700567 mmc_hostname(card->host));
Arindam Nath5371c922011-05-05 12:19:02 +0530568
Philip Rakity0aa67702012-05-27 18:36:33 -0700569 }
Aaron Lua39ca6ae2012-07-03 14:16:11 +0800570
Arindam Nath5371c922011-05-05 12:19:02 +0530571 return 0;
572}
573
Arindam Nathd6d50a12011-05-05 12:18:59 +0530574/*
575 * UHS-I specific initialization procedure
576 */
577static int mmc_sd_init_uhs_card(struct mmc_card *card)
578{
579 int err;
580 u8 *status;
581
582 if (!card->scr.sda_spec3)
583 return 0;
584
585 if (!(card->csd.cmdclass & CCC_SWITCH))
586 return 0;
587
588 status = kmalloc(64, GFP_KERNEL);
589 if (!status) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530590 pr_err("%s: could not allocate a buffer for "
Arindam Nathd6d50a12011-05-05 12:18:59 +0530591 "switch capabilities.\n", mmc_hostname(card->host));
592 return -ENOMEM;
593 }
594
595 /* Set 4-bit bus width */
596 if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
597 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
598 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
599 if (err)
600 goto out;
601
602 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
603 }
604
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530605 /*
606 * Select the bus speed mode depending on host
607 * and card capability.
608 */
609 sd_update_bus_speed_mode(card);
610
Arindam Nathd6d50a12011-05-05 12:18:59 +0530611 /* Set the driver strength for the card */
612 err = sd_select_driver_type(card, status);
Arindam Nath49c468f2011-05-05 12:19:01 +0530613 if (err)
614 goto out;
615
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530616 /* Set current limit for the card */
617 err = sd_set_current_limit(card, status);
Arindam Nath5371c922011-05-05 12:19:02 +0530618 if (err)
619 goto out;
620
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530621 /* Set bus speed mode of the card */
622 err = sd_set_bus_speed_mode(card, status);
Arindam Nathb513ea22011-05-05 12:19:04 +0530623 if (err)
624 goto out;
625
Fredrik Soderstedt810e08e2013-04-17 13:50:53 +0200626 /*
627 * SPI mode doesn't define CMD19 and tuning is only valid for SDR50 and
628 * SDR104 mode SD-cards. Note that tuning is mandatory for SDR104.
629 */
Adrian Hunter63e415c2014-12-05 19:40:59 +0200630 if (!mmc_host_is_spi(card->host) &&
Carlo Caionee10c3212016-01-13 09:36:55 +0100631 (card->host->ios.timing == MMC_TIMING_UHS_SDR50 ||
632 card->host->ios.timing == MMC_TIMING_UHS_DDR50 ||
633 card->host->ios.timing == MMC_TIMING_UHS_SDR104)) {
Adrian Hunter63e415c2014-12-05 19:40:59 +0200634 err = mmc_execute_tuning(card);
Weijun Yang4324f6d2015-10-04 12:04:11 +0000635
636 /*
637 * As SD Specifications Part1 Physical Layer Specification
638 * Version 3.01 says, CMD19 tuning is available for unlocked
639 * cards in transfer state of 1.8V signaling mode. The small
640 * difference between v3.00 and 3.01 spec means that CMD19
641 * tuning is also available for DDR50 mode.
642 */
Carlo Caionee10c3212016-01-13 09:36:55 +0100643 if (err && card->host->ios.timing == MMC_TIMING_UHS_DDR50) {
Weijun Yang4324f6d2015-10-04 12:04:11 +0000644 pr_warn("%s: ddr50 tuning failed\n",
645 mmc_hostname(card->host));
646 err = 0;
647 }
648 }
649
Arindam Nathd6d50a12011-05-05 12:18:59 +0530650out:
651 kfree(status);
652
653 return err;
654}
655
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100656MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
657 card->raw_cid[2], card->raw_cid[3]);
658MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
659 card->raw_csd[2], card->raw_csd[3]);
660MMC_DEV_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]);
Uri Yanai5275a652016-08-14 11:46:36 +0300661MMC_DEV_ATTR(ssr,
662 "%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x\n",
663 card->raw_ssr[0], card->raw_ssr[1], card->raw_ssr[2],
664 card->raw_ssr[3], card->raw_ssr[4], card->raw_ssr[5],
665 card->raw_ssr[6], card->raw_ssr[7], card->raw_ssr[8],
666 card->raw_ssr[9], card->raw_ssr[10], card->raw_ssr[11],
667 card->raw_ssr[12], card->raw_ssr[13], card->raw_ssr[14],
668 card->raw_ssr[15]);
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100669MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700670MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
671MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100672MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
673MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
674MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
675MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
676MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
677MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
Bojan Prtvar5fb06af2016-07-04 13:56:55 +0200678MMC_DEV_ATTR(ocr, "%08x\n", card->ocr);
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100679
680
Bojan Prtvar6825a602016-07-19 11:16:38 +0200681static ssize_t mmc_dsr_show(struct device *dev,
682 struct device_attribute *attr,
683 char *buf)
684{
685 struct mmc_card *card = mmc_dev_to_card(dev);
686 struct mmc_host *host = card->host;
687
688 if (card->csd.dsr_imp && host->dsr_req)
689 return sprintf(buf, "0x%x\n", host->dsr);
690 else
691 /* return default DSR value */
692 return sprintf(buf, "0x%x\n", 0x404);
693}
694
695static DEVICE_ATTR(dsr, S_IRUGO, mmc_dsr_show, NULL);
696
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100697static struct attribute *sd_std_attrs[] = {
698 &dev_attr_cid.attr,
699 &dev_attr_csd.attr,
700 &dev_attr_scr.attr,
Uri Yanai5275a652016-08-14 11:46:36 +0300701 &dev_attr_ssr.attr,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100702 &dev_attr_date.attr,
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700703 &dev_attr_erase_size.attr,
704 &dev_attr_preferred_erase_size.attr,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100705 &dev_attr_fwrev.attr,
706 &dev_attr_hwrev.attr,
707 &dev_attr_manfid.attr,
708 &dev_attr_name.attr,
709 &dev_attr_oemid.attr,
710 &dev_attr_serial.attr,
Bojan Prtvar5fb06af2016-07-04 13:56:55 +0200711 &dev_attr_ocr.attr,
Bojan Prtvar6825a602016-07-19 11:16:38 +0200712 &dev_attr_dsr.attr,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100713 NULL,
714};
Axel Lind1e58212014-03-08 15:05:27 +0800715ATTRIBUTE_GROUPS(sd_std);
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100716
Michal Miroslaw71578a12010-08-10 18:01:40 -0700717struct device_type sd_type = {
Axel Lind1e58212014-03-08 15:05:27 +0800718 .groups = sd_std_groups,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100719};
720
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100721/*
Michal Miroslaw71578a12010-08-10 18:01:40 -0700722 * Fetch CID from card.
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200723 */
Arindam Nathd6d50a12011-05-05 12:18:59 +0530724int mmc_sd_get_cid(struct mmc_host *host, u32 ocr, u32 *cid, u32 *rocr)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200725{
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200726 int err;
Aaron Lu55c46652012-07-04 13:31:48 +0800727 u32 max_current;
Johan Rudholm0797e5f2013-01-28 15:08:28 +0100728 int retries = 10;
Ulf Hansson69041152013-09-13 11:31:33 +0200729 u32 pocr = ocr;
Johan Rudholm0797e5f2013-01-28 15:08:28 +0100730
731try_again:
732 if (!retries) {
733 ocr &= ~SD_OCR_S18R;
Joe Perches66061102014-09-12 14:56:56 -0700734 pr_warn("%s: Skipping voltage switch\n", mmc_hostname(host));
Johan Rudholm0797e5f2013-01-28 15:08:28 +0100735 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200736
737 /*
738 * Since we're changing the OCR value, we seem to
739 * need to tell some cards to go back to the idle
740 * state. We wait 1ms to give cards time to
741 * respond.
742 */
743 mmc_go_idle(host);
744
745 /*
746 * If SD_SEND_IF_COND indicates an SD 2.0
747 * compliant card and we should set bit 30
748 * of the ocr to indicate that we can handle
749 * block-addressed SDHC cards.
750 */
751 err = mmc_send_if_cond(host, ocr);
Pierre Ossman17b04292007-07-22 22:18:46 +0200752 if (!err)
Arindam Nathf2119df2011-05-05 12:18:57 +0530753 ocr |= SD_OCR_CCS;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200754
Arindam Nathf2119df2011-05-05 12:18:57 +0530755 /*
756 * If the host supports one of UHS-I modes, request the card
Johan Rudholm0797e5f2013-01-28 15:08:28 +0100757 * to switch to 1.8V signaling level. If the card has failed
758 * repeatedly to switch however, skip this.
Arindam Nathf2119df2011-05-05 12:18:57 +0530759 */
Johan Rudholm0797e5f2013-01-28 15:08:28 +0100760 if (retries && mmc_host_uhs(host))
Arindam Nathf2119df2011-05-05 12:18:57 +0530761 ocr |= SD_OCR_S18R;
762
Aaron Lu55c46652012-07-04 13:31:48 +0800763 /*
764 * If the host can supply more than 150mA at current voltage,
765 * XPC should be set to 1.
766 */
767 max_current = sd_get_host_max_current(host);
768 if (max_current > 150)
Arindam Nathf2119df2011-05-05 12:18:57 +0530769 ocr |= SD_OCR_XPC;
770
Arindam Nathd6d50a12011-05-05 12:18:59 +0530771 err = mmc_send_app_op_cond(host, ocr, rocr);
Pierre Ossman17b04292007-07-22 22:18:46 +0200772 if (err)
Michal Miroslaw71578a12010-08-10 18:01:40 -0700773 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200774
Arindam Nathf2119df2011-05-05 12:18:57 +0530775 /*
776 * In case CCS and S18A in the response is set, start Signal Voltage
777 * Switch procedure. SPI mode doesn't support CMD11.
778 */
Arindam Nathd6d50a12011-05-05 12:18:59 +0530779 if (!mmc_host_is_spi(host) && rocr &&
780 ((*rocr & 0x41000000) == 0x41000000)) {
Ulf Hansson0f791fd2013-09-12 15:36:34 +0200781 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180,
Ulf Hansson69041152013-09-13 11:31:33 +0200782 pocr);
Johan Rudholm0797e5f2013-01-28 15:08:28 +0100783 if (err == -EAGAIN) {
784 retries--;
785 goto try_again;
786 } else if (err) {
787 retries = 0;
Arindam Nathf2119df2011-05-05 12:18:57 +0530788 goto try_again;
789 }
790 }
791
David Brownellaf517152007-08-08 09:11:32 -0700792 if (mmc_host_is_spi(host))
793 err = mmc_send_cid(host, cid);
794 else
795 err = mmc_all_send_cid(host, cid);
Michal Miroslaw71578a12010-08-10 18:01:40 -0700796
797 return err;
798}
799
800int mmc_sd_get_csd(struct mmc_host *host, struct mmc_card *card)
801{
802 int err;
803
804 /*
805 * Fetch CSD from card.
806 */
807 err = mmc_send_csd(card, card->raw_csd);
Pierre Ossman17b04292007-07-22 22:18:46 +0200808 if (err)
Michal Miroslaw71578a12010-08-10 18:01:40 -0700809 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200810
Michal Miroslaw71578a12010-08-10 18:01:40 -0700811 err = mmc_decode_csd(card);
812 if (err)
813 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200814
Michal Miroslaw71578a12010-08-10 18:01:40 -0700815 return 0;
816}
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200817
Lars-Peter Clausen9f6e0bf2015-05-06 20:31:19 +0200818static int mmc_sd_get_ro(struct mmc_host *host)
819{
820 int ro;
821
822 /*
823 * Some systems don't feature a write-protect pin and don't need one.
824 * E.g. because they only have micro-SD card slot. For those systems
825 * assume that the SD card is always read-write.
826 */
827 if (host->caps2 & MMC_CAP2_NO_WRITE_PROTECT)
828 return 0;
829
830 if (!host->ops->get_ro)
831 return -1;
832
Lars-Peter Clausen9f6e0bf2015-05-06 20:31:19 +0200833 ro = host->ops->get_ro(host);
Lars-Peter Clausen9f6e0bf2015-05-06 20:31:19 +0200834
835 return ro;
836}
837
Michal Miroslaw71578a12010-08-10 18:01:40 -0700838int mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card,
839 bool reinit)
840{
841 int err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200842
Michal Miroslaw71578a12010-08-10 18:01:40 -0700843 if (!reinit) {
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200844 /*
845 * Fetch SCR from card.
846 */
847 err = mmc_app_send_scr(card, card->raw_scr);
Pierre Ossman17b04292007-07-22 22:18:46 +0200848 if (err)
Michal Miroslaw71578a12010-08-10 18:01:40 -0700849 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200850
Pierre Ossmanbd766312007-05-01 16:11:57 +0200851 err = mmc_decode_scr(card);
Michal Miroslaw71578a12010-08-10 18:01:40 -0700852 if (err)
853 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200854
855 /*
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700856 * Fetch and process SD Status register.
857 */
858 err = mmc_read_ssr(card);
859 if (err)
860 return err;
861
862 /* Erase init depends on CSD and SSR */
863 mmc_init_erase(card);
864
865 /*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200866 * Fetch switch information from card.
867 */
868 err = mmc_read_switch(card);
Pierre Ossman17b04292007-07-22 22:18:46 +0200869 if (err)
Michal Miroslaw71578a12010-08-10 18:01:40 -0700870 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200871 }
872
873 /*
Wolfgang Muees9d9f25c2009-04-07 14:48:16 +0100874 * For SPI, enable CRC as appropriate.
875 * This CRC enable is located AFTER the reading of the
876 * card registers because some SDHC cards are not able
877 * to provide valid CRCs for non-512-byte blocks.
878 */
879 if (mmc_host_is_spi(host)) {
880 err = mmc_spi_set_crc(host, use_spi_crc);
881 if (err)
Michal Miroslaw71578a12010-08-10 18:01:40 -0700882 return err;
Wolfgang Muees9d9f25c2009-04-07 14:48:16 +0100883 }
884
885 /*
Michal Miroslaw71578a12010-08-10 18:01:40 -0700886 * Check if read-only switch is active.
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200887 */
Michal Miroslaw71578a12010-08-10 18:01:40 -0700888 if (!reinit) {
Lars-Peter Clausen9f6e0bf2015-05-06 20:31:19 +0200889 int ro = mmc_sd_get_ro(host);
Michal Miroslaw71578a12010-08-10 18:01:40 -0700890
891 if (ro < 0) {
Joe Perches66061102014-09-12 14:56:56 -0700892 pr_warn("%s: host does not support reading read-only switch, assuming write-enable\n",
Michal Miroslaw71578a12010-08-10 18:01:40 -0700893 mmc_hostname(host));
894 } else if (ro > 0) {
895 mmc_card_set_readonly(card);
896 }
897 }
898
899 return 0;
900}
901
902unsigned mmc_sd_get_max_clock(struct mmc_card *card)
903{
904 unsigned max_dtr = (unsigned int)-1;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200905
Seungwon Jeoncdc99172014-04-23 17:07:35 +0900906 if (mmc_card_hs(card)) {
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200907 if (max_dtr > card->sw_caps.hs_max_dtr)
908 max_dtr = card->sw_caps.hs_max_dtr;
909 } else if (max_dtr > card->csd.max_dtr) {
910 max_dtr = card->csd.max_dtr;
911 }
912
Michal Miroslaw71578a12010-08-10 18:01:40 -0700913 return max_dtr;
914}
915
Michal Miroslaw71578a12010-08-10 18:01:40 -0700916/*
917 * Handle the detection and initialisation of a card.
918 *
919 * In the case of a resume, "oldcard" will contain the card
920 * we're trying to reinitialise.
921 */
922static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
923 struct mmc_card *oldcard)
924{
925 struct mmc_card *card;
926 int err;
927 u32 cid[4];
Arindam Nathd6d50a12011-05-05 12:18:59 +0530928 u32 rocr = 0;
Michal Miroslaw71578a12010-08-10 18:01:40 -0700929
Michal Miroslaw71578a12010-08-10 18:01:40 -0700930 WARN_ON(!host->claimed);
931
Arindam Nathd6d50a12011-05-05 12:18:59 +0530932 err = mmc_sd_get_cid(host, ocr, cid, &rocr);
Michal Miroslaw71578a12010-08-10 18:01:40 -0700933 if (err)
934 return err;
935
936 if (oldcard) {
937 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0)
938 return -ENOENT;
939
940 card = oldcard;
941 } else {
942 /*
943 * Allocate card structure.
944 */
945 card = mmc_alloc_card(host, &sd_type);
946 if (IS_ERR(card))
947 return PTR_ERR(card);
948
Ulf Hansson69041152013-09-13 11:31:33 +0200949 card->ocr = ocr;
Michal Miroslaw71578a12010-08-10 18:01:40 -0700950 card->type = MMC_TYPE_SD;
951 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
952 }
953
954 /*
Doug Andersoneac86322014-12-02 15:42:45 -0800955 * Call the optional HC's init_card function to handle quirks.
956 */
957 if (host->ops->init_card)
958 host->ops->init_card(host, card);
959
960 /*
Michal Miroslaw71578a12010-08-10 18:01:40 -0700961 * For native busses: get card RCA and quit open drain mode.
962 */
963 if (!mmc_host_is_spi(host)) {
964 err = mmc_send_relative_addr(host, &card->rca);
965 if (err)
Wei WANG7fca9672013-07-17 14:21:10 +0800966 goto free_card;
Michal Miroslaw71578a12010-08-10 18:01:40 -0700967 }
968
969 if (!oldcard) {
970 err = mmc_sd_get_csd(host, card);
971 if (err)
Wei WANG7fca9672013-07-17 14:21:10 +0800972 goto free_card;
Michal Miroslaw71578a12010-08-10 18:01:40 -0700973
974 mmc_decode_cid(card);
975 }
976
977 /*
Sascha Hauer3d705d12014-08-19 10:45:51 +0200978 * handling only for cards supporting DSR and hosts requesting
979 * DSR configuration
980 */
981 if (card->csd.dsr_imp && host->dsr_req)
982 mmc_set_dsr(host);
983
984 /*
Michal Miroslaw71578a12010-08-10 18:01:40 -0700985 * Select card, as all following commands rely on that.
986 */
987 if (!mmc_host_is_spi(host)) {
988 err = mmc_select_card(card);
989 if (err)
Wei WANG7fca9672013-07-17 14:21:10 +0800990 goto free_card;
Michal Miroslaw71578a12010-08-10 18:01:40 -0700991 }
992
993 err = mmc_sd_setup_card(host, card, oldcard != NULL);
994 if (err)
995 goto free_card;
996
Arindam Nathd6d50a12011-05-05 12:18:59 +0530997 /* Initialization sequence for UHS-I cards */
998 if (rocr & SD_ROCR_S18A) {
999 err = mmc_sd_init_uhs_card(card);
Pierre Ossman17b04292007-07-22 22:18:46 +02001000 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001001 goto free_card;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301002 } else {
1003 /*
1004 * Attempt to change to high-speed (if supported)
1005 */
1006 err = mmc_sd_switch_hs(card);
1007 if (err > 0)
Seungwon Jeoncdc99172014-04-23 17:07:35 +09001008 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301009 else if (err)
1010 goto free_card;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001011
Arindam Nathd6d50a12011-05-05 12:18:59 +05301012 /*
1013 * Set bus speed.
1014 */
1015 mmc_set_clock(host, mmc_sd_get_max_clock(card));
1016
1017 /*
1018 * Switch to wider bus (if supported).
1019 */
1020 if ((host->caps & MMC_CAP_4_BIT_DATA) &&
1021 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
1022 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
1023 if (err)
1024 goto free_card;
1025
1026 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
1027 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001028 }
1029
Michal Miroslaw71578a12010-08-10 18:01:40 -07001030 host->card = card;
Pierre Ossman17b04292007-07-22 22:18:46 +02001031 return 0;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001032
1033free_card:
1034 if (!oldcard)
1035 mmc_remove_card(card);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001036
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001037 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001038}
1039
1040/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001041 * Host is being removed. Free up the current card.
1042 */
1043static void mmc_sd_remove(struct mmc_host *host)
1044{
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001045 mmc_remove_card(host->card);
1046 host->card = NULL;
1047}
1048
1049/*
Adrian Hunterd3049502011-11-28 16:22:00 +02001050 * Card detection - card is alive.
1051 */
1052static int mmc_sd_alive(struct mmc_host *host)
1053{
1054 return mmc_send_status(host->card, NULL);
1055}
1056
1057/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001058 * Card detection callback from host.
1059 */
1060static void mmc_sd_detect(struct mmc_host *host)
1061{
1062 int err;
1063
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001064 mmc_get_card(host->card);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001065
1066 /*
1067 * Just check if our card has been removed.
1068 */
Adrian Hunterd3049502011-11-28 16:22:00 +02001069 err = _mmc_detect_card_removed(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001070
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001071 mmc_put_card(host->card);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001072
Pierre Ossman17b04292007-07-22 22:18:46 +02001073 if (err) {
Pierre Ossman4101c162007-05-19 13:39:01 +02001074 mmc_sd_remove(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001075
1076 mmc_claim_host(host);
1077 mmc_detach_bus(host);
Ulf Hansson7f7e4122011-09-21 14:08:13 -04001078 mmc_power_off(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001079 mmc_release_host(host);
1080 }
1081}
1082
Ulf Hansson0cb403a2013-10-10 14:20:05 +02001083static int _mmc_sd_suspend(struct mmc_host *host)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001084{
Jaehoon Chung85e727e2012-05-31 20:31:47 +09001085 int err = 0;
1086
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001087 mmc_claim_host(host);
Ulf Hansson9ec775f2013-10-02 17:37:09 +02001088
1089 if (mmc_card_suspended(host->card))
1090 goto out;
1091
David Brownellaf517152007-08-08 09:11:32 -07001092 if (!mmc_host_is_spi(host))
Jaehoon Chung85e727e2012-05-31 20:31:47 +09001093 err = mmc_deselect_cards(host);
Seungwon Jeoncdc99172014-04-23 17:07:35 +09001094
Ulf Hansson9ec775f2013-10-02 17:37:09 +02001095 if (!err) {
Ulf Hansson74590262013-06-10 17:03:38 +02001096 mmc_power_off(host);
Ulf Hansson9ec775f2013-10-02 17:37:09 +02001097 mmc_card_set_suspended(host->card);
1098 }
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001099
Ulf Hansson9ec775f2013-10-02 17:37:09 +02001100out:
1101 mmc_release_host(host);
Jaehoon Chung85e727e2012-05-31 20:31:47 +09001102 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001103}
1104
1105/*
Ulf Hansson0cb403a2013-10-10 14:20:05 +02001106 * Callback for suspend
1107 */
1108static int mmc_sd_suspend(struct mmc_host *host)
1109{
1110 int err;
1111
1112 err = _mmc_sd_suspend(host);
1113 if (!err) {
1114 pm_runtime_disable(&host->card->dev);
1115 pm_runtime_set_suspended(&host->card->dev);
1116 }
1117
1118 return err;
1119}
1120
1121/*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001122 * This function tries to determine if the same card is still present
1123 * and, if so, restore all state to it.
1124 */
Ulf Hansson0cb403a2013-10-10 14:20:05 +02001125static int _mmc_sd_resume(struct mmc_host *host)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001126{
Ulf Hansson9ec775f2013-10-02 17:37:09 +02001127 int err = 0;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001128
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001129 mmc_claim_host(host);
Ulf Hansson9ec775f2013-10-02 17:37:09 +02001130
1131 if (!mmc_card_suspended(host->card))
1132 goto out;
1133
Ulf Hansson69041152013-09-13 11:31:33 +02001134 mmc_power_up(host, host->card->ocr);
Ulf Hansson69041152013-09-13 11:31:33 +02001135 err = mmc_sd_init_card(host, host->card->ocr, host->card);
Ulf Hansson9ec775f2013-10-02 17:37:09 +02001136 mmc_card_clr_suspended(host->card);
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001137
Ulf Hansson9ec775f2013-10-02 17:37:09 +02001138out:
1139 mmc_release_host(host);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001140 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001141}
1142
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02001143/*
Ulf Hansson0cb403a2013-10-10 14:20:05 +02001144 * Callback for resume
1145 */
1146static int mmc_sd_resume(struct mmc_host *host)
1147{
Ulf Hansson0cb403a2013-10-10 14:20:05 +02001148 pm_runtime_enable(&host->card->dev);
Ulf Hanssonc29536e2015-11-05 16:01:32 +01001149 return 0;
Ulf Hansson0cb403a2013-10-10 14:20:05 +02001150}
1151
1152/*
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02001153 * Callback for runtime_suspend.
1154 */
1155static int mmc_sd_runtime_suspend(struct mmc_host *host)
1156{
1157 int err;
1158
1159 if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
1160 return 0;
1161
Ulf Hansson0cb403a2013-10-10 14:20:05 +02001162 err = _mmc_sd_suspend(host);
Ulf Hansson0cc81a82013-10-03 11:24:44 +02001163 if (err)
Masanari Iidaf42cf8d2015-02-24 23:11:26 +09001164 pr_err("%s: error %d doing aggressive suspend\n",
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02001165 mmc_hostname(host), err);
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02001166
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02001167 return err;
1168}
1169
1170/*
1171 * Callback for runtime_resume.
1172 */
1173static int mmc_sd_runtime_resume(struct mmc_host *host)
1174{
1175 int err;
1176
Ulf Hansson0cb403a2013-10-10 14:20:05 +02001177 err = _mmc_sd_resume(host);
Adrian Hunter520322d2015-12-14 15:51:27 +02001178 if (err && err != -ENOMEDIUM)
Ulf Hanssonc29536e2015-11-05 16:01:32 +01001179 pr_err("%s: error %d doing runtime resume\n",
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02001180 mmc_hostname(host), err);
1181
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02001182 return 0;
1183}
1184
Johan Rudholmdc0ecfe2015-01-12 15:38:06 +01001185static int mmc_sd_reset(struct mmc_host *host)
1186{
1187 mmc_power_cycle(host, host->card->ocr);
Ulf Hansson3056c492015-06-01 11:14:58 +02001188 return mmc_sd_init_card(host, host->card->ocr, host->card);
Johan Rudholmdc0ecfe2015-01-12 15:38:06 +01001189}
1190
Adrian Hunter9feae242009-09-22 16:44:32 -07001191static const struct mmc_bus_ops mmc_sd_ops = {
1192 .remove = mmc_sd_remove,
1193 .detect = mmc_sd_detect,
Ulf Hanssonc4d770d2013-05-02 14:02:39 +02001194 .runtime_suspend = mmc_sd_runtime_suspend,
1195 .runtime_resume = mmc_sd_runtime_resume,
Adrian Hunter9feae242009-09-22 16:44:32 -07001196 .suspend = mmc_sd_suspend,
1197 .resume = mmc_sd_resume,
Adrian Hunterd3049502011-11-28 16:22:00 +02001198 .alive = mmc_sd_alive,
Ulf Hansson5992e782013-06-10 17:03:42 +02001199 .shutdown = mmc_sd_suspend,
Johan Rudholmdc0ecfe2015-01-12 15:38:06 +01001200 .reset = mmc_sd_reset,
Adrian Hunter9feae242009-09-22 16:44:32 -07001201};
1202
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001203/*
1204 * Starting point for SD card init.
1205 */
Andy Ross807e8e42011-01-03 10:36:56 -08001206int mmc_attach_sd(struct mmc_host *host)
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001207{
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001208 int err;
Ulf Hansson69041152013-09-13 11:31:33 +02001209 u32 ocr, rocr;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001210
Pierre Ossmand84075c82007-08-09 13:23:56 +02001211 WARN_ON(!host->claimed);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001212
Andy Ross807e8e42011-01-03 10:36:56 -08001213 err = mmc_send_app_op_cond(host, 0, &ocr);
1214 if (err)
1215 return err;
1216
Ulf Hansson2501c912013-10-30 01:00:18 +01001217 mmc_attach_bus(host, &mmc_sd_ops);
Takashi Iwai8f230f42010-12-08 10:04:30 +01001218 if (host->ocr_avail_sd)
1219 host->ocr_avail = host->ocr_avail_sd;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001220
Philip Langdale55556da2007-03-16 19:39:00 -07001221 /*
David Brownellaf517152007-08-08 09:11:32 -07001222 * We need to get OCR a different way for SPI.
1223 */
1224 if (mmc_host_is_spi(host)) {
1225 mmc_go_idle(host);
1226
1227 err = mmc_spi_read_ocr(host, 0, &ocr);
1228 if (err)
1229 goto err;
1230 }
1231
Ulf Hansson69041152013-09-13 11:31:33 +02001232 rocr = mmc_select_voltage(host, ocr);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001233
1234 /*
1235 * Can we support the voltage(s) of the card(s)?
1236 */
Ulf Hansson69041152013-09-13 11:31:33 +02001237 if (!rocr) {
Pierre Ossman109b5be2007-07-23 00:12:10 +02001238 err = -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001239 goto err;
Pierre Ossman109b5be2007-07-23 00:12:10 +02001240 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001241
1242 /*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001243 * Detect and init the card.
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001244 */
Ulf Hansson69041152013-09-13 11:31:33 +02001245 err = mmc_sd_init_card(host, rocr, NULL);
Pierre Ossman17b04292007-07-22 22:18:46 +02001246 if (err)
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001247 goto err;
1248
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001249 mmc_release_host(host);
Pierre Ossman4101c162007-05-19 13:39:01 +02001250 err = mmc_add_card(host->card);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001251 if (err)
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001252 goto remove_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001253
Sergei Shtylyov2860d062015-10-14 23:53:03 +03001254 mmc_claim_host(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001255 return 0;
1256
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001257remove_card:
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001258 mmc_remove_card(host->card);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001259 host->card = NULL;
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001260 mmc_claim_host(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001261err:
1262 mmc_detach_bus(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001263
Girish K Sa3c76eb2011-10-11 11:44:09 +05301264 pr_err("%s: error %d whilst initialising SD card\n",
Pierre Ossman109b5be2007-07-23 00:12:10 +02001265 mmc_hostname(host), err);
1266
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001267 return err;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001268}