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 AT91S_DATAFLASH_INFO dataflash_info[CFG_MAX_DATAFLASH_BANKS];
26 static AT91S_DataFlash DataFlashInst;
28 struct dataflash_addr {
33 #if defined(CONFIG_AT91SAM9260EK)
34 struct dataflash_addr cs[CFG_MAX_DATAFLASH_BANKS] = {
35 {CFG_DATAFLASH_LOGIC_ADDR_CS0, 0}, /* Logical adress, CS */
36 {CFG_DATAFLASH_LOGIC_ADDR_CS1, 1}
38 #elif defined(CONFIG_AT91SAM9263EK) || defined(CONFIG_AT91CAP9ADK)
39 struct dataflash_addr cs[CFG_MAX_DATAFLASH_BANKS] = {
40 {CFG_DATAFLASH_LOGIC_ADDR_CS0, 0}, /* Logical adress, CS */
43 struct dataflash_addr cs[CFG_MAX_DATAFLASH_BANKS] = {
44 {CFG_DATAFLASH_LOGIC_ADDR_CS0, 0}, /* Logical adress, CS */
45 {CFG_DATAFLASH_LOGIC_ADDR_CS3, 3}
49 /*define the area offsets*/
50 dataflash_protect_t area_list[NB_DATAFLASH_AREA] = {
51 {0x00000000, 0x000041FF, FLAG_PROTECT_SET, 0, "Bootstrap"},
52 {0x00004200, 0x000083FF, FLAG_PROTECT_CLEAR, 0, "Environment"},
53 {0x00008400, 0x0003DDFF, FLAG_PROTECT_SET, 0, "U-Boot"},
54 {0x0003DE00, 0x0023DE3F, FLAG_PROTECT_CLEAR, 0, "Kernel"},
55 {0x0023DE40, 0xFFFFFFFF, FLAG_PROTECT_CLEAR, 0, "FS"},
58 extern void AT91F_SpiInit (void);
59 extern int AT91F_DataflashProbe (int i, AT91PS_DataflashDesc pDesc);
60 extern int AT91F_DataFlashRead (AT91PS_DataFlash pDataFlash,
62 unsigned long size, char *buffer);
63 extern int AT91F_DataFlashWrite( AT91PS_DataFlash pDataFlash,
68 int AT91F_DataflashInit (void)
74 int found[CFG_MAX_DATAFLASH_BANKS];
75 unsigned char protected;
79 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
81 dataflash_info[i].Desc.state = IDLE;
82 dataflash_info[i].id = 0;
83 dataflash_info[i].Device.pages_number = 0;
84 dfcode = AT91F_DataflashProbe (cs[i].cs,
85 &dataflash_info[i].Desc);
89 dataflash_info[i].Device.pages_number = 4096;
90 dataflash_info[i].Device.pages_size = 528;
91 dataflash_info[i].Device.page_offset = 10;
92 dataflash_info[i].Device.byte_mask = 0x300;
93 dataflash_info[i].Device.cs = cs[i].cs;
94 dataflash_info[i].Desc.DataFlash_state = IDLE;
95 dataflash_info[i].logical_address = cs[i].addr;
96 dataflash_info[i].id = dfcode;
101 dataflash_info[i].Device.pages_number = 8192;
102 dataflash_info[i].Device.pages_size = 528;
103 dataflash_info[i].Device.page_offset = 10;
104 dataflash_info[i].Device.byte_mask = 0x300;
105 dataflash_info[i].Device.cs = cs[i].cs;
106 dataflash_info[i].Desc.DataFlash_state = IDLE;
107 dataflash_info[i].logical_address = cs[i].addr;
108 dataflash_info[i].id = dfcode;
113 dataflash_info[i].Device.pages_number = 8192;
114 dataflash_info[i].Device.pages_size = 1056;
115 dataflash_info[i].Device.page_offset = 11;
116 dataflash_info[i].Device.byte_mask = 0x700;
117 dataflash_info[i].Device.cs = cs[i].cs;
118 dataflash_info[i].Desc.DataFlash_state = IDLE;
119 dataflash_info[i].logical_address = cs[i].addr;
120 dataflash_info[i].id = dfcode;
125 dataflash_info[i].Device.pages_number = 16384;
126 dataflash_info[i].Device.pages_size = 1056;
127 dataflash_info[i].Device.page_offset = 11;
128 dataflash_info[i].Device.byte_mask = 0x700;
129 dataflash_info[i].Device.cs = cs[i].cs;
130 dataflash_info[i].Desc.DataFlash_state = IDLE;
131 dataflash_info[i].logical_address = cs[i].addr;
132 dataflash_info[i].id = dfcode;
140 /* set the last area end to the dataflash size*/
141 area_list[NB_DATAFLASH_AREA -1].end =
142 (dataflash_info[i].Device.pages_number *
143 dataflash_info[i].Device.pages_size)-1;
147 /* set the area addresses */
148 for(j = 0; j<NB_DATAFLASH_AREA; j++) {
150 dataflash_info[i].Device.area_list[j].start =
151 area_list[part].start +
152 dataflash_info[i].logical_address;
153 if(area_list[part].end == 0xffffffff) {
154 dataflash_info[i].Device.area_list[j].end =
155 dataflash_info[i].end_address +
156 dataflash_info [i].logical_address;
159 dataflash_info[i].Device.area_list[j].end =
160 area_list[part].end +
161 dataflash_info[i].logical_address;
163 protected = area_list[part].protected;
164 /* Set the environment according to the label...*/
165 if(protected == FLAG_PROTECT_INVALID) {
166 dataflash_info[i].Device.area_list[j].protected =
167 FLAG_PROTECT_INVALID;
169 dataflash_info[i].Device.area_list[j].protected =
172 strcpy((char*)(dataflash_info[i].Device.area_list[j].label),
173 (const char *)area_list[part].label);
181 #ifdef CONFIG_NEW_DF_PARTITION
182 int AT91F_DataflashSetEnv (void)
187 unsigned char s[32]; /* Will fit a long int in hex */
190 for (i = 0, part= 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
191 for(j = 0; j<NB_DATAFLASH_AREA; j++) {
192 env = area_list[part].setenv;
193 /* Set the environment according to the label...*/
194 if((env & FLAG_SETENV) == FLAG_SETENV) {
196 dataflash_info[i].Device.area_list[j].start;
197 sprintf(s,"%X",start);
198 setenv(area_list[part].label,s);
206 void dataflash_print_info (void)
210 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
211 if (dataflash_info[i].id != 0) {
212 printf("DataFlash:");
213 switch (dataflash_info[i].id) {
215 printf("AT45DB161\n");
219 printf("AT45DB321\n");
223 printf("AT45DB642\n");
226 printf("AT45DB128\n");
230 printf("Nb pages: %6d\n"
233 "Logical address: 0x%08X\n",
234 (unsigned int) dataflash_info[i].Device.pages_number,
235 (unsigned int) dataflash_info[i].Device.pages_size,
236 (unsigned int) dataflash_info[i].Device.pages_number *
237 dataflash_info[i].Device.pages_size,
238 (unsigned int) dataflash_info[i].logical_address);
239 for (j=0; j< NB_DATAFLASH_AREA; j++) {
240 switch(dataflash_info[i].Device.area_list[j].protected) {
241 case FLAG_PROTECT_SET:
242 case FLAG_PROTECT_CLEAR:
243 printf("Area %i:\t%08lX to %08lX %s", j,
244 dataflash_info[i].Device.area_list[j].start,
245 dataflash_info[i].Device.area_list[j].end,
246 (dataflash_info[i].Device.area_list[j].protected==FLAG_PROTECT_SET) ? "(RO)" : " ");
247 #ifdef CONFIG_NEW_DF_PARTITION
248 printf(" %s\n", dataflash_info[i].Device.area_list[j].label);
253 #ifdef CONFIG_NEW_DF_PARTITION
254 case FLAG_PROTECT_INVALID:
263 /*---------------------------------------------------------------------------*/
264 /* Function Name : AT91F_DataflashSelect */
265 /* Object : Select the correct device */
266 /*---------------------------------------------------------------------------*/
267 AT91PS_DataFlash AT91F_DataflashSelect (AT91PS_DataFlash pFlash,
273 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++)
274 if ( dataflash_info[i].id
275 && ((((int) *addr) & 0xFF000000) ==
276 dataflash_info[i].logical_address)) {
281 pFlash = (AT91PS_DataFlash) 0;
284 pFlash->pDataFlashDesc = &(dataflash_info[i].Desc);
285 pFlash->pDevice = &(dataflash_info[i].Device);
286 *addr -= dataflash_info[i].logical_address;
290 /*---------------------------------------------------------------------------*/
291 /* Function Name : addr_dataflash */
292 /* Object : Test if address is valid */
293 /*---------------------------------------------------------------------------*/
294 int addr_dataflash (unsigned long addr)
299 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
300 if ((((int) addr) & 0xFF000000) ==
301 dataflash_info[i].logical_address) {
310 /*---------------------------------------------------------------------------*/
311 /* Function Name : size_dataflash */
312 /* Object : Test if address is valid regarding the size */
313 /*---------------------------------------------------------------------------*/
314 int size_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr,
317 /* is outside the dataflash */
318 if (((int)addr & 0x0FFFFFFF) > (pdataFlash->pDevice->pages_size *
319 pdataFlash->pDevice->pages_number)) return 0;
320 /* is too large for the dataflash */
321 if (size > ((pdataFlash->pDevice->pages_size *
322 pdataFlash->pDevice->pages_number) -
323 ((int)addr & 0x0FFFFFFF))) return 0;
328 /*---------------------------------------------------------------------------*/
329 /* Function Name : prot_dataflash */
330 /* Object : Test if destination area is protected */
331 /*---------------------------------------------------------------------------*/
332 int prot_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr)
337 for (area=0; area < NB_DATAFLASH_AREA; area++) {
338 if ((addr >= pdataFlash->pDevice->area_list[area].start) &&
339 (addr < pdataFlash->pDevice->area_list[area].end))
342 if (area == NB_DATAFLASH_AREA)
345 /*test protection value*/
346 if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_SET)
348 if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_INVALID)
354 /*--------------------------------------------------------------------------*/
355 /* Function Name : dataflash_real_protect */
356 /* Object : protect/unprotect area */
357 /*--------------------------------------------------------------------------*/
358 int dataflash_real_protect (int flag, unsigned long start_addr,
359 unsigned long end_addr)
361 int i,j, area1, area2, addr_valid = 0;
364 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
365 if ((((int) start_addr) & 0xF0000000) ==
366 dataflash_info[i].logical_address) {
374 /* find start area */
375 for (area1=0; area1 < NB_DATAFLASH_AREA; area1++) {
376 if (start_addr == dataflash_info[i].Device.area_list[area1].start)
379 if (area1 == NB_DATAFLASH_AREA) return -1;
381 for (area2=0; area2 < NB_DATAFLASH_AREA; area2++) {
382 if (end_addr == dataflash_info[i].Device.area_list[area2].end)
385 if (area2 == NB_DATAFLASH_AREA)
388 /*set protection value*/
389 for(j = area1; j < area2+1 ; j++)
390 if(dataflash_info[i].Device.area_list[j].protected
391 != FLAG_PROTECT_INVALID) {
393 dataflash_info[i].Device.area_list[j].protected
394 = FLAG_PROTECT_CLEAR;
396 dataflash_info[i].Device.area_list[j].protected
401 return (area2-area1+1);
404 /*---------------------------------------------------------------------------*/
405 /* Function Name : read_dataflash */
406 /* Object : dataflash memory read */
407 /*---------------------------------------------------------------------------*/
408 int read_dataflash (unsigned long addr, unsigned long size, char *result)
410 unsigned long AddrToRead = addr;
411 AT91PS_DataFlash pFlash = &DataFlashInst;
413 pFlash = AT91F_DataflashSelect (pFlash, &AddrToRead);
416 return ERR_UNKNOWN_FLASH_TYPE;
418 if (size_dataflash(pFlash,addr,size) == 0)
421 return (AT91F_DataFlashRead (pFlash, AddrToRead, size, result));
424 /*---------------------------------------------------------------------------*/
425 /* Function Name : write_dataflash */
426 /* Object : write a block in dataflash */
427 /*---------------------------------------------------------------------------*/
428 int write_dataflash (unsigned long addr_dest, unsigned long addr_src,
431 unsigned long AddrToWrite = addr_dest;
432 AT91PS_DataFlash pFlash = &DataFlashInst;
434 pFlash = AT91F_DataflashSelect (pFlash, &AddrToWrite);
437 return ERR_UNKNOWN_FLASH_TYPE;
439 if (size_dataflash(pFlash,addr_dest,size) == 0)
442 if (prot_dataflash(pFlash,addr_dest) == 0)
443 return ERR_PROTECTED;
445 if (AddrToWrite == -1)
448 return AT91F_DataFlashWrite (pFlash, (uchar *)addr_src,
452 void dataflash_perror (int err)
458 printf("Timeout writing to DataFlash\n");
461 printf("Can't write to protected/invalid DataFlash sectors\n");
464 printf("Outside available DataFlash\n");
466 case ERR_UNKNOWN_FLASH_TYPE:
467 printf("Unknown Type of DataFlash\n");
470 printf("General DataFlash Programming Error\n");
473 printf("%s[%d] FIXME: rc=%d\n", __FILE__, __LINE__, err);