blob: a075ebd5d74d40080999e0bba84fddaffe61efeb [file] [log] [blame]
Peter Pearsedcbfd2e2007-08-14 10:14:05 +01001/* Driver for ATMEL DataFlash support
2 * Author : Hamid Ikdoumi (Atmel)
3 *
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.
8 *
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.
13 *
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,
17 * MA 02111-1307 USA
18 *
19 */
20
21#include <config.h>
22#include <common.h>
23
24#ifdef CONFIG_HAS_DATAFLASH
25#include <dataflash.h>
26
Peter Pearsee54b9702007-08-14 15:40:00 +010027/*
28 * spi.c API
29 */
Peter Pearse6e4bf9b2007-09-04 14:25:51 +010030extern unsigned int AT91F_SpiWrite(AT91PS_DataflashDesc pDesc);
31extern void AT91F_SpiEnable(int cs);
Peter Pearsedcbfd2e2007-08-14 10:14:05 +010032
33#define AT91C_TIMEOUT_WRDY 200000
34
Peter Pearsedcbfd2e2007-08-14 10:14:05 +010035/*----------------------------------------------------------------------*/
36/* \fn AT91F_DataFlashSendCommand */
37/* \brief Generic function to send a command to the dataflash */
Wolfgang Denkf01dbb52007-08-14 18:42:36 +020038/*----------------------------------------------------------------------*/
Peter Pearse6e4bf9b2007-09-04 14:25:51 +010039AT91S_DataFlashStatus AT91F_DataFlashSendCommand(AT91PS_DataFlash pDataFlash,
40 unsigned char OpCode,
41 unsigned int CmdSize,
42 unsigned int DataflashAddress)
Peter Pearsedcbfd2e2007-08-14 10:14:05 +010043{
44 unsigned int adr;
45
Peter Pearse6e4bf9b2007-09-04 14:25:51 +010046 if ((pDataFlash->pDataFlashDesc->state) != IDLE)
Peter Pearsedcbfd2e2007-08-14 10:14:05 +010047 return DATAFLASH_BUSY;
48
49 /* process the address to obtain page address and byte address */
Wolfgang Denkf01dbb52007-08-14 18:42:36 +020050 adr = ((DataflashAddress / (pDataFlash->pDevice->pages_size)) <<
Peter Pearsebd862202007-09-18 13:07:54 +010051 pDataFlash->pDevice->page_offset) +
52 (DataflashAddress % (pDataFlash->pDevice->pages_size));
Peter Pearsedcbfd2e2007-08-14 10:14:05 +010053
Peter Pearsebd862202007-09-18 13:07:54 +010054 /* fill the command buffer */
Peter Pearsedcbfd2e2007-08-14 10:14:05 +010055 pDataFlash->pDataFlashDesc->command[0] = OpCode;
56 if (pDataFlash->pDevice->pages_number >= 16384) {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +020057 pDataFlash->pDataFlashDesc->command[1] =
Peter Pearsebd862202007-09-18 13:07:54 +010058 (unsigned char)((adr & 0x0F000000) >> 24);
Wolfgang Denkf01dbb52007-08-14 18:42:36 +020059 pDataFlash->pDataFlashDesc->command[2] =
Peter Pearsebd862202007-09-18 13:07:54 +010060 (unsigned char)((adr & 0x00FF0000) >> 16);
Wolfgang Denkf01dbb52007-08-14 18:42:36 +020061 pDataFlash->pDataFlashDesc->command[3] =
Peter Pearsebd862202007-09-18 13:07:54 +010062 (unsigned char)((adr & 0x0000FF00) >> 8);
Wolfgang Denkf01dbb52007-08-14 18:42:36 +020063 pDataFlash->pDataFlashDesc->command[4] =
Peter Pearsebd862202007-09-18 13:07:54 +010064 (unsigned char)(adr & 0x000000FF);
Peter Pearsedcbfd2e2007-08-14 10:14:05 +010065 } else {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +020066 pDataFlash->pDataFlashDesc->command[1] =
Peter Pearsebd862202007-09-18 13:07:54 +010067 (unsigned char)((adr & 0x00FF0000) >> 16);
Wolfgang Denkf01dbb52007-08-14 18:42:36 +020068 pDataFlash->pDataFlashDesc->command[2] =
Peter Pearsebd862202007-09-18 13:07:54 +010069 (unsigned char)((adr & 0x0000FF00) >> 8);
Wolfgang Denkf01dbb52007-08-14 18:42:36 +020070 pDataFlash->pDataFlashDesc->command[3] =
Peter Pearsebd862202007-09-18 13:07:54 +010071 (unsigned char)(adr & 0x000000FF);
Peter Pearsedcbfd2e2007-08-14 10:14:05 +010072 pDataFlash->pDataFlashDesc->command[4] = 0;
73 }
74 pDataFlash->pDataFlashDesc->command[5] = 0;
75 pDataFlash->pDataFlashDesc->command[6] = 0;
76 pDataFlash->pDataFlashDesc->command[7] = 0;
77
78 /* Initialize the SpiData structure for the spi write fuction */
Peter Pearse6e4bf9b2007-09-04 14:25:51 +010079 pDataFlash->pDataFlashDesc->tx_cmd_pt =
Peter Pearsebd862202007-09-18 13:07:54 +010080 pDataFlash->pDataFlashDesc->command;
Peter Pearse6e4bf9b2007-09-04 14:25:51 +010081 pDataFlash->pDataFlashDesc->tx_cmd_size = CmdSize;
82 pDataFlash->pDataFlashDesc->rx_cmd_pt =
Peter Pearsebd862202007-09-18 13:07:54 +010083 pDataFlash->pDataFlashDesc->command;
Peter Pearse6e4bf9b2007-09-04 14:25:51 +010084 pDataFlash->pDataFlashDesc->rx_cmd_size = CmdSize;
Peter Pearsedcbfd2e2007-08-14 10:14:05 +010085
86 /* send the command and read the data */
Peter Pearse6e4bf9b2007-09-04 14:25:51 +010087 return AT91F_SpiWrite(pDataFlash->pDataFlashDesc);
88}
Peter Pearsedcbfd2e2007-08-14 10:14:05 +010089
90/*----------------------------------------------------------------------*/
91/* \fn AT91F_DataFlashGetStatus */
92/* \brief Read the status register of the dataflash */
Wolfgang Denkf01dbb52007-08-14 18:42:36 +020093/*----------------------------------------------------------------------*/
94AT91S_DataFlashStatus AT91F_DataFlashGetStatus(AT91PS_DataflashDesc pDesc)
Peter Pearsedcbfd2e2007-08-14 10:14:05 +010095{
96 AT91S_DataFlashStatus status;
97
98 /* if a transfert is in progress ==> return 0 */
Peter Pearse6e4bf9b2007-09-04 14:25:51 +010099 if ((pDesc->state) != IDLE)
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100100 return DATAFLASH_BUSY;
101
102 /* first send the read status command (D7H) */
103 pDesc->command[0] = DB_STATUS;
104 pDesc->command[1] = 0;
105
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100106 pDesc->DataFlash_state = GET_STATUS;
107 pDesc->tx_data_size = 0; /* Transmit the command */
108 /* and receive response */
109 pDesc->tx_cmd_pt = pDesc->command;
110 pDesc->rx_cmd_pt = pDesc->command;
111 pDesc->rx_cmd_size = 2;
112 pDesc->tx_cmd_size = 2;
113 status = AT91F_SpiWrite(pDesc);
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100114
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100115 pDesc->DataFlash_state = *((unsigned char *)(pDesc->rx_cmd_pt) + 1);
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100116
117 return status;
118}
119
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100120/*----------------------------------------------------------------------*/
121/* \fn AT91F_DataFlashWaitReady */
122/* \brief wait for dataflash ready (bit7 of the status register == 1) */
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200123/*----------------------------------------------------------------------*/
124AT91S_DataFlashStatus AT91F_DataFlashWaitReady(AT91PS_DataflashDesc
Peter Pearsebd862202007-09-18 13:07:54 +0100125 pDataFlashDesc,
126 unsigned int timeout)
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100127{
128 pDataFlashDesc->DataFlash_state = IDLE;
129
130 do {
131 AT91F_DataFlashGetStatus(pDataFlashDesc);
132 timeout--;
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100133 } while (((pDataFlashDesc->DataFlash_state & 0x80) != 0x80) &&
134 (timeout > 0));
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100135
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100136 if ((pDataFlashDesc->DataFlash_state & 0x80) != 0x80)
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100137 return DATAFLASH_ERROR;
138
139 return DATAFLASH_OK;
140}
141
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100142/*--------------------------------------------------------------------------*/
143/* Function Name : AT91F_DataFlashContinuousRead */
144/* Object : Continuous stream Read */
145/* Input Parameters : DataFlash Service */
146/* : <src> = dataflash address */
147/* : <*dataBuffer> = data buffer pointer */
148/* : <sizeToRead> = data buffer size */
149/* Return value : State of the dataflash */
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200150/*--------------------------------------------------------------------------*/
Peter Pearsebd862202007-09-18 13:07:54 +0100151AT91S_DataFlashStatus AT91F_DataFlashContinuousRead(
152 AT91PS_DataFlash pDataFlash,
153 int src,
154 unsigned char *dataBuffer,
155 int sizeToRead)
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100156{
157 AT91S_DataFlashStatus status;
158 /* Test the size to read in the device */
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100159 if ((src + sizeToRead) >
Peter Pearsebd862202007-09-18 13:07:54 +0100160 (pDataFlash->pDevice->pages_size *
161 (pDataFlash->pDevice->pages_number)))
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100162 return DATAFLASH_MEMORY_OVERFLOW;
163
164 pDataFlash->pDataFlashDesc->rx_data_pt = dataBuffer;
165 pDataFlash->pDataFlashDesc->rx_data_size = sizeToRead;
166 pDataFlash->pDataFlashDesc->tx_data_pt = dataBuffer;
167 pDataFlash->pDataFlashDesc->tx_data_size = sizeToRead;
168
Peter Pearsebd862202007-09-18 13:07:54 +0100169 status = AT91F_DataFlashSendCommand(
170 pDataFlash, DB_CONTINUOUS_ARRAY_READ, 8, src);
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100171 /* Send the command to the dataflash */
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100172 return (status);
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100173}
174
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100175/*---------------------------------------------------------------------------*/
176/* Function Name : AT91F_DataFlashPagePgmBuf */
177/* Object : Main memory page program thru buffer 1 or buffer 2 */
178/* Input Parameters : DataFlash Service */
179/* : <*src> = Source buffer */
180/* : <dest> = dataflash destination address */
181/* : <SizeToWrite> = data buffer size */
182/* Return value : State of the dataflash */
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200183/*---------------------------------------------------------------------------*/
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100184AT91S_DataFlashStatus AT91F_DataFlashPagePgmBuf(AT91PS_DataFlash pDataFlash,
185 unsigned char *src,
186 unsigned int dest,
187 unsigned int SizeToWrite)
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100188{
189 int cmdsize;
190 pDataFlash->pDataFlashDesc->tx_data_pt = src;
191 pDataFlash->pDataFlashDesc->tx_data_size = SizeToWrite;
192 pDataFlash->pDataFlashDesc->rx_data_pt = src;
193 pDataFlash->pDataFlashDesc->rx_data_size = SizeToWrite;
194
195 cmdsize = 4;
196 /* Send the command to the dataflash */
197 if (pDataFlash->pDevice->pages_number >= 16384)
198 cmdsize = 5;
Peter Pearsebd862202007-09-18 13:07:54 +0100199 return (AT91F_DataFlashSendCommand(
200 pDataFlash, DB_PAGE_PGM_BUF1, cmdsize, dest));
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100201}
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100202
203/*---------------------------------------------------------------------------*/
204/* Function Name : AT91F_MainMemoryToBufferTransfert */
205/* Object : Read a page in the SRAM Buffer 1 or 2 */
206/* Input Parameters : DataFlash Service */
207/* : Page concerned */
208/* : */
209/* Return value : State of the dataflash */
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200210/*---------------------------------------------------------------------------*/
Peter Pearsebd862202007-09-18 13:07:54 +0100211AT91S_DataFlashStatus AT91F_MainMemoryToBufferTransfert(
212 AT91PS_DataFlash
213 pDataFlash,
214 unsigned char
215 BufferCommand,
216 unsigned int page)
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100217{
218 int cmdsize;
219 /* Test if the buffer command is legal */
Peter Pearsebd862202007-09-18 13:07:54 +0100220 if ((BufferCommand != DB_PAGE_2_BUF1_TRF)&&
221 (BufferCommand != DB_PAGE_2_BUF2_TRF))
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100222 return DATAFLASH_BAD_COMMAND;
223
224 /* no data to transmit or receive */
225 pDataFlash->pDataFlashDesc->tx_data_size = 0;
226 cmdsize = 4;
227 if (pDataFlash->pDevice->pages_number >= 16384)
228 cmdsize = 5;
Peter Pearsebd862202007-09-18 13:07:54 +0100229 return (AT91F_DataFlashSendCommand(
230 pDataFlash, BufferCommand, cmdsize,
231 page * pDataFlash->pDevice->pages_size));
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100232}
233
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100234/*-------------------------------------------------------------------------- */
235/* Function Name : AT91F_DataFlashWriteBuffer */
236/* Object : Write data to the internal sram buffer 1 or 2 */
237/* Input Parameters : DataFlash Service */
238/* : <BufferCommand> = command to write buffer1 or 2 */
239/* : <*dataBuffer> = data buffer to write */
240/* : <bufferAddress> = address in the internal buffer */
241/* : <SizeToWrite> = data buffer size */
242/* Return value : State of the dataflash */
243/*---------------------------------------------------------------------------*/
Peter Pearsebd862202007-09-18 13:07:54 +0100244AT91S_DataFlashStatus AT91F_DataFlashWriteBuffer(
245 AT91PS_DataFlash pDataFlash,
246 unsigned char BufferCommand,
247 unsigned char *dataBuffer,
248 unsigned int bufferAddress,
249 int SizeToWrite)
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100250{
251 int cmdsize;
252 /* Test if the buffer command is legal */
Peter Pearsebd862202007-09-18 13:07:54 +0100253 if ((BufferCommand != DB_BUF1_WRITE) &&
254 (BufferCommand != DB_BUF2_WRITE))
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100255 return DATAFLASH_BAD_COMMAND;
256
257 /* buffer address must be lower than page size */
258 if (bufferAddress > pDataFlash->pDevice->pages_size)
259 return DATAFLASH_BAD_ADDRESS;
260
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100261 if ((pDataFlash->pDataFlashDesc->state) != IDLE)
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100262 return DATAFLASH_BUSY;
263
264 /* Send first Write Command */
265 pDataFlash->pDataFlashDesc->command[0] = BufferCommand;
266 pDataFlash->pDataFlashDesc->command[1] = 0;
267 if (pDataFlash->pDevice->pages_number >= 16384) {
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100268 pDataFlash->pDataFlashDesc->command[2] = 0;
269 pDataFlash->pDataFlashDesc->command[3] =
Peter Pearsebd862202007-09-18 13:07:54 +0100270 (unsigned char)(((unsigned int)(bufferAddress &
271 pDataFlash->pDevice->
272 byte_mask)) >> 8);
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100273 pDataFlash->pDataFlashDesc->command[4] =
Peter Pearsebd862202007-09-18 13:07:54 +0100274 (unsigned char)((unsigned int)bufferAddress & 0x00FF);
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100275 cmdsize = 5;
276 } else {
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100277 pDataFlash->pDataFlashDesc->command[2] =
Peter Pearsebd862202007-09-18 13:07:54 +0100278 (unsigned char)(((unsigned int)(bufferAddress &
279 pDataFlash->pDevice->
280 byte_mask)) >> 8);
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100281 pDataFlash->pDataFlashDesc->command[3] =
Peter Pearsebd862202007-09-18 13:07:54 +0100282 (unsigned char)((unsigned int)bufferAddress & 0x00FF);
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100283 pDataFlash->pDataFlashDesc->command[4] = 0;
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100284 cmdsize = 4;
285 }
286
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100287 pDataFlash->pDataFlashDesc->tx_cmd_pt =
Peter Pearsebd862202007-09-18 13:07:54 +0100288 pDataFlash->pDataFlashDesc->command;
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100289 pDataFlash->pDataFlashDesc->tx_cmd_size = cmdsize;
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100290 pDataFlash->pDataFlashDesc->rx_cmd_pt =
Peter Pearsebd862202007-09-18 13:07:54 +0100291 pDataFlash->pDataFlashDesc->command;
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100292 pDataFlash->pDataFlashDesc->rx_cmd_size = cmdsize;
293
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100294 pDataFlash->pDataFlashDesc->rx_data_pt = dataBuffer;
295 pDataFlash->pDataFlashDesc->tx_data_pt = dataBuffer;
296 pDataFlash->pDataFlashDesc->rx_data_size = SizeToWrite;
297 pDataFlash->pDataFlashDesc->tx_data_size = SizeToWrite;
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100298
299 return AT91F_SpiWrite(pDataFlash->pDataFlashDesc);
300}
301
302/*---------------------------------------------------------------------------*/
303/* Function Name : AT91F_PageErase */
304/* Object : Erase a page */
305/* Input Parameters : DataFlash Service */
306/* : Page concerned */
307/* : */
308/* Return value : State of the dataflash */
309/*---------------------------------------------------------------------------*/
Peter Pearsebd862202007-09-18 13:07:54 +0100310AT91S_DataFlashStatus AT91F_PageErase(
311 AT91PS_DataFlash pDataFlash,
312 unsigned int page)
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100313{
314 int cmdsize;
315 /* Test if the buffer command is legal */
316 /* no data to transmit or receive */
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100317 pDataFlash->pDataFlashDesc->tx_data_size = 0;
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100318
319 cmdsize = 4;
320 if (pDataFlash->pDevice->pages_number >= 16384)
321 cmdsize = 5;
Peter Pearsebd862202007-09-18 13:07:54 +0100322 return (AT91F_DataFlashSendCommand(pDataFlash,
323 DB_PAGE_ERASE, cmdsize,
324 page * pDataFlash->pDevice->pages_size));
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100325}
326
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100327/*---------------------------------------------------------------------------*/
328/* Function Name : AT91F_BlockErase */
329/* Object : Erase a Block */
330/* Input Parameters : DataFlash Service */
331/* : Page concerned */
332/* : */
333/* Return value : State of the dataflash */
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200334/*---------------------------------------------------------------------------*/
Peter Pearsebd862202007-09-18 13:07:54 +0100335AT91S_DataFlashStatus AT91F_BlockErase(
336 AT91PS_DataFlash pDataFlash,
337 unsigned int block)
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100338{
339 int cmdsize;
340 /* Test if the buffer command is legal */
341 /* no data to transmit or receive */
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100342 pDataFlash->pDataFlashDesc->tx_data_size = 0;
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100343 cmdsize = 4;
344 if (pDataFlash->pDevice->pages_number >= 16384)
345 cmdsize = 5;
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100346 return (AT91F_DataFlashSendCommand(pDataFlash, DB_BLOCK_ERASE, cmdsize,
Peter Pearsebd862202007-09-18 13:07:54 +0100347 block * 8 *
348 pDataFlash->pDevice->pages_size));
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100349}
350
351/*---------------------------------------------------------------------------*/
352/* Function Name : AT91F_WriteBufferToMain */
353/* Object : Write buffer to the main memory */
354/* Input Parameters : DataFlash Service */
355/* : <BufferCommand> = command to send to buffer1 or buffer2 */
356/* : <dest> = main memory address */
357/* Return value : State of the dataflash */
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200358/*---------------------------------------------------------------------------*/
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100359AT91S_DataFlashStatus AT91F_WriteBufferToMain(AT91PS_DataFlash pDataFlash,
Peter Pearsebd862202007-09-18 13:07:54 +0100360 unsigned char BufferCommand,
361 unsigned int dest)
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100362{
363 int cmdsize;
364 /* Test if the buffer command is correct */
365 if ((BufferCommand != DB_BUF1_PAGE_PGM) &&
Peter Pearsebd862202007-09-18 13:07:54 +0100366 (BufferCommand != DB_BUF1_PAGE_ERASE_PGM) &&
367 (BufferCommand != DB_BUF2_PAGE_PGM) &&
368 (BufferCommand != DB_BUF2_PAGE_ERASE_PGM))
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100369 return DATAFLASH_BAD_COMMAND;
370
371 /* no data to transmit or receive */
372 pDataFlash->pDataFlashDesc->tx_data_size = 0;
373
374 cmdsize = 4;
375 if (pDataFlash->pDevice->pages_number >= 16384)
376 cmdsize = 5;
377 /* Send the command to the dataflash */
Peter Pearsebd862202007-09-18 13:07:54 +0100378 return (AT91F_DataFlashSendCommand(pDataFlash, BufferCommand,
379 cmdsize, dest));
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100380}
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100381
382/*---------------------------------------------------------------------------*/
383/* Function Name : AT91F_PartialPageWrite */
384/* Object : Erase partielly a page */
385/* Input Parameters : <page> = page number */
386/* : <AdrInpage> = adr to begin the fading */
387/* : <length> = Number of bytes to erase */
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200388/*---------------------------------------------------------------------------*/
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100389AT91S_DataFlashStatus AT91F_PartialPageWrite(AT91PS_DataFlash pDataFlash,
Peter Pearsebd862202007-09-18 13:07:54 +0100390 unsigned char *src,
391 unsigned int dest,
392 unsigned int size)
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100393{
394 unsigned int page;
395 unsigned int AdrInPage;
396
397 page = dest / (pDataFlash->pDevice->pages_size);
398 AdrInPage = dest % (pDataFlash->pDevice->pages_size);
399
400 /* Read the contents of the page in the Sram Buffer */
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100401 AT91F_MainMemoryToBufferTransfert(pDataFlash, DB_PAGE_2_BUF1_TRF, page);
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200402 AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc,
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100403 AT91C_TIMEOUT_WRDY);
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100404 /*Update the SRAM buffer */
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200405 AT91F_DataFlashWriteBuffer(pDataFlash, DB_BUF1_WRITE, src,
Peter Pearsebd862202007-09-18 13:07:54 +0100406 AdrInPage, size);
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100407
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200408 AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc,
Peter Pearsebd862202007-09-18 13:07:54 +0100409 AT91C_TIMEOUT_WRDY);
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100410
411 /* Erase page if a 128 Mbits device */
412 if (pDataFlash->pDevice->pages_number >= 16384) {
413 AT91F_PageErase(pDataFlash, page);
414 /* Rewrite the modified Sram Buffer in the main memory */
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200415 AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc,
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100416 AT91C_TIMEOUT_WRDY);
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100417 }
418
419 /* Rewrite the modified Sram Buffer in the main memory */
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100420 return (AT91F_WriteBufferToMain(pDataFlash, DB_BUF1_PAGE_ERASE_PGM,
421 (page *
422 pDataFlash->pDevice->pages_size)));
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100423}
424
425/*---------------------------------------------------------------------------*/
426/* Function Name : AT91F_DataFlashWrite */
427/* Object : */
428/* Input Parameters : <*src> = Source buffer */
429/* : <dest> = dataflash adress */
430/* : <size> = data buffer size */
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200431/*---------------------------------------------------------------------------*/
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100432AT91S_DataFlashStatus AT91F_DataFlashWrite(AT91PS_DataFlash pDataFlash,
Peter Pearsebd862202007-09-18 13:07:54 +0100433 unsigned char *src,
434 int dest, int size)
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100435{
436 unsigned int length;
437 unsigned int page;
438 unsigned int status;
439
440 AT91F_SpiEnable(pDataFlash->pDevice->cs);
441
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100442 if ((dest + size) > (pDataFlash->pDevice->pages_size *
Peter Pearsebd862202007-09-18 13:07:54 +0100443 (pDataFlash->pDevice->pages_number)))
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100444 return DATAFLASH_MEMORY_OVERFLOW;
445
446 /* If destination does not fit a page start address */
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100447 if ((dest % ((unsigned int)(pDataFlash->pDevice->pages_size))) != 0) {
Peter Pearsebd862202007-09-18 13:07:54 +0100448 length =
449 pDataFlash->pDevice->pages_size -
450 (dest % ((unsigned int)(pDataFlash->pDevice->pages_size)));
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100451
452 if (size < length)
453 length = size;
454
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100455 if (!AT91F_PartialPageWrite(pDataFlash, src, dest, length))
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100456 return DATAFLASH_ERROR;
457
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200458 AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc,
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100459 AT91C_TIMEOUT_WRDY);
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100460
461 /* Update size, source and destination pointers */
462 size -= length;
463 dest += length;
464 src += length;
465 }
466
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100467 while ((size - pDataFlash->pDevice->pages_size) >= 0) {
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100468 /* program dataflash page */
469 page = (unsigned int)dest / (pDataFlash->pDevice->pages_size);
470
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200471 status = AT91F_DataFlashWriteBuffer(pDataFlash,
Peter Pearsebd862202007-09-18 13:07:54 +0100472 DB_BUF1_WRITE, src, 0,
473 pDataFlash->pDevice->
474 pages_size);
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200475 AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc,
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100476 AT91C_TIMEOUT_WRDY);
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100477
478 status = AT91F_PageErase(pDataFlash, page);
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200479 AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc,
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100480 AT91C_TIMEOUT_WRDY);
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100481 if (!status)
482 return DATAFLASH_ERROR;
483
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100484 status = AT91F_WriteBufferToMain(pDataFlash,
Peter Pearsebd862202007-09-18 13:07:54 +0100485 DB_BUF1_PAGE_PGM, dest);
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100486 if (!status)
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100487 return DATAFLASH_ERROR;
488
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200489 AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc,
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100490 AT91C_TIMEOUT_WRDY);
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100491
492 /* Update size, source and destination pointers */
493 size -= pDataFlash->pDevice->pages_size;
494 dest += pDataFlash->pDevice->pages_size;
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100495 src += pDataFlash->pDevice->pages_size;
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100496 }
497
498 /* If still some bytes to read */
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100499 if (size > 0) {
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100500 /* program dataflash page */
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100501 if (!AT91F_PartialPageWrite(pDataFlash, src, dest, size))
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100502 return DATAFLASH_ERROR;
503
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200504 AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc,
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100505 AT91C_TIMEOUT_WRDY);
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100506 }
507 return DATAFLASH_OK;
508}
509
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100510/*---------------------------------------------------------------------------*/
511/* Function Name : AT91F_DataFlashRead */
512/* Object : Read a block in dataflash */
513/* Input Parameters : */
514/* Return value : */
515/*---------------------------------------------------------------------------*/
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100516int AT91F_DataFlashRead(AT91PS_DataFlash pDataFlash,
517 unsigned long addr, unsigned long size, char *buffer)
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100518{
519 unsigned long SizeToRead;
520
521 AT91F_SpiEnable(pDataFlash->pDevice->cs);
522
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100523 if (AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc,
Peter Pearsebd862202007-09-18 13:07:54 +0100524 AT91C_TIMEOUT_WRDY) != DATAFLASH_OK)
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100525 return -1;
526
527 while (size) {
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100528 SizeToRead = (size < 0x8000) ? size : 0x8000;
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100529
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200530 if (AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc,
Peter Pearsebd862202007-09-18 13:07:54 +0100531 AT91C_TIMEOUT_WRDY) !=
532 DATAFLASH_OK)
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100533 return -1;
534
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100535 if (AT91F_DataFlashContinuousRead(pDataFlash, addr,
Peter Pearsebd862202007-09-18 13:07:54 +0100536 (uchar *) buffer,
537 SizeToRead) != DATAFLASH_OK)
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100538 return -1;
539
540 size -= SizeToRead;
541 addr += SizeToRead;
542 buffer += SizeToRead;
543 }
544
545 return DATAFLASH_OK;
546}
547
Peter Pearse0c42f362007-08-14 10:46:32 +0100548/*---------------------------------------------------------------------------*/
549/* Function Name : AT91F_DataflashProbe */
550/* Object : */
551/* Input Parameters : */
552/* Return value : Dataflash status register */
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200553/*---------------------------------------------------------------------------*/
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100554int AT91F_DataflashProbe(int cs, AT91PS_DataflashDesc pDesc)
555{
Peter Pearse0c42f362007-08-14 10:46:32 +0100556 AT91F_SpiEnable(cs);
557 AT91F_DataFlashGetStatus(pDesc);
Peter Pearse6e4bf9b2007-09-04 14:25:51 +0100558 return ((pDesc->command[1] == 0xFF) ? 0 : pDesc->command[1] & 0x3C);
Peter Pearse0c42f362007-08-14 10:46:32 +0100559}
Peter Pearse0c42f362007-08-14 10:46:32 +0100560#endif