00001 /* ---------------------------------------------------------------------------- 00002 * SAM Software Package License 00003 * ---------------------------------------------------------------------------- 00004 * Copyright (c) 2014, Atmel Corporation 00005 * 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions are met: 00010 * 00011 * - Redistributions of source code must retain the above copyright notice, 00012 * this list of conditions and the disclaimer below. 00013 * 00014 * Atmel's name may not be used to endorse or promote products derived from 00015 * this software without specific prior written permission. 00016 * 00017 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR 00018 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00019 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE 00020 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, 00021 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00022 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 00023 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00024 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00025 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 00026 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 * ---------------------------------------------------------------------------- 00028 */ 00029 //------------------------------------------------------------------------------ 00030 // Headers 00031 //------------------------------------------------------------------------------ 00032 00033 #include "at26.h" 00034 #include "board.h" 00035 #include <assert.h> 00036 00037 //------------------------------------------------------------------------------ 00038 // Internal definitions 00039 //------------------------------------------------------------------------------ 00040 00041 /// SPI clock frequency used in Hz. 00042 #define SPCK 1000000 00043 00044 /// SPI chip select configuration value. 00045 #define CSR (SPI_CSR_NCPHA | \ 00046 SPID_CSR_DLYBCT(BOARD_MCK, 100) | \ 00047 SPID_CSR_DLYBS(BOARD_MCK, 5) | \ 00048 SPID_CSR_SCBR(BOARD_MCK, SPCK)) 00049 00050 /// Number of recognized dataflash. 00051 #define NUMDATAFLASH (sizeof(at26Devices) / sizeof(At26Desc)) 00052 00053 //------------------------------------------------------------------------------ 00054 // Internal variables 00055 //------------------------------------------------------------------------------ 00056 00057 /// Array of recognized serial firmware dataflash chips. 00058 static const At26Desc at26Devices[] = { 00059 // name, Jedec ID, size, page size, block size, block erase command 00060 {"AT25DF041A" , 0x0001441F, 512 * 1024 , 256, 64 * 1024, AT26_BLOCK_ERASE_64K}, 00061 {"AT25DF161" , 0x0002461F, 2 * 1024 * 1024, 256, 64 * 1024, AT26_BLOCK_ERASE_64K}, 00062 {"AT26DF081A" , 0x0001451F, 1 * 1024 * 1024, 256, 64 * 1024, AT26_BLOCK_ERASE_64K}, 00063 {"AT26DF0161" , 0x0000461F, 2 * 1024 * 1024, 256, 64 * 1024, AT26_BLOCK_ERASE_64K}, 00064 {"AT26DF161A" , 0x0001461F, 2 * 1024 * 1024, 256, 64 * 1024, AT26_BLOCK_ERASE_64K}, 00065 {"AT26DF321" , 0x0000471F, 4 * 1024 * 1024, 256, 64 * 1024, AT26_BLOCK_ERASE_64K}, 00066 {"AT25DF512B" , 0x0001651F, 64 * 1024, 256, 32 * 1024, AT26_BLOCK_ERASE_32K}, 00067 {"AT25DF512B" , 0x0000651F, 64 * 1024, 256, 32 * 1024, AT26_BLOCK_ERASE_32K}, 00068 {"AT25DF021" , 0x0000431F, 256 * 1024, 256, 64 * 1024, AT26_BLOCK_ERASE_64K}, 00069 {"AT26DF641" , 0x0000481F, 8 * 1024 * 1024, 256, 64 * 1024, AT26_BLOCK_ERASE_64K}, 00070 // Manufacturer: ST 00071 {"M25P05" , 0x00102020, 64 * 1024, 256, 32 * 1024, AT26_BLOCK_ERASE_64K}, 00072 {"M25P10" , 0x00112020, 128 * 1024, 256, 32 * 1024, AT26_BLOCK_ERASE_64K}, 00073 {"M25P20" , 0x00122020, 256 * 1024, 256, 64 * 1024, AT26_BLOCK_ERASE_64K}, 00074 {"M25P40" , 0x00132020, 512 * 1024, 256, 64 * 1024, AT26_BLOCK_ERASE_64K}, 00075 {"M25P80" , 0x00142020, 1 * 1024 * 1024, 256, 64 * 1024, AT26_BLOCK_ERASE_64K}, 00076 {"M25P16" , 0x00152020, 2 * 1024 * 1024, 256, 64 * 1024, AT26_BLOCK_ERASE_64K}, 00077 {"M25P32" , 0x00162020, 4 * 1024 * 1024, 256, 64 * 1024, AT26_BLOCK_ERASE_64K}, 00078 {"M25P64" , 0x00172020, 8 * 1024 * 1024, 256, 64 * 1024, AT26_BLOCK_ERASE_64K}, 00079 // Manufacturer: Windbond 00080 {"W25X10" , 0x001130EF, 128 * 1024, 256, 64 * 1024, AT26_BLOCK_ERASE_64K}, 00081 {"W25X20" , 0x001230EF, 256 * 1024, 256, 64 * 1024, AT26_BLOCK_ERASE_64K}, 00082 {"W25X40" , 0x001330EF, 512 * 1024, 256, 64 * 1024, AT26_BLOCK_ERASE_64K}, 00083 {"W25X80" , 0x001430EF, 1 * 1024 * 1024, 256, 64 * 1024, AT26_BLOCK_ERASE_64K}, 00084 // Manufacturer: Macronix 00085 {"MX25L512" , 0x001020C2, 64 * 1024, 256, 64 * 1024, AT26_BLOCK_ERASE_64K}, 00086 {"MX25L3205" , 0x001620C2, 4 * 1024 * 1024, 256, 64 * 1024, AT26_BLOCK_ERASE_64K}, 00087 {"MX25L6405" , 0x001720C2, 8 * 1024 * 1024, 256, 64 * 1024, AT26_BLOCK_ERASE_64K}, 00088 // Other 00089 {"SST25VF512" , 0x000048BF, 64 * 1024, 256, 32 * 1024, AT26_BLOCK_ERASE_32K} 00090 }; 00091 00092 //------------------------------------------------------------------------------ 00093 // Exported functions 00094 //------------------------------------------------------------------------------ 00095 00096 //------------------------------------------------------------------------------ 00097 /// Initializes an AT26 driver instance with the given SPI driver and chip 00098 /// select value. 00099 /// \param pAt26 Pointer to an AT26 driver instance. 00100 /// \param pSpid Pointer to an SPI driver instance. 00101 /// \param cs Chip select value to communicate with the serial flash. 00102 //------------------------------------------------------------------------------ 00103 void AT26_Configure(At26 *pAt26, Spid *pSpid, unsigned char cs) 00104 { 00105 SpidCmd *pCommand; 00106 00107 assert(pAt26 != NULL); 00108 assert(pSpid != NULL); 00109 assert(cs < 4); 00110 00111 // Configure the SPI chip select for the serial flash 00112 SPID_ConfigureCS(pSpid, cs, CSR); 00113 00114 // Initialize the AT26 fields 00115 pAt26->pSpid = pSpid; 00116 pAt26->pDesc = 0; 00117 00118 // Initialize the command structure 00119 pCommand = &(pAt26->command); 00120 pCommand->pCmd = (unsigned char *) pAt26->pCmdBuffer; 00121 pCommand->callback = 0; 00122 pCommand->pArgument = 0; 00123 pCommand->spiCs = cs; 00124 } 00125 00126 //------------------------------------------------------------------------------ 00127 /// Returns 1 if the serial flash driver is currently busy executing a command; 00128 /// otherwise returns 0. 00129 /// \param pAt26 Pointer to an At26 driver instance. 00130 //------------------------------------------------------------------------------ 00131 unsigned char AT26_IsBusy(At26 *pAt26) 00132 { 00133 return SPID_IsBusy(pAt26->pSpid); 00134 } 00135 00136 //------------------------------------------------------------------------------ 00137 /// Sends a command to the serial flash through the SPI. The command is made up 00138 /// of two parts: the first is used to transmit the command byte and optionally, 00139 /// address and dummy bytes. The second part is the data to send or receive. 00140 /// This function does not block: it returns as soon as the transfer has been 00141 /// started. An optional callback can be invoked to notify the end of transfer. 00142 /// Return 0 if successful; otherwise, returns AT26_ERROR_BUSY if the AT26 00143 /// driver is currently executing a command, or AT26_ERROR_SPI if the command 00144 /// cannot be sent because of a SPI error. 00145 /// \param pAt26 Pointer to an At26 driver instance. 00146 /// \param cmd Command byte. 00147 /// \param cmdSize Size of command (command byte + address bytes + dummy bytes). 00148 /// \param pData Data buffer. 00149 /// \param dataSize Number of bytes to send/receive. 00150 /// \param address Address to transmit. 00151 /// \param callback Optional user-provided callback to invoke at end of transfer. 00152 /// \param pArgument Optional argument to the callback function. 00153 //------------------------------------------------------------------------------ 00154 unsigned char AT26_SendCommand( 00155 At26 *pAt26, 00156 unsigned char cmd, 00157 unsigned char cmdSize, 00158 unsigned char *pData, 00159 unsigned int dataSize, 00160 unsigned int address, 00161 SpidCallback callback, 00162 void *pArgument) 00163 00164 { 00165 SpidCmd *pCommand; 00166 00167 assert(pAt26 != NULL); 00168 00169 // Check if the SPI driver is available 00170 if (AT26_IsBusy(pAt26)) { 00171 00172 return AT26_ERROR_BUSY; 00173 } 00174 00175 // Store command and address in command buffer 00176 pAt26->pCmdBuffer[0] = (cmd & 0x000000FF) 00177 | ((address & 0x0000FF) << 24) 00178 | ((address & 0x00FF00) << 8) 00179 | ((address & 0xFF0000) >> 8); 00180 00181 // Update the SPI transfer descriptor 00182 pCommand = &(pAt26->command); 00183 pCommand->cmdSize = cmdSize; 00184 pCommand->pData = pData; 00185 pCommand->dataSize = dataSize; 00186 pCommand->callback = callback; 00187 pCommand->pArgument = pArgument; 00188 00189 // Start the SPI transfer 00190 if (SPID_SendCommand(pAt26->pSpid, pCommand)) { 00191 00192 return AT26_ERROR_SPI; 00193 } 00194 00195 return 0; 00196 } 00197 00198 //------------------------------------------------------------------------------ 00199 /// Tries to detect a serial firmware flash device given its JEDEC identifier. 00200 /// The JEDEC id can be retrieved by sending the correct command to the device. 00201 /// Returns the corresponding AT26 descriptor if found; otherwise returns 0. 00202 /// \param pAt26 Pointer to an AT26 driver instance. 00203 /// \param jedecId JEDEC identifier of device. 00204 //------------------------------------------------------------------------------ 00205 const At26Desc * AT26_FindDevice(At26 *pAt26, unsigned int jedecId) 00206 { 00207 unsigned int i = 0; 00208 00209 assert(pAt26 != NULL); 00210 00211 // Search if device is recognized 00212 pAt26->pDesc = 0; 00213 while ((i < NUMDATAFLASH) && !(pAt26->pDesc)) { 00214 00215 if (jedecId == at26Devices[i].jedecId) { 00216 00217 pAt26->pDesc = &(at26Devices[i]); 00218 } 00219 00220 i++; 00221 } 00222 00223 return( pAt26->pDesc ); 00224 } 00225