Skip to contentSkip to conversation

net: dsa: microchip: ksz9477: enable interrupt for internal phy link detection

PATCH Superseded Arun Ramadoss 2 participants 8 messages Sep 14, 2022 – Sep 15, 2022

Subsystems (5)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover net: dsa: microchip: ksz9477: enable interrupt for internal phy link detection

This patch series implements the common interrupt handling for ksz9477 based switches and lan937x. The ksz9477 and lan937x has similar interrupt registers except ksz9477 has 4 port based interrupts whereas lan937x has 6 interrupts. The patch moves the phy interrupt hanler implemented in lan937x_main.c to ksz_common.c, along with the mdio_register functionality.

v1 -> v2
- Added the .port_nirqs = 2 for ksz9896

Changes in RFC -> v1
- modified the return -ENODEV to 0 if mdio node not present

Arun Ramadoss (5):
net: dsa: microchip: determine number of port irq based on switch type
net: dsa: microchip: enable phy interrupts only if interrupt enabled
in dts
net: dsa: microchip: lan937x: return zero if mdio node not present
net: dsa: microchip: move interrupt handling logic from lan937x to
ksz_common
net: phy: micrel: enable interrupt for ksz9477 phy

 drivers/net/dsa/microchip/ksz_common.c   | 436 +++++++++++++++++++++++
 drivers/net/dsa/microchip/ksz_common.h   |  10 +
 drivers/net/dsa/microchip/lan937x_main.c | 425 ----------------------
 drivers/net/phy/micrel.c                 |   2 +
 4 files changed, 448 insertions(+), 425 deletions(-)

base-commit: c9ae520ac3faf2f272b5705b085b3778c7997ec8

Signature · 1 line
2.36.1
#1 lore raw

PATCH v2 1/5 net: dsa: microchip: determine number of port irq based on switch type

Currently the number of port irqs is hard coded for the lan937x switch as 6. In order to make the generic interrupt handler for ksz switches, number of port irq supported by the switch is added to the ksz_chip_data. It is 4 for ksz9477, 2 for ksz9897/9896 and 3 for ksz9567.

---
 drivers/net/dsa/microchip/ksz_common.c   | 10 ++++++++++
 drivers/net/dsa/microchip/ksz_common.h   |  1 +
 drivers/net/dsa/microchip/lan937x_main.c |  4 +---
 3 files changed, 12 insertions(+), 3 deletions(-)
