Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:39

0001 /*
0002  * Copyright (C) 2024 Contemporary Software
0003  *
0004  * Redistribution and use in source and binary forms, with or without
0005  * modification, are permitted provided that the following conditions
0006  * are met:
0007  * 1. Redistributions of source code must retain the above copyright
0008  *    notice, this list of conditions and the following disclaimer.
0009  * 2. Redistributions in binary form must reproduce the above copyright
0010  *    notice, this list of conditions and the following disclaimer in the
0011  *    documentation and/or other materials provided with the distribution.
0012  *
0013  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
0014  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0015  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0016  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0017  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0018  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0019  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0020  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0021  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0022  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0023  * POSSIBILITY OF SUCH DAMAGE.
0024  */
0025 
0026 #if !defined(_ZYNQ_QSPI_FLASH_H_)
0027 #define _ZYNQ_QSPI_FLASH_H_
0028 
0029 #include <stdbool.h>
0030 #include <stdlib.h>
0031 
0032 /*
0033  * Driver configuration.
0034  */
0035 #define ZQSPI_FLASH_4BYTE_ADDRESSING  1
0036 #define ZQSPI_FLASH_FAST_READ         1
0037 
0038 #define ZQSPI_FLASH_COMMAND_OFFSET    (0) /* FLASH instruction */
0039 #define ZQSPI_FLASH_ADDRESS_1_OFFSET  (1) /* Bits 31-24 of the address */
0040 #define ZQSPI_FLASH_ADDRESS_2_OFFSET  (2) /* Bits 23-16 of the address */
0041 #define ZQSPI_FLASH_ADDRESS_3_OFFSET  (3) /* Bits 16-8 of the address */
0042 #if ZQSPI_FLASH_4BYTE_ADDRESSING
0043  #define ZQSPI_FLASH_ADDRESS_4_OFFSET (4) /* Bits 8-0 of the address */
0044  #define ZQSPI_FLASH_DATA_OFFSET      (5) /* Start of Data for Read/Write */
0045 #else
0046  #define ZQSPI_FLASH_DATA_OFFSET      (4) /* Start of Data for Read/Write */
0047 #endif
0048 #define ZQSPI_FLASH_SPI_MAX_PADDING   (4) /* Maximum amount of padding. */
0049 
0050 #define ZQSPI_FLASH_TX_TRANS 0
0051 #define ZQSPI_FLASH_RX_TRANS 1
0052 
0053 #define ZQSPI_FLASH_BUFFER_SIZE 0x1008
0054 
0055 #define ZQPSI_ZYNQ_QSPI_IRQ 51
0056 
0057 #define ZQSPI_TIMEOUT_US   5000000U
0058 #define ZQSPI_TIMEOUT_TICKS (ZQSPI_TIMEOUT_US \
0059     / rtems_configuration_get_microseconds_per_tick())
0060 
0061 
0062 /*
0063  * Zynq QSPI Flash driver errors.
0064  */
0065 typedef enum
0066 {
0067   ZQSPI_FLASH_NO_ERROR = 0,
0068   ZQSPI_FLASH_NOT_OPEN,
0069   ZQSPI_FLASH_ALREADY_OPEN,
0070   ZQSPI_FLASH_NO_MEMORY,
0071   ZQSPI_FLASH_4BYTE_ADDR_NOT_SUPPORTED,
0072   ZQSPI_FLASH_BUFFER_OVERFLOW,
0073   ZQSPI_FLASH_BUFFER_UNDERFLOW,
0074   ZQSPI_FLASH_BAD_ADDRESS,
0075   ZQSPI_FLASH_NOT_BLANK,
0076   ZQSPI_FLASH_ERASE_FAILURE,
0077   ZQSPI_FLASH_READ_ONLY,
0078   ZQSPI_FLASH_WRITE_LATCH_CLEAR_FAIL,
0079   ZQSPI_FLASH_WRITE_LOCK_FAIL,
0080   ZQSPI_FLASH_WRITE_ACROSS_SECTION,
0081   ZQSPI_FLASH_WRITE_ERASE_CMD_FAIL,
0082   ZQSPI_FLASH_LOCK_FAIL,
0083   ZQSPI_FLASH_INVALID_DEVICE,
0084   ZQPSI_FLASH_TRANSFER_FAILED,
0085   ZQSPI_FLASH_RTEMS_INTR
0086 } zqspi_error;
0087 
0088 /*
0089  * A transfer buffer.
0090  */
0091 typedef struct
0092 {
0093   size_t    size;
0094   size_t    length;
0095   size_t    padding;
0096   size_t    in;
0097   size_t    out;
0098   uint32_t  trans_dir;
0099   uint32_t  command_len;
0100   uint8_t   *buffer;
0101   uint32_t  *tx_data;
0102   uint32_t  *rx_data;
0103   size_t    tx_length;
0104   size_t    rx_length;
0105   size_t    sending;
0106   bool      start;
0107 } zqspi_transfer_buffer;
0108 
0109 /*
0110  * A flash driver instance
0111  */
0112 typedef struct
0113 {
0114   zqspi_transfer_buffer buf;
0115   bool                  initialised;
0116   uint32_t              jedec_id;
0117   uint64_t              flash_size;
0118   uint32_t              flash_read_dummies;
0119   uint32_t              flash_erase_sector_size;
0120   uint32_t              flash_page_size;
0121 } zqspiflash;
0122 
0123 /**
0124  * @brief Initialize zqspiflash driver.
0125  *
0126  * @param zqspiflash Device context.
0127  *
0128  * @retval zqspi_error code.
0129  */
0130 zqspi_error zqspi_init(zqspiflash *driver);
0131 
0132 /**
0133  * @brief Close zqspiflash driver.
0134  *
0135  * @param zqspiflash Device context.
0136  */
0137 void zqspi_close(zqspiflash *driver);
0138 
0139 /**
0140  * @brief Read from zqspiflash.
0141  *
0142  * @param zqspiflash Device context.
0143  * @param address Address to read from.
0144  * @param buffer Buffer to read data into.
0145  * @param length Length of requested read.
0146  *
0147  * @retval zqspi_error Error code.
0148  */
0149 zqspi_error zqspi_read(
0150   zqspiflash *driver,
0151   uint32_t address,
0152   void* buffer,
0153   size_t length
0154 );
0155 
0156 /**
0157  * @brief Write to zqspiflash.
0158  *
0159  * @param zqspiflash Device context.
0160  * @param address Address to write to.
0161  * @param buffer Buffer to write data from.
0162  * @param length Length of requested write.
0163  *
0164  * @retval zqspi_error Error code.
0165  */
0166 zqspi_error zqspi_write(
0167   zqspiflash *driver,
0168   uint32_t address,
0169   const void* buffer,
0170   size_t length
0171 );
0172 
0173 /**
0174  * @brief Write to zqspiflash.
0175  *
0176  * @param zqspiflash Device context.
0177  * @param address Address to erase.
0178  * @param length Length of requested erase.
0179  *
0180  * @retval zqspi_error Error code.
0181  */
0182 zqspi_error zqspi_erase(
0183   zqspiflash *driver,
0184   uint32_t address,
0185   size_t length
0186 );
0187 
0188 /**
0189  * @brief Blank check zqspiflash.
0190  *
0191  * @param zqspiflash Device context.
0192  * @param address Address to check.
0193  * @param length Length of requested check.
0194  *
0195  * @retval zqspi_error Error code.
0196  */
0197 zqspi_error zqspi_blank(
0198   zqspiflash *driver,
0199   uint32_t address,
0200   size_t length
0201 );
0202 
0203 /**
0204  * @brief Erase sector of zqspiflash.
0205  *
0206  * @param zqspiflash Device context.
0207  * @param address Address of sector to erase.
0208  *
0209  * @retval zqspi_error Error code.
0210  */
0211 zqspi_error zqspi_erase_sector(
0212   zqspiflash *driver,
0213   uint32_t address
0214 );
0215 
0216 /**
0217  * @brief Write sector to zqspiflash.
0218  *
0219  * @param zqspiflash Device context.
0220  * @param address Address to write to.
0221  * @param length Length of requested write.
0222  *
0223  * @retval zqspi_error Error code.
0224  */
0225 zqspi_error zqspi_write_sector(
0226   zqspiflash *driver,
0227   uint32_t address,
0228   const void* buffer,
0229   size_t length
0230 );
0231 
0232 /**
0233  * @brief Erases all of zqspiflash.
0234  *
0235  * @param zqspiflash Device context.
0236  *
0237  * @retval zqspi_error error code.
0238  */
0239 zqspi_error zqspi_erase_device(zqspiflash *driver);
0240 
0241 /**
0242  * @brief Get JEDEC ID of zqspiflash.
0243  *
0244  * @param zqspiflash Device context.
0245  * @param jedec_id Pounter to uin32_t which the JEDEC ID is
0246  * returned in.
0247  *
0248  * @retval zqspi_error error code.
0249  */
0250 zqspi_error zqspi_readid(zqspiflash *driver, uint32_t *jedec_id);
0251 
0252 /**
0253  * @brief Get JEDEC ID of zqspiflash.
0254  *
0255  * @param zqspiflash Device context.
0256  *
0257  * @retval size Size of flash.
0258  */
0259 size_t zqspi_device_size(zqspiflash *driver);
0260 
0261 /**
0262  * @brief Get size of erase sectors of zqspiflash.
0263  *
0264  * @param zqspiflash Device context.
0265  *
0266  * @retval size Size of erase sector of flash.
0267  */
0268 size_t zqspi_device_sector_erase_size(zqspiflash *driver);
0269 
0270 /**
0271  * @brief If debug enabled prints trace of flash transfer.
0272  *
0273  * @param message Trace header.
0274  * @param transfer Transfer to print.
0275  */
0276 void zqspi_transfer_trace(
0277   const char* message,
0278   const zqspi_transfer_buffer* transfer
0279 );
0280 
0281 /**
0282  * @brief Writes a uint32_t to a ZYNQ7000 QSPI register.
0283  *
0284  * @param reg Register offset.
0285  * @param value Value to write.
0286  */
0287 void qspi_reg_write(uint32_t reg, uint32_t value);
0288 
0289 /**
0290  * @brief Read a uint32_t from a ZYNQ7000 QSPI register.
0291  *
0292  * @param reg Register offset.
0293  *
0294  * @retval value Value read from register.
0295  */
0296 uint32_t qspi_reg_read(uint32_t reg);
0297 
0298 /**
0299  * @brief Skip value in transfer buffer.
0300  *
0301  * @param transfer Buffer work on.
0302  * @param size Number of bytes to skip.
0303  *
0304  * @retval zqspi_error error code.
0305  */
0306 zqspi_error zqspi_transfer_buffer_skip(
0307   zqspi_transfer_buffer* transfer,
0308   const size_t size
0309 );
0310 
0311 #endif /* _ZYNQ_QSPI_FLASH_H_ */