Skip to content

Commit e6e12df

Browse files
vladimirolteandavem330
authored andcommitted
net: mscc: ocelot: convert to phylink
The felix DSA driver, which is a wrapper over the same hardware class as ocelot, is integrated with phylink, but ocelot is using the plain PHY library. It makes sense to bring together the two implementations, which is what this patch achieves. This is a large patch and hard to break up, but it does the following: The existing ocelot_adjust_link writes some registers, and felix_phylink_mac_link_up writes some registers, some of them are common, but both functions write to some registers to which the other doesn't. The main reasons for this are: - Felix switches so far have used an NXP PCS so they had no need to write the PCS1G registers that ocelot_adjust_link writes - Felix switches have the MAC fixed at 1G, so some of the MAC speed changes actually break the link and must be avoided. The naming conventions for the functions introduced in this patch are: - vsc7514_phylink_{mac_config,validate} are specific to the Ocelot instantiations and placed in ocelot_net.c which is built only for the ocelot switchdev driver. - ocelot_phylink_mac_link_{up,down} are shared between the ocelot switchdev driver and the felix DSA driver (they are put in the common lib). One by one, the registers written by ocelot_adjust_link are: DEV_MAC_MODE_CFG - felix_phylink_mac_link_up had no need to write this register since its out-of-reset value was fine and did not need changing. The write is moved to the common ocelot_phylink_mac_link_up and on felix it is guarded by a quirk bit that makes the written value identical with the out-of-reset one DEV_PORT_MISC - runtime invariant, was moved to vsc7514_phylink_mac_config PCS1G_MODE_CFG - same as above PCS1G_SD_CFG - same as above PCS1G_CFG - same as above PCS1G_ANEG_CFG - same as above PCS1G_LB_CFG - same as above DEV_MAC_ENA_CFG - both ocelot_adjust_link and ocelot_port_disable touched this. felix_phylink_mac_link_{up,down} also do. We go with what felix does and put it in ocelot_phylink_mac_link_up. DEV_CLOCK_CFG - ocelot_adjust_link and felix_phylink_mac_link_up both write this, but to different values. Move to the common ocelot_phylink_mac_link_up and make sure via the quirk that the old values are preserved for both. ANA_PFC_PFC_CFG - ocelot_adjust_link wrote this, felix_phylink_mac_link_up did not. Runtime invariant, speed does not matter since PFC is disabled via the RX_PFC_ENA bits which are cleared. Move to vsc7514_phylink_mac_config. QSYS_SWITCH_PORT_MODE_PORT_ENA - both ocelot_adjust_link and felix_phylink_mac_link_{up,down} wrote this. Ocelot also wrote this register from ocelot_port_disable. Keep what felix did, move in ocelot_phylink_mac_link_{up,down} and delete ocelot_port_disable. ANA_POL_FLOWC - same as above SYS_MAC_FC_CFG - same as above, except slight behavior change. Whereas ocelot always enabled RX and TX flow control, felix listened to phylink (for the most part, at least - see the 2500base-X comment). The registers which only felix_phylink_mac_link_up wrote are: SYS_PAUSE_CFG_PAUSE_ENA - this is why I am not sure that flow control worked on ocelot. Not it should, since the code is shared with felix where it does. ANA_PORT_PORT_CFG - this is a Frame Analyzer block register, phylink should be the one touching them, deleted. Other changes: - The old phylib registration code was in mscc_ocelot_init_ports. It is hard to work with 2 levels of indentation already in, and with hard to follow teardown logic. The new phylink registration code was moved inside ocelot_probe_port(), right between alloc_etherdev() and register_netdev(). It could not be done before (=> outside of) ocelot_probe_port() because ocelot_probe_port() allocates the struct ocelot_port which we then use to assign ocelot_port->phy_mode to. It is more preferable to me to have all PHY handling logic inside the same function. - On the same topic: struct ocelot_port_private :: serdes is only used in ocelot_port_open to set the SERDES protocol to Ethernet. This is logically a runtime invariant and can be done just once, when the port registers with phylink. We therefore don't even need to keep the serdes reference inside struct ocelot_port_private, or to use the devm variant of of_phy_get(). - Phylink needs a valid phy-mode for phylink_create() to succeed, and the existing device tree bindings in arch/mips/boot/dts/mscc/ocelot_pcb120.dts don't define one for the internal PHY ports. So we patch PHY_INTERFACE_MODE_NA into PHY_INTERFACE_MODE_INTERNAL. - There was a strategically placed: switch (priv->phy_mode) { case PHY_INTERFACE_MODE_NA: continue; which made the code skip the serdes initialization for the internal PHY ports. Frankly that is not all that obvious, so now we explicitly initialize the serdes under an "if" condition and not rely on code jumps, so everything is clearer. - There was a write of OCELOT_SPEED_1000 to DEV_CLOCK_CFG for QSGMII ports. Since that is in fact the default value for the register field DEV_CLOCK_CFG_LINK_SPEED, I can only guess the intention was to clear the adjacent fields, MAC_TX_RST and MAC_RX_RST, aka take the port out of reset, which does match the comment. I don't even want to know why this code is placed there, but if there is indeed an issue that all ports that share a QSGMII lane must all be up, then this logic is already buggy, since mscc_ocelot_init_ports iterates using for_each_available_child_of_node, so nobody prevents the user from putting a 'status = "disabled";' for some QSGMII ports which would break the driver's assumption. In any case, in the eventuality that I'm right, we would have yet another issue if ocelot_phylink_mac_link_down would reset those ports and that would be forbidden, so since the ocelot_adjust_link logic did not do that (maybe for a reason), add another quirk to preserve the old logic. The ocelot driver teardown goes through all ports in one fell swoop. When initialization of one port fails, the ocelot->ports[port] pointer for that is reset to NULL, and teardown is done only for non-NULL ports, so there is no reason to do partial teardowns, let the central mscc_ocelot_release_ports() do its job. Tested bind, unbind, rebind, link up, link down, speed change on mock-up hardware (modified the driver to probe on Felix VSC9959). Also regression tested the felix DSA driver. Could not test the Ocelot specific bits (PCS1G, SERDES, device tree bindings). Signed-off-by: Vladimir Oltean <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 46efe4e commit e6e12df

