1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
3 * Copyright (C) 2020, STMicroelectronics - All Rights Reserved
16 #include <asm/arch/stm32prog.h>
18 #define DFU_ALT_BUF_LEN SZ_1K
20 static void board_get_alt_info_mmc(struct udevice *dev, char *buf)
22 struct disk_partition info;
27 struct blk_desc *desc;
29 mmc = mmc_get_mmc_dev(dev);
36 desc = mmc_get_blk_desc(mmc);
40 name = blk_get_if_type_name(desc->if_type);
41 devnum = desc->devnum;
45 len += snprintf(buf + len,
46 DFU_ALT_BUF_LEN - len, "&");
47 len += snprintf(buf + len, DFU_ALT_BUF_LEN - len,
48 "%s %d=", name, devnum);
50 if (IS_MMC(mmc) && mmc->capacity_boot) {
51 len += snprintf(buf + len, DFU_ALT_BUF_LEN - len,
52 "%s%d_boot1 raw 0x0 0x%llx mmcpart 1;",
53 name, devnum, mmc->capacity_boot);
54 len += snprintf(buf + len, DFU_ALT_BUF_LEN - len,
55 "%s%d_boot2 raw 0x0 0x%llx mmcpart 2",
56 name, devnum, mmc->capacity_boot);
60 for (p = 1; p < MAX_SEARCH_PARTITIONS; p++) {
61 if (part_get_info(desc, p, &info))
64 len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, ";");
66 len += snprintf(buf + len, DFU_ALT_BUF_LEN - len,
68 name, devnum, info.name, devnum, p);
72 static void board_get_alt_info_mtd(struct mtd_info *mtd, char *buf)
74 struct mtd_info *part;
83 len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, "&");
84 len += snprintf(buf + len, DFU_ALT_BUF_LEN - len,
87 len += snprintf(buf + len, DFU_ALT_BUF_LEN - len,
91 list_for_each_entry(part, &mtd->partitions, node) {
94 len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, ";");
97 len += snprintf(buf + len, DFU_ALT_BUF_LEN - len,
99 name, part->name, partnum);
103 void set_dfu_alt_info(char *interface, char *devstr)
106 struct mtd_info *mtd;
108 ALLOC_CACHE_ALIGN_BUFFER(char, buf, DFU_ALT_BUF_LEN);
110 if (env_get("dfu_alt_info"))
113 memset(buf, 0, sizeof(buf));
115 snprintf(buf, DFU_ALT_BUF_LEN,
116 "ram 0=%s", CONFIG_DFU_ALT_RAM0);
118 if (CONFIG_IS_ENABLED(MMC)) {
119 if (!uclass_get_device(UCLASS_MMC, 0, &dev))
120 board_get_alt_info_mmc(dev, buf);
122 if (!uclass_get_device(UCLASS_MMC, 1, &dev))
123 board_get_alt_info_mmc(dev, buf);
126 if (CONFIG_IS_ENABLED(MTD)) {
127 /* probe all MTD devices */
130 /* probe SPI flash device on a bus */
131 if (!uclass_get_device(UCLASS_SPI_FLASH, 0, &dev)) {
132 mtd = get_mtd_device_nm("nor0");
133 if (!IS_ERR_OR_NULL(mtd))
134 board_get_alt_info_mtd(mtd, buf);
136 mtd = get_mtd_device_nm("nor1");
137 if (!IS_ERR_OR_NULL(mtd))
138 board_get_alt_info_mtd(mtd, buf);
141 mtd = get_mtd_device_nm("nand0");
142 if (!IS_ERR_OR_NULL(mtd))
143 board_get_alt_info_mtd(mtd, buf);
145 mtd = get_mtd_device_nm("spi-nand0");
146 if (!IS_ERR_OR_NULL(mtd))
147 board_get_alt_info_mtd(mtd, buf);
150 if (IS_ENABLED(CONFIG_DFU_VIRT) &&
151 IS_ENABLED(CMD_STM32PROG_USB)) {
152 strncat(buf, "&virt 0=OTP", DFU_ALT_BUF_LEN);
154 if (IS_ENABLED(CONFIG_PMIC_STPMIC1))
155 strncat(buf, "&virt 1=PMIC", DFU_ALT_BUF_LEN);
158 env_set("dfu_alt_info", buf);
159 puts("DFU alt info setting: done\n");
162 #if CONFIG_IS_ENABLED(DFU_VIRT)
164 #include <power/stpmic1.h>
166 static int dfu_otp_read(u64 offset, u8 *buffer, long *size)
171 ret = uclass_get_device_by_driver(UCLASS_MISC,
172 DM_DRIVER_GET(stm32mp_bsec),
177 ret = misc_read(dev, offset + STM32_BSEC_OTP_OFFSET, buffer, *size);
186 static int dfu_pmic_read(u64 offset, u8 *buffer, long *size)
189 #ifdef CONFIG_PMIC_STPMIC1
192 ret = uclass_get_device_by_driver(UCLASS_MISC,
193 DM_DRIVER_GET(stpmic1_nvm),
198 ret = misc_read(dev, 0xF8 + offset, buffer, *size);
203 if (ret == -EACCES) {
208 log_err("PMIC update not supported");
215 int dfu_read_medium_virt(struct dfu_entity *dfu, u64 offset,
216 void *buf, long *len)
218 switch (dfu->data.virt.dev_num) {
220 return dfu_otp_read(offset, buf, len);
222 return dfu_pmic_read(offset, buf, len);
225 if (IS_ENABLED(CONFIG_CMD_STM32PROG_USB) &&
226 dfu->data.virt.dev_num >= STM32PROG_VIRT_FIRST_DEV_NUM)
227 return stm32prog_read_medium_virt(dfu, offset, buf, len);
233 int dfu_write_medium_virt(struct dfu_entity *dfu, u64 offset,
234 void *buf, long *len)
236 if (IS_ENABLED(CONFIG_CMD_STM32PROG_USB) &&
237 dfu->data.virt.dev_num >= STM32PROG_VIRT_FIRST_DEV_NUM)
238 return stm32prog_write_medium_virt(dfu, offset, buf, len);
243 int __weak dfu_get_medium_size_virt(struct dfu_entity *dfu, u64 *size)
245 if (IS_ENABLED(CONFIG_CMD_STM32PROG_USB) &&
246 dfu->data.virt.dev_num >= STM32PROG_VIRT_FIRST_DEV_NUM)
247 return stm32prog_get_medium_size_virt(dfu, size);