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 u32
__ocelot_read_ix(struct ocelot
*ocelot
, u32 reg
, u32 offset
)
15 u16 target
= reg
>> TARGET_OFFSET
;
20 regmap_read(ocelot
->targets
[target
],
21 ocelot
->map
[target
][reg
& REG_MASK
] + offset
, &val
);
24 EXPORT_SYMBOL(__ocelot_read_ix
);
26 void __ocelot_write_ix(struct ocelot
*ocelot
, u32 val
, u32 reg
, u32 offset
)
28 u16 target
= reg
>> TARGET_OFFSET
;
32 regmap_write(ocelot
->targets
[target
],
33 ocelot
->map
[target
][reg
& REG_MASK
] + offset
, val
);
35 EXPORT_SYMBOL(__ocelot_write_ix
);
37 void __ocelot_rmw_ix(struct ocelot
*ocelot
, u32 val
, u32 mask
, u32 reg
,
40 u16 target
= reg
>> TARGET_OFFSET
;
44 regmap_update_bits(ocelot
->targets
[target
],
45 ocelot
->map
[target
][reg
& REG_MASK
] + offset
,
48 EXPORT_SYMBOL(__ocelot_rmw_ix
);
50 u32
ocelot_port_readl(struct ocelot_port
*port
, u32 reg
)
52 return readl(port
->regs
+ reg
);
54 EXPORT_SYMBOL(ocelot_port_readl
);
56 void ocelot_port_writel(struct ocelot_port
*port
, u32 val
, u32 reg
)
58 writel(val
, port
->regs
+ reg
);
60 EXPORT_SYMBOL(ocelot_port_writel
);
62 int ocelot_regfields_init(struct ocelot
*ocelot
,
63 const struct reg_field
*const regfields
)
68 for (i
= 0; i
< REGFIELD_MAX
; i
++) {
69 struct reg_field regfield
= {};
70 u32 reg
= regfields
[i
].reg
;
75 target
= regfields
[i
].reg
>> TARGET_OFFSET
;
77 regfield
.reg
= ocelot
->map
[target
][reg
& REG_MASK
];
78 regfield
.lsb
= regfields
[i
].lsb
;
79 regfield
.msb
= regfields
[i
].msb
;
81 ocelot
->regfields
[i
] =
82 devm_regmap_field_alloc(ocelot
->dev
,
83 ocelot
->targets
[target
],
86 if (IS_ERR(ocelot
->regfields
[i
]))
87 return PTR_ERR(ocelot
->regfields
[i
]);
92 EXPORT_SYMBOL(ocelot_regfields_init
);
94 static struct regmap_config ocelot_regmap_config
= {
100 struct regmap
*ocelot_regmap_init(struct ocelot
*ocelot
, struct resource
*res
)
104 regs
= devm_ioremap_resource(ocelot
->dev
, res
);
106 return ERR_CAST(regs
);
108 ocelot_regmap_config
.name
= res
->name
;
110 return devm_regmap_init_mmio(ocelot
->dev
, regs
, &ocelot_regmap_config
);
112 EXPORT_SYMBOL(ocelot_regmap_init
);