File tree

8 files changed

+329
-254
lines changed

8 files changed

+329
-254
lines changed

drivers/net/dsa/ocelot/felix.c

Lines changed: 5 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -824,25 +824,9 @@ static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port,
824824
phy_interface_t interface)
825825
{
826826
struct ocelot *ocelot = ds->priv;
827-
struct ocelot_port *ocelot_port = ocelot->ports[port];
828-
int err;
829-
830-
ocelot_port_rmwl(ocelot_port, 0, DEV_MAC_ENA_CFG_RX_ENA,
831-
DEV_MAC_ENA_CFG);
832-
833-
ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
834827

835-
err = ocelot_port_flush(ocelot, port);
836-
if (err)
837-
dev_err(ocelot->dev, "failed to flush port %d: %d\n",
838-
port, err);
839-
840-
/* Put the port in reset. */
841-
ocelot_port_writel(ocelot_port,
842-
DEV_CLOCK_CFG_MAC_TX_RST |
843-
DEV_CLOCK_CFG_MAC_RX_RST |
844-
DEV_CLOCK_CFG_LINK_SPEED(OCELOT_SPEED_1000),
845-
DEV_CLOCK_CFG);
828+
ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface,
829+
FELIX_MAC_QUIRKS);
846830
}
847831

848832
static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port,
@@ -853,75 +837,11 @@ static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port,
853837
bool tx_pause, bool rx_pause)
854838
{
855839
struct ocelot *ocelot = ds->priv;
856-
struct ocelot_port *ocelot_port = ocelot->ports[port];
857840
struct felix *felix = ocelot_to_felix(ocelot);
858-
u32 mac_fc_cfg;
859-
860-
/* Take port out of reset by clearing the MAC_TX_RST, MAC_RX_RST and
861-
* PORT_RST bits in DEV_CLOCK_CFG. Note that the way this system is
862-
* integrated is that the MAC speed is fixed and it's the PCS who is
863-
* performing the rate adaptation, so we have to write "1000Mbps" into
864-
* the LINK_SPEED field of DEV_CLOCK_CFG (which is also its default
865-
* value).
866-
*/
867-
ocelot_port_writel(ocelot_port,
868-
DEV_CLOCK_CFG_LINK_SPEED(OCELOT_SPEED_1000),
869-
DEV_CLOCK_CFG);
870841

871-
switch (speed) {
872-
case SPEED_10:
873-
mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(3);
874-
break;
875-
case SPEED_100:
876-
mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(2);
877-
break;
878-
case SPEED_1000:
879-
case SPEED_2500:
880-
mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(1);
881-
break;
882-
default:
883-
dev_err(ocelot->dev, "Unsupported speed on port %d: %d\n",
884-
port, speed);
885-
return;
886-
}
887-
888-
/* handle Rx pause in all cases, with 2500base-X this is used for rate
889-
* adaptation.
890-
*/
891-
mac_fc_cfg |= SYS_MAC_FC_CFG_RX_FC_ENA;
892-
893-
if (tx_pause)
894-
mac_fc_cfg |= SYS_MAC_FC_CFG_TX_FC_ENA |
895-
SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
896-
SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
897-
SYS_MAC_FC_CFG_ZERO_PAUSE_ENA;
898-
899-
/* Flow control. Link speed is only used here to evaluate the time
900-
* specification in incoming pause frames.
901-
*/
902-
ocelot_write_rix(ocelot, mac_fc_cfg, SYS_MAC_FC_CFG, port);
903-
904-
ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
905-
906-
ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, tx_pause);
907-
908-
/* Undo the effects of felix_phylink_mac_link_down:
909-
* enable MAC module
910-
*/
911-
ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
912-
DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
913-
914-
/* Enable receiving frames on the port, and activate auto-learning of
915-
* MAC addresses.
916-
*/
917-
ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
918-
ANA_PORT_PORT_CFG_RECV_ENA |
919-
ANA_PORT_PORT_CFG_PORTID_VAL(port),
920-
ANA_PORT_PORT_CFG, port);
921-
922-
/* Core: Enable port for frame transfer */
923-
ocelot_fields_write(ocelot, port,
924-
QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
842+
ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode,
843+
interface, speed, duplex, tx_pause, rx_pause,
844+
FELIX_MAC_QUIRKS);
925845

