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 #include <asm/hardware.h>
23 #include <dataflash.h>
25 static AT91S_DataFlash DataFlashInst;
27 extern void AT91F_SpiInit (void);
28 extern int AT91F_DataflashProbe (int i, AT91PS_DataflashDesc pDesc);
29 extern int AT91F_DataFlashRead (AT91PS_DataFlash pDataFlash,
31 unsigned long size, char *buffer);
32 extern int AT91F_DataFlashWrite( AT91PS_DataFlash pDataFlash,
37 int AT91F_DataflashInit (void)
43 int found[CFG_MAX_DATAFLASH_BANKS];
44 unsigned char protected;
48 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
50 dataflash_info[i].Desc.state = IDLE;
51 dataflash_info[i].id = 0;
52 dataflash_info[i].Device.pages_number = 0;
53 dfcode = AT91F_DataflashProbe (cs[i].cs,
54 &dataflash_info[i].Desc);
58 dataflash_info[i].Device.pages_number = 1024;
59 dataflash_info[i].Device.pages_size = 264;
60 dataflash_info[i].Device.page_offset = 9;
61 dataflash_info[i].Device.byte_mask = 0x300;
62 dataflash_info[i].Device.cs = cs[i].cs;
63 dataflash_info[i].Desc.DataFlash_state = IDLE;
64 dataflash_info[i].logical_address = cs[i].addr;
65 dataflash_info[i].id = dfcode;
70 dataflash_info[i].Device.pages_number = 4096;
71 dataflash_info[i].Device.pages_size = 264;
72 dataflash_info[i].Device.page_offset = 9;
73 dataflash_info[i].Device.byte_mask = 0x300;
74 dataflash_info[i].Device.cs = cs[i].cs;
75 dataflash_info[i].Desc.DataFlash_state = IDLE;
76 dataflash_info[i].logical_address = cs[i].addr;
77 dataflash_info[i].id = dfcode;
82 dataflash_info[i].Device.pages_number = 4096;
83 dataflash_info[i].Device.pages_size = 528;
84 dataflash_info[i].Device.page_offset = 10;
85 dataflash_info[i].Device.byte_mask = 0x300;
86 dataflash_info[i].Device.cs = cs[i].cs;
87 dataflash_info[i].Desc.DataFlash_state = IDLE;
88 dataflash_info[i].logical_address = cs[i].addr;
89 dataflash_info[i].id = dfcode;
94 dataflash_info[i].Device.pages_number = 8192;
95 dataflash_info[i].Device.pages_size = 528;
96 dataflash_info[i].Device.page_offset = 10;
97 dataflash_info[i].Device.byte_mask = 0x300;
98 dataflash_info[i].Device.cs = cs[i].cs;
99 dataflash_info[i].Desc.DataFlash_state = IDLE;
100 dataflash_info[i].logical_address = cs[i].addr;
101 dataflash_info[i].id = dfcode;
106 dataflash_info[i].Device.pages_number = 8192;
107 dataflash_info[i].Device.pages_size = 1056;
108 dataflash_info[i].Device.page_offset = 11;
109 dataflash_info[i].Device.byte_mask = 0x700;
110 dataflash_info[i].Device.cs = cs[i].cs;
111 dataflash_info[i].Desc.DataFlash_state = IDLE;
112 dataflash_info[i].logical_address = cs[i].addr;
113 dataflash_info[i].id = dfcode;
118 dataflash_info[i].Device.pages_number = 16384;
119 dataflash_info[i].Device.pages_size = 1056;
120 dataflash_info[i].Device.page_offset = 11;
121 dataflash_info[i].Device.byte_mask = 0x700;
122 dataflash_info[i].Device.cs = cs[i].cs;
123 dataflash_info[i].Desc.DataFlash_state = IDLE;
124 dataflash_info[i].logical_address = cs[i].addr;
125 dataflash_info[i].id = dfcode;
133 /* set the last area end to the dataflash size*/
134 area_list[NB_DATAFLASH_AREA -1].end =
135 (dataflash_info[i].Device.pages_number *
136 dataflash_info[i].Device.pages_size)-1;
140 /* set the area addresses */
141 for(j = 0; j<NB_DATAFLASH_AREA; j++) {
143 dataflash_info[i].Device.area_list[j].start =
144 area_list[part].start +
145 dataflash_info[i].logical_address;
146 if(area_list[part].end == 0xffffffff) {
147 dataflash_info[i].Device.area_list[j].end =
148 dataflash_info[i].end_address +
149 dataflash_info [i].logical_address;
152 dataflash_info[i].Device.area_list[j].end =
153 area_list[part].end +
154 dataflash_info[i].logical_address;
156 protected = area_list[part].protected;
157 /* Set the environment according to the label...*/
158 if(protected == FLAG_PROTECT_INVALID) {
159 dataflash_info[i].Device.area_list[j].protected =
160 FLAG_PROTECT_INVALID;
162 dataflash_info[i].Device.area_list[j].protected =
165 strcpy((char*)(dataflash_info[i].Device.area_list[j].label),
166 (const char *)area_list[part].label);
174 void AT91F_DataflashSetEnv (void)
179 unsigned char s[32]; /* Will fit a long int in hex */
182 for (i = 0, part= 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
183 for(j = 0; j<NB_DATAFLASH_AREA; j++) {
184 env = area_list[part].setenv;
185 /* Set the environment according to the label...*/
186 if((env & FLAG_SETENV) == FLAG_SETENV) {
188 dataflash_info[i].Device.area_list[j].start;
189 sprintf((char*) s,"%lX",start);
190 setenv((char*) area_list[part].label,(char*) s);
197 void dataflash_print_info (void)
201 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
202 if (dataflash_info[i].id != 0) {
203 printf("DataFlash:");
204 switch (dataflash_info[i].id) {
206 printf("AT45DB021\n");
209 printf("AT45DB161\n");
213 printf("AT45DB321\n");
217 printf("AT45DB642\n");
220 printf("AT45DB128\n");
224 printf("Nb pages: %6d\n"
227 "Logical address: 0x%08X\n",
228 (unsigned int) dataflash_info[i].Device.pages_number,
229 (unsigned int) dataflash_info[i].Device.pages_size,
230 (unsigned int) dataflash_info[i].Device.pages_number *
231 dataflash_info[i].Device.pages_size,
232 (unsigned int) dataflash_info[i].logical_address);
233 for (j=0; j< NB_DATAFLASH_AREA; j++) {
234 switch(dataflash_info[i].Device.area_list[j].protected) {
235 case FLAG_PROTECT_SET:
236 case FLAG_PROTECT_CLEAR:
237 printf("Area %i:\t%08lX to %08lX %s", j,
238 dataflash_info[i].Device.area_list[j].start,
239 dataflash_info[i].Device.area_list[j].end,
240 (dataflash_info[i].Device.area_list[j].protected==FLAG_PROTECT_SET) ? "(RO)" : " ");
241 printf(" %s\n", dataflash_info[i].Device.area_list[j].label);
243 case FLAG_PROTECT_INVALID:
251 /*---------------------------------------------------------------------------*/
252 /* Function Name : AT91F_DataflashSelect */
253 /* Object : Select the correct device */
254 /*---------------------------------------------------------------------------*/
255 AT91PS_DataFlash AT91F_DataflashSelect (AT91PS_DataFlash pFlash,
261 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++)
262 if ( dataflash_info[i].id
263 && ((((int) *addr) & 0xFF000000) ==
264 dataflash_info[i].logical_address)) {
269 pFlash = (AT91PS_DataFlash) 0;
272 pFlash->pDataFlashDesc = &(dataflash_info[i].Desc);
273 pFlash->pDevice = &(dataflash_info[i].Device);
274 *addr -= dataflash_info[i].logical_address;
278 /*---------------------------------------------------------------------------*/
279 /* Function Name : addr_dataflash */
280 /* Object : Test if address is valid */
281 /*---------------------------------------------------------------------------*/
282 int addr_dataflash (unsigned long addr)
287 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
288 if ((((int) addr) & 0xFF000000) ==
289 dataflash_info[i].logical_address) {
298 /*---------------------------------------------------------------------------*/
299 /* Function Name : size_dataflash */
300 /* Object : Test if address is valid regarding the size */
301 /*---------------------------------------------------------------------------*/
302 int size_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr,
305 /* is outside the dataflash */
306 if (((int)addr & 0x0FFFFFFF) > (pdataFlash->pDevice->pages_size *
307 pdataFlash->pDevice->pages_number)) return 0;
308 /* is too large for the dataflash */
309 if (size > ((pdataFlash->pDevice->pages_size *
310 pdataFlash->pDevice->pages_number) -
311 ((int)addr & 0x0FFFFFFF))) return 0;
316 /*---------------------------------------------------------------------------*/
317 /* Function Name : prot_dataflash */
318 /* Object : Test if destination area is protected */
319 /*---------------------------------------------------------------------------*/
320 int prot_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr)
325 for (area=0; area < NB_DATAFLASH_AREA; area++) {
326 if ((addr >= pdataFlash->pDevice->area_list[area].start) &&
327 (addr < pdataFlash->pDevice->area_list[area].end))
330 if (area == NB_DATAFLASH_AREA)
333 /*test protection value*/
334 if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_SET)
336 if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_INVALID)
342 /*--------------------------------------------------------------------------*/
343 /* Function Name : dataflash_real_protect */
344 /* Object : protect/unprotect area */
345 /*--------------------------------------------------------------------------*/
346 int dataflash_real_protect (int flag, unsigned long start_addr,
347 unsigned long end_addr)
349 int i,j, area1, area2, addr_valid = 0;
352 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
353 if ((((int) start_addr) & 0xF0000000) ==
354 dataflash_info[i].logical_address) {
362 /* find start area */
363 for (area1=0; area1 < NB_DATAFLASH_AREA; area1++) {
364 if (start_addr == dataflash_info[i].Device.area_list[area1].start)
367 if (area1 == NB_DATAFLASH_AREA) return -1;
369 for (area2=0; area2 < NB_DATAFLASH_AREA; area2++) {
370 if (end_addr == dataflash_info[i].Device.area_list[area2].end)
373 if (area2 == NB_DATAFLASH_AREA)
376 /*set protection value*/
377 for(j = area1; j < area2+1 ; j++)
378 if(dataflash_info[i].Device.area_list[j].protected
379 != FLAG_PROTECT_INVALID) {
381 dataflash_info[i].Device.area_list[j].protected
382 = FLAG_PROTECT_CLEAR;
384 dataflash_info[i].Device.area_list[j].protected
389 return (area2-area1+1);
392 /*---------------------------------------------------------------------------*/
393 /* Function Name : read_dataflash */
394 /* Object : dataflash memory read */
395 /*---------------------------------------------------------------------------*/
396 int read_dataflash (unsigned long addr, unsigned long size, char *result)
398 unsigned long AddrToRead = addr;
399 AT91PS_DataFlash pFlash = &DataFlashInst;
401 pFlash = AT91F_DataflashSelect (pFlash, &AddrToRead);
404 return ERR_UNKNOWN_FLASH_TYPE;
406 if (size_dataflash(pFlash,addr,size) == 0)
409 return (AT91F_DataFlashRead (pFlash, AddrToRead, size, result));
412 /*---------------------------------------------------------------------------*/
413 /* Function Name : write_dataflash */
414 /* Object : write a block in dataflash */
415 /*---------------------------------------------------------------------------*/
416 int write_dataflash (unsigned long addr_dest, unsigned long addr_src,
419 unsigned long AddrToWrite = addr_dest;
420 AT91PS_DataFlash pFlash = &DataFlashInst;
422 pFlash = AT91F_DataflashSelect (pFlash, &AddrToWrite);
425 return ERR_UNKNOWN_FLASH_TYPE;
427 if (size_dataflash(pFlash,addr_dest,size) == 0)
430 if (prot_dataflash(pFlash,addr_dest) == 0)
431 return ERR_PROTECTED;
433 if (AddrToWrite == -1)
436 return AT91F_DataFlashWrite (pFlash, (uchar *)addr_src,
440 void dataflash_perror (int err)
446 printf("Timeout writing to DataFlash\n");
449 printf("Can't write to protected/invalid DataFlash sectors\n");
452 printf("Outside available DataFlash\n");
454 case ERR_UNKNOWN_FLASH_TYPE:
455 printf("Unknown Type of DataFlash\n");
458 printf("General DataFlash Programming Error\n");
461 printf("%s[%d] FIXME: rc=%d\n", __FILE__, __LINE__, err);