1*4882a593Smuzhiyun // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Microsemi Ocelot Switch driver
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (c) 2017 Microsemi Corporation
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun #include <linux/io.h>
8*4882a593Smuzhiyun #include <linux/kernel.h>
9*4882a593Smuzhiyun #include <linux/platform_device.h>
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include "ocelot.h"
12*4882a593Smuzhiyun
__ocelot_read_ix(struct ocelot * ocelot,u32 reg,u32 offset)13*4882a593Smuzhiyun u32 __ocelot_read_ix(struct ocelot *ocelot, u32 reg, u32 offset)
14*4882a593Smuzhiyun {
15*4882a593Smuzhiyun u16 target = reg >> TARGET_OFFSET;
16*4882a593Smuzhiyun u32 val;
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun WARN_ON(!target);
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun regmap_read(ocelot->targets[target],
21*4882a593Smuzhiyun ocelot->map[target][reg & REG_MASK] + offset, &val);
22*4882a593Smuzhiyun return val;
23*4882a593Smuzhiyun }
24*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(__ocelot_read_ix);
25*4882a593Smuzhiyun
__ocelot_write_ix(struct ocelot * ocelot,u32 val,u32 reg,u32 offset)26*4882a593Smuzhiyun void __ocelot_write_ix(struct ocelot *ocelot, u32 val, u32 reg, u32 offset)
27*4882a593Smuzhiyun {
28*4882a593Smuzhiyun u16 target = reg >> TARGET_OFFSET;
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun WARN_ON(!target);
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun regmap_write(ocelot->targets[target],
33*4882a593Smuzhiyun ocelot->map[target][reg & REG_MASK] + offset, val);
34*4882a593Smuzhiyun }
35*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(__ocelot_write_ix);
36*4882a593Smuzhiyun
__ocelot_rmw_ix(struct ocelot * ocelot,u32 val,u32 mask,u32 reg,u32 offset)37*4882a593Smuzhiyun void __ocelot_rmw_ix(struct ocelot *ocelot, u32 val, u32 mask, u32 reg,
38*4882a593Smuzhiyun u32 offset)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun u16 target = reg >> TARGET_OFFSET;
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun WARN_ON(!target);
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun regmap_update_bits(ocelot->targets[target],
45*4882a593Smuzhiyun ocelot->map[target][reg & REG_MASK] + offset,
46*4882a593Smuzhiyun mask, val);
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(__ocelot_rmw_ix);
49*4882a593Smuzhiyun
ocelot_port_readl(struct ocelot_port * port,u32 reg)50*4882a593Smuzhiyun u32 ocelot_port_readl(struct ocelot_port *port, u32 reg)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun struct ocelot *ocelot = port->ocelot;
53*4882a593Smuzhiyun u16 target = reg >> TARGET_OFFSET;
54*4882a593Smuzhiyun u32 val;
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun WARN_ON(!target);
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun regmap_read(port->target, ocelot->map[target][reg & REG_MASK], &val);
59*4882a593Smuzhiyun return val;
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(ocelot_port_readl);
62*4882a593Smuzhiyun
ocelot_port_writel(struct ocelot_port * port,u32 val,u32 reg)63*4882a593Smuzhiyun void ocelot_port_writel(struct ocelot_port *port, u32 val, u32 reg)
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun struct ocelot *ocelot = port->ocelot;
66*4882a593Smuzhiyun u16 target = reg >> TARGET_OFFSET;
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun WARN_ON(!target);
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun regmap_write(port->target, ocelot->map[target][reg & REG_MASK], val);
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(ocelot_port_writel);
73*4882a593Smuzhiyun
ocelot_port_rmwl(struct ocelot_port * port,u32 val,u32 mask,u32 reg)74*4882a593Smuzhiyun void ocelot_port_rmwl(struct ocelot_port *port, u32 val, u32 mask, u32 reg)
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun u32 cur = ocelot_port_readl(port, reg);
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun ocelot_port_writel(port, (cur & (~mask)) | val, reg);
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(ocelot_port_rmwl);
81*4882a593Smuzhiyun
__ocelot_target_read_ix(struct ocelot * ocelot,enum ocelot_target target,u32 reg,u32 offset)82*4882a593Smuzhiyun u32 __ocelot_target_read_ix(struct ocelot *ocelot, enum ocelot_target target,
83*4882a593Smuzhiyun u32 reg, u32 offset)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun u32 val;
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun regmap_read(ocelot->targets[target],
88*4882a593Smuzhiyun ocelot->map[target][reg] + offset, &val);
89*4882a593Smuzhiyun return val;
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun
__ocelot_target_write_ix(struct ocelot * ocelot,enum ocelot_target target,u32 val,u32 reg,u32 offset)92*4882a593Smuzhiyun void __ocelot_target_write_ix(struct ocelot *ocelot, enum ocelot_target target,
93*4882a593Smuzhiyun u32 val, u32 reg, u32 offset)
94*4882a593Smuzhiyun {
95*4882a593Smuzhiyun regmap_write(ocelot->targets[target],
96*4882a593Smuzhiyun ocelot->map[target][reg] + offset, val);
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun
ocelot_regfields_init(struct ocelot * ocelot,const struct reg_field * const regfields)99*4882a593Smuzhiyun int ocelot_regfields_init(struct ocelot *ocelot,
100*4882a593Smuzhiyun const struct reg_field *const regfields)
101*4882a593Smuzhiyun {
102*4882a593Smuzhiyun unsigned int i;
103*4882a593Smuzhiyun u16 target;
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun for (i = 0; i < REGFIELD_MAX; i++) {
106*4882a593Smuzhiyun struct reg_field regfield = {};
107*4882a593Smuzhiyun u32 reg = regfields[i].reg;
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun if (!reg)
110*4882a593Smuzhiyun continue;
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun target = regfields[i].reg >> TARGET_OFFSET;
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun regfield.reg = ocelot->map[target][reg & REG_MASK];
115*4882a593Smuzhiyun regfield.lsb = regfields[i].lsb;
116*4882a593Smuzhiyun regfield.msb = regfields[i].msb;
117*4882a593Smuzhiyun regfield.id_size = regfields[i].id_size;
118*4882a593Smuzhiyun regfield.id_offset = regfields[i].id_offset;
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun ocelot->regfields[i] =
121*4882a593Smuzhiyun devm_regmap_field_alloc(ocelot->dev,
122*4882a593Smuzhiyun ocelot->targets[target],
123*4882a593Smuzhiyun regfield);
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun if (IS_ERR(ocelot->regfields[i]))
126*4882a593Smuzhiyun return PTR_ERR(ocelot->regfields[i]);
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun return 0;
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(ocelot_regfields_init);
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun static struct regmap_config ocelot_regmap_config = {
134*4882a593Smuzhiyun .reg_bits = 32,
135*4882a593Smuzhiyun .val_bits = 32,
136*4882a593Smuzhiyun .reg_stride = 4,
137*4882a593Smuzhiyun };
138*4882a593Smuzhiyun
ocelot_regmap_init(struct ocelot * ocelot,struct resource * res)139*4882a593Smuzhiyun struct regmap *ocelot_regmap_init(struct ocelot *ocelot, struct resource *res)
140*4882a593Smuzhiyun {
141*4882a593Smuzhiyun void __iomem *regs;
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun regs = devm_ioremap_resource(ocelot->dev, res);
144*4882a593Smuzhiyun if (IS_ERR(regs))
145*4882a593Smuzhiyun return ERR_CAST(regs);
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun ocelot_regmap_config.name = res->name;
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun return devm_regmap_init_mmio(ocelot->dev, regs, &ocelot_regmap_config);
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(ocelot_regmap_init);
152