fdt: Add a function to get the alias sequence of a node

Aliases are used to provide U-Boot's numbering of devices, such as:

aliases {
	spi0 = "/spi@12330000";
}

spi@12330000 {
	...
}

This tells us that the SPI controller at 12330000 is considered to be the
first SPI controller (SPI 0). So we have a numbering for the SPI node.

Add a function that returns the numbering for a node assume that it exists
in the list of aliases.

Signed-off-by: Simon Glass <sjg@chromium.org>
diff --git a/lib/fdtdec.c b/lib/fdtdec.c
index aaa6620..1b4ae9f 100644
--- a/lib/fdtdec.c
+++ b/lib/fdtdec.c
@@ -5,9 +5,11 @@
 
 #ifndef USE_HOSTCC
 #include <common.h>
+#include <errno.h>
 #include <serial.h>
 #include <libfdt.h>
 #include <fdtdec.h>
+#include <linux/ctype.h>
 
 #include <asm/gpio.h>
 
@@ -319,6 +321,50 @@
 	return num_found;
 }
 
+int fdtdec_get_alias_seq(const void *blob, const char *base, int offset,
+			 int *seqp)
+{
+	int base_len = strlen(base);
+	const char *find_name;
+	int find_namelen;
+	int prop_offset;
+	int aliases;
+
+	find_name = fdt_get_name(blob, offset, &find_namelen);
+	debug("Looking for '%s' at %d, name %s\n", base, offset, find_name);
+
+	aliases = fdt_path_offset(blob, "/aliases");
+	for (prop_offset = fdt_first_property_offset(blob, aliases);
+	     prop_offset > 0;
+	     prop_offset = fdt_next_property_offset(blob, prop_offset)) {
+		const char *prop;
+		const char *name;
+		const char *slash;
+		const char *p;
+		int len;
+
+		prop = fdt_getprop_by_offset(blob, prop_offset, &name, &len);
+		debug("   - %s, %s\n", name, prop);
+		if (len < find_namelen || *prop != '/' || prop[len - 1] ||
+		    strncmp(name, base, base_len))
+			continue;
+
+		slash = strrchr(prop, '/');
+		if (strcmp(slash + 1, find_name))
+			continue;
+		for (p = name; *p; p++) {
+			if (isdigit(*p)) {
+				*seqp = simple_strtoul(p, NULL, 10);
+				debug("Found seq %d\n", *seqp);
+				return 0;
+			}
+		}
+	}
+
+	debug("Not found\n");
+	return -ENOENT;
+}
+
 int fdtdec_check_fdt(void)
 {
 	/*