|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 TOKITA Hiroshi <[email protected]> |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/kernel.h> |
| 8 | +#include <zephyr/devicetree.h> |
| 9 | +#include <zephyr/sys/util_macro.h> |
| 10 | + |
| 11 | +#include <zephyr/dt-bindings/pinctrl/rpi-pico-pinctrl-common.h> |
| 12 | + |
| 13 | +#include <soc.h> |
| 14 | + |
| 15 | +#include <boot_stage2/config.h> |
| 16 | +#include <pico/binary_info.h> |
| 17 | + |
| 18 | +#include <version.h> |
| 19 | +#ifdef HAS_APP_VERSION |
| 20 | +#include <app_version.h> |
| 21 | +#endif |
| 22 | + |
| 23 | +/* utils for pin encoding calcluation */ |
| 24 | + |
| 25 | +#ifdef CONFIG_SOC_RP2040 |
| 26 | +#define MAX_PIN_ENTRIES 4 |
| 27 | +#define ENCODE_BASE_IDX 1 |
| 28 | +#define ENCODE_ENTRY_WIDTH 5 |
| 29 | +#define ENCODE_PADDING 2 |
| 30 | +#define ENCODE_PINS_WITH_FUNC __bi_encoded_pins_with_func |
| 31 | +#else |
| 32 | +#define MAX_PIN_ENTRIES 6 |
| 33 | +#define ENCODE_BASE_IDX 1 |
| 34 | +#define ENCODE_ENTRY_WIDTH 8 |
| 35 | +#define ENCODE_PADDING 0 |
| 36 | +#define ENCODE_PINS_WITH_FUNC __bi_encoded_pins_64_with_func |
| 37 | +#endif |
| 38 | + |
| 39 | +#define PIN_FUNC(n, idx) (DT_PROP_BY_IDX(n, pinmux, idx) & RP2_ALT_FUNC_MASK) |
| 40 | +#define PIN_NUM(n, idx) ((DT_PROP_BY_IDX(n, pinmux, idx) >> RP2_PIN_NUM_POS) & RP2_PIN_NUM_MASK) |
| 41 | + |
| 42 | +#define ENCODE_OFFSET(idx) (((idx + ENCODE_BASE_IDX) * ENCODE_ENTRY_WIDTH) + ENCODE_PADDING) |
| 43 | +#define ENCODE_PIN(n, idx, off) ((uint64_t)PIN_NUM(n, idx) << ENCODE_OFFSET(idx + off)) |
| 44 | + |
| 45 | +/* Pin amounts and offets for groups */ |
| 46 | + |
| 47 | +#define PIN_GROUP_AMOUNT(node_id) \ |
| 48 | + COND_CODE_1(DT_NODE_HAS_PROP(node_id, pinmux), (DT_PROP_LEN(node_id, pinmux)), (0)) |
| 49 | +#define PIN_GROUP_AMOUNT_IF_MATCH_IDX(child, idx) \ |
| 50 | + COND_CODE_1(IS_EQ(DT_NODE_CHILD_IDX(child), idx), (PIN_GROUP_AMOUNT(child)), (0)) |
| 51 | +/* |
| 52 | + * The raspberrypi,pico-pinctrl child binding does not permit a "status" |
| 53 | + * property, so the STATUS_OK iterator still visits every group. |
| 54 | + * Use this to avoid nested calls to the same macro. |
| 55 | + */ |
| 56 | +#define PIN_GROUP_AMOUNT_BY_IDX(node_id, idx) \ |
| 57 | + (DT_FOREACH_CHILD_STATUS_OKAY_SEP_VARGS(node_id, PIN_GROUP_AMOUNT_IF_MATCH_IDX, (+), idx)) |
| 58 | + |
| 59 | +#define PIN_GROUP_OFFSET_TERM(i, node_id) +PIN_GROUP_AMOUNT_BY_IDX(node_id, i) |
| 60 | +#define PIN_GROUP_OFFSET(node_id, count) (0 LISTIFY(count, PIN_GROUP_OFFSET_TERM, (), node_id)) |
| 61 | +#define PIN_AMOUNT(node_id) PIN_GROUP_OFFSET(node_id, DT_CHILD_NUM(node_id)) |
| 62 | + |
| 63 | +/* Iterate groups */ |
| 64 | + |
| 65 | +#define FOREACH_PIN_GROUP_ENTRY(n, fn, sep, ...) \ |
| 66 | + DT_FOREACH_PROP_ELEM_SEP_VARGS(n, pinmux, fn, sep, __VA_ARGS__) |
| 67 | +#define FOREACH_PIN_GROUP(n, sep, fn, ...) \ |
| 68 | + DT_FOREACH_CHILD_SEP_VARGS(n, FOREACH_PIN_GROUP_ENTRY, sep, fn, sep, __VA_ARGS__) |
| 69 | + |
| 70 | +/* Encode the pins in a group */ |
| 71 | + |
| 72 | +#define TERMINATE_FLAG(end, idx, off) (((idx) + (off) + ENCODE_BASE_IDX) == (end)) |
| 73 | +#define PIN_ENTRY(n, p, i, off, end) (ENCODE_PIN(n, i, off) + TERMINATE_FLAG(end, i, off)) |
| 74 | +#define ENCODE_EACH_PIN(n, p, i, end) \ |
| 75 | + COND_CODE_1(DT_PROP_HAS_IDX(n, p, i), \ |
| 76 | + (PIN_ENTRY(n, p, i, PIN_GROUP_OFFSET(DT_PARENT(n), DT_NODE_CHILD_IDX(n)), end)), (0)) |
| 77 | +#define ENCODE_GROUP_PINS(n) (FOREACH_PIN_GROUP(n, (|), ENCODE_EACH_PIN, PIN_GROUP_AMOUNT(n))) |
| 78 | + |
| 79 | +/* Get group-wide pin functions */ |
| 80 | + |
| 81 | +#define PIN_FUNC_OFFSET 3 |
| 82 | +#define PIN_FUNC_(n, p, i, _) PIN_FUNC(n, i) |
| 83 | +#define PIN_GROUP_FUNC(n) (FOREACH_PIN_GROUP(n, (|), PIN_FUNC_)) |
| 84 | +#define PIN_GROUP_HEADER(n) (BI_PINS_ENCODING_MULTI | (PIN_GROUP_FUNC(n) << PIN_FUNC_OFFSET)) |
| 85 | + |
| 86 | +/* Check if pin functions are all equal within a group */ |
| 87 | + |
| 88 | +#define PIN_FUNC_IS(n, p, i, func) (PIN_FUNC(n, i) == func) |
| 89 | +#define ALL_PINS_FUNC_IS(n, pinfunc) (FOREACH_PIN_GROUP(n, (&&), PIN_FUNC_IS, pinfunc)) |
| 90 | + |
| 91 | +#define DECLARE_PIN_GROUP(n) \ |
| 92 | + BUILD_ASSERT(PIN_AMOUNT(n) > 0, "Group must contain at least one pin"); \ |
| 93 | + BUILD_ASSERT(PIN_AMOUNT(n) <= MAX_PIN_ENTRIES, "Too many pins in group"); \ |
| 94 | + BUILD_ASSERT(ALL_PINS_FUNC_IS(n, PIN_GROUP_FUNC(n)), \ |
| 95 | + "Group pins must share identical function"); \ |
| 96 | + bi_decl(ENCODE_PINS_WITH_FUNC(PIN_GROUP_HEADER(n) | ENCODE_GROUP_PINS(n))) |
| 97 | + |
| 98 | +#define DECLARE_PIN_GROUP_IF_MATCH_IDX(child, idx) \ |
| 99 | + COND_CODE_1(IS_EQ(DT_NODE_CHILD_IDX(child), idx), (DECLARE_PIN_GROUP(child)), ()) |
| 100 | + |
| 101 | +#define BINARY_INFO_FROM_PINCFG(node_id, idx) \ |
| 102 | + DT_FOREACH_CHILD_VARGS(node_id, DECLARE_PIN_GROUP_IF_MATCH_IDX, idx) |
| 103 | + |
| 104 | +#ifdef CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_NAME |
| 105 | +#define BI_PROGRAM_NAME CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_NAME |
| 106 | +#endif |
| 107 | + |
| 108 | +#ifdef CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_DESCRIPTION |
| 109 | +#define BI_PROGRAM_DESCRIPTION CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_DESCRIPTION |
| 110 | +#endif |
| 111 | + |
| 112 | +#ifdef CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_URL |
| 113 | +#define BI_PROGRAM_URL CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_URL |
| 114 | +#endif |
| 115 | + |
| 116 | +#ifdef CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_BUILD_DATE |
| 117 | +#define BI_PROGRAM_BUILD_DATE CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_BUILD_DATE |
| 118 | +#else |
| 119 | +#define BI_PROGRAM_BUILD_DATE __DATE__ |
| 120 | +#endif |
| 121 | + |
| 122 | +extern uint32_t __rom_region_end; |
| 123 | +bi_decl(bi_binary_end((intptr_t)&__rom_region_end)); |
| 124 | + |
| 125 | +#ifdef CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_NAME_ENABLE |
| 126 | +bi_decl(bi_program_name((uint32_t)BI_PROGRAM_NAME)); |
| 127 | +#endif |
| 128 | + |
| 129 | +#ifdef CONFIG_RPI_PICO_BINARY_INFO_PICO_BOARD_ENABLE |
| 130 | +bi_decl(bi_string(BINARY_INFO_TAG_RASPBERRY_PI, BINARY_INFO_ID_RP_PICO_BOARD, |
| 131 | + (uint32_t)CONFIG_BOARD)); |
| 132 | +#endif |
| 133 | + |
| 134 | +#ifdef CONFIG_RPI_PICO_BINARY_INFO_SDK_VERSION_STRING_ENABLE |
| 135 | +bi_decl(bi_string(BINARY_INFO_TAG_RASPBERRY_PI, BINARY_INFO_ID_RP_SDK_VERSION, |
| 136 | + (uint32_t)"zephyr-" STRINGIFY(BUILD_VERSION))); |
| 137 | +#endif |
| 138 | + |
| 139 | +#if defined(CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_VERSION_STRING_ENABLE) && defined(HAS_APP_VERSION) |
| 140 | +bi_decl(bi_program_version_string((uint32_t)APP_VERSION_EXTENDED_STRING)); |
| 141 | +#endif |
| 142 | + |
| 143 | +#ifdef CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_DESCRIPTION_ENABLE |
| 144 | +bi_decl(bi_program_description((uint32_t)BI_PROGRAM_DESCRIPTION)); |
| 145 | +#endif |
| 146 | + |
| 147 | +#ifdef CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_URL_ENABLE |
| 148 | +bi_decl(bi_program_url((uint32_t)BI_PROGRAM_URL)); |
| 149 | +#endif |
| 150 | + |
| 151 | +#ifdef CONFIG_RPI_PICO_BINARY_INFO_PROGRAM_BUILD_DATE_ENABLE |
| 152 | +bi_decl(bi_program_build_date_string((uint32_t)BI_PROGRAM_BUILD_DATE)); |
| 153 | +#endif |
| 154 | + |
| 155 | +#ifdef CONFIG_RPI_PICO_BINARY_INFO_BOOT_STAGE2_NAME_ENABLE |
| 156 | +bi_decl(bi_string(BINARY_INFO_TAG_RASPBERRY_PI, BINARY_INFO_ID_RP_BOOT2_NAME, |
| 157 | + (uint32_t)PICO_BOOT_STAGE2_NAME)); |
| 158 | +#endif |
| 159 | + |
| 160 | +#ifdef CONFIG_RPI_PICO_BINARY_INFO_ATTRIBUTE_BUILD_TYPE_ENABLE |
| 161 | +#ifdef CONFIG_DEBUG |
| 162 | +bi_decl(bi_program_build_attribute((uint32_t)"Debug")); |
| 163 | +#else |
| 164 | +bi_decl(bi_program_build_attribute((uint32_t)"Release")); |
| 165 | +#endif |
| 166 | +#endif |
| 167 | + |
| 168 | +#ifdef CONFIG_RPI_PICO_BINARY_INFO_PINS_WITH_FUNC_ENABLE |
| 169 | +#if DT_NODE_EXISTS(DT_NODELABEL(pinctrl)) |
| 170 | +/* |
| 171 | + * Extract pin info from pinctrl |
| 172 | + * |
| 173 | + * The Raspberry Pi Pico SDK ``bi_decl`` macro derives unique symbol names from |
| 174 | + * ``__LINE__``. Keep each instantiation on a dedicated source line rather |
| 175 | + * than using DT_FOREACH_CHILD, which would expand every group on the same line |
| 176 | + * and violate the uniqueness requirement. |
| 177 | + */ |
| 178 | +#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 0 |
| 179 | +BINARY_INFO_FROM_PINCFG(DT_NODELABEL(pinctrl), 0); |
| 180 | +#endif |
| 181 | +#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 1 |
| 182 | +BINARY_INFO_FROM_PINCFG(DT_NODELABEL(pinctrl), 1); |
| 183 | +#endif |
| 184 | +#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 2 |
| 185 | +BINARY_INFO_FROM_PINCFG(DT_NODELABEL(pinctrl), 2); |
| 186 | +#endif |
| 187 | +#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 3 |
| 188 | +BINARY_INFO_FROM_PINCFG(DT_NODELABEL(pinctrl), 3); |
| 189 | +#endif |
| 190 | +#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 4 |
| 191 | +BINARY_INFO_FROM_PINCFG(DT_NODELABEL(pinctrl), 4); |
| 192 | +#endif |
| 193 | +#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 5 |
| 194 | +BINARY_INFO_FROM_PINCFG(DT_NODELABEL(pinctrl), 5); |
| 195 | +#endif |
| 196 | +#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 6 |
| 197 | +BINARY_INFO_FROM_PINCFG(DT_NODELABEL(pinctrl), 6); |
| 198 | +#endif |
| 199 | +#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 7 |
| 200 | +BINARY_INFO_FROM_PINCFG(DT_NODELABEL(pinctrl), 7); |
| 201 | +#endif |
| 202 | +#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 8 |
| 203 | +BINARY_INFO_FROM_PINCFG(DT_NODELABEL(pinctrl), 8); |
| 204 | +#endif |
| 205 | +#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 9 |
| 206 | +BINARY_INFO_FROM_PINCFG(DT_NODELABEL(pinctrl), 9); |
| 207 | +#endif |
| 208 | +#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 10 |
| 209 | +BINARY_INFO_FROM_PINCFG(DT_NODELABEL(pinctrl), 10); |
| 210 | +#endif |
| 211 | +#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 11 |
| 212 | +BINARY_INFO_FROM_PINCFG(DT_NODELABEL(pinctrl), 11); |
| 213 | +#endif |
| 214 | +#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 12 |
| 215 | +BINARY_INFO_FROM_PINCFG(DT_NODELABEL(pinctrl), 12); |
| 216 | +#endif |
| 217 | +#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 13 |
| 218 | +BINARY_INFO_FROM_PINCFG(DT_NODELABEL(pinctrl), 13); |
| 219 | +#endif |
| 220 | +#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 14 |
| 221 | +BINARY_INFO_FROM_PINCFG(DT_NODELABEL(pinctrl), 14); |
| 222 | +#endif |
| 223 | +#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 15 |
| 224 | +BINARY_INFO_FROM_PINCFG(DT_NODELABEL(pinctrl), 15); |
| 225 | +#endif |
| 226 | +#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 16 |
| 227 | +BINARY_INFO_FROM_PINCFG(DT_NODELABEL(pinctrl), 16); |
| 228 | +#endif |
| 229 | +#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 17 |
| 230 | +BINARY_INFO_FROM_PINCFG(DT_NODELABEL(pinctrl), 17); |
| 231 | +#endif |
| 232 | +#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 18 |
| 233 | +BINARY_INFO_FROM_PINCFG(DT_NODELABEL(pinctrl), 18); |
| 234 | +#endif |
| 235 | +#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 19 |
| 236 | +BINARY_INFO_FROM_PINCFG(DT_NODELABEL(pinctrl), 19); |
| 237 | +#endif |
| 238 | +#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 20 |
| 239 | +BINARY_INFO_FROM_PINCFG(DT_NODELABEL(pinctrl), 20); |
| 240 | +#endif |
| 241 | +#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 21 |
| 242 | +BINARY_INFO_FROM_PINCFG(DT_NODELABEL(pinctrl), 21); |
| 243 | +#endif |
| 244 | +#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 22 |
| 245 | +BINARY_INFO_FROM_PINCFG(DT_NODELABEL(pinctrl), 22); |
| 246 | +#endif |
| 247 | +#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 23 |
| 248 | +BINARY_INFO_FROM_PINCFG(DT_NODELABEL(pinctrl), 23); |
| 249 | +#endif |
| 250 | +#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 24 |
| 251 | +BINARY_INFO_FROM_PINCFG(DT_NODELABEL(pinctrl), 24); |
| 252 | +#endif |
| 253 | +#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 25 |
| 254 | +BINARY_INFO_FROM_PINCFG(DT_NODELABEL(pinctrl), 25); |
| 255 | +#endif |
| 256 | +#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 26 |
| 257 | +BINARY_INFO_FROM_PINCFG(DT_NODELABEL(pinctrl), 26); |
| 258 | +#endif |
| 259 | +#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 27 |
| 260 | +BINARY_INFO_FROM_PINCFG(DT_NODELABEL(pinctrl), 27); |
| 261 | +#endif |
| 262 | +#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 28 |
| 263 | +BINARY_INFO_FROM_PINCFG(DT_NODELABEL(pinctrl), 28); |
| 264 | +#endif |
| 265 | +#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 29 |
| 266 | +BINARY_INFO_FROM_PINCFG(DT_NODELABEL(pinctrl), 29); |
| 267 | +#endif |
| 268 | +#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 30 |
| 269 | +BINARY_INFO_FROM_PINCFG(DT_NODELABEL(pinctrl), 30); |
| 270 | +#endif |
| 271 | +#if DT_CHILD_NUM(DT_NODELABEL(pinctrl)) > 31 |
| 272 | +BINARY_INFO_FROM_PINCFG(DT_NODELABEL(pinctrl), 31); |
| 273 | +#endif |
| 274 | +#endif /* DT_NODE_EXISTS(DT_NODELABEL(pinctrl)) */ |
| 275 | +#endif /* CONFIG_RPI_PICO_BINARY_INFO_PINS_WITH_FUNC_ENABLE */ |
0 commit comments