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 //*----------------------------------------------------------------------------
15 #include "dataflash.h"
17 void AT91F_InitSdram(void);
21 AT91S_DATAFLASH_INFO dataflash_info
[CFG_MAX_DATAFLASH_BANKS
];
22 static AT91S_DataFlash DataFlashInst
;
24 int cs
[][CFG_MAX_DATAFLASH_BANKS
] = {
25 {CFG_DATAFLASH_LOGIC_ADDR_CS0
, 0}, /* Logical adress, CS */
26 {CFG_DATAFLASH_LOGIC_ADDR_CS1
, 1}, /* Logical adress, CS */
27 {CFG_DATAFLASH_LOGIC_ADDR_CS2
, 2}, /* Logical adress, CS */
28 {CFG_DATAFLASH_LOGIC_ADDR_CS3
, 3}
31 #define SIZE_REGS_64M 0x2188c159
32 #define SIZE_REGS_128M 0x2188c15a
35 int AT91F_DataflashInit (void)
42 for (i
= 0; i
< CFG_MAX_DATAFLASH_BANKS
; i
++) {
43 dataflash_info
[i
].Desc
.state
= IDLE
;
44 dataflash_info
[i
].id
= 0;
45 dataflash_info
[i
].Device
.pages_number
= 0;
46 dfcode
= AT91F_DataflashProbe(cs
[i
][1], &dataflash_info
[i
].Desc
);
51 printf("\n\rflash nb %d found",i
);
52 dataflash_info
[i
].Device
.pages_number
= 4096;
53 dataflash_info
[i
].Device
.pages_size
= 528;
54 dataflash_info
[i
].Device
.page_offset
= 10;
55 dataflash_info
[i
].Device
.byte_mask
= 0x300;
56 dataflash_info
[i
].Device
.cs
= cs
[i
][1];
57 dataflash_info
[i
].Desc
.DataFlash_state
= IDLE
;
58 dataflash_info
[i
].logical_address
= cs
[i
][0];
59 dataflash_info
[i
].id
= dfcode
;
63 printf("\n\rflash nb %d found",i
);
64 dataflash_info
[i
].Device
.pages_number
= 8192;
65 dataflash_info
[i
].Device
.pages_size
= 528;
66 dataflash_info
[i
].Device
.page_offset
= 10;
67 dataflash_info
[i
].Device
.byte_mask
= 0x300;
68 dataflash_info
[i
].Device
.cs
= cs
[i
][1];
69 dataflash_info
[i
].Desc
.DataFlash_state
= IDLE
;
70 dataflash_info
[i
].logical_address
= cs
[i
][0];
71 dataflash_info
[i
].id
= dfcode
;
76 printf("\n\rflash nb %d found",i
);
77 dataflash_info
[i
].Device
.pages_number
= 8192;
78 dataflash_info
[i
].Device
.pages_size
= 1056;
79 dataflash_info
[i
].Device
.page_offset
= 11;
80 dataflash_info
[i
].Device
.byte_mask
= 0x700;
81 dataflash_info
[i
].Device
.cs
= cs
[i
][1];
82 dataflash_info
[i
].Desc
.DataFlash_state
= IDLE
;
83 dataflash_info
[i
].logical_address
= cs
[i
][0];
84 dataflash_info
[i
].id
= dfcode
;
87 printf("\n\rflash nb %d found",i
);
88 dataflash_info
[i
].Device
.pages_number
= 16384;
89 dataflash_info
[i
].Device
.pages_size
= 1056;
90 dataflash_info
[i
].Device
.page_offset
= 11;
91 dataflash_info
[i
].Device
.byte_mask
= 0x700;
92 dataflash_info
[i
].Device
.cs
= cs
[i
][1];
93 dataflash_info
[i
].Desc
.DataFlash_state
= IDLE
;
94 dataflash_info
[i
].logical_address
= cs
[i
][0];
95 dataflash_info
[i
].id
= dfcode
;
99 printf("\n\rflash nb %d not found",i
);
110 if ((dataflash_info
[2].id
!= 0) && (dataflash_info
[3].id
!= 0 )){
111 return SIZE_REGS_128M
;
114 return SIZE_REGS_64M
;
118 void AT91F_DataflashPrintInfo(void)
123 for (i
= 0; i
< CFG_MAX_DATAFLASH_BANKS
; i
++) {
124 if (dataflash_info
[i
].id
!= 0) {
125 printf ("DataFlash:");
126 switch (dataflash_info
[i
].id
) {
127 #ifndef REDUCE_CODE_SIZE
129 printf ("AT45DB161\n\r");
133 printf ("AT45DB321\n\r");
137 printf ("AT45DB642\n\r");
140 printf ("AT45DB128\n\r");
144 printf ("Nb pages: %6d\n\r"
147 "Logical address: 0x%08X\n\r",
148 (unsigned int) dataflash_info
[i
].Device
.pages_number
,
149 (unsigned int) dataflash_info
[i
].Device
.pages_size
,
150 (unsigned int) dataflash_info
[i
].Device
.pages_number
*
151 dataflash_info
[i
].Device
.pages_size
,
152 (unsigned int) dataflash_info
[i
].logical_address
);
158 /*------------------------------------------------------------------------------*/
159 /* Function Name : AT91F_DataflashSelect */
160 /* Object : Select the correct device */
161 /*------------------------------------------------------------------------------*/
162 AT91PS_DataFlash
AT91F_DataflashSelect (AT91PS_DataFlash pFlash
,
168 for (i
= 0; i
< CFG_MAX_DATAFLASH_BANKS
; i
++)
169 if ((*addr
& 0xFF000000) == dataflash_info
[i
].logical_address
) {
174 pFlash
= (AT91PS_DataFlash
) 0;
177 pFlash
->pDataFlashDesc
= &(dataflash_info
[i
].Desc
);
178 pFlash
->pDevice
= &(dataflash_info
[i
].Device
);
179 *addr
-= dataflash_info
[i
].logical_address
;
184 /*------------------------------------------------------------------------------*/
185 /* Function Name : addr_dataflash */
186 /* Object : Test if address is valid */
187 /*------------------------------------------------------------------------------*/
188 int addr_dataflash (unsigned long addr
)
193 for (i
= 0; i
< CFG_MAX_DATAFLASH_BANKS
; i
++) {
194 if ((((int) addr
) & 0xFF000000) ==
195 dataflash_info
[i
].logical_address
) {
204 /*------------------------------------------------------------------------------*/
205 /* Function Name : read_dataflash */
206 /* Object : dataflash memory read */
207 /*------------------------------------------------------------------------------*/
208 int read_dataflash (unsigned long addr
, unsigned long size
, char *result
)
210 unsigned int AddrToRead
= addr
;
211 // AT91F_DataflashPrintInfo();
212 AT91PS_DataFlash pFlash
= &DataFlashInst
;
214 pFlash
= AT91F_DataflashSelect (pFlash
, &AddrToRead
);
218 return (AT91F_DataFlashRead (pFlash
, AddrToRead
, size
, result
));
222 /*-----------------------------------------------------------------------------*/
223 /* Function Name : write_dataflash */
224 /* Object : write a block in dataflash */
225 /*-----------------------------------------------------------------------------*/
226 int write_dataflash (unsigned long addr_dest
, unsigned int addr_src
,
229 unsigned int AddrToWrite
= addr_dest
;
230 AT91PS_DataFlash pFlash
= &DataFlashInst
;
232 pFlash
= AT91F_DataflashSelect (pFlash
, &AddrToWrite
);
233 if (AddrToWrite
== -1)
236 return AT91F_DataFlashWrite (pFlash
, (unsigned char *) addr_src
, AddrToWrite
, size
);
240 int erase_dataflash(int nb
)
242 AT91PS_DataFlash pFlash
= &DataFlashInst
;
243 AT91S_DataFlashStatus status
;
246 if ( dataflash_info
[nb
].id
!= 0) {
247 AT91F_SpiEnable(pFlash
->pDevice
->cs
);
249 pFlash
->pDataFlashDesc
= &(dataflash_info
[nb
].Desc
);
250 pFlash
->pDevice
= &(dataflash_info
[nb
].Device
);
251 for ( page_nb
= 0 ; page_nb
< dataflash_info
[nb
].Device
.pages_number
; page_nb
++) {
252 if ( page_nb
% 16 == 0)
253 printf("Erase page nb %d\r\n",page_nb
);
254 status
= AT91F_PageErase(pFlash
,page_nb
);
255 AT91F_DataFlashWaitReady(pFlash
->pDataFlashDesc
, AT91C_DATAFLASH_TIMEOUT
);
257 printf("Erase page nb %d error\n",page_nb
);
258 return AT91C_DATAFLASH_ERROR
;
268 * Le sector protection register ( spr) contient la liste des secteurs de dataflash
271 * On efface le spr par la cmd
273 * Erase Sector Protection Register 3DH 2AH 7FH CFH
275 * On programme le spr par
277 * Program Sector Protection Register 3DH 2AH 7FH FCH
279 * avec dans les bytes suivants la protection des secteurs.
280 * Cette protection peut être activée par la command
282 * Enable Sector Protection 3DH 2AH 7FH A9H
284 * Le conte,u du registre peut etre lu par
286 * Read Sector Protection Register 32H xxH xxH xxH
290 * Disable Sector Protection 3DH 2AH 7FH 9AH
293 static void clear_desc( AT91S_DataflashDesc
*d
)
295 #ifdef REDUCE_CODE_SIZE
297 char *ptr1
= ( char *)d
;
298 for ( i
= 0 ; i
< sizeof(AT91S_DataflashDesc
) ; i
++)
314 d
->DataFlash_state
= 0;
327 void memset(unsigned char *ptr
,char val
, int size
)
330 for ( i
= 0 ; i
< size
; i
++) {
335 AT91S_DataflashDesc desc
;
337 t_read_lock_cmd read_lock_cmd_sector
=
346 t_read_lock_cmd read_lock_cmd_lockdown
=
355 t_read_lock_cmd read_lock_cmd_security
=
364 void AT91F_SpiEnable(int cs
);
366 void read_lock_reg(t_read_lock_cmd
*ptrcmd
)
369 unsigned char rx
[129];
373 for ( fl_nb
= 0 ; fl_nb
< CFG_MAX_DATAFLASH_BANKS
; fl_nb
++) {
375 if (dataflash_info
[fl_nb
].id
!= 0) {
376 AT91F_SpiEnable(cs
[fl_nb
][1]);
379 memset(rx
,0,sizeof(rx
));
381 desc
.tx_cmd_pt
= ptrcmd
->cmd
;
383 desc
.tx_cmd_size
= 4;
384 desc
.rx_cmd_size
= 4;
385 desc
.tx_data_size
= ptrcmd
->txsize
;
386 desc
.rx_data_size
= ptrcmd
->rxsize
;
387 desc
.tx_data_pt
= rx
;
388 desc
.rx_data_pt
= rx
;
390 AT91F_SpiWrite(&desc
);
392 if ( ptrcmd
== &read_lock_cmd_sector
) {
393 printf("Lock status register");
395 if ( ptrcmd
== &read_lock_cmd_lockdown
) {
396 printf("lockdown status register");
398 if ( ptrcmd
== &read_lock_cmd_security
) {
399 printf("security register ");
401 printf("size %d flash_nb %d \n\r",ptrcmd
->rxsize
,fl_nb
);
402 for ( i
= 0 ; i
< ptrcmd
->rxsize
; i
++ ) {
406 printf("0x%02x:",rx
[i
]);
414 unsigned char clear_lock_cmd
[4]=
419 int test_fl_info(int fl_nb
)
421 if (dataflash_info
[fl_nb
].id
!= 0) {
422 AT91F_SpiEnable(cs
[fl_nb
][1]);
432 void erase_lock_reg(int fl_nb
)
435 unsigned char rx
[33];
436 if (test_fl_info(fl_nb
)) {
437 memset(rx
,0,sizeof(rx
));
439 desc
.tx_cmd_pt
= clear_lock_cmd
;
441 desc
.tx_cmd_size
= 4;
442 desc
.rx_cmd_size
= 4;
443 AT91F_SpiWrite(&desc
);
447 unsigned char lock_unlock_cmd
[4]=
452 unsigned char enable_lock_unlock_cmd
[4]=
457 unsigned char disable_lock_unlock_cmd
[4]=
463 void flash_en_lock_unlock(unsigned char *cmd
)
465 unsigned char rxf
[4];
466 unsigned char rx
[33];
468 desc
.tx_cmd_pt
= cmd
;
469 desc
.rx_cmd_pt
= rxf
;
470 desc
.tx_cmd_size
= 4;
471 desc
.rx_cmd_size
= 4;
473 AT91F_SpiWrite(&desc
);
476 void program_lock_reg(int fl_nb
)
478 unsigned char rxf
[4];
479 unsigned char rx
[33];
480 if (test_fl_info(fl_nb
)) {
482 memset(rx
,0xff,sizeof(rx
));
483 desc
.tx_cmd_pt
= lock_unlock_cmd
;
484 desc
.rx_cmd_pt
= rxf
;
485 desc
.tx_cmd_size
= 4;
486 desc
.rx_cmd_size
= 4;
487 desc
.tx_data_size
= 32;
488 desc
.rx_data_size
= 32;
489 desc
.tx_data_pt
= rx
;
490 desc
.rx_data_pt
= rx
;
492 AT91F_SpiWrite(&desc
);
496 void flash_enable_lock(int fl_nb
)
499 if (test_fl_info(fl_nb
)) {
500 flash_en_lock_unlock(enable_lock_unlock_cmd
);
504 void flash_disable_lock(int fl_nb
)
507 if (test_fl_info(fl_nb
)) {
508 flash_en_lock_unlock(disable_lock_unlock_cmd
);