926846
if (felix->info->port_sched_speed_set)
927847
felix->info->port_sched_speed_set(ocelot, port, speed);

drivers/net/dsa/ocelot/felix.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#define _MSCC_FELIX_H
66

77
#define ocelot_to_felix(o) container_of((o), struct felix, ocelot)
8+
#define FELIX_MAC_QUIRKS OCELOT_QUIRK_PCS_PERFORMS_RATE_ADAPTATION
89

910
/* Platform-specific information */
1011
struct felix_info {

drivers/net/ethernet/mscc/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ config MSCC_OCELOT_SWITCH_LIB
1616
select NET_DEVLINK
1717
select REGMAP_MMIO
1818
select PACKING
19-
select PHYLIB
19+
select PHYLINK
2020
tristate
2121
help
2222
This is a hardware support library for Ocelot network switches. It is

drivers/net/ethernet/mscc/ocelot.c

Lines changed: 94 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ static u32 ocelot_read_eq_avail(struct ocelot *ocelot, int port)
377377
return ocelot_read_rix(ocelot, QSYS_SW_STATUS, port);
378378
}
379379

380-
int ocelot_port_flush(struct ocelot *ocelot, int port)
380+
static int ocelot_port_flush(struct ocelot *ocelot, int port)
381381
{
382382
unsigned int pause_ena;
383383
int err, val;
@@ -429,63 +429,118 @@ int ocelot_port_flush(struct ocelot *ocelot, int port)
429429

430430
return err;
431431
}
432-
EXPORT_SYMBOL(ocelot_port_flush);
433432

