1 /* 2 * Copyright (c) 2022 Winner Microelectronics Co., Ltd. All rights reserved. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 /** 17 * @file wm_flash.h 18 * 19 * @brief flash Driver module 20 * 21 * @author dave 22 * 23 * Copyright (c) 2015 Winner Microelectronics Co., Ltd. 24 */ 25 #ifndef WM_FLASH_H 26 #define WM_FLASH_H 27 28 #include "wm_type_def.h" 29 #include "wm_osal.h" 30 31 #define TLS_FLS_STATUS_OK (0) 32 #define TLS_FLS_STATUS_EINVAL (1) 33 #define TLS_FLS_STATUS_EBUSY (2) 34 #define TLS_FLS_STATUS_EPERM (3) 35 #define TLS_FLS_STATUS_ENOSUPPORT (4) 36 #define TLS_FLS_STATUS_EEXIST (5) 37 #define TLS_FLS_STATUS_ENOMEM (6) 38 #define TLS_FLS_STATUS_EOVERFLOW (7) 39 #define TLS_FLS_STATUS_ENODEV (8) 40 #define TLS_FLS_STATUS_EDEV (9) 41 #define TLS_FLS_STATUS_EIO (10) 42 #define TLS_FLS_STATUS_ENODRV (11) 43 44 #define TLS_FLS_PARAM_TYPE_ID (0) 45 #define TLS_FLS_PARAM_TYPE_SIZE (1) 46 #define TLS_FLS_PARAM_TYPE_PAGE_SIZE (2) 47 #define TLS_FLS_PARAM_TYPE_PROG_SIZE (3) 48 #define TLS_FLS_PARAM_TYPE_SECTOR_SIZE (4) 49 50 #define TLS_FLS_FLAG_UNDER_PROTECT (1<<0) 51 #define TLS_FLS_FLAG_FAST_READ (1<<1) 52 #define TLS_FLS_FLAG_AAAI (1<<2) 53 54 #define FLS_CMD_READ_DEV_ID (0x9F) // read device id // (0x9f) 55 56 /** 57 * @struct fls_list list 58 */ 59 struct fls_list { 60 struct fls_list *next; 61 struct fls_list *prev; 62 }; 63 64 /** 65 * @struct tls_fls_drv flash driver 66 */ 67 struct tls_fls_drv { 68 struct fls_list drv_list; 69 u32 id; 70 u32 total_size; 71 u32 page_size; 72 u32 program_size; 73 u32 sector_size; 74 u32 clock; 75 u8 mode; 76 u8 cs_active; 77 u8 flags; 78 int (*read) (u32, u8 *, u32); 79 int (*fast_read) (u32, u8 *, u32); 80 int (*page_write) (u32, u8 *); 81 int (*erase) (u32); 82 int (*chip_erase) (void); 83 int (*probe)(u32 id); 84 void (*remove) (void); 85 }; 86 87 /** 88 * @struct tls_fls flash 89 */ 90 struct tls_fls { 91 struct fls_list fls_drvs; 92 struct tls_fls_drv *current_drv; 93 tls_os_sem_t *fls_lock; 94 }; 95 96 /** 97 * @defgroup Driver_APIs Driver APIs 98 * @brief Driver APIs 99 */ 100 101 /** 102 * @addtogroup Driver_APIs 103 * @{ 104 */ 105 106 /** 107 * @defgroup SPIFLASH_Driver_APIs SPI FLASH Driver APIs 108 * @brief SPI FLASH driver APIs 109 */ 110 111 /** 112 * @addtogroup SPIFLASH_Driver_APIs 113 * @{ 114 */ 115 116 /** 117 * @brief This function is used to initial flash module structer. 118 * 119 * @param[in] None 120 * 121 * @retval TLS_FLS_STATUS_OK if init sucsess 122 * @retval TLS_FLS_STATUS_EBUSY already inited 123 * @retval TLS_FLS_STATUS_ENOMEM memory error 124 * 125 * @note None 126 */ 127 int tls_spifls_init(void); 128 129 /** 130 * @brief This function is used to read data from the flash. 131 * 132 * @param[in] addr Specifies the starting address to read from 133 * @param[in] buf Pointer to a byte array that is to be written. 134 * @param[in] len length to read. 135 * 136 * @retval TLS_FLS_STATUS_OK if read sucsess 137 * @retval TLS_FLS_STATUS_EIO if read fail 138 * 139 * @note None 140 */ 141 int tls_spifls_read(u32 addr, u8 *buf, u32 len); 142 143 /** 144 * @brief This function is used to write data into the flash. 145 * 146 * @param[in] addr Specifies the starting address to write to. 147 * @param[in] buf Pointer to a byte array that holds the data to be written. 148 * @param[in] len length to write. 149 * 150 * @retval TLS_FLS_STATUS_OK if write flash success 151 * @retval TLS_FLS_STATUS_EPERM if flash struct point is null 152 * @retval TLS_FLS_STATUS_ENODRV if flash driver is not installed 153 * @retval TLS_FLS_STATUS_EINVAL if argument is invalid 154 * @retval TLS_FLS_STATUS_EIO if io error 155 * 156 * @note None 157 */ 158 int tls_spifls_write(u32 addr, u8 *buf, u32 len); 159 160 /** 161 * @} 162 */ 163 164 /** 165 * @} 166 */ 167 168 #endif /* WM_FLASH_H */ 169