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 41 static int yt8511_read_page(struct phy_device *phydev) 42 { 43 return __phy_read(phydev, YT8511_PAGE_SELECT); 44 }; 45 46 static int yt8511_write_page(struct phy_device *phydev, int page) 47 { 48 return __phy_write(phydev, YT8511_PAGE_SELECT, page); 49 }; 50 51 static int yt8511_config_init(struct phy_device *phydev) 52 { 53 unsigned int ge, fe; 54 int ret, oldpage; 55 56 /* set clock mode to 125mhz */ 57 oldpage = phy_select_page(phydev, YT8511_EXT_CLK_GATE); 58 if (oldpage < 0) 59 goto err_restore_page; 60 61 ret = __phy_modify(phydev, YT8511_PAGE, 0, YT8511_CLK_125M); 62 if (ret < 0) 63 goto err_restore_page; 64 65 /* set rgmii delay mode */ 66 switch (phydev->interface) { 67 case PHY_INTERFACE_MODE_RGMII: 68 ge = YT8511_DELAY_GE_TX_DIS; 69 fe = YT8511_DELAY_FE_TX_DIS; 70 break; 71 case PHY_INTERFACE_MODE_RGMII_RXID: 72 ge = YT8511_DELAY_RX | YT8511_DELAY_GE_TX_DIS; 73 fe = YT8511_DELAY_FE_TX_DIS; 74 break; 75 case PHY_INTERFACE_MODE_RGMII_TXID: 76 ge = YT8511_DELAY_GE_TX_EN; 77 fe = YT8511_DELAY_FE_TX_EN; 78 break; 79 case PHY_INTERFACE_MODE_RGMII_ID: 80 ge = YT8511_DELAY_RX | YT8511_DELAY_GE_TX_EN; 81 fe = YT8511_DELAY_FE_TX_EN; 82 break; 83 default: /* leave everything alone in other modes */ 84 break; 85 } 86 87 ret = __phy_modify(phydev, YT8511_PAGE, (YT8511_DELAY_RX | YT8511_DELAY_GE_TX_EN), ge); 88 if (ret < 0) 89 goto err_restore_page; 90 91 /* fast ethernet delay is in a separate page */ 92 ret = __phy_write(phydev, YT8511_PAGE_SELECT, YT8511_EXT_DELAY_DRIVE); 93 if (ret < 0) 94 goto err_restore_page; 95 96 ret = __phy_modify(phydev, YT8511_PAGE, YT8511_DELAY_FE_TX_EN, fe); 97 if (ret < 0) 98 goto err_restore_page; 99 100 /* leave pll enabled in sleep */ 101 ret = __phy_write(phydev, YT8511_PAGE_SELECT, YT8511_EXT_SLEEP_CTRL); 102 if (ret < 0) 103 goto err_restore_page; 104 105 ret = __phy_modify(phydev, YT8511_PAGE, 0, YT8511_PLLON_SLP); 106 if (ret < 0) 107 goto err_restore_page; 108 109 err_restore_page: 110 return phy_restore_page(phydev, oldpage, ret); 111 } 112 113 static struct phy_driver motorcomm_phy_drvs[] = { 114 { 115 PHY_ID_MATCH_EXACT(PHY_ID_YT8511), 116 .name = "YT8511 Gigabit Ethernet", 117 .config_init = yt8511_config_init, 118 .suspend = genphy_suspend, 119 .resume = genphy_resume, 120 .read_page = yt8511_read_page, 121 .write_page = yt8511_write_page, 122 }, 123 }; 124 125 module_phy_driver(motorcomm_phy_drvs); 126 127 MODULE_DESCRIPTION("Motorcomm PHY driver"); 128 MODULE_AUTHOR("Peter Geis"); 129 MODULE_LICENSE("GPL"); 130 131 static const struct mdio_device_id __maybe_unused motorcomm_tbl[] = { 132 { PHY_ID_MATCH_EXACT(PHY_ID_YT8511) }, 133 { /* sentinal */ } 134 }; 135 136 MODULE_DEVICE_TABLE(mdio, motorcomm_tbl); 137