From 8c1de5e08b845eb71c9a247d16bc81ebdab0598d Mon Sep 17 00:00:00 2001 From: Masahiro Yamada Date: Thu, 19 Apr 2018 12:14:01 +0900 Subject: [PATCH] regmap: clean up regmap allocation 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 Reviewed-by: Simon Glass [trini: fixup cpu_info-rcar.c] Signed-off-by: Tom Rini --- arch/arm/mach-rmobile/cpu_info-rcar.c | 2 +- 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 +++++------ 9 files changed, 24 insertions(+), 43 deletions(-) diff --git a/arch/arm/mach-rmobile/cpu_info-rcar.c b/arch/arm/mach-rmobile/cpu_info-rcar.c index fbff55ed51..ccb7a32ac4 100644 --- a/arch/arm/mach-rmobile/cpu_info-rcar.c +++ b/arch/arm/mach-rmobile/cpu_info-rcar.c @@ -74,7 +74,7 @@ static u32 rmobile_get_prr(void) hang(); } - return readl(map->base); + return readl(map->ranges[0].start); } static const struct udevice_id renesas_prr_ids[] = { diff --git a/drivers/core/regmap.c b/drivers/core/regmap.c index a5e4e74825..0f787ab4e1 100644 --- a/drivers/core/regmap.c +++ b/drivers/core/regmap.c @@ -17,22 +17,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; @@ -45,12 +36,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]; } @@ -83,11 +73,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()) { @@ -101,7 +91,6 @@ int regmap_init_mem(struct udevice *dev, struct regmap **mapp) range->size = sz; } } - map->base = map->range[0].start; *mapp = map; @@ -115,15 +104,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; @@ -131,7 +118,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)); @@ -140,7 +127,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 c63967c2a1..ec597da84e 100644 --- a/drivers/phy/sti_usb_phy.c +++ b/drivers/phy/sti_usb_phy.c @@ -64,12 +64,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 ed137d62b5..f0cd845fea 100644 --- a/drivers/pinctrl/pinctrl-sti.c +++ b/drivers/pinctrl/pinctrl-sti.c @@ -61,7 +61,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" */ @@ -95,7 +95,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 5d70e20c35..d8cc485ce6 100644 --- a/drivers/reset/sti-reset.c +++ b/drivers/reset/sti-reset.c @@ -217,7 +217,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 df2fa1cf7a..9996716f13 100644 --- a/drivers/sysreset/sysreset_sti.c +++ b/drivers/sysreset/sysreset_sti.c @@ -58,7 +58,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 3211c8ee28..ad7cf6e6b5 100644 --- a/drivers/usb/host/dwc3-sti-glue.c +++ b/drivers/usb/host/dwc3-sti-glue.c @@ -133,7 +133,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 8d18b07c30..d827a0bd52 100644 --- a/include/regmap.h +++ b/include/regmap.h @@ -21,15 +21,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 abb718a89e..77b82ce8ac 100644 --- a/test/dm/regmap.c +++ b/test/dm/regmap.c @@ -23,23 +23,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.39.5