blob: d19eca565bd397c4c84c9efea314bc0ca0c65f38 [file] [log] [blame]
Shashank Mittal52525ff2010-04-13 11:11:10 -07001/* Copyright (c) 2010, Code Aurora Forum. All rights reserved.
2
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer in the documentation and/or other materials provided
11 * with the distribution.
12 * * Neither the name of Code Aurora Forum, Inc. nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <string.h>
30#include <stdlib.h>
31#include <debug.h>
32#include <reg.h>
33#include "mmc.h"
34#include <platform/iomap.h>
35
36#ifndef NULL
37#define NULL 0
38#endif
39
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -070040#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
41
Shashank Mittal52525ff2010-04-13 11:11:10 -070042/* data access time unit in ns */
43static const unsigned int taac_unit[] =
44{ 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000 };
45/* data access time value x 10 */
46static const unsigned int taac_value[] =
47{ 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 };
48
49/* data transfer rate in kbit/s */
50static const unsigned int xfer_rate_unit[] =
51{ 100, 1000, 10000, 100000, 0, 0, 0, 0 };
52/* data transfer rate value x 10*/
53static const unsigned int xfer_rate_value[] =
54{ 0, 10, 12, 13, 15, 20, 26, 30, 35, 40, 45, 52, 55, 60, 70, 80 };
55
56char *ext3_partitions[] = {"system", "userdata"};
57unsigned int ext3_count = 0;
58
59static unsigned mmc_sdc_clk[] = { SDC1_CLK, SDC2_CLK, SDC3_CLK, SDC4_CLK};
60static unsigned mmc_sdc_pclk[] = { SDC1_PCLK, SDC2_PCLK, SDC3_PCLK, SDC4_PCLK};
61
62int mmc_clock_enable_disable(unsigned id, unsigned enable);
63int mmc_clock_get_rate(unsigned id);
64int mmc_clock_set_rate(unsigned id, unsigned rate);
65void mdelay(unsigned msecs);
66
67struct mmc_boot_host mmc_host;
68struct mmc_boot_card mmc_card;
69struct mbr_entry mbr[MAX_PARTITIONS];
70unsigned mmc_partition_count = 0;
71static void mbr_fill_name (struct mbr_entry *mbr_ent, unsigned int type);
72unsigned int mmc_read (unsigned long long data_addr, unsigned int* out, unsigned int data_len);
73
74
75unsigned int SWAP_ENDIAN(unsigned int val)
76{
77 return ((val & 0xFF) << 24) |
78 (((val >> 8) & 0xFF) << 16) |
79 (((val >> 16) & 0xFF) << 8) |
80 (val >> 24);
81}
82
83/*
84 * Function to enable and set master and peripheral clock for
85 * MMC card.
86 */
87static unsigned int mmc_boot_enable_clock( struct mmc_boot_host* host,
88 unsigned int mclk)
89{
90 unsigned int mmc_clk = 0;
91
92#ifndef PLATFORM_MSM8X60
93 int mmc_signed_ret = 0;
94 unsigned SDC_CLK = mmc_sdc_clk[MMC_SLOT - 1];
95 unsigned SDC_PCLK = mmc_sdc_pclk[MMC_SLOT - 1];
96
97 if( host == NULL )
98 {
99 return MMC_BOOT_E_INVAL;
100 }
101
102 if( !host->clk_enabled )
103 {
104 /* set clock */
105 if( mmc_clock_enable_disable(SDC_PCLK, MMC_CLK_ENABLE) < 0 )
106 {
107 dprintf(CRITICAL, "Failure enabling PCLK!\n");
108 goto error_pclk;
109 }
110
111 if( mmc_clock_enable_disable(SDC_CLK, MMC_CLK_ENABLE) < 0 )
112 {
113 dprintf(CRITICAL, "Failure enabling MMC Clock!\n");
114 goto error;
115 }
116 host->clk_enabled = 1;
117 }
118 if( host->mclk_rate != mclk )
119 {
120 if( mmc_clock_set_rate(SDC_CLK, mclk) < 0 )
121 {
122 dprintf(CRITICAL, "Failure setting clock rate for MCLK - clk_rate: %d\n!", mclk );
123 goto error_mclk;
124 }
125
126 if( ( mmc_signed_ret = mmc_clock_get_rate(SDC_CLK) ) < 0 )
127 {
128 dprintf(CRITICAL, "Failure getting clock rate for MCLK - clk_rate: %d\n!", host->mclk_rate );
129 goto error_mclk;
130 }
131
132 host->mclk_rate = (unsigned int)mmc_signed_ret;
133 }
134
135 if( ( mmc_signed_ret = mmc_clock_get_rate(SDC_PCLK) ) < 0 )
136 {
137 dprintf(CRITICAL, "Failure getting clock rate for PCLK - clk_rate: %d\n!", host->pclk_rate );
138 goto error_pclk;
139 }
140
141 host->pclk_rate = ( unsigned int )mmc_signed_ret;
142 dprintf(INFO, "Clock rate - mclk: %dHz pclk: %dHz\n", host->mclk_rate, host->pclk_rate );
143#else
144 clock_set_enable(mclk);
145 host->mclk_rate = mclk;
146 host->pclk_rate = mclk;
147 host->clk_enabled = 1;
148#endif
149 //enable mci clock
150 mmc_clk |= MMC_BOOT_MCI_CLK_ENABLE;
151 //enable flow control
152 mmc_clk |= MMC_BOOT_MCI_CLK_ENA_FLOW;
153 //latch data and command using feedback clock
154 mmc_clk |= MMC_BOOT_MCI_CLK_IN_FEEDBACK;
155 writel( mmc_clk, MMC_BOOT_MCI_CLK );
156 return MMC_BOOT_E_SUCCESS;
157
158#ifndef PLATFORM_MSM8X60
159error_pclk:
160 mmc_clock_enable_disable(SDC_PCLK, MMC_CLK_DISABLE);
161error_mclk:
162 mmc_clock_enable_disable(SDC_CLK, MMC_CLK_DISABLE);
163error:
164 return MMC_BOOT_E_CLK_ENABLE_FAIL;
165#endif
166}
167
168
169/* Sets a timeout for read operation.
170 */
171static unsigned int mmc_boot_set_read_timeout( struct mmc_boot_host* host,
172 struct mmc_boot_card* card )
173{
174 unsigned int timeout_ns = 0;
175
176 if( ( host == NULL ) || ( card == NULL ) )
177 {
178 return MMC_BOOT_E_INVAL;
179 }
180
181 if( card->type == MMC_BOOT_TYPE_SDHC )
182 {
183 card->rd_timeout_ns = 100000000;
184 }
185 else if( card->type == MMC_BOOT_TYPE_STD_SD )
186 {
187 timeout_ns = 10 * ( (card->csd.taac_ns ) +
188 ( card->csd.nsac_clk_cycle / (host->mclk_rate/1000000000)));
189 }
190 else
191 {
192 return MMC_BOOT_E_NOT_SUPPORTED;
193 }
194
195 dprintf(INFO, " Read timeout set: %d ns\n", card->rd_timeout_ns );
196
197 return MMC_BOOT_E_SUCCESS;
198}
199
200/* Sets a timeout for write operation.
201 */
202static unsigned int mmc_boot_set_write_timeout( struct mmc_boot_host* host,
203 struct mmc_boot_card* card )
204{
205 unsigned int timeout_ns = 0;
206
207 if( ( host == NULL ) || ( card == NULL ) )
208 {
209 return MMC_BOOT_E_INVAL;
210 }
211
212 if( card->type == MMC_BOOT_TYPE_SDHC )
213 {
214 card->wr_timeout_ns = 100000000;
215 }
216 else if( card->type == MMC_BOOT_TYPE_STD_SD )
217 {
218 timeout_ns = 10 * ( ( card->csd.taac_ns ) +
219 ( card->csd.nsac_clk_cycle / ( host->mclk_rate/1000000000 ) ) );
220 timeout_ns = timeout_ns << card->csd.r2w_factor;
221 }
222 else
223 {
224 return MMC_BOOT_E_NOT_SUPPORTED;
225 }
226
227 dprintf(INFO, " Write timeout set: %d ns\n", card->wr_timeout_ns );
228
229 return MMC_BOOT_E_SUCCESS;
230}
231
232
233/*
234 * Decodes CSD response received from the card. Note that we have defined only
235 * few of the CSD elements in csd structure. We'll only decode those values.
236 */
237static unsigned int mmc_boot_decode_and_save_csd( struct mmc_boot_card* card,
238 unsigned int* raw_csd )
239{
240 unsigned int mmc_sizeof = 0;
241 unsigned int mmc_unit = 0;
242 unsigned int mmc_value = 0;
243 unsigned int mmc_temp = 0;
244
245 struct mmc_boot_csd mmc_csd;
246
247 if( ( card == NULL ) || ( raw_csd == NULL ) )
248 {
249 return MMC_BOOT_E_INVAL;
250 }
251
252 /* CSD register is little bit differnet for CSD version 2.0 High Capacity
253 * and CSD version 1.0/2.0 Standard memory cards. In Version 2.0 some of
254 * the fields have fixed values and it's not necessary for host to refer
255 * these fields in CSD sent by card */
256
257 mmc_sizeof = sizeof(unsigned int) * 8;
258
259 mmc_csd.cmmc_structure = UNPACK_BITS( raw_csd, 126, 2, mmc_sizeof );
260
261 /* cmmc_structure- 0: Version 1.0 1: Version 2.0 */
262 if( mmc_csd.cmmc_structure )
263 {
264 mmc_csd.card_cmd_class = UNPACK_BITS( raw_csd, 84, 12, mmc_sizeof );
265 mmc_csd.write_blk_len = 512; /* Fixed value is 9 = 2^9 = 512 */
266 mmc_csd.read_blk_len = 512; /* Fixed value is 9 = 512 */
267 mmc_csd.r2w_factor = UNPACK_BITS( raw_csd, 26, 3, mmc_sizeof ); /* Fixed value: 010b */
268 mmc_csd.c_size_mult = 0; /* not there in version 2.0 */
269 mmc_csd.c_size = UNPACK_BITS( raw_csd, 62, 12, mmc_sizeof );
270 mmc_csd.nsac_clk_cycle = UNPACK_BITS( raw_csd, 104, 8, mmc_sizeof) * 100;
271
272 mmc_unit = UNPACK_BITS( raw_csd, 112, 3, mmc_sizeof );
273 mmc_value = UNPACK_BITS( raw_csd, 115, 4, mmc_sizeof );
274 mmc_csd.taac_ns = ( taac_value[mmc_value] * taac_unit[mmc_unit]) / 10;
275
276 mmc_csd.erase_blk_len = 1;
277 mmc_csd.read_blk_misalign = 0;
278 mmc_csd.write_blk_misalign = 0;
279 mmc_csd.read_blk_partial = 0;
280 mmc_csd.write_blk_partial = 0;
281
282 mmc_unit = UNPACK_BITS( raw_csd, 96, 3, mmc_sizeof );
283 mmc_value = UNPACK_BITS( raw_csd, 99, 4, mmc_sizeof );
284 mmc_csd.tran_speed = ( xfer_rate_value[mmc_value] * xfer_rate_unit[mmc_unit]) / 10;
285
286 /* Calculate card capcity now itself */
287 card->capacity = ( 1 + mmc_csd.c_size ) * 512000;
288 }
289 else
290 {
291 mmc_csd.card_cmd_class = UNPACK_BITS( raw_csd, 84, 12, mmc_sizeof );
292
293 mmc_temp = UNPACK_BITS( raw_csd, 22, 4, mmc_sizeof );
294 mmc_csd.write_blk_len = ( mmc_temp > 8 && mmc_temp < 12 )? ( 1 << mmc_temp ) : 512;
295
296 mmc_temp = UNPACK_BITS( raw_csd, 80, 4, mmc_sizeof );
297 mmc_csd.read_blk_len = ( mmc_temp > 8 && mmc_temp < 12 )? ( 1 << mmc_temp ) : 512;
298
299 mmc_unit = UNPACK_BITS( raw_csd, 112, 3, mmc_sizeof );
300 mmc_value = UNPACK_BITS( raw_csd, 115, 4, mmc_sizeof );
301 mmc_csd.taac_ns = ( taac_value[mmc_value] * taac_unit[mmc_unit]) / 10;
302
303 mmc_unit = UNPACK_BITS( raw_csd, 96, 3, mmc_sizeof );
304 mmc_value = UNPACK_BITS( raw_csd, 99, 4, mmc_sizeof );
305 mmc_csd.tran_speed = ( xfer_rate_value[mmc_value] * xfer_rate_unit[mmc_unit]) / 10;
306
307 mmc_csd.nsac_clk_cycle = UNPACK_BITS( raw_csd, 104, 8, mmc_sizeof ) * 100;
308
309 mmc_csd.r2w_factor = UNPACK_BITS( raw_csd, 26, 3, mmc_sizeof );
310 mmc_csd.sector_size = UNPACK_BITS( raw_csd, 39, 7, mmc_sizeof ) + 1;
311
312 mmc_csd.erase_blk_len = UNPACK_BITS( raw_csd, 46, 1, mmc_sizeof );
313 mmc_csd.read_blk_misalign = UNPACK_BITS( raw_csd, 77, 1, mmc_sizeof );
314 mmc_csd.write_blk_misalign = UNPACK_BITS( raw_csd, 78, 1, mmc_sizeof );
315 mmc_csd.read_blk_partial = UNPACK_BITS( raw_csd, 79, 1, mmc_sizeof );
316 mmc_csd.write_blk_partial = UNPACK_BITS( raw_csd, 21, 1, mmc_sizeof );
317
318 mmc_csd.c_size_mult = UNPACK_BITS( raw_csd, 47, 3, mmc_sizeof );
319 mmc_csd.c_size = UNPACK_BITS( raw_csd, 62, 12, mmc_sizeof );
320 mmc_temp = ( 1 << ( mmc_csd.c_size_mult + 2 ) ) * ( mmc_csd.c_size + 1 );
321 card->capacity = mmc_temp * mmc_csd.read_blk_len;
322 }
323
324 /* save the information in card structure */
325 memcpy( (struct mmc_boot_csd *)&card->csd, (struct mmc_boot_csd *)&mmc_csd,
326 sizeof(struct mmc_boot_csd) );
327
328 dprintf(INFO, "Decoded CSD fields:\n" );
329 dprintf(INFO, "cmmc_structure: %d\n", mmc_csd.cmmc_structure );
330 dprintf(INFO, "card_cmd_class: %x\n", mmc_csd.card_cmd_class );
331 dprintf(INFO, "write_blk_len: %d\n", mmc_csd.write_blk_len );
332 dprintf(INFO, "read_blk_len: %d\n", mmc_csd.read_blk_len );
333 dprintf(INFO, "r2w_factor: %d\n", mmc_csd.r2w_factor );
334 dprintf(INFO, "sector_size: %d\n", mmc_csd.sector_size );
335 dprintf(INFO, "c_size_mult:%d\n", mmc_csd.c_size_mult );
336 dprintf(INFO, "c_size: %d\n", mmc_csd.c_size );
337 dprintf(INFO, "nsac_clk_cycle: %d\n", mmc_csd.nsac_clk_cycle );
338 dprintf(INFO, "taac_ns: %d\n", mmc_csd.taac_ns );
339 dprintf(INFO, "tran_speed: %d kbps\n", mmc_csd.tran_speed );
340 dprintf(INFO, "erase_blk_len: %d\n", mmc_csd.erase_blk_len );
341 dprintf(INFO, "read_blk_misalign: %d\n", mmc_csd.read_blk_misalign );
342 dprintf(INFO, "write_blk_misalign: %d\n", mmc_csd.write_blk_misalign );
343 dprintf(INFO, "read_blk_partial: %d\n", mmc_csd.read_blk_partial );
344 dprintf(INFO, "write_blk_partial: %d\n", mmc_csd.write_blk_partial );
345 dprintf(INFO, "Card Capacity: %d Bytes\n", card->capacity );
346
347 return MMC_BOOT_E_SUCCESS;
348
349}
350
351/*
352 * Decode CID sent by the card.
353 */
354static unsigned int mmc_boot_decode_and_save_cid( struct mmc_boot_card* card,
355 unsigned int* raw_cid )
356{
357 struct mmc_boot_cid mmc_cid;
358 unsigned int mmc_sizeof = 0;
359 int i = 0;
360
361 if( ( card == NULL ) || ( raw_cid == NULL ) )
362 {
363 return MMC_BOOT_E_INVAL;
364 }
365
366 mmc_sizeof = sizeof( unsigned int ) * 8;
367 mmc_cid.mid = UNPACK_BITS( raw_cid, 120, 8, mmc_sizeof );
368 mmc_cid.oid = UNPACK_BITS( raw_cid, 104, 16, mmc_sizeof );
369
370 for( i = 0; i < 6; i++ )
371 {
372 mmc_cid.pnm[i] = (unsigned char) UNPACK_BITS(raw_cid, \
373 (104 - 8 * (i+1)), 8, mmc_sizeof );
374 }
375 mmc_cid.pnm[6] = 0;
376
377 mmc_cid.prv = UNPACK_BITS( raw_cid, 48, 8, mmc_sizeof );
378 mmc_cid.psn = UNPACK_BITS( raw_cid, 16, 32, mmc_sizeof );
379 mmc_cid.month = UNPACK_BITS( raw_cid, 12, 4, mmc_sizeof );
380 mmc_cid.year = UNPACK_BITS( raw_cid, 8, 4, mmc_sizeof );
381
382 /* save it in card database */
383 memcpy( ( struct mmc_boot_cid * )&card->cid, \
384 ( struct mmc_boot_cid * )&mmc_cid, \
385 sizeof( struct mmc_boot_cid ) );
386
387 dprintf(INFO, "Decoded CID fields:\n" );
388 dprintf(INFO, "Manufacturer ID: %x\n", mmc_cid.mid );
389 dprintf(INFO, "OEM ID: 0x%x\n", mmc_cid.oid );
390 dprintf(INFO, "Product Name: %s\n", mmc_cid.pnm );
391 dprintf(INFO, "Product revision: %d.%d\n", (mmc_cid.prv >> 4), (mmc_cid.prv & 0xF) );
392 dprintf(INFO, "Product serial number: %X\n", mmc_cid.psn );
393 dprintf(INFO, "Manufacturing date: %d %d\n", mmc_cid.month, mmc_cid.year + 1997 );
394
395 return MMC_BOOT_E_SUCCESS;
396}
397
398/*
399 * Sends specified command to a card and waits for a response.
400 */
401static unsigned int mmc_boot_send_command( struct mmc_boot_command* cmd )
402{
403 unsigned int mmc_cmd = 0;
404 unsigned int mmc_status = 0;
405 unsigned int mmc_resp = 0;
406 unsigned int mmc_return = MMC_BOOT_E_SUCCESS;
407 unsigned int cmd_index = 0;
408 int i = 0;
409
410 /* basic check */
411 if( cmd == NULL )
412 {
413 return MMC_BOOT_E_INVAL;
414 }
415
416 /* 1. Write command argument to MMC_BOOT_MCI_ARGUMENT register */
417 writel( cmd->argument, MMC_BOOT_MCI_ARGUMENT );
418
419 /* 2. Set appropriate fields and write MMC_BOOT_MCI_CMD */
420 /* 2a. Write command index in CMD_INDEX field */
421 cmd_index = cmd->cmd_index;
422 mmc_cmd |= cmd->cmd_index;
423 /* 2b. Set RESPONSE bit to 1 for all cmds except CMD0 */
424 if( cmd_index != CMD0_GO_IDLE_STATE )
425 {
426 mmc_cmd |= MMC_BOOT_MCI_CMD_RESPONSE;
427 }
428
429 /* 2c. Set LONGRESP bit to 1 for CMD2, CMD9 and CMD10 */
430 if( IS_RESP_136_BITS(cmd->resp_type) )
431 {
432 mmc_cmd |= MMC_BOOT_MCI_CMD_LONGRSP;
433 }
434
435 /* 2d. Set INTERRUPT bit to 1 to disable command timeout */
436
437 /* 2e. Set PENDING bit to 1 for CMD12 in the beginning of stream
438 mode data transfer*/
439 if( cmd->xfer_mode == MMC_BOOT_XFER_MODE_STREAM )
440 {
441 mmc_cmd |= MMC_BOOT_MCI_CMD_PENDING;
442 }
443
444 /* 2f. Set ENABLE bit to 1 */
445 mmc_cmd |= MMC_BOT_MCI_CMD_ENABLE;
446
447 /* 2g. Set PROG_ENA bit to 1 for CMD12, CMD13 issued at the end of
448 write data transfer */
449 if( ( cmd_index == CMD12_STOP_TRANSMISSION ||
450 cmd_index == CMD13_SEND_STATUS ) && cmd->prg_enabled )
451 {
452 mmc_cmd |= MMC_BOOT_MCI_CMD_PROG_ENA;
453 }
454
455 /* 2h. Set MCIABORT bit to 1 for CMD12 when working with SDIO card */
456 /* 2i. Set CCS_ENABLE bit to 1 for CMD61 when Command Completion Signal
457 of CE-ATA device is enabled */
458
459 /* 2j. clear all static status bits */
460 writel( MMC_BOOT_MCI_STATIC_STATUS, MMC_BOOT_MCI_CLEAR );
461
462 /* 2k. Write to MMC_BOOT_MCI_CMD register */
463 writel( mmc_cmd, MMC_BOOT_MCI_CMD );
464
465 dprintf(INFO, "Command sent: CMD%d MCI_CMD_REG:%x MCI_ARG:%x\n",
466 cmd_index, mmc_cmd, cmd->argument );
467
468 /* 3. Wait for interrupt or poll on the following bits of MCI_STATUS
469 register */
470 do{
471 /* 3a. Read MCI_STATUS register */
472 while(readl( MMC_BOOT_MCI_STATUS ) \
473 & MMC_BOOT_MCI_STAT_CMD_ACTIVE);
474
475 mmc_status = readl( MMC_BOOT_MCI_STATUS );
476
477 /* 3b. CMD_SENT bit supposed to be set to 1 only after CMD0 is sent -
478 no response required. */
479 if( ( cmd->resp_type == MMC_BOOT_RESP_NONE ) &&
480 (mmc_status & MMC_BOOT_MCI_STAT_CMD_SENT ) )
481 {
482 break;
483 }
484
485 /* 3c. If CMD_TIMEOUT bit is set then no response was received */
486 else if( mmc_status & MMC_BOOT_MCI_STAT_CMD_TIMEOUT )
487 {
488 mmc_return = MMC_BOOT_E_TIMEOUT;
489 break;
490 }
491
492 /* 3d. If CMD_RESPONSE_END bit is set to 1 then command's response was
493 received and CRC check passed
494 Spcial case for ACMD41: it seems to always fail CRC even if
495 the response is valid
496 */
497 else if (( mmc_status & MMC_BOOT_MCI_STAT_CMD_RESP_END ) || (cmd_index == CMD1_SEND_OP_COND))
498 {
499 /* 3i. Read MCI_RESP_CMD register to verify that response index is
500 equal to command index */
501 mmc_resp = readl( MMC_BOOT_MCI_RESP_CMD ) & 0x3F;
502
503 /* However, long response does not contain the command index field.
504 * In that case, response index field must be set to 111111b (0x3F) */
505 if( ( mmc_resp == cmd_index ) ||
506 ( cmd->resp_type == MMC_BOOT_RESP_R2 ||
507 cmd->resp_type == MMC_BOOT_RESP_R3 ||
508 cmd->resp_type == MMC_BOOT_RESP_R6 ) )
509 {
510 /* 3j. If resp index is equal to cmd index, read command resp
511 from MCI_RESPn registers
512 - MCI_RESP0/1/2/3 for CMD2/9/10
513 - MCI_RESP0 for all other registers */
514 if( IS_RESP_136_BITS( cmd->resp_type ) )
515 {
516 for( i = 0; i < 4; i++ )
517 {
518 cmd->resp[3-i] = readl( MMC_BOOT_MCI_RESP_0 + ( i * 4 ) );
519
520 }
521 }
522 else
523 {
524 cmd->resp[0] = readl( MMC_BOOT_MCI_RESP_0 );
525 }
526 }
527 else
528 {
529 /* command index mis-match */
530 mmc_return = MMC_BOOT_E_CMD_INDX_MISMATCH;
531 }
532
533 dprintf(INFO, "Command response received: %X\n", cmd->resp[0] );
534 break;
535 }
536
537 /* 3e. If CMD_CRC_FAIL bit is set to 1 then cmd's response was recvd,
538 but CRC check failed. */
539 else if( ( mmc_status & MMC_BOOT_MCI_STAT_CMD_CRC_FAIL ) )
540 {
541 mmc_return = MMC_BOOT_E_CRC_FAIL;
542 break;
543 }
544
545 }while(1);
546
547 return mmc_return;
548}
549
550/*
551 * Reset all the cards to idle condition (CMD 0)
552 */
553static unsigned int mmc_boot_reset_cards( void )
554{
555 struct mmc_boot_command cmd;
556
557 memset( (struct mmc_boot_command *)&cmd, 0,
558 sizeof(struct mmc_boot_command) );
559
560 cmd.cmd_index = CMD0_GO_IDLE_STATE;
561 cmd.argument = 0; // stuff bits - ignored
562 cmd.cmd_type = MMC_BOOT_CMD_BCAST;
563 cmd.resp_type = MMC_BOOT_RESP_NONE;
564
565 /* send command */
566 return mmc_boot_send_command( &cmd );
567}
568
569/*
570 * Send CMD1 to know whether the card supports host VDD profile or not.
571 */
572static unsigned int mmc_boot_send_op_cond( struct mmc_boot_host* host,
573 struct mmc_boot_card* card )
574{
575 struct mmc_boot_command cmd;
576 unsigned int mmc_resp = 0;
577 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
578
579 /* basic check */
580 if( ( host == NULL ) || ( card == NULL ) )
581 {
582 return MMC_BOOT_E_INVAL;
583 }
584
585 memset( (struct mmc_boot_command *)&cmd, 0,
586 sizeof(struct mmc_boot_command) );
587
588 /* CMD1 format:
589 * [31] Busy bit
590 * [30:29] Access mode
591 * [28:24] reserved
592 * [23:15] 2.7-3.6
593 * [14:8] 2.0-2.6
594 * [7] 1.7-1.95
595 * [6:0] reserved
596 */
597
598 cmd.cmd_index = CMD1_SEND_OP_COND;
599 cmd.argument = host->ocr;
600 cmd.cmd_type = MMC_BOOT_CMD_BCAST_W_RESP;
601 cmd.resp_type = MMC_BOOT_RESP_R3;
602
603 mmc_ret = mmc_boot_send_command( &cmd );
604 if( mmc_ret != MMC_BOOT_E_SUCCESS )
605 {
606 return mmc_ret;
607 }
608
609 /* Now it's time to examine response */
610 mmc_resp = cmd.resp[0];
611
612 /* Response contains card's ocr. Update card's information */
613 card->ocr = mmc_resp;
614
615 /* Check the response for busy status */
616 if( !( mmc_resp & MMC_BOOT_OCR_BUSY ) )
617 {
618 return MMC_BOOT_E_CARD_BUSY;
619 }
620
621 return MMC_BOOT_E_SUCCESS;
622}
623
624/*
625 * Request any card to send its uniquie card identification (CID) number (CMD2).
626 */
627static unsigned int mmc_boot_all_send_cid( struct mmc_boot_card* card )
628{
629 struct mmc_boot_command cmd;
630 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
631
632 /* basic check */
633 if( card == NULL )
634 {
635 return MMC_BOOT_E_INVAL;
636 }
637
638 memset( (struct mmc_boot_command *)&cmd, 0,
639 sizeof(struct mmc_boot_command) );
640
641 /* CMD2 Format:
642 * [31:0] stuff bits
643 */
644 cmd.cmd_index = CMD2_ALL_SEND_CID;
645 cmd.argument = 0;
646 cmd.cmd_type = MMC_BOOT_CMD_BCAST_W_RESP;
647 cmd.resp_type = MMC_BOOT_RESP_R2;
648
649 /* send command */
650 mmc_ret = mmc_boot_send_command( &cmd );
651 if( mmc_ret != MMC_BOOT_E_SUCCESS )
652 {
653 return mmc_ret;
654 }
655
656 /* Response contains card's 128 bits CID register */
657 mmc_ret = mmc_boot_decode_and_save_cid( card, cmd.resp );
658 if( mmc_ret != MMC_BOOT_E_SUCCESS )
659 {
660 return mmc_ret;
661 }
662 return MMC_BOOT_E_SUCCESS;
663}
664
665/*
666 * Ask any card to send it's relative card address (RCA).This RCA number is
667 * shorter than CID and is used by the host to address the card in future (CMD3)
668 */
669static unsigned int mmc_boot_send_relative_address( struct mmc_boot_card* card )
670{
671 struct mmc_boot_command cmd;
672 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
673
674 /* basic check */
675 if( card == NULL )
676 {
677 return MMC_BOOT_E_INVAL;
678 }
679
680 memset( (struct mmc_boot_command *)&cmd, 0,
681 sizeof(struct mmc_boot_command) );
682
683 /* CMD3 Format:
684 * [31:0] stuff bits
685 */
686 cmd.cmd_index = CMD3_SEND_RELATIVE_ADDR;
687 cmd.argument = (MMC_RCA << 16);
688 card->rca = (cmd.argument >> 16);
689 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
690 cmd.resp_type = MMC_BOOT_RESP_R1;
691
692 /* send command */
693 mmc_ret = mmc_boot_send_command( &cmd );
694 if( mmc_ret != MMC_BOOT_E_SUCCESS )
695 {
696 return mmc_ret;
697 }
698
699 return MMC_BOOT_E_SUCCESS;
700}
701
702/*
703 * Requests card to send it's CSD register's contents. (CMD9)
704 */
705static unsigned int mmc_boot_send_csd( struct mmc_boot_card* card )
706{
707 struct mmc_boot_command cmd;
708 unsigned int mmc_arg = 0;
709 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
710
711 /* basic check */
712 if( card == NULL )
713 {
714 return MMC_BOOT_E_INVAL;
715 }
716
717 memset( (struct mmc_boot_command *)&cmd, 0,
718 sizeof(struct mmc_boot_command) );
719
720 /* CMD9 Format:
721 * [31:16] RCA
722 * [15:0] stuff bits
723 */
724 mmc_arg |= card->rca << 16;
725
726 cmd.cmd_index = CMD9_SEND_CSD;
727 cmd.argument = mmc_arg;
728 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
729 cmd.resp_type = MMC_BOOT_RESP_R2;
730
731 /* send command */
732 mmc_ret = mmc_boot_send_command( &cmd );
733 if( mmc_ret != MMC_BOOT_E_SUCCESS )
734 {
735 return mmc_ret;
736 }
737
738 /* Response contains card's 128 bits CSD register */
739 /* Decode and save the register */
740 mmc_ret = mmc_boot_decode_and_save_csd( card, cmd.resp );
741 if( mmc_ret != MMC_BOOT_E_SUCCESS )
742 {
743 return mmc_ret;
744 }
745
746 return MMC_BOOT_E_SUCCESS;
747}
748
749/*
750 * Selects a card by sending CMD7 to the card with its RCA.
751 * If RCA field is set as 0 ( or any other address ),
752 * the card will be de-selected. (CMD7)
753 */
754static unsigned int mmc_boot_select_card( struct mmc_boot_card* card,
755 unsigned int rca )
756{
757 struct mmc_boot_command cmd;
758 unsigned int mmc_arg = 0;
759 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
760
761 /* basic check */
762 if( card == NULL )
763 {
764 return MMC_BOOT_E_INVAL;
765 }
766
767 memset( (struct mmc_boot_command *)&cmd, 0,
768 sizeof(struct mmc_boot_command) );
769
770 /* CMD7 Format:
771 * [31:16] RCA
772 * [15:0] stuff bits
773 */
774 mmc_arg |= rca << 16;
775
776 cmd.cmd_index = CMD7_SELECT_DESELECT_CARD;
777 cmd.argument = mmc_arg;
778 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
779 /* If we are deselecting card, we do not get response */
780 if( rca == card->rca && rca)
781 {
782 cmd.resp_type = MMC_BOOT_RESP_R1;
783 }
784 else
785 {
786 cmd.resp_type = MMC_BOOT_RESP_NONE;
787 }
788
789 /* send command */
790 mmc_ret = mmc_boot_send_command( &cmd );
791 if( mmc_ret != MMC_BOOT_E_SUCCESS )
792 {
793 return mmc_ret;
794 }
795
796 /* As of now no need to look into a response. If it's required
797 * we'll explore later on */
798
799 return MMC_BOOT_E_SUCCESS;
800}
801
802/*
803 * Send command to set block length.
804 */
805static unsigned int mmc_boot_set_block_len( struct mmc_boot_card* card,
806 unsigned int block_len )
807{
808 struct mmc_boot_command cmd;
809 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
810
811 /* basic check */
812 if( card == NULL )
813 {
814 return MMC_BOOT_E_INVAL;
815 }
816
817 memset( (struct mmc_boot_command *)&cmd, 0,
818 sizeof(struct mmc_boot_command) );
819
820 /* CMD16 Format:
821 * [31:0] block length
822 */
823
824 cmd.cmd_index = CMD16_SET_BLOCKLEN;
825 cmd.argument = block_len;
826 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
827 cmd.resp_type = MMC_BOOT_RESP_R1;
828
829 /* send command */
830 mmc_ret = mmc_boot_send_command( &cmd );
831 if( mmc_ret != MMC_BOOT_E_SUCCESS )
832 {
833 return mmc_ret;
834 }
835
836 /* If blocklength is larger than 512 bytes,
837 * the card sets BLOCK_LEN_ERROR bit. */
838 if( cmd.resp[0] & MMC_BOOT_R1_BLOCK_LEN_ERR )
839 {
840 return MMC_BOOT_E_BLOCKLEN_ERR;
841 }
842 return MMC_BOOT_E_SUCCESS;
843}
844
845/*
846 * Requests the card to stop transmission of data.
847 */
848static unsigned int mmc_boot_send_stop_transmission( struct mmc_boot_card* card,
849 unsigned int prg_enabled )
850{
851 struct mmc_boot_command cmd;
852 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
853
854 /* basic check */
855 if( card == NULL )
856 {
857 return MMC_BOOT_E_INVAL;
858 }
859
860 memset( (struct mmc_boot_command *)&cmd, 0,
861 sizeof(struct mmc_boot_command) );
862
863 /* CMD12 Format:
864 * [31:0] stuff bits
865 */
866
867 cmd.cmd_index = CMD12_STOP_TRANSMISSION;
868 cmd.argument = 0;
869 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
870 cmd.resp_type = MMC_BOOT_RESP_R1B;
871 cmd.xfer_mode = MMC_BOOT_XFER_MODE_BLOCK;
872 cmd.prg_enabled = prg_enabled;
873
874 /* send command */
875 mmc_ret = mmc_boot_send_command( &cmd );
876 if( mmc_ret != MMC_BOOT_E_SUCCESS )
877 {
878 return mmc_ret;
879 }
880 return MMC_BOOT_E_SUCCESS;
881}
882
883/*
884 * Get the card's current status
885 */
886static unsigned int mmc_boot_get_card_status( struct mmc_boot_card* card,
887 unsigned int prg_enabled )
888{
889 struct mmc_boot_command cmd;
890 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
891
892 /* basic check */
893 if( card == NULL )
894 {
895 return MMC_BOOT_E_INVAL;
896 }
897
898 memset( (struct mmc_boot_command *)&cmd, 0,
899 sizeof(struct mmc_boot_command) );
900
901 /* CMD13 Format:
902 * [31:16] RCA
903 * [15:0] stuff bits
904 */
905 cmd.cmd_index = CMD13_SEND_STATUS;
906 cmd.argument = card->rca << 16;
907 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
908 cmd.resp_type = MMC_BOOT_RESP_R1;
909 cmd.prg_enabled = prg_enabled;
910
911 /* send command */
912 mmc_ret = mmc_boot_send_command( &cmd );
913 if( mmc_ret != MMC_BOOT_E_SUCCESS )
914 {
915 return mmc_ret;
916 }
917
918 return MMC_BOOT_E_SUCCESS;
919}
920/*
921 * Send ext csd command.
922 */
923static unsigned int mmc_boot_send_ext_cmd (struct mmc_boot_card* card)
924{
925 struct mmc_boot_command cmd;
926 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
927 unsigned int mmc_reg = 0;
928 unsigned char buf[512];
929 unsigned int mmc_status = 0;
930 unsigned int* mmc_ptr = (unsigned int *)buf;
931 unsigned int mmc_count = 0;
932
933 // start from the back
934 mmc_ptr += ( (512/sizeof(int)) - 1 );
935
936 /* basic check */
937 if( card == NULL )
938 {
939 return MMC_BOOT_E_INVAL;
940 }
941
942 /* set block len */
943 mmc_ret = mmc_boot_set_block_len( card, 512);
944 if( mmc_ret != MMC_BOOT_E_SUCCESS )
945 {
946 dprintf(CRITICAL, "Error No.%d: Failure setting block length for Card (RCA:%s)\n",
947 mmc_ret, (char *)(card->rca) );
948 return mmc_ret;
949 }
950
951 /* Set the FLOW_ENA bit of MCI_CLK register to 1 */
952 mmc_reg = readl( MMC_BOOT_MCI_CLK );
953 mmc_reg |= MMC_BOOT_MCI_CLK_ENA_FLOW ;
954 writel( mmc_reg, MMC_BOOT_MCI_CLK );
955
956 /* Write data timeout period to MCI_DATA_TIMER register. */
957 /* Data timeout period should be in card bus clock periods */
958 mmc_reg =0xFFFFFFFF;
959 writel( mmc_reg, MMC_BOOT_MCI_DATA_TIMER );
960 writel( 512, MMC_BOOT_MCI_DATA_LENGTH );
961
962 /* Set appropriate fields and write the MCI_DATA_CTL register. */
963 /* Set ENABLE bit to 1 to enable the data transfer. */
964 mmc_reg = MMC_BOOT_MCI_DATA_ENABLE | MMC_BOOT_MCI_DATA_DIR | (512 << MMC_BOOT_MCI_BLKSIZE_POS);
965 writel( mmc_reg, MMC_BOOT_MCI_DATA_CTL );
966
967 memset( (struct mmc_boot_command *)&cmd, 0,
968 sizeof(struct mmc_boot_command) );
969 /* CMD8 */
970 cmd.cmd_index = CMD8_SEND_EXT_CSD;
971 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
972 cmd.resp_type = MMC_BOOT_RESP_R1;
973 cmd.xfer_mode = MMC_BOOT_XFER_MODE_BLOCK;
974
975 /* send command */
976 mmc_ret = mmc_boot_send_command( &cmd );
977 if( mmc_ret != MMC_BOOT_E_SUCCESS )
978 {
979 return mmc_ret;
980 }
981
982 do
983 {
984 mmc_ret = MMC_BOOT_E_SUCCESS;
985 mmc_status = readl( MMC_BOOT_MCI_STATUS );
986
987 /* If DATA_CRC_FAIL bit is set to 1 then CRC error was detected by
988 card/device during the data transfer */
989 if( mmc_status & MMC_BOOT_MCI_STAT_DATA_CRC_FAIL )
990 {
991 mmc_ret = MMC_BOOT_E_DATA_CRC_FAIL;
992 break;
993 }
994 /* If DATA_TIMEOUT bit is set to 1 then the data transfer time exceeded
995 the data timeout period without completing the transfer */
996 else if( mmc_status & MMC_BOOT_MCI_STAT_DATA_TIMEOUT )
997 {
998 mmc_ret = MMC_BOOT_E_DATA_TIMEOUT;
999 break;
1000 }
1001 /* If RX_OVERRUN bit is set to 1 then SDCC2 tried to receive data from
1002 the card before empty storage for new received data was available.
1003 Verify that bit FLOW_ENA in MCI_CLK is set to 1 during the data xfer.*/
1004 else if( mmc_status & MMC_BOOT_MCI_STAT_RX_OVRRUN )
1005 {
1006 /* Note: We've set FLOW_ENA bit in MCI_CLK to 1. so no need to verify
1007 for now */
1008 mmc_ret = MMC_BOOT_E_RX_OVRRUN;
1009 break;
1010 }
1011
1012 if( mmc_status & MMC_BOOT_MCI_STAT_RX_DATA_AVLBL )
1013 {
1014 /* FIFO contains 16 32-bit data buffer on 16 sequential addresses*/
1015 *mmc_ptr = SWAP_ENDIAN(readl( MMC_BOOT_MCI_FIFO +
1016 ( mmc_count % MMC_BOOT_MCI_FIFO_SIZE ) ));
1017 mmc_ptr--;
1018
1019 /* increase mmc_count by word size */
1020 mmc_count += sizeof( unsigned int );
1021
1022 /* quit if we have read enough of data */
1023 if (mmc_count >= 512)
1024 break;
1025 }
1026 else if( mmc_status & MMC_BOOT_MCI_STAT_DATA_END )
1027 {
1028 break;
1029 }
1030
1031 }while(1);
1032
1033 return MMC_BOOT_E_SUCCESS;
1034
1035}
1036
1037
1038
1039/*
1040 * Switch command
1041 */
1042static unsigned int mmc_boot_switch_cmd (struct mmc_boot_card* card,
1043 unsigned access,
1044 unsigned index,
1045 unsigned value)
1046{
1047
1048 struct mmc_boot_command cmd;
1049 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
1050
1051 /* basic check */
1052 if( card == NULL )
1053 {
1054 return MMC_BOOT_E_INVAL;
1055 }
1056
1057 memset( (struct mmc_boot_command *)&cmd, 0,
1058 sizeof(struct mmc_boot_command) );
1059
1060 /* CMD6 Format:
1061 * [31:26] set to 0
1062 * [25:24] access
1063 * [23:16] index
1064 * [15:8] value
1065 * [7:3] set to 0
1066 * [2:0] cmd set
1067 */
1068 cmd.cmd_index = CMD6_SWITCH_FUNC;
1069 cmd.argument |= (access << 24);
1070 cmd.argument |= (index << 16);
1071 cmd.argument |= (value << 8);
1072 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
1073 cmd.resp_type = MMC_BOOT_RESP_R1B;
1074
1075 mmc_ret = mmc_boot_send_command( &cmd );
1076 if( mmc_ret != MMC_BOOT_E_SUCCESS )
1077 {
1078 return mmc_ret;
1079 }
1080
1081 return MMC_BOOT_E_SUCCESS;
1082}
1083
1084/*
1085 * A command to set the data bus width for card. Set width to either
1086 */
1087static unsigned int mmc_boot_set_bus_width( struct mmc_boot_card* card,
1088 unsigned int width )
1089{
1090 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
1091 unsigned int mmc_reg = 0;
1092 unsigned int mmc_width = 0;
1093
1094 if( width != MMC_BOOT_BUS_WIDTH_1_BIT)
1095 {
1096 mmc_width = width-1;
1097 }
1098
1099 mmc_ret = mmc_boot_switch_cmd(card, MMC_BOOT_ACCESS_WRITE, MMC_BOOT_EXT_CMMC_BUS_WIDTH, mmc_width);
1100
1101 if( mmc_ret != MMC_BOOT_E_SUCCESS )
1102 {
1103 return mmc_ret;
1104 }
1105
1106 /* set MCI_CLK accordingly */
1107 mmc_reg = readl( MMC_BOOT_MCI_CLK );
1108 mmc_reg &= ~MMC_BOOT_MCI_CLK_WIDEBUS_MODE;
1109 if ( width == MMC_BOOT_BUS_WIDTH_1_BIT )
1110 {
1111 mmc_reg |= MMC_BOOT_MCI_CLK_WIDEBUS_1_BIT;
1112 }
1113 else if (width == MMC_BOOT_BUS_WIDTH_4_BIT )
1114 {
1115 mmc_reg |= MMC_BOOT_MCI_CLK_WIDEBUS_4_BIT;
1116 }
1117 else if (width == MMC_BOOT_BUS_WIDTH_8_BIT )
1118 {
1119 mmc_reg |= MMC_BOOT_MCI_CLK_WIDEBUS_8_BIT;
1120 }
1121 writel( mmc_reg, MMC_BOOT_MCI_CLK );
Shashank Mittal23b8f422010-04-16 19:27:21 -07001122
1123 mdelay(10); // Giving some time to card to stabilize.
1124
Shashank Mittal52525ff2010-04-13 11:11:10 -07001125 return MMC_BOOT_E_SUCCESS;
1126}
1127
1128
1129/*
1130 * A command to start data read from card. Either a single block or
1131 * multiple blocks can be read. Multiple blocks read will continuously
1132 * transfer data from card to host unless requested to stop by issuing
1133 * CMD12 - STOP_TRANSMISSION.
1134 */
1135static unsigned int mmc_boot_send_read_command( struct mmc_boot_card* card,
1136 unsigned int xfer_type,
1137 unsigned int data_addr )
1138{
1139 struct mmc_boot_command cmd;
1140 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
1141
1142 /* basic check */
1143 if( card == NULL )
1144 {
1145 return MMC_BOOT_E_INVAL;
1146 }
1147
1148 memset( (struct mmc_boot_command *)&cmd, 0,
1149 sizeof(struct mmc_boot_command) );
1150
1151 /* CMD17/18 Format:
1152 * [31:0] Data Address
1153 */
1154 if( xfer_type == MMC_BOOT_XFER_MULTI_BLOCK )
1155 {
1156 cmd.cmd_index = CMD18_READ_MULTIPLE_BLOCK;
1157 }
1158 else
1159 {
1160 cmd.cmd_index = CMD17_READ_SINGLE_BLOCK;
1161 }
1162
1163 cmd.argument = data_addr;
1164 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
1165 cmd.resp_type = MMC_BOOT_RESP_R1;
1166
1167 /* send command */
1168 mmc_ret = mmc_boot_send_command( &cmd );
1169 if( mmc_ret != MMC_BOOT_E_SUCCESS )
1170 {
1171 return mmc_ret;
1172 }
1173
1174 /* Response contains 32 bit Card status. Here we'll check
1175 BLOCK_LEN_ERROR and ADDRESS_ERROR */
1176 if( cmd.resp[0] & MMC_BOOT_R1_BLOCK_LEN_ERR )
1177 {
1178 return MMC_BOOT_E_BLOCKLEN_ERR;
1179 }
1180 /* Misaligned address not matching block length */
1181 if( cmd.resp[0] & MMC_BOOT_R1_ADDR_ERR )
1182 {
1183 return MMC_BOOT_E_ADDRESS_ERR;
1184 }
1185
1186 return MMC_BOOT_E_SUCCESS;
1187}
1188
1189/*
1190 * A command to start data write to card. Either a single block or
1191 * multiple blocks can be written. Multiple block write will continuously
1192 * transfer data from host to card unless requested to stop by issuing
1193 * CMD12 - STOP_TRANSMISSION.
1194 */
1195static unsigned int mmc_boot_send_write_command( struct mmc_boot_card* card,
1196 unsigned int xfer_type,
1197 unsigned int data_addr )
1198{
1199 struct mmc_boot_command cmd;
1200 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
1201
1202 /* basic check */
1203 if( card == NULL )
1204 {
1205 return MMC_BOOT_E_INVAL;
1206 }
1207
1208 memset( (struct mmc_boot_command *)&cmd, 0,
1209 sizeof(struct mmc_boot_command) );
1210
1211 /* CMD24/25 Format:
1212 * [31:0] Data Address
1213 */
1214 if( xfer_type == MMC_BOOT_XFER_MULTI_BLOCK )
1215 {
1216 cmd.cmd_index = CMD25_WRITE_MULTIPLE_BLOCK;
1217 }
1218 else
1219 {
1220 cmd.cmd_index = CMD24_WRITE_SINGLE_BLOCK;
1221 }
1222
1223 cmd.argument = data_addr;
1224 cmd.cmd_type = MMC_BOOT_CMD_ADDRESS;
1225 cmd.resp_type = MMC_BOOT_RESP_R1;
1226
1227 /* send command */
1228 mmc_ret = mmc_boot_send_command( &cmd );
1229 if( mmc_ret != MMC_BOOT_E_SUCCESS )
1230 {
1231 return mmc_ret;
1232 }
1233
1234 /* Response contains 32 bit Card status. Here we'll check
1235 BLOCK_LEN_ERROR and ADDRESS_ERROR */
1236 if( cmd.resp[0] & MMC_BOOT_R1_BLOCK_LEN_ERR )
1237 {
1238 return MMC_BOOT_E_BLOCKLEN_ERR;
1239 }
1240 /* Misaligned address not matching block length */
1241 if( cmd.resp[0] & MMC_BOOT_R1_ADDR_ERR )
1242 {
1243 return MMC_BOOT_E_ADDRESS_ERR;
1244 }
1245
1246 return MMC_BOOT_E_SUCCESS;
1247}
1248
Shashank Mittal7afbf282010-06-02 19:48:31 -07001249/*
1250 * Decode type of error caused during read and write
1251 */
1252static unsigned int mmc_boot_status_error(unsigned mmc_status)
1253{
1254 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
1255
1256 /* If DATA_CRC_FAIL bit is set to 1 then CRC error was detected by
1257 card/device during the data transfer */
1258 if( mmc_status & MMC_BOOT_MCI_STAT_DATA_CRC_FAIL )
1259 {
1260 mmc_ret = MMC_BOOT_E_DATA_CRC_FAIL;
1261 }
1262 /* If DATA_TIMEOUT bit is set to 1 then the data transfer time exceeded
1263 the data timeout period without completing the transfer */
1264 else if( mmc_status & MMC_BOOT_MCI_STAT_DATA_TIMEOUT )
1265 {
1266 mmc_ret = MMC_BOOT_E_DATA_TIMEOUT;
1267 }
1268 /* If RX_OVERRUN bit is set to 1 then SDCC2 tried to receive data from
1269 the card before empty storage for new received data was available.
1270 Verify that bit FLOW_ENA in MCI_CLK is set to 1 during the data xfer.*/
1271 else if( mmc_status & MMC_BOOT_MCI_STAT_RX_OVRRUN )
1272 {
1273 /* Note: We've set FLOW_ENA bit in MCI_CLK to 1. so no need to verify
1274 for now */
1275 mmc_ret = MMC_BOOT_E_RX_OVRRUN;
1276 }
1277 /* If TX_UNDERRUN bit is set to 1 then SDCC2 tried to send data to
1278 the card before new data for sending was available. Verify that bit
1279 FLOW_ENA in MCI_CLK is set to 1 during the data xfer.*/
1280 else if( mmc_status & MMC_BOOT_MCI_STAT_TX_UNDRUN )
1281 {
1282 /* Note: We've set FLOW_ENA bit in MCI_CLK to 1.so skipping it now*/
1283 mmc_ret = MMC_BOOT_E_RX_OVRRUN;
1284 }
1285 return mmc_ret;
1286}
Shashank Mittal52525ff2010-04-13 11:11:10 -07001287
1288/*
1289 * Write data_len data to address specified by data_addr. data_len is
1290 * multiple of blocks for block data transfer.
1291 */
1292static unsigned int mmc_boot_write_to_card( struct mmc_boot_host* host,
1293 struct mmc_boot_card* card,
1294 unsigned long long data_addr,
1295 unsigned int data_len,
1296 unsigned int* in )
1297{
1298 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
1299 unsigned int mmc_status = 0;
1300 unsigned int* mmc_ptr = in;
1301 unsigned int mmc_count = 0;
1302 unsigned int mmc_reg = 0;
1303 unsigned int addr;
1304 unsigned int xfer_type;
Shashank Mittal7afbf282010-06-02 19:48:31 -07001305 unsigned int write_error;
Shashank Mittal52525ff2010-04-13 11:11:10 -07001306
1307 if( ( host == NULL ) || ( card == NULL ) )
1308 {
1309 return MMC_BOOT_E_INVAL;
1310 }
1311
1312 /* Set block length. High Capacity MMC card uses fixed 512 bytes block
1313 length. So no need to send CMD16. */
1314 if( card->type != MMC_BOOT_TYPE_SDHC )
1315 {
1316 mmc_ret = mmc_boot_set_block_len( card, card->wr_block_len );
1317 if( mmc_ret != MMC_BOOT_E_SUCCESS )
1318 {
1319 dprintf(CRITICAL, "Error No.%d: Failure setting block length for Card\
1320 (RCA:%s)\n", mmc_ret, (char *)(card->rca) );
1321 return mmc_ret;
1322 }
1323 }
1324
1325 /* use multi-block mode to transfer for data larger than a block */
1326 xfer_type = (data_len > card->rd_block_len) ? MMC_BOOT_XFER_MULTI_BLOCK :
1327 MMC_BOOT_XFER_SINGLE_BLOCK;
1328
1329 /* For SDHC data address is specified in unit of 512B */
1330 addr = ( card->type != MMC_BOOT_TYPE_SDHC ) ? (unsigned int) data_addr :
1331 (unsigned int) (data_addr / 512);
1332
1333 /* Set the FLOW_ENA bit of MCI_CLK register to 1 */
1334 mmc_reg = readl( MMC_BOOT_MCI_CLK );
1335 mmc_reg |= MMC_BOOT_MCI_CLK_ENA_FLOW ;
1336 writel( mmc_reg, MMC_BOOT_MCI_CLK );
1337
1338 /* Write data timeout period to MCI_DATA_TIMER register */
1339 /* Data timeout period should be in card bus clock periods */
1340 /*TODO: Fix timeout value*/
1341 mmc_reg = 0xFFFFFFFF;
1342 writel( mmc_reg, MMC_BOOT_MCI_DATA_TIMER );
1343
1344 /* Write the total size of the transfer data to MCI_DATA_LENGTH register */
1345 writel( data_len, MMC_BOOT_MCI_DATA_LENGTH );
1346
1347 /* Send command to the card/device in order to start the write data xfer.
1348 The possible commands are CMD24/25/53/60/61 */
1349 mmc_ret = mmc_boot_send_write_command( card, xfer_type, addr );
1350 if( mmc_ret != MMC_BOOT_E_SUCCESS )
1351 {
1352 dprintf(CRITICAL, "Error No.%d: Failure sending write command to the\
1353 Card(RCA:%x)\n", mmc_ret, card->rca );
1354 return mmc_ret;
1355 }
1356
1357 /* Set appropriate fields and write the MCI_DATA_CTL register */
1358 /* Set ENABLE bit to 1 to enable the data transfer. */
1359 mmc_reg = 0;
1360 mmc_reg |= MMC_BOOT_MCI_DATA_ENABLE;
1361 /* Clear DIRECTION bit to 0 to enable transfer from host to card */
1362 /* Clear MODE bit to 0 to enable block oriented data transfer. For
1363 MMC cards only, if stream data transfer mode is desired, set
1364 MODE bit to 1. */
1365 /* Set DM_ENABLE bit to 1 in order to enable DMA, otherwise set 0 */
1366 /* Write size of block to be used during the data transfer to
1367 BLOCKSIZE field */
1368 mmc_reg |= card->wr_block_len << MMC_BOOT_MCI_BLKSIZE_POS;
1369 writel( mmc_reg, MMC_BOOT_MCI_DATA_CTL );
1370
Shashank Mittal7afbf282010-06-02 19:48:31 -07001371 write_error = MMC_BOOT_MCI_STAT_DATA_CRC_FAIL | \
1372 MMC_BOOT_MCI_STAT_DATA_TIMEOUT | \
1373 MMC_BOOT_MCI_STAT_TX_UNDRUN;
1374
Shashank Mittal52525ff2010-04-13 11:11:10 -07001375 /* Write the transfer data to SDCC3 FIFO */
1376 /* If Data Mover is used for data transfer, prepare a command list entry
1377 and enable the Data Mover to work with SDCC2 */
1378 /* If Data Mover is NOT used for data xfer: */
1379 do
1380 {
1381 mmc_ret = MMC_BOOT_E_SUCCESS;
1382 mmc_status = readl( MMC_BOOT_MCI_STATUS );
1383
Shashank Mittal7afbf282010-06-02 19:48:31 -07001384 if( mmc_status & write_error )
Shashank Mittal52525ff2010-04-13 11:11:10 -07001385 {
Shashank Mittal7afbf282010-06-02 19:48:31 -07001386 mmc_ret = mmc_boot_status_error(mmc_status);
Shashank Mittal52525ff2010-04-13 11:11:10 -07001387 break;
1388 }
Shashank Mittal7afbf282010-06-02 19:48:31 -07001389
Shashank Mittal52525ff2010-04-13 11:11:10 -07001390 /* Write the data in MCI_FIFO register as long as TXFIFO_FULL bit of
1391 MCI_STATUS register is 0. Continue the writes until the whole
1392 transfer data is written. */
Shashank Mittal7afbf282010-06-02 19:48:31 -07001393 if (((data_len-mmc_count) >= MMC_BOOT_MCI_FIFO_SIZE/2) &&
1394 ( mmc_status & MMC_BOOT_MCI_STAT_TX_FIFO_HFULL ))
1395 {
1396 for (int i=0; i < MMC_BOOT_MCI_HFIFO_COUNT; i++ )
1397 {
1398 /* FIFO contains 16 32-bit data buffer on 16 sequential addresses*/
1399 writel( *mmc_ptr, MMC_BOOT_MCI_FIFO +
1400 ( mmc_count % MMC_BOOT_MCI_FIFO_SIZE ) );
1401 mmc_ptr++;
1402 /* increase mmc_count by word size */
1403 mmc_count += sizeof( unsigned int );
1404 }
1405
1406 }
1407 else if( !( mmc_status & MMC_BOOT_MCI_STAT_TX_FIFO_FULL ) && (mmc_count != data_len))
Shashank Mittal52525ff2010-04-13 11:11:10 -07001408 {
1409 /* FIFO contains 16 32-bit data buffer on 16 sequential addresses*/
1410 writel( *mmc_ptr, MMC_BOOT_MCI_FIFO +
1411 ( mmc_count % MMC_BOOT_MCI_FIFO_SIZE ) );
1412 mmc_ptr++;
1413 /* increase mmc_count by word size */
1414 mmc_count += sizeof( unsigned int );
1415 }
1416 else if((mmc_status & MMC_BOOT_MCI_STAT_DATA_END))
1417 {
1418 break; //success
1419 }
1420
1421 } while(1);
1422
1423 if( mmc_ret != MMC_BOOT_E_SUCCESS )
1424 {
1425 dprintf(CRITICAL, "Error No.%d: Failure on data transfer from the \
1426 Card(RCA:%x)\n", mmc_ret, card->rca );
1427 return mmc_ret;
1428 }
1429
1430 /* Send command to the card/device in order to poll the de-assertion of
1431 card/device BUSY condition. It is important to set PROG_ENA bit in
1432 MCI_CLK register before sending the command. Possible commands are
1433 CMD12/13. */
1434 if( xfer_type == MMC_BOOT_XFER_MULTI_BLOCK )
1435 {
1436 mmc_ret = mmc_boot_send_stop_transmission( card, 1 );
1437 if( mmc_ret != MMC_BOOT_E_SUCCESS )
1438 {
1439 dprintf(CRITICAL, "Error No.%d: Failure sending Stop Transmission \
1440 command to the Card(RCA:%x)\n", mmc_ret, card->rca );
1441 return mmc_ret;
1442 }
1443 }
1444 else
1445 {
1446 mmc_ret = mmc_boot_get_card_status( card, 1 );
1447 if( mmc_ret != MMC_BOOT_E_SUCCESS )
1448 {
1449 dprintf(CRITICAL, "Error No.%d: Failure getting card status of Card(RCA:%x)\n",
1450 mmc_ret, card->rca );
1451 return mmc_ret;
1452 }
1453 }
1454
1455 /* Wait for interrupt or poll on PROG_DONE bit of MCI_STATUS register. If
1456 PROG_DONE bit is set to 1 it means that the card finished it programming
1457 and stopped driving DAT0 line to 0 */
Shashank Mittal52525ff2010-04-13 11:11:10 -07001458 do
1459 {
1460 mmc_status = readl( MMC_BOOT_MCI_STATUS );
1461 if( mmc_status & MMC_BOOT_MCI_STAT_PROG_DONE )
1462 {
1463 break;
1464 }
1465 } while(1);
1466
1467 return MMC_BOOT_E_SUCCESS;
1468}
1469
1470
1471/*
1472 * Adjust the interface speed to optimal speed
1473 */
1474static unsigned int mmc_boot_adjust_interface_speed( struct mmc_boot_host* host,
1475 struct mmc_boot_card* card )
1476{
1477 int mmc_ret;
1478
1479 mmc_boot_send_ext_cmd (card);
1480
1481 /* Setting HS_TIMING in EXT_CSD (CMD6) */
1482 mmc_ret = mmc_boot_switch_cmd(card, MMC_BOOT_ACCESS_WRITE, MMC_BOOT_EXT_CMMC_HS_TIMING, 1);
1483
1484 mmc_boot_send_ext_cmd (card);
1485 if(mmc_ret!= MMC_BOOT_E_SUCCESS)
1486 {
1487 return mmc_ret;
1488 }
1489#ifdef PLATFORM_MSM8X60
Shashank Mittal7afbf282010-06-02 19:48:31 -07001490 mmc_ret = mmc_boot_enable_clock( host, MMC_CLK_48MHZ);
Shashank Mittal52525ff2010-04-13 11:11:10 -07001491#else
1492 mmc_ret = mmc_boot_enable_clock( host, MMC_CLK_50MHZ);
1493#endif
1494 if( mmc_ret != MMC_BOOT_E_SUCCESS )
1495 {
1496 return MMC_BOOT_E_CLK_ENABLE_FAIL;
1497 }
1498 return MMC_BOOT_E_SUCCESS;
1499}
1500
1501/*
1502 * Reads a data of data_len from the address specified. data_len
1503 * should be multiple of block size for block data transfer.
1504 */
1505static unsigned int mmc_boot_read_from_card( struct mmc_boot_host* host,
1506 struct mmc_boot_card* card,
1507 unsigned long long data_addr,
1508 unsigned int data_len,
1509 unsigned int* out )
1510{
1511 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
1512 unsigned int mmc_status = 0;
1513 unsigned int* mmc_ptr = out;
1514 unsigned int mmc_count = 0;
1515 unsigned int mmc_reg = 0;
1516 unsigned int xfer_type;
1517 unsigned int addr = 0;
Shashank Mittal7afbf282010-06-02 19:48:31 -07001518 unsigned int read_error;
1519
Shashank Mittal52525ff2010-04-13 11:11:10 -07001520 if( ( host == NULL ) || ( card == NULL ) )
1521 {
1522 return MMC_BOOT_E_INVAL;
1523 }
1524
1525 /* Set block length. High Capacity MMC card uses fixed 512 bytes block
1526 length. So no need to send CMD16. */
1527 if( card->type != MMC_BOOT_TYPE_SDHC )
1528 {
1529 mmc_ret = mmc_boot_set_block_len( card, card->rd_block_len );
1530 if( mmc_ret != MMC_BOOT_E_SUCCESS )
1531 {
1532 dprintf(CRITICAL, "Error No.%d: Failure setting block length for Card (RCA:%s)\n",
1533 mmc_ret, (char *)(card->rca) );
1534 return mmc_ret;
1535 }
1536 }
1537
1538 /* use multi-block mode to transfer for data larger than a block */
1539 xfer_type = (data_len > card->rd_block_len) ? MMC_BOOT_XFER_MULTI_BLOCK :
1540 MMC_BOOT_XFER_SINGLE_BLOCK;
1541
1542 /* Set the FLOW_ENA bit of MCI_CLK register to 1 */
1543 /* Note: It's already enabled */
1544
1545 /* If Data Mover is used for data transfer then prepare Command
1546 List Entry and enable the Data mover to work with SDCC2 */
1547 /* Note: Data Mover not used */
1548
1549 /* Write data timeout period to MCI_DATA_TIMER register. */
1550 /* Data timeout period should be in card bus clock periods */
1551 mmc_reg = (unsigned long)(card->rd_timeout_ns / 1000000) *
1552 (host->mclk_rate / 1000);
1553 mmc_reg += 1000; // add some extra clock cycles to be safe
1554 mmc_reg = mmc_reg/2;
1555 writel( mmc_reg, MMC_BOOT_MCI_DATA_TIMER );
1556
1557 /* Write the total size of the transfer data to MCI_DATA_LENGTH
1558 register. For block xfer it must be multiple of the block
1559 size. */
1560 writel( data_len, MMC_BOOT_MCI_DATA_LENGTH );
1561
1562 /* For SDHC data address is specified in unit of 512B */
1563 addr = ( card->type != MMC_BOOT_TYPE_SDHC ) ? (unsigned int) data_addr :
1564 (unsigned int) (data_addr / 512);
1565
1566 /* Set appropriate fields and write the MCI_DATA_CTL register. */
1567 /* Set ENABLE bit to 1 to enable the data transfer. */
1568 mmc_reg = 0;
1569 mmc_reg |= MMC_BOOT_MCI_DATA_ENABLE;
1570 /* Clear DIRECTION bit to 1 to enable transfer from card to host */
1571 mmc_reg |= MMC_BOOT_MCI_DATA_DIR;
1572 /* Clear MODE bit to 0 to enable block oriented data transfer. For
1573 MMC cards only, if stream data transfer mode is desired, set
1574 MODE bit to 1. */
1575 /* Set DM_ENABLE bit to 1 in order to enable DMA, otherwise set 0 */
1576 /* Write size of block to be used during the data transfer to
1577 BLOCKSIZE field */
1578 mmc_reg |= (card->rd_block_len << MMC_BOOT_MCI_BLKSIZE_POS);
1579 writel( mmc_reg, MMC_BOOT_MCI_DATA_CTL );
1580
1581 /* Send command to the card/device in order to start the read data
1582 transfer. Possible commands: CMD17/18/53/60/61. */
1583 mmc_ret = mmc_boot_send_read_command( card, xfer_type, addr );
1584 if( mmc_ret != MMC_BOOT_E_SUCCESS )
1585 {
1586 dprintf(CRITICAL, "Error No.%d: Failure sending read command to the Card(RCA:%x)\n",
1587 mmc_ret, card->rca );
1588 return mmc_ret;
1589 }
1590
Shashank Mittal7afbf282010-06-02 19:48:31 -07001591 read_error = MMC_BOOT_MCI_STAT_DATA_CRC_FAIL | \
1592 MMC_BOOT_MCI_STAT_DATA_TIMEOUT | \
1593 MMC_BOOT_MCI_STAT_RX_OVRRUN;
1594
Shashank Mittal52525ff2010-04-13 11:11:10 -07001595 /* Read the transfer data from SDCC2 FIFO. If Data Mover is not used
1596 read the data from the MCI_FIFO register as long as RXDATA_AVLBL
1597 bit of MCI_STATUS register is set to 1 and bits DATA_CRC_FAIL,
1598 DATA_TIMEOUT, RX_OVERRUN of MCI_STATUS register are cleared to 0.
1599 Continue the reads until the whole transfer data is received */
1600
1601 do
1602 {
1603 mmc_ret = MMC_BOOT_E_SUCCESS;
1604 mmc_status = readl( MMC_BOOT_MCI_STATUS );
1605
Shashank Mittal7afbf282010-06-02 19:48:31 -07001606 if( mmc_status & read_error )
Shashank Mittal52525ff2010-04-13 11:11:10 -07001607 {
Shashank Mittal7afbf282010-06-02 19:48:31 -07001608 mmc_ret = mmc_boot_status_error(mmc_status);
Shashank Mittal52525ff2010-04-13 11:11:10 -07001609 break;
1610 }
1611
1612 if( mmc_status & MMC_BOOT_MCI_STAT_RX_DATA_AVLBL )
1613 {
Shashank Mittal7afbf282010-06-02 19:48:31 -07001614 unsigned read_count = 1;
1615 if ( mmc_status & MMC_BOOT_MCI_STAT_RX_FIFO_HFULL)
1616 {
1617 read_count = MMC_BOOT_MCI_HFIFO_COUNT;
1618 }
Shashank Mittal52525ff2010-04-13 11:11:10 -07001619
Shashank Mittal7afbf282010-06-02 19:48:31 -07001620 for (int i=0; i<read_count; i++)
1621 {
1622 /* FIFO contains 16 32-bit data buffer on 16 sequential addresses*/
1623 *mmc_ptr = readl( MMC_BOOT_MCI_FIFO +
1624 ( mmc_count % MMC_BOOT_MCI_FIFO_SIZE ) );
1625 mmc_ptr++;
1626 /* increase mmc_count by word size */
1627 mmc_count += sizeof( unsigned int );
1628 }
Shashank Mittal52525ff2010-04-13 11:11:10 -07001629 /* quit if we have read enough of data */
1630 if (mmc_count == data_len)
1631 break;
1632 }
1633 else if( mmc_status & MMC_BOOT_MCI_STAT_DATA_END )
1634 {
1635 break;
1636 }
1637 }while(1);
1638
1639 if( mmc_ret != MMC_BOOT_E_SUCCESS )
1640 {
1641 dprintf(CRITICAL, "Error No.%d: Failure on data transfer from the \
1642 Card(RCA:%x)\n", mmc_ret, card->rca );
1643 return mmc_ret;
1644 }
1645
1646 /* In case a multiple block transfer was performed, send CMD12 to the
1647 card/device in order to indicate the end of read data transfer */
1648 if( xfer_type == MMC_BOOT_XFER_MULTI_BLOCK )
1649 {
1650 mmc_ret = mmc_boot_send_stop_transmission( card, 0 );
1651 if( mmc_ret != MMC_BOOT_E_SUCCESS )
1652 {
1653 dprintf(CRITICAL, "Error No.%d: Failure sending Stop Transmission \
1654 command to the Card(RCA:%x)\n", mmc_ret, card->rca );
1655 return mmc_ret;
1656 }
1657 }
1658
1659 return MMC_BOOT_E_SUCCESS;
1660}
1661
1662/*
1663 * Initialize host structure, set and enable clock-rate and power mode.
1664 */
1665unsigned int mmc_boot_init( struct mmc_boot_host* host )
1666{
1667 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
1668 unsigned int mmc_pwr = 0;
1669
1670
1671 host->ocr = MMC_BOOT_OCR_27_36 | MMC_BOOT_OCR_SEC_MODE;
1672 host->cmd_retry = MMC_BOOT_MAX_COMMAND_RETRY;
1673 host->clk_enabled = 0;
1674
1675 /* clock frequency should be less than 400KHz in identification mode */
1676 mmc_ret = mmc_boot_enable_clock( host, MMC_CLK_400KHZ);
1677
1678 if( mmc_ret != MMC_BOOT_E_SUCCESS )
1679 {
1680 return MMC_BOOT_E_CLK_ENABLE_FAIL;
1681 }
1682
1683 /* set power mode*/
1684 /* give some time to reach minimum voltate */
1685 mdelay(2);
1686 mmc_pwr &= ~MMC_BOOT_MCI_PWR_UP;
1687 mmc_pwr |= MMC_BOOT_MCI_PWR_ON;
1688 mmc_pwr |= MMC_BOOT_MCI_PWR_UP;
1689 writel( mmc_pwr, MMC_BOOT_MCI_POWER );
1690 /* some more time to stabilize voltage */
1691 mdelay(2);
1692
1693 return MMC_BOOT_E_SUCCESS;
1694}
1695
1696/*
1697 * Performs card identification process by getting card's unique identification
1698 * number (CID) and relative card address (RCA). After that card will be in
1699 * stand-by state.
1700 */
1701static unsigned int mmc_boot_identify_card( struct mmc_boot_host* host,
1702 struct mmc_boot_card* card)
1703{
1704 unsigned int mmc_return = MMC_BOOT_E_SUCCESS;
1705
1706 /* basic check */
1707 if( ( host == NULL ) || ( card == NULL ) )
1708 {
1709 return MMC_BOOT_E_INVAL;
1710 }
1711
1712 /* Ask card to send its unique card identification (CID) number (CMD2) */
1713 mmc_return = mmc_boot_all_send_cid( card );
1714 if( mmc_return != MMC_BOOT_E_SUCCESS )
1715 {
1716 dprintf(CRITICAL, "Error No. %d: Failure getting card's CID number!\n",
1717 mmc_return );
1718 return mmc_return;
1719 }
1720
1721 /* Ask card to send a relative card address (RCA) (CMD3) */
1722 mmc_return = mmc_boot_send_relative_address( card );
1723 if( mmc_return != MMC_BOOT_E_SUCCESS )
1724 {
1725 dprintf(CRITICAL, "Error No. %d: Failure getting card's RCA!\n",
1726 mmc_return );
1727 return mmc_return;
1728 }
1729
1730 /* Set the card status as active */
1731 card->status = MMC_BOOT_STATUS_ACTIVE;
1732
1733 /* Get card's CSD register (CMD9) */
1734 mmc_return = mmc_boot_send_csd( card );
1735 if( mmc_return != MMC_BOOT_E_SUCCESS )
1736 {
1737 dprintf(CRITICAL, "Error No.%d: Failure getting card's CSD information!\n",
1738 mmc_return );
1739 return mmc_return;
1740 }
1741
1742 /* Once CSD is received, set read and write timeout value now itself */
1743 mmc_return = mmc_boot_set_read_timeout( host, card );
1744 if( mmc_return != MMC_BOOT_E_SUCCESS )
1745 {
1746 dprintf(CRITICAL, "Error No.%d: Failure setting Read Timeout value!\n",
1747 mmc_return );
1748 return mmc_return;
1749 }
1750
1751 mmc_return = mmc_boot_set_write_timeout( host, card );
1752 if( mmc_return != MMC_BOOT_E_SUCCESS )
1753 {
1754 dprintf(CRITICAL, "Error No.%d: Failure setting Write Timeout value!\n",
1755 mmc_return );
1756 return mmc_return;
1757 }
1758
1759 return MMC_BOOT_E_SUCCESS;
1760}
1761
1762/*
1763 * Routine to initialize MMC card. It resets a card to idle state, verify operating
1764 * voltage and set the card inready state.
1765 */
1766static unsigned int mmc_boot_init_card( struct mmc_boot_host* host,
1767 struct mmc_boot_card* card )
1768{
1769 unsigned int mmc_retry = 0;
1770 unsigned int mmc_return = MMC_BOOT_E_SUCCESS;
1771
1772 /* basic check */
1773 if( ( host == NULL ) || ( card == NULL ) )
1774 {
1775 return MMC_BOOT_E_INVAL;
1776 }
1777
1778 /* 1. Card Reset - not necessary*/
1779 mmc_return = mmc_boot_reset_cards();
1780 if( mmc_return != MMC_BOOT_E_SUCCESS )
1781 {
1782 dprintf(CRITICAL, "Error No.:%d: Failure resetting MMC cards!\n", mmc_return);
1783 return mmc_return;
1784 }
1785
1786 /* 2. Card Initialization process */
1787
1788 /* Send CMD1 to identify and reject cards that do not match host's VDD range
1789 profile. Cards sends its OCR register in response.
1790 */
1791 mmc_retry = 0;
1792 do
1793 {
1794 mmc_return = mmc_boot_send_op_cond( host, card );
1795 /* Card returns busy status. We'll retry again! */
1796 if( mmc_return == MMC_BOOT_E_CARD_BUSY )
1797 {
1798 mmc_retry++;
1799 mdelay(200);
1800 continue;
1801 }
1802 else if( mmc_return == MMC_BOOT_E_SUCCESS )
1803 {
1804 break;
1805 }
1806 else
1807 {
1808 dprintf(CRITICAL, "Error No. %d: Failure Initializing MMC Card!\n",
1809 mmc_return );
1810 return mmc_return;
1811 }
1812 }while( mmc_retry < host->cmd_retry );
1813
1814 /* If card still returned busy status we are out of luck.
1815 * Card cannot be initialized */
1816 if( mmc_return == MMC_BOOT_E_CARD_BUSY )
1817 {
1818 dprintf(CRITICAL, "Error No. %d: Card has busy status set. \
1819 Initialization not completed\n", mmc_return );
1820 return MMC_BOOT_E_CARD_BUSY;
1821 }
1822
1823 /*Assuming high capacity mmc card*/
1824 card->type = MMC_BOOT_TYPE_SDHC;
1825
1826 return MMC_BOOT_E_SUCCESS;
1827}
1828
1829
1830/*
1831 * Performs initialization and identification of all the MMC cards connected
1832 * to the host.
1833 */
1834
1835static unsigned int mmc_boot_init_and_identify_cards( struct mmc_boot_host* host, struct mmc_boot_card* card )
1836{
1837 unsigned int mmc_return = MMC_BOOT_E_SUCCESS;
1838
1839 /* Basic check */
1840 if( host == NULL )
1841 {
1842 return MMC_BOOT_E_INVAL;
1843 }
1844
1845 /* Initialize MMC card structure */
1846 card->status = MMC_BOOT_STATUS_INACTIVE;
1847 card->rd_block_len = MMC_BOOT_RD_BLOCK_LEN;
1848 card->wr_block_len = MMC_BOOT_WR_BLOCK_LEN;
1849
1850 /* Start initialization process (CMD0 & CMD1) */
1851 mmc_return = mmc_boot_init_card( host, card );
1852 if( mmc_return != MMC_BOOT_E_SUCCESS )
1853 {
1854 return mmc_return;
1855 }
1856
1857 /* Start card identification process (CMD2, CMD3 & CMD9)*/
1858 mmc_return = mmc_boot_identify_card( host, card );
1859 if( mmc_return != MMC_BOOT_E_SUCCESS )
1860 {
1861 return mmc_return;
1862 }
1863
1864 /* Select the card (CMD7) */
1865 mmc_return = mmc_boot_select_card( card, card->rca );
1866 if( mmc_return != MMC_BOOT_E_SUCCESS )
1867 {
1868 dprintf(CRITICAL, "Error No.%d: Failure selecting the Card with RCA: %x\n",
1869 mmc_return, card->rca );
1870 return mmc_return;
1871 }
1872
1873 /* set interface speed */
1874 mmc_return = mmc_boot_adjust_interface_speed( host, card );
1875 if( mmc_return != MMC_BOOT_E_SUCCESS )
1876 {
1877 dprintf(CRITICAL, "Error No.%d: Error adjusting interface speed!\n",
1878 mmc_return );
1879 return mmc_return;
1880 }
1881
1882 /* enable wide bus */
1883 mmc_return = mmc_boot_set_bus_width( card, MMC_BOOT_BUS_WIDTH_4_BIT );
1884 if( mmc_return != MMC_BOOT_E_SUCCESS )
1885 {
1886 dprintf(CRITICAL, "Error No.%d: Failure to set wide bus for Card(RCA:%x)\n",
1887 mmc_return, card->rca );
1888 return mmc_return;
1889 }
1890
1891 return MMC_BOOT_E_SUCCESS;
1892}
1893
1894/*
1895 * Read MBR from MMC card and fill partition table.
1896 */
1897static unsigned int mmc_boot_read_MBR(void)
1898{
1899 unsigned char buffer[MMC_BOOT_RD_BLOCK_LEN];
1900 unsigned int dtype;
1901 unsigned int dfirstsec;
1902 unsigned int EBR_first_sec;
1903 unsigned int EBR_current_sec;
1904 int ret = 0;
1905 int idx, i;
1906
1907 /* Print out the MBR first */
1908 ret = mmc_boot_read_from_card( &mmc_host, &mmc_card, 0, \
1909 MMC_BOOT_RD_BLOCK_LEN, \
1910 (unsigned int *)buffer);
1911 if (ret)
1912 {
1913 return ret;
1914 }
1915
1916 /* Check to see if signature exists */
1917 if ((buffer[TABLE_SIGNATURE] != 0x55) || \
1918 (buffer[TABLE_SIGNATURE + 1] != 0xAA))
1919 {
1920 return -1;
1921 }
1922
1923 /* Print out the first 4 partition */
1924 idx = TABLE_ENTRY_0;
1925 for (i = 0; i < 4; i++)
1926 {
1927 mbr[mmc_partition_count].dstatus = \
1928 buffer[idx + i * TABLE_ENTRY_SIZE + OFFSET_STATUS];
1929 mbr[mmc_partition_count].dtype = \
1930 buffer[idx + i * TABLE_ENTRY_SIZE + OFFSET_TYPE];
1931 mbr[mmc_partition_count].dfirstsec = \
1932 GET_LWORD_FROM_BYTE(&buffer[idx + \
1933 i * TABLE_ENTRY_SIZE + \
1934 OFFSET_FIRST_SEC]);
1935 mbr[mmc_partition_count].dsize = \
1936 GET_LWORD_FROM_BYTE(&buffer[idx + \
1937 i * TABLE_ENTRY_SIZE + \
1938 OFFSET_SIZE]);
1939 dtype = mbr[mmc_partition_count].dtype;
1940 dfirstsec = mbr[mmc_partition_count].dfirstsec;
1941 mbr_fill_name(&mbr[mmc_partition_count], \
1942 mbr[mmc_partition_count].dtype);
1943 mmc_partition_count++;
1944 if (mmc_partition_count == MAX_PARTITIONS)
1945 return ret;
1946 }
1947
1948 /* See if the last partition is EBR, if not, parsing is done */
1949 if (dtype != 0x05)
1950 {
1951 return ret;
1952 }
1953
1954 EBR_first_sec = dfirstsec;
1955 EBR_current_sec = dfirstsec;
1956
1957 ret = mmc_boot_read_from_card( &mmc_host, &mmc_card, \
1958 (EBR_first_sec * 512), \
1959 MMC_BOOT_RD_BLOCK_LEN, \
1960 (unsigned int *)buffer);
1961 if (ret)
1962 {
1963 return ret;
1964 }
1965 /* Loop to parse the EBR */
1966 for (i = 0;; i++)
1967 {
1968 if ((buffer[TABLE_SIGNATURE] != 0x55) || (buffer[TABLE_SIGNATURE + 1] != 0xAA))
1969 {
1970 break;
1971 }
1972 mbr[mmc_partition_count].dstatus = \
1973 buffer[TABLE_ENTRY_0 + OFFSET_STATUS];
1974 mbr[mmc_partition_count].dtype = \
1975 buffer[TABLE_ENTRY_0 + OFFSET_TYPE];
1976 mbr[mmc_partition_count].dfirstsec = \
1977 GET_LWORD_FROM_BYTE(&buffer[TABLE_ENTRY_0 + \
1978 OFFSET_FIRST_SEC]) + \
1979 EBR_current_sec;
1980 mbr[mmc_partition_count].dsize = \
1981 GET_LWORD_FROM_BYTE(&buffer[TABLE_ENTRY_0 + \
1982 OFFSET_SIZE]);
1983 mbr_fill_name(&(mbr[mmc_partition_count]), \
1984 mbr[mmc_partition_count].dtype);
1985 mmc_partition_count++;
1986 if (mmc_partition_count == MAX_PARTITIONS)
1987 return ret;
1988
1989 dfirstsec = GET_LWORD_FROM_BYTE(&buffer[TABLE_ENTRY_1 + OFFSET_FIRST_SEC]);
1990 if(dfirstsec == 0)
1991 {
1992 /* Getting to the end of the EBR tables */
1993 break;
1994 }
1995 /* More EBR to follow - read in the next EBR sector */
1996 ret = mmc_boot_read_from_card( &mmc_host, &mmc_card, \
1997 ((EBR_first_sec + dfirstsec) * 512), \
1998 MMC_BOOT_RD_BLOCK_LEN, \
1999 (unsigned int *)buffer);
2000 if (ret)
2001 {
2002 return ret;
2003 }
2004 EBR_current_sec = EBR_first_sec + dfirstsec;
2005 }
2006 return ret;
2007}
2008
2009/*
2010 * Entry point to MMC boot process
2011 */
2012unsigned int mmc_boot_main(void)
2013{
2014 unsigned int mmc_ret = MMC_BOOT_E_SUCCESS;
2015
2016 memset( (struct mmc_boot_host*)&mmc_host, 0, sizeof( struct mmc_boot_host ) );
2017 memset( (struct mmc_boot_card*)&mmc_card, 0, sizeof(struct mmc_boot_card) );
2018
2019#ifndef PLATFORM_MSM8X60
2020 /* Waiting for modem to come up */
2021 while (readl(MSM_SHARED_BASE + 0x14) != 1);
2022#endif
2023 /* Initialize necessary data structure and enable/set clock and power */
2024 dprintf(INFO," Initializing MMC host data structure and clock!\n" );
2025 mmc_ret = mmc_boot_init( &mmc_host );
2026 if( mmc_ret != MMC_BOOT_E_SUCCESS )
2027 {
2028 dprintf(CRITICAL, "MMC Boot: Error Initializing MMC Card!!!\n" );
2029 return MMC_BOOT_E_FAILURE;
2030 }
2031
2032 /* Initialize and identify cards connected to host */
2033 mmc_ret = mmc_boot_init_and_identify_cards( &mmc_host, &mmc_card );
2034 if( mmc_ret != MMC_BOOT_E_SUCCESS )
2035 {
2036 dprintf(CRITICAL, "MMC Boot: Failure detecting MMC card!!!\n" );
2037 return MMC_BOOT_E_FAILURE;
2038 }
2039
2040 /* Read MBR of the card */
2041 mmc_ret = mmc_boot_read_MBR();
2042 if( mmc_ret != MMC_BOOT_E_SUCCESS )
2043 {
2044 dprintf(CRITICAL, "MMC Boot: MBR read failed!\n" );
2045 return MMC_BOOT_E_FAILURE;
2046 }
2047
2048 return MMC_BOOT_E_SUCCESS;
2049}
2050
2051/*
2052 * MMC write function
2053 */
2054unsigned int mmc_write (unsigned long long data_addr, unsigned int data_len, unsigned int* in)
2055{
2056 int val = 0;
2057 unsigned int write_size = ((unsigned)(0xFFFFFF/512))*512;
2058 unsigned offset = 0;
2059 unsigned int *sptr = in;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002060
2061 if(data_len % 512)
2062 data_len = ROUND_TO_PAGE(data_len, 511);
2063
Shashank Mittal52525ff2010-04-13 11:11:10 -07002064 while(data_len > write_size)
2065 {
2066 val = mmc_boot_write_to_card( &mmc_host, &mmc_card, \
2067 data_addr + offset, \
2068 write_size, sptr);
2069 if(val)
2070 {
2071 return val;
2072 }
2073
2074 sptr += (write_size/sizeof(unsigned));
2075 offset += write_size;
2076 data_len -= write_size;
2077 }
2078 if (data_len)
2079 {
2080 val = mmc_boot_write_to_card( &mmc_host, &mmc_card, \
2081 data_addr + offset, \
2082 data_len, sptr);
2083 }
2084 return val;
2085}
2086
2087/*
2088 * MMC read function
2089 */
2090unsigned int mmc_read (unsigned long long data_addr, unsigned int* out, unsigned int data_len)
2091{
2092 int val = 0;
2093 val = mmc_boot_read_from_card( &mmc_host, &mmc_card, data_addr, data_len, out);
2094 return val;
2095}
2096
2097/*
2098 * Fill name for android partition found.
2099 */
2100static void mbr_fill_name (struct mbr_entry *mbr_ent, unsigned int type)
2101{
2102 switch(type)
2103 {
2104 memset(mbr_ent->name, 0, 64);
David Ng77849d32010-08-04 15:18:50 -07002105 case MMC_MODEM_TYPE:
Subbaraman Narayanamurthy88aa13d2010-08-25 19:06:16 -07002106 case MMC_MODEM_TYPE2:
David Ng77849d32010-08-04 15:18:50 -07002107 /* if there are more than one with type "modem", mmc_ptn_offset will return the first one */
2108 memcpy(mbr_ent->name,"modem",5);
2109 break;
Ajay Dudani02704632010-08-30 14:40:07 -07002110 case MMC_SBL1_TYPE:
2111 memcpy(mbr_ent->name,"sbl1",4);
2112 break;
2113 case MMC_SBL2_TYPE:
2114 memcpy(mbr_ent->name,"sbl2",4);
2115 break;
2116 case MMC_SBL3_TYPE:
2117 memcpy(mbr_ent->name,"sbl3",4);
2118 break;
2119 case MMC_RPM_TYPE:
2120 memcpy(mbr_ent->name,"rpm",3);
2121 break;
2122 case MMC_TZ_TYPE:
2123 memcpy(mbr_ent->name,"tz",2);
2124 break;
2125 case MMC_ABOOT_TYPE:
2126 memcpy(mbr_ent->name,"aboot",5);
2127 break;
Shashank Mittal52525ff2010-04-13 11:11:10 -07002128 case MMC_BOOT_TYPE:
David Ng77849d32010-08-04 15:18:50 -07002129 memcpy(mbr_ent->name,"boot",4);
2130 break;
Shashank Mittal52525ff2010-04-13 11:11:10 -07002131 case MMC_USERDATA_TYPE:
David Ng77849d32010-08-04 15:18:50 -07002132 strcpy((char *)mbr_ent->name,(const char *)ext3_partitions[ext3_count]);
2133 ext3_count++;
2134 break;
Shashank Mittal52525ff2010-04-13 11:11:10 -07002135 };
2136}
2137
2138/*
2139 * Returns offset of given partition
2140 */
2141unsigned long long mmc_ptn_offset (unsigned char * name)
2142{
2143 unsigned n;
2144 for(n = 0; n < mmc_partition_count; n++) {
2145 if(!strcmp((const char *)mbr[n].name, (const char *)name)) {
2146 return (mbr[n].dfirstsec * MMC_BOOT_RD_BLOCK_LEN);
2147 }
2148 }
2149 return 0;
2150}
2151
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002152unsigned long long mmc_ptn_size (unsigned char * name)
2153{
2154 unsigned n;
2155 for(n = 0; n < mmc_partition_count; n++) {
2156 if(!strcmp((const char *)mbr[n].name, (const char *)name)) {
2157 return (mbr[n].dsize * MMC_BOOT_RD_BLOCK_LEN);
2158 }
2159 }
2160 return 0;
2161}