1 /*----------------------------------------------------------------------------
2 * ATMEL Microcontroller Software Support - ROUSSET -
3 *----------------------------------------------------------------------------
4 * The software is delivered "AS IS" without warranty or condition of any
5 * kind, either express, implied or statutory. This includes without
6 * limitation any warranty or condition with respect to merchantability or
7 * fitness for any particular purpose, or against the infringements of
8 * intellectual property rights of others.
9 *----------------------------------------------------------------------------
10 * File Name : dataflash.c
11 * Object : High level functions for the dataflash
12 * Creation : HIi 10/10/2003
13 *----------------------------------------------------------------------------
17 #include "dataflash.h"
20 AT91S_DATAFLASH_INFO dataflash_info
[CFG_MAX_DATAFLASH_BANKS
];
21 static AT91S_DataFlash DataFlashInst
;
23 int cs
[][CFG_MAX_DATAFLASH_BANKS
] = {
24 {CFG_DATAFLASH_LOGIC_ADDR_CS0
, 0}, /* Logical adress, CS */
25 {CFG_DATAFLASH_LOGIC_ADDR_CS3
, 3}
28 int AT91F_DataflashInit(void)
36 for (i
= 0; i
< CFG_MAX_DATAFLASH_BANKS
; i
++) {
37 dataflash_info
[i
].id
= 0;
38 dataflash_info
[i
].Device
.pages_number
= 0;
39 dfcode
= AT91F_DataflashProbe (cs
[i
][1], &dataflash_info
[i
].Desc
);
43 dataflash_info
[i
].Device
.pages_number
= 4096;
44 dataflash_info
[i
].Device
.pages_size
= 528;
45 dataflash_info
[i
].Device
.page_offset
= 10;
46 dataflash_info
[i
].Device
.byte_mask
= 0x300;
47 dataflash_info
[i
].Device
.cs
= cs
[i
][1];
48 dataflash_info
[i
].Desc
.DataFlash_state
= IDLE
;
49 dataflash_info
[i
].logical_address
= cs
[i
][0];
50 dataflash_info
[i
].id
= dfcode
;
55 dataflash_info
[i
].Device
.pages_number
= 8192;
56 dataflash_info
[i
].Device
.pages_size
= 528;
57 dataflash_info
[i
].Device
.page_offset
= 10;
58 dataflash_info
[i
].Device
.byte_mask
= 0x300;
59 dataflash_info
[i
].Device
.cs
= cs
[i
][1];
60 dataflash_info
[i
].Desc
.DataFlash_state
= IDLE
;
61 dataflash_info
[i
].logical_address
= cs
[i
][0];
62 dataflash_info
[i
].id
= dfcode
;
67 dataflash_info
[i
].Device
.pages_number
= 8192;
68 dataflash_info
[i
].Device
.pages_size
= 1056;
69 dataflash_info
[i
].Device
.page_offset
= 11;
70 dataflash_info
[i
].Device
.byte_mask
= 0x700;
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
= 16384;
79 dataflash_info
[i
].Device
.pages_size
= 1056;
80 dataflash_info
[i
].Device
.page_offset
= 11;
81 dataflash_info
[i
].Device
.byte_mask
= 0x700;
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
;
96 void AT91F_DataflashPrintInfo(void)
99 for (i
= 0; i
< CFG_MAX_DATAFLASH_BANKS
; i
++) {
100 if (dataflash_info
[i
].id
!= 0) {
101 printf ("DF:AT45DB");
102 switch (dataflash_info
[i
].id
) {
119 printf ("\n# PG: %6d\n"
123 (unsigned int) dataflash_info
[i
].Device
.pages_number
,
124 (unsigned int) dataflash_info
[i
].Device
.pages_size
,
125 (unsigned int) dataflash_info
[i
].Device
.pages_number
*
126 dataflash_info
[i
].Device
.pages_size
,
127 (unsigned int) dataflash_info
[i
].logical_address
);
133 /*------------------------------------------------------------------------------*/
134 /* Function Name : AT91F_DataflashSelect */
135 /* Object : Select the correct device */
136 /*------------------------------------------------------------------------------*/
137 static AT91PS_DataFlash
AT91F_DataflashSelect(AT91PS_DataFlash pFlash
,
143 for (i
= 0; i
< CFG_MAX_DATAFLASH_BANKS
; i
++)
144 if ((*addr
& 0xFF000000) == dataflash_info
[i
].logical_address
) {
149 pFlash
= (AT91PS_DataFlash
) 0;
152 pFlash
->pDataFlashDesc
= &(dataflash_info
[i
].Desc
);
153 pFlash
->pDevice
= &(dataflash_info
[i
].Device
);
154 *addr
-= dataflash_info
[i
].logical_address
;
159 /*------------------------------------------------------------------------------*/
160 /* Function Name : read_dataflash */
161 /* Object : dataflash memory read */
162 /*------------------------------------------------------------------------------*/
163 int read_dataflash(unsigned long addr
, unsigned long size
, char *result
)
165 unsigned int AddrToRead
= addr
;
166 AT91PS_DataFlash pFlash
= &DataFlashInst
;
168 pFlash
= AT91F_DataflashSelect (pFlash
, &AddrToRead
);
172 return (AT91F_DataFlashRead(pFlash
, AddrToRead
, size
, result
));
176 /*-----------------------------------------------------------------------------*/
177 /* Function Name : write_dataflash */
178 /* Object : write a block in dataflash */
179 /*-----------------------------------------------------------------------------*/
180 int write_dataflash(unsigned long addr_dest
, unsigned int addr_src
,
183 unsigned int AddrToWrite
= addr_dest
;
184 AT91PS_DataFlash pFlash
= &DataFlashInst
;
186 pFlash
= AT91F_DataflashSelect(pFlash
, &AddrToWrite
);
187 if (AddrToWrite
== -1)
190 return AT91F_DataFlashWrite(pFlash
, (unsigned char *) addr_src
, AddrToWrite
, size
);
193 /*-----------------------------------------------------------------------------*/
194 /* Function Name : erase_dataflash */
195 /* Object : Erase entire dataflash */
196 /*-----------------------------------------------------------------------------*/
197 int erase_dataflash(unsigned long addr_dest
)
199 unsigned int AddrToWrite
= addr_dest
;
200 AT91PS_DataFlash pFlash
= &DataFlashInst
;
202 pFlash
= AT91F_DataflashSelect (pFlash
, &AddrToWrite
);
203 if (AddrToWrite
== -1)
206 return AT91F_DataFlashErase(pFlash
);