Merge tag 'regmap-fix-v4.9-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / net / phy / dp83867.c
blob91177a4a32ad21c1cbea044424659f5604cef7de
1 /*
2 * Driver for the Texas Instruments DP83867 PHY
4 * Copyright (C) 2015 Texas Instruments Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/ethtool.h>
17 #include <linux/kernel.h>
18 #include <linux/mii.h>
19 #include <linux/module.h>
20 #include <linux/of.h>
21 #include <linux/phy.h>
23 #include <dt-bindings/net/ti-dp83867.h>
25 #define DP83867_PHY_ID 0x2000a231
26 #define DP83867_DEVADDR 0x1f
28 #define MII_DP83867_PHYCTRL 0x10
29 #define MII_DP83867_MICR 0x12
30 #define MII_DP83867_ISR 0x13
31 #define DP83867_CTRL 0x1f
33 /* Extended Registers */
34 #define DP83867_RGMIICTL 0x0032
35 #define DP83867_RGMIIDCTL 0x0086
37 #define DP83867_SW_RESET BIT(15)
38 #define DP83867_SW_RESTART BIT(14)
40 /* MICR Interrupt bits */
41 #define MII_DP83867_MICR_AN_ERR_INT_EN BIT(15)
42 #define MII_DP83867_MICR_SPEED_CHNG_INT_EN BIT(14)
43 #define MII_DP83867_MICR_DUP_MODE_CHNG_INT_EN BIT(13)
44 #define MII_DP83867_MICR_PAGE_RXD_INT_EN BIT(12)
45 #define MII_DP83867_MICR_AUTONEG_COMP_INT_EN BIT(11)
46 #define MII_DP83867_MICR_LINK_STS_CHNG_INT_EN BIT(10)
47 #define MII_DP83867_MICR_FALSE_CARRIER_INT_EN BIT(8)
48 #define MII_DP83867_MICR_SLEEP_MODE_CHNG_INT_EN BIT(4)
49 #define MII_DP83867_MICR_WOL_INT_EN BIT(3)
50 #define MII_DP83867_MICR_XGMII_ERR_INT_EN BIT(2)
51 #define MII_DP83867_MICR_POL_CHNG_INT_EN BIT(1)
52 #define MII_DP83867_MICR_JABBER_INT_EN BIT(0)
54 /* RGMIICTL bits */
55 #define DP83867_RGMII_TX_CLK_DELAY_EN BIT(1)
56 #define DP83867_RGMII_RX_CLK_DELAY_EN BIT(0)
58 /* PHY CTRL bits */
59 #define DP83867_PHYCR_FIFO_DEPTH_SHIFT 14
60 #define DP83867_PHYCR_FIFO_DEPTH_MASK (3 << 14)
62 /* RGMIIDCTL bits */
63 #define DP83867_RGMII_TX_CLK_DELAY_SHIFT 4
65 struct dp83867_private {
66 int rx_id_delay;
67 int tx_id_delay;
68 int fifo_depth;
71 static int dp83867_ack_interrupt(struct phy_device *phydev)
73 int err = phy_read(phydev, MII_DP83867_ISR);
75 if (err < 0)
76 return err;
78 return 0;
81 static int dp83867_config_intr(struct phy_device *phydev)
83 int micr_status;
85 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
86 micr_status = phy_read(phydev, MII_DP83867_MICR);
87 if (micr_status < 0)
88 return micr_status;
90 micr_status |=
91 (MII_DP83867_MICR_AN_ERR_INT_EN |
92 MII_DP83867_MICR_SPEED_CHNG_INT_EN |
93 MII_DP83867_MICR_DUP_MODE_CHNG_INT_EN |
94 MII_DP83867_MICR_SLEEP_MODE_CHNG_INT_EN);
96 return phy_write(phydev, MII_DP83867_MICR, micr_status);
99 micr_status = 0x0;
100 return phy_write(phydev, MII_DP83867_MICR, micr_status);
103 #ifdef CONFIG_OF_MDIO
104 static int dp83867_of_init(struct phy_device *phydev)
106 struct dp83867_private *dp83867 = phydev->priv;
107 struct device *dev = &phydev->mdio.dev;
108 struct device_node *of_node = dev->of_node;
109 int ret;
111 if (!of_node)
112 return -ENODEV;
114 ret = of_property_read_u32(of_node, "ti,rx-internal-delay",
115 &dp83867->rx_id_delay);
116 if (ret)
117 return ret;
119 ret = of_property_read_u32(of_node, "ti,tx-internal-delay",
120 &dp83867->tx_id_delay);
121 if (ret)
122 return ret;
124 return of_property_read_u32(of_node, "ti,fifo-depth",
125 &dp83867->fifo_depth);
127 #else
128 static int dp83867_of_init(struct phy_device *phydev)
130 return 0;
132 #endif /* CONFIG_OF_MDIO */
134 static int dp83867_config_init(struct phy_device *phydev)
136 struct dp83867_private *dp83867;
137 int ret, val;
138 u16 delay;
140 if (!phydev->priv) {
141 dp83867 = devm_kzalloc(&phydev->mdio.dev, sizeof(*dp83867),
142 GFP_KERNEL);
143 if (!dp83867)
144 return -ENOMEM;
146 phydev->priv = dp83867;
147 ret = dp83867_of_init(phydev);
148 if (ret)
149 return ret;
150 } else {
151 dp83867 = (struct dp83867_private *)phydev->priv;
154 if (phy_interface_is_rgmii(phydev)) {
155 val = phy_read(phydev, MII_DP83867_PHYCTRL);
156 if (val < 0)
157 return val;
158 val &= ~DP83867_PHYCR_FIFO_DEPTH_MASK;
159 val |= (dp83867->fifo_depth << DP83867_PHYCR_FIFO_DEPTH_SHIFT);
160 ret = phy_write(phydev, MII_DP83867_PHYCTRL, val);
161 if (ret)
162 return ret;
165 if ((phydev->interface >= PHY_INTERFACE_MODE_RGMII_ID) &&
166 (phydev->interface <= PHY_INTERFACE_MODE_RGMII_RXID)) {
167 val = phy_read_mmd_indirect(phydev, DP83867_RGMIICTL,
168 DP83867_DEVADDR);
170 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
171 val |= (DP83867_RGMII_TX_CLK_DELAY_EN | DP83867_RGMII_RX_CLK_DELAY_EN);
173 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
174 val |= DP83867_RGMII_TX_CLK_DELAY_EN;
176 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
177 val |= DP83867_RGMII_RX_CLK_DELAY_EN;
179 phy_write_mmd_indirect(phydev, DP83867_RGMIICTL,
180 DP83867_DEVADDR, val);
182 delay = (dp83867->rx_id_delay |
183 (dp83867->tx_id_delay << DP83867_RGMII_TX_CLK_DELAY_SHIFT));
185 phy_write_mmd_indirect(phydev, DP83867_RGMIIDCTL,
186 DP83867_DEVADDR, delay);
189 return 0;
192 static int dp83867_phy_reset(struct phy_device *phydev)
194 int err;
196 err = phy_write(phydev, DP83867_CTRL, DP83867_SW_RESET);
197 if (err < 0)
198 return err;
200 return dp83867_config_init(phydev);
203 static struct phy_driver dp83867_driver[] = {
205 .phy_id = DP83867_PHY_ID,
206 .phy_id_mask = 0xfffffff0,
207 .name = "TI DP83867",
208 .features = PHY_GBIT_FEATURES,
209 .flags = PHY_HAS_INTERRUPT,
211 .config_init = dp83867_config_init,
212 .soft_reset = dp83867_phy_reset,
214 /* IRQ related */
215 .ack_interrupt = dp83867_ack_interrupt,
216 .config_intr = dp83867_config_intr,
218 .config_aneg = genphy_config_aneg,
219 .read_status = genphy_read_status,
220 .suspend = genphy_suspend,
221 .resume = genphy_resume,
224 module_phy_driver(dp83867_driver);
226 static struct mdio_device_id __maybe_unused dp83867_tbl[] = {
227 { DP83867_PHY_ID, 0xfffffff0 },
231 MODULE_DEVICE_TABLE(mdio, dp83867_tbl);
233 MODULE_DESCRIPTION("Texas Instruments DP83867 PHY driver");
234 MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com");
235 MODULE_LICENSE("GPL");