drivers/net/dsa/microchip/ksz_common.c +10 -0
index fcaa71f66322..07283279c578 100644
@@ -1168,6 +1168,7 @@ const struct ksz_chip_data ksz_switch_chips[] = {
 		.num_statics = 16,
 		.cpu_ports = 0x7F,	/* can be configured as cpu port */
 		.port_cnt = 7,		/* total physical port count */
+		.port_nirqs = 4,
 		.ops = &ksz9477_dev_ops,
 		.phy_errata_9477 = true,
 		.mib_names = ksz9477_mib_names,
@@ -1199,6 +1200,7 @@ const struct ksz_chip_data ksz_switch_chips[] = {
 		.num_statics = 16,
 		.cpu_ports = 0x3F,	/* can be configured as cpu port */
 		.port_cnt = 6,		/* total physical port count */
+		.port_nirqs = 2,
 		.ops = &ksz9477_dev_ops,
 		.phy_errata_9477 = true,
 		.mib_names = ksz9477_mib_names,
@@ -1230,6 +1232,7 @@ const struct ksz_chip_data ksz_switch_chips[] = {
 		.num_statics = 16,
 		.cpu_ports = 0x7F,	/* can be configured as cpu port */
 		.port_cnt = 7,		/* total physical port count */
+		.port_nirqs = 2,
 		.ops = &ksz9477_dev_ops,
 		.phy_errata_9477 = true,
 		.mib_names = ksz9477_mib_names,
@@ -1259,6 +1262,7 @@ const struct ksz_chip_data ksz_switch_chips[] = {
 		.num_statics = 16,
 		.cpu_ports = 0x07,	/* can be configured as cpu port */
 		.port_cnt = 3,		/* total port count */
+		.port_nirqs = 2,
 		.ops = &ksz9477_dev_ops,
 		.mib_names = ksz9477_mib_names,
 		.mib_cnt = ARRAY_SIZE(ksz9477_mib_names),
@@ -1283,6 +1287,7 @@ const struct ksz_chip_data ksz_switch_chips[] = {
 		.num_statics = 16,
 		.cpu_ports = 0x7F,	/* can be configured as cpu port */
 		.port_cnt = 7,		/* total physical port count */
+		.port_nirqs = 3,
 		.ops = &ksz9477_dev_ops,
 		.phy_errata_9477 = true,
 		.mib_names = ksz9477_mib_names,
@@ -1312,6 +1317,7 @@ const struct ksz_chip_data ksz_switch_chips[] = {
 		.num_statics = 256,
 		.cpu_ports = 0x10,	/* can be configured as cpu port */
 		.port_cnt = 5,		/* total physical port count */
+		.port_nirqs = 6,
 		.ops = &lan937x_dev_ops,
 		.mib_names = ksz9477_mib_names,
 		.mib_cnt = ARRAY_SIZE(ksz9477_mib_names),
@@ -1335,6 +1341,7 @@ const struct ksz_chip_data ksz_switch_chips[] = {
 		.num_statics = 256,
 		.cpu_ports = 0x30,	/* can be configured as cpu port */
 		.port_cnt = 6,		/* total physical port count */
+		.port_nirqs = 6,
 		.ops = &lan937x_dev_ops,
 		.mib_names = ksz9477_mib_names,
 		.mib_cnt = ARRAY_SIZE(ksz9477_mib_names),
@@ -1358,6 +1365,7 @@ const struct ksz_chip_data ksz_switch_chips[] = {
 		.num_statics = 256,
 		.cpu_ports = 0x30,	/* can be configured as cpu port */
 		.port_cnt = 8,		/* total physical port count */
+		.port_nirqs = 6,
 		.ops = &lan937x_dev_ops,
 		.mib_names = ksz9477_mib_names,
 		.mib_cnt = ARRAY_SIZE(ksz9477_mib_names),
@@ -1385,6 +1393,7 @@ const struct ksz_chip_data ksz_switch_chips[] = {
 		.num_statics = 256,
 		.cpu_ports = 0x38,	/* can be configured as cpu port */
 		.port_cnt = 5,		/* total physical port count */
+		.port_nirqs = 6,
 		.ops = &lan937x_dev_ops,
 		.mib_names = ksz9477_mib_names,
 		.mib_cnt = ARRAY_SIZE(ksz9477_mib_names),
@@ -1412,6 +1421,7 @@ const struct ksz_chip_data ksz_switch_chips[] = {
 		.num_statics = 256,
 		.cpu_ports = 0x30,	/* can be configured as cpu port */
 		.port_cnt = 8,		/* total physical port count */
+		.port_nirqs = 6,
 		.ops = &lan937x_dev_ops,
 		.mib_names = ksz9477_mib_names,
 		.mib_cnt = ARRAY_SIZE(ksz9477_mib_names),
drivers/net/dsa/microchip/ksz_common.h +1 -0
index 6203dcd8c8f7..baa1e1bc1b7c 100644
@@ -45,6 +45,7 @@ struct ksz_chip_data {
 	int num_statics;
 	int cpu_ports;
 	int port_cnt;
+	u8 port_nirqs;
 	const struct ksz_dev_ops *ops;
 	bool phy_errata_9477;
 	bool ksz87xx_eee_link_erratum;
drivers/net/dsa/microchip/lan937x_main.c +1 -3
index 9b6760b1e572..7136d9c55315 100644
@@ -20,8 +20,6 @@
 #include "ksz_common.h"
 #include "lan937x.h"
 
-#define LAN937x_PNIRQS 6
-
 static int lan937x_cfg(struct ksz_device *dev, u32 addr, u8 bits, bool set)
 {
 	return regmap_update_bits(dev->regmap[0], addr, bits, set ? bits : 0);
@@ -697,7 +695,7 @@ static int lan937x_pirq_setup(struct ksz_device *dev, u8 p)
 	int ret, irq;
 	int irq_num;
 
-	port->pirq.nirqs = LAN937x_PNIRQS;
+	port->pirq.nirqs = dev->info->port_nirqs;
 	port->pirq.domain = irq_domain_add_simple(dev->dev->of_node,
 						  port->pirq.nirqs, 0,
 						  &lan937x_pirq_domain_ops,
Signature · 1 line
2.36.1
#2 lore raw

PATCH v2 2/5 net: dsa: microchip: enable phy interrupts only if interrupt enabled in dts

In reply to Arun Ramadoss

In the lan937x_mdio_register function, phy interrupts are enabled irrespective of irq is enabled in the switch. Now, the check is added to enable the phy interrupt only if the irq is enabled in the switch.

---
 drivers/net/dsa/microchip/lan937x_main.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/net/dsa/microchip/lan937x_main.c +7 -4
index 7136d9c55315..1f4472c90a1f 100644
@@ -235,15 +235,18 @@ static int lan937x_mdio_register(struct ksz_device *dev)
 
 	ds->slave_mii_bus = bus;
 
-	ret = lan937x_irq_phy_setup(dev);
-	if (ret)
-		return ret;
+	if (dev->irq > 0) {
+		ret = lan937x_irq_phy_setup(dev);
+		if (ret)
+			return ret;
+	}
 
 	ret = devm_of_mdiobus_register(ds->dev, bus, mdio_np);
 	if (ret) {
 		dev_err(ds->dev, "unable to register MDIO bus %s\n",
 			bus->id);
-		lan937x_irq_phy_free(dev);
+		if (dev->irq > 0)
+			lan937x_irq_phy_free(dev);
 	}
 
 	of_node_put(mdio_np);
Signature · 1 line
2.36.1
#3 lore raw

PATCH v2 3/5 net: dsa: microchip: lan937x: return zero if mdio node not present

In reply to Arun Ramadoss

Currently, if the mdio node is not present in the dts file then lan937x_mdio_register return -ENODEV and entire probing process fails. To make the mdio_register generic for all ksz series switches and to maintain back-compatibility with existing dts file, return -ENODEV is replaced with return 0.

---
 drivers/net/dsa/microchip/lan937x_main.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/net/dsa/microchip/lan937x_main.c +2 -4
index 1f4472c90a1f..36acef385de3 100644
@@ -214,10 +214,8 @@ static int lan937x_mdio_register(struct ksz_device *dev)
 	int ret;
 
 	mdio_np = of_get_child_by_name(dev->dev->of_node, "mdio");
-	if (!mdio_np) {
-		dev_err(ds->dev, "no MDIO bus node\n");
-		return -ENODEV;
-	}
+	if (!mdio_np)
+		return 0;
 
 	bus = devm_mdiobus_alloc(ds->dev);
 	if (!bus) {
Signature · 1 line
2.36.1
#4 lore raw

PATCH v2 4/5 net: dsa: microchip: move interrupt handling logic from lan937x to ksz_common

In reply to Arun Ramadoss

To support the phy link detection through interrupt method for ksz9477 based switch, the interrupt handling routines are moved from lan937x_main.c to ksz_common.c. The only changes made are functions names are prefixed with ksz_ instead of lan937x_.

---
 drivers/net/dsa/microchip/ksz_common.c   | 426 +++++++++++++++++++++++
 drivers/net/dsa/microchip/ksz_common.h   |   9 +
 drivers/net/dsa/microchip/lan937x_main.c | 424 ----------------------
 3 files changed, 435 insertions(+), 424 deletions(-)
drivers/net/dsa/microchip/ksz_common.c +426 -0
index 07283279c578..c041d9f5f3d7 100644
@@ -14,6 +14,9 @@
 #include <linux/phy.h>
 #include <linux/etherdevice.h>
 #include <linux/if_bridge.h>
+#include <linux/irq.h>
+#include <linux/irqdomain.h>
+#include <linux/of_mdio.h>
 #include <linux/of_device.h>
 #include <linux/of_net.h>
 #include <linux/micrel_phy.h>
@@ -1653,9 +1656,396 @@ static void ksz_update_port_member(struct ksz_device *dev, int port)
 	dev->dev_ops->cfg_port_member(dev, port, port_member | cpu_port);
 }
 
+static int ksz_sw_mdio_read(struct mii_bus *bus, int addr, int regnum)
+{
+	struct ksz_device *dev = bus->priv;
+	u16 val;
+	int ret;
+
+	if (regnum & MII_ADDR_C45)
+		return -EOPNOTSUPP;
+
+	ret = dev->dev_ops->r_phy(dev, addr, regnum, &val);
+	if (ret < 0)
+		return ret;
+
+	return val;
+}
+
+static int ksz_sw_mdio_write(struct mii_bus *bus, int addr, int regnum,
+			     u16 val)
+{
+	struct ksz_device *dev = bus->priv;
+
+	if (regnum & MII_ADDR_C45)
+		return -EOPNOTSUPP;
+
+	return dev->dev_ops->w_phy(dev, addr, regnum, val);
+}
+
+static int ksz_irq_phy_setup(struct ksz_device *dev)
+{
+	struct dsa_switch *ds = dev->ds;
+	int phy, err_phy;
+	int irq;
+	int ret;
+
+	for (phy = 0; phy < KSZ_MAX_NUM_PORTS; phy++) {
+		if (BIT(phy) & ds->phys_mii_mask) {
+			irq = irq_find_mapping(dev->ports[phy].pirq.domain,
+					       PORT_SRC_PHY_INT);
+			if (irq < 0) {
+				ret = irq;
+				goto out;
+			}
+			ds->slave_mii_bus->irq[phy] = irq;
+		}
+	}
+	return 0;
+out:
+	err_phy = phy;
+
+	for (phy = 0; phy < err_phy; phy++)
+		if (BIT(phy) & ds->phys_mii_mask)
+			irq_dispose_mapping(ds->slave_mii_bus->irq[phy]);
+
+	return ret;
+}
+
+static void ksz_irq_phy_free(struct ksz_device *dev)
+{
+	struct dsa_switch *ds = dev->ds;
+	int phy;
+
+	for (phy = 0; phy < KSZ_MAX_NUM_PORTS; phy++)
+		if (BIT(phy) & ds->phys_mii_mask)
+			irq_dispose_mapping(ds->slave_mii_bus->irq[phy]);
+}
+
+static int ksz_mdio_register(struct ksz_device *dev)
+{
+	struct dsa_switch *ds = dev->ds;
+	struct device_node *mdio_np;
+	struct mii_bus *bus;
+	int ret;
+
+	mdio_np = of_get_child_by_name(dev->dev->of_node, "mdio");
+	if (!mdio_np)
+		return 0;
+
+	bus = devm_mdiobus_alloc(ds->dev);
+	if (!bus) {
+		of_node_put(mdio_np);
+		return -ENOMEM;
+	}
+
+	bus->priv = dev;
+	bus->read = ksz_sw_mdio_read;
+	bus->write = ksz_sw_mdio_write;
+	bus->name = "ksz slave smi";
+	snprintf(bus->id, MII_BUS_ID_SIZE, "SMI-%d", ds->index);
+	bus->parent = ds->dev;
+	bus->phy_mask = ~ds->phys_mii_mask;
+
+	ds->slave_mii_bus = bus;
+
+	if (dev->irq > 0) {
+		ret = ksz_irq_phy_setup(dev);
+		if (ret) {
+			of_node_put(mdio_np);
+			return ret;
+		}
+	}
+
+	ret = devm_of_mdiobus_register(ds->dev, bus, mdio_np);
+	if (ret) {
+		dev_err(ds->dev, "unable to register MDIO bus %s\n",
+			bus->id);
+		if (dev->irq > 0)
+			ksz_irq_phy_free(dev);
+	}
+
+	of_node_put(mdio_np);
+
+	return ret;
+}
+
+static void ksz_girq_mask(struct irq_data *d)
+{
+	struct ksz_device *dev = irq_data_get_irq_chip_data(d);
+	unsigned int n = d->hwirq;
+
+	dev->girq.masked |= (1 << n);
+}
+
+static void ksz_girq_unmask(struct irq_data *d)
+{
+	struct ksz_device *dev = irq_data_get_irq_chip_data(d);
+	unsigned int n = d->hwirq;
+
+	dev->girq.masked &= ~(1 << n);
+}
+
+static void ksz_girq_bus_lock(struct irq_data *d)
+{
+	struct ksz_device *dev = irq_data_get_irq_chip_data(d);
+
+	mutex_lock(&dev->lock_irq);
+}
+
+static void ksz_girq_bus_sync_unlock(struct irq_data *d)
+{
+	struct ksz_device *dev = irq_data_get_irq_chip_data(d);
+	int ret;
+
+	ret = ksz_write32(dev, REG_SW_PORT_INT_MASK__4, dev->girq.masked);
+	if (ret)
+		dev_err(dev->dev, "failed to change IRQ mask\n");
+
+	mutex_unlock(&dev->lock_irq);
+}
+
+static const struct irq_chip ksz_girq_chip = {
+	.name			= "ksz-global",
+	.irq_mask		= ksz_girq_mask,
+	.irq_unmask		= ksz_girq_unmask,
+	.irq_bus_lock		= ksz_girq_bus_lock,
+	.irq_bus_sync_unlock	= ksz_girq_bus_sync_unlock,
+};
+
+static int ksz_girq_domain_map(struct irq_domain *d,
+			       unsigned int irq, irq_hw_number_t hwirq)
+{
+	struct ksz_device *dev = d->host_data;
+
+	irq_set_chip_data(irq, d->host_data);
+	irq_set_chip_and_handler(irq, &dev->girq.chip, handle_level_irq);
+	irq_set_noprobe(irq);
+
+	return 0;
+}
+
+static const struct irq_domain_ops ksz_girq_domain_ops = {
+	.map	= ksz_girq_domain_map,
+	.xlate	= irq_domain_xlate_twocell,
+};
+
+static void ksz_girq_free(struct ksz_device *dev)
+{
+	int irq, virq;
+
+	free_irq(dev->irq, dev);
+
+	for (irq = 0; irq < dev->girq.nirqs; irq++) {
+		virq = irq_find_mapping(dev->girq.domain, irq);
+		irq_dispose_mapping(virq);
+	}
+
+	irq_domain_remove(dev->girq.domain);
+}
+
+static irqreturn_t ksz_girq_thread_fn(int irq, void *dev_id)
+{
+	struct ksz_device *dev = dev_id;
+	unsigned int nhandled = 0;
+	unsigned int sub_irq;
+	unsigned int n;
+	u32 data;
+	int ret;
+
+	/* Read global interrupt status register */
+	ret = ksz_read32(dev, REG_SW_PORT_INT_STATUS__4, &data);
+	if (ret)
+		goto out;
+
+	for (n = 0; n < dev->girq.nirqs; ++n) {
+		if (data & (1 << n)) {
+			sub_irq = irq_find_mapping(dev->girq.domain, n);
+			handle_nested_irq(sub_irq);
+			++nhandled;
+		}
+	}
+out:
+	return (nhandled > 0 ? IRQ_HANDLED : IRQ_NONE);
+}
+
+static int ksz_girq_setup(struct ksz_device *dev)
+{
+	int ret, irq;
+
+	dev->girq.nirqs = dev->info->port_cnt;
+	dev->girq.domain = irq_domain_add_simple(NULL, dev->girq.nirqs, 0,
+						 &ksz_girq_domain_ops, dev);
+	if (!dev->girq.domain)
+		return -ENOMEM;
+
+	for (irq = 0; irq < dev->girq.nirqs; irq++)
+		irq_create_mapping(dev->girq.domain, irq);
+
+	dev->girq.chip = ksz_girq_chip;
+	dev->girq.masked = ~0;
+
+	ret = request_threaded_irq(dev->irq, NULL, ksz_girq_thread_fn,
+				   IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
+				   dev_name(dev->dev), dev);
+	if (ret)
+		goto out;
+
+	return 0;
+
+out:
+	ksz_girq_free(dev);
+
+	return ret;
+}
+
+static void ksz_pirq_mask(struct irq_data *d)
+{
+	struct ksz_port *port = irq_data_get_irq_chip_data(d);
+	unsigned int n = d->hwirq;
+
+	port->pirq.masked |= (1 << n);
+}
+
+static void ksz_pirq_unmask(struct irq_data *d)
+{
+	struct ksz_port *port = irq_data_get_irq_chip_data(d);
+	unsigned int n = d->hwirq;
+
+	port->pirq.masked &= ~(1 << n);
+}
+
+static void ksz_pirq_bus_lock(struct irq_data *d)
+{
+	struct ksz_port *port = irq_data_get_irq_chip_data(d);
+	struct ksz_device *dev = port->ksz_dev;
+
+	mutex_lock(&dev->lock_irq);
+}
+
+static void ksz_pirq_bus_sync_unlock(struct irq_data *d)
+{
+	struct ksz_port *port = irq_data_get_irq_chip_data(d);
+	struct ksz_device *dev = port->ksz_dev;
+
+	ksz_pwrite8(dev, port->num, REG_PORT_INT_MASK, port->pirq.masked);
+	mutex_unlock(&dev->lock_irq);
+}
+
+static const struct irq_chip ksz_pirq_chip = {
+	.name			= "ksz-port",
+	.irq_mask		= ksz_pirq_mask,
+	.irq_unmask		= ksz_pirq_unmask,
+	.irq_bus_lock		= ksz_pirq_bus_lock,
+	.irq_bus_sync_unlock	= ksz_pirq_bus_sync_unlock,
+};
+
+static int ksz_pirq_domain_map(struct irq_domain *d, unsigned int irq,
+			       irq_hw_number_t hwirq)
+{
+	struct ksz_port *port = d->host_data;
+
+	irq_set_chip_data(irq, d->host_data);
+	irq_set_chip_and_handler(irq, &port->pirq.chip, handle_level_irq);
+	irq_set_noprobe(irq);
+
+	return 0;
+}
+
+static const struct irq_domain_ops ksz_pirq_domain_ops = {
+	.map	= ksz_pirq_domain_map,
+	.xlate	= irq_domain_xlate_twocell,
+};
+
+static void ksz_pirq_free(struct ksz_device *dev, u8 p)
+{
+	struct ksz_port *port = &dev->ports[p];
+	int irq, virq;
+	int irq_num;
+
+	irq_num = irq_find_mapping(dev->girq.domain, p);
+	if (irq_num < 0)
+		return;
+
+	free_irq(irq_num, port);
+
+	for (irq = 0; irq < port->pirq.nirqs; irq++) {
+		virq = irq_find_mapping(port->pirq.domain, irq);
+		irq_dispose_mapping(virq);
+	}
+
+	irq_domain_remove(port->pirq.domain);
+}
+
+static irqreturn_t ksz_pirq_thread_fn(int irq, void *dev_id)
+{
+	struct ksz_port *port = dev_id;
+	unsigned int nhandled = 0;
+	struct ksz_device *dev;
+	unsigned int sub_irq;
+	unsigned int n;
+	u8 data;
+
+	dev = port->ksz_dev;
+
+	/* Read port interrupt status register */
+	ksz_pread8(dev, port->num, REG_PORT_INT_STATUS, &data);
+
+	for (n = 0; n < port->pirq.nirqs; ++n) {
+		if (data & (1 << n)) {
+			sub_irq = irq_find_mapping(port->pirq.domain, n);
+			handle_nested_irq(sub_irq);
+			++nhandled;
+		}
+	}
+
+	return (nhandled > 0 ? IRQ_HANDLED : IRQ_NONE);
+}
+
+static int ksz_pirq_setup(struct ksz_device *dev, u8 p)
+{
+	struct ksz_port *port = &dev->ports[p];
+	int ret, irq;
+	int irq_num;
+
+	port->pirq.nirqs = dev->info->port_nirqs;
+	port->pirq.domain = irq_domain_add_simple(dev->dev->of_node,
+						  port->pirq.nirqs, 0,
+						  &ksz_pirq_domain_ops,
+						  port);
+	if (!port->pirq.domain)
+		return -ENOMEM;
+
+	for (irq = 0; irq < port->pirq.nirqs; irq++)
+		irq_create_mapping(port->pirq.domain, irq);
+
+	port->pirq.chip = ksz_pirq_chip;
+	port->pirq.masked = ~0;
+
+	irq_num = irq_find_mapping(dev->girq.domain, p);
+	if (irq_num < 0)
+		return irq_num;
+
+	snprintf(port->pirq.name, sizeof(port->pirq.name), "port_irq-%d", p);
+
+	ret = request_threaded_irq(irq_num, NULL, ksz_pirq_thread_fn,
+				   IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
+				   port->pirq.name, port);
+	if (ret)
+		goto out;
+
+	return 0;
+
+out:
+	ksz_pirq_free(dev, p);
+
+	return ret;
+}
+
 static int ksz_setup(struct dsa_switch *ds)
 {
 	struct ksz_device *dev = ds->priv;
+	struct dsa_port *dp;
 	struct ksz_port *p;
 	const u16 *regs;
 	int ret;
@@ -1704,16 +2094,52 @@ static int ksz_setup(struct dsa_switch *ds)
 	p = &dev->ports[dev->cpu_port];
 	p->learning = true;
 
+	if (dev->irq > 0) {
+		ret = ksz_girq_setup(dev);
+		if (ret)
+			return ret;
+
+		dsa_switch_for_each_user_port(dp, dev->ds) {
+			ret = ksz_pirq_setup(dev, dp->index);
+			if (ret)
+				goto out_girq;
+		}
+	}
+
+	ret = ksz_mdio_register(dev);
+	if (ret < 0) {
+		dev_err(dev->dev, "failed to register the mdio");
+		goto out_pirq;
+	}
+
 	/* start switch */
 	regmap_update_bits(dev->regmap[0], regs[S_START_CTRL],
 			   SW_START, SW_START);
 
 	return 0;
+
+out_pirq:
+	if (dev->irq > 0)
+		dsa_switch_for_each_user_port(dp, dev->ds)
+			ksz_pirq_free(dev, dp->index);
+out_girq:
+	if (dev->irq > 0)
+		ksz_girq_free(dev);
+
+	return ret;
 }
 
 static void ksz_teardown(struct dsa_switch *ds)
 {
 	struct ksz_device *dev = ds->priv;
+	struct dsa_port *dp;
+
+	if (dev->irq > 0) {
+		dsa_switch_for_each_user_port(dp, dev->ds)
+			ksz_pirq_free(dev, dp->index);
+
+		ksz_girq_free(dev);
+	}
 
 	if (dev->dev_ops->teardown)
 		dev->dev_ops->teardown(ds);
drivers/net/dsa/microchip/ksz_common.h +9 -0
index baa1e1bc1b7c..6edce587bfd2 100644
@@ -573,6 +573,15 @@ static inline int is_lan937x(struct ksz_device *dev)
 #define P_MII_MAC_MODE			BIT(2)
 #define P_MII_SEL_M			0x3
 
+/* Interrupt */
+#define REG_SW_PORT_INT_STATUS__4	0x0018
+#define REG_SW_PORT_INT_MASK__4		0x001C
+
+#define REG_PORT_INT_STATUS		0x001B
+#define REG_PORT_INT_MASK		0x001F
+
+#define PORT_SRC_PHY_INT		1
+
 /* Regmap tables generation */
 #define KSZ_SPI_OP_RD		3
 #define KSZ_SPI_OP_WR		2
drivers/net/dsa/microchip/lan937x_main.c +0 -424

464 lines not on this page · raw message

Signature · 1 line
2.36.1
#5 lore raw

Re: [Patch net-next v2 4/5] net: dsa: microchip: move interrupt handling logic from lan937x to ksz_common

Hi,

Some suggestions for a few improvements in a future patch:

Quoted 15 lines · Arun Ramadoss
drivers/net/dsa/microchip/ksz_common.c +15 -0
+static int ksz_irq_phy_setup(struct ksz_device *dev)
+{
+	struct dsa_switch *ds = dev->ds;
+	int phy, err_phy;
+	int irq;
+	int ret;
+
+	for (phy = 0; phy < KSZ_MAX_NUM_PORTS; phy++) {
+		if (BIT(phy) & ds->phys_mii_mask) {
+			irq = irq_find_mapping(dev->ports[phy].pirq.domain,
+					       PORT_SRC_PHY_INT);
+			if (irq < 0) {
+				ret = irq;
+				goto out;
+			}

drivers/net/dsa/microchip/ksz_common.c static void ksz_update_port_member(struct ksz_device *dev, int port) · line 1710

Quoted 10 lines
drivers/net/dsa/microchip/ksz_common.c +10 -0
+			ds->slave_mii_bus->irq[phy] = irq;
+		}
+	}
+	return 0;
+out:
+	err_phy = phy;
+
+	for (phy = 0; phy < err_phy; phy++)
+		if (BIT(phy) & ds->phys_mii_mask)
+			irq_dispose_mapping(ds->slave_mii_bus->irq[phy]);
	while (phy--)
		if (BIT(phy) & ds->phys_mii_mask)
			irq_dispose_mapping(ds->slave_mii_bus->irq[phy]);

?

drivers/net/dsa/microchip/ksz_common.c static void ksz_update_port_member(struct ksz_device *dev, int port) · line 1778

Quoted 6 lines
drivers/net/dsa/microchip/ksz_common.c +6 -0
+static void ksz_girq_mask(struct irq_data *d)
+{
+	struct ksz_device *dev = irq_data_get_irq_chip_data(d);
+	unsigned int n = d->hwirq;
+
+	dev->girq.masked |= (1 << n);
	dev->girq.masked |= BIT(d->hwirq);

?

drivers/net/dsa/microchip/ksz_common.c static void ksz_update_port_member(struct ksz_device *dev, int port) · line 1786

Quoted 8 lines
drivers/net/dsa/microchip/ksz_common.c +8 -0
+}
+
+static void ksz_girq_unmask(struct irq_data *d)
+{
+	struct ksz_device *dev = irq_data_get_irq_chip_data(d);
+	unsigned int n = d->hwirq;
+
+	dev->girq.masked &= ~(1 << n);
	dev->girq.masked &= ~BIT(d->hw_irq);

?

Quoted 18 lines
drivers/net/dsa/microchip/ksz_common.c +18 -0
+}
+
+static void ksz_girq_bus_lock(struct irq_data *d)
+{
+	struct ksz_device *dev = irq_data_get_irq_chip_data(d);
+
+	mutex_lock(&dev->lock_irq);
+}
+
+static void ksz_girq_bus_sync_unlock(struct irq_data *d)
+{
+	struct ksz_device *dev = irq_data_get_irq_chip_data(d);
+	int ret;
+
+	ret = ksz_write32(dev, REG_SW_PORT_INT_MASK__4, dev->girq.masked);
+	if (ret)
+		dev_err(dev->dev, "failed to change IRQ mask\n");
+

drivers/net/dsa/microchip/ksz_common.c static void ksz_update_port_member(struct ksz_device *dev, int port) · line 1814

Quoted 10 lines
drivers/net/dsa/microchip/ksz_common.c +10 -0
+	mutex_unlock(&dev->lock_irq);
+}
+
+static const struct irq_chip ksz_girq_chip = {
+	.name			= "ksz-global",
+	.irq_mask		= ksz_girq_mask,
+	.irq_unmask		= ksz_girq_unmask,
+	.irq_bus_lock		= ksz_girq_bus_lock,
+	.irq_bus_sync_unlock	= ksz_girq_bus_sync_unlock,
+};

As the pirq code is almost identical to the girq code, how about putting a "reg_mask", "reg_status" and a pointer to ksz_device into ksz_irq, and using the ksz_irq as the chip data?

These would then become:

static void ksz_irq_mask(struct irq_data *d)

{
	struct ksz_irq *ki = irq_data_get_irq_chip_data(d);

	ki->masked |= BIT(d->hwirq);
}

static void ksz_irq_unmask(struct irq_data *d)

{
	struct ksz_irq *ki = irq_data_get_irq_chip_data(d);

	ki->masked &= ~BIT(d->hwirq);
}

static void ksz_irq_bus_lock(struct irq_data *d)

{
	struct ksz_irq *ki = irq_data_get_irq_chip_data(d);

	mutex_lock(&ki->dev->lock_irq);
}

static void ksz_irq_bus_sync_unlock(struct irq_data *d)

{
	struct ksz_irq *ki = irq_data_get_irq_chip_data(d);
	struct ksz_device *dev = ki->dev;
	int ret;

	ret = ksz_write32(dev, ki->reg_masked, ki->masked);
	if (ret)
		dev_err(dev->dev, "failed to change IRQ mask\n");

	mutex_unlock(&dev->lock_irq);
}

and thus this code could be shared between both pirq and girq. I'm pretty sure the thead_fn could be shared as well, and I'm sure that the setup and tear down could be improved in a similar way.

Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last!
#6 lore raw

Re: [Patch net-next v2 4/5] net: dsa: microchip: move interrupt handling logic from lan937x to ksz_common

Hi Russel,
Thanks for the comment.

Quoted 34 lines · Russell King (Oracle)

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

Hi,

Some suggestions for a few improvements in a future patch:

Quoted 26 lines · Arun Ramadoss
drivers/net/dsa/microchip/ksz_common.c +10 -0
+static int ksz_irq_phy_setup(struct ksz_device *dev)
+{
+     struct dsa_switch *ds = dev->ds;
+     int phy, err_phy;
+     int irq;
+     int ret;
+
+     for (phy = 0; phy < KSZ_MAX_NUM_PORTS; phy++) {
+             if (BIT(phy) & ds->phys_mii_mask) {
+                     irq = irq_find_mapping(dev-
Quoted 1 line

ports[phy].pirq.domain,

drivers/net/dsa/microchip/ksz_common.c +15 -0
+                                            PORT_SRC_PHY_INT);
+                     if (irq < 0) {
+                             ret = irq;
+                             goto out;
+                     }
+                     ds->slave_mii_bus->irq[phy] = irq;
+             }
+     }
+     return 0;
+out:
+     err_phy = phy;
+
+     for (phy = 0; phy < err_phy; phy++)
+             if (BIT(phy) & ds->phys_mii_mask)
+                     irq_dispose_mapping(ds->slave_mii_bus-
Quoted 6 lines
Quoted 1 line
Quoted 1 line

irq[phy]);

        while (phy--)
                if (BIT(phy) & ds->phys_mii_mask)
                        irq_dispose_mapping(ds->slave_mii_bus-
Quoted 1 line

irq[phy]);

Ok. I will update.

Quoted 14 lines

?

drivers/net/dsa/microchip/ksz_common.c static void ksz_update_port_member(struct ksz_device *dev, int port) · line 1778

Quoted 6 lines
drivers/net/dsa/microchip/ksz_common.c +6 -0
+static void ksz_girq_mask(struct irq_data *d)
+{
+     struct ksz_device *dev = irq_data_get_irq_chip_data(d);
+     unsigned int n = d->hwirq;
+
+     dev->girq.masked |= (1 << n);
        dev->girq.masked |= BIT(d->hwirq);

?

Quoted 10 lines

drivers/net/dsa/microchip/ksz_common.c static void ksz_update_port_member(struct ksz_device *dev, int port) · line 1786

Quoted 8 lines
drivers/net/dsa/microchip/ksz_common.c +8 -0
+}
+
+static void ksz_girq_unmask(struct irq_data *d)
+{
+     struct ksz_device *dev = irq_data_get_irq_chip_data(d);
+     unsigned int n = d->hwirq;
+
+     dev->girq.masked &= ~(1 << n);
        dev->girq.masked &= ~BIT(d->hw_irq);

Ok. I will replace them with macros.

Quoted 75 lines

?

Quoted 19 lines
drivers/net/dsa/microchip/ksz_common.c +15 -0
+}
+
+static void ksz_girq_bus_lock(struct irq_data *d)
+{
+     struct ksz_device *dev = irq_data_get_irq_chip_data(d);
+
+     mutex_lock(&dev->lock_irq);
+}
+
+static void ksz_girq_bus_sync_unlock(struct irq_data *d)
+{
+     struct ksz_device *dev = irq_data_get_irq_chip_data(d);
+     int ret;
+
+     ret = ksz_write32(dev, REG_SW_PORT_INT_MASK__4, dev-
Quoted 1 line

girq.masked);

drivers/net/dsa/microchip/ksz_common.c +3 -0
+     if (ret)
+             dev_err(dev->dev, "failed to change IRQ mask\n");
+

drivers/net/dsa/microchip/ksz_common.c static void ksz_update_port_member(struct ksz_device *dev, int port) · line 1814

Quoted 10 lines
drivers/net/dsa/microchip/ksz_common.c +10 -0
+     mutex_unlock(&dev->lock_irq);
+}
+
+static const struct irq_chip ksz_girq_chip = {
+     .name                   = "ksz-global",
+     .irq_mask               = ksz_girq_mask,
+     .irq_unmask             = ksz_girq_unmask,
+     .irq_bus_lock           = ksz_girq_bus_lock,
+     .irq_bus_sync_unlock    = ksz_girq_bus_sync_unlock,
+};

As the pirq code is almost identical to the girq code, how about putting
a "reg_mask", "reg_status" and a pointer to ksz_device into ksz_irq, and
using the ksz_irq as the chip data?

These would then become:

static void ksz_irq_mask(struct irq_data *d)

{
        struct ksz_irq *ki = irq_data_get_irq_chip_data(d);

        ki->masked |= BIT(d->hwirq);
}

static void ksz_irq_unmask(struct irq_data *d)

{
        struct ksz_irq *ki = irq_data_get_irq_chip_data(d);

        ki->masked &= ~BIT(d->hwirq);
}

static void ksz_irq_bus_lock(struct irq_data *d)

{
        struct ksz_irq *ki = irq_data_get_irq_chip_data(d);

        mutex_lock(&ki->dev->lock_irq);
}

static void ksz_irq_bus_sync_unlock(struct irq_data *d)

{
        struct ksz_irq *ki = irq_data_get_irq_chip_data(d);
        struct ksz_device *dev = ki->dev;
        int ret;

        ret = ksz_write32(dev, ki->reg_masked, ki->masked);
        if (ret)
                dev_err(dev->dev, "failed to change IRQ mask\n");

        mutex_unlock(&dev->lock_irq);
}
Quoted 4 lines

and thus this code could be shared between both pirq and girq. I'm pretty sure the thead_fn could be shared as well, and I'm sure that the setup and tear down could be improved in a similar way.

Thanks for the suggestion. I will update the code and send the next version of the patch.

Quoted 4 lines

--
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last!

#7 lore raw

PATCH v2 5/5 net: phy: micrel: enable interrupt for ksz9477 phy

In reply to Arun Ramadoss

Config_intr and handle_interrupt are enabled for ksz9477 phy. It is similar to all other phys in the micrel phys.

---
 drivers/net/phy/micrel.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/net/phy/micrel.c +2 -0
index 7b8c5c8d013e..09f2bef5d96c 100644
@@ -3191,6 +3191,8 @@ static struct phy_driver ksphy_driver[] = {
 	.name		= "Microchip KSZ9477",
 	/* PHY_GBIT_FEATURES */
 	.config_init	= kszphy_config_init,
+	.config_intr	= kszphy_config_intr,
+	.handle_interrupt = kszphy_handle_interrupt,
 	.suspend	= genphy_suspend,
 	.resume		= genphy_resume,
 } };
Signature · 1 line
2.36.1