Mistake on this page?
Report an issue in GitHub or email us
SFDP.h
1 /*
2  * Copyright (c) 2020, Arm Limited and affiliates.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef SFDP_H
19 #define SFDP_H
20 
21 #include <cstddef>
22 #include <cstdint>
23 #include "blockdevice/BlockDevice.h"
24 #include "platform/Callback.h"
25 
26 namespace mbed {
27 
28 /** \defgroup drivers-internal-api-sfdp SFDP
29  * \ingroup drivers-internal-api
30  * Serial Flash Discoverable Parameters.
31  *
32  * Based on <a href="https://www.jedec.org/standards-documents/docs/jesd216b">JESD216D.01 Standard</a>.
33  * @{
34  */
35 
36 constexpr int SFDP_HEADER_SIZE = 8; ///< Size of an SFDP header in bytes, 2 DWORDS
37 constexpr int SFDP_BASIC_PARAMS_TBL_SIZE = 80; ///< Basic Parameter Table size in bytes, 20 DWORDS
38 constexpr int SFDP_SECTOR_MAP_MAX_REGIONS = 10; ///< Maximum number of regions with different erase granularity
39 
40 // Erase Types Per Region BitMask
41 constexpr int SFDP_ERASE_BITMASK_TYPE4 = 0x08; ///< Erase type 4 (erase granularity) identifier
42 constexpr int SFDP_ERASE_BITMASK_TYPE3 = 0x04; ///< Erase type 3 (erase granularity) identifier
43 constexpr int SFDP_ERASE_BITMASK_TYPE2 = 0x02; ///< Erase type 2 (erase granularity) identifier
44 constexpr int SFDP_ERASE_BITMASK_TYPE1 = 0x01; ///< Erase type 1 (erase granularity) identifier
45 constexpr int SFDP_ERASE_BITMASK_NONE = 0x00; ///< Erase type None
46 constexpr int SFDP_ERASE_BITMASK_ALL = 0x0F; ///< Erase type All
47 
48 constexpr int SFDP_MAX_NUM_OF_ERASE_TYPES = 4; ///< Maximum number of different erase types (erase granularity)
49 
50 // Size of a command specified by SFDP
51 enum sfdp_cmd_addr_size_t {
52  SFDP_CMD_ADDR_NONE = 0x00, // No address in command
53  SFDP_CMD_ADDR_3_BYTE = 0x01, // 3-byte address
54  SFDP_CMD_ADDR_4_BYTE = 0x02, // 4-byte address
55  SFDP_CMD_ADDR_SIZE_VARIABLE = 0x03 // Address size from current setting
56 };
57 
58 // Parameters for SFDP Read command
59 constexpr sfdp_cmd_addr_size_t SFDP_READ_CMD_ADDR_TYPE = SFDP_CMD_ADDR_3_BYTE; // Read SFDP has 3-byte address
60 constexpr uint8_t SFDP_READ_CMD_INST = 0x5A; // Read SFDP instruction
61 constexpr uint8_t SFDP_READ_CMD_DUMMY_CYCLES = 8; // READ SFDP dummy cycles
62 
63 // Special value from SFDP for using dummy cycles from current setting
64 constexpr uint8_t SFDP_CMD_DUMMY_CYCLES_VARIABLE = 0xF;
65 
66 /** JEDEC Basic Flash Parameter Table info */
68  uint32_t addr; ///< Address
69  size_t size; ///< Size
70  bd_size_t device_size_bytes;
71  int legacy_erase_instruction; ///< Legacy 4K erase instruction
72 };
73 
74 /** JEDEC Sector Map Table info */
76  uint32_t addr; ///< Address
77  size_t size; ///< Size
78  int region_cnt; ///< Number of erase regions
79  int region_size[SFDP_SECTOR_MAP_MAX_REGIONS]; ///< Erase region size in bytes
80  uint8_t region_erase_types_bitfld[SFDP_SECTOR_MAP_MAX_REGIONS]; ///< Each Region can support a bit combination of any of the 4 Erase Types
81  unsigned int regions_min_common_erase_size; ///< Minimal common erase size for all regions (0 if none exists)
82  bd_size_t region_high_boundary[SFDP_SECTOR_MAP_MAX_REGIONS]; ///< Region high address offset boundary
83  int erase_type_inst_arr[SFDP_MAX_NUM_OF_ERASE_TYPES]; ///< // Up To 4 Erase Types are supported by SFDP (each with its own command Instruction and Size)
84  unsigned int erase_type_size_arr[SFDP_MAX_NUM_OF_ERASE_TYPES]; ///< Erase sizes for all different erase types
85 };
86 
87 /** JEDEC 4-byte Address Instruction Parameter Table info */
89  uint32_t addr; ///< Address
90  size_t size; ///< Size
91  int erase_type_4_byte_inst_arr[SFDP_MAX_NUM_OF_ERASE_TYPES]; ///< // Up To 4 Erase Types are supported by SFDP (each with its own command Instruction and Size)
92 };
93 
94 /** SFDP JEDEC Parameter Table info */
95 struct sfdp_hdr_info {
96  sfdp_bptbl_info bptbl;
97  sfdp_smptbl_info smptbl;
98  sfdp_fbatbl_info fbatbl;
99 };
100 
101 /** Parse SFDP Database
102  * Retrieves all headers from within a memory device and parses the information contained by the headers
103  *
104  * Only JEDEC headers are parsed, not vendor specific ones.
105  *
106  * @param sfdp_reader Callback function used to read headers from within a device
107  * @param[out] sfdp_info Contains the results of parsing the SFDP Database JEDEC headers
108  *
109  * @return MBED_SUCCESS on success, negative error code on failure
110  */
111 int sfdp_parse_headers(Callback<int(bd_addr_t, sfdp_cmd_addr_size_t, uint8_t, uint8_t, void *, bd_size_t)> sfdp_reader, sfdp_hdr_info &sfdp_info);
112 
113 /** Parse Sector Map Parameter Table
114  * Retrieves the table from a device and parses the information contained by the table
115  *
116  * @param sfdp_reader Callback function used to read headers from within a device
117  * @param[out] sfdp_info Contains the results of parsing the JEDEC Sector Map Table
118  *
119  * @return MBED_SUCCESS on success, negative error code on failure
120  */
121 int sfdp_parse_sector_map_table(Callback<int(bd_addr_t, sfdp_cmd_addr_size_t, uint8_t, uint8_t, void *, bd_size_t)> sfdp_reader, sfdp_hdr_info &sfdp_info);
122 
123 /** Detect page size used for writing on flash
124  *
125  * @param bptbl_ptr Pointer to memory holding a Basic Parameter Table structure
126  * @param bptbl_size Size of memory holding the Basic Parameter Table
127  *
128  * @return Page size
129  */
130 size_t sfdp_detect_page_size(uint8_t *bptbl_ptr, size_t bptbl_size);
131 
132 /** Detect all supported erase types
133  *
134  * @param bptbl_ptr Pointer to memory holding a JEDEC Basic Flash Parameter Table
135  * @param[in,out] sfdp_info Contains the results of parsing erase type instructions and sizes
136  *
137  * @return MBED_SUCCESS on success, negative error code on failure
138  */
139 int sfdp_detect_erase_types_inst_and_size(uint8_t *bptbl_ptr, sfdp_hdr_info &sfdp_info);
140 
141 /** Find the region to which the given offset belongs to
142  *
143  * @param offset Offset value
144  * @param sfdp_info Region information
145  *
146  * @return Region number
147  */
148 int sfdp_find_addr_region(bd_addr_t offset, const sfdp_hdr_info &sfdp_info);
149 
150 /** Finds the largest Erase Type of the Region to which the offset belongs to
151  *
152  * Iterates from highest type to lowest.
153  *
154  * @param bitfield Erase types bit field
155  * @param size Upper limit for region size
156  * @param offset Offset value
157  * @param region Region number
158  * @param smptbl Information about different erase types
159  *
160  * @return Largest erase type, or -1 if none matches the given address and size
161  */
162 int sfdp_iterate_next_largest_erase_type(uint8_t bitfield,
163  bd_size_t size,
164  bd_addr_t offset,
165  int region,
166  const sfdp_smptbl_info &smptbl);
167 
168 /** Detect device density
169  *
170  * @param bptbl_ptr Pointer to memory holding a Basic Parameter Table structure
171  * @param bptbl_info Basic Parameter Table information structure
172  *
173  * @return 0 on success, negative error code on failure
174  */
175 int sfdp_detect_device_density(uint8_t *bptbl_ptr, sfdp_bptbl_info &bptbl_info);
176 
177 /** Detect is it possible to access the whole memory region
178  *
179  * @param bptbl_ptr Pointer to memory holding a Basic Parameter Table structure
180  * @param bptbl_info Basic Parameter Table information structure
181  *
182  * @return 0 on success, negative error code on failure
183  */
184 int sfdp_detect_addressability(uint8_t *bptbl_ptr, sfdp_bptbl_info &bptbl_info);
185 
186 /** @}*/
187 } /* namespace mbed */
188 #endif
JEDEC 4-byte Address Instruction Parameter Table info.
Definition: SFDP.h:88
int sfdp_parse_headers(Callback< int(bd_addr_t, sfdp_cmd_addr_size_t, uint8_t, uint8_t, void *, bd_size_t)> sfdp_reader, sfdp_hdr_info &sfdp_info)
Parse SFDP Database Retrieves all headers from within a memory device and parses the information cont...
constexpr int SFDP_SECTOR_MAP_MAX_REGIONS
Maximum number of regions with different erase granularity.
Definition: SFDP.h:38
JEDEC Basic Flash Parameter Table info.
Definition: SFDP.h:67
uint32_t addr
Address.
Definition: SFDP.h:89
constexpr int SFDP_ERASE_BITMASK_TYPE3
Erase type 3 (erase granularity) identifier.
Definition: SFDP.h:42
constexpr int SFDP_MAX_NUM_OF_ERASE_TYPES
Maximum number of different erase types (erase granularity)
Definition: SFDP.h:48
uint32_t addr
Address.
Definition: SFDP.h:68
int sfdp_parse_sector_map_table(Callback< int(bd_addr_t, sfdp_cmd_addr_size_t, uint8_t, uint8_t, void *, bd_size_t)> sfdp_reader, sfdp_hdr_info &sfdp_info)
Parse Sector Map Parameter Table Retrieves the table from a device and parses the information contain...
constexpr int SFDP_ERASE_BITMASK_ALL
Erase type All.
Definition: SFDP.h:46
int sfdp_detect_erase_types_inst_and_size(uint8_t *bptbl_ptr, sfdp_hdr_info &sfdp_info)
Detect all supported erase types.
int legacy_erase_instruction
Legacy 4K erase instruction.
Definition: SFDP.h:71
int sfdp_detect_addressability(uint8_t *bptbl_ptr, sfdp_bptbl_info &bptbl_info)
Detect is it possible to access the whole memory region.
SFDP JEDEC Parameter Table info.
Definition: SFDP.h:95
int region_cnt
Number of erase regions.
Definition: SFDP.h:78
constexpr int SFDP_HEADER_SIZE
Size of an SFDP header in bytes, 2 DWORDS.
Definition: SFDP.h:36
int sfdp_iterate_next_largest_erase_type(uint8_t bitfield, bd_size_t size, bd_addr_t offset, int region, const sfdp_smptbl_info &smptbl)
Finds the largest Erase Type of the Region to which the offset belongs to.
constexpr int SFDP_ERASE_BITMASK_TYPE4
Erase type 4 (erase granularity) identifier.
Definition: SFDP.h:41
unsigned int regions_min_common_erase_size
Minimal common erase size for all regions (0 if none exists)
Definition: SFDP.h:81
constexpr int SFDP_BASIC_PARAMS_TBL_SIZE
Basic Parameter Table size in bytes, 20 DWORDS.
Definition: SFDP.h:37
size_t size
Size.
Definition: SFDP.h:90
size_t sfdp_detect_page_size(uint8_t *bptbl_ptr, size_t bptbl_size)
Detect page size used for writing on flash.
int sfdp_find_addr_region(bd_addr_t offset, const sfdp_hdr_info &sfdp_info)
Find the region to which the given offset belongs to.
size_t size
Size.
Definition: SFDP.h:69
size_t size
Size.
Definition: SFDP.h:77
constexpr int SFDP_ERASE_BITMASK_NONE
Erase type None.
Definition: SFDP.h:45
JEDEC Sector Map Table info.
Definition: SFDP.h:75
int sfdp_detect_device_density(uint8_t *bptbl_ptr, sfdp_bptbl_info &bptbl_info)
Detect device density.
Callback class based on template specialization.
Definition: Callback.h:53
Definition: ATHandler.h:46
constexpr int SFDP_ERASE_BITMASK_TYPE1
Erase type 1 (erase granularity) identifier.
Definition: SFDP.h:44
uint32_t addr
Address.
Definition: SFDP.h:76
constexpr int SFDP_ERASE_BITMASK_TYPE2
Erase type 2 (erase granularity) identifier.
Definition: SFDP.h:43
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.