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, unsigned long *addr)
182 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++)
183 if ((*addr & 0xFF000000) == dataflash_info[i].logical_address) {
188 pFlash = (AT91PS_DataFlash) 0;
191 pFlash->pDataFlashDesc = &(dataflash_info[i].Desc);
192 pFlash->pDevice = &(dataflash_info[i].Device);
193 *addr -= dataflash_info[i].logical_address;
197 /*------------------------------------------------------------------------------*/
198 /* Function Name : addr_dataflash */
199 /* Object : Test if address is valid */
200 /*------------------------------------------------------------------------------*/
201 int addr_dataflash (unsigned long addr)
206 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
207 if ((((int) addr) & 0xFF000000) ==
208 dataflash_info[i].logical_address) {
216 /*-----------------------------------------------------------------------------*/
217 /* Function Name : size_dataflash */
218 /* Object : Test if address is valid regarding the size */
219 /*-----------------------------------------------------------------------------*/
220 int size_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr, unsigned long size)
222 /* is outside the dataflash */
223 if (((int)addr & 0x0FFFFFFF) > (pdataFlash->pDevice->pages_size *
224 pdataFlash->pDevice->pages_number)) return 0;
225 /* is too large for the dataflash */
226 if (size > ((pdataFlash->pDevice->pages_size *
227 pdataFlash->pDevice->pages_number) - ((int)addr & 0x0FFFFFFF))) return 0;
231 /*-----------------------------------------------------------------------------*/
232 /* Function Name : prot_dataflash */
233 /* Object : Test if destination area is protected */
234 /*-----------------------------------------------------------------------------*/
235 int prot_dataflash (AT91PS_DataFlash pdataFlash, unsigned long addr)
239 for (area=0; area < NB_DATAFLASH_AREA; area++) {
240 if ((addr >= pdataFlash->pDevice->area_list[area].start) &&
241 (addr < pdataFlash->pDevice->area_list[area].end))
244 if (area == NB_DATAFLASH_AREA) return -1;
245 /*test protection value*/
246 if (pdataFlash->pDevice->area_list[area].protected == FLAG_PROTECT_SET) return 0;
250 /*-----------------------------------------------------------------------------*/
251 /* Function Name : dataflash_real_protect */
252 /* Object : protect/unprotect area */
253 /*-----------------------------------------------------------------------------*/
254 int dataflash_real_protect (int flag, unsigned long start_addr, unsigned long end_addr)
256 int i,j, area1, area2, addr_valid = 0;
258 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
259 if ((((int) start_addr) & 0xF0000000) ==
260 dataflash_info[i].logical_address) {
268 /* find start area */
269 for (area1=0; area1 < NB_DATAFLASH_AREA; area1++) {
270 if (start_addr == dataflash_info[i].Device.area_list[area1].start) break;
272 if (area1 == NB_DATAFLASH_AREA) return -1;
274 for (area2=0; area2 < NB_DATAFLASH_AREA; area2++) {
275 if (end_addr == dataflash_info[i].Device.area_list[area2].end) break;
277 if (area2 == NB_DATAFLASH_AREA) return -1;
279 /*set protection value*/
280 for(j = area1; j < area2+1 ; j++)
281 if (flag == 0) dataflash_info[i].Device.area_list[j].protected = FLAG_PROTECT_CLEAR;
282 else dataflash_info[i].Device.area_list[j].protected = FLAG_PROTECT_SET;
284 return (area2-area1+1);
287 /*------------------------------------------------------------------------------*/
288 /* Function Name : read_dataflash */
289 /* Object : dataflash memory read */
290 /*------------------------------------------------------------------------------*/
291 int read_dataflash (unsigned long addr, unsigned long size, char *result)
293 unsigned long AddrToRead = addr;
294 AT91PS_DataFlash pFlash = &DataFlashInst;
296 pFlash = AT91F_DataflashSelect (pFlash, &AddrToRead);
299 return ERR_UNKNOWN_FLASH_TYPE;
301 if (size_dataflash(pFlash,addr,size) == 0)
304 return (AT91F_DataFlashRead (pFlash, AddrToRead, size, result));
308 /*-----------------------------------------------------------------------------*/
309 /* Function Name : write_dataflash */
310 /* Object : write a block in dataflash */
311 /*-----------------------------------------------------------------------------*/
312 int write_dataflash (unsigned long addr_dest, unsigned long addr_src,
315 unsigned long AddrToWrite = addr_dest;
316 AT91PS_DataFlash pFlash = &DataFlashInst;
318 pFlash = AT91F_DataflashSelect (pFlash, &AddrToWrite);
321 return ERR_UNKNOWN_FLASH_TYPE;
323 if (size_dataflash(pFlash,addr_dest,size) == 0)
326 if (prot_dataflash(pFlash,addr_dest) == 0)
327 return ERR_PROTECTED;
329 if (AddrToWrite == -1)
332 return AT91F_DataFlashWrite (pFlash, (uchar *)addr_src, AddrToWrite, size);
336 void dataflash_perror (int err)
342 printf ("Timeout writing to DataFlash\n");
345 printf ("Can't write to protected DataFlash sectors\n");
348 printf ("Outside available DataFlash\n");
350 case ERR_UNKNOWN_FLASH_TYPE:
351 printf ("Unknown Type of DataFlash\n");
354 printf ("General DataFlash Programming Error\n");
357 printf ("%s[%d] FIXME: rc=%d\n", __FILE__, __LINE__, err);