434-
void ocelot_adjust_link(struct ocelot *ocelot, int port,
435-
struct phy_device *phydev)
433+
void ocelot_phylink_mac_link_down(struct ocelot *ocelot, int port,
434+
unsigned int link_an_mode,
435+
phy_interface_t interface,
436+
unsigned long quirks)
436437
{
437438
struct ocelot_port *ocelot_port = ocelot->ports[port];
438-
int speed, mode = 0;
439+
int err;
440+
441+
ocelot_port_rmwl(ocelot_port, 0, DEV_MAC_ENA_CFG_RX_ENA,
442+
DEV_MAC_ENA_CFG);
443+
444+
ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
445+
446+
err = ocelot_port_flush(ocelot, port);
447+
if (err)
448+
dev_err(ocelot->dev, "failed to flush port %d: %d\n",
449+
port, err);
450+
451+
/* Put the port in reset. */
452+
if (interface != PHY_INTERFACE_MODE_QSGMII ||
453+
!(quirks & OCELOT_QUIRK_QSGMII_PORTS_MUST_BE_UP))
454+
ocelot_port_rmwl(ocelot_port,
455+
DEV_CLOCK_CFG_MAC_TX_RST |
456+
DEV_CLOCK_CFG_MAC_TX_RST,
457+
DEV_CLOCK_CFG_MAC_TX_RST |
458+
DEV_CLOCK_CFG_MAC_TX_RST,
459+
DEV_CLOCK_CFG);
460+
}
461+
EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_down);
462+
463+
void ocelot_phylink_mac_link_up(struct ocelot *ocelot, int port,
464+
struct phy_device *phydev,
465+
unsigned int link_an_mode,
466+
phy_interface_t interface,
467+
int speed, int duplex,
468+
bool tx_pause, bool rx_pause,
469+
unsigned long quirks)
470+
{
471+
struct ocelot_port *ocelot_port = ocelot->ports[port];
472+
int mac_speed, mode = 0;
473+
u32 mac_fc_cfg;
474+
475+
/* The MAC might be integrated in systems where the MAC speed is fixed
476+
* and it's the PCS who is performing the rate adaptation, so we have
477+
* to write "1000Mbps" into the LINK_SPEED field of DEV_CLOCK_CFG
478+
* (which is also its default value).
479+
*/
480+
if ((quirks & OCELOT_QUIRK_PCS_PERFORMS_RATE_ADAPTATION) ||
481+
speed == SPEED_1000) {
482+
mac_speed = OCELOT_SPEED_1000;
483+
mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
484+
} else if (speed == SPEED_2500) {
485+
mac_speed = OCELOT_SPEED_2500;
486+
mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
487+
} else if (speed == SPEED_100) {
488+
mac_speed = OCELOT_SPEED_100;
489+
} else {
490+
mac_speed = OCELOT_SPEED_10;
491+
}
492+
493+
if (duplex == DUPLEX_FULL)
494+
mode |= DEV_MAC_MODE_CFG_FDX_ENA;
495+
496+
ocelot_port_writel(ocelot_port, mode, DEV_MAC_MODE_CFG);
497+
498+
/* Take port out of reset by clearing the MAC_TX_RST, MAC_RX_RST and
499+
* PORT_RST bits in DEV_CLOCK_CFG.
500+
*/
501+
ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(mac_speed),
502+
DEV_CLOCK_CFG);
439503

440-
switch (phydev->speed) {
504+
switch (speed) {
441505
case SPEED_10:
442-
speed = OCELOT_SPEED_10;
506+
mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_10);
443507
break;
444508
case SPEED_100:
445-
speed = OCELOT_SPEED_100;
509+
mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_100);
446510
break;
447511
case SPEED_1000:
448-
speed = OCELOT_SPEED_1000;
449-
mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
450-
break;
451512
case SPEED_2500:
452-
speed = OCELOT_SPEED_2500;
453-
mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
513+
mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_1000);
454514
break;
455515
default:
456-
dev_err(ocelot->dev, "Unsupported PHY speed on port %d: %d\n",
457-
port, phydev->speed);
516+
dev_err(ocelot->dev, "Unsupported speed on port %d: %d\n",
517+
port, speed);
458518
return;
459519
}
460520

461-
phy_print_status(phydev);
462-
463-
if (!phydev->link)
464-
return;
465-
466-
/* Only full duplex supported for now */
467-
ocelot_port_writel(ocelot_port, DEV_MAC_MODE_CFG_FDX_ENA |
468-
mode, DEV_MAC_MODE_CFG);
469-
470-
/* Disable HDX fast control */
471-
ocelot_port_writel(ocelot_port, DEV_PORT_MISC_HDX_FAST_DIS,
472-
DEV_PORT_MISC);
521+
/* Handle RX pause in all cases, with 2500base-X this is used for rate
522+
* adaptation.
523+
*/
524+
mac_fc_cfg |= SYS_MAC_FC_CFG_RX_FC_ENA;
473525

