M. Feser KBS GmbH
Date: 15.01.2019

This patch modifies the MMC driver to use an index from the devicetree

Modifications:
porting to Linux 4.9
additional warning added

Based on the following patch from Douglas Anderson (28.04.2016):
mmc: read mmc alias from device tree
-------------------------------------------------------------------------
diff -ruPN a/arch/arm/boot/dts/am33xx.dtsi b/arch/arm/boot/dts/am33xx.dtsi
--- a/arch/arm/boot/dts/am33xx.dtsi	2019-01-09 16:16:45.000000000 +0100
+++ b/arch/arm/boot/dts/am33xx.dtsi	2019-01-15 11:39:12.147534777 +0100
@@ -36,6 +36,9 @@
 		phy1 = &usb1_phy;
 		ethernet0 = &cpsw_emac0;
 		ethernet1 = &cpsw_emac1;
+		mmc0 = &mmc1;
+		mmc1 = &mmc2;
+		mmc2 = &mmc3;
 	};
 
 	cpus {
diff -ruPN a/drivers/mmc/core/host.c b/drivers/mmc/core/host.c
--- a/drivers/mmc/core/host.c	2019-01-09 16:16:45.000000000 +0100
+++ b/drivers/mmc/core/host.c	2019-01-15 12:00:25.939534777 +0100
@@ -342,6 +342,7 @@
  */
 struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
 {
+	int of_id = -1;
 	int err;
 	struct mmc_host *host;
 
@@ -351,6 +352,9 @@
 
 	/* scanning will be enabled when we're ready */
 	host->rescan_disable = 1;
+	
+	if (dev->of_node)
+		of_id = of_alias_get_id(dev->of_node, "mmc");
 
 again:
 	if (!ida_pre_get(&mmc_host_ida, GFP_KERNEL)) {
@@ -359,7 +363,10 @@
 	}
 
 	spin_lock(&mmc_host_lock);
-	err = ida_get_new(&mmc_host_ida, &host->index);
+	if (of_id != -1)
+		err = ida_get_new_above(&mmc_host_ida, of_id, &host->index);
+	else
+		err = ida_get_new(&mmc_host_ida, &host->index);
 	spin_unlock(&mmc_host_lock);
 
 	if (err == -EAGAIN) {
@@ -369,6 +376,12 @@
 		return NULL;
 	}
 
+	if (of_id != -1) {
+		if (host->index != of_id)
+			dev_warn(dev, "alias ID from DT not available (%d)\n", of_id);
+	} else {
+		dev_warn(dev, "no alias ID found in DT\n");
+	}
 	dev_set_name(&host->class_dev, "mmc%d", host->index);
 
 	host->parent = dev;
