blob: ae9fe7f7db8c7d6d097d1adf9be7e9070e5b39ff [file] [log] [blame]
developer7e3c5b72022-02-21 21:38:33 +08001Index: drivers/net/phy/Kconfig
2===================================================================
3--- a/drivers/net/phy/Kconfig
4+++ b/drivers/net/phy/Kconfig
5@@ -345,6 +345,11 @@ config SFP
6 depends on HWMON || HWMON=n
7 select MDIO_I2C
8
9+config AIROHA_EN8801S_PHY
10+ tristate "Drivers for Airoha EN8801S Gigabit PHYs"
11+ ---help---
12+ Currently supports the Airoha EN8801S PHY.
13+
14 config ADIN_PHY
15 tristate "Analog Devices Industrial Ethernet PHYs"
16 help
17Index: drivers/net/phy/Makefile
18===================================================================
19--- a/drivers/net/phy/Makefile
20+++ b/drivers/net/phy/Makefile
21@@ -67,6 +67,7 @@ aquantia-objs += aquantia_main.o
22 ifdef CONFIG_HWMON
23 aquantia-objs += aquantia_hwmon.o
24 endif
25+obj-$(CONFIG_AIROHA_EN8801S_PHY) += en8801s.o
26 obj-$(CONFIG_AQUANTIA_PHY) += aquantia.o
27 obj-$(CONFIG_AX88796B_PHY) += ax88796b.o
28 obj-$(CONFIG_AT803X_PHY) += at803x.o
29Index: drivers/net/phy/en8801s.c
30===================================================================
31--- /dev/null
32+++ b/drivers/net/phy/en8801s.c
developer87e071e2022-08-04 16:26:17 +080033@@ -0,0 +1,434 @@
developer7e3c5b72022-02-21 21:38:33 +080034+// SPDX-License-Identifier: GPL-2.0
developer87e071e2022-08-04 16:26:17 +080035+/* FILE NAME: en8801s.c
developer7e3c5b72022-02-21 21:38:33 +080036+ * PURPOSE:
37+ * EN8801S phy driver for Linux
38+ * NOTES:
39+ *
40+ */
41+
42+/* INCLUDE FILE DECLARATIONS
43+ */
44+
45+#include <linux/kernel.h>
46+#include <linux/string.h>
47+#include <linux/errno.h>
48+#include <linux/unistd.h>
49+#include <linux/interrupt.h>
50+#include <linux/init.h>
51+#include <linux/delay.h>
52+#include <linux/netdevice.h>
53+#include <linux/etherdevice.h>
54+#include <linux/skbuff.h>
55+#include <linux/spinlock.h>
56+#include <linux/mm.h>
57+#include <linux/module.h>
58+#include <linux/mii.h>
59+#include <linux/ethtool.h>
60+#include <linux/phy.h>
61+#include <linux/delay.h>
62+
developer7e3c5b72022-02-21 21:38:33 +080063+#include <linux/uaccess.h>
64+#include <linux/version.h>
65+
66+#include "en8801s.h"
67+
developer7e3c5b72022-02-21 21:38:33 +080068+MODULE_DESCRIPTION("Airoha EN8801S PHY drivers");
69+MODULE_AUTHOR("Airoha");
70+MODULE_LICENSE("GPL");
71+
developer87e071e2022-08-04 16:26:17 +080072+#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 5, 0))
73+#define phydev_mdio_bus(dev) ((dev)->bus)
74+#else
75+#define phydev_mdio_bus(dev) ((dev)->mdio.bus)
76+#endif
77+
78+enum {
79+ PHY_STATE_DONE = 0,
80+ PHY_STATE_INIT = 1,
81+ PHY_STATE_PROCESS = 2,
82+};
83+
developer7e3c5b72022-02-21 21:38:33 +080084+/************************************************************************
85+* F U N C T I O N S
86+************************************************************************/
developer87e071e2022-08-04 16:26:17 +080087+static unsigned int airoha_cl45_write(struct mii_bus *bus, u32 port, u32 devad, u32 reg, u16 val)
developer7e3c5b72022-02-21 21:38:33 +080088+{
89+ mdiobus_write(bus, port, MII_MMD_ACC_CTL_REG, devad);
90+ mdiobus_write(bus, port, MII_MMD_ADDR_DATA_REG, reg);
91+ mdiobus_write(bus, port, MII_MMD_ACC_CTL_REG, MMD_OP_MODE_DATA | devad);
92+ mdiobus_write(bus, port, MII_MMD_ADDR_DATA_REG, val);
93+ return 0;
94+}
95+
developer87e071e2022-08-04 16:26:17 +080096+static unsigned int airoha_cl45_read(struct mii_bus *bus, u32 port, u32 devad, u32 reg, u32 *read_data)
developer7e3c5b72022-02-21 21:38:33 +080097+{
98+ mdiobus_write(bus, port, MII_MMD_ACC_CTL_REG, devad);
99+ mdiobus_write(bus, port, MII_MMD_ADDR_DATA_REG, reg);
100+ mdiobus_write(bus, port, MII_MMD_ACC_CTL_REG, MMD_OP_MODE_DATA | devad);
101+ *read_data = mdiobus_read(bus, port, MII_MMD_ADDR_DATA_REG);
102+ return 0;
103+}
104+
developer87e071e2022-08-04 16:26:17 +0800105+static unsigned int airoha_cl22_read(struct mii_bus *ebus, unsigned int phy_addr, unsigned int phy_register, unsigned int *read_data)
developer7e3c5b72022-02-21 21:38:33 +0800106+{
107+ *read_data = mdiobus_read(ebus, phy_addr, phy_register);
108+ return 0;
109+}
110+
developer87e071e2022-08-04 16:26:17 +0800111+static unsigned int airoha_cl22_write(struct mii_bus *ebus, unsigned int phy_addr, unsigned int phy_register, unsigned int write_data)
developer7e3c5b72022-02-21 21:38:33 +0800112+{
113+ mdiobus_write(ebus, phy_addr, phy_register, write_data);
114+ return 0;
115+}
116+
developer87e071e2022-08-04 16:26:17 +0800117+static void airoha_pbus_write(struct mii_bus *ebus, unsigned long pbus_id, unsigned long pbus_address, unsigned long pbus_data)
developer7e3c5b72022-02-21 21:38:33 +0800118+{
developer87e071e2022-08-04 16:26:17 +0800119+ airoha_cl22_write(ebus, pbus_id, 0x1F, (unsigned int)(pbus_address >> 6));
120+ airoha_cl22_write(ebus, pbus_id, (unsigned int)((pbus_address >> 2) & 0xf), (unsigned int)(pbus_data & 0xFFFF));
121+ airoha_cl22_write(ebus, pbus_id, 0x10, (unsigned int)(pbus_data >> 16));
developer7e3c5b72022-02-21 21:38:33 +0800122+ return;
123+}
124+
developer87e071e2022-08-04 16:26:17 +0800125+static unsigned long airoha_pbus_read(struct mii_bus *ebus, unsigned long pbus_id, unsigned long pbus_address)
developer7e3c5b72022-02-21 21:38:33 +0800126+{
127+ unsigned long pbus_data;
128+ unsigned int pbus_data_low, pbus_data_high;
129+
developer87e071e2022-08-04 16:26:17 +0800130+ airoha_cl22_write(ebus, pbus_id, 0x1F, (unsigned int)(pbus_address >> 6));
131+ airoha_cl22_read(ebus, pbus_id, (unsigned int)((pbus_address >> 2) & 0xf), &pbus_data_low);
132+ airoha_cl22_read(ebus, pbus_id, 0x10, &pbus_data_high);
developer7e3c5b72022-02-21 21:38:33 +0800133+ pbus_data = (pbus_data_high << 16) + pbus_data_low;
134+ return pbus_data;
135+}
136+
developer87e071e2022-08-04 16:26:17 +0800137+/* Airoha Token Ring Write function */
138+static void airoha_tr_reg_write(struct mii_bus *ebus, unsigned long tr_address, unsigned long tr_data)
developer7e3c5b72022-02-21 21:38:33 +0800139+{
developer87e071e2022-08-04 16:26:17 +0800140+ airoha_cl22_write(ebus, EN8801S_MDIO_PHY_ID, 0x1F, 0x52b5); /* page select */
141+ airoha_cl22_write(ebus, EN8801S_MDIO_PHY_ID, 0x11, (unsigned int)(tr_data & 0xffff));
142+ airoha_cl22_write(ebus, EN8801S_MDIO_PHY_ID, 0x12, (unsigned int)(tr_data >> 16));
143+ airoha_cl22_write(ebus, EN8801S_MDIO_PHY_ID, 0x10, (unsigned int)(tr_address | TrReg_WR));
144+ airoha_cl22_write(ebus, EN8801S_MDIO_PHY_ID, 0x1F, 0x0); /* page resetore */
developer7e3c5b72022-02-21 21:38:33 +0800145+ return;
146+}
147+
developer87e071e2022-08-04 16:26:17 +0800148+/* Airoha Token Ring Read function */
149+static unsigned long airoha_tr_reg_read(struct mii_bus *ebus, unsigned long tr_address)
developer7e3c5b72022-02-21 21:38:33 +0800150+{
developer87e071e2022-08-04 16:26:17 +0800151+ unsigned long tr_data;
152+ unsigned int tr_data_low, tr_data_high;
developer7e3c5b72022-02-21 21:38:33 +0800153+
developer87e071e2022-08-04 16:26:17 +0800154+ airoha_cl22_write(ebus, EN8801S_MDIO_PHY_ID, 0x1F, 0x52b5); /* page select */
155+ airoha_cl22_write(ebus, EN8801S_MDIO_PHY_ID, 0x10, (unsigned int)(tr_address | TrReg_RD));
156+ airoha_cl22_read(ebus, EN8801S_MDIO_PHY_ID, 0x11, &tr_data_low);
157+ airoha_cl22_read(ebus, EN8801S_MDIO_PHY_ID, 0x12, &tr_data_high);
158+ airoha_cl22_write(ebus, EN8801S_MDIO_PHY_ID, 0x1F, 0x0); /* page resetore */
159+ tr_data = (tr_data_high << 16) + tr_data_low;
160+ return tr_data;
developer7e3c5b72022-02-21 21:38:33 +0800161+}
162+
developer87e071e2022-08-04 16:26:17 +0800163+static void en8801s_led_init(struct phy_device *phydev)
developer7e3c5b72022-02-21 21:38:33 +0800164+{
developer87e071e2022-08-04 16:26:17 +0800165+ struct mii_bus *mbus = phydev_mdio_bus(phydev);
166+ u32 reg_value;
167+
168+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x186c, 0x3);
169+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0X1870, 0x100);
170+ reg_value = (airoha_pbus_read(mbus, EN8801S_PBUS_PHY_ID, 0x1880) & ~(0x3));
171+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x1880, reg_value);
172+ airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1f, 0x21, 0x8008);
173+ airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1f, 0x22, 0x600);
174+ airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1f, 0x23, 0xc00);
175+ /* LED0: 10M/100M */
176+ airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1f, 0x24, 0x8006);
177+ /* LED0: blink 10M/100M Tx/Rx */
178+ airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1f, 0x25, 0x3c);
179+ /* LED1: 1000M */
180+ airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1f, 0x26, 0x8001);
181+ /* LED1: blink 1000M Tx/Rx */
182+ airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1f, 0x27, 0x3);
developer7e3c5b72022-02-21 21:38:33 +0800183+}
184+
developer87e071e2022-08-04 16:26:17 +0800185+static int en8801s_phy_process(struct phy_device *phydev)
developer7e3c5b72022-02-21 21:38:33 +0800186+{
developer87e071e2022-08-04 16:26:17 +0800187+ struct mii_bus *mbus = phydev_mdio_bus(phydev);
188+ u32 reg_value = 0;
developer7e3c5b72022-02-21 21:38:33 +0800189+
developer87e071e2022-08-04 16:26:17 +0800190+ reg_value = airoha_pbus_read(mbus, EN8801S_PBUS_PHY_ID, 0x19e0);
191+ reg_value |= (1 << 0);
192+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x19e0, reg_value);
193+ reg_value = airoha_pbus_read(mbus, EN8801S_PBUS_PHY_ID, 0x19e0);
194+ reg_value &= ~(1 << 0);
195+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x19e0, reg_value);
196+ return 0;
developer7e3c5b72022-02-21 21:38:33 +0800197+}
198+
developer87e071e2022-08-04 16:26:17 +0800199+static int en8801s_phase1_init(struct phy_device *phydev)
developer7e3c5b72022-02-21 21:38:33 +0800200+{
developer7e3c5b72022-02-21 21:38:33 +0800201+ unsigned long pbus_data;
202+ unsigned int pbusAddress;
203+ u32 reg_value;
204+ int retry;
developer87e071e2022-08-04 16:26:17 +0800205+ struct mii_bus *mbus = phydev_mdio_bus(phydev);
developer7e3c5b72022-02-21 21:38:33 +0800206+
developer87e071e2022-08-04 16:26:17 +0800207+ msleep(1500);
developer7e3c5b72022-02-21 21:38:33 +0800208+
209+ pbusAddress = EN8801S_PBUS_DEFAULT_ID;
210+ retry = MAX_OUI_CHECK;
developer87e071e2022-08-04 16:26:17 +0800211+ while (1) {
212+ pbus_data = airoha_pbus_read(mbus, pbusAddress, EN8801S_RG_ETHER_PHY_OUI); /* PHY OUI */
213+ if (EN8801S_PBUS_OUI == pbus_data) {
214+ pbus_data = airoha_pbus_read(mbus, pbusAddress, EN8801S_RG_SMI_ADDR); /* SMI ADDR */
215+ pbus_data = (pbus_data & 0xffff0000) | (unsigned long)(EN8801S_PBUS_PHY_ID << 8) | (unsigned long)(EN8801S_MDIO_PHY_ID);
216+ phydev_info(phydev, "SMI_ADDR=%lx (renew)\n", pbus_data);
217+ airoha_pbus_write(mbus, pbusAddress, EN8801S_RG_SMI_ADDR, pbus_data);
218+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, EN8801S_RG_BUCK_CTL, 0x03);
developer7e3c5b72022-02-21 21:38:33 +0800219+ mdelay(10);
220+ break;
developer87e071e2022-08-04 16:26:17 +0800221+ } else {
developer7e3c5b72022-02-21 21:38:33 +0800222+ pbusAddress = EN8801S_PBUS_PHY_ID;
223+ }
224+ retry --;
developer87e071e2022-08-04 16:26:17 +0800225+ if (0 == retry) {
226+ phydev_err(phydev, "Probe fail !\n");
developer7e3c5b72022-02-21 21:38:33 +0800227+ return 0;
228+ }
229+ }
230+
developer87e071e2022-08-04 16:26:17 +0800231+ reg_value = (airoha_pbus_read(mbus, EN8801S_PBUS_PHY_ID, EN8801S_RG_LTR_CTL) & 0xfffffffc) | 0x10 | (EN8801S_RX_POLARITY << 1) | EN8801S_TX_POLARITY;
232+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, EN8801S_RG_LTR_CTL, reg_value);
developer7e3c5b72022-02-21 21:38:33 +0800233+ mdelay(10);
234+ reg_value &= 0xffffffef;
developer87e071e2022-08-04 16:26:17 +0800235+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, EN8801S_RG_LTR_CTL, reg_value);
developer7e3c5b72022-02-21 21:38:33 +0800236+
237+ retry = MAX_RETRY;
developer87e071e2022-08-04 16:26:17 +0800238+ while (1) {
developer7e3c5b72022-02-21 21:38:33 +0800239+ mdelay(10);
240+ reg_value = phy_read(phydev, MII_PHYSID2);
developer87e071e2022-08-04 16:26:17 +0800241+ if (reg_value == EN8801S_PHY_ID2) {
developer7e3c5b72022-02-21 21:38:33 +0800242+ break; /* wait GPHY ready */
243+ }
244+ retry--;
developer87e071e2022-08-04 16:26:17 +0800245+ if (0 == retry) {
246+ phydev_err(phydev, "Initialize fail !\n");
developer7e3c5b72022-02-21 21:38:33 +0800247+ return 0;
248+ }
249+ }
250+ /* Software Reset PHY */
251+ reg_value = phy_read(phydev, MII_BMCR);
252+ reg_value |= BMCR_RESET;
253+ phy_write(phydev, MII_BMCR, reg_value);
254+ retry = MAX_RETRY;
developer87e071e2022-08-04 16:26:17 +0800255+ do {
developer7e3c5b72022-02-21 21:38:33 +0800256+ mdelay(10);
257+ reg_value = phy_read(phydev, MII_BMCR);
258+ retry--;
developer87e071e2022-08-04 16:26:17 +0800259+ if (0 == retry) {
260+ phydev_err(phydev, "Reset fail !\n");
developer7e3c5b72022-02-21 21:38:33 +0800261+ return 0;
262+ }
263+ } while (reg_value & BMCR_RESET);
264+
developer87e071e2022-08-04 16:26:17 +0800265+ phydev->dev_flags = PHY_STATE_INIT;
266+
267+ phydev_info(phydev, "Phase1 initialize OK ! (%s)\n", EN8801S_DRIVER_VERSION);
268+ return 0;
269+}
developer7e3c5b72022-02-21 21:38:33 +0800270+
developer87e071e2022-08-04 16:26:17 +0800271+static int en8801s_phase2_init(struct phy_device *phydev)
272+{
273+ gephy_all_REG_LpiReg1Ch GPHY_RG_LPI_1C;
274+ gephy_all_REG_dev1Eh_reg324h GPHY_RG_1E_324;
275+ gephy_all_REG_dev1Eh_reg012h GPHY_RG_1E_012;
276+ gephy_all_REG_dev1Eh_reg017h GPHY_RG_1E_017;
277+ unsigned long pbus_data;
278+ u32 reg_value;
279+ int retry;
280+ struct mii_bus *mbus = phydev_mdio_bus(phydev);
281+
282+ reg_value = (airoha_pbus_read(mbus, EN8801S_PBUS_PHY_ID, EN8801S_RG_LTR_CTL) & 0xfffffffc) | 0x10 | (EN8801S_RX_POLARITY << 1) | EN8801S_TX_POLARITY;
283+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, EN8801S_RG_LTR_CTL, reg_value);
284+ mdelay(10);
285+ reg_value &= 0xffffffef;
286+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, EN8801S_RG_LTR_CTL, reg_value);
287+
288+ pbus_data = airoha_pbus_read(mbus, EN8801S_PBUS_PHY_ID, 0x1690);
289+ pbus_data |= (1 << 31);
290+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x1690, pbus_data);
291+
292+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x0600, 0x0c000c00);
293+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x10, 0xD801);
294+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x0, 0x9140);
295+
296+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x0A14, 0x0003);
297+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x0600, 0x0c000c00);
developer7e3c5b72022-02-21 21:38:33 +0800298+ /* Set FCM control */
developer87e071e2022-08-04 16:26:17 +0800299+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x1404, 0x004b);
300+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x140c, 0x0007);
301+
302+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x142c, 0x05050505);
303+ pbus_data = airoha_pbus_read(mbus, EN8801S_PBUS_PHY_ID, 0x1440);
304+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x1440, pbus_data & ~(1 << 11));
developer7e3c5b72022-02-21 21:38:33 +0800305+ /* Set GPHY Perfomance*/
306+ /* Token Ring */
developer87e071e2022-08-04 16:26:17 +0800307+ airoha_tr_reg_write(mbus, RgAddr_PMA_01h, 0x6FB90A);
308+ airoha_tr_reg_write(mbus, RgAddr_PMA_18h, 0x0E2F00);
309+ airoha_tr_reg_write(mbus, RgAddr_DSPF_06h, 0x2EBAEF);
310+ airoha_tr_reg_write(mbus, RgAddr_DSPF_11h, 0x040001);
311+ airoha_tr_reg_write(mbus, RgAddr_DSPF_03h, 0x000004);
312+ airoha_tr_reg_write(mbus, RgAddr_DSPF_1Ch, 0x003210);
313+ airoha_tr_reg_write(mbus, RgAddr_DSPF_14h, 0x00024A);
314+ airoha_tr_reg_write(mbus, RgAddr_DSPF_0Ch, 0x00704D);
315+ airoha_tr_reg_write(mbus, RgAddr_DSPF_0Dh, 0x02314F);
316+ airoha_tr_reg_write(mbus, RgAddr_DSPF_10h, 0x005010);
317+ airoha_tr_reg_write(mbus, RgAddr_DSPF_0Fh, 0x003028);
318+ airoha_tr_reg_write(mbus, RgAddr_TR_26h, 0x444444);
319+ airoha_tr_reg_write(mbus, RgAddr_R1000DEC_15h, 0x0055A0);
developer7e3c5b72022-02-21 21:38:33 +0800320+ /* CL22 & CL45 */
321+ phy_write(phydev, 0x1f, 0x03);
322+ GPHY_RG_LPI_1C.DATA = phy_read(phydev, RgAddr_LpiReg1Ch);
323+ GPHY_RG_LPI_1C.DataBitField.smi_deton_th = 0x0C;
324+ phy_write(phydev, RgAddr_LpiReg1Ch, GPHY_RG_LPI_1C.DATA);
325+ phy_write(phydev, 0x1f, 0x0);
developer87e071e2022-08-04 16:26:17 +0800326+ airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1E, 0x122, 0xffff);
327+ airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1E, 0x234, 0x0180);
328+ airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1E, 0x238, 0x0120);
329+ airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1E, 0x120, 0x9014);
330+ airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1E, 0x239, 0x0117);
331+ airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1E, 0x14A, 0xEE20);
332+ airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1E, 0x19B, 0x0111);
333+ airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1F, 0x268, 0x07F4);
developer7e3c5b72022-02-21 21:38:33 +0800334+
developer87e071e2022-08-04 16:26:17 +0800335+ airoha_cl45_read(mbus, EN8801S_MDIO_PHY_ID, 0x1E, 0x324, &reg_value);
336+ GPHY_RG_1E_324.DATA = (u16)reg_value;
developer7e3c5b72022-02-21 21:38:33 +0800337+ GPHY_RG_1E_324.DataBitField.smi_det_deglitch_off = 0;
developer87e071e2022-08-04 16:26:17 +0800338+ airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1E, 0x324, (u32)GPHY_RG_1E_324.DATA);
339+ airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1E, 0x19E, 0xC2);
340+ airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1E, 0x013, 0x0);
developer7e3c5b72022-02-21 21:38:33 +0800341+
342+ /* EFUSE */
developer87e071e2022-08-04 16:26:17 +0800343+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x1C08, 0x40000040);
developer7e3c5b72022-02-21 21:38:33 +0800344+ retry = MAX_RETRY;
developer87e071e2022-08-04 16:26:17 +0800345+ while (0 != retry) {
developer7e3c5b72022-02-21 21:38:33 +0800346+ mdelay(1);
developer87e071e2022-08-04 16:26:17 +0800347+ reg_value = airoha_pbus_read(mbus, EN8801S_PBUS_PHY_ID, 0x1C08);
348+ if ((reg_value & (1 << 30)) == 0) {
developer7e3c5b72022-02-21 21:38:33 +0800349+ break;
350+ }
351+ retry--;
352+ }
developer87e071e2022-08-04 16:26:17 +0800353+ reg_value = airoha_pbus_read(mbus, EN8801S_PBUS_PHY_ID, 0x1C38); /* RAW#2 */
developer7e3c5b72022-02-21 21:38:33 +0800354+ GPHY_RG_1E_012.DataBitField.da_tx_i2mpb_a_tbt = reg_value & 0x03f;
developer87e071e2022-08-04 16:26:17 +0800355+ airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1E, 0x12, (u32)GPHY_RG_1E_012.DATA);
356+ GPHY_RG_1E_017.DataBitField.da_tx_i2mpb_b_tbt = (reg_value >> 8) & 0x03f;
357+ airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1E, 0x12, (u32)GPHY_RG_1E_017.DATA);
developer7e3c5b72022-02-21 21:38:33 +0800358+
developer87e071e2022-08-04 16:26:17 +0800359+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x1C08, 0x40400040);
developer7e3c5b72022-02-21 21:38:33 +0800360+ retry = MAX_RETRY;
developer87e071e2022-08-04 16:26:17 +0800361+ while (0 != retry) {
developer7e3c5b72022-02-21 21:38:33 +0800362+ mdelay(1);
developer87e071e2022-08-04 16:26:17 +0800363+ reg_value = airoha_pbus_read(mbus, EN8801S_PBUS_PHY_ID, 0x1C08);
364+ if ((reg_value & (1 << 30)) == 0) {
developer7e3c5b72022-02-21 21:38:33 +0800365+ break;
366+ }
367+ retry--;
368+ }
developer87e071e2022-08-04 16:26:17 +0800369+ reg_value = airoha_pbus_read(mbus, EN8801S_PBUS_PHY_ID, 0x1C30); /* RAW#16 */
developer7e3c5b72022-02-21 21:38:33 +0800370+ GPHY_RG_1E_324.DataBitField.smi_det_deglitch_off = (reg_value >> 12) & 0x01;
developer87e071e2022-08-04 16:26:17 +0800371+ airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1E, 0x324, (u32)GPHY_RG_1E_324.DATA);
developer7e3c5b72022-02-21 21:38:33 +0800372+
developer87e071e2022-08-04 16:26:17 +0800373+ en8801s_led_init(phydev);
374+
375+ phydev_info(phydev, "Phase2 initialize OK !\n");
developer7e3c5b72022-02-21 21:38:33 +0800376+ return 0;
377+}
378+
379+static int en8801s_read_status(struct phy_device *phydev)
380+{
developer87e071e2022-08-04 16:26:17 +0800381+ int ret, preSpeed = phydev->speed;
382+ struct mii_bus *mbus = phydev_mdio_bus(phydev);
383+ u32 reg_value;
developer7e3c5b72022-02-21 21:38:33 +0800384+
385+ ret = genphy_read_status(phydev);
developer87e071e2022-08-04 16:26:17 +0800386+ if (LINK_DOWN == phydev->link) preSpeed = phydev->speed = 0;
developer7e3c5b72022-02-21 21:38:33 +0800387+
developer87e071e2022-08-04 16:26:17 +0800388+ if (phydev->dev_flags == PHY_STATE_PROCESS) {
389+ en8801s_phy_process(phydev);
390+ phydev->dev_flags = PHY_STATE_DONE;
391+ }
392+
393+ if ((preSpeed != phydev->speed) && (LINK_UP == phydev->link)) {
developer7e3c5b72022-02-21 21:38:33 +0800394+ preSpeed = phydev->speed;
developer7e3c5b72022-02-21 21:38:33 +0800395+
developer87e071e2022-08-04 16:26:17 +0800396+ if (phydev->dev_flags == PHY_STATE_INIT) {
397+ en8801s_phase2_init(phydev);
398+ phydev->dev_flags = PHY_STATE_PROCESS;
developer7e3c5b72022-02-21 21:38:33 +0800399+ }
developer7e3c5b72022-02-21 21:38:33 +0800400+
developer87e071e2022-08-04 16:26:17 +0800401+ if (preSpeed == SPEED_10) {
402+ reg_value = airoha_pbus_read(mbus, EN8801S_PBUS_PHY_ID, 0x1694);
403+ reg_value |= (1 << 31);
404+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x1694, reg_value);
405+ phydev->dev_flags = PHY_STATE_PROCESS;
406+ } else {
407+ reg_value = airoha_pbus_read(mbus, EN8801S_PBUS_PHY_ID, 0x1694);
408+ reg_value &= ~(1 << 31);
409+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x1694, reg_value);
410+ phydev->dev_flags = PHY_STATE_PROCESS;
developer7e3c5b72022-02-21 21:38:33 +0800411+ }
developer87e071e2022-08-04 16:26:17 +0800412+
413+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x0600, 0x0c000c00);
414+ if (SPEED_1000 == preSpeed) {
415+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x10, 0xD801);
416+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x0, 0x9140);
developer7e3c5b72022-02-21 21:38:33 +0800417+
developer87e071e2022-08-04 16:26:17 +0800418+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x0A14, 0x0003);
419+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x0600, 0x0c000c00);
developer7e3c5b72022-02-21 21:38:33 +0800420+ mdelay(2); /* delay 2 ms */
developer87e071e2022-08-04 16:26:17 +0800421+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x1404, 0x004b);
422+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x140c, 0x0007);
423+ } else if (SPEED_100 == preSpeed) {
424+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x10, 0xD401);
425+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x0, 0x9140);
426+
427+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x0A14, 0x0007);
428+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x0600, 0x0c11);
429+ mdelay(2); /* delay 2 ms */
430+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x1404, 0x0027);
431+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x140c, 0x0007);
432+ } else if (SPEED_10 == preSpeed) {
433+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x10, 0xD001);
434+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x0, 0x9140);
435+
436+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x0A14, 0x000b);
437+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x0600, 0x0c11);
438+ mdelay(2); /* delay 2 ms */
439+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x1404, 0x0027);
440+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x140c, 0x0007);
developer7e3c5b72022-02-21 21:38:33 +0800441+ }
442+ }
443+ return ret;
444+}
445+
446+static struct phy_driver Airoha_driver[] = {
developer87e071e2022-08-04 16:26:17 +0800447+ {
448+ .phy_id = EN8801S_PHY_ID,
449+ .name = "Airoha EN8801S",
450+ .phy_id_mask = 0x0ffffff0,
451+ .features = PHY_GBIT_FEATURES,
452+ .config_init = en8801s_phase1_init,
453+ .config_aneg = genphy_config_aneg,
454+ .read_status = en8801s_read_status,
455+ .suspend = genphy_suspend,
456+ .resume = genphy_resume,
457+ }
458+};
developer7e3c5b72022-02-21 21:38:33 +0800459+
460+module_phy_driver(Airoha_driver);
461+
462+static struct mdio_device_id __maybe_unused Airoha_tbl[] = {
463+ { EN8801S_PHY_ID, 0x0ffffff0 },
464+ { }
465+};
466+
467+MODULE_DEVICE_TABLE(mdio, Airoha_tbl);
developer7e3c5b72022-02-21 21:38:33 +0800468Index: drivers/net/phy/en8801s.h
469===================================================================
470--- /dev/null
471+++ b/drivers/net/phy/en8801s.h
developer87e071e2022-08-04 16:26:17 +0800472@@ -0,0 +1,158 @@
developer7e3c5b72022-02-21 21:38:33 +0800473+// SPDX-License-Identifier: GPL-2.0
developer87e071e2022-08-04 16:26:17 +0800474+/* FILE NAME: en8801s.h
developer7e3c5b72022-02-21 21:38:33 +0800475+ * PURPOSE:
476+ * Define EN8801S driver function
477+ *
478+ * NOTES:
479+ *
480+ */
481+
developer87e071e2022-08-04 16:26:17 +0800482+#ifndef __EN8801S_H
483+#define __EN8801S_H
developer7e3c5b72022-02-21 21:38:33 +0800484+
485+/* NAMING DECLARATIONS
486+ */
developer87e071e2022-08-04 16:26:17 +0800487+#define EN8801S_DRIVER_VERSION "1.1.0"
488+
developer7e3c5b72022-02-21 21:38:33 +0800489+#define PHY_ADDRESS_RANGE 0x18
490+#define EN8801S_PBUS_DEFAULT_ID 0x1e
491+#define EN8801S_MDIO_PHY_ID 0x18 /* Range PHY_ADDRESS_RANGE .. 0x1e */
492+#define EN8801S_PBUS_PHY_ID (EN8801S_MDIO_PHY_ID + 1)
493+
494+#define EN8801S_RG_ETHER_PHY_OUI 0x19a4
495+#define EN8801S_RG_SMI_ADDR 0x19a8
496+#define EN8801S_RG_BUCK_CTL 0x1a20
497+#define EN8801S_RG_LTR_CTL 0x0cf8
498+
499+#define EN8801S_PBUS_OUI 0x17a5
500+#define EN8801S_PHY_ID1 0x03a2
501+#define EN8801S_PHY_ID2 0x9461
502+#define EN8801S_PHY_ID (unsigned long)((EN8801S_PHY_ID1 << 16) | EN8801S_PHY_ID2)
503+
504+#define DEV1E_REG013_VALUE 0
505+#define DEV1E_REG19E_VALUE 0xC2
506+#define DEV1E_REG324_VALUE 0x200
507+
508+#define TRUE 1
509+#define FALSE 0
510+#define LINK_UP 1
511+#define LINK_DOWN 0
512+
developer87e071e2022-08-04 16:26:17 +0800513+//#define TEST_BOARD
developer7e3c5b72022-02-21 21:38:33 +0800514+#if defined(TEST_BOARD)
developer87e071e2022-08-04 16:26:17 +0800515+/* SFP sample for verification */
developer7e3c5b72022-02-21 21:38:33 +0800516+#define EN8801S_TX_POLARITY 1
517+#define EN8801S_RX_POLARITY 0
518+#else
developer87e071e2022-08-04 16:26:17 +0800519+/* chip on board */
developer7e3c5b72022-02-21 21:38:33 +0800520+#define EN8801S_TX_POLARITY 0
developer87e071e2022-08-04 16:26:17 +0800521+#define EN8801S_RX_POLARITY 1 /* The pin default assignment is set to 1 */
developer7e3c5b72022-02-21 21:38:33 +0800522+#endif
523+
524+#define MAX_RETRY 5
525+#define MAX_OUI_CHECK 2
526+/* CL45 MDIO control */
527+#define MII_MMD_ACC_CTL_REG 0x0d
528+#define MII_MMD_ADDR_DATA_REG 0x0e
529+#define MMD_OP_MODE_DATA BIT(14)
530+
531+#define MAX_TRG_COUNTER 5
532+
533+/* CL22 Reg Support Page Select */
534+#define RgAddr_Reg1Fh 0x1f
535+#define CL22_Page_Reg 0x0000
536+#define CL22_Page_ExtReg 0x0001
537+#define CL22_Page_MiscReg 0x0002
538+#define CL22_Page_LpiReg 0x0003
539+#define CL22_Page_tReg 0x02A3
540+#define CL22_Page_TrReg 0x52B5
541+
542+/* CL45 Reg Support DEVID */
543+#define DEVID_03 0x03
544+#define DEVID_07 0x07
545+#define DEVID_1E 0x1E
546+#define DEVID_1F 0x1F
547+
548+/* TokenRing Reg Access */
549+#define TrReg_PKT_XMT_STA 0x8000
550+#define TrReg_WR 0x8000
551+#define TrReg_RD 0xA000
552+
553+#define RgAddr_LpiReg1Ch 0x1c
554+#define RgAddr_PMA_01h 0x0f82
555+#define RgAddr_PMA_18h 0x0fb0
556+#define RgAddr_DSPF_03h 0x1686
557+#define RgAddr_DSPF_06h 0x168c
558+#define RgAddr_DSPF_0Ch 0x1698
559+#define RgAddr_DSPF_0Dh 0x169a
560+#define RgAddr_DSPF_0Fh 0x169e
561+#define RgAddr_DSPF_10h 0x16a0
562+#define RgAddr_DSPF_11h 0x16a2
563+#define RgAddr_DSPF_14h 0x16a8
564+#define RgAddr_DSPF_1Ch 0x16b8
565+#define RgAddr_TR_26h 0x0ecc
566+#define RgAddr_R1000DEC_15h 0x03aa
567+
568+/* DATA TYPE DECLARATIONS
569+ */
570+typedef struct
571+{
572+ u16 DATA_Lo;
573+ u16 DATA_Hi;
574+}TR_DATA_T;
575+
576+typedef union
577+{
578+ struct
579+ {
580+ /* b[15:00] */
581+ u16 smi_deton_wt : 3;
582+ u16 smi_det_mdi_inv : 1;
583+ u16 smi_detoff_wt : 3;
584+ u16 smi_sigdet_debouncing_en : 1;
585+ u16 smi_deton_th : 6;
586+ u16 rsv_14 : 2;
587+ } DataBitField;
588+ u16 DATA;
589+} gephy_all_REG_LpiReg1Ch, *Pgephy_all_REG_LpiReg1Ch;
590+
591+typedef union
592+{
593+ struct
594+ {
595+ /* b[15:00] */
596+ u16 rg_smi_detcnt_max : 6;
597+ u16 rsv_6 : 2;
598+ u16 rg_smi_det_max_en : 1;
599+ u16 smi_det_deglitch_off : 1;
600+ u16 rsv_10 : 6;
601+ } DataBitField;
602+ u16 DATA;
603+} gephy_all_REG_dev1Eh_reg324h, *Pgephy_all_REG_dev1Eh_reg324h;
604+
605+typedef union
606+{
607+ struct
608+ {
609+ /* b[15:00] */
610+ u16 da_tx_i2mpb_a_tbt : 6;
611+ u16 rsv_6 : 4;
612+ u16 da_tx_i2mpb_a_gbe : 6;
613+ } DataBitField;
614+ u16 DATA;
615+} gephy_all_REG_dev1Eh_reg012h, *Pgephy_all_REG_dev1Eh_reg012h;
616+
617+typedef union
618+{
619+ struct
620+ {
621+ /* b[15:00] */
622+ u16 da_tx_i2mpb_b_tbt : 6;
623+ u16 rsv_6 : 2;
624+ u16 da_tx_i2mpb_b_gbe : 6;
625+ u16 rsv_14 : 2;
626+ } DataBitField;
627+ u16 DATA;
628+} gephy_all_REG_dev1Eh_reg017h, *Pgephy_all_REG_dev1Eh_reg017h;
629+
developer87e071e2022-08-04 16:26:17 +0800630+#endif /* End of __EN8801S_H */