blob: e961f518b085107e34636cb868f2316cc6508fdd [file] [log] [blame]
Wolfgang Denk1a459662013-07-08 09:37:19 +02001/*
2 * LowLevel function for ATMEL DataFlash support
wdenk2abbe072003-06-16 23:50:08 +00003 * Author : Hamid Ikdoumi (Atmel)
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk2abbe072003-06-16 23:50:08 +00006 */
7#include <common.h>
8#include <config.h>
wdenk2abbe072003-06-16 23:50:08 +00009#include <asm/hardware.h>
10#include <dataflash.h>
11
wdenk2abbe072003-06-16 23:50:08 +000012static AT91S_DataFlash DataFlashInst;
13
wdenk2abbe072003-06-16 23:50:08 +000014extern void AT91F_SpiInit (void);
15extern int AT91F_DataflashProbe (int i, AT91PS_DataflashDesc pDesc);
16extern int AT91F_DataFlashRead (AT91PS_DataFlash pDataFlash,
17 unsigned long addr,
18 unsigned long size, char *buffer);
wdenk5779d8d2003-12-06 23:55:10 +000019extern int AT91F_DataFlashWrite( AT91PS_DataFlash pDataFlash,
Peter Pearsed4fc6012007-08-14 10:10:52 +010020 unsigned char *src,
21 int dest,
22 int size );
wdenk2abbe072003-06-16 23:50:08 +000023
24int AT91F_DataflashInit (void)
25{
26 int i, j;
27 int dfcode;
Stelian Pop5604e212008-03-26 21:52:36 +010028 int part;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020029 int found[CONFIG_SYS_MAX_DATAFLASH_BANKS];
Peter Pearsed4fc6012007-08-14 10:10:52 +010030 unsigned char protected;
wdenk2abbe072003-06-16 23:50:08 +000031
32 AT91F_SpiInit ();
33
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020034 for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
Peter Pearsed4fc6012007-08-14 10:10:52 +010035 found[i] = 0;
wdenk5779d8d2003-12-06 23:55:10 +000036 dataflash_info[i].Desc.state = IDLE;
wdenk2abbe072003-06-16 23:50:08 +000037 dataflash_info[i].id = 0;
38 dataflash_info[i].Device.pages_number = 0;
Stelian Popd49fe4b2008-01-20 21:07:00 +000039 dfcode = AT91F_DataflashProbe (cs[i].cs,
Peter Pearsed4fc6012007-08-14 10:10:52 +010040 &dataflash_info[i].Desc);
wdenk2abbe072003-06-16 23:50:08 +000041
42 switch (dfcode) {
Sergey Lapinc6457e32008-06-05 11:06:29 +040043 case AT45DB021:
44 dataflash_info[i].Device.pages_number = 1024;
Sergey Lapinf77d92a2008-08-09 01:39:09 +040045 dataflash_info[i].Device.pages_size = 264;
Sergey Lapinc6457e32008-06-05 11:06:29 +040046 dataflash_info[i].Device.page_offset = 9;
47 dataflash_info[i].Device.byte_mask = 0x300;
48 dataflash_info[i].Device.cs = cs[i].cs;
49 dataflash_info[i].Desc.DataFlash_state = IDLE;
50 dataflash_info[i].logical_address = cs[i].addr;
51 dataflash_info[i].id = dfcode;
Masahiro Yamada51855e82017-06-13 15:17:28 +090052 found[i] += dfcode;
Sergey Lapinc6457e32008-06-05 11:06:29 +040053 break;
Sergey Lapinf77d92a2008-08-09 01:39:09 +040054
55 case AT45DB081:
56 dataflash_info[i].Device.pages_number = 4096;
57 dataflash_info[i].Device.pages_size = 264;
58 dataflash_info[i].Device.page_offset = 9;
59 dataflash_info[i].Device.byte_mask = 0x300;
60 dataflash_info[i].Device.cs = cs[i].cs;
61 dataflash_info[i].Desc.DataFlash_state = IDLE;
62 dataflash_info[i].logical_address = cs[i].addr;
63 dataflash_info[i].id = dfcode;
Masahiro Yamada51855e82017-06-13 15:17:28 +090064 found[i] += dfcode;
Sergey Lapinf77d92a2008-08-09 01:39:09 +040065 break;
66
wdenk2abbe072003-06-16 23:50:08 +000067 case AT45DB161:
68 dataflash_info[i].Device.pages_number = 4096;
69 dataflash_info[i].Device.pages_size = 528;
70 dataflash_info[i].Device.page_offset = 10;
71 dataflash_info[i].Device.byte_mask = 0x300;
Stelian Popd49fe4b2008-01-20 21:07:00 +000072 dataflash_info[i].Device.cs = cs[i].cs;
wdenk2abbe072003-06-16 23:50:08 +000073 dataflash_info[i].Desc.DataFlash_state = IDLE;
Stelian Popd49fe4b2008-01-20 21:07:00 +000074 dataflash_info[i].logical_address = cs[i].addr;
wdenk2abbe072003-06-16 23:50:08 +000075 dataflash_info[i].id = dfcode;
Masahiro Yamada51855e82017-06-13 15:17:28 +090076 found[i] += dfcode;
wdenk2abbe072003-06-16 23:50:08 +000077 break;
78
79 case AT45DB321:
80 dataflash_info[i].Device.pages_number = 8192;
81 dataflash_info[i].Device.pages_size = 528;
82 dataflash_info[i].Device.page_offset = 10;
83 dataflash_info[i].Device.byte_mask = 0x300;
Stelian Popd49fe4b2008-01-20 21:07:00 +000084 dataflash_info[i].Device.cs = cs[i].cs;
wdenk2abbe072003-06-16 23:50:08 +000085 dataflash_info[i].Desc.DataFlash_state = IDLE;
Stelian Popd49fe4b2008-01-20 21:07:00 +000086 dataflash_info[i].logical_address = cs[i].addr;
wdenk2abbe072003-06-16 23:50:08 +000087 dataflash_info[i].id = dfcode;
Masahiro Yamada51855e82017-06-13 15:17:28 +090088 found[i] += dfcode;
wdenk2abbe072003-06-16 23:50:08 +000089 break;
90
91 case AT45DB642:
92 dataflash_info[i].Device.pages_number = 8192;
93 dataflash_info[i].Device.pages_size = 1056;
94 dataflash_info[i].Device.page_offset = 11;
95 dataflash_info[i].Device.byte_mask = 0x700;
Stelian Popd49fe4b2008-01-20 21:07:00 +000096 dataflash_info[i].Device.cs = cs[i].cs;
wdenk2abbe072003-06-16 23:50:08 +000097 dataflash_info[i].Desc.DataFlash_state = IDLE;
Stelian Popd49fe4b2008-01-20 21:07:00 +000098 dataflash_info[i].logical_address = cs[i].addr;
wdenk2abbe072003-06-16 23:50:08 +000099 dataflash_info[i].id = dfcode;
Masahiro Yamada51855e82017-06-13 15:17:28 +0900100 found[i] += dfcode;
wdenk2abbe072003-06-16 23:50:08 +0000101 break;
Peter Pearsed4fc6012007-08-14 10:10:52 +0100102
wdenk5779d8d2003-12-06 23:55:10 +0000103 case AT45DB128:
104 dataflash_info[i].Device.pages_number = 16384;
105 dataflash_info[i].Device.pages_size = 1056;
106 dataflash_info[i].Device.page_offset = 11;
107 dataflash_info[i].Device.byte_mask = 0x700;
Stelian Popd49fe4b2008-01-20 21:07:00 +0000108 dataflash_info[i].Device.cs = cs[i].cs;
wdenk5779d8d2003-12-06 23:55:10 +0000109 dataflash_info[i].Desc.DataFlash_state = IDLE;
Stelian Popd49fe4b2008-01-20 21:07:00 +0000110 dataflash_info[i].logical_address = cs[i].addr;
wdenk5779d8d2003-12-06 23:55:10 +0000111 dataflash_info[i].id = dfcode;
Masahiro Yamada51855e82017-06-13 15:17:28 +0900112 found[i] += dfcode;
wdenk5779d8d2003-12-06 23:55:10 +0000113 break;
wdenk2abbe072003-06-16 23:50:08 +0000114
115 default:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100116 dfcode = 0;
wdenk2abbe072003-06-16 23:50:08 +0000117 break;
118 }
wdenk5779d8d2003-12-06 23:55:10 +0000119 /* set the last area end to the dataflash size*/
Ilko Ilievbd876772008-12-02 17:27:54 +0100120 dataflash_info[i].end_address =
wdenk5779d8d2003-12-06 23:55:10 +0000121 (dataflash_info[i].Device.pages_number *
Ilko Iliev03f79772008-12-02 17:20:17 +0100122 dataflash_info[i].Device.pages_size) - 1;
wdenk2abbe072003-06-16 23:50:08 +0000123
Stelian Pop5604e212008-03-26 21:52:36 +0100124 part = 0;
wdenk5779d8d2003-12-06 23:55:10 +0000125 /* set the area addresses */
Ilko Iliev03f79772008-12-02 17:20:17 +0100126 for(j = 0; j < NB_DATAFLASH_AREA; j++) {
Peter Pearsed4fc6012007-08-14 10:10:52 +0100127 if(found[i]!=0) {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200128 dataflash_info[i].Device.area_list[j].start =
129 area_list[part].start +
Peter Pearsed4fc6012007-08-14 10:10:52 +0100130 dataflash_info[i].logical_address;
131 if(area_list[part].end == 0xffffffff) {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200132 dataflash_info[i].Device.area_list[j].end =
133 dataflash_info[i].end_address +
Ilko Iliev03f79772008-12-02 17:20:17 +0100134 dataflash_info[i].logical_address;
Peter Pearsed4fc6012007-08-14 10:10:52 +0100135 } else {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200136 dataflash_info[i].Device.area_list[j].end =
137 area_list[part].end +
Peter Pearsed4fc6012007-08-14 10:10:52 +0100138 dataflash_info[i].logical_address;
139 }
140 protected = area_list[part].protected;
141 /* Set the environment according to the label...*/
142 if(protected == FLAG_PROTECT_INVALID) {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200143 dataflash_info[i].Device.area_list[j].protected =
Peter Pearsed4fc6012007-08-14 10:10:52 +0100144 FLAG_PROTECT_INVALID;
145 } else {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200146 dataflash_info[i].Device.area_list[j].protected =
Peter Pearsed4fc6012007-08-14 10:10:52 +0100147 protected;
148 }
149 strcpy((char*)(dataflash_info[i].Device.area_list[j].label),
150 (const char *)area_list[part].label);
151 }
152 part++;
wdenk5779d8d2003-12-06 23:55:10 +0000153 }
wdenk2abbe072003-06-16 23:50:08 +0000154 }
Peter Pearsed4fc6012007-08-14 10:10:52 +0100155 return found[0];
wdenk2abbe072003-06-16 23:50:08 +0000156}
157
Simon Glass382bee52017-08-03 12:22:09 -0600158void AT91F_Dataflashenv_set(void)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100159{
160 int i, j;
161 int part;
162 unsigned char env;
163 unsigned char s[32]; /* Will fit a long int in hex */
164 unsigned long start;
Stelian Pop5604e212008-03-26 21:52:36 +0100165
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200166 for (i = 0, part= 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
Ilko Iliev03f79772008-12-02 17:20:17 +0100167 for(j = 0; j < NB_DATAFLASH_AREA; j++) {
Peter Pearsed4fc6012007-08-14 10:10:52 +0100168 env = area_list[part].setenv;
169 /* Set the environment according to the label...*/
170 if((env & FLAG_SETENV) == FLAG_SETENV) {
Ilko Iliev03f79772008-12-02 17:20:17 +0100171 start = dataflash_info[i].Device.area_list[j].start;
Simon Glass382bee52017-08-03 12:22:09 -0600172 sprintf((char *)s, "%lX", start);
173 env_set((char *)area_list[part].label,
174 (char *)s);
Peter Pearsed4fc6012007-08-14 10:10:52 +0100175 }
176 part++;
177 }
178 }
179}
wdenk2abbe072003-06-16 23:50:08 +0000180
wdenk2abbe072003-06-16 23:50:08 +0000181void dataflash_print_info (void)
182{
wdenk5779d8d2003-12-06 23:55:10 +0000183 int i, j;
wdenk2abbe072003-06-16 23:50:08 +0000184
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200185 for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
wdenk2abbe072003-06-16 23:50:08 +0000186 if (dataflash_info[i].id != 0) {
Peter Pearsed4fc6012007-08-14 10:10:52 +0100187 printf("DataFlash:");
wdenk2abbe072003-06-16 23:50:08 +0000188 switch (dataflash_info[i].id) {
Sergey Lapinc6457e32008-06-05 11:06:29 +0400189 case AT45DB021:
190 printf("AT45DB021\n");
191 break;
wdenk2abbe072003-06-16 23:50:08 +0000192 case AT45DB161:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100193 printf("AT45DB161\n");
wdenk2abbe072003-06-16 23:50:08 +0000194 break;
195
196 case AT45DB321:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100197 printf("AT45DB321\n");
wdenk2abbe072003-06-16 23:50:08 +0000198 break;
199
200 case AT45DB642:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100201 printf("AT45DB642\n");
wdenk2abbe072003-06-16 23:50:08 +0000202 break;
wdenk5779d8d2003-12-06 23:55:10 +0000203 case AT45DB128:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100204 printf("AT45DB128\n");
wdenk5779d8d2003-12-06 23:55:10 +0000205 break;
wdenk2abbe072003-06-16 23:50:08 +0000206 }
207
Peter Pearsed4fc6012007-08-14 10:10:52 +0100208 printf("Nb pages: %6d\n"
wdenk2abbe072003-06-16 23:50:08 +0000209 "Page Size: %6d\n"
210 "Size=%8d bytes\n"
211 "Logical address: 0x%08X\n",
212 (unsigned int) dataflash_info[i].Device.pages_number,
213 (unsigned int) dataflash_info[i].Device.pages_size,
214 (unsigned int) dataflash_info[i].Device.pages_number *
215 dataflash_info[i].Device.pages_size,
216 (unsigned int) dataflash_info[i].logical_address);
Ilko Iliev03f79772008-12-02 17:20:17 +0100217 for (j = 0; j < NB_DATAFLASH_AREA; j++) {
Peter Pearsed4fc6012007-08-14 10:10:52 +0100218 switch(dataflash_info[i].Device.area_list[j].protected) {
219 case FLAG_PROTECT_SET:
220 case FLAG_PROTECT_CLEAR:
221 printf("Area %i:\t%08lX to %08lX %s", j,
222 dataflash_info[i].Device.area_list[j].start,
223 dataflash_info[i].Device.area_list[j].end,
224 (dataflash_info[i].Device.area_list[j].protected==FLAG_PROTECT_SET) ? "(RO)" : " ");
Peter Pearsed4fc6012007-08-14 10:10:52 +0100225 printf(" %s\n", dataflash_info[i].Device.area_list[j].label);
Peter Pearsed4fc6012007-08-14 10:10:52 +0100226 break;
Peter Pearsed4fc6012007-08-14 10:10:52 +0100227 case FLAG_PROTECT_INVALID:
228 break;
Peter Pearsed4fc6012007-08-14 10:10:52 +0100229 }
wdenk5779d8d2003-12-06 23:55:10 +0000230 }
wdenk2abbe072003-06-16 23:50:08 +0000231 }
232 }
233}
234
Peter Pearsed4fc6012007-08-14 10:10:52 +0100235/*---------------------------------------------------------------------------*/
Stelian Pop5604e212008-03-26 21:52:36 +0100236/* Function Name : AT91F_DataflashSelect */
Peter Pearsed4fc6012007-08-14 10:10:52 +0100237/* Object : Select the correct device */
238/*---------------------------------------------------------------------------*/
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200239AT91PS_DataFlash AT91F_DataflashSelect (AT91PS_DataFlash pFlash,
Peter Pearsed4fc6012007-08-14 10:10:52 +0100240 unsigned long *addr)
wdenk2abbe072003-06-16 23:50:08 +0000241{
242 char addr_valid = 0;
243 int i;
244
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200245 for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100246 if ( dataflash_info[i].id
Stelian Popc77ce472008-01-14 22:08:14 +0100247 && ((((int) *addr) & 0xFF000000) ==
Peter Pearsed4fc6012007-08-14 10:10:52 +0100248 dataflash_info[i].logical_address)) {
wdenk2abbe072003-06-16 23:50:08 +0000249 addr_valid = 1;
250 break;
251 }
252 if (!addr_valid) {
253 pFlash = (AT91PS_DataFlash) 0;
254 return pFlash;
255 }
256 pFlash->pDataFlashDesc = &(dataflash_info[i].Desc);
257 pFlash->pDevice = &(dataflash_info[i].Device);
258 *addr -= dataflash_info[i].logical_address;
259 return (pFlash);
260}
261
Peter Pearsed4fc6012007-08-14 10:10:52 +0100262/*---------------------------------------------------------------------------*/
Stelian Pop5604e212008-03-26 21:52:36 +0100263/* Function Name : addr_dataflash */
Peter Pearsed4fc6012007-08-14 10:10:52 +0100264/* Object : Test if address is valid */
265/*---------------------------------------------------------------------------*/
wdenk2abbe072003-06-16 23:50:08 +0000266int addr_dataflash (unsigned long addr)
267{
268 int addr_valid = 0;
269 int i;
270
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200271 for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
wdenk2abbe072003-06-16 23:50:08 +0000272 if ((((int) addr) & 0xFF000000) ==
273 dataflash_info[i].logical_address) {
274 addr_valid = 1;
275 break;
276 }
277 }
278
279 return addr_valid;
280}
Stelian Pop5604e212008-03-26 21:52:36 +0100281
Peter Pearsed4fc6012007-08-14 10:10:52 +0100282/*---------------------------------------------------------------------------*/
Stelian Pop5604e212008-03-26 21:52:36 +0100283/* Function Name : size_dataflash */
Peter Pearsed4fc6012007-08-14 10:10:52 +0100284/* Object : Test if address is valid regarding the size */
285/*---------------------------------------------------------------------------*/
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200286int size_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr,
Peter Pearsed4fc6012007-08-14 10:10:52 +0100287 unsigned long size)
wdenk5779d8d2003-12-06 23:55:10 +0000288{
289 /* is outside the dataflash */
290 if (((int)addr & 0x0FFFFFFF) > (pdataFlash->pDevice->pages_size *
291 pdataFlash->pDevice->pages_number)) return 0;
292 /* is too large for the dataflash */
293 if (size > ((pdataFlash->pDevice->pages_size *
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200294 pdataFlash->pDevice->pages_number) -
Peter Pearsed4fc6012007-08-14 10:10:52 +0100295 ((int)addr & 0x0FFFFFFF))) return 0;
wdenk5779d8d2003-12-06 23:55:10 +0000296
297 return 1;
298}
Stelian Pop5604e212008-03-26 21:52:36 +0100299
Peter Pearsed4fc6012007-08-14 10:10:52 +0100300/*---------------------------------------------------------------------------*/
Stelian Pop5604e212008-03-26 21:52:36 +0100301/* Function Name : prot_dataflash */
Peter Pearsed4fc6012007-08-14 10:10:52 +0100302/* Object : Test if destination area is protected */
303/*---------------------------------------------------------------------------*/
wdenk5779d8d2003-12-06 23:55:10 +0000304int prot_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr)
305{
Stelian Pop5604e212008-03-26 21:52:36 +0100306 int area;
307
wdenk5779d8d2003-12-06 23:55:10 +0000308 /* find area */
Ilko Iliev03f79772008-12-02 17:20:17 +0100309 for (area = 0; area < NB_DATAFLASH_AREA; area++) {
wdenk5779d8d2003-12-06 23:55:10 +0000310 if ((addr >= pdataFlash->pDevice->area_list[area].start) &&
311 (addr < pdataFlash->pDevice->area_list[area].end))
312 break;
313 }
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200314 if (area == NB_DATAFLASH_AREA)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100315 return -1;
316
wdenk5779d8d2003-12-06 23:55:10 +0000317 /*test protection value*/
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200318 if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_SET)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100319 return 0;
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200320 if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_INVALID)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100321 return 0;
wdenk5779d8d2003-12-06 23:55:10 +0000322
323 return 1;
324}
Stelian Pop5604e212008-03-26 21:52:36 +0100325
Peter Pearsed4fc6012007-08-14 10:10:52 +0100326/*--------------------------------------------------------------------------*/
327/* Function Name : dataflash_real_protect */
328/* Object : protect/unprotect area */
329/*--------------------------------------------------------------------------*/
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200330int dataflash_real_protect (int flag, unsigned long start_addr,
Peter Pearsed4fc6012007-08-14 10:10:52 +0100331 unsigned long end_addr)
wdenk5779d8d2003-12-06 23:55:10 +0000332{
Stelian Pop5604e212008-03-26 21:52:36 +0100333 int i,j, area1, area2, addr_valid = 0;
334
wdenk5779d8d2003-12-06 23:55:10 +0000335 /* find dataflash */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200336 for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
wdenk5779d8d2003-12-06 23:55:10 +0000337 if ((((int) start_addr) & 0xF0000000) ==
338 dataflash_info[i].logical_address) {
339 addr_valid = 1;
340 break;
341 }
342 }
343 if (!addr_valid) {
344 return -1;
345 }
346 /* find start area */
Ilko Iliev03f79772008-12-02 17:20:17 +0100347 for (area1 = 0; area1 < NB_DATAFLASH_AREA; area1++) {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200348 if (start_addr == dataflash_info[i].Device.area_list[area1].start)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100349 break;
wdenk5779d8d2003-12-06 23:55:10 +0000350 }
351 if (area1 == NB_DATAFLASH_AREA) return -1;
352 /* find end area */
Ilko Iliev03f79772008-12-02 17:20:17 +0100353 for (area2 = 0; area2 < NB_DATAFLASH_AREA; area2++) {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200354 if (end_addr == dataflash_info[i].Device.area_list[area2].end)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100355 break;
wdenk5779d8d2003-12-06 23:55:10 +0000356 }
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200357 if (area2 == NB_DATAFLASH_AREA)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100358 return -1;
wdenk5779d8d2003-12-06 23:55:10 +0000359
360 /*set protection value*/
Ilko Iliev03f79772008-12-02 17:20:17 +0100361 for(j = area1; j < area2 + 1 ; j++)
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200362 if(dataflash_info[i].Device.area_list[j].protected
Peter Pearsed4fc6012007-08-14 10:10:52 +0100363 != FLAG_PROTECT_INVALID) {
364 if (flag == 0) {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200365 dataflash_info[i].Device.area_list[j].protected
Peter Pearsed4fc6012007-08-14 10:10:52 +0100366 = FLAG_PROTECT_CLEAR;
367 } else {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200368 dataflash_info[i].Device.area_list[j].protected
Peter Pearsed4fc6012007-08-14 10:10:52 +0100369 = FLAG_PROTECT_SET;
370 }
371 }
wdenk5779d8d2003-12-06 23:55:10 +0000372
Ilko Iliev03f79772008-12-02 17:20:17 +0100373 return (area2 - area1 + 1);
wdenk5779d8d2003-12-06 23:55:10 +0000374}
wdenk2abbe072003-06-16 23:50:08 +0000375
Peter Pearsed4fc6012007-08-14 10:10:52 +0100376/*---------------------------------------------------------------------------*/
Stelian Pop5604e212008-03-26 21:52:36 +0100377/* Function Name : read_dataflash */
Peter Pearsed4fc6012007-08-14 10:10:52 +0100378/* Object : dataflash memory read */
379/*---------------------------------------------------------------------------*/
wdenk2abbe072003-06-16 23:50:08 +0000380int read_dataflash (unsigned long addr, unsigned long size, char *result)
381{
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100382 unsigned long AddrToRead = addr;
wdenk2abbe072003-06-16 23:50:08 +0000383 AT91PS_DataFlash pFlash = &DataFlashInst;
384
385 pFlash = AT91F_DataflashSelect (pFlash, &AddrToRead);
wdenk5779d8d2003-12-06 23:55:10 +0000386
wdenk2abbe072003-06-16 23:50:08 +0000387 if (pFlash == 0)
wdenk5779d8d2003-12-06 23:55:10 +0000388 return ERR_UNKNOWN_FLASH_TYPE;
389
390 if (size_dataflash(pFlash,addr,size) == 0)
391 return ERR_INVAL;
wdenk2abbe072003-06-16 23:50:08 +0000392
393 return (AT91F_DataFlashRead (pFlash, AddrToRead, size, result));
394}
395
Peter Pearsed4fc6012007-08-14 10:10:52 +0100396/*---------------------------------------------------------------------------*/
Stelian Pop5604e212008-03-26 21:52:36 +0100397/* Function Name : write_dataflash */
Peter Pearsed4fc6012007-08-14 10:10:52 +0100398/* Object : write a block in dataflash */
399/*---------------------------------------------------------------------------*/
wdenk2abbe072003-06-16 23:50:08 +0000400int write_dataflash (unsigned long addr_dest, unsigned long addr_src,
Peter Pearsed4fc6012007-08-14 10:10:52 +0100401 unsigned long size)
wdenk2abbe072003-06-16 23:50:08 +0000402{
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100403 unsigned long AddrToWrite = addr_dest;
wdenk2abbe072003-06-16 23:50:08 +0000404 AT91PS_DataFlash pFlash = &DataFlashInst;
405
406 pFlash = AT91F_DataflashSelect (pFlash, &AddrToWrite);
wdenk5779d8d2003-12-06 23:55:10 +0000407
408 if (pFlash == 0)
409 return ERR_UNKNOWN_FLASH_TYPE;
410
411 if (size_dataflash(pFlash,addr_dest,size) == 0)
412 return ERR_INVAL;
413
414 if (prot_dataflash(pFlash,addr_dest) == 0)
415 return ERR_PROTECTED;
416
wdenk2abbe072003-06-16 23:50:08 +0000417 if (AddrToWrite == -1)
418 return -1;
419
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200420 return AT91F_DataFlashWrite (pFlash, (uchar *)addr_src,
Peter Pearsed4fc6012007-08-14 10:10:52 +0100421 AddrToWrite, size);
wdenk2abbe072003-06-16 23:50:08 +0000422}
423
wdenk2abbe072003-06-16 23:50:08 +0000424void dataflash_perror (int err)
425{
426 switch (err) {
427 case ERR_OK:
428 break;
429 case ERR_TIMOUT:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100430 printf("Timeout writing to DataFlash\n");
wdenk2abbe072003-06-16 23:50:08 +0000431 break;
432 case ERR_PROTECTED:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100433 printf("Can't write to protected/invalid DataFlash sectors\n");
wdenk2abbe072003-06-16 23:50:08 +0000434 break;
435 case ERR_INVAL:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100436 printf("Outside available DataFlash\n");
wdenk2abbe072003-06-16 23:50:08 +0000437 break;
438 case ERR_UNKNOWN_FLASH_TYPE:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100439 printf("Unknown Type of DataFlash\n");
wdenk2abbe072003-06-16 23:50:08 +0000440 break;
441 case ERR_PROG_ERROR:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100442 printf("General DataFlash Programming Error\n");
wdenk2abbe072003-06-16 23:50:08 +0000443 break;
444 default:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100445 printf("%s[%d] FIXME: rc=%d\n", __FILE__, __LINE__, err);
wdenk2abbe072003-06-16 23:50:08 +0000446 break;
447 }
448}