[U-Boot] [PATCH v2 1/4] regmap: clean up regmap allocation

Masahiro Yamada yamada.masahiro at socionext.com
Thu Apr 19 03:14:01 UTC 2018


Putting zero length array at the end of struct is a common technique
to embed arbitrary length of members.  There is no good reason to let
regmap_alloc_count() branch by "if (count <= 1)".

As far as I understood the code, regmap->base is an alias of
regmap->ranges[0].start, but it is not helpful but make the code
just ugly.

Rename regmap_alloc_count() to regmap_alloc() because the _count
suffix seems pointless.

Signed-off-by: Masahiro Yamada <yamada.masahiro at socionext.com>
---

Changes in v2:
  - Fix up test and sti drivers

 drivers/core/regmap.c            | 31 +++++++++----------------------
 drivers/phy/sti_usb_phy.c        |  4 ++--
 drivers/pinctrl/pinctrl-sti.c    |  4 ++--
 drivers/reset/sti-reset.c        |  2 +-
 drivers/sysreset/sysreset_sti.c  |  2 +-
 drivers/usb/host/dwc3-sti-glue.c |  2 +-
 include/regmap.h                 |  7 ++-----
 test/dm/regmap.c                 | 13 +++++--------
 8 files changed, 23 insertions(+), 42 deletions(-)

diff --git a/drivers/core/regmap.c b/drivers/core/regmap.c
index 8a0e00f..6c3dbe9 100644
--- a/drivers/core/regmap.c
+++ b/drivers/core/regmap.c
@@ -18,22 +18,13 @@
 
 DECLARE_GLOBAL_DATA_PTR;
 
