1 /*
2  * Copyright (c) 2017 Nordic Semiconductor ASA
3  * Copyright (c) 2016 Intel Corporation
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 /**
9  * @file
10  * @brief Public API for FLASH drivers
11  */
12 
13 #ifndef ZEPHYR_INCLUDE_DRIVERS_FLASH_H_
14 #define ZEPHYR_INCLUDE_DRIVERS_FLASH_H_
15 
16 /**
17  * @brief FLASH internal Interface
18  * @defgroup flash_internal_interface FLASH internal Interface
19  * @ingroup io_interfaces
20  * @{
21  */
22 
23 #include <zephyr/types.h>
24 #include <stddef.h>
25 #include <sys/types.h>
26 #include <device.h>
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 #if defined(CONFIG_FLASH_PAGE_LAYOUT)
33 struct flash_pages_layout {
34 	size_t pages_count; /* count of pages sequence of the same size */
35 	size_t pages_size;
36 };
37 #endif /* CONFIG_FLASH_PAGE_LAYOUT */
38 
39 /**
40  * @}
41  */
42 
43 /**
44  * @brief FLASH Interface
45  * @defgroup flash_interface FLASH Interface
46  * @ingroup io_interfaces
47  * @{
48  */
49 
50 /**
51  * Flash memory parameters. Contents of this structure suppose to be
52  * filled in during flash device initialization and stay constant
53  * through a runtime.
54  */
55 struct flash_parameters {
56 	const size_t write_block_size;
57 	uint8_t erase_value; /* Byte value of erased flash */
58 };
59 
60 /**
61  * @}
62  */
63 
64 /**
65  * @addtogroup flash_internal_interface
66  * @{
67  */
68 
69 typedef int (*flash_api_read)(const struct device *dev, off_t offset,
70 			      void *data,
71 			      size_t len);
72 /**
73  * @brief Flash write implementation handler type
74  *
75  * @note Any necessary write protection management must be performed by
76  * the driver, with the driver responsible for ensuring the "write-protect"
77  * after the operation completes (successfully or not) matches the write-protect
78  * state when the operation was started.
79  */
80 typedef int (*flash_api_write)(const struct device *dev, off_t offset,
81 			       const void *data, size_t len);
82 
83 /**
84  * @brief Flash erase implementation handler type
85  *
86  * @note Any necessary erase protection management must be performed by
87  * the driver, with the driver responsible for ensuring the "erase-protect"
88  * after the operation completes (successfully or not) matches the erase-protect
89  * state when the operation was started.
90  */
91 typedef int (*flash_api_erase)(const struct device *dev, off_t offset,
92 			       size_t size);
93 
94 /*  This API is deprecated and will be removed in Zephyr 2.8. */
95 typedef int (*flash_api_write_protection)(const struct device *dev,
96 					  bool enable);
97 typedef const struct flash_parameters* (*flash_api_get_parameters)(const struct device *dev);
98 
99 #if defined(CONFIG_FLASH_PAGE_LAYOUT)
100 /**
101  * @brief Retrieve a flash device's layout.
102  *
103  * A flash device layout is a run-length encoded description of the
104  * pages on the device. (Here, "page" means the smallest erasable
105  * area on the flash device.)
106  *
107  * For flash memories which have uniform page sizes, this routine
108  * returns an array of length 1, which specifies the page size and
109  * number of pages in the memory.
110  *
111  * Layouts for flash memories with nonuniform page sizes will be
112  * returned as an array with multiple elements, each of which
113  * describes a group of pages that all have the same size. In this
114  * case, the sequence of array elements specifies the order in which
115  * these groups occur on the device.
116  *
117  * @param dev         Flash device whose layout to retrieve.
118  * @param layout      The flash layout will be returned in this argument.
119  * @param layout_size The number of elements in the returned layout.
120  */
121 typedef void (*flash_api_pages_layout)(const struct device *dev,
122 				       const struct flash_pages_layout **layout,
123 				       size_t *layout_size);
124 #endif /* CONFIG_FLASH_PAGE_LAYOUT */
125 
126 typedef int (*flash_api_sfdp_read)(const struct device *dev, off_t offset,
127 				   void *data, size_t len);
128 typedef int (*flash_api_read_jedec_id)(const struct device *dev, uint8_t *id);
129 
130 __subsystem struct flash_driver_api {
131 	flash_api_read read;
132 	flash_api_write write;
133 	flash_api_erase erase;
134 	flash_api_write_protection write_protection;
135 	flash_api_get_parameters get_parameters;
136 #if defined(CONFIG_FLASH_PAGE_LAYOUT)
137 	flash_api_pages_layout page_layout;
138 #endif /* CONFIG_FLASH_PAGE_LAYOUT */
139 #if defined(CONFIG_FLASH_JESD216_API)
140 	flash_api_sfdp_read sfdp_read;
141 	flash_api_read_jedec_id read_jedec_id;
142 #endif /* CONFIG_FLASH_JESD216_API */
143 };
144 
145 /**
146  * @}
147  */
148 
149 /**
150  * @addtogroup flash_interface
151  * @{
152  */
153 
154 /**
155  *  @brief  Read data from flash
156  *
157  *  All flash drivers support reads without alignment restrictions on
158  *  the read offset, the read size, or the destination address.
159  *
160  *  @param  dev             : flash dev
161  *  @param  offset          : Offset (byte aligned) to read
162  *  @param  data            : Buffer to store read data
163  *  @param  len             : Number of bytes to read.
164  *
165  *  @return  0 on success, negative errno code on fail.
166  */
167 __syscall int flash_read(const struct device *dev, off_t offset, void *data,
168 			 size_t len);
169 
z_impl_flash_read(const struct device * dev,off_t offset,void * data,size_t len)170 static inline int z_impl_flash_read(const struct device *dev, off_t offset,
171 				    void *data,
172 				    size_t len)
173 {
174 	const struct flash_driver_api *api =
175 		(const struct flash_driver_api *)dev->api;
176 
177 	return api->read(dev, offset, data, len);
178 }
179 
180 /**
181  *  @brief  Write buffer into flash memory.
182  *
183  *  All flash drivers support a source buffer located either in RAM or
184  *  SoC flash, without alignment restrictions on the source address.
185  *  Write size and offset must be multiples of the minimum write block size
186  *  supported by the driver.
187  *
188  *  Any necessary write protection management is performed by the driver
189  *  write implementation itself.
190  *
191  *  @param  dev             : flash device
192  *  @param  offset          : starting offset for the write
193  *  @param  data            : data to write
194  *  @param  len             : Number of bytes to write
195  *
196  *  @return  0 on success, negative errno code on fail.
197  */
198 __syscall int flash_write(const struct device *dev, off_t offset,
199 			  const void *data,
200 			  size_t len);
201 
z_impl_flash_write(const struct device * dev,off_t offset,const void * data,size_t len)202 static inline int z_impl_flash_write(const struct device *dev, off_t offset,
203 				     const void *data, size_t len)
204 {
205 	const struct flash_driver_api *api =
206 		(const struct flash_driver_api *)dev->api;
207 	int rc;
208 
209 	/* write protection management in this function exists for keeping
210 	 * compatibility with out-of-tree drivers which are not aligned jet
211 	 * with write-protection API depreciation.
212 	 * This will be removed with flash_api_write_protection handler type.
213 	 */
214 	if (api->write_protection != NULL) {
215 		rc = api->write_protection(dev, false);
216 		if (rc) {
217 			return rc;
218 		}
219 	}
220 
221 	rc = api->write(dev, offset, data, len);
222 
223 	if (api->write_protection != NULL) {
224 		(void) api->write_protection(dev, true);
225 	}
226 
227 	return rc;
228 }
229 
230 /**
231  *  @brief  Erase part or all of a flash memory
232  *
233  *  Acceptable values of erase size and offset are subject to
234  *  hardware-specific multiples of page size and offset. Please check
235  *  the API implemented by the underlying sub driver, for example by
236  *  using flash_get_page_info_by_offs() if that is supported by your
237  *  flash driver.
238  *
239  *  Any necessary erase protection management is performed by the driver
240  *  erase implementation itself.
241  *
242  *  @param  dev             : flash device
243  *  @param  offset          : erase area starting offset
244  *  @param  size            : size of area to be erased
245  *
246  *  @return  0 on success, negative errno code on fail.
247  *
248  *  @see flash_get_page_info_by_offs()
249  *  @see flash_get_page_info_by_idx()
250  */
251 __syscall int flash_erase(const struct device *dev, off_t offset, size_t size);
252 
z_impl_flash_erase(const struct device * dev,off_t offset,size_t size)253 static inline int z_impl_flash_erase(const struct device *dev, off_t offset,
254 				     size_t size)
255 {
256 	const struct flash_driver_api *api =
257 		(const struct flash_driver_api *)dev->api;
258 	int rc;
259 
260 	/* write protection management in this function exists for keeping
261 	 * compatibility with out-of-tree drivers which are not aligned jet
262 	 * with write-protection API depreciation.
263 	 * This will be removed with flash_api_write_protection handler type.
264 	 */
265 	if (api->write_protection != NULL) {
266 		rc = api->write_protection(dev, false);
267 		if (rc) {
268 			return rc;
269 		}
270 	}
271 
272 	rc = api->erase(dev, offset, size);
273 
274 	if (api->write_protection != NULL) {
275 		(void) api->write_protection(dev, true);
276 	}
277 
278 	return rc;
279 }
280 
281 /**
282  *  @brief  Enable or disable write protection for a flash memory
283  *
284  *  This API is deprecated and will be removed in Zephyr 2.8.
285  *  It will be keep as No-Operation until removal.
286  *  Flash write/erase protection management has been moved to write and erase
287  *  operations implementations in flash driver shims. For Out-of-tree drivers
288  *  which are not updated yet flash write/erase protection management is done
289  *  in flash_erase() and flash_write() using deprecated <p>write_protection</p>
290  *  shim handler.
291  *
292  *  @param  dev             : flash device
293  *  @param  enable          : enable or disable flash write protection
294  *
295  *  @return  0 on success, negative errno code on fail.
296  */
297 __deprecated
298 __syscall int flash_write_protection_set(const struct device *dev,
299 					 bool enable);
300 
z_impl_flash_write_protection_set(const struct device * dev,bool enable)301 static inline int z_impl_flash_write_protection_set(const struct device *dev,
302 						    bool enable)
303 {
304 	ARG_UNUSED(dev);
305 	ARG_UNUSED(enable);
306 
307 	return 0;
308 }
309 
310 struct flash_pages_info {
311 	off_t start_offset; /* offset from the base of flash address */
312 	size_t size;
313 	uint32_t index;
314 };
315 
316 #if defined(CONFIG_FLASH_PAGE_LAYOUT)
317 /**
318  *  @brief  Get the size and start offset of flash page at certain flash offset.
319  *
320  *  @param  dev flash device
321  *  @param  offset Offset within the page
322  *  @param  info Page Info structure to be filled
323  *
324  *  @return  0 on success, -EINVAL if page of the offset doesn't exist.
325  */
326 __syscall int flash_get_page_info_by_offs(const struct device *dev,
327 					  off_t offset,
328 					  struct flash_pages_info *info);
329 
330 /**
331  *  @brief  Get the size and start offset of flash page of certain index.
332  *
333  *  @param  dev flash device
334  *  @param  page_index Index of the page. Index are counted from 0.
335  *  @param  info Page Info structure to be filled
336  *
337  *  @return  0 on success, -EINVAL  if page of the index doesn't exist.
338  */
339 __syscall int flash_get_page_info_by_idx(const struct device *dev,
340 					 uint32_t page_index,
341 					 struct flash_pages_info *info);
342 
343 /**
344  *  @brief  Get the total number of flash pages.
345  *
346  *  @param  dev flash device
347  *
348  *  @return  Number of flash pages.
349  */
350 __syscall size_t flash_get_page_count(const struct device *dev);
351 
352 /**
353  * @brief Callback type for iterating over flash pages present on a device.
354  *
355  * The callback should return true to continue iterating, and false to halt.
356  *
357  * @param info Information for current page
358  * @param data Private data for callback
359  * @return True to continue iteration, false to halt iteration.
360  * @see flash_page_foreach()
361  */
362 typedef bool (*flash_page_cb)(const struct flash_pages_info *info, void *data);
363 
364 /**
365  * @brief Iterate over all flash pages on a device
366  *
367  * This routine iterates over all flash pages on the given device,
368  * ordered by increasing start offset. For each page, it invokes the
369  * given callback, passing it the page's information and a private
370  * data object.
371  *
372  * @param dev Device whose pages to iterate over
373  * @param cb Callback to invoke for each flash page
374  * @param data Private data for callback function
375  */
376 void flash_page_foreach(const struct device *dev, flash_page_cb cb,
377 			void *data);
378 #endif /* CONFIG_FLASH_PAGE_LAYOUT */
379 
380 #if defined(CONFIG_FLASH_JESD216_API)
381 /**
382  * @brief Read data from Serial Flash Discoverable Parameters
383  *
384  * This routine reads data from a serial flash device compatible with
385  * the JEDEC JESD216 standard for encoding flash memory
386  * characteristics.
387  *
388  * Availability of this API is conditional on selecting
389  * @c CONFIG_FLASH_JESD216_API and support of that functionality in
390  * the driver underlying @p dev.
391  *
392  * @param dev device from which parameters will be read
393  * @param offset address within the SFDP region containing data of interest
394  * @param data where the data to be read will be placed
395  * @param len the number of bytes of data to be read
396  *
397  * @retval 0 on success
398  * @retval -ENOTSUP if the flash driver does not support SFDP access
399  * @retval negative values for other errors.
400  */
401 __syscall int flash_sfdp_read(const struct device *dev, off_t offset,
402 			      void *data, size_t len);
403 
z_impl_flash_sfdp_read(const struct device * dev,off_t offset,void * data,size_t len)404 static inline int z_impl_flash_sfdp_read(const struct device *dev,
405 					 off_t offset,
406 					 void *data, size_t len)
407 {
408 	int rv = -ENOTSUP;
409 	const struct flash_driver_api *api =
410 		(const struct flash_driver_api *)dev->api;
411 
412 	if (api->sfdp_read != NULL) {
413 		rv = api->sfdp_read(dev, offset, data, len);
414 	}
415 	return rv;
416 }
417 
418 /**
419  * @brief Read the JEDEC ID from a compatible flash device.
420  *
421  * @param dev device from which id will be read
422  * @param id pointer to a buffer of at least 3 bytes into which id
423  * will be stored
424  *
425  * @retval 0 on successful store of 3-byte JEDEC id
426  * @retval -ENOTSUP if flash driver doesn't support this function
427  * @retval negative values for other errors
428  */
429 __syscall int flash_read_jedec_id(const struct device *dev, uint8_t *id);
430 
z_impl_flash_read_jedec_id(const struct device * dev,uint8_t * id)431 static inline int z_impl_flash_read_jedec_id(const struct device *dev,
432 					     uint8_t *id)
433 {
434 	int rv = -ENOTSUP;
435 	const struct flash_driver_api *api =
436 		(const struct flash_driver_api *)dev->api;
437 
438 	if (api->read_jedec_id != NULL) {
439 		rv = api->read_jedec_id(dev, id);
440 	}
441 	return rv;
442 }
443 #endif /* CONFIG_FLASH_JESD216_API */
444 
445 /**
446  *  @brief  Get the minimum write block size supported by the driver
447  *
448  *  The write block size supported by the driver might differ from the write
449  *  block size of memory used because the driver might implements write-modify
450  *  algorithm.
451  *
452  *  @param  dev flash device
453  *
454  *  @return  write block size in bytes.
455  */
456 __syscall size_t flash_get_write_block_size(const struct device *dev);
457 
z_impl_flash_get_write_block_size(const struct device * dev)458 static inline size_t z_impl_flash_get_write_block_size(const struct device *dev)
459 {
460 	const struct flash_driver_api *api =
461 		(const struct flash_driver_api *)dev->api;
462 
463 	return api->get_parameters(dev)->write_block_size;
464 }
465 
466 
467 /**
468  *  @brief  Get pointer to flash_parameters structure
469  *
470  *  Returned pointer points to a structure that should be considered
471  *  constant through a runtime, regardless if it is defined in RAM or
472  *  Flash.
473  *  Developer is free to cache the structure pointer or copy its contents.
474  *
475  *  @return pointer to flash_parameters structure characteristic for
476  *          the device.
477  */
478 __syscall const struct flash_parameters *flash_get_parameters(const struct device *dev);
479 
z_impl_flash_get_parameters(const struct device * dev)480 static inline const struct flash_parameters *z_impl_flash_get_parameters(const struct device *dev)
481 {
482 	const struct flash_driver_api *api =
483 		(const struct flash_driver_api *)dev->api;
484 
485 	return api->get_parameters(dev);
486 }
487 
488 #ifdef __cplusplus
489 }
490 #endif
491 
492 /**
493  * @}
494  */
495 
496 #include <syscalls/flash.h>
497 
498 #endif /* ZEPHYR_INCLUDE_DRIVERS_FLASH_H_ */
499