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 #if defined(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 dataflash_protect_t area_list[NB_DATAFLASH_AREA] = {
52 {0x00000000, 0x000041FF, FLAG_PROTECT_SET, 0, "Bootstrap"},
53 {0x00004200, 0x000083FF, FLAG_PROTECT_CLEAR, 0, "Environment"},
54 {0x00008400, 0x0003DDFF, FLAG_PROTECT_SET, 0, "U-Boot"},
55 {0x0003DE00, 0x0023DE3F, FLAG_PROTECT_CLEAR, 0, "Kernel"},
56 {0x0023DE40, 0xFFFFFFFF, FLAG_PROTECT_CLEAR, 0, "FS"},
59 extern void AT91F_SpiInit (void);
60 extern int AT91F_DataflashProbe (int i, AT91PS_DataflashDesc pDesc);
61 extern int AT91F_DataFlashRead (AT91PS_DataFlash pDataFlash,
63 unsigned long size, char *buffer);
64 extern int AT91F_DataFlashWrite( AT91PS_DataFlash pDataFlash,
69 int AT91F_DataflashInit (void)
75 int found[CFG_MAX_DATAFLASH_BANKS];
76 unsigned char protected;
80 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
82 dataflash_info[i].Desc.state = IDLE;
83 dataflash_info[i].id = 0;
84 dataflash_info[i].Device.pages_number = 0;
85 dfcode = AT91F_DataflashProbe (cs[i].cs,
86 &dataflash_info[i].Desc);
90 dataflash_info[i].Device.pages_number = 4096;
91 dataflash_info[i].Device.pages_size = 528;
92 dataflash_info[i].Device.page_offset = 10;
93 dataflash_info[i].Device.byte_mask = 0x300;
94 dataflash_info[i].Device.cs = cs[i].cs;
95 dataflash_info[i].Desc.DataFlash_state = IDLE;
96 dataflash_info[i].logical_address = cs[i].addr;
97 dataflash_info[i].id = dfcode;
102 dataflash_info[i].Device.pages_number = 8192;
103 dataflash_info[i].Device.pages_size = 528;
104 dataflash_info[i].Device.page_offset = 10;
105 dataflash_info[i].Device.byte_mask = 0x300;
106 dataflash_info[i].Device.cs = cs[i].cs;
107 dataflash_info[i].Desc.DataFlash_state = IDLE;
108 dataflash_info[i].logical_address = cs[i].addr;
109 dataflash_info[i].id = dfcode;
114 dataflash_info[i].Device.pages_number = 8192;
115 dataflash_info[i].Device.pages_size = 1056;
116 dataflash_info[i].Device.page_offset = 11;
117 dataflash_info[i].Device.byte_mask = 0x700;
118 dataflash_info[i].Device.cs = cs[i].cs;
119 dataflash_info[i].Desc.DataFlash_state = IDLE;
120 dataflash_info[i].logical_address = cs[i].addr;
121 dataflash_info[i].id = dfcode;
126 dataflash_info[i].Device.pages_number = 16384;
127 dataflash_info[i].Device.pages_size = 1056;
128 dataflash_info[i].Device.page_offset = 11;
129 dataflash_info[i].Device.byte_mask = 0x700;
130 dataflash_info[i].Device.cs = cs[i].cs;
131 dataflash_info[i].Desc.DataFlash_state = IDLE;
132 dataflash_info[i].logical_address = cs[i].addr;
133 dataflash_info[i].id = dfcode;
141 /* set the last area end to the dataflash size*/
142 area_list[NB_DATAFLASH_AREA -1].end =
143 (dataflash_info[i].Device.pages_number *
144 dataflash_info[i].Device.pages_size)-1;
148 /* set the area addresses */
149 for(j = 0; j<NB_DATAFLASH_AREA; j++) {
151 dataflash_info[i].Device.area_list[j].start =
152 area_list[part].start +
153 dataflash_info[i].logical_address;
154 if(area_list[part].end == 0xffffffff) {
155 dataflash_info[i].Device.area_list[j].end =
156 dataflash_info[i].end_address +
157 dataflash_info [i].logical_address;
160 dataflash_info[i].Device.area_list[j].end =
161 area_list[part].end +
162 dataflash_info[i].logical_address;
164 protected = area_list[part].protected;
165 /* Set the environment according to the label...*/
166 if(protected == FLAG_PROTECT_INVALID) {
167 dataflash_info[i].Device.area_list[j].protected =
168 FLAG_PROTECT_INVALID;
170 dataflash_info[i].Device.area_list[j].protected =
173 strcpy((char*)(dataflash_info[i].Device.area_list[j].label),
174 (const char *)area_list[part].label);
182 #ifdef CONFIG_NEW_DF_PARTITION
183 int AT91F_DataflashSetEnv (void)
188 unsigned char s[32]; /* Will fit a long int in hex */
191 for (i = 0, part= 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
192 for(j = 0; j<NB_DATAFLASH_AREA; j++) {
193 env = area_list[part].setenv;
194 /* Set the environment according to the label...*/
195 if((env & FLAG_SETENV) == FLAG_SETENV) {
197 dataflash_info[i].Device.area_list[j].start;
198 sprintf(s,"%X",start);
199 setenv(area_list[part].label,s);
207 void dataflash_print_info (void)
211 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
212 if (dataflash_info[i].id != 0) {
213 printf("DataFlash:");
214 switch (dataflash_info[i].id) {
216 printf("AT45DB161\n");
220 printf("AT45DB321\n");
224 printf("AT45DB642\n");
227 printf("AT45DB128\n");
231 printf("Nb pages: %6d\n"
234 "Logical address: 0x%08X\n",
235 (unsigned int) dataflash_info[i].Device.pages_number,
236 (unsigned int) dataflash_info[i].Device.pages_size,
237 (unsigned int) dataflash_info[i].Device.pages_number *
238 dataflash_info[i].Device.pages_size,
239 (unsigned int) dataflash_info[i].logical_address);
240 for (j=0; j< NB_DATAFLASH_AREA; j++) {
241 switch(dataflash_info[i].Device.area_list[j].protected) {
242 case FLAG_PROTECT_SET:
243 case FLAG_PROTECT_CLEAR:
244 printf("Area %i:\t%08lX to %08lX %s", j,
245 dataflash_info[i].Device.area_list[j].start,
246 dataflash_info[i].Device.area_list[j].end,
247 (dataflash_info[i].Device.area_list[j].protected==FLAG_PROTECT_SET) ? "(RO)" : " ");
248 #ifdef CONFIG_NEW_DF_PARTITION
249 printf(" %s\n", dataflash_info[i].Device.area_list[j].label);
254 #ifdef CONFIG_NEW_DF_PARTITION
255 case FLAG_PROTECT_INVALID:
264 /*---------------------------------------------------------------------------*/
265 /* Function Name : AT91F_DataflashSelect */
266 /* Object : Select the correct device */
267 /*---------------------------------------------------------------------------*/
268 AT91PS_DataFlash AT91F_DataflashSelect (AT91PS_DataFlash pFlash,
274 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++)
275 if ( dataflash_info[i].id
276 && ((((int) *addr) & 0xFF000000) ==
277 dataflash_info[i].logical_address)) {
282 pFlash = (AT91PS_DataFlash) 0;
285 pFlash->pDataFlashDesc = &(dataflash_info[i].Desc);
286 pFlash->pDevice = &(dataflash_info[i].Device);
287 *addr -= dataflash_info[i].logical_address;
291 /*---------------------------------------------------------------------------*/
292 /* Function Name : addr_dataflash */
293 /* Object : Test if address is valid */
294 /*---------------------------------------------------------------------------*/
295 int addr_dataflash (unsigned long addr)
300 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
301 if ((((int) addr) & 0xFF000000) ==
302 dataflash_info[i].logical_address) {
311 /*---------------------------------------------------------------------------*/
312 /* Function Name : size_dataflash */
313 /* Object : Test if address is valid regarding the size */
314 /*---------------------------------------------------------------------------*/
315 int size_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr,
318 /* is outside the dataflash */
319 if (((int)addr & 0x0FFFFFFF) > (pdataFlash->pDevice->pages_size *
320 pdataFlash->pDevice->pages_number)) return 0;
321 /* is too large for the dataflash */
322 if (size > ((pdataFlash->pDevice->pages_size *
323 pdataFlash->pDevice->pages_number) -
324 ((int)addr & 0x0FFFFFFF))) return 0;
329 /*---------------------------------------------------------------------------*/
330 /* Function Name : prot_dataflash */
331 /* Object : Test if destination area is protected */
332 /*---------------------------------------------------------------------------*/
333 int prot_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr)
338 for (area=0; area < NB_DATAFLASH_AREA; area++) {
339 if ((addr >= pdataFlash->pDevice->area_list[area].start) &&
340 (addr < pdataFlash->pDevice->area_list[area].end))
343 if (area == NB_DATAFLASH_AREA)
346 /*test protection value*/
347 if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_SET)
349 if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_INVALID)
355 /*--------------------------------------------------------------------------*/
356 /* Function Name : dataflash_real_protect */
357 /* Object : protect/unprotect area */
358 /*--------------------------------------------------------------------------*/
359 int dataflash_real_protect (int flag, unsigned long start_addr,
360 unsigned long end_addr)
362 int i,j, area1, area2, addr_valid = 0;
365 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
366 if ((((int) start_addr) & 0xF0000000) ==
367 dataflash_info[i].logical_address) {
375 /* find start area */
376 for (area1=0; area1 < NB_DATAFLASH_AREA; area1++) {
377 if (start_addr == dataflash_info[i].Device.area_list[area1].start)
380 if (area1 == NB_DATAFLASH_AREA) return -1;
382 for (area2=0; area2 < NB_DATAFLASH_AREA; area2++) {
383 if (end_addr == dataflash_info[i].Device.area_list[area2].end)
386 if (area2 == NB_DATAFLASH_AREA)
389 /*set protection value*/
390 for(j = area1; j < area2+1 ; j++)
391 if(dataflash_info[i].Device.area_list[j].protected
392 != FLAG_PROTECT_INVALID) {
394 dataflash_info[i].Device.area_list[j].protected
395 = FLAG_PROTECT_CLEAR;
397 dataflash_info[i].Device.area_list[j].protected
402 return (area2-area1+1);
405 /*---------------------------------------------------------------------------*/
406 /* Function Name : read_dataflash */
407 /* Object : dataflash memory read */
408 /*---------------------------------------------------------------------------*/
409 int read_dataflash (unsigned long addr, unsigned long size, char *result)
411 unsigned long AddrToRead = addr;
412 AT91PS_DataFlash pFlash = &DataFlashInst;
414 pFlash = AT91F_DataflashSelect (pFlash, &AddrToRead);
417 return ERR_UNKNOWN_FLASH_TYPE;
419 if (size_dataflash(pFlash,addr,size) == 0)
422 return (AT91F_DataFlashRead (pFlash, AddrToRead, size, result));
425 /*---------------------------------------------------------------------------*/
426 /* Function Name : write_dataflash */
427 /* Object : write a block in dataflash */
428 /*---------------------------------------------------------------------------*/
429 int write_dataflash (unsigned long addr_dest, unsigned long addr_src,
432 unsigned long AddrToWrite = addr_dest;
433 AT91PS_DataFlash pFlash = &DataFlashInst;
435 pFlash = AT91F_DataflashSelect (pFlash, &AddrToWrite);
438 return ERR_UNKNOWN_FLASH_TYPE;
440 if (size_dataflash(pFlash,addr_dest,size) == 0)
443 if (prot_dataflash(pFlash,addr_dest) == 0)
444 return ERR_PROTECTED;
446 if (AddrToWrite == -1)
449 return AT91F_DataFlashWrite (pFlash, (uchar *)addr_src,
453 void dataflash_perror (int err)
459 printf("Timeout writing to DataFlash\n");
462 printf("Can't write to protected/invalid DataFlash sectors\n");
465 printf("Outside available DataFlash\n");
467 case ERR_UNKNOWN_FLASH_TYPE:
468 printf("Unknown Type of DataFlash\n");
471 printf("General DataFlash Programming Error\n");
474 printf("%s[%d] FIXME: rc=%d\n", __FILE__, __LINE__, err);