1 // SPDX-License-Identifier: GPL-2.0
4 #include <linux/pcs/pcs-xpcs.h>
5 #include <linux/of_mdio.h>
8 #define SJA1110_PCS_BANK_REG SJA1110_SPI_ADDR(0x3fc)
10 int sja1105_pcs_mdio_read(struct mii_bus *bus, int phy, int reg)
12 struct sja1105_mdio_private *mdio_priv = bus->priv;
13 struct sja1105_private *priv = mdio_priv->priv;
19 if (!(reg & MII_ADDR_C45))
22 mmd = (reg >> MII_DEVADDR_C45_SHIFT) & 0x1f;
23 addr = (mmd << 16) | (reg & GENMASK(15, 0));
25 if (mmd != MDIO_MMD_VEND1 && mmd != MDIO_MMD_VEND2)
28 if (mmd == MDIO_MMD_VEND2 && (reg & GENMASK(15, 0)) == MII_PHYSID1)
29 return NXP_SJA1105_XPCS_ID >> 16;
30 if (mmd == MDIO_MMD_VEND2 && (reg & GENMASK(15, 0)) == MII_PHYSID2)
31 return NXP_SJA1105_XPCS_ID & GENMASK(15, 0);
33 rc = sja1105_xfer_u32(priv, SPI_READ, addr, &tmp, NULL);
40 int sja1105_pcs_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
42 struct sja1105_mdio_private *mdio_priv = bus->priv;
43 struct sja1105_private *priv = mdio_priv->priv;
48 if (!(reg & MII_ADDR_C45))
51 mmd = (reg >> MII_DEVADDR_C45_SHIFT) & 0x1f;
52 addr = (mmd << 16) | (reg & GENMASK(15, 0));
55 if (mmd != MDIO_MMD_VEND1 && mmd != MDIO_MMD_VEND2)
58 return sja1105_xfer_u32(priv, SPI_WRITE, addr, &tmp, NULL);
61 int sja1110_pcs_mdio_read(struct mii_bus *bus, int phy, int reg)
63 struct sja1105_mdio_private *mdio_priv = bus->priv;
64 struct sja1105_private *priv = mdio_priv->priv;
65 const struct sja1105_regs *regs = priv->info->regs;
72 if (!(reg & MII_ADDR_C45))
75 if (regs->pcs_base[phy] == SJA1105_RSV_ADDR)
78 mmd = (reg >> MII_DEVADDR_C45_SHIFT) & 0x1f;
79 addr = (mmd << 16) | (reg & GENMASK(15, 0));
81 if (mmd == MDIO_MMD_VEND2 && (reg & GENMASK(15, 0)) == MII_PHYSID1)
82 return NXP_SJA1110_XPCS_ID >> 16;
83 if (mmd == MDIO_MMD_VEND2 && (reg & GENMASK(15, 0)) == MII_PHYSID2)
84 return NXP_SJA1110_XPCS_ID & GENMASK(15, 0);
87 offset = addr & GENMASK(7, 0);
89 /* This addressing scheme reserves register 0xff for the bank address
90 * register, so that can never be addressed.
92 if (WARN_ON(offset == 0xff))
97 rc = sja1105_xfer_u32(priv, SPI_WRITE,
98 regs->pcs_base[phy] + SJA1110_PCS_BANK_REG,
103 rc = sja1105_xfer_u32(priv, SPI_READ, regs->pcs_base[phy] + offset,
111 int sja1110_pcs_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
113 struct sja1105_mdio_private *mdio_priv = bus->priv;
114 struct sja1105_private *priv = mdio_priv->priv;
115 const struct sja1105_regs *regs = priv->info->regs;
122 if (!(reg & MII_ADDR_C45))
125 if (regs->pcs_base[phy] == SJA1105_RSV_ADDR)
128 mmd = (reg >> MII_DEVADDR_C45_SHIFT) & 0x1f;
129 addr = (mmd << 16) | (reg & GENMASK(15, 0));
132 offset = addr & GENMASK(7, 0);
134 /* This addressing scheme reserves register 0xff for the bank address
135 * register, so that can never be addressed.
137 if (WARN_ON(offset == 0xff))
142 rc = sja1105_xfer_u32(priv, SPI_WRITE,
143 regs->pcs_base[phy] + SJA1110_PCS_BANK_REG,
150 return sja1105_xfer_u32(priv, SPI_WRITE, regs->pcs_base[phy] + offset,
154 enum sja1105_mdio_opcode {
155 SJA1105_C45_ADDR = 0,
157 SJA1105_C45_DATA = 2,
158 SJA1105_C45_DATA_AUTOINC = 3,
161 static u64 sja1105_base_t1_encode_addr(struct sja1105_private *priv,
162 int phy, enum sja1105_mdio_opcode op,
165 const struct sja1105_regs *regs = priv->info->regs;
167 return regs->mdio_100base_t1 | (phy << 7) | (op << 5) | (xad << 0);
170 static int sja1105_base_t1_mdio_read(struct mii_bus *bus, int phy, int reg)
172 struct sja1105_mdio_private *mdio_priv = bus->priv;
173 struct sja1105_private *priv = mdio_priv->priv;
178 if (reg & MII_ADDR_C45) {
179 u16 mmd = (reg >> MII_DEVADDR_C45_SHIFT) & 0x1f;
181 addr = sja1105_base_t1_encode_addr(priv, phy, SJA1105_C45_ADDR,
184 tmp = reg & MII_REGADDR_C45_MASK;
186 rc = sja1105_xfer_u32(priv, SPI_WRITE, addr, &tmp, NULL);
190 addr = sja1105_base_t1_encode_addr(priv, phy, SJA1105_C45_DATA,
193 rc = sja1105_xfer_u32(priv, SPI_READ, addr, &tmp, NULL);
201 addr = sja1105_base_t1_encode_addr(priv, phy, SJA1105_C22, reg & 0x1f);
203 rc = sja1105_xfer_u32(priv, SPI_READ, addr, &tmp, NULL);
210 static int sja1105_base_t1_mdio_write(struct mii_bus *bus, int phy, int reg,
213 struct sja1105_mdio_private *mdio_priv = bus->priv;
214 struct sja1105_private *priv = mdio_priv->priv;
219 if (reg & MII_ADDR_C45) {
220 u16 mmd = (reg >> MII_DEVADDR_C45_SHIFT) & 0x1f;
222 addr = sja1105_base_t1_encode_addr(priv, phy, SJA1105_C45_ADDR,
225 tmp = reg & MII_REGADDR_C45_MASK;
227 rc = sja1105_xfer_u32(priv, SPI_WRITE, addr, &tmp, NULL);
231 addr = sja1105_base_t1_encode_addr(priv, phy, SJA1105_C45_DATA,
236 rc = sja1105_xfer_u32(priv, SPI_WRITE, addr, &tmp, NULL);
243 /* Clause 22 write */
244 addr = sja1105_base_t1_encode_addr(priv, phy, SJA1105_C22, reg & 0x1f);
248 return sja1105_xfer_u32(priv, SPI_WRITE, addr, &tmp, NULL);
251 static int sja1105_base_tx_mdio_read(struct mii_bus *bus, int phy, int reg)
253 struct sja1105_mdio_private *mdio_priv = bus->priv;
254 struct sja1105_private *priv = mdio_priv->priv;
255 const struct sja1105_regs *regs = priv->info->regs;
259 rc = sja1105_xfer_u32(priv, SPI_READ, regs->mdio_100base_tx + reg,
267 static int sja1105_base_tx_mdio_write(struct mii_bus *bus, int phy, int reg,
270 struct sja1105_mdio_private *mdio_priv = bus->priv;
271 struct sja1105_private *priv = mdio_priv->priv;
272 const struct sja1105_regs *regs = priv->info->regs;
275 return sja1105_xfer_u32(priv, SPI_WRITE, regs->mdio_100base_tx + reg,
279 static int sja1105_mdiobus_base_tx_register(struct sja1105_private *priv,
280 struct device_node *mdio_node)
282 struct sja1105_mdio_private *mdio_priv;
283 struct device_node *np;
287 np = of_get_compatible_child(mdio_node, "nxp,sja1110-base-tx-mdio");
291 if (!of_device_is_available(np))
294 bus = mdiobus_alloc_size(sizeof(*mdio_priv));
300 bus->name = "SJA1110 100base-TX MDIO bus";
301 snprintf(bus->id, MII_BUS_ID_SIZE, "%s-base-tx",
302 dev_name(priv->ds->dev));
303 bus->read = sja1105_base_tx_mdio_read;
304 bus->write = sja1105_base_tx_mdio_write;
305 bus->parent = priv->ds->dev;
306 mdio_priv = bus->priv;
307 mdio_priv->priv = priv;
309 rc = of_mdiobus_register(bus, np);
315 priv->mdio_base_tx = bus;
323 static void sja1105_mdiobus_base_tx_unregister(struct sja1105_private *priv)
325 if (!priv->mdio_base_tx)
328 mdiobus_unregister(priv->mdio_base_tx);
329 mdiobus_free(priv->mdio_base_tx);
330 priv->mdio_base_tx = NULL;
333 static int sja1105_mdiobus_base_t1_register(struct sja1105_private *priv,
334 struct device_node *mdio_node)
336 struct sja1105_mdio_private *mdio_priv;
337 struct device_node *np;
341 np = of_get_compatible_child(mdio_node, "nxp,sja1110-base-t1-mdio");
345 if (!of_device_is_available(np))
348 bus = mdiobus_alloc_size(sizeof(*mdio_priv));
354 bus->name = "SJA1110 100base-T1 MDIO bus";
355 snprintf(bus->id, MII_BUS_ID_SIZE, "%s-base-t1",
356 dev_name(priv->ds->dev));
357 bus->read = sja1105_base_t1_mdio_read;
358 bus->write = sja1105_base_t1_mdio_write;
359 bus->parent = priv->ds->dev;
360 mdio_priv = bus->priv;
361 mdio_priv->priv = priv;
363 rc = of_mdiobus_register(bus, np);
369 priv->mdio_base_t1 = bus;
377 static void sja1105_mdiobus_base_t1_unregister(struct sja1105_private *priv)
379 if (!priv->mdio_base_t1)
382 mdiobus_unregister(priv->mdio_base_t1);
383 mdiobus_free(priv->mdio_base_t1);
384 priv->mdio_base_t1 = NULL;
387 static int sja1105_mdiobus_pcs_register(struct sja1105_private *priv)
389 struct sja1105_mdio_private *mdio_priv;
390 struct dsa_switch *ds = priv->ds;
395 if (!priv->info->pcs_mdio_read || !priv->info->pcs_mdio_write)
398 bus = mdiobus_alloc_size(sizeof(*mdio_priv));
402 bus->name = "SJA1105 PCS MDIO bus";
403 snprintf(bus->id, MII_BUS_ID_SIZE, "%s-pcs",
405 bus->read = priv->info->pcs_mdio_read;
406 bus->write = priv->info->pcs_mdio_write;
407 bus->parent = ds->dev;
408 /* There is no PHY on this MDIO bus => mask out all PHY addresses
412 mdio_priv = bus->priv;
413 mdio_priv->priv = priv;
415 rc = mdiobus_register(bus);
421 for (port = 0; port < ds->num_ports; port++) {
422 struct mdio_device *mdiodev;
423 struct dw_xpcs *xpcs;
425 if (dsa_is_unused_port(ds, port))
428 if (priv->phy_mode[port] != PHY_INTERFACE_MODE_SGMII &&
429 priv->phy_mode[port] != PHY_INTERFACE_MODE_2500BASEX)
432 mdiodev = mdio_device_create(bus, port);
433 if (IS_ERR(mdiodev)) {
434 rc = PTR_ERR(mdiodev);
438 xpcs = xpcs_create(mdiodev, priv->phy_mode[port]);
444 priv->xpcs[port] = xpcs;
447 priv->mdio_pcs = bus;
452 for (port = 0; port < ds->num_ports; port++) {
453 if (!priv->xpcs[port])
456 mdio_device_free(priv->xpcs[port]->mdiodev);
457 xpcs_destroy(priv->xpcs[port]);
458 priv->xpcs[port] = NULL;
461 mdiobus_unregister(bus);
467 static void sja1105_mdiobus_pcs_unregister(struct sja1105_private *priv)
469 struct dsa_switch *ds = priv->ds;
475 for (port = 0; port < ds->num_ports; port++) {
476 if (!priv->xpcs[port])
479 mdio_device_free(priv->xpcs[port]->mdiodev);
480 xpcs_destroy(priv->xpcs[port]);
481 priv->xpcs[port] = NULL;
484 mdiobus_unregister(priv->mdio_pcs);
485 mdiobus_free(priv->mdio_pcs);
486 priv->mdio_pcs = NULL;
489 int sja1105_mdiobus_register(struct dsa_switch *ds)
491 struct sja1105_private *priv = ds->priv;
492 const struct sja1105_regs *regs = priv->info->regs;
493 struct device_node *switch_node = ds->dev->of_node;
494 struct device_node *mdio_node;
497 rc = sja1105_mdiobus_pcs_register(priv);
501 mdio_node = of_get_child_by_name(switch_node, "mdios");
505 if (!of_device_is_available(mdio_node))
506 goto out_put_mdio_node;
508 if (regs->mdio_100base_tx != SJA1105_RSV_ADDR) {
509 rc = sja1105_mdiobus_base_tx_register(priv, mdio_node);
511 goto err_put_mdio_node;
514 if (regs->mdio_100base_t1 != SJA1105_RSV_ADDR) {
515 rc = sja1105_mdiobus_base_t1_register(priv, mdio_node);
517 goto err_free_base_tx_mdiobus;
521 of_node_put(mdio_node);
525 err_free_base_tx_mdiobus:
526 sja1105_mdiobus_base_tx_unregister(priv);
528 of_node_put(mdio_node);
529 sja1105_mdiobus_pcs_unregister(priv);
534 void sja1105_mdiobus_unregister(struct dsa_switch *ds)
536 struct sja1105_private *priv = ds->priv;
538 sja1105_mdiobus_base_t1_unregister(priv);
539 sja1105_mdiobus_base_tx_unregister(priv);
540 sja1105_mdiobus_pcs_unregister(priv);