Skip to content

Commit 958e2d3

Browse files
ulfalizergalak
authored andcommitted
scripts/dts: Refactor Flash code to be less twisty
- Remove DTFlash.extract(), which was just dispatching to either _extract_flash() or _extract_code_partition() depending on which magic string was passed in. Call them directly instead. - Fold constant and globally available parameter values into functions - Remove DTFlash._flash_node. It's easy to derive wherever it's needed, and it makes it clearer where it comes from (and means functions can be called in any order). - Remove DTFlash._flash_base_address, which is unused - Remove various unused parameters to functions Signed-off-by: Ulf Magnusson <[email protected]>
1 parent 2a4d8b0 commit 958e2d3

File tree

2 files changed

+24
-50
lines changed

2 files changed

+24
-50
lines changed

scripts/dts/extract/flash.py

Lines changed: 22 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
#
1616
class DTFlash(DTDirective):
1717
def __init__(self):
18-
# Node of the flash
19-
self._flash_node = None
2018
self._flash_area = {}
2119

2220
def _extract_partition(self, node_path):
@@ -138,20 +136,20 @@ def extract_partition(self, node_path):
138136

139137
insert_defs(node_path, prop_def, prop_alias)
140138

141-
def _extract_flash(self, node_path, prop, def_label):
142-
if node_path == 'dummy-flash':
143-
# We will add addr/size of 0 for systems with no flash controller
144-
# This is what they already do in the Kconfig options anyway
145-
insert_defs(node_path,
139+
def extract_flash(self):
140+
node_path = chosen.get('zephyr,flash')
141+
if not node_path:
142+
# Add addr/size 0 for systems with no flash controller. This is
143+
# what they already do in the Kconfig options anyway.
144+
insert_defs('dummy-flash',
146145
{'DT_FLASH_BASE_ADDRESS': 0, 'DT_FLASH_SIZE': 0},
147146
{})
148-
self._flash_base_address = 0
149147
return
150148

151-
self._flash_node = reduced[node_path]
149+
flash_node = reduced[node_path]
152150
orig_node_addr = node_path
153151

154-
(nr_address_cells, nr_size_cells) = get_addr_size_cells(node_path)
152+
nr_address_cells, nr_size_cells = get_addr_size_cells(node_path)
155153
# if the nr_size_cells is 0, assume a SPI flash, need to look at parent
156154
# for addr/size info, and the second reg property (assume first is mmio
157155
# register for the controller itself)
@@ -193,28 +191,26 @@ def _extract_flash(self, node_path, prop, def_label):
193191
{})
194192

195193
for prop in 'write-block-size', 'erase-block-size':
196-
if prop in self._flash_node['props']:
197-
default.extract(node_path, prop, None, def_label)
194+
if prop in flash_node['props']:
195+
default.extract(node_path, prop, None, 'DT_FLASH')
198196

199197
# Add an non-DT prefix alias for compatiability
200198
prop_alias = {}
201199
label_post = '_' + str_to_label(prop)
202-
prop_alias['FLASH' + label_post] = def_label + label_post
200+
prop_alias['FLASH' + label_post] = 'DT_FLASH' + label_post
203201
insert_defs(node_path, {}, prop_alias)
204202

205203

206-
def _extract_code_partition(self, node_path, prop, def_label):
207-
if node_path == 'dummy-flash':
208-
node = None
209-
else:
210-
node = reduced[node_path]
211-
if self._flash_node is None:
212-
# No flash node scanned before-
213-
raise Exception(
214-
"Code partition '{}' {} without flash definition."
215-
.format(prop, node_path))
216-
217-
if node and node is not self._flash_node:
204+
def extract_code_partition(self):
205+
node_path = chosen.get('zephyr,code-partition')
206+
if not node_path:
207+
# Fall back on zephyr,flash if zephyr,code-partition isn't set.
208+
# node_path will be 'dummy-flash' if neither is set.
209+
node_path = chosen.get('zephyr,flash', 'dummy-flash')
210+
211+
node = reduced.get(node_path)
212+
213+
if node and node is not reduced.get(chosen.get('zephyr,flash')):
218214
# only compute the load offset if the code partition
219215
# is not the same as the flash base address
220216
load_offset = node['props']['reg'][0]
@@ -228,27 +224,7 @@ def _extract_code_partition(self, node_path, prop, def_label):
228224
'DT_CODE_PARTITION_SIZE': load_size},
229225
{})
230226

231-
##
232-
# @brief Extract flash
233-
#
234-
# @param node_path Path to node owning the
235-
# flash definition.
236-
# @param prop compatible property name
237-
# @param def_label Define label string of node owning the
238-
# compatible definition.
239-
#
240-
def extract(self, node_path, prop, def_label):
241-
242-
if prop == 'zephyr,flash':
243-
# indicator for flash
244-
self._extract_flash(node_path, prop, def_label)
245-
elif prop == 'zephyr,code-partition':
246-
# indicator for code_partition
247-
self._extract_code_partition(node_path, prop, def_label)
248-
else:
249-
raise Exception(
250-
"DTFlash.extract called with unexpected directive ({})."
251-
.format(prop))
227+
252228
##
253229
# @brief Management information for flash.
254230
flash = DTFlash()

scripts/dts/extract_dts_includes.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -458,10 +458,8 @@ def generate_defines():
458458
if k in chosen:
459459
extract_string_prop(chosen[k], "label", v)
460460

461-
node_path = chosen.get('zephyr,flash', 'dummy-flash')
462-
flash.extract(node_path, 'zephyr,flash', 'DT_FLASH')
463-
node_path = chosen.get('zephyr,code-partition', node_path)
464-
flash.extract(node_path, 'zephyr,code-partition', None)
461+
flash.extract_flash()
462+
flash.extract_code_partition()
465463

466464
# Add DT_CHOSEN_<X> defines
467465
for c in sorted(chosen):

0 commit comments

Comments
 (0)