Skip to contentSkip to conversation

mtd: maps: gpio-addr-flash: Replace custom printk

PATCH Superseded Ricardo Ribalda Delgado 2 participants 14 messages Oct 1, 2018 – Oct 3, 2018

Subsystems (3)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/8 mtd: maps: gpio-addr-flash: Replace custom printk

Use preferred print methods dev_*

---
 drivers/mtd/maps/gpio-addr-flash.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/mtd/maps/gpio-addr-flash.c +3 -7
index 9d9723693217..17be47f72973 100644
@@ -25,11 +25,7 @@
 #include <linux/slab.h>
 #include <linux/types.h>
 
-#define pr_devinit(fmt, args...) \
-	({ static const char __fmt[] = fmt; printk(__fmt, ## args); })
-
 #define DRIVER_NAME "gpio-addr-flash"
-#define PFX DRIVER_NAME ": "
 
 /**
  * struct async_state - keep GPIO flash state
@@ -250,7 +246,7 @@ static int gpio_flash_probe(struct platform_device *pdev)
 	i = 0;
 	do {
 		if (gpio_request(state->gpio_addrs[i], DRIVER_NAME)) {
-			pr_devinit(KERN_ERR PFX "failed to request gpio %d\n",
+			dev_err(&pdev->dev, "failed to request gpio %d\n",
 				state->gpio_addrs[i]);
 			while (i--)
 				gpio_free(state->gpio_addrs[i]);
@@ -260,8 +256,8 @@ static int gpio_flash_probe(struct platform_device *pdev)
 		gpio_direction_output(state->gpio_addrs[i], 0);
 	} while (++i < state->gpio_count);
 
-	pr_devinit(KERN_NOTICE PFX "probing %d-bit flash bus\n",
-		state->map.bankwidth * 8);
+	dev_notice(&pdev->dev, "probing %d-bit flash bus\n",
+		   state->map.bankwidth * 8);
 	state->mtd = do_map_probe(memory->name, &state->map);
 	if (!state->mtd) {
 		for (i = 0; i < state->gpio_count; ++i)
Signature · 1 line
2.19.0
#1 lore raw

PATCH v4 2/8 mtd: maps: gpio-addr-flash: Fix ioremapped size

We should only iomap the area of the chip that is memory mapped. Otherwise we could be mapping devices beyond the memory space or that belong to other devices.

Fixes ebd71e3a4861 ("mtd: maps: gpio-addr-flash: fix warnings and make more portable")
---
 drivers/mtd/maps/gpio-addr-flash.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mtd/maps/gpio-addr-flash.c +1 -1
index 17be47f72973..6de16e81994c 100644
@@ -234,7 +234,7 @@ static int gpio_flash_probe(struct platform_device *pdev)
 	state->map.copy_to    = gf_copy_to;
 	state->map.bankwidth  = pdata->width;
 	state->map.size       = state->win_size * (1 << state->gpio_count);
-	state->map.virt       = ioremap_nocache(memory->start, state->map.size);
+	state->map.virt       = ioremap_nocache(memory->start, state->win_size);
 	if (!state->map.virt)
 		return -ENOMEM;
 
Signature · 1 line
2.19.0
#2 lore raw

PATCH v4 3/8 mtd: maps: gpio-addr-flash: Use devm_* functions

In reply to Ricardo Ribalda Delgado

By using devm functions we can make the code cleaner.

---
 drivers/mtd/maps/gpio-addr-flash.c | 26 ++++++++------------------
 1 file changed, 8 insertions(+), 18 deletions(-)
drivers/mtd/maps/gpio-addr-flash.c +8 -18
index 6de16e81994c..84404dcc7824 100644
@@ -213,7 +213,7 @@ static int gpio_flash_probe(struct platform_device *pdev)
 		return -EINVAL;
 
 	arr_size = sizeof(int) * gpios->end;
-	state = kzalloc(sizeof(*state) + arr_size, GFP_KERNEL);
+	state = devm_kzalloc(&pdev->dev, sizeof(*state) + arr_size, GFP_KERNEL);
 	if (!state)
 		return -ENOMEM;
 
@@ -234,9 +234,9 @@ static int gpio_flash_probe(struct platform_device *pdev)
 	state->map.copy_to    = gf_copy_to;
 	state->map.bankwidth  = pdata->width;
 	state->map.size       = state->win_size * (1 << state->gpio_count);
-	state->map.virt       = ioremap_nocache(memory->start, state->win_size);
-	if (!state->map.virt)
-		return -ENOMEM;
+	state->map.virt	      = devm_ioremap_resource(&pdev->dev, memory);
+	if (IS_ERR(state->map.virt))
+		return PTR_ERR(state->map.virt);
 
 	state->map.phys       = NO_XIP;
 	state->map.map_priv_1 = (unsigned long)state;
@@ -245,12 +245,10 @@ static int gpio_flash_probe(struct platform_device *pdev)
 
 	i = 0;
 	do {
-		if (gpio_request(state->gpio_addrs[i], DRIVER_NAME)) {
+		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]);
-			while (i--)
-				gpio_free(state->gpio_addrs[i]);
-			kfree(state);
 			return -EBUSY;
 		}
 		gpio_direction_output(state->gpio_addrs[i], 0);
@@ -259,12 +257,8 @@ static int gpio_flash_probe(struct platform_device *pdev)
 	dev_notice(&pdev->dev, "probing %d-bit flash bus\n",
 		   state->map.bankwidth * 8);
 	state->mtd = do_map_probe(memory->name, &state->map);
-	if (!state->mtd) {
-		for (i = 0; i < state->gpio_count; ++i)
-			gpio_free(state->gpio_addrs[i]);
-		kfree(state);
+	if (!state->mtd)
 		return -ENXIO;
-	}
 	state->mtd->dev.parent = &pdev->dev;
 
 	mtd_device_parse_register(state->mtd, part_probe_types, NULL,
@@ -276,13 +270,9 @@ static int gpio_flash_probe(struct platform_device *pdev)
 static int gpio_flash_remove(struct platform_device *pdev)
 {
 	struct async_state *state = platform_get_drvdata(pdev);
-	size_t i = 0;
-	do {
-		gpio_free(state->gpio_addrs[i]);
-	} while (++i < state->gpio_count);
+
 	mtd_device_unregister(state->mtd);
 	map_destroy(state->mtd);
-	kfree(state);
 	return 0;
 }
 
Signature · 1 line
2.19.0
#3 lore raw

PATCH v4 4/8 mtd: maps: gpio-addr-flash: Use order insted of size

In reply to Ricardo Ribalda Delgado

By using the order of the window instead of the size, we can replace a lot of expensive division and modulus on the code with simple bit operations.

---
 drivers/mtd/maps/gpio-addr-flash.c | 39 ++++++++++++++++--------------
 1 file changed, 21 insertions(+), 18 deletions(-)
drivers/mtd/maps/gpio-addr-flash.c +21 -18
index 84404dcc7824..89cc8cce161b 100644
@@ -25,6 +25,8 @@
 #include <linux/slab.h>
 #include <linux/types.h>
 
+#define win_mask(x) ((BIT(x)) - 1)
+
 #define DRIVER_NAME "gpio-addr-flash"
 
 /**
@@ -34,7 +36,7 @@
  *	@gpio_count:  number of GPIOs used to address
  *	@gpio_addrs:  array of GPIOs to twiddle
  *	@gpio_values: cached GPIO values
- *	@win_size:    dedicated memory size (if no GPIOs)
+ *	@win_order:   dedicated memory size (if no GPIOs)
  */
 struct async_state {
 	struct mtd_info *mtd;
@@ -42,7 +44,7 @@ struct async_state {
 	size_t gpio_count;
 	unsigned *gpio_addrs;
 	int *gpio_values;
-	unsigned long win_size;
+	unsigned int win_order;
 };
 #define gf_map_info_to_state(mi) ((struct async_state *)(mi)->map_priv_1)
 
@@ -60,7 +62,8 @@ static void gf_set_gpios(struct async_state *state, unsigned long ofs)
 {
 	size_t i = 0;
 	int value;
-	ofs /= state->win_size;
+
+	ofs >>= state->win_order;
 	do {
 		value = ofs & (1 << i);
 		if (state->gpio_values[i] != value) {
@@ -83,7 +86,7 @@ static map_word gf_read(struct map_info *map, unsigned long ofs)
 
 	gf_set_gpios(state, ofs);
 
-	word = readw(map->virt + (ofs % state->win_size));
+	word = readw(map->virt + (ofs & win_mask(state->win_order)));
 	test.x[0] = word;
 	return test;
 }
@@ -105,14 +108,14 @@ static void gf_copy_from(struct map_info *map, void *to, unsigned long from, ssi
 	int this_len;
 
 	while (len) {
-		if ((from % state->win_size) + len > state->win_size)
-			this_len = state->win_size - (from % state->win_size);
-		else
-			this_len = len;
+		this_len = from & win_mask(state->win_order);
+		this_len = BIT(state->win_order) - this_len;
+		this_len = min_t(int, len, this_len);
 
 		gf_set_gpios(state, from);
-		memcpy_fromio(to, map->virt + (from % state->win_size),
-			 this_len);
+		memcpy_fromio(to,
+			      map->virt + (from & win_mask(state->win_order)),
+			      this_len);
 		len -= this_len;
 		from += this_len;
 		to += this_len;
@@ -132,7 +135,7 @@ static void gf_write(struct map_info *map, map_word d1, unsigned long ofs)
 	gf_set_gpios(state, ofs);
 
 	d = d1.x[0];
-	writew(d, map->virt + (ofs % state->win_size));
+	writew(d, map->virt + (ofs & win_mask(state->win_order)));
 }
 
 /**
@@ -152,13 +155,13 @@ static void gf_copy_to(struct map_info *map, unsigned long to,
 	int this_len;
 
 	while (len) {
-		if ((to % state->win_size) + len > state->win_size)
-			this_len = state->win_size - (to % state->win_size);
-		else
-			this_len = len;
+		this_len = to & win_mask(state->win_order);
+		this_len = BIT(state->win_order) - this_len;
+		this_len = min_t(int, len, this_len);
 
 		gf_set_gpios(state, to);
-		memcpy_toio(map->virt + (to % state->win_size), from, len);
+		memcpy_toio(map->virt + (to & win_mask(state->win_order)),
+			    from, len);
 
 		len -= this_len;
 		to += this_len;
@@ -224,7 +227,7 @@ static int gpio_flash_probe(struct platform_device *pdev)
 	state->gpio_count     = gpios->end;
 	state->gpio_addrs     = (void *)(unsigned long)gpios->start;
 	state->gpio_values    = (void *)(state + 1);
-	state->win_size       = resource_size(memory);
+	state->win_order      = get_bitmask_order(resource_size(memory)) - 1;
 	memset(state->gpio_values, 0xff, arr_size);
 
 	state->map.name       = DRIVER_NAME;
@@ -233,7 +236,7 @@ static int gpio_flash_probe(struct platform_device *pdev)
 	state->map.write      = gf_write;
 	state->map.copy_to    = gf_copy_to;
 	state->map.bankwidth  = pdata->width;
-	state->map.size       = state->win_size * (1 << state->gpio_count);
+	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))
 		return PTR_ERR(state->map.virt);
Signature · 1 line
2.19.0
#4 lore raw

PATCH v4 5/8 mtd: maps: gpio-addr-flash: Replace array with an integer

In reply to Ricardo Ribalda Delgado

By replacing the array with an integer we can avoid completely the bit comparison loop if the value has not changed (by far the most common case).

---
 drivers/mtd/maps/gpio-addr-flash.c | 34 +++++++++++++++---------------
 1 file changed, 17 insertions(+), 17 deletions(-)
drivers/mtd/maps/gpio-addr-flash.c +17 -17
index 89cc8cce161b..47b12a6fead4 100644
@@ -43,7 +43,7 @@ struct async_state {
 	struct map_info map;
 	size_t gpio_count;
 	unsigned *gpio_addrs;
-	int *gpio_values;
+	unsigned int gpio_values;
 	unsigned int win_order;
 };
 #define gf_map_info_to_state(mi) ((struct async_state *)(mi)->map_priv_1)
@@ -55,22 +55,25 @@ struct async_state {
  *
  * Rather than call the GPIO framework every time, cache the last-programmed
  * value.  This speeds up sequential accesses (which are by far the most common
- * type).  We rely on the GPIO framework to treat non-zero value as high so
- * that we don't have to normalize the bits.
+ * type).
  */
 static void gf_set_gpios(struct async_state *state, unsigned long ofs)
 {
-	size_t i = 0;
-	int value;
+	int i;
 
 	ofs >>= state->win_order;
-	do {
-		value = ofs & (1 << i);
-		if (state->gpio_values[i] != value) {
-			gpio_set_value(state->gpio_addrs[i], value);
-			state->gpio_values[i] = value;
-		}
-	} while (++i < state->gpio_count);
+
+	if (ofs == state->gpio_values)
+		return;
+
+	for (i = 0; i < state->gpio_count; i++) {
+		if ((ofs & BIT(i)) == (state->gpio_values & BIT(i)))
+			continue;
+
+		gpio_set_value(state->gpio_addrs[i], !!(ofs & BIT(i)));
+	}
+
+	state->gpio_values = ofs;
 }
 
 /**
@@ -202,7 +205,7 @@ static const char * const part_probe_types[] = {
  */
 static int gpio_flash_probe(struct platform_device *pdev)
 {
-	size_t i, arr_size;
+	size_t i;
 	struct physmap_flash_data *pdata;
 	struct resource *memory;
 	struct resource *gpios;
@@ -215,8 +218,7 @@ static int gpio_flash_probe(struct platform_device *pdev)
 	if (!memory || !gpios || !gpios->end)
 		return -EINVAL;
 
-	arr_size = sizeof(int) * gpios->end;
-	state = devm_kzalloc(&pdev->dev, sizeof(*state) + arr_size, GFP_KERNEL);
+	state = devm_kzalloc(&pdev->dev, sizeof(*state), GFP_KERNEL);
 	if (!state)
 		return -ENOMEM;
 
@@ -226,9 +228,7 @@ static int gpio_flash_probe(struct platform_device *pdev)
 	 */
 	state->gpio_count     = gpios->end;
 	state->gpio_addrs     = (void *)(unsigned long)gpios->start;
-	state->gpio_values    = (void *)(state + 1);
 	state->win_order      = get_bitmask_order(resource_size(memory)) - 1;
-	memset(state->gpio_values, 0xff, arr_size);
 
 	state->map.name       = DRIVER_NAME;
 	state->map.read       = gf_read;
Signature · 1 line
2.19.0
#5 lore raw

PATCH v4 6/8 mtd: maps: gpio-addr-flash: Convert to gpiod

In reply to Ricardo Ribalda Delgado

Convert from legacy gpio API to gpiod.

---
 drivers/mtd/maps/gpio-addr-flash.c | 45 ++++++++++++++++++------------
 1 file changed, 27 insertions(+), 18 deletions(-)
drivers/mtd/maps/gpio-addr-flash.c +27 -18
index 47b12a6fead4..c6fe1de33a82 100644
@@ -14,6 +14,7 @@
  */
 
 #include <linux/gpio.h>
+#include <linux/gpio/consumer.h>
 #include <linux/io.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
@@ -33,16 +34,14 @@
  * struct async_state - keep GPIO flash state
  *	@mtd:         MTD state for this mapping
  *	@map:         MTD map state for this flash
- *	@gpio_count:  number of GPIOs used to address
- *	@gpio_addrs:  array of GPIOs to twiddle
+ *	@gpios:       Struct containing the array of GPIO descriptors
  *	@gpio_values: cached GPIO values
  *	@win_order:   dedicated memory size (if no GPIOs)
  */
 struct async_state {
 	struct mtd_info *mtd;
 	struct map_info map;
-	size_t gpio_count;
-	unsigned *gpio_addrs;
+	struct gpio_descs *gpios;
 	unsigned int gpio_values;
 	unsigned int win_order;
 };
@@ -66,11 +65,11 @@ static void gf_set_gpios(struct async_state *state, unsigned long ofs)
 	if (ofs == state->gpio_values)
 		return;
 
-	for (i = 0; i < state->gpio_count; i++) {
+	for (i = 0; i < state->gpios->ndescs; i++) {
 		if ((ofs & BIT(i)) == (state->gpio_values & BIT(i)))
 			continue;
 
-		gpio_set_value(state->gpio_addrs[i], !!(ofs & BIT(i)));
+		gpiod_set_value(state->gpios->desc[i], !!(ofs & BIT(i)));
 	}
 
 	state->gpio_values = ofs;
@@ -222,12 +221,17 @@ static int gpio_flash_probe(struct platform_device *pdev)
 	if (!state)
 		return -ENOMEM;
 
-	/*
-	 * 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     = (void *)(unsigned long)gpios->start;
+	state->gpios = devm_kzalloc(&pdev->dev,
+			sizeof(*state->gpios) +
+			gpios->end * sizeof(state->gpios->desc[0]),
+			GFP_KERNEL);
+	if (!state->gpios)
+		return -ENOMEM;
+	state->gpios->ndescs = gpios->end;
+
+	if (!state->gpios->desc)
+		return -ENOMEM;
+
 	state->win_order      = get_bitmask_order(resource_size(memory)) - 1;
 
 	state->map.name       = DRIVER_NAME;
@@ -236,7 +240,7 @@ static int gpio_flash_probe(struct platform_device *pdev)
 	state->map.write      = gf_write;
 	state->map.copy_to    = gf_copy_to;
 	state->map.bankwidth  = pdata->width;
-	state->map.size       = BIT(state->win_order + state->gpio_count);
+	state->map.size       = BIT(state->win_order + state->gpios->ndescs);
 	state->map.virt	      = devm_ioremap_resource(&pdev->dev, memory);
 	if (IS_ERR(state->map.virt))
 		return PTR_ERR(state->map.virt);
@@ -248,14 +252,19 @@ static int gpio_flash_probe(struct platform_device *pdev)
 
 	i = 0;
 	do {
-		if (devm_gpio_request(&pdev->dev, state->gpio_addrs[i],
-				      DRIVER_NAME)) {
+		unsigned int *gpio_id = (unsigned int *)gpios->start;
+
+		if (devm_gpio_request_one(&pdev->dev, gpio_id[i], GPIOD_OUT_LOW,
+					DRIVER_NAME)) {
 			dev_err(&pdev->dev, "failed to request gpio %d\n",
-				state->gpio_addrs[i]);
+				gpio_id[i]);
 			return -EBUSY;
 		}
-		gpio_direction_output(state->gpio_addrs[i], 0);
-	} while (++i < state->gpio_count);
+
+		state->gpios->desc[i] = gpio_to_desc(gpio_id[i]);
+		if (!state->gpios->desc[i])
+			return -EINVAL;
+	} while (++i < state->gpios->ndescs);
 
 	dev_notice(&pdev->dev, "probing %d-bit flash bus\n",
 		   state->map.bankwidth * 8);
Signature · 1 line
2.19.0
#6 lore raw

Re: [PATCH v4 6/8] mtd: maps: gpio-addr-flash: Convert to gpiod

Hi Ricardo,

Quoted 12 lines · On Mon, 1 Oct 2018 14:43:49 +0200 Ricardo Ribalda Delgado…
drivers/mtd/maps/gpio-addr-flash.c +4 -3
@@ -248,14 +252,19 @@ static int gpio_flash_probe(struct platform_device *pdev)
 
 	i = 0;
 	do {
-		if (devm_gpio_request(&pdev->dev, state->gpio_addrs[i],
-				      DRIVER_NAME)) {
+		unsigned int *gpio_id = (unsigned int *)gpios->start;
+
+		if (devm_gpio_request_one(&pdev->dev, gpio_id[i], GPIOD_OUT_LOW,
+					DRIVER_NAME)) {
 			dev_err(&pdev->dev, "failed to request gpio %d\n",
-				state->gpio_addrs[i]);

drivers/mtd/maps/gpio-addr-flash.c static int gpio_flash_probe(struct platform_device *pdev) · line 267

Quoted 10 lines
drivers/mtd/maps/gpio-addr-flash.c +6 -2
+				gpio_id[i]);
 			return -EBUSY;
 		}
-		gpio_direction_output(state->gpio_addrs[i], 0);
-	} while (++i < state->gpio_count);
+
+		state->gpios->desc[i] = gpio_to_desc(gpio_id[i]);
+		if (!state->gpios->desc[i])
+			return -EINVAL;
+	} while (++i < state->gpios->ndescs);

Actually, I was thinking about using devm_gpiod_get_array() here and defining a gpio lookup table in the board file registering the device. This way, adding support for DT based parsing is transparent.

Regards,

Boris

#7 lore raw

Re: [PATCH v4 6/8] mtd: maps: gpio-addr-flash: Convert to gpiod

Quoted 23 lines · On Wed, 3 Oct 2018 17:11:14 +0200 Boris Brezillon…

Hi Ricardo,

Quoted 18 lines · On Mon, 1 Oct 2018 14:43:49 +0200 Ricardo Ribalda Delgado…
drivers/mtd/maps/gpio-addr-flash.c +6 -5
@@ -248,14 +252,19 @@ static int gpio_flash_probe(struct platform_device *pdev)
 
 	i = 0;
 	do {
-		if (devm_gpio_request(&pdev->dev, state->gpio_addrs[i],
-				      DRIVER_NAME)) {
+		unsigned int *gpio_id = (unsigned int *)gpios->start;
+
+		if (devm_gpio_request_one(&pdev->dev, gpio_id[i], GPIOD_OUT_LOW,
+					DRIVER_NAME)) {
 			dev_err(&pdev->dev, "failed to request gpio %d\n",
-				state->gpio_addrs[i]);
+				gpio_id[i]);
 			return -EBUSY;
 		}
-		gpio_direction_output(state->gpio_addrs[i], 0);
-	} while (++i < state->gpio_count);
+
Quoted 8 lines

drivers/mtd/maps/gpio-addr-flash.c static int gpio_flash_probe(struct platform_device *pdev) · line 267

Quoted 4 lines
drivers/mtd/maps/gpio-addr-flash.c +4 -0
+		state->gpios->desc[i] = gpio_to_desc(gpio_id[i]);
+		if (!state->gpios->desc[i])
+			return -EINVAL;
+	} while (++i < state->gpios->ndescs);  

Actually, I was thinking about using devm_gpiod_get_array() here and defining a gpio lookup table in the board file registering the device. This way, adding support for DT based parsing is transparent.

It's actually easier than I thought since no one is registering such a device, so all you have to do is call devm_gpiod_get_array() and have a struct gpio_descs pointer in struct async_state.

#8 lore raw

Re: [PATCH v4 6/8] mtd: maps: gpio-addr-flash: Convert to gpiod

Hi Boris

Quoted 36 lines · Boris Brezillon
Quoted 23 lines · On Wed, 3 Oct 2018 17:11:14 +0200 Boris Brezillon…

Hi Ricardo,

Quoted 18 lines · On Mon, 1 Oct 2018 14:43:49 +0200 Ricardo Ribalda Delgado…
drivers/mtd/maps/gpio-addr-flash.c +6 -5
@@ -248,14 +252,19 @@ static int gpio_flash_probe(struct platform_device *pdev)
 
    i = 0;
    do {
-           if (devm_gpio_request(&pdev->dev, state->gpio_addrs[i],
-                                 DRIVER_NAME)) {
+           unsigned int *gpio_id = (unsigned int *)gpios->start;
+
+           if (devm_gpio_request_one(&pdev->dev, gpio_id[i], GPIOD_OUT_LOW,
+                                   DRIVER_NAME)) {
                    dev_err(&pdev->dev, "failed to request gpio %d\n",
-                           state->gpio_addrs[i]);
+                           gpio_id[i]);
                    return -EBUSY;
            }
-           gpio_direction_output(state->gpio_addrs[i], 0);
-   } while (++i < state->gpio_count);
+
Quoted 8 lines

drivers/mtd/maps/gpio-addr-flash.c static int gpio_flash_probe(struct platform_device *pdev) · line 267

Quoted 4 lines
drivers/mtd/maps/gpio-addr-flash.c +4 -0
+           state->gpios->desc[i] = gpio_to_desc(gpio_id[i]);
+           if (!state->gpios->desc[i])
+                   return -EINVAL;
+   } while (++i < state->gpios->ndescs);

Actually, I was thinking about using devm_gpiod_get_array() here and defining a gpio lookup table in the board file registering the device. This way, adding support for DT based parsing is transparent.

Quoted 3 lines

It's actually easier than I thought since no one is registering such a device, so all you have to do is call devm_gpiod_get_array() and have a struct gpio_descs pointer in struct async_state.

That is what I do in patch 8/8 for DT based parsing.

I am am doing the gpio_to_desc and other to maintain compatibility with old platform board files (in and out tree).

I think is important to maintain that compatibility, but you decide ;)

Cheers

Signature · 1 line
Ricardo Ribalda
#9 lore raw

Re: [PATCH v4 6/8] mtd: maps: gpio-addr-flash: Convert to gpiod

Hi Ricardo,

Quoted 39 lines · On Wed, 3 Oct 2018 17:56:03 +0200 Ricardo Ribalda Delgado…

Hi Boris

Quoted 36 lines · Boris Brezillon
Quoted 31 lines · On Wed, 3 Oct 2018 17:11:14 +0200 Boris Brezillon…

Hi Ricardo,

Quoted 12 lines · On Mon, 1 Oct 2018 14:43:49 +0200 Ricardo Ribalda Delgado…
drivers/mtd/maps/gpio-addr-flash.c +4 -3
@@ -248,14 +252,19 @@ static int gpio_flash_probe(struct platform_device *pdev)
 
    i = 0;
    do {
-           if (devm_gpio_request(&pdev->dev, state->gpio_addrs[i],
-                                 DRIVER_NAME)) {
+           unsigned int *gpio_id = (unsigned int *)gpios->start;
+
+           if (devm_gpio_request_one(&pdev->dev, gpio_id[i], GPIOD_OUT_LOW,
+                                   DRIVER_NAME)) {
                    dev_err(&pdev->dev, "failed to request gpio %d\n",
-                           state->gpio_addrs[i]);

drivers/mtd/maps/gpio-addr-flash.c static int gpio_flash_probe(struct platform_device *pdev) · line 267

Quoted 10 lines
drivers/mtd/maps/gpio-addr-flash.c +6 -2
+                           gpio_id[i]);
                    return -EBUSY;
            }
-           gpio_direction_output(state->gpio_addrs[i], 0);
-   } while (++i < state->gpio_count);
+
+           state->gpios->desc[i] = gpio_to_desc(gpio_id[i]);
+           if (!state->gpios->desc[i])
+                   return -EINVAL;
+   } while (++i < state->gpios->ndescs);  

Actually, I was thinking about using devm_gpiod_get_array() here and defining a gpio lookup table in the board file registering the device. This way, adding support for DT based parsing is transparent.

Quoted 5 lines
Quoted 3 lines

It's actually easier than I thought since no one is registering such a device, so all you have to do is call devm_gpiod_get_array() and have a struct gpio_descs pointer in struct async_state.

That is what I do in patch 8/8 for DT based parsing.

Well, yes, except you do it differently for the !DT case, while I'm suggesting to use the same patch for both DT and !DT.

Quoted 3 lines

I am am doing the gpio_to_desc and other to maintain compatibility with old platform board files (in and out tree).

I do care about keeping in-tree users functional (which is why I suggested to declare gpio lookup tables in the first place), but keeping out-of-tree code functional is on a best-effort basis. If it breaks because we have to rework an internal API then that's not our fault. Actually, that's one of the reason we push people to upstream their code => their maintenance burden is greatly reduced once the code has reached mainline.

Quoted 2 lines

I think is important to maintain that compatibility, but you decide ;)

And my decision is, keep the code as simple as possible even if it breaks out of tree users.

Regards,

Boris

#10 lore raw

PATCH v4 7/8 dt-binding: mtd: Document gpio-addr-flash

In reply to Ricardo Ribalda Delgado

Add documentation for gpio-addr-flash. This binding allow creating flash devices that are paged using GPIOs.

---
 .../bindings/mtd/gpio-addr-flash.txt          | 54 +++++++++++++++++++
 1 file changed, 54 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/mtd/gpio-addr-flash.txt
Documentation/devicetree/bindings/mtd/gpio-addr-flash.txt new file +54 -0
new file mode 100644
index 000000000000..5006a26e1753
@@ -0,0 +1,54 @@
+Memory Mapped flash with some address lines addressed using GPIOs
+
+Handle the case where a flash device is mostly addressed using physical
+line and supplemented by GPIOs.  This way you can hook up say a 8MiB flash
+to a 2MiB memory range and use the GPIOs to select a particular range.
+
+ - compatible : "cfi-gpio-addr-flash"
+ - reg : Address range of the mtd chip that is memory mapped, this is,
+   on the previous example 2MiB.
+ - bank-width : Width (in bytes) of the bank.  Equal to the
+   device width times the number of interleaved chips.
+ - gpios: List of GPIO specifiers that will be used to address the MSBs address
+   lines. The order goes from LSB to MSB.
+ - probe-type : (optional) "cfi_probe", "jedec_probe". How the mtd chip
+   is going to be probed. If omitted, assumed to be equal to "cfi_probe".
+ - #address-cells, #size-cells : Must be present if the device has
+   sub-nodes representing partitions (see below).  In this case
+   both #address-cells and #size-cells must be equal to 1.
+
+The device tree may optionally contain sub-nodes describing partitions of the
+address space. Check partition.txt for more details.
+
+Example:
+
+	flash@300000 {
+		#address-cells = <1>;
+		#size-cells = <1>;
+		compatible = "cfi-gpio-addr-flash";
+		bank-width = <2>;
+		reg = < 0x00300000 0x00200000 >;
+		gpios = <&gpio_0 3 0>, <&gpio_0 4 0>;
+		partitions {
+			compatible = "fixed-partitions";
+			#address-cells = <1>;
+			#size-cells = <1>;
+
+			partition@0 {
+				reg = < 0x0 0x200000 >;
+				label = "Golden Bitstream";
+			};
+			partition@200000 {
+				reg = < 0x200000 0x200000 >;
+				label = "User Bitstream";
+			};
+			partition@400000 {
+				reg = < 0x400000 0x200000 >;
+				label = "V4L Controls";
+			};
+			partition@600000 {
+				reg = < 0x600000 0x200000 >;
+				label = "Production Data";
+			};
+		}
+	} ;
Signature · 1 line
2.19.0
#11 lore raw

PATCH v4 8/8 mtd: maps: gpio-addr-flash: Add support for device-tree devices

In reply to Ricardo Ribalda Delgado

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.

---
 drivers/mtd/maps/gpio-addr-flash.c | 184 ++++++++++++++++++++++-------
 1 file changed, 144 insertions(+), 40 deletions(-)
drivers/mtd/maps/gpio-addr-flash.c +144 -40
index c6fe1de33a82..afa9dce14f68 100644
@@ -7,6 +7,7 @@
  *
  * Copyright © 2000 Nicolas Pitre <[email protected]>
  * Copyright © 2005-2009 Analog Devices Inc.
+ * Copyright © 2018 Ricardo Ribalda <[email protected]>
  *
  * Enter bugs at http://blackfin.uclinux.org/
  *
@@ -171,8 +172,120 @@ 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->gpios = devm_gpiod_get_array(&pdev->dev, NULL,
+						    GPIOD_OUT_LOW);
+		if (IS_ERR(state->gpios)) {
+			dev_err(&pdev->dev, "Error getting gpio array %ld\n",
+				   PTR_ERR(state->gpios));
+			return PTR_ERR(state->gpios);
+		}
+		return 0;
+	}
+
+	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->gpios = devm_kzalloc(&pdev->dev,
+				    sizeof(*state->gpios) +
+				    gpios->end * sizeof(state->gpios->desc[0]),
+				    GFP_KERNEL);
+	if (!state->gpios)
+		return -ENOMEM;
+
+	for (i = 0; i < gpios->end ; i++) {
+		unsigned int *gpio_id = (unsigned int *)gpios->start;
+
+		if (devm_gpio_request_one(&pdev->dev, gpio_id[i], GPIOD_OUT_LOW,
+					DRIVER_NAME)) {
+			dev_err(&pdev->dev, "failed to request gpio %d\n",
+				gpio_id[i]);
+			return -EBUSY;
+		}
+
+		state->gpios->desc[i] = gpio_to_desc(gpio_id[i]);
+		if (!state->gpios->desc[i])
+			return -EINVAL;
+	}
+
+	return 0;
+}
+
 
 /**
  * gpio_flash_probe() - setup a mapping for a GPIO assisted flash
@@ -204,42 +317,42 @@ 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);
 
-	state->gpios = devm_kzalloc(&pdev->dev,
-			sizeof(*state->gpios) +
-			gpios->end * sizeof(state->gpios->desc[0]),
-			GFP_KERNEL);
-	if (!state->gpios)
-		return -ENOMEM;
-	state->gpios->ndescs = gpios->end;
-
-	if (!state->gpios->desc)
-		return -ENOMEM;
+	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->gpios->ndescs);
 	state->map.virt	      = devm_ioremap_resource(&pdev->dev, memory);
 	if (IS_ERR(state->map.virt))
@@ -248,33 +361,15 @@ static int gpio_flash_probe(struct platform_device *pdev)
 	state->map.phys       = NO_XIP;
 	state->map.map_priv_1 = (unsigned long)state;
 
-	platform_set_drvdata(pdev, state);
-
-	i = 0;
-	do {
-		unsigned int *gpio_id = (unsigned int *)gpios->start;
-
-		if (devm_gpio_request_one(&pdev->dev, gpio_id[i], GPIOD_OUT_LOW,
-					DRIVER_NAME)) {
-			dev_err(&pdev->dev, "failed to request gpio %d\n",
-				gpio_id[i]);
-			return -EBUSY;
-		}
-
-		state->gpios->desc[i] = gpio_to_desc(gpio_id[i]);
-		if (!state->gpios->desc[i])
-			return -EINVAL;
-	} while (++i < state->gpios->ndescs);
-
 	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;
 }
@@ -288,11 +383,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,
 	},
 };
 
Signature · 1 line
2.19.0
#12 lore raw

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

drivers/mtd/maps/gpio-addr-flash.c static void gf_copy_to(struct map_info *map, unsigned long to, · line 248

Quoted 12 lines · On Mon, 1 Oct 2018 14:43:51 +0200 Ricardo Ribalda Delgado…
drivers/mtd/maps/gpio-addr-flash.c +12 -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->gpios = devm_gpiod_get_array(&pdev->dev, NULL,
+						    GPIOD_OUT_LOW);

Did you consider using gpiod_set_array_value() to update the gpio vals instead of having your own update loop (the for loop in the gf_set_gpios() func)?

#13 lore raw

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

Hi Boris

Quoted 4 lines · Boris Brezillon

On Mon, 1 Oct 2018 14:43:51 +0200
Ricardo Ribalda Delgado <[email protected]> wrote:

Quoted 16 lines

drivers/mtd/maps/gpio-addr-flash.c static void gf_copy_to(struct map_info *map, unsigned long to, · line 248

Quoted 12 lines
drivers/mtd/maps/gpio-addr-flash.c +12 -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->gpios = devm_gpiod_get_array(&pdev->dev, NULL,
+                                                 GPIOD_OUT_LOW);

Did you consider using gpiod_set_array_value() to update the gpio vals instead of having your own update loop (the for loop in the gf_set_gpios() func)?

Yes briefly but there are some cons:

- I need to allocate the value_array
- I want to relay on my cached old values, so I still have to loop

Signature · 1 line
Ricardo Ribalda