1cb675afcSDaniel Golle // SPDX-License-Identifier: GPL-2.0-only
2cb675afcSDaniel Golle
3cb675afcSDaniel Golle #include <linux/gpio/consumer.h>
4cb675afcSDaniel Golle #include <linux/mdio.h>
5cb675afcSDaniel Golle #include <linux/module.h>
6cb675afcSDaniel Golle #include <linux/pcs/pcs-mtk-lynxi.h>
7cb675afcSDaniel Golle #include <linux/of_irq.h>
8cb675afcSDaniel Golle #include <linux/of_mdio.h>
9cb675afcSDaniel Golle #include <linux/of_net.h>
10cb675afcSDaniel Golle #include <linux/of_platform.h>
11cb675afcSDaniel Golle #include <linux/regmap.h>
12cb675afcSDaniel Golle #include <linux/reset.h>
13cb675afcSDaniel Golle #include <linux/regulator/consumer.h>
14cb675afcSDaniel Golle #include <net/dsa.h>
15cb675afcSDaniel Golle
16cb675afcSDaniel Golle #include "mt7530.h"
17cb675afcSDaniel Golle
18cb675afcSDaniel Golle static int
mt7530_regmap_write(void * context,unsigned int reg,unsigned int val)19cb675afcSDaniel Golle mt7530_regmap_write(void *context, unsigned int reg, unsigned int val)
20cb675afcSDaniel Golle {
21cb675afcSDaniel Golle struct mii_bus *bus = context;
22cb675afcSDaniel Golle u16 page, r, lo, hi;
23cb675afcSDaniel Golle int ret;
24cb675afcSDaniel Golle
25cb675afcSDaniel Golle page = (reg >> 6) & 0x3ff;
26cb675afcSDaniel Golle r = (reg >> 2) & 0xf;
27cb675afcSDaniel Golle lo = val & 0xffff;
28cb675afcSDaniel Golle hi = val >> 16;
29cb675afcSDaniel Golle
30cb675afcSDaniel Golle /* MT7530 uses 31 as the pseudo port */
31cb675afcSDaniel Golle ret = bus->write(bus, 0x1f, 0x1f, page);
32cb675afcSDaniel Golle if (ret < 0)
33cb675afcSDaniel Golle return ret;
34cb675afcSDaniel Golle
35cb675afcSDaniel Golle ret = bus->write(bus, 0x1f, r, lo);
36cb675afcSDaniel Golle if (ret < 0)
37cb675afcSDaniel Golle return ret;
38cb675afcSDaniel Golle
39cb675afcSDaniel Golle ret = bus->write(bus, 0x1f, 0x10, hi);
40cb675afcSDaniel Golle return ret;
41cb675afcSDaniel Golle }
42cb675afcSDaniel Golle
43cb675afcSDaniel Golle static int
mt7530_regmap_read(void * context,unsigned int reg,unsigned int * val)44cb675afcSDaniel Golle mt7530_regmap_read(void *context, unsigned int reg, unsigned int *val)
45cb675afcSDaniel Golle {
46cb675afcSDaniel Golle struct mii_bus *bus = context;
47cb675afcSDaniel Golle u16 page, r, lo, hi;
48cb675afcSDaniel Golle int ret;
49cb675afcSDaniel Golle
50cb675afcSDaniel Golle page = (reg >> 6) & 0x3ff;
51cb675afcSDaniel Golle r = (reg >> 2) & 0xf;
52cb675afcSDaniel Golle
53cb675afcSDaniel Golle /* MT7530 uses 31 as the pseudo port */
54cb675afcSDaniel Golle ret = bus->write(bus, 0x1f, 0x1f, page);
55cb675afcSDaniel Golle if (ret < 0)
56cb675afcSDaniel Golle return ret;
57cb675afcSDaniel Golle
58cb675afcSDaniel Golle lo = bus->read(bus, 0x1f, r);
59cb675afcSDaniel Golle hi = bus->read(bus, 0x1f, 0x10);
60cb675afcSDaniel Golle
61cb675afcSDaniel Golle *val = (hi << 16) | (lo & 0xffff);
62cb675afcSDaniel Golle
63cb675afcSDaniel Golle return 0;
64cb675afcSDaniel Golle }
65cb675afcSDaniel Golle
66cb675afcSDaniel Golle static void
mt7530_mdio_regmap_lock(void * mdio_lock)67cb675afcSDaniel Golle mt7530_mdio_regmap_lock(void *mdio_lock)
68cb675afcSDaniel Golle {
69cb675afcSDaniel Golle mutex_lock_nested(mdio_lock, MDIO_MUTEX_NESTED);
70cb675afcSDaniel Golle }
71cb675afcSDaniel Golle
72cb675afcSDaniel Golle static void
mt7530_mdio_regmap_unlock(void * mdio_lock)73cb675afcSDaniel Golle mt7530_mdio_regmap_unlock(void *mdio_lock)
74cb675afcSDaniel Golle {
75cb675afcSDaniel Golle mutex_unlock(mdio_lock);
76cb675afcSDaniel Golle }
77cb675afcSDaniel Golle
78cb675afcSDaniel Golle static const struct regmap_bus mt7530_regmap_bus = {
79cb675afcSDaniel Golle .reg_write = mt7530_regmap_write,
80cb675afcSDaniel Golle .reg_read = mt7530_regmap_read,
81cb675afcSDaniel Golle };
82cb675afcSDaniel Golle
83cb675afcSDaniel Golle static int
mt7531_create_sgmii(struct mt7530_priv * priv,bool dual_sgmii)84*91daa4f6SDaniel Golle mt7531_create_sgmii(struct mt7530_priv *priv, bool dual_sgmii)
85cb675afcSDaniel Golle {
86*91daa4f6SDaniel Golle struct regmap_config *mt7531_pcs_config[2] = {};
87cb675afcSDaniel Golle struct phylink_pcs *pcs;
88cb675afcSDaniel Golle struct regmap *regmap;
89cb675afcSDaniel Golle int i, ret = 0;
90cb675afcSDaniel Golle
91*91daa4f6SDaniel Golle /* MT7531AE has two SGMII units for port 5 and port 6
92*91daa4f6SDaniel Golle * MT7531BE has only one SGMII unit for port 6
93*91daa4f6SDaniel Golle */
94*91daa4f6SDaniel Golle for (i = dual_sgmii ? 0 : 1; i < 2; i++) {
95cb675afcSDaniel Golle mt7531_pcs_config[i] = devm_kzalloc(priv->dev,
96cb675afcSDaniel Golle sizeof(struct regmap_config),
97cb675afcSDaniel Golle GFP_KERNEL);
98cb675afcSDaniel Golle if (!mt7531_pcs_config[i]) {
99cb675afcSDaniel Golle ret = -ENOMEM;
100cb675afcSDaniel Golle break;
101cb675afcSDaniel Golle }
102cb675afcSDaniel Golle
103cb675afcSDaniel Golle mt7531_pcs_config[i]->name = i ? "port6" : "port5";
104cb675afcSDaniel Golle mt7531_pcs_config[i]->reg_bits = 16;
105cb675afcSDaniel Golle mt7531_pcs_config[i]->val_bits = 32;
106cb675afcSDaniel Golle mt7531_pcs_config[i]->reg_stride = 4;
107cb675afcSDaniel Golle mt7531_pcs_config[i]->reg_base = MT7531_SGMII_REG_BASE(5 + i);
108cb675afcSDaniel Golle mt7531_pcs_config[i]->max_register = 0x17c;
109cb675afcSDaniel Golle mt7531_pcs_config[i]->lock = mt7530_mdio_regmap_lock;
110cb675afcSDaniel Golle mt7531_pcs_config[i]->unlock = mt7530_mdio_regmap_unlock;
111cb675afcSDaniel Golle mt7531_pcs_config[i]->lock_arg = &priv->bus->mdio_lock;
112cb675afcSDaniel Golle
113cb675afcSDaniel Golle regmap = devm_regmap_init(priv->dev,
114cb675afcSDaniel Golle &mt7530_regmap_bus, priv->bus,
115cb675afcSDaniel Golle mt7531_pcs_config[i]);
116cb675afcSDaniel Golle if (IS_ERR(regmap)) {
117cb675afcSDaniel Golle ret = PTR_ERR(regmap);
118cb675afcSDaniel Golle break;
119cb675afcSDaniel Golle }
120cb675afcSDaniel Golle pcs = mtk_pcs_lynxi_create(priv->dev, regmap,
121cb675afcSDaniel Golle MT7531_PHYA_CTRL_SIGNAL3, 0);
122cb675afcSDaniel Golle if (!pcs) {
123cb675afcSDaniel Golle ret = -ENXIO;
124cb675afcSDaniel Golle break;
125cb675afcSDaniel Golle }
126cb675afcSDaniel Golle priv->ports[5 + i].sgmii_pcs = pcs;
127cb675afcSDaniel Golle }
128cb675afcSDaniel Golle
129cb675afcSDaniel Golle if (ret && i)
130cb675afcSDaniel Golle mtk_pcs_lynxi_destroy(priv->ports[5].sgmii_pcs);
131cb675afcSDaniel Golle
132cb675afcSDaniel Golle return ret;
133cb675afcSDaniel Golle }
134cb675afcSDaniel Golle
135cb675afcSDaniel Golle static const struct of_device_id mt7530_of_match[] = {
136cb675afcSDaniel Golle { .compatible = "mediatek,mt7621", .data = &mt753x_table[ID_MT7621], },
137cb675afcSDaniel Golle { .compatible = "mediatek,mt7530", .data = &mt753x_table[ID_MT7530], },
138cb675afcSDaniel Golle { .compatible = "mediatek,mt7531", .data = &mt753x_table[ID_MT7531], },
139cb675afcSDaniel Golle { /* sentinel */ },
140cb675afcSDaniel Golle };
141cb675afcSDaniel Golle MODULE_DEVICE_TABLE(of, mt7530_of_match);
142cb675afcSDaniel Golle
143cb675afcSDaniel Golle static int
mt7530_probe(struct mdio_device * mdiodev)144cb675afcSDaniel Golle mt7530_probe(struct mdio_device *mdiodev)
145cb675afcSDaniel Golle {
146cb675afcSDaniel Golle static struct regmap_config *regmap_config;
147cb675afcSDaniel Golle struct mt7530_priv *priv;
148cb675afcSDaniel Golle struct device_node *dn;
149cb675afcSDaniel Golle int ret;
150cb675afcSDaniel Golle
151cb675afcSDaniel Golle dn = mdiodev->dev.of_node;
152cb675afcSDaniel Golle
153cb675afcSDaniel Golle priv = devm_kzalloc(&mdiodev->dev, sizeof(*priv), GFP_KERNEL);
154cb675afcSDaniel Golle if (!priv)
155cb675afcSDaniel Golle return -ENOMEM;
156cb675afcSDaniel Golle
157cb675afcSDaniel Golle priv->bus = mdiodev->bus;
158cb675afcSDaniel Golle priv->dev = &mdiodev->dev;
159cb675afcSDaniel Golle
160cb675afcSDaniel Golle ret = mt7530_probe_common(priv);
161cb675afcSDaniel Golle if (ret)
162cb675afcSDaniel Golle return ret;
163cb675afcSDaniel Golle
164cb675afcSDaniel Golle /* Use medatek,mcm property to distinguish hardware type that would
165cb675afcSDaniel Golle * cause a little bit differences on power-on sequence.
166cb675afcSDaniel Golle * Not MCM that indicates switch works as the remote standalone
167cb675afcSDaniel Golle * integrated circuit so the GPIO pin would be used to complete
168cb675afcSDaniel Golle * the reset, otherwise memory-mapped register accessing used
169cb675afcSDaniel Golle * through syscon provides in the case of MCM.
170cb675afcSDaniel Golle */
171cb675afcSDaniel Golle priv->mcm = of_property_read_bool(dn, "mediatek,mcm");
172cb675afcSDaniel Golle if (priv->mcm) {
173cb675afcSDaniel Golle dev_info(&mdiodev->dev, "MT7530 adapts as multi-chip module\n");
174cb675afcSDaniel Golle
175cb675afcSDaniel Golle priv->rstc = devm_reset_control_get(&mdiodev->dev, "mcm");
176cb675afcSDaniel Golle if (IS_ERR(priv->rstc)) {
177cb675afcSDaniel Golle dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
178cb675afcSDaniel Golle return PTR_ERR(priv->rstc);
179cb675afcSDaniel Golle }
180cb675afcSDaniel Golle } else {
181cb675afcSDaniel Golle priv->reset = devm_gpiod_get_optional(&mdiodev->dev, "reset",
182cb675afcSDaniel Golle GPIOD_OUT_LOW);
183cb675afcSDaniel Golle if (IS_ERR(priv->reset)) {
184cb675afcSDaniel Golle dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
185cb675afcSDaniel Golle return PTR_ERR(priv->reset);
186cb675afcSDaniel Golle }
187cb675afcSDaniel Golle }
188cb675afcSDaniel Golle
189cb675afcSDaniel Golle if (priv->id == ID_MT7530) {
190cb675afcSDaniel Golle priv->core_pwr = devm_regulator_get(&mdiodev->dev, "core");
191cb675afcSDaniel Golle if (IS_ERR(priv->core_pwr))
192cb675afcSDaniel Golle return PTR_ERR(priv->core_pwr);
193cb675afcSDaniel Golle
194cb675afcSDaniel Golle priv->io_pwr = devm_regulator_get(&mdiodev->dev, "io");
195cb675afcSDaniel Golle if (IS_ERR(priv->io_pwr))
196cb675afcSDaniel Golle return PTR_ERR(priv->io_pwr);
197cb675afcSDaniel Golle }
198cb675afcSDaniel Golle
199cb675afcSDaniel Golle regmap_config = devm_kzalloc(&mdiodev->dev, sizeof(*regmap_config),
200cb675afcSDaniel Golle GFP_KERNEL);
201cb675afcSDaniel Golle if (!regmap_config)
202cb675afcSDaniel Golle return -ENOMEM;
203cb675afcSDaniel Golle
204cb675afcSDaniel Golle regmap_config->reg_bits = 16;
205cb675afcSDaniel Golle regmap_config->val_bits = 32;
206cb675afcSDaniel Golle regmap_config->reg_stride = 4;
207cb675afcSDaniel Golle regmap_config->max_register = MT7530_CREV;
208cb675afcSDaniel Golle regmap_config->disable_locking = true;
209cb675afcSDaniel Golle priv->regmap = devm_regmap_init(priv->dev, &mt7530_regmap_bus,
210cb675afcSDaniel Golle priv->bus, regmap_config);
211cb675afcSDaniel Golle if (IS_ERR(priv->regmap))
212cb675afcSDaniel Golle return PTR_ERR(priv->regmap);
213cb675afcSDaniel Golle
214*91daa4f6SDaniel Golle if (priv->id == ID_MT7531)
215*91daa4f6SDaniel Golle priv->create_sgmii = mt7531_create_sgmii;
216cb675afcSDaniel Golle
217cb675afcSDaniel Golle return dsa_register_switch(priv->ds);
218cb675afcSDaniel Golle }
219cb675afcSDaniel Golle
220cb675afcSDaniel Golle static void
mt7530_remove(struct mdio_device * mdiodev)221cb675afcSDaniel Golle mt7530_remove(struct mdio_device *mdiodev)
222cb675afcSDaniel Golle {
223cb675afcSDaniel Golle struct mt7530_priv *priv = dev_get_drvdata(&mdiodev->dev);
224cb675afcSDaniel Golle int ret = 0, i;
225cb675afcSDaniel Golle
226cb675afcSDaniel Golle if (!priv)
227cb675afcSDaniel Golle return;
228cb675afcSDaniel Golle
229cb675afcSDaniel Golle ret = regulator_disable(priv->core_pwr);
230cb675afcSDaniel Golle if (ret < 0)
231cb675afcSDaniel Golle dev_err(priv->dev,
232cb675afcSDaniel Golle "Failed to disable core power: %d\n", ret);
233cb675afcSDaniel Golle
234cb675afcSDaniel Golle ret = regulator_disable(priv->io_pwr);
235cb675afcSDaniel Golle if (ret < 0)
236cb675afcSDaniel Golle dev_err(priv->dev, "Failed to disable io pwr: %d\n",
237cb675afcSDaniel Golle ret);
238cb675afcSDaniel Golle
239cb675afcSDaniel Golle mt7530_remove_common(priv);
240cb675afcSDaniel Golle
241cb675afcSDaniel Golle for (i = 0; i < 2; ++i)
242cb675afcSDaniel Golle mtk_pcs_lynxi_destroy(priv->ports[5 + i].sgmii_pcs);
243cb675afcSDaniel Golle }
244cb675afcSDaniel Golle
mt7530_shutdown(struct mdio_device * mdiodev)245cb675afcSDaniel Golle static void mt7530_shutdown(struct mdio_device *mdiodev)
246cb675afcSDaniel Golle {
247cb675afcSDaniel Golle struct mt7530_priv *priv = dev_get_drvdata(&mdiodev->dev);
248cb675afcSDaniel Golle
249cb675afcSDaniel Golle if (!priv)
250cb675afcSDaniel Golle return;
251cb675afcSDaniel Golle
252cb675afcSDaniel Golle dsa_switch_shutdown(priv->ds);
253cb675afcSDaniel Golle
254cb675afcSDaniel Golle dev_set_drvdata(&mdiodev->dev, NULL);
255cb675afcSDaniel Golle }
256cb675afcSDaniel Golle
257cb675afcSDaniel Golle static struct mdio_driver mt7530_mdio_driver = {
258cb675afcSDaniel Golle .probe = mt7530_probe,
259cb675afcSDaniel Golle .remove = mt7530_remove,
260cb675afcSDaniel Golle .shutdown = mt7530_shutdown,
261cb675afcSDaniel Golle .mdiodrv.driver = {
262cb675afcSDaniel Golle .name = "mt7530-mdio",
263cb675afcSDaniel Golle .of_match_table = mt7530_of_match,
264cb675afcSDaniel Golle },
265cb675afcSDaniel Golle };
266cb675afcSDaniel Golle
267cb675afcSDaniel Golle mdio_module_driver(mt7530_mdio_driver);
268cb675afcSDaniel Golle
269cb675afcSDaniel Golle MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
270cb675afcSDaniel Golle MODULE_DESCRIPTION("Driver for Mediatek MT7530 Switch (MDIO)");
271cb675afcSDaniel Golle MODULE_LICENSE("GPL");
272