ppc4xx: Mask 'vec' with 0x1f in uic_interrupt() for bit set/clear
[u-boot-openmoko.git] / drivers / mtd / dataflash.c
blob36c99a04995188e04968dcabc7589e75252596de
1 /* LowLevel function for ATMEL DataFlash support
2 * Author : Hamid Ikdoumi (Atmel)
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.
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.
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
20 #include <common.h>
21 #include <config.h>
22 #ifdef CONFIG_HAS_DATAFLASH
23 #include <asm/hardware.h>
24 #include <dataflash.h>
26 AT91S_DATAFLASH_INFO dataflash_info[CFG_MAX_DATAFLASH_BANKS];
27 static AT91S_DataFlash DataFlashInst;
29 struct dataflash_addr {
30 unsigned long addr;
31 int cs;
34 #ifdef CONFIG_AT91SAM9260EK
35 struct dataflash_addr cs[CFG_MAX_DATAFLASH_BANKS] = {
36 {CFG_DATAFLASH_LOGIC_ADDR_CS0, 0}, /* Logical adress, CS */
37 {CFG_DATAFLASH_LOGIC_ADDR_CS1, 1}
39 #elif defined(CONFIG_AT91SAM9263EK) || defined(CONFIG_AT91CAP9ADK)
40 struct dataflash_addr cs[CFG_MAX_DATAFLASH_BANKS] = {
41 {CFG_DATAFLASH_LOGIC_ADDR_CS0, 0}, /* Logical adress, CS */
43 #else
44 struct dataflash_addr cs[CFG_MAX_DATAFLASH_BANKS] = {
45 {CFG_DATAFLASH_LOGIC_ADDR_CS0, 0}, /* Logical adress, CS */
46 {CFG_DATAFLASH_LOGIC_ADDR_CS3, 3}
48 #endif
50 /*define the area offsets*/
51 #if defined(CONFIG_AT91SAM9261EK) || defined(CONFIG_AT91SAM9260EK) || \
52 defined(CONFIG_AT91SAM9263EK) || defined(CONFIG_AT91CAP9ADK)
53 #if defined(CONFIG_NEW_PARTITION)
54 dataflash_protect_t area_list[NB_DATAFLASH_AREA] = {
55 {0x00000000, 0x00003FFF, FLAG_PROTECT_SET, 0, "Bootstrap"}, /* ROM code */
56 {0x00004200, 0x000083FF, FLAG_PROTECT_CLEAR, 0, "Environment"}, /* u-boot environment */
57 {0x00008400, 0x0003DDFF, FLAG_PROTECT_SET, 0, "U-Boot"}, /* u-boot code */
58 {0x0003DE00, 0x00041FFF, FLAG_PROTECT_CLEAR, FLAG_SETENV, "MON"}, /* Room for alternative boot monitor */
59 {0x00042000, 0x0018BFFF, FLAG_PROTECT_CLEAR, FLAG_SETENV, "OS"}, /* data area size to tune */
60 {0x0018C000, 0xFFFFFFFF, FLAG_PROTECT_CLEAR, FLAG_SETENV, "FS"}, /* data area size to tune */
62 #else
63 dataflash_protect_t area_list[NB_DATAFLASH_AREA] = {
64 {0, 0x3fff, FLAG_PROTECT_SET}, /* ROM code */
65 {0x4000, 0x7fff, FLAG_PROTECT_CLEAR}, /* u-boot environment */
66 {0x8000, 0x37fff, FLAG_PROTECT_SET}, /* u-boot code */
67 {0x38000, 0x1fffff, FLAG_PROTECT_CLEAR}, /* data area size to tune */
69 #endif
70 #elif defined(CONFIG_NEW_PARTITION)
71 /*define the area offsets*/
72 /* Invalid partitions should be defined with start > end */
73 dataflash_protect_t area_list[NB_DATAFLASH_AREA*CFG_MAX_DATAFLASH_BANKS] = {
74 {0x00000000, 0x000083ff, FLAG_PROTECT_SET, 0, "Bootstrap"}, /* ROM code */
75 {0x00008400, 0x00020fff, FLAG_PROTECT_SET, 0, "U-Boot"}, /* u-boot code */
76 {0x00021000, 0x000293ff, FLAG_PROTECT_CLEAR, 0, "Environment"}, /* u-boot environment 8Kb */
77 {0x00029400, 0x00041fff, FLAG_PROTECT_INVALID, 0, "<Unused>"}, /* Rest of Sector 1 */
78 {0x00042000, 0x0018Bfff, FLAG_PROTECT_CLEAR, FLAG_SETENV, "OS"}, /* data area size to tune */
79 {0x0018C000, 0xffffffff, FLAG_PROTECT_CLEAR, FLAG_SETENV, "FS"}, /* data area size to tune */
81 {0x00000000, 0xffffffff, FLAG_PROTECT_CLEAR, FLAG_SETENV, "Data"}, /* data area */
82 {0xffffffff, 0x00000000, FLAG_PROTECT_INVALID, 0, "<Invalid>"}, /* Invalid */
83 {0xffffffff, 0x00000000, FLAG_PROTECT_INVALID, 0, "<Invalid>"}, /* Invalid */
84 {0xffffffff, 0x00000000, FLAG_PROTECT_INVALID, 0, "<Invalid>"}, /* Invalid */
85 {0xffffffff, 0x00000000, FLAG_PROTECT_INVALID, 0, "<Invalid>"}, /* Invalid */
86 {0xffffffff, 0x00000000, FLAG_PROTECT_INVALID, 0, "<Invalid>"}, /* Invalid */
88 #else
89 dataflash_protect_t area_list[NB_DATAFLASH_AREA] = {
90 {0, 0x7fff, FLAG_PROTECT_SET}, /* ROM code */
91 {0x8000, 0x1ffff, FLAG_PROTECT_SET}, /* u-boot code */
92 {0x20000, 0x27fff, FLAG_PROTECT_CLEAR}, /* u-boot environment */
93 {0x28000, 0x1fffff, FLAG_PROTECT_CLEAR}, /* data area size to tune */
95 #endif
97 extern void AT91F_SpiInit (void);
98 extern int AT91F_DataflashProbe (int i, AT91PS_DataflashDesc pDesc);
99 extern int AT91F_DataFlashRead (AT91PS_DataFlash pDataFlash,
100 unsigned long addr,
101 unsigned long size, char *buffer);
102 extern int AT91F_DataFlashWrite( AT91PS_DataFlash pDataFlash,
103 unsigned char *src,
104 int dest,
105 int size );
107 int AT91F_DataflashInit (void)
109 int i, j;
110 int dfcode;
111 int part = 0;
112 int last_part;
113 int found[CFG_MAX_DATAFLASH_BANKS];
114 unsigned char protected;
116 AT91F_SpiInit ();
118 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
119 found[i] = 0;
120 dataflash_info[i].Desc.state = IDLE;
121 dataflash_info[i].id = 0;
122 dataflash_info[i].Device.pages_number = 0;
123 dfcode = AT91F_DataflashProbe (cs[i].cs,
124 &dataflash_info[i].Desc);
126 switch (dfcode) {
127 case AT45DB161:
128 dataflash_info[i].Device.pages_number = 4096;
129 dataflash_info[i].Device.pages_size = 528;
130 dataflash_info[i].Device.page_offset = 10;
131 dataflash_info[i].Device.byte_mask = 0x300;
132 dataflash_info[i].Device.cs = cs[i].cs;
133 dataflash_info[i].Desc.DataFlash_state = IDLE;
134 dataflash_info[i].logical_address = cs[i].addr;
135 dataflash_info[i].id = dfcode;
136 found[i] += dfcode;;
137 break;
139 case AT45DB321:
140 dataflash_info[i].Device.pages_number = 8192;
141 dataflash_info[i].Device.pages_size = 528;
142 dataflash_info[i].Device.page_offset = 10;
143 dataflash_info[i].Device.byte_mask = 0x300;
144 dataflash_info[i].Device.cs = cs[i].cs;
145 dataflash_info[i].Desc.DataFlash_state = IDLE;
146 dataflash_info[i].logical_address = cs[i].addr;
147 dataflash_info[i].id = dfcode;
148 found[i] += dfcode;;
149 break;
151 case AT45DB642:
152 dataflash_info[i].Device.pages_number = 8192;
153 dataflash_info[i].Device.pages_size = 1056;
154 dataflash_info[i].Device.page_offset = 11;
155 dataflash_info[i].Device.byte_mask = 0x700;
156 dataflash_info[i].Device.cs = cs[i].cs;
157 dataflash_info[i].Desc.DataFlash_state = IDLE;
158 dataflash_info[i].logical_address = cs[i].addr;
159 dataflash_info[i].id = dfcode;
160 found[i] += dfcode;;
161 break;
163 case AT45DB128:
164 dataflash_info[i].Device.pages_number = 16384;
165 dataflash_info[i].Device.pages_size = 1056;
166 dataflash_info[i].Device.page_offset = 11;
167 dataflash_info[i].Device.byte_mask = 0x700;
168 dataflash_info[i].Device.cs = cs[i].cs;
169 dataflash_info[i].Desc.DataFlash_state = IDLE;
170 dataflash_info[i].logical_address = cs[i].addr;
171 dataflash_info[i].id = dfcode;
172 found[i] += dfcode;;
173 break;
175 default:
176 dfcode = 0;
177 break;
179 /* set the last area end to the dataflash size*/
180 area_list[NB_DATAFLASH_AREA -1].end =
181 (dataflash_info[i].Device.pages_number *
182 dataflash_info[i].Device.pages_size)-1;
184 last_part=0;
185 /* set the area addresses */
186 for(j = 0; j<NB_DATAFLASH_AREA; j++) {
187 if(found[i]!=0) {
188 dataflash_info[i].Device.area_list[j].start =
189 area_list[part].start +
190 dataflash_info[i].logical_address;
191 if(area_list[part].end == 0xffffffff) {
192 dataflash_info[i].Device.area_list[j].end =
193 dataflash_info[i].end_address +
194 dataflash_info [i].logical_address;
195 last_part = 1;
196 } else {
197 dataflash_info[i].Device.area_list[j].end =
198 area_list[part].end +
199 dataflash_info[i].logical_address;
201 protected = area_list[part].protected;
202 /* Set the environment according to the label...*/
203 if(protected == FLAG_PROTECT_INVALID) {
204 dataflash_info[i].Device.area_list[j].protected =
205 FLAG_PROTECT_INVALID;
206 } else {
207 dataflash_info[i].Device.area_list[j].protected =
208 protected;
210 strcpy((char*)(dataflash_info[i].Device.area_list[j].label),
211 (const char *)area_list[part].label);
213 part++;
216 return found[0];
219 #ifdef CONFIG_NEW_DF_PARTITION
220 int AT91F_DataflashSetEnv (void)
222 int i, j;
223 int part;
224 unsigned char env;
225 unsigned char s[32]; /* Will fit a long int in hex */
226 unsigned long start;
227 for (i = 0, part= 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
228 for(j = 0; j<NB_DATAFLASH_AREA; j++) {
229 env = area_list[part].setenv;
230 /* Set the environment according to the label...*/
231 if((env & FLAG_SETENV) == FLAG_SETENV) {
232 start =
233 dataflash_info[i].Device.area_list[j].start;
234 sprintf(s,"%X",start);
235 setenv(area_list[part].label,s);
237 part++;
241 #endif
243 void dataflash_print_info (void)
245 int i, j;
247 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
248 if (dataflash_info[i].id != 0) {
249 printf("DataFlash:");
250 switch (dataflash_info[i].id) {
251 case AT45DB161:
252 printf("AT45DB161\n");
253 break;
255 case AT45DB321:
256 printf("AT45DB321\n");
257 break;
259 case AT45DB642:
260 printf("AT45DB642\n");
261 break;
262 case AT45DB128:
263 printf("AT45DB128\n");
264 break;
267 printf("Nb pages: %6d\n"
268 "Page Size: %6d\n"
269 "Size=%8d bytes\n"
270 "Logical address: 0x%08X\n",
271 (unsigned int) dataflash_info[i].Device.pages_number,
272 (unsigned int) dataflash_info[i].Device.pages_size,
273 (unsigned int) dataflash_info[i].Device.pages_number *
274 dataflash_info[i].Device.pages_size,
275 (unsigned int) dataflash_info[i].logical_address);
276 for (j=0; j< NB_DATAFLASH_AREA; j++) {
277 switch(dataflash_info[i].Device.area_list[j].protected) {
278 case FLAG_PROTECT_SET:
279 case FLAG_PROTECT_CLEAR:
280 printf("Area %i:\t%08lX to %08lX %s", j,
281 dataflash_info[i].Device.area_list[j].start,
282 dataflash_info[i].Device.area_list[j].end,
283 (dataflash_info[i].Device.area_list[j].protected==FLAG_PROTECT_SET) ? "(RO)" : " ");
284 #ifdef CONFIG_NEW_DF_PARTITION
285 printf(" %s\n", dataflash_info[i].Device.area_list[j].label);
286 #else
287 printf("\n");
288 #endif
289 break;
290 #ifdef CONFIG_NEW_DF_PARTITION
291 case FLAG_PROTECT_INVALID:
292 break;
293 #endif
301 /*---------------------------------------------------------------------------*/
302 /* Function Name : AT91F_DataflashSelect */
303 /* Object : Select the correct device */
304 /*---------------------------------------------------------------------------*/
305 AT91PS_DataFlash AT91F_DataflashSelect (AT91PS_DataFlash pFlash,
306 unsigned long *addr)
308 char addr_valid = 0;
309 int i;
311 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++)
312 if ( dataflash_info[i].id
313 && ((((int) *addr) & 0xFF000000) ==
314 dataflash_info[i].logical_address)) {
315 addr_valid = 1;
316 break;
318 if (!addr_valid) {
319 pFlash = (AT91PS_DataFlash) 0;
320 return pFlash;
322 pFlash->pDataFlashDesc = &(dataflash_info[i].Desc);
323 pFlash->pDevice = &(dataflash_info[i].Device);
324 *addr -= dataflash_info[i].logical_address;
325 return (pFlash);
328 /*---------------------------------------------------------------------------*/
329 /* Function Name : addr_dataflash */
330 /* Object : Test if address is valid */
331 /*---------------------------------------------------------------------------*/
332 int addr_dataflash (unsigned long addr)
334 int addr_valid = 0;
335 int i;
337 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
338 if ((((int) addr) & 0xFF000000) ==
339 dataflash_info[i].logical_address) {
340 addr_valid = 1;
341 break;
345 return addr_valid;
347 /*---------------------------------------------------------------------------*/
348 /* Function Name : size_dataflash */
349 /* Object : Test if address is valid regarding the size */
350 /*---------------------------------------------------------------------------*/
351 int size_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr,
352 unsigned long size)
354 /* is outside the dataflash */
355 if (((int)addr & 0x0FFFFFFF) > (pdataFlash->pDevice->pages_size *
356 pdataFlash->pDevice->pages_number)) return 0;
357 /* is too large for the dataflash */
358 if (size > ((pdataFlash->pDevice->pages_size *
359 pdataFlash->pDevice->pages_number) -
360 ((int)addr & 0x0FFFFFFF))) return 0;
362 return 1;
364 /*---------------------------------------------------------------------------*/
365 /* Function Name : prot_dataflash */
366 /* Object : Test if destination area is protected */
367 /*---------------------------------------------------------------------------*/
368 int prot_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr)
370 int area;
371 /* find area */
372 for (area=0; area < NB_DATAFLASH_AREA; area++) {
373 if ((addr >= pdataFlash->pDevice->area_list[area].start) &&
374 (addr < pdataFlash->pDevice->area_list[area].end))
375 break;
377 if (area == NB_DATAFLASH_AREA)
378 return -1;
380 /*test protection value*/
381 if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_SET)
382 return 0;
383 if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_INVALID)
384 return 0;
386 return 1;
388 /*--------------------------------------------------------------------------*/
389 /* Function Name : dataflash_real_protect */
390 /* Object : protect/unprotect area */
391 /*--------------------------------------------------------------------------*/
392 int dataflash_real_protect (int flag, unsigned long start_addr,
393 unsigned long end_addr)
395 int i,j, area1, area2, addr_valid = 0;
396 /* find dataflash */
397 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
398 if ((((int) start_addr) & 0xF0000000) ==
399 dataflash_info[i].logical_address) {
400 addr_valid = 1;
401 break;
404 if (!addr_valid) {
405 return -1;
407 /* find start area */
408 for (area1=0; area1 < NB_DATAFLASH_AREA; area1++) {
409 if (start_addr == dataflash_info[i].Device.area_list[area1].start)
410 break;
412 if (area1 == NB_DATAFLASH_AREA) return -1;
413 /* find end area */
414 for (area2=0; area2 < NB_DATAFLASH_AREA; area2++) {
415 if (end_addr == dataflash_info[i].Device.area_list[area2].end)
416 break;
418 if (area2 == NB_DATAFLASH_AREA)
419 return -1;
421 /*set protection value*/
422 for(j = area1; j < area2+1 ; j++)
423 if(dataflash_info[i].Device.area_list[j].protected
424 != FLAG_PROTECT_INVALID) {
425 if (flag == 0) {
426 dataflash_info[i].Device.area_list[j].protected
427 = FLAG_PROTECT_CLEAR;
428 } else {
429 dataflash_info[i].Device.area_list[j].protected
430 = FLAG_PROTECT_SET;
434 return (area2-area1+1);
437 /*---------------------------------------------------------------------------*/
438 /* Function Name : read_dataflash */
439 /* Object : dataflash memory read */
440 /*---------------------------------------------------------------------------*/
441 int read_dataflash (unsigned long addr, unsigned long size, char *result)
443 unsigned long AddrToRead = addr;
444 AT91PS_DataFlash pFlash = &DataFlashInst;
446 pFlash = AT91F_DataflashSelect (pFlash, &AddrToRead);
448 if (pFlash == 0)
449 return ERR_UNKNOWN_FLASH_TYPE;
451 if (size_dataflash(pFlash,addr,size) == 0)
452 return ERR_INVAL;
454 return (AT91F_DataFlashRead (pFlash, AddrToRead, size, result));
458 /*---------------------------------------------------------------------------*/
459 /* Function Name : write_dataflash */
460 /* Object : write a block in dataflash */
461 /*---------------------------------------------------------------------------*/
462 int write_dataflash (unsigned long addr_dest, unsigned long addr_src,
463 unsigned long size)
465 unsigned long AddrToWrite = addr_dest;
466 AT91PS_DataFlash pFlash = &DataFlashInst;
468 pFlash = AT91F_DataflashSelect (pFlash, &AddrToWrite);
470 if (pFlash == 0)
471 return ERR_UNKNOWN_FLASH_TYPE;
473 if (size_dataflash(pFlash,addr_dest,size) == 0)
474 return ERR_INVAL;
476 if (prot_dataflash(pFlash,addr_dest) == 0)
477 return ERR_PROTECTED;
479 if (AddrToWrite == -1)
480 return -1;
482 return AT91F_DataFlashWrite (pFlash, (uchar *)addr_src,
483 AddrToWrite, size);
487 void dataflash_perror (int err)
489 switch (err) {
490 case ERR_OK:
491 break;
492 case ERR_TIMOUT:
493 printf("Timeout writing to DataFlash\n");
494 break;
495 case ERR_PROTECTED:
496 printf("Can't write to protected/invalid DataFlash sectors\n");
497 break;
498 case ERR_INVAL:
499 printf("Outside available DataFlash\n");
500 break;
501 case ERR_UNKNOWN_FLASH_TYPE:
502 printf("Unknown Type of DataFlash\n");
503 break;
504 case ERR_PROG_ERROR:
505 printf("General DataFlash Programming Error\n");
506 break;
507 default:
508 printf("%s[%d] FIXME: rc=%d\n", __FILE__, __LINE__, err);
509 break;
513 #endif