blob: d4b0bd47f2579b14f477a8d323536d1de1a037c1 [file] [log] [blame]
Channagoud Kadabiec0f7f72013-03-11 15:21:36 -07001/* Copyright (c) 2013, The Linux Foundation. All rights reserved.
2 *
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>
35#include <partition_parser.h>
36#include <platform/iomap.h>
37#include <platform/timer.h>
38
39extern void clock_init_mmc(uint32_t);
40extern void clock_config_mmc(uint32_t, uint32_t);
41
42/* data access time unit in ns */
43static const uint32_t taac_unit[] =
44{
45 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
46};
47
48/* data access time value x 10 */
49static const uint32_t taac_value[] =
50{
51 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80
52};
53
54/* data transfer rate in kbit/s */
55static const uint32_t xfer_rate_unit[] =
56{
57 100, 1000, 10000, 100000, 0, 0, 0, 0
58};
59
60/* data transfer rate value x 10*/
61static const uint32_t xfer_rate_value[] =
62{
63 0, 10, 12, 13, 15, 20, 26, 30, 35, 40, 45, 52, 55, 60, 70, 80
64};
65
66/*
67 * Function: mmc decode and save csd
68 * Arg : Card structure & raw csd
69 * Return : 0 on Success, 1 on Failure
70 * Flow : Decodes CSD response received from the card.
71 * Note that we have defined only few of the CSD elements
72 * in csd structure. We'll only decode those values.
73 */
74static uint32_t mmc_decode_and_save_csd(struct mmc_card *card)
75{
76 uint32_t mmc_sizeof = 0;
77 uint32_t mmc_unit = 0;
78 uint32_t mmc_value = 0;
79 uint32_t mmc_temp = 0;
80 uint32_t *raw_csd = card->raw_csd;
81
82 struct mmc_csd mmc_csd;
83
84 mmc_sizeof = sizeof(uint32_t) * 8;
85
86 mmc_csd.cmmc_structure = UNPACK_BITS(raw_csd, 126, 2, mmc_sizeof);
87
88 if ((card->type == MMC_TYPE_SDHC)
89 || (card->type == MMC_TYPE_STD_SD)) {
90 /* 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 */
136 card->capacity = (1 + mmc_csd.c_size) * 512 * 1024;
137 } 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);
180 card->capacity = mmc_temp * mmc_csd.read_blk_len;
181 }
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);
216 card->capacity = mmc_temp * mmc_csd.read_blk_len;
217 } 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];
227
228 card->capacity = sec_count * MMC_BLK_SZ;
229 }
230 }
231
232 /* save the information in card structure */
233 memcpy((struct mmc_csd *)&card->csd,(struct mmc_csd *)&mmc_csd,
234 sizeof(struct mmc_csd));
235
236 dprintf(SPEW, "Decoded CSD fields:\n");
237 dprintf(SPEW, "cmmc_structure: %d\n", mmc_csd.cmmc_structure);
238 dprintf(SPEW, "card_cmd_class: %x\n", mmc_csd.card_cmd_class);
239 dprintf(SPEW, "write_blk_len: %d\n", mmc_csd.write_blk_len);
240 dprintf(SPEW, "read_blk_len: %d\n", mmc_csd.read_blk_len);
241 dprintf(SPEW, "r2w_factor: %d\n", mmc_csd.r2w_factor);
242 dprintf(SPEW, "sector_size: %d\n", mmc_csd.sector_size);
243 dprintf(SPEW, "c_size_mult:%d\n", mmc_csd.c_size_mult);
244 dprintf(SPEW, "c_size: %d\n", mmc_csd.c_size);
245 dprintf(SPEW, "nsac_clk_cycle: %d\n", mmc_csd.nsac_clk_cycle);
246 dprintf(SPEW, "taac_ns: %d\n", mmc_csd.taac_ns);
247 dprintf(SPEW, "tran_speed: %d kbps\n", mmc_csd.tran_speed);
248 dprintf(SPEW, "erase_blk_len: %d\n", mmc_csd.erase_blk_len);
249 dprintf(SPEW, "read_blk_misalign: %d\n", mmc_csd.read_blk_misalign);
250 dprintf(SPEW, "write_blk_misalign: %d\n", mmc_csd.write_blk_misalign);
251 dprintf(SPEW, "read_blk_partial: %d\n", mmc_csd.read_blk_partial);
252 dprintf(SPEW, "write_blk_partial: %d\n", mmc_csd.write_blk_partial);
253 dprintf(SPEW, "Card Capacity: %llu Bytes\n", card->capacity);
254
255 return 0;
256}
257
258/*
259 * Function: mmc decode & save cid
260 * Arg : card structure & raw cid
261 * Return : 0 on Success, 1 on Failure
262 * Flow : Decode CID sent by the card.
263 */
264static uint32_t mmc_decode_and_save_cid(struct mmc_card *card,
265 uint32_t *raw_cid)
266{
267 struct mmc_cid mmc_cid;
268 uint32_t mmc_sizeof = 0;
269 int i = 0;
270
271 if (!raw_cid) {
272 return 1;
273 }
274
275 mmc_sizeof = sizeof(uint32_t) * 8;
276
277 if ((card->type == MMC_TYPE_SDHC) ||
278 (card->type == MMC_TYPE_STD_SD)) {
279 mmc_cid.mid = UNPACK_BITS(raw_cid, 120, 8, mmc_sizeof);
280 mmc_cid.oid = UNPACK_BITS(raw_cid, 104, 16, mmc_sizeof);
281
282 for (i = 0; i < 5; i++) {
283 mmc_cid.pnm[i] = (uint8_t)UNPACK_BITS(raw_cid,
284 (104 - 8 * (i + 1)),
285 8,
286 mmc_sizeof);
287 }
288 mmc_cid.pnm[5] = 0;
289 mmc_cid.pnm[6] = 0;
290
291 mmc_cid.prv = UNPACK_BITS(raw_cid, 56, 8, mmc_sizeof);
292 mmc_cid.psn = UNPACK_BITS(raw_cid, 24, 31, mmc_sizeof);
293 mmc_cid.month = UNPACK_BITS(raw_cid, 8, 4, mmc_sizeof);
294 mmc_cid.year = UNPACK_BITS(raw_cid, 12, 8, mmc_sizeof);
295 mmc_cid.year += 2000;
296 } else {
297 mmc_cid.mid = UNPACK_BITS(raw_cid, 120, 8, mmc_sizeof);
298 mmc_cid.oid = UNPACK_BITS(raw_cid, 104, 16, mmc_sizeof);
299
300 for (i = 0; i < 6; i++) {
301 mmc_cid.pnm[i] = (uint8_t)UNPACK_BITS(raw_cid, (104 - 8 * (i + 1)),
302 8, mmc_sizeof);
303 }
304 mmc_cid.pnm[6] = 0;
305
306 mmc_cid.prv = UNPACK_BITS(raw_cid, 48, 8, mmc_sizeof);
307 mmc_cid.psn = UNPACK_BITS(raw_cid, 16, 31, mmc_sizeof);
308 mmc_cid.month = UNPACK_BITS(raw_cid, 8, 4, mmc_sizeof);
309 mmc_cid.year = UNPACK_BITS(raw_cid, 12, 4, mmc_sizeof);
310 mmc_cid.year += 1997;
311 }
312
313 /* save it in card database */
314 memcpy((struct mmc_cid *)&card->cid,
315 (struct mmc_cid *)&mmc_cid, sizeof(struct mmc_cid));
316
317 dprintf(SPEW, "Decoded CID fields:\n");
318 dprintf(SPEW, "Manufacturer ID: %x\n", mmc_cid.mid);
319 dprintf(SPEW, "OEM ID: 0x%x\n", mmc_cid.oid);
320 dprintf(SPEW, "Product Name: %s\n", mmc_cid.pnm);
321 dprintf(SPEW, "Product revision: %d.%d\n", (mmc_cid.prv >> 4),
322 (mmc_cid.prv & 0xF));
323 dprintf(SPEW, "Product serial number: %X\n", mmc_cid.psn);
324 dprintf(SPEW, "Manufacturing date: %d %d\n", mmc_cid.month, mmc_cid.year);
325
326 return 0;
327}
328
329/*
330 * Function: mmc reset cards
331 * Arg : host structure
332 * Return : 0 on Success, 1 on Failure
333 * Flow : Reset all the cards to idle condition (CMD 0)
334 */
335static uint8_t mmc_reset_card(struct sdhci_host *host)
336{
337 struct mmc_command cmd;
338
339 memset((struct mmc_command *)&cmd, 0, sizeof(struct mmc_command));
340
341 cmd.cmd_index = CMD0_GO_IDLE_STATE;
342 cmd.argument = 0;
343 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
344 cmd.resp_type = SDHCI_CMD_RESP_NONE;
345
346 /* send command */
347 return sdhci_send_command(host, &cmd);
348}
349
350/*
351 * Function: mmc operations command
352 * Arg : host & card structure
353 * Return : 0 on Success, 1 on Failure
354 * Flow : Send CMD1 to know whether the card supports host VDD profile or not.
355 */
356static uint32_t mmc_send_op_cond(struct sdhci_host *host, struct mmc_card *card)
357{
358 struct mmc_command cmd;
359 uint32_t mmc_resp = 0;
360 uint32_t mmc_ret = 0;
361 uint32_t mmc_retry = 0;
362
363 memset((struct mmc_command *)&cmd, 0, sizeof(struct mmc_command));
364
365 /* CMD1 format:
366 * [31] Busy bit
367 * [30:29] Access mode
368 * [28:24] reserved
369 * [23:15] 2.7-3.6
370 * [14:8] 2.0-2.6
371 * [7] 1.7-1.95
372 * [6:0] reserved
373 */
374
375 cmd.cmd_index = CMD1_SEND_OP_COND;
376 cmd.argument = card->ocr;
377 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
378 cmd.resp_type = SDHCI_CMD_RESP_R3;
379
380 do {
381 mmc_ret = sdhci_send_command(host, &cmd);
382 if (mmc_ret)
383 return mmc_ret;
384
385 /* Command returned success, now it's time to examine response */
386 mmc_resp = cmd.resp[0];
387
388 /* Check the response for busy status */
389 if (!(mmc_resp & MMC_OCR_BUSY)) {
390 mmc_retry++;
391 mdelay(1);
392 continue;
393 } else
394 break;
395 } while (mmc_retry < MMC_MAX_COMMAND_RETRY);
396
397 /* If we reached here after max retries, we failed to get OCR */
398 if (mmc_retry == MMC_MAX_COMMAND_RETRY && !(mmc_resp & MMC_OCR_BUSY)) {
399 dprintf(CRITICAL, "Card has busy status set. Init did not complete\n");
400 return 1;
401 }
402
403 /* Response contains card's ocr. Update card's information */
404 card->ocr = mmc_resp;
405
406 if (mmc_resp & MMC_OCR_SEC_MODE)
407 card->type = MMC_TYPE_MMCHC;
408 else
409 card->type = MMC_TYPE_STD_MMC;
410
411 return 0;
412}
413
414/*
415 * Function: mmc send cid
416 * Arg : host & card structure
417 * Return : 0 on Success, 1 on Failure
418 * Flow : Request any card to send its uniquie card identification
419 * (CID) number (CMD2).
420 */
421static uint32_t mmc_all_send_cid(struct sdhci_host *host, struct mmc_card *card)
422{
423 struct mmc_command cmd;
424 uint32_t mmc_ret = 0;
425
426 memset((struct mmc_command *)&cmd, 0, sizeof(struct mmc_command));
427
428 /* CMD2 Format:
429 * [31:0] stuff bits
430 */
431 cmd.cmd_index = CMD2_ALL_SEND_CID;
432 cmd.argument = 0;
433 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
434 cmd.resp_type = SDHCI_CMD_RESP_R2;
435
436 /* send command */
437 mmc_ret = sdhci_send_command(host, &cmd);
438 if (mmc_ret) {
439 return mmc_ret;
440 }
441
442 /* Response contains card's 128 bits CID register */
443 mmc_ret = mmc_decode_and_save_cid(card, cmd.resp);
444 if (mmc_ret) {
445 return mmc_ret;
446 }
447
448 return 0;
449}
450
451/*
452 * Function: mmc send relative address
453 * Arg : host & card structure
454 * Return : 0 on Success, 1 on Failure
455 * Flow : Ask card to send it's relative card address (RCA).
456 * This RCA number is shorter than CID and is used by
457 * the host to address the card in future (CMD3)
458 */
459static uint32_t mmc_send_relative_address(struct sdhci_host *host,
460 struct mmc_card *card)
461{
462 struct mmc_command cmd;
463 uint32_t mmc_ret = 0;
464
465 memset((struct mmc_command *)&cmd, 0, sizeof(struct mmc_command));
466
467 /* CMD3 Format:
468 * [31:0] stuff bits
469 */
470 if (card->type == MMC_TYPE_SDHC ||
471 card->type == MMC_TYPE_STD_SD) {
472 cmd.cmd_index = CMD3_SEND_RELATIVE_ADDR;
473 cmd.argument = 0;
474 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
475 cmd.resp_type = SDHCI_CMD_RESP_R6;
476
477 /* send command */
478 mmc_ret = sdhci_send_command(host, &cmd);
479 if (mmc_ret)
480 return mmc_ret;
481
482 /* For sD, card will send RCA. Store it */
483 card->rca = (cmd.resp[0] >> 16);
484 } else {
485 cmd.cmd_index = CMD3_SEND_RELATIVE_ADDR;
486 cmd.argument = (MMC_RCA << 16);
487 card->rca = (cmd.argument >> 16);
488 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
489 cmd.resp_type = SDHCI_CMD_RESP_R6;
490
491 /* send command */
492 mmc_ret = sdhci_send_command(host, &cmd);
493 if (mmc_ret)
494 return mmc_ret;
495 }
496
497 return 0;
498}
499
500/*
501 * Function: mmc send csd
502 * Arg : host, card structure & o/p arg to store csd
503 * Return : 0 on Success, 1 on Failure
504 * Flow : Requests card to send it's CSD register's contents. (CMD9)
505 */
506static uint32_t mmc_send_csd(struct sdhci_host *host, struct mmc_card *card)
507{
508 struct mmc_command cmd;
509 uint32_t mmc_arg = 0;
510 uint32_t mmc_ret = 0;
511
512 memset((struct mmc_command *)&cmd, 0, sizeof(struct mmc_command));
513
514 /* CMD9 Format:
515 * [31:16] RCA
516 * [15:0] stuff bits
517 */
518 mmc_arg |= card->rca << 16;
519
520 cmd.cmd_index = CMD9_SEND_CSD;
521 cmd.argument = mmc_arg;
522 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
523 cmd.resp_type = SDHCI_CMD_RESP_R2;
524
525 /* send command */
526 mmc_ret = sdhci_send_command(host, &cmd);
527 if (mmc_ret)
528 return mmc_ret;
529
530 /* response contains the card csd */
531 memcpy(card->raw_csd, cmd.resp, sizeof(cmd.resp));
532
533 return 0;
534}
535
536/*
537 * Function: mmc select card
538 * Arg : host, card structure & RCA
539 * Return : 0 on Success, 1 on Failure
540 * Flow : Selects a card by sending CMD7 to the card with its RCA.
541 * If RCA field is set as 0 ( or any other address ),
542 * the card will be de-selected. (CMD7)
543 */
544static uint32_t mmc_select_card(struct sdhci_host *host, struct mmc_card *card,
545 uint32_t rca)
546{
547 struct mmc_command cmd;
548 uint32_t mmc_arg = 0;
549 uint32_t mmc_ret = 0;
550
551 memset((struct mmc_command *)&cmd, 0, sizeof(struct mmc_command));
552
553 /* CMD7 Format:
554 * [31:16] RCA
555 * [15:0] stuff bits
556 */
557 mmc_arg |= rca << 16;
558
559 cmd.cmd_index = CMD7_SELECT_DESELECT_CARD;
560 cmd.argument = mmc_arg;
561 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
562
563 /* If we are deselecting card, we do not get response */
564 if (rca == card->rca && rca) {
565 if (card->type == MMC_TYPE_SDHC ||
566 card->type == MMC_TYPE_STD_SD)
567 cmd.resp_type = SDHCI_CMD_RESP_R1B;
568 else
569 cmd.resp_type = SDHCI_CMD_RESP_R1;
570 } else
571 cmd.resp_type = SDHCI_CMD_RESP_NONE;
572
573 /* send command */
574 mmc_ret = sdhci_send_command(host, &cmd);
575 if (mmc_ret)
576 return mmc_ret;
577
578 return 0;
579}
580
581/*
582 * Function: mmc set block len
583 * Arg : host, card structure & block length
584 * Return : 0 on Success, 1 on Failure
585 * Flow : Send command to set block length.
586 */
587static uint32_t mmc_set_block_len(struct sdhci_host *host,
588 struct mmc_card *card,
589 uint32_t block_len)
590{
591 struct mmc_command cmd;
592 uint32_t mmc_ret = 0;
593
594 memset((struct mmc_command *)&cmd, 0, sizeof(struct mmc_command));
595
596 /* CMD16 Format:
597 * [31:0] block length
598 */
599
600 cmd.cmd_index = CMD16_SET_BLOCKLEN;
601 cmd.argument = block_len;
602 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
603 cmd.resp_type = SDHCI_CMD_RESP_R1;
604
605 /* send command */
606 mmc_ret = sdhci_send_command(host, &cmd);
607 if (mmc_ret)
608 return mmc_ret;
609
610 /*
611 * If blocklength is larger than 512 bytes,
612 * the card sets BLOCK_LEN_ERROR bit.
613 */
614 if (cmd.resp[0] & MMC_R1_BLOCK_LEN_ERR) {
615 dprintf(CRITICAL, "The block length is not supported by the card\n");
616 return 1;
617 }
618
619 return 0;
620}
621
622/*
623 * Function: mmc get card status
624 * Arg : host, card structure & o/p argument card status
625 * Return : 0 on Success, 1 on Failure
626 * Flow : Get the current status of the card
627 */
628static uint32_t mmc_get_card_status(struct sdhci_host *host,
629 struct mmc_card *card, uint32_t *status)
630{
631 struct mmc_command cmd;
632 uint32_t mmc_ret = 0;
633
634 memset((struct mmc_command *)&cmd, 0, sizeof(struct mmc_command));
635
636 /* CMD13 Format:
637 * [31:16] RCA
638 * [15:0] stuff bits
639 */
640 cmd.cmd_index = CMD13_SEND_STATUS;
641 cmd.argument = card->rca << 16;
642 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
643 cmd.resp_type = SDHCI_CMD_RESP_R1;
644
645 /* send command */
646 mmc_ret = sdhci_send_command(host, &cmd);
647 if (mmc_ret)
648 return mmc_ret;
649
650 /* Checking ADDR_OUT_OF_RANGE error in CMD13 response */
651 if ((cmd.resp[0] >> 31) & 0x01)
652 return 1;
653
654 *status = cmd.resp[0];
655 return 0;
656}
657
658/*
659 * Function: mmc get ext csd
660 * Arg : host, card structure & array to hold ext attributes
661 * Return : 0 on Success, 1 on Failure
662 * Flow : Send ext csd command & get the card attributes
663 */
664static uint32_t mmc_get_ext_csd(struct sdhci_host *host, struct mmc_card *card)
665{
666 struct mmc_command cmd;
667 uint32_t mmc_ret = 0;
668
669 card->ext_csd = memalign(CACHE_LINE, ROUNDUP(512, CACHE_LINE));
670
671 ASSERT(card->ext_csd);
672
673 memset(card->ext_csd, 0, sizeof(card->ext_csd));
674
675 memset((struct mmc_command *)&cmd, 0, sizeof(struct mmc_command));
676
677 /* CMD8 */
678 cmd.cmd_index = CMD8_SEND_EXT_CSD;
679 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
680 cmd.resp_type = SDHCI_CMD_RESP_R1;
681 cmd.data.data_ptr = card->ext_csd;
682 cmd.data.num_blocks = 1;
683 cmd.data_present = 0x1;
684 cmd.trans_mode = SDHCI_MMC_READ;
685
686 /* send command */
687 mmc_ret = sdhci_send_command(host, &cmd);
688 if (mmc_ret)
689 return mmc_ret;
690
691 return mmc_ret;
692}
693
694/*
695 * Function: mmc switch command
696 * Arg : Host, card structure, access mode, index & value to be set
697 * Return : 0 on Success, 1 on Failure
698 * Flow : Send switch command to the card to set the ext attribute @ index
699 */
700static uint32_t mmc_switch_cmd(struct sdhci_host *host, struct mmc_card *card,
701 uint32_t access, uint32_t index, uint32_t value)
702{
703
704 struct mmc_command cmd;
705 uint32_t mmc_ret = 0;
706 uint32_t mmc_status;
707
708 memset((struct mmc_command *)&cmd, 0, sizeof(struct mmc_command));
709
710 /* CMD6 Format:
711 * [31:26] set to 0
712 * [25:24] access
713 * [23:16] index
714 * [15:8] value
715 * [7:3] set to 0
716 * [2:0] cmd set
717 */
718 cmd.cmd_index = CMD6_SWITCH_FUNC;
719 cmd.argument |= (access << 24);
720 cmd.argument |= (index << 16);
721 cmd.argument |= (value << 8);
722 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
723 cmd.resp_type = SDHCI_CMD_RESP_R1B;
724
725 mmc_ret = sdhci_send_command(host, &cmd);
726 if (mmc_ret) {
727 dprintf(CRITICAL, "CMD6 send failed\n");
728 return mmc_ret;
729 }
730
731 /* Check if the card completed the switch command processing */
732 mmc_ret = mmc_get_card_status(host, card, &mmc_status);
733 if (mmc_ret) {
734 dprintf(CRITICAL, "Get card status failed\n");
735 return mmc_ret;
736 }
737
738 if (MMC_CARD_STATUS(mmc_status) != MMC_TRAN_STATE) {
739 dprintf(CRITICAL, "Switch cmd failed. Card not in tran state\n");
740 mmc_ret = 1;
741 }
742
743 if (mmc_status & MMC_SWITCH_FUNC_ERR_FLAG) {
744 dprintf(CRITICAL, "Switch cmd failed. Switch Error.\n");
745 mmc_ret = 1;
746 }
747
748 return mmc_ret;
749}
750
751/*
752 * Function: mmc set bus width
753 * Arg : Host, card structure & width
754 * Return : 0 on Success, 1 on Failure
755 * Flow : Send switch command to set bus width
756 */
757static uint32_t mmc_set_bus_width(struct sdhci_host *host,
758 struct mmc_card *card,
759 uint32_t width)
760{
761 uint32_t mmc_ret = 0;
762
763 mmc_ret = mmc_switch_cmd(host, card, MMC_ACCESS_WRITE,
764 MMC_EXT_MMC_BUS_WIDTH, width);
765
766 if (mmc_ret) {
767 dprintf(CRITICAL, "Switch cmd failed\n");
768 return mmc_ret;
769 }
770
771 return 0;
772}
773
774
775/*
776 * Function: mmc card supports ddr mode
777 * Arg : None
778 * Return : 1 if DDR mode is supported, 0 otherwise
779 * Flow : Check the ext csd attributes of the card
780 */
781static uint8_t mmc_card_supports_hs200_mode(struct mmc_card *card)
782{
783 if (card->ext_csd[MMC_DEVICE_TYPE] & MMC_HS_HS200_MODE)
784 return 1;
785 else
786 return 0;
787}
788
789/*
790 * Function: mmc card supports ddr mode
791 * Arg : None
792 * Return : 1 if DDR mode is supported, 0 otherwise
793 * Flow : Check the ext csd attributes of the card
794 */
795static uint8_t mmc_card_supports_ddr_mode(struct mmc_card *card)
796{
797 if (card->ext_csd[MMC_DEVICE_TYPE] & MMC_HS_DDR_MODE)
798 return 1;
799 else
800 return 0;
801}
802
803/*
804 * Function : Enable HS200 mode
805 * Arg : Host, card structure and bus width
806 * Return : 0 on Success, 1 on Failure
807 * Flow :
808 * - Set the bus width to 4/8 bit SDR as supported by the target & host
809 * - Set the HS_TIMING on ext_csd 185 for the card
810 */
811static uint32_t mmc_set_hs200_mode(struct sdhci_host *host,
812 struct mmc_card *card, uint32_t width)
813{
814 uint32_t mmc_ret = 0;
815
816 /* Set 4/8 bit SDR bus width */
817 mmc_ret = mmc_set_bus_width(host, card, width);
818 if (mmc_ret) {
819 dprintf(CRITICAL, "Failure to set wide bus for Card(RCA:%x)\n",
820 card->rca);
821 return mmc_ret;
822 }
823
824 /* Setting HS200 in HS_TIMING using EXT_CSD (CMD6) */
825 mmc_ret = mmc_switch_cmd(host, card, MMC_ACCESS_WRITE, MMC_EXT_MMC_HS_TIMING, MMC_HS200_TIMING);
826
827 if (mmc_ret) {
828 dprintf(CRITICAL, "Switch cmd returned failure %d\n", __LINE__);
829 return mmc_ret;
830 }
831
832 /* Enable hs200 mode in controller */
833 sdhci_set_sdr_mode(host);
834
835 return mmc_ret;
836}
837
838/*
839 * Function: mmc set ddr mode
840 * Arg : Host & card structure
841 * Return : 0 on Success, 1 on Failure
842 * Flow : Set bus width for ddr mode & set controller in DDR mode
843*/
844static uint8_t mmc_set_ddr_mode(struct sdhci_host *host, struct mmc_card *card)
845{
846 uint8_t mmc_ret = 0;
847
848 /* Set width for 8 bit DDR mode by default */
849 mmc_ret = mmc_set_bus_width(host, card, DATA_DDR_BUS_WIDTH_8BIT);
850
851 if (mmc_ret) {
852 dprintf(CRITICAL, "Failure to set DDR mode for Card(RCA:%x)\n",
853 card->rca);
854 return mmc_ret;
855 }
856
857 sdhci_set_ddr_mode(host);
858
859 return 0;
860}
861
862/*
863 * Function: mmc set high speed interface
864 * Arg : Host & card structure
865 * Return : None
866 * Flow : Sets the sdcc clock & clock divider in the host controller
867 * Adjust the interface speed to optimal speed
868 */
869static uint32_t mmc_set_hs_interface(struct sdhci_host *host,
870 struct mmc_card *card)
871{
872 uint32_t mmc_ret = 0;
873
874 /* Setting HS_TIMING in EXT_CSD (CMD6) */
875 mmc_ret = mmc_switch_cmd(host, card, MMC_ACCESS_WRITE,
876 MMC_EXT_MMC_HS_TIMING, MMC_HS_TIMING);
877
878 if (mmc_ret) {
879 dprintf(CRITICAL, "Switch cmd returned failure %d\n", __LINE__);
880 return mmc_ret;
881 }
882
883 return 0;
884}
885
886/*
887 * Function: mmc_host_init
888 * Arg : mmc device structure
889 * Return : 0 on success, 1 on Failure
890 * Flow : Initialize the host contoller
891 * Set the clock rate to 400 KHZ for init
892 */
893static uint8_t mmc_host_init(struct mmc_device *dev)
894{
895 uint8_t mmc_ret = 0;
896
897 struct sdhci_host *host;
898 struct mmc_config_data *cfg;
899
900 host = &dev->host;
901 cfg = &dev->config;
902
903 /*
904 * Initialize the controller, read the host capabilities
905 * set power on mode
906 */
907 sdhci_init(host);
908
909 /* Initialize any clocks needed for SDC controller */
910 clock_init_mmc(cfg->slot);
911
912 /* Setup initial freq to 400KHz */
913 clock_config_mmc(cfg->slot, cfg->max_clk_rate);
914
915 mmc_ret = sdhci_clk_supply(host, SDHCI_CLK_400KHZ);
916
917 return mmc_ret;
918}
919
920/*
921 * Function: mmc identify card
922 * Arg : host & card structure
923 * Return : 0 on Success, 1 on Failure
924 * Flow : Performs card identification process:
925 * 1. Get card's unique identification number (CID)
926 * 2. Get(for sd)/set (for mmc) relative card address (RCA)
927 * 3. Select the card to put it in TRAN state
928 */
929static uint32_t mmc_identify_card(struct sdhci_host *host, struct mmc_card *card)
930{
931 uint32_t mmc_return = 0;
932 uint32_t raw_csd[4];
933
934 /* Ask card to send its unique card identification (CID) number (CMD2) */
935 mmc_return = mmc_all_send_cid(host, card);
936 if (mmc_return) {
937 dprintf(CRITICAL,"Failure getting card's CID number!\n");
938 return mmc_return;
939 }
940
941 /* Ask card to send a relative card address (RCA) (CMD3) */
942 mmc_return = mmc_send_relative_address(host, card);
943 if (mmc_return) {
944 dprintf(CRITICAL, "Failure getting card's RCA!\n");
945 return mmc_return;
946 }
947
948 /* Get card's CSD register (CMD9) */
949 mmc_return = mmc_send_csd(host, card);
950 if (mmc_return) {
951 dprintf(CRITICAL,"Failure getting card's CSD information!\n");
952 return mmc_return;
953 }
954
955 /* Select the card (CMD7) */
956 mmc_return = mmc_select_card(host, card, card->rca);
957 if (mmc_return) {
958 dprintf(CRITICAL, "Failure selecting the Card with RCA: %x\n",card->rca);
959 return mmc_return;
960 }
961
962 /* Set the card status as active */
963 card->status = MMC_STATUS_ACTIVE;
964
965 return 0;
966}
967
968/*
969 * Function: mmc_reset_card_and_send_op
970 * Arg : Host & Card structure
971 * Return : 0 on Success, 1 on Failure
972 * Flow : Routine to initialize MMC card. It resets a card to idle state,
973 * verify operating voltage and set the card in ready state.
974 */
975static uint32_t mmc_reset_card_and_send_op(struct sdhci_host *host, struct mmc_card *card)
976{
977 uint32_t mmc_return = 0;
978
979 /* 1. Card Reset - CMD0 */
980 mmc_return = mmc_reset_card(host);
981 if (mmc_return) {
982 dprintf(CRITICAL, "Failure resetting MMC cards!\n");
983 return mmc_return;
984 }
985
986 /* 2. Card Initialization process */
987
988 /*
989 * Send CMD1 to identify and reject cards that do not match host's VDD range
990 * profile. Cards sends its OCR register in response.
991 */
992 mmc_return = mmc_send_op_cond(host, card);
993
994 /* OCR is not received, init could not complete */
995 if (mmc_return) {
996 dprintf(CRITICAL, "Failure getting OCR response from MMC Card\n");
997 return mmc_return;
998 }
999
1000 return 0;
1001}
1002
1003/*
1004 * Function: mmc_init_card
1005 * Arg : mmc device structure
1006 * Return : 0 on Success, 1 on Failure
1007 * Flow : Performs initialization and identification of eMMC cards connected
1008 * to the host.
1009 */
1010
1011static uint32_t mmc_card_init(struct mmc_device *dev)
1012{
1013 uint32_t mmc_return = 0;
1014 uint32_t status;
1015 uint8_t bus_width = 0;
1016
1017 struct sdhci_host *host;
1018 struct mmc_card *card;
1019 struct mmc_config_data *cfg;
1020
1021 host = &dev->host;
1022 card = &dev->card;
1023 cfg = &dev->config;
1024
1025 /* Initialize MMC card structure */
1026 card->status = MMC_STATUS_INACTIVE;
1027
1028 /* TODO: Get the OCR params from target */
1029 card->ocr = MMC_OCR_27_36 | MMC_OCR_SEC_MODE;
1030
1031 /* Reset the card & get the OCR */
1032 mmc_return = mmc_reset_card_and_send_op(host, card);
1033 if (mmc_return)
1034 return mmc_return;
1035
1036 /* Identify (CMD2, CMD3 & CMD9) and select the card (CMD7) */
1037 mmc_return = mmc_identify_card(host, card);
1038 if (mmc_return)
1039 return mmc_return;
1040
1041 /* set interface speed */
1042 mmc_return = mmc_set_hs_interface(host, card);
1043 if (mmc_return) {
1044 dprintf(CRITICAL, "Error adjusting interface speed!\n");
1045 return mmc_return;
1046 }
1047
1048 /* Set the sdcc clock to 50 MHZ */
1049 sdhci_clk_supply(host, SDHCI_CLK_50MHZ);
1050
1051 /* Now get the extended CSD for the card */
1052 if ((card->type == MMC_TYPE_STD_MMC) ||
1053 (card->type == MMC_TYPE_MMCHC)) {
1054 /* For MMC cards, also get the extended csd */
1055 mmc_return = mmc_get_ext_csd(host, card);
1056
1057 if (mmc_return) {
1058 dprintf(CRITICAL, "Failure getting card's ExtCSD information!\n");
1059 return mmc_return;
1060 }
1061 }
1062
1063 /* Decode and save the CSD register */
1064 mmc_return = mmc_decode_and_save_csd(card);
1065 if (mmc_return) {
1066 dprintf(CRITICAL, "Failure decoding card's CSD information!\n");
1067 return mmc_return;
1068 }
1069
1070
1071 /* Set the bus width based on host, target capbilities */
1072 if (cfg->bus_width == DATA_BUS_WIDTH_8BIT && host->caps.bus_width_8bit)
1073 bus_width = DATA_BUS_WIDTH_8BIT;
1074 /*
1075 * Host contoller by default supports 4 bit & 1 bit mode.
1076 * No need to check for host support here
1077 */
1078 else if (cfg->bus_width == DATA_BUS_WIDTH_4BIT)
1079 bus_width = DATA_BUS_WIDTH_4BIT;
1080 else
1081 bus_width = DATA_BUS_WIDTH_1BIT;
1082
1083 /* Set 4/8 bit SDR bus width in controller */
1084 mmc_return = sdhci_set_bus_width(host, bus_width);
1085
1086 if (mmc_return) {
1087 dprintf(CRITICAL, "Failed to set bus width for host controller\n");
1088 return 1;
1089 }
1090
1091 /* Enable high speed mode in the follwing order:
1092 * 1. HS200 mode if supported by host & card
1093 * 2. DDR mode host, if supported by host & card
1094 * 3. Use normal speed mode with supported bus width
1095 */
1096 if (mmc_card_supports_hs200_mode(card) && host->caps.sdr50_support) {
1097 mmc_return = mmc_set_hs200_mode(host, card, bus_width);
1098
1099 if (mmc_return) {
1100 dprintf(CRITICAL, "Failure to set HS200 mode for Card(RCA:%x)\n",
1101 card->rca);
1102 return mmc_return;
1103 }
1104 } else if (mmc_card_supports_ddr_mode(card) && host->caps.ddr_support) {
1105 mmc_return = mmc_set_ddr_mode(host, card);
1106
1107 if (mmc_return) {
1108 dprintf(CRITICAL, "Failure to set DDR mode for Card(RCA:%x)\n",
1109 card->rca);
1110 return mmc_return;
1111 }
1112 } else {
1113 /* Set 4/8 bit bus width for the card */
1114 mmc_return = mmc_set_bus_width(host, card, bus_width);
1115 if (mmc_return) {
1116 dprintf(CRITICAL, "Failure to set wide bus for Card(RCA:%x)\n",
1117 card->rca);
1118 return mmc_return;
1119 }
1120 }
1121
1122
1123 /* Verify TRAN state after changing speed and bus width */
1124 mmc_return = mmc_get_card_status(host, card, &status);
1125 if (mmc_return)
1126 return mmc_return;
1127
1128 if (MMC_CARD_STATUS(status) != MMC_TRAN_STATE)
1129 mmc_return = 1;
1130
1131 return mmc_return;
1132}
1133
1134/*
1135 * Function: mmc display csd
1136 * Arg : None
1137 * Return : None
1138 * Flow : Displays the csd information
1139 */
1140static void mmc_display_csd(struct mmc_card *card)
1141{
1142 dprintf(SPEW, "erase_grpsize: %d\n", card->csd.erase_grp_size);
1143 dprintf(SPEW, "erase_grpmult: %d\n", card->csd.erase_grp_mult);
1144 dprintf(SPEW, "wp_grpsize: %d\n", card->csd.wp_grp_size);
1145 dprintf(SPEW, "wp_grpen: %d\n", card->csd.wp_grp_enable);
1146 dprintf(SPEW, "perm_wp: %d\n", card->csd.perm_wp);
1147 dprintf(SPEW, "temp_wp: %d\n", card->csd.temp_wp);
1148}
1149
1150/*
1151 * Function: mmc_init
1152 * Arg : MMC configuration data
1153 * Return : Pointer to mmc device
1154 * Flow : Entry point to MMC boot process
1155 * Initialize the sd host controller
1156 * Initialize the mmc card
1157 * Set the clock & high speed mode
1158 */
1159struct mmc_device *mmc_init(struct mmc_config_data *data)
1160{
1161 uint8_t mmc_ret = 0;
1162 struct mmc_device *dev;
1163
1164 dev = (struct mmc_device *) malloc (sizeof(struct mmc_device));
1165
1166 if (!dev) {
1167 dprintf(CRITICAL, "Error allocating mmc device\n");
1168 return NULL;
1169 }
1170
1171 ASSERT(data);
1172
1173 memcpy((void*)&dev->config, (void*)data, sizeof(struct mmc_config_data));
1174
1175 memset((struct mmc_card *)&dev->card, 0, sizeof(struct mmc_card));
1176
1177 dev->host.base = data->base;
1178
1179 /* Initialize the host & clock */
1180 dprintf(SPEW, " Initializing MMC host data structure and clock!\n");
1181
1182 mmc_ret = mmc_host_init(dev);
1183 if (mmc_ret) {
1184 dprintf(CRITICAL, "Error Initializing MMC host : %u\n", mmc_ret);
1185 return NULL;
1186 }
1187
1188 /* Initialize and identify cards connected to host */
1189 mmc_ret = mmc_card_init(dev);
1190 if (mmc_ret) {
1191 dprintf(CRITICAL, "Failed detecting MMC/SDC @ slot%d\n",
1192 dev->config.slot);
1193 return NULL;
1194 }
1195
1196 dprintf(INFO, "Done initialization of the card\n");
1197
1198 mmc_display_csd(&dev->card);
1199
1200 return dev;
1201}
1202
1203/*
1204 * Function: mmc sdhci read
1205 * Arg : mmc device structure, block address, number of blocks & destination
1206 * Return : 0 on Success, non zero on success
1207 * Flow : Fill in the command structure & send the command
1208 */
1209uint32_t mmc_sdhci_read(struct mmc_device *dev, void *dest,
1210 uint64_t blk_addr, uint32_t num_blocks)
1211{
1212 uint32_t mmc_ret = 0;
1213 struct mmc_command cmd;
1214
1215 memset((struct mmc_command *)&cmd, 0, sizeof(struct mmc_command));
1216
1217 /* CMD17/18 Format:
1218 * [31:0] Data Address
1219 */
1220 if (num_blocks == 1)
1221 cmd.cmd_index = CMD17_READ_SINGLE_BLOCK;
1222 else
1223 cmd.cmd_index = CMD18_READ_MULTIPLE_BLOCK;
1224
1225 cmd.argument = blk_addr;
1226 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
1227 cmd.resp_type = SDHCI_CMD_RESP_R1;
1228 cmd.trans_mode = SDHCI_MMC_READ;
1229 cmd.data_present = 0x1;
1230 cmd.data.data_ptr = dest;
1231 cmd.data.num_blocks = num_blocks;
1232
1233 /* send command */
1234 mmc_ret = sdhci_send_command(&dev->host, &cmd);
1235 if (mmc_ret) {
1236 return mmc_ret;
1237 }
1238
1239 /* Response contains 32 bit Card status. Here we'll check
1240 BLOCK_LEN_ERROR and ADDRESS_ERROR */
1241 if (cmd.resp[0] & MMC_R1_BLOCK_LEN_ERR) {
1242 dprintf(CRITICAL, "The transferred bytes does not match the block length\n");
1243 return 1;
1244 }
1245
1246 /* Misaligned address not matching block length */
1247 if (cmd.resp[0] & MMC_R1_ADDR_ERR) {
1248 dprintf(CRITICAL, "The misaligned address did not match the block length used\n");
1249 return 1;
1250 }
1251
1252 if (MMC_CARD_STATUS(cmd.resp[0]) != MMC_TRAN_STATE) {
1253 dprintf(CRITICAL, "MMC read failed, card is not in TRAN state\n");
1254 return 1;
1255 }
1256
1257 return mmc_ret;
1258}
1259
1260/*
1261 * Function: mmc sdhci write
1262 * Arg : mmc device structure, block address, number of blocks & source
1263 * Return : 0 on Success, non zero on success
1264 * Flow : Fill in the command structure & send the command
1265 */
1266uint32_t mmc_sdhci_write(struct mmc_device *dev, void *src,
1267 uint64_t blk_addr, uint32_t num_blocks)
1268{
1269 uint32_t mmc_ret = 0;
1270 struct mmc_command cmd;
1271
1272 memset((struct mmc_command *)&cmd, 0, sizeof(struct mmc_command));
1273
1274 /* CMD24/25 Format:
1275 * [31:0] Data Address
1276 */
1277
1278 if (num_blocks == 1)
1279 cmd.cmd_index = CMD24_WRITE_SINGLE_BLOCK;
1280 else
1281 cmd.cmd_index = CMD25_WRITE_MULTIPLE_BLOCK;
1282
1283 cmd.argument = blk_addr;
1284 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
1285 cmd.resp_type = SDHCI_CMD_RESP_R1;
1286 cmd.trans_mode = SDHCI_MMC_WRITE;
1287 cmd.data_present = 0x1;
1288 cmd.data.data_ptr = src;
1289 cmd.data.num_blocks = num_blocks;
1290
1291 /* send command */
1292 mmc_ret = sdhci_send_command(&dev->host, &cmd);
1293 if (mmc_ret)
1294 return mmc_ret;
1295
1296 /* Response contains 32 bit Card status. Here we'll check
1297 BLOCK_LEN_ERROR and ADDRESS_ERROR */
1298 if (cmd.resp[0] & MMC_R1_BLOCK_LEN_ERR) {
1299 dprintf(CRITICAL, "The transferred bytes does not match the block length\n");
1300 return 1;
1301 }
1302
1303 /* Misaligned address not matching block length */
1304 if (cmd.resp[0] & MMC_R1_ADDR_ERR) {
1305 dprintf(CRITICAL, "The misaligned address did not match the block length used\n");
1306 return 1;
1307 }
1308
1309 if (MMC_CARD_STATUS(cmd.resp[0]) != MMC_TRAN_STATE) {
1310 dprintf(CRITICAL, "MMC read failed, card is not in TRAN state\n");
1311 return 1;
1312 }
1313
1314 return mmc_ret;
1315}
Channagoud Kadabi003171e2013-05-29 15:21:12 -07001316
1317/*
1318 * Send the erase group start address using CMD35
1319 */
1320static uint32_t mmc_send_erase_grp_start(struct mmc_device *dev, uint32_t erase_start)
1321{
1322 struct mmc_command cmd;
1323
1324 memset((struct mmc_command *)&cmd, 0, sizeof(struct mmc_command));
1325
1326 cmd.cmd_index = CMD35_ERASE_GROUP_START;
1327 cmd.argument = erase_start;
1328 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
1329 cmd.resp_type = SDHCI_CMD_RESP_R1;
1330
1331 /* send command */
1332 if (sdhci_send_command(&dev->host, &cmd))
1333 return 1;
1334
1335 /*
1336 * CMD35 on failure returns address out of range error
1337 */
1338 if (MMC_ADDR_OUT_OF_RANGE(cmd.resp[0]))
1339 {
1340 dprintf(CRITICAL, "Address for CMD35 is out of range\n");
1341 return 1;
1342 }
1343
1344 return 0;
1345}
1346
1347/*
1348 * Send the erase group end address using CMD36
1349 */
1350static uint32_t mmc_send_erase_grp_end(struct mmc_device *dev, uint32_t erase_end)
1351{
1352 struct mmc_command cmd;
1353
1354 memset((struct mmc_command *)&cmd, 0, sizeof(struct mmc_command));
1355
1356 cmd.cmd_index = CMD36_ERASE_GROUP_END;
1357 cmd.argument = erase_end;
1358 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
1359 cmd.resp_type = SDHCI_CMD_RESP_R1;
1360
1361 /* send command */
1362 if (sdhci_send_command(&dev->host, &cmd))
1363 return 1;
1364
1365 /*
1366 * CMD3 on failure returns address out of range error
1367 */
1368 if (MMC_ADDR_OUT_OF_RANGE(cmd.resp[0]))
1369 {
1370 dprintf(CRITICAL, "Address for CMD36 is out of range\n");
1371 return 1;
1372 }
1373
1374 return 0;
1375}
1376
1377/*
1378 * Send the erase CMD38, to erase the selected erase groups
1379 */
1380static uint32_t mmc_send_erase(struct mmc_device *dev)
1381{
1382 struct mmc_command cmd;
1383 uint32_t status;
Channagoud Kadabi6d1751b2013-07-05 13:03:52 -07001384 uint32_t retry = 0;
Channagoud Kadabi003171e2013-05-29 15:21:12 -07001385
1386 memset((struct mmc_command *)&cmd, 0, sizeof(struct mmc_command));
1387
1388 cmd.cmd_index = CMD38_ERASE;
1389 cmd.argument = 0x00000000;
1390 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
1391 cmd.resp_type = SDHCI_CMD_RESP_R1B;
1392
1393 /* send command */
1394 if (sdhci_send_command(&dev->host, &cmd))
1395 return 1;
1396
1397 do
1398 {
1399 if (mmc_get_card_status(&dev->host, &dev->card, &status))
1400 {
1401 dprintf(CRITICAL, "Failed to get card status after erase\n");
1402 return 1;
1403 }
1404 /* Check if the response of erase command has eras skip status set */
1405 if (status & MMC_R1_WP_ERASE_SKIP)
1406 dprintf(CRITICAL, "Write Protect set for the region, only partial space was erased\n");
1407
1408 retry++;
1409 udelay(1000);
1410 if (retry == MMC_MAX_CARD_STAT_RETRY)
1411 {
1412 dprintf(CRITICAL, "Card status check timed out after sending erase command\n");
1413 return 1;
1414 }
1415 } while(!(status & MMC_READY_FOR_DATA) || (MMC_CARD_STATUS(status) == MMC_PROG_STATE));
1416
1417
1418 return 0;
1419}
1420
1421
1422/*
1423 * Function: mmc sdhci erase
1424 * Arg : mmc device structure, block address and length
1425 * Return : 0 on Success, non zero on failure
1426 * Flow : Fill in the command structure & send the command
1427 */
1428uint32_t mmc_sdhci_erase(struct mmc_device *dev, uint32_t blk_addr, uint64_t len)
1429{
1430 uint32_t erase_unit_sz = 0;
1431 uint32_t erase_start;
1432 uint32_t erase_end;
1433 uint32_t blk_end;
1434 uint32_t num_erase_grps;
1435 uint32_t *out;
1436
1437 /*
1438 * Calculate the erase unit size as per the emmc specification v4.5
1439 */
1440 if (dev->card.ext_csd[MMC_ERASE_GRP_DEF])
1441 erase_unit_sz = (MMC_HC_ERASE_MULT * dev->card.ext_csd[MMC_HC_ERASE_GRP_SIZE]) / MMC_BLK_SZ;
1442 else
1443 erase_unit_sz = (dev->card.csd.erase_grp_size + 1) * (dev->card.csd.erase_grp_mult + 1);
1444
1445 /* Convert length in blocks */
1446 len = len / MMC_BLK_SZ;
1447
1448 if (len < erase_unit_sz)
1449 {
1450 dprintf(CRITICAL, "Requested length is less than min erase group size\n");
1451 return 1;
1452 }
1453
1454 /* Calculate erase groups based on the length in blocks */
1455 num_erase_grps = len / erase_unit_sz;
1456
1457 /* Start address of the erase range */
1458 erase_start = blk_addr;
1459
1460 /* Last address of the erase range */
1461 erase_end = blk_addr + ((num_erase_grps - 1) * erase_unit_sz);
1462
1463 /* Boundary check for overlap */
1464 blk_end = blk_addr + len;
1465
1466 if (erase_end > blk_end)
1467 {
1468 dprintf(CRITICAL, "The erase group overlaps the max requested for erase\n");
1469 erase_end -= erase_unit_sz;
1470 }
1471
1472 /* Send CMD35 for erase group start */
1473 if (mmc_send_erase_grp_start(dev, erase_start))
1474 {
1475 dprintf(CRITICAL, "Failed to send erase grp start address\n");
1476 return 1;
1477 }
1478
1479 /* Send CMD36 for erase group end */
1480 if (mmc_send_erase_grp_end(dev, erase_end))
1481 {
1482 dprintf(CRITICAL, "Failed to send erase grp end address\n");
1483 return 1;
1484 }
1485
1486 /* Send CMD38 to perform erase */
1487 if (mmc_send_erase(dev))
1488 {
1489 dprintf(CRITICAL, "Failed to erase the specified partition\n");
1490 return 1;
1491 }
1492 return 0;
1493}
1494
1495/*
1496 * Function: mmc get wp status
1497 * Arg : mmc device structure, block address and buffer for getting wp status
1498 * Return : 0 on Success, 1 on Failure
1499 * Flow : Get the WP group status by sending CMD31
1500 */
1501uint32_t mmc_get_wp_status(struct mmc_device *dev, uint32_t addr, uint8_t *wp_status)
1502{
1503 struct mmc_command cmd;
1504
1505 memset((struct mmc_command *)&cmd, 0, sizeof(struct mmc_command));
1506
1507 cmd.cmd_index = CMD31_SEND_WRITE_PROT_TYPE;
1508 cmd.argument = addr;
1509 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
1510 cmd.resp_type = SDHCI_CMD_RESP_R1;
1511 cmd.trans_mode = SDHCI_MMC_READ;
1512 cmd.data_present = 0x1;
1513 cmd.data.data_ptr = wp_status;
1514 cmd.data.num_blocks = 0x1;
1515 cmd.data.blk_sz = 0x8;
1516
1517 if (sdhci_send_command(&dev->host, &cmd))
1518 {
1519 dprintf(CRITICAL, "Failed to get status of write protect bits\n");
1520 return 1;
1521 }
1522
1523 return 0;
1524}
1525
1526/*
1527 * Function: mmc set/clear WP on user area
1528 * Arg : mmc device structure, block address,len, & flag to set or clear
1529 * Return : 0 on success, 1 on failure
1530 * Flow : Function to set/clear power on write protect on user area
1531 */
1532
1533uint32_t mmc_set_clr_power_on_wp_user(struct mmc_device *dev, uint32_t addr, uint64_t len, uint8_t set_clr)
1534{
1535 struct mmc_command cmd;
1536 uint32_t wp_grp_size;
1537 uint32_t status;
1538 uint32_t num_wp_grps;
1539 uint32_t ret;
Channagoud Kadabi6d1751b2013-07-05 13:03:52 -07001540 uint32_t retry = 0;
Channagoud Kadabi003171e2013-05-29 15:21:12 -07001541 uint32_t i;
1542
1543 memset((struct mmc_command *)&cmd, 0, sizeof(struct mmc_command));
1544
1545 /* Convert len into blocks */
1546 len = len / MMC_BLK_SZ;
1547
1548 /* Disable PERM WP */
1549 ret = mmc_switch_cmd(&dev->host, &dev->card, MMC_SET_BIT, MMC_USR_WP, MMC_US_PERM_WP_DIS);
1550
1551 if (ret)
1552 {
1553 dprintf(CRITICAL, "Failed to Disable PERM WP\n");
1554 return ret;
1555 }
1556
1557 /* Read the default values for user WP */
1558 ret = mmc_get_ext_csd(&dev->host, &dev->card);
1559
1560 if (ret)
1561 {
1562 dprintf(CRITICAL, "Failed to read ext csd for the card\n");
1563 return ret;
1564 }
1565
1566 /* Check if user power on WP is disabled or perm WP is enabled */
1567 if ((dev->card.ext_csd[MMC_USR_WP] & MMC_US_PWR_WP_DIS)
1568 || (dev->card.ext_csd[MMC_USR_WP] & MMC_US_PERM_WP_EN))
1569 {
1570 dprintf(CRITICAL, "Power on protection is disabled, cannot be set\n");
1571 return 1;
1572 }
1573
1574 /* Calculate the wp grp size */
1575 if (dev->card.ext_csd[MMC_ERASE_GRP_DEF])
1576 wp_grp_size = MMC_HC_ERASE_MULT * dev->card.ext_csd[MMC_HC_ERASE_GRP_SIZE] / MMC_BLK_SZ;
1577 else
1578 wp_grp_size = (dev->card.csd.wp_grp_size + 1) * (dev->card.csd.erase_grp_size + 1) \
1579 * (dev->card.csd.erase_grp_mult + 1);
1580
1581
1582 if (len < wp_grp_size)
1583 {
1584 dprintf(CRITICAL, "Length is less than min WP size, WP was not set\n");
1585 return 1;
1586 }
1587
1588 /* Set power on USER WP */
1589 ret = mmc_switch_cmd(&dev->host, &dev->card, MMC_SET_BIT, MMC_USR_WP, MMC_US_PWR_WP_EN);
1590
1591 if (ret)
1592 {
1593 dprintf(CRITICAL, "Failed to set power on WP for user\n");
1594 return ret;
1595 }
1596
1597 num_wp_grps = ROUNDUP(len, wp_grp_size) / wp_grp_size;
1598
1599 if (set_clr)
1600 cmd.cmd_index = CMD28_SET_WRITE_PROTECT;
1601 else
1602 cmd.cmd_index = CMD29_CLEAR_WRITE_PROTECT;
1603
1604 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
1605 cmd.resp_type = SDHCI_CMD_RESP_R1B;
1606
1607 for(i = 0; i < num_wp_grps; i++)
1608 {
1609 cmd.argument = addr + (i * wp_grp_size);
1610
1611 if (sdhci_send_command(&dev->host, &cmd))
1612 return 1;
1613
1614 /* CMD28/CMD29 On failure returns address out of range error */
1615 if (MMC_ADDR_OUT_OF_RANGE(cmd.resp[0]))
1616 {
1617 dprintf(CRITICAL, "Address for CMD28/29 is out of range\n");
1618 return 1;
1619 }
1620
1621 /* Check the card status */
1622 do
1623 {
1624 if (mmc_get_card_status(&dev->host, &dev->card, &status))
1625 {
1626 dprintf(CRITICAL, "Failed to get card status afterapplying write protect\n");
1627 return 1;
1628 }
1629
1630 /* Time out for WP command */
1631 retry++;
1632 udelay(1000);
1633 if (retry == MMC_MAX_CARD_STAT_RETRY)
1634 {
1635 dprintf(CRITICAL, "Card status timed out after sending write protect command\n");
1636 return 1;
1637 }
1638 } while (!(status & MMC_READY_FOR_DATA) || (MMC_CARD_STATUS(status) == MMC_PROG_STATE));
1639
1640 }
1641
1642 return 0;
1643}
Channagoud Kadabi9e3c3b92013-06-18 18:32:32 -07001644
1645/* Function to put the mmc card to sleep */
1646void mmc_put_card_to_sleep(struct mmc_device *dev)
1647{
1648 struct mmc_command cmd = {0};
1649 struct mmc_card *card = &dev->card;
1650
1651 cmd.cmd_index = CMD7_SELECT_DESELECT_CARD;
1652 cmd.argument = 0x00000000;
1653 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
1654 cmd.resp_type = SDHCI_CMD_RESP_NONE;
1655
1656 /* send command */
1657 if(sdhci_send_command(&dev->host, &cmd))
1658 {
1659 dprintf(CRITICAL, "card deselect error: %s\n", __func__);
1660 return;
1661 }
1662
1663 cmd.cmd_index = CMD5_SLEEP_AWAKE;
1664 cmd.argument = (card->rca << MMC_CARD_RCA_BIT) | MMC_CARD_SLEEP;
1665 cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
1666 cmd.resp_type = SDHCI_CMD_RESP_R1B;
1667
1668 /* send command */
1669 if(sdhci_send_command(&dev->host, &cmd))
1670 dprintf(CRITICAL, "card sleep error: %s\n", __func__);
1671}