summaryrefslogtreecommitdiff
path: root/board/st/u8500/emmc.c
blob: 110c73702dae71b80570e956adbd50335868078c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
* (C) Copyright 2009
* ST-Ericsson, <www.stericsson.com>
*
* See file CREDITS for list of people who contributed to this
* project.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*/
/* --- includes ----------------------------------------------------------- */
#include "common.h"	/* XXX: Arrgghh! "common.h" includes <common.h> */
#include <command.h>
#include "mmc.h"
#include "emmc.h"
#include "gpio.h"
#include <asm/boottime.h>

#define PIB_EMMC_ADDR 0x00
/* ========================================================================
Name:        init_emmc
Description: init embedded multimedia card interface

======================================================================== */
int emmc_init(u8 card_num)
{
    t_mmc_error mmc_error;
    t_mmc_error response;
    gpio_error gpioerror;    
    int error;

#ifndef CONFIG_U8500_V1
/* Initialize the base address of eMMC */
    mmc_error = mmc_init(card_num, CFG_EMMC_BASE);

    if (MMC_OK != mmc_error) 
    {
        printf("emmc_init() %d \n", mmc_error);
        goto end;
    }
    
    /* Initialize the gpio alternate function for eMMC */
    struct gpio_register *p_gpio_register = (void *) IO_ADDRESS(CFG_GPIO_6_BASE);    
    p_gpio_register -> gpio_dats |= 0x0000FFE0;
    p_gpio_register -> gpio_pdis &= ~0x0000FFE0;
    
    //enable the alternate function of EMMC
    gpioerror = gpio_altfuncenable(GPIO_ALT_EMMC, "EMMC");
    if(gpioerror != GPIO_OK)
    {
        printf("emmc_init() gpio_altfuncenable %d failed\n", gpioerror);
        goto end;
    }

#else
/* Initialize the base address of PoP eMMC */
    mmc_error = mmc_init(card_num, CFG_POP_EMMC_BASE);

    if (MMC_OK != mmc_error) 
    {
        printf("emmc_init() %d \n", mmc_error);
        goto end;
    }
    //enable the alternate function of PoP EMMC
    gpioerror = gpio_altfuncenable(GPIO_ALT_POP_EMMC, "EMMC");
    if (gpioerror != GPIO_OK) {
        printf("emmc_init() gpio_altfuncenable %d failed \n",
               gpioerror);
        goto end;
    }
#endif
    //Power-on the controller
    response = mmc_poweron(card_num);
    if (response != MMC_OK) 
    {
    	 printf("Error in eMMC power on, response is %d\n",response);
         goto end;     
    }
    // Initialise the cards,get CID and CSD on the bus
    response = mmc_initializeCards(card_num);

    if (response != MMC_OK) 
    {
        printf(" Error in eMMC initialization\n");
        goto end;
    }
    
    error = emmc_write_pib();
    if(error)
	    printf("PIB info writing into eMMC failed\n");
    printf("eMMC done\n");
    
    return 0;

end:
#ifndef CONFIG_U8500_V1
    gpio_altfuncdisable(GPIO_ALT_EMMC, "EMMC");
#else
    gpio_altfuncdisable(GPIO_ALT_POP_EMMC, "EMMC");
#endif
    mmc_poweroff(card_num);
    return 1;
}

