1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
3 * Microsemi Ocelot Switch driver
5 * Copyright (c) 2017 Microsemi Corporation
8 #include <linux/kernel.h>
9 #include <linux/platform_device.h>
13 int __ocelot_bulk_read_ix(struct ocelot *ocelot, enum ocelot_reg reg,
14 u32 offset, void *buf, int count)
16 enum ocelot_target target;
19 ocelot_reg_to_target_addr(ocelot, reg, &target, &addr);
22 return regmap_bulk_read(ocelot->targets[target], addr + offset,
25 EXPORT_SYMBOL_GPL(__ocelot_bulk_read_ix);
27 u32 __ocelot_read_ix(struct ocelot *ocelot, enum ocelot_reg reg, u32 offset)
29 enum ocelot_target target;
32 ocelot_reg_to_target_addr(ocelot, reg, &target, &addr);
35 regmap_read(ocelot->targets[target], addr + offset, &val);
38 EXPORT_SYMBOL_GPL(__ocelot_read_ix);
40 void __ocelot_write_ix(struct ocelot *ocelot, u32 val, enum ocelot_reg reg,
43 enum ocelot_target target;
46 ocelot_reg_to_target_addr(ocelot, reg, &target, &addr);
49 regmap_write(ocelot->targets[target], addr + offset, val);
51 EXPORT_SYMBOL_GPL(__ocelot_write_ix);
53 void __ocelot_rmw_ix(struct ocelot *ocelot, u32 val, u32 mask,
54 enum ocelot_reg reg, u32 offset)
56 enum ocelot_target target;
59 ocelot_reg_to_target_addr(ocelot, reg, &target, &addr);
62 regmap_update_bits(ocelot->targets[target], addr + offset, mask, val);
64 EXPORT_SYMBOL_GPL(__ocelot_rmw_ix);
66 u32 ocelot_port_readl(struct ocelot_port *port, enum ocelot_reg reg)
68 struct ocelot *ocelot = port->ocelot;
69 u16 target = reg >> TARGET_OFFSET;
74 regmap_read(port->target, ocelot->map[target][reg & REG_MASK], &val);
77 EXPORT_SYMBOL_GPL(ocelot_port_readl);
79 void ocelot_port_writel(struct ocelot_port *port, u32 val, enum ocelot_reg reg)
81 struct ocelot *ocelot = port->ocelot;
82 u16 target = reg >> TARGET_OFFSET;
86 regmap_write(port->target, ocelot->map[target][reg & REG_MASK], val);
88 EXPORT_SYMBOL_GPL(ocelot_port_writel);
90 void ocelot_port_rmwl(struct ocelot_port *port, u32 val, u32 mask,
93 u32 cur = ocelot_port_readl(port, reg);
95 ocelot_port_writel(port, (cur & (~mask)) | val, reg);
97 EXPORT_SYMBOL_GPL(ocelot_port_rmwl);
99 u32 __ocelot_target_read_ix(struct ocelot *ocelot, enum ocelot_target target,
104 regmap_read(ocelot->targets[target],
105 ocelot->map[target][reg] + offset, &val);
109 void __ocelot_target_write_ix(struct ocelot *ocelot, enum ocelot_target target,
110 u32 val, u32 reg, u32 offset)
112 regmap_write(ocelot->targets[target],
113 ocelot->map[target][reg] + offset, val);
116 int ocelot_regfields_init(struct ocelot *ocelot,
117 const struct reg_field *const regfields)
122 for (i = 0; i < REGFIELD_MAX; i++) {
123 struct reg_field regfield = {};
124 u32 reg = regfields[i].reg;
129 target = regfields[i].reg >> TARGET_OFFSET;
131 regfield.reg = ocelot->map[target][reg & REG_MASK];
132 regfield.lsb = regfields[i].lsb;
133 regfield.msb = regfields[i].msb;
134 regfield.id_size = regfields[i].id_size;
135 regfield.id_offset = regfields[i].id_offset;
137 ocelot->regfields[i] =
138 devm_regmap_field_alloc(ocelot->dev,
139 ocelot->targets[target],
142 if (IS_ERR(ocelot->regfields[i]))
143 return PTR_ERR(ocelot->regfields[i]);
148 EXPORT_SYMBOL_GPL(ocelot_regfields_init);
150 static struct regmap_config ocelot_regmap_config = {
156 struct regmap *ocelot_regmap_init(struct ocelot *ocelot, struct resource *res)
160 regs = devm_ioremap_resource(ocelot->dev, res);
162 return ERR_CAST(regs);
164 ocelot_regmap_config.name = res->name;
166 return devm_regmap_init_mmio(ocelot->dev, regs, &ocelot_regmap_config);
168 EXPORT_SYMBOL_GPL(ocelot_regmap_init);