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