blob: b1fc977f0d71b4c1cf83fa3a5ea8090e66669270 [file] [log] [blame]
Ajay Dudanib01e5062011-12-03 23:23:42 -08001/* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
Kinson Chikf1a43512011-07-14 11:28:39 -07002
Ajay Dudanib01e5062011-12-03 23:23:42 -08003 * 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 Code Aurora Forum, Inc. 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 */
Greg Grisco6e754772011-06-23 12:19:39 -070028
Ajay Dudanib01e5062011-12-03 23:23:42 -080029#include <string.h>
30#include <stdlib.h>
31#include <debug.h>
32#include <reg.h>
33#include "mmc.h"
34#include <partition_parser.h>
35#include <platform/iomap.h>
36#include <platform/timer.h>
Greg Griscod6250552011-06-29 14:40:23 -070037
Ajay Dudanib01e5062011-12-03 23:23:42 -080038#if MMC_BOOT_ADM
39#include "adm.h"
40#endif
Greg Griscod6250552011-06-29 14:40:23 -070041
Ajay Dudanib01e5062011-12-03 23:23:42 -080042#ifndef NULL
43#define NULL 0
44#endif
Greg Griscod6250552011-06-29 14:40:23 -070045
Ajay Dudanib01e5062011-12-03 23:23:42 -080046#define MMC_BOOT_DATA_READ 0
47#define MMC_BOOT_DATA_WRITE 1
Greg Griscod6250552011-06-29 14:40:23 -070048
Ajay Dudanib01e5062011-12-03 23:23:42 -080049static unsigned int mmc_boot_fifo_data_transfer(unsigned int *data_ptr,
50 unsigned int data_len,
51 unsigned char direction);
Greg Griscod6250552011-06-29 14:40:23 -070052
Ajay Dudanib01e5062011-12-03 23:23:42 -080053static unsigned int mmc_boot_fifo_read(unsigned int *data_ptr,
54 unsigned int data_len);
Greg Griscod6250552011-06-29 14:40:23 -070055
Ajay Dudanib01e5062011-12-03 23:23:42 -080056static unsigned int mmc_boot_fifo_write(unsigned int *data_ptr,
57 unsigned int data_len);
Greg Grisco6e754772011-06-23 12:19:39 -070058
Ajay Dudanib01e5062011-12-03 23:23:42 -080059#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Greg Grisco6e754772011-06-23 12:19:39 -070060
Ajay Dudanib01e5062011-12-03 23:23:42 -080061/* data access time unit in ns */
62static const unsigned int taac_unit[] =
63 { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000 };
64/* data access time value x 10 */
65static const unsigned int taac_value[] =
66 { 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 };
Greg Grisco6e754772011-06-23 12:19:39 -070067
Ajay Dudanib01e5062011-12-03 23:23:42 -080068/* data transfer rate in kbit/s */
69static const unsigned int xfer_rate_unit[] =
70 { 100, 1000, 10000, 100000, 0, 0, 0, 0 };
71/* data transfer rate value x 10*/
72static const unsigned int xfer_rate_value[] =
73 { 0, 10, 12, 13, 15, 20, 26, 30, 35, 40, 45, 52, 55, 60, 70, 80 };
74
75unsigned char mmc_slot = 0;
76unsigned int mmc_boot_mci_base = 0;
77
78static unsigned char ext_csd_buf[512];
79static unsigned char wp_status_buf[8];
80
81int mmc_clock_enable_disable(unsigned id, unsigned enable);
82int mmc_clock_get_rate(unsigned id);
83int mmc_clock_set_rate(unsigned id, unsigned rate);
84
85struct mmc_boot_host mmc_host;
86struct mmc_boot_card mmc_card;
87
88static unsigned int mmc_wp(unsigned int addr, unsigned int size,
89 unsigned char set_clear_wp);
90static unsigned int mmc_boot_send_ext_cmd(struct mmc_boot_card *card,
91 unsigned char *buf);
92static unsigned int mmc_boot_read_reg(struct mmc_boot_card *card,
93 unsigned int data_len,
94 unsigned int command,
95 unsigned int addr, unsigned int *out);
96
97unsigned int SWAP_ENDIAN(unsigned int val)
98{
99 return ((val & 0xFF) << 24) |
100 (((val >> 8) & 0xFF) << 16) | (((val >> 16) & 0xFF) << 8) | (val >>
101 24);
Amol Jadi84a546a2011-03-02 12:09:11 -0800102}
103
Ajay Dudanib01e5062011-12-03 23:23:42 -0800104/* Sets a timeout for read operation.
105 */
106static unsigned int
107mmc_boot_set_read_timeout(struct mmc_boot_host *host,
108 struct mmc_boot_card *card)
109{
110 unsigned int timeout_ns = 0;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700111
Ajay Dudanib01e5062011-12-03 23:23:42 -0800112 if ((host == NULL) || (card == NULL)) {
113 return MMC_BOOT_E_INVAL;
114 }
115
116 if ((card->type == MMC_BOOT_TYPE_MMCHC)
117 || (card->type == MMC_BOOT_TYPE_SDHC)) {
118 card->rd_timeout_ns = 100000000;
119 } else if ((card->type == MMC_BOOT_TYPE_STD_SD)
120 || (card->type == MMC_BOOT_TYPE_STD_MMC)) {
121 timeout_ns = 10 * ((card->csd.taac_ns) +
122 (card->csd.nsac_clk_cycle /
123 (host->mclk_rate / 1000000000)));
124 card->rd_timeout_ns = timeout_ns;
125 } else {
126 return MMC_BOOT_E_NOT_SUPPORTED;
127 }
128
129 dprintf(SPEW, " Read timeout set: %d ns\n", card->rd_timeout_ns);
130
131 return MMC_BOOT_E_SUCCESS;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700132}
133
Ajay Dudanib01e5062011-12-03 23:23:42 -0800134/* Sets a timeout for write operation.
135 */
136static unsigned int
137mmc_boot_set_write_timeout(struct mmc_boot_host *host,
138 struct mmc_boot_card *card)
139{
140 unsigned int timeout_ns = 0;
141
142 if ((host == NULL) || (card == NULL)) {
143 return MMC_BOOT_E_INVAL;
144 }
145
146 if ((card->type == MMC_BOOT_TYPE_MMCHC)
147 || (card->type == MMC_BOOT_TYPE_SDHC)) {
148 card->wr_timeout_ns = 100000000;
149 } else if (card->type == MMC_BOOT_TYPE_STD_SD
150 || (card->type == MMC_BOOT_TYPE_STD_MMC)) {
151 timeout_ns = 10 * ((card->csd.taac_ns) +
152 (card->csd.nsac_clk_cycle /
153 (host->mclk_rate / 1000000000)));
154 timeout_ns = timeout_ns << card->csd.r2w_factor;
155 card->wr_timeout_ns = timeout_ns;
156 } else {
157 return MMC_BOOT_E_NOT_SUPPORTED;
158 }
159
160 dprintf(SPEW, " Write timeout set: %d ns\n", card->wr_timeout_ns);
161
162 return MMC_BOOT_E_SUCCESS;
163}
164
165/*
166 * Decodes CSD response received from the card. Note that we have defined only
167 * few of the CSD elements in csd structure. We'll only decode those values.
168 */
169static unsigned int
170mmc_boot_decode_and_save_csd(struct mmc_boot_card *card, unsigned int *raw_csd)
171{
172 unsigned int mmc_sizeof = 0;
173 unsigned int mmc_unit = 0;
174 unsigned int mmc_value = 0;
175 unsigned int mmc_temp = 0;
176
177 struct mmc_boot_csd mmc_csd;
178
179 if ((card == NULL) || (raw_csd == NULL)) {
180 return MMC_BOOT_E_INVAL;
181 }
182
183 mmc_sizeof = sizeof(unsigned int) * 8;
184
185 mmc_csd.cmmc_structure = UNPACK_BITS(raw_csd, 126, 2, mmc_sizeof);
186
187 if ((card->type == MMC_BOOT_TYPE_SDHC)
188 || (card->type == MMC_BOOT_TYPE_STD_SD)) {
189 /* Parse CSD according to SD card spec. */
190
191 /* CSD register is little bit differnet for CSD version 2.0 High Capacity
192 * and CSD version 1.0/2.0 Standard memory cards. In Version 2.0 some of
193 * the fields have fixed values and it's not necessary for host to refer
194 * these fields in CSD sent by card */
195
196 if (mmc_csd.cmmc_structure == 1) {
197 /* CSD Version 2.0 */
198 mmc_csd.card_cmd_class =
199 UNPACK_BITS(raw_csd, 84, 12, mmc_sizeof);
200 mmc_csd.write_blk_len = 512; /* Fixed value is 9 = 2^9 = 512 */
201 mmc_csd.read_blk_len = 512; /* Fixed value is 9 = 512 */
202 mmc_csd.r2w_factor = 0x2; /* Fixed value: 010b */
203 mmc_csd.c_size_mult = 0; /* not there in version 2.0 */
204 mmc_csd.c_size =
205 UNPACK_BITS(raw_csd, 48, 22, mmc_sizeof);
206 mmc_csd.nsac_clk_cycle =
207 UNPACK_BITS(raw_csd, 104, 8, mmc_sizeof) * 100;
208
209//TODO: Investigate the nsac and taac. Spec suggests not using this for timeouts.
210
211 mmc_unit = UNPACK_BITS(raw_csd, 112, 3, mmc_sizeof);
212 mmc_value = UNPACK_BITS(raw_csd, 115, 4, mmc_sizeof);
213 mmc_csd.taac_ns =
214 (taac_value[mmc_value] * taac_unit[mmc_unit]) / 10;
215
216 mmc_csd.erase_blk_len = 1;
217 mmc_csd.read_blk_misalign = 0;
218 mmc_csd.write_blk_misalign = 0;
219 mmc_csd.read_blk_partial = 0;
220 mmc_csd.write_blk_partial = 0;
221
222 mmc_unit = UNPACK_BITS(raw_csd, 96, 3, mmc_sizeof);
223 mmc_value = UNPACK_BITS(raw_csd, 99, 4, mmc_sizeof);
224 mmc_csd.tran_speed =
225 (xfer_rate_value[mmc_value] *
226 xfer_rate_unit[mmc_unit]) / 10;
227
228 mmc_csd.wp_grp_size = 0x0;
229 mmc_csd.wp_grp_enable = 0x0;
230 mmc_csd.perm_wp =
231 UNPACK_BITS(raw_csd, 13, 1, mmc_sizeof);
232 mmc_csd.temp_wp =
233 UNPACK_BITS(raw_csd, 12, 1, mmc_sizeof);
234
235 /* Calculate the card capcity */
236 card->capacity = (1 + mmc_csd.c_size) * 512 * 1024;
237 } else {
238 /* CSD Version 1.0 */
239 mmc_csd.card_cmd_class =
240 UNPACK_BITS(raw_csd, 84, 12, mmc_sizeof);
241
242 mmc_temp = UNPACK_BITS(raw_csd, 22, 4, mmc_sizeof);
243 mmc_csd.write_blk_len = (mmc_temp > 8
244 && mmc_temp <
245 12) ? (1 << mmc_temp) : 512;
246
247 mmc_temp = UNPACK_BITS(raw_csd, 80, 4, mmc_sizeof);
248 mmc_csd.read_blk_len = (mmc_temp > 8
249 && mmc_temp <
250 12) ? (1 << mmc_temp) : 512;
251
252 mmc_unit = UNPACK_BITS(raw_csd, 112, 3, mmc_sizeof);
253 mmc_value = UNPACK_BITS(raw_csd, 115, 4, mmc_sizeof);
254 mmc_csd.taac_ns =
255 (taac_value[mmc_value] * taac_unit[mmc_unit]) / 10;
256
257 mmc_unit = UNPACK_BITS(raw_csd, 96, 3, mmc_sizeof);
258 mmc_value = UNPACK_BITS(raw_csd, 99, 4, mmc_sizeof);
259 mmc_csd.tran_speed =
260 (xfer_rate_value[mmc_value] *
261 xfer_rate_unit[mmc_unit]) / 10;
262
263 mmc_csd.nsac_clk_cycle =
264 UNPACK_BITS(raw_csd, 104, 8, mmc_sizeof) * 100;
265
266 mmc_csd.r2w_factor =
267 UNPACK_BITS(raw_csd, 26, 3, mmc_sizeof);
268 mmc_csd.sector_size =
269 UNPACK_BITS(raw_csd, 39, 7, mmc_sizeof) + 1;
270
271 mmc_csd.erase_blk_len =
272 UNPACK_BITS(raw_csd, 46, 1, mmc_sizeof);
273 mmc_csd.read_blk_misalign =
274 UNPACK_BITS(raw_csd, 77, 1, mmc_sizeof);
275 mmc_csd.write_blk_misalign =
276 UNPACK_BITS(raw_csd, 78, 1, mmc_sizeof);
277 mmc_csd.read_blk_partial =
278 UNPACK_BITS(raw_csd, 79, 1, mmc_sizeof);
279 mmc_csd.write_blk_partial =
280 UNPACK_BITS(raw_csd, 21, 1, mmc_sizeof);
281
282 mmc_csd.c_size_mult =
283 UNPACK_BITS(raw_csd, 47, 3, mmc_sizeof);
284 mmc_csd.c_size =
285 UNPACK_BITS(raw_csd, 62, 12, mmc_sizeof);
286 mmc_csd.wp_grp_size =
287 UNPACK_BITS(raw_csd, 32, 7, mmc_sizeof);
288 mmc_csd.wp_grp_enable =
289 UNPACK_BITS(raw_csd, 31, 1, mmc_sizeof);
290 mmc_csd.perm_wp =
291 UNPACK_BITS(raw_csd, 13, 1, mmc_sizeof);
292 mmc_csd.temp_wp =
293 UNPACK_BITS(raw_csd, 12, 1, mmc_sizeof);
294
295 /* Calculate the card capacity */
296 mmc_temp =
297 (1 << (mmc_csd.c_size_mult + 2)) * (mmc_csd.c_size +
298 1);
299 card->capacity = mmc_temp * mmc_csd.read_blk_len;
300 }
301 } else {
302 /* Parse CSD according to MMC card spec. */
303 mmc_csd.spec_vers = UNPACK_BITS(raw_csd, 122, 4, mmc_sizeof);
304 mmc_csd.card_cmd_class =
305 UNPACK_BITS(raw_csd, 84, 12, mmc_sizeof);
306 mmc_csd.write_blk_len =
307 1 << UNPACK_BITS(raw_csd, 22, 4, mmc_sizeof);
308 mmc_csd.read_blk_len =
309 1 << UNPACK_BITS(raw_csd, 80, 4, mmc_sizeof);
310 mmc_csd.r2w_factor = UNPACK_BITS(raw_csd, 26, 3, mmc_sizeof);
311 mmc_csd.c_size_mult = UNPACK_BITS(raw_csd, 47, 3, mmc_sizeof);
312 mmc_csd.c_size = UNPACK_BITS(raw_csd, 62, 12, mmc_sizeof);
313 mmc_csd.nsac_clk_cycle =
314 UNPACK_BITS(raw_csd, 104, 8, mmc_sizeof) * 100;
315
316 mmc_unit = UNPACK_BITS(raw_csd, 112, 3, mmc_sizeof);
317 mmc_value = UNPACK_BITS(raw_csd, 115, 4, mmc_sizeof);
318 mmc_csd.taac_ns =
319 (taac_value[mmc_value] * taac_unit[mmc_unit]) / 10;
320
321 mmc_csd.read_blk_misalign =
322 UNPACK_BITS(raw_csd, 77, 1, mmc_sizeof);
323 mmc_csd.write_blk_misalign =
324 UNPACK_BITS(raw_csd, 78, 1, mmc_sizeof);
325 mmc_csd.read_blk_partial =
326 UNPACK_BITS(raw_csd, 79, 1, mmc_sizeof);
327 mmc_csd.write_blk_partial =
328 UNPACK_BITS(raw_csd, 21, 1, mmc_sizeof);
329 mmc_csd.tran_speed = 0x00; /* Ignore -- no use of this value. */
330
331 mmc_csd.erase_grp_size =
332 UNPACK_BITS(raw_csd, 42, 5, mmc_sizeof);
333 mmc_csd.erase_grp_mult =
334 UNPACK_BITS(raw_csd, 37, 5, mmc_sizeof);
335 mmc_csd.wp_grp_size = UNPACK_BITS(raw_csd, 32, 5, mmc_sizeof);
336 mmc_csd.wp_grp_enable = UNPACK_BITS(raw_csd, 31, 1, mmc_sizeof);
337 mmc_csd.perm_wp = UNPACK_BITS(raw_csd, 13, 1, mmc_sizeof);
338 mmc_csd.temp_wp = UNPACK_BITS(raw_csd, 12, 1, mmc_sizeof);
339
340 /* Calculate the card capcity */
341 if (mmc_csd.c_size != 0xFFF) {
342 /* For cards less than or equal to 2GB */
343 mmc_temp =
344 (1 << (mmc_csd.c_size_mult + 2)) * (mmc_csd.c_size +
345 1);
346 card->capacity = mmc_temp * mmc_csd.read_blk_len;
347 } else {
348 /* For cards greater than 2GB, Ext CSD register's SEC_COUNT
349 * is used to calculate the size.
350 */
351 unsigned long long sec_count;
352
353 sec_count = (ext_csd_buf[215] << 24) |
354 (ext_csd_buf[214] << 16) |
355 (ext_csd_buf[213] << 8) | ext_csd_buf[212];
356
357 card->capacity = sec_count * 512;
358 }
359 }
360
361 /* save the information in card structure */
362 memcpy((struct mmc_boot_csd *)&card->csd,
363 (struct mmc_boot_csd *)&mmc_csd, sizeof(struct mmc_boot_csd));
364
365 dprintf(SPEW, "Decoded CSD fields:\n");
366 dprintf(SPEW, "cmmc_structure: %d\n", mmc_csd.cmmc_structure);
367 dprintf(SPEW, "card_cmd_class: %x\n", mmc_csd.card_cmd_class);
368 dprintf(SPEW, "write_blk_len: %d\n", mmc_csd.write_blk_len);
369 dprintf(SPEW, "read_blk_len: %d\n", mmc_csd.read_blk_len);
370 dprintf(SPEW, "r2w_factor: %d\n", mmc_csd.r2w_factor);
371 dprintf(SPEW, "sector_size: %d\n", mmc_csd.sector_size);
372 dprintf(SPEW, "c_size_mult:%d\n", mmc_csd.c_size_mult);
373 dprintf(SPEW, "c_size: %d\n", mmc_csd.c_size);
374 dprintf(SPEW, "nsac_clk_cycle: %d\n", mmc_csd.nsac_clk_cycle);
375 dprintf(SPEW, "taac_ns: %d\n", mmc_csd.taac_ns);
376 dprintf(SPEW, "tran_speed: %d kbps\n", mmc_csd.tran_speed);
377 dprintf(SPEW, "erase_blk_len: %d\n", mmc_csd.erase_blk_len);
378 dprintf(SPEW, "read_blk_misalign: %d\n", mmc_csd.read_blk_misalign);
379 dprintf(SPEW, "write_blk_misalign: %d\n", mmc_csd.write_blk_misalign);
380 dprintf(SPEW, "read_blk_partial: %d\n", mmc_csd.read_blk_partial);
381 dprintf(SPEW, "write_blk_partial: %d\n", mmc_csd.write_blk_partial);
382 dprintf(SPEW, "Card Capacity: %llu Bytes\n", card->capacity);
383
384 return MMC_BOOT_E_SUCCESS;
385}
386
387/*
388 * Decode CID sent by the card.
389 */
390static unsigned int
391mmc_boot_decode_and_save_cid(struct mmc_boot_card *card, unsigned int *raw_cid)
392{
393 struct mmc_boot_cid mmc_cid;
394 unsigned int mmc_sizeof = 0;
395 int i = 0;
396
397 if ((card == NULL) || (raw_cid == NULL)) {
398 return MMC_BOOT_E_INVAL;
399 }
400
401 mmc_sizeof = sizeof(unsigned int) * 8;
402
403 if ((card->type == MMC_BOOT_TYPE_SDHC)
404 || (card->type == MMC_BOOT_TYPE_STD_SD)) {
405 mmc_cid.mid = UNPACK_BITS(raw_cid, 120, 8, mmc_sizeof);
406 mmc_cid.oid = UNPACK_BITS(raw_cid, 104, 16, mmc_sizeof);
407
408 for (i = 0; i < 5; i++) {
409 mmc_cid.pnm[i] = (unsigned char)UNPACK_BITS(raw_cid,
410 (104 -
411 8 * (i +
412 1)),
413 8,
414 mmc_sizeof);
415 }
416 mmc_cid.pnm[5] = 0;
417 mmc_cid.pnm[6] = 0;
418
419 mmc_cid.prv = UNPACK_BITS(raw_cid, 56, 8, mmc_sizeof);
420 mmc_cid.psn = UNPACK_BITS(raw_cid, 24, 32, mmc_sizeof);
421 mmc_cid.month = UNPACK_BITS(raw_cid, 8, 4, mmc_sizeof);
422 mmc_cid.year = UNPACK_BITS(raw_cid, 12, 8, mmc_sizeof);
423 mmc_cid.year += 2000;
424 } else {
425 mmc_cid.mid = UNPACK_BITS(raw_cid, 120, 8, mmc_sizeof);
426 mmc_cid.oid = UNPACK_BITS(raw_cid, 104, 16, mmc_sizeof);
427
428 for (i = 0; i < 6; i++) {
429 mmc_cid.pnm[i] = (unsigned char)UNPACK_BITS(raw_cid,
430 (104 -
431 8 * (i +
432 1)),
433 8,
434 mmc_sizeof);
435 }
436 mmc_cid.pnm[6] = 0;
437
438 mmc_cid.prv = UNPACK_BITS(raw_cid, 48, 8, mmc_sizeof);
439 mmc_cid.psn = UNPACK_BITS(raw_cid, 16, 32, mmc_sizeof);
440 mmc_cid.month = UNPACK_BITS(raw_cid, 8, 4, mmc_sizeof);
441 mmc_cid.year = UNPACK_BITS(raw_cid, 12, 4, mmc_sizeof);
442 mmc_cid.year += 1997;
443 }
444
445 /* save it in card database */
446 memcpy((struct mmc_boot_cid *)&card->cid,
447 (struct mmc_boot_cid *)&mmc_cid, sizeof(struct mmc_boot_cid));
448
449 dprintf(SPEW, "Decoded CID fields:\n");
450 dprintf(SPEW, "Manufacturer ID: %x\n", mmc_cid.mid);
451 dprintf(SPEW, "OEM ID: 0x%x\n", mmc_cid.oid);
452 dprintf(SPEW, "Product Name: %s\n", mmc_cid.pnm);
453 dprintf(SPEW, "Product revision: %d.%d\n", (mmc_cid.prv >> 4),
454 (mmc_cid.prv & 0xF));
455 dprintf(SPEW, "Product serial number: %X\n", mmc_cid.psn);
456 dprintf(SPEW, "Manufacturing date: %d %d\n", mmc_cid.month,
457 mmc_cid.year);
458
459 return MMC_BOOT_E_SUCCESS;
460}
461
462/*
463 * Sends specified command to a card and waits for a response.
464 */
465static unsigned int mmc_boot_send_command(struct mmc_boot_command *cmd)
466{
467 unsigned int mmc_cmd = 0;
468 unsigned int mmc_status = 0;
469 unsigned int mmc_resp = 0;
470 unsigned int mmc_return = MMC_BOOT_E_SUCCESS;
471 unsigned int cmd_index = 0;
472 int i = 0;
473
474 /* basic check */
475 if (cmd == NULL) {
476 return MMC_BOOT_E_INVAL;
477 }
478
479 /* 1. Write command argument to MMC_BOOT_MCI_ARGUMENT register */
480 writel(cmd->argument, MMC_BOOT_MCI_ARGUMENT);
481
482 /* Writes to MCI port are not effective for 3 ticks of PCLK.
483 * The min pclk is 144KHz which gives 6.94 us/tick.
484 * Thus 21us == 3 ticks.
485 */
486 udelay(21);
487
488 /* 2. Set appropriate fields and write MMC_BOOT_MCI_CMD */
489 /* 2a. Write command index in CMD_INDEX field */
490 cmd_index = cmd->cmd_index;
491 mmc_cmd |= cmd->cmd_index;
492 /* 2b. Set RESPONSE bit to 1 for all cmds except CMD0 */
493 if (cmd_index != CMD0_GO_IDLE_STATE) {
494 mmc_cmd |= MMC_BOOT_MCI_CMD_RESPONSE;
495 }
496
497 /* 2c. Set LONGRESP bit to 1 for CMD2, CMD9 and CMD10 */
498 if (IS_RESP_136_BITS(cmd->resp_type)) {
499 mmc_cmd |= MMC_BOOT_MCI_CMD_LONGRSP;
500 }
501
502 /* 2d. Set INTERRUPT bit to 1 to disable command timeout */
503
504 /* 2e. Set PENDING bit to 1 for CMD12 in the beginning of stream
505 mode data transfer */
506 if (cmd->xfer_mode == MMC_BOOT_XFER_MODE_STREAM) {
507 mmc_cmd |= MMC_BOOT_MCI_CMD_PENDING;
508 }
509
510 /* 2f. Set ENABLE bit to 1 */
511 mmc_cmd |= MMC_BOOT_MCI_CMD_ENABLE;
512
513 /* 2g. Set PROG_ENA bit to 1 for CMD12, CMD13 issued at the end of
514 write data transfer */
515 if ((cmd_index == CMD12_STOP_TRANSMISSION ||
516 cmd_index == CMD13_SEND_STATUS) && cmd->prg_enabled) {
517 mmc_cmd |= MMC_BOOT_MCI_CMD_PROG_ENA;
518 }
519
520 /* 2h. Set MCIABORT bit to 1 for CMD12 when working with SDIO card */
521 /* 2i. Set CCS_ENABLE bit to 1 for CMD61 when Command Completion Signal
522 of CE-ATA device is enabled */
523
524 /* 2j. clear all static status bits */
525 writel(MMC_BOOT_MCI_STATIC_STATUS, MMC_BOOT_MCI_CLEAR);
526
527 /* 2k. Write to MMC_BOOT_MCI_CMD register */
528 writel(mmc_cmd, MMC_BOOT_MCI_CMD);
529
530 dprintf(SPEW, "Command sent: CMD%d MCI_CMD_REG:%x MCI_ARG:%x\n",
531 cmd_index, mmc_cmd, cmd->argument);
532
533 /* 3. Wait for interrupt or poll on the following bits of MCI_STATUS
534 register */
535 do {
536 /* 3a. Read MCI_STATUS register */
537 while (readl(MMC_BOOT_MCI_STATUS) &
538 MMC_BOOT_MCI_STAT_CMD_ACTIVE) ;
539
540 mmc_status = readl(MMC_BOOT_MCI_STATUS);
541
542 /* 3b. CMD_SENT bit supposed to be set to 1 only after CMD0 is sent -
543 no response required. */
544 if ((cmd->resp_type == MMC_BOOT_RESP_NONE) &&
545 (mmc_status & MMC_BOOT_MCI_STAT_CMD_SENT)) {
546 break;
547 }
548
549 /* 3c. If CMD_TIMEOUT bit is set then no response was received */
550 else if (mmc_status & MMC_BOOT_MCI_STAT_CMD_TIMEOUT) {
551 mmc_return = MMC_BOOT_E_TIMEOUT;
552 break;
553 }
554 /* 3d. If CMD_RESPONSE_END bit is set to 1 then command's response was
555 received and CRC check passed
556 Spcial case for ACMD41: it seems to always fail CRC even if
557 the response is valid
558 */
559 else if ((mmc_status & MMC_BOOT_MCI_STAT_CMD_RESP_END)
560 || (cmd_index == CMD1_SEND_OP_COND)
561 || (cmd_index == CMD8_SEND_IF_COND)) {
562 /* 3i. Read MCI_RESP_CMD register to verify that response index is
563 equal to command index */
564 mmc_resp = readl(MMC_BOOT_MCI_RESP_CMD) & 0x3F;
565
566 /* However, long response does not contain the command index field.
567 * In that case, response index field must be set to 111111b (0x3F) */
568 if ((mmc_resp == cmd_index) ||
569 (cmd->resp_type == MMC_BOOT_RESP_R2 ||
570 cmd->resp_type == MMC_BOOT_RESP_R3 ||
571 cmd->resp_type == MMC_BOOT_RESP_R6 ||
572 cmd->resp_type == MMC_BOOT_RESP_R7)) {
573 /* 3j. If resp index is equal to cmd index, read command resp
574 from MCI_RESPn registers
575 - MCI_RESP0/1/2/3 for CMD2/9/10
576 - MCI_RESP0 for all other registers */
577 if (IS_RESP_136_BITS(cmd->resp_type)) {
578 for (i = 0; i < 4; i++) {
579 cmd->resp[3 - i] =
580 readl(MMC_BOOT_MCI_RESP_0 +
581 (i * 4));
582
583 }
584 } else {
585 cmd->resp[0] =
586 readl(MMC_BOOT_MCI_RESP_0);
587 }
588 } else {
589 /* command index mis-match */
590 mmc_return = MMC_BOOT_E_CMD_INDX_MISMATCH;
591 }
592
593 dprintf(SPEW, "Command response received: %X\n",
594 cmd->resp[0]);
595 break;
596 }
597
598 /* 3e. If CMD_CRC_FAIL bit is set to 1 then cmd's response was recvd,
599 but CRC check failed. */
600 else if ((mmc_status & MMC_BOOT_MCI_STAT_CMD_CRC_FAIL)) {
601 if (cmd_index == ACMD41_SEND_OP_COND) {
602 cmd->resp[0] = readl(MMC_BOOT_MCI_RESP_0);
603 } else
604 mmc_return = MMC_BOOT_E_CRC_FAIL;
605 break;
606 }
607
608 }
609 while (1);
610
611 return mmc_return;
612}
613
614/*
615 * Reset all the cards to idle condition (CMD 0)
616 */
617static unsigned int mmc_boot_reset_cards(void)
618{
619 struct mmc_boot_command cmd;
620
621 memset((struct mmc_boot_command *)&cmd, 0,
622 sizeof(struct mmc_boot_command));
623
624 cmd.cmd_index = CMD0_GO_IDLE_STATE;
625 cmd.argument = 0; // stuff bits - ignored
626 cmd.cmd_type = MMC_BOOT_CMD_BCAST;
627 cmd.resp_type = MMC_BOOT_RESP_NONE;
628
629 /* send command */
630 return mmc_boot_send_command(&cmd);
631}
632
633/*
634 * Send CMD1 to know whether the card supports host VDD profile or not.
635 */
636static unsigned int
637mmc_boot_send_op_cond(struct mmc_boot_host *host, struct mmc_boot_card *card)
638{
639 struct mmc_boot_command cmd;
640 unsigned int mmc_resp = 0;
641 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
642
643 /* basic check */
644 if ((host == NULL) || (card == NULL)) {
645 return MMC_BOOT_E_INVAL;
646 }
647
648 memset((struct mmc_boot_command *)&cmd, 0,
649 sizeof(struct mmc_boot_command));
650
651 /* CMD1 format:
652 * [31] Busy bit
653 * [30:29] Access mode
654 * [28:24] reserved
655 * [23:15] 2.7-3.6
656 * [14:8] 2.0-2.6
657 * [7] 1.7-1.95
658 * [6:0] reserved
659 */
660
661 cmd.cmd_index = CMD1_SEND_OP_COND;
662 cmd.argument = host->ocr;
663 cmd.cmd_type = MMC_BOOT_CMD_BCAST_W_RESP;
664 cmd.resp_type = MMC_BOOT_RESP_R3;
665
666 mmc_ret = mmc_boot_send_command(&cmd);
667 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
668 return mmc_ret;
669 }
670
671 /* Now it's time to examine response */
672 mmc_resp = cmd.resp[0];
673
674 /* Response contains card's ocr. Update card's information */
675 card->ocr = mmc_resp;
676
677 /* Check the response for busy status */
678 if (!(mmc_resp & MMC_BOOT_OCR_BUSY)) {
679 return MMC_BOOT_E_CARD_BUSY;
680 }
681
682 if (mmc_resp & MMC_BOOT_OCR_SEC_MODE) {
683 card->type = MMC_BOOT_TYPE_MMCHC;
684 } else {
685 card->type = MMC_BOOT_TYPE_STD_MMC;
686 }
687 return MMC_BOOT_E_SUCCESS;
688}
689
690/*
691 * Request any card to send its uniquie card identification (CID) number (CMD2).
692 */
693static unsigned int mmc_boot_all_send_cid(struct mmc_boot_card *card)
694{
695 struct mmc_boot_command cmd;
696 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
697
698 /* basic check */
699 if (card == NULL) {
700 return MMC_BOOT_E_INVAL;
701 }
702
703 memset((struct mmc_boot_command *)&cmd, 0,
704 sizeof(struct mmc_boot_command));
705
706 /* CMD2 Format:
707 * [31:0] stuff bits
708 */
709 cmd.cmd_index = CMD2_ALL_SEND_CID;
710 cmd.argument = 0;
711 cmd.cmd_type = MMC_BOOT_CMD_BCAST_W_RESP;
712 cmd.resp_type = MMC_BOOT_RESP_R2;
713
714 /* send command */
715 mmc_ret = mmc_boot_send_command(&cmd);
716 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
717 return mmc_ret;
718 }
719
720 /* Response contains card's 128 bits CID register */
721 mmc_ret = mmc_boot_decode_and_save_cid(card, cmd.resp);
722 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
723 return mmc_ret;
724 }
725 return MMC_BOOT_E_SUCCESS;
726}
727
728/*
729 * Ask any card to send it's relative card address (RCA).This RCA number is
730 * shorter than CID and is used by the host to address the card in future (CMD3)
731 */
732static unsigned int mmc_boot_send_relative_address(struct mmc_boot_card *card)
733{
734 struct mmc_boot_command cmd;
735 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
736
737 /* basic check */
738 if (card == NULL) {
739 return MMC_BOOT_E_INVAL;
740 }
741
742 memset((struct mmc_boot_command *)&cmd, 0,
743 sizeof(struct mmc_boot_command));
744
745 /* CMD3 Format:
746 * [31:0] stuff bits
747 */
748 if (card->type == MMC_BOOT_TYPE_SDHC
749 || card->type == MMC_BOOT_TYPE_STD_SD) {
750 cmd.cmd_index = CMD3_SEND_RELATIVE_ADDR;
751 cmd.argument = 0;
752 cmd.cmd_type = MMC_BOOT_CMD_BCAST_W_RESP;
753 cmd.resp_type = MMC_BOOT_RESP_R6;
754
755 /* send command */
756 mmc_ret = mmc_boot_send_command(&cmd);
757 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
758 return mmc_ret;
759 }
760 /* For sD, card will send RCA. Store it */
761 card->rca = (cmd.resp[0] >> 16);
762 } else {
763 cmd.cmd_index = CMD3_SEND_RELATIVE_ADDR;
764 cmd.argument = (MMC_RCA << 16);
765 card->rca = (cmd.argument >> 16);
766 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
767 cmd.resp_type = MMC_BOOT_RESP_R1;
768
769 /* send command */
770 mmc_ret = mmc_boot_send_command(&cmd);
771 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
772 return mmc_ret;
773 }
774 }
775
776 return MMC_BOOT_E_SUCCESS;
777}
778
779/*
780 * Requests card to send it's CSD register's contents. (CMD9)
781 */
782static unsigned int
783mmc_boot_send_csd(struct mmc_boot_card *card, unsigned int *raw_csd)
784{
785 struct mmc_boot_command cmd;
786 unsigned int mmc_arg = 0;
787 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
788
789 /* basic check */
790 if (card == NULL) {
791 return MMC_BOOT_E_INVAL;
792 }
793
794 memset((struct mmc_boot_command *)&cmd, 0,
795 sizeof(struct mmc_boot_command));
796
797 /* CMD9 Format:
798 * [31:16] RCA
799 * [15:0] stuff bits
800 */
801 mmc_arg |= card->rca << 16;
802
803 cmd.cmd_index = CMD9_SEND_CSD;
804 cmd.argument = mmc_arg;
805 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
806 cmd.resp_type = MMC_BOOT_RESP_R2;
807
808 /* send command */
809 mmc_ret = mmc_boot_send_command(&cmd);
810 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
811 return mmc_ret;
812 }
813
814 /* response contains the card csd */
815 memcpy(raw_csd, cmd.resp, sizeof(cmd.resp));
816
817 return MMC_BOOT_E_SUCCESS;
818}
819
820/*
821 * Selects a card by sending CMD7 to the card with its RCA.
822 * If RCA field is set as 0 ( or any other address ),
823 * the card will be de-selected. (CMD7)
824 */
825static unsigned int
826mmc_boot_select_card(struct mmc_boot_card *card, unsigned int rca)
827{
828 struct mmc_boot_command cmd;
829 unsigned int mmc_arg = 0;
830 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
831
832 /* basic check */
833 if (card == NULL) {
834 return MMC_BOOT_E_INVAL;
835 }
836
837 memset((struct mmc_boot_command *)&cmd, 0,
838 sizeof(struct mmc_boot_command));
839
840 /* CMD7 Format:
841 * [31:16] RCA
842 * [15:0] stuff bits
843 */
844 mmc_arg |= rca << 16;
845
846 cmd.cmd_index = CMD7_SELECT_DESELECT_CARD;
847 cmd.argument = mmc_arg;
848 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
849 /* If we are deselecting card, we do not get response */
850 if (rca == card->rca && rca) {
851 if (card->type == MMC_BOOT_TYPE_SDHC
852 || card->type == MMC_BOOT_TYPE_STD_SD)
853 cmd.resp_type = MMC_BOOT_RESP_R1B;
854 else
855 cmd.resp_type = MMC_BOOT_RESP_R1;
856 } else {
857 cmd.resp_type = MMC_BOOT_RESP_NONE;
858 }
859
860 /* send command */
861 mmc_ret = mmc_boot_send_command(&cmd);
862 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
863 return mmc_ret;
864 }
865
866 /* As of now no need to look into a response. If it's required
867 * we'll explore later on */
868
869 return MMC_BOOT_E_SUCCESS;
870}
871
872/*
873 * Send command to set block length.
874 */
875static unsigned int
876mmc_boot_set_block_len(struct mmc_boot_card *card, unsigned int block_len)
877{
878 struct mmc_boot_command cmd;
879 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
880
881 /* basic check */
882 if (card == NULL) {
883 return MMC_BOOT_E_INVAL;
884 }
885
886 memset((struct mmc_boot_command *)&cmd, 0,
887 sizeof(struct mmc_boot_command));
888
889 /* CMD16 Format:
890 * [31:0] block length
891 */
892
893 cmd.cmd_index = CMD16_SET_BLOCKLEN;
894 cmd.argument = block_len;
895 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
896 cmd.resp_type = MMC_BOOT_RESP_R1;
897
898 /* send command */
899 mmc_ret = mmc_boot_send_command(&cmd);
900 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
901 return mmc_ret;
902 }
903
904 /* If blocklength is larger than 512 bytes,
905 * the card sets BLOCK_LEN_ERROR bit. */
906 if (cmd.resp[0] & MMC_BOOT_R1_BLOCK_LEN_ERR) {
907 return MMC_BOOT_E_BLOCKLEN_ERR;
908 }
909 return MMC_BOOT_E_SUCCESS;
910}
911
912/*
913 * Requests the card to stop transmission of data.
914 */
915static unsigned int
916mmc_boot_send_stop_transmission(struct mmc_boot_card *card,
917 unsigned int prg_enabled)
918{
919 struct mmc_boot_command cmd;
920 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
921
922 /* basic check */
923 if (card == NULL) {
924 return MMC_BOOT_E_INVAL;
925 }
926
927 memset((struct mmc_boot_command *)&cmd, 0,
928 sizeof(struct mmc_boot_command));
929
930 /* CMD12 Format:
931 * [31:0] stuff bits
932 */
933
934 cmd.cmd_index = CMD12_STOP_TRANSMISSION;
935 cmd.argument = 0;
936 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
937 cmd.resp_type = MMC_BOOT_RESP_R1B;
938 cmd.xfer_mode = MMC_BOOT_XFER_MODE_BLOCK;
939 cmd.prg_enabled = prg_enabled;
940
941 /* send command */
942 mmc_ret = mmc_boot_send_command(&cmd);
943 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
944 return mmc_ret;
945 }
946 return MMC_BOOT_E_SUCCESS;
947}
948
949/*
950 * Get the card's current status
951 */
952static unsigned int
953mmc_boot_get_card_status(struct mmc_boot_card *card,
954 unsigned int prg_enabled, unsigned int *status)
955{
956 struct mmc_boot_command cmd;
957 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
958
959 /* basic check */
960 if (card == NULL) {
961 return MMC_BOOT_E_INVAL;
962 }
963
964 memset((struct mmc_boot_command *)&cmd, 0,
965 sizeof(struct mmc_boot_command));
966
967 /* CMD13 Format:
968 * [31:16] RCA
969 * [15:0] stuff bits
970 */
971 cmd.cmd_index = CMD13_SEND_STATUS;
972 cmd.argument = card->rca << 16;
973 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
974 cmd.resp_type = MMC_BOOT_RESP_R1;
975 cmd.prg_enabled = prg_enabled;
976
977 /* send command */
978 mmc_ret = mmc_boot_send_command(&cmd);
979 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
980 return mmc_ret;
981 }
982
983 /* Checking ADDR_OUT_OF_RANGE error in CMD13 response */
984 if (IS_ADDR_OUT_OF_RANGE(cmd.resp[0])) {
985 return MMC_BOOT_E_FAILURE;
986 }
987
988 *status = cmd.resp[0];
989 return MMC_BOOT_E_SUCCESS;
990}
991
992/*
993 * Decode type of error caused during read and write
994 */
995static unsigned int mmc_boot_status_error(unsigned mmc_status)
996{
997 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
998
999 /* If DATA_CRC_FAIL bit is set to 1 then CRC error was detected by
1000 card/device during the data transfer */
1001 if (mmc_status & MMC_BOOT_MCI_STAT_DATA_CRC_FAIL) {
1002 mmc_ret = MMC_BOOT_E_DATA_CRC_FAIL;
1003 }
1004 /* If DATA_TIMEOUT bit is set to 1 then the data transfer time exceeded
1005 the data timeout period without completing the transfer */
1006 else if (mmc_status & MMC_BOOT_MCI_STAT_DATA_TIMEOUT) {
1007 mmc_ret = MMC_BOOT_E_DATA_TIMEOUT;
1008 }
1009 /* If RX_OVERRUN bit is set to 1 then SDCC2 tried to receive data from
1010 the card before empty storage for new received data was available.
1011 Verify that bit FLOW_ENA in MCI_CLK is set to 1 during the data xfer. */
1012 else if (mmc_status & MMC_BOOT_MCI_STAT_RX_OVRRUN) {
1013 /* Note: We've set FLOW_ENA bit in MCI_CLK to 1. so no need to verify
1014 for now */
1015 mmc_ret = MMC_BOOT_E_RX_OVRRUN;
1016 }
1017 /* If TX_UNDERRUN bit is set to 1 then SDCC2 tried to send data to
1018 the card before new data for sending was available. Verify that bit
1019 FLOW_ENA in MCI_CLK is set to 1 during the data xfer. */
1020 else if (mmc_status & MMC_BOOT_MCI_STAT_TX_UNDRUN) {
1021 /* Note: We've set FLOW_ENA bit in MCI_CLK to 1.so skipping it now */
1022 mmc_ret = MMC_BOOT_E_RX_OVRRUN;
1023 }
1024 return mmc_ret;
1025}
1026
1027/*
1028 * Send ext csd command.
1029 */
1030static unsigned int
1031mmc_boot_send_ext_cmd(struct mmc_boot_card *card, unsigned char *buf)
1032{
1033 struct mmc_boot_command cmd;
1034 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
1035 unsigned int mmc_reg = 0;
1036 unsigned int *mmc_ptr = (unsigned int *)buf;
1037
1038 memset(buf, 0, 512);
1039
1040 /* basic check */
1041 if (card == NULL) {
1042 return MMC_BOOT_E_INVAL;
1043 }
1044
1045 /* set block len */
1046 if ((card->type != MMC_BOOT_TYPE_MMCHC)
1047 && (card->type != MMC_BOOT_TYPE_SDHC)) {
1048 mmc_ret = mmc_boot_set_block_len(card, 512);
1049 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1050 dprintf(CRITICAL,
1051 "Error No.%d: Failure setting block length for Card (RCA:%s)\n",
1052 mmc_ret, (char *)(card->rca));
1053 return mmc_ret;
1054 }
1055 }
1056
1057 /* Set the FLOW_ENA bit of MCI_CLK register to 1 */
1058 mmc_reg = readl(MMC_BOOT_MCI_CLK);
1059 mmc_reg |= MMC_BOOT_MCI_CLK_ENA_FLOW;
1060 writel(mmc_reg, MMC_BOOT_MCI_CLK);
1061
1062 /* Write data timeout period to MCI_DATA_TIMER register. */
1063 /* Data timeout period should be in card bus clock periods */
1064 mmc_reg = 0xFFFFFFFF;
1065 writel(mmc_reg, MMC_BOOT_MCI_DATA_TIMER);
1066 writel(512, MMC_BOOT_MCI_DATA_LENGTH);
1067
1068 /* Set appropriate fields and write the MCI_DATA_CTL register. */
1069 /* Set ENABLE bit to 1 to enable the data transfer. */
1070 mmc_reg =
1071 MMC_BOOT_MCI_DATA_ENABLE | MMC_BOOT_MCI_DATA_DIR | (512 <<
1072 MMC_BOOT_MCI_BLKSIZE_POS);
1073
1074#if MMC_BOOT_ADM
1075 mmc_reg |= MMC_BOOT_MCI_DATA_DM_ENABLE;
1076#endif
1077
1078 writel(mmc_reg, MMC_BOOT_MCI_DATA_CTL);
1079
1080 memset((struct mmc_boot_command *)&cmd, 0,
1081 sizeof(struct mmc_boot_command));
1082 /* CMD8 */
1083 cmd.cmd_index = CMD8_SEND_EXT_CSD;
1084 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
1085 cmd.resp_type = MMC_BOOT_RESP_R1;
1086 cmd.xfer_mode = MMC_BOOT_XFER_MODE_BLOCK;
1087
1088 /* send command */
1089 mmc_ret = mmc_boot_send_command(&cmd);
1090 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1091 return mmc_ret;
1092 }
1093
1094 /* Read the transfer data from SDCC FIFO. */
1095 mmc_ret = mmc_boot_fifo_data_transfer(mmc_ptr, 512, MMC_BOOT_DATA_READ);
1096
1097 return mmc_ret;
1098}
1099
1100/*
1101 * Switch command
1102 */
1103static unsigned int
1104mmc_boot_switch_cmd(struct mmc_boot_card *card,
1105 unsigned access, unsigned index, unsigned value)
1106{
1107
1108 struct mmc_boot_command cmd;
1109 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
1110
1111 /* basic check */
1112 if (card == NULL) {
1113 return MMC_BOOT_E_INVAL;
1114 }
1115
1116 memset((struct mmc_boot_command *)&cmd, 0,
1117 sizeof(struct mmc_boot_command));
1118
1119 /* CMD6 Format:
1120 * [31:26] set to 0
1121 * [25:24] access
1122 * [23:16] index
1123 * [15:8] value
1124 * [7:3] set to 0
1125 * [2:0] cmd set
1126 */
1127 cmd.cmd_index = CMD6_SWITCH_FUNC;
1128 cmd.argument |= (access << 24);
1129 cmd.argument |= (index << 16);
1130 cmd.argument |= (value << 8);
1131 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
1132 cmd.resp_type = MMC_BOOT_RESP_R1B;
1133
1134 mmc_ret = mmc_boot_send_command(&cmd);
1135 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1136 return mmc_ret;
1137 }
1138
1139 return MMC_BOOT_E_SUCCESS;
1140}
1141
1142/*
1143 * A command to set the data bus width for card. Set width to either
1144 */
1145static unsigned int
1146mmc_boot_set_bus_width(struct mmc_boot_card *card, unsigned int width)
1147{
1148 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
1149 unsigned int mmc_reg = 0;
1150 unsigned int mmc_width = 0;
1151 unsigned int status;
1152 unsigned int wait_count = 100;
1153
1154 if (width != MMC_BOOT_BUS_WIDTH_1_BIT) {
1155 mmc_width = width - 1;
1156 }
1157
1158 mmc_ret = mmc_boot_switch_cmd(card, MMC_BOOT_ACCESS_WRITE,
1159 MMC_BOOT_EXT_CMMC_BUS_WIDTH, mmc_width);
1160
1161 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1162 return mmc_ret;
1163 }
1164
1165 /* Wait for the card to complete the switch command processing */
1166 do {
1167 mmc_ret = mmc_boot_get_card_status(card, 0, &status);
1168 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1169 return mmc_ret;
1170 }
1171
1172 wait_count--;
1173 if (wait_count == 0) {
1174 return MMC_BOOT_E_FAILURE;
1175 }
1176 }
1177 while (MMC_BOOT_CARD_STATUS(status) == MMC_BOOT_PROG_STATE);
1178
1179 /* set MCI_CLK accordingly */
1180 mmc_reg = readl(MMC_BOOT_MCI_CLK);
1181 mmc_reg &= ~MMC_BOOT_MCI_CLK_WIDEBUS_MODE;
1182 if (width == MMC_BOOT_BUS_WIDTH_1_BIT) {
1183 mmc_reg |= MMC_BOOT_MCI_CLK_WIDEBUS_1_BIT;
1184 } else if (width == MMC_BOOT_BUS_WIDTH_4_BIT) {
1185 mmc_reg |= MMC_BOOT_MCI_CLK_WIDEBUS_4_BIT;
1186 } else if (width == MMC_BOOT_BUS_WIDTH_8_BIT) {
1187 mmc_reg |= MMC_BOOT_MCI_CLK_WIDEBUS_8_BIT;
1188 }
1189 writel(mmc_reg, MMC_BOOT_MCI_CLK);
1190
1191 mdelay(10); // Giving some time to card to stabilize.
1192
1193 return MMC_BOOT_E_SUCCESS;
1194}
1195
1196/*
1197 * A command to start data read from card. Either a single block or
1198 * multiple blocks can be read. Multiple blocks read will continuously
1199 * transfer data from card to host unless requested to stop by issuing
1200 * CMD12 - STOP_TRANSMISSION.
1201 */
1202static unsigned int
1203mmc_boot_send_read_command(struct mmc_boot_card *card,
1204 unsigned int xfer_type, unsigned int data_addr)
1205{
1206 struct mmc_boot_command cmd;
1207 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
1208
1209 /* basic check */
1210 if (card == NULL) {
1211 return MMC_BOOT_E_INVAL;
1212 }
1213
1214 memset((struct mmc_boot_command *)&cmd, 0,
1215 sizeof(struct mmc_boot_command));
1216
1217 /* CMD17/18 Format:
1218 * [31:0] Data Address
1219 */
1220 if (xfer_type == MMC_BOOT_XFER_MULTI_BLOCK) {
1221 cmd.cmd_index = CMD18_READ_MULTIPLE_BLOCK;
1222 } else {
1223 cmd.cmd_index = CMD17_READ_SINGLE_BLOCK;
1224 }
1225
1226 cmd.argument = data_addr;
1227 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
1228 cmd.resp_type = MMC_BOOT_RESP_R1;
1229
1230 /* send command */
1231 mmc_ret = mmc_boot_send_command(&cmd);
1232 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1233 return mmc_ret;
1234 }
1235
1236 /* Response contains 32 bit Card status. Here we'll check
1237 BLOCK_LEN_ERROR and ADDRESS_ERROR */
1238 if (cmd.resp[0] & MMC_BOOT_R1_BLOCK_LEN_ERR) {
1239 return MMC_BOOT_E_BLOCKLEN_ERR;
1240 }
1241 /* Misaligned address not matching block length */
1242 if (cmd.resp[0] & MMC_BOOT_R1_ADDR_ERR) {
1243 return MMC_BOOT_E_ADDRESS_ERR;
1244 }
1245
1246 return MMC_BOOT_E_SUCCESS;
1247}
1248
1249/*
1250 * A command to start data write to card. Either a single block or
1251 * multiple blocks can be written. Multiple block write will continuously
1252 * transfer data from host to card unless requested to stop by issuing
1253 * CMD12 - STOP_TRANSMISSION.
1254 */
1255static unsigned int
1256mmc_boot_send_write_command(struct mmc_boot_card *card,
1257 unsigned int xfer_type, unsigned int data_addr)
1258{
1259 struct mmc_boot_command cmd;
1260 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
1261
1262 /* basic check */
1263 if (card == NULL) {
1264 return MMC_BOOT_E_INVAL;
1265 }
1266
1267 memset((struct mmc_boot_command *)&cmd, 0,
1268 sizeof(struct mmc_boot_command));
1269
1270 /* CMD24/25 Format:
1271 * [31:0] Data Address
1272 */
1273 if (xfer_type == MMC_BOOT_XFER_MULTI_BLOCK) {
1274 cmd.cmd_index = CMD25_WRITE_MULTIPLE_BLOCK;
1275 } else {
1276 cmd.cmd_index = CMD24_WRITE_SINGLE_BLOCK;
1277 }
1278
1279 cmd.argument = data_addr;
1280 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
1281 cmd.resp_type = MMC_BOOT_RESP_R1;
1282
1283 /* send command */
1284 mmc_ret = mmc_boot_send_command(&cmd);
1285 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1286 return mmc_ret;
1287 }
1288
1289 /* Response contains 32 bit Card status. Here we'll check
1290 BLOCK_LEN_ERROR and ADDRESS_ERROR */
1291 if (cmd.resp[0] & MMC_BOOT_R1_BLOCK_LEN_ERR) {
1292 return MMC_BOOT_E_BLOCKLEN_ERR;
1293 }
1294 /* Misaligned address not matching block length */
1295 if (cmd.resp[0] & MMC_BOOT_R1_ADDR_ERR) {
1296 return MMC_BOOT_E_ADDRESS_ERR;
1297 }
1298
1299 return MMC_BOOT_E_SUCCESS;
1300}
1301
1302/*
1303 * Write data_len data to address specified by data_addr. data_len is
1304 * multiple of blocks for block data transfer.
1305 */
1306unsigned int
1307mmc_boot_write_to_card(struct mmc_boot_host *host,
1308 struct mmc_boot_card *card,
1309 unsigned long long data_addr,
1310 unsigned int data_len, unsigned int *in)
1311{
1312 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
1313 unsigned int mmc_status = 0;
1314 unsigned int mmc_reg = 0;
1315 unsigned int addr;
1316 unsigned int xfer_type;
1317 unsigned int status;
1318
1319 if ((host == NULL) || (card == NULL)) {
1320 return MMC_BOOT_E_INVAL;
1321 }
1322
1323 /* Set block length. High Capacity MMC/SD card uses fixed 512 bytes block
1324 length. So no need to send CMD16. */
1325 if ((card->type != MMC_BOOT_TYPE_MMCHC)
1326 && (card->type != MMC_BOOT_TYPE_SDHC)) {
1327 mmc_ret = mmc_boot_set_block_len(card, card->wr_block_len);
1328 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1329 dprintf(CRITICAL, "Error No.%d: Failure setting block length for Card\
1330 (RCA:%s)\n", mmc_ret,
1331 (char *)(card->rca));
1332 return mmc_ret;
1333 }
1334 }
1335
1336 /* use multi-block mode to transfer for data larger than a block */
1337 xfer_type =
1338 (data_len >
1339 card->
1340 rd_block_len) ? MMC_BOOT_XFER_MULTI_BLOCK :
1341 MMC_BOOT_XFER_SINGLE_BLOCK;
1342
1343 /* For MMCHC/SDHC data address is specified in unit of 512B */
1344 addr = ((card->type != MMC_BOOT_TYPE_MMCHC)
1345 && (card->type !=
1346 MMC_BOOT_TYPE_SDHC)) ? (unsigned int)data_addr : (unsigned
1347 int)
1348 (data_addr / 512);
1349
1350 /* Set the FLOW_ENA bit of MCI_CLK register to 1 */
1351 mmc_reg = readl(MMC_BOOT_MCI_CLK);
1352 mmc_reg |= MMC_BOOT_MCI_CLK_ENA_FLOW;
1353 writel(mmc_reg, MMC_BOOT_MCI_CLK);
1354
1355 /* Write data timeout period to MCI_DATA_TIMER register */
1356 /* Data timeout period should be in card bus clock periods */
1357 /*TODO: Fix timeout value */
1358 mmc_reg = 0xFFFFFFFF;
1359 writel(mmc_reg, MMC_BOOT_MCI_DATA_TIMER);
1360
1361 /* Write the total size of the transfer data to MCI_DATA_LENGTH register */
1362 writel(data_len, MMC_BOOT_MCI_DATA_LENGTH);
1363
1364 /* Send command to the card/device in order to start the write data xfer.
1365 The possible commands are CMD24/25/53/60/61 */
1366 mmc_ret = mmc_boot_send_write_command(card, xfer_type, addr);
1367 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1368 dprintf(CRITICAL, "Error No.%d: Failure sending write command to the\
1369 Card(RCA:%x)\n", mmc_ret,
1370 card->rca);
1371 return mmc_ret;
1372 }
1373
1374 /* Set appropriate fields and write the MCI_DATA_CTL register */
1375 /* Set ENABLE bit to 1 to enable the data transfer. */
1376 mmc_reg = 0;
1377 mmc_reg |= MMC_BOOT_MCI_DATA_ENABLE;
1378 /* Clear DIRECTION bit to 0 to enable transfer from host to card */
1379 /* Clear MODE bit to 0 to enable block oriented data transfer. For
1380 MMC cards only, if stream data transfer mode is desired, set
1381 MODE bit to 1. */
1382
1383 /* Set DM_ENABLE bit to 1 in order to enable DMA, otherwise set 0 */
1384
1385#if MMC_BOOT_ADM
1386 mmc_reg |= MMC_BOOT_MCI_DATA_DM_ENABLE;
1387#endif
1388
1389 /* Write size of block to be used during the data transfer to
1390 BLOCKSIZE field */
1391 mmc_reg |= card->wr_block_len << MMC_BOOT_MCI_BLKSIZE_POS;
1392 writel(mmc_reg, MMC_BOOT_MCI_DATA_CTL);
1393
1394 /* write data to FIFO */
1395 mmc_ret =
1396 mmc_boot_fifo_data_transfer(in, data_len, MMC_BOOT_DATA_WRITE);
1397
1398 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1399 dprintf(CRITICAL, "Error No.%d: Failure on data transfer from the \
1400 Card(RCA:%x)\n", mmc_ret,
1401 card->rca);
1402 /* In case of any failure happening for multi block transfer */
1403 if (xfer_type == MMC_BOOT_XFER_MULTI_BLOCK)
1404 mmc_boot_send_stop_transmission(card, 1);
1405 return mmc_ret;
1406 }
1407
1408 /* Send command to the card/device in order to poll the de-assertion of
1409 card/device BUSY condition. It is important to set PROG_ENA bit in
1410 MCI_CLK register before sending the command. Possible commands are
1411 CMD12/13. */
1412 if (xfer_type == MMC_BOOT_XFER_MULTI_BLOCK) {
1413 mmc_ret = mmc_boot_send_stop_transmission(card, 1);
1414 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1415 dprintf(CRITICAL, "Error No.%d: Failure sending Stop Transmission \
1416 command to the Card(RCA:%x)\n", mmc_ret,
1417 card->rca);
1418 return mmc_ret;
1419 }
1420 } else {
1421 mmc_ret = mmc_boot_get_card_status(card, 1, &status);
1422 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1423 dprintf(CRITICAL,
1424 "Error No.%d: Failure getting card status of Card(RCA:%x)\n",
1425 mmc_ret, card->rca);
1426 return mmc_ret;
1427 }
1428 }
1429
1430 /* Wait for interrupt or poll on PROG_DONE bit of MCI_STATUS register. If
1431 PROG_DONE bit is set to 1 it means that the card finished it programming
1432 and stopped driving DAT0 line to 0 */
1433 do {
1434 mmc_status = readl(MMC_BOOT_MCI_STATUS);
1435 if (mmc_status & MMC_BOOT_MCI_STAT_PROG_DONE) {
1436 break;
1437 }
1438 }
1439 while (1);
1440
1441 return MMC_BOOT_E_SUCCESS;
1442}
1443
1444/*
1445 * Adjust the interface speed to optimal speed
1446 */
1447static unsigned int
1448mmc_boot_adjust_interface_speed(struct mmc_boot_host *host,
1449 struct mmc_boot_card *card)
1450{
1451 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
1452 unsigned int status;
1453 unsigned int wait_count = 100;
1454
1455 /* Setting HS_TIMING in EXT_CSD (CMD6) */
1456 mmc_ret = mmc_boot_switch_cmd(card, MMC_BOOT_ACCESS_WRITE,
1457 MMC_BOOT_EXT_CMMC_HS_TIMING, 1);
1458
1459 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1460 return mmc_ret;
1461 }
1462
1463 /* Wait for the card to complete the switch command processing */
1464 do {
1465 mmc_ret = mmc_boot_get_card_status(card, 0, &status);
1466 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1467 return mmc_ret;
1468 }
1469
1470 wait_count--;
1471 if (wait_count == 0) {
1472 return MMC_BOOT_E_FAILURE;
1473 }
1474 }
1475 while (MMC_BOOT_CARD_STATUS(status) == MMC_BOOT_PROG_STATE);
1476
1477 clock_config_mmc(mmc_slot, MMC_CLK_50MHZ);
1478
1479 host->mclk_rate = MMC_CLK_50MHZ;
1480
1481 return MMC_BOOT_E_SUCCESS;
1482}
1483
1484static unsigned int
1485mmc_boot_set_block_count(struct mmc_boot_card *card, unsigned int block_count)
1486{
1487 struct mmc_boot_command cmd;
1488 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
1489
1490 /* basic check */
1491 if (card == NULL) {
1492 return MMC_BOOT_E_INVAL;
1493 }
1494
1495 memset((struct mmc_boot_command *)&cmd, 0,
1496 sizeof(struct mmc_boot_command));
1497
1498 /* CMD23 Format:
1499 * [15:0] number of blocks
1500 */
1501
1502 cmd.cmd_index = CMD23_SET_BLOCK_COUNT;
1503 cmd.argument = block_count;
1504 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
1505 cmd.resp_type = MMC_BOOT_RESP_R1;
1506
1507 /* send command */
1508 mmc_ret = mmc_boot_send_command(&cmd);
1509 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1510 return mmc_ret;
1511 }
1512
1513 if (cmd.resp[0] & MMC_BOOT_R1_OUT_OF_RANGE) {
1514 return MMC_BOOT_E_BLOCKLEN_ERR;
1515 }
1516
1517 return MMC_BOOT_E_SUCCESS;
1518}
1519
1520/*
1521 * Reads a data of data_len from the address specified. data_len
1522 * should be multiple of block size for block data transfer.
1523 */
1524unsigned int
1525mmc_boot_read_from_card(struct mmc_boot_host *host,
1526 struct mmc_boot_card *card,
1527 unsigned long long data_addr,
1528 unsigned int data_len, unsigned int *out)
1529{
1530 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
1531 unsigned int mmc_reg = 0;
1532 unsigned int xfer_type;
1533 unsigned int addr = 0;
1534 unsigned char open_ended_read = 1;
1535
1536 if ((host == NULL) || (card == NULL)) {
1537 return MMC_BOOT_E_INVAL;
1538 }
1539
1540 /* Set block length. High Capacity MMC/SD card uses fixed 512 bytes block
1541 length. So no need to send CMD16. */
1542 if ((card->type != MMC_BOOT_TYPE_MMCHC)
1543 && (card->type != MMC_BOOT_TYPE_SDHC)) {
1544 mmc_ret = mmc_boot_set_block_len(card, card->rd_block_len);
1545 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1546 dprintf(CRITICAL,
1547 "Error No.%d: Failure setting block length for Card (RCA:%s)\n",
1548 mmc_ret, (char *)(card->rca));
1549 return mmc_ret;
1550 }
1551 }
1552
1553 /* use multi-block mode to transfer for data larger than a block */
1554 xfer_type =
1555 (data_len >
1556 card->
1557 rd_block_len) ? MMC_BOOT_XFER_MULTI_BLOCK :
1558 MMC_BOOT_XFER_SINGLE_BLOCK;
1559
1560 if (xfer_type == MMC_BOOT_XFER_MULTI_BLOCK) {
1561 if ((card->type == MMC_BOOT_TYPE_MMCHC)
1562 || (card->type == MMC_BOOT_TYPE_STD_MMC)) {
1563 /* Virtio model does not support open-ended multi-block reads.
1564 * So, block count must be set before sending read command.
1565 * All SD cards do not support this command. Restrict this to MMC.
1566 */
1567 mmc_ret =
1568 mmc_boot_set_block_count(card,
1569 data_len /
1570 (card->rd_block_len));
1571 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1572 dprintf(CRITICAL,
1573 "Error No.%d: Failure setting read block count for Card (RCA:%s)\n",
1574 mmc_ret, (char *)(card->rca));
1575 return mmc_ret;
1576 }
1577
1578 open_ended_read = 0;
1579 }
1580 }
1581
1582 /* Set the FLOW_ENA bit of MCI_CLK register to 1 */
1583 /* Note: It's already enabled */
1584
1585 /* If Data Mover is used for data transfer then prepare Command
1586 List Entry and enable the Data mover to work with SDCC2 */
1587
1588 /* Write data timeout period to MCI_DATA_TIMER register. */
1589 /* Data timeout period should be in card bus clock periods */
1590 mmc_reg = (unsigned long)(card->rd_timeout_ns / 1000000) *
1591 (host->mclk_rate / 1000);
1592 mmc_reg += 1000; // add some extra clock cycles to be safe
1593 mmc_reg = mmc_reg / 2;
1594 writel(mmc_reg, MMC_BOOT_MCI_DATA_TIMER);
1595
1596 /* Write the total size of the transfer data to MCI_DATA_LENGTH
1597 register. For block xfer it must be multiple of the block
1598 size. */
1599 writel(data_len, MMC_BOOT_MCI_DATA_LENGTH);
1600
1601 /* For MMCHC/SDHC data address is specified in unit of 512B */
1602 addr = ((card->type != MMC_BOOT_TYPE_MMCHC)
1603 && (card->type !=
1604 MMC_BOOT_TYPE_SDHC)) ? (unsigned int)data_addr : (unsigned
1605 int)
1606 (data_addr / 512);
1607
1608 /* Set appropriate fields and write the MCI_DATA_CTL register. */
1609 /* Set ENABLE bit to 1 to enable the data transfer. */
1610 mmc_reg = 0;
1611 mmc_reg |= MMC_BOOT_MCI_DATA_ENABLE;
1612 /* Clear DIRECTION bit to 1 to enable transfer from card to host */
1613 mmc_reg |= MMC_BOOT_MCI_DATA_DIR;
1614 /* Clear MODE bit to 0 to enable block oriented data transfer. For
1615 MMC cards only, if stream data transfer mode is desired, set
1616 MODE bit to 1. */
1617
1618 /* If DMA is to be used, Set DM_ENABLE bit to 1 */
1619
1620#if MMC_BOOT_ADM
1621 mmc_reg |= MMC_BOOT_MCI_DATA_DM_ENABLE;
1622#endif
1623
1624 /* Write size of block to be used during the data transfer to
1625 BLOCKSIZE field */
1626 mmc_reg |= (card->rd_block_len << MMC_BOOT_MCI_BLKSIZE_POS);
1627 writel(mmc_reg, MMC_BOOT_MCI_DATA_CTL);
1628
1629 /* Send command to the card/device in order to start the read data
1630 transfer. Possible commands: CMD17/18/53/60/61. */
1631 mmc_ret = mmc_boot_send_read_command(card, xfer_type, addr);
1632 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1633 dprintf(CRITICAL,
1634 "Error No.%d: Failure sending read command to the Card(RCA:%x)\n",
1635 mmc_ret, card->rca);
1636 return mmc_ret;
1637 }
1638
1639 /* Read the transfer data from SDCC FIFO. */
1640 mmc_ret =
1641 mmc_boot_fifo_data_transfer(out, data_len, MMC_BOOT_DATA_READ);
1642
1643 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1644 dprintf(CRITICAL, "Error No.%d: Failure on data transfer from the \
1645 Card(RCA:%x)\n", mmc_ret,
1646 card->rca);
1647 return mmc_ret;
1648 }
1649
1650 /* In case a multiple block transfer was performed, send CMD12 to the
1651 card/device in order to indicate the end of read data transfer */
1652 if ((xfer_type == MMC_BOOT_XFER_MULTI_BLOCK) && open_ended_read) {
1653 mmc_ret = mmc_boot_send_stop_transmission(card, 0);
1654 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1655 dprintf(CRITICAL, "Error No.%d: Failure sending Stop Transmission \
1656 command to the Card(RCA:%x)\n", mmc_ret,
1657 card->rca);
1658 return mmc_ret;
1659 }
1660 }
1661
1662 return MMC_BOOT_E_SUCCESS;
1663}
1664
1665/*
1666 * Initialize host structure, set and enable clock-rate and power mode.
1667 */
1668unsigned int mmc_boot_init(struct mmc_boot_host *host)
1669{
1670 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
1671 unsigned int mmc_pwr = 0;
1672
1673 host->ocr = MMC_BOOT_OCR_27_36 | MMC_BOOT_OCR_SEC_MODE;
1674 host->cmd_retry = MMC_BOOT_MAX_COMMAND_RETRY;
1675
1676 /* Initialize any clocks needed for SDC controller */
1677 clock_init_mmc(mmc_slot);
1678
1679 /* Setup initial freq to 400KHz */
1680 clock_config_mmc(mmc_slot, MMC_CLK_400KHZ);
1681
1682 host->mclk_rate = MMC_CLK_400KHZ;
1683
1684 /* set power mode */
1685 /* give some time to reach minimum voltate */
1686 mdelay(2);
1687 mmc_pwr &= ~MMC_BOOT_MCI_PWR_UP;
1688 mmc_pwr |= MMC_BOOT_MCI_PWR_ON;
1689 mmc_pwr |= MMC_BOOT_MCI_PWR_UP;
1690 writel(mmc_pwr, MMC_BOOT_MCI_POWER);
1691 /* some more time to stabilize voltage */
1692 mdelay(2);
1693
1694 return MMC_BOOT_E_SUCCESS;
1695}
1696
1697/*
1698 * Performs card identification process:
1699 * - get card's unique identification number (CID)
1700 * - get(for sd)/set (for mmc) relative card address (RCA)
1701 * - get CSD
1702 * - select the card, thus transitioning it to Transfer State
1703 * - get Extended CSD (for mmc)
1704 */
1705static unsigned int
1706mmc_boot_identify_card(struct mmc_boot_host *host, struct mmc_boot_card *card)
1707{
1708 unsigned int mmc_return = MMC_BOOT_E_SUCCESS;
1709 unsigned int raw_csd[4];
1710
1711 /* basic check */
1712 if ((host == NULL) || (card == NULL)) {
1713 return MMC_BOOT_E_INVAL;
1714 }
1715
1716 /* Ask card to send its unique card identification (CID) number (CMD2) */
1717 mmc_return = mmc_boot_all_send_cid(card);
1718 if (mmc_return != MMC_BOOT_E_SUCCESS) {
1719 dprintf(CRITICAL,
1720 "Error No. %d: Failure getting card's CID number!\n",
1721 mmc_return);
1722 return mmc_return;
1723 }
1724
1725 /* Ask card to send a relative card address (RCA) (CMD3) */
1726 mmc_return = mmc_boot_send_relative_address(card);
1727 if (mmc_return != MMC_BOOT_E_SUCCESS) {
1728 dprintf(CRITICAL, "Error No. %d: Failure getting card's RCA!\n",
1729 mmc_return);
1730 return mmc_return;
1731 }
1732
1733 /* Get card's CSD register (CMD9) */
1734 mmc_return = mmc_boot_send_csd(card, raw_csd);
1735 if (mmc_return != MMC_BOOT_E_SUCCESS) {
1736 dprintf(CRITICAL,
1737 "Error No.%d: Failure getting card's CSD information!\n",
1738 mmc_return);
1739 return mmc_return;
1740 }
1741
1742 /* Select the card (CMD7) */
1743 mmc_return = mmc_boot_select_card(card, card->rca);
1744 if (mmc_return != MMC_BOOT_E_SUCCESS) {
1745 dprintf(CRITICAL,
1746 "Error No.%d: Failure selecting the Card with RCA: %x\n",
1747 mmc_return, card->rca);
1748 return mmc_return;
1749 }
1750
1751 /* Set the card status as active */
1752 card->status = MMC_BOOT_STATUS_ACTIVE;
1753
1754 if ((card->type == MMC_BOOT_TYPE_STD_MMC)
1755 || (card->type == MMC_BOOT_TYPE_MMCHC)) {
1756 /* For MMC cards, also get the extended csd */
1757 mmc_return = mmc_boot_send_ext_cmd(card, ext_csd_buf);
1758
1759 if (mmc_return != MMC_BOOT_E_SUCCESS) {
1760 dprintf(CRITICAL,
1761 "Error No.%d: Failure getting card's ExtCSD information!\n",
1762 mmc_return);
1763
1764 return mmc_return;
1765 }
1766
1767 }
1768
1769 /* Decode and save the CSD register */
1770 mmc_return = mmc_boot_decode_and_save_csd(card, raw_csd);
1771 if (mmc_return != MMC_BOOT_E_SUCCESS) {
1772 dprintf(CRITICAL,
1773 "Error No.%d: Failure decoding card's CSD information!\n",
1774 mmc_return);
1775 return mmc_return;
1776 }
1777
1778 /* Once CSD is received, set read and write timeout value now itself */
1779 mmc_return = mmc_boot_set_read_timeout(host, card);
1780 if (mmc_return != MMC_BOOT_E_SUCCESS) {
1781 dprintf(CRITICAL,
1782 "Error No.%d: Failure setting Read Timeout value!\n",
1783 mmc_return);
1784 return mmc_return;
1785 }
1786
1787 mmc_return = mmc_boot_set_write_timeout(host, card);
1788 if (mmc_return != MMC_BOOT_E_SUCCESS) {
1789 dprintf(CRITICAL,
1790 "Error No.%d: Failure setting Write Timeout value!\n",
1791 mmc_return);
1792 return mmc_return;
1793 }
1794
1795 return MMC_BOOT_E_SUCCESS;
1796}
1797
1798static unsigned int mmc_boot_send_app_cmd(unsigned int rca)
1799{
1800 struct mmc_boot_command cmd;
1801 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
1802
1803 memset((struct mmc_boot_command *)&cmd, 0,
1804 sizeof(struct mmc_boot_command));
1805
1806 cmd.cmd_index = CMD55_APP_CMD;
1807 cmd.argument = (rca << 16);
1808 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
1809 cmd.resp_type = MMC_BOOT_RESP_R1;
1810
1811 mmc_ret = mmc_boot_send_command(&cmd);
1812
1813 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1814 return mmc_ret;
1815 }
1816
1817 return MMC_BOOT_E_SUCCESS;
1818}
1819
1820static unsigned int mmc_boot_sd_init_card(struct mmc_boot_card *card)
1821{
1822 unsigned int i, mmc_ret;
1823 unsigned int ocr_cmd_arg;
1824 struct mmc_boot_command cmd;
1825
1826 memset((struct mmc_boot_command *)&cmd, 0,
1827 sizeof(struct mmc_boot_command));
1828
1829 /* Send CMD8 to set interface condition */
1830 for (i = 0; i < 3; i++) {
1831 cmd.cmd_index = CMD8_SEND_IF_COND;
1832 cmd.argument = MMC_BOOT_SD_HC_VOLT_SUPPLIED;
1833 cmd.cmd_type = MMC_BOOT_CMD_BCAST_W_RESP;
1834 cmd.resp_type = MMC_BOOT_RESP_R7;
1835
1836 mmc_ret = mmc_boot_send_command(&cmd);
1837 if (mmc_ret == MMC_BOOT_E_SUCCESS) {
1838 if (cmd.resp[0] != MMC_BOOT_SD_HC_VOLT_SUPPLIED)
1839 return MMC_BOOT_E_FAILURE;
1840 /* Set argument for ACMD41 */
1841 ocr_cmd_arg = MMC_BOOT_SD_NEG_OCR | MMC_BOOT_SD_HC_HCS;
1842 break;
1843 }
1844 mdelay(1);
1845 }
1846
1847 /* Send ACMD41 to set operating condition */
1848 /* Try for a max of 1 sec as per spec */
1849 for (i = 0; i < 20; i++) {
1850 mmc_ret = mmc_boot_send_app_cmd(0);
1851 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1852 return mmc_ret;
1853 }
1854
1855 cmd.cmd_index = ACMD41_SEND_OP_COND;
1856 cmd.argument = ocr_cmd_arg;
1857 cmd.cmd_type = MMC_BOOT_CMD_BCAST_W_RESP;
1858 cmd.resp_type = MMC_BOOT_RESP_R3;
1859
1860 mmc_ret = mmc_boot_send_command(&cmd);
1861 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1862 return mmc_ret;
1863 } else if (cmd.resp[0] & MMC_BOOT_SD_DEV_READY) {
1864 /* Check for HC */
1865 if (cmd.resp[0] & (1 << 30)) {
1866 card->type = MMC_BOOT_TYPE_SDHC;
1867 } else {
1868 card->type = MMC_BOOT_TYPE_STD_SD;
1869 }
1870 break;
1871 }
1872 mdelay(50);
1873 }
1874 return MMC_BOOT_E_SUCCESS;
1875}
1876
1877/*
1878 * Routine to initialize MMC card. It resets a card to idle state, verify operating
1879 * voltage and set the card inready state.
1880 */
1881static unsigned int
1882mmc_boot_init_card(struct mmc_boot_host *host, struct mmc_boot_card *card)
1883{
1884 unsigned int mmc_retry = 0;
1885 unsigned int mmc_return = MMC_BOOT_E_SUCCESS;
1886
1887 /* basic check */
1888 if ((host == NULL) || (card == NULL)) {
1889 return MMC_BOOT_E_INVAL;
1890 }
1891
1892 /* 1. Card Reset - CMD0 */
1893 mmc_return = mmc_boot_reset_cards();
1894 if (mmc_return != MMC_BOOT_E_SUCCESS) {
1895 dprintf(CRITICAL,
1896 "Error No.:%d: Failure resetting MMC cards!\n",
1897 mmc_return);
1898 return mmc_return;
1899 }
1900
1901 /* 2. Card Initialization process */
1902
1903 /* Send CMD1 to identify and reject cards that do not match host's VDD range
1904 profile. Cards sends its OCR register in response.
1905 */
1906 mmc_retry = 0;
1907 do {
1908 mmc_return = mmc_boot_send_op_cond(host, card);
1909 /* Card returns busy status. We'll retry again! */
1910 if (mmc_return == MMC_BOOT_E_CARD_BUSY) {
1911 mmc_retry++;
1912 mdelay(1);
1913 continue;
1914 } else if (mmc_return == MMC_BOOT_E_SUCCESS) {
1915 break;
1916 } else {
1917 dprintf(CRITICAL,
1918 "Error No. %d: Failure Initializing MMC Card!\n",
1919 mmc_return);
1920
1921 /* Check for sD card */
1922 mmc_return = mmc_boot_sd_init_card(card);
1923 return mmc_return;
1924 }
1925 }
1926 while (mmc_retry < host->cmd_retry);
1927
1928 /* If card still returned busy status we are out of luck.
1929 * Card cannot be initialized */
1930 if (mmc_return == MMC_BOOT_E_CARD_BUSY) {
1931 dprintf(CRITICAL, "Error No. %d: Card has busy status set. \
1932 Initialization not completed\n", mmc_return);
1933 return MMC_BOOT_E_CARD_BUSY;
1934 }
1935 return MMC_BOOT_E_SUCCESS;
1936}
1937
1938static unsigned int
1939mmc_boot_set_sd_bus_width(struct mmc_boot_card *card, unsigned int width)
1940{
1941 struct mmc_boot_command cmd;
1942 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
1943 unsigned int sd_reg;
1944
1945 mmc_ret = mmc_boot_send_app_cmd(card->rca);
1946
1947 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1948 return mmc_ret;
1949 }
1950
1951 memset((struct mmc_boot_command *)&cmd, 0,
1952 sizeof(struct mmc_boot_command));
1953
1954 /* Send ACMD6 to set bus width */
1955 cmd.cmd_index = ACMD6_SET_BUS_WIDTH;
1956 /* 10 => 4 bit wide */
1957 if (width == MMC_BOOT_BUS_WIDTH_1_BIT) {
1958 cmd.argument = 0;
1959 } else if (width == MMC_BOOT_BUS_WIDTH_4_BIT) {
1960 cmd.argument = (1 << 1);
1961 }
1962 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
1963 cmd.resp_type = MMC_BOOT_RESP_R1;
1964
1965 mmc_ret = mmc_boot_send_command(&cmd);
1966
1967 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1968 return mmc_ret;
1969 }
1970
1971 /* set MCI_CLK accordingly */
1972 sd_reg = readl(MMC_BOOT_MCI_CLK);
1973 sd_reg &= ~MMC_BOOT_MCI_CLK_WIDEBUS_MODE;
1974 if (width == MMC_BOOT_BUS_WIDTH_1_BIT) {
1975 sd_reg |= MMC_BOOT_MCI_CLK_WIDEBUS_1_BIT;
1976 } else if (width == MMC_BOOT_BUS_WIDTH_4_BIT) {
1977 sd_reg |= MMC_BOOT_MCI_CLK_WIDEBUS_4_BIT;
1978 } else if (width == MMC_BOOT_BUS_WIDTH_8_BIT) {
1979 sd_reg |= MMC_BOOT_MCI_CLK_WIDEBUS_8_BIT;
1980 }
1981 writel(sd_reg, MMC_BOOT_MCI_CLK);
1982
1983 mdelay(10); // Giving some time to card to stabilize.
1984
1985 return MMC_BOOT_E_SUCCESS;
1986}
1987
1988static unsigned int
1989mmc_boot_set_sd_hs(struct mmc_boot_host *host, struct mmc_boot_card *card)
1990{
1991 unsigned char sw_buf[64];
1992 unsigned int mmc_ret;
1993
1994 /* CMD6 is a data transfer command. sD card returns 512 bits of data */
1995 /* Refer 4.3.10 of sD card specification 3.0 */
1996 mmc_ret =
1997 mmc_boot_read_reg(card, 64, CMD6_SWITCH_FUNC, MMC_BOOT_SD_SWITCH_HS,
1998 (unsigned int *)&sw_buf);
1999
2000 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
2001 return mmc_ret;
2002 }
2003
2004 mdelay(1);
2005
2006 clock_config_mmc(mmc_slot, MMC_CLK_50MHZ);
2007
2008 host->mclk_rate = MMC_CLK_50MHZ;
2009
2010 return MMC_BOOT_E_SUCCESS;
2011}
2012
2013/*
2014 * Performs initialization and identification of all the MMC cards connected
2015 * to the host.
2016 */
2017
2018static unsigned int
2019mmc_boot_init_and_identify_cards(struct mmc_boot_host *host,
2020 struct mmc_boot_card *card)
2021{
2022 unsigned int mmc_return = MMC_BOOT_E_SUCCESS;
2023 unsigned int status;
2024
2025 /* Basic check */
2026 if (host == NULL) {
2027 return MMC_BOOT_E_INVAL;
2028 }
2029
2030 /* Initialize MMC card structure */
2031 card->status = MMC_BOOT_STATUS_INACTIVE;
2032 card->rd_block_len = MMC_BOOT_RD_BLOCK_LEN;
2033 card->wr_block_len = MMC_BOOT_WR_BLOCK_LEN;
2034
2035 /* Start initialization process (CMD0 & CMD1) */
2036 mmc_return = mmc_boot_init_card(host, card);
2037 if (mmc_return != MMC_BOOT_E_SUCCESS) {
2038 return mmc_return;
2039 }
2040
2041 /* Identify (CMD2, CMD3 & CMD9) and select the card (CMD7) */
2042 mmc_return = mmc_boot_identify_card(host, card);
2043 if (mmc_return != MMC_BOOT_E_SUCCESS) {
2044 return mmc_return;
2045 }
2046
2047 if (card->type == MMC_BOOT_TYPE_SDHC
2048 || card->type == MMC_BOOT_TYPE_STD_SD) {
2049 /* Setting sD card to high speed without checking card's capability.
2050 Cards that do not support high speed may fail to boot */
2051 mmc_return = mmc_boot_set_sd_hs(host, card);
2052 if (mmc_return != MMC_BOOT_E_SUCCESS) {
2053 return mmc_return;
2054 }
2055
2056 mmc_return =
2057 mmc_boot_set_sd_bus_width(card, MMC_BOOT_BUS_WIDTH_4_BIT);
2058 if (mmc_return != MMC_BOOT_E_SUCCESS) {
2059 dprintf(CRITICAL,
2060 "Couldn't set 4bit mode for sD card\n");
2061 mmc_return =
2062 mmc_boot_set_sd_bus_width(card,
2063 MMC_BOOT_BUS_WIDTH_1_BIT);
2064 if (mmc_return != MMC_BOOT_E_SUCCESS) {
2065 dprintf(CRITICAL,
2066 "Error No.%d: Failed in setting bus width!\n",
2067 mmc_return);
2068 return mmc_return;
2069 }
2070 }
2071 } else {
2072 /* set interface speed */
2073 mmc_return = mmc_boot_adjust_interface_speed(host, card);
2074 if (mmc_return != MMC_BOOT_E_SUCCESS) {
2075 dprintf(CRITICAL,
2076 "Error No.%d: Error adjusting interface speed!\n",
2077 mmc_return);
2078 return mmc_return;
2079 }
2080
2081 /* enable wide bus */
2082 mmc_return =
2083 mmc_boot_set_bus_width(card, MMC_BOOT_BUS_WIDTH_4_BIT);
2084 if (mmc_return != MMC_BOOT_E_SUCCESS) {
2085 dprintf(CRITICAL,
2086 "Error No.%d: Failure to set wide bus for Card(RCA:%x)\n",
2087 mmc_return, card->rca);
2088 return mmc_return;
2089 }
2090 }
2091
2092 /* Just checking whether we're in TRAN state after changing speed and bus width */
2093 mmc_return = mmc_boot_get_card_status(card, 0, &status);
2094 if (mmc_return != MMC_BOOT_E_SUCCESS) {
2095 return mmc_return;
2096 }
2097
2098 if (MMC_BOOT_CARD_STATUS(status) != MMC_BOOT_TRAN_STATE)
2099 return MMC_BOOT_E_FAILURE;
2100
2101 return MMC_BOOT_E_SUCCESS;
2102}
2103
2104void mmc_display_ext_csd(void)
2105{
2106 dprintf(SPEW, "part_config: %x\n", ext_csd_buf[179]);
2107 dprintf(SPEW, "erase_group_def: %x\n", ext_csd_buf[175]);
2108 dprintf(SPEW, "user_wp: %x\n", ext_csd_buf[171]);
2109}
2110
2111void mmc_display_csd(void)
2112{
2113 dprintf(SPEW, "erase_grpsize: %d\n", mmc_card.csd.erase_grp_size);
2114 dprintf(SPEW, "erase_grpmult: %d\n", mmc_card.csd.erase_grp_mult);
2115 dprintf(SPEW, "wp_grpsize: %d\n", mmc_card.csd.wp_grp_size);
2116 dprintf(SPEW, "wp_grpen: %d\n", mmc_card.csd.wp_grp_enable);
2117 dprintf(SPEW, "perm_wp: %d\n", mmc_card.csd.perm_wp);
2118 dprintf(SPEW, "temp_wp: %d\n", mmc_card.csd.temp_wp);
2119}
2120
2121/*
2122 * Entry point to MMC boot process
2123 */
2124unsigned int mmc_boot_main(unsigned char slot, unsigned int base)
2125{
2126 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
2127
2128 memset((struct mmc_boot_host *)&mmc_host, 0,
2129 sizeof(struct mmc_boot_host));
2130 memset((struct mmc_boot_card *)&mmc_card, 0,
2131 sizeof(struct mmc_boot_card));
2132
2133 mmc_slot = slot;
2134 mmc_boot_mci_base = base;
2135
2136 /* Initialize necessary data structure and enable/set clock and power */
2137 dprintf(SPEW, " Initializing MMC host data structure and clock!\n");
2138 mmc_ret = mmc_boot_init(&mmc_host);
2139 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
2140 dprintf(CRITICAL, "MMC Boot: Error Initializing MMC Card!!!\n");
2141 return MMC_BOOT_E_FAILURE;
2142 }
2143
2144 /* Initialize and identify cards connected to host */
2145 mmc_ret = mmc_boot_init_and_identify_cards(&mmc_host, &mmc_card);
2146 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
2147 dprintf(CRITICAL,
2148 "MMC Boot: Failed detecting MMC/SDC @ slot%d\n", slot);
2149 return MMC_BOOT_E_FAILURE;
2150 }
2151
2152 mmc_display_csd();
2153 mmc_display_ext_csd();
2154
2155 mmc_ret = partition_read_table(&mmc_host, &mmc_card);
2156 return mmc_ret;
2157}
2158
2159/*
2160 * MMC write function
2161 */
2162unsigned int
2163mmc_write(unsigned long long data_addr, unsigned int data_len, unsigned int *in)
2164{
2165 int val = 0;
2166 unsigned int write_size = ((unsigned)(0xFFFFFF / 512)) * 512;
2167 unsigned offset = 0;
2168 unsigned int *sptr = in;
2169
2170 if (data_len % 512)
2171 data_len = ROUND_TO_PAGE(data_len, 511);
2172
2173 while (data_len > write_size) {
2174 val = mmc_boot_write_to_card(&mmc_host, &mmc_card,
2175 data_addr + offset, write_size,
2176 sptr);
2177 if (val) {
2178 return val;
2179 }
2180
2181 sptr += (write_size / sizeof(unsigned));
2182 offset += write_size;
2183 data_len -= write_size;
2184 }
2185 if (data_len) {
2186 val = mmc_boot_write_to_card(&mmc_host, &mmc_card,
2187 data_addr + offset, data_len,
2188 sptr);
2189 }
2190 return val;
2191}
2192
2193/*
2194 * MMC read function
2195 */
2196
2197unsigned int
2198mmc_read(unsigned long long data_addr, unsigned int *out, unsigned int data_len)
2199{
2200 int val = 0;
2201 val =
2202 mmc_boot_read_from_card(&mmc_host, &mmc_card, data_addr, data_len,
2203 out);
2204 return val;
2205}
2206
2207/*
2208 * Function to read registers from MMC or SD card
2209 */
2210static unsigned int
2211mmc_boot_read_reg(struct mmc_boot_card *card,
2212 unsigned int data_len,
2213 unsigned int command, unsigned int addr, unsigned int *out)
2214{
2215 struct mmc_boot_command cmd;
2216 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
2217 unsigned int mmc_reg = 0;
2218
2219 /* Set the FLOW_ENA bit of MCI_CLK register to 1 */
2220 mmc_reg = readl(MMC_BOOT_MCI_CLK);
2221 mmc_reg |= MMC_BOOT_MCI_CLK_ENA_FLOW;
2222 writel(mmc_reg, MMC_BOOT_MCI_CLK);
2223
2224 /* Write data timeout period to MCI_DATA_TIMER register. */
2225 /* Data timeout period should be in card bus clock periods */
2226 mmc_reg = 0xFFFFFFFF;
2227 writel(mmc_reg, MMC_BOOT_MCI_DATA_TIMER);
2228 writel(data_len, MMC_BOOT_MCI_DATA_LENGTH);
2229
2230 /* Set appropriate fields and write the MCI_DATA_CTL register. */
2231 /* Set ENABLE bit to 1 to enable the data transfer. */
2232 mmc_reg =
2233 MMC_BOOT_MCI_DATA_ENABLE | MMC_BOOT_MCI_DATA_DIR | (data_len <<
2234 MMC_BOOT_MCI_BLKSIZE_POS);
2235
2236#if MMC_BOOT_ADM
2237 mmc_reg |= MMC_BOOT_MCI_DATA_DM_ENABLE;
2238#endif
2239
2240 writel(mmc_reg, MMC_BOOT_MCI_DATA_CTL);
2241
2242 memset((struct mmc_boot_command *)&cmd, 0,
2243 sizeof(struct mmc_boot_command));
2244
2245 cmd.cmd_index = command;
2246 cmd.argument = addr;
2247 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
2248 cmd.resp_type = MMC_BOOT_RESP_R1;
2249
2250 /* send command */
2251 mmc_ret = mmc_boot_send_command(&cmd);
2252 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
2253 return mmc_ret;
2254 }
2255
2256 /* Read the transfer data from SDCC FIFO. */
2257 mmc_ret =
2258 mmc_boot_fifo_data_transfer(out, data_len, MMC_BOOT_DATA_READ);
2259
2260 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
2261 dprintf(CRITICAL, "Error No.%d: Failure on data transfer from the \
2262 Card(RCA:%x)\n", mmc_ret,
2263 card->rca);
2264 return mmc_ret;
2265 }
2266
2267 return MMC_BOOT_E_SUCCESS;
2268}
2269
2270/*
2271 * Function to set/clear power-on write protection for the user area partitions
2272 */
2273static unsigned int
2274mmc_boot_set_clr_power_on_wp_user(struct mmc_boot_card *card,
2275 unsigned int addr,
2276 unsigned int size, unsigned char set_clear_wp)
2277{
2278 struct mmc_boot_command cmd;
2279 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
2280 unsigned int wp_group_size, loop_count;
2281 unsigned int status;
2282
2283 memset((struct mmc_boot_command *)&cmd, 0,
2284 sizeof(struct mmc_boot_command));
2285
2286 /* Disabling PERM_WP for USER AREA (CMD6) */
2287 mmc_ret = mmc_boot_switch_cmd(card, MMC_BOOT_ACCESS_WRITE,
2288 MMC_BOOT_EXT_USER_WP,
2289 MMC_BOOT_US_PERM_WP_DIS);
2290
2291 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
2292 return mmc_ret;
2293 }
2294
2295 /* Sending CMD13 to check card status */
2296 do {
2297 mmc_ret = mmc_boot_get_card_status(card, 0, &status);
2298 if (MMC_BOOT_CARD_STATUS(status) == MMC_BOOT_TRAN_STATE)
2299 break;
2300 }
2301 while ((mmc_ret == MMC_BOOT_E_SUCCESS) &&
2302 (MMC_BOOT_CARD_STATUS(status) == MMC_BOOT_PROG_STATE));
2303
2304 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
2305 return mmc_ret;
2306 }
2307
2308 mmc_ret = mmc_boot_send_ext_cmd(card, ext_csd_buf);
2309
2310 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
2311 return mmc_ret;
2312 }
2313
2314 /* Make sure power-on write protection for user area is not disabled
2315 and permanent write protection for user area is not enabled */
2316
2317 if ((IS_BIT_SET_EXT_CSD(MMC_BOOT_EXT_USER_WP, MMC_BOOT_US_PERM_WP_EN))
2318 ||
2319 (IS_BIT_SET_EXT_CSD(MMC_BOOT_EXT_USER_WP, MMC_BOOT_US_PWR_WP_DIS)))
2320 {
2321 return MMC_BOOT_E_FAILURE;
2322 }
2323
2324 if (ext_csd_buf[MMC_BOOT_EXT_ERASE_GROUP_DEF]) {
2325 /* wp_group_size = 512KB * HC_WP_GRP_SIZE * HC_ERASE_GRP_SIZE.
2326 Getting write protect group size in sectors here. */
2327
2328 wp_group_size =
2329 (512 * 1024) * ext_csd_buf[MMC_BOOT_EXT_HC_WP_GRP_SIZE] *
2330 ext_csd_buf[MMC_BOOT_EXT_HC_ERASE_GRP_SIZE] /
2331 MMC_BOOT_WR_BLOCK_LEN;
2332 } else {
2333 /* wp_group_size = (WP_GRP_SIZE + 1) * (ERASE_GRP_SIZE + 1)
2334 * (ERASE_GRP_MULT + 1).
2335 This is defined as the number of write blocks directly */
2336
2337 wp_group_size = (card->csd.erase_grp_size + 1) *
2338 (card->csd.erase_grp_mult + 1) * (card->csd.wp_grp_size +
2339 1);
2340 }
2341
2342 if (wp_group_size == 0) {
2343 return MMC_BOOT_E_FAILURE;
2344 }
2345
2346 /* Setting POWER_ON_WP for USER AREA (CMD6) */
2347
2348 mmc_ret = mmc_boot_switch_cmd(card, MMC_BOOT_ACCESS_WRITE,
2349 MMC_BOOT_EXT_USER_WP,
2350 MMC_BOOT_US_PWR_WP_EN);
2351
2352 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
2353 return mmc_ret;
2354 }
2355
2356 /* Sending CMD13 to check card status */
2357 do {
2358 mmc_ret = mmc_boot_get_card_status(card, 0, &status);
2359 if (MMC_BOOT_CARD_STATUS(status) == MMC_BOOT_TRAN_STATE)
2360 break;
2361 }
2362 while ((mmc_ret == MMC_BOOT_E_SUCCESS) &&
2363 (MMC_BOOT_CARD_STATUS(status) == MMC_BOOT_PROG_STATE));
2364
2365 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
2366 return mmc_ret;
2367 }
2368
2369 /* Calculating the loop count for sending SET_WRITE_PROTECT (CMD28)
2370 or CLEAR_WRITE_PROTECT (CMD29).
2371 We are write protecting the partitions in blocks of write protect
2372 group sizes only */
2373
2374 if (size % wp_group_size) {
2375 loop_count = (size / wp_group_size) + 1;
2376 } else {
2377 loop_count = (size / wp_group_size);
2378 }
2379
2380 if (set_clear_wp)
2381 cmd.cmd_index = CMD28_SET_WRITE_PROTECT;
2382 else
2383 cmd.cmd_index = CMD29_CLEAR_WRITE_PROTECT;
2384
2385 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
2386 cmd.resp_type = MMC_BOOT_RESP_R1B;
2387
2388 for (unsigned int i = 0; i < loop_count; i++) {
2389 /* Sending CMD28 for each WP group size
2390 address is in sectors already */
2391 cmd.argument = (addr + (i * wp_group_size));
2392
2393 mmc_ret = mmc_boot_send_command(&cmd);
2394
2395 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
2396 return mmc_ret;
2397 }
2398
2399 /* Checking ADDR_OUT_OF_RANGE error in CMD28 response */
2400 if (IS_ADDR_OUT_OF_RANGE(cmd.resp[0])) {
2401 return MMC_BOOT_E_FAILURE;
2402 }
2403
2404 /* Sending CMD13 to check card status */
2405 do {
2406 mmc_ret = mmc_boot_get_card_status(card, 0, &status);
2407 if (MMC_BOOT_CARD_STATUS(status) == MMC_BOOT_TRAN_STATE)
2408 break;
2409 }
2410 while ((mmc_ret == MMC_BOOT_E_SUCCESS) &&
2411 (MMC_BOOT_CARD_STATUS(status) == MMC_BOOT_PROG_STATE));
2412
2413 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
2414 return mmc_ret;
2415 }
2416 }
2417
2418 return MMC_BOOT_E_SUCCESS;
2419}
2420
2421/*
2422 * Function to get Write Protect status of the given sector
2423 */
2424static unsigned int
2425mmc_boot_get_wp_status(struct mmc_boot_card *card, unsigned int sector)
2426{
2427 unsigned int rc = MMC_BOOT_E_SUCCESS;
2428 memset(wp_status_buf, 0, 8);
2429
2430 rc = mmc_boot_read_reg(card, 8, CMD31_SEND_WRITE_PROT_TYPE, sector,
2431 (unsigned int *)wp_status_buf);
2432 return rc;
2433}
2434
2435/*
2436 * Test Function for setting Write protect for given sector
2437 */
2438static unsigned int
2439mmc_wp(unsigned int sector, unsigned int size, unsigned char set_clear_wp)
2440{
2441 unsigned int rc = MMC_BOOT_E_SUCCESS;
2442
2443 /* Checking whether group write protection feature is available */
2444 if (mmc_card.csd.wp_grp_enable) {
2445 rc = mmc_boot_get_wp_status(&mmc_card, sector);
2446 rc = mmc_boot_set_clr_power_on_wp_user(&mmc_card, sector, size,
2447 set_clear_wp);
2448 rc = mmc_boot_get_wp_status(&mmc_card, sector);
2449 return rc;
2450 } else
2451 return MMC_BOOT_E_FAILURE;
2452}
2453
2454void mmc_wp_test(void)
2455{
2456 unsigned int mmc_ret = 0;
2457 mmc_ret = mmc_wp(0xE06000, 0x5000, 1);
2458}
2459
2460unsigned mmc_get_psn(void)
2461{
2462 return mmc_card.cid.psn;
2463}
2464
2465/*
2466 * Read/write data from/to SDC FIFO.
2467 */
2468static unsigned int
2469mmc_boot_fifo_data_transfer(unsigned int *data_ptr,
2470 unsigned int data_len, unsigned char direction)
2471{
2472 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
2473
2474#if MMC_BOOT_ADM
2475 adm_result_t ret;
2476 adm_dir_t adm_dir;
2477
2478 if (direction == MMC_BOOT_DATA_READ) {
2479 adm_dir = ADM_MMC_READ;
2480 } else {
2481 adm_dir = ADM_MMC_WRITE;
2482 }
2483
2484 ret = adm_transfer_mmc_data(mmc_slot,
2485 (unsigned char *)data_ptr, data_len,
2486 adm_dir);
2487
2488 if (ret != ADM_RESULT_SUCCESS) {
2489 dprintf(CRITICAL, "MMC ADM transfer error: %d\n", ret);
2490 mmc_ret = MMC_BOOT_E_FAILURE;
2491 }
2492#else
2493
2494 if (direction == MMC_BOOT_DATA_READ) {
2495 mmc_ret = mmc_boot_fifo_read(data_ptr, data_len);
2496 } else {
2497 mmc_ret = mmc_boot_fifo_write(data_ptr, data_len);
2498 }
2499#endif
2500 return mmc_ret;
2501}
2502
2503/*
2504 * Read data to SDC FIFO.
2505 */
2506static unsigned int
2507mmc_boot_fifo_read(unsigned int *mmc_ptr, unsigned int data_len)
2508{
2509 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
2510 unsigned int mmc_status = 0;
2511 unsigned int mmc_count = 0;
2512 unsigned int read_error = MMC_BOOT_MCI_STAT_DATA_CRC_FAIL |
2513 MMC_BOOT_MCI_STAT_DATA_TIMEOUT | MMC_BOOT_MCI_STAT_RX_OVRRUN;
2514
2515 /* Read the data from the MCI_FIFO register as long as RXDATA_AVLBL
2516 bit of MCI_STATUS register is set to 1 and bits DATA_CRC_FAIL,
2517 DATA_TIMEOUT, RX_OVERRUN of MCI_STATUS register are cleared to 0.
2518 Continue the reads until the whole transfer data is received */
2519
2520 do {
2521 mmc_ret = MMC_BOOT_E_SUCCESS;
2522 mmc_status = readl(MMC_BOOT_MCI_STATUS);
2523
2524 if (mmc_status & read_error) {
2525 mmc_ret = mmc_boot_status_error(mmc_status);
2526 break;
2527 }
2528
2529 if (mmc_status & MMC_BOOT_MCI_STAT_RX_DATA_AVLBL) {
2530 unsigned read_count = 1;
2531 if (mmc_status & MMC_BOOT_MCI_STAT_RX_FIFO_HFULL) {
2532 read_count = MMC_BOOT_MCI_HFIFO_COUNT;
2533 }
2534
2535 for (unsigned int i = 0; i < read_count; i++) {
2536 /* FIFO contains 16 32-bit data buffer on 16 sequential addresses */
2537 *mmc_ptr = readl(MMC_BOOT_MCI_FIFO +
2538 (mmc_count %
2539 MMC_BOOT_MCI_FIFO_SIZE));
2540 mmc_ptr++;
2541 /* increase mmc_count by word size */
2542 mmc_count += sizeof(unsigned int);
2543 }
2544 /* quit if we have read enough of data */
2545 if (mmc_count == data_len)
2546 break;
2547 } else if (mmc_status & MMC_BOOT_MCI_STAT_DATA_END) {
2548 break;
2549 }
2550 }
2551 while (1);
2552
2553 return mmc_ret;
2554}
2555
2556/*
2557 * Write data to SDC FIFO.
2558 */
2559static unsigned int
2560mmc_boot_fifo_write(unsigned int *mmc_ptr, unsigned int data_len)
2561{
2562 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
2563 unsigned int mmc_status = 0;
2564 unsigned int mmc_count = 0;
2565 unsigned int write_error = MMC_BOOT_MCI_STAT_DATA_CRC_FAIL |
2566 MMC_BOOT_MCI_STAT_DATA_TIMEOUT | MMC_BOOT_MCI_STAT_TX_UNDRUN;
2567
2568 /* Write the transfer data to SDCC3 FIFO */
2569 do {
2570 mmc_ret = MMC_BOOT_E_SUCCESS;
2571 mmc_status = readl(MMC_BOOT_MCI_STATUS);
2572
2573 if (mmc_status & write_error) {
2574 mmc_ret = mmc_boot_status_error(mmc_status);
2575 break;
2576 }
2577
2578 /* Write the data in MCI_FIFO register as long as TXFIFO_FULL bit of
2579 MCI_STATUS register is 0. Continue the writes until the whole
2580 transfer data is written. */
2581 if (((data_len - mmc_count) >= MMC_BOOT_MCI_FIFO_SIZE / 2) &&
2582 (mmc_status & MMC_BOOT_MCI_STAT_TX_FIFO_HFULL)) {
2583 for (int i = 0; i < MMC_BOOT_MCI_HFIFO_COUNT; i++) {
2584 /* FIFO contains 16 32-bit data buffer on 16 sequential addresses */
2585 writel(*mmc_ptr, MMC_BOOT_MCI_FIFO +
2586 (mmc_count % MMC_BOOT_MCI_FIFO_SIZE));
2587 mmc_ptr++;
2588 /* increase mmc_count by word size */
2589 mmc_count += sizeof(unsigned int);
2590 }
2591
2592 } else if (!(mmc_status & MMC_BOOT_MCI_STAT_TX_FIFO_FULL)
2593 && (mmc_count != data_len)) {
2594 /* FIFO contains 16 32-bit data buffer on 16 sequential addresses */
2595 writel(*mmc_ptr, MMC_BOOT_MCI_FIFO +
2596 (mmc_count % MMC_BOOT_MCI_FIFO_SIZE));
2597 mmc_ptr++;
2598 /* increase mmc_count by word size */
2599 mmc_count += sizeof(unsigned int);
2600 } else if ((mmc_status & MMC_BOOT_MCI_STAT_DATA_END)) {
2601 break; //success
2602 }
2603
2604 }
2605 while (1);
2606 return mmc_ret;
2607}
2608
2609/*
2610 * CMD35_ERASE_GROUP_START
2611 */
2612
2613static unsigned int
2614mmc_boot_send_erase_group_start(struct mmc_boot_card *card,
2615 unsigned long long data_addr)
2616{
2617 struct mmc_boot_command cmd;
2618 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
2619
2620 if (card == NULL)
2621 return MMC_BOOT_E_INVAL;
2622
2623 memset((struct mmc_boot_command *)&cmd, 0,
2624 sizeof(struct mmc_boot_command));
2625
2626 cmd.cmd_index = CMD35_ERASE_GROUP_START;
2627 cmd.argument = data_addr;
2628 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
2629 cmd.resp_type = MMC_BOOT_RESP_R1;
2630
2631 mmc_ret = mmc_boot_send_command(&cmd);
2632 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
2633 return mmc_ret;
2634 }
2635
2636 /* Checking for address error */
2637 if (IS_ADDR_OUT_OF_RANGE(cmd.resp[0])) {
2638 return MMC_BOOT_E_BLOCKLEN_ERR;
2639 }
2640
2641 return MMC_BOOT_E_SUCCESS;
2642
2643}
2644
2645/*
2646 * CMD36 ERASE GROUP END
2647 */
2648static unsigned int
2649mmc_boot_send_erase_group_end(struct mmc_boot_card *card,
2650 unsigned long long data_addr)
2651{
2652 struct mmc_boot_command cmd;
2653 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
2654
2655 if (card == NULL)
2656 return MMC_BOOT_E_INVAL;
2657
2658 memset((struct mmc_boot_command *)&cmd, 0,
2659 sizeof(struct mmc_boot_command));
2660
2661 cmd.cmd_index = CMD36_ERASE_GROUP_END;
2662 cmd.argument = data_addr;
2663 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
2664 cmd.resp_type = MMC_BOOT_RESP_R1;
2665
2666 mmc_ret = mmc_boot_send_command(&cmd);
2667 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
2668 return mmc_ret;
2669 }
2670
2671 /* Checking for address error */
2672 if (IS_ADDR_OUT_OF_RANGE(cmd.resp[0])) {
2673 return MMC_BOOT_E_BLOCKLEN_ERR;
2674 }
2675
2676 return MMC_BOOT_E_SUCCESS;
2677}
2678
2679/*
2680 * CMD38 ERASE
2681 */
2682static unsigned int mmc_boot_send_erase(struct mmc_boot_card *card)
2683{
2684
2685 struct mmc_boot_command cmd;
2686 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
2687 unsigned int status;
2688
2689 if (card == NULL)
2690 return MMC_BOOT_E_INVAL;
2691
2692 memset((struct mmc_boot_command *)&cmd, 0,
2693 sizeof(struct mmc_boot_command));
2694
2695 cmd.cmd_index = CMD38_ERASE;
2696 cmd.argument = 0x00000000;
2697 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
2698 cmd.resp_type = MMC_BOOT_RESP_R1B;
2699
2700 /* Checking if the card is in the transfer state */
2701 do {
2702 mmc_ret = mmc_boot_get_card_status(card, 0, &status);
2703 if (MMC_BOOT_CARD_STATUS(status) == MMC_BOOT_TRAN_STATE)
2704 break;
2705 }
2706 while ((mmc_ret == MMC_BOOT_E_SUCCESS) &&
2707 (MMC_BOOT_CARD_STATUS(status) == MMC_BOOT_PROG_STATE));
2708
2709 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
2710 return mmc_ret;
2711 }
2712 mmc_ret = mmc_boot_send_command(&cmd);
2713 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
2714 return mmc_ret;
2715 }
2716
2717 /* Checking for write protect */
2718 if (cmd.resp[0] & MMC_BOOT_R1_WP_ERASE_SKIP) {
2719 dprintf(CRITICAL, "Write protect enabled for sector \n");
2720 return;
2721 }
2722
2723 /* Checking if the erase operation for the card is compelete */
2724 do {
2725 mmc_ret = mmc_boot_get_card_status(card, 0, &status);
2726 if (MMC_BOOT_CARD_STATUS(status) == MMC_BOOT_TRAN_STATE)
2727 break;
2728 }
2729 while ((mmc_ret == MMC_BOOT_E_SUCCESS) &&
2730 (MMC_BOOT_CARD_STATUS(status) == MMC_BOOT_PROG_STATE));
2731
2732 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
2733 return mmc_ret;
2734 }
2735
2736 return MMC_BOOT_E_SUCCESS;
2737}
2738
2739/*
2740 * Function to erase data on the eMMC card
2741 */
2742unsigned int
2743mmc_erase_card(unsigned long long data_addr, unsigned long long size)
2744{
2745 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
2746 unsigned long long erase_grp_size;
2747 unsigned long long data_end = 0x00000000;
2748 unsigned long long loop_count;
2749 unsigned int out[512] = { 0 };
2750
2751 /* Converting size to sectors */
2752 size = size / 512;
2753
2754 if (ext_csd_buf[MMC_BOOT_EXT_ERASE_GROUP_DEF]) {
2755 erase_grp_size =
2756 (512 * ext_csd_buf[MMC_BOOT_EXT_HC_ERASE_GRP_SIZE] * 1024);
2757 erase_grp_size = erase_grp_size / 512;
2758 } else {
2759 erase_grp_size = (mmc_card.csd.erase_grp_size + 1) *
2760 (mmc_card.csd.erase_grp_mult + 1);
2761 }
2762
2763 if (erase_grp_size == 0) {
2764 return MMC_BOOT_E_FAILURE;
2765 }
2766
2767 if (size % erase_grp_size) {
2768 dprintf(CRITICAL, "Overflow beyond ERASE_GROUP_SIZE:%llu\n",
2769 (size % erase_grp_size));
2770
2771 }
2772 loop_count = (size / erase_grp_size);
2773 /*
2774 *In case the partition size is less than the erase_grp_size
2775 0 is written to the first block of the partition.
2776 */
2777 if (loop_count < 1) {
2778 mmc_ret = mmc_write(data_addr, 512, (unsigned int *)out);
2779 if (mmc_ret != MMC_BOOT_E_SUCCESS)
2780 return mmc_ret;
2781 else
2782 return MMC_BOOT_E_SUCCESS;
2783 } else {
2784 data_addr = ((mmc_card.type != MMC_BOOT_TYPE_MMCHC) &&
2785 (mmc_card.type != MMC_BOOT_TYPE_SDHC))
2786 ? (unsigned int)data_addr : (unsigned int)(data_addr / 512);
2787 data_end = data_addr + erase_grp_size * (loop_count - 1);
2788 }
2789
2790 /* Sending CMD35 */
2791 mmc_ret = mmc_boot_send_erase_group_start(&mmc_card, data_addr);
2792 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
2793 dprintf(CRITICAL, "Error %d: Failure sending erase group start "
2794 "command to the card (RCA:%x)\n", mmc_ret,
2795 mmc_card.rca);
2796 return mmc_ret;
2797 }
2798
2799 /* Sending CMD36 */
2800 mmc_ret = mmc_boot_send_erase_group_end(&mmc_card, data_end - 1);
2801 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
2802 dprintf(CRITICAL, "Error %d: Failure sending erase group end "
2803 "command to the card (RCA:%x)\n", mmc_ret,
2804 mmc_card.rca);
2805 return mmc_ret;
2806 }
2807
2808 for (unsigned long long i = 0; i < loop_count; i++) {
2809 /* Sending CMD38 */
2810 mmc_ret = mmc_boot_send_erase(&mmc_card);
2811 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
2812 dprintf(CRITICAL,
2813 "Error %d: Failure sending erase command "
2814 "to the card (RCA:%x)\n", mmc_ret,
2815 mmc_card.rca);
2816 return mmc_ret;
2817
2818 }
2819 }
2820 dprintf(CRITICAL, "ERASE SUCCESSFULLY COMPLETED\n");
2821 return MMC_BOOT_E_SUCCESS;
2822}
2823
2824struct mmc_boot_host *get_mmc_host(void)
2825{
2826 return &mmc_host;
2827}
2828
2829struct mmc_boot_card *get_mmc_card(void)
2830{
2831 return &mmc_card;
2832}