dm: Allow drivers to be marked 'before relocation'

Driver model currently only operates after relocation is complete. In this
state U-Boot typically has a small amount of memory available. In adding
support for driver model prior to relocation we must try to use as little
memory as possible.

In addition, on some machines the memory has not be inited and/or the CPU
is not running at full speed or the data cache is off. These can reduce
execution performance, so the less initialisation that is done before
relocation the better.

An immediately-obvious improvement is to only initialise drivers which are
actually going to be used before relocation. On many boards the only such
driver is a serial UART, so this provides a very large potential benefit.

Allow drivers to mark themselves as 'pre-reloc' which means that they will
be initialised prior to relocation. This can be done either with a driver
flag or with a 'dm,pre-reloc' device tree property.

To support this, the various dm scanning function now take a 'pre_reloc_only'
parameter which indicates that only drivers marked pre-reloc should be
bound.

Signed-off-by: Simon Glass <sjg@chromium.org>
diff --git a/test/dm/core.c b/test/dm/core.c
index 8c18780..24e0b6b 100644
--- a/test/dm/core.c
+++ b/test/dm/core.c
@@ -25,6 +25,7 @@
 	TEST_INTVAL2		= 3,
 	TEST_INTVAL3		= 6,
 	TEST_INTVAL_MANUAL	= 101112,
+	TEST_INTVAL_PRE_RELOC	= 7,
 };
 
 static const struct dm_test_pdata test_pdata[] = {
@@ -37,6 +38,10 @@
 	.ping_add		= TEST_INTVAL_MANUAL,
 };
 
+static const struct dm_test_pdata test_pdata_pre_reloc = {
+	.ping_add		= TEST_INTVAL_PRE_RELOC,
+};
+
 U_BOOT_DEVICE(dm_test_info1) = {
 	.name = "test_drv",
 	.platdata = &test_pdata[0],
@@ -57,6 +62,11 @@
 	.platdata = &test_pdata_manual,
 };
 
+static struct driver_info driver_info_pre_reloc = {
+	.name = "test_pre_reloc_drv",
+	.platdata = &test_pdata_manual,
+};
+
 /* Test that binding with platdata occurs correctly */
 static int dm_test_autobind(struct dm_test_state *dms)
 {
@@ -71,7 +81,7 @@
 	ut_asserteq(0, list_count_items(&gd->dm_root->child_head));
 	ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]);
 
-	ut_assertok(dm_scan_platdata());
+	ut_assertok(dm_scan_platdata(false));
 
 	/* We should have our test class now at least, plus more children */
 	ut_assert(1 < list_count_items(&gd->uclass_root));
@@ -181,7 +191,7 @@
 
 	memcpy(op_count, dm_testdrv_op_count, sizeof(op_count));
 
-	ut_assertok(device_bind_by_name(dms->root, &driver_info_manual,
+	ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
 					&dev));
 	ut_assert(dev);
 	ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND]
@@ -232,15 +242,15 @@
 	struct udevice *dev, *dev_penultimate, *dev_last, *test_dev;
 	int pingret;
 
-	ut_assertok(device_bind_by_name(dms->root, &driver_info_manual,
+	ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
 					&dev));
 	ut_assert(dev);
 
 	/* Bind two new devices (numbers 4 and 5) */
-	ut_assertok(device_bind_by_name(dms->root, &driver_info_manual,
+	ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
 					&dev_penultimate));
 	ut_assert(dev_penultimate);
-	ut_assertok(device_bind_by_name(dms->root, &driver_info_manual,
+	ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
 					&dev_last));
 	ut_assert(dev_last);
 
@@ -255,7 +265,8 @@
 	ut_assert(dev_last == test_dev);
 
 	/* Add back the original device 3, now in position 5 */
-	ut_assertok(device_bind_by_name(dms->root, &driver_info_manual, &dev));
+	ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
+					&dev));
 	ut_assert(dev);
 
 	/* Try ping */
@@ -375,8 +386,8 @@
 		if (!start.uordblks)
 			puts("Warning: Please add '#define DEBUG' to the top of common/dlmalloc.c\n");
 
-		ut_assertok(dm_scan_platdata());
-		ut_assertok(dm_scan_fdt(gd->fdt_blob));
+		ut_assertok(dm_scan_platdata(false));
+		ut_assertok(dm_scan_fdt(gd->fdt_blob, false));
 
 		/* Scanning the uclass is enough to probe all the devices */
 		for (id = UCLASS_ROOT; id < UCLASS_COUNT; id++) {
@@ -444,8 +455,8 @@
 	for (i = 0; i < count; i++) {
 		struct dm_test_pdata *pdata;
 
-		ut_assertok(device_bind_by_name(parent, &driver_info_manual,
-						&dev));
+		ut_assertok(device_bind_by_name(parent, false,
+						&driver_info_manual, &dev));
 		pdata = calloc(1, sizeof(*pdata));
 		pdata->ping_add = key + i;
 		dev->platdata = pdata;
@@ -542,3 +553,20 @@
 	return 0;
 }
 DM_TEST(dm_test_children, 0);
+
+/* Test that pre-relocation devices work as expected */
+static int dm_test_pre_reloc(struct dm_test_state *dms)
+{
+	struct udevice *dev;
+
+	/* The normal driver should refuse to bind before relocation */
+	ut_asserteq(-EPERM, device_bind_by_name(dms->root, true,
+						&driver_info_manual, &dev));
+
+	/* But this one is marked pre-reloc */
+	ut_assertok(device_bind_by_name(dms->root, true,
+					&driver_info_pre_reloc, &dev));
+
+	return 0;
+}
+DM_TEST(dm_test_pre_reloc, 0);