blob: 982af7ae38cf64fee05d5032bdf99a2ad3a19453 [file] [log] [blame]
Channagoud Kadabie9168e82014-01-28 21:33:34 -08001/* Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -07002 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer in the documentation and/or other materials provided
11 * with the distribution.
12 * * Neither the name of The Linux Foundation nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <string.h>
30#include <stdlib.h>
31#include <debug.h>
32#include <reg.h>
33#include <mmc_sdhci.h>
34#include <sdhci.h>
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -070035#include <sdhci_msm.h>
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -070036#include <partition_parser.h>
37#include <platform/iomap.h>
38#include <platform/timer.h>
39
40extern void clock_init_mmc(uint32_t);
41extern void clock_config_mmc(uint32_t, uint32_t);
42
43/* data access time unit in ns */
44static const uint32_t taac_unit[] =
45{
46 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
47};
48
49/* data access time value x 10 */
50static const uint32_t taac_value[] =
51{
52 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80
53};
54
55/* data transfer rate in kbit/s */
56static const uint32_t xfer_rate_unit[] =
57{
58 100, 1000, 10000, 100000, 0, 0, 0, 0
59};
60
61/* data transfer rate value x 10*/
62static const uint32_t xfer_rate_value[] =
63{
64 0, 10, 12, 13, 15, 20, 26, 30, 35, 40, 45, 52, 55, 60, 70, 80
65};
66
67/*
68 * Function: mmc decode and save csd
69 * Arg : Card structure & raw csd
70 * Return : 0 on Success, 1 on Failure
71 * Flow : Decodes CSD response received from the card.
72 * Note that we have defined only few of the CSD elements
73 * in csd structure. We'll only decode those values.
74 */
75static uint32_t mmc_decode_and_save_csd(struct mmc_card *card)
76{
77 uint32_t mmc_sizeof = 0;
78 uint32_t mmc_unit = 0;
79 uint32_t mmc_value = 0;
80 uint32_t mmc_temp = 0;
81 uint32_t *raw_csd = card->raw_csd;
82
83 struct mmc_csd mmc_csd;
84
85 mmc_sizeof = sizeof(uint32_t) * 8;
86
87 mmc_csd.cmmc_structure = UNPACK_BITS(raw_csd, 126, 2, mmc_sizeof);
88
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -070089 if (MMC_CARD_SD(card)) {
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -070090 /* Parse CSD according to SD card spec. */
91
92 /* CSD register is little bit differnet for CSD version 2.0 High
93 * Capacity and CSD version 1.0/2.0 Standard memory cards.
94 * In Version 2.0 some of the fields have fixed values and it's
95 * not necessary for host to refer these fields in CSD sent by
96 * card
97 */
98
99 if (mmc_csd.cmmc_structure == 1) {
100 /* CSD Version 2.0 */
101 mmc_csd.card_cmd_class = UNPACK_BITS(raw_csd, 84, 12, mmc_sizeof);
102 /* Fixed value is 9 = 2^9 = 512 */
103 mmc_csd.write_blk_len = 512;
104 /* Fixed value is 9 = 512 */
105 mmc_csd.read_blk_len = 512;
106 /* Fixed value: 010b */
107 mmc_csd.r2w_factor = 0x2;
108 /* Not there in version 2.0 */
109 mmc_csd.c_size_mult = 0;
110 mmc_csd.c_size = UNPACK_BITS(raw_csd, 48, 22, mmc_sizeof);
111 mmc_csd.nsac_clk_cycle = UNPACK_BITS(raw_csd, 104, 8, mmc_sizeof)
112 * 100;
113
114 mmc_unit = UNPACK_BITS(raw_csd, 112, 3, mmc_sizeof);
115 mmc_value = UNPACK_BITS(raw_csd, 115, 4, mmc_sizeof);
116 mmc_csd.taac_ns = (taac_value[mmc_value] * taac_unit[mmc_unit])
117 / 10;
118
119 mmc_csd.erase_blk_len = 1;
120 mmc_csd.read_blk_misalign = 0;
121 mmc_csd.write_blk_misalign = 0;
122 mmc_csd.read_blk_partial = 0;
123 mmc_csd.write_blk_partial = 0;
124
125 mmc_unit = UNPACK_BITS(raw_csd, 96, 3, mmc_sizeof);
126 mmc_value = UNPACK_BITS(raw_csd, 99, 4, mmc_sizeof);
127 mmc_csd.tran_speed = (xfer_rate_value[mmc_value] *
128 xfer_rate_unit[mmc_unit]) / 10;
129
130 mmc_csd.wp_grp_size = 0x0;
131 mmc_csd.wp_grp_enable = 0x0;
132 mmc_csd.perm_wp = UNPACK_BITS(raw_csd, 13, 1, mmc_sizeof);
133 mmc_csd.temp_wp = UNPACK_BITS(raw_csd, 12, 1, mmc_sizeof);
134
135 /* Calculate the card capcity */
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -0700136 card->capacity = (unsigned long long) (1 + mmc_csd.c_size) * 512 * 1024;
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -0700137 } else {
138 /* CSD Version 1.0 */
139 mmc_csd.card_cmd_class = UNPACK_BITS(raw_csd, 84, 12, mmc_sizeof);
140
141 mmc_temp = UNPACK_BITS(raw_csd, 22, 4, mmc_sizeof);
142 mmc_csd.write_blk_len = (mmc_temp > 8 && mmc_temp < 12) ?
143 (1 << mmc_temp) : 512;
144
145 mmc_temp = UNPACK_BITS(raw_csd, 80, 4, mmc_sizeof);
146 mmc_csd.read_blk_len = (mmc_temp > 8 && mmc_temp < 12) ?
147 (1 << mmc_temp) : 512;
148
149 mmc_unit = UNPACK_BITS(raw_csd, 112, 3, mmc_sizeof);
150 mmc_value = UNPACK_BITS(raw_csd, 115, 4, mmc_sizeof);
151 mmc_csd.taac_ns = (taac_value[mmc_value] * taac_unit[mmc_unit])
152 / 10;
153
154 mmc_unit = UNPACK_BITS(raw_csd, 96, 3, mmc_sizeof);
155 mmc_value = UNPACK_BITS(raw_csd, 99, 4, mmc_sizeof);
156 mmc_csd.tran_speed = (xfer_rate_value[mmc_value] *
157 xfer_rate_unit[mmc_unit]) / 10;
158
159 mmc_csd.nsac_clk_cycle = UNPACK_BITS(raw_csd, 104, 8, mmc_sizeof)
160 * 100;
161
162 mmc_csd.r2w_factor = UNPACK_BITS(raw_csd, 26, 3, mmc_sizeof);
163 mmc_csd.sector_size = UNPACK_BITS(raw_csd, 39, 7, mmc_sizeof) + 1;
164
165 mmc_csd.erase_blk_len = UNPACK_BITS(raw_csd, 46, 1, mmc_sizeof);
166 mmc_csd.read_blk_misalign = UNPACK_BITS(raw_csd, 77, 1, mmc_sizeof);
167 mmc_csd.write_blk_misalign = UNPACK_BITS(raw_csd, 78, 1, mmc_sizeof);
168 mmc_csd.read_blk_partial = UNPACK_BITS(raw_csd, 79, 1, mmc_sizeof);
169 mmc_csd.write_blk_partial = UNPACK_BITS(raw_csd, 21, 1, mmc_sizeof);
170
171 mmc_csd.c_size_mult = UNPACK_BITS(raw_csd, 47, 3, mmc_sizeof);
172 mmc_csd.c_size = UNPACK_BITS(raw_csd, 62, 12, mmc_sizeof);
173 mmc_csd.wp_grp_size = UNPACK_BITS(raw_csd, 32, 7, mmc_sizeof);
174 mmc_csd.wp_grp_enable = UNPACK_BITS(raw_csd, 31, 1, mmc_sizeof);
175 mmc_csd.perm_wp = UNPACK_BITS(raw_csd, 13, 1, mmc_sizeof);
176 mmc_csd.temp_wp = UNPACK_BITS(raw_csd, 12, 1, mmc_sizeof);
177
178 /* Calculate the card capacity */
179 mmc_temp = (1 << (mmc_csd.c_size_mult + 2)) * (mmc_csd.c_size + 1);
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -0700180 card->capacity = (unsigned long long)mmc_temp * mmc_csd.read_blk_len;
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -0700181 }
182 } else {
183 /* Parse CSD according to MMC card spec. */
184 mmc_csd.spec_vers = UNPACK_BITS(raw_csd, 122, 4, mmc_sizeof);
185 mmc_csd.card_cmd_class = UNPACK_BITS(raw_csd, 84, 12, mmc_sizeof);
186 mmc_csd.write_blk_len = 1 << UNPACK_BITS(raw_csd, 22, 4, mmc_sizeof);
187 mmc_csd.read_blk_len = 1 << UNPACK_BITS(raw_csd, 80, 4, mmc_sizeof);
188 mmc_csd.r2w_factor = UNPACK_BITS(raw_csd, 26, 3, mmc_sizeof);
189 mmc_csd.c_size_mult = UNPACK_BITS(raw_csd, 47, 3, mmc_sizeof);
190 mmc_csd.c_size = UNPACK_BITS(raw_csd, 62, 12, mmc_sizeof);
191 mmc_csd.nsac_clk_cycle = UNPACK_BITS(raw_csd, 104, 8, mmc_sizeof) * 100;
192
193 mmc_unit = UNPACK_BITS(raw_csd, 112, 3, mmc_sizeof);
194 mmc_value = UNPACK_BITS(raw_csd, 115, 4, mmc_sizeof);
195 mmc_csd.taac_ns = (taac_value[mmc_value] * taac_unit[mmc_unit]) / 10;
196
197 mmc_csd.read_blk_misalign = UNPACK_BITS(raw_csd, 77, 1, mmc_sizeof);
198 mmc_csd.write_blk_misalign = UNPACK_BITS(raw_csd, 78, 1, mmc_sizeof);
199 mmc_csd.read_blk_partial = UNPACK_BITS(raw_csd, 79, 1, mmc_sizeof);
200 mmc_csd.write_blk_partial = UNPACK_BITS(raw_csd, 21, 1, mmc_sizeof);
201
202 /* Ignore -- no use of this value. */
203 mmc_csd.tran_speed = 0x00;
204
205 mmc_csd.erase_grp_size = UNPACK_BITS(raw_csd, 42, 5, mmc_sizeof);
206 mmc_csd.erase_grp_mult = UNPACK_BITS(raw_csd, 37, 5, mmc_sizeof);
207 mmc_csd.wp_grp_size = UNPACK_BITS(raw_csd, 32, 5, mmc_sizeof);
208 mmc_csd.wp_grp_enable = UNPACK_BITS(raw_csd, 31, 1, mmc_sizeof);
209 mmc_csd.perm_wp = UNPACK_BITS(raw_csd, 13, 1, mmc_sizeof);
210 mmc_csd.temp_wp = UNPACK_BITS(raw_csd, 12, 1, mmc_sizeof);
211
212 /* Calculate the card capcity */
213 if (mmc_csd.c_size != 0xFFF) {
214 /* For cards less than or equal to 2GB */
215 mmc_temp = (1 << (mmc_csd.c_size_mult + 2)) * (mmc_csd.c_size + 1);
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -0700216 card->capacity = (unsigned long long) mmc_temp * mmc_csd.read_blk_len;
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -0700217 } else {
218 /* For cards greater than 2GB, Ext CSD register's SEC_COUNT
219 * is used to calculate the size.
220 */
221 uint64_t sec_count;
222
223 sec_count = (card->ext_csd[MMC_SEC_COUNT4] << MMC_SEC_COUNT4_SHIFT)
224 | (card->ext_csd[MMC_SEC_COUNT3] << MMC_SEC_COUNT3_SHIFT)
225 | (card->ext_csd[MMC_SEC_COUNT2] << MMC_SEC_COUNT2_SHIFT)
226 | card->ext_csd[MMC_SEC_COUNT1];
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -0700227 card->capacity = sec_count * MMC_BLK_SZ;
228 }
229 }
230
231 /* save the information in card structure */
232 memcpy((struct mmc_csd *)&card->csd,(struct mmc_csd *)&mmc_csd,
233 sizeof(struct mmc_csd));
234
vijay kumar697dbfd2014-04-24 17:12:49 +0530235 /* Calculate the wp grp size */
236 if (card->ext_csd[MMC_ERASE_GRP_DEF])
237 card->wp_grp_size = MMC_HC_ERASE_MULT * card->ext_csd[MMC_HC_ERASE_GRP_SIZE] / MMC_BLK_SZ;
238 else
239 card->wp_grp_size = (card->csd.wp_grp_size + 1) * (card->csd.erase_grp_size + 1) \
240 * (card->csd.erase_grp_mult + 1);
241
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -0700242 dprintf(SPEW, "Decoded CSD fields:\n");
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -0700243 dprintf(SPEW, "cmmc_structure: %u\n", mmc_csd.cmmc_structure);
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -0700244 dprintf(SPEW, "card_cmd_class: %x\n", mmc_csd.card_cmd_class);
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -0700245 dprintf(SPEW, "write_blk_len: %u\n", mmc_csd.write_blk_len);
246 dprintf(SPEW, "read_blk_len: %u\n", mmc_csd.read_blk_len);
247 dprintf(SPEW, "r2w_factor: %u\n", mmc_csd.r2w_factor);
248 dprintf(SPEW, "sector_size: %u\n", mmc_csd.sector_size);
249 dprintf(SPEW, "c_size_mult:%u\n", mmc_csd.c_size_mult);
250 dprintf(SPEW, "c_size: %u\n", mmc_csd.c_size);
251 dprintf(SPEW, "nsac_clk_cycle: %u\n", mmc_csd.nsac_clk_cycle);
252 dprintf(SPEW, "taac_ns: %u\n", mmc_csd.taac_ns);
253 dprintf(SPEW, "tran_speed: %u kbps\n", mmc_csd.tran_speed);
254 dprintf(SPEW, "erase_blk_len: %u\n", mmc_csd.erase_blk_len);
255 dprintf(SPEW, "read_blk_misalign: %u\n", mmc_csd.read_blk_misalign);
256 dprintf(SPEW, "write_blk_misalign: %u\n", mmc_csd.write_blk_misalign);
257 dprintf(SPEW, "read_blk_partial: %u\n", mmc_csd.read_blk_partial);
258 dprintf(SPEW, "write_blk_partial: %u\n", mmc_csd.write_blk_partial);
vijay kumar697dbfd2014-04-24 17:12:49 +0530259 dprintf(SPEW, "wp_grp_size: %u\n", card->wp_grp_size);
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -0700260 dprintf(SPEW, "Card Capacity: %llu Bytes\n", card->capacity);
261
262 return 0;
263}
264
265/*
266 * Function: mmc decode & save cid
267 * Arg : card structure & raw cid
268 * Return : 0 on Success, 1 on Failure
269 * Flow : Decode CID sent by the card.
270 */
271static uint32_t mmc_decode_and_save_cid(struct mmc_card *card,
272 uint32_t *raw_cid)
273{
274 struct mmc_cid mmc_cid;
275 uint32_t mmc_sizeof = 0;
276 int i = 0;
277
278 if (!raw_cid) {
279 return 1;
280 }
281
282 mmc_sizeof = sizeof(uint32_t) * 8;
283
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -0700284 if (MMC_CARD_SD(card)) {
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -0700285 mmc_cid.mid = UNPACK_BITS(raw_cid, 120, 8, mmc_sizeof);
286 mmc_cid.oid = UNPACK_BITS(raw_cid, 104, 16, mmc_sizeof);
287
288 for (i = 0; i < 5; i++) {
289 mmc_cid.pnm[i] = (uint8_t)UNPACK_BITS(raw_cid,
290 (104 - 8 * (i + 1)),
291 8,
292 mmc_sizeof);
293 }
294 mmc_cid.pnm[5] = 0;
295 mmc_cid.pnm[6] = 0;
296
297 mmc_cid.prv = UNPACK_BITS(raw_cid, 56, 8, mmc_sizeof);
David Ng571b8c32013-07-30 19:17:42 -0700298 mmc_cid.psn = UNPACK_BITS(raw_cid, 24, 32, mmc_sizeof);
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -0700299 mmc_cid.month = UNPACK_BITS(raw_cid, 8, 4, mmc_sizeof);
300 mmc_cid.year = UNPACK_BITS(raw_cid, 12, 8, mmc_sizeof);
301 mmc_cid.year += 2000;
302 } else {
303 mmc_cid.mid = UNPACK_BITS(raw_cid, 120, 8, mmc_sizeof);
304 mmc_cid.oid = UNPACK_BITS(raw_cid, 104, 16, mmc_sizeof);
305
306 for (i = 0; i < 6; i++) {
307 mmc_cid.pnm[i] = (uint8_t)UNPACK_BITS(raw_cid, (104 - 8 * (i + 1)),
308 8, mmc_sizeof);
309 }
310 mmc_cid.pnm[6] = 0;
311
312 mmc_cid.prv = UNPACK_BITS(raw_cid, 48, 8, mmc_sizeof);
David Ng571b8c32013-07-30 19:17:42 -0700313 mmc_cid.psn = UNPACK_BITS(raw_cid, 16, 32, mmc_sizeof);
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -0700314 mmc_cid.month = UNPACK_BITS(raw_cid, 8, 4, mmc_sizeof);
315 mmc_cid.year = UNPACK_BITS(raw_cid, 12, 4, mmc_sizeof);
316 mmc_cid.year += 1997;
317 }
318
319 /* save it in card database */
320 memcpy((struct mmc_cid *)&card->cid,
321 (struct mmc_cid *)&mmc_cid, sizeof(struct mmc_cid));
322
323 dprintf(SPEW, "Decoded CID fields:\n");
324 dprintf(SPEW, "Manufacturer ID: %x\n", mmc_cid.mid);
325 dprintf(SPEW, "OEM ID: 0x%x\n", mmc_cid.oid);
326 dprintf(SPEW, "Product Name: %s\n", mmc_cid.pnm);
327 dprintf(SPEW, "Product revision: %d.%d\n", (mmc_cid.prv >> 4),
328 (mmc_cid.prv & 0xF));
329 dprintf(SPEW, "Product serial number: %X\n", mmc_cid.psn);
330 dprintf(SPEW, "Manufacturing date: %d %d\n", mmc_cid.month, mmc_cid.year);
331
332 return 0;
333}
334
335/*
336 * Function: mmc reset cards
337 * Arg : host structure
338 * Return : 0 on Success, 1 on Failure
339 * Flow : Reset all the cards to idle condition (CMD 0)
340 */
341static uint8_t mmc_reset_card(struct sdhci_host *host)
342{
343 struct mmc_command cmd;
344
345 memset((struct mmc_command *)&cmd, 0, sizeof(struct mmc_command));
346
347 cmd.cmd_index = CMD0_GO_IDLE_STATE;
348 cmd.argument = 0;
349 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
350 cmd.resp_type = SDHCI_CMD_RESP_NONE;
351
352 /* send command */
353 return sdhci_send_command(host, &cmd);
354}
355
356/*
357 * Function: mmc operations command
358 * Arg : host & card structure
359 * Return : 0 on Success, 1 on Failure
360 * Flow : Send CMD1 to know whether the card supports host VDD profile or not.
361 */
362static uint32_t mmc_send_op_cond(struct sdhci_host *host, struct mmc_card *card)
363{
364 struct mmc_command cmd;
365 uint32_t mmc_resp = 0;
366 uint32_t mmc_ret = 0;
367 uint32_t mmc_retry = 0;
368
369 memset((struct mmc_command *)&cmd, 0, sizeof(struct mmc_command));
370
371 /* CMD1 format:
372 * [31] Busy bit
373 * [30:29] Access mode
374 * [28:24] reserved
375 * [23:15] 2.7-3.6
376 * [14:8] 2.0-2.6
377 * [7] 1.7-1.95
378 * [6:0] reserved
379 */
380
381 cmd.cmd_index = CMD1_SEND_OP_COND;
382 cmd.argument = card->ocr;
383 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
384 cmd.resp_type = SDHCI_CMD_RESP_R3;
385
386 do {
387 mmc_ret = sdhci_send_command(host, &cmd);
388 if (mmc_ret)
389 return mmc_ret;
390
391 /* Command returned success, now it's time to examine response */
392 mmc_resp = cmd.resp[0];
393
394 /* Check the response for busy status */
395 if (!(mmc_resp & MMC_OCR_BUSY)) {
396 mmc_retry++;
397 mdelay(1);
398 continue;
399 } else
400 break;
401 } while (mmc_retry < MMC_MAX_COMMAND_RETRY);
402
403 /* If we reached here after max retries, we failed to get OCR */
404 if (mmc_retry == MMC_MAX_COMMAND_RETRY && !(mmc_resp & MMC_OCR_BUSY)) {
405 dprintf(CRITICAL, "Card has busy status set. Init did not complete\n");
406 return 1;
407 }
408
409 /* Response contains card's ocr. Update card's information */
410 card->ocr = mmc_resp;
411
412 if (mmc_resp & MMC_OCR_SEC_MODE)
413 card->type = MMC_TYPE_MMCHC;
414 else
415 card->type = MMC_TYPE_STD_MMC;
416
417 return 0;
418}
419
420/*
421 * Function: mmc send cid
422 * Arg : host & card structure
423 * Return : 0 on Success, 1 on Failure
424 * Flow : Request any card to send its uniquie card identification
425 * (CID) number (CMD2).
426 */
427static uint32_t mmc_all_send_cid(struct sdhci_host *host, struct mmc_card *card)
428{
429 struct mmc_command cmd;
430 uint32_t mmc_ret = 0;
431
432 memset((struct mmc_command *)&cmd, 0, sizeof(struct mmc_command));
433
434 /* CMD2 Format:
435 * [31:0] stuff bits
436 */
437 cmd.cmd_index = CMD2_ALL_SEND_CID;
438 cmd.argument = 0;
439 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
440 cmd.resp_type = SDHCI_CMD_RESP_R2;
441
442 /* send command */
443 mmc_ret = sdhci_send_command(host, &cmd);
444 if (mmc_ret) {
445 return mmc_ret;
446 }
447
448 /* Response contains card's 128 bits CID register */
449 mmc_ret = mmc_decode_and_save_cid(card, cmd.resp);
450 if (mmc_ret) {
451 return mmc_ret;
452 }
453
454 return 0;
455}
456
457/*
458 * Function: mmc send relative address
459 * Arg : host & card structure
460 * Return : 0 on Success, 1 on Failure
461 * Flow : Ask card to send it's relative card address (RCA).
462 * This RCA number is shorter than CID and is used by
463 * the host to address the card in future (CMD3)
464 */
465static uint32_t mmc_send_relative_address(struct sdhci_host *host,
466 struct mmc_card *card)
467{
468 struct mmc_command cmd;
469 uint32_t mmc_ret = 0;
470
471 memset((struct mmc_command *)&cmd, 0, sizeof(struct mmc_command));
472
473 /* CMD3 Format:
474 * [31:0] stuff bits
475 */
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -0700476 if (MMC_CARD_SD(card)) {
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -0700477 cmd.cmd_index = CMD3_SEND_RELATIVE_ADDR;
478 cmd.argument = 0;
479 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
480 cmd.resp_type = SDHCI_CMD_RESP_R6;
481
482 /* send command */
483 mmc_ret = sdhci_send_command(host, &cmd);
484 if (mmc_ret)
485 return mmc_ret;
486
487 /* For sD, card will send RCA. Store it */
488 card->rca = (cmd.resp[0] >> 16);
489 } else {
490 cmd.cmd_index = CMD3_SEND_RELATIVE_ADDR;
491 cmd.argument = (MMC_RCA << 16);
492 card->rca = (cmd.argument >> 16);
493 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
494 cmd.resp_type = SDHCI_CMD_RESP_R6;
495
496 /* send command */
497 mmc_ret = sdhci_send_command(host, &cmd);
498 if (mmc_ret)
499 return mmc_ret;
500 }
501
502 return 0;
503}
504
505/*
506 * Function: mmc send csd
507 * Arg : host, card structure & o/p arg to store csd
508 * Return : 0 on Success, 1 on Failure
509 * Flow : Requests card to send it's CSD register's contents. (CMD9)
510 */
511static uint32_t mmc_send_csd(struct sdhci_host *host, struct mmc_card *card)
512{
513 struct mmc_command cmd;
514 uint32_t mmc_arg = 0;
515 uint32_t mmc_ret = 0;
516
517 memset((struct mmc_command *)&cmd, 0, sizeof(struct mmc_command));
518
519 /* CMD9 Format:
520 * [31:16] RCA
521 * [15:0] stuff bits
522 */
523 mmc_arg |= card->rca << 16;
524
525 cmd.cmd_index = CMD9_SEND_CSD;
526 cmd.argument = mmc_arg;
527 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
528 cmd.resp_type = SDHCI_CMD_RESP_R2;
529
530 /* send command */
531 mmc_ret = sdhci_send_command(host, &cmd);
532 if (mmc_ret)
533 return mmc_ret;
534
535 /* response contains the card csd */
536 memcpy(card->raw_csd, cmd.resp, sizeof(cmd.resp));
537
538 return 0;
539}
540
541/*
542 * Function: mmc select card
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -0700543 * Arg : host, card structure
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -0700544 * Return : 0 on Success, 1 on Failure
545 * Flow : Selects a card by sending CMD7 to the card with its RCA.
546 * If RCA field is set as 0 ( or any other address ),
547 * the card will be de-selected. (CMD7)
548 */
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -0700549static uint32_t mmc_select_card(struct sdhci_host *host, struct mmc_card *card)
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -0700550{
551 struct mmc_command cmd;
552 uint32_t mmc_arg = 0;
553 uint32_t mmc_ret = 0;
554
555 memset((struct mmc_command *)&cmd, 0, sizeof(struct mmc_command));
556
557 /* CMD7 Format:
558 * [31:16] RCA
559 * [15:0] stuff bits
560 */
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -0700561 mmc_arg |= card->rca << 16;
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -0700562
563 cmd.cmd_index = CMD7_SELECT_DESELECT_CARD;
564 cmd.argument = mmc_arg;
565 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
566
567 /* If we are deselecting card, we do not get response */
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -0700568 if (card->rca) {
569 if (MMC_CARD_SD(card))
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -0700570 cmd.resp_type = SDHCI_CMD_RESP_R1B;
571 else
572 cmd.resp_type = SDHCI_CMD_RESP_R1;
573 } else
574 cmd.resp_type = SDHCI_CMD_RESP_NONE;
575
576 /* send command */
577 mmc_ret = sdhci_send_command(host, &cmd);
578 if (mmc_ret)
579 return mmc_ret;
580
581 return 0;
582}
583
584/*
585 * Function: mmc set block len
586 * Arg : host, card structure & block length
587 * Return : 0 on Success, 1 on Failure
588 * Flow : Send command to set block length.
589 */
590static uint32_t mmc_set_block_len(struct sdhci_host *host,
591 struct mmc_card *card,
592 uint32_t block_len)
593{
594 struct mmc_command cmd;
595 uint32_t mmc_ret = 0;
596
597 memset((struct mmc_command *)&cmd, 0, sizeof(struct mmc_command));
598
599 /* CMD16 Format:
600 * [31:0] block length
601 */
602
603 cmd.cmd_index = CMD16_SET_BLOCKLEN;
604 cmd.argument = block_len;
605 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
606 cmd.resp_type = SDHCI_CMD_RESP_R1;
607
608 /* send command */
609 mmc_ret = sdhci_send_command(host, &cmd);
610 if (mmc_ret)
611 return mmc_ret;
612
613 /*
614 * If blocklength is larger than 512 bytes,
615 * the card sets BLOCK_LEN_ERROR bit.
616 */
617 if (cmd.resp[0] & MMC_R1_BLOCK_LEN_ERR) {
618 dprintf(CRITICAL, "The block length is not supported by the card\n");
619 return 1;
620 }
621
622 return 0;
623}
624
625/*
626 * Function: mmc get card status
627 * Arg : host, card structure & o/p argument card status
628 * Return : 0 on Success, 1 on Failure
629 * Flow : Get the current status of the card
630 */
631static uint32_t mmc_get_card_status(struct sdhci_host *host,
632 struct mmc_card *card, uint32_t *status)
633{
634 struct mmc_command cmd;
635 uint32_t mmc_ret = 0;
636
637 memset((struct mmc_command *)&cmd, 0, sizeof(struct mmc_command));
638
639 /* CMD13 Format:
640 * [31:16] RCA
641 * [15:0] stuff bits
642 */
643 cmd.cmd_index = CMD13_SEND_STATUS;
644 cmd.argument = card->rca << 16;
645 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
646 cmd.resp_type = SDHCI_CMD_RESP_R1;
647
648 /* send command */
649 mmc_ret = sdhci_send_command(host, &cmd);
650 if (mmc_ret)
651 return mmc_ret;
652
653 /* Checking ADDR_OUT_OF_RANGE error in CMD13 response */
654 if ((cmd.resp[0] >> 31) & 0x01)
655 return 1;
656
657 *status = cmd.resp[0];
658 return 0;
659}
660
661/*
662 * Function: mmc get ext csd
663 * Arg : host, card structure & array to hold ext attributes
664 * Return : 0 on Success, 1 on Failure
665 * Flow : Send ext csd command & get the card attributes
666 */
667static uint32_t mmc_get_ext_csd(struct sdhci_host *host, struct mmc_card *card)
668{
669 struct mmc_command cmd;
670 uint32_t mmc_ret = 0;
671
672 card->ext_csd = memalign(CACHE_LINE, ROUNDUP(512, CACHE_LINE));
673
674 ASSERT(card->ext_csd);
675
676 memset(card->ext_csd, 0, sizeof(card->ext_csd));
677
678 memset((struct mmc_command *)&cmd, 0, sizeof(struct mmc_command));
679
680 /* CMD8 */
681 cmd.cmd_index = CMD8_SEND_EXT_CSD;
682 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
683 cmd.resp_type = SDHCI_CMD_RESP_R1;
684 cmd.data.data_ptr = card->ext_csd;
685 cmd.data.num_blocks = 1;
686 cmd.data_present = 0x1;
687 cmd.trans_mode = SDHCI_MMC_READ;
688
689 /* send command */
690 mmc_ret = sdhci_send_command(host, &cmd);
691 if (mmc_ret)
692 return mmc_ret;
693
694 return mmc_ret;
695}
696
697/*
698 * Function: mmc switch command
699 * Arg : Host, card structure, access mode, index & value to be set
700 * Return : 0 on Success, 1 on Failure
701 * Flow : Send switch command to the card to set the ext attribute @ index
702 */
703static uint32_t mmc_switch_cmd(struct sdhci_host *host, struct mmc_card *card,
704 uint32_t access, uint32_t index, uint32_t value)
705{
706
707 struct mmc_command cmd;
708 uint32_t mmc_ret = 0;
709 uint32_t mmc_status;
710
711 memset((struct mmc_command *)&cmd, 0, sizeof(struct mmc_command));
712
713 /* CMD6 Format:
714 * [31:26] set to 0
715 * [25:24] access
716 * [23:16] index
717 * [15:8] value
718 * [7:3] set to 0
719 * [2:0] cmd set
720 */
721 cmd.cmd_index = CMD6_SWITCH_FUNC;
722 cmd.argument |= (access << 24);
723 cmd.argument |= (index << 16);
724 cmd.argument |= (value << 8);
725 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
726 cmd.resp_type = SDHCI_CMD_RESP_R1B;
727
728 mmc_ret = sdhci_send_command(host, &cmd);
729 if (mmc_ret) {
730 dprintf(CRITICAL, "CMD6 send failed\n");
731 return mmc_ret;
732 }
733
734 /* Check if the card completed the switch command processing */
735 mmc_ret = mmc_get_card_status(host, card, &mmc_status);
736 if (mmc_ret) {
737 dprintf(CRITICAL, "Get card status failed\n");
738 return mmc_ret;
739 }
740
741 if (MMC_CARD_STATUS(mmc_status) != MMC_TRAN_STATE) {
Channagoud Kadabie106d1f2014-04-25 18:26:26 -0700742 dprintf(CRITICAL, "Switch cmd failed. Card not in tran state %x\n", mmc_status);
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -0700743 mmc_ret = 1;
744 }
745
746 if (mmc_status & MMC_SWITCH_FUNC_ERR_FLAG) {
747 dprintf(CRITICAL, "Switch cmd failed. Switch Error.\n");
748 mmc_ret = 1;
749 }
750
751 return mmc_ret;
752}
753
Channagoud Kadabie106d1f2014-04-25 18:26:26 -0700754bool mmc_set_drv_type(struct sdhci_host *host, struct mmc_card *card, uint8_t drv_type)
755{
756 uint32_t ret = 0;
757 bool drv_type_changed = false;
758
759 uint32_t value = ((drv_type << 4) | MMC_HS200_TIMING);
760
761 if (card->ext_csd[MMC_EXT_MMC_DRV_STRENGTH] & (1 << drv_type))
762 ret = mmc_switch_cmd(host, card, MMC_ACCESS_WRITE, MMC_EXT_MMC_HS_TIMING, value);
763 if (!ret)
764 drv_type_changed = true;
765
766 return drv_type_changed;
767}
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -0700768/*
769 * Function: mmc set bus width
770 * Arg : Host, card structure & width
771 * Return : 0 on Success, 1 on Failure
772 * Flow : Send switch command to set bus width
773 */
774static uint32_t mmc_set_bus_width(struct sdhci_host *host,
775 struct mmc_card *card,
776 uint32_t width)
777{
778 uint32_t mmc_ret = 0;
779
780 mmc_ret = mmc_switch_cmd(host, card, MMC_ACCESS_WRITE,
781 MMC_EXT_MMC_BUS_WIDTH, width);
782
783 if (mmc_ret) {
784 dprintf(CRITICAL, "Switch cmd failed\n");
785 return mmc_ret;
786 }
787
788 return 0;
789}
790
791
792/*
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700793 * Function: mmc card supports hs400 mode
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -0700794 * Arg : None
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700795 * Return : 1 if hs400 mode is supported, 0 otherwise
796 * Flow : Check the ext csd attributes of the card
797 */
798static uint8_t mmc_card_supports_hs400_mode(struct mmc_card *card)
799{
800 if (card->ext_csd[MMC_DEVICE_TYPE] & MMC_HS_HS400_MODE)
801 return 1;
802 else
803 return 0;
804}
805
806/*
807 * Function: mmc card supports hs200 mode
808 * Arg : None
809 * Return : 1 if HS200 mode is supported, 0 otherwise
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -0700810 * Flow : Check the ext csd attributes of the card
811 */
812static uint8_t mmc_card_supports_hs200_mode(struct mmc_card *card)
813{
814 if (card->ext_csd[MMC_DEVICE_TYPE] & MMC_HS_HS200_MODE)
815 return 1;
816 else
817 return 0;
818}
819
820/*
821 * Function: mmc card supports ddr mode
822 * Arg : None
823 * Return : 1 if DDR mode is supported, 0 otherwise
824 * Flow : Check the ext csd attributes of the card
825 */
826static uint8_t mmc_card_supports_ddr_mode(struct mmc_card *card)
827{
828 if (card->ext_csd[MMC_DEVICE_TYPE] & MMC_HS_DDR_MODE)
829 return 1;
830 else
831 return 0;
832}
833
834/*
835 * Function : Enable HS200 mode
836 * Arg : Host, card structure and bus width
837 * Return : 0 on Success, 1 on Failure
838 * Flow :
839 * - Set the bus width to 4/8 bit SDR as supported by the target & host
840 * - Set the HS_TIMING on ext_csd 185 for the card
841 */
842static uint32_t mmc_set_hs200_mode(struct sdhci_host *host,
843 struct mmc_card *card, uint32_t width)
844{
845 uint32_t mmc_ret = 0;
846
Channagoud Kadabie632e252014-03-31 15:26:00 -0700847 DBG("\n Enabling HS200 Mode Start\n");
848
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -0700849 /* Set 4/8 bit SDR bus width */
850 mmc_ret = mmc_set_bus_width(host, card, width);
851 if (mmc_ret) {
852 dprintf(CRITICAL, "Failure to set wide bus for Card(RCA:%x)\n",
853 card->rca);
854 return mmc_ret;
855 }
856
857 /* Setting HS200 in HS_TIMING using EXT_CSD (CMD6) */
858 mmc_ret = mmc_switch_cmd(host, card, MMC_ACCESS_WRITE, MMC_EXT_MMC_HS_TIMING, MMC_HS200_TIMING);
859
860 if (mmc_ret) {
861 dprintf(CRITICAL, "Switch cmd returned failure %d\n", __LINE__);
862 return mmc_ret;
863 }
864
Channagoud Kadabi1dc6aab2013-11-20 12:46:15 -0800865 /* Enable SDR104 mode in controller */
866 sdhci_set_uhs_mode(host, SDHCI_SDR104_MODE);
867
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700868 /* Run the clock @ 400 Mhz */
Channagoud Kadabie9168e82014-01-28 21:33:34 -0800869 if (host->caps.hs400_support && mmc_card_supports_hs400_mode(card))
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700870 {
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700871 /* Save the timing value, before changing the clock */
872 MMC_SAVE_TIMING(host, MMC_HS400_TIMING);
Channagoud Kadabi86756c12014-03-12 17:16:39 -0700873 /*
874 * Set the MCI_CLK divider before changing the sdcc core
875 * core clk to ensure card receives no more than 200 MHZ
876 * clock frequency
877 */
878 sdhci_msm_set_mci_clk(host);
879 clock_config_mmc(host->msm_host->slot, SDHCI_CLK_400MHZ);
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700880 }
Channagoud Kadabi642ff552014-08-12 21:40:59 -0700881
882 /* Execute Tuning for hs200 mode */
883 if ((mmc_ret = sdhci_msm_execute_tuning(host, card, width)))
884 dprintf(CRITICAL, "Tuning for hs200 failed\n");
885
886 /* Once the tuning is executed revert back the clock to 200MHZ
887 * and disable the MCI_CLK divider so that we can use SDHC clock
888 * divider to supply clock to the card
889 */
890 if (host->timing == MMC_HS400_TIMING)
891 {
892 MMC_SAVE_TIMING(host, MMC_HS200_TIMING);
893 sdhci_msm_set_mci_clk(host);
894 clock_config_mmc(host->msm_host->slot, MMC_CLK_192MHZ);
895 }
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700896 else
897 {
898 /* Save the timing value, before changing the clock */
899 MMC_SAVE_TIMING(host, MMC_HS200_TIMING);
900 }
901
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -0700902
Channagoud Kadabie632e252014-03-31 15:26:00 -0700903 DBG("\n Enabling HS200 Mode Done\n");
904
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -0700905 return mmc_ret;
906}
907
908/*
909 * Function: mmc set ddr mode
910 * Arg : Host & card structure
911 * Return : 0 on Success, 1 on Failure
912 * Flow : Set bus width for ddr mode & set controller in DDR mode
913*/
914static uint8_t mmc_set_ddr_mode(struct sdhci_host *host, struct mmc_card *card)
915{
916 uint8_t mmc_ret = 0;
917
Channagoud Kadabie632e252014-03-31 15:26:00 -0700918 DBG("\n Enabling DDR Mode Start\n");
919
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -0700920 /* Set width for 8 bit DDR mode by default */
921 mmc_ret = mmc_set_bus_width(host, card, DATA_DDR_BUS_WIDTH_8BIT);
922
923 if (mmc_ret) {
924 dprintf(CRITICAL, "Failure to set DDR mode for Card(RCA:%x)\n",
925 card->rca);
926 return mmc_ret;
927 }
928
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700929 /* Save the timing value, before changing the clock */
930 MMC_SAVE_TIMING(host, SDHCI_DDR50_MODE);
931
932 /* Set the DDR mode in controller */
933 sdhci_set_uhs_mode(host, SDHCI_DDR50_MODE);
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -0700934
Channagoud Kadabie632e252014-03-31 15:26:00 -0700935 DBG("\n Enabling DDR Mode Done\n");
936
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -0700937 return 0;
938}
939
940/*
941 * Function: mmc set high speed interface
942 * Arg : Host & card structure
943 * Return : None
944 * Flow : Sets the sdcc clock & clock divider in the host controller
945 * Adjust the interface speed to optimal speed
946 */
947static uint32_t mmc_set_hs_interface(struct sdhci_host *host,
948 struct mmc_card *card)
949{
950 uint32_t mmc_ret = 0;
951
952 /* Setting HS_TIMING in EXT_CSD (CMD6) */
953 mmc_ret = mmc_switch_cmd(host, card, MMC_ACCESS_WRITE,
954 MMC_EXT_MMC_HS_TIMING, MMC_HS_TIMING);
955
956 if (mmc_ret) {
957 dprintf(CRITICAL, "Switch cmd returned failure %d\n", __LINE__);
958 return mmc_ret;
959 }
960
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700961 /* Save the timing value, before changing the clock */
962 MMC_SAVE_TIMING(host, SDHCI_SDR25_MODE);
963
964 /* Set the SDR25 mode in controller */
965 sdhci_set_uhs_mode(host, SDHCI_SDR25_MODE);
966
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -0700967 return 0;
968}
969
970/*
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700971 * Function : Enable HS400 mode
972 * Arg : Host, card structure and bus width
973 * Return : 0 on Success, 1 on Failure
974 * Flow :
975 * - Set the bus width to 8 bit DDR
976 * - Set the HS_TIMING on ext_csd 185 for the card
977 */
978uint32_t mmc_set_hs400_mode(struct sdhci_host *host,
979 struct mmc_card *card, uint32_t width)
980{
981 uint32_t mmc_ret = 0;
982
983 /*
984 * Emmc 5.0 spec does not allow changing to hs400 mode directly
985 * Need to follow the sequence to change to hs400 mode
986 * 1. Enable HS200 mode, perform tuning
987 * 2. Change to high speed mode
988 * 3. Enable DDR mode
989 * 4. Enable HS400 mode & execute tuning
990 */
991
Channagoud Kadabie632e252014-03-31 15:26:00 -0700992 DBG("\n Enabling HS400 Mode Start\n");
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -0700993 /* HS400 mode is supported only in DDR 8-bit */
994 if (width != DATA_BUS_WIDTH_8BIT)
995 {
996 dprintf(CRITICAL, "Bus width is not 8-bit, cannot switch to hs400: %u\n", width);
997 return 1;
998 }
999
1000 /* 1.Enable HS200 mode */
1001 mmc_ret = mmc_set_hs200_mode(host, card, width);
1002
1003 if (mmc_ret)
1004 {
1005 dprintf(CRITICAL, "Failure Setting HS200 mode %s\t%d\n",__func__, __LINE__);
1006 return mmc_ret;
1007 }
1008
1009 /* 2. Enable High speed mode */
1010 /* This is needed to set the clock to a low value &
1011 * so that we can switch to hs_timing --> 0x1 */
1012 /* Save the timing value, before changing the clock */
1013 MMC_SAVE_TIMING(host, SDHCI_SDR12_MODE);
1014 sdhci_set_uhs_mode(host, SDHCI_SDR12_MODE);
1015
1016 /* 3. Set HS_TIMING to 0x1 */
1017 mmc_ret = mmc_set_hs_interface(host, card);
1018 if (mmc_ret)
1019 {
1020 dprintf(CRITICAL, "Error adjusting interface speed!:%s\t%d\n", __func__, __LINE__);
1021 return mmc_ret;
1022 }
1023
1024 /*4. Enable DDR mode */
1025 mmc_ret = mmc_set_ddr_mode(host, card);
1026 if (mmc_ret)
1027 {
1028 dprintf(CRITICAL, "Failure setting DDR mode:%s\t%d\n", __func__, __LINE__);
1029 return mmc_ret;
1030 }
1031
1032 /*5. Set hs400 timing */
1033 mmc_ret = mmc_switch_cmd(host, card, MMC_ACCESS_WRITE, MMC_EXT_MMC_HS_TIMING, MMC_HS400_TIMING);
1034
1035 if (mmc_ret)
1036 {
1037 dprintf(CRITICAL, "Switch cmd returned failure %s\t%d\n",__func__, __LINE__);
1038 return mmc_ret;
1039 }
1040
1041 /* 6. Enable SDR104 mode in controller */
1042 /* Save the timing value, before changing the clock */
1043 MMC_SAVE_TIMING(host, MMC_HS400_TIMING);
1044 sdhci_set_uhs_mode(host, SDHCI_SDR104_MODE);
Channagoud Kadabi86756c12014-03-12 17:16:39 -07001045 /*
1046 * Enable HS400 mode
1047 */
1048 sdhci_msm_set_mci_clk(host);
Channagoud Kadabi642ff552014-08-12 21:40:59 -07001049 /* Set the clock back to 400 MHZ */
1050 clock_config_mmc(host->msm_host->slot, SDHCI_CLK_400MHZ);
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -07001051
1052 /* 7. Execute Tuning for hs400 mode */
Channagoud Kadabie106d1f2014-04-25 18:26:26 -07001053 if ((mmc_ret = sdhci_msm_execute_tuning(host, card, width)))
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -07001054 dprintf(CRITICAL, "Tuning for hs400 failed\n");
1055
Channagoud Kadabie632e252014-03-31 15:26:00 -07001056 DBG("\n Enabling HS400 Mode Done\n");
1057
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -07001058 return mmc_ret;
1059}
1060
1061/*
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -07001062 * Function: mmc_host_init
1063 * Arg : mmc device structure
1064 * Return : 0 on success, 1 on Failure
1065 * Flow : Initialize the host contoller
1066 * Set the clock rate to 400 KHZ for init
1067 */
1068static uint8_t mmc_host_init(struct mmc_device *dev)
1069{
1070 uint8_t mmc_ret = 0;
1071
1072 struct sdhci_host *host;
1073 struct mmc_config_data *cfg;
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -07001074 struct sdhci_msm_data *data;
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -07001075
1076 event_t sdhc_event;
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -07001077
1078 host = &dev->host;
1079 cfg = &dev->config;
1080
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -07001081 event_init(&sdhc_event, false, EVENT_FLAG_AUTOUNSIGNAL);
1082
1083 host->base = cfg->sdhc_base;
1084 host->sdhc_event = &sdhc_event;
Aparna Mallavarapue1cdd302014-03-07 07:12:44 +05301085 host->caps.hs400_support = cfg->hs400_support;
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -07001086
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -07001087 data = (struct sdhci_msm_data *) malloc(sizeof(struct sdhci_msm_data));
1088 ASSERT(data);
1089
1090 data->sdhc_event = &sdhc_event;
1091 data->pwrctl_base = cfg->pwrctl_base;
1092 data->pwr_irq = cfg->pwr_irq;
1093 data->slot = cfg->slot;
1094
1095 host->msm_host = data;
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -07001096
Channagoud Kadabi4b2f9672013-08-08 17:44:03 -07001097 /* Initialize any clocks needed for SDC controller */
1098 clock_init_mmc(cfg->slot);
1099
1100 clock_config_mmc(cfg->slot, cfg->max_clk_rate);
1101
Channagoud Kadabie632e252014-03-31 15:26:00 -07001102 /* Configure the CDC clocks needed for emmc storage
1103 * we use slot '1' for emmc
1104 */
1105 if (cfg->slot == 1)
1106 clock_config_cdc(cfg->slot);
1107
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -07001108 /*
1109 * MSM specific sdhc init
1110 */
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -07001111 sdhci_msm_init(host, data);
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -07001112
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -07001113 /*
1114 * Initialize the controller, read the host capabilities
1115 * set power on mode
1116 */
1117 sdhci_init(host);
1118
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -07001119 /* Setup initial freq to 400KHz */
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -07001120 mmc_ret = sdhci_clk_supply(host, SDHCI_CLK_400KHZ);
1121
1122 return mmc_ret;
1123}
1124
1125/*
1126 * Function: mmc identify card
1127 * Arg : host & card structure
1128 * Return : 0 on Success, 1 on Failure
1129 * Flow : Performs card identification process:
1130 * 1. Get card's unique identification number (CID)
1131 * 2. Get(for sd)/set (for mmc) relative card address (RCA)
1132 * 3. Select the card to put it in TRAN state
1133 */
1134static uint32_t mmc_identify_card(struct sdhci_host *host, struct mmc_card *card)
1135{
1136 uint32_t mmc_return = 0;
1137 uint32_t raw_csd[4];
1138
1139 /* Ask card to send its unique card identification (CID) number (CMD2) */
1140 mmc_return = mmc_all_send_cid(host, card);
1141 if (mmc_return) {
1142 dprintf(CRITICAL,"Failure getting card's CID number!\n");
1143 return mmc_return;
1144 }
1145
1146 /* Ask card to send a relative card address (RCA) (CMD3) */
1147 mmc_return = mmc_send_relative_address(host, card);
1148 if (mmc_return) {
1149 dprintf(CRITICAL, "Failure getting card's RCA!\n");
1150 return mmc_return;
1151 }
1152
1153 /* Get card's CSD register (CMD9) */
1154 mmc_return = mmc_send_csd(host, card);
1155 if (mmc_return) {
1156 dprintf(CRITICAL,"Failure getting card's CSD information!\n");
1157 return mmc_return;
1158 }
1159
1160 /* Select the card (CMD7) */
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -07001161 mmc_return = mmc_select_card(host, card);
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -07001162 if (mmc_return) {
1163 dprintf(CRITICAL, "Failure selecting the Card with RCA: %x\n",card->rca);
1164 return mmc_return;
1165 }
1166
1167 /* Set the card status as active */
1168 card->status = MMC_STATUS_ACTIVE;
1169
1170 return 0;
1171}
1172
1173/*
1174 * Function: mmc_reset_card_and_send_op
1175 * Arg : Host & Card structure
1176 * Return : 0 on Success, 1 on Failure
1177 * Flow : Routine to initialize MMC card. It resets a card to idle state,
1178 * verify operating voltage and set the card in ready state.
1179 */
1180static uint32_t mmc_reset_card_and_send_op(struct sdhci_host *host, struct mmc_card *card)
1181{
1182 uint32_t mmc_return = 0;
1183
1184 /* 1. Card Reset - CMD0 */
1185 mmc_return = mmc_reset_card(host);
1186 if (mmc_return) {
1187 dprintf(CRITICAL, "Failure resetting MMC cards!\n");
1188 return mmc_return;
1189 }
1190
1191 /* 2. Card Initialization process */
1192
1193 /*
1194 * Send CMD1 to identify and reject cards that do not match host's VDD range
1195 * profile. Cards sends its OCR register in response.
1196 */
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -07001197
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -07001198 mmc_return = mmc_send_op_cond(host, card);
1199
1200 /* OCR is not received, init could not complete */
1201 if (mmc_return) {
1202 dprintf(CRITICAL, "Failure getting OCR response from MMC Card\n");
1203 return mmc_return;
1204 }
1205
1206 return 0;
1207}
1208
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -07001209static uint32_t mmc_send_app_cmd(struct sdhci_host *host, struct mmc_card *card)
1210{
1211 struct mmc_command cmd = {0};
1212
1213 cmd.cmd_index = CMD55_APP_CMD;
1214 cmd.argument = (card->rca << 16);
1215 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
1216 cmd.resp_type = SDHCI_CMD_RESP_R1;
1217
1218 if (sdhci_send_command(host, &cmd))
1219 {
1220 dprintf(CRITICAL, "Failed Sending CMD55\n");
1221 return 1;
1222 }
1223 return 0;
1224}
1225
1226uint32_t mmc_sd_card_init(struct sdhci_host *host, struct mmc_card *card)
1227{
1228 uint8_t i;
1229 uint32_t mmc_ret;
1230 struct mmc_command cmd;
1231
1232 memset((struct mmc_command *)&cmd, 0, sizeof(struct mmc_command));
1233
1234 /* Use the SD card RCA 0x0 during init */
1235 card->rca = SD_CARD_RCA;
1236
1237 /* Send CMD8 for voltage check*/
1238 for (i = 0 ;i < SD_CMD8_MAX_RETRY; i++)
1239 {
1240 cmd.cmd_index = CMD8_SEND_IF_COND;
1241 cmd.argument = MMC_SD_HC_VOLT_SUPPLIED;
1242 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
1243 cmd.resp_type = SDHCI_CMD_RESP_R7;
1244
1245 if (sdhci_send_command(host, &cmd))
1246 {
1247 dprintf(CRITICAL, "The response for CMD8 does not match the supplied value\n");
1248 return 1;
1249 }
1250 else
1251 {
1252 /* If the command response echos the voltage back */
1253 if (cmd.resp[0] == MMC_SD_HC_VOLT_SUPPLIED)
1254 break;
1255 }
1256 /* As per SDCC the spec try for max three times with
1257 * 1 ms delay
1258 */
1259 mdelay(1);
1260 }
1261
1262 if (i == SD_CMD8_MAX_RETRY && (cmd.resp[0] != MMC_SD_HC_VOLT_SUPPLIED))
1263 {
1264 dprintf(CRITICAL, "Error: CMD8 response timed out\n");
1265 return 1;
1266 }
1267
1268 /* Send ACMD41 for OCR */
1269 for (i = 0; i < SD_ACMD41_MAX_RETRY; i++)
1270 {
1271 /* Send APP_CMD before ACMD41*/
1272 if (mmc_send_app_cmd(host, card))
1273 {
1274 dprintf(CRITICAL, "Failed sending App command\n");
1275 return 1;
1276 }
1277
1278 /* APP_CMD is successful, send ACMD41 now */
1279 cmd.cmd_index = ACMD41_SEND_OP_COND;
1280 cmd.argument = MMC_SD_OCR | MMC_SD_HC_HCS;
1281 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
1282 cmd.resp_type = SDHCI_CMD_RESP_R3;
1283
1284 if (sdhci_send_command(host, &cmd))
1285 {
1286 dprintf(CRITICAL, "Failure sending ACMD41\n");
1287 return 1;
1288 }
1289 else
1290 {
1291 if (cmd.resp[0] & MMC_SD_DEV_READY)
1292 {
1293 if (cmd.resp[0] & (1 << 30))
1294 card->type = MMC_CARD_TYPE_SDHC;
1295 else
1296 card->type = MMC_CARD_TYPE_STD_SD;
1297
1298 break;
1299 }
1300 }
1301 /*
1302 * As per SDCC spec try for max 1 second
1303 */
1304 mdelay(50);
1305 }
1306
1307 if (i == SD_ACMD41_MAX_RETRY && !(cmd.resp[0] & MMC_SD_DEV_READY))
1308 {
1309 dprintf(CRITICAL, "Error: ACMD41 response timed out\n");
1310 return 1;
1311 }
1312
1313 return 0;
1314}
1315
1316/*
1317 * Function to read SD card information from SD status
1318 */
1319static uint32_t mmc_sd_get_card_ssr(struct sdhci_host *host, struct mmc_card *card)
1320{
1321 BUF_DMA_ALIGN(raw_sd_status, 64);
1322 struct mmc_command cmd = {0};
1323 uint32_t sd_status[16];
1324 uint32_t *status = sd_status;
1325 uint32_t au_size;
1326 int i;
1327 int j;
1328
1329 if (mmc_send_app_cmd(host, card))
1330 {
1331 dprintf(CRITICAL, "Failed sending App command\n");
1332 return 1;
1333 }
1334
1335 cmd.cmd_index = ACMD13_SEND_SD_STATUS;
1336 cmd.argument = 0x0;
1337 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
1338 cmd.resp_type = SDHCI_CMD_RESP_R2;
1339 cmd.trans_mode = SDHCI_MMC_READ;
1340 cmd.data_present = 0x1;
1341 cmd.data.data_ptr = raw_sd_status;
1342 cmd.data.num_blocks = 0x1;
1343 cmd.data.blk_sz = 0x40;
1344
1345 /* send command */
1346 if (sdhci_send_command(host, &cmd))
1347 return 1;
1348
1349 memcpy(sd_status, raw_sd_status, sizeof(sd_status));
1350
1351 for (i = 15, j = 0; i >=0 ; i--, j++)
1352 sd_status[i] = swap_endian32(sd_status[j]);
1353
1354 au_size = UNPACK_BITS(status, MMC_SD_AU_SIZE_BIT, MMC_SD_AU_SIZE_LEN, 32);
1355 /* Card AU size in sectors */
1356 card->ssr.au_size = 1 << (au_size + 4);
1357 card->ssr.num_aus = UNPACK_BITS(status, MMC_SD_ERASE_SIZE_BIT, MMC_SD_ERASE_SIZE_LEN, 32);
1358
1359 return 0;
1360}
1361
1362/*
1363 * Function to read the SD CARD configuration register
1364 */
1365static uint32_t mmc_sd_get_card_scr(struct sdhci_host *host, struct mmc_card *card)
1366{
1367 BUF_DMA_ALIGN(scr_resp, 8);
1368 struct mmc_command cmd = {0};
1369 uint32_t raw_scr[2];
1370
1371 /* Now read the SCR register */
1372 /* Send APP_CMD before ACMD51*/
1373 if (mmc_send_app_cmd(host, card))
1374 {
1375 dprintf(CRITICAL, "Failed sending App command\n");
1376 return 1;
1377 }
1378
1379 cmd.cmd_index = ACMD51_READ_CARD_SCR;
1380 cmd.argument = 0x0;
1381 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
1382 cmd.resp_type = SDHCI_CMD_RESP_R1;
1383 cmd.trans_mode = SDHCI_MMC_READ;
1384 cmd.data_present = 0x1;
1385 cmd.data.data_ptr = scr_resp;
1386 cmd.data.num_blocks = 0x1;
1387 cmd.data.blk_sz = 0x8;
1388
1389 /* send command */
1390 if (sdhci_send_command(host, &cmd))
1391 return 1;
1392
1393 memcpy(raw_scr, scr_resp, sizeof(raw_scr));
1394
1395 card->raw_scr[0] = swap_endian32(raw_scr[0]);
1396 card->raw_scr[1] = swap_endian32(raw_scr[1]);
1397
1398 /*
1399 * Parse & Populate the SCR data as per sdcc spec
1400 */
1401 card->scr.bus_widths = (card->raw_scr[0] & SD_SCR_BUS_WIDTH_MASK) >> SD_SCR_BUS_WIDTH;
1402 card->scr.cmd23_support = (card->raw_scr[0] & SD_SCR_CMD23_SUPPORT);
1403 card->scr.sd_spec = (card->raw_scr[0] & SD_SCR_SD_SPEC_MASK) >> SD_SCR_SD_SPEC;
1404 card->scr.sd3_spec = (card->raw_scr[0] & SD_SCR_SD_SPEC3_MASK) >> SD_SCR_SD_SPEC3;
1405
1406 return 0;
1407}
1408
1409/*
1410 * Function: mmc_set_sd_bus_width
1411 * Arg : host, device structure & width
1412 * Return : 0 on Success, 1 on Failure
1413 * Flow : Set the bus width for the card
1414 */
1415uint32_t mmc_sd_set_bus_width(struct sdhci_host *host, struct mmc_card *card, uint8_t width)
1416{
1417 struct mmc_command cmd = {0};
1418
1419 /* Send APP_CMD before ACMD6*/
1420 if (mmc_send_app_cmd(host, card))
1421 {
1422 dprintf(CRITICAL, "Failed sending App command\n");
1423 return 1;
1424 }
1425
1426 cmd.cmd_index = ACMD6_SET_BUS_WIDTH;
1427 cmd.argument = (width == DATA_BUS_WIDTH_4BIT) ? (1<<1) : 0;
1428 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
1429 cmd.resp_type = SDHCI_CMD_RESP_R1;
1430
1431 /* send command */
1432 if (sdhci_send_command(host, &cmd))
1433 return 1;
1434
1435 return 0;
1436}
1437
1438uint32_t mmc_sd_set_hs(struct sdhci_host *host, struct mmc_card *card)
1439{
1440 struct mmc_command cmd = {0};
1441 BUF_DMA_ALIGN(switch_resp, 64);
1442
1443 cmd.cmd_index = CMD6_SWITCH_FUNC;
1444 cmd.argument = MMC_SD_SWITCH_HS;
1445 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
1446 cmd.resp_type = SDHCI_CMD_RESP_R1;
1447 cmd.trans_mode = SDHCI_MMC_READ;
1448 cmd.data_present = 0x1;
1449 cmd.data.data_ptr = switch_resp;
1450 cmd.data.num_blocks = 0x1;
1451 cmd.data.blk_sz = 0x40;
1452
1453 /* send command */
1454 if (sdhci_send_command(host, &cmd))
1455 return 1;
1456
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -07001457 /* Set the SDR25 mode in controller*/
1458 sdhci_set_uhs_mode(host, SDHCI_SDR25_MODE);
1459
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -07001460 return 0;
1461}
1462
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -07001463/*
1464 * Function: mmc_init_card
1465 * Arg : mmc device structure
1466 * Return : 0 on Success, 1 on Failure
1467 * Flow : Performs initialization and identification of eMMC cards connected
1468 * to the host.
1469 */
1470
1471static uint32_t mmc_card_init(struct mmc_device *dev)
1472{
1473 uint32_t mmc_return = 0;
1474 uint32_t status;
1475 uint8_t bus_width = 0;
1476
1477 struct sdhci_host *host;
1478 struct mmc_card *card;
1479 struct mmc_config_data *cfg;
1480
1481 host = &dev->host;
1482 card = &dev->card;
1483 cfg = &dev->config;
1484
1485 /* Initialize MMC card structure */
1486 card->status = MMC_STATUS_INACTIVE;
1487
1488 /* TODO: Get the OCR params from target */
1489 card->ocr = MMC_OCR_27_36 | MMC_OCR_SEC_MODE;
1490
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -07001491 /* Initialize the internal MMC */
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -07001492 mmc_return = mmc_reset_card_and_send_op(host, card);
1493 if (mmc_return)
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -07001494 {
1495 dprintf(CRITICAL, "MMC card failed to respond, try for SD card\n");
1496 /* Reset the card & get the OCR */
1497 mmc_return = mmc_sd_card_init(host, card);
1498 if (mmc_return)
1499 {
1500 dprintf(CRITICAL, "Failed to initialize SD card\n");
1501 return mmc_return;
1502 }
1503 }
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -07001504
1505 /* Identify (CMD2, CMD3 & CMD9) and select the card (CMD7) */
1506 mmc_return = mmc_identify_card(host, card);
1507 if (mmc_return)
1508 return mmc_return;
1509
1510 /* set interface speed */
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -07001511 if (MMC_CARD_SD(card))
1512 {
1513 mmc_return = mmc_sd_set_hs(host, card);
1514 if (mmc_return)
1515 {
1516 dprintf(CRITICAL, "Failed to set HS for SD card\n");
1517 return mmc_return;
1518 }
1519 }
1520 else
1521 {
1522 mmc_return = mmc_set_hs_interface(host, card);
1523 if (mmc_return) {
1524 dprintf(CRITICAL, "Error adjusting interface speed!\n");
1525 return mmc_return;
1526 }
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -07001527 }
1528
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -07001529 /* Now get the extended CSD for the card */
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -07001530 if (MMC_CARD_MMC(card))
1531 {
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -07001532 /* For MMC cards, also get the extended csd */
1533 mmc_return = mmc_get_ext_csd(host, card);
1534
1535 if (mmc_return) {
1536 dprintf(CRITICAL, "Failure getting card's ExtCSD information!\n");
1537 return mmc_return;
1538 }
1539 }
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -07001540 else
1541 {
1542 /*Read SCR for sd card */
1543 if (mmc_sd_get_card_scr(host, card))
1544 {
1545 dprintf(CRITICAL, "Failure getting card's SCR register\n");
1546 return 1;
1547 }
1548 /* Read SSR for the SD card */
1549 if (mmc_sd_get_card_ssr(host, card))
1550 {
1551 dprintf(CRITICAL, "Failed to get SSR from the card\n");
1552 return 1;
1553 }
1554 }
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -07001555
1556 /* Decode and save the CSD register */
1557 mmc_return = mmc_decode_and_save_csd(card);
1558 if (mmc_return) {
1559 dprintf(CRITICAL, "Failure decoding card's CSD information!\n");
1560 return mmc_return;
1561 }
1562
1563
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -07001564 if (MMC_CARD_MMC(card))
1565 {
1566 /* Set the bus width based on host, target capbilities */
1567 if (cfg->bus_width == DATA_BUS_WIDTH_8BIT && host->caps.bus_width_8bit)
1568 bus_width = DATA_BUS_WIDTH_8BIT;
1569 /*
1570 * Host contoller by default supports 4 bit & 1 bit mode.
1571 * No need to check for host support here
1572 */
1573 else if (cfg->bus_width == DATA_BUS_WIDTH_4BIT)
1574 bus_width = DATA_BUS_WIDTH_4BIT;
1575 else
1576 bus_width = DATA_BUS_WIDTH_1BIT;
1577
1578 /* Set 4/8 bit SDR bus width in controller */
1579 mmc_return = sdhci_set_bus_width(host, bus_width);
1580
1581 if (mmc_return) {
1582 dprintf(CRITICAL, "Failed to set bus width for host controller\n");
1583 return 1;
1584 }
1585
1586 /* Enable high speed mode in the follwing order:
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -07001587 * 1. HS400 mode if supported by host & card
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -07001588 * 1. HS200 mode if supported by host & card
1589 * 2. DDR mode host, if supported by host & card
1590 * 3. Use normal speed mode with supported bus width
1591 */
Channagoud Kadabie9168e82014-01-28 21:33:34 -08001592 if (host->caps.hs400_support && mmc_card_supports_hs400_mode(card))
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -07001593 {
Channagoud Kadabiee5b3242014-06-25 11:15:06 -07001594 dprintf(INFO, "SDHC Running in HS400 mode\n");
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -07001595 mmc_return = mmc_set_hs400_mode(host, card, bus_width);
1596 if (mmc_return)
1597 {
1598 dprintf(CRITICAL, "Failure to set HS400 mode for Card(RCA:%x)\n",
1599 card->rca);
1600 return mmc_return;
1601 }
1602 }
Channagoud Kadabie9168e82014-01-28 21:33:34 -08001603 else if (host->caps.sdr104_support && mmc_card_supports_hs200_mode(card))
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -07001604 {
Channagoud Kadabiee5b3242014-06-25 11:15:06 -07001605 dprintf(INFO, "SDHC Running in HS200 mode\n");
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -07001606 mmc_return = mmc_set_hs200_mode(host, card, bus_width);
1607
1608 if (mmc_return) {
1609 dprintf(CRITICAL, "Failure to set HS200 mode for Card(RCA:%x)\n",
1610 card->rca);
1611 return mmc_return;
1612 }
Channagoud Kadabie9168e82014-01-28 21:33:34 -08001613 } else if (host->caps.ddr_support && mmc_card_supports_ddr_mode(card)) {
Channagoud Kadabiee5b3242014-06-25 11:15:06 -07001614 dprintf(INFO, "SDHC Running in DDR mode\n");
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -07001615 mmc_return = mmc_set_ddr_mode(host, card);
1616
1617 if (mmc_return) {
1618 dprintf(CRITICAL, "Failure to set DDR mode for Card(RCA:%x)\n",
1619 card->rca);
1620 return mmc_return;
1621 }
1622 } else {
Channagoud Kadabiee5b3242014-06-25 11:15:06 -07001623 dprintf(INFO, "SDHC Running in High Speed mode\n");
1624 /* Set HS_TIMING mode */
1625 mmc_return = mmc_set_hs_interface(host, card);
1626 if (mmc_return) {
1627 dprintf(CRITICAL, "Failure to enalbe HS mode for Card(RCA:%x)\n",
1628 card->rca);
1629 return mmc_return;
1630 }
1631 /* Set wide bus mode */
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -07001632 mmc_return = mmc_set_bus_width(host, card, bus_width);
1633 if (mmc_return) {
1634 dprintf(CRITICAL, "Failure to set wide bus for Card(RCA:%x)\n",
1635 card->rca);
1636 return mmc_return;
1637 }
1638 }
1639 }
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -07001640 else
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -07001641 {
1642 /* Check the supported bus width for the card from SCR register */
1643 if (card->scr.bus_widths & SD_SCR_WIDTH_4BIT)
1644 bus_width = DATA_BUS_WIDTH_4BIT;
1645 else
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -07001646 bus_width = DATA_BUS_WIDTH_1BIT;
1647
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -07001648 mmc_return = mmc_sd_set_bus_width(host, card, bus_width);
1649 if (mmc_return)
1650 {
1651 dprintf(CRITICAL, "Failed to set bus width for the card\n");
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -07001652 return mmc_return;
1653 }
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -07001654
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -07001655 /* Set bit SDR bus width in controller */
1656 mmc_return = sdhci_set_bus_width(host, bus_width);
1657 if (mmc_return)
1658 {
1659 dprintf(CRITICAL, "Failed to set bus width for host controller\n");
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -07001660 return mmc_return;
1661 }
1662 }
1663
1664
Channagoud Kadabi96c629e2013-09-10 14:21:30 -07001665 card->block_size = MMC_BLK_SZ;
1666
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -07001667 return mmc_return;
1668}
1669
1670/*
1671 * Function: mmc display csd
1672 * Arg : None
1673 * Return : None
1674 * Flow : Displays the csd information
1675 */
1676static void mmc_display_csd(struct mmc_card *card)
1677{
1678 dprintf(SPEW, "erase_grpsize: %d\n", card->csd.erase_grp_size);
1679 dprintf(SPEW, "erase_grpmult: %d\n", card->csd.erase_grp_mult);
1680 dprintf(SPEW, "wp_grpsize: %d\n", card->csd.wp_grp_size);
1681 dprintf(SPEW, "wp_grpen: %d\n", card->csd.wp_grp_enable);
1682 dprintf(SPEW, "perm_wp: %d\n", card->csd.perm_wp);
1683 dprintf(SPEW, "temp_wp: %d\n", card->csd.temp_wp);
1684}
1685
1686/*
1687 * Function: mmc_init
1688 * Arg : MMC configuration data
1689 * Return : Pointer to mmc device
1690 * Flow : Entry point to MMC boot process
1691 * Initialize the sd host controller
1692 * Initialize the mmc card
1693 * Set the clock & high speed mode
1694 */
1695struct mmc_device *mmc_init(struct mmc_config_data *data)
1696{
1697 uint8_t mmc_ret = 0;
1698 struct mmc_device *dev;
1699
1700 dev = (struct mmc_device *) malloc (sizeof(struct mmc_device));
1701
1702 if (!dev) {
1703 dprintf(CRITICAL, "Error allocating mmc device\n");
1704 return NULL;
1705 }
1706
1707 ASSERT(data);
1708
1709 memcpy((void*)&dev->config, (void*)data, sizeof(struct mmc_config_data));
1710
1711 memset((struct mmc_card *)&dev->card, 0, sizeof(struct mmc_card));
1712
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -07001713 /* Initialize the host & clock */
1714 dprintf(SPEW, " Initializing MMC host data structure and clock!\n");
1715
1716 mmc_ret = mmc_host_init(dev);
1717 if (mmc_ret) {
1718 dprintf(CRITICAL, "Error Initializing MMC host : %u\n", mmc_ret);
1719 return NULL;
1720 }
1721
1722 /* Initialize and identify cards connected to host */
1723 mmc_ret = mmc_card_init(dev);
1724 if (mmc_ret) {
1725 dprintf(CRITICAL, "Failed detecting MMC/SDC @ slot%d\n",
1726 dev->config.slot);
1727 return NULL;
1728 }
1729
1730 dprintf(INFO, "Done initialization of the card\n");
1731
1732 mmc_display_csd(&dev->card);
1733
1734 return dev;
1735}
1736
Channagoud Kadabi14fd2052013-10-17 18:00:50 -07001737static uint32_t mmc_parse_response(uint32_t resp)
1738{
1739 /* Trying to write beyond card capacity */
1740 if (resp & MMC_R1_ADDR_OUT_OF_RANGE) {
1741 dprintf(CRITICAL, "Attempting to read or write beyond the Device capacity\n");
1742 return 1;
1743 }
1744
1745 /* Misaligned address not matching block length */
1746 if (resp & MMC_R1_ADDR_ERR) {
1747 dprintf(CRITICAL, "The misaligned address did not match the block length used\n");
1748 return 1;
1749 }
1750
1751 /* Invalid block length */
1752 if (resp & MMC_R1_BLOCK_LEN_ERR) {
1753 dprintf(CRITICAL, "The transferred bytes does not match the block length\n");
1754 return 1;
1755 }
1756
1757 /* Tried to program write protected block */
1758 if (resp & MMC_R1_WP_VIOLATION) {
1759 dprintf(CRITICAL, "Attempt to program a write protected block\n");
1760 return 1;
1761 }
1762
1763 /* card controller error */
1764 if (resp & MMC_R1_CC_ERROR) {
1765 dprintf(CRITICAL, "Device error occurred, which is not related to the host command\n");
1766 return 1;
1767 }
1768
1769 /* Generic error */
1770 if (resp & MMC_R1_GENERIC_ERR) {
1771 dprintf(CRITICAL, "A generic Device error\n");
1772 return 1;
1773 }
1774
1775 /* Finally check for card in TRAN state */
1776 if (MMC_CARD_STATUS(resp) != MMC_TRAN_STATE) {
1777 dprintf(CRITICAL, "MMC card is not in TRAN state\n");
1778 return 1;
1779 }
1780
1781 return 0;
1782}
1783
Channagoud Kadabia9b74782014-04-09 16:35:16 -07001784static uint32_t mmc_stop_command(struct mmc_device *dev)
1785{
1786 struct mmc_command cmd;
1787 uint32_t mmc_ret = 0;
1788
1789 memset((struct mmc_command *)&cmd, 0, sizeof(struct mmc_command));
1790
1791 cmd.cmd_index = CMD12_STOP_TRANSMISSION;
1792 cmd.argument = (dev->card.rca << 16);
1793 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
1794 cmd.resp_type = SDHCI_CMD_RESP_R1;
1795
1796 mmc_ret = sdhci_send_command(&dev->host, &cmd);
1797 if(mmc_ret)
1798 {
1799 dprintf(CRITICAL, "Failed to send stop command\n");
1800 return mmc_ret;
1801 }
1802
1803 /* Response contains 32 bit Card status.
1804 * Parse the errors & provide relevant information */
1805
1806 return mmc_parse_response(cmd.resp[0]);
1807}
1808
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -07001809/*
1810 * Function: mmc sdhci read
1811 * Arg : mmc device structure, block address, number of blocks & destination
1812 * Return : 0 on Success, non zero on success
1813 * Flow : Fill in the command structure & send the command
1814 */
1815uint32_t mmc_sdhci_read(struct mmc_device *dev, void *dest,
1816 uint64_t blk_addr, uint32_t num_blocks)
1817{
1818 uint32_t mmc_ret = 0;
1819 struct mmc_command cmd;
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -07001820 struct mmc_card *card = &dev->card;
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -07001821
1822 memset((struct mmc_command *)&cmd, 0, sizeof(struct mmc_command));
1823
1824 /* CMD17/18 Format:
1825 * [31:0] Data Address
1826 */
1827 if (num_blocks == 1)
1828 cmd.cmd_index = CMD17_READ_SINGLE_BLOCK;
1829 else
1830 cmd.cmd_index = CMD18_READ_MULTIPLE_BLOCK;
1831
Channagoud Kadabi7e72e632013-12-20 11:17:45 -08001832 /*
1833 * Standard emmc cards use byte mode addressing
1834 * convert the block address to byte address before
1835 * sending the command
1836 */
1837 if (card->type == MMC_TYPE_STD_MMC)
1838 cmd.argument = blk_addr * card->block_size;
1839 else
1840 cmd.argument = blk_addr;
1841
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -07001842 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
1843 cmd.resp_type = SDHCI_CMD_RESP_R1;
1844 cmd.trans_mode = SDHCI_MMC_READ;
1845 cmd.data_present = 0x1;
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -07001846
1847 /* Use CMD23 If card supports CMD23:
1848 * For SD card use the value read from SCR register
1849 * For emmc by default use CMD23.
1850 * Also as per SDCC spec always use CMD23 to stop
1851 * multiblock read/write if UHS (Ultra High Speed) is
1852 * enabled
1853 */
1854 if (MMC_CARD_SD(card))
1855 cmd.cmd23_support = dev->card.scr.cmd23_support;
1856 else
1857 cmd.cmd23_support = 0x1;
1858
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -07001859 cmd.data.data_ptr = dest;
1860 cmd.data.num_blocks = num_blocks;
1861
1862 /* send command */
1863 mmc_ret = sdhci_send_command(&dev->host, &cmd);
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -07001864
Channagoud Kadabia9b74782014-04-09 16:35:16 -07001865 /* For multi block read failures send stop command */
1866 if (mmc_ret && num_blocks > 1)
Channagoud Kadabi14fd2052013-10-17 18:00:50 -07001867 {
Channagoud Kadabia9b74782014-04-09 16:35:16 -07001868 return mmc_stop_command(dev);
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -07001869 }
1870
Channagoud Kadabia9b74782014-04-09 16:35:16 -07001871 /*
1872 * Response contains 32 bit Card status.
1873 * Parse the errors & provide relevant information
1874 */
1875 return mmc_parse_response(cmd.resp[0]);
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -07001876}
1877
1878/*
1879 * Function: mmc sdhci write
1880 * Arg : mmc device structure, block address, number of blocks & source
1881 * Return : 0 on Success, non zero on success
1882 * Flow : Fill in the command structure & send the command
1883 */
1884uint32_t mmc_sdhci_write(struct mmc_device *dev, void *src,
1885 uint64_t blk_addr, uint32_t num_blocks)
1886{
1887 uint32_t mmc_ret = 0;
1888 struct mmc_command cmd;
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -07001889 struct mmc_card *card = &dev->card;
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -07001890
1891 memset((struct mmc_command *)&cmd, 0, sizeof(struct mmc_command));
1892
1893 /* CMD24/25 Format:
1894 * [31:0] Data Address
1895 */
1896
1897 if (num_blocks == 1)
1898 cmd.cmd_index = CMD24_WRITE_SINGLE_BLOCK;
1899 else
1900 cmd.cmd_index = CMD25_WRITE_MULTIPLE_BLOCK;
1901
Channagoud Kadabi7e72e632013-12-20 11:17:45 -08001902 /*
1903 * Standard emmc cards use byte mode addressing
1904 * convert the block address to byte address before
1905 * sending the command
1906 */
1907 if (card->type == MMC_TYPE_STD_MMC)
1908 cmd.argument = blk_addr * card->block_size;
1909 else
1910 cmd.argument = blk_addr;
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -07001911 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
1912 cmd.resp_type = SDHCI_CMD_RESP_R1;
1913 cmd.trans_mode = SDHCI_MMC_WRITE;
Channagoud Kadabi9b8f8fc2013-07-26 12:02:49 -07001914
1915 /* Use CMD23 If card supports CMD23:
1916 * For SD card use the value read from SCR register
1917 * For emmc by default use CMD23.
1918 * Also as per SDCC spec always use CMD23 to stop
1919 * multiblock read/write if UHS (Ultra High Speed) is
1920 * enabled
1921 */
1922 if (MMC_CARD_SD(card))
1923 cmd.cmd23_support = dev->card.scr.cmd23_support;
1924 else
1925 cmd.cmd23_support = 0x1;
1926
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -07001927 cmd.data_present = 0x1;
1928 cmd.data.data_ptr = src;
1929 cmd.data.num_blocks = num_blocks;
1930
1931 /* send command */
1932 mmc_ret = sdhci_send_command(&dev->host, &cmd);
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -07001933
Channagoud Kadabia9b74782014-04-09 16:35:16 -07001934 /* For multi block write failures send stop command */
1935 if (mmc_ret && num_blocks > 1)
Channagoud Kadabi14fd2052013-10-17 18:00:50 -07001936 {
Channagoud Kadabia9b74782014-04-09 16:35:16 -07001937 return mmc_stop_command(dev);
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -07001938 }
1939
Channagoud Kadabia9b74782014-04-09 16:35:16 -07001940 /*
1941 * Response contains 32 bit Card status.
1942 * Parse the errors & provide relevant information
1943 */
1944 return mmc_parse_response(cmd.resp[0]);
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -07001945}
Channagoud Kadabi003171e2013-05-29 15:21:12 -07001946
1947/*
1948 * Send the erase group start address using CMD35
1949 */
1950static uint32_t mmc_send_erase_grp_start(struct mmc_device *dev, uint32_t erase_start)
1951{
1952 struct mmc_command cmd;
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -07001953 struct mmc_card *card = &dev->card;
Channagoud Kadabi003171e2013-05-29 15:21:12 -07001954
1955 memset((struct mmc_command *)&cmd, 0, sizeof(struct mmc_command));
1956
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -07001957 if (MMC_CARD_MMC(card))
1958 cmd.cmd_index = CMD35_ERASE_GROUP_START;
1959 else
1960 cmd.cmd_index = CMD32_ERASE_WR_BLK_START;
1961
vijay kumar1129e4e2014-03-28 11:24:48 +05301962 /*
1963 * Standard emmc cards use byte mode addressing
1964 * convert the block address to byte address before
1965 * sending the command
1966 */
1967 if (card->type == MMC_TYPE_STD_MMC)
1968 cmd.argument = erase_start * card->block_size;
1969 else
1970 cmd.argument = erase_start;
Channagoud Kadabi003171e2013-05-29 15:21:12 -07001971 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
1972 cmd.resp_type = SDHCI_CMD_RESP_R1;
1973
1974 /* send command */
1975 if (sdhci_send_command(&dev->host, &cmd))
1976 return 1;
1977
1978 /*
1979 * CMD35 on failure returns address out of range error
1980 */
1981 if (MMC_ADDR_OUT_OF_RANGE(cmd.resp[0]))
1982 {
1983 dprintf(CRITICAL, "Address for CMD35 is out of range\n");
1984 return 1;
1985 }
1986
1987 return 0;
1988}
1989
1990/*
1991 * Send the erase group end address using CMD36
1992 */
1993static uint32_t mmc_send_erase_grp_end(struct mmc_device *dev, uint32_t erase_end)
1994{
1995 struct mmc_command cmd;
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -07001996 struct mmc_card *card = &dev->card;
Channagoud Kadabi003171e2013-05-29 15:21:12 -07001997
1998 memset((struct mmc_command *)&cmd, 0, sizeof(struct mmc_command));
1999
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -07002000 if (MMC_CARD_MMC(card))
2001 cmd.cmd_index = CMD36_ERASE_GROUP_END;
2002 else
2003 cmd.cmd_index = CMD33_ERASE_WR_BLK_END;
2004
vijay kumar1129e4e2014-03-28 11:24:48 +05302005 /*
2006 * Standard emmc cards use byte mode addressing
2007 * convert the block address to byte address before
2008 * sending the command
2009 */
2010 if (card->type == MMC_TYPE_STD_MMC)
2011 cmd.argument = erase_end * card->block_size;
2012 else
2013 cmd.argument = erase_end;
Channagoud Kadabi003171e2013-05-29 15:21:12 -07002014 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
2015 cmd.resp_type = SDHCI_CMD_RESP_R1;
2016
2017 /* send command */
2018 if (sdhci_send_command(&dev->host, &cmd))
2019 return 1;
2020
2021 /*
2022 * CMD3 on failure returns address out of range error
2023 */
2024 if (MMC_ADDR_OUT_OF_RANGE(cmd.resp[0]))
2025 {
2026 dprintf(CRITICAL, "Address for CMD36 is out of range\n");
2027 return 1;
2028 }
2029
2030 return 0;
2031}
2032
2033/*
2034 * Send the erase CMD38, to erase the selected erase groups
2035 */
Channagoud Kadabie86a40b2014-03-12 17:48:51 -07002036static uint32_t mmc_send_erase(struct mmc_device *dev, uint64_t erase_timeout)
Channagoud Kadabi003171e2013-05-29 15:21:12 -07002037{
2038 struct mmc_command cmd;
2039 uint32_t status;
Channagoud Kadabi6d1751b2013-07-05 13:03:52 -07002040 uint32_t retry = 0;
Channagoud Kadabi003171e2013-05-29 15:21:12 -07002041
2042 memset((struct mmc_command *)&cmd, 0, sizeof(struct mmc_command));
2043
2044 cmd.cmd_index = CMD38_ERASE;
2045 cmd.argument = 0x00000000;
2046 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
2047 cmd.resp_type = SDHCI_CMD_RESP_R1B;
Channagoud Kadabie86a40b2014-03-12 17:48:51 -07002048 cmd.cmd_timeout = erase_timeout;
Channagoud Kadabi003171e2013-05-29 15:21:12 -07002049
2050 /* send command */
2051 if (sdhci_send_command(&dev->host, &cmd))
2052 return 1;
2053
2054 do
2055 {
2056 if (mmc_get_card_status(&dev->host, &dev->card, &status))
2057 {
2058 dprintf(CRITICAL, "Failed to get card status after erase\n");
2059 return 1;
2060 }
2061 /* Check if the response of erase command has eras skip status set */
2062 if (status & MMC_R1_WP_ERASE_SKIP)
2063 dprintf(CRITICAL, "Write Protect set for the region, only partial space was erased\n");
2064
2065 retry++;
2066 udelay(1000);
2067 if (retry == MMC_MAX_CARD_STAT_RETRY)
2068 {
2069 dprintf(CRITICAL, "Card status check timed out after sending erase command\n");
2070 return 1;
2071 }
2072 } while(!(status & MMC_READY_FOR_DATA) || (MMC_CARD_STATUS(status) == MMC_PROG_STATE));
2073
2074
2075 return 0;
2076}
2077
2078
2079/*
2080 * Function: mmc sdhci erase
2081 * Arg : mmc device structure, block address and length
2082 * Return : 0 on Success, non zero on failure
2083 * Flow : Fill in the command structure & send the command
2084 */
2085uint32_t mmc_sdhci_erase(struct mmc_device *dev, uint32_t blk_addr, uint64_t len)
2086{
2087 uint32_t erase_unit_sz = 0;
2088 uint32_t erase_start;
2089 uint32_t erase_end;
2090 uint32_t blk_end;
2091 uint32_t num_erase_grps;
Channagoud Kadabie86a40b2014-03-12 17:48:51 -07002092 uint64_t erase_timeout = 0;
Channagoud Kadabi003171e2013-05-29 15:21:12 -07002093 uint32_t *out;
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -07002094 struct mmc_card *card;
2095
2096
2097 card = &dev->card;
Channagoud Kadabi003171e2013-05-29 15:21:12 -07002098
2099 /*
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -07002100 * Calculate the erase unit size,
2101 * 1. Based on emmc 4.5 spec for emmc card
2102 * 2. Use SD Card Status info for SD cards
Channagoud Kadabi003171e2013-05-29 15:21:12 -07002103 */
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -07002104 if (MMC_CARD_MMC(card))
2105 {
2106 /*
2107 * Calculate the erase unit size as per the emmc specification v4.5
2108 */
2109 if (dev->card.ext_csd[MMC_ERASE_GRP_DEF])
2110 erase_unit_sz = (MMC_HC_ERASE_MULT * dev->card.ext_csd[MMC_HC_ERASE_GRP_SIZE]) / MMC_BLK_SZ;
2111 else
2112 erase_unit_sz = (dev->card.csd.erase_grp_size + 1) * (dev->card.csd.erase_grp_mult + 1);
2113 }
Channagoud Kadabi003171e2013-05-29 15:21:12 -07002114 else
Channagoud Kadabi4d13b2c2013-06-18 12:43:29 -07002115 erase_unit_sz = dev->card.ssr.au_size * dev->card.ssr.num_aus;
2116
Channagoud Kadabi003171e2013-05-29 15:21:12 -07002117
2118 /* Convert length in blocks */
2119 len = len / MMC_BLK_SZ;
2120
2121 if (len < erase_unit_sz)
2122 {
2123 dprintf(CRITICAL, "Requested length is less than min erase group size\n");
2124 return 1;
2125 }
2126
2127 /* Calculate erase groups based on the length in blocks */
2128 num_erase_grps = len / erase_unit_sz;
2129
2130 /* Start address of the erase range */
2131 erase_start = blk_addr;
2132
2133 /* Last address of the erase range */
2134 erase_end = blk_addr + ((num_erase_grps - 1) * erase_unit_sz);
2135
2136 /* Boundary check for overlap */
2137 blk_end = blk_addr + len;
2138
2139 if (erase_end > blk_end)
2140 {
2141 dprintf(CRITICAL, "The erase group overlaps the max requested for erase\n");
2142 erase_end -= erase_unit_sz;
2143 }
2144
2145 /* Send CMD35 for erase group start */
2146 if (mmc_send_erase_grp_start(dev, erase_start))
2147 {
2148 dprintf(CRITICAL, "Failed to send erase grp start address\n");
2149 return 1;
2150 }
2151
2152 /* Send CMD36 for erase group end */
2153 if (mmc_send_erase_grp_end(dev, erase_end))
2154 {
2155 dprintf(CRITICAL, "Failed to send erase grp end address\n");
2156 return 1;
2157 }
2158
Channagoud Kadabie86a40b2014-03-12 17:48:51 -07002159 /*
2160 * As per emmc 4.5 spec section 7.4.27, calculate the erase timeout
2161 * erase_timeout = 300 * ERASE_TIMEOUT_MULT * num_erase_grps
2162 */
2163 erase_timeout = (300 * card->ext_csd[MMC_ERASE_TIMEOUT_MULT] * num_erase_grps);
2164
Channagoud Kadabi003171e2013-05-29 15:21:12 -07002165 /* Send CMD38 to perform erase */
Channagoud Kadabie86a40b2014-03-12 17:48:51 -07002166 if (mmc_send_erase(dev, erase_timeout))
Channagoud Kadabi003171e2013-05-29 15:21:12 -07002167 {
2168 dprintf(CRITICAL, "Failed to erase the specified partition\n");
2169 return 1;
2170 }
Channagoud Kadabie86a40b2014-03-12 17:48:51 -07002171
Channagoud Kadabi003171e2013-05-29 15:21:12 -07002172 return 0;
2173}
2174
2175/*
2176 * Function: mmc get wp status
2177 * Arg : mmc device structure, block address and buffer for getting wp status
2178 * Return : 0 on Success, 1 on Failure
2179 * Flow : Get the WP group status by sending CMD31
2180 */
2181uint32_t mmc_get_wp_status(struct mmc_device *dev, uint32_t addr, uint8_t *wp_status)
2182{
2183 struct mmc_command cmd;
2184
2185 memset((struct mmc_command *)&cmd, 0, sizeof(struct mmc_command));
2186
2187 cmd.cmd_index = CMD31_SEND_WRITE_PROT_TYPE;
2188 cmd.argument = addr;
2189 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
2190 cmd.resp_type = SDHCI_CMD_RESP_R1;
2191 cmd.trans_mode = SDHCI_MMC_READ;
2192 cmd.data_present = 0x1;
2193 cmd.data.data_ptr = wp_status;
2194 cmd.data.num_blocks = 0x1;
2195 cmd.data.blk_sz = 0x8;
2196
2197 if (sdhci_send_command(&dev->host, &cmd))
2198 {
2199 dprintf(CRITICAL, "Failed to get status of write protect bits\n");
2200 return 1;
2201 }
2202
2203 return 0;
2204}
2205
2206/*
2207 * Function: mmc set/clear WP on user area
2208 * Arg : mmc device structure, block address,len, & flag to set or clear
2209 * Return : 0 on success, 1 on failure
2210 * Flow : Function to set/clear power on write protect on user area
2211 */
2212
2213uint32_t mmc_set_clr_power_on_wp_user(struct mmc_device *dev, uint32_t addr, uint64_t len, uint8_t set_clr)
2214{
2215 struct mmc_command cmd;
vijay kumar7420baa2014-05-02 12:01:49 +05302216 struct mmc_card *card = &dev->card;
Channagoud Kadabi003171e2013-05-29 15:21:12 -07002217 uint32_t wp_grp_size;
2218 uint32_t status;
2219 uint32_t num_wp_grps;
2220 uint32_t ret;
Channagoud Kadabi6d1751b2013-07-05 13:03:52 -07002221 uint32_t retry = 0;
Channagoud Kadabi003171e2013-05-29 15:21:12 -07002222 uint32_t i;
2223
2224 memset((struct mmc_command *)&cmd, 0, sizeof(struct mmc_command));
2225
2226 /* Convert len into blocks */
2227 len = len / MMC_BLK_SZ;
vijay kumar697dbfd2014-04-24 17:12:49 +05302228 wp_grp_size = dev->card.wp_grp_size;
Channagoud Kadabi003171e2013-05-29 15:21:12 -07002229
2230 /* Disable PERM WP */
2231 ret = mmc_switch_cmd(&dev->host, &dev->card, MMC_SET_BIT, MMC_USR_WP, MMC_US_PERM_WP_DIS);
2232
2233 if (ret)
2234 {
2235 dprintf(CRITICAL, "Failed to Disable PERM WP\n");
2236 return ret;
2237 }
2238
2239 /* Read the default values for user WP */
2240 ret = mmc_get_ext_csd(&dev->host, &dev->card);
2241
2242 if (ret)
2243 {
2244 dprintf(CRITICAL, "Failed to read ext csd for the card\n");
2245 return ret;
2246 }
2247
2248 /* Check if user power on WP is disabled or perm WP is enabled */
2249 if ((dev->card.ext_csd[MMC_USR_WP] & MMC_US_PWR_WP_DIS)
2250 || (dev->card.ext_csd[MMC_USR_WP] & MMC_US_PERM_WP_EN))
2251 {
2252 dprintf(CRITICAL, "Power on protection is disabled, cannot be set\n");
2253 return 1;
2254 }
2255
Channagoud Kadabi003171e2013-05-29 15:21:12 -07002256 if (len < wp_grp_size)
2257 {
2258 dprintf(CRITICAL, "Length is less than min WP size, WP was not set\n");
2259 return 1;
2260 }
2261
2262 /* Set power on USER WP */
2263 ret = mmc_switch_cmd(&dev->host, &dev->card, MMC_SET_BIT, MMC_USR_WP, MMC_US_PWR_WP_EN);
2264
2265 if (ret)
2266 {
2267 dprintf(CRITICAL, "Failed to set power on WP for user\n");
2268 return ret;
2269 }
2270
2271 num_wp_grps = ROUNDUP(len, wp_grp_size) / wp_grp_size;
2272
2273 if (set_clr)
2274 cmd.cmd_index = CMD28_SET_WRITE_PROTECT;
2275 else
2276 cmd.cmd_index = CMD29_CLEAR_WRITE_PROTECT;
2277
2278 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
2279 cmd.resp_type = SDHCI_CMD_RESP_R1B;
2280
2281 for(i = 0; i < num_wp_grps; i++)
2282 {
vijay kumar7420baa2014-05-02 12:01:49 +05302283 /*
2284 * Standard emmc cards use byte mode addressing
2285 * convert the block address to byte address before
2286 * sending the command
2287 */
2288 if (card->type == MMC_TYPE_STD_MMC)
2289 cmd.argument = (addr + (i * wp_grp_size)) * card->block_size;
2290 else
2291 cmd.argument = addr + (i * wp_grp_size);
Channagoud Kadabi003171e2013-05-29 15:21:12 -07002292
2293 if (sdhci_send_command(&dev->host, &cmd))
2294 return 1;
2295
2296 /* CMD28/CMD29 On failure returns address out of range error */
2297 if (MMC_ADDR_OUT_OF_RANGE(cmd.resp[0]))
2298 {
2299 dprintf(CRITICAL, "Address for CMD28/29 is out of range\n");
2300 return 1;
2301 }
2302
2303 /* Check the card status */
2304 do
2305 {
2306 if (mmc_get_card_status(&dev->host, &dev->card, &status))
2307 {
2308 dprintf(CRITICAL, "Failed to get card status afterapplying write protect\n");
2309 return 1;
2310 }
2311
2312 /* Time out for WP command */
2313 retry++;
2314 udelay(1000);
2315 if (retry == MMC_MAX_CARD_STAT_RETRY)
2316 {
2317 dprintf(CRITICAL, "Card status timed out after sending write protect command\n");
2318 return 1;
2319 }
2320 } while (!(status & MMC_READY_FOR_DATA) || (MMC_CARD_STATUS(status) == MMC_PROG_STATE));
2321
2322 }
2323
2324 return 0;
2325}
Channagoud Kadabi9e3c3b92013-06-18 18:32:32 -07002326
2327/* Function to put the mmc card to sleep */
2328void mmc_put_card_to_sleep(struct mmc_device *dev)
2329{
2330 struct mmc_command cmd = {0};
2331 struct mmc_card *card = &dev->card;
2332
2333 cmd.cmd_index = CMD7_SELECT_DESELECT_CARD;
2334 cmd.argument = 0x00000000;
2335 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
2336 cmd.resp_type = SDHCI_CMD_RESP_NONE;
2337
2338 /* send command */
2339 if(sdhci_send_command(&dev->host, &cmd))
2340 {
2341 dprintf(CRITICAL, "card deselect error: %s\n", __func__);
2342 return;
2343 }
2344
2345 cmd.cmd_index = CMD5_SLEEP_AWAKE;
2346 cmd.argument = (card->rca << MMC_CARD_RCA_BIT) | MMC_CARD_SLEEP;
2347 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
2348 cmd.resp_type = SDHCI_CMD_RESP_R1B;
2349
2350 /* send command */
2351 if(sdhci_send_command(&dev->host, &cmd))
2352 dprintf(CRITICAL, "card sleep error: %s\n", __func__);
2353}