1 /* Driver 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,
23 #include <asm/hardware.h>
25 #ifdef CONFIG_HAS_DATAFLASH
26 #include <dataflash.h>
28 #define SPI_CLK 5000000
29 #define AT91C_SPI_PCS0_SERIAL_DATAFLASH 0xE /* Chip Select 0 : NPCS0 %1110 */
30 #define AT91C_SPI_PCS3_DATAFLASH_CARD 0x7 /* Chip Select 3 : NPCS3 %0111 */
32 void AT91F_SpiInit(void) {
34 /*-------------------------------------------------------------------*/
35 /* SPI DataFlash Init */
36 /*-------------------------------------------------------------------*/
38 AT91C_BASE_PIOA->PIO_ASR = AT91C_PA3_NPCS0 | AT91C_PA4_NPCS1 | AT91C_PA1_MOSI | AT91C_PA5_NPCS2 |
39 AT91C_PA6_NPCS3 | AT91C_PA0_MISO | AT91C_PA2_SPCK;
40 AT91C_BASE_PIOA->PIO_PDR = AT91C_PA3_NPCS0 | AT91C_PA4_NPCS1 | AT91C_PA1_MOSI | AT91C_PA5_NPCS2 |
41 AT91C_PA6_NPCS3 | AT91C_PA0_MISO | AT91C_PA2_SPCK;
43 AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_SPI;
46 AT91C_BASE_SPI->SPI_CR = AT91C_SPI_SWRST;
48 /* Configure SPI in Master Mode with No CS selected !!! */
49 AT91C_BASE_SPI->SPI_MR = AT91C_SPI_MSTR | AT91C_SPI_MODFDIS | AT91C_SPI_PCS;
51 /* Configure CS0 and CS3 */
52 *(AT91C_SPI_CSR + 0) = AT91C_SPI_CPOL | (AT91C_SPI_DLYBS & 0x100000) | ((AT91C_MASTER_CLOCK / (2*SPI_CLK)) << 8);
54 *(AT91C_SPI_CSR + 3) = AT91C_SPI_CPOL | (AT91C_SPI_DLYBS & 0x100000) | ((AT91C_MASTER_CLOCK / (2*SPI_CLK)) << 8);
58 void AT91F_SpiEnable(int cs) {
60 case 0: /* Configure SPI CS0 for Serial DataFlash AT45DBxx */
61 AT91C_BASE_SPI->SPI_MR &= 0xFFF0FFFF;
62 AT91C_BASE_SPI->SPI_MR |= ((AT91C_SPI_PCS0_SERIAL_DATAFLASH<<16) & AT91C_SPI_PCS);
64 case 3: /* Configure SPI CS3 for Serial DataFlash Card */
65 /* Set up PIO SDC_TYPE to switch on DataFlash Card and not MMC/SDCard */
66 AT91C_BASE_PIOB->PIO_PER = AT91C_PIO_PB7; /* Set in PIO mode */
67 AT91C_BASE_PIOB->PIO_OER = AT91C_PIO_PB7; /* Configure in output */
69 AT91C_BASE_PIOB->PIO_CODR = AT91C_PIO_PB7;
71 AT91C_BASE_SPI->SPI_MR &= 0xFFF0FFFF;
72 AT91C_BASE_SPI->SPI_MR |= ((AT91C_SPI_PCS3_DATAFLASH_CARD<<16) & AT91C_SPI_PCS);
77 AT91C_BASE_SPI->SPI_CR = AT91C_SPI_SPIEN;
80 /*----------------------------------------------------------------------------*/
81 /* \fn AT91F_SpiWrite */
82 /* \brief Set the PDC registers for a transfert */
83 /*----------------------------------------------------------------------------*/
84 unsigned int AT91F_SpiWrite ( AT91PS_DataflashDesc pDesc )
90 AT91C_BASE_SPI->SPI_PTCR = AT91C_PDC_TXTDIS + AT91C_PDC_RXTDIS;
92 /* Initialize the Transmit and Receive Pointer */
93 AT91C_BASE_SPI->SPI_RPR = (unsigned int)pDesc->rx_cmd_pt ;
94 AT91C_BASE_SPI->SPI_TPR = (unsigned int)pDesc->tx_cmd_pt ;
96 /* Intialize the Transmit and Receive Counters */
97 AT91C_BASE_SPI->SPI_RCR = pDesc->rx_cmd_size;
98 AT91C_BASE_SPI->SPI_TCR = pDesc->tx_cmd_size;
100 if ( pDesc->tx_data_size != 0 ) {
101 /* Initialize the Next Transmit and Next Receive Pointer */
102 AT91C_BASE_SPI->SPI_RNPR = (unsigned int)pDesc->rx_data_pt ;
103 AT91C_BASE_SPI->SPI_TNPR = (unsigned int)pDesc->tx_data_pt ;
105 /* Intialize the Next Transmit and Next Receive Counters */
106 AT91C_BASE_SPI->SPI_RNCR = pDesc->rx_data_size ;
107 AT91C_BASE_SPI->SPI_TNCR = pDesc->tx_data_size ;
110 /* arm simple, non interrupt dependent timer */
111 reset_timer_masked();
114 AT91C_BASE_SPI->SPI_PTCR = AT91C_PDC_TXTEN + AT91C_PDC_RXTEN;
115 while(!(AT91C_BASE_SPI->SPI_SR & AT91C_SPI_RXBUFF) && ((timeout = get_timer_masked() ) < CFG_SPI_WRITE_TOUT));
116 AT91C_BASE_SPI->SPI_PTCR = AT91C_PDC_TXTDIS + AT91C_PDC_RXTDIS;
119 if (timeout >= CFG_SPI_WRITE_TOUT){
120 printf("Error Timeout\n\r");
121 return DATAFLASH_ERROR;
128 /*----------------------------------------------------------------------*/
129 /* \fn AT91F_DataFlashSendCommand */
130 /* \brief Generic function to send a command to the dataflash */
131 /*----------------------------------------------------------------------*/
132 AT91S_DataFlashStatus AT91F_DataFlashSendCommand(
133 AT91PS_DataFlash pDataFlash,
134 unsigned char OpCode,
135 unsigned int CmdSize,
136 unsigned int DataflashAddress)
140 if ( (pDataFlash->pDataFlashDesc->state) != IDLE)
141 return DATAFLASH_BUSY;
143 /* process the address to obtain page address and byte address */
144 adr = ((DataflashAddress / (pDataFlash->pDevice->pages_size)) << pDataFlash->pDevice->page_offset) + (DataflashAddress % (pDataFlash->pDevice->pages_size));
146 /* fill the command buffer */
147 pDataFlash->pDataFlashDesc->command[0] = OpCode;
148 pDataFlash->pDataFlashDesc->command[1] = (unsigned char)((adr & 0x00FF0000) >> 16);
149 pDataFlash->pDataFlashDesc->command[2] = (unsigned char)((adr & 0x0000FF00) >> 8);
150 pDataFlash->pDataFlashDesc->command[3] = (unsigned char)(adr & 0x000000FF) ;
151 pDataFlash->pDataFlashDesc->command[4] = 0;
152 pDataFlash->pDataFlashDesc->command[5] = 0;
153 pDataFlash->pDataFlashDesc->command[6] = 0;
154 pDataFlash->pDataFlashDesc->command[7] = 0;
156 /* Initialize the SpiData structure for the spi write fuction */
157 pDataFlash->pDataFlashDesc->tx_cmd_pt = pDataFlash->pDataFlashDesc->command ;
158 pDataFlash->pDataFlashDesc->tx_cmd_size = CmdSize ;
159 pDataFlash->pDataFlashDesc->rx_cmd_pt = pDataFlash->pDataFlashDesc->command ;
160 pDataFlash->pDataFlashDesc->rx_cmd_size = CmdSize ;
162 /* send the command and read the data */
163 return AT91F_SpiWrite (pDataFlash->pDataFlashDesc);
167 /*----------------------------------------------------------------------*/
168 /* \fn AT91F_DataFlashGetStatus */
169 /* \brief Read the status register of the dataflash */
170 /*----------------------------------------------------------------------*/
171 AT91S_DataFlashStatus AT91F_DataFlashGetStatus(AT91PS_DataflashDesc pDesc)
173 AT91S_DataFlashStatus status;
175 /* if a transfert is in progress ==> return 0 */
176 if( (pDesc->state) != IDLE)
177 return DATAFLASH_BUSY;
179 /* first send the read status command (D7H) */
180 pDesc->command[0] = DB_STATUS;
181 pDesc->command[1] = 0;
183 pDesc->DataFlash_state = GET_STATUS;
184 pDesc->tx_data_size = 0 ; /* Transmit the command and receive response */
185 pDesc->tx_cmd_pt = pDesc->command ;
186 pDesc->rx_cmd_pt = pDesc->command ;
187 pDesc->rx_cmd_size = 2 ;
188 pDesc->tx_cmd_size = 2 ;
189 status = AT91F_SpiWrite (pDesc);
191 pDesc->DataFlash_state = *( (unsigned char *) (pDesc->rx_cmd_pt) +1);
197 /*----------------------------------------------------------------------*/
198 /* \fn AT91F_DataFlashWaitReady */
199 /* \brief wait for dataflash ready (bit7 of the status register == 1) */
200 /*----------------------------------------------------------------------*/
201 AT91S_DataFlashStatus AT91F_DataFlashWaitReady(AT91PS_DataflashDesc pDataFlashDesc, unsigned int timeout)
203 pDataFlashDesc->DataFlash_state = IDLE;
206 AT91F_DataFlashGetStatus(pDataFlashDesc);
209 while( ((pDataFlashDesc->DataFlash_state & 0x80) != 0x80) && (timeout > 0) );
211 if((pDataFlashDesc->DataFlash_state & 0x80) != 0x80)
212 return DATAFLASH_ERROR;
219 /*------------------------------------------------------------------------------*/
220 /* Function Name : AT91F_DataFlashContinuousRead */
221 /* Object : Continuous stream Read */
222 /* Input Parameters : DataFlash Service */
223 /* : <src> = dataflash address */
224 /* : <*dataBuffer> = data buffer pointer */
225 /* : <sizeToRead> = data buffer size */
226 /* Return value : State of the dataflash */
227 /*------------------------------------------------------------------------------*/
228 AT91S_DataFlashStatus AT91F_DataFlashContinuousRead (
229 AT91PS_DataFlash pDataFlash,
231 unsigned char *dataBuffer,
234 /* Test the size to read in the device */
235 if ( (src + sizeToRead) > (pDataFlash->pDevice->pages_size * (pDataFlash->pDevice->pages_number)))
236 return DATAFLASH_MEMORY_OVERFLOW;
238 pDataFlash->pDataFlashDesc->rx_data_pt = dataBuffer;
239 pDataFlash->pDataFlashDesc->rx_data_size = sizeToRead;
240 pDataFlash->pDataFlashDesc->tx_data_pt = dataBuffer;
241 pDataFlash->pDataFlashDesc->tx_data_size = sizeToRead;
243 /* Send the command to the dataflash */
244 return(AT91F_DataFlashSendCommand (pDataFlash, DB_CONTINUOUS_ARRAY_READ, 8, src));
249 /*------------------------------------------------------------------------------*/
250 /* Function Name : AT91F_DataFlashPagePgmBuf */
251 /* Object : Main memory page program through buffer 1 or buffer 2 */
252 /* Input Parameters : DataFlash Service */
253 /* : <*src> = Source buffer */
254 /* : <dest> = dataflash destination address */
255 /* : <SizeToWrite> = data buffer size */
256 /* Return value : State of the dataflash */
257 /*------------------------------------------------------------------------------*/
258 AT91S_DataFlashStatus AT91F_DataFlashPagePgmBuf(
259 AT91PS_DataFlash pDataFlash,
262 unsigned int SizeToWrite)
264 pDataFlash->pDataFlashDesc->tx_data_pt = src ;
265 pDataFlash->pDataFlashDesc->tx_data_size = SizeToWrite ;
266 pDataFlash->pDataFlashDesc->rx_data_pt = src;
267 pDataFlash->pDataFlashDesc->rx_data_size = SizeToWrite;
269 /* Send the command to the dataflash */
270 return(AT91F_DataFlashSendCommand (pDataFlash, DB_PAGE_PGM_BUF1, 4, dest));
274 /*------------------------------------------------------------------------------*/
275 /* Function Name : AT91F_MainMemoryToBufferTransfert */
276 /* Object : Read a page in the SRAM Buffer 1 or 2 */
277 /* Input Parameters : DataFlash Service */
278 /* : Page concerned */
280 /* Return value : State of the dataflash */
281 /*------------------------------------------------------------------------------*/
282 AT91S_DataFlashStatus AT91F_MainMemoryToBufferTransfert(
283 AT91PS_DataFlash pDataFlash,
284 unsigned char BufferCommand,
287 /* Test if the buffer command is legal */
288 if ((BufferCommand != DB_PAGE_2_BUF1_TRF) && (BufferCommand != DB_PAGE_2_BUF2_TRF))
289 return DATAFLASH_BAD_COMMAND;
291 /* no data to transmit or receive */
292 pDataFlash->pDataFlashDesc->tx_data_size = 0;
294 return(AT91F_DataFlashSendCommand (pDataFlash, BufferCommand, 4, page*pDataFlash->pDevice->pages_size));
299 /*----------------------------------------------------------------------------- */
300 /* Function Name : AT91F_DataFlashWriteBuffer */
301 /* Object : Write data to the internal sram buffer 1 or 2 */
302 /* Input Parameters : DataFlash Service */
303 /* : <BufferCommand> = command to write buffer1 or buffer2 */
304 /* : <*dataBuffer> = data buffer to write */
305 /* : <bufferAddress> = address in the internal buffer */
306 /* : <SizeToWrite> = data buffer size */
307 /* Return value : State of the dataflash */
308 /*------------------------------------------------------------------------------*/
309 AT91S_DataFlashStatus AT91F_DataFlashWriteBuffer (
310 AT91PS_DataFlash pDataFlash,
311 unsigned char BufferCommand,
312 unsigned char *dataBuffer,
313 unsigned int bufferAddress,
316 /* Test if the buffer command is legal */
317 if ((BufferCommand != DB_BUF1_WRITE) && (BufferCommand != DB_BUF2_WRITE))
318 return DATAFLASH_BAD_COMMAND;
320 /* buffer address must be lower than page size */
321 if (bufferAddress > pDataFlash->pDevice->pages_size)
322 return DATAFLASH_BAD_ADDRESS;
324 if ( (pDataFlash->pDataFlashDesc->state) != IDLE)
325 return DATAFLASH_BUSY;
327 /* Send first Write Command */
328 pDataFlash->pDataFlashDesc->command[0] = BufferCommand;
329 pDataFlash->pDataFlashDesc->command[1] = 0;
330 pDataFlash->pDataFlashDesc->command[2] = (unsigned char)(((unsigned int)(bufferAddress & pDataFlash->pDevice->byte_mask)) >> 8) ;
331 pDataFlash->pDataFlashDesc->command[3] = (unsigned char)((unsigned int)bufferAddress & 0x00FF) ;
334 pDataFlash->pDataFlashDesc->tx_cmd_pt = pDataFlash->pDataFlashDesc->command ;
335 pDataFlash->pDataFlashDesc->tx_cmd_size = 4 ;
336 pDataFlash->pDataFlashDesc->rx_cmd_pt = pDataFlash->pDataFlashDesc->command ;
337 pDataFlash->pDataFlashDesc->rx_cmd_size = 4 ;
339 pDataFlash->pDataFlashDesc->rx_data_pt = dataBuffer ;
340 pDataFlash->pDataFlashDesc->tx_data_pt = dataBuffer ;
341 pDataFlash->pDataFlashDesc->rx_data_size = SizeToWrite ;
342 pDataFlash->pDataFlashDesc->tx_data_size = SizeToWrite ;
344 return AT91F_SpiWrite(pDataFlash->pDataFlashDesc);
348 /*------------------------------------------------------------------------------*/
349 /* Function Name : AT91F_WriteBufferToMain */
350 /* Object : Write buffer to the main memory */
351 /* Input Parameters : DataFlash Service */
352 /* : <BufferCommand> = command to send to buffer1 or buffer2 */
353 /* : <dest> = main memory address */
354 /* Return value : State of the dataflash */
355 /*------------------------------------------------------------------------------*/
356 AT91S_DataFlashStatus AT91F_WriteBufferToMain (
357 AT91PS_DataFlash pDataFlash,
358 unsigned char BufferCommand,
361 /* Test if the buffer command is correct */
362 if ((BufferCommand != DB_BUF1_PAGE_PGM) &&
363 (BufferCommand != DB_BUF1_PAGE_ERASE_PGM) &&
364 (BufferCommand != DB_BUF2_PAGE_PGM) &&
365 (BufferCommand != DB_BUF2_PAGE_ERASE_PGM) )
366 return DATAFLASH_BAD_COMMAND;
368 /* no data to transmit or receive */
369 pDataFlash->pDataFlashDesc->tx_data_size = 0;
371 /* Send the command to the dataflash */
372 return(AT91F_DataFlashSendCommand (pDataFlash, BufferCommand, 4, dest));
376 /*------------------------------------------------------------------------------*/
377 /* Function Name : AT91F_PartialPageWrite */
378 /* Object : Erase partielly a page */
379 /* Input Parameters : <page> = page number */
380 /* : <AdrInpage> = adr to begin the fading */
381 /* : <length> = Number of bytes to erase */
382 /*------------------------------------------------------------------------------*/
383 AT91S_DataFlashStatus AT91F_PartialPageWrite (
384 AT91PS_DataFlash pDataFlash,
390 unsigned int AdrInPage;
392 page = dest / (pDataFlash->pDevice->pages_size);
393 AdrInPage = dest % (pDataFlash->pDevice->pages_size);
395 /* Read the contents of the page in the Sram Buffer */
396 AT91F_MainMemoryToBufferTransfert(pDataFlash, DB_PAGE_2_BUF1_TRF, page);
397 AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc, 1000);
398 /*Update the SRAM buffer */
399 AT91F_DataFlashWriteBuffer(pDataFlash, DB_BUF1_WRITE, src, AdrInPage, size);
400 AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc, 1000);
402 /* Rewrite the modified Sram Buffer in the main memory */
403 return(AT91F_WriteBufferToMain(pDataFlash, DB_BUF1_PAGE_ERASE_PGM, (page*pDataFlash->pDevice->pages_size)));
408 /*------------------------------------------------------------------------------*/
409 /* Function Name : AT91F_DataFlashWrite_Overloaded */
411 /* Input Parameters : <*src> = Source buffer */
412 /* : <dest> = dataflash adress */
413 /* : <size> = data buffer size */
414 /*------------------------------------------------------------------------------*/
415 AT91S_DataFlashStatus AT91F_DataFlashWrite(
416 AT91PS_DataFlash pDataFlash,
423 AT91F_SpiEnable(pDataFlash->pDevice->cs);
425 if ( (dest + size) > (pDataFlash->pDevice->pages_size * (pDataFlash->pDevice->pages_number)))
426 return DATAFLASH_MEMORY_OVERFLOW;
428 /* If destination does not fit a page start address */
429 if ((dest % ((unsigned int)(pDataFlash->pDevice->pages_size))) != 0 ) {
430 length = pDataFlash->pDevice->pages_size - (dest % ((unsigned int)(pDataFlash->pDevice->pages_size)));
435 if(!AT91F_PartialPageWrite(pDataFlash,src, dest, length))
436 return DATAFLASH_ERROR;
438 AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc, 1000);
440 /* Update size, source and destination pointers */
446 while (( size - pDataFlash->pDevice->pages_size ) >= 0 ) {
447 /* program dataflash page */
448 if(!AT91F_DataFlashPagePgmBuf(pDataFlash, src, dest, pDataFlash->pDevice->pages_size ))
449 return DATAFLASH_ERROR;
451 AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc, 1000);
453 /* Update size, source and destination pointers */
454 size -= pDataFlash->pDevice->pages_size ;
455 dest += pDataFlash->pDevice->pages_size ;
456 src += pDataFlash->pDevice->pages_size ;
459 /* If still some bytes to read */
461 /* program dataflash page */
462 if(!AT91F_PartialPageWrite(pDataFlash, src, dest, size) )
463 return DATAFLASH_ERROR;
465 AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc, 1000);
471 /*------------------------------------------------------------------------------*/
472 /* Function Name : AT91F_DataFlashRead */
473 /* Object : Read a block in dataflash */
474 /* Input Parameters : */
476 /*------------------------------------------------------------------------------*/
477 int AT91F_DataFlashRead(
478 AT91PS_DataFlash pDataFlash,
483 unsigned long SizeToRead;
485 AT91F_SpiEnable(pDataFlash->pDevice->cs);
487 if(AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc, 1000) != DATAFLASH_OK)
491 SizeToRead = (size < 0x8000)? size:0x8000;
493 if (AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc, 1000) != DATAFLASH_OK)
496 if (AT91F_DataFlashContinuousRead (pDataFlash, addr, buffer, SizeToRead) != DATAFLASH_OK)
501 buffer += SizeToRead;
508 /*------------------------------------------------------------------------------*/
509 /* Function Name : AT91F_DataflashProbe */
511 /* Input Parameters : */
512 /* Return value : Dataflash status register */
513 /*------------------------------------------------------------------------------*/
514 int AT91F_DataflashProbe(int cs, AT91PS_DataflashDesc pDesc)
517 AT91F_DataFlashGetStatus(pDesc);
518 return((pDesc->command[1] == 0xFF)? 0: pDesc->command[1] & 0x3C);