zephyr/scripts/dts/extract/reg.py
Ulf Magnusson 5999f10ba2 scripts/dts: Get rid of deepcopy() and 'except:' in translate_addr()
- No deep copying is needed. The deepcopy() is just a hack to work
   around that the value might not be a list. That deserves a comment at
   least.

 - A catch-all try/except will hide stuff like misspelled variables as
   well. It's usually a bad idea.

Rewrite things to be more explicit, and add some comments.

reg.py was indirectly using the deepcopy() imported in global.py,
because it does 'from global import *'. Have it import deepcopy() itself
instead. reg.py is the only remaining user of deepcopy().

reg.py relying on the deepcopy() import in global.py was super hard to
discover, due to another catch-all try/except around the deepcopy()
call.

Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
2019-02-27 10:02:35 -06:00

127 lines
4.1 KiB
Python

#
# Copyright (c) 2018 Bobby Noelte
#
# SPDX-License-Identifier: Apache-2.0
#
from copy import deepcopy
from extract.globals import *
from extract.directive import DTDirective
##
# @brief Manage reg directive.
#
class DTReg(DTDirective):
def __init__(self):
pass
##
# @brief Extract reg directive info
#
# @param node_path Path to node owning the
# reg definition.
# @param names (unused)
# @param def_label Define label string of node owning the
# compatible definition.
#
def extract(self, node_path, names, def_label, div):
node = reduced[node_path]
node_compat = get_compat(node_path)
binding = get_binding(node_path)
reg = reduced[node_path]['props']['reg']
if type(reg) is not list: reg = [ reg, ]
(nr_address_cells, nr_size_cells) = get_addr_size_cells(node_path)
if 'parent' in binding:
bus = binding['parent']['bus']
if bus == 'spi':
cs_gpios = None
try:
cs_gpios = deepcopy(find_parent_prop(node_path, 'cs-gpios'))
except:
pass
if cs_gpios:
extract_controller(node_path, "cs-gpios", cs_gpios, reg[0], def_label, "cs-gpio", True)
extract_cells(node_path, "cs-gpios", cs_gpios, None, reg[0], def_label, "cs-gpio", True)
# generate defines
l_base = def_label.split('/')
l_addr = [str_to_label("BASE_ADDRESS")]
l_size = ["SIZE"]
index = 0
props = list(reg)
while props:
prop_def = {}
prop_alias = {}
addr = 0
size = 0
# Check is defined should be indexed (_0, _1)
if index == 0 and len(props) < 3:
# 1 element (len 2) or no element (len 0) in props
l_idx = []
else:
l_idx = [str(index)]
try:
name = [names.pop(0).upper()]
except:
name = []
for x in range(nr_address_cells):
addr += props.pop(0) << (32 * (nr_address_cells - x - 1))
for x in range(nr_size_cells):
size += props.pop(0) << (32 * (nr_size_cells - x - 1))
addr += translate_addr(addr, node_path,
nr_address_cells, nr_size_cells)
l_addr_fqn = '_'.join(l_base + l_addr + l_idx)
l_size_fqn = '_'.join(l_base + l_size + l_idx)
if nr_address_cells:
prop_def[l_addr_fqn] = hex(addr)
add_compat_alias(node_path, '_'.join(l_addr + l_idx), l_addr_fqn, prop_alias)
if nr_size_cells:
prop_def[l_size_fqn] = int(size / div)
add_compat_alias(node_path, '_'.join(l_size + l_idx), l_size_fqn, prop_alias)
if len(name):
if nr_address_cells:
prop_alias['_'.join(l_base + name + l_addr)] = l_addr_fqn
add_compat_alias(node_path, '_'.join(name + l_addr), l_addr_fqn, prop_alias)
if nr_size_cells:
prop_alias['_'.join(l_base + name + l_size)] = l_size_fqn
add_compat_alias(node_path, '_'.join(name + l_size), l_size_fqn, prop_alias)
# generate defs for node aliases
if node_path in aliases:
add_prop_aliases(
node_path,
lambda alias:
'_'.join([str_to_label(alias)] + l_addr + l_idx),
l_addr_fqn,
prop_alias)
if nr_size_cells:
add_prop_aliases(
node_path,
lambda alias:
'_'.join([str_to_label(alias)] + l_size + l_idx),
l_size_fqn,
prop_alias)
insert_defs(node_path, prop_def, prop_alias)
# increment index for definition creation
index += 1
##
# @brief Management information for registers.
reg = DTReg()