1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Driver for Motorcomm PHYs
4  *
5  * Author: Peter Geis <pgwipeout@gmail.com>
6  */
7 
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/phy.h>
11 
12 #define PHY_ID_YT8511		0x0000010a
13 
14 #define YT8511_PAGE_SELECT	0x1e
15 #define YT8511_PAGE		0x1f
16 #define YT8511_EXT_CLK_GATE	0x0c
17 #define YT8511_EXT_DELAY_DRIVE	0x0d
18 #define YT8511_EXT_SLEEP_CTRL	0x27
19 
20 /* 2b00 25m from pll
21  * 2b01 25m from xtl *default*
22  * 2b10 62.m from pll
23  * 2b11 125m from pll
24  */
25 #define YT8511_CLK_125M		(BIT(2) | BIT(1))
26 #define YT8511_PLLON_SLP	BIT(14)
27 
28 /* RX Delay enabled = 1.8ns 1000T, 8ns 10/100T */
29 #define YT8511_DELAY_RX		BIT(0)
30 
31 /* TX Gig-E Delay is bits 7:4, default 0x5
32  * TX Fast-E Delay is bits 15:12, default 0xf
33  * Delay = 150ps * N - 250ps
34  * On = 2000ps, off = 50ps
35  */
36 #define YT8511_DELAY_GE_TX_EN	(0xf << 4)
37 #define YT8511_DELAY_GE_TX_DIS	(0x2 << 4)
38 #define YT8511_DELAY_FE_TX_EN	(0xf << 12)
39 #define YT8511_DELAY_FE_TX_DIS	(0x2 << 12)
40 
yt8511_read_page(struct phy_device * phydev)41 static int yt8511_read_page(struct phy_device *phydev)
42 {
43 	return __phy_read(phydev, YT8511_PAGE_SELECT);
44 };
45 
yt8511_write_page(struct phy_device * phydev,int page)46 static int yt8511_write_page(struct phy_device *phydev, int page)
47 {
48 	return __phy_write(phydev, YT8511_PAGE_SELECT, page);
49 };
50 
yt8511_config_init(struct phy_device * phydev)51 static int yt8511_config_init(struct phy_device *phydev)
52 {
53 	int oldpage, ret = 0;
54 	unsigned int ge, fe;
55 
56 	oldpage = phy_select_page(phydev, YT8511_EXT_CLK_GATE);
57 	if (oldpage < 0)
58 		goto err_restore_page;
59 
60 	/* set rgmii delay mode */
61 	switch (phydev->interface) {
62 	case PHY_INTERFACE_MODE_RGMII:
63 		ge = YT8511_DELAY_GE_TX_DIS;
64 		fe = YT8511_DELAY_FE_TX_DIS;
65 		break;
66 	case PHY_INTERFACE_MODE_RGMII_RXID:
67 		ge = YT8511_DELAY_RX | YT8511_DELAY_GE_TX_DIS;
68 		fe = YT8511_DELAY_FE_TX_DIS;
69 		break;
70 	case PHY_INTERFACE_MODE_RGMII_TXID:
71 		ge = YT8511_DELAY_GE_TX_EN;
72 		fe = YT8511_DELAY_FE_TX_EN;
73 		break;
74 	case PHY_INTERFACE_MODE_RGMII_ID:
75 		ge = YT8511_DELAY_RX | YT8511_DELAY_GE_TX_EN;
76 		fe = YT8511_DELAY_FE_TX_EN;
77 		break;
78 	default: /* do not support other modes */
79 		ret = -EOPNOTSUPP;
80 		goto err_restore_page;
81 	}
82 
83 	ret = __phy_modify(phydev, YT8511_PAGE, (YT8511_DELAY_RX | YT8511_DELAY_GE_TX_EN), ge);
84 	if (ret < 0)
85 		goto err_restore_page;
86 
87 	/* set clock mode to 125mhz */
88 	ret = __phy_modify(phydev, YT8511_PAGE, 0, YT8511_CLK_125M);
89 	if (ret < 0)
90 		goto err_restore_page;
91 
92 	/* fast ethernet delay is in a separate page */
93 	ret = __phy_write(phydev, YT8511_PAGE_SELECT, YT8511_EXT_DELAY_DRIVE);
94 	if (ret < 0)
95 		goto err_restore_page;
96 
97 	ret = __phy_modify(phydev, YT8511_PAGE, YT8511_DELAY_FE_TX_EN, fe);
98 	if (ret < 0)
99 		goto err_restore_page;
100 
101 	/* leave pll enabled in sleep */
102 	ret = __phy_write(phydev, YT8511_PAGE_SELECT, YT8511_EXT_SLEEP_CTRL);
103 	if (ret < 0)
104 		goto err_restore_page;
105 
106 	ret = __phy_modify(phydev, YT8511_PAGE, 0, YT8511_PLLON_SLP);
107 	if (ret < 0)
108 		goto err_restore_page;
109 
110 err_restore_page:
111 	return phy_restore_page(phydev, oldpage, ret);
112 }
113 
114 static struct phy_driver motorcomm_phy_drvs[] = {
115 	{
116 		PHY_ID_MATCH_EXACT(PHY_ID_YT8511),
117 		.name		= "YT8511 Gigabit Ethernet",
118 		.config_init	= yt8511_config_init,
119 		.suspend	= genphy_suspend,
120 		.resume		= genphy_resume,
121 		.read_page	= yt8511_read_page,
122 		.write_page	= yt8511_write_page,
123 	},
124 };
125 
126 module_phy_driver(motorcomm_phy_drvs);
127 
128 MODULE_DESCRIPTION("Motorcomm PHY driver");
129 MODULE_AUTHOR("Peter Geis");
130 MODULE_LICENSE("GPL");
131 
132 static const struct mdio_device_id __maybe_unused motorcomm_tbl[] = {
133 	{ PHY_ID_MATCH_EXACT(PHY_ID_YT8511) },
134 	{ /* sentinal */ }
135 };
136 
137 MODULE_DEVICE_TABLE(mdio, motorcomm_tbl);
138