Patrick Delaunay | ec2933e | 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 | 31325e1 | 2020-03-18 09:22:46 +0100 | [diff] [blame] | 7 | #include <blk.h> |
Patrick Delaunay | ec2933e | 2020-03-18 09:22:45 +0100 | [diff] [blame] | 8 | #include <dfu.h> |
| 9 | #include <env.h> |
| 10 | #include <memalign.h> |
| 11 | #include <misc.h> |
| 12 | #include <mtd.h> |
| 13 | #include <mtd_node.h> |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 14 | #include <asm/arch/stm32prog.h> |
Patrick Delaunay | ec2933e | 2020-03-18 09:22:45 +0100 | [diff] [blame] | 15 | |
| 16 | #define DFU_ALT_BUF_LEN SZ_1K |
| 17 | |
Patrick Delaunay | 31325e1 | 2020-03-18 09:22:46 +0100 | [diff] [blame] | 18 | static void board_get_alt_info_mmc(struct udevice *dev, char *buf) |
Patrick Delaunay | ec2933e | 2020-03-18 09:22:45 +0100 | [diff] [blame] | 19 | { |
Simon Glass | 0528979 | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 20 | struct disk_partition info; |
Patrick Delaunay | 31325e1 | 2020-03-18 09:22:46 +0100 | [diff] [blame] | 21 | int p, len, devnum; |
| 22 | bool first = true; |
| 23 | const char *name; |
| 24 | struct mmc *mmc; |
| 25 | struct blk_desc *desc; |
Patrick Delaunay | ec2933e | 2020-03-18 09:22:45 +0100 | [diff] [blame] | 26 | |
Patrick Delaunay | 31325e1 | 2020-03-18 09:22:46 +0100 | [diff] [blame] | 27 | mmc = mmc_get_mmc_dev(dev); |
| 28 | if (!mmc) |
| 29 | return; |
Patrick Delaunay | ec2933e | 2020-03-18 09:22:45 +0100 | [diff] [blame] | 30 | |
Patrick Delaunay | 31325e1 | 2020-03-18 09:22:46 +0100 | [diff] [blame] | 31 | if (mmc_init(mmc)) |
| 32 | return; |
| 33 | |
| 34 | desc = mmc_get_blk_desc(mmc); |
| 35 | if (!desc) |
| 36 | return; |
| 37 | |
| 38 | name = blk_get_if_type_name(desc->if_type); |
| 39 | devnum = desc->devnum; |
| 40 | len = strlen(buf); |
| 41 | |
| 42 | if (buf[0] != '\0') |
| 43 | len += snprintf(buf + len, |
| 44 | DFU_ALT_BUF_LEN - len, "&"); |
| 45 | len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, |
| 46 | "%s %d=", name, devnum); |
| 47 | |
| 48 | if (IS_MMC(mmc) && mmc->capacity_boot) { |
| 49 | len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, |
| 50 | "%s%d_boot1 raw 0x0 0x%llx mmcpart 1;", |
| 51 | name, devnum, mmc->capacity_boot); |
| 52 | len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, |
| 53 | "%s%d_boot2 raw 0x0 0x%llx mmcpart 2", |
| 54 | name, devnum, mmc->capacity_boot); |
| 55 | first = false; |
| 56 | } |
| 57 | |
| 58 | for (p = 1; p < MAX_SEARCH_PARTITIONS; p++) { |
| 59 | if (part_get_info(desc, p, &info)) |
| 60 | continue; |
| 61 | if (!first) |
| 62 | len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, ";"); |
| 63 | first = false; |
| 64 | len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, |
| 65 | "%s%d_%s part %d %d", |
| 66 | name, devnum, info.name, devnum, p); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | static void board_get_alt_info_mtd(struct mtd_info *mtd, char *buf) |
| 71 | { |
| 72 | struct mtd_info *part; |
| 73 | bool first = true; |
| 74 | const char *name; |
| 75 | int len, partnum = 0; |
| 76 | |
| 77 | name = mtd->name; |
| 78 | len = strlen(buf); |
| 79 | |
| 80 | if (buf[0] != '\0') |
| 81 | len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, "&"); |
| 82 | len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, |
| 83 | "mtd %s=", name); |
| 84 | |
| 85 | len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, |
| 86 | "%s raw 0x0 0x%llx ", |
| 87 | name, mtd->size); |
| 88 | |
| 89 | list_for_each_entry(part, &mtd->partitions, node) { |
| 90 | partnum++; |
| 91 | if (!first) |
| 92 | len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, ";"); |
| 93 | first = false; |
| 94 | |
| 95 | len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, |
| 96 | "%s_%s part %d", |
| 97 | name, part->name, partnum); |
Patrick Delaunay | ec2933e | 2020-03-18 09:22:45 +0100 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
| 101 | void set_dfu_alt_info(char *interface, char *devstr) |
| 102 | { |
| 103 | struct udevice *dev; |
| 104 | struct mtd_info *mtd; |
| 105 | |
| 106 | ALLOC_CACHE_ALIGN_BUFFER(char, buf, DFU_ALT_BUF_LEN); |
| 107 | |
| 108 | if (env_get("dfu_alt_info")) |
| 109 | return; |
| 110 | |
| 111 | memset(buf, 0, sizeof(buf)); |
| 112 | |
Patrick Delaunay | 31325e1 | 2020-03-18 09:22:46 +0100 | [diff] [blame] | 113 | snprintf(buf, DFU_ALT_BUF_LEN, |
| 114 | "ram 0=%s", CONFIG_DFU_ALT_RAM0); |
Patrick Delaunay | ec2933e | 2020-03-18 09:22:45 +0100 | [diff] [blame] | 115 | |
| 116 | if (!uclass_get_device(UCLASS_MMC, 0, &dev)) |
Patrick Delaunay | 31325e1 | 2020-03-18 09:22:46 +0100 | [diff] [blame] | 117 | board_get_alt_info_mmc(dev, buf); |
Patrick Delaunay | ec2933e | 2020-03-18 09:22:45 +0100 | [diff] [blame] | 118 | |
| 119 | if (!uclass_get_device(UCLASS_MMC, 1, &dev)) |
Patrick Delaunay | 31325e1 | 2020-03-18 09:22:46 +0100 | [diff] [blame] | 120 | board_get_alt_info_mmc(dev, buf); |
Patrick Delaunay | ec2933e | 2020-03-18 09:22:45 +0100 | [diff] [blame] | 121 | |
Patrick Delaunay | 31325e1 | 2020-03-18 09:22:46 +0100 | [diff] [blame] | 122 | if (CONFIG_IS_ENABLED(MTD)) { |
| 123 | /* probe all MTD devices */ |
| 124 | mtd_probe_devices(); |
Patrick Delaunay | ec2933e | 2020-03-18 09:22:45 +0100 | [diff] [blame] | 125 | |
Patrick Delaunay | 31325e1 | 2020-03-18 09:22:46 +0100 | [diff] [blame] | 126 | /* probe SPI flash device on a bus */ |
| 127 | if (!uclass_get_device(UCLASS_SPI_FLASH, 0, &dev)) { |
| 128 | mtd = get_mtd_device_nm("nor0"); |
| 129 | if (!IS_ERR_OR_NULL(mtd)) |
| 130 | board_get_alt_info_mtd(mtd, buf); |
| 131 | } |
Patrick Delaunay | ec2933e | 2020-03-18 09:22:45 +0100 | [diff] [blame] | 132 | |
Patrick Delaunay | 31325e1 | 2020-03-18 09:22:46 +0100 | [diff] [blame] | 133 | mtd = get_mtd_device_nm("nand0"); |
| 134 | if (!IS_ERR_OR_NULL(mtd)) |
| 135 | board_get_alt_info_mtd(mtd, buf); |
| 136 | |
| 137 | mtd = get_mtd_device_nm("spi-nand0"); |
| 138 | if (!IS_ERR_OR_NULL(mtd)) |
| 139 | board_get_alt_info_mtd(mtd, buf); |
| 140 | } |
Patrick Delaunay | ec2933e | 2020-03-18 09:22:45 +0100 | [diff] [blame] | 141 | |
| 142 | #ifdef CONFIG_DFU_VIRT |
| 143 | strncat(buf, "&virt 0=OTP", DFU_ALT_BUF_LEN); |
| 144 | |
| 145 | if (IS_ENABLED(CONFIG_PMIC_STPMIC1)) |
| 146 | strncat(buf, "&virt 1=PMIC", DFU_ALT_BUF_LEN); |
| 147 | #endif |
| 148 | |
| 149 | env_set("dfu_alt_info", buf); |
| 150 | puts("DFU alt info setting: done\n"); |
| 151 | } |
| 152 | |
| 153 | #if CONFIG_IS_ENABLED(DFU_VIRT) |
| 154 | #include <dfu.h> |
| 155 | #include <power/stpmic1.h> |
| 156 | |
| 157 | static int dfu_otp_read(u64 offset, u8 *buffer, long *size) |
| 158 | { |
| 159 | struct udevice *dev; |
| 160 | int ret; |
| 161 | |
| 162 | ret = uclass_get_device_by_driver(UCLASS_MISC, |
| 163 | DM_GET_DRIVER(stm32mp_bsec), |
| 164 | &dev); |
| 165 | if (ret) |
| 166 | return ret; |
| 167 | |
| 168 | ret = misc_read(dev, offset + STM32_BSEC_OTP_OFFSET, buffer, *size); |
| 169 | if (ret >= 0) { |
| 170 | *size = ret; |
| 171 | ret = 0; |
| 172 | } |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | static int dfu_pmic_read(u64 offset, u8 *buffer, long *size) |
| 178 | { |
| 179 | int ret; |
| 180 | #ifdef CONFIG_PMIC_STPMIC1 |
| 181 | struct udevice *dev; |
| 182 | |
| 183 | ret = uclass_get_device_by_driver(UCLASS_MISC, |
| 184 | DM_GET_DRIVER(stpmic1_nvm), |
| 185 | &dev); |
| 186 | if (ret) |
| 187 | return ret; |
| 188 | |
| 189 | ret = misc_read(dev, 0xF8 + offset, buffer, *size); |
| 190 | if (ret >= 0) { |
| 191 | *size = ret; |
| 192 | ret = 0; |
| 193 | } |
| 194 | if (ret == -EACCES) { |
| 195 | *size = 0; |
| 196 | ret = 0; |
| 197 | } |
| 198 | #else |
| 199 | pr_err("PMIC update not supported"); |
| 200 | ret = -EOPNOTSUPP; |
| 201 | #endif |
| 202 | |
| 203 | return ret; |
| 204 | } |
| 205 | |
| 206 | int dfu_read_medium_virt(struct dfu_entity *dfu, u64 offset, |
| 207 | void *buf, long *len) |
| 208 | { |
| 209 | switch (dfu->data.virt.dev_num) { |
| 210 | case 0x0: |
| 211 | return dfu_otp_read(offset, buf, len); |
| 212 | case 0x1: |
| 213 | return dfu_pmic_read(offset, buf, len); |
| 214 | } |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 215 | |
| 216 | if (CONFIG_IS_ENABLED(CMD_STM32PROG) && |
| 217 | dfu->data.virt.dev_num >= STM32PROG_VIRT_FIRST_DEV_NUM) |
| 218 | return stm32prog_read_medium_virt(dfu, offset, buf, len); |
| 219 | |
Patrick Delaunay | ec2933e | 2020-03-18 09:22:45 +0100 | [diff] [blame] | 220 | *len = 0; |
| 221 | return 0; |
| 222 | } |
| 223 | |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 224 | int dfu_write_medium_virt(struct dfu_entity *dfu, u64 offset, |
| 225 | void *buf, long *len) |
| 226 | { |
| 227 | if (CONFIG_IS_ENABLED(CMD_STM32PROG) && |
| 228 | dfu->data.virt.dev_num >= STM32PROG_VIRT_FIRST_DEV_NUM) |
| 229 | return stm32prog_write_medium_virt(dfu, offset, buf, len); |
| 230 | |
| 231 | return -EOPNOTSUPP; |
| 232 | } |
| 233 | |
Patrick Delaunay | ec2933e | 2020-03-18 09:22:45 +0100 | [diff] [blame] | 234 | int __weak dfu_get_medium_size_virt(struct dfu_entity *dfu, u64 *size) |
| 235 | { |
Patrick Delaunay | 954bd1a | 2020-03-18 09:24:49 +0100 | [diff] [blame] | 236 | if (CONFIG_IS_ENABLED(CMD_STM32PROG) && |
| 237 | dfu->data.virt.dev_num >= STM32PROG_VIRT_FIRST_DEV_NUM) |
| 238 | return stm32prog_get_medium_size_virt(dfu, size); |
| 239 | |
Patrick Delaunay | ec2933e | 2020-03-18 09:22:45 +0100 | [diff] [blame] | 240 | *size = SZ_1K; |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | #endif |