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) || defined(CONFIG_AT91CAP9ADK)
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) || \
52 defined(CONFIG_AT91SAM9263EK) || defined(CONFIG_AT91CAP9ADK)
53 #if defined(CONFIG_NEW_PARTITION)
54 dataflash_protect_t area_list
[NB_DATAFLASH_AREA
] = {
55 {0x00000000, 0x00003FFF, FLAG_PROTECT_SET
, 0, "Bootstrap"}, /* ROM code */
56 {0x00004200, 0x000083FF, FLAG_PROTECT_CLEAR
, 0, "Environment"}, /* u-boot environment */
57 {0x00008400, 0x0003DDFF, FLAG_PROTECT_SET
, 0, "U-Boot"}, /* u-boot code */
58 {0x0003DE00, 0x00041FFF, FLAG_PROTECT_CLEAR
, FLAG_SETENV
, "MON"}, /* Room for alternative boot monitor */
59 {0x00042000, 0x0018BFFF, FLAG_PROTECT_CLEAR
, FLAG_SETENV
, "OS"}, /* data area size to tune */
60 {0x0018C000, 0xFFFFFFFF, FLAG_PROTECT_CLEAR
, FLAG_SETENV
, "FS"}, /* data area size to tune */
63 dataflash_protect_t area_list
[NB_DATAFLASH_AREA
] = {
64 {0, 0x3fff, FLAG_PROTECT_SET
}, /* ROM code */
65 {0x4000, 0x7fff, FLAG_PROTECT_CLEAR
}, /* u-boot environment */
66 {0x8000, 0x37fff, FLAG_PROTECT_SET
}, /* u-boot code */
67 {0x38000, 0x1fffff, FLAG_PROTECT_CLEAR
}, /* data area size to tune */
70 #elif defined(CONFIG_NEW_PARTITION)
71 /*define the area offsets*/
72 /* Invalid partitions should be defined with start > end */
73 dataflash_protect_t area_list
[NB_DATAFLASH_AREA
*CFG_MAX_DATAFLASH_BANKS
] = {
74 {0x00000000, 0x000083ff, FLAG_PROTECT_SET
, 0, "Bootstrap"}, /* ROM code */
75 {0x00008400, 0x00020fff, FLAG_PROTECT_SET
, 0, "U-Boot"}, /* u-boot code */
76 {0x00021000, 0x000293ff, FLAG_PROTECT_CLEAR
, 0, "Environment"}, /* u-boot environment 8Kb */
77 {0x00029400, 0x00041fff, FLAG_PROTECT_INVALID
, 0, "<Unused>"}, /* Rest of Sector 1 */
78 {0x00042000, 0x0018Bfff, FLAG_PROTECT_CLEAR
, FLAG_SETENV
, "OS"}, /* data area size to tune */
79 {0x0018C000, 0xffffffff, FLAG_PROTECT_CLEAR
, FLAG_SETENV
, "FS"}, /* data area size to tune */
81 {0x00000000, 0xffffffff, FLAG_PROTECT_CLEAR
, FLAG_SETENV
, "Data"}, /* data area */
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 */
86 {0xffffffff, 0x00000000, FLAG_PROTECT_INVALID
, 0, "<Invalid>"}, /* Invalid */
89 dataflash_protect_t area_list
[NB_DATAFLASH_AREA
] = {
90 {0, 0x7fff, FLAG_PROTECT_SET
}, /* ROM code */
91 {0x8000, 0x1ffff, FLAG_PROTECT_SET
}, /* u-boot code */
92 {0x20000, 0x27fff, FLAG_PROTECT_CLEAR
}, /* u-boot environment */
93 {0x28000, 0x1fffff, FLAG_PROTECT_CLEAR
}, /* data area size to tune */
97 extern void AT91F_SpiInit (void);
98 extern int AT91F_DataflashProbe (int i
, AT91PS_DataflashDesc pDesc
);
99 extern int AT91F_DataFlashRead (AT91PS_DataFlash pDataFlash
,
101 unsigned long size
, char *buffer
);
102 extern int AT91F_DataFlashWrite( AT91PS_DataFlash pDataFlash
,
107 int AT91F_DataflashInit (void)
113 int found
[CFG_MAX_DATAFLASH_BANKS
];
114 unsigned char protected;
118 for (i
= 0; i
< CFG_MAX_DATAFLASH_BANKS
; i
++) {
120 dataflash_info
[i
].Desc
.state
= IDLE
;
121 dataflash_info
[i
].id
= 0;
122 dataflash_info
[i
].Device
.pages_number
= 0;
123 dfcode
= AT91F_DataflashProbe (cs
[i
].cs
,
124 &dataflash_info
[i
].Desc
);
128 dataflash_info
[i
].Device
.pages_number
= 4096;
129 dataflash_info
[i
].Device
.pages_size
= 528;
130 dataflash_info
[i
].Device
.page_offset
= 10;
131 dataflash_info
[i
].Device
.byte_mask
= 0x300;
132 dataflash_info
[i
].Device
.cs
= cs
[i
].cs
;
133 dataflash_info
[i
].Desc
.DataFlash_state
= IDLE
;
134 dataflash_info
[i
].logical_address
= cs
[i
].addr
;
135 dataflash_info
[i
].id
= dfcode
;
140 dataflash_info
[i
].Device
.pages_number
= 8192;
141 dataflash_info
[i
].Device
.pages_size
= 528;
142 dataflash_info
[i
].Device
.page_offset
= 10;
143 dataflash_info
[i
].Device
.byte_mask
= 0x300;
144 dataflash_info
[i
].Device
.cs
= cs
[i
].cs
;
145 dataflash_info
[i
].Desc
.DataFlash_state
= IDLE
;
146 dataflash_info
[i
].logical_address
= cs
[i
].addr
;
147 dataflash_info
[i
].id
= dfcode
;
152 dataflash_info
[i
].Device
.pages_number
= 8192;
153 dataflash_info
[i
].Device
.pages_size
= 1056;
154 dataflash_info
[i
].Device
.page_offset
= 11;
155 dataflash_info
[i
].Device
.byte_mask
= 0x700;
156 dataflash_info
[i
].Device
.cs
= cs
[i
].cs
;
157 dataflash_info
[i
].Desc
.DataFlash_state
= IDLE
;
158 dataflash_info
[i
].logical_address
= cs
[i
].addr
;
159 dataflash_info
[i
].id
= dfcode
;
164 dataflash_info
[i
].Device
.pages_number
= 16384;
165 dataflash_info
[i
].Device
.pages_size
= 1056;
166 dataflash_info
[i
].Device
.page_offset
= 11;
167 dataflash_info
[i
].Device
.byte_mask
= 0x700;
168 dataflash_info
[i
].Device
.cs
= cs
[i
].cs
;
169 dataflash_info
[i
].Desc
.DataFlash_state
= IDLE
;
170 dataflash_info
[i
].logical_address
= cs
[i
].addr
;
171 dataflash_info
[i
].id
= dfcode
;
179 /* set the last area end to the dataflash size*/
180 area_list
[NB_DATAFLASH_AREA
-1].end
=
181 (dataflash_info
[i
].Device
.pages_number
*
182 dataflash_info
[i
].Device
.pages_size
)-1;
185 /* set the area addresses */
186 for(j
= 0; j
<NB_DATAFLASH_AREA
; j
++) {
188 dataflash_info
[i
].Device
.area_list
[j
].start
=
189 area_list
[part
].start
+
190 dataflash_info
[i
].logical_address
;
191 if(area_list
[part
].end
== 0xffffffff) {
192 dataflash_info
[i
].Device
.area_list
[j
].end
=
193 dataflash_info
[i
].end_address
+
194 dataflash_info
[i
].logical_address
;
197 dataflash_info
[i
].Device
.area_list
[j
].end
=
198 area_list
[part
].end
+
199 dataflash_info
[i
].logical_address
;
201 protected = area_list
[part
].protected;
202 /* Set the environment according to the label...*/
203 if(protected == FLAG_PROTECT_INVALID
) {
204 dataflash_info
[i
].Device
.area_list
[j
].protected =
205 FLAG_PROTECT_INVALID
;
207 dataflash_info
[i
].Device
.area_list
[j
].protected =
210 strcpy((char*)(dataflash_info
[i
].Device
.area_list
[j
].label
),
211 (const char *)area_list
[part
].label
);
219 #ifdef CONFIG_NEW_DF_PARTITION
220 int AT91F_DataflashSetEnv (void)
225 unsigned char s
[32]; /* Will fit a long int in hex */
227 for (i
= 0, part
= 0; i
< CFG_MAX_DATAFLASH_BANKS
; i
++) {
228 for(j
= 0; j
<NB_DATAFLASH_AREA
; j
++) {
229 env
= area_list
[part
].setenv
;
230 /* Set the environment according to the label...*/
231 if((env
& FLAG_SETENV
) == FLAG_SETENV
) {
233 dataflash_info
[i
].Device
.area_list
[j
].start
;
234 sprintf(s
,"%X",start
);
235 setenv(area_list
[part
].label
,s
);
243 void dataflash_print_info (void)
247 for (i
= 0; i
< CFG_MAX_DATAFLASH_BANKS
; i
++) {
248 if (dataflash_info
[i
].id
!= 0) {
249 printf("DataFlash:");
250 switch (dataflash_info
[i
].id
) {
252 printf("AT45DB161\n");
256 printf("AT45DB321\n");
260 printf("AT45DB642\n");
263 printf("AT45DB128\n");
267 printf("Nb pages: %6d\n"
270 "Logical address: 0x%08X\n",
271 (unsigned int) dataflash_info
[i
].Device
.pages_number
,
272 (unsigned int) dataflash_info
[i
].Device
.pages_size
,
273 (unsigned int) dataflash_info
[i
].Device
.pages_number
*
274 dataflash_info
[i
].Device
.pages_size
,
275 (unsigned int) dataflash_info
[i
].logical_address
);
276 for (j
=0; j
< NB_DATAFLASH_AREA
; j
++) {
277 switch(dataflash_info
[i
].Device
.area_list
[j
].protected) {
278 case FLAG_PROTECT_SET
:
279 case FLAG_PROTECT_CLEAR
:
280 printf("Area %i:\t%08lX to %08lX %s", j
,
281 dataflash_info
[i
].Device
.area_list
[j
].start
,
282 dataflash_info
[i
].Device
.area_list
[j
].end
,
283 (dataflash_info
[i
].Device
.area_list
[j
].protected==FLAG_PROTECT_SET
) ? "(RO)" : " ");
284 #ifdef CONFIG_NEW_DF_PARTITION
285 printf(" %s\n", dataflash_info
[i
].Device
.area_list
[j
].label
);
290 #ifdef CONFIG_NEW_DF_PARTITION
291 case FLAG_PROTECT_INVALID
:
301 /*---------------------------------------------------------------------------*/
302 /* Function Name : AT91F_DataflashSelect */
303 /* Object : Select the correct device */
304 /*---------------------------------------------------------------------------*/
305 AT91PS_DataFlash
AT91F_DataflashSelect (AT91PS_DataFlash pFlash
,
311 for (i
= 0; i
< CFG_MAX_DATAFLASH_BANKS
; i
++)
312 if ( dataflash_info
[i
].id
313 && ((((int) *addr
) & 0xFF000000) ==
314 dataflash_info
[i
].logical_address
)) {
319 pFlash
= (AT91PS_DataFlash
) 0;
322 pFlash
->pDataFlashDesc
= &(dataflash_info
[i
].Desc
);
323 pFlash
->pDevice
= &(dataflash_info
[i
].Device
);
324 *addr
-= dataflash_info
[i
].logical_address
;
328 /*---------------------------------------------------------------------------*/
329 /* Function Name : addr_dataflash */
330 /* Object : Test if address is valid */
331 /*---------------------------------------------------------------------------*/
332 int addr_dataflash (unsigned long addr
)
337 for (i
= 0; i
< CFG_MAX_DATAFLASH_BANKS
; i
++) {
338 if ((((int) addr
) & 0xFF000000) ==
339 dataflash_info
[i
].logical_address
) {
347 /*---------------------------------------------------------------------------*/
348 /* Function Name : size_dataflash */
349 /* Object : Test if address is valid regarding the size */
350 /*---------------------------------------------------------------------------*/
351 int size_dataflash (AT91PS_DataFlash pdataFlash
, unsigned long addr
,
354 /* is outside the dataflash */
355 if (((int)addr
& 0x0FFFFFFF) > (pdataFlash
->pDevice
->pages_size
*
356 pdataFlash
->pDevice
->pages_number
)) return 0;
357 /* is too large for the dataflash */
358 if (size
> ((pdataFlash
->pDevice
->pages_size
*
359 pdataFlash
->pDevice
->pages_number
) -
360 ((int)addr
& 0x0FFFFFFF))) return 0;
364 /*---------------------------------------------------------------------------*/
365 /* Function Name : prot_dataflash */
366 /* Object : Test if destination area is protected */
367 /*---------------------------------------------------------------------------*/
368 int prot_dataflash (AT91PS_DataFlash pdataFlash
, unsigned long addr
)
372 for (area
=0; area
< NB_DATAFLASH_AREA
; area
++) {
373 if ((addr
>= pdataFlash
->pDevice
->area_list
[area
].start
) &&
374 (addr
< pdataFlash
->pDevice
->area_list
[area
].end
))
377 if (area
== NB_DATAFLASH_AREA
)
380 /*test protection value*/
381 if (pdataFlash
->pDevice
->area_list
[area
].protected == FLAG_PROTECT_SET
)
383 if (pdataFlash
->pDevice
->area_list
[area
].protected == FLAG_PROTECT_INVALID
)
388 /*--------------------------------------------------------------------------*/
389 /* Function Name : dataflash_real_protect */
390 /* Object : protect/unprotect area */
391 /*--------------------------------------------------------------------------*/
392 int dataflash_real_protect (int flag
, unsigned long start_addr
,
393 unsigned long end_addr
)
395 int i
,j
, area1
, area2
, addr_valid
= 0;
397 for (i
= 0; i
< CFG_MAX_DATAFLASH_BANKS
; i
++) {
398 if ((((int) start_addr
) & 0xF0000000) ==
399 dataflash_info
[i
].logical_address
) {
407 /* find start area */
408 for (area1
=0; area1
< NB_DATAFLASH_AREA
; area1
++) {
409 if (start_addr
== dataflash_info
[i
].Device
.area_list
[area1
].start
)
412 if (area1
== NB_DATAFLASH_AREA
) return -1;
414 for (area2
=0; area2
< NB_DATAFLASH_AREA
; area2
++) {
415 if (end_addr
== dataflash_info
[i
].Device
.area_list
[area2
].end
)
418 if (area2
== NB_DATAFLASH_AREA
)
421 /*set protection value*/
422 for(j
= area1
; j
< area2
+1 ; j
++)
423 if(dataflash_info
[i
].Device
.area_list
[j
].protected
424 != FLAG_PROTECT_INVALID
) {
426 dataflash_info
[i
].Device
.area_list
[j
].protected
427 = FLAG_PROTECT_CLEAR
;
429 dataflash_info
[i
].Device
.area_list
[j
].protected
434 return (area2
-area1
+1);
437 /*---------------------------------------------------------------------------*/
438 /* Function Name : read_dataflash */
439 /* Object : dataflash memory read */
440 /*---------------------------------------------------------------------------*/
441 int read_dataflash (unsigned long addr
, unsigned long size
, char *result
)
443 unsigned long AddrToRead
= addr
;
444 AT91PS_DataFlash pFlash
= &DataFlashInst
;
446 pFlash
= AT91F_DataflashSelect (pFlash
, &AddrToRead
);
449 return ERR_UNKNOWN_FLASH_TYPE
;
451 if (size_dataflash(pFlash
,addr
,size
) == 0)
454 return (AT91F_DataFlashRead (pFlash
, AddrToRead
, size
, result
));
458 /*---------------------------------------------------------------------------*/
459 /* Function Name : write_dataflash */
460 /* Object : write a block in dataflash */
461 /*---------------------------------------------------------------------------*/
462 int write_dataflash (unsigned long addr_dest
, unsigned long addr_src
,
465 unsigned long AddrToWrite
= addr_dest
;
466 AT91PS_DataFlash pFlash
= &DataFlashInst
;
468 pFlash
= AT91F_DataflashSelect (pFlash
, &AddrToWrite
);
471 return ERR_UNKNOWN_FLASH_TYPE
;
473 if (size_dataflash(pFlash
,addr_dest
,size
) == 0)
476 if (prot_dataflash(pFlash
,addr_dest
) == 0)
477 return ERR_PROTECTED
;
479 if (AddrToWrite
== -1)
482 return AT91F_DataFlashWrite (pFlash
, (uchar
*)addr_src
,
487 void dataflash_perror (int err
)
493 printf("Timeout writing to DataFlash\n");
496 printf("Can't write to protected/invalid DataFlash sectors\n");
499 printf("Outside available DataFlash\n");
501 case ERR_UNKNOWN_FLASH_TYPE
:
502 printf("Unknown Type of DataFlash\n");
505 printf("General DataFlash Programming Error\n");
508 printf("%s[%d] FIXME: rc=%d\n", __FILE__
, __LINE__
, err
);