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 = 4096;
59 dataflash_info[i].Device.pages_size = 528;
60 dataflash_info[i].Device.page_offset = 10;
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 = 8192;
71 dataflash_info[i].Device.pages_size = 528;
72 dataflash_info[i].Device.page_offset = 10;
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 = 8192;
83 dataflash_info[i].Device.pages_size = 1056;
84 dataflash_info[i].Device.page_offset = 11;
85 dataflash_info[i].Device.byte_mask = 0x700;
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 = 16384;
95 dataflash_info[i].Device.pages_size = 1056;
96 dataflash_info[i].Device.page_offset = 11;
97 dataflash_info[i].Device.byte_mask = 0x700;
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;
109 /* set the last area end to the dataflash size*/
110 area_list[NB_DATAFLASH_AREA -1].end =
111 (dataflash_info[i].Device.pages_number *
112 dataflash_info[i].Device.pages_size)-1;
116 /* set the area addresses */
117 for(j = 0; j<NB_DATAFLASH_AREA; j++) {
119 dataflash_info[i].Device.area_list[j].start =
120 area_list[part].start +
121 dataflash_info[i].logical_address;
122 if(area_list[part].end == 0xffffffff) {
123 dataflash_info[i].Device.area_list[j].end =
124 dataflash_info[i].end_address +
125 dataflash_info [i].logical_address;
128 dataflash_info[i].Device.area_list[j].end =
129 area_list[part].end +
130 dataflash_info[i].logical_address;
132 protected = area_list[part].protected;
133 /* Set the environment according to the label...*/
134 if(protected == FLAG_PROTECT_INVALID) {
135 dataflash_info[i].Device.area_list[j].protected =
136 FLAG_PROTECT_INVALID;
138 dataflash_info[i].Device.area_list[j].protected =
141 strcpy((char*)(dataflash_info[i].Device.area_list[j].label),
142 (const char *)area_list[part].label);
150 void AT91F_DataflashSetEnv (void)
155 unsigned char s[32]; /* Will fit a long int in hex */
158 for (i = 0, part= 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
159 for(j = 0; j<NB_DATAFLASH_AREA; j++) {
160 env = area_list[part].setenv;
161 /* Set the environment according to the label...*/
162 if((env & FLAG_SETENV) == FLAG_SETENV) {
164 dataflash_info[i].Device.area_list[j].start;
165 sprintf((char*) s,"%X",start);
166 setenv((char*) area_list[part].label,(char*) s);
173 void dataflash_print_info (void)
177 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
178 if (dataflash_info[i].id != 0) {
179 printf("DataFlash:");
180 switch (dataflash_info[i].id) {
182 printf("AT45DB161\n");
186 printf("AT45DB321\n");
190 printf("AT45DB642\n");
193 printf("AT45DB128\n");
197 printf("Nb pages: %6d\n"
200 "Logical address: 0x%08X\n",
201 (unsigned int) dataflash_info[i].Device.pages_number,
202 (unsigned int) dataflash_info[i].Device.pages_size,
203 (unsigned int) dataflash_info[i].Device.pages_number *
204 dataflash_info[i].Device.pages_size,
205 (unsigned int) dataflash_info[i].logical_address);
206 for (j=0; j< NB_DATAFLASH_AREA; j++) {
207 switch(dataflash_info[i].Device.area_list[j].protected) {
208 case FLAG_PROTECT_SET:
209 case FLAG_PROTECT_CLEAR:
210 printf("Area %i:\t%08lX to %08lX %s", j,
211 dataflash_info[i].Device.area_list[j].start,
212 dataflash_info[i].Device.area_list[j].end,
213 (dataflash_info[i].Device.area_list[j].protected==FLAG_PROTECT_SET) ? "(RO)" : " ");
214 printf(" %s\n", dataflash_info[i].Device.area_list[j].label);
216 case FLAG_PROTECT_INVALID:
224 /*---------------------------------------------------------------------------*/
225 /* Function Name : AT91F_DataflashSelect */
226 /* Object : Select the correct device */
227 /*---------------------------------------------------------------------------*/
228 AT91PS_DataFlash AT91F_DataflashSelect (AT91PS_DataFlash pFlash,
234 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++)
235 if ( dataflash_info[i].id
236 && ((((int) *addr) & 0xFF000000) ==
237 dataflash_info[i].logical_address)) {
242 pFlash = (AT91PS_DataFlash) 0;
245 pFlash->pDataFlashDesc = &(dataflash_info[i].Desc);
246 pFlash->pDevice = &(dataflash_info[i].Device);
247 *addr -= dataflash_info[i].logical_address;
251 /*---------------------------------------------------------------------------*/
252 /* Function Name : addr_dataflash */
253 /* Object : Test if address is valid */
254 /*---------------------------------------------------------------------------*/
255 int addr_dataflash (unsigned long addr)
260 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
261 if ((((int) addr) & 0xFF000000) ==
262 dataflash_info[i].logical_address) {
271 /*---------------------------------------------------------------------------*/
272 /* Function Name : size_dataflash */
273 /* Object : Test if address is valid regarding the size */
274 /*---------------------------------------------------------------------------*/
275 int size_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr,
278 /* is outside the dataflash */
279 if (((int)addr & 0x0FFFFFFF) > (pdataFlash->pDevice->pages_size *
280 pdataFlash->pDevice->pages_number)) return 0;
281 /* is too large for the dataflash */
282 if (size > ((pdataFlash->pDevice->pages_size *
283 pdataFlash->pDevice->pages_number) -
284 ((int)addr & 0x0FFFFFFF))) return 0;
289 /*---------------------------------------------------------------------------*/
290 /* Function Name : prot_dataflash */
291 /* Object : Test if destination area is protected */
292 /*---------------------------------------------------------------------------*/
293 int prot_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr)
298 for (area=0; area < NB_DATAFLASH_AREA; area++) {
299 if ((addr >= pdataFlash->pDevice->area_list[area].start) &&
300 (addr < pdataFlash->pDevice->area_list[area].end))
303 if (area == NB_DATAFLASH_AREA)
306 /*test protection value*/
307 if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_SET)
309 if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_INVALID)
315 /*--------------------------------------------------------------------------*/
316 /* Function Name : dataflash_real_protect */
317 /* Object : protect/unprotect area */
318 /*--------------------------------------------------------------------------*/
319 int dataflash_real_protect (int flag, unsigned long start_addr,
320 unsigned long end_addr)
322 int i,j, area1, area2, addr_valid = 0;
325 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
326 if ((((int) start_addr) & 0xF0000000) ==
327 dataflash_info[i].logical_address) {
335 /* find start area */
336 for (area1=0; area1 < NB_DATAFLASH_AREA; area1++) {
337 if (start_addr == dataflash_info[i].Device.area_list[area1].start)
340 if (area1 == NB_DATAFLASH_AREA) return -1;
342 for (area2=0; area2 < NB_DATAFLASH_AREA; area2++) {
343 if (end_addr == dataflash_info[i].Device.area_list[area2].end)
346 if (area2 == NB_DATAFLASH_AREA)
349 /*set protection value*/
350 for(j = area1; j < area2+1 ; j++)
351 if(dataflash_info[i].Device.area_list[j].protected
352 != FLAG_PROTECT_INVALID) {
354 dataflash_info[i].Device.area_list[j].protected
355 = FLAG_PROTECT_CLEAR;
357 dataflash_info[i].Device.area_list[j].protected
362 return (area2-area1+1);
365 /*---------------------------------------------------------------------------*/
366 /* Function Name : read_dataflash */
367 /* Object : dataflash memory read */
368 /*---------------------------------------------------------------------------*/
369 int read_dataflash (unsigned long addr, unsigned long size, char *result)
371 unsigned long AddrToRead = addr;
372 AT91PS_DataFlash pFlash = &DataFlashInst;
374 pFlash = AT91F_DataflashSelect (pFlash, &AddrToRead);
377 return ERR_UNKNOWN_FLASH_TYPE;
379 if (size_dataflash(pFlash,addr,size) == 0)
382 return (AT91F_DataFlashRead (pFlash, AddrToRead, size, result));
385 /*---------------------------------------------------------------------------*/
386 /* Function Name : write_dataflash */
387 /* Object : write a block in dataflash */
388 /*---------------------------------------------------------------------------*/
389 int write_dataflash (unsigned long addr_dest, unsigned long addr_src,
392 unsigned long AddrToWrite = addr_dest;
393 AT91PS_DataFlash pFlash = &DataFlashInst;
395 pFlash = AT91F_DataflashSelect (pFlash, &AddrToWrite);
398 return ERR_UNKNOWN_FLASH_TYPE;
400 if (size_dataflash(pFlash,addr_dest,size) == 0)
403 if (prot_dataflash(pFlash,addr_dest) == 0)
404 return ERR_PROTECTED;
406 if (AddrToWrite == -1)
409 return AT91F_DataFlashWrite (pFlash, (uchar *)addr_src,
413 void dataflash_perror (int err)
419 printf("Timeout writing to DataFlash\n");
422 printf("Can't write to protected/invalid DataFlash sectors\n");
425 printf("Outside available DataFlash\n");
427 case ERR_UNKNOWN_FLASH_TYPE:
428 printf("Unknown Type of DataFlash\n");
431 printf("General DataFlash Programming Error\n");
434 printf("%s[%d] FIXME: rc=%d\n", __FILE__, __LINE__, err);