blob: 981ccd5b4b1ce697ef26d884d0e90bf6d1ad0a84 [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;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020042 int found[CONFIG_SYS_MAX_DATAFLASH_BANKS];
Peter Pearsed4fc6012007-08-14 10:10:52 +010043 unsigned char protected;
wdenk2abbe072003-06-16 23:50:08 +000044
45 AT91F_SpiInit ();
46
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020047 for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
Peter Pearsed4fc6012007-08-14 10:10:52 +010048 found[i] = 0;
wdenk5779d8d2003-12-06 23:55:10 +000049 dataflash_info[i].Desc.state = IDLE;
wdenk2abbe072003-06-16 23:50:08 +000050 dataflash_info[i].id = 0;
51 dataflash_info[i].Device.pages_number = 0;
Stelian Popd49fe4b2008-01-20 21:07:00 +000052 dfcode = AT91F_DataflashProbe (cs[i].cs,
Peter Pearsed4fc6012007-08-14 10:10:52 +010053 &dataflash_info[i].Desc);
wdenk2abbe072003-06-16 23:50:08 +000054
55 switch (dfcode) {
Sergey Lapinc6457e32008-06-05 11:06:29 +040056 case AT45DB021:
57 dataflash_info[i].Device.pages_number = 1024;
Sergey Lapinf77d92a2008-08-09 01:39:09 +040058 dataflash_info[i].Device.pages_size = 264;
Sergey Lapinc6457e32008-06-05 11:06:29 +040059 dataflash_info[i].Device.page_offset = 9;
60 dataflash_info[i].Device.byte_mask = 0x300;
61 dataflash_info[i].Device.cs = cs[i].cs;
62 dataflash_info[i].Desc.DataFlash_state = IDLE;
63 dataflash_info[i].logical_address = cs[i].addr;
64 dataflash_info[i].id = dfcode;
65 found[i] += dfcode;;
66 break;
Sergey Lapinf77d92a2008-08-09 01:39:09 +040067
68 case AT45DB081:
69 dataflash_info[i].Device.pages_number = 4096;
70 dataflash_info[i].Device.pages_size = 264;
71 dataflash_info[i].Device.page_offset = 9;
72 dataflash_info[i].Device.byte_mask = 0x300;
73 dataflash_info[i].Device.cs = cs[i].cs;
74 dataflash_info[i].Desc.DataFlash_state = IDLE;
75 dataflash_info[i].logical_address = cs[i].addr;
76 dataflash_info[i].id = dfcode;
77 found[i] += dfcode;;
78 break;
79
wdenk2abbe072003-06-16 23:50:08 +000080 case AT45DB161:
81 dataflash_info[i].Device.pages_number = 4096;
82 dataflash_info[i].Device.pages_size = 528;
83 dataflash_info[i].Device.page_offset = 10;
84 dataflash_info[i].Device.byte_mask = 0x300;
Stelian Popd49fe4b2008-01-20 21:07:00 +000085 dataflash_info[i].Device.cs = cs[i].cs;
wdenk2abbe072003-06-16 23:50:08 +000086 dataflash_info[i].Desc.DataFlash_state = IDLE;
Stelian Popd49fe4b2008-01-20 21:07:00 +000087 dataflash_info[i].logical_address = cs[i].addr;
wdenk2abbe072003-06-16 23:50:08 +000088 dataflash_info[i].id = dfcode;
Peter Pearsed4fc6012007-08-14 10:10:52 +010089 found[i] += dfcode;;
wdenk2abbe072003-06-16 23:50:08 +000090 break;
91
92 case AT45DB321:
93 dataflash_info[i].Device.pages_number = 8192;
94 dataflash_info[i].Device.pages_size = 528;
95 dataflash_info[i].Device.page_offset = 10;
96 dataflash_info[i].Device.byte_mask = 0x300;
Stelian Popd49fe4b2008-01-20 21:07:00 +000097 dataflash_info[i].Device.cs = cs[i].cs;
wdenk2abbe072003-06-16 23:50:08 +000098 dataflash_info[i].Desc.DataFlash_state = IDLE;
Stelian Popd49fe4b2008-01-20 21:07:00 +000099 dataflash_info[i].logical_address = cs[i].addr;
wdenk2abbe072003-06-16 23:50:08 +0000100 dataflash_info[i].id = dfcode;
Peter Pearsed4fc6012007-08-14 10:10:52 +0100101 found[i] += dfcode;;
wdenk2abbe072003-06-16 23:50:08 +0000102 break;
103
104 case AT45DB642:
105 dataflash_info[i].Device.pages_number = 8192;
106 dataflash_info[i].Device.pages_size = 1056;
107 dataflash_info[i].Device.page_offset = 11;
108 dataflash_info[i].Device.byte_mask = 0x700;
Stelian Popd49fe4b2008-01-20 21:07:00 +0000109 dataflash_info[i].Device.cs = cs[i].cs;
wdenk2abbe072003-06-16 23:50:08 +0000110 dataflash_info[i].Desc.DataFlash_state = IDLE;
Stelian Popd49fe4b2008-01-20 21:07:00 +0000111 dataflash_info[i].logical_address = cs[i].addr;
wdenk2abbe072003-06-16 23:50:08 +0000112 dataflash_info[i].id = dfcode;
Peter Pearsed4fc6012007-08-14 10:10:52 +0100113 found[i] += dfcode;;
wdenk2abbe072003-06-16 23:50:08 +0000114 break;
Peter Pearsed4fc6012007-08-14 10:10:52 +0100115
wdenk5779d8d2003-12-06 23:55:10 +0000116 case AT45DB128:
117 dataflash_info[i].Device.pages_number = 16384;
118 dataflash_info[i].Device.pages_size = 1056;
119 dataflash_info[i].Device.page_offset = 11;
120 dataflash_info[i].Device.byte_mask = 0x700;
Stelian Popd49fe4b2008-01-20 21:07:00 +0000121 dataflash_info[i].Device.cs = cs[i].cs;
wdenk5779d8d2003-12-06 23:55:10 +0000122 dataflash_info[i].Desc.DataFlash_state = IDLE;
Stelian Popd49fe4b2008-01-20 21:07:00 +0000123 dataflash_info[i].logical_address = cs[i].addr;
wdenk5779d8d2003-12-06 23:55:10 +0000124 dataflash_info[i].id = dfcode;
Peter Pearsed4fc6012007-08-14 10:10:52 +0100125 found[i] += dfcode;;
wdenk5779d8d2003-12-06 23:55:10 +0000126 break;
wdenk2abbe072003-06-16 23:50:08 +0000127
128 default:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100129 dfcode = 0;
wdenk2abbe072003-06-16 23:50:08 +0000130 break;
131 }
wdenk5779d8d2003-12-06 23:55:10 +0000132 /* set the last area end to the dataflash size*/
Ilko Ilievbd876772008-12-02 17:27:54 +0100133 dataflash_info[i].end_address =
wdenk5779d8d2003-12-06 23:55:10 +0000134 (dataflash_info[i].Device.pages_number *
Ilko Iliev03f79772008-12-02 17:20:17 +0100135 dataflash_info[i].Device.pages_size) - 1;
wdenk2abbe072003-06-16 23:50:08 +0000136
Stelian Pop5604e212008-03-26 21:52:36 +0100137 part = 0;
wdenk5779d8d2003-12-06 23:55:10 +0000138 /* set the area addresses */
Ilko Iliev03f79772008-12-02 17:20:17 +0100139 for(j = 0; j < NB_DATAFLASH_AREA; j++) {
Peter Pearsed4fc6012007-08-14 10:10:52 +0100140 if(found[i]!=0) {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200141 dataflash_info[i].Device.area_list[j].start =
142 area_list[part].start +
Peter Pearsed4fc6012007-08-14 10:10:52 +0100143 dataflash_info[i].logical_address;
144 if(area_list[part].end == 0xffffffff) {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200145 dataflash_info[i].Device.area_list[j].end =
146 dataflash_info[i].end_address +
Ilko Iliev03f79772008-12-02 17:20:17 +0100147 dataflash_info[i].logical_address;
Peter Pearsed4fc6012007-08-14 10:10:52 +0100148 } else {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200149 dataflash_info[i].Device.area_list[j].end =
150 area_list[part].end +
Peter Pearsed4fc6012007-08-14 10:10:52 +0100151 dataflash_info[i].logical_address;
152 }
153 protected = area_list[part].protected;
154 /* Set the environment according to the label...*/
155 if(protected == FLAG_PROTECT_INVALID) {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200156 dataflash_info[i].Device.area_list[j].protected =
Peter Pearsed4fc6012007-08-14 10:10:52 +0100157 FLAG_PROTECT_INVALID;
158 } else {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200159 dataflash_info[i].Device.area_list[j].protected =
Peter Pearsed4fc6012007-08-14 10:10:52 +0100160 protected;
161 }
162 strcpy((char*)(dataflash_info[i].Device.area_list[j].label),
163 (const char *)area_list[part].label);
164 }
165 part++;
wdenk5779d8d2003-12-06 23:55:10 +0000166 }
wdenk2abbe072003-06-16 23:50:08 +0000167 }
Peter Pearsed4fc6012007-08-14 10:10:52 +0100168 return found[0];
wdenk2abbe072003-06-16 23:50:08 +0000169}
170
Ulf Samuelssonc3a60cb2008-04-12 20:29:44 +0200171void AT91F_DataflashSetEnv (void)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100172{
173 int i, j;
174 int part;
175 unsigned char env;
176 unsigned char s[32]; /* Will fit a long int in hex */
177 unsigned long start;
Stelian Pop5604e212008-03-26 21:52:36 +0100178
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200179 for (i = 0, part= 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
Ilko Iliev03f79772008-12-02 17:20:17 +0100180 for(j = 0; j < NB_DATAFLASH_AREA; j++) {
Peter Pearsed4fc6012007-08-14 10:10:52 +0100181 env = area_list[part].setenv;
182 /* Set the environment according to the label...*/
183 if((env & FLAG_SETENV) == FLAG_SETENV) {
Ilko Iliev03f79772008-12-02 17:20:17 +0100184 start = dataflash_info[i].Device.area_list[j].start;
Jean-Christophe PLAGNIOL-VILLARD0a5676b2008-07-12 14:36:34 +0200185 sprintf((char*) s,"%lX",start);
Ulf Samuelssonc3a60cb2008-04-12 20:29:44 +0200186 setenv((char*) area_list[part].label,(char*) s);
Peter Pearsed4fc6012007-08-14 10:10:52 +0100187 }
188 part++;
189 }
190 }
191}
wdenk2abbe072003-06-16 23:50:08 +0000192
wdenk2abbe072003-06-16 23:50:08 +0000193void dataflash_print_info (void)
194{
wdenk5779d8d2003-12-06 23:55:10 +0000195 int i, j;
wdenk2abbe072003-06-16 23:50:08 +0000196
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200197 for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
wdenk2abbe072003-06-16 23:50:08 +0000198 if (dataflash_info[i].id != 0) {
Peter Pearsed4fc6012007-08-14 10:10:52 +0100199 printf("DataFlash:");
wdenk2abbe072003-06-16 23:50:08 +0000200 switch (dataflash_info[i].id) {
Sergey Lapinc6457e32008-06-05 11:06:29 +0400201 case AT45DB021:
202 printf("AT45DB021\n");
203 break;
wdenk2abbe072003-06-16 23:50:08 +0000204 case AT45DB161:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100205 printf("AT45DB161\n");
wdenk2abbe072003-06-16 23:50:08 +0000206 break;
207
208 case AT45DB321:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100209 printf("AT45DB321\n");
wdenk2abbe072003-06-16 23:50:08 +0000210 break;
211
212 case AT45DB642:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100213 printf("AT45DB642\n");
wdenk2abbe072003-06-16 23:50:08 +0000214 break;
wdenk5779d8d2003-12-06 23:55:10 +0000215 case AT45DB128:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100216 printf("AT45DB128\n");
wdenk5779d8d2003-12-06 23:55:10 +0000217 break;
wdenk2abbe072003-06-16 23:50:08 +0000218 }
219
Peter Pearsed4fc6012007-08-14 10:10:52 +0100220 printf("Nb pages: %6d\n"
wdenk2abbe072003-06-16 23:50:08 +0000221 "Page Size: %6d\n"
222 "Size=%8d bytes\n"
223 "Logical address: 0x%08X\n",
224 (unsigned int) dataflash_info[i].Device.pages_number,
225 (unsigned int) dataflash_info[i].Device.pages_size,
226 (unsigned int) dataflash_info[i].Device.pages_number *
227 dataflash_info[i].Device.pages_size,
228 (unsigned int) dataflash_info[i].logical_address);
Ilko Iliev03f79772008-12-02 17:20:17 +0100229 for (j = 0; j < NB_DATAFLASH_AREA; j++) {
Peter Pearsed4fc6012007-08-14 10:10:52 +0100230 switch(dataflash_info[i].Device.area_list[j].protected) {
231 case FLAG_PROTECT_SET:
232 case FLAG_PROTECT_CLEAR:
233 printf("Area %i:\t%08lX to %08lX %s", j,
234 dataflash_info[i].Device.area_list[j].start,
235 dataflash_info[i].Device.area_list[j].end,
236 (dataflash_info[i].Device.area_list[j].protected==FLAG_PROTECT_SET) ? "(RO)" : " ");
Peter Pearsed4fc6012007-08-14 10:10:52 +0100237 printf(" %s\n", dataflash_info[i].Device.area_list[j].label);
Peter Pearsed4fc6012007-08-14 10:10:52 +0100238 break;
Peter Pearsed4fc6012007-08-14 10:10:52 +0100239 case FLAG_PROTECT_INVALID:
240 break;
Peter Pearsed4fc6012007-08-14 10:10:52 +0100241 }
wdenk5779d8d2003-12-06 23:55:10 +0000242 }
wdenk2abbe072003-06-16 23:50:08 +0000243 }
244 }
245}
246
Peter Pearsed4fc6012007-08-14 10:10:52 +0100247/*---------------------------------------------------------------------------*/
Stelian Pop5604e212008-03-26 21:52:36 +0100248/* Function Name : AT91F_DataflashSelect */
Peter Pearsed4fc6012007-08-14 10:10:52 +0100249/* Object : Select the correct device */
250/*---------------------------------------------------------------------------*/
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200251AT91PS_DataFlash AT91F_DataflashSelect (AT91PS_DataFlash pFlash,
Peter Pearsed4fc6012007-08-14 10:10:52 +0100252 unsigned long *addr)
wdenk2abbe072003-06-16 23:50:08 +0000253{
254 char addr_valid = 0;
255 int i;
256
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200257 for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100258 if ( dataflash_info[i].id
Stelian Popc77ce472008-01-14 22:08:14 +0100259 && ((((int) *addr) & 0xFF000000) ==
Peter Pearsed4fc6012007-08-14 10:10:52 +0100260 dataflash_info[i].logical_address)) {
wdenk2abbe072003-06-16 23:50:08 +0000261 addr_valid = 1;
262 break;
263 }
264 if (!addr_valid) {
265 pFlash = (AT91PS_DataFlash) 0;
266 return pFlash;
267 }
268 pFlash->pDataFlashDesc = &(dataflash_info[i].Desc);
269 pFlash->pDevice = &(dataflash_info[i].Device);
270 *addr -= dataflash_info[i].logical_address;
271 return (pFlash);
272}
273
Peter Pearsed4fc6012007-08-14 10:10:52 +0100274/*---------------------------------------------------------------------------*/
Stelian Pop5604e212008-03-26 21:52:36 +0100275/* Function Name : addr_dataflash */
Peter Pearsed4fc6012007-08-14 10:10:52 +0100276/* Object : Test if address is valid */
277/*---------------------------------------------------------------------------*/
wdenk2abbe072003-06-16 23:50:08 +0000278int addr_dataflash (unsigned long addr)
279{
280 int addr_valid = 0;
281 int i;
282
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200283 for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
wdenk2abbe072003-06-16 23:50:08 +0000284 if ((((int) addr) & 0xFF000000) ==
285 dataflash_info[i].logical_address) {
286 addr_valid = 1;
287 break;
288 }
289 }
290
291 return addr_valid;
292}
Stelian Pop5604e212008-03-26 21:52:36 +0100293
Peter Pearsed4fc6012007-08-14 10:10:52 +0100294/*---------------------------------------------------------------------------*/
Stelian Pop5604e212008-03-26 21:52:36 +0100295/* Function Name : size_dataflash */
Peter Pearsed4fc6012007-08-14 10:10:52 +0100296/* Object : Test if address is valid regarding the size */
297/*---------------------------------------------------------------------------*/
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200298int size_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr,
Peter Pearsed4fc6012007-08-14 10:10:52 +0100299 unsigned long size)
wdenk5779d8d2003-12-06 23:55:10 +0000300{
301 /* is outside the dataflash */
302 if (((int)addr & 0x0FFFFFFF) > (pdataFlash->pDevice->pages_size *
303 pdataFlash->pDevice->pages_number)) return 0;
304 /* is too large for the dataflash */
305 if (size > ((pdataFlash->pDevice->pages_size *
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200306 pdataFlash->pDevice->pages_number) -
Peter Pearsed4fc6012007-08-14 10:10:52 +0100307 ((int)addr & 0x0FFFFFFF))) return 0;
wdenk5779d8d2003-12-06 23:55:10 +0000308
309 return 1;
310}
Stelian Pop5604e212008-03-26 21:52:36 +0100311
Peter Pearsed4fc6012007-08-14 10:10:52 +0100312/*---------------------------------------------------------------------------*/
Stelian Pop5604e212008-03-26 21:52:36 +0100313/* Function Name : prot_dataflash */
Peter Pearsed4fc6012007-08-14 10:10:52 +0100314/* Object : Test if destination area is protected */
315/*---------------------------------------------------------------------------*/
wdenk5779d8d2003-12-06 23:55:10 +0000316int prot_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr)
317{
Stelian Pop5604e212008-03-26 21:52:36 +0100318 int area;
319
wdenk5779d8d2003-12-06 23:55:10 +0000320 /* find area */
Ilko Iliev03f79772008-12-02 17:20:17 +0100321 for (area = 0; area < NB_DATAFLASH_AREA; area++) {
wdenk5779d8d2003-12-06 23:55:10 +0000322 if ((addr >= pdataFlash->pDevice->area_list[area].start) &&
323 (addr < pdataFlash->pDevice->area_list[area].end))
324 break;
325 }
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200326 if (area == NB_DATAFLASH_AREA)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100327 return -1;
328
wdenk5779d8d2003-12-06 23:55:10 +0000329 /*test protection value*/
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200330 if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_SET)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100331 return 0;
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200332 if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_INVALID)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100333 return 0;
wdenk5779d8d2003-12-06 23:55:10 +0000334
335 return 1;
336}
Stelian Pop5604e212008-03-26 21:52:36 +0100337
Peter Pearsed4fc6012007-08-14 10:10:52 +0100338/*--------------------------------------------------------------------------*/
339/* Function Name : dataflash_real_protect */
340/* Object : protect/unprotect area */
341/*--------------------------------------------------------------------------*/
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200342int dataflash_real_protect (int flag, unsigned long start_addr,
Peter Pearsed4fc6012007-08-14 10:10:52 +0100343 unsigned long end_addr)
wdenk5779d8d2003-12-06 23:55:10 +0000344{
Stelian Pop5604e212008-03-26 21:52:36 +0100345 int i,j, area1, area2, addr_valid = 0;
346
wdenk5779d8d2003-12-06 23:55:10 +0000347 /* find dataflash */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200348 for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
wdenk5779d8d2003-12-06 23:55:10 +0000349 if ((((int) start_addr) & 0xF0000000) ==
350 dataflash_info[i].logical_address) {
351 addr_valid = 1;
352 break;
353 }
354 }
355 if (!addr_valid) {
356 return -1;
357 }
358 /* find start area */
Ilko Iliev03f79772008-12-02 17:20:17 +0100359 for (area1 = 0; area1 < NB_DATAFLASH_AREA; area1++) {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200360 if (start_addr == dataflash_info[i].Device.area_list[area1].start)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100361 break;
wdenk5779d8d2003-12-06 23:55:10 +0000362 }
363 if (area1 == NB_DATAFLASH_AREA) return -1;
364 /* find end area */
Ilko Iliev03f79772008-12-02 17:20:17 +0100365 for (area2 = 0; area2 < NB_DATAFLASH_AREA; area2++) {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200366 if (end_addr == dataflash_info[i].Device.area_list[area2].end)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100367 break;
wdenk5779d8d2003-12-06 23:55:10 +0000368 }
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200369 if (area2 == NB_DATAFLASH_AREA)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100370 return -1;
wdenk5779d8d2003-12-06 23:55:10 +0000371
372 /*set protection value*/
Ilko Iliev03f79772008-12-02 17:20:17 +0100373 for(j = area1; j < area2 + 1 ; j++)
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200374 if(dataflash_info[i].Device.area_list[j].protected
Peter Pearsed4fc6012007-08-14 10:10:52 +0100375 != FLAG_PROTECT_INVALID) {
376 if (flag == 0) {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200377 dataflash_info[i].Device.area_list[j].protected
Peter Pearsed4fc6012007-08-14 10:10:52 +0100378 = FLAG_PROTECT_CLEAR;
379 } else {
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_SET;
382 }
383 }
wdenk5779d8d2003-12-06 23:55:10 +0000384
Ilko Iliev03f79772008-12-02 17:20:17 +0100385 return (area2 - area1 + 1);
wdenk5779d8d2003-12-06 23:55:10 +0000386}
wdenk2abbe072003-06-16 23:50:08 +0000387
Peter Pearsed4fc6012007-08-14 10:10:52 +0100388/*---------------------------------------------------------------------------*/
Stelian Pop5604e212008-03-26 21:52:36 +0100389/* Function Name : read_dataflash */
Peter Pearsed4fc6012007-08-14 10:10:52 +0100390/* Object : dataflash memory read */
391/*---------------------------------------------------------------------------*/
wdenk2abbe072003-06-16 23:50:08 +0000392int read_dataflash (unsigned long addr, unsigned long size, char *result)
393{
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100394 unsigned long AddrToRead = addr;
wdenk2abbe072003-06-16 23:50:08 +0000395 AT91PS_DataFlash pFlash = &DataFlashInst;
396
397 pFlash = AT91F_DataflashSelect (pFlash, &AddrToRead);
wdenk5779d8d2003-12-06 23:55:10 +0000398
wdenk2abbe072003-06-16 23:50:08 +0000399 if (pFlash == 0)
wdenk5779d8d2003-12-06 23:55:10 +0000400 return ERR_UNKNOWN_FLASH_TYPE;
401
402 if (size_dataflash(pFlash,addr,size) == 0)
403 return ERR_INVAL;
wdenk2abbe072003-06-16 23:50:08 +0000404
405 return (AT91F_DataFlashRead (pFlash, AddrToRead, size, result));
406}
407
Peter Pearsed4fc6012007-08-14 10:10:52 +0100408/*---------------------------------------------------------------------------*/
Stelian Pop5604e212008-03-26 21:52:36 +0100409/* Function Name : write_dataflash */
Peter Pearsed4fc6012007-08-14 10:10:52 +0100410/* Object : write a block in dataflash */
411/*---------------------------------------------------------------------------*/
wdenk2abbe072003-06-16 23:50:08 +0000412int write_dataflash (unsigned long addr_dest, unsigned long addr_src,
Peter Pearsed4fc6012007-08-14 10:10:52 +0100413 unsigned long size)
wdenk2abbe072003-06-16 23:50:08 +0000414{
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100415 unsigned long AddrToWrite = addr_dest;
wdenk2abbe072003-06-16 23:50:08 +0000416 AT91PS_DataFlash pFlash = &DataFlashInst;
417
418 pFlash = AT91F_DataflashSelect (pFlash, &AddrToWrite);
wdenk5779d8d2003-12-06 23:55:10 +0000419
420 if (pFlash == 0)
421 return ERR_UNKNOWN_FLASH_TYPE;
422
423 if (size_dataflash(pFlash,addr_dest,size) == 0)
424 return ERR_INVAL;
425
426 if (prot_dataflash(pFlash,addr_dest) == 0)
427 return ERR_PROTECTED;
428
wdenk2abbe072003-06-16 23:50:08 +0000429 if (AddrToWrite == -1)
430 return -1;
431
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200432 return AT91F_DataFlashWrite (pFlash, (uchar *)addr_src,
Peter Pearsed4fc6012007-08-14 10:10:52 +0100433 AddrToWrite, size);
wdenk2abbe072003-06-16 23:50:08 +0000434}
435
wdenk2abbe072003-06-16 23:50:08 +0000436void dataflash_perror (int err)
437{
438 switch (err) {
439 case ERR_OK:
440 break;
441 case ERR_TIMOUT:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100442 printf("Timeout writing to DataFlash\n");
wdenk2abbe072003-06-16 23:50:08 +0000443 break;
444 case ERR_PROTECTED:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100445 printf("Can't write to protected/invalid DataFlash sectors\n");
wdenk2abbe072003-06-16 23:50:08 +0000446 break;
447 case ERR_INVAL:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100448 printf("Outside available DataFlash\n");
wdenk2abbe072003-06-16 23:50:08 +0000449 break;
450 case ERR_UNKNOWN_FLASH_TYPE:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100451 printf("Unknown Type of DataFlash\n");
wdenk2abbe072003-06-16 23:50:08 +0000452 break;
453 case ERR_PROG_ERROR:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100454 printf("General DataFlash Programming Error\n");
wdenk2abbe072003-06-16 23:50:08 +0000455 break;
456 default:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100457 printf("%s[%d] FIXME: rc=%d\n", __FILE__, __LINE__, err);
wdenk2abbe072003-06-16 23:50:08 +0000458 break;
459 }
460}