int emmc_write_pib(void)
{
    int i;
    t_mmc_error mmc_error;
    u32 block_offset = PIB_EMMC_ADDR;
    u8 emmc_last_sector[512];
    u8 card_num = 4;

    for (i = 0; i < 0x1BF; i++) {
        emmc_last_sector[i] = 0;
    }
    emmc_last_sector[0x1BF] = 0x03;
    emmc_last_sector[0x1C0] = 0xD0;
    emmc_last_sector[0x1C1] = 0xFF;
    emmc_last_sector[0x1C2] = 0x83;
    emmc_last_sector[0x1C3] = 0x03;
    emmc_last_sector[0x1C4] = 0xD0;
    emmc_last_sector[0x1C5] = 0xFF;
    emmc_last_sector[0x1C6] = 0x00;
    emmc_last_sector[0x1C7] = 0x00;
    emmc_last_sector[0x1C8] = 0x0A;
    emmc_last_sector[0x1C9] = 0x00;
    emmc_last_sector[0x1CA] = 0x00;
    emmc_last_sector[0x1CB] = 0x40;
    emmc_last_sector[0x1CC] = 0x00;
    emmc_last_sector[0x1CD] = 0x00;
    emmc_last_sector[0x1CE] = 0x00;
    emmc_last_sector[0x1CF] = 0x03;
    emmc_last_sector[0x1D0] = 0xD0;
    emmc_last_sector[0x1D1] = 0xFF;
    emmc_last_sector[0x1D2] = 0x83;
    emmc_last_sector[0x1D3] = 0x03;
    emmc_last_sector[0x1D4] = 0xD0;
    emmc_last_sector[0x1D5] = 0xFF;
    emmc_last_sector[0x1D6] = 0x00;
    emmc_last_sector[0x1D7] = 0x40;
    emmc_last_sector[0x1D8] = 0x0A;
    emmc_last_sector[0x1D9] = 0x00;
    emmc_last_sector[0x1DA] = 0x00;
    emmc_last_sector[0x1DB] = 0x00;
    emmc_last_sector[0x1DC] = 0x08;
    emmc_last_sector[0x1DD] = 0x00;
    emmc_last_sector[0x1DE] = 0x00;
    emmc_last_sector[0x1DF] = 0x03;
    emmc_last_sector[0x1E0] = 0xD0;
    emmc_last_sector[0x1E1] = 0xFF;
    emmc_last_sector[0x1E2] = 0x83;
    emmc_last_sector[0x1E3] = 0x03;
    emmc_last_sector[0x1E4] = 0xD0;
    emmc_last_sector[0x1E5] = 0xFF;
    emmc_last_sector[0x1E6] = 0x00;
    emmc_last_sector[0x1E7] = 0x40;
    emmc_last_sector[0x1E8] = 0x12;
    emmc_last_sector[0x1E9] = 0x00;
    emmc_last_sector[0x1EA] = 0x00;
    emmc_last_sector[0x1EB] = 0xC0;
    emmc_last_sector[0x1EC] = 0x22;
    emmc_last_sector[0x1ED] = 0x00;
    emmc_last_sector[0x1EE] = 0x00;
    emmc_last_sector[0x1EF] = 0x03;
    emmc_last_sector[0x1F0] = 0xD0;
    emmc_last_sector[0x1F1] = 0xFF;
    emmc_last_sector[0x1F2] = 0x0C;
    emmc_last_sector[0x1F3] = 0x03;
    emmc_last_sector[0x1F4] = 0xD0;
    emmc_last_sector[0x1F5] = 0xFF;
    emmc_last_sector[0x1F6] = 0x00;
    emmc_last_sector[0x1F7] = 0x00;
    emmc_last_sector[0x1F8] = 0x35;
    emmc_last_sector[0x1F9] = 0x00;
    emmc_last_sector[0x1FA] = 0x00;
    emmc_last_sector[0x1FB] = 0xA0;
    emmc_last_sector[0x1FC] = 0xB9;
    emmc_last_sector[0x1FD] = 0x00;
    emmc_last_sector[0x1FE] = 0x55;
    emmc_last_sector[0x1FF] = 0xAA;
    
/*  HACK required for HREF board as erase block size = 512KB */  
/*
    mmc_error = mmc_erase(card_num, 0x0, 0x1FF);
    if (mmc_error != MMC_OK) {
        printf(" eMMC erase failed in PIB \n");
        return 1;
    }
*/
    mmc_error =
        mmc_writeblocks(card_num, block_offset, (u32 *) emmc_last_sector,
                512, 1);
    if (mmc_error != MMC_OK) {
        printf(" eMMC PIB write failed \n");
        return 1;
    }
    return 0;
}

int emmc_erase(u32 start, u32 end)
{
    t_mmc_error mmc_error;
    u8 card_num = 4;
    printf("emmc erase start \n");
    mmc_error = mmc_erase(card_num, start, end);
    if (mmc_error != MMC_OK) {
        printf(" eMMC erase failed \n");
        return 1;
    }
    printf("emmc erase done \n");
    return 0;
}

int emmc_read(u32 block_offset, u32 read_buffer, u32 filesize)
{
    	t_mmc_error mmc_error;
    	u32 remaining;
    	u8 card_num = 4;
    	u8 *mem_address = (u8 *) read_buffer;    
    	u32 n=filesize,blocks;
 
    	remaining = filesize;

    	printf(" eMMC read start filesize=0x%x \n", filesize);    	

    	blocks = (n%512==0)?(n/512):(n/512)+1;   	
    	
    	while(blocks>=8)
    	{    		
    		mmc_error = mmc_readblocks(card_num, block_offset, (u32 *) mem_address,	512, 8);
        	if (mmc_error != MMC_OK) 
        	{
        		printf(" eMMC read blocks failed \n");
            		return 1;
        	}

        	block_offset += 4096;
        	mem_address += 4096;
        	blocks -=8;
        	remaining -= 4096;        	
        }
        if(blocks)
    	{    		
    		mmc_error = mmc_readblocks(card_num, block_offset, (u32 *) mem_address,	512, blocks);
        	if (mmc_error != MMC_OK) 
        	{
        		printf(" eMMC read blocks failed \n");
        		return 1;
        	}        	    	
        }
    
     	printf(" eMMC read done \n");
    	return 0;
}

