1 // SPDX-License-Identifier: GPL-2.0+
3 * drivers/net/phy/rockchip.c
5 * Driver for ROCKCHIP Ethernet PHYs
7 * Copyright (c) 2017, Fuzhou Rockchip Electronics Co., Ltd
9 * David Wu <david.wu@rock-chips.com>
12 #include <linux/ethtool.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/mii.h>
16 #include <linux/netdevice.h>
17 #include <linux/phy.h>
19 #define INTERNAL_EPHY_ID 0x1234d400
21 #define MII_INTERNAL_CTRL_STATUS 17
22 #define SMI_ADDR_TSTCNTL 20
23 #define SMI_ADDR_TSTREAD1 21
24 #define SMI_ADDR_TSTREAD2 22
25 #define SMI_ADDR_TSTWRITE 23
26 #define MII_SPECIAL_CONTROL_STATUS 31
28 #define MII_AUTO_MDIX_EN BIT(7)
29 #define MII_MDIX_EN BIT(6)
31 #define MII_SPEED_10 BIT(2)
32 #define MII_SPEED_100 BIT(3)
34 #define TSTCNTL_RD (BIT(15) | BIT(10))
35 #define TSTCNTL_WR (BIT(14) | BIT(10))
37 #define TSTMODE_ENABLE 0x400
38 #define TSTMODE_DISABLE 0x0
40 #define WR_ADDR_A7CFG 0x18
42 static int rockchip_init_tstmode(struct phy_device
*phydev
)
46 /* Enable access to Analog and DSP register banks */
47 ret
= phy_write(phydev
, SMI_ADDR_TSTCNTL
, TSTMODE_ENABLE
);
51 ret
= phy_write(phydev
, SMI_ADDR_TSTCNTL
, TSTMODE_DISABLE
);
55 return phy_write(phydev
, SMI_ADDR_TSTCNTL
, TSTMODE_ENABLE
);
58 static int rockchip_close_tstmode(struct phy_device
*phydev
)
60 /* Back to basic register bank */
61 return phy_write(phydev
, SMI_ADDR_TSTCNTL
, TSTMODE_DISABLE
);
64 static int rockchip_integrated_phy_analog_init(struct phy_device
*phydev
)
68 ret
= rockchip_init_tstmode(phydev
);
73 * Adjust tx amplitude to make sginal better,
74 * the default value is 0x8.
76 ret
= phy_write(phydev
, SMI_ADDR_TSTWRITE
, 0xB);
79 ret
= phy_write(phydev
, SMI_ADDR_TSTCNTL
, TSTCNTL_WR
| WR_ADDR_A7CFG
);
83 return rockchip_close_tstmode(phydev
);
86 static int rockchip_integrated_phy_config_init(struct phy_device
*phydev
)
91 * The auto MIDX has linked problem on some board,
92 * workround to disable auto MDIX.
94 val
= phy_read(phydev
, MII_INTERNAL_CTRL_STATUS
);
97 val
&= ~MII_AUTO_MDIX_EN
;
98 ret
= phy_write(phydev
, MII_INTERNAL_CTRL_STATUS
, val
);
102 return rockchip_integrated_phy_analog_init(phydev
);
105 static void rockchip_link_change_notify(struct phy_device
*phydev
)
108 * If mode switch happens from 10BT to 100BT, all DSP/AFE
109 * registers are set to default values. So any AFE/DSP
110 * registers have to be re-initialized in this case.
112 if (phydev
->state
== PHY_RUNNING
&& phydev
->speed
== SPEED_100
) {
113 int ret
= rockchip_integrated_phy_analog_init(phydev
);
116 phydev_err(phydev
, "rockchip_integrated_phy_analog_init err: %d.\n",
121 static int rockchip_set_polarity(struct phy_device
*phydev
, int polarity
)
125 /* get the current settings */
126 reg
= phy_read(phydev
, MII_INTERNAL_CTRL_STATUS
);
130 reg
&= ~MII_AUTO_MDIX_EN
;
139 case ETH_TP_MDI_AUTO
:
140 case ETH_TP_MDI_INVALID
:
146 /* Set the new polarity value in the register */
147 err
= phy_write(phydev
, MII_INTERNAL_CTRL_STATUS
, val
);
155 static int rockchip_config_aneg(struct phy_device
*phydev
)
159 err
= rockchip_set_polarity(phydev
, phydev
->mdix
);
163 return genphy_config_aneg(phydev
);
166 static int rockchip_phy_resume(struct phy_device
*phydev
)
168 genphy_resume(phydev
);
170 return rockchip_integrated_phy_config_init(phydev
);
173 static struct phy_driver rockchip_phy_driver
[] = {
175 .phy_id
= INTERNAL_EPHY_ID
,
176 .phy_id_mask
= 0xfffffff0,
177 .name
= "Rockchip integrated EPHY",
178 /* PHY_BASIC_FEATURES */
180 .link_change_notify
= rockchip_link_change_notify
,
181 .soft_reset
= genphy_soft_reset
,
182 .config_init
= rockchip_integrated_phy_config_init
,
183 .config_aneg
= rockchip_config_aneg
,
184 .suspend
= genphy_suspend
,
185 .resume
= rockchip_phy_resume
,
189 module_phy_driver(rockchip_phy_driver
);
191 static struct mdio_device_id __maybe_unused rockchip_phy_tbl
[] = {
192 { INTERNAL_EPHY_ID
, 0xfffffff0 },
196 MODULE_DEVICE_TABLE(mdio
, rockchip_phy_tbl
);
198 MODULE_AUTHOR("David Wu <david.wu@rock-chips.com>");
199 MODULE_DESCRIPTION("Rockchip Ethernet PHY driver");
200 MODULE_LICENSE("GPL");