1 /* LowLevel function for ATMEL DataFlash support
2 * Author : Hamid Ikdoumi (Atmel)
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22 #ifdef CONFIG_HAS_DATAFLASH
23 #include <asm/hardware.h>
24 #include <dataflash.h>
26 AT91S_DATAFLASH_INFO dataflash_info[CFG_MAX_DATAFLASH_BANKS];
27 static AT91S_DataFlash DataFlashInst;
29 #ifdef CONFIG_AT91SAM9260EK
30 int cs[][CFG_MAX_DATAFLASH_BANKS] = {
31 {CFG_DATAFLASH_LOGIC_ADDR_CS0, 0}, /* Logical adress, CS */
32 {CFG_DATAFLASH_LOGIC_ADDR_CS1, 1}
34 #elif defined(CONFIG_AT91SAM9263EK)
35 int cs[][CFG_MAX_DATAFLASH_BANKS] = {
36 {CFG_DATAFLASH_LOGIC_ADDR_CS0, 0} /* Logical adress, CS */
39 int cs[][CFG_MAX_DATAFLASH_BANKS] = {
40 {CFG_DATAFLASH_LOGIC_ADDR_CS0, 0}, /* Logical adress, CS */
41 {CFG_DATAFLASH_LOGIC_ADDR_CS3, 3}
45 /*define the area offsets*/
46 #if defined(CONFIG_AT91SAM9261EK) || defined(CONFIG_AT91SAM9260EK) || defined(CONFIG_AT91SAM9263EK)
47 #if defined(CONFIG_NEW_PARTITION)
48 dataflash_protect_t area_list[NB_DATAFLASH_AREA] = {
49 {0x00000000, 0x00003FFF, FLAG_PROTECT_SET, 0, "Bootstrap"}, /* ROM code */
50 {0x00004200, 0x000083FF, FLAG_PROTECT_CLEAR, 0, "Environment"}, /* u-boot environment */
51 {0x00008400, 0x0003DDFF, FLAG_PROTECT_SET, 0, "U-Boot"}, /* u-boot code */
52 {0x0003DE00, 0x00041FFF, FLAG_PROTECT_CLEAR, FLAG_SETENV, "MON"}, /* Room for alternative boot monitor */
53 {0x00042000, 0x0018BFFF, FLAG_PROTECT_CLEAR, FLAG_SETENV, "OS"}, /* data area size to tune */
54 {0x0018C000, 0xFFFFFFFF, FLAG_PROTECT_CLEAR, FLAG_SETENV, "FS"}, /* data area size to tune */
57 dataflash_protect_t area_list[NB_DATAFLASH_AREA] = {
58 {0, 0x3fff, FLAG_PROTECT_SET}, /* ROM code */
59 {0x4000, 0x7fff, FLAG_PROTECT_CLEAR}, /* u-boot environment */
60 {0x8000, 0x37fff, FLAG_PROTECT_SET}, /* u-boot code */
61 {0x38000, 0x1fffff, FLAG_PROTECT_CLEAR}, /* data area size to tune */
64 #elif defined(CONFIG_NEW_PARTITION)
65 /*define the area offsets*/
66 /* Invalid partitions should be defined with start > end */
67 dataflash_protect_t area_list[NB_DATAFLASH_AREA*CFG_MAX_DATAFLASH_BANKS] = {
68 {0x00000000, 0x000083ff, FLAG_PROTECT_SET, 0, "Bootstrap"}, /* ROM code */
69 {0x00008400, 0x00020fff, FLAG_PROTECT_SET, 0, "U-Boot"}, /* u-boot code */
70 {0x00021000, 0x000293ff, FLAG_PROTECT_CLEAR, 0, "Environment"}, /* u-boot environment 8Kb */
71 {0x00029400, 0x00041fff, FLAG_PROTECT_INVALID, 0, "<Unused>"}, /* Rest of Sector 1 */
72 {0x00042000, 0x0018Bfff, FLAG_PROTECT_CLEAR, FLAG_SETENV, "OS"}, /* data area size to tune */
73 {0x0018C000, 0xffffffff, FLAG_PROTECT_CLEAR, FLAG_SETENV, "FS"}, /* data area size to tune */
75 {0x00000000, 0xffffffff, FLAG_PROTECT_CLEAR, FLAG_SETENV, "Data"}, /* data area */
76 {0xffffffff, 0x00000000, FLAG_PROTECT_INVALID, 0, "<Invalid>"}, /* Invalid */
77 {0xffffffff, 0x00000000, FLAG_PROTECT_INVALID, 0, "<Invalid>"}, /* Invalid */
78 {0xffffffff, 0x00000000, FLAG_PROTECT_INVALID, 0, "<Invalid>"}, /* Invalid */
79 {0xffffffff, 0x00000000, FLAG_PROTECT_INVALID, 0, "<Invalid>"}, /* Invalid */
80 {0xffffffff, 0x00000000, FLAG_PROTECT_INVALID, 0, "<Invalid>"}, /* Invalid */
83 dataflash_protect_t area_list[NB_DATAFLASH_AREA] = {
84 {0, 0x7fff, FLAG_PROTECT_SET}, /* ROM code */
85 {0x8000, 0x1ffff, FLAG_PROTECT_SET}, /* u-boot code */
86 {0x20000, 0x27fff, FLAG_PROTECT_CLEAR}, /* u-boot environment */
87 {0x28000, 0x1fffff, FLAG_PROTECT_CLEAR}, /* data area size to tune */
91 extern void AT91F_SpiInit (void);
92 extern int AT91F_DataflashProbe (int i, AT91PS_DataflashDesc pDesc);
93 extern int AT91F_DataFlashRead (AT91PS_DataFlash pDataFlash,
95 unsigned long size, char *buffer);
96 extern int AT91F_DataFlashWrite( AT91PS_DataFlash pDataFlash,
101 int AT91F_DataflashInit (void)
107 int found[CFG_MAX_DATAFLASH_BANKS];
108 unsigned char protected;
112 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
114 dataflash_info[i].Desc.state = IDLE;
115 dataflash_info[i].id = 0;
116 dataflash_info[i].Device.pages_number = 0;
117 dfcode = AT91F_DataflashProbe (cs[i][1],
118 &dataflash_info[i].Desc);
122 dataflash_info[i].Device.pages_number = 4096;
123 dataflash_info[i].Device.pages_size = 528;
124 dataflash_info[i].Device.page_offset = 10;
125 dataflash_info[i].Device.byte_mask = 0x300;
126 dataflash_info[i].Device.cs = cs[i][1];
127 dataflash_info[i].Desc.DataFlash_state = IDLE;
128 dataflash_info[i].logical_address = cs[i][0];
129 dataflash_info[i].id = dfcode;
134 dataflash_info[i].Device.pages_number = 8192;
135 dataflash_info[i].Device.pages_size = 528;
136 dataflash_info[i].Device.page_offset = 10;
137 dataflash_info[i].Device.byte_mask = 0x300;
138 dataflash_info[i].Device.cs = cs[i][1];
139 dataflash_info[i].Desc.DataFlash_state = IDLE;
140 dataflash_info[i].logical_address = cs[i][0];
141 dataflash_info[i].id = dfcode;
146 dataflash_info[i].Device.pages_number = 8192;
147 dataflash_info[i].Device.pages_size = 1056;
148 dataflash_info[i].Device.page_offset = 11;
149 dataflash_info[i].Device.byte_mask = 0x700;
150 dataflash_info[i].Device.cs = cs[i][1];
151 dataflash_info[i].Desc.DataFlash_state = IDLE;
152 dataflash_info[i].logical_address = cs[i][0];
153 dataflash_info[i].id = dfcode;
158 dataflash_info[i].Device.pages_number = 16384;
159 dataflash_info[i].Device.pages_size = 1056;
160 dataflash_info[i].Device.page_offset = 11;
161 dataflash_info[i].Device.byte_mask = 0x700;
162 dataflash_info[i].Device.cs = cs[i][1];
163 dataflash_info[i].Desc.DataFlash_state = IDLE;
164 dataflash_info[i].logical_address = cs[i][0];
165 dataflash_info[i].id = dfcode;
173 /* set the last area end to the dataflash size*/
174 area_list[NB_DATAFLASH_AREA -1].end =
175 (dataflash_info[i].Device.pages_number *
176 dataflash_info[i].Device.pages_size)-1;
179 /* set the area addresses */
180 for(j = 0; j<NB_DATAFLASH_AREA; j++) {
182 dataflash_info[i].Device.area_list[j].start =
183 area_list[part].start +
184 dataflash_info[i].logical_address;
185 if(area_list[part].end == 0xffffffff) {
186 dataflash_info[i].Device.area_list[j].end =
187 dataflash_info[i].end_address +
188 dataflash_info [i].logical_address;
191 dataflash_info[i].Device.area_list[j].end =
192 area_list[part].end +
193 dataflash_info[i].logical_address;
195 protected = area_list[part].protected;
196 /* Set the environment according to the label...*/
197 if(protected == FLAG_PROTECT_INVALID) {
198 dataflash_info[i].Device.area_list[j].protected =
199 FLAG_PROTECT_INVALID;
201 dataflash_info[i].Device.area_list[j].protected =
204 strcpy((char*)(dataflash_info[i].Device.area_list[j].label),
205 (const char *)area_list[part].label);
213 #ifdef CONFIG_NEW_DF_PARTITION
214 int AT91F_DataflashSetEnv (void)
219 unsigned char s[32]; /* Will fit a long int in hex */
221 for (i = 0, part= 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
222 for(j = 0; j<NB_DATAFLASH_AREA; j++) {
223 env = area_list[part].setenv;
224 /* Set the environment according to the label...*/
225 if((env & FLAG_SETENV) == FLAG_SETENV) {
227 dataflash_info[i].Device.area_list[j].start;
228 sprintf(s,"%X",start);
229 setenv(area_list[part].label,s);
237 void dataflash_print_info (void)
241 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
242 if (dataflash_info[i].id != 0) {
243 printf("DataFlash:");
244 switch (dataflash_info[i].id) {
246 printf("AT45DB161\n");
250 printf("AT45DB321\n");
254 printf("AT45DB642\n");
257 printf("AT45DB128\n");
261 printf("Nb pages: %6d\n"
264 "Logical address: 0x%08X\n",
265 (unsigned int) dataflash_info[i].Device.pages_number,
266 (unsigned int) dataflash_info[i].Device.pages_size,
267 (unsigned int) dataflash_info[i].Device.pages_number *
268 dataflash_info[i].Device.pages_size,
269 (unsigned int) dataflash_info[i].logical_address);
270 for (j=0; j< NB_DATAFLASH_AREA; j++) {
271 switch(dataflash_info[i].Device.area_list[j].protected) {
272 case FLAG_PROTECT_SET:
273 case FLAG_PROTECT_CLEAR:
274 printf("Area %i:\t%08lX to %08lX %s", j,
275 dataflash_info[i].Device.area_list[j].start,
276 dataflash_info[i].Device.area_list[j].end,
277 (dataflash_info[i].Device.area_list[j].protected==FLAG_PROTECT_SET) ? "(RO)" : " ");
278 #ifdef CONFIG_NEW_DF_PARTITION
279 printf(" %s\n", dataflash_info[i].Device.area_list[j].label);
284 #ifdef CONFIG_NEW_DF_PARTITION
285 case FLAG_PROTECT_INVALID:
295 /*---------------------------------------------------------------------------*/
296 /* Function Name : AT91F_DataflashSelect */
297 /* Object : Select the correct device */
298 /*---------------------------------------------------------------------------*/
299 AT91PS_DataFlash AT91F_DataflashSelect (AT91PS_DataFlash pFlash,
305 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++)
306 if ( dataflash_info[i].id
307 && ((((int) addr) & 0xFF000000) ==
308 dataflash_info[i].logical_address)) {
313 pFlash = (AT91PS_DataFlash) 0;
316 pFlash->pDataFlashDesc = &(dataflash_info[i].Desc);
317 pFlash->pDevice = &(dataflash_info[i].Device);
318 *addr -= dataflash_info[i].logical_address;
322 /*---------------------------------------------------------------------------*/
323 /* Function Name : addr_dataflash */
324 /* Object : Test if address is valid */
325 /*---------------------------------------------------------------------------*/
326 int addr_dataflash (unsigned long addr)
331 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
332 if ((((int) addr) & 0xFF000000) ==
333 dataflash_info[i].logical_address) {
341 /*---------------------------------------------------------------------------*/
342 /* Function Name : size_dataflash */
343 /* Object : Test if address is valid regarding the size */
344 /*---------------------------------------------------------------------------*/
345 int size_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr,
348 /* is outside the dataflash */
349 if (((int)addr & 0x0FFFFFFF) > (pdataFlash->pDevice->pages_size *
350 pdataFlash->pDevice->pages_number)) return 0;
351 /* is too large for the dataflash */
352 if (size > ((pdataFlash->pDevice->pages_size *
353 pdataFlash->pDevice->pages_number) -
354 ((int)addr & 0x0FFFFFFF))) return 0;
358 /*---------------------------------------------------------------------------*/
359 /* Function Name : prot_dataflash */
360 /* Object : Test if destination area is protected */
361 /*---------------------------------------------------------------------------*/
362 int prot_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr)
366 for (area=0; area < NB_DATAFLASH_AREA; area++) {
367 if ((addr >= pdataFlash->pDevice->area_list[area].start) &&
368 (addr < pdataFlash->pDevice->area_list[area].end))
371 if (area == NB_DATAFLASH_AREA)
374 /*test protection value*/
375 if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_SET)
377 if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_INVALID)
382 /*--------------------------------------------------------------------------*/
383 /* Function Name : dataflash_real_protect */
384 /* Object : protect/unprotect area */
385 /*--------------------------------------------------------------------------*/
386 int dataflash_real_protect (int flag, unsigned long start_addr,
387 unsigned long end_addr)
389 int i,j, area1, area2, addr_valid = 0;
391 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
392 if ((((int) start_addr) & 0xF0000000) ==
393 dataflash_info[i].logical_address) {
401 /* find start area */
402 for (area1=0; area1 < NB_DATAFLASH_AREA; area1++) {
403 if (start_addr == dataflash_info[i].Device.area_list[area1].start)
406 if (area1 == NB_DATAFLASH_AREA) return -1;
408 for (area2=0; area2 < NB_DATAFLASH_AREA; area2++) {
409 if (end_addr == dataflash_info[i].Device.area_list[area2].end)
412 if (area2 == NB_DATAFLASH_AREA)
415 /*set protection value*/
416 for(j = area1; j < area2+1 ; j++)
417 if(dataflash_info[i].Device.area_list[j].protected
418 != FLAG_PROTECT_INVALID) {
420 dataflash_info[i].Device.area_list[j].protected
421 = FLAG_PROTECT_CLEAR;
423 dataflash_info[i].Device.area_list[j].protected
428 return (area2-area1+1);
431 /*---------------------------------------------------------------------------*/
432 /* Function Name : read_dataflash */
433 /* Object : dataflash memory read */
434 /*---------------------------------------------------------------------------*/
435 int read_dataflash (unsigned long addr, unsigned long size, char *result)
437 unsigned long AddrToRead = addr;
438 AT91PS_DataFlash pFlash = &DataFlashInst;
440 pFlash = AT91F_DataflashSelect (pFlash, &AddrToRead);
443 return ERR_UNKNOWN_FLASH_TYPE;
445 if (size_dataflash(pFlash,addr,size) == 0)
448 return (AT91F_DataFlashRead (pFlash, AddrToRead, size, result));
452 /*---------------------------------------------------------------------------*/
453 /* Function Name : write_dataflash */
454 /* Object : write a block in dataflash */
455 /*---------------------------------------------------------------------------*/
456 int write_dataflash (unsigned long addr_dest, unsigned long addr_src,
459 unsigned long AddrToWrite = addr_dest;
460 AT91PS_DataFlash pFlash = &DataFlashInst;
462 pFlash = AT91F_DataflashSelect (pFlash, &AddrToWrite);
465 return ERR_UNKNOWN_FLASH_TYPE;
467 if (size_dataflash(pFlash,addr_dest,size) == 0)
470 if (prot_dataflash(pFlash,addr_dest) == 0)
471 return ERR_PROTECTED;
473 if (AddrToWrite == -1)
476 return AT91F_DataFlashWrite (pFlash, (uchar *)addr_src,
481 void dataflash_perror (int err)
487 printf("Timeout writing to DataFlash\n");
490 printf("Can't write to protected/invalid DataFlash sectors\n");
493 printf("Outside available DataFlash\n");
495 case ERR_UNKNOWN_FLASH_TYPE:
496 printf("Unknown Type of DataFlash\n");
499 printf("General DataFlash Programming Error\n");
502 printf("%s[%d] FIXME: rc=%d\n", __FILE__, __LINE__, err);