int emmc_write(u32 block_offset, u32 write_buffer, u32 filesize)
{
    	t_mmc_error mmc_error;
    	u32 remaining;
    	u8 card_num = 4;
    	u8 *mem_address = (u8 *) write_buffer;
    	u32 n=filesize,blocks;

    	remaining = filesize;

    	printf(" eMMC write start filesize=0x%x \n", filesize);    	
    
    	blocks = (n%512==0)?(n/512):(n/512)+1;    	        
    	while(blocks>=8)
    	{    		
    		mmc_error = mmc_writeblocks(card_num, block_offset, (u32 *) mem_address,512, 8);
        	if (mmc_error != MMC_OK) 
        	{
        		printf(" eMMC write blocks failed \n");
            		return 1;
        	}

        	block_offset += 4096;
        	mem_address += 4096;
        	blocks -=8;
        	remaining -= 4096;        	
        }
        if(blocks)
    	{
    		
    		mmc_error = mmc_writeblocks(card_num, block_offset, (u32 *) mem_address,512, blocks);
        	if (mmc_error != MMC_OK) 
        	{
        		printf(" eMMC write blocks failed \n");
        		return 1;
        	}       	
        	
        }	
    
    	printf(" eMMC write done \n");
    	return 0;
}

/*
 * command line commands
 */
#ifdef CONFIG_CMD_EMMC
int do_emmc_erase (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
    	u32       start_address;
    	u32       end_address;    	
    	int       load_result = 1;
    	u32       error_name  = 0;

    	start_address  = simple_strtoul (argv[1],0,16);
    	end_address  = simple_strtoul (argv[2],0,16);        
	
	printf("emmc_erase :: start address = %x end_address=0x%x\n",start_address,end_address);
            
        load_result      = emmc_erase(start_address,end_address);
        if (load_result != 0)
        {
            error_name   = (unsigned long) (-load_result);
            printf("emmc_erase error : failed  \n");         
        }
        return(0);    
}

U_BOOT_CMD(
	emmc_erase,	3,	0,	do_emmc_erase,
	"- erase the eMMC flash \n",
	"start_address- start address of the eMMC block\n"
	"end_address- end address of the eMMC block\n"	
);

int do_emmc_read (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
    	u32       ram_address;
    	u32       block_offset;
    	u32       filesize;    	
    	int       load_result = 1;
    	u32       error_name  = 0;

    	ram_address  	= simple_strtoul (argv[1],0,16);
        block_offset 	= simple_strtoul (argv[2],0,16);
        filesize  	= simple_strtoul (argv[3],0,16);
	
	boottime_tag("load_image");
	printf("emmc_read :: ram address = 0x%x block address=0x%x \n",ram_address,block_offset);
            
        load_result      = emmc_read(block_offset,ram_address,filesize);
        if (load_result != 0)
        {
		boottime_remove_last();
            error_name   = (unsigned long) (-load_result);
            printf("emmc_read error : in reading data from eMMC block \n");         
        }
        return(0);    
}

U_BOOT_CMD(
	emmc_read,	4,	0,	do_emmc_read,
	"- read file from emmc flash \n",
	"ram_address - read from eMMC and copy into ram address\n"
	"block_offset - address to read the file from the eMMC block \n"
	"filesize - size of the file \n"
);

int do_emmc_write (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
    	u32       ram_address;
    	u32       block_offset;
    	u32       filesize;    	
    	int       load_result = 1;
    	u32       error_name  = 0;

    	ram_address  	= simple_strtoul (argv[1],0,16);
        block_offset 	= simple_strtoul (argv[2],0,16);
        filesize  	= simple_strtoul (argv[3],0,16);
	
	printf("emmc_write :: ram address = %x block address=0x%x \n",ram_address,block_offset);
            
        load_result      = emmc_write(block_offset,ram_address,filesize);
        if (load_result != 0)
        {
            error_name   = (unsigned long) (-load_result);
            printf("emmc_read error : in writing data into eMMC block \n");         
        }
        return(0);    
}

U_BOOT_CMD(
	emmc_write,	4,	0,	do_emmc_write,
	"- write file from emmc flash \n",
	"ram_address - write to eMMC by copying from ram address\n"
	"block_offset - address to write the file into the eMMC block \n"
	"filesize - size of the file \n"
);

#endif	/* CONFIG_CMD_EMMC */
/* ------------------------------- End of file ---------------------------- */