xref: /rk3399_rockchip-uboot/drivers/mtd/mtd_uboot.c (revision b97f59501ad5ba57650edc98a0caf156ca0cd16d)
109c32807SHeiko Schocher /*
209c32807SHeiko Schocher  * (C) Copyright 2014
309c32807SHeiko Schocher  * Heiko Schocher, DENX Software Engineering, hs@denx.de.
409c32807SHeiko Schocher  *
509c32807SHeiko Schocher  * SPDX-License-Identifier:	GPL-2.0+
609c32807SHeiko Schocher  */
709c32807SHeiko Schocher #include <common.h>
8f87151b3SMiquel Raynal #include <dm/device.h>
9*b97f5950SJon Lin #include <dm/device-internal.h>
10f87151b3SMiquel Raynal #include <dm/uclass-internal.h>
11f87151b3SMiquel Raynal #include <jffs2/jffs2.h> /* LEGACY */
1209c32807SHeiko Schocher #include <linux/mtd/mtd.h>
13f87151b3SMiquel Raynal #include <linux/mtd/partitions.h>
14f87151b3SMiquel Raynal #include <mtd.h>
15f87151b3SMiquel Raynal 
16f87151b3SMiquel Raynal #define MTD_NAME_MAX_LEN 20
17f87151b3SMiquel Raynal 
184ac4e964SBoris Brezillon void board_mtdparts_default(const char **mtdids, const char **mtdparts);
194ac4e964SBoris Brezillon 
get_mtdids(void)204ac4e964SBoris Brezillon static const char *get_mtdids(void)
214ac4e964SBoris Brezillon {
224ac4e964SBoris Brezillon 	__maybe_unused const char *mtdparts = NULL;
234ac4e964SBoris Brezillon 	const char *mtdids = env_get("mtdids");
244ac4e964SBoris Brezillon 
254ac4e964SBoris Brezillon 	if (mtdids)
264ac4e964SBoris Brezillon 		return mtdids;
274ac4e964SBoris Brezillon 
284ac4e964SBoris Brezillon #if defined(CONFIG_SYS_MTDPARTS_RUNTIME)
294ac4e964SBoris Brezillon 	board_mtdparts_default(&mtdids, &mtdparts);
304ac4e964SBoris Brezillon #elif defined(MTDIDS_DEFAULT)
314ac4e964SBoris Brezillon 	mtdids = MTDIDS_DEFAULT;
324ac4e964SBoris Brezillon #elif defined(CONFIG_MTDIDS_DEFAULT)
334ac4e964SBoris Brezillon 	mtdids = CONFIG_MTDIDS_DEFAULT;
344ac4e964SBoris Brezillon #endif
354ac4e964SBoris Brezillon 
364ac4e964SBoris Brezillon 	if (mtdids)
374ac4e964SBoris Brezillon 		env_set("mtdids", mtdids);
384ac4e964SBoris Brezillon 
394ac4e964SBoris Brezillon 	return mtdids;
404ac4e964SBoris Brezillon }
4150466819SMiquel Raynal 
4250466819SMiquel Raynal /**
4350466819SMiquel Raynal  * mtd_search_alternate_name - Search an alternate name for @mtdname thanks to
4450466819SMiquel Raynal  *                             the mtdids legacy environment variable.
4550466819SMiquel Raynal  *
4650466819SMiquel Raynal  * The mtdids string is a list of comma-separated 'dev_id=mtd_id' tupples.
4750466819SMiquel Raynal  * Check if one of the mtd_id matches mtdname, in this case save dev_id in
4850466819SMiquel Raynal  * altname.
4950466819SMiquel Raynal  *
5050466819SMiquel Raynal  * @mtdname: Current MTD device name
5150466819SMiquel Raynal  * @altname: Alternate name to return
5250466819SMiquel Raynal  * @max_len: Length of the alternate name buffer
5350466819SMiquel Raynal  *
5450466819SMiquel Raynal  * @return 0 on success, an error otherwise.
5550466819SMiquel Raynal  */
mtd_search_alternate_name(const char * mtdname,char * altname,unsigned int max_len)5650466819SMiquel Raynal int mtd_search_alternate_name(const char *mtdname, char *altname,
5750466819SMiquel Raynal 			      unsigned int max_len)
5850466819SMiquel Raynal {
5950466819SMiquel Raynal 	const char *mtdids, *equal, *comma, *dev_id, *mtd_id;
6050466819SMiquel Raynal 	int dev_id_len, mtd_id_len;
6150466819SMiquel Raynal 
624ac4e964SBoris Brezillon 	mtdids = get_mtdids();
6350466819SMiquel Raynal 	if (!mtdids)
6450466819SMiquel Raynal 		return -EINVAL;
6550466819SMiquel Raynal 
6650466819SMiquel Raynal 	do {
6750466819SMiquel Raynal 		/* Find the '=' sign */
6850466819SMiquel Raynal 		dev_id = mtdids;
6950466819SMiquel Raynal 		equal = strchr(dev_id, '=');
7050466819SMiquel Raynal 		if (!equal)
7150466819SMiquel Raynal 			break;
7250466819SMiquel Raynal 		dev_id_len = equal - mtdids;
7350466819SMiquel Raynal 		mtd_id = equal + 1;
7450466819SMiquel Raynal 
7550466819SMiquel Raynal 		/* Find the end of the tupple */
7650466819SMiquel Raynal 		comma = strchr(mtdids, ',');
7750466819SMiquel Raynal 		if (comma)
7850466819SMiquel Raynal 			mtd_id_len = comma - mtd_id;
7950466819SMiquel Raynal 		else
8050466819SMiquel Raynal 			mtd_id_len = &mtdids[strlen(mtdids)] - mtd_id + 1;
8150466819SMiquel Raynal 
8250466819SMiquel Raynal 		if (!dev_id_len || !mtd_id_len)
8350466819SMiquel Raynal 			return -EINVAL;
8450466819SMiquel Raynal 
8550466819SMiquel Raynal 		if (dev_id_len + 1 > max_len)
8650466819SMiquel Raynal 			continue;
8750466819SMiquel Raynal 
8850466819SMiquel Raynal 		/* Compare the name we search with the current mtd_id */
8950466819SMiquel Raynal 		if (!strncmp(mtdname, mtd_id, mtd_id_len)) {
9050466819SMiquel Raynal 			strncpy(altname, dev_id, dev_id_len);
9150466819SMiquel Raynal 			altname[dev_id_len] = 0;
9250466819SMiquel Raynal 
9350466819SMiquel Raynal 			return 0;
9450466819SMiquel Raynal 		}
9550466819SMiquel Raynal 
9650466819SMiquel Raynal 		/* Go to the next tupple */
9750466819SMiquel Raynal 		mtdids = comma + 1;
9850466819SMiquel Raynal 	} while (comma);
9950466819SMiquel Raynal 
10050466819SMiquel Raynal 	return -EINVAL;
10150466819SMiquel Raynal }
10250466819SMiquel Raynal 
103f87151b3SMiquel Raynal #if IS_ENABLED(CONFIG_MTD)
mtd_probe_uclass_mtd_devs(void)104f87151b3SMiquel Raynal static void mtd_probe_uclass_mtd_devs(void)
105f87151b3SMiquel Raynal {
106f87151b3SMiquel Raynal 	struct udevice *dev;
107f87151b3SMiquel Raynal 	int idx = 0;
108f87151b3SMiquel Raynal 
109f87151b3SMiquel Raynal 	/* Probe devices with DM compliant drivers */
110f87151b3SMiquel Raynal 	while (!uclass_find_device(UCLASS_MTD, idx, &dev) && dev) {
111f87151b3SMiquel Raynal 		mtd_probe(dev);
112f87151b3SMiquel Raynal 		idx++;
113f87151b3SMiquel Raynal 	}
114f87151b3SMiquel Raynal }
115f87151b3SMiquel Raynal #else
mtd_probe_uclass_mtd_devs(void)116f87151b3SMiquel Raynal static void mtd_probe_uclass_mtd_devs(void) { }
117f87151b3SMiquel Raynal #endif
118f87151b3SMiquel Raynal 
119*b97f5950SJon Lin #if IS_ENABLED(CONFIG_DM_SPI_FLASH) && IS_ENABLED(CONFIG_SPI_FLASH_MTD)
mtd_probe_uclass_spi_nor_devs(void)120*b97f5950SJon Lin static void __maybe_unused mtd_probe_uclass_spi_nor_devs(void)
121*b97f5950SJon Lin {
122*b97f5950SJon Lin 	struct udevice *dev;
123*b97f5950SJon Lin 	int idx = 0;
124*b97f5950SJon Lin 
125*b97f5950SJon Lin 	/* Probe devices with DM compliant drivers */
126*b97f5950SJon Lin 	while (!uclass_find_device(UCLASS_SPI_FLASH, idx, &dev) && dev) {
127*b97f5950SJon Lin 		device_probe(dev);
128*b97f5950SJon Lin 		idx++;
129*b97f5950SJon Lin 	}
130*b97f5950SJon Lin }
131*b97f5950SJon Lin #else
mtd_probe_uclass_spi_nor_devs(void)132*b97f5950SJon Lin static void __maybe_unused mtd_probe_uclass_spi_nor_devs(void) { }
133*b97f5950SJon Lin #endif
134*b97f5950SJon Lin 
135f87151b3SMiquel Raynal #if defined(CONFIG_MTD_PARTITIONS)
136b95c8f6cSBoris Brezillon 
137b95c8f6cSBoris Brezillon #define MTDPARTS_MAXLEN         512
138b95c8f6cSBoris Brezillon 
get_mtdparts(void)139b95c8f6cSBoris Brezillon static const char *get_mtdparts(void)
140b95c8f6cSBoris Brezillon {
141b95c8f6cSBoris Brezillon 	__maybe_unused const char *mtdids = NULL;
142b95c8f6cSBoris Brezillon 	static char tmp_parts[MTDPARTS_MAXLEN];
143b95c8f6cSBoris Brezillon 	const char *mtdparts = NULL;
144b95c8f6cSBoris Brezillon 
145b95c8f6cSBoris Brezillon 	if (gd->flags & GD_FLG_ENV_READY)
146b95c8f6cSBoris Brezillon 		mtdparts = env_get("mtdparts");
147b95c8f6cSBoris Brezillon 	else if (env_get_f("mtdparts", tmp_parts, sizeof(tmp_parts)) != -1)
148b95c8f6cSBoris Brezillon 		mtdparts = tmp_parts;
149b95c8f6cSBoris Brezillon 
150a62211b4SPatrice Chotard 	if (mtdparts)
151b95c8f6cSBoris Brezillon 		return mtdparts;
152b95c8f6cSBoris Brezillon 
153b95c8f6cSBoris Brezillon #if defined(CONFIG_SYS_MTDPARTS_RUNTIME)
154b95c8f6cSBoris Brezillon 	board_mtdparts_default(&mtdids, &mtdparts);
155b95c8f6cSBoris Brezillon #elif defined(MTDPARTS_DEFAULT)
156b95c8f6cSBoris Brezillon 	mtdparts = MTDPARTS_DEFAULT;
157b95c8f6cSBoris Brezillon #elif defined(CONFIG_MTDPARTS_DEFAULT)
158b95c8f6cSBoris Brezillon 	mtdparts = CONFIG_MTDPARTS_DEFAULT;
159b95c8f6cSBoris Brezillon #endif
160b95c8f6cSBoris Brezillon 
161b95c8f6cSBoris Brezillon 	if (mtdparts)
162b95c8f6cSBoris Brezillon 		env_set("mtdparts", mtdparts);
163b95c8f6cSBoris Brezillon 
164b95c8f6cSBoris Brezillon 	return mtdparts;
165b95c8f6cSBoris Brezillon }
166b95c8f6cSBoris Brezillon 
mtd_del_parts(struct mtd_info * mtd,bool quiet)167ef964b01SBoris Brezillon static int mtd_del_parts(struct mtd_info *mtd, bool quiet)
168ef964b01SBoris Brezillon {
169ef964b01SBoris Brezillon 	int ret;
170ef964b01SBoris Brezillon 
171ef964b01SBoris Brezillon 	if (!mtd_has_partitions(mtd))
172ef964b01SBoris Brezillon 		return 0;
173ef964b01SBoris Brezillon 
174ef964b01SBoris Brezillon 	/* do not delete partitions if they are in use. */
175ef964b01SBoris Brezillon 	if (mtd_partitions_used(mtd)) {
176ef964b01SBoris Brezillon 		if (!quiet)
177ef964b01SBoris Brezillon 			printf("\"%s\" partitions still in use, can't delete them\n",
178ef964b01SBoris Brezillon 			       mtd->name);
179ef964b01SBoris Brezillon 		return -EACCES;
180ef964b01SBoris Brezillon 	}
181ef964b01SBoris Brezillon 
182ef964b01SBoris Brezillon 	ret = del_mtd_partitions(mtd);
183ef964b01SBoris Brezillon 	if (ret)
184ef964b01SBoris Brezillon 		return ret;
185ef964b01SBoris Brezillon 
186ef964b01SBoris Brezillon 	return 1;
187ef964b01SBoris Brezillon }
188ef964b01SBoris Brezillon 
189ef964b01SBoris Brezillon static bool mtd_del_all_parts_failed;
190ef964b01SBoris Brezillon 
mtd_del_all_parts(void)191ef964b01SBoris Brezillon static void mtd_del_all_parts(void)
192ef964b01SBoris Brezillon {
193ef964b01SBoris Brezillon 	struct mtd_info *mtd;
194ef964b01SBoris Brezillon 	int ret = 0;
195ef964b01SBoris Brezillon 
196ef964b01SBoris Brezillon 	mtd_del_all_parts_failed = false;
197ef964b01SBoris Brezillon 
198ef964b01SBoris Brezillon 	/*
199ef964b01SBoris Brezillon 	 * It is not safe to remove entries from the mtd_for_each_device loop
200ef964b01SBoris Brezillon 	 * as it uses idr indexes and the partitions removal is done in bulk
201ef964b01SBoris Brezillon 	 * (all partitions of one device at the same time), so break and
202ef964b01SBoris Brezillon 	 * iterate from start each time a new partition is found and deleted.
203ef964b01SBoris Brezillon 	 */
204ef964b01SBoris Brezillon 	do {
205ef964b01SBoris Brezillon 		mtd_for_each_device(mtd) {
206ef964b01SBoris Brezillon 			ret = mtd_del_parts(mtd, false);
207ef964b01SBoris Brezillon 			if (ret > 0)
208ef964b01SBoris Brezillon 				break;
209ef964b01SBoris Brezillon 			else if (ret < 0)
210ef964b01SBoris Brezillon 				mtd_del_all_parts_failed = true;
211ef964b01SBoris Brezillon 		}
212ef964b01SBoris Brezillon 	} while (ret > 0);
213ef964b01SBoris Brezillon }
214ef964b01SBoris Brezillon 
mtd_probe_devices(void)215f87151b3SMiquel Raynal int mtd_probe_devices(void)
216f87151b3SMiquel Raynal {
217f87151b3SMiquel Raynal 	static char *old_mtdparts;
218f87151b3SMiquel Raynal 	static char *old_mtdids;
219b95c8f6cSBoris Brezillon 	const char *mtdparts = get_mtdparts();
220b95c8f6cSBoris Brezillon 	const char *mtdids = get_mtdids();
221a38bf0a9SBoris Brezillon 	const char *mtdparts_next = mtdparts;
222f87151b3SMiquel Raynal 	struct mtd_info *mtd;
223f87151b3SMiquel Raynal 
224f87151b3SMiquel Raynal 	mtd_probe_uclass_mtd_devs();
225*b97f5950SJon Lin 	mtd_probe_uclass_spi_nor_devs();
226f87151b3SMiquel Raynal 
2274c33ae3bSBoris Brezillon 	/*
228ef964b01SBoris Brezillon 	 * Check if mtdparts/mtdids changed, if the MTD dev list was updated
229ef964b01SBoris Brezillon 	 * or if our previous attempt to delete existing partititions failed.
230ef964b01SBoris Brezillon 	 * In any of these cases we want to update the partitions, otherwise,
231ef964b01SBoris Brezillon 	 * everything is up-to-date and we can return 0 directly.
2324c33ae3bSBoris Brezillon 	 */
2334696f08fSAdam Ford 	if ((!mtdparts && !old_mtdparts && !mtdids && !old_mtdids) ||
2344696f08fSAdam Ford 	    (mtdparts && old_mtdparts && mtdids && old_mtdids &&
235ef964b01SBoris Brezillon 	     !mtd_dev_list_updated() && !mtd_del_all_parts_failed &&
2364696f08fSAdam Ford 	     !strcmp(mtdparts, old_mtdparts) &&
2374696f08fSAdam Ford 	     !strcmp(mtdids, old_mtdids)))
238f87151b3SMiquel Raynal 		return 0;
239f87151b3SMiquel Raynal 
240f87151b3SMiquel Raynal 	/* Update the local copy of mtdparts */
241f87151b3SMiquel Raynal 	free(old_mtdparts);
242f87151b3SMiquel Raynal 	free(old_mtdids);
243f87151b3SMiquel Raynal 	old_mtdparts = strdup(mtdparts);
244f87151b3SMiquel Raynal 	old_mtdids = strdup(mtdids);
245f87151b3SMiquel Raynal 
246f87151b3SMiquel Raynal 	/*
247ef964b01SBoris Brezillon 	 * Remove all old parts. Note that partition removal can fail in case
248ef964b01SBoris Brezillon 	 * one of the partition is still being used by an MTD user, so this
249ef964b01SBoris Brezillon 	 * does not guarantee that all old partitions are gone.
250f87151b3SMiquel Raynal 	 */
251ef964b01SBoris Brezillon 	mtd_del_all_parts();
252f87151b3SMiquel Raynal 
2534c33ae3bSBoris Brezillon 	/*
2544c33ae3bSBoris Brezillon 	 * Call mtd_dev_list_updated() to clear updates generated by our own
2554c33ae3bSBoris Brezillon 	 * parts removal loop.
2564c33ae3bSBoris Brezillon 	 */
2574c33ae3bSBoris Brezillon 	mtd_dev_list_updated();
2584c33ae3bSBoris Brezillon 
2594696f08fSAdam Ford 	/* If either mtdparts or mtdids is empty, then exit */
2604696f08fSAdam Ford 	if (!mtdparts || !mtdids)
2614696f08fSAdam Ford 		return 0;
2624696f08fSAdam Ford 
263f87151b3SMiquel Raynal 	/* Start the parsing by ignoring the extra 'mtdparts=' prefix, if any */
264e3112a2cSBoris Brezillon 	if (!strncmp(mtdparts, "mtdparts=", sizeof("mtdparts=") - 1))
265f87151b3SMiquel Raynal 		mtdparts += 9;
266f87151b3SMiquel Raynal 
267f87151b3SMiquel Raynal 	/* For each MTD device in mtdparts */
268a38bf0a9SBoris Brezillon 	for (; mtdparts[0] != '\0'; mtdparts = mtdparts_next) {
269f87151b3SMiquel Raynal 		char mtd_name[MTD_NAME_MAX_LEN], *colon;
270f87151b3SMiquel Raynal 		struct mtd_partition *parts;
2717a13c786SBoris Brezillon 		unsigned int mtd_name_len;
2727a13c786SBoris Brezillon 		int nparts, ret;
273f87151b3SMiquel Raynal 
274a38bf0a9SBoris Brezillon 		mtdparts_next = strchr(mtdparts, ';');
275a38bf0a9SBoris Brezillon 		if (!mtdparts_next)
276a38bf0a9SBoris Brezillon 			mtdparts_next = mtdparts + strlen(mtdparts);
277a38bf0a9SBoris Brezillon 		else
278a38bf0a9SBoris Brezillon 			mtdparts_next++;
279a38bf0a9SBoris Brezillon 
280f87151b3SMiquel Raynal 		colon = strchr(mtdparts, ':');
281a38bf0a9SBoris Brezillon 		if (colon > mtdparts_next)
282a38bf0a9SBoris Brezillon 			colon = NULL;
283a38bf0a9SBoris Brezillon 
284f87151b3SMiquel Raynal 		if (!colon) {
285f87151b3SMiquel Raynal 			printf("Wrong mtdparts: %s\n", mtdparts);
286f87151b3SMiquel Raynal 			return -EINVAL;
287f87151b3SMiquel Raynal 		}
288f87151b3SMiquel Raynal 
2897a13c786SBoris Brezillon 		mtd_name_len = (unsigned int)(colon - mtdparts);
2907a13c786SBoris Brezillon 		if (mtd_name_len + 1 > sizeof(mtd_name)) {
2917a13c786SBoris Brezillon 			printf("MTD name too long: %s\n", mtdparts);
2927a13c786SBoris Brezillon 			return -EINVAL;
2937a13c786SBoris Brezillon 		}
2947a13c786SBoris Brezillon 
295f87151b3SMiquel Raynal 		strncpy(mtd_name, mtdparts, mtd_name_len);
296f87151b3SMiquel Raynal 		mtd_name[mtd_name_len] = '\0';
297f87151b3SMiquel Raynal 		/* Move the pointer forward (including the ':') */
298f87151b3SMiquel Raynal 		mtdparts += mtd_name_len + 1;
299f87151b3SMiquel Raynal 		mtd = get_mtd_device_nm(mtd_name);
300f87151b3SMiquel Raynal 		if (IS_ERR_OR_NULL(mtd)) {
301f87151b3SMiquel Raynal 			char linux_name[MTD_NAME_MAX_LEN];
302f87151b3SMiquel Raynal 
303f87151b3SMiquel Raynal 			/*
304f87151b3SMiquel Raynal 			 * The MTD device named "mtd_name" does not exist. Try
305f87151b3SMiquel Raynal 			 * to find a correspondance with an MTD device having
306f87151b3SMiquel Raynal 			 * the same type and number as defined in the mtdids.
307f87151b3SMiquel Raynal 			 */
308f87151b3SMiquel Raynal 			debug("No device named %s\n", mtd_name);
309f87151b3SMiquel Raynal 			ret = mtd_search_alternate_name(mtd_name, linux_name,
310f87151b3SMiquel Raynal 							MTD_NAME_MAX_LEN);
311f87151b3SMiquel Raynal 			if (!ret)
312f87151b3SMiquel Raynal 				mtd = get_mtd_device_nm(linux_name);
313f87151b3SMiquel Raynal 
314f87151b3SMiquel Raynal 			/*
315f87151b3SMiquel Raynal 			 * If no device could be found, move the mtdparts
316f87151b3SMiquel Raynal 			 * pointer forward until the next set of partitions.
317f87151b3SMiquel Raynal 			 */
318f87151b3SMiquel Raynal 			if (ret || IS_ERR_OR_NULL(mtd)) {
319f87151b3SMiquel Raynal 				printf("Could not find a valid device for %s\n",
320f87151b3SMiquel Raynal 				       mtd_name);
321a38bf0a9SBoris Brezillon 				mtdparts = mtdparts_next;
322f87151b3SMiquel Raynal 				continue;
323f87151b3SMiquel Raynal 			}
324f87151b3SMiquel Raynal 		}
325f87151b3SMiquel Raynal 
326f87151b3SMiquel Raynal 		/*
327ef964b01SBoris Brezillon 		 * Call mtd_del_parts() again, even if it's already been called
328ef964b01SBoris Brezillon 		 * in mtd_del_all_parts(). We need to know if old partitions are
329ef964b01SBoris Brezillon 		 * still around (because they are still being used by someone),
330ef964b01SBoris Brezillon 		 * and if they are, we shouldn't create new partitions, so just
331ef964b01SBoris Brezillon 		 * skip this MTD device and try the next one.
332ef964b01SBoris Brezillon 		 */
333ef964b01SBoris Brezillon 		ret = mtd_del_parts(mtd, true);
334ef964b01SBoris Brezillon 		if (ret < 0)
335ef964b01SBoris Brezillon 			continue;
336ef964b01SBoris Brezillon 
337ef964b01SBoris Brezillon 		/*
338f87151b3SMiquel Raynal 		 * Parse the MTD device partitions. It will update the mtdparts
339f87151b3SMiquel Raynal 		 * pointer, create an array of parts (that must be freed), and
340f87151b3SMiquel Raynal 		 * return the number of partition structures in the array.
341f87151b3SMiquel Raynal 		 */
342f87151b3SMiquel Raynal 		ret = mtd_parse_partitions(mtd, &mtdparts, &parts, &nparts);
343f87151b3SMiquel Raynal 		if (ret) {
344f87151b3SMiquel Raynal 			printf("Could not parse device %s\n", mtd->name);
345f87151b3SMiquel Raynal 			put_mtd_device(mtd);
346f87151b3SMiquel Raynal 			return -EINVAL;
347f87151b3SMiquel Raynal 		}
348f87151b3SMiquel Raynal 
349f87151b3SMiquel Raynal 		if (!nparts)
350f87151b3SMiquel Raynal 			continue;
351f87151b3SMiquel Raynal 
352f87151b3SMiquel Raynal 		/* Create the new MTD partitions */
353f87151b3SMiquel Raynal 		add_mtd_partitions(mtd, parts, nparts);
354f87151b3SMiquel Raynal 
355f87151b3SMiquel Raynal 		/* Free the structures allocated during the parsing */
356f87151b3SMiquel Raynal 		mtd_free_parsed_partitions(parts, nparts);
357f87151b3SMiquel Raynal 
358f87151b3SMiquel Raynal 		put_mtd_device(mtd);
359f87151b3SMiquel Raynal 	}
360f87151b3SMiquel Raynal 
3614c33ae3bSBoris Brezillon 	/*
3624c33ae3bSBoris Brezillon 	 * Call mtd_dev_list_updated() to clear updates generated by our own
3634c33ae3bSBoris Brezillon 	 * parts registration loop.
3644c33ae3bSBoris Brezillon 	 */
3654c33ae3bSBoris Brezillon 	mtd_dev_list_updated();
3664c33ae3bSBoris Brezillon 
367f87151b3SMiquel Raynal 	return 0;
368f87151b3SMiquel Raynal }
369f87151b3SMiquel Raynal #else
mtd_probe_devices(void)370f87151b3SMiquel Raynal int mtd_probe_devices(void)
371f87151b3SMiquel Raynal {
372f87151b3SMiquel Raynal 	mtd_probe_uclass_mtd_devs();
373*b97f5950SJon Lin 	mtd_probe_uclass_spi_nor_devs();
374f87151b3SMiquel Raynal 
375f87151b3SMiquel Raynal 	return 0;
376f87151b3SMiquel Raynal }
377f87151b3SMiquel Raynal #endif /* defined(CONFIG_MTD_PARTITIONS) */
378f87151b3SMiquel Raynal 
37950466819SMiquel Raynal /* Legacy */
38009c32807SHeiko Schocher 
get_part(const char * partname,int * idx,loff_t * off,loff_t * size,loff_t * maxsize,int devtype)38109c32807SHeiko Schocher static int get_part(const char *partname, int *idx, loff_t *off, loff_t *size,
38209c32807SHeiko Schocher 		loff_t *maxsize, int devtype)
38309c32807SHeiko Schocher {
38409c32807SHeiko Schocher #ifdef CONFIG_CMD_MTDPARTS
38509c32807SHeiko Schocher 	struct mtd_device *dev;
38609c32807SHeiko Schocher 	struct part_info *part;
38709c32807SHeiko Schocher 	u8 pnum;
38809c32807SHeiko Schocher 	int ret;
38909c32807SHeiko Schocher 
39009c32807SHeiko Schocher 	ret = mtdparts_init();
39109c32807SHeiko Schocher 	if (ret)
39209c32807SHeiko Schocher 		return ret;
39309c32807SHeiko Schocher 
39409c32807SHeiko Schocher 	ret = find_dev_and_part(partname, &dev, &pnum, &part);
39509c32807SHeiko Schocher 	if (ret)
39609c32807SHeiko Schocher 		return ret;
39709c32807SHeiko Schocher 
39809c32807SHeiko Schocher 	if (dev->id->type != devtype) {
39909c32807SHeiko Schocher 		printf("not same typ %d != %d\n", dev->id->type, devtype);
40009c32807SHeiko Schocher 		return -1;
40109c32807SHeiko Schocher 	}
40209c32807SHeiko Schocher 
40309c32807SHeiko Schocher 	*off = part->offset;
40409c32807SHeiko Schocher 	*size = part->size;
40509c32807SHeiko Schocher 	*maxsize = part->size;
40609c32807SHeiko Schocher 	*idx = dev->id->num;
40709c32807SHeiko Schocher 
40809c32807SHeiko Schocher 	return 0;
40909c32807SHeiko Schocher #else
41010b69712SMaxime Ripard 	puts("mtdparts support missing.\n");
41109c32807SHeiko Schocher 	return -1;
41209c32807SHeiko Schocher #endif
41309c32807SHeiko Schocher }
41409c32807SHeiko Schocher 
mtd_arg_off(const char * arg,int * idx,loff_t * off,loff_t * size,loff_t * maxsize,int devtype,uint64_t chipsize)41509c32807SHeiko Schocher int mtd_arg_off(const char *arg, int *idx, loff_t *off, loff_t *size,
416f18d1116SMasahiro Yamada 		loff_t *maxsize, int devtype, uint64_t chipsize)
41709c32807SHeiko Schocher {
41809c32807SHeiko Schocher 	if (!str2off(arg, off))
41909c32807SHeiko Schocher 		return get_part(arg, idx, off, size, maxsize, devtype);
42009c32807SHeiko Schocher 
42109c32807SHeiko Schocher 	if (*off >= chipsize) {
42209c32807SHeiko Schocher 		puts("Offset exceeds device limit\n");
42309c32807SHeiko Schocher 		return -1;
42409c32807SHeiko Schocher 	}
42509c32807SHeiko Schocher 
42609c32807SHeiko Schocher 	*maxsize = chipsize - *off;
42709c32807SHeiko Schocher 	*size = *maxsize;
42809c32807SHeiko Schocher 	return 0;
42909c32807SHeiko Schocher }
43009c32807SHeiko Schocher 
mtd_arg_off_size(int argc,char * const argv[],int * idx,loff_t * off,loff_t * size,loff_t * maxsize,int devtype,uint64_t chipsize)43109c32807SHeiko Schocher int mtd_arg_off_size(int argc, char *const argv[], int *idx, loff_t *off,
432f18d1116SMasahiro Yamada 		     loff_t *size, loff_t *maxsize, int devtype,
433f18d1116SMasahiro Yamada 		     uint64_t chipsize)
43409c32807SHeiko Schocher {
43509c32807SHeiko Schocher 	int ret;
43609c32807SHeiko Schocher 
43709c32807SHeiko Schocher 	if (argc == 0) {
43809c32807SHeiko Schocher 		*off = 0;
43909c32807SHeiko Schocher 		*size = chipsize;
44009c32807SHeiko Schocher 		*maxsize = *size;
44109c32807SHeiko Schocher 		goto print;
44209c32807SHeiko Schocher 	}
44309c32807SHeiko Schocher 
44409c32807SHeiko Schocher 	ret = mtd_arg_off(argv[0], idx, off, size, maxsize, devtype,
44509c32807SHeiko Schocher 			  chipsize);
44609c32807SHeiko Schocher 	if (ret)
44709c32807SHeiko Schocher 		return ret;
44809c32807SHeiko Schocher 
44909c32807SHeiko Schocher 	if (argc == 1)
45009c32807SHeiko Schocher 		goto print;
45109c32807SHeiko Schocher 
45209c32807SHeiko Schocher 	if (!str2off(argv[1], size)) {
45309c32807SHeiko Schocher 		printf("'%s' is not a number\n", argv[1]);
45409c32807SHeiko Schocher 		return -1;
45509c32807SHeiko Schocher 	}
45609c32807SHeiko Schocher 
45709c32807SHeiko Schocher 	if (*size > *maxsize) {
45809c32807SHeiko Schocher 		puts("Size exceeds partition or device limit\n");
45909c32807SHeiko Schocher 		return -1;
46009c32807SHeiko Schocher 	}
46109c32807SHeiko Schocher 
46209c32807SHeiko Schocher print:
46309c32807SHeiko Schocher 	printf("device %d ", *idx);
46409c32807SHeiko Schocher 	if (*size == chipsize)
46509c32807SHeiko Schocher 		puts("whole chip\n");
46609c32807SHeiko Schocher 	else
46709c32807SHeiko Schocher 		printf("offset 0x%llx, size 0x%llx\n",
46809c32807SHeiko Schocher 		       (unsigned long long)*off, (unsigned long long)*size);
46909c32807SHeiko Schocher 	return 0;
47009c32807SHeiko Schocher }
471