Patrick Delaunay | cf07343 | 2020-03-18 09:22:45 +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 | c7d5b8c | 2020-03-18 09:22:46 +0100 | [diff] [blame] | 7 | #include <blk.h> |
Simon Glass | 7130b95 | 2020-07-19 10:15:40 -0600 | [diff] [blame] | 8 | #include <dm.h> |
Patrick Delaunay | cf07343 | 2020-03-18 09:22:45 +0100 | [diff] [blame] | 9 | #include <dfu.h> |
| 10 | #include <env.h> |
Patrick Delaunay | 9742bee | 2020-11-06 19:02:00 +0100 | [diff] [blame] | 11 | #include <log.h> |
Patrick Delaunay | cf07343 | 2020-03-18 09:22:45 +0100 | [diff] [blame] | 12 | #include <memalign.h> |
| 13 | #include <misc.h> |
| 14 | #include <mtd.h> |
| 15 | #include <mtd_node.h> |
Patrick Delaunay | 7daa91d | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 16 | #include <asm/arch/stm32prog.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 17 | #include <linux/printk.h> |
Patrick Delaunay | cf07343 | 2020-03-18 09:22:45 +0100 | [diff] [blame] | 18 | |
| 19 | #define DFU_ALT_BUF_LEN SZ_1K |
| 20 | |
Patrick Delaunay | c7d5b8c | 2020-03-18 09:22:46 +0100 | [diff] [blame] | 21 | static void board_get_alt_info_mmc(struct udevice *dev, char *buf) |
Patrick Delaunay | cf07343 | 2020-03-18 09:22:45 +0100 | [diff] [blame] | 22 | { |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 23 | struct disk_partition info; |
Patrick Delaunay | c7d5b8c | 2020-03-18 09:22:46 +0100 | [diff] [blame] | 24 | int p, len, devnum; |
| 25 | bool first = true; |
| 26 | const char *name; |
| 27 | struct mmc *mmc; |
| 28 | struct blk_desc *desc; |
Patrick Delaunay | cf07343 | 2020-03-18 09:22:45 +0100 | [diff] [blame] | 29 | |
Patrick Delaunay | c7d5b8c | 2020-03-18 09:22:46 +0100 | [diff] [blame] | 30 | mmc = mmc_get_mmc_dev(dev); |
| 31 | if (!mmc) |
| 32 | return; |
| 33 | |
| 34 | if (mmc_init(mmc)) |
| 35 | return; |
| 36 | |
| 37 | desc = mmc_get_blk_desc(mmc); |
| 38 | if (!desc) |
| 39 | return; |
| 40 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 41 | name = blk_get_uclass_name(desc->uclass_id); |
Patrick Delaunay | c7d5b8c | 2020-03-18 09:22:46 +0100 | [diff] [blame] | 42 | devnum = desc->devnum; |
| 43 | len = strlen(buf); |
Patrick Delaunay | cf07343 | 2020-03-18 09:22:45 +0100 | [diff] [blame] | 44 | |
Patrick Delaunay | c7d5b8c | 2020-03-18 09:22:46 +0100 | [diff] [blame] | 45 | if (buf[0] != '\0') |
| 46 | len += snprintf(buf + len, |
| 47 | DFU_ALT_BUF_LEN - len, "&"); |
| 48 | len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, |
| 49 | "%s %d=", name, devnum); |
| 50 | |
| 51 | if (IS_MMC(mmc) && mmc->capacity_boot) { |
| 52 | len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, |
| 53 | "%s%d_boot1 raw 0x0 0x%llx mmcpart 1;", |
| 54 | name, devnum, mmc->capacity_boot); |
| 55 | len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, |
| 56 | "%s%d_boot2 raw 0x0 0x%llx mmcpart 2", |
| 57 | name, devnum, mmc->capacity_boot); |
| 58 | first = false; |
| 59 | } |
| 60 | |
Heinrich Schuchardt | 890c55c | 2022-01-11 15:58:08 +0100 | [diff] [blame] | 61 | for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) { |
Patrick Delaunay | c7d5b8c | 2020-03-18 09:22:46 +0100 | [diff] [blame] | 62 | if (part_get_info(desc, p, &info)) |
| 63 | continue; |
| 64 | if (!first) |
| 65 | len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, ";"); |
| 66 | first = false; |
| 67 | len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, |
| 68 | "%s%d_%s part %d %d", |
| 69 | name, devnum, info.name, devnum, p); |
Patrick Delaunay | cf07343 | 2020-03-18 09:22:45 +0100 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
Patrick Delaunay | c7d5b8c | 2020-03-18 09:22:46 +0100 | [diff] [blame] | 73 | static void board_get_alt_info_mtd(struct mtd_info *mtd, char *buf) |
| 74 | { |
| 75 | struct mtd_info *part; |
| 76 | bool first = true; |
| 77 | const char *name; |
| 78 | int len, partnum = 0; |
| 79 | |
| 80 | name = mtd->name; |
| 81 | len = strlen(buf); |
| 82 | |
| 83 | if (buf[0] != '\0') |
| 84 | len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, "&"); |
| 85 | len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, |
| 86 | "mtd %s=", name); |
| 87 | |
| 88 | len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, |
| 89 | "%s raw 0x0 0x%llx ", |
| 90 | name, mtd->size); |
| 91 | |
| 92 | list_for_each_entry(part, &mtd->partitions, node) { |
| 93 | partnum++; |
| 94 | if (!first) |
| 95 | len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, ";"); |
| 96 | first = false; |
| 97 | |
| 98 | len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, |
| 99 | "%s_%s part %d", |
| 100 | name, part->name, partnum); |
| 101 | } |
| 102 | } |
| 103 | |
Patrick Delaunay | cf07343 | 2020-03-18 09:22:45 +0100 | [diff] [blame] | 104 | void set_dfu_alt_info(char *interface, char *devstr) |
| 105 | { |
| 106 | struct udevice *dev; |
| 107 | struct mtd_info *mtd; |
| 108 | |
| 109 | ALLOC_CACHE_ALIGN_BUFFER(char, buf, DFU_ALT_BUF_LEN); |
| 110 | |
| 111 | if (env_get("dfu_alt_info")) |
| 112 | return; |
| 113 | |
| 114 | memset(buf, 0, sizeof(buf)); |
| 115 | |
Patrick Delaunay | c7d5b8c | 2020-03-18 09:22:46 +0100 | [diff] [blame] | 116 | snprintf(buf, DFU_ALT_BUF_LEN, |
| 117 | "ram 0=%s", CONFIG_DFU_ALT_RAM0); |
Patrick Delaunay | cf07343 | 2020-03-18 09:22:45 +0100 | [diff] [blame] | 118 | |
Patrick Delaunay | b7b6752 | 2020-07-31 16:31:46 +0200 | [diff] [blame] | 119 | if (CONFIG_IS_ENABLED(MMC)) { |
| 120 | if (!uclass_get_device(UCLASS_MMC, 0, &dev)) |
| 121 | board_get_alt_info_mmc(dev, buf); |
Patrick Delaunay | cf07343 | 2020-03-18 09:22:45 +0100 | [diff] [blame] | 122 | |
Patrick Delaunay | b7b6752 | 2020-07-31 16:31:46 +0200 | [diff] [blame] | 123 | if (!uclass_get_device(UCLASS_MMC, 1, &dev)) |
| 124 | board_get_alt_info_mmc(dev, buf); |
| 125 | } |
Patrick Delaunay | cf07343 | 2020-03-18 09:22:45 +0100 | [diff] [blame] | 126 | |
Simon Glass | 8e05bb1 | 2023-02-05 15:40:17 -0700 | [diff] [blame] | 127 | if (IS_ENABLED(CONFIG_MTD)) { |
Patrick Delaunay | c7d5b8c | 2020-03-18 09:22:46 +0100 | [diff] [blame] | 128 | /* probe all MTD devices */ |
| 129 | mtd_probe_devices(); |
Patrick Delaunay | cf07343 | 2020-03-18 09:22:45 +0100 | [diff] [blame] | 130 | |
Patrick Delaunay | c7d5b8c | 2020-03-18 09:22:46 +0100 | [diff] [blame] | 131 | /* probe SPI flash device on a bus */ |
| 132 | if (!uclass_get_device(UCLASS_SPI_FLASH, 0, &dev)) { |
| 133 | mtd = get_mtd_device_nm("nor0"); |
| 134 | if (!IS_ERR_OR_NULL(mtd)) |
| 135 | board_get_alt_info_mtd(mtd, buf); |
Patrick Delaunay | b82c97e | 2021-11-25 11:54:53 +0100 | [diff] [blame] | 136 | |
| 137 | mtd = get_mtd_device_nm("nor1"); |
| 138 | if (!IS_ERR_OR_NULL(mtd)) |
| 139 | board_get_alt_info_mtd(mtd, buf); |
Patrick Delaunay | c7d5b8c | 2020-03-18 09:22:46 +0100 | [diff] [blame] | 140 | } |
Patrick Delaunay | cf07343 | 2020-03-18 09:22:45 +0100 | [diff] [blame] | 141 | |
Patrick Delaunay | c7d5b8c | 2020-03-18 09:22:46 +0100 | [diff] [blame] | 142 | mtd = get_mtd_device_nm("nand0"); |
| 143 | if (!IS_ERR_OR_NULL(mtd)) |
| 144 | board_get_alt_info_mtd(mtd, buf); |
| 145 | |
| 146 | mtd = get_mtd_device_nm("spi-nand0"); |
| 147 | if (!IS_ERR_OR_NULL(mtd)) |
| 148 | board_get_alt_info_mtd(mtd, buf); |
| 149 | } |
Patrick Delaunay | cf07343 | 2020-03-18 09:22:45 +0100 | [diff] [blame] | 150 | |
Patrick Delaunay | 16d3771 | 2023-09-26 17:09:23 +0200 | [diff] [blame] | 151 | if (IS_ENABLED(CONFIG_DFU_VIRT)) { |
| 152 | /* virtual device id 0 is aligned with stm32mp_dfu_virt.c */ |
| 153 | strlcat(buf, "&virt 0=OTP", DFU_ALT_BUF_LEN); |
Patrick Delaunay | cf07343 | 2020-03-18 09:22:45 +0100 | [diff] [blame] | 154 | |
Patrick Delaunay | b7b6752 | 2020-07-31 16:31:46 +0200 | [diff] [blame] | 155 | if (IS_ENABLED(CONFIG_PMIC_STPMIC1)) |
Patrick Delaunay | 16d3771 | 2023-09-26 17:09:23 +0200 | [diff] [blame] | 156 | strlcat(buf, "&virt 1=PMIC", DFU_ALT_BUF_LEN); |
Patrick Delaunay | b7b6752 | 2020-07-31 16:31:46 +0200 | [diff] [blame] | 157 | } |
Patrick Delaunay | cf07343 | 2020-03-18 09:22:45 +0100 | [diff] [blame] | 158 | |
| 159 | env_set("dfu_alt_info", buf); |
| 160 | puts("DFU alt info setting: done\n"); |
| 161 | } |