blob: 96cd395e71a7ad30ab230cc1b56f040e3bcc0e7c [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>
wdenk2abbe072003-06-16 23:50:08 +000022#include <asm/hardware.h>
23#include <dataflash.h>
24
wdenk2abbe072003-06-16 23:50:08 +000025static AT91S_DataFlash DataFlashInst;
26
wdenk2abbe072003-06-16 23:50:08 +000027extern void AT91F_SpiInit (void);
28extern int AT91F_DataflashProbe (int i, AT91PS_DataflashDesc pDesc);
29extern int AT91F_DataFlashRead (AT91PS_DataFlash pDataFlash,
30 unsigned long addr,
31 unsigned long size, char *buffer);
wdenk5779d8d2003-12-06 23:55:10 +000032extern int AT91F_DataFlashWrite( AT91PS_DataFlash pDataFlash,
Peter Pearsed4fc6012007-08-14 10:10:52 +010033 unsigned char *src,
34 int dest,
35 int size );
wdenk2abbe072003-06-16 23:50:08 +000036
37int AT91F_DataflashInit (void)
38{
39 int i, j;
40 int dfcode;
Stelian Pop5604e212008-03-26 21:52:36 +010041 int part;
Peter Pearsed4fc6012007-08-14 10:10:52 +010042 int last_part;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020043 int found[CONFIG_SYS_MAX_DATAFLASH_BANKS];
Peter Pearsed4fc6012007-08-14 10:10:52 +010044 unsigned char protected;
wdenk2abbe072003-06-16 23:50:08 +000045
46 AT91F_SpiInit ();
47
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020048 for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
Peter Pearsed4fc6012007-08-14 10:10:52 +010049 found[i] = 0;
wdenk5779d8d2003-12-06 23:55:10 +000050 dataflash_info[i].Desc.state = IDLE;
wdenk2abbe072003-06-16 23:50:08 +000051 dataflash_info[i].id = 0;
52 dataflash_info[i].Device.pages_number = 0;
Stelian Popd49fe4b2008-01-20 21:07:00 +000053 dfcode = AT91F_DataflashProbe (cs[i].cs,
Peter Pearsed4fc6012007-08-14 10:10:52 +010054 &dataflash_info[i].Desc);
wdenk2abbe072003-06-16 23:50:08 +000055
56 switch (dfcode) {
Sergey Lapinc6457e32008-06-05 11:06:29 +040057 case AT45DB021:
58 dataflash_info[i].Device.pages_number = 1024;
Sergey Lapinf77d92a2008-08-09 01:39:09 +040059 dataflash_info[i].Device.pages_size = 264;
Sergey Lapinc6457e32008-06-05 11:06:29 +040060 dataflash_info[i].Device.page_offset = 9;
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;
66 found[i] += dfcode;;
67 break;
Sergey Lapinf77d92a2008-08-09 01:39:09 +040068
69 case AT45DB081:
70 dataflash_info[i].Device.pages_number = 4096;
71 dataflash_info[i].Device.pages_size = 264;
72 dataflash_info[i].Device.page_offset = 9;
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;
78 found[i] += dfcode;;
79 break;
80
wdenk2abbe072003-06-16 23:50:08 +000081 case AT45DB161:
82 dataflash_info[i].Device.pages_number = 4096;
83 dataflash_info[i].Device.pages_size = 528;
84 dataflash_info[i].Device.page_offset = 10;
85 dataflash_info[i].Device.byte_mask = 0x300;
Stelian Popd49fe4b2008-01-20 21:07:00 +000086 dataflash_info[i].Device.cs = cs[i].cs;
wdenk2abbe072003-06-16 23:50:08 +000087 dataflash_info[i].Desc.DataFlash_state = IDLE;
Stelian Popd49fe4b2008-01-20 21:07:00 +000088 dataflash_info[i].logical_address = cs[i].addr;
wdenk2abbe072003-06-16 23:50:08 +000089 dataflash_info[i].id = dfcode;
Peter Pearsed4fc6012007-08-14 10:10:52 +010090 found[i] += dfcode;;
wdenk2abbe072003-06-16 23:50:08 +000091 break;
92
93 case AT45DB321:
94 dataflash_info[i].Device.pages_number = 8192;
95 dataflash_info[i].Device.pages_size = 528;
96 dataflash_info[i].Device.page_offset = 10;
97 dataflash_info[i].Device.byte_mask = 0x300;
Stelian Popd49fe4b2008-01-20 21:07:00 +000098 dataflash_info[i].Device.cs = cs[i].cs;
wdenk2abbe072003-06-16 23:50:08 +000099 dataflash_info[i].Desc.DataFlash_state = IDLE;
Stelian Popd49fe4b2008-01-20 21:07:00 +0000100 dataflash_info[i].logical_address = cs[i].addr;
wdenk2abbe072003-06-16 23:50:08 +0000101 dataflash_info[i].id = dfcode;
Peter Pearsed4fc6012007-08-14 10:10:52 +0100102 found[i] += dfcode;;
wdenk2abbe072003-06-16 23:50:08 +0000103 break;
104
105 case AT45DB642:
106 dataflash_info[i].Device.pages_number = 8192;
107 dataflash_info[i].Device.pages_size = 1056;
108 dataflash_info[i].Device.page_offset = 11;
109 dataflash_info[i].Device.byte_mask = 0x700;
Stelian Popd49fe4b2008-01-20 21:07:00 +0000110 dataflash_info[i].Device.cs = cs[i].cs;
wdenk2abbe072003-06-16 23:50:08 +0000111 dataflash_info[i].Desc.DataFlash_state = IDLE;
Stelian Popd49fe4b2008-01-20 21:07:00 +0000112 dataflash_info[i].logical_address = cs[i].addr;
wdenk2abbe072003-06-16 23:50:08 +0000113 dataflash_info[i].id = dfcode;
Peter Pearsed4fc6012007-08-14 10:10:52 +0100114 found[i] += dfcode;;
wdenk2abbe072003-06-16 23:50:08 +0000115 break;
Peter Pearsed4fc6012007-08-14 10:10:52 +0100116
wdenk5779d8d2003-12-06 23:55:10 +0000117 case AT45DB128:
118 dataflash_info[i].Device.pages_number = 16384;
119 dataflash_info[i].Device.pages_size = 1056;
120 dataflash_info[i].Device.page_offset = 11;
121 dataflash_info[i].Device.byte_mask = 0x700;
Stelian Popd49fe4b2008-01-20 21:07:00 +0000122 dataflash_info[i].Device.cs = cs[i].cs;
wdenk5779d8d2003-12-06 23:55:10 +0000123 dataflash_info[i].Desc.DataFlash_state = IDLE;
Stelian Popd49fe4b2008-01-20 21:07:00 +0000124 dataflash_info[i].logical_address = cs[i].addr;
wdenk5779d8d2003-12-06 23:55:10 +0000125 dataflash_info[i].id = dfcode;
Peter Pearsed4fc6012007-08-14 10:10:52 +0100126 found[i] += dfcode;;
wdenk5779d8d2003-12-06 23:55:10 +0000127 break;
wdenk2abbe072003-06-16 23:50:08 +0000128
129 default:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100130 dfcode = 0;
wdenk2abbe072003-06-16 23:50:08 +0000131 break;
132 }
wdenk5779d8d2003-12-06 23:55:10 +0000133 /* set the last area end to the dataflash size*/
Ilko Ilievbd876772008-12-02 17:27:54 +0100134 dataflash_info[i].end_address =
wdenk5779d8d2003-12-06 23:55:10 +0000135 (dataflash_info[i].Device.pages_number *
Ilko Iliev03f79772008-12-02 17:20:17 +0100136 dataflash_info[i].Device.pages_size) - 1;
wdenk2abbe072003-06-16 23:50:08 +0000137
Stelian Pop5604e212008-03-26 21:52:36 +0100138 part = 0;
139 last_part = 0;
wdenk5779d8d2003-12-06 23:55:10 +0000140 /* set the area addresses */
Ilko Iliev03f79772008-12-02 17:20:17 +0100141 for(j = 0; j < NB_DATAFLASH_AREA; j++) {
Peter Pearsed4fc6012007-08-14 10:10:52 +0100142 if(found[i]!=0) {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200143 dataflash_info[i].Device.area_list[j].start =
144 area_list[part].start +
Peter Pearsed4fc6012007-08-14 10:10:52 +0100145 dataflash_info[i].logical_address;
146 if(area_list[part].end == 0xffffffff) {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200147 dataflash_info[i].Device.area_list[j].end =
148 dataflash_info[i].end_address +
Ilko Iliev03f79772008-12-02 17:20:17 +0100149 dataflash_info[i].logical_address;
Peter Pearsed4fc6012007-08-14 10:10:52 +0100150 last_part = 1;
151 } else {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200152 dataflash_info[i].Device.area_list[j].end =
153 area_list[part].end +
Peter Pearsed4fc6012007-08-14 10:10:52 +0100154 dataflash_info[i].logical_address;
155 }
156 protected = area_list[part].protected;
157 /* Set the environment according to the label...*/
158 if(protected == FLAG_PROTECT_INVALID) {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200159 dataflash_info[i].Device.area_list[j].protected =
Peter Pearsed4fc6012007-08-14 10:10:52 +0100160 FLAG_PROTECT_INVALID;
161 } else {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200162 dataflash_info[i].Device.area_list[j].protected =
Peter Pearsed4fc6012007-08-14 10:10:52 +0100163 protected;
164 }
165 strcpy((char*)(dataflash_info[i].Device.area_list[j].label),
166 (const char *)area_list[part].label);
167 }
168 part++;
wdenk5779d8d2003-12-06 23:55:10 +0000169 }
wdenk2abbe072003-06-16 23:50:08 +0000170 }
Peter Pearsed4fc6012007-08-14 10:10:52 +0100171 return found[0];
wdenk2abbe072003-06-16 23:50:08 +0000172}
173
Ulf Samuelssonc3a60cb2008-04-12 20:29:44 +0200174void AT91F_DataflashSetEnv (void)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100175{
176 int i, j;
177 int part;
178 unsigned char env;
179 unsigned char s[32]; /* Will fit a long int in hex */
180 unsigned long start;
Stelian Pop5604e212008-03-26 21:52:36 +0100181
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200182 for (i = 0, part= 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
Ilko Iliev03f79772008-12-02 17:20:17 +0100183 for(j = 0; j < NB_DATAFLASH_AREA; j++) {
Peter Pearsed4fc6012007-08-14 10:10:52 +0100184 env = area_list[part].setenv;
185 /* Set the environment according to the label...*/
186 if((env & FLAG_SETENV) == FLAG_SETENV) {
Ilko Iliev03f79772008-12-02 17:20:17 +0100187 start = dataflash_info[i].Device.area_list[j].start;
Jean-Christophe PLAGNIOL-VILLARD0a5676b2008-07-12 14:36:34 +0200188 sprintf((char*) s,"%lX",start);
Ulf Samuelssonc3a60cb2008-04-12 20:29:44 +0200189 setenv((char*) area_list[part].label,(char*) s);
Peter Pearsed4fc6012007-08-14 10:10:52 +0100190 }
191 part++;
192 }
193 }
194}
wdenk2abbe072003-06-16 23:50:08 +0000195
wdenk2abbe072003-06-16 23:50:08 +0000196void dataflash_print_info (void)
197{
wdenk5779d8d2003-12-06 23:55:10 +0000198 int i, j;
wdenk2abbe072003-06-16 23:50:08 +0000199
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200200 for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
wdenk2abbe072003-06-16 23:50:08 +0000201 if (dataflash_info[i].id != 0) {
Peter Pearsed4fc6012007-08-14 10:10:52 +0100202 printf("DataFlash:");
wdenk2abbe072003-06-16 23:50:08 +0000203 switch (dataflash_info[i].id) {
Sergey Lapinc6457e32008-06-05 11:06:29 +0400204 case AT45DB021:
205 printf("AT45DB021\n");
206 break;
wdenk2abbe072003-06-16 23:50:08 +0000207 case AT45DB161:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100208 printf("AT45DB161\n");
wdenk2abbe072003-06-16 23:50:08 +0000209 break;
210
211 case AT45DB321:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100212 printf("AT45DB321\n");
wdenk2abbe072003-06-16 23:50:08 +0000213 break;
214
215 case AT45DB642:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100216 printf("AT45DB642\n");
wdenk2abbe072003-06-16 23:50:08 +0000217 break;
wdenk5779d8d2003-12-06 23:55:10 +0000218 case AT45DB128:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100219 printf("AT45DB128\n");
wdenk5779d8d2003-12-06 23:55:10 +0000220 break;
wdenk2abbe072003-06-16 23:50:08 +0000221 }
222
Peter Pearsed4fc6012007-08-14 10:10:52 +0100223 printf("Nb pages: %6d\n"
wdenk2abbe072003-06-16 23:50:08 +0000224 "Page Size: %6d\n"
225 "Size=%8d bytes\n"
226 "Logical address: 0x%08X\n",
227 (unsigned int) dataflash_info[i].Device.pages_number,
228 (unsigned int) dataflash_info[i].Device.pages_size,
229 (unsigned int) dataflash_info[i].Device.pages_number *
230 dataflash_info[i].Device.pages_size,
231 (unsigned int) dataflash_info[i].logical_address);
Ilko Iliev03f79772008-12-02 17:20:17 +0100232 for (j = 0; j < NB_DATAFLASH_AREA; j++) {
Peter Pearsed4fc6012007-08-14 10:10:52 +0100233 switch(dataflash_info[i].Device.area_list[j].protected) {
234 case FLAG_PROTECT_SET:
235 case FLAG_PROTECT_CLEAR:
236 printf("Area %i:\t%08lX to %08lX %s", j,
237 dataflash_info[i].Device.area_list[j].start,
238 dataflash_info[i].Device.area_list[j].end,
239 (dataflash_info[i].Device.area_list[j].protected==FLAG_PROTECT_SET) ? "(RO)" : " ");
Peter Pearsed4fc6012007-08-14 10:10:52 +0100240 printf(" %s\n", dataflash_info[i].Device.area_list[j].label);
Peter Pearsed4fc6012007-08-14 10:10:52 +0100241 break;
Peter Pearsed4fc6012007-08-14 10:10:52 +0100242 case FLAG_PROTECT_INVALID:
243 break;
Peter Pearsed4fc6012007-08-14 10:10:52 +0100244 }
wdenk5779d8d2003-12-06 23:55:10 +0000245 }
wdenk2abbe072003-06-16 23:50:08 +0000246 }
247 }
248}
249
Peter Pearsed4fc6012007-08-14 10:10:52 +0100250/*---------------------------------------------------------------------------*/
Stelian Pop5604e212008-03-26 21:52:36 +0100251/* Function Name : AT91F_DataflashSelect */
Peter Pearsed4fc6012007-08-14 10:10:52 +0100252/* Object : Select the correct device */
253/*---------------------------------------------------------------------------*/
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200254AT91PS_DataFlash AT91F_DataflashSelect (AT91PS_DataFlash pFlash,
Peter Pearsed4fc6012007-08-14 10:10:52 +0100255 unsigned long *addr)
wdenk2abbe072003-06-16 23:50:08 +0000256{
257 char addr_valid = 0;
258 int i;
259
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200260 for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100261 if ( dataflash_info[i].id
Stelian Popc77ce472008-01-14 22:08:14 +0100262 && ((((int) *addr) & 0xFF000000) ==
Peter Pearsed4fc6012007-08-14 10:10:52 +0100263 dataflash_info[i].logical_address)) {
wdenk2abbe072003-06-16 23:50:08 +0000264 addr_valid = 1;
265 break;
266 }
267 if (!addr_valid) {
268 pFlash = (AT91PS_DataFlash) 0;
269 return pFlash;
270 }
271 pFlash->pDataFlashDesc = &(dataflash_info[i].Desc);
272 pFlash->pDevice = &(dataflash_info[i].Device);
273 *addr -= dataflash_info[i].logical_address;
274 return (pFlash);
275}
276
Peter Pearsed4fc6012007-08-14 10:10:52 +0100277/*---------------------------------------------------------------------------*/
Stelian Pop5604e212008-03-26 21:52:36 +0100278/* Function Name : addr_dataflash */
Peter Pearsed4fc6012007-08-14 10:10:52 +0100279/* Object : Test if address is valid */
280/*---------------------------------------------------------------------------*/
wdenk2abbe072003-06-16 23:50:08 +0000281int addr_dataflash (unsigned long addr)
282{
283 int addr_valid = 0;
284 int i;
285
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200286 for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
wdenk2abbe072003-06-16 23:50:08 +0000287 if ((((int) addr) & 0xFF000000) ==
288 dataflash_info[i].logical_address) {
289 addr_valid = 1;
290 break;
291 }
292 }
293
294 return addr_valid;
295}
Stelian Pop5604e212008-03-26 21:52:36 +0100296
Peter Pearsed4fc6012007-08-14 10:10:52 +0100297/*---------------------------------------------------------------------------*/
Stelian Pop5604e212008-03-26 21:52:36 +0100298/* Function Name : size_dataflash */
Peter Pearsed4fc6012007-08-14 10:10:52 +0100299/* Object : Test if address is valid regarding the size */
300/*---------------------------------------------------------------------------*/
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200301int size_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr,
Peter Pearsed4fc6012007-08-14 10:10:52 +0100302 unsigned long size)
wdenk5779d8d2003-12-06 23:55:10 +0000303{
304 /* is outside the dataflash */
305 if (((int)addr & 0x0FFFFFFF) > (pdataFlash->pDevice->pages_size *
306 pdataFlash->pDevice->pages_number)) return 0;
307 /* is too large for the dataflash */
308 if (size > ((pdataFlash->pDevice->pages_size *
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200309 pdataFlash->pDevice->pages_number) -
Peter Pearsed4fc6012007-08-14 10:10:52 +0100310 ((int)addr & 0x0FFFFFFF))) return 0;
wdenk5779d8d2003-12-06 23:55:10 +0000311
312 return 1;
313}
Stelian Pop5604e212008-03-26 21:52:36 +0100314
Peter Pearsed4fc6012007-08-14 10:10:52 +0100315/*---------------------------------------------------------------------------*/
Stelian Pop5604e212008-03-26 21:52:36 +0100316/* Function Name : prot_dataflash */
Peter Pearsed4fc6012007-08-14 10:10:52 +0100317/* Object : Test if destination area is protected */
318/*---------------------------------------------------------------------------*/
wdenk5779d8d2003-12-06 23:55:10 +0000319int prot_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr)
320{
Stelian Pop5604e212008-03-26 21:52:36 +0100321 int area;
322
wdenk5779d8d2003-12-06 23:55:10 +0000323 /* find area */
Ilko Iliev03f79772008-12-02 17:20:17 +0100324 for (area = 0; area < NB_DATAFLASH_AREA; area++) {
wdenk5779d8d2003-12-06 23:55:10 +0000325 if ((addr >= pdataFlash->pDevice->area_list[area].start) &&
326 (addr < pdataFlash->pDevice->area_list[area].end))
327 break;
328 }
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200329 if (area == NB_DATAFLASH_AREA)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100330 return -1;
331
wdenk5779d8d2003-12-06 23:55:10 +0000332 /*test protection value*/
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200333 if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_SET)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100334 return 0;
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200335 if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_INVALID)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100336 return 0;
wdenk5779d8d2003-12-06 23:55:10 +0000337
338 return 1;
339}
Stelian Pop5604e212008-03-26 21:52:36 +0100340
Peter Pearsed4fc6012007-08-14 10:10:52 +0100341/*--------------------------------------------------------------------------*/
342/* Function Name : dataflash_real_protect */
343/* Object : protect/unprotect area */
344/*--------------------------------------------------------------------------*/
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200345int dataflash_real_protect (int flag, unsigned long start_addr,
Peter Pearsed4fc6012007-08-14 10:10:52 +0100346 unsigned long end_addr)
wdenk5779d8d2003-12-06 23:55:10 +0000347{
Stelian Pop5604e212008-03-26 21:52:36 +0100348 int i,j, area1, area2, addr_valid = 0;
349
wdenk5779d8d2003-12-06 23:55:10 +0000350 /* find dataflash */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200351 for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
wdenk5779d8d2003-12-06 23:55:10 +0000352 if ((((int) start_addr) & 0xF0000000) ==
353 dataflash_info[i].logical_address) {
354 addr_valid = 1;
355 break;
356 }
357 }
358 if (!addr_valid) {
359 return -1;
360 }
361 /* find start area */
Ilko Iliev03f79772008-12-02 17:20:17 +0100362 for (area1 = 0; area1 < NB_DATAFLASH_AREA; area1++) {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200363 if (start_addr == dataflash_info[i].Device.area_list[area1].start)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100364 break;
wdenk5779d8d2003-12-06 23:55:10 +0000365 }
366 if (area1 == NB_DATAFLASH_AREA) return -1;
367 /* find end area */
Ilko Iliev03f79772008-12-02 17:20:17 +0100368 for (area2 = 0; area2 < NB_DATAFLASH_AREA; area2++) {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200369 if (end_addr == dataflash_info[i].Device.area_list[area2].end)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100370 break;
wdenk5779d8d2003-12-06 23:55:10 +0000371 }
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200372 if (area2 == NB_DATAFLASH_AREA)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100373 return -1;
wdenk5779d8d2003-12-06 23:55:10 +0000374
375 /*set protection value*/
Ilko Iliev03f79772008-12-02 17:20:17 +0100376 for(j = area1; j < area2 + 1 ; j++)
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200377 if(dataflash_info[i].Device.area_list[j].protected
Peter Pearsed4fc6012007-08-14 10:10:52 +0100378 != FLAG_PROTECT_INVALID) {
379 if (flag == 0) {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200380 dataflash_info[i].Device.area_list[j].protected
Peter Pearsed4fc6012007-08-14 10:10:52 +0100381 = FLAG_PROTECT_CLEAR;
382 } else {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200383 dataflash_info[i].Device.area_list[j].protected
Peter Pearsed4fc6012007-08-14 10:10:52 +0100384 = FLAG_PROTECT_SET;
385 }
386 }
wdenk5779d8d2003-12-06 23:55:10 +0000387
Ilko Iliev03f79772008-12-02 17:20:17 +0100388 return (area2 - area1 + 1);
wdenk5779d8d2003-12-06 23:55:10 +0000389}
wdenk2abbe072003-06-16 23:50:08 +0000390
Peter Pearsed4fc6012007-08-14 10:10:52 +0100391/*---------------------------------------------------------------------------*/
Stelian Pop5604e212008-03-26 21:52:36 +0100392/* Function Name : read_dataflash */
Peter Pearsed4fc6012007-08-14 10:10:52 +0100393/* Object : dataflash memory read */
394/*---------------------------------------------------------------------------*/
wdenk2abbe072003-06-16 23:50:08 +0000395int read_dataflash (unsigned long addr, unsigned long size, char *result)
396{
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100397 unsigned long AddrToRead = addr;
wdenk2abbe072003-06-16 23:50:08 +0000398 AT91PS_DataFlash pFlash = &DataFlashInst;
399
400 pFlash = AT91F_DataflashSelect (pFlash, &AddrToRead);
wdenk5779d8d2003-12-06 23:55:10 +0000401
wdenk2abbe072003-06-16 23:50:08 +0000402 if (pFlash == 0)
wdenk5779d8d2003-12-06 23:55:10 +0000403 return ERR_UNKNOWN_FLASH_TYPE;
404
405 if (size_dataflash(pFlash,addr,size) == 0)
406 return ERR_INVAL;
wdenk2abbe072003-06-16 23:50:08 +0000407
408 return (AT91F_DataFlashRead (pFlash, AddrToRead, size, result));
409}
410
Peter Pearsed4fc6012007-08-14 10:10:52 +0100411/*---------------------------------------------------------------------------*/
Stelian Pop5604e212008-03-26 21:52:36 +0100412/* Function Name : write_dataflash */
Peter Pearsed4fc6012007-08-14 10:10:52 +0100413/* Object : write a block in dataflash */
414/*---------------------------------------------------------------------------*/
wdenk2abbe072003-06-16 23:50:08 +0000415int write_dataflash (unsigned long addr_dest, unsigned long addr_src,
Peter Pearsed4fc6012007-08-14 10:10:52 +0100416 unsigned long size)
wdenk2abbe072003-06-16 23:50:08 +0000417{
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100418 unsigned long AddrToWrite = addr_dest;
wdenk2abbe072003-06-16 23:50:08 +0000419 AT91PS_DataFlash pFlash = &DataFlashInst;
420
421 pFlash = AT91F_DataflashSelect (pFlash, &AddrToWrite);
wdenk5779d8d2003-12-06 23:55:10 +0000422
423 if (pFlash == 0)
424 return ERR_UNKNOWN_FLASH_TYPE;
425
426 if (size_dataflash(pFlash,addr_dest,size) == 0)
427 return ERR_INVAL;
428
429 if (prot_dataflash(pFlash,addr_dest) == 0)
430 return ERR_PROTECTED;
431
wdenk2abbe072003-06-16 23:50:08 +0000432 if (AddrToWrite == -1)
433 return -1;
434
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200435 return AT91F_DataFlashWrite (pFlash, (uchar *)addr_src,
Peter Pearsed4fc6012007-08-14 10:10:52 +0100436 AddrToWrite, size);
wdenk2abbe072003-06-16 23:50:08 +0000437}
438
wdenk2abbe072003-06-16 23:50:08 +0000439void dataflash_perror (int err)
440{
441 switch (err) {
442 case ERR_OK:
443 break;
444 case ERR_TIMOUT:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100445 printf("Timeout writing to DataFlash\n");
wdenk2abbe072003-06-16 23:50:08 +0000446 break;
447 case ERR_PROTECTED:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100448 printf("Can't write to protected/invalid DataFlash sectors\n");
wdenk2abbe072003-06-16 23:50:08 +0000449 break;
450 case ERR_INVAL:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100451 printf("Outside available DataFlash\n");
wdenk2abbe072003-06-16 23:50:08 +0000452 break;
453 case ERR_UNKNOWN_FLASH_TYPE:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100454 printf("Unknown Type of DataFlash\n");
wdenk2abbe072003-06-16 23:50:08 +0000455 break;
456 case ERR_PROG_ERROR:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100457 printf("General DataFlash Programming Error\n");
wdenk2abbe072003-06-16 23:50:08 +0000458 break;
459 default:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100460 printf("%s[%d] FIXME: rc=%d\n", __FILE__, __LINE__, err);
wdenk2abbe072003-06-16 23:50:08 +0000461 break;
462 }
463}