Probe memory to find 32 64 or 128 M
[romboot.git] / dataflash.cpp
blob8dee812d6e745b31cf91c948820dab95c265f591
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"
16 #include "com.h"
18 AT91S_DATAFLASH_INFO dataflash_info[CFG_MAX_DATAFLASH_BANKS];
19 static AT91S_DataFlash DataFlashInst;
21 int cs[][CFG_MAX_DATAFLASH_BANKS] = {
22 {CFG_DATAFLASH_LOGIC_ADDR_CS0, 0}, /* Logical adress, CS */
23 {CFG_DATAFLASH_LOGIC_ADDR_CS3, 3}
26 int AT91F_DataflashInit (void)
28 int i;
29 int dfcode;
31 AT91F_SpiInit ();
33 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
34 dataflash_info[i].Desc.state = IDLE;
35 dataflash_info[i].id = 0;
36 dataflash_info[i].Device.pages_number = 0;
37 dfcode = AT91F_DataflashProbe (cs[i][1], &dataflash_info[i].Desc);
39 switch (dfcode) {
40 case AT45DB161:
41 dataflash_info[i].Device.pages_number = 4096;
42 dataflash_info[i].Device.pages_size = 528;
43 dataflash_info[i].Device.page_offset = 10;
44 dataflash_info[i].Device.byte_mask = 0x300;
45 dataflash_info[i].Device.cs = cs[i][1];
46 dataflash_info[i].Desc.DataFlash_state = IDLE;
47 dataflash_info[i].logical_address = cs[i][0];
48 dataflash_info[i].id = dfcode;
49 break;
51 case AT45DB321:
52 dataflash_info[i].Device.pages_number = 8192;
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;
60 break;
62 case AT45DB642:
63 dataflash_info[i].Device.pages_number = 8192;
64 dataflash_info[i].Device.pages_size = 1056;
65 dataflash_info[i].Device.page_offset = 11;
66 dataflash_info[i].Device.byte_mask = 0x700;
67 dataflash_info[i].Device.cs = cs[i][1];
68 dataflash_info[i].Desc.DataFlash_state = IDLE;
69 dataflash_info[i].logical_address = cs[i][0];
70 dataflash_info[i].id = dfcode;
71 break;
72 case AT45DB128:
73 dataflash_info[i].Device.pages_number = 16384;
74 dataflash_info[i].Device.pages_size = 1056;
75 dataflash_info[i].Device.page_offset = 11;
76 dataflash_info[i].Device.byte_mask = 0x700;
77 dataflash_info[i].Device.cs = cs[i][1];
78 dataflash_info[i].Desc.DataFlash_state = IDLE;
79 dataflash_info[i].logical_address = cs[i][0];
80 dataflash_info[i].id = dfcode;
81 break;
83 default:
84 break;
87 return (1);
91 void AT91F_DataflashPrintInfo(void)
93 int i;
95 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
96 if (dataflash_info[i].id != 0) {
97 printf ("DataFlash:");
98 switch (dataflash_info[i].id) {
99 case AT45DB161:
100 printf ("AT45DB161\n\r");
101 break;
103 case AT45DB321:
104 printf ("AT45DB321\n\r");
105 break;
107 case AT45DB642:
108 printf ("AT45DB642\n\r");
109 break;
110 case AT45DB128:
111 printf ("AT45DB128\n\r");
112 break;
115 printf ("Nb pages: %6d\n\r"
116 "Page Size: %6d\n\r"
117 "Size=%8d bytes\n\r"
118 "Logical address: 0x%08X\n\r",
119 (unsigned int) dataflash_info[i].Device.pages_number,
120 (unsigned int) dataflash_info[i].Device.pages_size,
121 (unsigned int) dataflash_info[i].Device.pages_number *
122 dataflash_info[i].Device.pages_size,
123 (unsigned int) dataflash_info[i].logical_address);
129 /*------------------------------------------------------------------------------*/
130 /* Function Name : AT91F_DataflashSelect */
131 /* Object : Select the correct device */
132 /*------------------------------------------------------------------------------*/
133 AT91PS_DataFlash AT91F_DataflashSelect (AT91PS_DataFlash pFlash,
134 unsigned int *addr)
136 char addr_valid = 0;
137 int i;
139 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++)
140 if ((*addr & 0xFF000000) == dataflash_info[i].logical_address) {
141 addr_valid = 1;
142 break;
144 if (!addr_valid) {
145 pFlash = (AT91PS_DataFlash) 0;
146 return pFlash;
148 pFlash->pDataFlashDesc = &(dataflash_info[i].Desc);
149 pFlash->pDevice = &(dataflash_info[i].Device);
150 *addr -= dataflash_info[i].logical_address;
151 return (pFlash);
155 /*------------------------------------------------------------------------------*/
156 /* Function Name : addr_dataflash */
157 /* Object : Test if address is valid */
158 /*------------------------------------------------------------------------------*/
159 int addr_dataflash (unsigned long addr)
161 int addr_valid = 0;
162 int i;
164 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
165 if ((((int) addr) & 0xFF000000) ==
166 dataflash_info[i].logical_address) {
167 addr_valid = 1;
168 break;
172 return addr_valid;
175 /*------------------------------------------------------------------------------*/
176 /* Function Name : read_dataflash */
177 /* Object : dataflash memory read */
178 /*------------------------------------------------------------------------------*/
179 int read_dataflash (unsigned long addr, unsigned long size, char *result)
181 unsigned int AddrToRead = addr;
182 AT91PS_DataFlash pFlash = &DataFlashInst;
184 pFlash = AT91F_DataflashSelect (pFlash, &AddrToRead);
185 if (pFlash == 0)
186 return -1;
188 return (AT91F_DataFlashRead (pFlash, AddrToRead, size, result));
192 /*-----------------------------------------------------------------------------*/
193 /* Function Name : write_dataflash */
194 /* Object : write a block in dataflash */
195 /*-----------------------------------------------------------------------------*/
196 int write_dataflash (unsigned long addr_dest, unsigned int addr_src,
197 unsigned int size)
199 unsigned int AddrToWrite = addr_dest;
200 AT91PS_DataFlash pFlash = &DataFlashInst;
202 pFlash = AT91F_DataflashSelect (pFlash, &AddrToWrite);
203 if (AddrToWrite == -1)
204 return -1;
206 return AT91F_DataFlashWrite (pFlash, (unsigned char *) addr_src, AddrToWrite, size);