[PATCH v2 7/8] mtd: maps: gpio-addr-flash: Add support for device-tree devices

From: Ricardo Ribalda Delgado
Date: Wed Sep 05 2018 - 10:37:14 EST


Allow creating gpio-addr-flash via device-tree and not just via platform
data.

The gpio probing has been moved to a different function allowing
deferred probing if they are not ready.

Option parsing has been also moved to separated functions.

Signed-off-by: Ricardo Ribalda Delgado <ricardo.ribalda@xxxxxxxxx>
---
Changelog v2:

>From kbuild:
- Fix warnings

drivers/mtd/maps/gpio-addr-flash.c | 186 +++++++++++++++++++++++------
1 file changed, 147 insertions(+), 39 deletions(-)

diff --git a/drivers/mtd/maps/gpio-addr-flash.c b/drivers/mtd/maps/gpio-addr-flash.c
index 9455a8448064..fea1e3ae3745 100644
--- a/drivers/mtd/maps/gpio-addr-flash.c
+++ b/drivers/mtd/maps/gpio-addr-flash.c
@@ -7,6 +7,7 @@
*
* Copyright  2000 Nicolas Pitre <nico@xxxxxxx>
* Copyright  2005-2009 Analog Devices Inc.
+ * Copyright  2018 Ricardo Ribalda <ricardo.ribalda@xxxxxxxxx>
*
* Enter bugs at http://blackfin.uclinux.org/
*
@@ -24,6 +25,7 @@
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/types.h>
+#include <linux/of_gpio.h>

#define win_mask(x) ((BIT(x)) - 1)

@@ -172,8 +174,121 @@ static void gf_copy_to(struct map_info *map, unsigned long to,
}
}

-static const char * const part_probe_types[] = {
- "cmdlinepart", "RedBoot", NULL };
+static int gf_bankwidth(struct platform_device *pdev)
+{
+ struct device_node *dn;
+ int ret;
+ u32 bankwidth;
+
+ dn = pdev->dev.of_node;
+ if (!dn) {
+ struct physmap_flash_data *pdata;
+
+ pdata = dev_get_platdata(&pdev->dev);
+ return pdata->width;
+ }
+
+ ret = of_property_read_u32(dn, "bank-width", &bankwidth);
+ if (ret) {
+ dev_err(&pdev->dev, "failed to get bank-width\n");
+ return -EINVAL;
+ }
+
+ return bankwidth;
+}
+
+static const char *gf_probe_type(struct platform_device *pdev)
+{
+ struct device_node *dn;
+ struct resource *memory;
+ const char *of_probe;
+
+ dn = pdev->dev.of_node;
+ if (!dn) {
+ memory = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+ return memory->name;
+ }
+
+ of_probe = of_get_property(dn, "probe-type", NULL);
+ if (of_probe)
+ return of_probe;
+
+ return "cfi_probe";
+}
+
+static void gf_device_parse_register(struct platform_device *pdev,
+ struct async_state *state)
+{
+ static const char * const part_probe_types[] = {
+ "cmdlinepart", "RedBoot", "ofpart", "ofoldpart", NULL };
+ struct device_node *dn;
+
+ dn = pdev->dev.of_node;
+ if (!dn) {
+ struct physmap_flash_data *pdata;
+
+ pdata = dev_get_platdata(&pdev->dev);
+ mtd_device_parse_register(state->mtd, part_probe_types, NULL,
+ pdata->parts, pdata->nr_parts);
+ return;
+ }
+
+ mtd_device_parse_register(state->mtd, part_probe_types, NULL, NULL, 0);
+}
+
+static int gpio_flash_probe_gpios(struct platform_device *pdev,
+ struct async_state *state)
+{
+ struct physmap_flash_data *pdata;
+ struct device_node *dn;
+ struct resource *gpios = NULL;
+ int i;
+
+ dn = pdev->dev.of_node;
+ if (dn) {
+ state->gpio_count = of_gpio_count(dn);
+ } else {
+ pdata = dev_get_platdata(&pdev->dev);
+ if (!pdata)
+ return -EINVAL;
+ gpios = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
+ if (IS_ERR(gpios) || !gpios->end)
+ return -EINVAL;
+ state->gpio_count = gpios->end;
+ }
+
+ state->gpio_addrs = devm_kzalloc(&pdev->dev,
+ sizeof(state->gpio_addrs[0])
+ * state->gpio_count,
+ GFP_KERNEL);
+ if (!state->gpio_addrs)
+ return -ENOMEM;
+
+ for (i = 0; i < state->gpio_count; i++) {
+ long gpio;
+ int ret;
+
+ if (dn)
+ gpio = of_get_gpio(dn, i);
+ else
+ gpio = ((unsigned long *)gpios->start)[i];
+
+ if (gpio < 0)
+ return gpio;
+
+ ret = devm_gpio_request(&pdev->dev, gpio, DRIVER_NAME);
+ if (ret < 0) {
+ dev_err(&pdev->dev, "failed to request gpio %ld\n",
+ gpio);
+ return ret;
+ }
+ state->gpio_addrs[i] = gpio;
+ gpio_direction_output(state->gpio_addrs[i], 0);
+ }
+
+ return 0;
+}
+

