1 /* LowLevel function for ATMEL DataFlash support
2 * Author : Hamid Ikdoumi (Atmel)
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22 #ifdef CONFIG_HAS_DATAFLASH
23 #include <asm/hardware.h>
24 #include <dataflash.h>
26 AT91S_DATAFLASH_INFO dataflash_info
[CFG_MAX_DATAFLASH_BANKS
];
27 static AT91S_DataFlash DataFlashInst
;
29 int cs
[][CFG_MAX_DATAFLASH_BANKS
] = {
30 {CFG_DATAFLASH_LOGIC_ADDR_CS0
, 0}, /* Logical adress, CS */
31 {CFG_DATAFLASH_LOGIC_ADDR_CS3
, 3}
34 /*define the area offsets*/
35 dataflash_protect_t area_list
[NB_DATAFLASH_AREA
] = {
36 {0, 0x7fff, FLAG_PROTECT_SET
}, /* ROM code */
37 {0x8000, 0x1ffff, FLAG_PROTECT_SET
}, /* u-boot code */
38 {0x20000, 0x27fff, FLAG_PROTECT_CLEAR
}, /* u-boot environment */
39 {0x28000, 0x1fffff, FLAG_PROTECT_CLEAR
}, /* data area size to tune */
42 extern void AT91F_SpiInit (void);
43 extern int AT91F_DataflashProbe (int i
, AT91PS_DataflashDesc pDesc
);
44 extern int AT91F_DataFlashRead (AT91PS_DataFlash pDataFlash
,
46 unsigned long size
, char *buffer
);
47 extern int AT91F_DataFlashWrite( AT91PS_DataFlash pDataFlash
,
52 int AT91F_DataflashInit (void)
59 for (i
= 0; i
< CFG_MAX_DATAFLASH_BANKS
; i
++) {
60 dataflash_info
[i
].Desc
.state
= IDLE
;
61 dataflash_info
[i
].id
= 0;
62 dataflash_info
[i
].Device
.pages_number
= 0;
63 dfcode
= AT91F_DataflashProbe (cs
[i
][1], &dataflash_info
[i
].Desc
);
67 dataflash_info
[i
].Device
.pages_number
= 4096;
68 dataflash_info
[i
].Device
.pages_size
= 528;
69 dataflash_info
[i
].Device
.page_offset
= 10;
70 dataflash_info
[i
].Device
.byte_mask
= 0x300;
71 dataflash_info
[i
].Device
.cs
= cs
[i
][1];
72 dataflash_info
[i
].Desc
.DataFlash_state
= IDLE
;
73 dataflash_info
[i
].logical_address
= cs
[i
][0];
74 dataflash_info
[i
].id
= dfcode
;
78 dataflash_info
[i
].Device
.pages_number
= 8192;
79 dataflash_info
[i
].Device
.pages_size
= 528;
80 dataflash_info
[i
].Device
.page_offset
= 10;
81 dataflash_info
[i
].Device
.byte_mask
= 0x300;
82 dataflash_info
[i
].Device
.cs
= cs
[i
][1];
83 dataflash_info
[i
].Desc
.DataFlash_state
= IDLE
;
84 dataflash_info
[i
].logical_address
= cs
[i
][0];
85 dataflash_info
[i
].id
= dfcode
;
89 dataflash_info
[i
].Device
.pages_number
= 8192;
90 dataflash_info
[i
].Device
.pages_size
= 1056;
91 dataflash_info
[i
].Device
.page_offset
= 11;
92 dataflash_info
[i
].Device
.byte_mask
= 0x700;
93 dataflash_info
[i
].Device
.cs
= cs
[i
][1];
94 dataflash_info
[i
].Desc
.DataFlash_state
= IDLE
;
95 dataflash_info
[i
].logical_address
= cs
[i
][0];
96 dataflash_info
[i
].id
= dfcode
;
99 dataflash_info
[i
].Device
.pages_number
= 16384;
100 dataflash_info
[i
].Device
.pages_size
= 1056;
101 dataflash_info
[i
].Device
.page_offset
= 11;
102 dataflash_info
[i
].Device
.byte_mask
= 0x700;
103 dataflash_info
[i
].Device
.cs
= cs
[i
][1];
104 dataflash_info
[i
].Desc
.DataFlash_state
= IDLE
;
105 dataflash_info
[i
].logical_address
= cs
[i
][0];
106 dataflash_info
[i
].id
= dfcode
;
112 /* set the last area end to the dataflash size*/
113 area_list
[NB_DATAFLASH_AREA
-1].end
=
114 (dataflash_info
[i
].Device
.pages_number
*
115 dataflash_info
[i
].Device
.pages_size
)-1;
117 /* set the area addresses */
118 for(j
= 0; j
<NB_DATAFLASH_AREA
; j
++) {
119 dataflash_info
[i
].Device
.area_list
[j
].start
= area_list
[j
].start
+ dataflash_info
[i
].logical_address
;
120 dataflash_info
[i
].Device
.area_list
[j
].end
= area_list
[j
].end
+ dataflash_info
[i
].logical_address
;
121 dataflash_info
[i
].Device
.area_list
[j
].protected = area_list
[j
].protected;
128 void dataflash_print_info (void)
132 for (i
= 0; i
< CFG_MAX_DATAFLASH_BANKS
; i
++) {
133 if (dataflash_info
[i
].id
!= 0) {
134 printf ("DataFlash:");
135 switch (dataflash_info
[i
].id
) {
137 printf ("AT45DB161\n");
141 printf ("AT45DB321\n");
145 printf ("AT45DB642\n");
148 printf ("AT45DB128\n");
152 printf ("Nb pages: %6d\n"
155 "Logical address: 0x%08X\n",
156 (unsigned int) dataflash_info
[i
].Device
.pages_number
,
157 (unsigned int) dataflash_info
[i
].Device
.pages_size
,
158 (unsigned int) dataflash_info
[i
].Device
.pages_number
*
159 dataflash_info
[i
].Device
.pages_size
,
160 (unsigned int) dataflash_info
[i
].logical_address
);
161 for (j
=0; j
< NB_DATAFLASH_AREA
; j
++) {
162 printf ("Area %i:\t%08lX to %08lX %s\n", j
,
163 dataflash_info
[i
].Device
.area_list
[j
].start
,
164 dataflash_info
[i
].Device
.area_list
[j
].end
,
165 (dataflash_info
[i
].Device
.area_list
[j
].protected ==
166 FLAG_PROTECT_SET
) ? "(RO)" : "");
173 /*------------------------------------------------------------------------------*/
174 /* Function Name : AT91F_DataflashSelect */
175 /* Object : Select the correct device */
176 /*------------------------------------------------------------------------------*/
177 AT91PS_DataFlash
AT91F_DataflashSelect (AT91PS_DataFlash pFlash
,
183 for (i
= 0; i
< CFG_MAX_DATAFLASH_BANKS
; i
++)
184 if ((*addr
& 0xFF000000) == dataflash_info
[i
].logical_address
) {
189 pFlash
= (AT91PS_DataFlash
) 0;
192 pFlash
->pDataFlashDesc
= &(dataflash_info
[i
].Desc
);
193 pFlash
->pDevice
= &(dataflash_info
[i
].Device
);
194 *addr
-= dataflash_info
[i
].logical_address
;
198 /*------------------------------------------------------------------------------*/
199 /* Function Name : addr_dataflash */
200 /* Object : Test if address is valid */
201 /*------------------------------------------------------------------------------*/
202 int addr_dataflash (unsigned long addr
)
207 for (i
= 0; i
< CFG_MAX_DATAFLASH_BANKS
; i
++) {
208 if ((((int) addr
) & 0xFF000000) ==
209 dataflash_info
[i
].logical_address
) {
217 /*-----------------------------------------------------------------------------*/
218 /* Function Name : size_dataflash */
219 /* Object : Test if address is valid regarding the size */
220 /*-----------------------------------------------------------------------------*/
221 int size_dataflash (AT91PS_DataFlash pdataFlash
, unsigned long addr
, unsigned long size
)
223 /* is outside the dataflash */
224 if (((int)addr
& 0x0FFFFFFF) > (pdataFlash
->pDevice
->pages_size
*
225 pdataFlash
->pDevice
->pages_number
)) return 0;
226 /* is too large for the dataflash */
227 if (size
> ((pdataFlash
->pDevice
->pages_size
*
228 pdataFlash
->pDevice
->pages_number
) - ((int)addr
& 0x0FFFFFFF))) return 0;
232 /*-----------------------------------------------------------------------------*/
233 /* Function Name : prot_dataflash */
234 /* Object : Test if destination area is protected */
235 /*-----------------------------------------------------------------------------*/
236 int prot_dataflash (AT91PS_DataFlash pdataFlash
, unsigned long addr
)
240 for (area
=0; area
< NB_DATAFLASH_AREA
; area
++) {
241 if ((addr
>= pdataFlash
->pDevice
->area_list
[area
].start
) &&
242 (addr
< pdataFlash
->pDevice
->area_list
[area
].end
))
245 if (area
== NB_DATAFLASH_AREA
) return -1;
246 /*test protection value*/
247 if (pdataFlash
->pDevice
->area_list
[area
].protected == FLAG_PROTECT_SET
) return 0;
251 /*-----------------------------------------------------------------------------*/
252 /* Function Name : dataflash_real_protect */
253 /* Object : protect/unprotect area */
254 /*-----------------------------------------------------------------------------*/
255 int dataflash_real_protect (int flag
, unsigned long start_addr
, unsigned long end_addr
)
257 int i
,j
, area1
, area2
, addr_valid
= 0;
259 for (i
= 0; i
< CFG_MAX_DATAFLASH_BANKS
; i
++) {
260 if ((((int) start_addr
) & 0xF0000000) ==
261 dataflash_info
[i
].logical_address
) {
269 /* find start area */
270 for (area1
=0; area1
< NB_DATAFLASH_AREA
; area1
++) {
271 if (start_addr
== dataflash_info
[i
].Device
.area_list
[area1
].start
) break;
273 if (area1
== NB_DATAFLASH_AREA
) return -1;
275 for (area2
=0; area2
< NB_DATAFLASH_AREA
; area2
++) {
276 if (end_addr
== dataflash_info
[i
].Device
.area_list
[area2
].end
) break;
278 if (area2
== NB_DATAFLASH_AREA
) return -1;
280 /*set protection value*/
281 for(j
= area1
; j
< area2
+1 ; j
++)
282 if (flag
== 0) dataflash_info
[i
].Device
.area_list
[j
].protected = FLAG_PROTECT_CLEAR
;
283 else dataflash_info
[i
].Device
.area_list
[j
].protected = FLAG_PROTECT_SET
;
285 return (area2
-area1
+1);
288 /*------------------------------------------------------------------------------*/
289 /* Function Name : read_dataflash */
290 /* Object : dataflash memory read */
291 /*------------------------------------------------------------------------------*/
292 int read_dataflash (unsigned long addr
, unsigned long size
, char *result
)
294 int AddrToRead
= addr
;
295 AT91PS_DataFlash pFlash
= &DataFlashInst
;
297 pFlash
= AT91F_DataflashSelect (pFlash
, &AddrToRead
);
300 return ERR_UNKNOWN_FLASH_TYPE
;
302 if (size_dataflash(pFlash
,addr
,size
) == 0)
305 return (AT91F_DataFlashRead (pFlash
, AddrToRead
, size
, result
));
309 /*-----------------------------------------------------------------------------*/
310 /* Function Name : write_dataflash */
311 /* Object : write a block in dataflash */
312 /*-----------------------------------------------------------------------------*/
313 int write_dataflash (unsigned long addr_dest
, unsigned long addr_src
,
316 int AddrToWrite
= addr_dest
;
317 AT91PS_DataFlash pFlash
= &DataFlashInst
;
319 pFlash
= AT91F_DataflashSelect (pFlash
, &AddrToWrite
);
322 return ERR_UNKNOWN_FLASH_TYPE
;
324 if (size_dataflash(pFlash
,addr_dest
,size
) == 0)
327 if (prot_dataflash(pFlash
,addr_dest
) == 0)
328 return ERR_PROTECTED
;
330 if (AddrToWrite
== -1)
333 return AT91F_DataFlashWrite (pFlash
, (char *) addr_src
, AddrToWrite
, size
);
337 void dataflash_perror (int err
)
343 printf ("Timeout writing to DataFlash\n");
346 printf ("Can't write to protected DataFlash sectors\n");
349 printf ("Outside available DataFlash\n");
351 case ERR_UNKNOWN_FLASH_TYPE
:
352 printf ("Unknown Type of DataFlash\n");
355 printf ("General DataFlash Programming Error\n");
358 printf ("%s[%d] FIXME: rc=%d\n", __FILE__
, __LINE__
, err
);