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