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