blob: b50a7bbd3fb9b0cc7580ee07fd9adadc1bd62ce2 [file] [log] [blame]
developer8a021832022-08-02 23:47:49 +08001Index: drivers/net/phy/en8801sc.c
2===================================================================
3--- /dev/null
4+++ b/drivers/net/phy/en8801sc.c
developer878c5d62023-01-13 12:13:33 +08005@@ -0,0 +1,732 @@
developer8a021832022-08-02 23:47:49 +08006+// SPDX-License-Identifier: GPL-2.0
7+/* FILE NAME: en8801sc.c
8+ * PURPOSE:
developer878c5d62023-01-13 12:13:33 +08009+ * EN8801SC phy driver for Linux
developer8a021832022-08-02 23:47:49 +080010+ * NOTES:
11+ *
12+ */
13+
14+/* INCLUDE FILE DECLARATIONS
15+ */
16+
17+#include <linux/kernel.h>
18+#include <linux/string.h>
19+#include <linux/errno.h>
20+#include <linux/unistd.h>
21+#include <linux/interrupt.h>
22+#include <linux/init.h>
23+#include <linux/delay.h>
24+#include <linux/netdevice.h>
25+#include <linux/etherdevice.h>
26+#include <linux/skbuff.h>
27+#include <linux/spinlock.h>
28+#include <linux/mm.h>
29+#include <linux/module.h>
30+#include <linux/mii.h>
31+#include <linux/ethtool.h>
32+#include <linux/phy.h>
33+#include <linux/delay.h>
34+
35+#include <linux/uaccess.h>
36+#include <linux/version.h>
37+
38+#include "en8801sc.h"
39+
developer878c5d62023-01-13 12:13:33 +080040+MODULE_DESCRIPTION("Airoha EN8801S PHY drivers for MediaTek SoC");
developer8a021832022-08-02 23:47:49 +080041+MODULE_AUTHOR("Airoha");
42+MODULE_LICENSE("GPL");
43+
44+#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 5, 0))
45+#define phydev_mdio_bus(dev) ((dev)->bus)
46+#else
47+#define phydev_mdio_bus(dev) ((dev)->mdio.bus)
48+#endif
49+
50+enum {
51+ PHY_STATE_DONE = 0,
52+ PHY_STATE_INIT = 1,
53+ PHY_STATE_PROCESS = 2,
developer878c5d62023-01-13 12:13:33 +080054+ PHY_STATE_SS_FAIL = 3,
55+ PHY_STATE_FAIL = 4
developer8a021832022-08-02 23:47:49 +080056+};
57+
developer878c5d62023-01-13 12:13:33 +080058+/*
59+The following led_cfg example is for reference only.
60+LED5 1000M/LINK/ACT (GPIO5) <-> BASE_T_LED0,
61+LED6 10/100M/LINK/ACT (GPIO9) <-> BASE_T_LED1,
62+LED4 100M/LINK/ACT (GPIO8) <-> BASE_T_LED2,
63+*/
64+/* User-defined.B */
65+#define AIR_LED_SUPPORT
66+#ifdef AIR_LED_SUPPORT
67+static const AIR_BASE_T_LED_CFG_T led_cfg[4] =
68+{
69+ /*
70+ * LED Enable, GPIO, LED Polarity, LED ON, LED Blink
71+ */
72+ {LED_ENABLE, 5, AIR_ACTIVE_LOW, BASE_T_LED0_ON_CFG, BASE_T_LED0_BLK_CFG}, /* BASE-T LED0 */
73+ {LED_ENABLE, 9, AIR_ACTIVE_LOW, BASE_T_LED1_ON_CFG, BASE_T_LED1_BLK_CFG}, /* BASE-T LED1 */
74+ {LED_ENABLE, 8, AIR_ACTIVE_LOW, BASE_T_LED2_ON_CFG, BASE_T_LED2_BLK_CFG}, /* BASE-T LED2 */
75+ {LED_DISABLE, 1, AIR_ACTIVE_LOW, BASE_T_LED3_ON_CFG, BASE_T_LED3_BLK_CFG} /* BASE-T LED3 */
76+};
77+static const u16 led_dur = UNIT_LED_BLINK_DURATION << AIR_LED_BLK_DUR_64M;
78+#endif
79+/* User-defined.E */
80+
developer8a021832022-08-02 23:47:49 +080081+/************************************************************************
82+* F U N C T I O N S
83+************************************************************************/
developer878c5d62023-01-13 12:13:33 +080084+static int airoha_cl45_write(struct mii_bus *bus, u32 port, u32 devad, u32 reg, u16 val)
developer8a021832022-08-02 23:47:49 +080085+{
developer878c5d62023-01-13 12:13:33 +080086+ int ret = 0;
87+ struct device *dev = &bus->dev;
88+
89+ ret = mdiobus_write(bus, port, MII_MMD_ACC_CTL_REG, devad);
90+ AIR_RTN_ON_ERR_MSG(ret < 0, ret, "%s fail. (ret=%d)\n", __func__, ret);
91+ ret = mdiobus_write(bus, port, MII_MMD_ADDR_DATA_REG, reg);
92+ AIR_RTN_ON_ERR_MSG(ret < 0, ret, "%s fail. (ret=%d)\n", __func__, ret);
93+ ret = mdiobus_write(bus, port, MII_MMD_ACC_CTL_REG, MMD_OP_MODE_DATA | devad);
94+ AIR_RTN_ON_ERR_MSG(ret < 0, ret, "%s fail. (ret=%d)\n", __func__, ret);
95+ ret = mdiobus_write(bus, port, MII_MMD_ADDR_DATA_REG, val);
96+ AIR_RTN_ON_ERR_MSG(ret < 0, ret, "%s fail. (ret=%d)\n", __func__, ret);
97+ return ret;
developer8a021832022-08-02 23:47:49 +080098+}
99+
developer878c5d62023-01-13 12:13:33 +0800100+static int airoha_cl45_read(struct mii_bus *bus, u32 port, u32 devad, u32 reg, u16 *read_data)
developer8a021832022-08-02 23:47:49 +0800101+{
developer878c5d62023-01-13 12:13:33 +0800102+ int ret = 0;
103+ struct device *dev = &bus->dev;
104+
105+ ret = mdiobus_write(bus, port, MII_MMD_ACC_CTL_REG, devad);
106+ AIR_RTN_ON_ERR_MSG(ret < 0, ret, "%s fail. (ret=%d)\n", __func__, ret);
107+ ret = mdiobus_write(bus, port, MII_MMD_ADDR_DATA_REG, reg);
108+ AIR_RTN_ON_ERR_MSG(ret < 0, ret, "%s fail. (ret=%d)\n", __func__, ret);
109+ ret = mdiobus_write(bus, port, MII_MMD_ACC_CTL_REG, MMD_OP_MODE_DATA | devad);
110+ AIR_RTN_ON_ERR_MSG(ret < 0, ret, "%s fail. (ret=%d)\n", __func__, ret);
developer8a021832022-08-02 23:47:49 +0800111+ *read_data = mdiobus_read(bus, port, MII_MMD_ADDR_DATA_REG);
112+ return 0;
113+}
114+
developer878c5d62023-01-13 12:13:33 +0800115+static unsigned int airoha_cl22_read(struct mii_bus *ebus, unsigned int phy_addr, unsigned int phy_register, unsigned int *read_data)
developer8a021832022-08-02 23:47:49 +0800116+{
117+ *read_data = mdiobus_read(ebus, phy_addr, phy_register);
118+ return 0;
119+}
120+
developer878c5d62023-01-13 12:13:33 +0800121+static int airoha_cl22_write(struct mii_bus *ebus, unsigned int phy_addr, unsigned int phy_register, unsigned int write_data)
developer8a021832022-08-02 23:47:49 +0800122+{
developer878c5d62023-01-13 12:13:33 +0800123+ int ret = 0;
124+ struct device *dev = &ebus->dev;
125+
126+ ret = mdiobus_write(ebus, phy_addr, phy_register, write_data);
127+ AIR_RTN_ON_ERR_MSG(ret < 0, ret, "%s fail. (ret=%d)\n", __func__, ret);
128+ return ret;
developer8a021832022-08-02 23:47:49 +0800129+}
130+
developer878c5d62023-01-13 12:13:33 +0800131+static int airoha_pbus_write(struct mii_bus *ebus, unsigned long pbus_id, unsigned long pbus_address, unsigned long pbus_data)
developer8a021832022-08-02 23:47:49 +0800132+{
developer878c5d62023-01-13 12:13:33 +0800133+ int ret = 0;
134+
135+ ret = airoha_cl22_write(ebus, pbus_id, 0x1F, (unsigned int)(pbus_address >> 6));
136+ AIR_RTN_ERR(ret);
137+ ret = airoha_cl22_write(ebus, pbus_id, (unsigned int)((pbus_address >> 2) & 0xf), (unsigned int)(pbus_data & 0xFFFF));
138+ AIR_RTN_ERR(ret);
139+ ret = airoha_cl22_write(ebus, pbus_id, 0x10, (unsigned int)(pbus_data >> 16));
140+ AIR_RTN_ERR(ret);
141+ return ret;
developer8a021832022-08-02 23:47:49 +0800142+}
143+
developer878c5d62023-01-13 12:13:33 +0800144+static unsigned long airoha_pbus_read(struct mii_bus *ebus, unsigned long pbus_id, unsigned long pbus_address)
developer8a021832022-08-02 23:47:49 +0800145+{
146+ unsigned long pbus_data;
147+ unsigned int pbus_data_low, pbus_data_high;
developer878c5d62023-01-13 12:13:33 +0800148+ int ret = 0;
149+ struct device *dev = &ebus->dev;
150+ ret = airoha_cl22_write(ebus, pbus_id, 0x1F, (unsigned int)(pbus_address >> 6));
151+ if ( ret < 0) {
152+ AIR_RTN_ON_ERR_MSG(ret < 0, ret, "%s fail. (ret=%d)\n", __func__, ret);
153+ return INVALID_DATA;
154+ }
developer8a021832022-08-02 23:47:49 +0800155+ airoha_cl22_read(ebus, pbus_id, (unsigned int)((pbus_address >> 2) & 0xf), &pbus_data_low);
156+ airoha_cl22_read(ebus, pbus_id, 0x10, &pbus_data_high);
157+ pbus_data = (pbus_data_high << 16) + pbus_data_low;
158+ return pbus_data;
159+}
160+
161+/* Airoha Token Ring Write function */
developer878c5d62023-01-13 12:13:33 +0800162+static int airoha_tr_reg_write(struct mii_bus *ebus, unsigned long tr_address, unsigned long tr_data)
developer8a021832022-08-02 23:47:49 +0800163+{
developer878c5d62023-01-13 12:13:33 +0800164+ int ret = 0;
165+ ret = airoha_cl22_write(ebus, EN8801S_MDIO_PHY_ID, 0x1F, 0x52b5); /* page select */
166+ AIR_RTN_ERR(ret);
167+ ret = airoha_cl22_write(ebus, EN8801S_MDIO_PHY_ID, 0x11, (unsigned int)(tr_data & 0xffff));
168+ AIR_RTN_ERR(ret);
169+ ret = airoha_cl22_write(ebus, EN8801S_MDIO_PHY_ID, 0x12, (unsigned int)(tr_data >> 16));
170+ AIR_RTN_ERR(ret);
171+ ret = airoha_cl22_write(ebus, EN8801S_MDIO_PHY_ID, 0x10, (unsigned int)(tr_address | TrReg_WR));
172+ AIR_RTN_ERR(ret);
173+ ret = airoha_cl22_write(ebus, EN8801S_MDIO_PHY_ID, 0x1F, 0x0); /* page resetore */
174+ AIR_RTN_ERR(ret);
175+ return ret;
developer8a021832022-08-02 23:47:49 +0800176+}
177+
developer878c5d62023-01-13 12:13:33 +0800178+#if 0
developer8a021832022-08-02 23:47:49 +0800179+/* Airoha Token Ring Read function */
developer878c5d62023-01-13 12:13:33 +0800180+static unsigned long airoha_tr_reg_read(struct mii_bus *ebus, unsigned long tr_address)
developer8a021832022-08-02 23:47:49 +0800181+{
182+ unsigned long tr_data;
183+ unsigned int tr_data_low, tr_data_high;
184+
185+ airoha_cl22_write(ebus, EN8801S_MDIO_PHY_ID, 0x1F, 0x52b5); /* page select */
186+ airoha_cl22_write(ebus, EN8801S_MDIO_PHY_ID, 0x10, (unsigned int)(tr_address | TrReg_RD));
187+ airoha_cl22_read(ebus, EN8801S_MDIO_PHY_ID, 0x11, &tr_data_low);
188+ airoha_cl22_read(ebus, EN8801S_MDIO_PHY_ID, 0x12, &tr_data_high);
189+ airoha_cl22_write(ebus, EN8801S_MDIO_PHY_ID, 0x1F, 0x0); /* page resetore */
190+ tr_data = (tr_data_high << 16) + tr_data_low;
191+ return tr_data;
192+}
developer878c5d62023-01-13 12:13:33 +0800193+#endif
194+#ifdef AIR_LED_SUPPORT
195+static int airoha_led_set_usr_def(struct mii_bus *mbus, u8 entity, int polar,
196+ u16 on_evt, u16 blk_evt)
197+{
198+ int ret = 0;
199+ if (AIR_ACTIVE_HIGH == polar) {
200+ on_evt |= LED_ON_POL;
201+ } else {
202+ on_evt &= ~LED_ON_POL;
203+ }
204+ ret = airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1f, LED_ON_CTRL(entity), on_evt | LED_ON_EN);
205+ AIR_RTN_ERR(ret);
206+ ret = airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1f, LED_BLK_CTRL(entity), blk_evt);
207+ AIR_RTN_ERR(ret);
208+ return 0;
209+}
developer8a021832022-08-02 23:47:49 +0800210+
developer878c5d62023-01-13 12:13:33 +0800211+static int airoha_led_set_mode(struct mii_bus *mbus, u8 mode)
developer8a021832022-08-02 23:47:49 +0800212+{
developer878c5d62023-01-13 12:13:33 +0800213+ u16 cl45_data;
214+ int err = 0;
215+
216+ err = airoha_cl45_read(mbus, EN8801S_MDIO_PHY_ID, 0x1f, LED_BCR, &cl45_data);
217+ AIR_RTN_ERR(err);
218+
219+ switch (mode) {
220+ case AIR_LED_MODE_DISABLE:
221+ cl45_data &= ~LED_BCR_EXT_CTRL;
222+ cl45_data &= ~LED_BCR_MODE_MASK;
223+ cl45_data |= LED_BCR_MODE_DISABLE;
224+ break;
225+ case AIR_LED_MODE_USER_DEFINE:
226+ cl45_data |= LED_BCR_EXT_CTRL;
227+ cl45_data |= LED_BCR_CLK_EN;
228+ break;
229+ default:
230+ return -EINVAL;
231+ }
232+
233+ err = airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1f, LED_BCR, cl45_data);
234+ AIR_RTN_ERR(err);
235+ return 0;
developer8a021832022-08-02 23:47:49 +0800236+}
237+
developer878c5d62023-01-13 12:13:33 +0800238+static int airoha_led_set_state(struct mii_bus *mbus, u8 entity, u8 state)
239+{
240+ u16 cl45_data;
241+ int err;
242+
243+ err = airoha_cl45_read(mbus, EN8801S_MDIO_PHY_ID, 0x1f, LED_ON_CTRL(entity), &cl45_data);
244+ AIR_RTN_ERR(err);
245+ if (LED_ENABLE == state) {
246+ cl45_data |= LED_ON_EN;
247+ } else {
248+ cl45_data &= ~LED_ON_EN;
249+ }
250+
251+ err = airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1f, LED_ON_CTRL(entity), cl45_data);
252+ AIR_RTN_ERR(err);
253+ return 0;
254+}
255+
256+static int en8801s_led_init(struct phy_device *phydev)
257+{
258+
259+ unsigned long led_gpio = 0, reg_value = 0;
260+ int ret = 0, led_id;
261+ struct mii_bus *mbus = phydev_mdio_bus(phydev);
262+ int gpio_led_rg[3] = {0x1870, 0x1874, 0x1878};
263+ u16 cl45_data = led_dur;
264+ struct device *dev = &mbus->dev;
265+
266+ ret = airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1f, LED_BLK_DUR, cl45_data);
267+ AIR_RTN_ERR(ret);
268+ cl45_data >>= 1;
269+ ret = airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1f, LED_ON_DUR, cl45_data);
270+ AIR_RTN_ERR(ret);
271+ ret = airoha_led_set_mode(mbus, AIR_LED_MODE_USER_DEFINE);
272+ if (ret != 0) {
273+ dev_err(dev, "LED fail to set mode, ret %d !\n", ret);
274+ return ret;
275+ }
276+ for(led_id = 0; led_id < EN8801S_LED_COUNT; led_id++)
277+ {
278+ reg_value = 0;
279+ ret = airoha_led_set_state(mbus, led_id, led_cfg[led_id].en);
280+ if (ret != 0)
281+ {
282+ dev_err(dev, "LED fail to set state, ret %d !\n", ret);
283+ return ret;
284+ }
285+ if (LED_ENABLE == led_cfg[led_id].en)
286+ {
287+ if ( (led_cfg[led_id].gpio < 0) || led_cfg[led_id].gpio > 9)
288+ {
289+ dev_err(dev, "GPIO%d is out of range!! GPIO number is 0~9.\n", led_cfg[led_id].gpio);
290+ return -EIO;
291+ }
292+ led_gpio |= BIT(led_cfg[led_id].gpio);
293+ reg_value = airoha_pbus_read(mbus, EN8801S_PBUS_PHY_ID, gpio_led_rg[led_cfg[led_id].gpio / 4]);
294+ LED_SET_GPIO_SEL(led_cfg[led_id].gpio, led_id, reg_value);
295+ dev_dbg(dev, "[Airoha] gpio%d, reg_value 0x%lx\n", led_cfg[led_id].gpio, reg_value);
296+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, gpio_led_rg[led_cfg[led_id].gpio / 4], reg_value);
297+ AIR_RTN_ERR(ret);
298+ ret = airoha_led_set_usr_def(mbus, led_id, led_cfg[led_id].pol, led_cfg[led_id].on_cfg, led_cfg[led_id].blk_cfg);
299+ if (ret != 0)
300+ {
301+ dev_err(dev, "LED fail to set usr def, ret %d !\n", ret);
302+ return ret;
303+ }
304+ }
305+ }
306+ reg_value = (airoha_pbus_read(mbus, EN8801S_PBUS_PHY_ID, 0x1880) & ~led_gpio);
307+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x1880, reg_value);
308+ AIR_RTN_ERR(ret);
309+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x186c, led_gpio);
310+ AIR_RTN_ERR(ret);
311+
312+ dev_info(dev, "LED initialize OK !\n");
313+ return 0;
314+}
315+#endif
developer8a021832022-08-02 23:47:49 +0800316+static int en8801s_phy_process(struct phy_device *phydev)
317+{
318+ struct mii_bus *mbus = phydev_mdio_bus(phydev);
developer878c5d62023-01-13 12:13:33 +0800319+ unsigned long reg_value = 0;
320+ int ret = 0;
developer8a021832022-08-02 23:47:49 +0800321+
322+ reg_value = airoha_pbus_read(mbus, EN8801S_PBUS_PHY_ID, 0x19e0);
developer878c5d62023-01-13 12:13:33 +0800323+ reg_value |= BIT(0);
324+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x19e0, reg_value);
325+ AIR_RTN_ERR(ret);
developer8a021832022-08-02 23:47:49 +0800326+ reg_value = airoha_pbus_read(mbus, EN8801S_PBUS_PHY_ID, 0x19e0);
developer878c5d62023-01-13 12:13:33 +0800327+ reg_value &= ~BIT(0);
328+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x19e0, reg_value);
329+ AIR_RTN_ERR(ret);
330+ return ret;
developer8a021832022-08-02 23:47:49 +0800331+}
332+
333+static int en8801s_phase1_init(struct phy_device *phydev)
334+{
335+ unsigned long pbus_data;
336+ unsigned int pbusAddress;
developer878c5d62023-01-13 12:13:33 +0800337+ u16 reg_value;
338+ int retry, ret = 0;
developer8a021832022-08-02 23:47:49 +0800339+ struct mii_bus *mbus = phydev_mdio_bus(phydev);
developer878c5d62023-01-13 12:13:33 +0800340+ struct device *dev = &mbus->dev;
developer8a021832022-08-02 23:47:49 +0800341+ msleep(1500);
342+
343+ pbusAddress = EN8801S_PBUS_DEFAULT_ID;
344+ retry = MAX_OUI_CHECK;
developer878c5d62023-01-13 12:13:33 +0800345+ while (1) {
developer8a021832022-08-02 23:47:49 +0800346+ pbus_data = airoha_pbus_read(mbus, pbusAddress, EN8801S_RG_ETHER_PHY_OUI); /* PHY OUI */
developer878c5d62023-01-13 12:13:33 +0800347+ if (EN8801S_PBUS_OUI == pbus_data) {
developer8a021832022-08-02 23:47:49 +0800348+ pbus_data = airoha_pbus_read(mbus, pbusAddress, EN8801S_RG_SMI_ADDR); /* SMI ADDR */
developer878c5d62023-01-13 12:13:33 +0800349+ pbus_data = (pbus_data & 0xffff0000) | (unsigned long)(EN8801S_PBUS_PHY_ID << 8) | (unsigned long)(EN8801S_MDIO_PHY_ID);
350+ dev_info(dev, "SMI_ADDR=%lx (renew)\n", pbus_data);
351+ ret = airoha_pbus_write(mbus, pbusAddress, EN8801S_RG_SMI_ADDR, pbus_data);
352+ AIR_RTN_ERR(ret);
353+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, EN8801S_RG_BUCK_CTL, 0x03);
354+ AIR_RTN_ERR(ret);
developer8a021832022-08-02 23:47:49 +0800355+ mdelay(10);
356+ break;
developer878c5d62023-01-13 12:13:33 +0800357+ } else {
developer8a021832022-08-02 23:47:49 +0800358+ pbusAddress = EN8801S_PBUS_PHY_ID;
359+ }
developer878c5d62023-01-13 12:13:33 +0800360+ if (0 == --retry) {
361+ dev_err(dev, "Probe fail !\n");
developer8a021832022-08-02 23:47:49 +0800362+ return 0;
363+ }
364+ }
365+
developer878c5d62023-01-13 12:13:33 +0800366+ pbus_data = (airoha_pbus_read(mbus, EN8801S_PBUS_PHY_ID, EN8801S_RG_LTR_CTL) & 0xfffffffc) | 0x10 | (EN8801S_RX_POLARITY << 1) | EN8801S_TX_POLARITY;
367+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, EN8801S_RG_LTR_CTL, pbus_data);
368+ AIR_RTN_ERR(ret);
developer8a021832022-08-02 23:47:49 +0800369+ mdelay(10);
developer878c5d62023-01-13 12:13:33 +0800370+ pbus_data &= ~BIT(4);
371+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, EN8801S_RG_LTR_CTL, pbus_data);
372+ AIR_RTN_ERR(ret);
developer8a021832022-08-02 23:47:49 +0800373+
374+ retry = MAX_RETRY;
developer878c5d62023-01-13 12:13:33 +0800375+ while (1) {
developer8a021832022-08-02 23:47:49 +0800376+ mdelay(10);
377+ reg_value = phy_read(phydev, MII_PHYSID2);
developer878c5d62023-01-13 12:13:33 +0800378+ if (reg_value == EN8801S_PHY_ID2) {
developer8a021832022-08-02 23:47:49 +0800379+ break; /* wait GPHY ready */
380+ }
381+ retry--;
developer878c5d62023-01-13 12:13:33 +0800382+ if (0 == retry) {
383+ dev_err(dev, "Initialize fail !\n");
developer8a021832022-08-02 23:47:49 +0800384+ return 0;
385+ }
386+ }
387+ /* Software Reset PHY */
388+ reg_value = phy_read(phydev, MII_BMCR);
389+ reg_value |= BMCR_RESET;
developer878c5d62023-01-13 12:13:33 +0800390+ ret = phy_write(phydev, MII_BMCR, reg_value);
391+ AIR_RTN_ERR(ret);
developer8a021832022-08-02 23:47:49 +0800392+ retry = MAX_RETRY;
developer878c5d62023-01-13 12:13:33 +0800393+ do {
developer8a021832022-08-02 23:47:49 +0800394+ mdelay(10);
395+ reg_value = phy_read(phydev, MII_BMCR);
396+ retry--;
developer878c5d62023-01-13 12:13:33 +0800397+ if (0 == retry) {
398+ dev_err(dev, "Reset fail !\n");
developer8a021832022-08-02 23:47:49 +0800399+ return 0;
400+ }
401+ } while (reg_value & BMCR_RESET);
402+
developer878c5d62023-01-13 12:13:33 +0800403+ phydev->dev_flags = PHY_STATE_INIT;
404+
405+ dev_info(dev, "Phase1 initialize OK ! (%s)\n", EN8801S_DRIVER_VERSION);
developer8a021832022-08-02 23:47:49 +0800406+ return 0;
407+}
408+
409+static int en8801s_phase2_init(struct phy_device *phydev)
410+{
411+ gephy_all_REG_LpiReg1Ch GPHY_RG_LPI_1C;
412+ gephy_all_REG_dev1Eh_reg324h GPHY_RG_1E_324;
413+ gephy_all_REG_dev1Eh_reg012h GPHY_RG_1E_012;
414+ gephy_all_REG_dev1Eh_reg017h GPHY_RG_1E_017;
415+ unsigned long pbus_data;
developer878c5d62023-01-13 12:13:33 +0800416+ u16 cl45_value;
417+ int retry, ret = 0;
developer8a021832022-08-02 23:47:49 +0800418+ struct mii_bus *mbus = phydev_mdio_bus(phydev);
developer878c5d62023-01-13 12:13:33 +0800419+ struct device *dev = &mbus->dev;
developer8a021832022-08-02 23:47:49 +0800420+
developer878c5d62023-01-13 12:13:33 +0800421+ pbus_data = (airoha_pbus_read(mbus, EN8801S_PBUS_PHY_ID, EN8801S_RG_LTR_CTL) & 0xfffffffc) | 0x10 | (EN8801S_RX_POLARITY << 1) | EN8801S_TX_POLARITY;
422+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, EN8801S_RG_LTR_CTL, pbus_data);
423+ AIR_RTN_ERR(ret);
developer8a021832022-08-02 23:47:49 +0800424+ mdelay(10);
developer878c5d62023-01-13 12:13:33 +0800425+ pbus_data &= 0xffffffef;
426+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, EN8801S_RG_LTR_CTL, pbus_data);
427+ AIR_RTN_ERR(ret);
developer8a021832022-08-02 23:47:49 +0800428+
429+ pbus_data = airoha_pbus_read(mbus, EN8801S_PBUS_PHY_ID, 0x1690);
developer878c5d62023-01-13 12:13:33 +0800430+ pbus_data |= BIT(31);
431+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x1690, pbus_data);
432+ AIR_RTN_ERR(ret);
developer8a021832022-08-02 23:47:49 +0800433+
developer878c5d62023-01-13 12:13:33 +0800434+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x0600, 0x0c000c00);
435+ AIR_RTN_ERR(ret);
436+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x10, 0xD801);
437+ AIR_RTN_ERR(ret);
438+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x0, 0x9140);
439+ AIR_RTN_ERR(ret);
developer8a021832022-08-02 23:47:49 +0800440+
developer878c5d62023-01-13 12:13:33 +0800441+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x0A14, 0x0003);
442+ AIR_RTN_ERR(ret);
443+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x0600, 0x0c000c00);
444+ AIR_RTN_ERR(ret);
developer8a021832022-08-02 23:47:49 +0800445+ /* Set FCM control */
developer878c5d62023-01-13 12:13:33 +0800446+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x1404, 0x004b);
447+ AIR_RTN_ERR(ret);
448+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x140c, 0x0007);
449+ AIR_RTN_ERR(ret);
developer8a021832022-08-02 23:47:49 +0800450+
developer878c5d62023-01-13 12:13:33 +0800451+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x142c, 0x05050505);
452+ AIR_RTN_ERR(ret);
453+ pbus_data = airoha_pbus_read(mbus, EN8801S_PBUS_PHY_ID, 0x1440);
454+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x1440, pbus_data & ~BIT(11));
455+ AIR_RTN_ERR(ret);
456+
457+ pbus_data = airoha_pbus_read(mbus, EN8801S_PBUS_PHY_ID, 0x1408);
458+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x1408, pbus_data | BIT(5));
459+ AIR_RTN_ERR(ret);
460+
developer8a021832022-08-02 23:47:49 +0800461+ /* Set GPHY Perfomance*/
462+ /* Token Ring */
developer878c5d62023-01-13 12:13:33 +0800463+ ret = airoha_tr_reg_write(mbus, RgAddr_R1000DEC_15h, 0x0055A0);
464+ AIR_RTN_ERR(ret);
465+ ret = airoha_tr_reg_write(mbus, RgAddr_R1000DEC_17h, 0x07FF3F);
466+ AIR_RTN_ERR(ret);
467+ ret = airoha_tr_reg_write(mbus, RgAddr_PMA_00h, 0x00001E);
468+ AIR_RTN_ERR(ret);
469+ ret = airoha_tr_reg_write(mbus, RgAddr_PMA_01h, 0x6FB90A);
470+ AIR_RTN_ERR(ret);
471+ ret = airoha_tr_reg_write(mbus, RgAddr_PMA_17h, 0x060671);
472+ AIR_RTN_ERR(ret);
473+ ret = airoha_tr_reg_write(mbus, RgAddr_PMA_18h, 0x0E2F00);
474+ AIR_RTN_ERR(ret);
475+ ret = airoha_tr_reg_write(mbus, RgAddr_TR_26h, 0x444444);
476+ AIR_RTN_ERR(ret);
477+ ret = airoha_tr_reg_write(mbus, RgAddr_DSPF_03h, 0x000000);
478+ AIR_RTN_ERR(ret);
479+ ret = airoha_tr_reg_write(mbus, RgAddr_DSPF_06h, 0x2EBAEF);
480+ AIR_RTN_ERR(ret);
481+ ret = airoha_tr_reg_write(mbus, RgAddr_DSPF_08h, 0x00000B);
482+ AIR_RTN_ERR(ret);
483+ ret = airoha_tr_reg_write(mbus, RgAddr_DSPF_0Ch, 0x00504D);
484+ AIR_RTN_ERR(ret);
485+ ret = airoha_tr_reg_write(mbus, RgAddr_DSPF_0Dh, 0x02314F);
486+ AIR_RTN_ERR(ret);
487+ ret = airoha_tr_reg_write(mbus, RgAddr_DSPF_0Fh, 0x003028);
488+ AIR_RTN_ERR(ret);
489+ ret = airoha_tr_reg_write(mbus, RgAddr_DSPF_10h, 0x005010);
490+ AIR_RTN_ERR(ret);
491+ ret = airoha_tr_reg_write(mbus, RgAddr_DSPF_11h, 0x040001);
492+ AIR_RTN_ERR(ret);
493+ ret = airoha_tr_reg_write(mbus, RgAddr_DSPF_13h, 0x018670);
494+ AIR_RTN_ERR(ret);
495+ ret = airoha_tr_reg_write(mbus, RgAddr_DSPF_14h, 0x00024A);
496+ AIR_RTN_ERR(ret);
497+ ret = airoha_tr_reg_write(mbus, RgAddr_DSPF_1Bh, 0x000072);
498+ AIR_RTN_ERR(ret);
499+ ret = airoha_tr_reg_write(mbus, RgAddr_DSPF_1Ch, 0x003210);
500+ AIR_RTN_ERR(ret);
501+
developer8a021832022-08-02 23:47:49 +0800502+ /* CL22 & CL45 */
developer878c5d62023-01-13 12:13:33 +0800503+ ret = phy_write(phydev, 0x1f, 0x03);
504+ AIR_RTN_ERR(ret);
505+ GPHY_RG_LPI_1C.DATA = phy_read(phydev, RgAddr_LPI_1Ch);
developer8a021832022-08-02 23:47:49 +0800506+ GPHY_RG_LPI_1C.DataBitField.smi_deton_th = 0x0C;
developer878c5d62023-01-13 12:13:33 +0800507+ ret = phy_write(phydev, RgAddr_LPI_1Ch, GPHY_RG_LPI_1C.DATA);
508+ AIR_RTN_ERR(ret);
509+ ret = phy_write(phydev, RgAddr_LPI_1Ch, 0xC92);
510+ AIR_RTN_ERR(ret);
511+ ret = phy_write(phydev, RgAddr_AUXILIARY_1Dh, 0x1);
512+ AIR_RTN_ERR(ret);
513+ ret = phy_write(phydev, 0x1f, 0x0);
514+ AIR_RTN_ERR(ret);
515+ ret = airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1E, 0x120, 0x8014);
516+ AIR_RTN_ERR(ret);
517+ ret = airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1E, 0x122, 0xffff);
518+ AIR_RTN_ERR(ret);
519+ ret = airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1E, 0x123, 0xffff);
520+ AIR_RTN_ERR(ret);
521+ ret = airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1E, 0x144, 0x0200);
522+ AIR_RTN_ERR(ret);
523+ ret = airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1E, 0x14A, 0xEE20);
524+ AIR_RTN_ERR(ret);
525+ ret = airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1E, 0x189, 0x0110);
526+ AIR_RTN_ERR(ret);
527+ ret = airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1E, 0x19B, 0x0111);
528+ AIR_RTN_ERR(ret);
529+ ret = airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1E, 0x234, 0x0181);
530+ AIR_RTN_ERR(ret);
531+ ret = airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1E, 0x238, 0x0120);
532+ AIR_RTN_ERR(ret);
533+ ret = airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1E, 0x239, 0x0117);
534+ AIR_RTN_ERR(ret);
535+ ret = airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1E, 0x268, 0x07F4);
536+ AIR_RTN_ERR(ret);
537+ ret = airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1E, 0x2D1, 0x0733);
538+ AIR_RTN_ERR(ret);
539+ ret = airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1E, 0x323, 0x0011);
540+ AIR_RTN_ERR(ret);
541+ ret = airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1E, 0x324, 0x013F);
542+ AIR_RTN_ERR(ret);
543+ ret = airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1E, 0x326, 0x0037);
544+ AIR_RTN_ERR(ret);
developer8a021832022-08-02 23:47:49 +0800545+
developer878c5d62023-01-13 12:13:33 +0800546+ ret = airoha_cl45_read(mbus, EN8801S_MDIO_PHY_ID, 0x1E, 0x324, &cl45_value);
547+ AIR_RTN_ERR(ret);
548+ GPHY_RG_1E_324.DATA = cl45_value;
developer8a021832022-08-02 23:47:49 +0800549+ GPHY_RG_1E_324.DataBitField.smi_det_deglitch_off = 0;
developer878c5d62023-01-13 12:13:33 +0800550+ ret = airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1E, 0x324, GPHY_RG_1E_324.DATA);
551+ AIR_RTN_ERR(ret);
552+ ret = airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1E, 0x19E, 0xC2);
553+ AIR_RTN_ERR(ret);
554+ ret = airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1E, 0x013, 0x0);
555+ AIR_RTN_ERR(ret);
developer8a021832022-08-02 23:47:49 +0800556+
557+ /* EFUSE */
558+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x1C08, 0x40000040);
559+ retry = MAX_RETRY;
developer878c5d62023-01-13 12:13:33 +0800560+ while (0 != retry) {
developer8a021832022-08-02 23:47:49 +0800561+ mdelay(1);
developer878c5d62023-01-13 12:13:33 +0800562+ pbus_data = airoha_pbus_read(mbus, EN8801S_PBUS_PHY_ID, 0x1C08);
563+ if ((pbus_data & BIT(30)) == 0) {
developer8a021832022-08-02 23:47:49 +0800564+ break;
565+ }
566+ retry--;
567+ }
developer878c5d62023-01-13 12:13:33 +0800568+ pbus_data = airoha_pbus_read(mbus, EN8801S_PBUS_PHY_ID, 0x1C38); /* RAW#2 */
569+ GPHY_RG_1E_012.DataBitField.da_tx_i2mpb_a_tbt = (u16)(pbus_data & 0x03f);
570+ ret = airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1E, 0x12, GPHY_RG_1E_012.DATA);
571+ AIR_RTN_ERR(ret);
572+ GPHY_RG_1E_017.DataBitField.da_tx_i2mpb_b_tbt = (u16)((pbus_data >> 8) & 0x03f);
573+ ret = airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1E, 0x12, GPHY_RG_1E_017.DATA);
574+ AIR_RTN_ERR(ret);
developer8a021832022-08-02 23:47:49 +0800575+
576+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x1C08, 0x40400040);
577+ retry = MAX_RETRY;
developer878c5d62023-01-13 12:13:33 +0800578+ while (0 != retry) {
developer8a021832022-08-02 23:47:49 +0800579+ mdelay(1);
developer878c5d62023-01-13 12:13:33 +0800580+ pbus_data = airoha_pbus_read(mbus, EN8801S_PBUS_PHY_ID, 0x1C08);
581+ if ((pbus_data & BIT(30)) == 0) {
developer8a021832022-08-02 23:47:49 +0800582+ break;
583+ }
584+ retry--;
585+ }
developer878c5d62023-01-13 12:13:33 +0800586+ pbus_data = airoha_pbus_read(mbus, EN8801S_PBUS_PHY_ID, 0x1C30); /* RAW#16 */
587+ GPHY_RG_1E_324.DataBitField.smi_det_deglitch_off = (u16)((pbus_data >> 12) & 0x01);
588+ ret = airoha_cl45_write(mbus, EN8801S_MDIO_PHY_ID, 0x1E, 0x324, GPHY_RG_1E_324.DATA);
589+ AIR_RTN_ERR(ret);
590+#ifdef AIR_LED_SUPPORT
591+ ret = en8801s_led_init(phydev);
592+ if (ret != 0){
593+ dev_err(dev, "en8801s_led_init fail (ret:%d) !\n", ret);
594+ }
595+#endif
596+ pbus_data = airoha_pbus_read(mbus, EN8801S_PBUS_PHY_ID, 0x1960);
597+ pbus_data -= (2 << 22);
598+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x1960, pbus_data);
599+ mdelay(10);
600+ pbus_data -= (2 << 22);
601+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x1960, pbus_data);
developer8a021832022-08-02 23:47:49 +0800602+
developer878c5d62023-01-13 12:13:33 +0800603+ dev_info(dev, "Phase2 initialize OK !\n");
developer8a021832022-08-02 23:47:49 +0800604+ return 0;
605+}
606+
607+static int en8801s_read_status(struct phy_device *phydev)
608+{
developer878c5d62023-01-13 12:13:33 +0800609+ int ret = 0, preSpeed = phydev->speed, retry = MAX_RETRY;
developer8a021832022-08-02 23:47:49 +0800610+ struct mii_bus *mbus = phydev_mdio_bus(phydev);
611+ u32 reg_value;
developer878c5d62023-01-13 12:13:33 +0800612+ struct device *dev = &mbus->dev;
developer8a021832022-08-02 23:47:49 +0800613+
614+ ret = genphy_read_status(phydev);
developer878c5d62023-01-13 12:13:33 +0800615+ if (LINK_DOWN == phydev->link) preSpeed = phydev->speed = 0;
developer8a021832022-08-02 23:47:49 +0800616+
developer878c5d62023-01-13 12:13:33 +0800617+ if (phydev->dev_flags == PHY_STATE_PROCESS) {
developer8a021832022-08-02 23:47:49 +0800618+ en8801s_phy_process(phydev);
developer878c5d62023-01-13 12:13:33 +0800619+ phydev->dev_flags = PHY_STATE_DONE;
developer8a021832022-08-02 23:47:49 +0800620+ }
621+
developer878c5d62023-01-13 12:13:33 +0800622+ if (phydev->dev_flags == PHY_STATE_INIT) {
623+ do {
624+ mdelay(100);
625+ reg_value = airoha_pbus_read(mbus, EN8801S_PBUS_PHY_ID, 0xb04);
626+ dev_dbg(dev, "[Airoha] 0xB04, reg_value 0x%x\n", reg_value);
627+ reg_value &= 0x21;
628+ if(reg_value == 0x21) {
629+ ret = en8801s_phase2_init(phydev);
630+ if (ret != 0) {
631+ dev_info(dev, "en8801_phase2_init failed\n");
632+ phydev->dev_flags = PHY_STATE_FAIL;
633+ return -1;
634+ } else {
635+ phydev->dev_flags = PHY_STATE_PROCESS;
636+ break;
637+ }
developer8a021832022-08-02 23:47:49 +0800638+
developer878c5d62023-01-13 12:13:33 +0800639+ }
640+ if(0 == --retry) {
641+ dev_err(dev, "0xB04 return 0x%x !\n", reg_value);
642+ phydev->dev_flags = PHY_STATE_SS_FAIL;
643+ return -1;
644+ }
645+ } while(retry);
646+ }
647+
648+ if ((preSpeed != phydev->speed) && (LINK_UP == phydev->link)) {
649+ preSpeed = phydev->speed;
developer8a021832022-08-02 23:47:49 +0800650+
651+ if (preSpeed == SPEED_10) {
652+ reg_value = airoha_pbus_read(mbus, EN8801S_PBUS_PHY_ID, 0x1694);
developer878c5d62023-01-13 12:13:33 +0800653+ reg_value |= BIT(31);
654+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x1694, reg_value);
655+ AIR_RTN_ERR(ret);
656+ phydev->dev_flags = PHY_STATE_PROCESS;
developer8a021832022-08-02 23:47:49 +0800657+ } else {
658+ reg_value = airoha_pbus_read(mbus, EN8801S_PBUS_PHY_ID, 0x1694);
developer878c5d62023-01-13 12:13:33 +0800659+ reg_value &= ~BIT(31);
660+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x1694, reg_value);
661+ AIR_RTN_ERR(ret);
662+ phydev->dev_flags = PHY_STATE_PROCESS;
developer8a021832022-08-02 23:47:49 +0800663+ }
664+
665+ airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x0600, 0x0c000c00);
developer878c5d62023-01-13 12:13:33 +0800666+ if (SPEED_1000 == preSpeed) {
667+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x10, 0xD801);
668+ AIR_RTN_ERR(ret);
669+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x0, 0x9140);
670+ AIR_RTN_ERR(ret);
developer8a021832022-08-02 23:47:49 +0800671+
developer878c5d62023-01-13 12:13:33 +0800672+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x0A14, 0x0003);
673+ AIR_RTN_ERR(ret);
674+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x0600, 0x0c000c00);
675+ AIR_RTN_ERR(ret);
developer8a021832022-08-02 23:47:49 +0800676+ mdelay(2); /* delay 2 ms */
developer878c5d62023-01-13 12:13:33 +0800677+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x1404, 0x004b);
678+ AIR_RTN_ERR(ret);
679+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x140c, 0x0007);
680+ AIR_RTN_ERR(ret);
681+ } else if (SPEED_100 == preSpeed) {
682+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x10, 0xD401);
683+ AIR_RTN_ERR(ret);
684+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x0, 0x9140);
685+ AIR_RTN_ERR(ret);
developer8a021832022-08-02 23:47:49 +0800686+
developer878c5d62023-01-13 12:13:33 +0800687+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x0A14, 0x0007);
688+ AIR_RTN_ERR(ret);
689+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x0600, 0x0c11);
690+ AIR_RTN_ERR(ret);
developer8a021832022-08-02 23:47:49 +0800691+ mdelay(2); /* delay 2 ms */
developer878c5d62023-01-13 12:13:33 +0800692+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x1404, 0x0027);
693+ AIR_RTN_ERR(ret);
694+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x140c, 0x0007);
695+ AIR_RTN_ERR(ret);
696+ } else if (SPEED_10 == preSpeed) {
697+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x10, 0xD001);
698+ AIR_RTN_ERR(ret);
699+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x0, 0x9140);
700+ AIR_RTN_ERR(ret);
developer8a021832022-08-02 23:47:49 +0800701+
developer878c5d62023-01-13 12:13:33 +0800702+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x0A14, 0x000b);
703+ AIR_RTN_ERR(ret);
704+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x0600, 0x0c11);
705+ AIR_RTN_ERR(ret);
developer8a021832022-08-02 23:47:49 +0800706+ mdelay(2); /* delay 2 ms */
developer878c5d62023-01-13 12:13:33 +0800707+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x1404, 0x0027);
708+ AIR_RTN_ERR(ret);
709+ ret = airoha_pbus_write(mbus, EN8801S_PBUS_PHY_ID, 0x140c, 0x0007);
710+ AIR_RTN_ERR(ret);
developer8a021832022-08-02 23:47:49 +0800711+ }
712+ }
713+ return ret;
714+}
715+
716+static struct phy_driver Airoha_driver[] = {
developer878c5d62023-01-13 12:13:33 +0800717+ {
718+ .phy_id = EN8801SC_PHY_ID,
719+ .name = "Airoha EN8801SC",
720+ .phy_id_mask = 0x0ffffff0,
721+ .features = PHY_GBIT_FEATURES,
722+ .config_init = en8801s_phase1_init,
723+ .config_aneg = genphy_config_aneg,
724+ .read_status = en8801s_read_status,
725+ .suspend = genphy_suspend,
726+ .resume = genphy_resume,
727+ }
728+};
developer8a021832022-08-02 23:47:49 +0800729+
730+module_phy_driver(Airoha_driver);
731+
732+static struct mdio_device_id __maybe_unused Airoha_tbl[] = {
developer878c5d62023-01-13 12:13:33 +0800733+ { EN8801SC_PHY_ID, 0x0ffffff0 },
developer8a021832022-08-02 23:47:49 +0800734+ { }
735+};
736+
737+MODULE_DEVICE_TABLE(mdio, Airoha_tbl);
738Index: drivers/net/phy/en8801sc.h
739===================================================================
740--- /dev/null
741+++ b/drivers/net/phy/en8801sc.h
developer878c5d62023-01-13 12:13:33 +0800742@@ -0,0 +1,276 @@
developer8a021832022-08-02 23:47:49 +0800743+// SPDX-License-Identifier: GPL-2.0
744+/* FILE NAME: en8801sc.h
745+ * PURPOSE:
developer878c5d62023-01-13 12:13:33 +0800746+ * Define EN8801SC driver function
developer8a021832022-08-02 23:47:49 +0800747+ *
748+ * NOTES:
749+ *
750+ */
751+
developer878c5d62023-01-13 12:13:33 +0800752+#ifndef __EN8801SC_H
753+#define __EN8801SC_H
developer8a021832022-08-02 23:47:49 +0800754+
755+/* NAMING DECLARATIONS
756+ */
developer878c5d62023-01-13 12:13:33 +0800757+#define EN8801S_DRIVER_VERSION "1.1.5"
developer8a021832022-08-02 23:47:49 +0800758+
759+#define PHY_ADDRESS_RANGE 0x18
760+#define EN8801S_PBUS_DEFAULT_ID 0x1e
761+#define EN8801S_MDIO_PHY_ID 0x18 /* Range PHY_ADDRESS_RANGE .. 0x1e */
762+#define EN8801S_PBUS_PHY_ID (EN8801S_MDIO_PHY_ID + 1)
763+
764+#define EN8801S_RG_ETHER_PHY_OUI 0x19a4
765+#define EN8801S_RG_SMI_ADDR 0x19a8
766+#define EN8801S_RG_BUCK_CTL 0x1a20
767+#define EN8801S_RG_LTR_CTL 0x0cf8
768+
769+#define EN8801S_PBUS_OUI 0x17a5
770+#define EN8801S_PHY_ID1 0x03a2
developer878c5d62023-01-13 12:13:33 +0800771+#define EN8801S_PHY_ID2 0x9461
772+#define EN8801SC_PHY_ID 0x03a29471
developer8a021832022-08-02 23:47:49 +0800773+
developer878c5d62023-01-13 12:13:33 +0800774+#define LED_ON_CTRL(i) (0x024 + ((i)*2))
775+#define LED_ON_EN (1 << 15)
776+#define LED_ON_POL (1 << 14)
777+#define LED_ON_EVT_MASK (0x7f)
778+/* LED ON Event Option.B */
779+#define LED_ON_EVT_FORCE (1 << 6)
780+#define LED_ON_EVT_LINK_DOWN (1 << 3)
781+#define LED_ON_EVT_LINK_10M (1 << 2)
782+#define LED_ON_EVT_LINK_100M (1 << 1)
783+#define LED_ON_EVT_LINK_1000M (1 << 0)
784+/* LED ON Event Option.E */
developer8a021832022-08-02 23:47:49 +0800785+
developer878c5d62023-01-13 12:13:33 +0800786+#define LED_BLK_CTRL(i) (0x025 + ((i)*2))
787+#define LED_BLK_EVT_MASK (0x3ff)
788+/* LED Blinking Event Option.B*/
789+#define LED_BLK_EVT_FORCE (1 << 9)
790+#define LED_BLK_EVT_10M_RX_ACT (1 << 5)
791+#define LED_BLK_EVT_10M_TX_ACT (1 << 4)
792+#define LED_BLK_EVT_100M_RX_ACT (1 << 3)
793+#define LED_BLK_EVT_100M_TX_ACT (1 << 2)
794+#define LED_BLK_EVT_1000M_RX_ACT (1 << 1)
795+#define LED_BLK_EVT_1000M_TX_ACT (1 << 0)
796+/* LED Blinking Event Option.E*/
797+#define LED_ENABLE 1
798+#define LED_DISABLE 0
799+
developer8a021832022-08-02 23:47:49 +0800800+#define LINK_UP 1
801+#define LINK_DOWN 0
802+
803+//#define TEST_BOARD
804+#if defined(TEST_BOARD)
805+/* SFP sample for verification */
806+#define EN8801S_TX_POLARITY 1
807+#define EN8801S_RX_POLARITY 0
808+#else
809+/* chip on board */
810+#define EN8801S_TX_POLARITY 0
811+#define EN8801S_RX_POLARITY 1 /* The pin default assignment is set to 1 */
812+#endif
813+
developer878c5d62023-01-13 12:13:33 +0800814+/*
815+The following led_cfg example is for reference only.
816+LED5 1000M/LINK/ACT (GPIO5) <-> BASE_T_LED0,
817+LED6 10/100M/LINK/ACT(GPIO9) <-> BASE_T_LED1,
818+LED4 100M/LINK/ACT (GPIO8) <-> BASE_T_LED2,
819+*/
820+/* User-defined.B */
821+#define BASE_T_LED0_ON_CFG (LED_ON_EVT_LINK_1000M)
822+#define BASE_T_LED0_BLK_CFG (LED_BLK_EVT_1000M_TX_ACT | LED_BLK_EVT_1000M_RX_ACT)
823+#define BASE_T_LED1_ON_CFG (LED_ON_EVT_LINK_100M | LED_ON_EVT_LINK_10M)
824+#define BASE_T_LED1_BLK_CFG (LED_BLK_EVT_100M_TX_ACT | LED_BLK_EVT_100M_RX_ACT | \
825+ LED_BLK_EVT_10M_TX_ACT | LED_BLK_EVT_10M_RX_ACT )
826+#define BASE_T_LED2_ON_CFG (LED_ON_EVT_LINK_100M)
827+#define BASE_T_LED2_BLK_CFG (LED_BLK_EVT_100M_TX_ACT | LED_BLK_EVT_100M_RX_ACT)
828+#define BASE_T_LED3_ON_CFG (0x0)
829+#define BASE_T_LED3_BLK_CFG (0x0)
830+/* User-defined.E */
831+
832+#define EN8801S_LED_COUNT 4
833+
developer8a021832022-08-02 23:47:49 +0800834+#define MAX_RETRY 5
835+#define MAX_OUI_CHECK 2
836+/* CL45 MDIO control */
837+#define MII_MMD_ACC_CTL_REG 0x0d
838+#define MII_MMD_ADDR_DATA_REG 0x0e
839+#define MMD_OP_MODE_DATA BIT(14)
840+
841+#define MAX_TRG_COUNTER 5
842+
843+/* CL22 Reg Support Page Select */
844+#define RgAddr_Reg1Fh 0x1f
845+#define CL22_Page_Reg 0x0000
846+#define CL22_Page_ExtReg 0x0001
847+#define CL22_Page_MiscReg 0x0002
848+#define CL22_Page_LpiReg 0x0003
849+#define CL22_Page_tReg 0x02A3
850+#define CL22_Page_TrReg 0x52B5
851+
852+/* CL45 Reg Support DEVID */
853+#define DEVID_03 0x03
854+#define DEVID_07 0x07
855+#define DEVID_1E 0x1E
856+#define DEVID_1F 0x1F
857+
858+/* TokenRing Reg Access */
859+#define TrReg_PKT_XMT_STA 0x8000
860+#define TrReg_WR 0x8000
861+#define TrReg_RD 0xA000
862+
developer878c5d62023-01-13 12:13:33 +0800863+#define RgAddr_LPI_1Ch 0x1c
864+#define RgAddr_AUXILIARY_1Dh 0x1d
865+#define RgAddr_PMA_00h 0x0f80
developer8a021832022-08-02 23:47:49 +0800866+#define RgAddr_PMA_01h 0x0f82
developer878c5d62023-01-13 12:13:33 +0800867+#define RgAddr_PMA_17h 0x0fae
developer8a021832022-08-02 23:47:49 +0800868+#define RgAddr_PMA_18h 0x0fb0
869+#define RgAddr_DSPF_03h 0x1686
870+#define RgAddr_DSPF_06h 0x168c
developer878c5d62023-01-13 12:13:33 +0800871+#define RgAddr_DSPF_08h 0x1690
developer8a021832022-08-02 23:47:49 +0800872+#define RgAddr_DSPF_0Ch 0x1698
873+#define RgAddr_DSPF_0Dh 0x169a
874+#define RgAddr_DSPF_0Fh 0x169e
875+#define RgAddr_DSPF_10h 0x16a0
876+#define RgAddr_DSPF_11h 0x16a2
developer878c5d62023-01-13 12:13:33 +0800877+#define RgAddr_DSPF_13h 0x16a6
developer8a021832022-08-02 23:47:49 +0800878+#define RgAddr_DSPF_14h 0x16a8
developer878c5d62023-01-13 12:13:33 +0800879+#define RgAddr_DSPF_1Bh 0x16b6
developer8a021832022-08-02 23:47:49 +0800880+#define RgAddr_DSPF_1Ch 0x16b8
881+#define RgAddr_TR_26h 0x0ecc
882+#define RgAddr_R1000DEC_15h 0x03aa
developer878c5d62023-01-13 12:13:33 +0800883+#define RgAddr_R1000DEC_17h 0x03ae
884+
885+#define LED_BCR (0x021)
886+#define LED_BCR_EXT_CTRL (1 << 15)
887+#define LED_BCR_CLK_EN (1 << 3)
888+#define LED_BCR_TIME_TEST (1 << 2)
889+#define LED_BCR_MODE_MASK (3)
890+#define LED_BCR_MODE_DISABLE (0)
891+
892+#define LED_ON_DUR (0x022)
893+#define LED_ON_DUR_MASK (0xffff)
894+
895+#define LED_BLK_DUR (0x023)
896+#define LED_BLK_DUR_MASK (0xffff)
897+
898+#define LED_GPIO_SEL_MASK 0x7FFFFFF
899+
900+#define UNIT_LED_BLINK_DURATION 1024
901+
902+/* Invalid data */
903+#define INVALID_DATA 0xffffffff
904+
905+#define AIR_RTN_ON_ERR(cond, err) \
906+ do { if ((cond)) return (err); } while(0)
907+
908+#define AIR_RTN_ERR(err) AIR_RTN_ON_ERR(err < 0, err)
909+#define AIR_RTN_ON_ERR_MSG(cond, err, msg...) \
910+ do { if ((cond)) { dev_err(dev, ##msg); return (err); } } while(0)
911+
912+#define LED_SET_EVT(reg, cod, result, bit) do \
913+ { \
914+ if(reg & cod) { \
915+ result |= bit; \
916+ } \
917+ } while(0)
918+
919+#define LED_SET_GPIO_SEL(gpio, led, val) do \
920+ { \
921+ val |= (led << (8 * (gpio % 4))); \
922+ } while(0)
developer8a021832022-08-02 23:47:49 +0800923+
924+/* DATA TYPE DECLARATIONS
925+ */
developer878c5d62023-01-13 12:13:33 +0800926+typedef struct AIR_BASE_T_LED_CFG_S
927+{
928+ u16 en;
929+ u16 gpio;
930+ u16 pol;
931+ u16 on_cfg;
932+ u16 blk_cfg;
933+}AIR_BASE_T_LED_CFG_T;
934+
developer8a021832022-08-02 23:47:49 +0800935+typedef struct
936+{
937+ u16 DATA_Lo;
938+ u16 DATA_Hi;
939+}TR_DATA_T;
940+
941+typedef union
942+{
943+ struct
944+ {
945+ /* b[15:00] */
946+ u16 smi_deton_wt : 3;
947+ u16 smi_det_mdi_inv : 1;
948+ u16 smi_detoff_wt : 3;
949+ u16 smi_sigdet_debouncing_en : 1;
950+ u16 smi_deton_th : 6;
951+ u16 rsv_14 : 2;
952+ } DataBitField;
953+ u16 DATA;
954+} gephy_all_REG_LpiReg1Ch, *Pgephy_all_REG_LpiReg1Ch;
955+
956+typedef union
957+{
958+ struct
959+ {
960+ /* b[15:00] */
961+ u16 rg_smi_detcnt_max : 6;
962+ u16 rsv_6 : 2;
963+ u16 rg_smi_det_max_en : 1;
964+ u16 smi_det_deglitch_off : 1;
965+ u16 rsv_10 : 6;
966+ } DataBitField;
967+ u16 DATA;
968+} gephy_all_REG_dev1Eh_reg324h, *Pgephy_all_REG_dev1Eh_reg324h;
969+
970+typedef union
971+{
972+ struct
973+ {
974+ /* b[15:00] */
975+ u16 da_tx_i2mpb_a_tbt : 6;
976+ u16 rsv_6 : 4;
977+ u16 da_tx_i2mpb_a_gbe : 6;
978+ } DataBitField;
979+ u16 DATA;
980+} gephy_all_REG_dev1Eh_reg012h, *Pgephy_all_REG_dev1Eh_reg012h;
981+
982+typedef union
983+{
984+ struct
985+ {
986+ /* b[15:00] */
987+ u16 da_tx_i2mpb_b_tbt : 6;
988+ u16 rsv_6 : 2;
989+ u16 da_tx_i2mpb_b_gbe : 6;
990+ u16 rsv_14 : 2;
991+ } DataBitField;
992+ u16 DATA;
993+} gephy_all_REG_dev1Eh_reg017h, *Pgephy_all_REG_dev1Eh_reg017h;
994+
developer878c5d62023-01-13 12:13:33 +0800995+typedef enum
996+{
997+ AIR_LED_BLK_DUR_32M,
998+ AIR_LED_BLK_DUR_64M,
999+ AIR_LED_BLK_DUR_128M,
1000+ AIR_LED_BLK_DUR_256M,
1001+ AIR_LED_BLK_DUR_512M,
1002+ AIR_LED_BLK_DUR_1024M,
1003+ AIR_LED_BLK_DUR_LAST
1004+} AIR_LED_BLK_DUT_T;
1005+
1006+typedef enum
1007+{
1008+ AIR_ACTIVE_LOW,
1009+ AIR_ACTIVE_HIGH,
1010+} AIR_LED_POLARITY;
1011+typedef enum
1012+{
1013+ AIR_LED_MODE_DISABLE,
1014+ AIR_LED_MODE_USER_DEFINE,
1015+ AIR_LED_MODE_LAST
1016+} AIR_LED_MODE_T;
1017+
1018+#endif /* End of __EN8801SC_H */
developer8a021832022-08-02 23:47:49 +08001019Index: drivers/net/phy/Kconfig
1020===================================================================
1021--- a/drivers/net/phy/Kconfig
1022+++ b/drivers/net/phy/Kconfig
1023@@ -350,6 +350,11 @@ config AIROHA_EN8801S_PHY
developer878c5d62023-01-13 12:13:33 +08001024 depends on HWMON || HWMON=n
1025 select MDIO_I2C
developer8a021832022-08-02 23:47:49 +08001026
1027+config AIROHA_EN8801SC_PHY
developer878c5d62023-01-13 12:13:33 +08001028+ tristate "Drivers for Airoha EN8801S Gigabit PHYs for MediaTek SoC."
1029+ ---help---
1030+ Currently supports the Airoha EN8801S PHY for MediaTek SoC.
developer8a021832022-08-02 23:47:49 +08001031+
developer878c5d62023-01-13 12:13:33 +08001032 config AIROHA_EN8811H_PHY
1033 tristate "Drivers for Airoha EN8811H 2.5G Gigabit PHY"
1034 ---help---
developer8a021832022-08-02 23:47:49 +08001035Index: drivers/net/phy/Makefile
1036===================================================================
1037--- a/drivers/net/phy/Makefile
1038+++ b/drivers/net/phy/Makefile
developera29111c2023-01-12 10:20:04 +08001039@@ -68,5 +68,6 @@ ifdef CONFIG_HWMON
developer8a021832022-08-02 23:47:49 +08001040 aquantia-objs += aquantia_hwmon.o
1041 endif
developer8a021832022-08-02 23:47:49 +08001042+obj-$(CONFIG_AIROHA_EN8801SC_PHY) += en8801sc.o
developera29111c2023-01-12 10:20:04 +08001043 obj-$(CONFIG_AIROHA_EN8811H_PHY) += air_en8811h.o
developer8a021832022-08-02 23:47:49 +08001044 obj-$(CONFIG_AQUANTIA_PHY) += aquantia.o
1045 obj-$(CONFIG_AX88796B_PHY) += ax88796b.o