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