blob: 91903c8c8f739abd7c2310d3220483af6982f42f [file] [log] [blame]
wdenk381669a2003-06-16 23:50:08 +00001/* LowLevel function for ATMEL DataFlash support
2 * Author : Hamid Ikdoumi (Atmel)
3 *
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.
8 *
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.
13 *
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,
17 * MA 02111-1307 USA
18 *
19 */
20#include <common.h>
21#include <config.h>
22#ifdef CONFIG_HAS_DATAFLASH
23#include <asm/hardware.h>
24#include <dataflash.h>
25
26AT91S_DATAFLASH_INFO dataflash_info[CFG_MAX_DATAFLASH_BANKS];
27static AT91S_DataFlash DataFlashInst;
28
Peter Pearsede5b02c2007-08-14 10:10:52 +010029#ifdef CONFIG_AT91SAM9260EK
wdenk381669a2003-06-16 23:50:08 +000030int cs[][CFG_MAX_DATAFLASH_BANKS] = {
31 {CFG_DATAFLASH_LOGIC_ADDR_CS0, 0}, /* Logical adress, CS */
Peter Pearsede5b02c2007-08-14 10:10:52 +010032 {CFG_DATAFLASH_LOGIC_ADDR_CS1, 1}
33};
34#elif defined(CONFIG_AT91SAM9263EK)
35int cs[][CFG_MAX_DATAFLASH_BANKS] = {
36 {CFG_DATAFLASH_LOGIC_ADDR_CS0, 0} /* Logical adress, CS */
37};
38#else
39int cs[][CFG_MAX_DATAFLASH_BANKS] = {
40 {CFG_DATAFLASH_LOGIC_ADDR_CS0, 0}, /* Logical adress, CS */
wdenk381669a2003-06-16 23:50:08 +000041 {CFG_DATAFLASH_LOGIC_ADDR_CS3, 3}
42};
Peter Pearsede5b02c2007-08-14 10:10:52 +010043#endif
wdenk381669a2003-06-16 23:50:08 +000044
wdenk86765902003-12-06 23:55:10 +000045/*define the area offsets*/
Peter Pearsede5b02c2007-08-14 10:10:52 +010046#if defined(CONFIG_AT91SAM9261EK) || defined(CONFIG_AT91SAM9260EK) || defined(CONFIG_AT91SAM9263EK)
47#if defined(CONFIG_NEW_PARTITION)
48dataflash_protect_t area_list[NB_DATAFLASH_AREA] = {
49 {0x00000000, 0x00003FFF, FLAG_PROTECT_SET, 0, "Bootstrap"}, /* ROM code */
50 {0x00004200, 0x000083FF, FLAG_PROTECT_CLEAR, 0, "Environment"}, /* u-boot environment */
51 {0x00008400, 0x0003DDFF, FLAG_PROTECT_SET, 0, "U-Boot"}, /* u-boot code */
52 {0x0003DE00, 0x00041FFF, FLAG_PROTECT_CLEAR, FLAG_SETENV, "MON"}, /* Room for alternative boot monitor */
53 {0x00042000, 0x0018BFFF, FLAG_PROTECT_CLEAR, FLAG_SETENV, "OS"}, /* data area size to tune */
54 {0x0018C000, 0xFFFFFFFF, FLAG_PROTECT_CLEAR, FLAG_SETENV, "FS"}, /* data area size to tune */
55};
56#else
57dataflash_protect_t area_list[NB_DATAFLASH_AREA] = {
58 {0, 0x3fff, FLAG_PROTECT_SET}, /* ROM code */
59 {0x4000, 0x7fff, FLAG_PROTECT_CLEAR}, /* u-boot environment */
60 {0x8000, 0x37fff, FLAG_PROTECT_SET}, /* u-boot code */
61 {0x38000, 0x1fffff, FLAG_PROTECT_CLEAR}, /* data area size to tune */
62};
63#endif
64#elif defined(CONFIG_NEW_PARTITION)
65/*define the area offsets*/
Wolfgang Denk0611c492007-08-14 18:42:36 +020066/* Invalid partitions should be defined with start > end */
Peter Pearsede5b02c2007-08-14 10:10:52 +010067dataflash_protect_t area_list[NB_DATAFLASH_AREA*CFG_MAX_DATAFLASH_BANKS] = {
68 {0x00000000, 0x000083ff, FLAG_PROTECT_SET, 0, "Bootstrap"}, /* ROM code */
69 {0x00008400, 0x00020fff, FLAG_PROTECT_SET, 0, "U-Boot"}, /* u-boot code */
70 {0x00021000, 0x000293ff, FLAG_PROTECT_CLEAR, 0, "Environment"}, /* u-boot environment 8Kb */
71 {0x00029400, 0x00041fff, FLAG_PROTECT_INVALID, 0, "<Unused>"}, /* Rest of Sector 1 */
72 {0x00042000, 0x0018Bfff, FLAG_PROTECT_CLEAR, FLAG_SETENV, "OS"}, /* data area size to tune */
73 {0x0018C000, 0xffffffff, FLAG_PROTECT_CLEAR, FLAG_SETENV, "FS"}, /* data area size to tune */
74
75 {0x00000000, 0xffffffff, FLAG_PROTECT_CLEAR, FLAG_SETENV, "Data"}, /* data area */
76 {0xffffffff, 0x00000000, FLAG_PROTECT_INVALID, 0, "<Invalid>"}, /* Invalid */
77 {0xffffffff, 0x00000000, FLAG_PROTECT_INVALID, 0, "<Invalid>"}, /* Invalid */
78 {0xffffffff, 0x00000000, FLAG_PROTECT_INVALID, 0, "<Invalid>"}, /* Invalid */
79 {0xffffffff, 0x00000000, FLAG_PROTECT_INVALID, 0, "<Invalid>"}, /* Invalid */
80 {0xffffffff, 0x00000000, FLAG_PROTECT_INVALID, 0, "<Invalid>"}, /* Invalid */
81};
82#else
wdenk86765902003-12-06 23:55:10 +000083dataflash_protect_t area_list[NB_DATAFLASH_AREA] = {
84 {0, 0x7fff, FLAG_PROTECT_SET}, /* ROM code */
85 {0x8000, 0x1ffff, FLAG_PROTECT_SET}, /* u-boot code */
86 {0x20000, 0x27fff, FLAG_PROTECT_CLEAR}, /* u-boot environment */
87 {0x28000, 0x1fffff, FLAG_PROTECT_CLEAR}, /* data area size to tune */
88};
Peter Pearsede5b02c2007-08-14 10:10:52 +010089#endif
wdenk86765902003-12-06 23:55:10 +000090
wdenk381669a2003-06-16 23:50:08 +000091extern void AT91F_SpiInit (void);
92extern int AT91F_DataflashProbe (int i, AT91PS_DataflashDesc pDesc);
93extern int AT91F_DataFlashRead (AT91PS_DataFlash pDataFlash,
94 unsigned long addr,
95 unsigned long size, char *buffer);
wdenk86765902003-12-06 23:55:10 +000096extern int AT91F_DataFlashWrite( AT91PS_DataFlash pDataFlash,
Peter Pearsede5b02c2007-08-14 10:10:52 +010097 unsigned char *src,
98 int dest,
99 int size );
wdenk381669a2003-06-16 23:50:08 +0000100
101int AT91F_DataflashInit (void)
102{
103 int i, j;
104 int dfcode;
Peter Pearsede5b02c2007-08-14 10:10:52 +0100105 int part = 0;
106 int last_part;
107 int found[CFG_MAX_DATAFLASH_BANKS];
108 unsigned char protected;
wdenk381669a2003-06-16 23:50:08 +0000109
110 AT91F_SpiInit ();
111
112 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
Peter Pearsede5b02c2007-08-14 10:10:52 +0100113 found[i] = 0;
wdenk86765902003-12-06 23:55:10 +0000114 dataflash_info[i].Desc.state = IDLE;
wdenk381669a2003-06-16 23:50:08 +0000115 dataflash_info[i].id = 0;
116 dataflash_info[i].Device.pages_number = 0;
Wolfgang Denk0611c492007-08-14 18:42:36 +0200117 dfcode = AT91F_DataflashProbe (cs[i][1],
Peter Pearsede5b02c2007-08-14 10:10:52 +0100118 &dataflash_info[i].Desc);
wdenk381669a2003-06-16 23:50:08 +0000119
120 switch (dfcode) {
121 case AT45DB161:
122 dataflash_info[i].Device.pages_number = 4096;
123 dataflash_info[i].Device.pages_size = 528;
124 dataflash_info[i].Device.page_offset = 10;
125 dataflash_info[i].Device.byte_mask = 0x300;
126 dataflash_info[i].Device.cs = cs[i][1];
127 dataflash_info[i].Desc.DataFlash_state = IDLE;
128 dataflash_info[i].logical_address = cs[i][0];
129 dataflash_info[i].id = dfcode;
Peter Pearsede5b02c2007-08-14 10:10:52 +0100130 found[i] += dfcode;;
wdenk381669a2003-06-16 23:50:08 +0000131 break;
132
133 case AT45DB321:
134 dataflash_info[i].Device.pages_number = 8192;
135 dataflash_info[i].Device.pages_size = 528;
136 dataflash_info[i].Device.page_offset = 10;
137 dataflash_info[i].Device.byte_mask = 0x300;
138 dataflash_info[i].Device.cs = cs[i][1];
139 dataflash_info[i].Desc.DataFlash_state = IDLE;
140 dataflash_info[i].logical_address = cs[i][0];
141 dataflash_info[i].id = dfcode;
Peter Pearsede5b02c2007-08-14 10:10:52 +0100142 found[i] += dfcode;;
wdenk381669a2003-06-16 23:50:08 +0000143 break;
144
145 case AT45DB642:
146 dataflash_info[i].Device.pages_number = 8192;
147 dataflash_info[i].Device.pages_size = 1056;
148 dataflash_info[i].Device.page_offset = 11;
149 dataflash_info[i].Device.byte_mask = 0x700;
150 dataflash_info[i].Device.cs = cs[i][1];
151 dataflash_info[i].Desc.DataFlash_state = IDLE;
152 dataflash_info[i].logical_address = cs[i][0];
153 dataflash_info[i].id = dfcode;
Peter Pearsede5b02c2007-08-14 10:10:52 +0100154 found[i] += dfcode;;
wdenk381669a2003-06-16 23:50:08 +0000155 break;
Peter Pearsede5b02c2007-08-14 10:10:52 +0100156
wdenk86765902003-12-06 23:55:10 +0000157 case AT45DB128:
158 dataflash_info[i].Device.pages_number = 16384;
159 dataflash_info[i].Device.pages_size = 1056;
160 dataflash_info[i].Device.page_offset = 11;
161 dataflash_info[i].Device.byte_mask = 0x700;
162 dataflash_info[i].Device.cs = cs[i][1];
163 dataflash_info[i].Desc.DataFlash_state = IDLE;
164 dataflash_info[i].logical_address = cs[i][0];
165 dataflash_info[i].id = dfcode;
Peter Pearsede5b02c2007-08-14 10:10:52 +0100166 found[i] += dfcode;;
wdenk86765902003-12-06 23:55:10 +0000167 break;
wdenk381669a2003-06-16 23:50:08 +0000168
169 default:
Peter Pearsede5b02c2007-08-14 10:10:52 +0100170 dfcode = 0;
wdenk381669a2003-06-16 23:50:08 +0000171 break;
172 }
wdenk86765902003-12-06 23:55:10 +0000173 /* set the last area end to the dataflash size*/
174 area_list[NB_DATAFLASH_AREA -1].end =
175 (dataflash_info[i].Device.pages_number *
176 dataflash_info[i].Device.pages_size)-1;
wdenk381669a2003-06-16 23:50:08 +0000177
Peter Pearsede5b02c2007-08-14 10:10:52 +0100178 last_part=0;
wdenk86765902003-12-06 23:55:10 +0000179 /* set the area addresses */
180 for(j = 0; j<NB_DATAFLASH_AREA; j++) {
Peter Pearsede5b02c2007-08-14 10:10:52 +0100181 if(found[i]!=0) {
Wolfgang Denk0611c492007-08-14 18:42:36 +0200182 dataflash_info[i].Device.area_list[j].start =
183 area_list[part].start +
Peter Pearsede5b02c2007-08-14 10:10:52 +0100184 dataflash_info[i].logical_address;
185 if(area_list[part].end == 0xffffffff) {
Wolfgang Denk0611c492007-08-14 18:42:36 +0200186 dataflash_info[i].Device.area_list[j].end =
187 dataflash_info[i].end_address +
Peter Pearsede5b02c2007-08-14 10:10:52 +0100188 dataflash_info [i].logical_address;
189 last_part = 1;
190 } else {
Wolfgang Denk0611c492007-08-14 18:42:36 +0200191 dataflash_info[i].Device.area_list[j].end =
192 area_list[part].end +
Peter Pearsede5b02c2007-08-14 10:10:52 +0100193 dataflash_info[i].logical_address;
194 }
195 protected = area_list[part].protected;
196 /* Set the environment according to the label...*/
197 if(protected == FLAG_PROTECT_INVALID) {
Wolfgang Denk0611c492007-08-14 18:42:36 +0200198 dataflash_info[i].Device.area_list[j].protected =
Peter Pearsede5b02c2007-08-14 10:10:52 +0100199 FLAG_PROTECT_INVALID;
200 } else {
Wolfgang Denk0611c492007-08-14 18:42:36 +0200201 dataflash_info[i].Device.area_list[j].protected =
Peter Pearsede5b02c2007-08-14 10:10:52 +0100202 protected;
203 }
204 strcpy((char*)(dataflash_info[i].Device.area_list[j].label),
205 (const char *)area_list[part].label);
206 }
207 part++;
wdenk86765902003-12-06 23:55:10 +0000208 }
wdenk381669a2003-06-16 23:50:08 +0000209 }
Peter Pearsede5b02c2007-08-14 10:10:52 +0100210 return found[0];
wdenk381669a2003-06-16 23:50:08 +0000211}
212
Peter Pearsede5b02c2007-08-14 10:10:52 +0100213#ifdef CONFIG_NEW_DF_PARTITION
214int AT91F_DataflashSetEnv (void)
215{
216 int i, j;
217 int part;
218 unsigned char env;
219 unsigned char s[32]; /* Will fit a long int in hex */
220 unsigned long start;
221 for (i = 0, part= 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
222 for(j = 0; j<NB_DATAFLASH_AREA; j++) {
223 env = area_list[part].setenv;
224 /* Set the environment according to the label...*/
225 if((env & FLAG_SETENV) == FLAG_SETENV) {
Wolfgang Denk0611c492007-08-14 18:42:36 +0200226 start =
Peter Pearsede5b02c2007-08-14 10:10:52 +0100227 dataflash_info[i].Device.area_list[j].start;
228 sprintf(s,"%X",start);
229 setenv(area_list[part].label,s);
230 }
231 part++;
232 }
233 }
234}
235#endif
wdenk381669a2003-06-16 23:50:08 +0000236
wdenk381669a2003-06-16 23:50:08 +0000237void dataflash_print_info (void)
238{
wdenk86765902003-12-06 23:55:10 +0000239 int i, j;
wdenk381669a2003-06-16 23:50:08 +0000240
241 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
242 if (dataflash_info[i].id != 0) {
Peter Pearsede5b02c2007-08-14 10:10:52 +0100243 printf("DataFlash:");
wdenk381669a2003-06-16 23:50:08 +0000244 switch (dataflash_info[i].id) {
245 case AT45DB161:
Peter Pearsede5b02c2007-08-14 10:10:52 +0100246 printf("AT45DB161\n");
wdenk381669a2003-06-16 23:50:08 +0000247 break;
248
249 case AT45DB321:
Peter Pearsede5b02c2007-08-14 10:10:52 +0100250 printf("AT45DB321\n");
wdenk381669a2003-06-16 23:50:08 +0000251 break;
252
253 case AT45DB642:
Peter Pearsede5b02c2007-08-14 10:10:52 +0100254 printf("AT45DB642\n");
wdenk381669a2003-06-16 23:50:08 +0000255 break;
wdenk86765902003-12-06 23:55:10 +0000256 case AT45DB128:
Peter Pearsede5b02c2007-08-14 10:10:52 +0100257 printf("AT45DB128\n");
wdenk86765902003-12-06 23:55:10 +0000258 break;
wdenk381669a2003-06-16 23:50:08 +0000259 }
260
Peter Pearsede5b02c2007-08-14 10:10:52 +0100261 printf("Nb pages: %6d\n"
wdenk381669a2003-06-16 23:50:08 +0000262 "Page Size: %6d\n"
263 "Size=%8d bytes\n"
264 "Logical address: 0x%08X\n",
265 (unsigned int) dataflash_info[i].Device.pages_number,
266 (unsigned int) dataflash_info[i].Device.pages_size,
267 (unsigned int) dataflash_info[i].Device.pages_number *
268 dataflash_info[i].Device.pages_size,
269 (unsigned int) dataflash_info[i].logical_address);
wdenk86765902003-12-06 23:55:10 +0000270 for (j=0; j< NB_DATAFLASH_AREA; j++) {
Peter Pearsede5b02c2007-08-14 10:10:52 +0100271 switch(dataflash_info[i].Device.area_list[j].protected) {
272 case FLAG_PROTECT_SET:
273 case FLAG_PROTECT_CLEAR:
274 printf("Area %i:\t%08lX to %08lX %s", j,
275 dataflash_info[i].Device.area_list[j].start,
276 dataflash_info[i].Device.area_list[j].end,
277 (dataflash_info[i].Device.area_list[j].protected==FLAG_PROTECT_SET) ? "(RO)" : " ");
278#ifdef CONFIG_NEW_DF_PARTITION
279 printf(" %s\n", dataflash_info[i].Device.area_list[j].label);
280#else
281 printf("\n");
282#endif
283 break;
284#ifdef CONFIG_NEW_DF_PARTITION
285 case FLAG_PROTECT_INVALID:
286 break;
287#endif
288 }
wdenk86765902003-12-06 23:55:10 +0000289 }
wdenk381669a2003-06-16 23:50:08 +0000290 }
291 }
292}
293
294
Peter Pearsede5b02c2007-08-14 10:10:52 +0100295/*---------------------------------------------------------------------------*/
296/* Function Name : AT91F_DataflashSelect */
297/* Object : Select the correct device */
298/*---------------------------------------------------------------------------*/
Wolfgang Denk0611c492007-08-14 18:42:36 +0200299AT91PS_DataFlash AT91F_DataflashSelect (AT91PS_DataFlash pFlash,
Peter Pearsede5b02c2007-08-14 10:10:52 +0100300 unsigned long *addr)
wdenk381669a2003-06-16 23:50:08 +0000301{
302 char addr_valid = 0;
303 int i;
304
305 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++)
Peter Pearsede5b02c2007-08-14 10:10:52 +0100306 if ( dataflash_info[i].id
307 && ((((int) addr) & 0xFF000000) ==
308 dataflash_info[i].logical_address)) {
wdenk381669a2003-06-16 23:50:08 +0000309 addr_valid = 1;
310 break;
311 }
312 if (!addr_valid) {
313 pFlash = (AT91PS_DataFlash) 0;
314 return pFlash;
315 }
316 pFlash->pDataFlashDesc = &(dataflash_info[i].Desc);
317 pFlash->pDevice = &(dataflash_info[i].Device);
318 *addr -= dataflash_info[i].logical_address;
319 return (pFlash);
320}
321
Peter Pearsede5b02c2007-08-14 10:10:52 +0100322/*---------------------------------------------------------------------------*/
323/* Function Name : addr_dataflash */
324/* Object : Test if address is valid */
325/*---------------------------------------------------------------------------*/
wdenk381669a2003-06-16 23:50:08 +0000326int addr_dataflash (unsigned long addr)
327{
328 int addr_valid = 0;
329 int i;
330
331 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
332 if ((((int) addr) & 0xFF000000) ==
333 dataflash_info[i].logical_address) {
334 addr_valid = 1;
335 break;
336 }
337 }
338
339 return addr_valid;
340}
Peter Pearsede5b02c2007-08-14 10:10:52 +0100341/*---------------------------------------------------------------------------*/
342/* Function Name : size_dataflash */
343/* Object : Test if address is valid regarding the size */
344/*---------------------------------------------------------------------------*/
Wolfgang Denk0611c492007-08-14 18:42:36 +0200345int size_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr,
Peter Pearsede5b02c2007-08-14 10:10:52 +0100346 unsigned long size)
wdenk86765902003-12-06 23:55:10 +0000347{
348 /* is outside the dataflash */
349 if (((int)addr & 0x0FFFFFFF) > (pdataFlash->pDevice->pages_size *
350 pdataFlash->pDevice->pages_number)) return 0;
351 /* is too large for the dataflash */
352 if (size > ((pdataFlash->pDevice->pages_size *
Wolfgang Denk0611c492007-08-14 18:42:36 +0200353 pdataFlash->pDevice->pages_number) -
Peter Pearsede5b02c2007-08-14 10:10:52 +0100354 ((int)addr & 0x0FFFFFFF))) return 0;
wdenk86765902003-12-06 23:55:10 +0000355
356 return 1;
357}
Peter Pearsede5b02c2007-08-14 10:10:52 +0100358/*---------------------------------------------------------------------------*/
359/* Function Name : prot_dataflash */
360/* Object : Test if destination area is protected */
361/*---------------------------------------------------------------------------*/
wdenk86765902003-12-06 23:55:10 +0000362int prot_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr)
363{
364int area;
365 /* find area */
366 for (area=0; area < NB_DATAFLASH_AREA; area++) {
367 if ((addr >= pdataFlash->pDevice->area_list[area].start) &&
368 (addr < pdataFlash->pDevice->area_list[area].end))
369 break;
370 }
Wolfgang Denk0611c492007-08-14 18:42:36 +0200371 if (area == NB_DATAFLASH_AREA)
Peter Pearsede5b02c2007-08-14 10:10:52 +0100372 return -1;
373
wdenk86765902003-12-06 23:55:10 +0000374 /*test protection value*/
Wolfgang Denk0611c492007-08-14 18:42:36 +0200375 if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_SET)
Peter Pearsede5b02c2007-08-14 10:10:52 +0100376 return 0;
Wolfgang Denk0611c492007-08-14 18:42:36 +0200377 if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_INVALID)
Peter Pearsede5b02c2007-08-14 10:10:52 +0100378 return 0;
wdenk86765902003-12-06 23:55:10 +0000379
380 return 1;
381}
Peter Pearsede5b02c2007-08-14 10:10:52 +0100382/*--------------------------------------------------------------------------*/
383/* Function Name : dataflash_real_protect */
384/* Object : protect/unprotect area */
385/*--------------------------------------------------------------------------*/
Wolfgang Denk0611c492007-08-14 18:42:36 +0200386int dataflash_real_protect (int flag, unsigned long start_addr,
Peter Pearsede5b02c2007-08-14 10:10:52 +0100387 unsigned long end_addr)
wdenk86765902003-12-06 23:55:10 +0000388{
389int i,j, area1, area2, addr_valid = 0;
390 /* find dataflash */
391 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
392 if ((((int) start_addr) & 0xF0000000) ==
393 dataflash_info[i].logical_address) {
394 addr_valid = 1;
395 break;
396 }
397 }
398 if (!addr_valid) {
399 return -1;
400 }
401 /* find start area */
402 for (area1=0; area1 < NB_DATAFLASH_AREA; area1++) {
Wolfgang Denk0611c492007-08-14 18:42:36 +0200403 if (start_addr == dataflash_info[i].Device.area_list[area1].start)
Peter Pearsede5b02c2007-08-14 10:10:52 +0100404 break;
wdenk86765902003-12-06 23:55:10 +0000405 }
406 if (area1 == NB_DATAFLASH_AREA) return -1;
407 /* find end area */
408 for (area2=0; area2 < NB_DATAFLASH_AREA; area2++) {
Wolfgang Denk0611c492007-08-14 18:42:36 +0200409 if (end_addr == dataflash_info[i].Device.area_list[area2].end)
Peter Pearsede5b02c2007-08-14 10:10:52 +0100410 break;
wdenk86765902003-12-06 23:55:10 +0000411 }
Wolfgang Denk0611c492007-08-14 18:42:36 +0200412 if (area2 == NB_DATAFLASH_AREA)
Peter Pearsede5b02c2007-08-14 10:10:52 +0100413 return -1;
wdenk86765902003-12-06 23:55:10 +0000414
415 /*set protection value*/
416 for(j = area1; j < area2+1 ; j++)
Wolfgang Denk0611c492007-08-14 18:42:36 +0200417 if(dataflash_info[i].Device.area_list[j].protected
Peter Pearsede5b02c2007-08-14 10:10:52 +0100418 != FLAG_PROTECT_INVALID) {
419 if (flag == 0) {
Wolfgang Denk0611c492007-08-14 18:42:36 +0200420 dataflash_info[i].Device.area_list[j].protected
Peter Pearsede5b02c2007-08-14 10:10:52 +0100421 = FLAG_PROTECT_CLEAR;
422 } else {
Wolfgang Denk0611c492007-08-14 18:42:36 +0200423 dataflash_info[i].Device.area_list[j].protected
Peter Pearsede5b02c2007-08-14 10:10:52 +0100424 = FLAG_PROTECT_SET;
425 }
426 }
wdenk86765902003-12-06 23:55:10 +0000427
428 return (area2-area1+1);
429}
wdenk381669a2003-06-16 23:50:08 +0000430
Peter Pearsede5b02c2007-08-14 10:10:52 +0100431/*---------------------------------------------------------------------------*/
432/* Function Name : read_dataflash */
433/* Object : dataflash memory read */
434/*---------------------------------------------------------------------------*/
wdenk381669a2003-06-16 23:50:08 +0000435int read_dataflash (unsigned long addr, unsigned long size, char *result)
436{
Wolfgang Denk7fa6e902006-03-11 22:53:33 +0100437 unsigned long AddrToRead = addr;
wdenk381669a2003-06-16 23:50:08 +0000438 AT91PS_DataFlash pFlash = &DataFlashInst;
439
440 pFlash = AT91F_DataflashSelect (pFlash, &AddrToRead);
wdenk86765902003-12-06 23:55:10 +0000441
wdenk381669a2003-06-16 23:50:08 +0000442 if (pFlash == 0)
wdenk86765902003-12-06 23:55:10 +0000443 return ERR_UNKNOWN_FLASH_TYPE;
444
445 if (size_dataflash(pFlash,addr,size) == 0)
446 return ERR_INVAL;
wdenk381669a2003-06-16 23:50:08 +0000447
448 return (AT91F_DataFlashRead (pFlash, AddrToRead, size, result));
449}
450
451
Peter Pearsede5b02c2007-08-14 10:10:52 +0100452/*---------------------------------------------------------------------------*/
453/* Function Name : write_dataflash */
454/* Object : write a block in dataflash */
455/*---------------------------------------------------------------------------*/
wdenk381669a2003-06-16 23:50:08 +0000456int write_dataflash (unsigned long addr_dest, unsigned long addr_src,
Peter Pearsede5b02c2007-08-14 10:10:52 +0100457 unsigned long size)
wdenk381669a2003-06-16 23:50:08 +0000458{
Wolfgang Denk7fa6e902006-03-11 22:53:33 +0100459 unsigned long AddrToWrite = addr_dest;
wdenk381669a2003-06-16 23:50:08 +0000460 AT91PS_DataFlash pFlash = &DataFlashInst;
461
462 pFlash = AT91F_DataflashSelect (pFlash, &AddrToWrite);
wdenk86765902003-12-06 23:55:10 +0000463
464 if (pFlash == 0)
465 return ERR_UNKNOWN_FLASH_TYPE;
466
467 if (size_dataflash(pFlash,addr_dest,size) == 0)
468 return ERR_INVAL;
469
470 if (prot_dataflash(pFlash,addr_dest) == 0)
471 return ERR_PROTECTED;
472
wdenk381669a2003-06-16 23:50:08 +0000473 if (AddrToWrite == -1)
474 return -1;
475
Wolfgang Denk0611c492007-08-14 18:42:36 +0200476 return AT91F_DataFlashWrite (pFlash, (uchar *)addr_src,
Peter Pearsede5b02c2007-08-14 10:10:52 +0100477 AddrToWrite, size);
wdenk381669a2003-06-16 23:50:08 +0000478}
479
480
481void dataflash_perror (int err)
482{
483 switch (err) {
484 case ERR_OK:
485 break;
486 case ERR_TIMOUT:
Peter Pearsede5b02c2007-08-14 10:10:52 +0100487 printf("Timeout writing to DataFlash\n");
wdenk381669a2003-06-16 23:50:08 +0000488 break;
489 case ERR_PROTECTED:
Peter Pearsede5b02c2007-08-14 10:10:52 +0100490 printf("Can't write to protected/invalid DataFlash sectors\n");
wdenk381669a2003-06-16 23:50:08 +0000491 break;
492 case ERR_INVAL:
Peter Pearsede5b02c2007-08-14 10:10:52 +0100493 printf("Outside available DataFlash\n");
wdenk381669a2003-06-16 23:50:08 +0000494 break;
495 case ERR_UNKNOWN_FLASH_TYPE:
Peter Pearsede5b02c2007-08-14 10:10:52 +0100496 printf("Unknown Type of DataFlash\n");
wdenk381669a2003-06-16 23:50:08 +0000497 break;
498 case ERR_PROG_ERROR:
Peter Pearsede5b02c2007-08-14 10:10:52 +0100499 printf("General DataFlash Programming Error\n");
wdenk381669a2003-06-16 23:50:08 +0000500 break;
501 default:
Peter Pearsede5b02c2007-08-14 10:10:52 +0100502 printf("%s[%d] FIXME: rc=%d\n", __FILE__, __LINE__, err);
wdenk381669a2003-06-16 23:50:08 +0000503 break;
504 }
505}
506
507#endif