2 * LowLevel function for ATMEL DataFlash support
3 * Author : Hamid Ikdoumi (Atmel)
5 * SPDX-License-Identifier: GPL-2.0+
9 #include <asm/hardware.h>
10 #include <dataflash.h>
12 static AT91S_DataFlash DataFlashInst;
14 extern void AT91F_SpiInit (void);
15 extern int AT91F_DataflashProbe (int i, AT91PS_DataflashDesc pDesc);
16 extern int AT91F_DataFlashRead (AT91PS_DataFlash pDataFlash,
18 unsigned long size, char *buffer);
19 extern int AT91F_DataFlashWrite( AT91PS_DataFlash pDataFlash,
24 int AT91F_DataflashInit (void)
29 int found[CONFIG_SYS_MAX_DATAFLASH_BANKS];
30 unsigned char protected;
34 for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
36 dataflash_info[i].Desc.state = IDLE;
37 dataflash_info[i].id = 0;
38 dataflash_info[i].Device.pages_number = 0;
39 dfcode = AT91F_DataflashProbe (cs[i].cs,
40 &dataflash_info[i].Desc);
44 dataflash_info[i].Device.pages_number = 1024;
45 dataflash_info[i].Device.pages_size = 264;
46 dataflash_info[i].Device.page_offset = 9;
47 dataflash_info[i].Device.byte_mask = 0x300;
48 dataflash_info[i].Device.cs = cs[i].cs;
49 dataflash_info[i].Desc.DataFlash_state = IDLE;
50 dataflash_info[i].logical_address = cs[i].addr;
51 dataflash_info[i].id = dfcode;
56 dataflash_info[i].Device.pages_number = 4096;
57 dataflash_info[i].Device.pages_size = 264;
58 dataflash_info[i].Device.page_offset = 9;
59 dataflash_info[i].Device.byte_mask = 0x300;
60 dataflash_info[i].Device.cs = cs[i].cs;
61 dataflash_info[i].Desc.DataFlash_state = IDLE;
62 dataflash_info[i].logical_address = cs[i].addr;
63 dataflash_info[i].id = dfcode;
68 dataflash_info[i].Device.pages_number = 4096;
69 dataflash_info[i].Device.pages_size = 528;
70 dataflash_info[i].Device.page_offset = 10;
71 dataflash_info[i].Device.byte_mask = 0x300;
72 dataflash_info[i].Device.cs = cs[i].cs;
73 dataflash_info[i].Desc.DataFlash_state = IDLE;
74 dataflash_info[i].logical_address = cs[i].addr;
75 dataflash_info[i].id = dfcode;
80 dataflash_info[i].Device.pages_number = 8192;
81 dataflash_info[i].Device.pages_size = 528;
82 dataflash_info[i].Device.page_offset = 10;
83 dataflash_info[i].Device.byte_mask = 0x300;
84 dataflash_info[i].Device.cs = cs[i].cs;
85 dataflash_info[i].Desc.DataFlash_state = IDLE;
86 dataflash_info[i].logical_address = cs[i].addr;
87 dataflash_info[i].id = dfcode;
92 dataflash_info[i].Device.pages_number = 8192;
93 dataflash_info[i].Device.pages_size = 1056;
94 dataflash_info[i].Device.page_offset = 11;
95 dataflash_info[i].Device.byte_mask = 0x700;
96 dataflash_info[i].Device.cs = cs[i].cs;
97 dataflash_info[i].Desc.DataFlash_state = IDLE;
98 dataflash_info[i].logical_address = cs[i].addr;
99 dataflash_info[i].id = dfcode;
104 dataflash_info[i].Device.pages_number = 16384;
105 dataflash_info[i].Device.pages_size = 1056;
106 dataflash_info[i].Device.page_offset = 11;
107 dataflash_info[i].Device.byte_mask = 0x700;
108 dataflash_info[i].Device.cs = cs[i].cs;
109 dataflash_info[i].Desc.DataFlash_state = IDLE;
110 dataflash_info[i].logical_address = cs[i].addr;
111 dataflash_info[i].id = dfcode;
119 /* set the last area end to the dataflash size*/
120 dataflash_info[i].end_address =
121 (dataflash_info[i].Device.pages_number *
122 dataflash_info[i].Device.pages_size) - 1;
125 /* set the area addresses */
126 for(j = 0; j < NB_DATAFLASH_AREA; j++) {
128 dataflash_info[i].Device.area_list[j].start =
129 area_list[part].start +
130 dataflash_info[i].logical_address;
131 if(area_list[part].end == 0xffffffff) {
132 dataflash_info[i].Device.area_list[j].end =
133 dataflash_info[i].end_address +
134 dataflash_info[i].logical_address;
136 dataflash_info[i].Device.area_list[j].end =
137 area_list[part].end +
138 dataflash_info[i].logical_address;
140 protected = area_list[part].protected;
141 /* Set the environment according to the label...*/
142 if(protected == FLAG_PROTECT_INVALID) {
143 dataflash_info[i].Device.area_list[j].protected =
144 FLAG_PROTECT_INVALID;
146 dataflash_info[i].Device.area_list[j].protected =
149 strcpy((char*)(dataflash_info[i].Device.area_list[j].label),
150 (const char *)area_list[part].label);
158 void AT91F_DataflashSetEnv (void)
163 unsigned char s[32]; /* Will fit a long int in hex */
166 for (i = 0, part= 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
167 for(j = 0; j < NB_DATAFLASH_AREA; j++) {
168 env = area_list[part].setenv;
169 /* Set the environment according to the label...*/
170 if((env & FLAG_SETENV) == FLAG_SETENV) {
171 start = dataflash_info[i].Device.area_list[j].start;
172 sprintf((char*) s,"%lX",start);
173 setenv((char*) area_list[part].label,(char*) s);
180 void dataflash_print_info (void)
184 for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
185 if (dataflash_info[i].id != 0) {
186 printf("DataFlash:");
187 switch (dataflash_info[i].id) {
189 printf("AT45DB021\n");
192 printf("AT45DB161\n");
196 printf("AT45DB321\n");
200 printf("AT45DB642\n");
203 printf("AT45DB128\n");
207 printf("Nb pages: %6d\n"
210 "Logical address: 0x%08X\n",
211 (unsigned int) dataflash_info[i].Device.pages_number,
212 (unsigned int) dataflash_info[i].Device.pages_size,
213 (unsigned int) dataflash_info[i].Device.pages_number *
214 dataflash_info[i].Device.pages_size,
215 (unsigned int) dataflash_info[i].logical_address);
216 for (j = 0; j < NB_DATAFLASH_AREA; j++) {
217 switch(dataflash_info[i].Device.area_list[j].protected) {
218 case FLAG_PROTECT_SET:
219 case FLAG_PROTECT_CLEAR:
220 printf("Area %i:\t%08lX to %08lX %s", j,
221 dataflash_info[i].Device.area_list[j].start,
222 dataflash_info[i].Device.area_list[j].end,
223 (dataflash_info[i].Device.area_list[j].protected==FLAG_PROTECT_SET) ? "(RO)" : " ");
224 printf(" %s\n", dataflash_info[i].Device.area_list[j].label);
226 case FLAG_PROTECT_INVALID:
234 /*---------------------------------------------------------------------------*/
235 /* Function Name : AT91F_DataflashSelect */
236 /* Object : Select the correct device */
237 /*---------------------------------------------------------------------------*/
238 AT91PS_DataFlash AT91F_DataflashSelect (AT91PS_DataFlash pFlash,
244 for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++)
245 if ( dataflash_info[i].id
246 && ((((int) *addr) & 0xFF000000) ==
247 dataflash_info[i].logical_address)) {
252 pFlash = (AT91PS_DataFlash) 0;
255 pFlash->pDataFlashDesc = &(dataflash_info[i].Desc);
256 pFlash->pDevice = &(dataflash_info[i].Device);
257 *addr -= dataflash_info[i].logical_address;
261 /*---------------------------------------------------------------------------*/
262 /* Function Name : addr_dataflash */
263 /* Object : Test if address is valid */
264 /*---------------------------------------------------------------------------*/
265 int addr_dataflash (unsigned long addr)
270 for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
271 if ((((int) addr) & 0xFF000000) ==
272 dataflash_info[i].logical_address) {
281 /*---------------------------------------------------------------------------*/
282 /* Function Name : size_dataflash */
283 /* Object : Test if address is valid regarding the size */
284 /*---------------------------------------------------------------------------*/
285 int size_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr,
288 /* is outside the dataflash */
289 if (((int)addr & 0x0FFFFFFF) > (pdataFlash->pDevice->pages_size *
290 pdataFlash->pDevice->pages_number)) return 0;
291 /* is too large for the dataflash */
292 if (size > ((pdataFlash->pDevice->pages_size *
293 pdataFlash->pDevice->pages_number) -
294 ((int)addr & 0x0FFFFFFF))) return 0;
299 /*---------------------------------------------------------------------------*/
300 /* Function Name : prot_dataflash */
301 /* Object : Test if destination area is protected */
302 /*---------------------------------------------------------------------------*/
303 int prot_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr)
308 for (area = 0; area < NB_DATAFLASH_AREA; area++) {
309 if ((addr >= pdataFlash->pDevice->area_list[area].start) &&
310 (addr < pdataFlash->pDevice->area_list[area].end))
313 if (area == NB_DATAFLASH_AREA)
316 /*test protection value*/
317 if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_SET)
319 if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_INVALID)
325 /*--------------------------------------------------------------------------*/
326 /* Function Name : dataflash_real_protect */
327 /* Object : protect/unprotect area */
328 /*--------------------------------------------------------------------------*/
329 int dataflash_real_protect (int flag, unsigned long start_addr,
330 unsigned long end_addr)
332 int i,j, area1, area2, addr_valid = 0;
335 for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
336 if ((((int) start_addr) & 0xF0000000) ==
337 dataflash_info[i].logical_address) {
345 /* find start area */
346 for (area1 = 0; area1 < NB_DATAFLASH_AREA; area1++) {
347 if (start_addr == dataflash_info[i].Device.area_list[area1].start)
350 if (area1 == NB_DATAFLASH_AREA) return -1;
352 for (area2 = 0; area2 < NB_DATAFLASH_AREA; area2++) {
353 if (end_addr == dataflash_info[i].Device.area_list[area2].end)
356 if (area2 == NB_DATAFLASH_AREA)
359 /*set protection value*/
360 for(j = area1; j < area2 + 1 ; j++)
361 if(dataflash_info[i].Device.area_list[j].protected
362 != FLAG_PROTECT_INVALID) {
364 dataflash_info[i].Device.area_list[j].protected
365 = FLAG_PROTECT_CLEAR;
367 dataflash_info[i].Device.area_list[j].protected
372 return (area2 - area1 + 1);
375 /*---------------------------------------------------------------------------*/
376 /* Function Name : read_dataflash */
377 /* Object : dataflash memory read */
378 /*---------------------------------------------------------------------------*/
379 int read_dataflash (unsigned long addr, unsigned long size, char *result)
381 unsigned long AddrToRead = addr;
382 AT91PS_DataFlash pFlash = &DataFlashInst;
384 pFlash = AT91F_DataflashSelect (pFlash, &AddrToRead);
387 return ERR_UNKNOWN_FLASH_TYPE;
389 if (size_dataflash(pFlash,addr,size) == 0)
392 return (AT91F_DataFlashRead (pFlash, AddrToRead, size, result));
395 /*---------------------------------------------------------------------------*/
396 /* Function Name : write_dataflash */
397 /* Object : write a block in dataflash */
398 /*---------------------------------------------------------------------------*/
399 int write_dataflash (unsigned long addr_dest, unsigned long addr_src,
402 unsigned long AddrToWrite = addr_dest;
403 AT91PS_DataFlash pFlash = &DataFlashInst;
405 pFlash = AT91F_DataflashSelect (pFlash, &AddrToWrite);
408 return ERR_UNKNOWN_FLASH_TYPE;
410 if (size_dataflash(pFlash,addr_dest,size) == 0)
413 if (prot_dataflash(pFlash,addr_dest) == 0)
414 return ERR_PROTECTED;
416 if (AddrToWrite == -1)
419 return AT91F_DataFlashWrite (pFlash, (uchar *)addr_src,
423 void dataflash_perror (int err)
429 printf("Timeout writing to DataFlash\n");
432 printf("Can't write to protected/invalid DataFlash sectors\n");
435 printf("Outside available DataFlash\n");
437 case ERR_UNKNOWN_FLASH_TYPE:
438 printf("Unknown Type of DataFlash\n");
441 printf("General DataFlash Programming Error\n");
444 printf("%s[%d] FIXME: rc=%d\n", __FILE__, __LINE__, err);