/**
* gpio_flash_probe() - setup a mapping for a GPIO assisted flash
@@ -205,74 +320,58 @@ static const char * const part_probe_types[] = {
*/
static int gpio_flash_probe(struct platform_device *pdev)
{
- size_t i;
- struct physmap_flash_data *pdata;
struct resource *memory;
- struct resource *gpios;
struct async_state *state;
+ int ret;

- pdata = dev_get_platdata(&pdev->dev);
memory = platform_get_resource(pdev, IORESOURCE_MEM, 0);
- gpios = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
-
- if (!memory || !gpios || !gpios->end)
+ if (!memory)
return -EINVAL;

+ if (!is_power_of_2(resource_size(memory))) {
+ dev_err(&pdev->dev, "Window size must be aligned\n");
+ return -EIO;
+ }
+
state = devm_kzalloc(&pdev->dev, sizeof(*state), GFP_KERNEL);
if (!state)
return -ENOMEM;
+ platform_set_drvdata(pdev, state);

- /*
- * We cast start/end to known types in the boards file, so cast
- * away their pointer types here to the known types (gpios->xxx).
- */
- state->gpio_count = gpios->end;
- state->gpio_addrs = devm_kzalloc(&pdev->dev,
- sizeof(state->gpio_addrs[0]) *
- gpios->end,
- GFP_KERNEL);
- if (!state->gpio_addrs)
- return -ENOMEM;
- state->win_order = get_bitmask_order(resource_size(memory)) - 1;
+ ret = gpio_flash_probe_gpios(pdev, state);
+ if (ret < 0)
+ return ret;

+ state->win_order = get_bitmask_order(resource_size(memory)) - 1;
state->map.name = DRIVER_NAME;
state->map.read = gf_read;
state->map.copy_from = gf_copy_from;
state->map.write = gf_write;
state->map.copy_to = gf_copy_to;
- state->map.bankwidth = pdata->width;
+
+ ret = gf_bankwidth(pdev);
+ if (ret < 0)
+ return ret;
+ state->map.bankwidth = ret;
+
state->map.size = BIT(state->win_order + state->gpio_count);
state->map.virt = devm_ioremap_resource(&pdev->dev, memory);
if (IS_ERR(state->map.virt)) {
dev_err(&pdev->dev, "failed to map memory\n");
return PTR_ERR(state->map.virt);
}
-
state->map.phys = NO_XIP;
state->map.map_priv_1 = (unsigned long)state;

- platform_set_drvdata(pdev, state);
-
- i = 0;
- do {
- if (devm_gpio_request(&pdev->dev, state->gpio_addrs[i],
- DRIVER_NAME)) {
- dev_err(&pdev->dev, "failed to request gpio %d\n",
- state->gpio_addrs[i]);
- return -EBUSY;
- }
- gpio_direction_output(state->gpio_addrs[i], 0);
- } while (++i < state->gpio_count);
-
dev_notice(&pdev->dev, "probing %d-bit flash bus\n",
state->map.bankwidth * 8);
- state->mtd = do_map_probe(memory->name, &state->map);
+ state->mtd = do_map_probe(gf_probe_type(pdev), &state->map);
if (!state->mtd)
return -ENXIO;
state->mtd->dev.parent = &pdev->dev;
+ mtd_set_of_node(state->mtd, pdev->dev.of_node);

- mtd_device_parse_register(state->mtd, part_probe_types, NULL,
- pdata->parts, pdata->nr_parts);
+ gf_device_parse_register(pdev, state);

return 0;
}
@@ -286,11 +385,20 @@ static int gpio_flash_remove(struct platform_device *pdev)
return 0;
}

+static const struct of_device_id gpio_flash_match[] = {
+ {
+ .compatible = "cfi-gpio-addr-flash",
+ },
+ {},
+};
+MODULE_DEVICE_TABLE(of, gpio_flash_match);
+
static struct platform_driver gpio_flash_driver = {
.probe = gpio_flash_probe,
.remove = gpio_flash_remove,
.driver = {
.name = DRIVER_NAME,
+ .of_match_table = gpio_flash_match,
},
};

--
2.18.0