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