Patrick Delaunay | daa5f01 | 2020-03-18 09:22:44 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause |
| 2 | /* |
| 3 | * Copyright (C) 2020, STMicroelectronics - All Rights Reserved |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Patrick Delaunay | c511224 | 2020-03-18 09:24:55 +0100 | [diff] [blame] | 7 | #include <dfu.h> |
Patrick Delaunay | daa5f01 | 2020-03-18 09:22:44 +0100 | [diff] [blame] | 8 | #include <dm.h> |
| 9 | #include <env.h> |
| 10 | #include <env_internal.h> |
| 11 | #include <mtd.h> |
| 12 | #include <mtd_node.h> |
Patrick Delaunay | 472407a | 2020-03-18 09:22:49 +0100 | [diff] [blame] | 13 | #include <tee.h> |
Patrick Delaunay | c511224 | 2020-03-18 09:24:55 +0100 | [diff] [blame] | 14 | #include <asm/arch/stm32prog.h> |
Patrick Delaunay | cbbb53a | 2020-03-18 09:22:53 +0100 | [diff] [blame] | 15 | #include <asm/arch/sys_proto.h> |
Patrick Delaunay | daa5f01 | 2020-03-18 09:22:44 +0100 | [diff] [blame] | 16 | |
| 17 | #define MTDPARTS_LEN 256 |
| 18 | #define MTDIDS_LEN 128 |
| 19 | |
| 20 | /* |
| 21 | * Get a global data pointer |
| 22 | */ |
| 23 | DECLARE_GLOBAL_DATA_PTR; |
| 24 | |
| 25 | /** |
Patrick Delaunay | 7511d14 | 2020-03-18 09:22:47 +0100 | [diff] [blame] | 26 | * update the variables "mtdids" and "mtdparts" with boot, tee and user strings |
Patrick Delaunay | daa5f01 | 2020-03-18 09:22:44 +0100 | [diff] [blame] | 27 | */ |
Patrick Delaunay | cbbb53a | 2020-03-18 09:22:53 +0100 | [diff] [blame] | 28 | static void board_set_mtdparts(const char *dev, |
Patrick Delaunay | daa5f01 | 2020-03-18 09:22:44 +0100 | [diff] [blame] | 29 | char *mtdids, |
Patrick Delaunay | 7511d14 | 2020-03-18 09:22:47 +0100 | [diff] [blame] | 30 | char *mtdparts, |
| 31 | const char *boot, |
| 32 | const char *tee, |
| 33 | const char *user) |
Patrick Delaunay | daa5f01 | 2020-03-18 09:22:44 +0100 | [diff] [blame] | 34 | { |
Patrick Delaunay | 7511d14 | 2020-03-18 09:22:47 +0100 | [diff] [blame] | 35 | /* mtdids: "<dev>=<dev>, ...." */ |
| 36 | if (mtdids[0] != '\0') |
| 37 | strcat(mtdids, ","); |
| 38 | strcat(mtdids, dev); |
| 39 | strcat(mtdids, "="); |
| 40 | strcat(mtdids, dev); |
Patrick Delaunay | daa5f01 | 2020-03-18 09:22:44 +0100 | [diff] [blame] | 41 | |
Patrick Delaunay | 7511d14 | 2020-03-18 09:22:47 +0100 | [diff] [blame] | 42 | /* mtdparts: "mtdparts=<dev>:<mtdparts_<dev>>;..." */ |
| 43 | if (mtdparts[0] != '\0') |
| 44 | strncat(mtdparts, ";", MTDPARTS_LEN); |
| 45 | else |
| 46 | strcat(mtdparts, "mtdparts="); |
Patrick Delaunay | daa5f01 | 2020-03-18 09:22:44 +0100 | [diff] [blame] | 47 | |
Patrick Delaunay | 7511d14 | 2020-03-18 09:22:47 +0100 | [diff] [blame] | 48 | strncat(mtdparts, dev, MTDPARTS_LEN); |
| 49 | strncat(mtdparts, ":", MTDPARTS_LEN); |
| 50 | |
| 51 | if (boot) { |
| 52 | strncat(mtdparts, boot, MTDPARTS_LEN); |
| 53 | strncat(mtdparts, ",", MTDPARTS_LEN); |
| 54 | } |
| 55 | |
Patrick Delaunay | 472407a | 2020-03-18 09:22:49 +0100 | [diff] [blame] | 56 | if (tee) { |
Patrick Delaunay | 7511d14 | 2020-03-18 09:22:47 +0100 | [diff] [blame] | 57 | strncat(mtdparts, tee, MTDPARTS_LEN); |
| 58 | strncat(mtdparts, ",", MTDPARTS_LEN); |
Patrick Delaunay | daa5f01 | 2020-03-18 09:22:44 +0100 | [diff] [blame] | 59 | } |
Patrick Delaunay | 7511d14 | 2020-03-18 09:22:47 +0100 | [diff] [blame] | 60 | |
| 61 | strncat(mtdparts, user, MTDPARTS_LEN); |
Patrick Delaunay | daa5f01 | 2020-03-18 09:22:44 +0100 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void board_mtdparts_default(const char **mtdids, const char **mtdparts) |
| 65 | { |
| 66 | struct mtd_info *mtd; |
| 67 | struct udevice *dev; |
| 68 | static char parts[3 * MTDPARTS_LEN + 1]; |
| 69 | static char ids[MTDIDS_LEN + 1]; |
| 70 | static bool mtd_initialized; |
Patrick Delaunay | c511224 | 2020-03-18 09:24:55 +0100 | [diff] [blame] | 71 | bool tee, nor, nand, spinand, serial; |
Patrick Delaunay | daa5f01 | 2020-03-18 09:22:44 +0100 | [diff] [blame] | 72 | |
| 73 | if (mtd_initialized) { |
| 74 | *mtdids = ids; |
| 75 | *mtdparts = parts; |
| 76 | return; |
| 77 | } |
| 78 | |
Patrick Delaunay | cbbb53a | 2020-03-18 09:22:53 +0100 | [diff] [blame] | 79 | tee = false; |
| 80 | nor = false; |
| 81 | nand = false; |
| 82 | spinand = false; |
Patrick Delaunay | c511224 | 2020-03-18 09:24:55 +0100 | [diff] [blame] | 83 | serial = false; |
Patrick Delaunay | cbbb53a | 2020-03-18 09:22:53 +0100 | [diff] [blame] | 84 | |
| 85 | switch (get_bootmode() & TAMP_BOOT_DEVICE_MASK) { |
| 86 | case BOOT_SERIAL_UART: |
| 87 | case BOOT_SERIAL_USB: |
Patrick Delaunay | c511224 | 2020-03-18 09:24:55 +0100 | [diff] [blame] | 88 | serial = true; |
| 89 | if (CONFIG_IS_ENABLED(CMD_STM32PROG)) { |
| 90 | tee = stm32prog_get_tee_partitions(); |
| 91 | nor = stm32prog_get_fsbl_nor(); |
| 92 | } |
| 93 | nand = true; |
| 94 | spinand = true; |
Patrick Delaunay | cbbb53a | 2020-03-18 09:22:53 +0100 | [diff] [blame] | 95 | break; |
| 96 | case BOOT_FLASH_NAND: |
| 97 | nand = true; |
| 98 | break; |
| 99 | case BOOT_FLASH_SPINAND: |
| 100 | spinand = true; |
| 101 | break; |
| 102 | case BOOT_FLASH_NOR: |
| 103 | nor = true; |
| 104 | break; |
| 105 | default: |
| 106 | break; |
| 107 | } |
| 108 | |
Patrick Delaunay | c511224 | 2020-03-18 09:24:55 +0100 | [diff] [blame] | 109 | if (!serial && CONFIG_IS_ENABLED(OPTEE) && |
Patrick Delaunay | 472407a | 2020-03-18 09:22:49 +0100 | [diff] [blame] | 110 | tee_find_device(NULL, NULL, NULL, NULL)) |
Patrick Delaunay | 7511d14 | 2020-03-18 09:22:47 +0100 | [diff] [blame] | 111 | tee = true; |
| 112 | |
Patrick Delaunay | daa5f01 | 2020-03-18 09:22:44 +0100 | [diff] [blame] | 113 | memset(parts, 0, sizeof(parts)); |
| 114 | memset(ids, 0, sizeof(ids)); |
| 115 | |
| 116 | /* probe all MTD devices */ |
| 117 | for (uclass_first_device(UCLASS_MTD, &dev); |
| 118 | dev; |
| 119 | uclass_next_device(&dev)) { |
| 120 | pr_debug("mtd device = %s\n", dev->name); |
| 121 | } |
| 122 | |
Patrick Delaunay | cbbb53a | 2020-03-18 09:22:53 +0100 | [diff] [blame] | 123 | if (nor || nand) { |
| 124 | mtd = get_mtd_device_nm("nand0"); |
| 125 | if (!IS_ERR_OR_NULL(mtd)) { |
| 126 | const char *mtd_boot = CONFIG_MTDPARTS_NAND0_BOOT; |
| 127 | const char *mtd_tee = CONFIG_MTDPARTS_NAND0_TEE; |
| 128 | |
| 129 | board_set_mtdparts("nand0", ids, parts, |
| 130 | !nor ? mtd_boot : NULL, |
| 131 | !nor && tee ? mtd_tee : NULL, |
| 132 | "-(UBI)"); |
| 133 | put_mtd_device(mtd); |
| 134 | } |
Patrick Delaunay | daa5f01 | 2020-03-18 09:22:44 +0100 | [diff] [blame] | 135 | } |
| 136 | |
Patrick Delaunay | cbbb53a | 2020-03-18 09:22:53 +0100 | [diff] [blame] | 137 | if (nor || spinand) { |
| 138 | mtd = get_mtd_device_nm("spi-nand0"); |
| 139 | if (!IS_ERR_OR_NULL(mtd)) { |
| 140 | const char *mtd_boot = CONFIG_MTDPARTS_SPINAND0_BOOT; |
| 141 | const char *mtd_tee = CONFIG_MTDPARTS_SPINAND0_TEE; |
| 142 | |
| 143 | board_set_mtdparts("spi-nand0", ids, parts, |
| 144 | !nor ? mtd_boot : NULL, |
| 145 | !nor && tee ? mtd_tee : NULL, |
| 146 | "-(UBI)"); |
| 147 | put_mtd_device(mtd); |
| 148 | } |
Patrick Delaunay | daa5f01 | 2020-03-18 09:22:44 +0100 | [diff] [blame] | 149 | } |
| 150 | |
Patrick Delaunay | cbbb53a | 2020-03-18 09:22:53 +0100 | [diff] [blame] | 151 | if (nor) { |
| 152 | if (!uclass_get_device(UCLASS_SPI_FLASH, 0, &dev)) { |
| 153 | const char *mtd_boot = CONFIG_MTDPARTS_NOR0_BOOT; |
| 154 | const char *mtd_tee = CONFIG_MTDPARTS_NOR0_TEE; |
| 155 | |
| 156 | board_set_mtdparts("nor0", ids, parts, |
| 157 | mtd_boot, |
| 158 | tee ? mtd_tee : NULL, |
| 159 | "-(nor_user)"); |
| 160 | } |
| 161 | } |
Patrick Delaunay | daa5f01 | 2020-03-18 09:22:44 +0100 | [diff] [blame] | 162 | |
| 163 | mtd_initialized = true; |
| 164 | *mtdids = ids; |
| 165 | *mtdparts = parts; |
| 166 | debug("%s:mtdids=%s & mtdparts=%s\n", __func__, ids, parts); |
| 167 | } |