mmc: Allow setting slot index via devicetree alias
authorSascha Hauer <s.hauer@pengutronix.de>
Fri, 20 Jun 2014 07:08:16 +0000 (15:08 +0800)
committerNitin Garg <nitin.garg@nxp.com>
Mon, 19 Mar 2018 19:48:47 +0000 (14:48 -0500)
As with gpio, uart and others, allow specifying the name_idx via the
aliases-node in the devicetree.

On embedded devices, there is often a combination of removable (e.g.
SD card) and non-removable mmc devices (e.g. eMMC).
Therefore the name_idx might change depending on
- host of removable device
- removable card present or not

This makes it difficult to hard code the root device, if it is on the
non-removable device. E.g. if SD card is present eMMC will be mmcblk1,
if SD card is not present at boot, eMMC will be mmcblk0.

If the aliases-node is not found, the driver will act as before.

The original patch is from here:
https://www.mail-archive.com/linux-mmc@vger.kernel.org/msg26472.html

The patch requires additional alias_id fix or it won't work.
Because according to function definition the max_idx parameter of idx_alloc
is exclusive, so need add 1 or it will be unable to find the proper idx
within an invalid range.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Signed-off-by: Dong Aisheng <b29396@freescale.com>
(cherry picked from commit 35928d6c6a76a24a16edfa636f4c08293614a1e0)

drivers/mmc/card/block.c
drivers/mmc/core/core.c
drivers/mmc/core/host.c
include/linux/mmc/core.h

index c0300c4..e1c2719 100644 (file)
@@ -2228,7 +2228,13 @@ again:
                return ERR_PTR(-ENOMEM);
 
        spin_lock(&mmc_blk_lock);
-       ret = ida_get_new(&mmc_blk_ida, &devidx);
+       devidx = mmc_get_reserved_index(card->host);
+       if (devidx >= 0)
+               devidx = ida_simple_get(&mmc_blk_ida, devidx, devidx,
+                                       GFP_NOWAIT);
+       if (devidx < 0)
+               ret = ida_get_new_above(&mmc_blk_ida,
+                                       mmc_first_nonreserved_index(), &devidx);
        spin_unlock(&mmc_blk_lock);
 
        if (ret == -EAGAIN)
index 2553d90..4c6dc37 100644 (file)
@@ -16,6 +16,7 @@
 #include <linux/completion.h>
 #include <linux/device.h>
 #include <linux/delay.h>
+#include <linux/of.h>
 #include <linux/pagemap.h>
 #include <linux/err.h>
 #include <linux/leds.h>
@@ -3026,10 +3027,47 @@ void mmc_init_context_info(struct mmc_host *host)
        init_waitqueue_head(&host->context_info.wait);
 }
 
+static int __mmc_max_reserved_idx = -1;
+
+/**
+ * mmc_first_nonreserved_index() - get the first index that is not reserved
+ */
+int mmc_first_nonreserved_index(void)
+{
+       return __mmc_max_reserved_idx + 1;
+}
+
+/**
+ * mmc_get_reserved_index() - get the index reserved for this host
+ *
+ * Return: The index reserved for this host or negative error value if
+ *         no index is reserved for this host
+ */
+int mmc_get_reserved_index(struct mmc_host *host)
+{
+       return of_alias_get_id(host->parent->of_node, "mmc");
+}
+
+static void mmc_of_reserve_idx(void)
+{
+       int max;
+
+       max = of_alias_max_index("mmc");
+       if (max < 0)
+               return;
+
+       __mmc_max_reserved_idx = max;
+
+       pr_debug("MMC: reserving %d slots for of aliases\n",
+                       __mmc_max_reserved_idx + 1);
+}
+
 static int __init mmc_init(void)
 {
        int ret;
 
+       mmc_of_reserve_idx();
+
        ret = mmc_register_bus();
        if (ret)
                return ret;
index 848b345..d3c149d 100644 (file)
@@ -333,6 +333,8 @@ int mmc_of_parse(struct mmc_host *host)
 
 EXPORT_SYMBOL(mmc_of_parse);
 
+int mmc_max_reserved_idx(void);
+
 /**
  *     mmc_alloc_host - initialise the per-host structure.
  *     @extra: sizeof private data structure
@@ -344,6 +346,7 @@ struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
 {
        int err;
        struct mmc_host *host;
+       int alias_id, min_idx, max_idx;
 
        host = kzalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
        if (!host)
@@ -351,6 +354,7 @@ struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
 
        /* scanning will be enabled when we're ready */
        host->rescan_disable = 1;
+       host->parent = dev;
 
 again:
        if (!ida_pre_get(&mmc_host_ida, GFP_KERNEL)) {
@@ -359,7 +363,24 @@ again:
        }
 
        spin_lock(&mmc_host_lock);
-       err = ida_get_new(&mmc_host_ida, &host->index);
+
+       alias_id = mmc_get_reserved_index(host);
+       if (alias_id >= 0) {
+               min_idx = alias_id;
+               max_idx = alias_id + 1;
+       } else {
+               min_idx = mmc_first_nonreserved_index();
+               max_idx = 0;
+       }
+
+       err = ida_get_new_above(&mmc_host_ida, min_idx, &host->index);
+       if (!err) {
+               if (host->index > max_idx) {
+                       ida_remove(&mmc_host_ida, host->index);
+                       err = -ENOSPC;
+               }
+       }
+
        spin_unlock(&mmc_host_lock);
 
        if (err == -EAGAIN) {
@@ -371,7 +392,6 @@ again:
 
        dev_set_name(&host->class_dev, "mmc%d", host->index);
 
-       host->parent = dev;
        host->class_dev.parent = dev;
        host->class_dev.class = &mmc_host_class;
        device_initialize(&host->class_dev);
index 2b953eb..aa3edb7 100644 (file)
@@ -205,6 +205,9 @@ extern int mmc_flush_cache(struct mmc_card *);
 
 extern int mmc_detect_card_removed(struct mmc_host *host);
 
+int mmc_first_nonreserved_index(void);
+int mmc_get_reserved_index(struct mmc_host *host);
+
 /**
  *     mmc_claim_host - exclusively claim a host
  *     @host: mmc host to claim