blob: 350c40a27b7f9aede6ca91668f2ed6845d548aed [file] [log] [blame]
Channagoud Kadabie6f59dc2014-01-06 17:31:39 -08001/* Copyright (c) 2010-2014, 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>
Channagoud Kadabi676c2e32013-04-02 11:39:01 -070037#include <bits.h>
Greg Griscod6250552011-06-29 14:40:23 -070038
Ajay Dudanib01e5062011-12-03 23:23:42 -080039#if MMC_BOOT_ADM
40#include "adm.h"
41#endif
Greg Griscod6250552011-06-29 14:40:23 -070042
Deepa Dinamani07e66872012-06-29 18:32:05 -070043#if MMC_BOOT_BAM
44#include "bam.h"
45#include "mmc_dml.h"
46#endif
47
Ajay Dudanib01e5062011-12-03 23:23:42 -080048#ifndef NULL
49#define NULL 0
50#endif
Greg Griscod6250552011-06-29 14:40:23 -070051
Channagoud Kadabi672c4c42012-12-20 17:51:45 -080052#define USEC_PER_SEC (1000000L)
53
Ajay Dudanib01e5062011-12-03 23:23:42 -080054#define MMC_BOOT_DATA_READ 0
55#define MMC_BOOT_DATA_WRITE 1
Greg Griscod6250552011-06-29 14:40:23 -070056
Deepa Dinamani07e66872012-06-29 18:32:05 -070057static unsigned int mmc_boot_data_transfer(unsigned int *data_ptr,
Ajay Dudanib01e5062011-12-03 23:23:42 -080058 unsigned int data_len,
59 unsigned char direction);
Greg Griscod6250552011-06-29 14:40:23 -070060
Ajay Dudanib01e5062011-12-03 23:23:42 -080061static unsigned int mmc_boot_fifo_read(unsigned int *data_ptr,
62 unsigned int data_len);
Greg Griscod6250552011-06-29 14:40:23 -070063
Ajay Dudanib01e5062011-12-03 23:23:42 -080064static unsigned int mmc_boot_fifo_write(unsigned int *data_ptr,
65 unsigned int data_len);
Greg Grisco6e754772011-06-23 12:19:39 -070066
Deepa Dinamani07e66872012-06-29 18:32:05 -070067static unsigned int mmc_boot_status_error(unsigned mmc_status);
68
69#if MMC_BOOT_BAM
70
71void mmc_boot_dml_init();
72
73static void mmc_boot_dml_producer_trans_init(unsigned trans_end,
74 unsigned size);
75
76static void mmc_boot_dml_consumer_trans_init();
77
78static uint32_t mmc_boot_dml_chk_producer_idle();
79
80static void mmc_boot_dml_wait_producer_idle();
81static void mmc_boot_dml_wait_consumer_idle();
82static void mmc_boot_dml_reset();
83static int mmc_bam_init(uint32_t bam_base);
84static int mmc_bam_transfer_data();
85static unsigned int
86mmc_boot_bam_setup_desc(unsigned int *data_ptr,
87 unsigned int data_len, unsigned char direction);
Sundarajan Srinivasand16443b2013-10-02 00:16:06 -070088uint32_t mmc_page_size();
Deepa Dinamani07e66872012-06-29 18:32:05 -070089
90#endif
91
92
Ajay Dudanib01e5062011-12-03 23:23:42 -080093#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Greg Grisco6e754772011-06-23 12:19:39 -070094
Ajay Dudanib01e5062011-12-03 23:23:42 -080095/* data access time unit in ns */
96static const unsigned int taac_unit[] =
97 { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000 };
98/* data access time value x 10 */
99static const unsigned int taac_value[] =
100 { 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 };
Greg Grisco6e754772011-06-23 12:19:39 -0700101
Ajay Dudanib01e5062011-12-03 23:23:42 -0800102/* data transfer rate in kbit/s */
103static const unsigned int xfer_rate_unit[] =
104 { 100, 1000, 10000, 100000, 0, 0, 0, 0 };
105/* data transfer rate value x 10*/
106static const unsigned int xfer_rate_value[] =
107 { 0, 10, 12, 13, 15, 20, 26, 30, 35, 40, 45, 52, 55, 60, 70, 80 };
108
109unsigned char mmc_slot = 0;
110unsigned int mmc_boot_mci_base = 0;
111
112static unsigned char ext_csd_buf[512];
113static unsigned char wp_status_buf[8];
114
Deepa Dinamani07e66872012-06-29 18:32:05 -0700115#if MMC_BOOT_BAM
116
117static uint32_t mmc_sdc_bam_base[] =
118 { MSM_SDC1_BAM_BASE, MSM_SDC2_BAM_BASE, MSM_SDC3_BAM_BASE, MSM_SDC4_BAM_BASE };
119
120static uint32_t mmc_sdc_dml_base[] =
121 { MSM_SDC1_DML_BASE, MSM_SDC2_DML_BASE, MSM_SDC3_DML_BASE, MSM_SDC4_DML_BASE };
122
123uint32_t dml_base;
124static struct bam_instance bam;
125
126#define MMC_BOOT_BAM_FIFO_SIZE 100
127
128#define MMC_BOOT_BAM_READ_PIPE_INDEX 0
129#define MMC_BOOT_BAM_WRITE_PIPE_INDEX 1
130
131#define MMC_BOOT_BAM_READ_PIPE 0
132#define MMC_BOOT_BAM_WRITE_PIPE 1
133
134/* Align at BAM_DESC_SIZE boundary */
135static struct bam_desc desc_fifo[MMC_BOOT_BAM_FIFO_SIZE] __attribute__ ((aligned(BAM_DESC_SIZE)));
136
137#endif
138
Ajay Dudanib01e5062011-12-03 23:23:42 -0800139int mmc_clock_enable_disable(unsigned id, unsigned enable);
140int mmc_clock_get_rate(unsigned id);
141int mmc_clock_set_rate(unsigned id, unsigned rate);
142
Channagoud Kadabi398e1a22013-03-18 12:06:11 -0700143struct mmc_host mmc_host;
144struct mmc_card mmc_card;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800145
Shashank Mittalac23fa12012-02-13 17:38:15 -0800146static unsigned int mmc_wp(unsigned int addr, unsigned int size,
147 unsigned char set_clear_wp);
Channagoud Kadabi398e1a22013-03-18 12:06:11 -0700148static unsigned int mmc_boot_send_ext_cmd(struct mmc_card *card,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800149 unsigned char *buf);
Channagoud Kadabi398e1a22013-03-18 12:06:11 -0700150static unsigned int mmc_boot_read_reg(struct mmc_card *card,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800151 unsigned int data_len,
152 unsigned int command,
153 unsigned int addr, unsigned int *out);
154
155unsigned int SWAP_ENDIAN(unsigned int val)
156{
157 return ((val & 0xFF) << 24) |
158 (((val >> 8) & 0xFF) << 16) | (((val >> 16) & 0xFF) << 8) | (val >>
159 24);
Amol Jadi84a546a2011-03-02 12:09:11 -0800160}
161
Sundarajan Srinivasand16443b2013-10-02 00:16:06 -0700162uint32_t mmc_page_size()
163{
164 return BOARD_KERNEL_PAGESIZE;
165}
166
Channagoud Kadabi672c4c42012-12-20 17:51:45 -0800167void mmc_mclk_reg_wr_delay()
168{
169 if (mmc_host.mmc_cont_version)
170 {
171 /* Wait for the MMC_BOOT_MCI register write to go through. */
172 while(readl(MMC_BOOT_MCI_STATUS2) & MMC_BOOT_MCI_MCLK_REG_WR_ACTIVE);
173 }
174 else
175 udelay((1 + ((3 * USEC_PER_SEC) / (mmc_host.mclk_rate? mmc_host.mclk_rate : MMC_CLK_400KHZ))));
176}
177
Ajay Dudanib01e5062011-12-03 23:23:42 -0800178/* Sets a timeout for read operation.
179 */
180static unsigned int
Channagoud Kadabi398e1a22013-03-18 12:06:11 -0700181mmc_boot_set_read_timeout(struct mmc_host *host,
182 struct mmc_card *card)
Ajay Dudanib01e5062011-12-03 23:23:42 -0800183{
184 unsigned int timeout_ns = 0;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700185
Ajay Dudanib01e5062011-12-03 23:23:42 -0800186 if ((host == NULL) || (card == NULL)) {
187 return MMC_BOOT_E_INVAL;
188 }
189
190 if ((card->type == MMC_BOOT_TYPE_MMCHC)
191 || (card->type == MMC_BOOT_TYPE_SDHC)) {
192 card->rd_timeout_ns = 100000000;
193 } else if ((card->type == MMC_BOOT_TYPE_STD_SD)
194 || (card->type == MMC_BOOT_TYPE_STD_MMC)) {
195 timeout_ns = 10 * ((card->csd.taac_ns) +
196 (card->csd.nsac_clk_cycle /
197 (host->mclk_rate / 1000000000)));
198 card->rd_timeout_ns = timeout_ns;
199 } else {
200 return MMC_BOOT_E_NOT_SUPPORTED;
201 }
202
203 dprintf(SPEW, " Read timeout set: %d ns\n", card->rd_timeout_ns);
204
205 return MMC_BOOT_E_SUCCESS;
Neeti Desai5f26aff2011-09-30 10:27:40 -0700206}
207
Ajay Dudanib01e5062011-12-03 23:23:42 -0800208/* Sets a timeout for write operation.
209 */
210static unsigned int
Channagoud Kadabi398e1a22013-03-18 12:06:11 -0700211mmc_boot_set_write_timeout(struct mmc_host *host,
212 struct mmc_card *card)
Ajay Dudanib01e5062011-12-03 23:23:42 -0800213{
214 unsigned int timeout_ns = 0;
215
216 if ((host == NULL) || (card == NULL)) {
217 return MMC_BOOT_E_INVAL;
218 }
219
220 if ((card->type == MMC_BOOT_TYPE_MMCHC)
221 || (card->type == MMC_BOOT_TYPE_SDHC)) {
222 card->wr_timeout_ns = 100000000;
223 } else if (card->type == MMC_BOOT_TYPE_STD_SD
224 || (card->type == MMC_BOOT_TYPE_STD_MMC)) {
225 timeout_ns = 10 * ((card->csd.taac_ns) +
226 (card->csd.nsac_clk_cycle /
227 (host->mclk_rate / 1000000000)));
228 timeout_ns = timeout_ns << card->csd.r2w_factor;
229 card->wr_timeout_ns = timeout_ns;
230 } else {
231 return MMC_BOOT_E_NOT_SUPPORTED;
232 }
233
234 dprintf(SPEW, " Write timeout set: %d ns\n", card->wr_timeout_ns);
235
236 return MMC_BOOT_E_SUCCESS;
237}
238
239/*
240 * Decodes CSD response received from the card. Note that we have defined only
241 * few of the CSD elements in csd structure. We'll only decode those values.
242 */
243static unsigned int
Channagoud Kadabi398e1a22013-03-18 12:06:11 -0700244mmc_boot_decode_and_save_csd(struct mmc_card *card, unsigned int *raw_csd)
Ajay Dudanib01e5062011-12-03 23:23:42 -0800245{
246 unsigned int mmc_sizeof = 0;
247 unsigned int mmc_unit = 0;
248 unsigned int mmc_value = 0;
249 unsigned int mmc_temp = 0;
250
Channagoud Kadabi398e1a22013-03-18 12:06:11 -0700251 struct mmc_csd mmc_csd;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800252
253 if ((card == NULL) || (raw_csd == NULL)) {
254 return MMC_BOOT_E_INVAL;
255 }
256
257 mmc_sizeof = sizeof(unsigned int) * 8;
258
259 mmc_csd.cmmc_structure = UNPACK_BITS(raw_csd, 126, 2, mmc_sizeof);
260
261 if ((card->type == MMC_BOOT_TYPE_SDHC)
262 || (card->type == MMC_BOOT_TYPE_STD_SD)) {
263 /* Parse CSD according to SD card spec. */
264
265 /* CSD register is little bit differnet for CSD version 2.0 High Capacity
266 * and CSD version 1.0/2.0 Standard memory cards. In Version 2.0 some of
267 * the fields have fixed values and it's not necessary for host to refer
268 * these fields in CSD sent by card */
269
270 if (mmc_csd.cmmc_structure == 1) {
271 /* CSD Version 2.0 */
272 mmc_csd.card_cmd_class =
273 UNPACK_BITS(raw_csd, 84, 12, mmc_sizeof);
274 mmc_csd.write_blk_len = 512; /* Fixed value is 9 = 2^9 = 512 */
275 mmc_csd.read_blk_len = 512; /* Fixed value is 9 = 512 */
276 mmc_csd.r2w_factor = 0x2; /* Fixed value: 010b */
277 mmc_csd.c_size_mult = 0; /* not there in version 2.0 */
278 mmc_csd.c_size =
279 UNPACK_BITS(raw_csd, 48, 22, mmc_sizeof);
280 mmc_csd.nsac_clk_cycle =
281 UNPACK_BITS(raw_csd, 104, 8, mmc_sizeof) * 100;
282
283//TODO: Investigate the nsac and taac. Spec suggests not using this for timeouts.
284
285 mmc_unit = UNPACK_BITS(raw_csd, 112, 3, mmc_sizeof);
286 mmc_value = UNPACK_BITS(raw_csd, 115, 4, mmc_sizeof);
287 mmc_csd.taac_ns =
288 (taac_value[mmc_value] * taac_unit[mmc_unit]) / 10;
289
290 mmc_csd.erase_blk_len = 1;
291 mmc_csd.read_blk_misalign = 0;
292 mmc_csd.write_blk_misalign = 0;
293 mmc_csd.read_blk_partial = 0;
294 mmc_csd.write_blk_partial = 0;
295
296 mmc_unit = UNPACK_BITS(raw_csd, 96, 3, mmc_sizeof);
297 mmc_value = UNPACK_BITS(raw_csd, 99, 4, mmc_sizeof);
298 mmc_csd.tran_speed =
299 (xfer_rate_value[mmc_value] *
300 xfer_rate_unit[mmc_unit]) / 10;
301
302 mmc_csd.wp_grp_size = 0x0;
303 mmc_csd.wp_grp_enable = 0x0;
304 mmc_csd.perm_wp =
305 UNPACK_BITS(raw_csd, 13, 1, mmc_sizeof);
306 mmc_csd.temp_wp =
307 UNPACK_BITS(raw_csd, 12, 1, mmc_sizeof);
308
309 /* Calculate the card capcity */
310 card->capacity = (1 + mmc_csd.c_size) * 512 * 1024;
311 } else {
312 /* CSD Version 1.0 */
313 mmc_csd.card_cmd_class =
314 UNPACK_BITS(raw_csd, 84, 12, mmc_sizeof);
315
316 mmc_temp = UNPACK_BITS(raw_csd, 22, 4, mmc_sizeof);
317 mmc_csd.write_blk_len = (mmc_temp > 8
318 && mmc_temp <
319 12) ? (1 << mmc_temp) : 512;
320
321 mmc_temp = UNPACK_BITS(raw_csd, 80, 4, mmc_sizeof);
322 mmc_csd.read_blk_len = (mmc_temp > 8
323 && mmc_temp <
324 12) ? (1 << mmc_temp) : 512;
325
326 mmc_unit = UNPACK_BITS(raw_csd, 112, 3, mmc_sizeof);
327 mmc_value = UNPACK_BITS(raw_csd, 115, 4, mmc_sizeof);
328 mmc_csd.taac_ns =
329 (taac_value[mmc_value] * taac_unit[mmc_unit]) / 10;
330
331 mmc_unit = UNPACK_BITS(raw_csd, 96, 3, mmc_sizeof);
332 mmc_value = UNPACK_BITS(raw_csd, 99, 4, mmc_sizeof);
333 mmc_csd.tran_speed =
334 (xfer_rate_value[mmc_value] *
335 xfer_rate_unit[mmc_unit]) / 10;
336
337 mmc_csd.nsac_clk_cycle =
338 UNPACK_BITS(raw_csd, 104, 8, mmc_sizeof) * 100;
339
340 mmc_csd.r2w_factor =
341 UNPACK_BITS(raw_csd, 26, 3, mmc_sizeof);
342 mmc_csd.sector_size =
343 UNPACK_BITS(raw_csd, 39, 7, mmc_sizeof) + 1;
344
345 mmc_csd.erase_blk_len =
346 UNPACK_BITS(raw_csd, 46, 1, mmc_sizeof);
347 mmc_csd.read_blk_misalign =
348 UNPACK_BITS(raw_csd, 77, 1, mmc_sizeof);
349 mmc_csd.write_blk_misalign =
350 UNPACK_BITS(raw_csd, 78, 1, mmc_sizeof);
351 mmc_csd.read_blk_partial =
352 UNPACK_BITS(raw_csd, 79, 1, mmc_sizeof);
353 mmc_csd.write_blk_partial =
354 UNPACK_BITS(raw_csd, 21, 1, mmc_sizeof);
355
356 mmc_csd.c_size_mult =
357 UNPACK_BITS(raw_csd, 47, 3, mmc_sizeof);
358 mmc_csd.c_size =
359 UNPACK_BITS(raw_csd, 62, 12, mmc_sizeof);
360 mmc_csd.wp_grp_size =
361 UNPACK_BITS(raw_csd, 32, 7, mmc_sizeof);
362 mmc_csd.wp_grp_enable =
363 UNPACK_BITS(raw_csd, 31, 1, mmc_sizeof);
364 mmc_csd.perm_wp =
365 UNPACK_BITS(raw_csd, 13, 1, mmc_sizeof);
366 mmc_csd.temp_wp =
367 UNPACK_BITS(raw_csd, 12, 1, mmc_sizeof);
368
369 /* Calculate the card capacity */
370 mmc_temp =
371 (1 << (mmc_csd.c_size_mult + 2)) * (mmc_csd.c_size +
372 1);
373 card->capacity = mmc_temp * mmc_csd.read_blk_len;
374 }
375 } else {
376 /* Parse CSD according to MMC card spec. */
377 mmc_csd.spec_vers = UNPACK_BITS(raw_csd, 122, 4, mmc_sizeof);
378 mmc_csd.card_cmd_class =
379 UNPACK_BITS(raw_csd, 84, 12, mmc_sizeof);
380 mmc_csd.write_blk_len =
381 1 << UNPACK_BITS(raw_csd, 22, 4, mmc_sizeof);
382 mmc_csd.read_blk_len =
383 1 << UNPACK_BITS(raw_csd, 80, 4, mmc_sizeof);
384 mmc_csd.r2w_factor = UNPACK_BITS(raw_csd, 26, 3, mmc_sizeof);
385 mmc_csd.c_size_mult = UNPACK_BITS(raw_csd, 47, 3, mmc_sizeof);
386 mmc_csd.c_size = UNPACK_BITS(raw_csd, 62, 12, mmc_sizeof);
387 mmc_csd.nsac_clk_cycle =
388 UNPACK_BITS(raw_csd, 104, 8, mmc_sizeof) * 100;
389
390 mmc_unit = UNPACK_BITS(raw_csd, 112, 3, mmc_sizeof);
391 mmc_value = UNPACK_BITS(raw_csd, 115, 4, mmc_sizeof);
392 mmc_csd.taac_ns =
393 (taac_value[mmc_value] * taac_unit[mmc_unit]) / 10;
394
395 mmc_csd.read_blk_misalign =
396 UNPACK_BITS(raw_csd, 77, 1, mmc_sizeof);
397 mmc_csd.write_blk_misalign =
398 UNPACK_BITS(raw_csd, 78, 1, mmc_sizeof);
399 mmc_csd.read_blk_partial =
400 UNPACK_BITS(raw_csd, 79, 1, mmc_sizeof);
401 mmc_csd.write_blk_partial =
402 UNPACK_BITS(raw_csd, 21, 1, mmc_sizeof);
403 mmc_csd.tran_speed = 0x00; /* Ignore -- no use of this value. */
404
405 mmc_csd.erase_grp_size =
406 UNPACK_BITS(raw_csd, 42, 5, mmc_sizeof);
407 mmc_csd.erase_grp_mult =
408 UNPACK_BITS(raw_csd, 37, 5, mmc_sizeof);
409 mmc_csd.wp_grp_size = UNPACK_BITS(raw_csd, 32, 5, mmc_sizeof);
410 mmc_csd.wp_grp_enable = UNPACK_BITS(raw_csd, 31, 1, mmc_sizeof);
411 mmc_csd.perm_wp = UNPACK_BITS(raw_csd, 13, 1, mmc_sizeof);
412 mmc_csd.temp_wp = UNPACK_BITS(raw_csd, 12, 1, mmc_sizeof);
413
414 /* Calculate the card capcity */
415 if (mmc_csd.c_size != 0xFFF) {
416 /* For cards less than or equal to 2GB */
417 mmc_temp =
418 (1 << (mmc_csd.c_size_mult + 2)) * (mmc_csd.c_size +
419 1);
420 card->capacity = mmc_temp * mmc_csd.read_blk_len;
421 } else {
422 /* For cards greater than 2GB, Ext CSD register's SEC_COUNT
423 * is used to calculate the size.
424 */
425 unsigned long long sec_count;
426
427 sec_count = (ext_csd_buf[215] << 24) |
428 (ext_csd_buf[214] << 16) |
429 (ext_csd_buf[213] << 8) | ext_csd_buf[212];
430
431 card->capacity = sec_count * 512;
432 }
433 }
434
435 /* save the information in card structure */
Channagoud Kadabi398e1a22013-03-18 12:06:11 -0700436 memcpy((struct mmc_csd *)&card->csd,
437 (struct mmc_csd *)&mmc_csd, sizeof(struct mmc_csd));
Ajay Dudanib01e5062011-12-03 23:23:42 -0800438
439 dprintf(SPEW, "Decoded CSD fields:\n");
440 dprintf(SPEW, "cmmc_structure: %d\n", mmc_csd.cmmc_structure);
441 dprintf(SPEW, "card_cmd_class: %x\n", mmc_csd.card_cmd_class);
442 dprintf(SPEW, "write_blk_len: %d\n", mmc_csd.write_blk_len);
443 dprintf(SPEW, "read_blk_len: %d\n", mmc_csd.read_blk_len);
444 dprintf(SPEW, "r2w_factor: %d\n", mmc_csd.r2w_factor);
445 dprintf(SPEW, "sector_size: %d\n", mmc_csd.sector_size);
446 dprintf(SPEW, "c_size_mult:%d\n", mmc_csd.c_size_mult);
447 dprintf(SPEW, "c_size: %d\n", mmc_csd.c_size);
448 dprintf(SPEW, "nsac_clk_cycle: %d\n", mmc_csd.nsac_clk_cycle);
449 dprintf(SPEW, "taac_ns: %d\n", mmc_csd.taac_ns);
450 dprintf(SPEW, "tran_speed: %d kbps\n", mmc_csd.tran_speed);
451 dprintf(SPEW, "erase_blk_len: %d\n", mmc_csd.erase_blk_len);
452 dprintf(SPEW, "read_blk_misalign: %d\n", mmc_csd.read_blk_misalign);
453 dprintf(SPEW, "write_blk_misalign: %d\n", mmc_csd.write_blk_misalign);
454 dprintf(SPEW, "read_blk_partial: %d\n", mmc_csd.read_blk_partial);
455 dprintf(SPEW, "write_blk_partial: %d\n", mmc_csd.write_blk_partial);
456 dprintf(SPEW, "Card Capacity: %llu Bytes\n", card->capacity);
457
458 return MMC_BOOT_E_SUCCESS;
459}
460
461/*
462 * Decode CID sent by the card.
463 */
464static unsigned int
Channagoud Kadabi398e1a22013-03-18 12:06:11 -0700465mmc_boot_decode_and_save_cid(struct mmc_card *card, unsigned int *raw_cid)
Ajay Dudanib01e5062011-12-03 23:23:42 -0800466{
Channagoud Kadabi398e1a22013-03-18 12:06:11 -0700467 struct mmc_cid mmc_cid;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800468 unsigned int mmc_sizeof = 0;
469 int i = 0;
470
471 if ((card == NULL) || (raw_cid == NULL)) {
472 return MMC_BOOT_E_INVAL;
473 }
474
475 mmc_sizeof = sizeof(unsigned int) * 8;
476
477 if ((card->type == MMC_BOOT_TYPE_SDHC)
478 || (card->type == MMC_BOOT_TYPE_STD_SD)) {
479 mmc_cid.mid = UNPACK_BITS(raw_cid, 120, 8, mmc_sizeof);
480 mmc_cid.oid = UNPACK_BITS(raw_cid, 104, 16, mmc_sizeof);
481
482 for (i = 0; i < 5; i++) {
483 mmc_cid.pnm[i] = (unsigned char)UNPACK_BITS(raw_cid,
484 (104 -
485 8 * (i +
486 1)),
487 8,
488 mmc_sizeof);
489 }
490 mmc_cid.pnm[5] = 0;
491 mmc_cid.pnm[6] = 0;
492
493 mmc_cid.prv = UNPACK_BITS(raw_cid, 56, 8, mmc_sizeof);
494 mmc_cid.psn = UNPACK_BITS(raw_cid, 24, 32, mmc_sizeof);
495 mmc_cid.month = UNPACK_BITS(raw_cid, 8, 4, mmc_sizeof);
496 mmc_cid.year = UNPACK_BITS(raw_cid, 12, 8, mmc_sizeof);
497 mmc_cid.year += 2000;
498 } else {
499 mmc_cid.mid = UNPACK_BITS(raw_cid, 120, 8, mmc_sizeof);
500 mmc_cid.oid = UNPACK_BITS(raw_cid, 104, 16, mmc_sizeof);
501
502 for (i = 0; i < 6; i++) {
503 mmc_cid.pnm[i] = (unsigned char)UNPACK_BITS(raw_cid,
504 (104 -
505 8 * (i +
506 1)),
507 8,
508 mmc_sizeof);
509 }
510 mmc_cid.pnm[6] = 0;
511
512 mmc_cid.prv = UNPACK_BITS(raw_cid, 48, 8, mmc_sizeof);
513 mmc_cid.psn = UNPACK_BITS(raw_cid, 16, 32, mmc_sizeof);
514 mmc_cid.month = UNPACK_BITS(raw_cid, 8, 4, mmc_sizeof);
515 mmc_cid.year = UNPACK_BITS(raw_cid, 12, 4, mmc_sizeof);
516 mmc_cid.year += 1997;
517 }
518
519 /* save it in card database */
Channagoud Kadabi398e1a22013-03-18 12:06:11 -0700520 memcpy((struct mmc_cid *)&card->cid,
521 (struct mmc_cid *)&mmc_cid, sizeof(struct mmc_cid));
Ajay Dudanib01e5062011-12-03 23:23:42 -0800522
523 dprintf(SPEW, "Decoded CID fields:\n");
524 dprintf(SPEW, "Manufacturer ID: %x\n", mmc_cid.mid);
525 dprintf(SPEW, "OEM ID: 0x%x\n", mmc_cid.oid);
526 dprintf(SPEW, "Product Name: %s\n", mmc_cid.pnm);
527 dprintf(SPEW, "Product revision: %d.%d\n", (mmc_cid.prv >> 4),
528 (mmc_cid.prv & 0xF));
529 dprintf(SPEW, "Product serial number: %X\n", mmc_cid.psn);
530 dprintf(SPEW, "Manufacturing date: %d %d\n", mmc_cid.month,
531 mmc_cid.year);
532
533 return MMC_BOOT_E_SUCCESS;
534}
535
536/*
537 * Sends specified command to a card and waits for a response.
538 */
539static unsigned int mmc_boot_send_command(struct mmc_boot_command *cmd)
540{
541 unsigned int mmc_cmd = 0;
542 unsigned int mmc_status = 0;
543 unsigned int mmc_resp = 0;
544 unsigned int mmc_return = MMC_BOOT_E_SUCCESS;
545 unsigned int cmd_index = 0;
546 int i = 0;
547
548 /* basic check */
549 if (cmd == NULL) {
550 return MMC_BOOT_E_INVAL;
551 }
552
553 /* 1. Write command argument to MMC_BOOT_MCI_ARGUMENT register */
554 writel(cmd->argument, MMC_BOOT_MCI_ARGUMENT);
555
Ajay Dudanib01e5062011-12-03 23:23:42 -0800556 /* 2. Set appropriate fields and write MMC_BOOT_MCI_CMD */
557 /* 2a. Write command index in CMD_INDEX field */
558 cmd_index = cmd->cmd_index;
559 mmc_cmd |= cmd->cmd_index;
Channagoud Kadabi54dd5312013-06-18 18:31:44 -0700560 /* 2b. Set RESPONSE bit to 1 for all cmds except CMD0
561 * And dont set RESPONSE bit for commands with no response
562 */
563 if (cmd_index != CMD0_GO_IDLE_STATE && cmd->resp_type != MMC_BOOT_RESP_NONE) {
Ajay Dudanib01e5062011-12-03 23:23:42 -0800564 mmc_cmd |= MMC_BOOT_MCI_CMD_RESPONSE;
565 }
566
567 /* 2c. Set LONGRESP bit to 1 for CMD2, CMD9 and CMD10 */
568 if (IS_RESP_136_BITS(cmd->resp_type)) {
569 mmc_cmd |= MMC_BOOT_MCI_CMD_LONGRSP;
570 }
571
572 /* 2d. Set INTERRUPT bit to 1 to disable command timeout */
573
574 /* 2e. Set PENDING bit to 1 for CMD12 in the beginning of stream
575 mode data transfer */
576 if (cmd->xfer_mode == MMC_BOOT_XFER_MODE_STREAM) {
577 mmc_cmd |= MMC_BOOT_MCI_CMD_PENDING;
578 }
579
580 /* 2f. Set ENABLE bit to 1 */
581 mmc_cmd |= MMC_BOOT_MCI_CMD_ENABLE;
582
Channagoud Kadabi672c4c42012-12-20 17:51:45 -0800583 /* 2g. Set PROG_ENA bit */
584 if (cmd->prg_enabled) {
Ajay Dudanib01e5062011-12-03 23:23:42 -0800585 mmc_cmd |= MMC_BOOT_MCI_CMD_PROG_ENA;
586 }
587
588 /* 2h. Set MCIABORT bit to 1 for CMD12 when working with SDIO card */
589 /* 2i. Set CCS_ENABLE bit to 1 for CMD61 when Command Completion Signal
590 of CE-ATA device is enabled */
591
592 /* 2j. clear all static status bits */
593 writel(MMC_BOOT_MCI_STATIC_STATUS, MMC_BOOT_MCI_CLEAR);
594
595 /* 2k. Write to MMC_BOOT_MCI_CMD register */
596 writel(mmc_cmd, MMC_BOOT_MCI_CMD);
597
Channagoud Kadabi672c4c42012-12-20 17:51:45 -0800598 /* Wait for the MMC_BOOT_MCI_CMD write to go through. */
599 mmc_mclk_reg_wr_delay();
600
Ajay Dudanib01e5062011-12-03 23:23:42 -0800601 dprintf(SPEW, "Command sent: CMD%d MCI_CMD_REG:%x MCI_ARG:%x\n",
602 cmd_index, mmc_cmd, cmd->argument);
603
604 /* 3. Wait for interrupt or poll on the following bits of MCI_STATUS
605 register */
606 do {
607 /* 3a. Read MCI_STATUS register */
608 while (readl(MMC_BOOT_MCI_STATUS) &
609 MMC_BOOT_MCI_STAT_CMD_ACTIVE) ;
610
611 mmc_status = readl(MMC_BOOT_MCI_STATUS);
612
613 /* 3b. CMD_SENT bit supposed to be set to 1 only after CMD0 is sent -
614 no response required. */
615 if ((cmd->resp_type == MMC_BOOT_RESP_NONE) &&
616 (mmc_status & MMC_BOOT_MCI_STAT_CMD_SENT)) {
617 break;
618 }
619
620 /* 3c. If CMD_TIMEOUT bit is set then no response was received */
621 else if (mmc_status & MMC_BOOT_MCI_STAT_CMD_TIMEOUT) {
622 mmc_return = MMC_BOOT_E_TIMEOUT;
623 break;
624 }
625 /* 3d. If CMD_RESPONSE_END bit is set to 1 then command's response was
626 received and CRC check passed
627 Spcial case for ACMD41: it seems to always fail CRC even if
628 the response is valid
629 */
630 else if ((mmc_status & MMC_BOOT_MCI_STAT_CMD_RESP_END)
631 || (cmd_index == CMD1_SEND_OP_COND)
632 || (cmd_index == CMD8_SEND_IF_COND)) {
633 /* 3i. Read MCI_RESP_CMD register to verify that response index is
634 equal to command index */
635 mmc_resp = readl(MMC_BOOT_MCI_RESP_CMD) & 0x3F;
636
637 /* However, long response does not contain the command index field.
638 * In that case, response index field must be set to 111111b (0x3F) */
639 if ((mmc_resp == cmd_index) ||
640 (cmd->resp_type == MMC_BOOT_RESP_R2 ||
641 cmd->resp_type == MMC_BOOT_RESP_R3 ||
642 cmd->resp_type == MMC_BOOT_RESP_R6 ||
643 cmd->resp_type == MMC_BOOT_RESP_R7)) {
644 /* 3j. If resp index is equal to cmd index, read command resp
645 from MCI_RESPn registers
646 - MCI_RESP0/1/2/3 for CMD2/9/10
647 - MCI_RESP0 for all other registers */
648 if (IS_RESP_136_BITS(cmd->resp_type)) {
649 for (i = 0; i < 4; i++) {
650 cmd->resp[3 - i] =
651 readl(MMC_BOOT_MCI_RESP_0 +
652 (i * 4));
653
654 }
655 } else {
656 cmd->resp[0] =
657 readl(MMC_BOOT_MCI_RESP_0);
658 }
659 } else {
660 /* command index mis-match */
661 mmc_return = MMC_BOOT_E_CMD_INDX_MISMATCH;
662 }
663
664 dprintf(SPEW, "Command response received: %X\n",
665 cmd->resp[0]);
666 break;
667 }
668
669 /* 3e. If CMD_CRC_FAIL bit is set to 1 then cmd's response was recvd,
670 but CRC check failed. */
671 else if ((mmc_status & MMC_BOOT_MCI_STAT_CMD_CRC_FAIL)) {
672 if (cmd_index == ACMD41_SEND_OP_COND) {
673 cmd->resp[0] = readl(MMC_BOOT_MCI_RESP_0);
674 } else
675 mmc_return = MMC_BOOT_E_CRC_FAIL;
676 break;
677 }
678
679 }
680 while (1);
681
Channagoud Kadabi672c4c42012-12-20 17:51:45 -0800682
683 /* 2k. Write to MMC_BOOT_MCI_CMD register */
684 writel(0, MMC_BOOT_MCI_CMD);
685
686 /* Wait for the MMC_BOOT_MCI_CMD write to go through. */
687 mmc_mclk_reg_wr_delay();
688
Ajay Dudanib01e5062011-12-03 23:23:42 -0800689 return mmc_return;
690}
691
692/*
693 * Reset all the cards to idle condition (CMD 0)
694 */
695static unsigned int mmc_boot_reset_cards(void)
696{
697 struct mmc_boot_command cmd;
698
699 memset((struct mmc_boot_command *)&cmd, 0,
700 sizeof(struct mmc_boot_command));
701
702 cmd.cmd_index = CMD0_GO_IDLE_STATE;
703 cmd.argument = 0; // stuff bits - ignored
704 cmd.cmd_type = MMC_BOOT_CMD_BCAST;
705 cmd.resp_type = MMC_BOOT_RESP_NONE;
706
707 /* send command */
708 return mmc_boot_send_command(&cmd);
709}
710
711/*
712 * Send CMD1 to know whether the card supports host VDD profile or not.
713 */
714static unsigned int
Channagoud Kadabi398e1a22013-03-18 12:06:11 -0700715mmc_boot_send_op_cond(struct mmc_host *host, struct mmc_card *card)
Ajay Dudanib01e5062011-12-03 23:23:42 -0800716{
717 struct mmc_boot_command cmd;
718 unsigned int mmc_resp = 0;
719 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
720
721 /* basic check */
722 if ((host == NULL) || (card == NULL)) {
723 return MMC_BOOT_E_INVAL;
724 }
725
726 memset((struct mmc_boot_command *)&cmd, 0,
727 sizeof(struct mmc_boot_command));
728
729 /* CMD1 format:
730 * [31] Busy bit
731 * [30:29] Access mode
732 * [28:24] reserved
733 * [23:15] 2.7-3.6
734 * [14:8] 2.0-2.6
735 * [7] 1.7-1.95
736 * [6:0] reserved
737 */
738
739 cmd.cmd_index = CMD1_SEND_OP_COND;
740 cmd.argument = host->ocr;
741 cmd.cmd_type = MMC_BOOT_CMD_BCAST_W_RESP;
742 cmd.resp_type = MMC_BOOT_RESP_R3;
743
744 mmc_ret = mmc_boot_send_command(&cmd);
745 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
746 return mmc_ret;
747 }
748
749 /* Now it's time to examine response */
750 mmc_resp = cmd.resp[0];
751
752 /* Response contains card's ocr. Update card's information */
753 card->ocr = mmc_resp;
754
755 /* Check the response for busy status */
756 if (!(mmc_resp & MMC_BOOT_OCR_BUSY)) {
757 return MMC_BOOT_E_CARD_BUSY;
758 }
759
760 if (mmc_resp & MMC_BOOT_OCR_SEC_MODE) {
761 card->type = MMC_BOOT_TYPE_MMCHC;
762 } else {
763 card->type = MMC_BOOT_TYPE_STD_MMC;
764 }
765 return MMC_BOOT_E_SUCCESS;
766}
767
768/*
769 * Request any card to send its uniquie card identification (CID) number (CMD2).
770 */
Channagoud Kadabi398e1a22013-03-18 12:06:11 -0700771static unsigned int mmc_boot_all_send_cid(struct mmc_card *card)
Ajay Dudanib01e5062011-12-03 23:23:42 -0800772{
773 struct mmc_boot_command cmd;
774 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
775
776 /* basic check */
777 if (card == NULL) {
778 return MMC_BOOT_E_INVAL;
779 }
780
781 memset((struct mmc_boot_command *)&cmd, 0,
782 sizeof(struct mmc_boot_command));
783
784 /* CMD2 Format:
785 * [31:0] stuff bits
786 */
787 cmd.cmd_index = CMD2_ALL_SEND_CID;
788 cmd.argument = 0;
789 cmd.cmd_type = MMC_BOOT_CMD_BCAST_W_RESP;
790 cmd.resp_type = MMC_BOOT_RESP_R2;
791
792 /* send command */
793 mmc_ret = mmc_boot_send_command(&cmd);
794 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
795 return mmc_ret;
796 }
797
798 /* Response contains card's 128 bits CID register */
799 mmc_ret = mmc_boot_decode_and_save_cid(card, cmd.resp);
800 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
801 return mmc_ret;
802 }
803 return MMC_BOOT_E_SUCCESS;
804}
805
806/*
807 * Ask any card to send it's relative card address (RCA).This RCA number is
808 * shorter than CID and is used by the host to address the card in future (CMD3)
809 */
Channagoud Kadabi398e1a22013-03-18 12:06:11 -0700810static unsigned int mmc_boot_send_relative_address(struct mmc_card *card)
Ajay Dudanib01e5062011-12-03 23:23:42 -0800811{
812 struct mmc_boot_command cmd;
813 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
814
815 /* basic check */
816 if (card == NULL) {
817 return MMC_BOOT_E_INVAL;
818 }
819
820 memset((struct mmc_boot_command *)&cmd, 0,
821 sizeof(struct mmc_boot_command));
822
823 /* CMD3 Format:
824 * [31:0] stuff bits
825 */
826 if (card->type == MMC_BOOT_TYPE_SDHC
827 || card->type == MMC_BOOT_TYPE_STD_SD) {
828 cmd.cmd_index = CMD3_SEND_RELATIVE_ADDR;
829 cmd.argument = 0;
830 cmd.cmd_type = MMC_BOOT_CMD_BCAST_W_RESP;
831 cmd.resp_type = MMC_BOOT_RESP_R6;
832
833 /* send command */
834 mmc_ret = mmc_boot_send_command(&cmd);
835 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
836 return mmc_ret;
837 }
838 /* For sD, card will send RCA. Store it */
839 card->rca = (cmd.resp[0] >> 16);
840 } else {
841 cmd.cmd_index = CMD3_SEND_RELATIVE_ADDR;
842 cmd.argument = (MMC_RCA << 16);
843 card->rca = (cmd.argument >> 16);
844 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
845 cmd.resp_type = MMC_BOOT_RESP_R1;
846
847 /* send command */
848 mmc_ret = mmc_boot_send_command(&cmd);
849 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
850 return mmc_ret;
851 }
852 }
853
854 return MMC_BOOT_E_SUCCESS;
855}
856
857/*
858 * Requests card to send it's CSD register's contents. (CMD9)
859 */
860static unsigned int
Channagoud Kadabi398e1a22013-03-18 12:06:11 -0700861mmc_boot_send_csd(struct mmc_card *card, unsigned int *raw_csd)
Ajay Dudanib01e5062011-12-03 23:23:42 -0800862{
863 struct mmc_boot_command cmd;
864 unsigned int mmc_arg = 0;
865 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
866
867 /* basic check */
868 if (card == NULL) {
869 return MMC_BOOT_E_INVAL;
870 }
871
872 memset((struct mmc_boot_command *)&cmd, 0,
873 sizeof(struct mmc_boot_command));
874
875 /* CMD9 Format:
876 * [31:16] RCA
877 * [15:0] stuff bits
878 */
879 mmc_arg |= card->rca << 16;
880
881 cmd.cmd_index = CMD9_SEND_CSD;
882 cmd.argument = mmc_arg;
883 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
884 cmd.resp_type = MMC_BOOT_RESP_R2;
885
886 /* send command */
887 mmc_ret = mmc_boot_send_command(&cmd);
888 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
889 return mmc_ret;
890 }
891
892 /* response contains the card csd */
893 memcpy(raw_csd, cmd.resp, sizeof(cmd.resp));
894
895 return MMC_BOOT_E_SUCCESS;
896}
897
898/*
899 * Selects a card by sending CMD7 to the card with its RCA.
900 * If RCA field is set as 0 ( or any other address ),
901 * the card will be de-selected. (CMD7)
902 */
903static unsigned int
Channagoud Kadabi398e1a22013-03-18 12:06:11 -0700904mmc_boot_select_card(struct mmc_card *card, unsigned int rca)
Ajay Dudanib01e5062011-12-03 23:23:42 -0800905{
906 struct mmc_boot_command cmd;
907 unsigned int mmc_arg = 0;
908 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
909
910 /* basic check */
911 if (card == NULL) {
912 return MMC_BOOT_E_INVAL;
913 }
914
915 memset((struct mmc_boot_command *)&cmd, 0,
916 sizeof(struct mmc_boot_command));
917
918 /* CMD7 Format:
919 * [31:16] RCA
920 * [15:0] stuff bits
921 */
922 mmc_arg |= rca << 16;
923
924 cmd.cmd_index = CMD7_SELECT_DESELECT_CARD;
925 cmd.argument = mmc_arg;
926 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
927 /* If we are deselecting card, we do not get response */
928 if (rca == card->rca && rca) {
929 if (card->type == MMC_BOOT_TYPE_SDHC
930 || card->type == MMC_BOOT_TYPE_STD_SD)
931 cmd.resp_type = MMC_BOOT_RESP_R1B;
932 else
933 cmd.resp_type = MMC_BOOT_RESP_R1;
934 } else {
935 cmd.resp_type = MMC_BOOT_RESP_NONE;
936 }
937
938 /* send command */
939 mmc_ret = mmc_boot_send_command(&cmd);
940 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
941 return mmc_ret;
942 }
943
944 /* As of now no need to look into a response. If it's required
945 * we'll explore later on */
946
947 return MMC_BOOT_E_SUCCESS;
948}
949
950/*
951 * Send command to set block length.
952 */
953static unsigned int
Channagoud Kadabi398e1a22013-03-18 12:06:11 -0700954mmc_boot_set_block_len(struct mmc_card *card, unsigned int block_len)
Ajay Dudanib01e5062011-12-03 23:23:42 -0800955{
956 struct mmc_boot_command cmd;
957 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
958
959 /* basic check */
960 if (card == NULL) {
961 return MMC_BOOT_E_INVAL;
962 }
963
964 memset((struct mmc_boot_command *)&cmd, 0,
965 sizeof(struct mmc_boot_command));
966
967 /* CMD16 Format:
968 * [31:0] block length
969 */
970
971 cmd.cmd_index = CMD16_SET_BLOCKLEN;
972 cmd.argument = block_len;
973 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
974 cmd.resp_type = MMC_BOOT_RESP_R1;
975
976 /* send command */
977 mmc_ret = mmc_boot_send_command(&cmd);
978 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
979 return mmc_ret;
980 }
981
982 /* If blocklength is larger than 512 bytes,
983 * the card sets BLOCK_LEN_ERROR bit. */
984 if (cmd.resp[0] & MMC_BOOT_R1_BLOCK_LEN_ERR) {
985 return MMC_BOOT_E_BLOCKLEN_ERR;
986 }
987 return MMC_BOOT_E_SUCCESS;
988}
989
990/*
991 * Requests the card to stop transmission of data.
992 */
993static unsigned int
Channagoud Kadabi398e1a22013-03-18 12:06:11 -0700994mmc_boot_send_stop_transmission(struct mmc_card *card,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800995 unsigned int prg_enabled)
996{
997 struct mmc_boot_command cmd;
998 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
999
1000 /* basic check */
1001 if (card == NULL) {
1002 return MMC_BOOT_E_INVAL;
1003 }
1004
1005 memset((struct mmc_boot_command *)&cmd, 0,
1006 sizeof(struct mmc_boot_command));
1007
1008 /* CMD12 Format:
1009 * [31:0] stuff bits
1010 */
1011
1012 cmd.cmd_index = CMD12_STOP_TRANSMISSION;
1013 cmd.argument = 0;
1014 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
1015 cmd.resp_type = MMC_BOOT_RESP_R1B;
1016 cmd.xfer_mode = MMC_BOOT_XFER_MODE_BLOCK;
1017 cmd.prg_enabled = prg_enabled;
1018
1019 /* send command */
1020 mmc_ret = mmc_boot_send_command(&cmd);
1021 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1022 return mmc_ret;
1023 }
1024 return MMC_BOOT_E_SUCCESS;
1025}
1026
1027/*
1028 * Get the card's current status
1029 */
1030static unsigned int
Channagoud Kadabi398e1a22013-03-18 12:06:11 -07001031mmc_boot_get_card_status(struct mmc_card *card,
Ajay Dudanib01e5062011-12-03 23:23:42 -08001032 unsigned int prg_enabled, unsigned int *status)
1033{
1034 struct mmc_boot_command cmd;
1035 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
1036
1037 /* basic check */
1038 if (card == NULL) {
1039 return MMC_BOOT_E_INVAL;
1040 }
1041
1042 memset((struct mmc_boot_command *)&cmd, 0,
1043 sizeof(struct mmc_boot_command));
1044
1045 /* CMD13 Format:
1046 * [31:16] RCA
1047 * [15:0] stuff bits
1048 */
1049 cmd.cmd_index = CMD13_SEND_STATUS;
1050 cmd.argument = card->rca << 16;
1051 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
1052 cmd.resp_type = MMC_BOOT_RESP_R1;
1053 cmd.prg_enabled = prg_enabled;
1054
1055 /* send command */
1056 mmc_ret = mmc_boot_send_command(&cmd);
1057 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1058 return mmc_ret;
1059 }
1060
1061 /* Checking ADDR_OUT_OF_RANGE error in CMD13 response */
1062 if (IS_ADDR_OUT_OF_RANGE(cmd.resp[0])) {
1063 return MMC_BOOT_E_FAILURE;
1064 }
1065
1066 *status = cmd.resp[0];
1067 return MMC_BOOT_E_SUCCESS;
1068}
1069
1070/*
1071 * Decode type of error caused during read and write
1072 */
1073static unsigned int mmc_boot_status_error(unsigned mmc_status)
1074{
1075 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
1076
1077 /* If DATA_CRC_FAIL bit is set to 1 then CRC error was detected by
1078 card/device during the data transfer */
1079 if (mmc_status & MMC_BOOT_MCI_STAT_DATA_CRC_FAIL) {
1080 mmc_ret = MMC_BOOT_E_DATA_CRC_FAIL;
1081 }
1082 /* If DATA_TIMEOUT bit is set to 1 then the data transfer time exceeded
1083 the data timeout period without completing the transfer */
1084 else if (mmc_status & MMC_BOOT_MCI_STAT_DATA_TIMEOUT) {
1085 mmc_ret = MMC_BOOT_E_DATA_TIMEOUT;
1086 }
1087 /* If RX_OVERRUN bit is set to 1 then SDCC2 tried to receive data from
1088 the card before empty storage for new received data was available.
1089 Verify that bit FLOW_ENA in MCI_CLK is set to 1 during the data xfer. */
1090 else if (mmc_status & MMC_BOOT_MCI_STAT_RX_OVRRUN) {
1091 /* Note: We've set FLOW_ENA bit in MCI_CLK to 1. so no need to verify
1092 for now */
1093 mmc_ret = MMC_BOOT_E_RX_OVRRUN;
1094 }
1095 /* If TX_UNDERRUN bit is set to 1 then SDCC2 tried to send data to
1096 the card before new data for sending was available. Verify that bit
1097 FLOW_ENA in MCI_CLK is set to 1 during the data xfer. */
1098 else if (mmc_status & MMC_BOOT_MCI_STAT_TX_UNDRUN) {
1099 /* Note: We've set FLOW_ENA bit in MCI_CLK to 1.so skipping it now */
1100 mmc_ret = MMC_BOOT_E_RX_OVRRUN;
1101 }
1102 return mmc_ret;
1103}
1104
1105/*
1106 * Send ext csd command.
1107 */
1108static unsigned int
Channagoud Kadabi398e1a22013-03-18 12:06:11 -07001109mmc_boot_send_ext_cmd(struct mmc_card *card, unsigned char *buf)
Ajay Dudanib01e5062011-12-03 23:23:42 -08001110{
1111 struct mmc_boot_command cmd;
1112 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
1113 unsigned int mmc_reg = 0;
1114 unsigned int *mmc_ptr = (unsigned int *)buf;
1115
1116 memset(buf, 0, 512);
1117
1118 /* basic check */
1119 if (card == NULL) {
1120 return MMC_BOOT_E_INVAL;
1121 }
1122
1123 /* set block len */
1124 if ((card->type != MMC_BOOT_TYPE_MMCHC)
1125 && (card->type != MMC_BOOT_TYPE_SDHC)) {
1126 mmc_ret = mmc_boot_set_block_len(card, 512);
1127 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1128 dprintf(CRITICAL,
1129 "Error No.%d: Failure setting block length for Card (RCA:%s)\n",
1130 mmc_ret, (char *)(card->rca));
1131 return mmc_ret;
1132 }
1133 }
1134
1135 /* Set the FLOW_ENA bit of MCI_CLK register to 1 */
1136 mmc_reg = readl(MMC_BOOT_MCI_CLK);
1137 mmc_reg |= MMC_BOOT_MCI_CLK_ENA_FLOW;
1138 writel(mmc_reg, MMC_BOOT_MCI_CLK);
1139
Channagoud Kadabi672c4c42012-12-20 17:51:45 -08001140 /* Wait for the MMC_BOOT_MCI_CLK write to go through. */
1141 mmc_mclk_reg_wr_delay();
1142
Ajay Dudanib01e5062011-12-03 23:23:42 -08001143 /* Write data timeout period to MCI_DATA_TIMER register. */
1144 /* Data timeout period should be in card bus clock periods */
1145 mmc_reg = 0xFFFFFFFF;
1146 writel(mmc_reg, MMC_BOOT_MCI_DATA_TIMER);
1147 writel(512, MMC_BOOT_MCI_DATA_LENGTH);
1148
1149 /* Set appropriate fields and write the MCI_DATA_CTL register. */
1150 /* Set ENABLE bit to 1 to enable the data transfer. */
1151 mmc_reg =
1152 MMC_BOOT_MCI_DATA_ENABLE | MMC_BOOT_MCI_DATA_DIR | (512 <<
1153 MMC_BOOT_MCI_BLKSIZE_POS);
1154
Deepa Dinamani07e66872012-06-29 18:32:05 -07001155#if MMC_BOOT_ADM || MMC_BOOT_BAM
Ajay Dudanib01e5062011-12-03 23:23:42 -08001156 mmc_reg |= MMC_BOOT_MCI_DATA_DM_ENABLE;
1157#endif
1158
1159 writel(mmc_reg, MMC_BOOT_MCI_DATA_CTL);
1160
Channagoud Kadabi672c4c42012-12-20 17:51:45 -08001161 /* Wait for the MMC_BOOT_MCI_DATA_CTL write to go through. */
1162 mmc_mclk_reg_wr_delay();
1163
Deepa Dinamani07e66872012-06-29 18:32:05 -07001164#if MMC_BOOT_BAM
1165 /* Setup SDCC BAM descriptors for Read operation. */
1166 mmc_ret = mmc_boot_bam_setup_desc(mmc_ptr, 512, MMC_BOOT_DATA_READ);
1167#endif
1168
Ajay Dudanib01e5062011-12-03 23:23:42 -08001169 memset((struct mmc_boot_command *)&cmd, 0,
1170 sizeof(struct mmc_boot_command));
1171 /* CMD8 */
1172 cmd.cmd_index = CMD8_SEND_EXT_CSD;
1173 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
1174 cmd.resp_type = MMC_BOOT_RESP_R1;
1175 cmd.xfer_mode = MMC_BOOT_XFER_MODE_BLOCK;
1176
1177 /* send command */
1178 mmc_ret = mmc_boot_send_command(&cmd);
1179 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1180 return mmc_ret;
1181 }
1182
1183 /* Read the transfer data from SDCC FIFO. */
Deepa Dinamani07e66872012-06-29 18:32:05 -07001184 mmc_ret = mmc_boot_data_transfer(mmc_ptr, 512, MMC_BOOT_DATA_READ);
1185
1186 /* Reset DPSM */
1187 writel(0, MMC_BOOT_MCI_DATA_CTL);
Ajay Dudanib01e5062011-12-03 23:23:42 -08001188
Channagoud Kadabi672c4c42012-12-20 17:51:45 -08001189 /* Wait for the MMC_BOOT_MCI_DATA_CTL write to go through. */
1190 mmc_mclk_reg_wr_delay();
1191
Ajay Dudanib01e5062011-12-03 23:23:42 -08001192 return mmc_ret;
1193}
1194
1195/*
1196 * Switch command
1197 */
1198static unsigned int
Channagoud Kadabi398e1a22013-03-18 12:06:11 -07001199mmc_boot_switch_cmd(struct mmc_card *card,
Ajay Dudanib01e5062011-12-03 23:23:42 -08001200 unsigned access, unsigned index, unsigned value)
1201{
1202
1203 struct mmc_boot_command cmd;
1204 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
Channagoud Kadabi672c4c42012-12-20 17:51:45 -08001205 uint32_t mmc_status;
Ajay Dudanib01e5062011-12-03 23:23:42 -08001206
1207 /* basic check */
1208 if (card == NULL) {
1209 return MMC_BOOT_E_INVAL;
1210 }
1211
1212 memset((struct mmc_boot_command *)&cmd, 0,
1213 sizeof(struct mmc_boot_command));
1214
1215 /* CMD6 Format:
1216 * [31:26] set to 0
1217 * [25:24] access
1218 * [23:16] index
1219 * [15:8] value
1220 * [7:3] set to 0
1221 * [2:0] cmd set
1222 */
1223 cmd.cmd_index = CMD6_SWITCH_FUNC;
1224 cmd.argument |= (access << 24);
1225 cmd.argument |= (index << 16);
1226 cmd.argument |= (value << 8);
1227 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
1228 cmd.resp_type = MMC_BOOT_RESP_R1B;
Channagoud Kadabi672c4c42012-12-20 17:51:45 -08001229 cmd.prg_enabled = 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -08001230
1231 mmc_ret = mmc_boot_send_command(&cmd);
1232 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
Channagoud Kadabi672c4c42012-12-20 17:51:45 -08001233 dprintf(CRITICAL,"Send cmd6 failed\n");
Ajay Dudanib01e5062011-12-03 23:23:42 -08001234 return mmc_ret;
1235 }
1236
Channagoud Kadabi672c4c42012-12-20 17:51:45 -08001237 /* Wait for interrupt or poll on PROG_DONE bit of MCI_STATUS register.
1238 * If PROG_DONE bit is set to 1 it means that the card finished it programming
1239 * and stopped driving DAT0 line to 0.
1240 */
1241 do {
1242 mmc_status = readl(MMC_BOOT_MCI_STATUS);
1243 if (mmc_status & MMC_BOOT_MCI_STAT_PROG_DONE) {
1244 break;
1245 }
1246 }
1247 while (1);
1248
1249 /* Check if the card completed the switch command processing */
1250 mmc_ret = mmc_boot_get_card_status(card, 0, &mmc_status);
1251 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1252 dprintf(CRITICAL, "Get card status failed\n");
1253 return mmc_ret;
1254 }
1255
1256 if (MMC_BOOT_CARD_STATUS(mmc_status) != MMC_BOOT_TRAN_STATE)
1257 {
1258 dprintf(CRITICAL, "Switch cmd failed. Card not in tran state\n");
1259 mmc_ret = MMC_BOOT_E_FAILURE;
1260 }
1261
1262 if (mmc_status & MMC_BOOT_SWITCH_FUNC_ERR_FLAG)
1263 {
1264 dprintf(CRITICAL, "Switch cmd failed. Switch Error.\n");
1265 mmc_ret = MMC_BOOT_E_FAILURE;
1266 }
1267
1268 return mmc_ret;
Ajay Dudanib01e5062011-12-03 23:23:42 -08001269}
1270
1271/*
1272 * A command to set the data bus width for card. Set width to either
1273 */
1274static unsigned int
Channagoud Kadabi398e1a22013-03-18 12:06:11 -07001275mmc_boot_set_bus_width(struct mmc_card *card, unsigned int width)
Ajay Dudanib01e5062011-12-03 23:23:42 -08001276{
1277 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
1278 unsigned int mmc_reg = 0;
1279 unsigned int mmc_width = 0;
Ajay Dudanib01e5062011-12-03 23:23:42 -08001280
1281 if (width != MMC_BOOT_BUS_WIDTH_1_BIT) {
1282 mmc_width = width - 1;
1283 }
1284
1285 mmc_ret = mmc_boot_switch_cmd(card, MMC_BOOT_ACCESS_WRITE,
1286 MMC_BOOT_EXT_CMMC_BUS_WIDTH, mmc_width);
1287
1288 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
Channagoud Kadabi672c4c42012-12-20 17:51:45 -08001289 dprintf(CRITICAL, "Switch cmd failed\n");
Ajay Dudanib01e5062011-12-03 23:23:42 -08001290 return mmc_ret;
1291 }
1292
Ajay Dudanib01e5062011-12-03 23:23:42 -08001293 /* set MCI_CLK accordingly */
1294 mmc_reg = readl(MMC_BOOT_MCI_CLK);
1295 mmc_reg &= ~MMC_BOOT_MCI_CLK_WIDEBUS_MODE;
1296 if (width == MMC_BOOT_BUS_WIDTH_1_BIT) {
1297 mmc_reg |= MMC_BOOT_MCI_CLK_WIDEBUS_1_BIT;
1298 } else if (width == MMC_BOOT_BUS_WIDTH_4_BIT) {
1299 mmc_reg |= MMC_BOOT_MCI_CLK_WIDEBUS_4_BIT;
1300 } else if (width == MMC_BOOT_BUS_WIDTH_8_BIT) {
1301 mmc_reg |= MMC_BOOT_MCI_CLK_WIDEBUS_8_BIT;
1302 }
Channagoud Kadabi676c2e32013-04-02 11:39:01 -07001303
Ajay Dudanib01e5062011-12-03 23:23:42 -08001304 writel(mmc_reg, MMC_BOOT_MCI_CLK);
1305
Channagoud Kadabi672c4c42012-12-20 17:51:45 -08001306 /* Wait for the MMC_BOOT_MCI_CLK write to go through. */
1307 mmc_mclk_reg_wr_delay();
Ajay Dudanib01e5062011-12-03 23:23:42 -08001308
1309 return MMC_BOOT_E_SUCCESS;
1310}
1311
1312/*
Channagoud Kadabi676c2e32013-04-02 11:39:01 -07001313 * Function to enable HS200 mode
1314 * 1. Set the clock frequency to 100 MHZ
1315 * 2. Set the bus width to 4/8 bit SDR as supported byt the target & host
1316 * 3. Set the HS_TIMING on ext_csd 185 for the card
1317 */
1318static uint32_t mmc_set_hs200_mode(struct mmc_host *host,
1319 struct mmc_card *card)
1320{
1321 uint32_t mmc_ret = MMC_BOOT_E_SUCCESS;
1322
1323 /* Set Clock @ 100 MHZ */
1324 clock_config_mmc(mmc_slot, host->caps.hs_clk_rate);
1325 host->mclk_rate = host->caps.hs_clk_rate;
1326
1327 /* Set 4/8 bit SDR bus width */
1328 mmc_ret = mmc_boot_set_bus_width(card, host->caps.bus_width);
1329 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1330 dprintf(CRITICAL,
1331 "Error No.%d: Failure to set wide bus for Card(RCA:%x)\n",
1332 mmc_ret, card->rca);
1333 return mmc_ret;
1334 }
1335
1336 /* Setting HS200 in HS_TIMING using EXT_CSD (CMD6) */
1337 mmc_ret = mmc_boot_switch_cmd(card, MMC_BOOT_ACCESS_WRITE,
1338 MMC_BOOT_EXT_CMMC_HS_TIMING, 2);
1339
1340 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1341 dprintf(CRITICAL, "Switch cmd returned failure %d\n", __LINE__);
1342 return mmc_ret;
1343 }
1344
1345 return mmc_ret;
1346}
1347
1348/*
1349 * Function to enable DDR mode
1350 * 1. Set the bus width to 8 bit DDR
1351 * 1. Set the clock frequency to 100 MHZ
1352 * 3. Set DDR mode in mci clk register
1353 * 4. Set Widebus enable in mci clock register
1354 */
1355static uint32_t mmc_set_ddr_mode(struct mmc_host *host,
1356 struct mmc_card *card)
1357{
1358 uint8_t mmc_ret = MMC_BOOT_E_SUCCESS;
1359 uint32_t mmc_reg = 0;
1360 uint32_t width = 0;
1361
1362 switch (host->caps.bus_width) {
1363 case MMC_BOOT_BUS_WIDTH_4_BIT:
1364 width = MMC_DDR_BUS_WIDTH_4_BIT;
1365 break;
1366 case MMC_BOOT_BUS_WIDTH_8_BIT:
1367 width = MMC_DDR_BUS_WIDTH_8_BIT;
1368 break;
1369 default:
1370 dprintf(CRITICAL, "Invalid bus width, DDR mode is not enabled\n");
1371 mmc_ret = MMC_BOOT_E_FAILURE;
1372 goto end;
1373 };
1374
1375 /* Set width for 4/8 bit DDR bus width */
1376 mmc_ret = mmc_boot_set_bus_width(card, width);
1377
1378 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1379 dprintf(CRITICAL,
1380 "Error No.%d: Failure to set wide bus for Card(RCA:%x)\n",
1381 mmc_ret, card->rca);
1382 return mmc_ret;
1383 }
1384
1385 /* Bump up the clock frequency */
1386 clock_config_mmc(mmc_slot, host->caps.hs_clk_rate);
1387 host->mclk_rate = host->caps.hs_clk_rate;
1388
1389 /* Select DDR mode in mci register */
1390 mmc_reg = readl(MMC_BOOT_MCI_CLK);
1391 mmc_reg |= (MMC_MCI_DDR_MODE_EN << MMC_MCI_MODE_SELECT);
1392
1393 writel(mmc_reg, MMC_BOOT_MCI_CLK);
1394
1395 /* Wait for the MMC_BOOT_MCI_CLK write to go through. */
1396 mmc_mclk_reg_wr_delay();
1397
1398 /* Enable wide bus for DDR */
1399 mmc_reg = readl(MMC_BOOT_MCI_CLK);
1400 mmc_reg |= MMC_BOOT_MCI_CLK_WIDEBUS_MODE;
1401 writel(mmc_reg, MMC_BOOT_MCI_CLK);
1402
1403 /* Wait for the MMC_BOOT_MCI_CLK write to go through. */
1404 mmc_mclk_reg_wr_delay();
1405
1406end:
1407 return MMC_BOOT_E_SUCCESS;
1408}
1409
1410/*
Ajay Dudanib01e5062011-12-03 23:23:42 -08001411 * A command to start data read from card. Either a single block or
1412 * multiple blocks can be read. Multiple blocks read will continuously
1413 * transfer data from card to host unless requested to stop by issuing
1414 * CMD12 - STOP_TRANSMISSION.
1415 */
1416static unsigned int
Channagoud Kadabi398e1a22013-03-18 12:06:11 -07001417mmc_boot_send_read_command(struct mmc_card *card,
Ajay Dudanib01e5062011-12-03 23:23:42 -08001418 unsigned int xfer_type, unsigned int data_addr)
1419{
1420 struct mmc_boot_command cmd;
1421 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
1422
1423 /* basic check */
1424 if (card == NULL) {
1425 return MMC_BOOT_E_INVAL;
1426 }
1427
1428 memset((struct mmc_boot_command *)&cmd, 0,
1429 sizeof(struct mmc_boot_command));
1430
1431 /* CMD17/18 Format:
1432 * [31:0] Data Address
1433 */
1434 if (xfer_type == MMC_BOOT_XFER_MULTI_BLOCK) {
1435 cmd.cmd_index = CMD18_READ_MULTIPLE_BLOCK;
1436 } else {
1437 cmd.cmd_index = CMD17_READ_SINGLE_BLOCK;
1438 }
1439
1440 cmd.argument = data_addr;
1441 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
1442 cmd.resp_type = MMC_BOOT_RESP_R1;
1443
1444 /* send command */
1445 mmc_ret = mmc_boot_send_command(&cmd);
1446 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1447 return mmc_ret;
1448 }
1449
1450 /* Response contains 32 bit Card status. Here we'll check
1451 BLOCK_LEN_ERROR and ADDRESS_ERROR */
1452 if (cmd.resp[0] & MMC_BOOT_R1_BLOCK_LEN_ERR) {
1453 return MMC_BOOT_E_BLOCKLEN_ERR;
1454 }
1455 /* Misaligned address not matching block length */
1456 if (cmd.resp[0] & MMC_BOOT_R1_ADDR_ERR) {
1457 return MMC_BOOT_E_ADDRESS_ERR;
1458 }
1459
1460 return MMC_BOOT_E_SUCCESS;
1461}
1462
1463/*
1464 * A command to start data write to card. Either a single block or
1465 * multiple blocks can be written. Multiple block write will continuously
1466 * transfer data from host to card unless requested to stop by issuing
1467 * CMD12 - STOP_TRANSMISSION.
1468 */
1469static unsigned int
Channagoud Kadabi398e1a22013-03-18 12:06:11 -07001470mmc_boot_send_write_command(struct mmc_card *card,
Ajay Dudanib01e5062011-12-03 23:23:42 -08001471 unsigned int xfer_type, unsigned int data_addr)
1472{
1473 struct mmc_boot_command cmd;
1474 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
1475
1476 /* basic check */
1477 if (card == NULL) {
1478 return MMC_BOOT_E_INVAL;
1479 }
1480
1481 memset((struct mmc_boot_command *)&cmd, 0,
1482 sizeof(struct mmc_boot_command));
1483
1484 /* CMD24/25 Format:
1485 * [31:0] Data Address
1486 */
1487 if (xfer_type == MMC_BOOT_XFER_MULTI_BLOCK) {
1488 cmd.cmd_index = CMD25_WRITE_MULTIPLE_BLOCK;
1489 } else {
1490 cmd.cmd_index = CMD24_WRITE_SINGLE_BLOCK;
1491 }
1492
1493 cmd.argument = data_addr;
1494 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
1495 cmd.resp_type = MMC_BOOT_RESP_R1;
1496
1497 /* send command */
1498 mmc_ret = mmc_boot_send_command(&cmd);
1499 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1500 return mmc_ret;
1501 }
1502
1503 /* Response contains 32 bit Card status. Here we'll check
1504 BLOCK_LEN_ERROR and ADDRESS_ERROR */
1505 if (cmd.resp[0] & MMC_BOOT_R1_BLOCK_LEN_ERR) {
1506 return MMC_BOOT_E_BLOCKLEN_ERR;
1507 }
1508 /* Misaligned address not matching block length */
1509 if (cmd.resp[0] & MMC_BOOT_R1_ADDR_ERR) {
1510 return MMC_BOOT_E_ADDRESS_ERR;
1511 }
1512
1513 return MMC_BOOT_E_SUCCESS;
1514}
1515
1516/*
1517 * Write data_len data to address specified by data_addr. data_len is
1518 * multiple of blocks for block data transfer.
1519 */
Channagoud Kadabi7cf5d042013-04-19 12:58:15 -07001520static unsigned int
Channagoud Kadabi398e1a22013-03-18 12:06:11 -07001521mmc_boot_write_to_card(struct mmc_host *host,
1522 struct mmc_card *card,
Ajay Dudanib01e5062011-12-03 23:23:42 -08001523 unsigned long long data_addr,
1524 unsigned int data_len, unsigned int *in)
1525{
1526 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
1527 unsigned int mmc_status = 0;
1528 unsigned int mmc_reg = 0;
1529 unsigned int addr;
1530 unsigned int xfer_type;
1531 unsigned int status;
1532
1533 if ((host == NULL) || (card == NULL)) {
1534 return MMC_BOOT_E_INVAL;
1535 }
1536
1537 /* Set block length. High Capacity MMC/SD card uses fixed 512 bytes block
1538 length. So no need to send CMD16. */
1539 if ((card->type != MMC_BOOT_TYPE_MMCHC)
1540 && (card->type != MMC_BOOT_TYPE_SDHC)) {
1541 mmc_ret = mmc_boot_set_block_len(card, card->wr_block_len);
1542 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1543 dprintf(CRITICAL, "Error No.%d: Failure setting block length for Card\
1544 (RCA:%s)\n", mmc_ret,
1545 (char *)(card->rca));
1546 return mmc_ret;
1547 }
1548 }
1549
1550 /* use multi-block mode to transfer for data larger than a block */
1551 xfer_type =
1552 (data_len >
1553 card->
1554 rd_block_len) ? MMC_BOOT_XFER_MULTI_BLOCK :
1555 MMC_BOOT_XFER_SINGLE_BLOCK;
1556
1557 /* For MMCHC/SDHC data address is specified in unit of 512B */
1558 addr = ((card->type != MMC_BOOT_TYPE_MMCHC)
1559 && (card->type !=
1560 MMC_BOOT_TYPE_SDHC)) ? (unsigned int)data_addr : (unsigned
1561 int)
1562 (data_addr / 512);
1563
1564 /* Set the FLOW_ENA bit of MCI_CLK register to 1 */
1565 mmc_reg = readl(MMC_BOOT_MCI_CLK);
1566 mmc_reg |= MMC_BOOT_MCI_CLK_ENA_FLOW;
1567 writel(mmc_reg, MMC_BOOT_MCI_CLK);
1568
Channagoud Kadabi672c4c42012-12-20 17:51:45 -08001569 /* Wait for the MMC_BOOT_MCI_CLK write to go through. */
1570 mmc_mclk_reg_wr_delay();
1571
Ajay Dudanib01e5062011-12-03 23:23:42 -08001572 /* Write data timeout period to MCI_DATA_TIMER register */
1573 /* Data timeout period should be in card bus clock periods */
1574 /*TODO: Fix timeout value */
1575 mmc_reg = 0xFFFFFFFF;
1576 writel(mmc_reg, MMC_BOOT_MCI_DATA_TIMER);
1577
1578 /* Write the total size of the transfer data to MCI_DATA_LENGTH register */
1579 writel(data_len, MMC_BOOT_MCI_DATA_LENGTH);
1580
Deepa Dinamani07e66872012-06-29 18:32:05 -07001581#if MMC_BOOT_BAM
1582 mmc_boot_bam_setup_desc(in, data_len, MMC_BOOT_DATA_WRITE);
1583#endif
1584
Ajay Dudanib01e5062011-12-03 23:23:42 -08001585 /* Send command to the card/device in order to start the write data xfer.
1586 The possible commands are CMD24/25/53/60/61 */
1587 mmc_ret = mmc_boot_send_write_command(card, xfer_type, addr);
1588 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1589 dprintf(CRITICAL, "Error No.%d: Failure sending write command to the\
1590 Card(RCA:%x)\n", mmc_ret,
1591 card->rca);
1592 return mmc_ret;
1593 }
1594
1595 /* Set appropriate fields and write the MCI_DATA_CTL register */
1596 /* Set ENABLE bit to 1 to enable the data transfer. */
1597 mmc_reg = 0;
1598 mmc_reg |= MMC_BOOT_MCI_DATA_ENABLE;
1599 /* Clear DIRECTION bit to 0 to enable transfer from host to card */
1600 /* Clear MODE bit to 0 to enable block oriented data transfer. For
1601 MMC cards only, if stream data transfer mode is desired, set
1602 MODE bit to 1. */
1603
1604 /* Set DM_ENABLE bit to 1 in order to enable DMA, otherwise set 0 */
1605
Deepa Dinamani07e66872012-06-29 18:32:05 -07001606#if MMC_BOOT_ADM || MMC_BOOT_BAM
Ajay Dudanib01e5062011-12-03 23:23:42 -08001607 mmc_reg |= MMC_BOOT_MCI_DATA_DM_ENABLE;
1608#endif
1609
1610 /* Write size of block to be used during the data transfer to
1611 BLOCKSIZE field */
1612 mmc_reg |= card->wr_block_len << MMC_BOOT_MCI_BLKSIZE_POS;
1613 writel(mmc_reg, MMC_BOOT_MCI_DATA_CTL);
1614
Channagoud Kadabi672c4c42012-12-20 17:51:45 -08001615 /* Wait for the MMC_BOOT_MCI_DATA_CTL write to go through. */
1616 mmc_mclk_reg_wr_delay();
1617
Ajay Dudanib01e5062011-12-03 23:23:42 -08001618 /* write data to FIFO */
1619 mmc_ret =
Deepa Dinamani07e66872012-06-29 18:32:05 -07001620 mmc_boot_data_transfer(in, data_len, MMC_BOOT_DATA_WRITE);
Ajay Dudanib01e5062011-12-03 23:23:42 -08001621
1622 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1623 dprintf(CRITICAL, "Error No.%d: Failure on data transfer from the \
1624 Card(RCA:%x)\n", mmc_ret,
1625 card->rca);
1626 /* In case of any failure happening for multi block transfer */
1627 if (xfer_type == MMC_BOOT_XFER_MULTI_BLOCK)
1628 mmc_boot_send_stop_transmission(card, 1);
1629 return mmc_ret;
1630 }
1631
1632 /* Send command to the card/device in order to poll the de-assertion of
1633 card/device BUSY condition. It is important to set PROG_ENA bit in
1634 MCI_CLK register before sending the command. Possible commands are
1635 CMD12/13. */
1636 if (xfer_type == MMC_BOOT_XFER_MULTI_BLOCK) {
1637 mmc_ret = mmc_boot_send_stop_transmission(card, 1);
1638 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1639 dprintf(CRITICAL, "Error No.%d: Failure sending Stop Transmission \
1640 command to the Card(RCA:%x)\n", mmc_ret,
1641 card->rca);
1642 return mmc_ret;
1643 }
1644 } else {
1645 mmc_ret = mmc_boot_get_card_status(card, 1, &status);
1646 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1647 dprintf(CRITICAL,
1648 "Error No.%d: Failure getting card status of Card(RCA:%x)\n",
1649 mmc_ret, card->rca);
1650 return mmc_ret;
1651 }
1652 }
1653
1654 /* Wait for interrupt or poll on PROG_DONE bit of MCI_STATUS register. If
1655 PROG_DONE bit is set to 1 it means that the card finished it programming
1656 and stopped driving DAT0 line to 0 */
1657 do {
1658 mmc_status = readl(MMC_BOOT_MCI_STATUS);
1659 if (mmc_status & MMC_BOOT_MCI_STAT_PROG_DONE) {
1660 break;
1661 }
1662 }
1663 while (1);
1664
Deepa Dinamani07e66872012-06-29 18:32:05 -07001665#if MMC_BOOT_BAM
1666 /* Wait for DML trasaction to end */
1667 mmc_boot_dml_wait_consumer_idle();
1668#endif
1669
1670 /* Reset DPSM */
1671 writel(0, MMC_BOOT_MCI_DATA_CTL);
1672
Channagoud Kadabi672c4c42012-12-20 17:51:45 -08001673 /* Wait for the MMC_BOOT_MCI_DATA_CTL write to go through. */
1674 mmc_mclk_reg_wr_delay();
1675
Ajay Dudanib01e5062011-12-03 23:23:42 -08001676 return MMC_BOOT_E_SUCCESS;
1677}
1678
1679/*
1680 * Adjust the interface speed to optimal speed
1681 */
1682static unsigned int
Channagoud Kadabi398e1a22013-03-18 12:06:11 -07001683mmc_boot_adjust_interface_speed(struct mmc_host *host,
1684 struct mmc_card *card)
Ajay Dudanib01e5062011-12-03 23:23:42 -08001685{
1686 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
Ajay Dudanib01e5062011-12-03 23:23:42 -08001687
1688 /* Setting HS_TIMING in EXT_CSD (CMD6) */
1689 mmc_ret = mmc_boot_switch_cmd(card, MMC_BOOT_ACCESS_WRITE,
1690 MMC_BOOT_EXT_CMMC_HS_TIMING, 1);
1691
1692 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
Channagoud Kadabi672c4c42012-12-20 17:51:45 -08001693 dprintf(CRITICAL, "Switch cmd returned failure %d\n", __LINE__);
Ajay Dudanib01e5062011-12-03 23:23:42 -08001694 return mmc_ret;
1695 }
1696
Ajay Dudanib01e5062011-12-03 23:23:42 -08001697 clock_config_mmc(mmc_slot, MMC_CLK_50MHZ);
1698
1699 host->mclk_rate = MMC_CLK_50MHZ;
1700
1701 return MMC_BOOT_E_SUCCESS;
1702}
1703
1704static unsigned int
Channagoud Kadabi398e1a22013-03-18 12:06:11 -07001705mmc_boot_set_block_count(struct mmc_card *card, unsigned int block_count)
Ajay Dudanib01e5062011-12-03 23:23:42 -08001706{
1707 struct mmc_boot_command cmd;
1708 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
1709
1710 /* basic check */
1711 if (card == NULL) {
1712 return MMC_BOOT_E_INVAL;
1713 }
1714
1715 memset((struct mmc_boot_command *)&cmd, 0,
1716 sizeof(struct mmc_boot_command));
1717
1718 /* CMD23 Format:
1719 * [15:0] number of blocks
1720 */
1721
1722 cmd.cmd_index = CMD23_SET_BLOCK_COUNT;
1723 cmd.argument = block_count;
1724 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
1725 cmd.resp_type = MMC_BOOT_RESP_R1;
1726
1727 /* send command */
1728 mmc_ret = mmc_boot_send_command(&cmd);
1729 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1730 return mmc_ret;
1731 }
1732
1733 if (cmd.resp[0] & MMC_BOOT_R1_OUT_OF_RANGE) {
1734 return MMC_BOOT_E_BLOCKLEN_ERR;
1735 }
1736
1737 return MMC_BOOT_E_SUCCESS;
1738}
1739
1740/*
1741 * Reads a data of data_len from the address specified. data_len
1742 * should be multiple of block size for block data transfer.
1743 */
Channagoud Kadabi7cf5d042013-04-19 12:58:15 -07001744static unsigned int
Channagoud Kadabi398e1a22013-03-18 12:06:11 -07001745mmc_boot_read_from_card(struct mmc_host *host,
1746 struct mmc_card *card,
Ajay Dudanib01e5062011-12-03 23:23:42 -08001747 unsigned long long data_addr,
1748 unsigned int data_len, unsigned int *out)
1749{
1750 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
1751 unsigned int mmc_reg = 0;
1752 unsigned int xfer_type;
1753 unsigned int addr = 0;
1754 unsigned char open_ended_read = 1;
1755
1756 if ((host == NULL) || (card == NULL)) {
1757 return MMC_BOOT_E_INVAL;
1758 }
1759
1760 /* Set block length. High Capacity MMC/SD card uses fixed 512 bytes block
1761 length. So no need to send CMD16. */
1762 if ((card->type != MMC_BOOT_TYPE_MMCHC)
1763 && (card->type != MMC_BOOT_TYPE_SDHC)) {
1764 mmc_ret = mmc_boot_set_block_len(card, card->rd_block_len);
1765 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1766 dprintf(CRITICAL,
1767 "Error No.%d: Failure setting block length for Card (RCA:%s)\n",
1768 mmc_ret, (char *)(card->rca));
1769 return mmc_ret;
1770 }
1771 }
1772
1773 /* use multi-block mode to transfer for data larger than a block */
1774 xfer_type =
1775 (data_len >
1776 card->
1777 rd_block_len) ? MMC_BOOT_XFER_MULTI_BLOCK :
1778 MMC_BOOT_XFER_SINGLE_BLOCK;
1779
1780 if (xfer_type == MMC_BOOT_XFER_MULTI_BLOCK) {
1781 if ((card->type == MMC_BOOT_TYPE_MMCHC)
1782 || (card->type == MMC_BOOT_TYPE_STD_MMC)) {
1783 /* Virtio model does not support open-ended multi-block reads.
1784 * So, block count must be set before sending read command.
1785 * All SD cards do not support this command. Restrict this to MMC.
1786 */
1787 mmc_ret =
1788 mmc_boot_set_block_count(card,
1789 data_len /
1790 (card->rd_block_len));
1791 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1792 dprintf(CRITICAL,
1793 "Error No.%d: Failure setting read block count for Card (RCA:%s)\n",
1794 mmc_ret, (char *)(card->rca));
1795 return mmc_ret;
1796 }
1797
1798 open_ended_read = 0;
1799 }
1800 }
1801
1802 /* Set the FLOW_ENA bit of MCI_CLK register to 1 */
1803 /* Note: It's already enabled */
1804
1805 /* If Data Mover is used for data transfer then prepare Command
1806 List Entry and enable the Data mover to work with SDCC2 */
1807
1808 /* Write data timeout period to MCI_DATA_TIMER register. */
1809 /* Data timeout period should be in card bus clock periods */
1810 mmc_reg = (unsigned long)(card->rd_timeout_ns / 1000000) *
1811 (host->mclk_rate / 1000);
1812 mmc_reg += 1000; // add some extra clock cycles to be safe
1813 mmc_reg = mmc_reg / 2;
1814 writel(mmc_reg, MMC_BOOT_MCI_DATA_TIMER);
1815
1816 /* Write the total size of the transfer data to MCI_DATA_LENGTH
1817 register. For block xfer it must be multiple of the block
1818 size. */
1819 writel(data_len, MMC_BOOT_MCI_DATA_LENGTH);
1820
1821 /* For MMCHC/SDHC data address is specified in unit of 512B */
1822 addr = ((card->type != MMC_BOOT_TYPE_MMCHC)
1823 && (card->type !=
1824 MMC_BOOT_TYPE_SDHC)) ? (unsigned int)data_addr : (unsigned
1825 int)
1826 (data_addr / 512);
1827
1828 /* Set appropriate fields and write the MCI_DATA_CTL register. */
1829 /* Set ENABLE bit to 1 to enable the data transfer. */
1830 mmc_reg = 0;
1831 mmc_reg |= MMC_BOOT_MCI_DATA_ENABLE;
1832 /* Clear DIRECTION bit to 1 to enable transfer from card to host */
1833 mmc_reg |= MMC_BOOT_MCI_DATA_DIR;
1834 /* Clear MODE bit to 0 to enable block oriented data transfer. For
1835 MMC cards only, if stream data transfer mode is desired, set
1836 MODE bit to 1. */
1837
1838 /* If DMA is to be used, Set DM_ENABLE bit to 1 */
1839
Deepa Dinamani07e66872012-06-29 18:32:05 -07001840#if MMC_BOOT_ADM || MMC_BOOT_BAM
Ajay Dudanib01e5062011-12-03 23:23:42 -08001841 mmc_reg |= MMC_BOOT_MCI_DATA_DM_ENABLE;
1842#endif
1843
1844 /* Write size of block to be used during the data transfer to
1845 BLOCKSIZE field */
1846 mmc_reg |= (card->rd_block_len << MMC_BOOT_MCI_BLKSIZE_POS);
1847 writel(mmc_reg, MMC_BOOT_MCI_DATA_CTL);
1848
Channagoud Kadabi672c4c42012-12-20 17:51:45 -08001849 /* Wait for the MMC_BOOT_MCI_DATA_CTL write to go through. */
1850 mmc_mclk_reg_wr_delay();
1851
Deepa Dinamani07e66872012-06-29 18:32:05 -07001852#if MMC_BOOT_BAM
1853 /* Setup SDCC FIFO descriptors for Read operation. */
1854 mmc_ret = mmc_boot_bam_setup_desc(out, data_len, MMC_BOOT_DATA_READ);
1855#endif
Ajay Dudanib01e5062011-12-03 23:23:42 -08001856 /* Send command to the card/device in order to start the read data
1857 transfer. Possible commands: CMD17/18/53/60/61. */
1858 mmc_ret = mmc_boot_send_read_command(card, xfer_type, addr);
1859 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1860 dprintf(CRITICAL,
1861 "Error No.%d: Failure sending read command to the Card(RCA:%x)\n",
1862 mmc_ret, card->rca);
1863 return mmc_ret;
1864 }
1865
1866 /* Read the transfer data from SDCC FIFO. */
Deepa Dinamani07e66872012-06-29 18:32:05 -07001867 mmc_ret = mmc_boot_data_transfer(out, data_len, MMC_BOOT_DATA_READ);
Ajay Dudanib01e5062011-12-03 23:23:42 -08001868
1869 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1870 dprintf(CRITICAL, "Error No.%d: Failure on data transfer from the \
1871 Card(RCA:%x)\n", mmc_ret,
1872 card->rca);
1873 return mmc_ret;
1874 }
1875
1876 /* In case a multiple block transfer was performed, send CMD12 to the
1877 card/device in order to indicate the end of read data transfer */
1878 if ((xfer_type == MMC_BOOT_XFER_MULTI_BLOCK) && open_ended_read) {
1879 mmc_ret = mmc_boot_send_stop_transmission(card, 0);
1880 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
1881 dprintf(CRITICAL, "Error No.%d: Failure sending Stop Transmission \
1882 command to the Card(RCA:%x)\n", mmc_ret,
1883 card->rca);
1884 return mmc_ret;
1885 }
1886 }
1887
Deepa Dinamani07e66872012-06-29 18:32:05 -07001888 /* Reset DPSM */
1889 writel(0, MMC_BOOT_MCI_DATA_CTL);
1890
Channagoud Kadabi672c4c42012-12-20 17:51:45 -08001891 /* Wait for the MMC_BOOT_MCI_DATA_CTL write to go through. */
1892 mmc_mclk_reg_wr_delay();
1893
Ajay Dudanib01e5062011-12-03 23:23:42 -08001894 return MMC_BOOT_E_SUCCESS;
1895}
1896
1897/*
1898 * Initialize host structure, set and enable clock-rate and power mode.
1899 */
Channagoud Kadabi398e1a22013-03-18 12:06:11 -07001900unsigned int mmc_boot_init(struct mmc_host *host)
Ajay Dudanib01e5062011-12-03 23:23:42 -08001901{
1902 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
1903 unsigned int mmc_pwr = 0;
1904
1905 host->ocr = MMC_BOOT_OCR_27_36 | MMC_BOOT_OCR_SEC_MODE;
1906 host->cmd_retry = MMC_BOOT_MAX_COMMAND_RETRY;
1907
Channagoud Kadabie6f59dc2014-01-06 17:31:39 -08001908 /* Disable sdhc mode */
1909 RMWREG32(MMC_BOOT_MCI_HC_MODE, SDHCI_HC_START_BIT, SDHCI_HC_WIDTH, 0);
1910
1911 /* Wait for the MMC_BOOT_MCI_POWER write to go through. */
1912 mmc_mclk_reg_wr_delay();
1913
1914 /* Reset controller */
1915 RMWREG32(MMC_BOOT_MCI_POWER, CORE_SW_RST_START, CORE_SW_RST_WIDTH, 1);
1916
1917 /* Wait for the MMC_BOOT_MCI_POWER write to go through. */
1918 mmc_mclk_reg_wr_delay();
1919
Ajay Dudanib01e5062011-12-03 23:23:42 -08001920 /* Initialize any clocks needed for SDC controller */
1921 clock_init_mmc(mmc_slot);
1922
Channagoud Kadabi672c4c42012-12-20 17:51:45 -08001923 /* Save the verison on the mmc controller. */
1924 host->mmc_cont_version = readl(MMC_BOOT_MCI_VERSION);
1925
Ajay Dudanib01e5062011-12-03 23:23:42 -08001926 /* Setup initial freq to 400KHz */
1927 clock_config_mmc(mmc_slot, MMC_CLK_400KHZ);
1928
1929 host->mclk_rate = MMC_CLK_400KHZ;
1930
1931 /* set power mode */
1932 /* give some time to reach minimum voltate */
Channagoud Kadabi672c4c42012-12-20 17:51:45 -08001933
Ajay Dudanib01e5062011-12-03 23:23:42 -08001934 mmc_pwr &= ~MMC_BOOT_MCI_PWR_UP;
1935 mmc_pwr |= MMC_BOOT_MCI_PWR_ON;
1936 mmc_pwr |= MMC_BOOT_MCI_PWR_UP;
1937 writel(mmc_pwr, MMC_BOOT_MCI_POWER);
Channagoud Kadabi672c4c42012-12-20 17:51:45 -08001938
1939 /* Wait for the MMC_BOOT_MCI_POWER write to go through. */
1940 mmc_mclk_reg_wr_delay();
Ajay Dudanib01e5062011-12-03 23:23:42 -08001941
1942 return MMC_BOOT_E_SUCCESS;
1943}
1944
1945/*
1946 * Performs card identification process:
1947 * - get card's unique identification number (CID)
1948 * - get(for sd)/set (for mmc) relative card address (RCA)
1949 * - get CSD
1950 * - select the card, thus transitioning it to Transfer State
1951 * - get Extended CSD (for mmc)
1952 */
1953static unsigned int
Channagoud Kadabi398e1a22013-03-18 12:06:11 -07001954mmc_boot_identify_card(struct mmc_host *host, struct mmc_card *card)
Ajay Dudanib01e5062011-12-03 23:23:42 -08001955{
1956 unsigned int mmc_return = MMC_BOOT_E_SUCCESS;
1957 unsigned int raw_csd[4];
1958
1959 /* basic check */
1960 if ((host == NULL) || (card == NULL)) {
1961 return MMC_BOOT_E_INVAL;
1962 }
1963
1964 /* Ask card to send its unique card identification (CID) number (CMD2) */
1965 mmc_return = mmc_boot_all_send_cid(card);
1966 if (mmc_return != MMC_BOOT_E_SUCCESS) {
1967 dprintf(CRITICAL,
1968 "Error No. %d: Failure getting card's CID number!\n",
1969 mmc_return);
1970 return mmc_return;
1971 }
1972
1973 /* Ask card to send a relative card address (RCA) (CMD3) */
1974 mmc_return = mmc_boot_send_relative_address(card);
1975 if (mmc_return != MMC_BOOT_E_SUCCESS) {
1976 dprintf(CRITICAL, "Error No. %d: Failure getting card's RCA!\n",
1977 mmc_return);
1978 return mmc_return;
1979 }
1980
1981 /* Get card's CSD register (CMD9) */
1982 mmc_return = mmc_boot_send_csd(card, raw_csd);
1983 if (mmc_return != MMC_BOOT_E_SUCCESS) {
1984 dprintf(CRITICAL,
1985 "Error No.%d: Failure getting card's CSD information!\n",
1986 mmc_return);
1987 return mmc_return;
1988 }
1989
1990 /* Select the card (CMD7) */
1991 mmc_return = mmc_boot_select_card(card, card->rca);
1992 if (mmc_return != MMC_BOOT_E_SUCCESS) {
1993 dprintf(CRITICAL,
1994 "Error No.%d: Failure selecting the Card with RCA: %x\n",
1995 mmc_return, card->rca);
1996 return mmc_return;
1997 }
1998
1999 /* Set the card status as active */
2000 card->status = MMC_BOOT_STATUS_ACTIVE;
2001
2002 if ((card->type == MMC_BOOT_TYPE_STD_MMC)
2003 || (card->type == MMC_BOOT_TYPE_MMCHC)) {
2004 /* For MMC cards, also get the extended csd */
2005 mmc_return = mmc_boot_send_ext_cmd(card, ext_csd_buf);
2006
2007 if (mmc_return != MMC_BOOT_E_SUCCESS) {
2008 dprintf(CRITICAL,
2009 "Error No.%d: Failure getting card's ExtCSD information!\n",
2010 mmc_return);
2011
2012 return mmc_return;
2013 }
2014
2015 }
2016
2017 /* Decode and save the CSD register */
2018 mmc_return = mmc_boot_decode_and_save_csd(card, raw_csd);
2019 if (mmc_return != MMC_BOOT_E_SUCCESS) {
2020 dprintf(CRITICAL,
2021 "Error No.%d: Failure decoding card's CSD information!\n",
2022 mmc_return);
2023 return mmc_return;
2024 }
2025
2026 /* Once CSD is received, set read and write timeout value now itself */
2027 mmc_return = mmc_boot_set_read_timeout(host, card);
2028 if (mmc_return != MMC_BOOT_E_SUCCESS) {
2029 dprintf(CRITICAL,
2030 "Error No.%d: Failure setting Read Timeout value!\n",
2031 mmc_return);
2032 return mmc_return;
2033 }
2034
2035 mmc_return = mmc_boot_set_write_timeout(host, card);
2036 if (mmc_return != MMC_BOOT_E_SUCCESS) {
2037 dprintf(CRITICAL,
2038 "Error No.%d: Failure setting Write Timeout value!\n",
2039 mmc_return);
2040 return mmc_return;
2041 }
2042
2043 return MMC_BOOT_E_SUCCESS;
2044}
2045
2046static unsigned int mmc_boot_send_app_cmd(unsigned int rca)
2047{
2048 struct mmc_boot_command cmd;
2049 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
2050
2051 memset((struct mmc_boot_command *)&cmd, 0,
2052 sizeof(struct mmc_boot_command));
2053
2054 cmd.cmd_index = CMD55_APP_CMD;
2055 cmd.argument = (rca << 16);
2056 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
2057 cmd.resp_type = MMC_BOOT_RESP_R1;
2058
2059 mmc_ret = mmc_boot_send_command(&cmd);
2060
2061 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
2062 return mmc_ret;
2063 }
2064
2065 return MMC_BOOT_E_SUCCESS;
2066}
2067
Channagoud Kadabi398e1a22013-03-18 12:06:11 -07002068static unsigned int mmc_boot_sd_init_card(struct mmc_card *card)
Ajay Dudanib01e5062011-12-03 23:23:42 -08002069{
2070 unsigned int i, mmc_ret;
2071 unsigned int ocr_cmd_arg;
2072 struct mmc_boot_command cmd;
2073
2074 memset((struct mmc_boot_command *)&cmd, 0,
2075 sizeof(struct mmc_boot_command));
2076
2077 /* Send CMD8 to set interface condition */
2078 for (i = 0; i < 3; i++) {
2079 cmd.cmd_index = CMD8_SEND_IF_COND;
2080 cmd.argument = MMC_BOOT_SD_HC_VOLT_SUPPLIED;
2081 cmd.cmd_type = MMC_BOOT_CMD_BCAST_W_RESP;
2082 cmd.resp_type = MMC_BOOT_RESP_R7;
2083
2084 mmc_ret = mmc_boot_send_command(&cmd);
2085 if (mmc_ret == MMC_BOOT_E_SUCCESS) {
2086 if (cmd.resp[0] != MMC_BOOT_SD_HC_VOLT_SUPPLIED)
2087 return MMC_BOOT_E_FAILURE;
2088 /* Set argument for ACMD41 */
2089 ocr_cmd_arg = MMC_BOOT_SD_NEG_OCR | MMC_BOOT_SD_HC_HCS;
2090 break;
2091 }
2092 mdelay(1);
2093 }
2094
2095 /* Send ACMD41 to set operating condition */
2096 /* Try for a max of 1 sec as per spec */
2097 for (i = 0; i < 20; i++) {
2098 mmc_ret = mmc_boot_send_app_cmd(0);
2099 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
2100 return mmc_ret;
2101 }
2102
2103 cmd.cmd_index = ACMD41_SEND_OP_COND;
2104 cmd.argument = ocr_cmd_arg;
2105 cmd.cmd_type = MMC_BOOT_CMD_BCAST_W_RESP;
2106 cmd.resp_type = MMC_BOOT_RESP_R3;
2107
2108 mmc_ret = mmc_boot_send_command(&cmd);
2109 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
2110 return mmc_ret;
2111 } else if (cmd.resp[0] & MMC_BOOT_SD_DEV_READY) {
2112 /* Check for HC */
2113 if (cmd.resp[0] & (1 << 30)) {
2114 card->type = MMC_BOOT_TYPE_SDHC;
2115 } else {
2116 card->type = MMC_BOOT_TYPE_STD_SD;
2117 }
2118 break;
2119 }
2120 mdelay(50);
2121 }
2122 return MMC_BOOT_E_SUCCESS;
2123}
2124
2125/*
2126 * Routine to initialize MMC card. It resets a card to idle state, verify operating
2127 * voltage and set the card inready state.
2128 */
2129static unsigned int
Channagoud Kadabi398e1a22013-03-18 12:06:11 -07002130mmc_boot_init_card(struct mmc_host *host, struct mmc_card *card)
Ajay Dudanib01e5062011-12-03 23:23:42 -08002131{
2132 unsigned int mmc_retry = 0;
2133 unsigned int mmc_return = MMC_BOOT_E_SUCCESS;
2134
2135 /* basic check */
2136 if ((host == NULL) || (card == NULL)) {
2137 return MMC_BOOT_E_INVAL;
2138 }
2139
2140 /* 1. Card Reset - CMD0 */
2141 mmc_return = mmc_boot_reset_cards();
2142 if (mmc_return != MMC_BOOT_E_SUCCESS) {
2143 dprintf(CRITICAL,
2144 "Error No.:%d: Failure resetting MMC cards!\n",
2145 mmc_return);
2146 return mmc_return;
2147 }
2148
2149 /* 2. Card Initialization process */
2150
2151 /* Send CMD1 to identify and reject cards that do not match host's VDD range
2152 profile. Cards sends its OCR register in response.
2153 */
2154 mmc_retry = 0;
2155 do {
2156 mmc_return = mmc_boot_send_op_cond(host, card);
2157 /* Card returns busy status. We'll retry again! */
2158 if (mmc_return == MMC_BOOT_E_CARD_BUSY) {
2159 mmc_retry++;
2160 mdelay(1);
2161 continue;
2162 } else if (mmc_return == MMC_BOOT_E_SUCCESS) {
2163 break;
2164 } else {
2165 dprintf(CRITICAL,
2166 "Error No. %d: Failure Initializing MMC Card!\n",
2167 mmc_return);
2168
2169 /* Check for sD card */
2170 mmc_return = mmc_boot_sd_init_card(card);
2171 return mmc_return;
2172 }
2173 }
2174 while (mmc_retry < host->cmd_retry);
2175
2176 /* If card still returned busy status we are out of luck.
2177 * Card cannot be initialized */
2178 if (mmc_return == MMC_BOOT_E_CARD_BUSY) {
2179 dprintf(CRITICAL, "Error No. %d: Card has busy status set. \
2180 Initialization not completed\n", mmc_return);
2181 return MMC_BOOT_E_CARD_BUSY;
2182 }
Channagoud Kadabi96c629e2013-09-10 14:21:30 -07002183
2184 card->block_size = BLOCK_SIZE;
2185
Ajay Dudanib01e5062011-12-03 23:23:42 -08002186 return MMC_BOOT_E_SUCCESS;
2187}
2188
2189static unsigned int
Channagoud Kadabi398e1a22013-03-18 12:06:11 -07002190mmc_boot_set_sd_bus_width(struct mmc_card *card, unsigned int width)
Ajay Dudanib01e5062011-12-03 23:23:42 -08002191{
2192 struct mmc_boot_command cmd;
2193 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
2194 unsigned int sd_reg;
2195
2196 mmc_ret = mmc_boot_send_app_cmd(card->rca);
2197
2198 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
2199 return mmc_ret;
2200 }
2201
2202 memset((struct mmc_boot_command *)&cmd, 0,
2203 sizeof(struct mmc_boot_command));
2204
2205 /* Send ACMD6 to set bus width */
2206 cmd.cmd_index = ACMD6_SET_BUS_WIDTH;
2207 /* 10 => 4 bit wide */
2208 if (width == MMC_BOOT_BUS_WIDTH_1_BIT) {
2209 cmd.argument = 0;
2210 } else if (width == MMC_BOOT_BUS_WIDTH_4_BIT) {
2211 cmd.argument = (1 << 1);
2212 }
2213 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
2214 cmd.resp_type = MMC_BOOT_RESP_R1;
2215
2216 mmc_ret = mmc_boot_send_command(&cmd);
2217
2218 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
2219 return mmc_ret;
2220 }
2221
2222 /* set MCI_CLK accordingly */
2223 sd_reg = readl(MMC_BOOT_MCI_CLK);
2224 sd_reg &= ~MMC_BOOT_MCI_CLK_WIDEBUS_MODE;
2225 if (width == MMC_BOOT_BUS_WIDTH_1_BIT) {
2226 sd_reg |= MMC_BOOT_MCI_CLK_WIDEBUS_1_BIT;
2227 } else if (width == MMC_BOOT_BUS_WIDTH_4_BIT) {
2228 sd_reg |= MMC_BOOT_MCI_CLK_WIDEBUS_4_BIT;
2229 } else if (width == MMC_BOOT_BUS_WIDTH_8_BIT) {
2230 sd_reg |= MMC_BOOT_MCI_CLK_WIDEBUS_8_BIT;
2231 }
2232 writel(sd_reg, MMC_BOOT_MCI_CLK);
2233
Channagoud Kadabi672c4c42012-12-20 17:51:45 -08002234 /* Wait for the MMC_BOOT_MCI_CLK write to go through. */
2235 mmc_mclk_reg_wr_delay();
Ajay Dudanib01e5062011-12-03 23:23:42 -08002236
2237 return MMC_BOOT_E_SUCCESS;
2238}
2239
2240static unsigned int
Channagoud Kadabi398e1a22013-03-18 12:06:11 -07002241mmc_boot_set_sd_hs(struct mmc_host *host, struct mmc_card *card)
Ajay Dudanib01e5062011-12-03 23:23:42 -08002242{
2243 unsigned char sw_buf[64];
2244 unsigned int mmc_ret;
2245
2246 /* CMD6 is a data transfer command. sD card returns 512 bits of data */
2247 /* Refer 4.3.10 of sD card specification 3.0 */
2248 mmc_ret =
2249 mmc_boot_read_reg(card, 64, CMD6_SWITCH_FUNC, MMC_BOOT_SD_SWITCH_HS,
2250 (unsigned int *)&sw_buf);
2251
2252 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
2253 return mmc_ret;
2254 }
2255
Ajay Dudanib01e5062011-12-03 23:23:42 -08002256 clock_config_mmc(mmc_slot, MMC_CLK_50MHZ);
2257
2258 host->mclk_rate = MMC_CLK_50MHZ;
2259
2260 return MMC_BOOT_E_SUCCESS;
2261}
2262
2263/*
2264 * Performs initialization and identification of all the MMC cards connected
2265 * to the host.
2266 */
2267
2268static unsigned int
Channagoud Kadabi398e1a22013-03-18 12:06:11 -07002269mmc_boot_init_and_identify_cards(struct mmc_host *host,
2270 struct mmc_card *card)
Ajay Dudanib01e5062011-12-03 23:23:42 -08002271{
2272 unsigned int mmc_return = MMC_BOOT_E_SUCCESS;
2273 unsigned int status;
Channagoud Kadabi7d84dd62012-08-24 21:20:56 +05302274 uint8_t mmc_bus_width = 0;
Ajay Dudanib01e5062011-12-03 23:23:42 -08002275
2276 /* Basic check */
2277 if (host == NULL) {
2278 return MMC_BOOT_E_INVAL;
2279 }
2280
2281 /* Initialize MMC card structure */
2282 card->status = MMC_BOOT_STATUS_INACTIVE;
2283 card->rd_block_len = MMC_BOOT_RD_BLOCK_LEN;
2284 card->wr_block_len = MMC_BOOT_WR_BLOCK_LEN;
2285
2286 /* Start initialization process (CMD0 & CMD1) */
2287 mmc_return = mmc_boot_init_card(host, card);
2288 if (mmc_return != MMC_BOOT_E_SUCCESS) {
2289 return mmc_return;
2290 }
2291
2292 /* Identify (CMD2, CMD3 & CMD9) and select the card (CMD7) */
2293 mmc_return = mmc_boot_identify_card(host, card);
2294 if (mmc_return != MMC_BOOT_E_SUCCESS) {
2295 return mmc_return;
2296 }
2297
2298 if (card->type == MMC_BOOT_TYPE_SDHC
2299 || card->type == MMC_BOOT_TYPE_STD_SD) {
2300 /* Setting sD card to high speed without checking card's capability.
2301 Cards that do not support high speed may fail to boot */
2302 mmc_return = mmc_boot_set_sd_hs(host, card);
2303 if (mmc_return != MMC_BOOT_E_SUCCESS) {
2304 return mmc_return;
2305 }
2306
2307 mmc_return =
2308 mmc_boot_set_sd_bus_width(card, MMC_BOOT_BUS_WIDTH_4_BIT);
2309 if (mmc_return != MMC_BOOT_E_SUCCESS) {
2310 dprintf(CRITICAL,
2311 "Couldn't set 4bit mode for sD card\n");
2312 mmc_return =
2313 mmc_boot_set_sd_bus_width(card,
2314 MMC_BOOT_BUS_WIDTH_1_BIT);
2315 if (mmc_return != MMC_BOOT_E_SUCCESS) {
2316 dprintf(CRITICAL,
2317 "Error No.%d: Failed in setting bus width!\n",
2318 mmc_return);
2319 return mmc_return;
2320 }
2321 }
2322 } else {
2323 /* set interface speed */
2324 mmc_return = mmc_boot_adjust_interface_speed(host, card);
2325 if (mmc_return != MMC_BOOT_E_SUCCESS) {
2326 dprintf(CRITICAL,
2327 "Error No.%d: Error adjusting interface speed!\n",
2328 mmc_return);
2329 return mmc_return;
2330 }
Ajay Dudanib01e5062011-12-03 23:23:42 -08002331
Channagoud Kadabi676c2e32013-04-02 11:39:01 -07002332 /* Enable HS200 mode by default if supported,
2333 * else if DDR mode is supported enable it.
2334 * else use default 4/8 bit mode
2335 */
2336 if (card_supports_hs200_mode() && host->caps.hs200_mode) {
2337 mmc_return = mmc_set_hs200_mode(host, card);
2338 if (mmc_return != MMC_BOOT_E_SUCCESS) {
2339 dprintf(CRITICAL,
2340 "Error No.%d: Failure to set HS200 mode for Card(RCA:%x)\n",
2341 mmc_return, card->rca);
2342 return mmc_return;
2343 }
2344 } else if (card_supports_ddr_mode() && host->caps.ddr_mode) {
2345 mmc_return = mmc_set_ddr_mode(host, card);
2346 if (mmc_return != MMC_BOOT_E_SUCCESS) {
2347 dprintf(CRITICAL,
2348 "Error No.%d: Failure to set DDR mode for Card(RCA:%x)\n",
2349 mmc_return, card->rca);
2350 return mmc_return;
2351 }
2352 } else {
2353 mmc_return =
2354 mmc_boot_set_bus_width(card, host->caps.bus_width);
2355 if (mmc_return != MMC_BOOT_E_SUCCESS) {
2356 dprintf(CRITICAL,
2357 "Error No.%d: Failure to set wide bus for Card(RCA:%x)\n",
2358 mmc_return, card->rca);
2359 return mmc_return;
2360 }
2361 }
Channagoud Kadabi4f8fdb52013-05-30 13:10:41 -07002362 }
Channagoud Kadabi676c2e32013-04-02 11:39:01 -07002363
Ajay Dudanib01e5062011-12-03 23:23:42 -08002364 /* Just checking whether we're in TRAN state after changing speed and bus width */
2365 mmc_return = mmc_boot_get_card_status(card, 0, &status);
2366 if (mmc_return != MMC_BOOT_E_SUCCESS) {
2367 return mmc_return;
2368 }
2369
2370 if (MMC_BOOT_CARD_STATUS(status) != MMC_BOOT_TRAN_STATE)
2371 return MMC_BOOT_E_FAILURE;
2372
2373 return MMC_BOOT_E_SUCCESS;
2374}
2375
2376void mmc_display_ext_csd(void)
2377{
2378 dprintf(SPEW, "part_config: %x\n", ext_csd_buf[179]);
2379 dprintf(SPEW, "erase_group_def: %x\n", ext_csd_buf[175]);
2380 dprintf(SPEW, "user_wp: %x\n", ext_csd_buf[171]);
2381}
2382
2383void mmc_display_csd(void)
2384{
2385 dprintf(SPEW, "erase_grpsize: %d\n", mmc_card.csd.erase_grp_size);
2386 dprintf(SPEW, "erase_grpmult: %d\n", mmc_card.csd.erase_grp_mult);
2387 dprintf(SPEW, "wp_grpsize: %d\n", mmc_card.csd.wp_grp_size);
2388 dprintf(SPEW, "wp_grpen: %d\n", mmc_card.csd.wp_grp_enable);
2389 dprintf(SPEW, "perm_wp: %d\n", mmc_card.csd.perm_wp);
2390 dprintf(SPEW, "temp_wp: %d\n", mmc_card.csd.temp_wp);
2391}
2392
2393/*
2394 * Entry point to MMC boot process
2395 */
2396unsigned int mmc_boot_main(unsigned char slot, unsigned int base)
2397{
2398 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
2399
Channagoud Kadabi398e1a22013-03-18 12:06:11 -07002400 memset((struct mmc_host *)&mmc_host, 0,
2401 sizeof(struct mmc_host));
2402 memset((struct mmc_card *)&mmc_card, 0,
2403 sizeof(struct mmc_card));
Ajay Dudanib01e5062011-12-03 23:23:42 -08002404
2405 mmc_slot = slot;
2406 mmc_boot_mci_base = base;
2407
Channagoud Kadabi676c2e32013-04-02 11:39:01 -07002408 /* Get the capabilities for the host/target */
2409 target_mmc_caps(&mmc_host);
2410
Ajay Dudanib01e5062011-12-03 23:23:42 -08002411 /* Initialize necessary data structure and enable/set clock and power */
2412 dprintf(SPEW, " Initializing MMC host data structure and clock!\n");
2413 mmc_ret = mmc_boot_init(&mmc_host);
2414 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
2415 dprintf(CRITICAL, "MMC Boot: Error Initializing MMC Card!!!\n");
2416 return MMC_BOOT_E_FAILURE;
2417 }
2418
Deepa Dinamani07e66872012-06-29 18:32:05 -07002419#if MMC_BOOT_BAM
2420
2421 mmc_ret = mmc_bam_init(mmc_sdc_bam_base[slot - 1]);
2422 dml_base = mmc_sdc_dml_base[slot - 1];
2423#endif
2424
Ajay Dudanib01e5062011-12-03 23:23:42 -08002425 /* Initialize and identify cards connected to host */
2426 mmc_ret = mmc_boot_init_and_identify_cards(&mmc_host, &mmc_card);
2427 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
2428 dprintf(CRITICAL,
2429 "MMC Boot: Failed detecting MMC/SDC @ slot%d\n", slot);
2430 return MMC_BOOT_E_FAILURE;
2431 }
2432
2433 mmc_display_csd();
2434 mmc_display_ext_csd();
2435
Channagoud Kadabi7cf5d042013-04-19 12:58:15 -07002436 mmc_ret = partition_read_table();
Ajay Dudanib01e5062011-12-03 23:23:42 -08002437 return mmc_ret;
2438}
2439
2440/*
2441 * MMC write function
2442 */
2443unsigned int
2444mmc_write(unsigned long long data_addr, unsigned int data_len, unsigned int *in)
2445{
2446 int val = 0;
2447 unsigned int write_size = ((unsigned)(0xFFFFFF / 512)) * 512;
2448 unsigned offset = 0;
2449 unsigned int *sptr = in;
2450
2451 if (data_len % 512)
2452 data_len = ROUND_TO_PAGE(data_len, 511);
2453
2454 while (data_len > write_size) {
2455 val = mmc_boot_write_to_card(&mmc_host, &mmc_card,
2456 data_addr + offset, write_size,
2457 sptr);
2458 if (val) {
2459 return val;
2460 }
2461
2462 sptr += (write_size / sizeof(unsigned));
2463 offset += write_size;
2464 data_len -= write_size;
2465 }
2466 if (data_len) {
2467 val = mmc_boot_write_to_card(&mmc_host, &mmc_card,
2468 data_addr + offset, data_len,
2469 sptr);
2470 }
2471 return val;
2472}
2473
2474/*
2475 * MMC read function
2476 */
2477
2478unsigned int
2479mmc_read(unsigned long long data_addr, unsigned int *out, unsigned int data_len)
2480{
2481 int val = 0;
Niranjana Vishwanathapuraf32e2a42014-02-14 10:29:46 -05002482 unsigned int data_limit = mmc_card.rd_block_len * 0xffff;
2483 unsigned int this_len;
2484
2485 do {
2486 this_len = (data_len > data_limit) ? data_limit : data_len;
2487
2488 val =
2489 mmc_boot_read_from_card(&mmc_host, &mmc_card, data_addr,
2490 this_len, out);
2491
2492 if (val != MMC_BOOT_E_SUCCESS)
2493 return val;
2494
2495 data_len -= this_len;
2496 data_addr += this_len;
2497 out += (this_len / sizeof(*out));
2498 } while (data_len > 0);
2499
Ajay Dudanib01e5062011-12-03 23:23:42 -08002500 return val;
2501}
2502
2503/*
2504 * Function to read registers from MMC or SD card
2505 */
2506static unsigned int
Channagoud Kadabi398e1a22013-03-18 12:06:11 -07002507mmc_boot_read_reg(struct mmc_card *card,
Ajay Dudanib01e5062011-12-03 23:23:42 -08002508 unsigned int data_len,
2509 unsigned int command, unsigned int addr, unsigned int *out)
2510{
2511 struct mmc_boot_command cmd;
2512 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
2513 unsigned int mmc_reg = 0;
2514
2515 /* Set the FLOW_ENA bit of MCI_CLK register to 1 */
2516 mmc_reg = readl(MMC_BOOT_MCI_CLK);
2517 mmc_reg |= MMC_BOOT_MCI_CLK_ENA_FLOW;
2518 writel(mmc_reg, MMC_BOOT_MCI_CLK);
2519
Channagoud Kadabi672c4c42012-12-20 17:51:45 -08002520 /* Wait for the MMC_BOOT_MCI_CLK write to go through. */
2521 mmc_mclk_reg_wr_delay();
2522
Ajay Dudanib01e5062011-12-03 23:23:42 -08002523 /* Write data timeout period to MCI_DATA_TIMER register. */
2524 /* Data timeout period should be in card bus clock periods */
2525 mmc_reg = 0xFFFFFFFF;
2526 writel(mmc_reg, MMC_BOOT_MCI_DATA_TIMER);
2527 writel(data_len, MMC_BOOT_MCI_DATA_LENGTH);
2528
2529 /* Set appropriate fields and write the MCI_DATA_CTL register. */
2530 /* Set ENABLE bit to 1 to enable the data transfer. */
2531 mmc_reg =
2532 MMC_BOOT_MCI_DATA_ENABLE | MMC_BOOT_MCI_DATA_DIR | (data_len <<
2533 MMC_BOOT_MCI_BLKSIZE_POS);
2534
Deepa Dinamani07e66872012-06-29 18:32:05 -07002535#if MMC_BOOT_ADM || MMC_BOOT_BAM
Ajay Dudanib01e5062011-12-03 23:23:42 -08002536 mmc_reg |= MMC_BOOT_MCI_DATA_DM_ENABLE;
2537#endif
2538
2539 writel(mmc_reg, MMC_BOOT_MCI_DATA_CTL);
2540
Channagoud Kadabi672c4c42012-12-20 17:51:45 -08002541 /* Wait for the MMC_BOOT_MCI_DATA_CTL write to go through. */
2542 mmc_mclk_reg_wr_delay();
2543
Ajay Dudanib01e5062011-12-03 23:23:42 -08002544 memset((struct mmc_boot_command *)&cmd, 0,
2545 sizeof(struct mmc_boot_command));
2546
2547 cmd.cmd_index = command;
2548 cmd.argument = addr;
2549 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
2550 cmd.resp_type = MMC_BOOT_RESP_R1;
2551
2552 /* send command */
2553 mmc_ret = mmc_boot_send_command(&cmd);
2554 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
2555 return mmc_ret;
2556 }
2557
2558 /* Read the transfer data from SDCC FIFO. */
2559 mmc_ret =
Deepa Dinamani07e66872012-06-29 18:32:05 -07002560 mmc_boot_data_transfer(out, data_len, MMC_BOOT_DATA_READ);
Ajay Dudanib01e5062011-12-03 23:23:42 -08002561
2562 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
2563 dprintf(CRITICAL, "Error No.%d: Failure on data transfer from the \
2564 Card(RCA:%x)\n", mmc_ret,
2565 card->rca);
2566 return mmc_ret;
2567 }
2568
2569 return MMC_BOOT_E_SUCCESS;
2570}
2571
2572/*
2573 * Function to set/clear power-on write protection for the user area partitions
2574 */
2575static unsigned int
Channagoud Kadabi398e1a22013-03-18 12:06:11 -07002576mmc_boot_set_clr_power_on_wp_user(struct mmc_card *card,
Ajay Dudanib01e5062011-12-03 23:23:42 -08002577 unsigned int addr,
2578 unsigned int size, unsigned char set_clear_wp)
2579{
2580 struct mmc_boot_command cmd;
2581 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
2582 unsigned int wp_group_size, loop_count;
2583 unsigned int status;
2584
2585 memset((struct mmc_boot_command *)&cmd, 0,
2586 sizeof(struct mmc_boot_command));
2587
2588 /* Disabling PERM_WP for USER AREA (CMD6) */
Channagoud Kadabi0dcccec2013-11-22 14:57:56 -08002589 mmc_ret = mmc_boot_switch_cmd(card, MMC_BOOT_SET_BIT,
Ajay Dudanib01e5062011-12-03 23:23:42 -08002590 MMC_BOOT_EXT_USER_WP,
2591 MMC_BOOT_US_PERM_WP_DIS);
2592
2593 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
2594 return mmc_ret;
2595 }
2596
Ajay Dudanib01e5062011-12-03 23:23:42 -08002597 mmc_ret = mmc_boot_send_ext_cmd(card, ext_csd_buf);
2598
2599 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
2600 return mmc_ret;
2601 }
2602
2603 /* Make sure power-on write protection for user area is not disabled
2604 and permanent write protection for user area is not enabled */
2605
2606 if ((IS_BIT_SET_EXT_CSD(MMC_BOOT_EXT_USER_WP, MMC_BOOT_US_PERM_WP_EN))
2607 ||
2608 (IS_BIT_SET_EXT_CSD(MMC_BOOT_EXT_USER_WP, MMC_BOOT_US_PWR_WP_DIS)))
2609 {
2610 return MMC_BOOT_E_FAILURE;
2611 }
2612
2613 if (ext_csd_buf[MMC_BOOT_EXT_ERASE_GROUP_DEF]) {
2614 /* wp_group_size = 512KB * HC_WP_GRP_SIZE * HC_ERASE_GRP_SIZE.
2615 Getting write protect group size in sectors here. */
2616
2617 wp_group_size =
2618 (512 * 1024) * ext_csd_buf[MMC_BOOT_EXT_HC_WP_GRP_SIZE] *
2619 ext_csd_buf[MMC_BOOT_EXT_HC_ERASE_GRP_SIZE] /
2620 MMC_BOOT_WR_BLOCK_LEN;
2621 } else {
2622 /* wp_group_size = (WP_GRP_SIZE + 1) * (ERASE_GRP_SIZE + 1)
2623 * (ERASE_GRP_MULT + 1).
2624 This is defined as the number of write blocks directly */
2625
2626 wp_group_size = (card->csd.erase_grp_size + 1) *
2627 (card->csd.erase_grp_mult + 1) * (card->csd.wp_grp_size +
2628 1);
2629 }
Shashank Mittalac23fa12012-02-13 17:38:15 -08002630
Ajay Dudanib01e5062011-12-03 23:23:42 -08002631 if (wp_group_size == 0) {
2632 return MMC_BOOT_E_FAILURE;
2633 }
2634
2635 /* Setting POWER_ON_WP for USER AREA (CMD6) */
Channagoud Kadabi0dcccec2013-11-22 14:57:56 -08002636 mmc_ret = mmc_boot_switch_cmd(card, MMC_BOOT_SET_BIT,
Ajay Dudanib01e5062011-12-03 23:23:42 -08002637 MMC_BOOT_EXT_USER_WP,
2638 MMC_BOOT_US_PWR_WP_EN);
2639
2640 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
2641 return mmc_ret;
2642 }
2643
Ajay Dudanib01e5062011-12-03 23:23:42 -08002644 /* Calculating the loop count for sending SET_WRITE_PROTECT (CMD28)
2645 or CLEAR_WRITE_PROTECT (CMD29).
2646 We are write protecting the partitions in blocks of write protect
2647 group sizes only */
2648
2649 if (size % wp_group_size) {
2650 loop_count = (size / wp_group_size) + 1;
2651 } else {
2652 loop_count = (size / wp_group_size);
2653 }
2654
2655 if (set_clear_wp)
2656 cmd.cmd_index = CMD28_SET_WRITE_PROTECT;
2657 else
2658 cmd.cmd_index = CMD29_CLEAR_WRITE_PROTECT;
2659
2660 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
2661 cmd.resp_type = MMC_BOOT_RESP_R1B;
2662
2663 for (unsigned int i = 0; i < loop_count; i++) {
2664 /* Sending CMD28 for each WP group size
2665 address is in sectors already */
2666 cmd.argument = (addr + (i * wp_group_size));
2667
2668 mmc_ret = mmc_boot_send_command(&cmd);
2669
2670 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
2671 return mmc_ret;
2672 }
2673
2674 /* Checking ADDR_OUT_OF_RANGE error in CMD28 response */
2675 if (IS_ADDR_OUT_OF_RANGE(cmd.resp[0])) {
2676 return MMC_BOOT_E_FAILURE;
2677 }
2678
2679 /* Sending CMD13 to check card status */
2680 do {
2681 mmc_ret = mmc_boot_get_card_status(card, 0, &status);
2682 if (MMC_BOOT_CARD_STATUS(status) == MMC_BOOT_TRAN_STATE)
2683 break;
2684 }
2685 while ((mmc_ret == MMC_BOOT_E_SUCCESS) &&
2686 (MMC_BOOT_CARD_STATUS(status) == MMC_BOOT_PROG_STATE));
2687
2688 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
2689 return mmc_ret;
2690 }
2691 }
2692
2693 return MMC_BOOT_E_SUCCESS;
2694}
2695
2696/*
2697 * Function to get Write Protect status of the given sector
2698 */
2699static unsigned int
Channagoud Kadabi398e1a22013-03-18 12:06:11 -07002700mmc_boot_get_wp_status(struct mmc_card *card, unsigned int sector)
Ajay Dudanib01e5062011-12-03 23:23:42 -08002701{
2702 unsigned int rc = MMC_BOOT_E_SUCCESS;
2703 memset(wp_status_buf, 0, 8);
2704
2705 rc = mmc_boot_read_reg(card, 8, CMD31_SEND_WRITE_PROT_TYPE, sector,
2706 (unsigned int *)wp_status_buf);
2707 return rc;
2708}
2709
2710/*
Shashank Mittalac23fa12012-02-13 17:38:15 -08002711 * Test Function for setting Write protect for given sector
Ajay Dudanib01e5062011-12-03 23:23:42 -08002712 */
Shashank Mittalac23fa12012-02-13 17:38:15 -08002713static unsigned int
Ajay Dudanib01e5062011-12-03 23:23:42 -08002714mmc_wp(unsigned int sector, unsigned int size, unsigned char set_clear_wp)
2715{
2716 unsigned int rc = MMC_BOOT_E_SUCCESS;
2717
2718 /* Checking whether group write protection feature is available */
Channagoud Kadabi0dcccec2013-11-22 14:57:56 -08002719 if (mmc_card.csd.wp_grp_enable)
2720 {
Ajay Dudanib01e5062011-12-03 23:23:42 -08002721 rc = mmc_boot_get_wp_status(&mmc_card, sector);
Channagoud Kadabi0dcccec2013-11-22 14:57:56 -08002722 if (rc != MMC_BOOT_E_SUCCESS)
2723 {
2724 dprintf(CRITICAL, "Failure in getting wp_status (%u):%s:%u\n", rc, __FILE__, __LINE__);
2725 return rc;
2726 }
Ajay Dudanib01e5062011-12-03 23:23:42 -08002727 rc = mmc_boot_set_clr_power_on_wp_user(&mmc_card, sector, size,
2728 set_clear_wp);
Channagoud Kadabi0dcccec2013-11-22 14:57:56 -08002729 if (rc != MMC_BOOT_E_SUCCESS)
2730 {
2731 dprintf(CRITICAL, "Failure in setting power on wp user (%u):%s:%u\n", rc, __FILE__, __LINE__);
2732 return rc;
2733 }
Ajay Dudanib01e5062011-12-03 23:23:42 -08002734 rc = mmc_boot_get_wp_status(&mmc_card, sector);
Channagoud Kadabi0dcccec2013-11-22 14:57:56 -08002735 if (rc != MMC_BOOT_E_SUCCESS)
2736 {
2737 dprintf(CRITICAL, "Failure in getting wp_status (%u)%s:%u\n", rc, __FILE__, __LINE__);
2738 return rc;
2739 }
2740 }
2741 else
Ajay Dudanib01e5062011-12-03 23:23:42 -08002742 return MMC_BOOT_E_FAILURE;
2743}
2744
2745void mmc_wp_test(void)
2746{
2747 unsigned int mmc_ret = 0;
2748 mmc_ret = mmc_wp(0xE06000, 0x5000, 1);
2749}
2750
2751unsigned mmc_get_psn(void)
2752{
2753 return mmc_card.cid.psn;
2754}
2755
2756/*
2757 * Read/write data from/to SDC FIFO.
2758 */
2759static unsigned int
Deepa Dinamani07e66872012-06-29 18:32:05 -07002760mmc_boot_data_transfer(unsigned int *data_ptr,
Ajay Dudanib01e5062011-12-03 23:23:42 -08002761 unsigned int data_len, unsigned char direction)
2762{
2763 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
2764
2765#if MMC_BOOT_ADM
2766 adm_result_t ret;
2767 adm_dir_t adm_dir;
2768
2769 if (direction == MMC_BOOT_DATA_READ) {
2770 adm_dir = ADM_MMC_READ;
2771 } else {
2772 adm_dir = ADM_MMC_WRITE;
2773 }
2774
2775 ret = adm_transfer_mmc_data(mmc_slot,
2776 (unsigned char *)data_ptr, data_len,
2777 adm_dir);
2778
2779 if (ret != ADM_RESULT_SUCCESS) {
2780 dprintf(CRITICAL, "MMC ADM transfer error: %d\n", ret);
2781 mmc_ret = MMC_BOOT_E_FAILURE;
2782 }
Deepa Dinamani07e66872012-06-29 18:32:05 -07002783
2784#elif MMC_BOOT_BAM
2785 mmc_ret = mmc_bam_transfer_data(data_ptr, data_len, direction);
Ajay Dudanib01e5062011-12-03 23:23:42 -08002786#else
2787
2788 if (direction == MMC_BOOT_DATA_READ) {
2789 mmc_ret = mmc_boot_fifo_read(data_ptr, data_len);
2790 } else {
2791 mmc_ret = mmc_boot_fifo_write(data_ptr, data_len);
2792 }
2793#endif
Deepa Dinamani07e66872012-06-29 18:32:05 -07002794
Ajay Dudanib01e5062011-12-03 23:23:42 -08002795 return mmc_ret;
2796}
2797
2798/*
2799 * Read data to SDC FIFO.
2800 */
2801static unsigned int
2802mmc_boot_fifo_read(unsigned int *mmc_ptr, unsigned int data_len)
2803{
2804 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
2805 unsigned int mmc_status = 0;
2806 unsigned int mmc_count = 0;
2807 unsigned int read_error = MMC_BOOT_MCI_STAT_DATA_CRC_FAIL |
2808 MMC_BOOT_MCI_STAT_DATA_TIMEOUT | MMC_BOOT_MCI_STAT_RX_OVRRUN;
2809
2810 /* Read the data from the MCI_FIFO register as long as RXDATA_AVLBL
2811 bit of MCI_STATUS register is set to 1 and bits DATA_CRC_FAIL,
2812 DATA_TIMEOUT, RX_OVERRUN of MCI_STATUS register are cleared to 0.
2813 Continue the reads until the whole transfer data is received */
2814
2815 do {
2816 mmc_ret = MMC_BOOT_E_SUCCESS;
2817 mmc_status = readl(MMC_BOOT_MCI_STATUS);
2818
2819 if (mmc_status & read_error) {
2820 mmc_ret = mmc_boot_status_error(mmc_status);
2821 break;
2822 }
2823
2824 if (mmc_status & MMC_BOOT_MCI_STAT_RX_DATA_AVLBL) {
2825 unsigned read_count = 1;
2826 if (mmc_status & MMC_BOOT_MCI_STAT_RX_FIFO_HFULL) {
2827 read_count = MMC_BOOT_MCI_HFIFO_COUNT;
2828 }
2829
2830 for (unsigned int i = 0; i < read_count; i++) {
2831 /* FIFO contains 16 32-bit data buffer on 16 sequential addresses */
2832 *mmc_ptr = readl(MMC_BOOT_MCI_FIFO +
2833 (mmc_count %
2834 MMC_BOOT_MCI_FIFO_SIZE));
2835 mmc_ptr++;
2836 /* increase mmc_count by word size */
2837 mmc_count += sizeof(unsigned int);
2838 }
2839 /* quit if we have read enough of data */
2840 if (mmc_count == data_len)
2841 break;
2842 } else if (mmc_status & MMC_BOOT_MCI_STAT_DATA_END) {
2843 break;
2844 }
2845 }
2846 while (1);
2847
2848 return mmc_ret;
2849}
2850
2851/*
2852 * Write data to SDC FIFO.
2853 */
2854static unsigned int
2855mmc_boot_fifo_write(unsigned int *mmc_ptr, unsigned int data_len)
2856{
2857 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
2858 unsigned int mmc_status = 0;
2859 unsigned int mmc_count = 0;
2860 unsigned int write_error = MMC_BOOT_MCI_STAT_DATA_CRC_FAIL |
2861 MMC_BOOT_MCI_STAT_DATA_TIMEOUT | MMC_BOOT_MCI_STAT_TX_UNDRUN;
Shashank Mittal5d191112012-09-19 17:02:21 -07002862 unsigned int count = 0;
2863 unsigned int sz = 0;
Ajay Dudanib01e5062011-12-03 23:23:42 -08002864
2865 /* Write the transfer data to SDCC3 FIFO */
2866 do {
Ajay Dudanib01e5062011-12-03 23:23:42 -08002867 mmc_status = readl(MMC_BOOT_MCI_STATUS);
2868
Shashank Mittal5d191112012-09-19 17:02:21 -07002869 /* Bytes left to write */
2870 count = data_len - mmc_count;
Ajay Dudanib01e5062011-12-03 23:23:42 -08002871
Shashank Mittal5d191112012-09-19 17:02:21 -07002872 /* Break if whole data is transferred */
2873 if (!count)
2874 break;
2875
2876 /* Write half FIFO or less (remaining) words in MCI_FIFO as long as either
2877 TX_FIFO_EMPTY or TX_FIFO_HFULL bits of MCI_STATUS register are set. */
2878 if ((mmc_status & MMC_BOOT_MCI_STAT_TX_FIFO_EMPTY) ||
2879 (mmc_status & MMC_BOOT_MCI_STAT_TX_FIFO_HFULL)) {
2880
2881 /* Write minimum of half FIFO and remaining words */
2882 sz = ((count >> 2) > MMC_BOOT_MCI_HFIFO_COUNT) \
2883 ? MMC_BOOT_MCI_HFIFO_COUNT : (count >> 2);
2884
2885 for (int i = 0; i < sz; i++) {
2886 writel(*mmc_ptr, MMC_BOOT_MCI_FIFO);
Ajay Dudanib01e5062011-12-03 23:23:42 -08002887 mmc_ptr++;
2888 /* increase mmc_count by word size */
2889 mmc_count += sizeof(unsigned int);
2890 }
Ajay Dudanib01e5062011-12-03 23:23:42 -08002891 }
Ajay Dudanib01e5062011-12-03 23:23:42 -08002892 }
2893 while (1);
Shashank Mittal5d191112012-09-19 17:02:21 -07002894
2895 do
2896 {
2897 mmc_status = readl(MMC_BOOT_MCI_STATUS);
2898 if (mmc_status & write_error) {
2899 mmc_ret = mmc_boot_status_error(mmc_status);
2900 break;
2901 }
2902 }
2903 while (!(mmc_status & MMC_BOOT_MCI_STAT_DATA_END));
2904
Ajay Dudanib01e5062011-12-03 23:23:42 -08002905 return mmc_ret;
2906}
2907
2908/*
2909 * CMD35_ERASE_GROUP_START
2910 */
2911
2912static unsigned int
Channagoud Kadabi398e1a22013-03-18 12:06:11 -07002913mmc_boot_send_erase_group_start(struct mmc_card *card,
Ajay Dudanib01e5062011-12-03 23:23:42 -08002914 unsigned long long data_addr)
2915{
2916 struct mmc_boot_command cmd;
2917 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
2918
2919 if (card == NULL)
2920 return MMC_BOOT_E_INVAL;
2921
2922 memset((struct mmc_boot_command *)&cmd, 0,
2923 sizeof(struct mmc_boot_command));
2924
2925 cmd.cmd_index = CMD35_ERASE_GROUP_START;
2926 cmd.argument = data_addr;
2927 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
2928 cmd.resp_type = MMC_BOOT_RESP_R1;
2929
2930 mmc_ret = mmc_boot_send_command(&cmd);
2931 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
2932 return mmc_ret;
2933 }
2934
2935 /* Checking for address error */
2936 if (IS_ADDR_OUT_OF_RANGE(cmd.resp[0])) {
2937 return MMC_BOOT_E_BLOCKLEN_ERR;
2938 }
2939
2940 return MMC_BOOT_E_SUCCESS;
2941
2942}
2943
2944/*
2945 * CMD36 ERASE GROUP END
2946 */
2947static unsigned int
Channagoud Kadabi398e1a22013-03-18 12:06:11 -07002948mmc_boot_send_erase_group_end(struct mmc_card *card,
Ajay Dudanib01e5062011-12-03 23:23:42 -08002949 unsigned long long data_addr)
2950{
2951 struct mmc_boot_command cmd;
2952 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
2953
2954 if (card == NULL)
2955 return MMC_BOOT_E_INVAL;
2956
2957 memset((struct mmc_boot_command *)&cmd, 0,
2958 sizeof(struct mmc_boot_command));
2959
2960 cmd.cmd_index = CMD36_ERASE_GROUP_END;
2961 cmd.argument = data_addr;
2962 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
2963 cmd.resp_type = MMC_BOOT_RESP_R1;
2964
2965 mmc_ret = mmc_boot_send_command(&cmd);
2966 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
2967 return mmc_ret;
2968 }
2969
2970 /* Checking for address error */
2971 if (IS_ADDR_OUT_OF_RANGE(cmd.resp[0])) {
2972 return MMC_BOOT_E_BLOCKLEN_ERR;
2973 }
2974
2975 return MMC_BOOT_E_SUCCESS;
2976}
2977
2978/*
2979 * CMD38 ERASE
2980 */
Channagoud Kadabi398e1a22013-03-18 12:06:11 -07002981static unsigned int mmc_boot_send_erase(struct mmc_card *card)
Ajay Dudanib01e5062011-12-03 23:23:42 -08002982{
2983
2984 struct mmc_boot_command cmd;
2985 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
2986 unsigned int status;
2987
2988 if (card == NULL)
2989 return MMC_BOOT_E_INVAL;
2990
2991 memset((struct mmc_boot_command *)&cmd, 0,
2992 sizeof(struct mmc_boot_command));
2993
2994 cmd.cmd_index = CMD38_ERASE;
2995 cmd.argument = 0x00000000;
2996 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
2997 cmd.resp_type = MMC_BOOT_RESP_R1B;
2998
2999 /* Checking if the card is in the transfer state */
3000 do {
3001 mmc_ret = mmc_boot_get_card_status(card, 0, &status);
3002 if (MMC_BOOT_CARD_STATUS(status) == MMC_BOOT_TRAN_STATE)
3003 break;
3004 }
3005 while ((mmc_ret == MMC_BOOT_E_SUCCESS) &&
3006 (MMC_BOOT_CARD_STATUS(status) == MMC_BOOT_PROG_STATE));
3007
3008 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
3009 return mmc_ret;
3010 }
3011 mmc_ret = mmc_boot_send_command(&cmd);
3012 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
3013 return mmc_ret;
3014 }
3015
3016 /* Checking for write protect */
3017 if (cmd.resp[0] & MMC_BOOT_R1_WP_ERASE_SKIP) {
3018 dprintf(CRITICAL, "Write protect enabled for sector \n");
3019 return;
3020 }
3021
3022 /* Checking if the erase operation for the card is compelete */
3023 do {
3024 mmc_ret = mmc_boot_get_card_status(card, 0, &status);
3025 if (MMC_BOOT_CARD_STATUS(status) == MMC_BOOT_TRAN_STATE)
3026 break;
3027 }
3028 while ((mmc_ret == MMC_BOOT_E_SUCCESS) &&
3029 (MMC_BOOT_CARD_STATUS(status) == MMC_BOOT_PROG_STATE));
3030
3031 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
3032 return mmc_ret;
3033 }
3034
3035 return MMC_BOOT_E_SUCCESS;
3036}
3037
3038/*
3039 * Function to erase data on the eMMC card
3040 */
3041unsigned int
3042mmc_erase_card(unsigned long long data_addr, unsigned long long size)
3043{
3044 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
3045 unsigned long long erase_grp_size;
3046 unsigned long long data_end = 0x00000000;
3047 unsigned long long loop_count;
3048 unsigned int out[512] = { 0 };
3049
3050 /* Converting size to sectors */
3051 size = size / 512;
3052
3053 if (ext_csd_buf[MMC_BOOT_EXT_ERASE_GROUP_DEF]) {
3054 erase_grp_size =
3055 (512 * ext_csd_buf[MMC_BOOT_EXT_HC_ERASE_GRP_SIZE] * 1024);
3056 erase_grp_size = erase_grp_size / 512;
3057 } else {
3058 erase_grp_size = (mmc_card.csd.erase_grp_size + 1) *
3059 (mmc_card.csd.erase_grp_mult + 1);
3060 }
3061
3062 if (erase_grp_size == 0) {
3063 return MMC_BOOT_E_FAILURE;
3064 }
3065
3066 if (size % erase_grp_size) {
3067 dprintf(CRITICAL, "Overflow beyond ERASE_GROUP_SIZE:%llu\n",
3068 (size % erase_grp_size));
3069
3070 }
3071 loop_count = (size / erase_grp_size);
3072 /*
3073 *In case the partition size is less than the erase_grp_size
3074 0 is written to the first block of the partition.
3075 */
3076 if (loop_count < 1) {
3077 mmc_ret = mmc_write(data_addr, 512, (unsigned int *)out);
3078 if (mmc_ret != MMC_BOOT_E_SUCCESS)
3079 return mmc_ret;
3080 else
3081 return MMC_BOOT_E_SUCCESS;
3082 } else {
3083 data_addr = ((mmc_card.type != MMC_BOOT_TYPE_MMCHC) &&
3084 (mmc_card.type != MMC_BOOT_TYPE_SDHC))
3085 ? (unsigned int)data_addr : (unsigned int)(data_addr / 512);
3086 data_end = data_addr + erase_grp_size * (loop_count - 1);
3087 }
3088
3089 /* Sending CMD35 */
3090 mmc_ret = mmc_boot_send_erase_group_start(&mmc_card, data_addr);
3091 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
3092 dprintf(CRITICAL, "Error %d: Failure sending erase group start "
3093 "command to the card (RCA:%x)\n", mmc_ret,
3094 mmc_card.rca);
3095 return mmc_ret;
3096 }
3097
3098 /* Sending CMD36 */
Neeti Desaie8bd43d2012-04-11 17:12:31 -07003099 mmc_ret = mmc_boot_send_erase_group_end(&mmc_card, data_end);
Ajay Dudanib01e5062011-12-03 23:23:42 -08003100 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
3101 dprintf(CRITICAL, "Error %d: Failure sending erase group end "
3102 "command to the card (RCA:%x)\n", mmc_ret,
3103 mmc_card.rca);
3104 return mmc_ret;
3105 }
3106
Neeti Desaie8bd43d2012-04-11 17:12:31 -07003107 /* Sending CMD38 */
3108 mmc_ret = mmc_boot_send_erase(&mmc_card);
3109 if (mmc_ret != MMC_BOOT_E_SUCCESS) {
3110 dprintf(CRITICAL,
3111 "Error %d: Failure sending erase command "
3112 "to the card (RCA:%x)\n", mmc_ret, mmc_card.rca);
3113 return mmc_ret;
Ajay Dudanib01e5062011-12-03 23:23:42 -08003114
Ajay Dudanib01e5062011-12-03 23:23:42 -08003115 }
Neeti Desaie8bd43d2012-04-11 17:12:31 -07003116
Ajay Dudanib01e5062011-12-03 23:23:42 -08003117 dprintf(CRITICAL, "ERASE SUCCESSFULLY COMPLETED\n");
3118 return MMC_BOOT_E_SUCCESS;
3119}
3120
Channagoud Kadabie2ec5a42013-03-14 16:30:42 -07003121/*
3122 * Disable MCI clk
3123 */
3124void mmc_boot_mci_clk_disable()
3125{
3126 uint32_t reg = 0;
3127
3128 reg |= MMC_BOOT_MCI_CLK_DISABLE;
3129 writel(reg, MMC_BOOT_MCI_CLK);
3130
3131 /* Wait for the MMC_BOOT_MCI_CLK write to go through. */
3132 mmc_mclk_reg_wr_delay();
3133}
3134
3135/*
3136 * Enable MCI CLK
3137 */
3138void mmc_boot_mci_clk_enable()
3139{
3140 uint32_t reg = 0;
3141
3142 reg |= MMC_BOOT_MCI_CLK_ENABLE;
3143 reg |= MMC_BOOT_MCI_CLK_ENA_FLOW;
3144 reg |= MMC_BOOT_MCI_CLK_IN_FEEDBACK;
3145 writel(reg, MMC_BOOT_MCI_CLK);
3146
3147 /* Wait for the MMC_BOOT_MCI_CLK write to go through. */
3148 mmc_mclk_reg_wr_delay();
Channagoud Kadabie2ec5a42013-03-14 16:30:42 -07003149}
3150
Deepa Dinamani07e66872012-06-29 18:32:05 -07003151#if MMC_BOOT_BAM
3152
3153void mmc_boot_dml_init()
3154{
3155 uint32_t val = 0;
3156
3157 /* Initialize s/w reset for DML core */
3158 mmc_boot_dml_reset();
3159
3160 /* Program DML config:
3161 * 1. Disable producer and consumer CRCI.
3162 * 2. Set Bypass mode for the DML for Direct access.
3163 */
3164 val = 0;
3165 val |= 1 >> SDCC_BYPASS_SHIFT;
3166 writel(val, SDCC_DML_CONFIG(dml_base));
3167
3168 /* Program consumer logic size:
3169 * This is for handshaking between the BAM and the DML blocks.
3170 */
3171 writel(4096, SDCC_DML_CONSUMER_PIPE_LOGICAL_SIZE(dml_base));
3172
3173 /* Program producer logic size
3174 * This is for handshaking between the BAM and the DML blocks.
3175 */
3176 writel(4096, SDCC_DML_PRODUCER_PIPE_LOGICAL_SIZE(dml_base));
3177
3178
3179 /* Write the pipe id numbers. */
3180 val = 0;
3181 val |= bam.pipe[MMC_BOOT_BAM_READ_PIPE_INDEX].pipe_num << SDCC_PRODUCER_PIPE_ID_SHIFT;
3182 val |= bam.pipe[MMC_BOOT_BAM_WRITE_PIPE_INDEX].pipe_num << SDCC_CONSUMER_PIPE_ID_SHIFT;
3183
3184 writel(val, SDCC_DML_PIPE_ID(dml_base));
3185
3186}
3187
3188/* Function to set up SDCC dml for System producer transaction. */
3189static void mmc_boot_dml_consumer_trans_init()
3190{
3191 uint32_t val = 0;
3192
3193 val = 0 << SDCC_PRODUCER_CRCI_SEL_SHIFT;
3194 val |= 1 << SDCC_CONSUMER_CRCI_SEL_SHIFT;
3195 writel(val, SDCC_DML_CONFIG(dml_base));
3196
3197
3198 /* Start the consumer transaction */
3199 writel(1, SDCC_DML_CONSUMER_START(dml_base));
3200
3201}
3202
3203/* Function to set up SDCC dml for System consumer transaction.
3204 * trans_end: 1: Assert DML trasaction signal
3205 * at the end of transaction.
3206 * 0: Do not assert DML transaction signal.
3207 * size: Transaction size
3208 */
3209static void mmc_boot_dml_producer_trans_init(unsigned trans_end,
3210 unsigned size)
3211{
3212 uint32_t val = 0;
3213
3214 val = 1 << SDCC_PRODUCER_CRCI_SEL_SHIFT;
3215 val |= 0 << SDCC_CONSUMER_CRCI_SEL_SHIFT;
3216 val |= trans_end << SDCC_PRODUCER_TRANS_END_EN_SHIFT;
3217 writel(val, SDCC_DML_CONFIG(dml_base));
3218
3219 /* Set block size */
3220 writel(BLOCK_SIZE, SDCC_DML_PRODUCER_BAM_BLOCK_SIZE(dml_base));
3221
3222 /* Write transaction size */
3223 writel(size, SDCC_DML_PRODUCER_BAM_TRANS_SIZE(dml_base));
3224
3225 /* Start the producer transaction */
3226 writel(1, SDCC_DML_PRODUCER_START(dml_base));
3227}
3228
3229/* Function to check producer idle status of the DML.
3230 * return value: 1: Producer is idle
3231 * 0: Producer is busy
3232 */
3233static uint32_t mmc_boot_dml_chk_producer_idle()
3234{
3235 uint32_t val = 0;
3236
3237 val = readl(SDCC_DML_STATUS(dml_base));
3238
3239 /* Read only the producer idle status */
3240 val &= (1 << SDCC_DML_PRODUCER_IDLE_SHIFT);
3241
3242 return val;
3243}
3244
3245/* Function to clear transaction complete flag */
3246static void mmc_boot_dml_clr_trans_complete()
3247{
3248 uint32_t val;
3249
3250 val = readl(SDCC_DML_CONFIG(dml_base));
3251
3252 val &= ~(1 << SDCC_PRODUCER_TRANS_END_EN_SHIFT);
3253 writel(val, SDCC_DML_CONFIG(dml_base));
3254}
3255
3256/* Blocking function to wait until DML is idle. */
3257static void mmc_boot_dml_wait_producer_idle()
3258{
3259 while(!(readl(SDCC_DML_STATUS(dml_base)) & 1));
3260}
3261
3262/* Blocking function to wait until DML is idle. */
3263static void mmc_boot_dml_wait_consumer_idle()
3264{
3265 while(!(readl(SDCC_DML_STATUS(dml_base)) & (1 << SDCC_DML_CONSUMER_IDLE_SHIFT)));
3266}
3267
3268/* Initialize S/W reset */
3269static void mmc_boot_dml_reset()
3270{
3271 /* Initialize s/w reset for DML core */
3272 writel(1, SDCC_DML_SW_RESET(dml_base));
3273
3274}
3275
3276static int mmc_bam_init(uint32_t bam_base)
3277{
3278
3279 uint32_t mmc_ret = MMC_BOOT_E_SUCCESS;
3280
3281 bam.base = bam_base;
3282 /* Read pipe parameter initializations. */
3283 bam.pipe[MMC_BOOT_BAM_READ_PIPE_INDEX].pipe_num = MMC_BOOT_BAM_READ_PIPE;
3284 /* System consumer */
3285 bam.pipe[MMC_BOOT_BAM_READ_PIPE_INDEX].trans_type = BAM2SYS;
3286 /* Set the descriptor FIFO start ptr */
3287 bam.pipe[MMC_BOOT_BAM_READ_PIPE_INDEX].fifo.head = desc_fifo;
3288 /* Set the descriptor FIFO lengths */
3289 bam.pipe[MMC_BOOT_BAM_READ_PIPE_INDEX].fifo.size = MMC_BOOT_BAM_FIFO_SIZE;
3290
3291 /* Write pipe parameter initializations.*/
3292 bam.pipe[MMC_BOOT_BAM_WRITE_PIPE_INDEX].pipe_num = MMC_BOOT_BAM_WRITE_PIPE;
3293 /* System producer */
3294 bam.pipe[MMC_BOOT_BAM_WRITE_PIPE_INDEX].trans_type = SYS2BAM;
3295 /* Write fifo uses the same fifo as read */
3296 bam.pipe[MMC_BOOT_BAM_WRITE_PIPE_INDEX].fifo.head = desc_fifo;
3297 /* Set the descriptor FIFO lengths */
3298 bam.pipe[MMC_BOOT_BAM_WRITE_PIPE_INDEX].fifo.size = MMC_BOOT_BAM_FIFO_SIZE;
3299
3300 /* Programs the minimum threshold for BAM transfer*/
3301 bam.threshold = BLOCK_SIZE;
3302
3303 /* Initialize MMC BAM */
3304 bam_init(&bam);
3305
3306 /* Initialize BAM MMC read pipe */
3307 bam_sys_pipe_init(&bam, MMC_BOOT_BAM_READ_PIPE_INDEX);
3308
3309 mmc_ret = bam_pipe_fifo_init(&bam, bam.pipe[MMC_BOOT_BAM_READ_PIPE_INDEX].pipe_num);
3310
3311 if (mmc_ret)
3312 {
3313 dprintf(CRITICAL, "MMC: BAM Read pipe fifo init error\n");
3314 goto mmc_bam_init_error;
3315 }
3316
3317 /* Initialize BAM MMC write pipe */
3318 bam_sys_pipe_init(&bam, MMC_BOOT_BAM_WRITE_PIPE_INDEX);
3319
3320 mmc_ret = bam_pipe_fifo_init(&bam, bam.pipe[MMC_BOOT_BAM_WRITE_PIPE_INDEX].pipe_num);
3321
3322 if (mmc_ret)
3323 {
3324 dprintf(CRITICAL, "MMC: BAM Write pipe fifo init error\n");
3325 goto mmc_bam_init_error;
3326 }
3327
3328 mmc_boot_dml_init();
3329
3330 mmc_bam_init_error:
3331
3332 return mmc_ret;
3333}
3334
3335static int mmc_bam_transfer_data(unsigned int *data_ptr,
3336 unsigned int data_len,
3337 unsigned int dir)
3338{
3339 uint32_t mmc_ret;
3340 uint32_t offset;
3341
3342 mmc_ret = MMC_BOOT_E_SUCCESS;
3343
3344 if(dir == MMC_BOOT_DATA_READ)
3345 {
3346 /* Check BAM IRQ status reg to verify the desc has been processed */
3347 mmc_ret = bam_wait_for_interrupt(&bam,
3348 MMC_BOOT_BAM_READ_PIPE_INDEX, P_PRCSD_DESC_EN_MASK);
3349
3350 if (mmc_ret != BAM_RESULT_SUCCESS)
3351 {
3352 dprintf(CRITICAL, "BAM transfer error \n");
3353 mmc_ret = MMC_BOOT_E_FAILURE;
3354 goto mmc_bam_transfer_err;
3355 }
3356
3357 mmc_boot_dml_wait_producer_idle();
3358
3359 /* Update BAM pipe fifo offsets */
3360 offset = bam_read_offset_update(&bam, MMC_BOOT_BAM_READ_PIPE_INDEX);
3361
3362 /* Reset DPSM */
3363 writel(0, MMC_BOOT_MCI_DATA_CTL);
3364
Channagoud Kadabi672c4c42012-12-20 17:51:45 -08003365 /* Wait for the MMC_BOOT_MCI_DATA_CTL write to go through. */
3366 mmc_mclk_reg_wr_delay();
3367
Deepa Dinamani07e66872012-06-29 18:32:05 -07003368 dprintf(SPEW, "Offset value is %d \n", offset);
3369 }
3370 else
3371 {
3372 /* Check BAM IRQ status reg to verify the desc has been processed */
3373 mmc_ret = bam_wait_for_interrupt(&bam,
3374 MMC_BOOT_BAM_WRITE_PIPE_INDEX, P_TRNSFR_END_EN_MASK);
3375
3376 if (mmc_ret != BAM_RESULT_SUCCESS)
3377 {
3378 dprintf(CRITICAL, "BAM transfer error \n");
3379 mmc_ret = MMC_BOOT_E_FAILURE;
3380 goto mmc_bam_transfer_err;
3381 }
3382
3383 /* Update BAM pipe fifo offsets */
3384 offset = bam_read_offset_update(&bam, MMC_BOOT_BAM_WRITE_PIPE_INDEX);
3385
3386 dprintf(SPEW, "Offset value is %d \n", offset);
3387 }
3388
3389mmc_bam_transfer_err:
3390
3391 return mmc_ret;
3392}
3393
3394static unsigned int
3395mmc_boot_bam_setup_desc(unsigned int *data_ptr,
3396 unsigned int data_len,
3397 unsigned char direction)
3398{
3399 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
3400
3401 if (direction == MMC_BOOT_DATA_READ)
3402 {
3403 mmc_boot_dml_producer_trans_init(1, data_len);
3404 mmc_ret = bam_add_desc(&bam, MMC_BOOT_BAM_READ_PIPE_INDEX,
3405 (unsigned char *)data_ptr, data_len);
3406 }
3407 else
3408 {
3409 mmc_boot_dml_consumer_trans_init();
3410 mmc_ret = bam_add_desc(&bam, MMC_BOOT_BAM_WRITE_PIPE_INDEX,
3411 (unsigned char *)data_ptr, data_len);
3412 }
3413
3414 /* Update return value enums */
3415 if (mmc_ret != BAM_RESULT_SUCCESS)
3416 {
3417 dprintf(CRITICAL, "MMC BAM transfer error: %d\n", mmc_ret);
3418 mmc_ret = MMC_BOOT_E_FAILURE;
3419 }
3420}
3421
3422#endif
Channagoud Kadabi676c2e32013-04-02 11:39:01 -07003423
3424/*
3425 * Check if card supports DDR mode
3426 */
3427uint8_t card_supports_ddr_mode()
3428{
3429 if (IS_BIT_SET_EXT_CSD(MMC_DEVICE_TYPE, 2) ||
3430 IS_BIT_SET_EXT_CSD(MMC_DEVICE_TYPE, 3))
3431 return 1;
3432 else
3433 return 0;
3434}
3435
3436/*
3437 * Check if card suppports HS200 mode
3438 */
3439uint8_t card_supports_hs200_mode()
3440{
3441 if (IS_BIT_SET_EXT_CSD(MMC_DEVICE_TYPE, 4) ||
3442 IS_BIT_SET_EXT_CSD(MMC_DEVICE_TYPE, 5))
3443 return 1;
3444 else
3445 return 0;
3446}
Channagoud Kadabi7cf5d042013-04-19 12:58:15 -07003447
3448/* Return the density of the mmc device */
3449uint64_t mmc_get_device_capacity()
3450{
3451 return mmc_card.capacity;
3452}
Channagoud Kadabi54dd5312013-06-18 18:31:44 -07003453
Channagoud Kadabi96c629e2013-09-10 14:21:30 -07003454/* Return the block size of the mmc device */
3455uint32_t mmc_get_device_blocksize()
3456{
3457 return mmc_card.block_size;
3458}
3459
Channagoud Kadabi54dd5312013-06-18 18:31:44 -07003460void mmc_put_card_to_sleep(void)
3461{
3462 uint32_t mmc_ret;
3463 struct mmc_boot_command cmd = {0};
3464
3465 cmd.cmd_index = CMD7_SELECT_DESELECT_CARD;
3466 cmd.argument = 0x00000000;
3467 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
3468 cmd.resp_type = MMC_BOOT_RESP_NONE;
3469
3470 /* send command */
3471 mmc_ret = mmc_boot_send_command(&cmd);
3472 if (mmc_ret != MMC_BOOT_E_SUCCESS)
3473 {
3474 dprintf(CRITICAL, "card deselect error: %d\n", mmc_ret);
3475 return;
3476 }
3477
3478 cmd.cmd_index = CMD5_SLEEP_AWAKE;
3479 cmd.argument = (mmc_card.rca << MMC_CARD_RCA_BIT) | MMC_CARD_SLEEP;
3480 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
3481 cmd.resp_type = MMC_BOOT_RESP_R1B;
3482
3483 /* send command */
3484 mmc_ret = mmc_boot_send_command(&cmd);
3485 if (mmc_ret != MMC_BOOT_E_SUCCESS)
3486 dprintf(CRITICAL, "card sleep error: %d\n", mmc_ret);
3487}