drivers/net/phy/m88e1512: Add downshift enable

This patch provides the functionality to enable downshift on Marvell
PHY. By setting a downshift counter, the PHY is correspondingly often
attempted to establish Gigabit link before the PHY downshifts to the
next highest speed. The range is limited to 8 trials. To activate
downshift, a software reset must follow to take effect.

Change-Id: I4224eab6c1fc13824d53556c80435bc130a13bdb
Signed-off-by: Mario Scheithauer <mario.scheithauer@siemens.com>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/69853
Reviewed-by: Arthur Heymans <arthur@aheymans.xyz>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
diff --git a/src/drivers/net/phy/m88e1512/chip.h b/src/drivers/net/phy/m88e1512/chip.h
index 9a3523d..915001a 100644
--- a/src/drivers/net/phy/m88e1512/chip.h
+++ b/src/drivers/net/phy/m88e1512/chip.h
@@ -8,4 +8,7 @@
 	unsigned char led_1_ctrl;	/* LED[1] Control */
 	unsigned char led_2_ctrl;	/* LED[2] Control */
 	bool enable_int;		/* INTn can be routed to LED[2] pin */
+	/* 1x, 2x,...8x is the number of times the PHY attempts to establish Gigabit link
+	   before the PHY downshifts to the next highest speed. */
+	unsigned char downshift_cnt;
 };
diff --git a/src/drivers/net/phy/m88e1512/m88e1512.c b/src/drivers/net/phy/m88e1512/m88e1512.c
index a8d1c73..f32991f 100644
--- a/src/drivers/net/phy/m88e1512/m88e1512.c
+++ b/src/drivers/net/phy/m88e1512/m88e1512.c
@@ -17,6 +17,28 @@
 	struct drivers_net_phy_m88e1512_config *config = dev->chip_info;
 	uint16_t reg;
 
+	/* Enable downshift. */
+	if (config->downshift_cnt) {
+		if (config->downshift_cnt > DOWNSHIFT_CNT_MAX) {
+			printk(BIOS_INFO, "%s: Downshift counter for %s is too large.\n",
+					dev_path(dev->bus->dev), dev->chip_ops->name);
+		} else {
+			printk(BIOS_DEBUG, "%s: Enable downshift after %d attempts for %s.\n",
+					dev_path(dev->bus->dev), config->downshift_cnt,
+					dev->chip_ops->name);
+
+			reg = mdio_read(dev, COPPER_SPEC_CTRL_REG_1);
+			clrsetbits16(&reg, DOWNSHIFT_CNT_MASK,
+					DOWNSHIFT_CNT(config->downshift_cnt) | DOWNSHIFT_EN);
+			mdio_write(dev, COPPER_SPEC_CTRL_REG_1, reg);
+
+			/* Downshift enable requires a software reset to take effect. */
+			reg = mdio_read(dev, COPPER_CTRL_REG);
+			setbits16(&reg, SOFTWARE_RESET);
+			mdio_write(dev, COPPER_CTRL_REG, reg);
+		}
+	}
+
 	/* Configure LEDs if requested. */
 	if (config->configure_leds) {
 		printk(BIOS_DEBUG, "%s: Set a customized LED mode for %s.\n",
diff --git a/src/drivers/net/phy/m88e1512/m88e1512.h b/src/drivers/net/phy/m88e1512/m88e1512.h
index ca0f756..449cc57 100644
--- a/src/drivers/net/phy/m88e1512/m88e1512.h
+++ b/src/drivers/net/phy/m88e1512/m88e1512.h
@@ -5,6 +5,15 @@
 
 /* Register layout */
 #define PAGE_REG		0x16
+/* Page 0 registers */
+#define COPPER_CTRL_REG		0
+#define  SOFTWARE_RESET		(1 << 15)
+#define COPPER_SPEC_CTRL_REG_1	0x10
+#define DOWNSHIFT_CNT_MASK	0x7000
+#define DOWNSHIFT_CNT_MAX	8
+#define  DOWNSHIFT_CNT(cnt)	((cnt - 1) << 12)
+#define  DOWNSHIFT_EN		(1 << 11)
+/* Page 3 registers */
 #define LED_FUNC_CTRL_REG	0x10
 #define LED_FUNC_CTRL_MASK	0x0FFF
 #define LED_TIMER_CTRL_REG	0x12