]> git.ipfire.org Git - people/ms/u-boot.git/blame - drivers/mtd/dataflash.c
Merge branch 'master' of ../work into next
[people/ms/u-boot.git] / drivers / mtd / dataflash.c
CommitLineData
2abbe075
WD
1/* 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>
2abbe075
WD
22#include <asm/hardware.h>
23#include <dataflash.h>
24
2abbe075
WD
25static AT91S_DataFlash DataFlashInst;
26
2abbe075
WD
27extern 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);
5779d8d9 32extern int AT91F_DataFlashWrite( AT91PS_DataFlash pDataFlash,
d4fc6012
PP
33 unsigned char *src,
34 int dest,
35 int size );
2abbe075
WD
36
37int AT91F_DataflashInit (void)
38{
39 int i, j;
40 int dfcode;
5604e217 41 int part;
d4fc6012 42 int last_part;
6d0f6bcf 43 int found[CONFIG_SYS_MAX_DATAFLASH_BANKS];
d4fc6012 44 unsigned char protected;
2abbe075
WD
45
46 AT91F_SpiInit ();
47
6d0f6bcf 48 for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
d4fc6012 49 found[i] = 0;
5779d8d9 50 dataflash_info[i].Desc.state = IDLE;
2abbe075
WD
51 dataflash_info[i].id = 0;
52 dataflash_info[i].Device.pages_number = 0;
d49fe4be 53 dfcode = AT91F_DataflashProbe (cs[i].cs,
d4fc6012 54 &dataflash_info[i].Desc);
2abbe075
WD
55
56 switch (dfcode) {
c6457e3b
SL
57 case AT45DB021:
58 dataflash_info[i].Device.pages_number = 1024;
f77d92a3 59 dataflash_info[i].Device.pages_size = 264;
c6457e3b
SL
60 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;
f77d92a3
SL
68
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
2abbe075
WD
81 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;
d49fe4be 86 dataflash_info[i].Device.cs = cs[i].cs;
2abbe075 87 dataflash_info[i].Desc.DataFlash_state = IDLE;
d49fe4be 88 dataflash_info[i].logical_address = cs[i].addr;
2abbe075 89 dataflash_info[i].id = dfcode;
d4fc6012 90 found[i] += dfcode;;
2abbe075
WD
91 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;
d49fe4be 98 dataflash_info[i].Device.cs = cs[i].cs;
2abbe075 99 dataflash_info[i].Desc.DataFlash_state = IDLE;
d49fe4be 100 dataflash_info[i].logical_address = cs[i].addr;
2abbe075 101 dataflash_info[i].id = dfcode;
d4fc6012 102 found[i] += dfcode;;
2abbe075
WD
103 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;
d49fe4be 110 dataflash_info[i].Device.cs = cs[i].cs;
2abbe075 111 dataflash_info[i].Desc.DataFlash_state = IDLE;
d49fe4be 112 dataflash_info[i].logical_address = cs[i].addr;
2abbe075 113 dataflash_info[i].id = dfcode;
d4fc6012 114 found[i] += dfcode;;
2abbe075 115 break;
d4fc6012 116
5779d8d9
WD
117 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;
d49fe4be 122 dataflash_info[i].Device.cs = cs[i].cs;
5779d8d9 123 dataflash_info[i].Desc.DataFlash_state = IDLE;
d49fe4be 124 dataflash_info[i].logical_address = cs[i].addr;
5779d8d9 125 dataflash_info[i].id = dfcode;
d4fc6012 126 found[i] += dfcode;;
5779d8d9 127 break;
2abbe075
WD
128
129 default:
d4fc6012 130 dfcode = 0;
2abbe075
WD
131 break;
132 }
5779d8d9 133 /* set the last area end to the dataflash size*/
bd876772 134 dataflash_info[i].end_address =
5779d8d9 135 (dataflash_info[i].Device.pages_number *
03f79779 136 dataflash_info[i].Device.pages_size) - 1;
5779d8d9 137
5604e217
SP
138 part = 0;
139 last_part = 0;
5779d8d9 140 /* set the area addresses */
03f79779 141 for(j = 0; j < NB_DATAFLASH_AREA; j++) {
d4fc6012 142 if(found[i]!=0) {
f01dbb54
WD
143 dataflash_info[i].Device.area_list[j].start =
144 area_list[part].start +
d4fc6012
PP
145 dataflash_info[i].logical_address;
146 if(area_list[part].end == 0xffffffff) {
f01dbb54
WD
147 dataflash_info[i].Device.area_list[j].end =
148 dataflash_info[i].end_address +
03f79779 149 dataflash_info[i].logical_address;
d4fc6012
PP
150 last_part = 1;
151 } else {
f01dbb54
WD
152 dataflash_info[i].Device.area_list[j].end =
153 area_list[part].end +
d4fc6012
PP
154 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) {
f01dbb54 159 dataflash_info[i].Device.area_list[j].protected =
d4fc6012
PP
160 FLAG_PROTECT_INVALID;
161 } else {
f01dbb54 162 dataflash_info[i].Device.area_list[j].protected =
d4fc6012
PP
163 protected;
164 }
165 strcpy((char*)(dataflash_info[i].Device.area_list[j].label),
166 (const char *)area_list[part].label);
167 }
168 part++;
5779d8d9 169 }
2abbe075 170 }
d4fc6012 171 return found[0];
2abbe075
WD
172}
173
c3a60cb3 174void AT91F_DataflashSetEnv (void)
d4fc6012
PP
175{
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;
5604e217 181
6d0f6bcf 182 for (i = 0, part= 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
03f79779 183 for(j = 0; j < NB_DATAFLASH_AREA; j++) {
d4fc6012
PP
184 env = area_list[part].setenv;
185 /* Set the environment according to the label...*/
186 if((env & FLAG_SETENV) == FLAG_SETENV) {
03f79779 187 start = dataflash_info[i].Device.area_list[j].start;
0a5676be 188 sprintf((char*) s,"%lX",start);
c3a60cb3 189 setenv((char*) area_list[part].label,(char*) s);
d4fc6012
PP
190 }
191 part++;
192 }
193 }
194}
2abbe075 195
2abbe075
WD
196void dataflash_print_info (void)
197{
5779d8d9 198 int i, j;
2abbe075 199
6d0f6bcf 200 for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
2abbe075 201 if (dataflash_info[i].id != 0) {
d4fc6012 202 printf("DataFlash:");
2abbe075 203 switch (dataflash_info[i].id) {
c6457e3b
SL
204 case AT45DB021:
205 printf("AT45DB021\n");
206 break;
2abbe075 207 case AT45DB161:
d4fc6012 208 printf("AT45DB161\n");
2abbe075
WD
209 break;
210
211 case AT45DB321:
d4fc6012 212 printf("AT45DB321\n");
2abbe075
WD
213 break;
214
215 case AT45DB642:
d4fc6012 216 printf("AT45DB642\n");
2abbe075 217 break;
5779d8d9 218 case AT45DB128:
d4fc6012 219 printf("AT45DB128\n");
5779d8d9 220 break;
2abbe075
WD
221 }
222
d4fc6012 223 printf("Nb pages: %6d\n"
2abbe075
WD
224 "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);
03f79779 232 for (j = 0; j < NB_DATAFLASH_AREA; j++) {
d4fc6012
PP
233 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)" : " ");
d4fc6012 240 printf(" %s\n", dataflash_info[i].Device.area_list[j].label);
d4fc6012 241 break;
d4fc6012
PP
242 case FLAG_PROTECT_INVALID:
243 break;
d4fc6012 244 }
5779d8d9 245 }
2abbe075
WD
246 }
247 }
248}
249
d4fc6012 250/*---------------------------------------------------------------------------*/
5604e217 251/* Function Name : AT91F_DataflashSelect */
d4fc6012
PP
252/* Object : Select the correct device */
253/*---------------------------------------------------------------------------*/
f01dbb54 254AT91PS_DataFlash AT91F_DataflashSelect (AT91PS_DataFlash pFlash,
d4fc6012 255 unsigned long *addr)
2abbe075
WD
256{
257 char addr_valid = 0;
258 int i;
259
6d0f6bcf 260 for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++)
d4fc6012 261 if ( dataflash_info[i].id
c77ce474 262 && ((((int) *addr) & 0xFF000000) ==
d4fc6012 263 dataflash_info[i].logical_address)) {
2abbe075
WD
264 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
d4fc6012 277/*---------------------------------------------------------------------------*/
5604e217 278/* Function Name : addr_dataflash */
d4fc6012
PP
279/* Object : Test if address is valid */
280/*---------------------------------------------------------------------------*/
2abbe075
WD
281int addr_dataflash (unsigned long addr)
282{
283 int addr_valid = 0;
284 int i;
285
6d0f6bcf 286 for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
2abbe075
WD
287 if ((((int) addr) & 0xFF000000) ==
288 dataflash_info[i].logical_address) {
289 addr_valid = 1;
290 break;
291 }
292 }
293
294 return addr_valid;
295}
5604e217 296
d4fc6012 297/*---------------------------------------------------------------------------*/
5604e217 298/* Function Name : size_dataflash */
d4fc6012
PP
299/* Object : Test if address is valid regarding the size */
300/*---------------------------------------------------------------------------*/
f01dbb54 301int size_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr,
d4fc6012 302 unsigned long size)
5779d8d9
WD
303{
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 *
f01dbb54 309 pdataFlash->pDevice->pages_number) -
d4fc6012 310 ((int)addr & 0x0FFFFFFF))) return 0;
5779d8d9
WD
311
312 return 1;
313}
5604e217 314
d4fc6012 315/*---------------------------------------------------------------------------*/
5604e217 316/* Function Name : prot_dataflash */
d4fc6012
PP
317/* Object : Test if destination area is protected */
318/*---------------------------------------------------------------------------*/
5779d8d9
WD
319int prot_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr)
320{
5604e217
SP
321 int area;
322
5779d8d9 323 /* find area */
03f79779 324 for (area = 0; area < NB_DATAFLASH_AREA; area++) {
5779d8d9
WD
325 if ((addr >= pdataFlash->pDevice->area_list[area].start) &&
326 (addr < pdataFlash->pDevice->area_list[area].end))
327 break;
328 }
f01dbb54 329 if (area == NB_DATAFLASH_AREA)
d4fc6012
PP
330 return -1;
331
5779d8d9 332 /*test protection value*/
f01dbb54 333 if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_SET)
d4fc6012 334 return 0;
f01dbb54 335 if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_INVALID)
d4fc6012 336 return 0;
5779d8d9
WD
337
338 return 1;
339}
5604e217 340
d4fc6012
PP
341/*--------------------------------------------------------------------------*/
342/* Function Name : dataflash_real_protect */
343/* Object : protect/unprotect area */
344/*--------------------------------------------------------------------------*/
f01dbb54 345int dataflash_real_protect (int flag, unsigned long start_addr,
d4fc6012 346 unsigned long end_addr)
5779d8d9 347{
5604e217
SP
348 int i,j, area1, area2, addr_valid = 0;
349
5779d8d9 350 /* find dataflash */
6d0f6bcf 351 for (i = 0; i < CONFIG_SYS_MAX_DATAFLASH_BANKS; i++) {
5779d8d9
WD
352 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 */
03f79779 362 for (area1 = 0; area1 < NB_DATAFLASH_AREA; area1++) {
f01dbb54 363 if (start_addr == dataflash_info[i].Device.area_list[area1].start)
d4fc6012 364 break;
5779d8d9
WD
365 }
366 if (area1 == NB_DATAFLASH_AREA) return -1;
367 /* find end area */
03f79779 368 for (area2 = 0; area2 < NB_DATAFLASH_AREA; area2++) {
f01dbb54 369 if (end_addr == dataflash_info[i].Device.area_list[area2].end)
d4fc6012 370 break;
5779d8d9 371 }
f01dbb54 372 if (area2 == NB_DATAFLASH_AREA)
d4fc6012 373 return -1;
5779d8d9
WD
374
375 /*set protection value*/
03f79779 376 for(j = area1; j < area2 + 1 ; j++)
f01dbb54 377 if(dataflash_info[i].Device.area_list[j].protected
d4fc6012
PP
378 != FLAG_PROTECT_INVALID) {
379 if (flag == 0) {
f01dbb54 380 dataflash_info[i].Device.area_list[j].protected
d4fc6012
PP
381 = FLAG_PROTECT_CLEAR;
382 } else {
f01dbb54 383 dataflash_info[i].Device.area_list[j].protected
d4fc6012
PP
384 = FLAG_PROTECT_SET;
385 }
386 }
5779d8d9 387
03f79779 388 return (area2 - area1 + 1);
5779d8d9 389}
2abbe075 390
d4fc6012 391/*---------------------------------------------------------------------------*/
5604e217 392/* Function Name : read_dataflash */
d4fc6012
PP
393/* Object : dataflash memory read */
394/*---------------------------------------------------------------------------*/
2abbe075
WD
395int read_dataflash (unsigned long addr, unsigned long size, char *result)
396{
d52fb7e3 397 unsigned long AddrToRead = addr;
2abbe075
WD
398 AT91PS_DataFlash pFlash = &DataFlashInst;
399
400 pFlash = AT91F_DataflashSelect (pFlash, &AddrToRead);
5779d8d9 401
2abbe075 402 if (pFlash == 0)
5779d8d9
WD
403 return ERR_UNKNOWN_FLASH_TYPE;
404
405 if (size_dataflash(pFlash,addr,size) == 0)
406 return ERR_INVAL;
2abbe075
WD
407
408 return (AT91F_DataFlashRead (pFlash, AddrToRead, size, result));
409}
410
d4fc6012 411/*---------------------------------------------------------------------------*/
5604e217 412/* Function Name : write_dataflash */
d4fc6012
PP
413/* Object : write a block in dataflash */
414/*---------------------------------------------------------------------------*/
2abbe075 415int write_dataflash (unsigned long addr_dest, unsigned long addr_src,
d4fc6012 416 unsigned long size)
2abbe075 417{
d52fb7e3 418 unsigned long AddrToWrite = addr_dest;
2abbe075
WD
419 AT91PS_DataFlash pFlash = &DataFlashInst;
420
421 pFlash = AT91F_DataflashSelect (pFlash, &AddrToWrite);
5779d8d9
WD
422
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
2abbe075
WD
432 if (AddrToWrite == -1)
433 return -1;
434
f01dbb54 435 return AT91F_DataFlashWrite (pFlash, (uchar *)addr_src,
d4fc6012 436 AddrToWrite, size);
2abbe075
WD
437}
438
2abbe075
WD
439void dataflash_perror (int err)
440{
441 switch (err) {
442 case ERR_OK:
443 break;
444 case ERR_TIMOUT:
d4fc6012 445 printf("Timeout writing to DataFlash\n");
2abbe075
WD
446 break;
447 case ERR_PROTECTED:
d4fc6012 448 printf("Can't write to protected/invalid DataFlash sectors\n");
2abbe075
WD
449 break;
450 case ERR_INVAL:
d4fc6012 451 printf("Outside available DataFlash\n");
2abbe075
WD
452 break;
453 case ERR_UNKNOWN_FLASH_TYPE:
d4fc6012 454 printf("Unknown Type of DataFlash\n");
2abbe075
WD
455 break;
456 case ERR_PROG_ERROR:
d4fc6012 457 printf("General DataFlash Programming Error\n");
2abbe075
WD
458 break;
459 default:
d4fc6012 460 printf("%s[%d] FIXME: rc=%d\n", __FILE__, __LINE__, err);
2abbe075
WD
461 break;
462 }
463}