pinctrl: meson: axg: Fix GPIO pin offsets
[platform/kernel/u-boot.git] / drivers / mtd / mtd_uboot.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2014
4  * Heiko Schocher, DENX Software Engineering, hs@denx.de.
5  */
6 #include <common.h>
7 #include <dm/device.h>
8 #include <dm/uclass-internal.h>
9 #include <jffs2/jffs2.h> /* LEGACY */
10 #include <linux/mtd/mtd.h>
11 #include <linux/mtd/partitions.h>
12 #include <mtd.h>
13
14 #define MTD_NAME_MAX_LEN 20
15
16
17 /**
18  * mtd_search_alternate_name - Search an alternate name for @mtdname thanks to
19  *                             the mtdids legacy environment variable.
20  *
21  * The mtdids string is a list of comma-separated 'dev_id=mtd_id' tupples.
22  * Check if one of the mtd_id matches mtdname, in this case save dev_id in
23  * altname.
24  *
25  * @mtdname: Current MTD device name
26  * @altname: Alternate name to return
27  * @max_len: Length of the alternate name buffer
28  *
29  * @return 0 on success, an error otherwise.
30  */
31 int mtd_search_alternate_name(const char *mtdname, char *altname,
32                               unsigned int max_len)
33 {
34         const char *mtdids, *equal, *comma, *dev_id, *mtd_id;
35         int dev_id_len, mtd_id_len;
36
37         mtdids = env_get("mtdids");
38         if (!mtdids)
39                 return -EINVAL;
40
41         do {
42                 /* Find the '=' sign */
43                 dev_id = mtdids;
44                 equal = strchr(dev_id, '=');
45                 if (!equal)
46                         break;
47                 dev_id_len = equal - mtdids;
48                 mtd_id = equal + 1;
49
50                 /* Find the end of the tupple */
51                 comma = strchr(mtdids, ',');
52                 if (comma)
53                         mtd_id_len = comma - mtd_id;
54                 else
55                         mtd_id_len = &mtdids[strlen(mtdids)] - mtd_id + 1;
56
57                 if (!dev_id_len || !mtd_id_len)
58                         return -EINVAL;
59
60                 if (dev_id_len + 1 > max_len)
61                         continue;
62
63                 /* Compare the name we search with the current mtd_id */
64                 if (!strncmp(mtdname, mtd_id, mtd_id_len)) {
65                         strncpy(altname, dev_id, dev_id_len);
66                         altname[dev_id_len] = 0;
67
68                         return 0;
69                 }
70
71                 /* Go to the next tupple */
72                 mtdids = comma + 1;
73         } while (comma);
74
75         return -EINVAL;
76 }
77
78 #if IS_ENABLED(CONFIG_MTD)
79 static void mtd_probe_uclass_mtd_devs(void)
80 {
81         struct udevice *dev;
82         int idx = 0;
83
84         /* Probe devices with DM compliant drivers */
85         while (!uclass_find_device(UCLASS_MTD, idx, &dev) && dev) {
86                 mtd_probe(dev);
87                 idx++;
88         }
89 }
90 #else
91 static void mtd_probe_uclass_mtd_devs(void) { }
92 #endif
93
94 #if defined(CONFIG_MTD_PARTITIONS)
95 extern void board_mtdparts_default(const char **mtdids,
96                                    const char **mtdparts);
97
98 static const char *get_mtdids(void)
99 {
100         __maybe_unused const char *mtdparts = NULL;
101         const char *mtdids = env_get("mtdids");
102
103         if (mtdids)
104                 return mtdids;
105
106 #if defined(CONFIG_SYS_MTDPARTS_RUNTIME)
107         board_mtdparts_default(&mtdids, &mtdparts);
108 #elif defined(MTDIDS_DEFAULT)
109         mtdids = MTDIDS_DEFAULT;
110 #elif defined(CONFIG_MTDIDS_DEFAULT)
111         mtdids = CONFIG_MTDIDS_DEFAULT;
112 #endif
113
114         if (mtdids)
115                 env_set("mtdids", mtdids);
116
117         return mtdids;
118 }
119
120 #define MTDPARTS_MAXLEN         512
121
122 static const char *get_mtdparts(void)
123 {
124         __maybe_unused const char *mtdids = NULL;
125         static char tmp_parts[MTDPARTS_MAXLEN];
126         static bool use_defaults = true;
127         const char *mtdparts = NULL;
128
129         if (gd->flags & GD_FLG_ENV_READY)
130                 mtdparts = env_get("mtdparts");
131         else if (env_get_f("mtdparts", tmp_parts, sizeof(tmp_parts)) != -1)
132                 mtdparts = tmp_parts;
133
134         if (mtdparts || !use_defaults)
135                 return mtdparts;
136
137 #if defined(CONFIG_SYS_MTDPARTS_RUNTIME)
138         board_mtdparts_default(&mtdids, &mtdparts);
139 #elif defined(MTDPARTS_DEFAULT)
140         mtdparts = MTDPARTS_DEFAULT;
141 #elif defined(CONFIG_MTDPARTS_DEFAULT)
142         mtdparts = CONFIG_MTDPARTS_DEFAULT;
143 #endif
144
145         if (mtdparts)
146                 env_set("mtdparts", mtdparts);
147
148         use_defaults = false;
149
150         return mtdparts;
151 }
152
153 int mtd_probe_devices(void)
154 {
155         static char *old_mtdparts;
156         static char *old_mtdids;
157         const char *mtdparts = get_mtdparts();
158         const char *mtdids = get_mtdids();
159         bool remaining_partitions = true;
160         struct mtd_info *mtd;
161
162         mtd_probe_uclass_mtd_devs();
163
164         /* Check if mtdparts/mtdids changed since last call, otherwise: exit */
165         if ((!mtdparts && !old_mtdparts && !mtdids && !old_mtdids) ||
166             (mtdparts && old_mtdparts && mtdids && old_mtdids &&
167              !strcmp(mtdparts, old_mtdparts) &&
168              !strcmp(mtdids, old_mtdids)))
169                 return 0;
170
171         /* Update the local copy of mtdparts */
172         free(old_mtdparts);
173         free(old_mtdids);
174         old_mtdparts = strdup(mtdparts);
175         old_mtdids = strdup(mtdids);
176
177         /* If at least one partition is still in use, do not delete anything */
178         mtd_for_each_device(mtd) {
179                 if (mtd->usecount) {
180                         printf("Partition \"%s\" already in use, aborting\n",
181                                mtd->name);
182                         return -EACCES;
183                 }
184         }
185
186         /*
187          * Everything looks clear, remove all partitions. It is not safe to
188          * remove entries from the mtd_for_each_device loop as it uses idr
189          * indexes and the partitions removal is done in bulk (all partitions of
190          * one device at the same time), so break and iterate from start each
191          * time a new partition is found and deleted.
192          */
193         while (remaining_partitions) {
194                 remaining_partitions = false;
195                 mtd_for_each_device(mtd) {
196                         if (!mtd_is_partition(mtd) && mtd_has_partitions(mtd)) {
197                                 del_mtd_partitions(mtd);
198                                 remaining_partitions = true;
199                                 break;
200                         }
201                 }
202         }
203
204         /* If either mtdparts or mtdids is empty, then exit */
205         if (!mtdparts || !mtdids)
206                 return 0;
207
208         /* Start the parsing by ignoring the extra 'mtdparts=' prefix, if any */
209         if (strstr(mtdparts, "mtdparts="))
210                 mtdparts += 9;
211
212         /* For each MTD device in mtdparts */
213         while (mtdparts[0] != '\0') {
214                 char mtd_name[MTD_NAME_MAX_LEN], *colon;
215                 struct mtd_partition *parts;
216                 int mtd_name_len, nparts;
217                 int ret;
218
219                 colon = strchr(mtdparts, ':');
220                 if (!colon) {
221                         printf("Wrong mtdparts: %s\n", mtdparts);
222                         return -EINVAL;
223                 }
224
225                 mtd_name_len = colon - mtdparts;
226                 strncpy(mtd_name, mtdparts, mtd_name_len);
227                 mtd_name[mtd_name_len] = '\0';
228                 /* Move the pointer forward (including the ':') */
229                 mtdparts += mtd_name_len + 1;
230                 mtd = get_mtd_device_nm(mtd_name);
231                 if (IS_ERR_OR_NULL(mtd)) {
232                         char linux_name[MTD_NAME_MAX_LEN];
233
234                         /*
235                          * The MTD device named "mtd_name" does not exist. Try
236                          * to find a correspondance with an MTD device having
237                          * the same type and number as defined in the mtdids.
238                          */
239                         debug("No device named %s\n", mtd_name);
240                         ret = mtd_search_alternate_name(mtd_name, linux_name,
241                                                         MTD_NAME_MAX_LEN);
242                         if (!ret)
243                                 mtd = get_mtd_device_nm(linux_name);
244
245                         /*
246                          * If no device could be found, move the mtdparts
247                          * pointer forward until the next set of partitions.
248                          */
249                         if (ret || IS_ERR_OR_NULL(mtd)) {
250                                 printf("Could not find a valid device for %s\n",
251                                        mtd_name);
252                                 mtdparts = strchr(mtdparts, ';');
253                                 if (mtdparts)
254                                         mtdparts++;
255
256                                 continue;
257                         }
258                 }
259
260                 /*
261                  * Parse the MTD device partitions. It will update the mtdparts
262                  * pointer, create an array of parts (that must be freed), and
263                  * return the number of partition structures in the array.
264                  */
265                 ret = mtd_parse_partitions(mtd, &mtdparts, &parts, &nparts);
266                 if (ret) {
267                         printf("Could not parse device %s\n", mtd->name);
268                         put_mtd_device(mtd);
269                         return -EINVAL;
270                 }
271
272                 if (!nparts)
273                         continue;
274
275                 /* Create the new MTD partitions */
276                 add_mtd_partitions(mtd, parts, nparts);
277
278                 /* Free the structures allocated during the parsing */
279                 mtd_free_parsed_partitions(parts, nparts);
280
281                 put_mtd_device(mtd);
282         }
283
284         return 0;
285 }
286 #else
287 int mtd_probe_devices(void)
288 {
289         mtd_probe_uclass_mtd_devs();
290
291         return 0;
292 }
293 #endif /* defined(CONFIG_MTD_PARTITIONS) */
294
295 /* Legacy */
296
297 static int get_part(const char *partname, int *idx, loff_t *off, loff_t *size,
298                 loff_t *maxsize, int devtype)
299 {
300 #ifdef CONFIG_CMD_MTDPARTS
301         struct mtd_device *dev;
302         struct part_info *part;
303         u8 pnum;
304         int ret;
305
306         ret = mtdparts_init();
307         if (ret)
308                 return ret;
309
310         ret = find_dev_and_part(partname, &dev, &pnum, &part);
311         if (ret)
312                 return ret;
313
314         if (dev->id->type != devtype) {
315                 printf("not same typ %d != %d\n", dev->id->type, devtype);
316                 return -1;
317         }
318
319         *off = part->offset;
320         *size = part->size;
321         *maxsize = part->size;
322         *idx = dev->id->num;
323
324         return 0;
325 #else
326         puts("mtdparts support missing.\n");
327         return -1;
328 #endif
329 }
330
331 int mtd_arg_off(const char *arg, int *idx, loff_t *off, loff_t *size,
332                 loff_t *maxsize, int devtype, uint64_t chipsize)
333 {
334         if (!str2off(arg, off))
335                 return get_part(arg, idx, off, size, maxsize, devtype);
336
337         if (*off >= chipsize) {
338                 puts("Offset exceeds device limit\n");
339                 return -1;
340         }
341
342         *maxsize = chipsize - *off;
343         *size = *maxsize;
344         return 0;
345 }
346
347 int mtd_arg_off_size(int argc, char *const argv[], int *idx, loff_t *off,
348                      loff_t *size, loff_t *maxsize, int devtype,
349                      uint64_t chipsize)
350 {
351         int ret;
352
353         if (argc == 0) {
354                 *off = 0;
355                 *size = chipsize;
356                 *maxsize = *size;
357                 goto print;
358         }
359
360         ret = mtd_arg_off(argv[0], idx, off, size, maxsize, devtype,
361                           chipsize);
362         if (ret)
363                 return ret;
364
365         if (argc == 1)
366                 goto print;
367
368         if (!str2off(argv[1], size)) {
369                 printf("'%s' is not a number\n", argv[1]);
370                 return -1;
371         }
372
373         if (*size > *maxsize) {
374                 puts("Size exceeds partition or device limit\n");
375                 return -1;
376         }
377
378 print:
379         printf("device %d ", *idx);
380         if (*size == chipsize)
381                 puts("whole chip\n");
382         else
383                 printf("offset 0x%llx, size 0x%llx\n",
384                        (unsigned long long)*off, (unsigned long long)*size);
385         return 0;
386 }