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