-static struct regmap *regmap_alloc_count(int count)
+static struct regmap *regmap_alloc(int count)
 {
 	struct regmap *map;
 
-	map = malloc(sizeof(struct regmap));
+	map = malloc(sizeof(*map) + sizeof(map->ranges[0]) * count);
 	if (!map)
 		return NULL;
-	if (count <= 1) {
-		map->range = &map->base_range;
-	} else {
-		map->range = malloc(count * sizeof(struct regmap_range));
-		if (!map->range) {
-			free(map);
-			return NULL;
-		}
-	}
 	map->range_count = count;
 
 	return map;
@@ -46,12 +37,11 @@ int regmap_init_mem_platdata(struct udevice *dev, fdt_val_t *reg, int count,
 	struct regmap_range *range;
 	struct regmap *map;
 
-	map = regmap_alloc_count(count);
+	map = regmap_alloc(count);
 	if (!map)
 		return -ENOMEM;
 
-	map->base = *reg;
-	for (range = map->range; count > 0; reg += 2, range++, count--) {
+	for (range = map->ranges; count > 0; reg += 2, range++, count--) {
 		range->start = *reg;
 		range->size = reg[1];
 	}
@@ -84,11 +74,11 @@ int regmap_init_mem(struct udevice *dev, struct regmap **mapp)
 	if (!count)
 		return -EINVAL;
 
-	map = regmap_alloc_count(count);
+	map = regmap_alloc(count);
 	if (!map)
 		return -ENOMEM;
 
-	for (range = map->range, index = 0; count > 0;
+	for (range = map->ranges, index = 0; count > 0;
 	     count--, range++, index++) {
 		fdt_size_t sz;
 		if (of_live_active()) {
@@ -102,7 +92,6 @@ int regmap_init_mem(struct udevice *dev, struct regmap **mapp)
 			range->size = sz;
 		}
 	}
-	map->base = map->range[0].start;
 
 	*mapp = map;
 
@@ -116,15 +105,13 @@ void *regmap_get_range(struct regmap *map, unsigned int range_num)
 
 	if (range_num >= map->range_count)
 		return NULL;
-	range = &map->range[range_num];
+	range = &map->ranges[range_num];
 
 	return map_sysmem(range->start, range->size);
 }
 
 int regmap_uninit(struct regmap *map)
 {
-	if (map->range_count > 1)
-		free(map->range);
 	free(map);
 
 	return 0;
@@ -132,7 +119,7 @@ int regmap_uninit(struct regmap *map)
 
 int regmap_read(struct regmap *map, uint offset, uint *valp)
 {
-	uint32_t *ptr = map_physmem(map->base + offset, 4, MAP_NOCACHE);
+	u32 *ptr = map_physmem(map->ranges[0].start + offset, 4, MAP_NOCACHE);
 
 	*valp = le32_to_cpu(readl(ptr));
 
@@ -141,7 +128,7 @@ int regmap_read(struct regmap *map, uint offset, uint *valp)
 
 int regmap_write(struct regmap *map, uint offset, uint val)
 {
-	uint32_t *ptr = map_physmem(map->base + offset, 4, MAP_NOCACHE);
+	u32 *ptr = map_physmem(map->ranges[0].start + offset, 4, MAP_NOCACHE);
 
 	writel(cpu_to_le32(val), ptr);
 
diff --git a/drivers/phy/sti_usb_phy.c b/drivers/phy/sti_usb_phy.c
index 727fb8b..181a338 100644
--- a/drivers/phy/sti_usb_phy.c
+++ b/drivers/phy/sti_usb_phy.c
@@ -65,12 +65,12 @@ static int sti_usb_phy_init(struct phy *usb_phy)
 	void __iomem *reg;
 
 	/* set ctrl picophy value */
-	reg = (void __iomem *)phy->regmap->base + phy->ctrl;
+	reg = (void __iomem *)phy->regmap->ranges[0].start + phy->ctrl;
 	/* CTRL_PORT mask is 0x1f */
 	clrsetbits_le32(reg, 0x1f, STIH407_USB_PICOPHY_CTRL_PORT_CONF);
 
 	/* set ports parameters overriding */
-	reg = (void __iomem *)phy->regmap->base + phy->param;
+	reg = (void __iomem *)phy->regmap->ranges[0].start + phy->param;
 	/* PARAM_DEF mask is 0xffffffff */
 	clrsetbits_le32(reg, 0xffffffff, STIH407_USB_PICOPHY_PARAM_DEF);
 
diff --git a/drivers/pinctrl/pinctrl-sti.c b/drivers/pinctrl/pinctrl-sti.c
index a9c1495..890a7c2 100644
--- a/drivers/pinctrl/pinctrl-sti.c
+++ b/drivers/pinctrl/pinctrl-sti.c
@@ -62,7 +62,7 @@ void sti_alternate_select(struct udevice *dev, struct sti_pin_desc *pin_desc)
 	int bank = pin_desc->bank;
 	int pin = pin_desc->pin;
 
-	sysconfreg = (unsigned long *)plat->regmap->base;
+	sysconfreg = (unsigned long *)plat->regmap->ranges[0].start;
 
 	switch (bank) {
 	case 0 ... 5:		/* in "SBC Bank" */
@@ -96,7 +96,7 @@ void sti_pin_configure(struct udevice *dev, struct sti_pin_desc *pin_desc)
 	unsigned long *sysconfreg;
 	int bank = pin_desc->bank;
 
-	sysconfreg = (unsigned long *)plat->regmap->base + 40;
+	sysconfreg = (unsigned long *)plat->regmap->ranges[0].start + 40;
 
 	/*
 	 * NOTE: The PIO configuration for the PIO pins in the
diff --git a/drivers/reset/sti-reset.c b/drivers/reset/sti-reset.c
index 0fc5a28..41d7db4 100644
--- a/drivers/reset/sti-reset.c
+++ b/drivers/reset/sti-reset.c
@@ -218,7 +218,7 @@ phys_addr_t sti_reset_get_regmap(const char *compatible)
 		return -ENODEV;
 	}
 
-	return regmap->base;
+	return regmap->ranges[0].start;
 }
 
 static int sti_reset_program_hw(struct reset_ctl *reset_ctl, int assert)
diff --git a/drivers/sysreset/sysreset_sti.c b/drivers/sysreset/sysreset_sti.c
index 910f486..4fa7fd5 100644
--- a/drivers/sysreset/sysreset_sti.c
+++ b/drivers/sysreset/sysreset_sti.c
@@ -59,7 +59,7 @@ static int sti_sysreset_probe(struct udevice *dev)
 		return -ENODEV;
 	}
 
-	priv->base = regmap->base;
+	priv->base = regmap->ranges[0].start;
 
 	return 0;
 }
diff --git a/drivers/usb/host/dwc3-sti-glue.c b/drivers/usb/host/dwc3-sti-glue.c
index ce9335e..f505738 100644
--- a/drivers/usb/host/dwc3-sti-glue.c
+++ b/drivers/usb/host/dwc3-sti-glue.c
@@ -134,7 +134,7 @@ static int sti_dwc3_glue_ofdata_to_platdata(struct udevice *dev)
 		pr_err("unable to find regmap\n");
 		return -ENODEV;
 	}
-	plat->syscfg_base = regmap->base;
+	plat->syscfg_base = regmap->ranges[0].start;
 
 	/* get powerdown reset */
 	ret = reset_get_by_name(dev, "powerdown", &plat->powerdown_ctl);
diff --git a/include/regmap.h b/include/regmap.h
index 493a5d8..0d1dc5a 100644
--- a/include/regmap.h
+++ b/include/regmap.h
@@ -22,15 +22,12 @@ struct regmap_range {
 /**
  * struct regmap - a way of accessing hardware/bus registers
  *
- * @base:	Base address of register map
  * @range_count: Number of ranges available within the map
- * @range:	Pointer to the list of ranges, allocated if @range_count > 1
- * @base_range:	If @range_count is <= 1, @range points here
+ * @ranges:	Array of ranges
  */
 struct regmap {
-	phys_addr_t base;
 	int range_count;
-	struct regmap_range *range, base_range;
+	struct regmap_range ranges[0];
 };
 
 /*
diff --git a/test/dm/regmap.c b/test/dm/regmap.c
index 7f66058..8125345 100644
--- a/test/dm/regmap.c
+++ b/test/dm/regmap.c
@@ -26,23 +26,20 @@ static int dm_test_regmap_base(struct unit_test_state *uts)
 	map = syscon_get_regmap(dev);
 	ut_assertok_ptr(map);
 	ut_asserteq(1, map->range_count);
-	ut_asserteq(0x10, map->base);
-	ut_asserteq(0x10, map->range->start);
-	ut_asserteq(4, map->range->size);
-	ut_asserteq_ptr(&map->base_range, map->range);
+	ut_asserteq(0x10, map->ranges[0].start);
+	ut_asserteq(4, map->ranges[0].size);
 	ut_asserteq(0x10, map_to_sysmem(regmap_get_range(map, 0)));
 
 	ut_assertok(uclass_get_device(UCLASS_SYSCON, 1, &dev));
 	map = syscon_get_regmap(dev);
 	ut_assertok_ptr(map);
 	ut_asserteq(4, map->range_count);
-	ut_asserteq(0x20, map->base);
-	ut_assert(&map->base_range != map->range);
+	ut_asserteq(0x20, map->ranges[0].start);
 	for (i = 0; i < 4; i++) {
 		const unsigned long addr = 0x20 + 8 * i;
 
-		ut_asserteq(addr, map->range[i].start);
-		ut_asserteq(5 + i, map->range[i].size);
+		ut_asserteq(addr, map->ranges[i].start);
+		ut_asserteq(5 + i, map->ranges[i].size);
 		ut_asserteq(addr, map_to_sysmem(regmap_get_range(map, i)));
 	}
 
-- 
2.7.4



More information about the U-Boot mailing list