From 314bf3d4ba1effbda9199068037217691ce1ee2d Mon Sep 17 00:00:00 2001 From: Fan Bin Date: Tue, 3 Dec 2019 15:26:22 +0800 Subject: [PATCH] MMC: Use the device ID implied from device tree Signed-off-by: Fan Bin --- drivers/mmc/core/core.c | 44 +++++++++++++++++++++++++++++++++++++++++ drivers/mmc/core/core.h | 5 +++++ drivers/mmc/core/host.c | 17 ++++++++++++++++ drivers/of/base.c | 32 ++++++++++++++++++++++++++++++ include/linux/of.h | 10 ++++++++++ 5 files changed, 108 insertions(+) diff --git a/drivers/mmc/core/core.c b/drivers/mmc/core/core.c index 29bfff2ed..a2d573a26 100644 --- a/drivers/mmc/core/core.c +++ b/drivers/mmc/core/core.c @@ -58,6 +58,10 @@ static const unsigned freqs[] = { 400000, 300000, 200000, 100000 }; +#ifdef CONFIG_ARCH_MXC +static int __mmc_max_reserved_idx = -1; +#endif + /* * Enabling software CRCs on the data blocks can be a significant (30%) * performance cost, and for other reasons may not always be desired. @@ -2810,10 +2814,50 @@ void mmc_init_context_info(struct mmc_host *host) init_waitqueue_head(&host->context_info.wait); } +#ifdef CONFIG_ARCH_MXC +/* + * mmc_first_nonreserved_index() - get the first index that + * is not reserved + */ +int mmc_first_nonreserved_index(void) +{ + return __mmc_max_reserved_idx + 1; +} +EXPORT_SYMBOL(mmc_first_nonreserved_index); + +/* + * 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"); +} +EXPORT_SYMBOL(mmc_get_reserved_index); + +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); +} +#endif + static int __init mmc_init(void) { int ret; +#ifdef CONFIG_ARCH_MXC + mmc_of_reserve_idx(); +#endif + ret = mmc_register_bus(); if (ret) return ret; diff --git a/drivers/mmc/core/core.h b/drivers/mmc/core/core.h index ca861091a..3c3fc0f25 100644 --- a/drivers/mmc/core/core.h +++ b/drivers/mmc/core/core.h @@ -80,6 +80,11 @@ int mmc_attach_mmc(struct mmc_host *host); int mmc_attach_sd(struct mmc_host *host); int mmc_attach_sdio(struct mmc_host *host); +#ifdef CONFIG_ARCH_MXC +int mmc_first_nonreserved_index(void); +int mmc_get_reserved_index(struct mmc_host *host); +#endif + /* Module parameters */ extern bool use_spi_crc; diff --git a/drivers/mmc/core/host.c b/drivers/mmc/core/host.c index ad88deb2e..bfdbc6987 100644 --- a/drivers/mmc/core/host.c +++ b/drivers/mmc/core/host.c @@ -349,6 +349,9 @@ EXPORT_SYMBOL(mmc_of_parse); struct mmc_host *mmc_alloc_host(int extra, struct device *dev) { int err; +#ifdef CONFIG_ARCH_MXC + int alias_id; +#endif struct mmc_host *host; host = kzalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL); @@ -357,8 +360,20 @@ struct mmc_host *mmc_alloc_host(int extra, struct device *dev) /* scanning will be enabled when we're ready */ host->rescan_disable = 1; +#ifdef CONFIG_ARCH_MXC + host->parent = dev; + alias_id = mmc_get_reserved_index(host); + if (alias_id >= 0) + err = ida_simple_get(&mmc_host_ida, alias_id, + alias_id + 1, GFP_KERNEL); + else + err = ida_simple_get(&mmc_host_ida, + mmc_first_nonreserved_index(), + 0, GFP_KERNEL); +#else err = ida_simple_get(&mmc_host_ida, 0, 0, GFP_KERNEL); +#endif if (err < 0) { kfree(host); return NULL; @@ -368,7 +383,9 @@ struct mmc_host *mmc_alloc_host(int extra, struct device *dev) dev_set_name(&host->class_dev, "mmc%d", host->index); +#if !defined(CONFIG_ARCH_MXC) host->parent = dev; +#endif host->class_dev.parent = dev; host->class_dev.class = &mmc_host_class; device_initialize(&host->class_dev); diff --git a/drivers/of/base.c b/drivers/of/base.c index ce8a6e0c9..f8417a430 100644 --- a/drivers/of/base.c +++ b/drivers/of/base.c @@ -1666,6 +1666,38 @@ static void of_alias_add(struct alias_prop *ap, struct device_node *np, ap->alias, ap->stem, ap->id, np); } +#ifdef CONFIG_ARCH_MXC +/* + * of_alias_max_index() - get the maximum index for a given alias stem + * @stem: The alias stem for which the maximum index is searched for + * + * Given an alias stem (the alias without the number) this function + * returns the maximum number for which an alias exists. + * + * Return: The maximum existing alias index or -ENODEV if no alias + * exists for this stem. + */ +int of_alias_max_index(const char *stem) +{ + struct alias_prop *app; + int max = -ENODEV; + + mutex_lock(&of_mutex); + + list_for_each_entry(app, &aliases_lookup, link) { + if (strcmp(app->stem, stem)) + continue; + if (app->id > max) + max = app->id; + } + + mutex_unlock(&of_mutex); + + return max; +} +EXPORT_SYMBOL_GPL(of_alias_max_index); +#endif + /** * of_alias_scan - Scan all properties of the 'aliases' node * diff --git a/include/linux/of.h b/include/linux/of.h index 70b7dacf9..239b99737 100644 --- a/include/linux/of.h +++ b/include/linux/of.h @@ -384,6 +384,9 @@ extern int of_phandle_iterator_args(struct of_phandle_iterator *it, extern void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align)); extern int of_alias_get_id(struct device_node *np, const char *stem); extern int of_alias_get_highest_id(const char *stem); +#ifdef CONFIG_ARCH_MXC +extern int of_alias_max_index(const char *stem); +#endif extern int of_machine_is_compatible(const char *compat); @@ -826,6 +829,13 @@ static inline int of_alias_get_highest_id(const char *stem) return -ENOSYS; } +#ifdef CONFIG_ARCH_MXC +static inline int of_alias_max_index(const char *stem) +{ + return -ENODEV; +} +#endif + static inline int of_machine_is_compatible(const char *compat) { return 0;