474-
/* SGMII only for now */
475-
ocelot_port_writel(ocelot_port, PCS1G_MODE_CFG_SGMII_MODE_ENA,
476-
PCS1G_MODE_CFG);
477-
ocelot_port_writel(ocelot_port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
526+
if (tx_pause)
527+
mac_fc_cfg |= SYS_MAC_FC_CFG_TX_FC_ENA |
528+
SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
529+
SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
530+
SYS_MAC_FC_CFG_ZERO_PAUSE_ENA;
478531

479-
/* Enable PCS */
480-
ocelot_port_writel(ocelot_port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
532+
/* Flow control. Link speed is only used here to evaluate the time
533+
* specification in incoming pause frames.
534+
*/
535+
ocelot_write_rix(ocelot, mac_fc_cfg, SYS_MAC_FC_CFG, port);
481536

482-
/* No aneg on SGMII */
483-
ocelot_port_writel(ocelot_port, 0, PCS1G_ANEG_CFG);
537+
ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
484538

485-
/* No loopback */
486-
ocelot_port_writel(ocelot_port, 0, PCS1G_LB_CFG);
539+
ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, tx_pause);
487540

488-
/* Enable MAC module */
541+
/* Undo the effects of ocelot_phylink_mac_link_down:
542+
* enable MAC module
543+
*/
489544
ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
490545
DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
491546

@@ -502,26 +557,8 @@ void ocelot_adjust_link(struct ocelot *ocelot, int port,
502557
/* Core: Enable port for frame transfer */
503558
ocelot_fields_write(ocelot, port,
504559
QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
505-
506-
/* Flow control */
507-
ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
508-
SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA |
509-
SYS_MAC_FC_CFG_ZERO_PAUSE_ENA |
510-
SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
511-
SYS_MAC_FC_CFG_FC_LINK_SPEED(speed),
512-
SYS_MAC_FC_CFG, port);
513-
ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
514-
}
515-
EXPORT_SYMBOL(ocelot_adjust_link);
516-
517-
void ocelot_port_disable(struct ocelot *ocelot, int port)
518-
{
519-
struct ocelot_port *ocelot_port = ocelot->ports[port];
520-
521-
ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG);
522-
ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
523560
}
524-
EXPORT_SYMBOL(ocelot_port_disable);
561+
EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_up);
525562

526563
static void ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port,
527564
struct sk_buff *clone)

drivers/net/ethernet/mscc/ocelot.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
#include <linux/etherdevice.h>
1313
#include <linux/if_vlan.h>
1414
#include <linux/net_tstamp.h>
15-
#include <linux/phy.h>
16-
#include <linux/phy/phy.h>
15+
#include <linux/phylink.h>
1716
#include <linux/platform_device.h>
1817
#include <linux/regmap.h>
1918

@@ -42,11 +41,9 @@ struct ocelot_port_tc {
4241
struct ocelot_port_private {
4342
struct ocelot_port port;
4443
struct net_device *dev;
45-
struct phy_device *phy;
44+
struct phylink *phylink;
45+
struct phylink_config phylink_config;
4646
u8 chip_port;
47-
48-
struct phy *serdes;
49-
5047
struct ocelot_port_tc tc;
5148
};
5249

@@ -107,7 +104,7 @@ u32 ocelot_port_readl(struct ocelot_port *port, u32 reg);
107104
void ocelot_port_writel(struct ocelot_port *port, u32 val, u32 reg);
108105

109106
int ocelot_probe_port(struct ocelot *ocelot, int port, struct regmap *target,
110-
struct phy_device *phy);
107+
struct device_node *portnp);
111108
void ocelot_release_port(struct ocelot_port *ocelot_port);
112109
int ocelot_devlink_init(struct ocelot *ocelot);
113110
void ocelot_devlink_teardown(struct ocelot *ocelot);

0 commit comments

Comments
 (0)