kconfigfunctions.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. # Copyright (c) 2018-2019 Linaro
  2. # Copyright (c) 2019 Nordic Semiconductor ASA
  3. #
  4. # SPDX-License-Identifier: Apache-2.0
  5. import os
  6. import pickle
  7. import sys
  8. ZEPHYR_BASE = os.environ["ZEPHYR_BASE"]
  9. sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts", "dts",
  10. "python-devicetree", "src"))
  11. from devicetree import edtlib
  12. # Types we support
  13. # 'string', 'int', 'hex', 'bool'
  14. doc_mode = os.environ.get('KCONFIG_DOC_MODE') == "1"
  15. if not doc_mode:
  16. EDT_PICKLE = os.environ.get("EDT_PICKLE")
  17. # The "if" handles a missing dts.
  18. if EDT_PICKLE is not None and os.path.isfile(EDT_PICKLE):
  19. with open(EDT_PICKLE, 'rb') as f:
  20. edt = pickle.load(f)
  21. else:
  22. edt = None
  23. def _warn(kconf, msg):
  24. print("{}:{}: WARNING: {}".format(kconf.filename, kconf.linenr, msg))
  25. def _dt_units_to_scale(unit):
  26. if not unit:
  27. return 0
  28. if unit in {'k', 'K'}:
  29. return 10
  30. if unit in {'m', 'M'}:
  31. return 20
  32. if unit in {'g', 'G'}:
  33. return 30
  34. def dt_chosen_label(kconf, _, chosen):
  35. """
  36. This function takes a 'chosen' property and treats that property as a path
  37. to an EDT node. If it finds an EDT node, it will look to see if that node
  38. has a "label" property and return the value of that "label", if not we
  39. return an empty string.
  40. """
  41. if doc_mode or edt is None:
  42. return ""
  43. node = edt.chosen_node(chosen)
  44. if not node:
  45. return ""
  46. if "label" not in node.props:
  47. return ""
  48. return node.props["label"].val
  49. def dt_chosen_enabled(kconf, _, chosen):
  50. """
  51. This function returns "y" if /chosen contains a property named 'chosen'
  52. that points to an enabled node, and "n" otherwise
  53. """
  54. if doc_mode or edt is None:
  55. return "n"
  56. node = edt.chosen_node(chosen)
  57. return "y" if node and node.status == "okay" else "n"
  58. def dt_chosen_path(kconf, _, chosen):
  59. """
  60. This function takes a /chosen node property and returns the path
  61. to the node in the property value, or the empty string.
  62. """
  63. if doc_mode or edt is None:
  64. return "n"
  65. node = edt.chosen_node(chosen)
  66. return node.path if node else ""
  67. def dt_node_enabled(kconf, name, node):
  68. """
  69. This function is used to test if a node is enabled (has status
  70. 'okay') or not.
  71. The 'node' argument is a string which is either a path or an
  72. alias, or both, depending on 'name'.
  73. If 'name' is 'dt_path_enabled', 'node' is an alias or a path. If
  74. 'name' is 'dt_alias_enabled, 'node' is an alias.
  75. """
  76. if doc_mode or edt is None:
  77. return "n"
  78. if name == "dt_alias_enabled":
  79. if node.startswith("/"):
  80. # EDT.get_node() works with either aliases or paths. If we
  81. # are specifically being asked about an alias, reject paths.
  82. return "n"
  83. else:
  84. # Make sure this is being called appropriately.
  85. assert name == "dt_path_enabled"
  86. try:
  87. node = edt.get_node(node)
  88. except edtlib.EDTError:
  89. return "n"
  90. return "y" if node and node.status == "okay" else "n"
  91. def dt_nodelabel_enabled(kconf, _, label):
  92. """
  93. This function is like dt_node_enabled(), but the 'label' argument
  94. should be a node label, like "foo" is here:
  95. foo: some-node { ... };
  96. """
  97. if doc_mode or edt is None:
  98. return "n"
  99. node = edt.label2node.get(label)
  100. return "y" if node and node.status == "okay" else "n"
  101. def _node_reg_addr(node, index, unit):
  102. if not node:
  103. return 0
  104. if not node.regs:
  105. return 0
  106. if int(index) >= len(node.regs):
  107. return 0
  108. if node.regs[int(index)].addr is None:
  109. return 0
  110. return node.regs[int(index)].addr >> _dt_units_to_scale(unit)
  111. def _node_reg_size(node, index, unit):
  112. if not node:
  113. return 0
  114. if not node.regs:
  115. return 0
  116. if int(index) >= len(node.regs):
  117. return 0
  118. if node.regs[int(index)].size is None:
  119. return 0
  120. return node.regs[int(index)].size >> _dt_units_to_scale(unit)
  121. def _node_int_prop(node, prop):
  122. if not node:
  123. return 0
  124. if prop not in node.props:
  125. return 0
  126. if node.props[prop].type != "int":
  127. return 0
  128. return node.props[prop].val
  129. def _dt_chosen_reg_addr(kconf, chosen, index=0, unit=None):
  130. """
  131. This function takes a 'chosen' property and treats that property as a path
  132. to an EDT node. If it finds an EDT node, it will look to see if that
  133. nodnode has a register at the given 'index' and return the address value of
  134. that reg, if not we return 0.
  135. The function will divide the value based on 'unit':
  136. None No division
  137. 'k' or 'K' divide by 1024 (1 << 10)
  138. 'm' or 'M' divide by 1,048,576 (1 << 20)
  139. 'g' or 'G' divide by 1,073,741,824 (1 << 30)
  140. """
  141. if doc_mode or edt is None:
  142. return 0
  143. node = edt.chosen_node(chosen)
  144. return _node_reg_addr(node, index, unit)
  145. def _dt_chosen_reg_size(kconf, chosen, index=0, unit=None):
  146. """
  147. This function takes a 'chosen' property and treats that property as a path
  148. to an EDT node. If it finds an EDT node, it will look to see if that node
  149. has a register at the given 'index' and return the size value of that reg,
  150. if not we return 0.
  151. The function will divide the value based on 'unit':
  152. None No division
  153. 'k' or 'K' divide by 1024 (1 << 10)
  154. 'm' or 'M' divide by 1,048,576 (1 << 20)
  155. 'g' or 'G' divide by 1,073,741,824 (1 << 30)
  156. """
  157. if doc_mode or edt is None:
  158. return 0
  159. node = edt.chosen_node(chosen)
  160. return _node_reg_size(node, index, unit)
  161. def dt_chosen_reg(kconf, name, chosen, index=0, unit=None):
  162. """
  163. This function just routes to the proper function and converts
  164. the result to either a string int or string hex value.
  165. """
  166. if name == "dt_chosen_reg_size_int":
  167. return str(_dt_chosen_reg_size(kconf, chosen, index, unit))
  168. if name == "dt_chosen_reg_size_hex":
  169. return hex(_dt_chosen_reg_size(kconf, chosen, index, unit))
  170. if name == "dt_chosen_reg_addr_int":
  171. return str(_dt_chosen_reg_addr(kconf, chosen, index, unit))
  172. if name == "dt_chosen_reg_addr_hex":
  173. return hex(_dt_chosen_reg_addr(kconf, chosen, index, unit))
  174. def _dt_node_reg_addr(kconf, path, index=0, unit=None):
  175. """
  176. This function takes a 'path' and looks for an EDT node at that path. If it
  177. finds an EDT node, it will look to see if that node has a register at the
  178. given 'index' and return the address value of that reg, if not we return 0.
  179. The function will divide the value based on 'unit':
  180. None No division
  181. 'k' or 'K' divide by 1024 (1 << 10)
  182. 'm' or 'M' divide by 1,048,576 (1 << 20)
  183. 'g' or 'G' divide by 1,073,741,824 (1 << 30)
  184. """
  185. if doc_mode or edt is None:
  186. return 0
  187. try:
  188. node = edt.get_node(path)
  189. except edtlib.EDTError:
  190. return 0
  191. return _node_reg_addr(node, index, unit)
  192. def _dt_node_reg_size(kconf, path, index=0, unit=None):
  193. """
  194. This function takes a 'path' and looks for an EDT node at that path. If it
  195. finds an EDT node, it will look to see if that node has a register at the
  196. given 'index' and return the size value of that reg, if not we return 0.
  197. The function will divide the value based on 'unit':
  198. None No division
  199. 'k' or 'K' divide by 1024 (1 << 10)
  200. 'm' or 'M' divide by 1,048,576 (1 << 20)
  201. 'g' or 'G' divide by 1,073,741,824 (1 << 30)
  202. """
  203. if doc_mode or edt is None:
  204. return 0
  205. try:
  206. node = edt.get_node(path)
  207. except edtlib.EDTError:
  208. return 0
  209. return _node_reg_size(node, index, unit)
  210. def dt_node_reg(kconf, name, path, index=0, unit=None):
  211. """
  212. This function just routes to the proper function and converts
  213. the result to either a string int or string hex value.
  214. """
  215. if name == "dt_node_reg_size_int":
  216. return str(_dt_node_reg_size(kconf, path, index, unit))
  217. if name == "dt_node_reg_size_hex":
  218. return hex(_dt_node_reg_size(kconf, path, index, unit))
  219. if name == "dt_node_reg_addr_int":
  220. return str(_dt_node_reg_addr(kconf, path, index, unit))
  221. if name == "dt_node_reg_addr_hex":
  222. return hex(_dt_node_reg_addr(kconf, path, index, unit))
  223. def dt_node_has_bool_prop(kconf, _, path, prop):
  224. """
  225. This function takes a 'path' and looks for an EDT node at that path. If it
  226. finds an EDT node, it will look to see if that node has a boolean property
  227. by the name of 'prop'. If the 'prop' exists it will return "y" otherwise
  228. we return "n".
  229. """
  230. if doc_mode or edt is None:
  231. return "n"
  232. try:
  233. node = edt.get_node(path)
  234. except edtlib.EDTError:
  235. return "n"
  236. if prop not in node.props:
  237. return "n"
  238. if node.props[prop].type != "boolean":
  239. return "n"
  240. if node.props[prop].val:
  241. return "y"
  242. return "n"
  243. def dt_node_has_prop(kconf, _, label, prop):
  244. """
  245. This function takes a 'label' and looks for an EDT node for that label. If
  246. it finds an EDT node, it will look to see if that node has a property
  247. by the name of 'prop'. If the 'prop' exists it will return "y" otherwise
  248. we return "n".
  249. """
  250. if doc_mode or edt is None:
  251. return "n"
  252. try:
  253. node = edt.label2node.get(label)
  254. except edtlib.EDTError:
  255. return "n"
  256. if node is None:
  257. return "n"
  258. if prop in node.props:
  259. return "y"
  260. return "n"
  261. def dt_node_int_prop(kconf, name, path, prop):
  262. """
  263. This function takes a 'path' and property name ('prop') looks for an EDT
  264. node at that path. If it finds an EDT node, it will look to see if that
  265. node has a property called 'prop' and if that 'prop' is an integer type
  266. will return the value of the property 'prop' as either a string int or
  267. string hex value, if not we return 0.
  268. """
  269. if doc_mode or edt is None:
  270. return "0"
  271. try:
  272. node = edt.get_node(path)
  273. except edtlib.EDTError:
  274. return "0"
  275. if name == "dt_node_int_prop_int":
  276. return str(_node_int_prop(node, prop))
  277. if name == "dt_node_int_prop_hex":
  278. return hex(_node_int_prop(node, prop))
  279. def dt_compat_enabled(kconf, _, compat):
  280. """
  281. This function takes a 'compat' and returns "y" if we find a status "okay"
  282. compatible node in the EDT otherwise we return "n"
  283. """
  284. if doc_mode or edt is None:
  285. return "n"
  286. return "y" if compat in edt.compat2okay else "n"
  287. def dt_compat_on_bus(kconf, _, compat, bus):
  288. """
  289. This function takes a 'compat' and returns "y" if we find an "enabled"
  290. compatible node in the EDT which is on bus 'bus'. It returns "n" otherwise.
  291. """
  292. if doc_mode or edt is None:
  293. return "n"
  294. if compat in edt.compat2okay:
  295. for node in edt.compat2okay[compat]:
  296. if node.on_bus is not None and node.on_bus == bus:
  297. return "y"
  298. return "n"
  299. def dt_nodelabel_has_compat(kconf, _, label, compat):
  300. """
  301. This function takes a 'label' and returns "y" if an "enabled" node with
  302. such label can be found in the EDT and that node is compatible with the
  303. provided 'compat', otherwise it returns "n".
  304. """
  305. if doc_mode or edt is None:
  306. return "n"
  307. if compat in edt.compat2okay:
  308. for node in edt.compat2okay[compat]:
  309. if label in node.labels:
  310. return "y"
  311. return "n"
  312. def dt_nodelabel_path(kconf, _, label):
  313. """
  314. This function takes a node label (not a label property) and
  315. returns the path to the node which has that label, or an empty
  316. string if there is no such node.
  317. """
  318. if doc_mode or edt is None:
  319. return ""
  320. node = edt.label2node.get(label)
  321. return node.path if node else ""
  322. def shields_list_contains(kconf, _, shield):
  323. """
  324. Return "n" if cmake environment variable 'SHIELD_AS_LIST' doesn't exist.
  325. Return "y" if 'shield' is present list obtained after 'SHIELD_AS_LIST'
  326. has been split using ";" as a separator and "n" otherwise.
  327. """
  328. try:
  329. list = os.environ['SHIELD_AS_LIST']
  330. except KeyError:
  331. return "n"
  332. return "y" if shield in list.split(";") else "n"
  333. # Keys in this dict are the function names as they appear
  334. # in Kconfig files. The values are tuples in this form:
  335. #
  336. # (python_function, minimum_number_of_args, maximum_number_of_args)
  337. #
  338. # Each python function is given a kconf object and its name in the
  339. # Kconfig file, followed by arguments from the Kconfig file.
  340. #
  341. # See the kconfiglib documentation for more details.
  342. functions = {
  343. "dt_compat_enabled": (dt_compat_enabled, 1, 1),
  344. "dt_compat_on_bus": (dt_compat_on_bus, 2, 2),
  345. "dt_chosen_label": (dt_chosen_label, 1, 1),
  346. "dt_chosen_enabled": (dt_chosen_enabled, 1, 1),
  347. "dt_chosen_path": (dt_chosen_path, 1, 1),
  348. "dt_path_enabled": (dt_node_enabled, 1, 1),
  349. "dt_alias_enabled": (dt_node_enabled, 1, 1),
  350. "dt_nodelabel_enabled": (dt_nodelabel_enabled, 1, 1),
  351. "dt_chosen_reg_addr_int": (dt_chosen_reg, 1, 3),
  352. "dt_chosen_reg_addr_hex": (dt_chosen_reg, 1, 3),
  353. "dt_chosen_reg_size_int": (dt_chosen_reg, 1, 3),
  354. "dt_chosen_reg_size_hex": (dt_chosen_reg, 1, 3),
  355. "dt_node_reg_addr_int": (dt_node_reg, 1, 3),
  356. "dt_node_reg_addr_hex": (dt_node_reg, 1, 3),
  357. "dt_node_reg_size_int": (dt_node_reg, 1, 3),
  358. "dt_node_reg_size_hex": (dt_node_reg, 1, 3),
  359. "dt_node_has_bool_prop": (dt_node_has_bool_prop, 2, 2),
  360. "dt_node_has_prop": (dt_node_has_prop, 2, 2),
  361. "dt_node_int_prop_int": (dt_node_int_prop, 2, 2),
  362. "dt_node_int_prop_hex": (dt_node_int_prop, 2, 2),
  363. "dt_nodelabel_has_compat": (dt_nodelabel_has_compat, 2, 2),
  364. "dt_nodelabel_path": (dt_nodelabel_path, 1, 1),
  365. "shields_list_contains": (shields_list_contains, 1, 1),
  366. }