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 struct dataflash_addr {
34 #ifdef CONFIG_AT91SAM9260EK
35 struct dataflash_addr cs[CFG_MAX_DATAFLASH_BANKS] = {
36 {CFG_DATAFLASH_LOGIC_ADDR_CS0, 0}, /* Logical adress, CS */
37 {CFG_DATAFLASH_LOGIC_ADDR_CS1, 1}
39 #elif defined(CONFIG_AT91SAM9263EK)
40 struct dataflash_addr cs[CFG_MAX_DATAFLASH_BANKS] = {
41 {CFG_DATAFLASH_LOGIC_ADDR_CS0, 0}, /* Logical adress, CS */
44 struct dataflash_addr cs[CFG_MAX_DATAFLASH_BANKS] = {
45 {CFG_DATAFLASH_LOGIC_ADDR_CS0, 0}, /* Logical adress, CS */
46 {CFG_DATAFLASH_LOGIC_ADDR_CS3, 3}
50 /*define the area offsets*/
51 #if defined(CONFIG_AT91SAM9261EK) || defined(CONFIG_AT91SAM9260EK) || defined(CONFIG_AT91SAM9263EK)
52 #if defined(CONFIG_NEW_PARTITION)
53 dataflash_protect_t area_list[NB_DATAFLASH_AREA] = {
54 {0x00000000, 0x00003FFF, FLAG_PROTECT_SET, 0, "Bootstrap"}, /* ROM code */
55 {0x00004200, 0x000083FF, FLAG_PROTECT_CLEAR, 0, "Environment"}, /* u-boot environment */
56 {0x00008400, 0x0003DDFF, FLAG_PROTECT_SET, 0, "U-Boot"}, /* u-boot code */
57 {0x0003DE00, 0x00041FFF, FLAG_PROTECT_CLEAR, FLAG_SETENV, "MON"}, /* Room for alternative boot monitor */
58 {0x00042000, 0x0018BFFF, FLAG_PROTECT_CLEAR, FLAG_SETENV, "OS"}, /* data area size to tune */
59 {0x0018C000, 0xFFFFFFFF, FLAG_PROTECT_CLEAR, FLAG_SETENV, "FS"}, /* data area size to tune */
62 dataflash_protect_t area_list[NB_DATAFLASH_AREA] = {
63 {0, 0x3fff, FLAG_PROTECT_SET}, /* ROM code */
64 {0x4000, 0x7fff, FLAG_PROTECT_CLEAR}, /* u-boot environment */
65 {0x8000, 0x37fff, FLAG_PROTECT_SET}, /* u-boot code */
66 {0x38000, 0x1fffff, FLAG_PROTECT_CLEAR}, /* data area size to tune */
69 #elif defined(CONFIG_NEW_PARTITION)
70 /*define the area offsets*/
71 /* Invalid partitions should be defined with start > end */
72 dataflash_protect_t area_list[NB_DATAFLASH_AREA*CFG_MAX_DATAFLASH_BANKS] = {
73 {0x00000000, 0x000083ff, FLAG_PROTECT_SET, 0, "Bootstrap"}, /* ROM code */
74 {0x00008400, 0x00020fff, FLAG_PROTECT_SET, 0, "U-Boot"}, /* u-boot code */
75 {0x00021000, 0x000293ff, FLAG_PROTECT_CLEAR, 0, "Environment"}, /* u-boot environment 8Kb */
76 {0x00029400, 0x00041fff, FLAG_PROTECT_INVALID, 0, "<Unused>"}, /* Rest of Sector 1 */
77 {0x00042000, 0x0018Bfff, FLAG_PROTECT_CLEAR, FLAG_SETENV, "OS"}, /* data area size to tune */
78 {0x0018C000, 0xffffffff, FLAG_PROTECT_CLEAR, FLAG_SETENV, "FS"}, /* data area size to tune */
80 {0x00000000, 0xffffffff, FLAG_PROTECT_CLEAR, FLAG_SETENV, "Data"}, /* data area */
81 {0xffffffff, 0x00000000, FLAG_PROTECT_INVALID, 0, "<Invalid>"}, /* Invalid */
82 {0xffffffff, 0x00000000, FLAG_PROTECT_INVALID, 0, "<Invalid>"}, /* Invalid */
83 {0xffffffff, 0x00000000, FLAG_PROTECT_INVALID, 0, "<Invalid>"}, /* Invalid */
84 {0xffffffff, 0x00000000, FLAG_PROTECT_INVALID, 0, "<Invalid>"}, /* Invalid */
85 {0xffffffff, 0x00000000, FLAG_PROTECT_INVALID, 0, "<Invalid>"}, /* Invalid */
88 dataflash_protect_t area_list[NB_DATAFLASH_AREA] = {
89 {0, 0x7fff, FLAG_PROTECT_SET}, /* ROM code */
90 {0x8000, 0x1ffff, FLAG_PROTECT_SET}, /* u-boot code */
91 {0x20000, 0x27fff, FLAG_PROTECT_CLEAR}, /* u-boot environment */
92 {0x28000, 0x1fffff, FLAG_PROTECT_CLEAR}, /* data area size to tune */
96 extern void AT91F_SpiInit (void);
97 extern int AT91F_DataflashProbe (int i, AT91PS_DataflashDesc pDesc);
98 extern int AT91F_DataFlashRead (AT91PS_DataFlash pDataFlash,
100 unsigned long size, char *buffer);
101 extern int AT91F_DataFlashWrite( AT91PS_DataFlash pDataFlash,
106 int AT91F_DataflashInit (void)
112 int found[CFG_MAX_DATAFLASH_BANKS];
113 unsigned char protected;
117 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
119 dataflash_info[i].Desc.state = IDLE;
120 dataflash_info[i].id = 0;
121 dataflash_info[i].Device.pages_number = 0;
122 dfcode = AT91F_DataflashProbe (cs[i].cs,
123 &dataflash_info[i].Desc);
127 dataflash_info[i].Device.pages_number = 4096;
128 dataflash_info[i].Device.pages_size = 528;
129 dataflash_info[i].Device.page_offset = 10;
130 dataflash_info[i].Device.byte_mask = 0x300;
131 dataflash_info[i].Device.cs = cs[i].cs;
132 dataflash_info[i].Desc.DataFlash_state = IDLE;
133 dataflash_info[i].logical_address = cs[i].addr;
134 dataflash_info[i].id = dfcode;
139 dataflash_info[i].Device.pages_number = 8192;
140 dataflash_info[i].Device.pages_size = 528;
141 dataflash_info[i].Device.page_offset = 10;
142 dataflash_info[i].Device.byte_mask = 0x300;
143 dataflash_info[i].Device.cs = cs[i].cs;
144 dataflash_info[i].Desc.DataFlash_state = IDLE;
145 dataflash_info[i].logical_address = cs[i].addr;
146 dataflash_info[i].id = dfcode;
151 dataflash_info[i].Device.pages_number = 8192;
152 dataflash_info[i].Device.pages_size = 1056;
153 dataflash_info[i].Device.page_offset = 11;
154 dataflash_info[i].Device.byte_mask = 0x700;
155 dataflash_info[i].Device.cs = cs[i].cs;
156 dataflash_info[i].Desc.DataFlash_state = IDLE;
157 dataflash_info[i].logical_address = cs[i].addr;
158 dataflash_info[i].id = dfcode;
163 dataflash_info[i].Device.pages_number = 16384;
164 dataflash_info[i].Device.pages_size = 1056;
165 dataflash_info[i].Device.page_offset = 11;
166 dataflash_info[i].Device.byte_mask = 0x700;
167 dataflash_info[i].Device.cs = cs[i].cs;
168 dataflash_info[i].Desc.DataFlash_state = IDLE;
169 dataflash_info[i].logical_address = cs[i].addr;
170 dataflash_info[i].id = dfcode;
178 /* set the last area end to the dataflash size*/
179 area_list[NB_DATAFLASH_AREA -1].end =
180 (dataflash_info[i].Device.pages_number *
181 dataflash_info[i].Device.pages_size)-1;
184 /* set the area addresses */
185 for(j = 0; j<NB_DATAFLASH_AREA; j++) {
187 dataflash_info[i].Device.area_list[j].start =
188 area_list[part].start +
189 dataflash_info[i].logical_address;
190 if(area_list[part].end == 0xffffffff) {
191 dataflash_info[i].Device.area_list[j].end =
192 dataflash_info[i].end_address +
193 dataflash_info [i].logical_address;
196 dataflash_info[i].Device.area_list[j].end =
197 area_list[part].end +
198 dataflash_info[i].logical_address;
200 protected = area_list[part].protected;
201 /* Set the environment according to the label...*/
202 if(protected == FLAG_PROTECT_INVALID) {
203 dataflash_info[i].Device.area_list[j].protected =
204 FLAG_PROTECT_INVALID;
206 dataflash_info[i].Device.area_list[j].protected =
209 strcpy((char*)(dataflash_info[i].Device.area_list[j].label),
210 (const char *)area_list[part].label);
218 #ifdef CONFIG_NEW_DF_PARTITION
219 int AT91F_DataflashSetEnv (void)
224 unsigned char s[32]; /* Will fit a long int in hex */
226 for (i = 0, part= 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
227 for(j = 0; j<NB_DATAFLASH_AREA; j++) {
228 env = area_list[part].setenv;
229 /* Set the environment according to the label...*/
230 if((env & FLAG_SETENV) == FLAG_SETENV) {
232 dataflash_info[i].Device.area_list[j].start;
233 sprintf(s,"%X",start);
234 setenv(area_list[part].label,s);
242 void dataflash_print_info (void)
246 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
247 if (dataflash_info[i].id != 0) {
248 printf("DataFlash:");
249 switch (dataflash_info[i].id) {
251 printf("AT45DB161\n");
255 printf("AT45DB321\n");
259 printf("AT45DB642\n");
262 printf("AT45DB128\n");
266 printf("Nb pages: %6d\n"
269 "Logical address: 0x%08X\n",
270 (unsigned int) dataflash_info[i].Device.pages_number,
271 (unsigned int) dataflash_info[i].Device.pages_size,
272 (unsigned int) dataflash_info[i].Device.pages_number *
273 dataflash_info[i].Device.pages_size,
274 (unsigned int) dataflash_info[i].logical_address);
275 for (j=0; j< NB_DATAFLASH_AREA; j++) {
276 switch(dataflash_info[i].Device.area_list[j].protected) {
277 case FLAG_PROTECT_SET:
278 case FLAG_PROTECT_CLEAR:
279 printf("Area %i:\t%08lX to %08lX %s", j,
280 dataflash_info[i].Device.area_list[j].start,
281 dataflash_info[i].Device.area_list[j].end,
282 (dataflash_info[i].Device.area_list[j].protected==FLAG_PROTECT_SET) ? "(RO)" : " ");
283 #ifdef CONFIG_NEW_DF_PARTITION
284 printf(" %s\n", dataflash_info[i].Device.area_list[j].label);
289 #ifdef CONFIG_NEW_DF_PARTITION
290 case FLAG_PROTECT_INVALID:
300 /*---------------------------------------------------------------------------*/
301 /* Function Name : AT91F_DataflashSelect */
302 /* Object : Select the correct device */
303 /*---------------------------------------------------------------------------*/
304 AT91PS_DataFlash AT91F_DataflashSelect (AT91PS_DataFlash pFlash,
310 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++)
311 if ( dataflash_info[i].id
312 && ((((int) *addr) & 0xFF000000) ==
313 dataflash_info[i].logical_address)) {
318 pFlash = (AT91PS_DataFlash) 0;
321 pFlash->pDataFlashDesc = &(dataflash_info[i].Desc);
322 pFlash->pDevice = &(dataflash_info[i].Device);
323 *addr -= dataflash_info[i].logical_address;
327 /*---------------------------------------------------------------------------*/
328 /* Function Name : addr_dataflash */
329 /* Object : Test if address is valid */
330 /*---------------------------------------------------------------------------*/
331 int addr_dataflash (unsigned long addr)
336 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
337 if ((((int) addr) & 0xFF000000) ==
338 dataflash_info[i].logical_address) {
346 /*---------------------------------------------------------------------------*/
347 /* Function Name : size_dataflash */
348 /* Object : Test if address is valid regarding the size */
349 /*---------------------------------------------------------------------------*/
350 int size_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr,
353 /* is outside the dataflash */
354 if (((int)addr & 0x0FFFFFFF) > (pdataFlash->pDevice->pages_size *
355 pdataFlash->pDevice->pages_number)) return 0;
356 /* is too large for the dataflash */
357 if (size > ((pdataFlash->pDevice->pages_size *
358 pdataFlash->pDevice->pages_number) -
359 ((int)addr & 0x0FFFFFFF))) return 0;
363 /*---------------------------------------------------------------------------*/
364 /* Function Name : prot_dataflash */
365 /* Object : Test if destination area is protected */
366 /*---------------------------------------------------------------------------*/
367 int prot_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr)
371 for (area=0; area < NB_DATAFLASH_AREA; area++) {
372 if ((addr >= pdataFlash->pDevice->area_list[area].start) &&
373 (addr < pdataFlash->pDevice->area_list[area].end))
376 if (area == NB_DATAFLASH_AREA)
379 /*test protection value*/
380 if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_SET)
382 if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_INVALID)
387 /*--------------------------------------------------------------------------*/
388 /* Function Name : dataflash_real_protect */
389 /* Object : protect/unprotect area */
390 /*--------------------------------------------------------------------------*/
391 int dataflash_real_protect (int flag, unsigned long start_addr,
392 unsigned long end_addr)
394 int i,j, area1, area2, addr_valid = 0;
396 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
397 if ((((int) start_addr) & 0xF0000000) ==
398 dataflash_info[i].logical_address) {
406 /* find start area */
407 for (area1=0; area1 < NB_DATAFLASH_AREA; area1++) {
408 if (start_addr == dataflash_info[i].Device.area_list[area1].start)
411 if (area1 == NB_DATAFLASH_AREA) return -1;
413 for (area2=0; area2 < NB_DATAFLASH_AREA; area2++) {
414 if (end_addr == dataflash_info[i].Device.area_list[area2].end)
417 if (area2 == NB_DATAFLASH_AREA)
420 /*set protection value*/
421 for(j = area1; j < area2+1 ; j++)
422 if(dataflash_info[i].Device.area_list[j].protected
423 != FLAG_PROTECT_INVALID) {
425 dataflash_info[i].Device.area_list[j].protected
426 = FLAG_PROTECT_CLEAR;
428 dataflash_info[i].Device.area_list[j].protected
433 return (area2-area1+1);
436 /*---------------------------------------------------------------------------*/
437 /* Function Name : read_dataflash */
438 /* Object : dataflash memory read */
439 /*---------------------------------------------------------------------------*/
440 int read_dataflash (unsigned long addr, unsigned long size, char *result)
442 unsigned long AddrToRead = addr;
443 AT91PS_DataFlash pFlash = &DataFlashInst;
445 pFlash = AT91F_DataflashSelect (pFlash, &AddrToRead);
448 return ERR_UNKNOWN_FLASH_TYPE;
450 if (size_dataflash(pFlash,addr,size) == 0)
453 return (AT91F_DataFlashRead (pFlash, AddrToRead, size, result));
457 /*---------------------------------------------------------------------------*/
458 /* Function Name : write_dataflash */
459 /* Object : write a block in dataflash */
460 /*---------------------------------------------------------------------------*/
461 int write_dataflash (unsigned long addr_dest, unsigned long addr_src,
464 unsigned long AddrToWrite = addr_dest;
465 AT91PS_DataFlash pFlash = &DataFlashInst;
467 pFlash = AT91F_DataflashSelect (pFlash, &AddrToWrite);
470 return ERR_UNKNOWN_FLASH_TYPE;
472 if (size_dataflash(pFlash,addr_dest,size) == 0)
475 if (prot_dataflash(pFlash,addr_dest) == 0)
476 return ERR_PROTECTED;
478 if (AddrToWrite == -1)
481 return AT91F_DataFlashWrite (pFlash, (uchar *)addr_src,
486 void dataflash_perror (int err)
492 printf("Timeout writing to DataFlash\n");
495 printf("Can't write to protected/invalid DataFlash sectors\n");
498 printf("Outside available DataFlash\n");
500 case ERR_UNKNOWN_FLASH_TYPE:
501 printf("Unknown Type of DataFlash\n");
504 printf("General DataFlash Programming Error\n");
507 printf("%s[%d] FIXME: rc=%d\n", __FILE__, __LINE__, err);