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