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