Skip to content

Commit db093b2

Browse files
de-nordicrghaddab
authored andcommitted
[nrf fromtree] drivers/flash: Add explicit_erase property
The commit adds and no_explicit_erase capability to flash_parameters, that indicates whether device is program-erase type device; such devices, like Flash type devices, require erase prior to writing random data at any previously programmed location. This capability should only be set by drivers. The flash_params_get_erase_cap() function has been added, for parsing flash_parameters object to obtain erase capabilities of device. The function returns capabilities as combination of bits representing them. Currently it will return: 0 -- no erase capabilities FLASH_ERASE_C_EXPLICIT -- erase required before write of random data. Additional capabilities have been reserved but are not yet used. There are following Kconfig options added: FLASH_HAS_EXPLICIT_ERASE FLASH_HAS_NO_EXPLICIT_ERASE that should be selected by device driver to indicate whether devices served by driver needs erase prior to write. The above Kconfigs are used to figure out whether app is built for hardware that requires erase prior to write. They can be also used to detect that it is attempted to build some subsystem that will not work with provided hardware, for example file system that has not been prepared to work with devices that do not require erase. Signed-off-by: Dominik Ermel <[email protected]> (cherry picked from commit b3c9f95)
1 parent 4322f51 commit db093b2

File tree

2 files changed

+107
-3
lines changed

2 files changed

+107
-3
lines changed

drivers/flash/Kconfig

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,56 @@ config FLASH_HAS_EX_OP
1717
This option is selected by drivers that support flash extended
1818
operations.
1919

20+
config FLASH_HAS_EXPLICIT_ERASE
21+
bool
22+
help
23+
Device has does not do erase-on-write (erase-on-program, auto-erase
24+
on write) and requires explicit erase procedure to be programmed
25+
with random value, in place where it has already been programmed with
26+
some other value, as program can only change bits from erased-value
27+
to the opposite.
28+
All pure Flash devices are evolution of EEPROM where erase has
29+
been separated from write, EEPROM has erase-on-write, giving
30+
it advantage of higher write speeds at a cost of larger erase block.
31+
Note that explicit-erase capability does not warrants that
32+
write without erase is not allowed, taking the above restrictions,
33+
it only states that write of a random information will require
34+
erase.
35+
Erase is usually performed in pages, as we have chosen to name
36+
the unit in Zephyr, that may have different naming in device
37+
specifications, like pages, sectors or blocks, and may vary
38+
in size, depending how they are named by vendor.
39+
This option should be selected by drivers that serve devices with
40+
such characteristic and is used and may be used by users to provide
41+
paths in code that only serve such devices, and could be
42+
optimized-out by compiler in case where there is no such device in
43+
a system.
44+
45+
config FLASH_HAS_NO_EXPLICIT_ERASE
46+
bool
47+
help
48+
Device does not require explicit erase before programming
49+
a new random value at any location that has been previously
50+
programmed with some other value.
51+
Note that the device may have erase-on-write (auto-erase),
52+
as for example in EEPROM devices, but may also have no erase
53+
at all.
54+
A device driver may still provide erase callback,
55+
especially if it is able to perform erase to accelerate
56+
further writes or is able to fill the area requested for
57+
erase, with single value, faster than consecutive writes
58+
that would be used to emulate erase.
59+
This option should be selected by drivers that serve
60+
devices with such characteristic and is used and may be
61+
used by users to provide paths in code that only serve
62+
such devices, and could be optimized-out by compiler in
63+
case where there is no such device in a system.
64+
This option should be selected for any device that
65+
can change storage bits, by write, from any value to opposite
66+
value at any time.
67+
When your driver sets this option you also need to set
68+
no_explicit_erase capability in your drivers flash_parameters.
69+
2070
config FLASH_HAS_PAGE_LAYOUT
2171
bool
2272
help
@@ -77,13 +127,17 @@ config FLASH_SHELL_BUFFER_SIZE
77127

78128
endif # FLASH_SHELL
79129

130+
131+
if FLASH_HAS_PAGE_LAYOUT
132+
80133
config FLASH_PAGE_LAYOUT
81134
bool "API for retrieving the layout of pages"
82-
depends on FLASH_HAS_PAGE_LAYOUT
83-
default y
135+
default FLASH_HAS_PAGE_LAYOUT
84136
help
85137
Enables API for retrieving the layout of flash memory pages.
86138

139+
endif
140+
87141
config FLASH_EX_OP_ENABLED
88142
bool "API for extended flash operations"
89143
depends on FLASH_HAS_EX_OP

include/zephyr/drivers/flash.h

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017 Nordic Semiconductor ASA
2+
* Copyright (c) 2017-2024 Nordic Semiconductor ASA
33
* Copyright (c) 2016 Intel Corporation
44
*
55
* SPDX-License-Identifier: Apache-2.0
@@ -58,9 +58,59 @@ struct flash_pages_layout {
5858
*/
5959
struct flash_parameters {
6060
const size_t write_block_size;
61+
/* Device capabilities. Only drivers are supposed to set the
62+
* capabilities directly, users need to call the FLASH_CAPS_
63+
* macros on pointer to flash_parameters to get capabilities.
64+
*/
65+
struct {
66+
/* Device has no explicit erase, so it either erases on
67+
* write or does not require it at all.
68+
* This also includes devices that support erase but
69+
* do not require it.
70+
*/
71+
bool no_explicit_erase: 1;
72+
} caps;
6173
uint8_t erase_value; /* Byte value of erased flash */
6274
};
6375

76+
/** Set for ordinary Flash where erase is needed before write of random data */
77+
#define FLASH_ERASE_C_EXPLICIT 0x01
78+
/** Reserved for users as initializer for variables that will later store
79+
* capabilities.
80+
*/
81+
#define FLASH_ERASE_CAPS_UNSET (int)-1
82+
/* The values below are now reserved but not used */
83+
#define FLASH_ERASE_C_SUPPORTED 0x02
84+
#define FLASH_ERASE_C_VAL_BIT 0x04
85+
#define FLASH_ERASE_UNIFORM_PAGE 0x08
86+
87+
88+
/* @brief Parser for flash_parameters for retrieving erase capabilities
89+
*
90+
* The functions parses flash_parameters type object and returns combination
91+
* of erase capabilities of 0 if device does not have any.
92+
* Not that in some cases availability of erase may be dependent on driver
93+
* options, so even if by hardware design a device provides some erase
94+
* capabilities, the function may return 0 if these been disabled or not
95+
* implemented by driver.
96+
*
97+
* @param p pointer to flash_parameters type object
98+
*
99+
* @return 0 or combination of FLASH_ERASE_C_ capabilities.
100+
*/
101+
static inline
102+
int flash_params_get_erase_cap(const struct flash_parameters *p)
103+
{
104+
#if defined(CONFIG_FLASH_HAS_EXPLICIT_ERASE)
105+
#if defined(CONFIG_FLASH_HAS_NO_EXPLICIT_ERASE)
106+
return (p->caps.no_explicit_erase) ? 0 : FLASH_ERASE_C_EXPLICIT;
107+
#else
108+
return FLASH_ERASE_C_EXPLICIT;
109+
#endif
110+
#endif
111+
return 0;
112+
}
113+
64114
/**
65115
* @}
66116
*/

0 commit comments

Comments
 (0)