blob: 3fb6ed6df7cdcc6e65f34105756c765828be98ea [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;
52 found[i] += dfcode;;
53 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;
64 found[i] += dfcode;;
65 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;
Peter Pearsed4fc6012007-08-14 10:10:52 +010076 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;
Peter Pearsed4fc6012007-08-14 10:10:52 +010088 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;
Peter Pearsed4fc6012007-08-14 10:10:52 +0100100 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;
Peter Pearsed4fc6012007-08-14 10:10:52 +0100112 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
Ulf Samuelssonc3a60cb2008-04-12 20:29:44 +0200158void AT91F_DataflashSetEnv (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;
Jean-Christophe PLAGNIOL-VILLARD0a5676b2008-07-12 14:36:34 +0200172 sprintf((char*) s,"%lX",start);
Ulf Samuelssonc3a60cb2008-04-12 20:29:44 +0200173 setenv((char*) area_list[part].label,(char*) s);
Peter Pearsed4fc6012007-08-14 10:10:52 +0100174 }
175 part++;
176 }
177 }
178}
wdenk2abbe072003-06-16 23:50:08 +0000179
wdenk2abbe072003-06-16 23:50:08 +0000180void dataflash_print_info (void)
181{
wdenk5779d8d2003-12-06 23:55:10 +0000182 int i, j;
wdenk2abbe072003-06-16 23:50:08 +0000183
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200184 for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
wdenk2abbe072003-06-16 23:50:08 +0000185 if (dataflash_info[i].id != 0) {
Peter Pearsed4fc6012007-08-14 10:10:52 +0100186 printf("DataFlash:");
wdenk2abbe072003-06-16 23:50:08 +0000187 switch (dataflash_info[i].id) {
Sergey Lapinc6457e32008-06-05 11:06:29 +0400188 case AT45DB021:
189 printf("AT45DB021\n");
190 break;
wdenk2abbe072003-06-16 23:50:08 +0000191 case AT45DB161:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100192 printf("AT45DB161\n");
wdenk2abbe072003-06-16 23:50:08 +0000193 break;
194
195 case AT45DB321:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100196 printf("AT45DB321\n");
wdenk2abbe072003-06-16 23:50:08 +0000197 break;
198
199 case AT45DB642:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100200 printf("AT45DB642\n");
wdenk2abbe072003-06-16 23:50:08 +0000201 break;
wdenk5779d8d2003-12-06 23:55:10 +0000202 case AT45DB128:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100203 printf("AT45DB128\n");
wdenk5779d8d2003-12-06 23:55:10 +0000204 break;
wdenk2abbe072003-06-16 23:50:08 +0000205 }
206
Peter Pearsed4fc6012007-08-14 10:10:52 +0100207 printf("Nb pages: %6d\n"
wdenk2abbe072003-06-16 23:50:08 +0000208 "Page Size: %6d\n"
209 "Size=%8d bytes\n"
210 "Logical address: 0x%08X\n",
211 (unsigned int) dataflash_info[i].Device.pages_number,
212 (unsigned int) dataflash_info[i].Device.pages_size,
213 (unsigned int) dataflash_info[i].Device.pages_number *
214 dataflash_info[i].Device.pages_size,
215 (unsigned int) dataflash_info[i].logical_address);
Ilko Iliev03f79772008-12-02 17:20:17 +0100216 for (j = 0; j < NB_DATAFLASH_AREA; j++) {
Peter Pearsed4fc6012007-08-14 10:10:52 +0100217 switch(dataflash_info[i].Device.area_list[j].protected) {
218 case FLAG_PROTECT_SET:
219 case FLAG_PROTECT_CLEAR:
220 printf("Area %i:\t%08lX to %08lX %s", j,
221 dataflash_info[i].Device.area_list[j].start,
222 dataflash_info[i].Device.area_list[j].end,
223 (dataflash_info[i].Device.area_list[j].protected==FLAG_PROTECT_SET) ? "(RO)" : " ");
Peter Pearsed4fc6012007-08-14 10:10:52 +0100224 printf(" %s\n", dataflash_info[i].Device.area_list[j].label);
Peter Pearsed4fc6012007-08-14 10:10:52 +0100225 break;
Peter Pearsed4fc6012007-08-14 10:10:52 +0100226 case FLAG_PROTECT_INVALID:
227 break;
Peter Pearsed4fc6012007-08-14 10:10:52 +0100228 }
wdenk5779d8d2003-12-06 23:55:10 +0000229 }
wdenk2abbe072003-06-16 23:50:08 +0000230 }
231 }
232}
233
Peter Pearsed4fc6012007-08-14 10:10:52 +0100234/*---------------------------------------------------------------------------*/
Stelian Pop5604e212008-03-26 21:52:36 +0100235/* Function Name : AT91F_DataflashSelect */
Peter Pearsed4fc6012007-08-14 10:10:52 +0100236/* Object : Select the correct device */
237/*---------------------------------------------------------------------------*/
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200238AT91PS_DataFlash AT91F_DataflashSelect (AT91PS_DataFlash pFlash,
Peter Pearsed4fc6012007-08-14 10:10:52 +0100239 unsigned long *addr)
wdenk2abbe072003-06-16 23:50:08 +0000240{
241 char addr_valid = 0;
242 int i;
243
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200244 for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100245 if ( dataflash_info[i].id
Stelian Popc77ce472008-01-14 22:08:14 +0100246 && ((((int) *addr) & 0xFF000000) ==
Peter Pearsed4fc6012007-08-14 10:10:52 +0100247 dataflash_info[i].logical_address)) {
wdenk2abbe072003-06-16 23:50:08 +0000248 addr_valid = 1;
249 break;
250 }
251 if (!addr_valid) {
252 pFlash = (AT91PS_DataFlash) 0;
253 return pFlash;
254 }
255 pFlash->pDataFlashDesc = &(dataflash_info[i].Desc);
256 pFlash->pDevice = &(dataflash_info[i].Device);
257 *addr -= dataflash_info[i].logical_address;
258 return (pFlash);
259}
260
Peter Pearsed4fc6012007-08-14 10:10:52 +0100261/*---------------------------------------------------------------------------*/
Stelian Pop5604e212008-03-26 21:52:36 +0100262/* Function Name : addr_dataflash */
Peter Pearsed4fc6012007-08-14 10:10:52 +0100263/* Object : Test if address is valid */
264/*---------------------------------------------------------------------------*/
wdenk2abbe072003-06-16 23:50:08 +0000265int addr_dataflash (unsigned long addr)
266{
267 int addr_valid = 0;
268 int i;
269
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200270 for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
wdenk2abbe072003-06-16 23:50:08 +0000271 if ((((int) addr) & 0xFF000000) ==
272 dataflash_info[i].logical_address) {
273 addr_valid = 1;
274 break;
275 }
276 }
277
278 return addr_valid;
279}
Stelian Pop5604e212008-03-26 21:52:36 +0100280
Peter Pearsed4fc6012007-08-14 10:10:52 +0100281/*---------------------------------------------------------------------------*/
Stelian Pop5604e212008-03-26 21:52:36 +0100282/* Function Name : size_dataflash */
Peter Pearsed4fc6012007-08-14 10:10:52 +0100283/* Object : Test if address is valid regarding the size */
284/*---------------------------------------------------------------------------*/
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200285int size_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr,
Peter Pearsed4fc6012007-08-14 10:10:52 +0100286 unsigned long size)
wdenk5779d8d2003-12-06 23:55:10 +0000287{
288 /* is outside the dataflash */
289 if (((int)addr & 0x0FFFFFFF) > (pdataFlash->pDevice->pages_size *
290 pdataFlash->pDevice->pages_number)) return 0;
291 /* is too large for the dataflash */
292 if (size > ((pdataFlash->pDevice->pages_size *
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200293 pdataFlash->pDevice->pages_number) -
Peter Pearsed4fc6012007-08-14 10:10:52 +0100294 ((int)addr & 0x0FFFFFFF))) return 0;
wdenk5779d8d2003-12-06 23:55:10 +0000295
296 return 1;
297}
Stelian Pop5604e212008-03-26 21:52:36 +0100298
Peter Pearsed4fc6012007-08-14 10:10:52 +0100299/*---------------------------------------------------------------------------*/
Stelian Pop5604e212008-03-26 21:52:36 +0100300/* Function Name : prot_dataflash */
Peter Pearsed4fc6012007-08-14 10:10:52 +0100301/* Object : Test if destination area is protected */
302/*---------------------------------------------------------------------------*/
wdenk5779d8d2003-12-06 23:55:10 +0000303int prot_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr)
304{
Stelian Pop5604e212008-03-26 21:52:36 +0100305 int area;
306
wdenk5779d8d2003-12-06 23:55:10 +0000307 /* find area */
Ilko Iliev03f79772008-12-02 17:20:17 +0100308 for (area = 0; area < NB_DATAFLASH_AREA; area++) {
wdenk5779d8d2003-12-06 23:55:10 +0000309 if ((addr >= pdataFlash->pDevice->area_list[area].start) &&
310 (addr < pdataFlash->pDevice->area_list[area].end))
311 break;
312 }
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200313 if (area == NB_DATAFLASH_AREA)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100314 return -1;
315
wdenk5779d8d2003-12-06 23:55:10 +0000316 /*test protection value*/
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200317 if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_SET)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100318 return 0;
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200319 if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_INVALID)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100320 return 0;
wdenk5779d8d2003-12-06 23:55:10 +0000321
322 return 1;
323}
Stelian Pop5604e212008-03-26 21:52:36 +0100324
Peter Pearsed4fc6012007-08-14 10:10:52 +0100325/*--------------------------------------------------------------------------*/
326/* Function Name : dataflash_real_protect */
327/* Object : protect/unprotect area */
328/*--------------------------------------------------------------------------*/
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200329int dataflash_real_protect (int flag, unsigned long start_addr,
Peter Pearsed4fc6012007-08-14 10:10:52 +0100330 unsigned long end_addr)
wdenk5779d8d2003-12-06 23:55:10 +0000331{
Stelian Pop5604e212008-03-26 21:52:36 +0100332 int i,j, area1, area2, addr_valid = 0;
333
wdenk5779d8d2003-12-06 23:55:10 +0000334 /* find dataflash */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200335 for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
wdenk5779d8d2003-12-06 23:55:10 +0000336 if ((((int) start_addr) & 0xF0000000) ==
337 dataflash_info[i].logical_address) {
338 addr_valid = 1;
339 break;
340 }
341 }
342 if (!addr_valid) {
343 return -1;
344 }
345 /* find start area */
Ilko Iliev03f79772008-12-02 17:20:17 +0100346 for (area1 = 0; area1 < NB_DATAFLASH_AREA; area1++) {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200347 if (start_addr == dataflash_info[i].Device.area_list[area1].start)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100348 break;
wdenk5779d8d2003-12-06 23:55:10 +0000349 }
350 if (area1 == NB_DATAFLASH_AREA) return -1;
351 /* find end area */
Ilko Iliev03f79772008-12-02 17:20:17 +0100352 for (area2 = 0; area2 < NB_DATAFLASH_AREA; area2++) {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200353 if (end_addr == dataflash_info[i].Device.area_list[area2].end)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100354 break;
wdenk5779d8d2003-12-06 23:55:10 +0000355 }
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200356 if (area2 == NB_DATAFLASH_AREA)
Peter Pearsed4fc6012007-08-14 10:10:52 +0100357 return -1;
wdenk5779d8d2003-12-06 23:55:10 +0000358
359 /*set protection value*/
Ilko Iliev03f79772008-12-02 17:20:17 +0100360 for(j = area1; j < area2 + 1 ; j++)
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200361 if(dataflash_info[i].Device.area_list[j].protected
Peter Pearsed4fc6012007-08-14 10:10:52 +0100362 != FLAG_PROTECT_INVALID) {
363 if (flag == 0) {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200364 dataflash_info[i].Device.area_list[j].protected
Peter Pearsed4fc6012007-08-14 10:10:52 +0100365 = FLAG_PROTECT_CLEAR;
366 } else {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200367 dataflash_info[i].Device.area_list[j].protected
Peter Pearsed4fc6012007-08-14 10:10:52 +0100368 = FLAG_PROTECT_SET;
369 }
370 }
wdenk5779d8d2003-12-06 23:55:10 +0000371
Ilko Iliev03f79772008-12-02 17:20:17 +0100372 return (area2 - area1 + 1);
wdenk5779d8d2003-12-06 23:55:10 +0000373}
wdenk2abbe072003-06-16 23:50:08 +0000374
Peter Pearsed4fc6012007-08-14 10:10:52 +0100375/*---------------------------------------------------------------------------*/
Stelian Pop5604e212008-03-26 21:52:36 +0100376/* Function Name : read_dataflash */
Peter Pearsed4fc6012007-08-14 10:10:52 +0100377/* Object : dataflash memory read */
378/*---------------------------------------------------------------------------*/
wdenk2abbe072003-06-16 23:50:08 +0000379int read_dataflash (unsigned long addr, unsigned long size, char *result)
380{
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100381 unsigned long AddrToRead = addr;
wdenk2abbe072003-06-16 23:50:08 +0000382 AT91PS_DataFlash pFlash = &DataFlashInst;
383
384 pFlash = AT91F_DataflashSelect (pFlash, &AddrToRead);
wdenk5779d8d2003-12-06 23:55:10 +0000385
wdenk2abbe072003-06-16 23:50:08 +0000386 if (pFlash == 0)
wdenk5779d8d2003-12-06 23:55:10 +0000387 return ERR_UNKNOWN_FLASH_TYPE;
388
389 if (size_dataflash(pFlash,addr,size) == 0)
390 return ERR_INVAL;
wdenk2abbe072003-06-16 23:50:08 +0000391
392 return (AT91F_DataFlashRead (pFlash, AddrToRead, size, result));
393}
394
Peter Pearsed4fc6012007-08-14 10:10:52 +0100395/*---------------------------------------------------------------------------*/
Stelian Pop5604e212008-03-26 21:52:36 +0100396/* Function Name : write_dataflash */
Peter Pearsed4fc6012007-08-14 10:10:52 +0100397/* Object : write a block in dataflash */
398/*---------------------------------------------------------------------------*/
wdenk2abbe072003-06-16 23:50:08 +0000399int write_dataflash (unsigned long addr_dest, unsigned long addr_src,
Peter Pearsed4fc6012007-08-14 10:10:52 +0100400 unsigned long size)
wdenk2abbe072003-06-16 23:50:08 +0000401{
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100402 unsigned long AddrToWrite = addr_dest;
wdenk2abbe072003-06-16 23:50:08 +0000403 AT91PS_DataFlash pFlash = &DataFlashInst;
404
405 pFlash = AT91F_DataflashSelect (pFlash, &AddrToWrite);
wdenk5779d8d2003-12-06 23:55:10 +0000406
407 if (pFlash == 0)
408 return ERR_UNKNOWN_FLASH_TYPE;
409
410 if (size_dataflash(pFlash,addr_dest,size) == 0)
411 return ERR_INVAL;
412
413 if (prot_dataflash(pFlash,addr_dest) == 0)
414 return ERR_PROTECTED;
415
wdenk2abbe072003-06-16 23:50:08 +0000416 if (AddrToWrite == -1)
417 return -1;
418
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200419 return AT91F_DataFlashWrite (pFlash, (uchar *)addr_src,
Peter Pearsed4fc6012007-08-14 10:10:52 +0100420 AddrToWrite, size);
wdenk2abbe072003-06-16 23:50:08 +0000421}
422
wdenk2abbe072003-06-16 23:50:08 +0000423void dataflash_perror (int err)
424{
425 switch (err) {
426 case ERR_OK:
427 break;
428 case ERR_TIMOUT:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100429 printf("Timeout writing to DataFlash\n");
wdenk2abbe072003-06-16 23:50:08 +0000430 break;
431 case ERR_PROTECTED:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100432 printf("Can't write to protected/invalid DataFlash sectors\n");
wdenk2abbe072003-06-16 23:50:08 +0000433 break;
434 case ERR_INVAL:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100435 printf("Outside available DataFlash\n");
wdenk2abbe072003-06-16 23:50:08 +0000436 break;
437 case ERR_UNKNOWN_FLASH_TYPE:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100438 printf("Unknown Type of DataFlash\n");
wdenk2abbe072003-06-16 23:50:08 +0000439 break;
440 case ERR_PROG_ERROR:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100441 printf("General DataFlash Programming Error\n");
wdenk2abbe072003-06-16 23:50:08 +0000442 break;
443 default:
Peter Pearsed4fc6012007-08-14 10:10:52 +0100444 printf("%s[%d] FIXME: rc=%d\n", __FILE__, __LINE__, err);
wdenk2abbe072003-06-16 23:50:08 +0000445 break;
446 }
447}