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
;
88 printf("\n\rflash nb %d found",i
);
89 dataflash_info
[i
].Device
.pages_number
= 16384;
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
;
100 printf("\n\rflash nb %d not found",i
);
111 if ((dataflash_info
[2].id
!= 0) && (dataflash_info
[3].id
!= 0 )){
112 return SIZE_REGS_128M
;
115 return SIZE_REGS_64M
;
119 void AT91F_DataflashPrintInfo(void)
124 for (i
= 0; i
< CFG_MAX_DATAFLASH_BANKS
; i
++) {
125 if (dataflash_info
[i
].id
!= 0) {
127 switch (dataflash_info
[i
].id
) {
128 #ifndef REDUCE_CODE_SIZE
130 printf ("AT45DB161\n\r");
134 printf ("AT45DB321\n\r");
138 printf ("AT45DB642\n\r");
140 #ifndef REDUCE_CODE_SIZE
142 printf ("AT45DB128\n\r");
146 printf ("Nb pages: %6d\n\r"
149 "Logical addr: 0x%08X\n\r",
150 (unsigned int) dataflash_info
[i
].Device
.pages_number
,
151 (unsigned int) dataflash_info
[i
].Device
.pages_size
,
152 (unsigned int) dataflash_info
[i
].Device
.pages_number
*
153 dataflash_info
[i
].Device
.pages_size
,
154 (unsigned int) dataflash_info
[i
].logical_address
);
160 /*------------------------------------------------------------------------------*/
161 /* Function Name : AT91F_DataflashSelect */
162 /* Object : Select the correct device */
163 /*------------------------------------------------------------------------------*/
164 AT91PS_DataFlash
AT91F_DataflashSelect (AT91PS_DataFlash pFlash
,
170 for (i
= 0; i
< CFG_MAX_DATAFLASH_BANKS
; i
++)
171 if ((*addr
& 0xFF000000) == dataflash_info
[i
].logical_address
) {
176 pFlash
= (AT91PS_DataFlash
) 0;
179 pFlash
->pDataFlashDesc
= &(dataflash_info
[i
].Desc
);
180 pFlash
->pDevice
= &(dataflash_info
[i
].Device
);
181 *addr
-= dataflash_info
[i
].logical_address
;
186 /*------------------------------------------------------------------------------*/
187 /* Function Name : addr_dataflash */
188 /* Object : Test if address is valid */
189 /*------------------------------------------------------------------------------*/
190 int addr_dataflash (unsigned long addr
)
195 for (i
= 0; i
< CFG_MAX_DATAFLASH_BANKS
; i
++) {
196 if ((((int) addr
) & 0xFF000000) ==
197 dataflash_info
[i
].logical_address
) {
206 /*------------------------------------------------------------------------------*/
207 /* Function Name : read_dataflash */
208 /* Object : dataflash memory read */
209 /*------------------------------------------------------------------------------*/
210 int read_dataflash (unsigned long addr
, unsigned long size
, char *result
)
212 unsigned int AddrToRead
= addr
;
213 // AT91F_DataflashPrintInfo();
214 AT91PS_DataFlash pFlash
= &DataFlashInst
;
216 pFlash
= AT91F_DataflashSelect (pFlash
, &AddrToRead
);
220 return (AT91F_DataFlashRead (pFlash
, AddrToRead
, size
, result
));
224 /*-----------------------------------------------------------------------------*/
225 /* Function Name : write_dataflash */
226 /* Object : write a block in dataflash */
227 /*-----------------------------------------------------------------------------*/
228 int write_dataflash (unsigned long addr_dest
, unsigned int addr_src
,
231 unsigned int AddrToWrite
= addr_dest
;
232 AT91PS_DataFlash pFlash
= &DataFlashInst
;
234 pFlash
= AT91F_DataflashSelect (pFlash
, &AddrToWrite
);
235 if (AddrToWrite
== -1)
238 return AT91F_DataFlashWrite (pFlash
, (unsigned char *) addr_src
, AddrToWrite
, size
);
242 int erase_dataflash(int nb
)
244 AT91PS_DataFlash pFlash
= &DataFlashInst
;
245 AT91S_DataFlashStatus status
;
248 if ( dataflash_info
[nb
].id
!= 0) {
249 AT91F_SpiEnable(pFlash
->pDevice
->cs
);
251 pFlash
->pDataFlashDesc
= &(dataflash_info
[nb
].Desc
);
252 pFlash
->pDevice
= &(dataflash_info
[nb
].Device
);
253 for ( page_nb
= 0 ; page_nb
< dataflash_info
[nb
].Device
.pages_number
; page_nb
++) {
254 if ( page_nb
% 16 == 0)
255 printf("Erase page nb %d\r\n",page_nb
);
256 status
= AT91F_PageErase(pFlash
,page_nb
);
257 AT91F_DataFlashWaitReady(pFlash
->pDataFlashDesc
, AT91C_DATAFLASH_TIMEOUT
);
259 printf("Erase page nb %d error\n",page_nb
);
260 return AT91C_DATAFLASH_ERROR
;
270 * Le sector protection register ( spr) contient la liste des secteurs de dataflash
273 * On efface le spr par la cmd
275 * Erase Sector Protection Register 3DH 2AH 7FH CFH
277 * On programme le spr par
279 * Program Sector Protection Register 3DH 2AH 7FH FCH
281 * avec dans les bytes suivants la protection des secteurs.
282 * Cette protection peut être activée par la command
284 * Enable Sector Protection 3DH 2AH 7FH A9H
286 * Le conte,u du registre peut etre lu par
288 * Read Sector Protection Register 32H xxH xxH xxH
292 * Disable Sector Protection 3DH 2AH 7FH 9AH
295 void memset(unsigned char *ptr
,char val
, int size
)
298 for ( i
= 0 ; i
< size
; i
++) {
304 static void clear_desc( AT91S_DataflashDesc
*d
)
306 memset((unsigned char *)d
,0,sizeof(AT91S_DataflashDesc
));
310 AT91S_DataflashDesc desc
;
312 t_read_lock_cmd read_lock_cmd_sector
=
321 t_read_lock_cmd read_lock_cmd_lockdown
=
330 t_read_lock_cmd read_lock_cmd_security
=
339 void AT91F_SpiEnable(int cs
);
341 void send_spi(int fl_nb
)
343 AT91PS_DataFlash pFlash
= &DataFlashInst
;
344 if (dataflash_info
[fl_nb
].id
!= 0) {
345 AT91F_SpiEnable(cs
[fl_nb
][1]);
346 AT91F_SpiWrite(&desc
);
347 pFlash
->pDataFlashDesc
= &(dataflash_info
[fl_nb
].Desc
);
348 AT91F_DataFlashWaitReady(pFlash
->pDataFlashDesc
, AT91C_DATAFLASH_TIMEOUT
);
352 void read_lock_reg(t_read_lock_cmd
*ptrcmd
)
355 unsigned char rx
[129];
359 for ( fl_nb
= 0 ; fl_nb
< CFG_MAX_DATAFLASH_BANKS
; fl_nb
++) {
363 memset(rx
,0,sizeof(rx
));
365 desc
.tx_cmd_pt
= ptrcmd
->cmd
;
367 desc
.tx_cmd_size
= 4;
368 desc
.rx_cmd_size
= 4;
369 desc
.tx_data_size
= ptrcmd
->txsize
;
370 desc
.rx_data_size
= ptrcmd
->rxsize
;
371 desc
.tx_data_pt
= rx
;
372 desc
.rx_data_pt
= rx
;
375 if (dataflash_info
[fl_nb
].id
!= 0) {
376 if ( ptrcmd
== &read_lock_cmd_sector
) {
377 printf("Lock status reg ");
379 if ( ptrcmd
== &read_lock_cmd_lockdown
) {
380 printf("lockdown status reg ");
382 if ( ptrcmd
== &read_lock_cmd_security
) {
383 printf("security reg ");
385 for ( i
= 0 ; i
< ptrcmd
->rxsize
; i
++ ) {
389 printf("0x%02x:",rx
[i
]);
396 unsigned char clear_lock_cmd
[4]=
402 unsigned char lock_unlock_cmd
[4]=
407 unsigned char enable_lock_unlock_cmd
[4]=
412 unsigned char disable_lock_unlock_cmd
[4]=
417 void flash_en_lock_unlock(int fl_nb
,unsigned char *cmd
)
419 unsigned char rxf
[4];
420 unsigned char rx
[33];
422 desc
.tx_cmd_pt
= cmd
;
423 desc
.rx_cmd_pt
= rxf
;
424 desc
.tx_cmd_size
= 4;
425 desc
.rx_cmd_size
= 4;
429 void write_lock_reg(int fl_nb
,int val
)
431 unsigned char rxf
[4];
432 unsigned char rx
[33];
433 memset(rx
,val
,sizeof(rx
));
435 desc
.tx_cmd_pt
= lock_unlock_cmd
;
436 desc
.rx_cmd_pt
= rxf
;
437 desc
.tx_cmd_size
= 4;
438 desc
.rx_cmd_size
= 4;
439 desc
.tx_data_size
= 32;
440 desc
.rx_data_size
= 32;
441 desc
.tx_data_pt
= rx
;
442 desc
.rx_data_pt
= rx
;
446 void erase_lock_reg(int fl_nb
)
449 memset(rx
,0,sizeof(rx
));
451 desc
.tx_cmd_pt
= clear_lock_cmd
;
453 desc
.tx_cmd_size
= 4;
454 desc
.rx_cmd_size
= 4;
458 void set_lock_reg(int fl_nb
)
460 erase_lock_reg(fl_nb
);
461 write_lock_reg(fl_nb
,0xff);
464 void clr_lock_reg(int fl_nb
)
466 erase_lock_reg(fl_nb
);
467 write_lock_reg(fl_nb
,0);
470 void flash_enable_lock(int fl_nb
)
472 flash_en_lock_unlock(fl_nb
,enable_lock_unlock_cmd
);
475 void flash_disable_lock(int fl_nb
)
477 flash_en_lock_unlock(fl_nb
,disable_lock_unlock_cmd
);