Ricardo Pardini | 7cfbe20 | 2024-07-31 09:03:31 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | |
| 3 | #include <env.h> |
| 4 | #include <asm/gpio.h> |
| 5 | |
| 6 | struct board_model { |
| 7 | int value; |
| 8 | const char *fdtfile; |
| 9 | const char *config; |
| 10 | }; |
| 11 | |
| 12 | static const struct board_model board_models[] = { |
| 13 | { 0, "rockchip/rk3566-orangepi-3b-v1.1.dtb", "rk3566-orangepi-3b-v1.1.dtb" }, |
| 14 | { 1, "rockchip/rk3566-orangepi-3b-v2.1.dtb", "rk3566-orangepi-3b-v2.1.dtb" }, |
| 15 | }; |
| 16 | |
| 17 | static int get_board_value(void) |
| 18 | { |
| 19 | struct gpio_desc desc; |
| 20 | int ret; |
| 21 | |
| 22 | /* |
| 23 | * GPIO4_C4 (E20): |
| 24 | * v1.1.1: x (internal pull-down) |
| 25 | * v2.1: PHY_RESET (external pull-up) |
| 26 | */ |
| 27 | ret = dm_gpio_lookup_name("E20", &desc); |
| 28 | if (ret) |
| 29 | return ret; |
| 30 | |
| 31 | ret = dm_gpio_request(&desc, "phy_reset"); |
| 32 | if (ret && ret != -EBUSY) |
| 33 | return ret; |
| 34 | |
| 35 | dm_gpio_set_dir_flags(&desc, GPIOD_IS_IN); |
| 36 | ret = dm_gpio_get_value(&desc); |
| 37 | dm_gpio_free(desc.dev, &desc); |
| 38 | |
| 39 | return ret; |
| 40 | } |
| 41 | |
| 42 | static const struct board_model *get_board_model(void) |
| 43 | { |
| 44 | int i, val; |
| 45 | |
| 46 | val = get_board_value(); |
| 47 | if (val < 0) |
| 48 | return NULL; |
| 49 | |
| 50 | for (i = 0; i < ARRAY_SIZE(board_models); i++) { |
| 51 | if (val == board_models[i].value) |
| 52 | return &board_models[i]; |
| 53 | } |
| 54 | |
| 55 | return NULL; |
| 56 | } |
| 57 | |
| 58 | int rk_board_late_init(void) |
| 59 | { |
| 60 | const struct board_model *model = get_board_model(); |
| 61 | |
| 62 | if (model) |
| 63 | env_set("fdtfile", model->fdtfile); |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | int board_fit_config_name_match(const char *name) |
| 69 | { |
| 70 | const struct board_model *model = get_board_model(); |
| 71 | |
| 72 | if (model && (!strcmp(name, model->fdtfile) || |
| 73 | !strcmp(name, model->config))) |
| 74 | return 0; |
| 75 | |
| 76 | return -EINVAL; |
| 77 | } |