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