blob: 507ff36d478367a63c64236da979977c891f3e6b [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 */
30extern 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
35
36/*----------------------------------------------------------------------*/
37/* \fn AT91F_DataFlashSendCommand */
38/* \brief Generic function to send a command to the dataflash */
Wolfgang Denkf01dbb52007-08-14 18:42:36 +020039/*----------------------------------------------------------------------*/
Peter Pearsedcbfd2e2007-08-14 10:14:05 +010040AT91S_DataFlashStatus AT91F_DataFlashSendCommand(
41 AT91PS_DataFlash pDataFlash,
42 unsigned char OpCode,
43 unsigned int CmdSize,
44 unsigned int DataflashAddress)
45{
46 unsigned int adr;
47
48 if ( (pDataFlash->pDataFlashDesc->state) != IDLE)
49 return DATAFLASH_BUSY;
50
51 /* process the address to obtain page address and byte address */
Wolfgang Denkf01dbb52007-08-14 18:42:36 +020052 adr = ((DataflashAddress / (pDataFlash->pDevice->pages_size)) <<
53 pDataFlash->pDevice->page_offset) + (DataflashAddress %
Peter Pearsedcbfd2e2007-08-14 10:14:05 +010054 (pDataFlash->pDevice->pages_size));
55
56 /* fill the command buffer */
57 pDataFlash->pDataFlashDesc->command[0] = OpCode;
58 if (pDataFlash->pDevice->pages_number >= 16384) {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +020059 pDataFlash->pDataFlashDesc->command[1] =
Peter Pearsedcbfd2e2007-08-14 10:14:05 +010060 (unsigned char)((adr & 0x0F000000) >> 24);
Wolfgang Denkf01dbb52007-08-14 18:42:36 +020061 pDataFlash->pDataFlashDesc->command[2] =
Peter Pearsedcbfd2e2007-08-14 10:14:05 +010062 (unsigned char)((adr & 0x00FF0000) >> 16);
Wolfgang Denkf01dbb52007-08-14 18:42:36 +020063 pDataFlash->pDataFlashDesc->command[3] =
Peter Pearsedcbfd2e2007-08-14 10:14:05 +010064 (unsigned char)((adr & 0x0000FF00) >> 8);
Wolfgang Denkf01dbb52007-08-14 18:42:36 +020065 pDataFlash->pDataFlashDesc->command[4] =
Peter Pearsedcbfd2e2007-08-14 10:14:05 +010066 (unsigned char)(adr & 0x000000FF);
67 } else {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +020068 pDataFlash->pDataFlashDesc->command[1] =
Peter Pearsedcbfd2e2007-08-14 10:14:05 +010069 (unsigned char)((adr & 0x00FF0000) >> 16);
Wolfgang Denkf01dbb52007-08-14 18:42:36 +020070 pDataFlash->pDataFlashDesc->command[2] =
Peter Pearsedcbfd2e2007-08-14 10:14:05 +010071 (unsigned char)((adr & 0x0000FF00) >> 8);
Wolfgang Denkf01dbb52007-08-14 18:42:36 +020072 pDataFlash->pDataFlashDesc->command[3] =
Peter Pearsedcbfd2e2007-08-14 10:14:05 +010073 (unsigned char)(adr & 0x000000FF);
74 pDataFlash->pDataFlashDesc->command[4] = 0;
75 }
76 pDataFlash->pDataFlashDesc->command[5] = 0;
77 pDataFlash->pDataFlashDesc->command[6] = 0;
78 pDataFlash->pDataFlashDesc->command[7] = 0;
79
80 /* Initialize the SpiData structure for the spi write fuction */
Wolfgang Denkf01dbb52007-08-14 18:42:36 +020081 pDataFlash->pDataFlashDesc->tx_cmd_pt =
Peter Pearsedcbfd2e2007-08-14 10:14:05 +010082 pDataFlash->pDataFlashDesc->command;
83 pDataFlash->pDataFlashDesc->tx_cmd_size = CmdSize;
Wolfgang Denkf01dbb52007-08-14 18:42:36 +020084 pDataFlash->pDataFlashDesc->rx_cmd_pt =
Peter Pearsedcbfd2e2007-08-14 10:14:05 +010085 pDataFlash->pDataFlashDesc->command;
86 pDataFlash->pDataFlashDesc->rx_cmd_size = CmdSize;
87
88 /* send the command and read the data */
89 return AT91F_SpiWrite (pDataFlash->pDataFlashDesc); }
90
91
92/*----------------------------------------------------------------------*/
93/* \fn AT91F_DataFlashGetStatus */
94/* \brief Read the status register of the dataflash */
Wolfgang Denkf01dbb52007-08-14 18:42:36 +020095/*----------------------------------------------------------------------*/
96AT91S_DataFlashStatus AT91F_DataFlashGetStatus(AT91PS_DataflashDesc pDesc)
Peter Pearsedcbfd2e2007-08-14 10:14:05 +010097{
98 AT91S_DataFlashStatus status;
99
100 /* if a transfert is in progress ==> return 0 */
101 if( (pDesc->state) != IDLE)
102 return DATAFLASH_BUSY;
103
104 /* first send the read status command (D7H) */
105 pDesc->command[0] = DB_STATUS;
106 pDesc->command[1] = 0;
107
108 pDesc->DataFlash_state = GET_STATUS;
109 pDesc->tx_data_size = 0; /* Transmit the command */
110 /* and receive response */
111 pDesc->tx_cmd_pt = pDesc->command;
112 pDesc->rx_cmd_pt = pDesc->command;
113 pDesc->rx_cmd_size = 2;
114 pDesc->tx_cmd_size = 2;
115 status = AT91F_SpiWrite (pDesc);
116
117 pDesc->DataFlash_state = *( (unsigned char *) (pDesc->rx_cmd_pt) +1);
118
119 return status;
120}
121
122
123/*----------------------------------------------------------------------*/
124/* \fn AT91F_DataFlashWaitReady */
125/* \brief wait for dataflash ready (bit7 of the status register == 1) */
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200126/*----------------------------------------------------------------------*/
127AT91S_DataFlashStatus AT91F_DataFlashWaitReady(AT91PS_DataflashDesc
128pDataFlashDesc, unsigned int timeout)
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100129{
130 pDataFlashDesc->DataFlash_state = IDLE;
131
132 do {
133 AT91F_DataFlashGetStatus(pDataFlashDesc);
134 timeout--;
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200135 } while( ((pDataFlashDesc->DataFlash_state & 0x80) != 0x80) &&
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100136 (timeout > 0) );
137
138 if((pDataFlashDesc->DataFlash_state & 0x80) != 0x80)
139 return DATAFLASH_ERROR;
140
141 return DATAFLASH_OK;
142}
143
144
145/*--------------------------------------------------------------------------*/
146/* Function Name : AT91F_DataFlashContinuousRead */
147/* Object : Continuous stream Read */
148/* Input Parameters : DataFlash Service */
149/* : <src> = dataflash address */
150/* : <*dataBuffer> = data buffer pointer */
151/* : <sizeToRead> = data buffer size */
152/* Return value : State of the dataflash */
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200153/*--------------------------------------------------------------------------*/
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100154AT91S_DataFlashStatus AT91F_DataFlashContinuousRead (
155 AT91PS_DataFlash pDataFlash,
156 int src,
157 unsigned char *dataBuffer,
158 int sizeToRead )
159{
160 AT91S_DataFlashStatus status;
161 /* Test the size to read in the device */
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200162 if ( (src + sizeToRead) >
163 (pDataFlash->pDevice->pages_size *
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100164 (pDataFlash->pDevice->pages_number)))
165 return DATAFLASH_MEMORY_OVERFLOW;
166
167 pDataFlash->pDataFlashDesc->rx_data_pt = dataBuffer;
168 pDataFlash->pDataFlashDesc->rx_data_size = sizeToRead;
169 pDataFlash->pDataFlashDesc->tx_data_pt = dataBuffer;
170 pDataFlash->pDataFlashDesc->tx_data_size = sizeToRead;
171
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200172 status = AT91F_DataFlashSendCommand
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100173 (pDataFlash, DB_CONTINUOUS_ARRAY_READ, 8, src);
174 /* Send the command to the dataflash */
175 return(status);
176}
177
178
179/*---------------------------------------------------------------------------*/
180/* Function Name : AT91F_DataFlashPagePgmBuf */
181/* Object : Main memory page program thru buffer 1 or buffer 2 */
182/* Input Parameters : DataFlash Service */
183/* : <*src> = Source buffer */
184/* : <dest> = dataflash destination address */
185/* : <SizeToWrite> = data buffer size */
186/* Return value : State of the dataflash */
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200187/*---------------------------------------------------------------------------*/
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100188AT91S_DataFlashStatus AT91F_DataFlashPagePgmBuf(
189 AT91PS_DataFlash pDataFlash,
190 unsigned char *src,
191 unsigned int dest,
192 unsigned int SizeToWrite)
193{
194 int cmdsize;
195 pDataFlash->pDataFlashDesc->tx_data_pt = src;
196 pDataFlash->pDataFlashDesc->tx_data_size = SizeToWrite;
197 pDataFlash->pDataFlashDesc->rx_data_pt = src;
198 pDataFlash->pDataFlashDesc->rx_data_size = SizeToWrite;
199
200 cmdsize = 4;
201 /* Send the command to the dataflash */
202 if (pDataFlash->pDevice->pages_number >= 16384)
203 cmdsize = 5;
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200204 return(AT91F_DataFlashSendCommand (pDataFlash, DB_PAGE_PGM_BUF1,
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100205cmdsize, dest)); }
206
207
208/*---------------------------------------------------------------------------*/
209/* Function Name : AT91F_MainMemoryToBufferTransfert */
210/* Object : Read a page in the SRAM Buffer 1 or 2 */
211/* Input Parameters : DataFlash Service */
212/* : Page concerned */
213/* : */
214/* Return value : State of the dataflash */
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200215/*---------------------------------------------------------------------------*/
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100216AT91S_DataFlashStatus AT91F_MainMemoryToBufferTransfert(
217 AT91PS_DataFlash pDataFlash,
218 unsigned char BufferCommand,
219 unsigned int page)
220{
221 int cmdsize;
222 /* Test if the buffer command is legal */
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200223 if ((BufferCommand != DB_PAGE_2_BUF1_TRF)
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100224 && (BufferCommand != DB_PAGE_2_BUF2_TRF))
225 return DATAFLASH_BAD_COMMAND;
226
227 /* no data to transmit or receive */
228 pDataFlash->pDataFlashDesc->tx_data_size = 0;
229 cmdsize = 4;
230 if (pDataFlash->pDevice->pages_number >= 16384)
231 cmdsize = 5;
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200232 return(AT91F_DataFlashSendCommand (pDataFlash, BufferCommand, cmdsize,
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100233page*pDataFlash->pDevice->pages_size));
234}
235
236
237/*-------------------------------------------------------------------------- */
238/* Function Name : AT91F_DataFlashWriteBuffer */
239/* Object : Write data to the internal sram buffer 1 or 2 */
240/* Input Parameters : DataFlash Service */
241/* : <BufferCommand> = command to write buffer1 or 2 */
242/* : <*dataBuffer> = data buffer to write */
243/* : <bufferAddress> = address in the internal buffer */
244/* : <SizeToWrite> = data buffer size */
245/* Return value : State of the dataflash */
246/*---------------------------------------------------------------------------*/
247AT91S_DataFlashStatus AT91F_DataFlashWriteBuffer (
248 AT91PS_DataFlash pDataFlash,
249 unsigned char BufferCommand,
250 unsigned char *dataBuffer,
251 unsigned int bufferAddress,
252 int SizeToWrite )
253{
254 int cmdsize;
255 /* Test if the buffer command is legal */
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200256 if ((BufferCommand != DB_BUF1_WRITE)
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100257 && (BufferCommand != DB_BUF2_WRITE))
258 return DATAFLASH_BAD_COMMAND;
259
260 /* buffer address must be lower than page size */
261 if (bufferAddress > pDataFlash->pDevice->pages_size)
262 return DATAFLASH_BAD_ADDRESS;
263
264 if ( (pDataFlash->pDataFlashDesc->state) != IDLE)
265 return DATAFLASH_BUSY;
266
267 /* Send first Write Command */
268 pDataFlash->pDataFlashDesc->command[0] = BufferCommand;
269 pDataFlash->pDataFlashDesc->command[1] = 0;
270 if (pDataFlash->pDevice->pages_number >= 16384) {
271 pDataFlash->pDataFlashDesc->command[2] = 0;
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200272 pDataFlash->pDataFlashDesc->command[3] =
273 (unsigned char)(((unsigned int)(bufferAddress &
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100274 pDataFlash->pDevice->byte_mask)) >> 8);
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200275 pDataFlash->pDataFlashDesc->command[4] =
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100276 (unsigned char)((unsigned int)bufferAddress & 0x00FF);
277 cmdsize = 5;
278 } else {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200279 pDataFlash->pDataFlashDesc->command[2] =
280 (unsigned char)(((unsigned int)(bufferAddress &
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100281 pDataFlash->pDevice->byte_mask)) >> 8);
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200282 pDataFlash->pDataFlashDesc->command[3] =
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100283 (unsigned char)((unsigned int)bufferAddress & 0x00FF);
284 pDataFlash->pDataFlashDesc->command[4] = 0;
285 cmdsize = 4;
286 }
287
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200288 pDataFlash->pDataFlashDesc->tx_cmd_pt =
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100289 pDataFlash->pDataFlashDesc->command;
290 pDataFlash->pDataFlashDesc->tx_cmd_size = cmdsize;
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200291 pDataFlash->pDataFlashDesc->rx_cmd_pt =
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100292 pDataFlash->pDataFlashDesc->command;
293 pDataFlash->pDataFlashDesc->rx_cmd_size = cmdsize;
294
295 pDataFlash->pDataFlashDesc->rx_data_pt = dataBuffer;
296 pDataFlash->pDataFlashDesc->tx_data_pt = dataBuffer;
297 pDataFlash->pDataFlashDesc->rx_data_size = SizeToWrite;
298 pDataFlash->pDataFlashDesc->tx_data_size = SizeToWrite;
299
300 return AT91F_SpiWrite(pDataFlash->pDataFlashDesc);
301}
302
303/*---------------------------------------------------------------------------*/
304/* Function Name : AT91F_PageErase */
305/* Object : Erase a page */
306/* Input Parameters : DataFlash Service */
307/* : Page concerned */
308/* : */
309/* Return value : State of the dataflash */
310/*---------------------------------------------------------------------------*/
311AT91S_DataFlashStatus AT91F_PageErase(
312 AT91PS_DataFlash pDataFlash,
313 unsigned int page)
314{
315 int cmdsize;
316 /* Test if the buffer command is legal */
317 /* no data to transmit or receive */
318 pDataFlash->pDataFlashDesc->tx_data_size = 0;
319
320 cmdsize = 4;
321 if (pDataFlash->pDevice->pages_number >= 16384)
322 cmdsize = 5;
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200323 return(AT91F_DataFlashSendCommand (pDataFlash, DB_PAGE_ERASE, cmdsize,
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100324page*pDataFlash->pDevice->pages_size));
325}
326
327
328/*---------------------------------------------------------------------------*/
329/* Function Name : AT91F_BlockErase */
330/* Object : Erase a Block */
331/* Input Parameters : DataFlash Service */
332/* : Page concerned */
333/* : */
334/* Return value : State of the dataflash */
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200335/*---------------------------------------------------------------------------*/
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100336AT91S_DataFlashStatus AT91F_BlockErase(
337 AT91PS_DataFlash pDataFlash,
338 unsigned int block)
339{
340 int cmdsize;
341 /* Test if the buffer command is legal */
342 /* no data to transmit or receive */
343 pDataFlash->pDataFlashDesc->tx_data_size = 0;
344 cmdsize = 4;
345 if (pDataFlash->pDevice->pages_number >= 16384)
346 cmdsize = 5;
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200347 return(AT91F_DataFlashSendCommand (pDataFlash, DB_BLOCK_ERASE,cmdsize,
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100348block*8*pDataFlash->pDevice->pages_size));
349}
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 Pearsedcbfd2e2007-08-14 10:14:05 +0100359AT91S_DataFlashStatus AT91F_WriteBufferToMain (
360 AT91PS_DataFlash pDataFlash,
361 unsigned char BufferCommand,
362 unsigned int dest )
363{
364 int cmdsize;
365 /* Test if the buffer command is correct */
366 if ((BufferCommand != DB_BUF1_PAGE_PGM) &&
367 (BufferCommand != DB_BUF1_PAGE_ERASE_PGM) &&
368 (BufferCommand != DB_BUF2_PAGE_PGM) &&
369 (BufferCommand != DB_BUF2_PAGE_ERASE_PGM) )
370 return DATAFLASH_BAD_COMMAND;
371
372 /* no data to transmit or receive */
373 pDataFlash->pDataFlashDesc->tx_data_size = 0;
374
375 cmdsize = 4;
376 if (pDataFlash->pDevice->pages_number >= 16384)
377 cmdsize = 5;
378 /* Send the command to the dataflash */
379 return(AT91F_DataFlashSendCommand (pDataFlash, BufferCommand, cmdsize,
380 dest)); }
381
382
383/*---------------------------------------------------------------------------*/
384/* Function Name : AT91F_PartialPageWrite */
385/* Object : Erase partielly a page */
386/* Input Parameters : <page> = page number */
387/* : <AdrInpage> = adr to begin the fading */
388/* : <length> = Number of bytes to erase */
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200389/*---------------------------------------------------------------------------*/
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100390AT91S_DataFlashStatus AT91F_PartialPageWrite (
391 AT91PS_DataFlash pDataFlash,
392 unsigned char *src,
393 unsigned int dest,
394 unsigned int size)
395{
396 unsigned int page;
397 unsigned int AdrInPage;
398
399 page = dest / (pDataFlash->pDevice->pages_size);
400 AdrInPage = dest % (pDataFlash->pDevice->pages_size);
401
402 /* Read the contents of the page in the Sram Buffer */
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200403 AT91F_MainMemoryToBufferTransfert(pDataFlash,
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100404 DB_PAGE_2_BUF1_TRF, page);
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200405 AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc,
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100406 AT91C_TIMEOUT_WRDY);
407 /*Update the SRAM buffer */
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200408 AT91F_DataFlashWriteBuffer(pDataFlash, DB_BUF1_WRITE, src,
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100409 AdrInPage, size);
410
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200411 AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc,
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100412 AT91C_TIMEOUT_WRDY);
413
414 /* Erase page if a 128 Mbits device */
415 if (pDataFlash->pDevice->pages_number >= 16384) {
416 AT91F_PageErase(pDataFlash, page);
417 /* Rewrite the modified Sram Buffer in the main memory */
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200418 AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc,
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100419 AT91C_TIMEOUT_WRDY);
420 }
421
422 /* Rewrite the modified Sram Buffer in the main memory */
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200423 return(AT91F_WriteBufferToMain(pDataFlash, DB_BUF1_PAGE_ERASE_PGM,
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100424 (page*pDataFlash->pDevice->pages_size)));
425}
426
427/*---------------------------------------------------------------------------*/
428/* Function Name : AT91F_DataFlashWrite */
429/* Object : */
430/* Input Parameters : <*src> = Source buffer */
431/* : <dest> = dataflash adress */
432/* : <size> = data buffer size */
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200433/*---------------------------------------------------------------------------*/
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100434AT91S_DataFlashStatus AT91F_DataFlashWrite(
435 AT91PS_DataFlash pDataFlash,
436 unsigned char *src,
437 int dest,
438 int size )
439{
440 unsigned int length;
441 unsigned int page;
442 unsigned int status;
443
444 AT91F_SpiEnable(pDataFlash->pDevice->cs);
445
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200446 if ( (dest + size) > (pDataFlash->pDevice->pages_size *
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100447 (pDataFlash->pDevice->pages_number)))
448 return DATAFLASH_MEMORY_OVERFLOW;
449
450 /* If destination does not fit a page start address */
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200451 if ((dest % ((unsigned int)(pDataFlash->pDevice->pages_size))) != 0 )
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100452 {
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200453 length = pDataFlash->pDevice->pages_size -
454 (dest %
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100455 ((unsigned int)
456 (pDataFlash->pDevice->pages_size)));
457
458 if (size < length)
459 length = size;
460
461 if(!AT91F_PartialPageWrite(pDataFlash,src, dest, length))
462 return DATAFLASH_ERROR;
463
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200464 AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc,
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100465 AT91C_TIMEOUT_WRDY);
466
467 /* Update size, source and destination pointers */
468 size -= length;
469 dest += length;
470 src += length;
471 }
472
473 while (( size - pDataFlash->pDevice->pages_size ) >= 0 ) {
474 /* program dataflash page */
475 page = (unsigned int)dest / (pDataFlash->pDevice->pages_size);
476
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200477 status = AT91F_DataFlashWriteBuffer(pDataFlash,
478 DB_BUF1_WRITE, src, 0,
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100479 pDataFlash->pDevice->pages_size);
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200480 AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc,
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100481 AT91C_TIMEOUT_WRDY);
482
483 status = AT91F_PageErase(pDataFlash, page);
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200484 AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc,
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100485 AT91C_TIMEOUT_WRDY);
486 if (!status)
487 return DATAFLASH_ERROR;
488
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200489 status = AT91F_WriteBufferToMain (pDataFlash,
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100490 DB_BUF1_PAGE_PGM, dest);
491 if(!status)
492 return DATAFLASH_ERROR;
493
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200494 AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc,
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100495 AT91C_TIMEOUT_WRDY);
496
497 /* Update size, source and destination pointers */
498 size -= pDataFlash->pDevice->pages_size;
499 dest += pDataFlash->pDevice->pages_size;
500 src += pDataFlash->pDevice->pages_size;
501 }
502
503 /* If still some bytes to read */
504 if ( size > 0 ) {
505 /* program dataflash page */
506 if(!AT91F_PartialPageWrite(pDataFlash, src, dest, size) )
507 return DATAFLASH_ERROR;
508
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200509 AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc,
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100510 AT91C_TIMEOUT_WRDY);
511 }
512 return DATAFLASH_OK;
513}
514
515
516/*---------------------------------------------------------------------------*/
517/* Function Name : AT91F_DataFlashRead */
518/* Object : Read a block in dataflash */
519/* Input Parameters : */
520/* Return value : */
521/*---------------------------------------------------------------------------*/
522int AT91F_DataFlashRead(
523 AT91PS_DataFlash pDataFlash,
524 unsigned long addr,
525 unsigned long size,
526 char *buffer)
527{
528 unsigned long SizeToRead;
529
530 AT91F_SpiEnable(pDataFlash->pDevice->cs);
531
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200532 if(AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc,
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100533 AT91C_TIMEOUT_WRDY) != DATAFLASH_OK)
534 return -1;
535
536 while (size) {
537 SizeToRead = (size < 0x8000)? size:0x8000;
538
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200539 if (AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc,
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100540 AT91C_TIMEOUT_WRDY) != DATAFLASH_OK)
541 return -1;
542
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200543 if (AT91F_DataFlashContinuousRead (pDataFlash, addr,
Peter Pearsedcbfd2e2007-08-14 10:14:05 +0100544 (uchar *) buffer, SizeToRead) != DATAFLASH_OK)
545 return -1;
546
547 size -= SizeToRead;
548 addr += SizeToRead;
549 buffer += SizeToRead;
550 }
551
552 return DATAFLASH_OK;
553}
554
Peter Pearse0c42f362007-08-14 10:46:32 +0100555/*---------------------------------------------------------------------------*/
556/* Function Name : AT91F_DataflashProbe */
557/* Object : */
558/* Input Parameters : */
559/* Return value : Dataflash status register */
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200560/*---------------------------------------------------------------------------*/
Peter Pearse0c42f362007-08-14 10:46:32 +0100561int AT91F_DataflashProbe(int cs, AT91PS_DataflashDesc pDesc) {
562 AT91F_SpiEnable(cs);
563 AT91F_DataFlashGetStatus(pDesc);
Wolfgang Denkf01dbb52007-08-14 18:42:36 +0200564 return((pDesc->command[1] == 0xFF)? 0: pDesc->command[1] & 0x3C);
Peter Pearse0c42f362007-08-14 10:46:32 +0100565}
Peter Pearse0c42f362007-08-14 10:46:32 +0100566#endif