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