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 extern void AT91F_SpiInit (void);
35 extern int AT91F_DataflashProbe (int i, AT91PS_DataflashDesc pDesc);
36 extern int AT91F_DataFlashRead (AT91PS_DataFlash pDataFlash,
38 unsigned long size, char *buffer);
41 int AT91F_DataflashInit (void)
48 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
50 dataflash_info[i].id = 0;
51 dataflash_info[i].Device.pages_number = 0;
52 dfcode = AT91F_DataflashProbe (cs[i][1], &dataflash_info[i].Desc);
56 dataflash_info[i].Device.pages_number = 4096;
57 dataflash_info[i].Device.pages_size = 528;
58 dataflash_info[i].Device.page_offset = 10;
59 dataflash_info[i].Device.byte_mask = 0x300;
60 dataflash_info[i].Device.cs = cs[i][1];
61 dataflash_info[i].Desc.DataFlash_state = IDLE;
62 dataflash_info[i].logical_address = cs[i][0];
63 dataflash_info[i].id = dfcode;
67 dataflash_info[i].Device.pages_number = 8192;
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 = 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;
92 for (j = 0; j < dataflash_info[i].Device.pages_number; j++)
93 dataflash_info[i].protect[j] = FLAG_PROTECT_SET;
100 void dataflash_print_info (void)
104 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
105 if (dataflash_info[i].id != 0) {
106 printf ("DataFlash:");
107 switch (dataflash_info[i].id) {
109 printf ("AT45DB161\n");
113 printf ("AT45DB321\n");
117 printf ("AT45DB642\n");
121 printf ("Nb pages: %6d\n"
124 "Logical address: 0x%08X\n",
125 (unsigned int) dataflash_info[i].Device.pages_number,
126 (unsigned int) dataflash_info[i].Device.pages_size,
127 (unsigned int) dataflash_info[i].Device.pages_number *
128 dataflash_info[i].Device.pages_size,
129 (unsigned int) dataflash_info[i].logical_address);
135 /*------------------------------------------------------------------------------*/
136 /* Function Name : AT91F_DataflashSelect */
137 /* Object : Select the correct device */
138 /*------------------------------------------------------------------------------*/
139 AT91PS_DataFlash AT91F_DataflashSelect (AT91PS_DataFlash pFlash,
145 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++)
146 if ((*addr & 0xFF000000) == dataflash_info[i].logical_address) {
151 pFlash = (AT91PS_DataFlash) 0;
154 pFlash->pDataFlashDesc = &(dataflash_info[i].Desc);
155 pFlash->pDevice = &(dataflash_info[i].Device);
156 *addr -= dataflash_info[i].logical_address;
160 /*------------------------------------------------------------------------------*/
161 /* Function Name : addr_dataflash */
162 /* Object : Test if address is valid */
163 /*------------------------------------------------------------------------------*/
164 int addr_dataflash (unsigned long addr)
169 for (i = 0; i < CFG_MAX_DATAFLASH_BANKS; i++) {
170 if ((((int) addr) & 0xFF000000) ==
171 dataflash_info[i].logical_address) {
180 /*------------------------------------------------------------------------------*/
181 /* Function Name : read_dataflash */
182 /* Object : dataflash memory read */
183 /*------------------------------------------------------------------------------*/
184 int read_dataflash (unsigned long addr, unsigned long size, char *result)
186 int AddrToRead = addr;
187 AT91PS_DataFlash pFlash = &DataFlashInst;
189 pFlash = AT91F_DataflashSelect (pFlash, &AddrToRead);
193 return (AT91F_DataFlashRead (pFlash, AddrToRead, size, result));
197 /*-----------------------------------------------------------------------------*/
198 /* Function Name : write_dataflash */
199 /* Object : write a block in dataflash */
200 /*-----------------------------------------------------------------------------*/
201 int write_dataflash (unsigned long addr_dest, unsigned long addr_src,
204 extern AT91S_DataFlashStatus AT91F_DataFlashWrite(
205 AT91PS_DataFlash, uchar *, int, int);
206 int AddrToWrite = addr_dest;
207 AT91PS_DataFlash pFlash = &DataFlashInst;
209 pFlash = AT91F_DataflashSelect (pFlash, &AddrToWrite);
210 if (AddrToWrite == -1)
213 return AT91F_DataFlashWrite (pFlash, (char *) addr_src, AddrToWrite,
218 void dataflash_perror (int err)
224 printf ("Timeout writing to DataFlash\n");
227 printf ("Can't write to protected DataFlash sectors\n");
230 printf ("Outside available DataFlash\n");
232 case ERR_UNKNOWN_FLASH_TYPE:
233 printf ("Unknown Type of DataFlash\n");
236 printf ("General DataFlash Programming Error\n");
239 printf ("%s[%d] FIXME: rc=%d\n", __FILE__, __LINE__, err);