Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

net: mscc: ocelot: refactor enum ocelot_reg decoding to helper

ocelot_io.c duplicates the decoding of an enum ocelot_reg (which holds
an enum ocelot_target in the upper bits and an index into a regmap array
in the lower bits) 4 times.

We'd like to reuse that logic once more, from ocelot.c. In order to do
that, let's consolidate the existing 4 instances into a header
accessible both by ocelot.c as well as by ocelot_io.c.

Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
Reviewed-by: Jacob Keller <jacob.e.keller@intel.com>
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Vladimir Oltean and committed by
Jakub Kicinski
40cd07cb 9ecd0579

+25 -14
+9
drivers/net/ethernet/mscc/ocelot.h
··· 74 74 struct ocelot_pgid *pgid; 75 75 }; 76 76 77 + static inline void ocelot_reg_to_target_addr(struct ocelot *ocelot, 78 + enum ocelot_reg reg, 79 + enum ocelot_target *target, 80 + u32 *addr) 81 + { 82 + *target = reg >> TARGET_OFFSET; 83 + *addr = ocelot->map[*target][reg & REG_MASK]; 84 + } 85 + 77 86 int ocelot_bridge_num_find(struct ocelot *ocelot, 78 87 const struct net_device *bridge); 79 88
+16 -14
drivers/net/ethernet/mscc/ocelot_io.c
··· 13 13 int __ocelot_bulk_read_ix(struct ocelot *ocelot, enum ocelot_reg reg, 14 14 u32 offset, void *buf, int count) 15 15 { 16 - u16 target = reg >> TARGET_OFFSET; 16 + enum ocelot_target target; 17 + u32 addr; 17 18 19 + ocelot_reg_to_target_addr(ocelot, reg, &target, &addr); 18 20 WARN_ON(!target); 19 21 20 - return regmap_bulk_read(ocelot->targets[target], 21 - ocelot->map[target][reg & REG_MASK] + offset, 22 + return regmap_bulk_read(ocelot->targets[target], addr + offset, 22 23 buf, count); 23 24 } 24 25 EXPORT_SYMBOL_GPL(__ocelot_bulk_read_ix); 25 26 26 27 u32 __ocelot_read_ix(struct ocelot *ocelot, enum ocelot_reg reg, u32 offset) 27 28 { 28 - u16 target = reg >> TARGET_OFFSET; 29 - u32 val; 29 + enum ocelot_target target; 30 + u32 addr, val; 30 31 32 + ocelot_reg_to_target_addr(ocelot, reg, &target, &addr); 31 33 WARN_ON(!target); 32 34 33 - regmap_read(ocelot->targets[target], 34 - ocelot->map[target][reg & REG_MASK] + offset, &val); 35 + regmap_read(ocelot->targets[target], addr + offset, &val); 35 36 return val; 36 37 } 37 38 EXPORT_SYMBOL_GPL(__ocelot_read_ix); ··· 40 39 void __ocelot_write_ix(struct ocelot *ocelot, u32 val, enum ocelot_reg reg, 41 40 u32 offset) 42 41 { 43 - u16 target = reg >> TARGET_OFFSET; 42 + enum ocelot_target target; 43 + u32 addr; 44 44 45 + ocelot_reg_to_target_addr(ocelot, reg, &target, &addr); 45 46 WARN_ON(!target); 46 47 47 - regmap_write(ocelot->targets[target], 48 - ocelot->map[target][reg & REG_MASK] + offset, val); 48 + regmap_write(ocelot->targets[target], addr + offset, val); 49 49 } 50 50 EXPORT_SYMBOL_GPL(__ocelot_write_ix); 51 51 52 52 void __ocelot_rmw_ix(struct ocelot *ocelot, u32 val, u32 mask, 53 53 enum ocelot_reg reg, u32 offset) 54 54 { 55 - u16 target = reg >> TARGET_OFFSET; 55 + enum ocelot_target target; 56 + u32 addr; 56 57 58 + ocelot_reg_to_target_addr(ocelot, reg, &target, &addr); 57 59 WARN_ON(!target); 58 60 59 - regmap_update_bits(ocelot->targets[target], 60 - ocelot->map[target][reg & REG_MASK] + offset, 61 - mask, val); 61 + regmap_update_bits(ocelot->targets[target], addr + offset, mask, val); 62 62 } 63 63 EXPORT_SYMBOL_GPL(__ocelot_rmw_ix); 64 64