blob: 242140745e19b4ee26b7d937f50b51d0a2a44a7b [file] [log] [blame]
wdenk2abbe072003-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
Stelian Popd49fe4b2008-01-20 21:07:00 +000029struct dataflash_addr {
30 unsigned long addr;
31 int cs;
32};
33
Peter Pearsed4fc6012007-08-14 10:10:52 +010034#ifdef CONFIG_AT91SAM9260EK
Stelian Popd49fe4b2008-01-20 21:07:00 +000035struct dataflash_addr cs[CFG_MAX_DATAFLASH_BANKS] = {
Peter Pearsed4fc6012007-08-14 10:10:52 +010036 {CFG_DATAFLASH_LOGIC_ADDR_CS0, 0}, /* Logical adress, CS */
37 {CFG_DATAFLASH_LOGIC_ADDR_CS1, 1}
38};
39#elif defined(CONFIG_AT91SAM9263EK)
Stelian Popd49fe4b2008-01-20 21:07:00 +000040struct dataflash_addr cs[CFG_MAX_DATAFLASH_BANKS] = {
41 {CFG_DATAFLASH_LOGIC_ADDR_CS0, 0}, /* Logical adress, CS */
Peter Pearsed4fc6012007-08-14 10:10:52 +010042};
43#else
Stelian Popd49fe4b2008-01-20 21:07:00 +000044struct dataflash_addr cs[CFG_MAX_DATAFLASH_BANKS] = {
wdenk2abbe072003-06-16 23:50:08 +000045 {CFG_DATAFLASH_LOGIC_ADDR_CS0, 0}, /* Logical adress, CS */
46 {CFG_DATAFLASH_LOGIC_ADDR_CS3, 3}
47};
Peter Pearsed4fc6012007-08-14 10:10:52 +010048#endif
wdenk2abbe072003-06-16 23:50:08 +000049
wdenk5779d8d2003-12-06 23:55:10 +000050/*define the area offsets*/
Peter Pearsed4fc6012007-08-14 10:10:52 +010051#if defined(CONFIG_AT91SAM9261EK) || defined(CONFIG_AT91SAM9260EK) || defined(CONFIG_AT91SAM9263EK)
52#if defined(CONFIG_NEW_PARTITION)
53dataflash_protect_t area_list[NB_DATAFLASH_AREA] = {
54 {0x00000000, 0x00003FFF, FLAG_PROTECT_SET, 0, "Bootstrap"}, /* ROM code */
55 {0x00004200, 0x000083FF, FLAG_PROTECT_CLEAR, 0, "Environment"}, /* u-boot environment */
56 {0x00008400, 0x0003DDFF, FLAG_PROTECT_SET, 0, "U-Boot"}, /* u-boot code */
57 {0x0003DE00, 0x00041FFF, FLAG_PROTECT_CLEAR, FLAG_SETENV, "MON"}, /* Room for alternative boot monitor */
58 {0x00042000, 0x0018BFFF, FLAG_PROTECT_CLEAR, FLAG_SETENV, "OS"}, /* data area size to tune */
59 {0x0018C000, 0xFFFFFFFF, FLAG_PROTECT_CLEAR, FLAG_SETENV, "FS"}, /* data area size to tune */
60};
61#else
62dataflash_protect_t area_list[NB_DATAFLASH_AREA] = {
63 {0, 0x3fff, FLAG_PROTECT_SET}, /* ROM code */
64 {0x4000, 0x7fff, FLAG_PROTECT_CLEAR}, /* u-boot environment */
65 {0x8000, 0x37fff, FLAG_PROTECT_SET}, /* u-boot code */
66 {0x38000, 0x1fffff, FLAG_PROTECT_CLEAR}, /* data area size to tune */
67};
68#endif
69#elif defined(CONFIG_NEW_PARTITION)
70/*define the area offsets*/
Wolfgang Denkf01dbb52007-08-14 18:42:36 +020071/* Invalid partitions should be defined with start > end */
Peter Pearsed4fc6012007-08-14 10:10:52 +010072dataflash_protect_t area_list[NB_DATAFLASH_AREA*CFG_MAX_DATAFLASH_BANKS] = {
73 {0x00000000, 0x000083ff, FLAG_PROTECT_SET, 0, "Bootstrap"}, /* ROM code */
74 {0x00008400, 0x00020fff, FLAG_PROTECT_SET, 0, "U-Boot"}, /* u-boot code */
75 {0x00021000, 0x000293ff, FLAG_PROTECT_CLEAR, 0, "Environment"}, /* u-boot environment 8Kb */
76 {0x00029400, 0x00041fff, FLAG_PROTECT_INVALID, 0, "<Unused>"}, /* Rest of Sector 1 */
77 {0x00042000, 0x0018Bfff, FLAG_PROTECT_CLEAR, FLAG_SETENV, "OS"}, /* data area size to tune */
78 {0x0018C000, 0xffffffff, FLAG_PROTECT_CLEAR, FLAG_SETENV, "FS"}, /* data area size to tune */
79
80 {0x00000000, 0xffffffff, FLAG_PROTECT_CLEAR, FLAG_SETENV, "Data"}, /* data area */
81 {0xffffffff, 0x00000000, FLAG_PROTECT_INVALID, 0, "<Invalid>"}, /* Invalid */
82 {0xffffffff, 0x00000000, FLAG_PROTECT_INVALID, 0, "<Invalid>"}, /* Invalid */
83 {0xffffffff, 0x00000000, FLAG_PROTECT_INVALID, 0, "<Invalid>"}, /* Invalid */
84 {0xffffffff, 0x00000000, FLAG_PROTECT_INVALID, 0, "<Invalid>"}, /* Invalid */
85 {0xffffffff, 0x00000000, FLAG_PROTECT_INVALID, 0, "<Invalid>"}, /* Invalid */
86};
87#else
wdenk5779d8d2003-12-06 23:55:10 +000088dataflash_protect_t area_list[NB_DATAFLASH_AREA] = {
89 {0, 0x7fff, FLAG_PROTECT_SET}, /* ROM code */
90 {0x8000, 0x1ffff, FLAG_PROTECT_SET}, /* u-boot code */
91 {0x20000, 0x27fff, FLAG_PROTECT_CLEAR}, /* u-boot environment */
92 {0x28000, 0x1fffff, FLAG_PROTECT_CLEAR}, /* data area size to tune */
93};
Peter Pearsed4fc6012007-08-14 10:10:52 +010094#endif
wdenk5779d8d2003-12-06 23:55:10 +000095
wdenk2abbe072003-06-16 23:50:08 +000096extern void AT91F_SpiInit (void);
97extern int AT91F_DataflashProbe (int i, AT91PS_DataflashDesc pDesc);
98extern int AT91F_DataFlashRead (AT91PS_DataFlash pDataFlash,
99 unsigned long addr,
100 unsigned long size, char *buffer);
wdenk5779d8d2003-12-06 23:55:10 +0000101extern int AT91F_DataFlashWrite( AT91PS_DataFlash pDataFlash,
Peter Pearsed4fc6012007-08-14 10:10:52 +0100102 unsigned char *src,
103 int dest,
104 int size );
wdenk2abbe072003-06-16 23:50:08 +0000105
106int AT91F_DataflashInit (void)
107{
108 int i, j;
109 int dfcode;
Peter Pearsed4fc6012007-08-14 10:10:52 +0100110 int part = 0;
111 int last_part;
112 int found[CFG_MAX_DATAFLASH_BANKS];
113 unsigned char protected;
wdenk2abbe072003-06-16 23:50:08 +0000114
115 AT91F_SpiInit ();
116
117 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
Peter Pearsed4fc6012007-08-14 10:10:52 +0100118 found[i] = 0;
wdenk5779d8d2003-12-06 23:55:10 +0000119 dataflash_info[i].Desc.state = IDLE;
wdenk2abbe072003-06-16 23:50:08 +0000120 dataflash_info[i].id = 0;
121 dataflash_info[i].Device.pages_number = 0;
Stelian Popd49fe4b2008-01-20 21:07:00 +0000122 dfcode = AT91F_DataflashProbe (cs[i].cs,
Peter Pearsed4fc6012007-08-14 10:10:52 +0100123 &dataflash_info[i].Desc);
wdenk2abbe072003-06-16 23:50:08 +0000124
125 switch (dfcode) {
126 case AT45DB161:
127 dataflash_info[i].Device.pages_number = 4096;
128 dataflash_info[i].Device.pages_size = 528;
129 dataflash_info[i].Device.page_offset = 10;
130 dataflash_info[i].Device.byte_mask = 0x300;
Stelian Popd49fe4b2008-01-20 21:07:00 +0000131 dataflash_info[i].Device.cs = cs[i].cs;
wdenk2abbe072003-06-16 23:50:08 +0000132 dataflash_info[i].Desc.DataFlash_state = IDLE;
Stelian Popd49fe4b2008-01-20 21:07:00 +0000133 dataflash_info[i].logical_address = cs[i].addr;
wdenk2abbe072003-06-16 23:50:08 +0000134 dataflash_info[i].id = dfcode;
Peter Pearsed4fc6012007-08-14 10:10:52 +0100135 found[i] += dfcode;;
wdenk2abbe072003-06-16 23:50:08 +0000136 break;
137
138 case AT45DB321:
139 dataflash_info[i].Device.pages_number = 8192;
140 dataflash_info[i].Device.pages_size = 528;
141 dataflash_info[i].Device.page_offset = 10;
142 dataflash_info[i].Device.byte_mask = 0x300;
Stelian Popd49fe4b2008-01-20 21:07:00 +0000143 dataflash_info[i].Device.cs = cs[i].cs;
wdenk2abbe072003-06-16 23:50:08 +0000144 dataflash_info[i].Desc.DataFlash_state = IDLE;
Stelian Popd49fe4b2008-01-20 21:07:00 +0000145 dataflash_info[i].logical_address = cs[i].addr;
wdenk2abbe072003-06-16 23:50:08 +0000146 dataflash_info[i].id = dfcode;
Peter Pearsed4fc6012007-08-14 10:10:52 +0100147 found[i] += dfcode;;
wdenk2abbe072003-06-16 23:50:08 +0000148 break;
149
150 case AT45DB642:
151 dataflash_info[i].Device.pages_number = 8192;
152 dataflash_info[i].Device.pages_size = 1056;
153 dataflash_info[i].Device.page_offset = 11;
154 dataflash_info[i].Device.byte_mask = 0x700;
Stelian Popd49fe4b2008-01-20 21:07:00 +0000155 dataflash_info[i].Device.cs = cs[i].cs;
wdenk2abbe072003-06-16 23:50:08 +0000156 dataflash_info[i].Desc.DataFlash_state = IDLE;
Stelian Popd49fe4b2008-01-20 21:07:00 +0000157 dataflash_info[i].logical_address = cs[i].addr;
wdenk2abbe072003-06-16 23:50:08 +0000158 dataflash_info[i].id = dfcode;
Peter Pearsed4fc6012007-08-14 10:10:52 +0100159 found[i] += dfcode;;
wdenk2abbe072003-06-16 23:50:08 +0000160 break;
Peter Pearsed4fc6012007-08-14 10:10:52 +0100161
wdenk5779d8d2003-12-06 23:55:10 +0000162 case AT45DB128:
163 dataflash_info[i].Device.pages_number = 16384;
164 dataflash_info[i].Device.pages_size = 1056;
165 dataflash_info[i].Device.page_offset = 11;
166 dataflash_info[i].Device.byte_mask = 0x700;
Stelian Popd49fe4b2008-01-20 21:07:00 +0000167 dataflash_info[i].Device.cs = cs[i].cs;
wdenk5779d8d2003-12-06 23:55:10 +0000168 dataflash_info[i].Desc.DataFlash_state = IDLE;
Stelian Popd49fe4b2008-01-20 21:07:00 +0000169 dataflash_info[i].logical_address = cs[i].addr;
wdenk5779d8d2003-12-06 23:55:10 +0000170 dataflash_info[i].id = dfcode;
Peter Pearsed4fc6012007-08-14 10:10:52 +0100171 found[i] += dfcode;;
wdenk5779d8d2003-12-06 23:55:10 +0000172 break;
wdenk2abbe072003-06-16 23:50:08 +0000173
174 default:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100175 dfcode = 0;
wdenk2abbe072003-06-16 23:50:08 +0000176 break;
177 }
wdenk5779d8d2003-12-06 23:55:10 +0000178 /* set the last area end to the dataflash size*/
179 area_list[NB_DATAFLASH_AREA -1].end =
180 (dataflash_info[i].Device.pages_number *
181 dataflash_info[i].Device.pages_size)-1;
wdenk2abbe072003-06-16 23:50:08 +0000182
Peter Pearsed4fc6012007-08-14 10:10:52 +0100183 last_part=0;
wdenk5779d8d2003-12-06 23:55:10 +0000184 /* set the area addresses */
185 for(j = 0; j<NB_DATAFLASH_AREA; j++) {
Peter Pearsed4fc6012007-08-14 10:10:52 +0100186 if(found[i]!=0) {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200187 dataflash_info[i].Device.area_list[j].start =
188 area_list[part].start +
Peter Pearsed4fc6012007-08-14 10:10:52 +0100189 dataflash_info[i].logical_address;
190 if(area_list[part].end == 0xffffffff) {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200191 dataflash_info[i].Device.area_list[j].end =
192 dataflash_info[i].end_address +
Peter Pearsed4fc6012007-08-14 10:10:52 +0100193 dataflash_info [i].logical_address;
194 last_part = 1;
195 } else {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200196 dataflash_info[i].Device.area_list[j].end =
197 area_list[part].end +
Peter Pearsed4fc6012007-08-14 10:10:52 +0100198 dataflash_info[i].logical_address;
199 }
200 protected = area_list[part].protected;
201 /* Set the environment according to the label...*/
202 if(protected == FLAG_PROTECT_INVALID) {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200203 dataflash_info[i].Device.area_list[j].protected =
Peter Pearsed4fc6012007-08-14 10:10:52 +0100204 FLAG_PROTECT_INVALID;
205 } else {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200206 dataflash_info[i].Device.area_list[j].protected =
Peter Pearsed4fc6012007-08-14 10:10:52 +0100207 protected;
208 }
209 strcpy((char*)(dataflash_info[i].Device.area_list[j].label),
210 (const char *)area_list[part].label);
211 }
212 part++;
wdenk5779d8d2003-12-06 23:55:10 +0000213 }
wdenk2abbe072003-06-16 23:50:08 +0000214 }
Peter Pearsed4fc6012007-08-14 10:10:52 +0100215 return found[0];
wdenk2abbe072003-06-16 23:50:08 +0000216}
217
Peter Pearsed4fc6012007-08-14 10:10:52 +0100218#ifdef CONFIG_NEW_DF_PARTITION
219int AT91F_DataflashSetEnv (void)
220{
221 int i, j;
222 int part;
223 unsigned char env;
224 unsigned char s[32]; /* Will fit a long int in hex */
225 unsigned long start;
226 for (i = 0, part= 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
227 for(j = 0; j<NB_DATAFLASH_AREA; j++) {
228 env = area_list[part].setenv;
229 /* Set the environment according to the label...*/
230 if((env & FLAG_SETENV) == FLAG_SETENV) {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200231 start =
Peter Pearsed4fc6012007-08-14 10:10:52 +0100232 dataflash_info[i].Device.area_list[j].start;
233 sprintf(s,"%X",start);
234 setenv(area_list[part].label,s);
235 }
236 part++;
237 }
238 }
239}
240#endif
wdenk2abbe072003-06-16 23:50:08 +0000241
wdenk2abbe072003-06-16 23:50:08 +0000242void dataflash_print_info (void)
243{
wdenk5779d8d2003-12-06 23:55:10 +0000244 int i, j;
wdenk2abbe072003-06-16 23:50:08 +0000245
246 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
247 if (dataflash_info[i].id != 0) {
Peter Pearsed4fc6012007-08-14 10:10:52 +0100248 printf("DataFlash:");
wdenk2abbe072003-06-16 23:50:08 +0000249 switch (dataflash_info[i].id) {
250 case AT45DB161:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100251 printf("AT45DB161\n");
wdenk2abbe072003-06-16 23:50:08 +0000252 break;
253
254 case AT45DB321:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100255 printf("AT45DB321\n");
wdenk2abbe072003-06-16 23:50:08 +0000256 break;
257
258 case AT45DB642:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100259 printf("AT45DB642\n");
wdenk2abbe072003-06-16 23:50:08 +0000260 break;
wdenk5779d8d2003-12-06 23:55:10 +0000261 case AT45DB128:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100262 printf("AT45DB128\n");
wdenk5779d8d2003-12-06 23:55:10 +0000263 break;
wdenk2abbe072003-06-16 23:50:08 +0000264 }
265
Peter Pearsed4fc6012007-08-14 10:10:52 +0100266 printf("Nb pages: %6d\n"
wdenk2abbe072003-06-16 23:50:08 +0000267 "Page Size: %6d\n"
268 "Size=%8d bytes\n"
269 "Logical address: 0x%08X\n",
270 (unsigned int) dataflash_info[i].Device.pages_number,
271 (unsigned int) dataflash_info[i].Device.pages_size,
272 (unsigned int) dataflash_info[i].Device.pages_number *
273 dataflash_info[i].Device.pages_size,
274 (unsigned int) dataflash_info[i].logical_address);
wdenk5779d8d2003-12-06 23:55:10 +0000275 for (j=0; j< NB_DATAFLASH_AREA; j++) {
Peter Pearsed4fc6012007-08-14 10:10:52 +0100276 switch(dataflash_info[i].Device.area_list[j].protected) {
277 case FLAG_PROTECT_SET:
278 case FLAG_PROTECT_CLEAR:
279 printf("Area %i:\t%08lX to %08lX %s", j,
280 dataflash_info[i].Device.area_list[j].start,
281 dataflash_info[i].Device.area_list[j].end,
282 (dataflash_info[i].Device.area_list[j].protected==FLAG_PROTECT_SET) ? "(RO)" : " ");
283#ifdef CONFIG_NEW_DF_PARTITION
284 printf(" %s\n", dataflash_info[i].Device.area_list[j].label);
285#else
286 printf("\n");
287#endif
288 break;
289#ifdef CONFIG_NEW_DF_PARTITION
290 case FLAG_PROTECT_INVALID:
291 break;
292#endif
293 }
wdenk5779d8d2003-12-06 23:55:10 +0000294 }
wdenk2abbe072003-06-16 23:50:08 +0000295 }
296 }
297}
298
299
Peter Pearsed4fc6012007-08-14 10:10:52 +0100300/*---------------------------------------------------------------------------*/
301/* Function Name : AT91F_DataflashSelect */
302/* Object : Select the correct device */
303/*---------------------------------------------------------------------------*/
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200304AT91PS_DataFlash AT91F_DataflashSelect (AT91PS_DataFlash pFlash,
Peter Pearsed4fc6012007-08-14 10:10:52 +0100305 unsigned long *addr)
wdenk2abbe072003-06-16 23:50:08 +0000306{
307 char addr_valid = 0;
308 int i;
309
310 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100311 if ( dataflash_info[i].id
Stelian Popc77ce472008-01-14 22:08:14 +0100312 && ((((int) *addr) & 0xFF000000) ==
Peter Pearsed4fc6012007-08-14 10:10:52 +0100313 dataflash_info[i].logical_address)) {
wdenk2abbe072003-06-16 23:50:08 +0000314 addr_valid = 1;
315 break;
316 }
317 if (!addr_valid) {
318 pFlash = (AT91PS_DataFlash) 0;
319 return pFlash;
320 }
321 pFlash->pDataFlashDesc = &(dataflash_info[i].Desc);
322 pFlash->pDevice = &(dataflash_info[i].Device);
323 *addr -= dataflash_info[i].logical_address;
324 return (pFlash);
325}
326
Peter Pearsed4fc6012007-08-14 10:10:52 +0100327/*---------------------------------------------------------------------------*/
328/* Function Name : addr_dataflash */
329/* Object : Test if address is valid */
330/*---------------------------------------------------------------------------*/
wdenk2abbe072003-06-16 23:50:08 +0000331int addr_dataflash (unsigned long addr)
332{
333 int addr_valid = 0;
334 int i;
335
336 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
337 if ((((int) addr) & 0xFF000000) ==
338 dataflash_info[i].logical_address) {
339 addr_valid = 1;
340 break;
341 }
342 }
343
344 return addr_valid;
345}
Peter Pearsed4fc6012007-08-14 10:10:52 +0100346/*---------------------------------------------------------------------------*/
347/* Function Name : size_dataflash */
348/* Object : Test if address is valid regarding the size */
349/*---------------------------------------------------------------------------*/
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200350int size_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr,
Peter Pearsed4fc6012007-08-14 10:10:52 +0100351 unsigned long size)
wdenk5779d8d2003-12-06 23:55:10 +0000352{
353 /* is outside the dataflash */
354 if (((int)addr & 0x0FFFFFFF) > (pdataFlash->pDevice->pages_size *
355 pdataFlash->pDevice->pages_number)) return 0;
356 /* is too large for the dataflash */
357 if (size > ((pdataFlash->pDevice->pages_size *
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200358 pdataFlash->pDevice->pages_number) -
Peter Pearsed4fc6012007-08-14 10:10:52 +0100359 ((int)addr & 0x0FFFFFFF))) return 0;
wdenk5779d8d2003-12-06 23:55:10 +0000360
361 return 1;
362}
Peter Pearsed4fc6012007-08-14 10:10:52 +0100363/*---------------------------------------------------------------------------*/
364/* Function Name : prot_dataflash */
365/* Object : Test if destination area is protected */
366/*---------------------------------------------------------------------------*/
wdenk5779d8d2003-12-06 23:55:10 +0000367int prot_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr)
368{
369int area;
370 /* find area */
371 for (area=0; area < NB_DATAFLASH_AREA; area++) {
372 if ((addr >= pdataFlash->pDevice->area_list[area].start) &&
373 (addr < pdataFlash->pDevice->area_list[area].end))
374 break;
375 }
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200376 if (area == NB_DATAFLASH_AREA)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100377 return -1;
378
wdenk5779d8d2003-12-06 23:55:10 +0000379 /*test protection value*/
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200380 if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_SET)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100381 return 0;
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200382 if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_INVALID)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100383 return 0;
wdenk5779d8d2003-12-06 23:55:10 +0000384
385 return 1;
386}
Peter Pearsed4fc6012007-08-14 10:10:52 +0100387/*--------------------------------------------------------------------------*/
388/* Function Name : dataflash_real_protect */
389/* Object : protect/unprotect area */
390/*--------------------------------------------------------------------------*/
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200391int dataflash_real_protect (int flag, unsigned long start_addr,
Peter Pearsed4fc6012007-08-14 10:10:52 +0100392 unsigned long end_addr)
wdenk5779d8d2003-12-06 23:55:10 +0000393{
394int i,j, area1, area2, addr_valid = 0;
395 /* find dataflash */
396 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
397 if ((((int) start_addr) & 0xF0000000) ==
398 dataflash_info[i].logical_address) {
399 addr_valid = 1;
400 break;
401 }
402 }
403 if (!addr_valid) {
404 return -1;
405 }
406 /* find start area */
407 for (area1=0; area1 < NB_DATAFLASH_AREA; area1++) {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200408 if (start_addr == dataflash_info[i].Device.area_list[area1].start)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100409 break;
wdenk5779d8d2003-12-06 23:55:10 +0000410 }
411 if (area1 == NB_DATAFLASH_AREA) return -1;
412 /* find end area */
413 for (area2=0; area2 < NB_DATAFLASH_AREA; area2++) {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200414 if (end_addr == dataflash_info[i].Device.area_list[area2].end)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100415 break;
wdenk5779d8d2003-12-06 23:55:10 +0000416 }
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200417 if (area2 == NB_DATAFLASH_AREA)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100418 return -1;
wdenk5779d8d2003-12-06 23:55:10 +0000419
420 /*set protection value*/
421 for(j = area1; j < area2+1 ; j++)
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200422 if(dataflash_info[i].Device.area_list[j].protected
Peter Pearsed4fc6012007-08-14 10:10:52 +0100423 != FLAG_PROTECT_INVALID) {
424 if (flag == 0) {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200425 dataflash_info[i].Device.area_list[j].protected
Peter Pearsed4fc6012007-08-14 10:10:52 +0100426 = FLAG_PROTECT_CLEAR;
427 } else {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200428 dataflash_info[i].Device.area_list[j].protected
Peter Pearsed4fc6012007-08-14 10:10:52 +0100429 = FLAG_PROTECT_SET;
430 }
431 }
wdenk5779d8d2003-12-06 23:55:10 +0000432
433 return (area2-area1+1);
434}
wdenk2abbe072003-06-16 23:50:08 +0000435
Peter Pearsed4fc6012007-08-14 10:10:52 +0100436/*---------------------------------------------------------------------------*/
437/* Function Name : read_dataflash */
438/* Object : dataflash memory read */
439/*---------------------------------------------------------------------------*/
wdenk2abbe072003-06-16 23:50:08 +0000440int read_dataflash (unsigned long addr, unsigned long size, char *result)
441{
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100442 unsigned long AddrToRead = addr;
wdenk2abbe072003-06-16 23:50:08 +0000443 AT91PS_DataFlash pFlash = &DataFlashInst;
444
445 pFlash = AT91F_DataflashSelect (pFlash, &AddrToRead);
wdenk5779d8d2003-12-06 23:55:10 +0000446
wdenk2abbe072003-06-16 23:50:08 +0000447 if (pFlash == 0)
wdenk5779d8d2003-12-06 23:55:10 +0000448 return ERR_UNKNOWN_FLASH_TYPE;
449
450 if (size_dataflash(pFlash,addr,size) == 0)
451 return ERR_INVAL;
wdenk2abbe072003-06-16 23:50:08 +0000452
453 return (AT91F_DataFlashRead (pFlash, AddrToRead, size, result));
454}
455
456
Peter Pearsed4fc6012007-08-14 10:10:52 +0100457/*---------------------------------------------------------------------------*/
458/* Function Name : write_dataflash */
459/* Object : write a block in dataflash */
460/*---------------------------------------------------------------------------*/
wdenk2abbe072003-06-16 23:50:08 +0000461int write_dataflash (unsigned long addr_dest, unsigned long addr_src,
Peter Pearsed4fc6012007-08-14 10:10:52 +0100462 unsigned long size)
wdenk2abbe072003-06-16 23:50:08 +0000463{
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100464 unsigned long AddrToWrite = addr_dest;
wdenk2abbe072003-06-16 23:50:08 +0000465 AT91PS_DataFlash pFlash = &DataFlashInst;
466
467 pFlash = AT91F_DataflashSelect (pFlash, &AddrToWrite);
wdenk5779d8d2003-12-06 23:55:10 +0000468
469 if (pFlash == 0)
470 return ERR_UNKNOWN_FLASH_TYPE;
471
472 if (size_dataflash(pFlash,addr_dest,size) == 0)
473 return ERR_INVAL;
474
475 if (prot_dataflash(pFlash,addr_dest) == 0)
476 return ERR_PROTECTED;
477
wdenk2abbe072003-06-16 23:50:08 +0000478 if (AddrToWrite == -1)
479 return -1;
480
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200481 return AT91F_DataFlashWrite (pFlash, (uchar *)addr_src,
Peter Pearsed4fc6012007-08-14 10:10:52 +0100482 AddrToWrite, size);
wdenk2abbe072003-06-16 23:50:08 +0000483}
484
485
486void dataflash_perror (int err)
487{
488 switch (err) {
489 case ERR_OK:
490 break;
491 case ERR_TIMOUT:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100492 printf("Timeout writing to DataFlash\n");
wdenk2abbe072003-06-16 23:50:08 +0000493 break;
494 case ERR_PROTECTED:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100495 printf("Can't write to protected/invalid DataFlash sectors\n");
wdenk2abbe072003-06-16 23:50:08 +0000496 break;
497 case ERR_INVAL:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100498 printf("Outside available DataFlash\n");
wdenk2abbe072003-06-16 23:50:08 +0000499 break;
500 case ERR_UNKNOWN_FLASH_TYPE:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100501 printf("Unknown Type of DataFlash\n");
wdenk2abbe072003-06-16 23:50:08 +0000502 break;
503 case ERR_PROG_ERROR:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100504 printf("General DataFlash Programming Error\n");
wdenk2abbe072003-06-16 23:50:08 +0000505 break;
506 default:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100507 printf("%s[%d] FIXME: rc=%d\n", __FILE__, __LINE__, err);
wdenk2abbe072003-06-16 23:50:08 +0000508 break;
509 }
510}
511
512#endif