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