dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / net / phy / bcm-phy-lib.c
bloba75642051b8bc4db808bc156dfa2092e93a5c16f
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2015-2017 Broadcom
4 */
6 #include "bcm-phy-lib.h"
7 #include <linux/brcmphy.h>
8 #include <linux/export.h>
9 #include <linux/mdio.h>
10 #include <linux/module.h>
11 #include <linux/phy.h>
12 #include <linux/ethtool.h>
14 #define MII_BCM_CHANNEL_WIDTH 0x2000
15 #define BCM_CL45VEN_EEE_ADV 0x3c
17 int bcm_phy_write_exp(struct phy_device *phydev, u16 reg, u16 val)
19 int rc;
21 rc = phy_write(phydev, MII_BCM54XX_EXP_SEL, reg);
22 if (rc < 0)
23 return rc;
25 return phy_write(phydev, MII_BCM54XX_EXP_DATA, val);
27 EXPORT_SYMBOL_GPL(bcm_phy_write_exp);
29 int bcm_phy_read_exp(struct phy_device *phydev, u16 reg)
31 int val;
33 val = phy_write(phydev, MII_BCM54XX_EXP_SEL, reg);
34 if (val < 0)
35 return val;
37 val = phy_read(phydev, MII_BCM54XX_EXP_DATA);
39 /* Restore default value. It's O.K. if this write fails. */
40 phy_write(phydev, MII_BCM54XX_EXP_SEL, 0);
42 return val;
44 EXPORT_SYMBOL_GPL(bcm_phy_read_exp);
46 int bcm54xx_auxctl_read(struct phy_device *phydev, u16 regnum)
48 /* The register must be written to both the Shadow Register Select and
49 * the Shadow Read Register Selector
51 phy_write(phydev, MII_BCM54XX_AUX_CTL, MII_BCM54XX_AUXCTL_SHDWSEL_MASK |
52 regnum << MII_BCM54XX_AUXCTL_SHDWSEL_READ_SHIFT);
53 return phy_read(phydev, MII_BCM54XX_AUX_CTL);
55 EXPORT_SYMBOL_GPL(bcm54xx_auxctl_read);
57 int bcm54xx_auxctl_write(struct phy_device *phydev, u16 regnum, u16 val)
59 return phy_write(phydev, MII_BCM54XX_AUX_CTL, regnum | val);
61 EXPORT_SYMBOL(bcm54xx_auxctl_write);
63 int bcm_phy_write_misc(struct phy_device *phydev,
64 u16 reg, u16 chl, u16 val)
66 int rc;
67 int tmp;
69 rc = phy_write(phydev, MII_BCM54XX_AUX_CTL,
70 MII_BCM54XX_AUXCTL_SHDWSEL_MISC);
71 if (rc < 0)
72 return rc;
74 tmp = phy_read(phydev, MII_BCM54XX_AUX_CTL);
75 tmp |= MII_BCM54XX_AUXCTL_ACTL_SMDSP_ENA;
76 rc = phy_write(phydev, MII_BCM54XX_AUX_CTL, tmp);
77 if (rc < 0)
78 return rc;
80 tmp = (chl * MII_BCM_CHANNEL_WIDTH) | reg;
81 rc = bcm_phy_write_exp(phydev, tmp, val);
83 return rc;
85 EXPORT_SYMBOL_GPL(bcm_phy_write_misc);
87 int bcm_phy_read_misc(struct phy_device *phydev,
88 u16 reg, u16 chl)
90 int rc;
91 int tmp;
93 rc = phy_write(phydev, MII_BCM54XX_AUX_CTL,
94 MII_BCM54XX_AUXCTL_SHDWSEL_MISC);
95 if (rc < 0)
96 return rc;
98 tmp = phy_read(phydev, MII_BCM54XX_AUX_CTL);
99 tmp |= MII_BCM54XX_AUXCTL_ACTL_SMDSP_ENA;
100 rc = phy_write(phydev, MII_BCM54XX_AUX_CTL, tmp);
101 if (rc < 0)
102 return rc;
104 tmp = (chl * MII_BCM_CHANNEL_WIDTH) | reg;
105 rc = bcm_phy_read_exp(phydev, tmp);
107 return rc;
109 EXPORT_SYMBOL_GPL(bcm_phy_read_misc);
111 int bcm_phy_ack_intr(struct phy_device *phydev)
113 int reg;
115 /* Clear pending interrupts. */
116 reg = phy_read(phydev, MII_BCM54XX_ISR);
117 if (reg < 0)
118 return reg;
120 return 0;
122 EXPORT_SYMBOL_GPL(bcm_phy_ack_intr);
124 int bcm_phy_config_intr(struct phy_device *phydev)
126 int reg;
128 reg = phy_read(phydev, MII_BCM54XX_ECR);
129 if (reg < 0)
130 return reg;
132 if (phydev->interrupts == PHY_INTERRUPT_ENABLED)
133 reg &= ~MII_BCM54XX_ECR_IM;
134 else
135 reg |= MII_BCM54XX_ECR_IM;
137 return phy_write(phydev, MII_BCM54XX_ECR, reg);
139 EXPORT_SYMBOL_GPL(bcm_phy_config_intr);
141 int bcm_phy_read_shadow(struct phy_device *phydev, u16 shadow)
143 phy_write(phydev, MII_BCM54XX_SHD, MII_BCM54XX_SHD_VAL(shadow));
144 return MII_BCM54XX_SHD_DATA(phy_read(phydev, MII_BCM54XX_SHD));
146 EXPORT_SYMBOL_GPL(bcm_phy_read_shadow);
148 int bcm_phy_write_shadow(struct phy_device *phydev, u16 shadow,
149 u16 val)
151 return phy_write(phydev, MII_BCM54XX_SHD,
152 MII_BCM54XX_SHD_WRITE |
153 MII_BCM54XX_SHD_VAL(shadow) |
154 MII_BCM54XX_SHD_DATA(val));
156 EXPORT_SYMBOL_GPL(bcm_phy_write_shadow);
158 int bcm_phy_enable_apd(struct phy_device *phydev, bool dll_pwr_down)
160 int val;
162 if (dll_pwr_down) {
163 val = bcm_phy_read_shadow(phydev, BCM54XX_SHD_SCR3);
164 if (val < 0)
165 return val;
167 val |= BCM54XX_SHD_SCR3_DLLAPD_DIS;
168 bcm_phy_write_shadow(phydev, BCM54XX_SHD_SCR3, val);
171 val = bcm_phy_read_shadow(phydev, BCM54XX_SHD_APD);
172 if (val < 0)
173 return val;
175 /* Clear APD bits */
176 val &= BCM_APD_CLR_MASK;
178 if (phydev->autoneg == AUTONEG_ENABLE)
179 val |= BCM54XX_SHD_APD_EN;
180 else
181 val |= BCM_NO_ANEG_APD_EN;
183 /* Enable energy detect single link pulse for easy wakeup */
184 val |= BCM_APD_SINGLELP_EN;
186 /* Enable Auto Power-Down (APD) for the PHY */
187 return bcm_phy_write_shadow(phydev, BCM54XX_SHD_APD, val);
189 EXPORT_SYMBOL_GPL(bcm_phy_enable_apd);
191 int bcm_phy_set_eee(struct phy_device *phydev, bool enable)
193 int val;
195 /* Enable EEE at PHY level */
196 val = phy_read_mmd(phydev, MDIO_MMD_AN, BRCM_CL45VEN_EEE_CONTROL);
197 if (val < 0)
198 return val;
200 if (enable)
201 val |= LPI_FEATURE_EN | LPI_FEATURE_EN_DIG1000X;
202 else
203 val &= ~(LPI_FEATURE_EN | LPI_FEATURE_EN_DIG1000X);
205 phy_write_mmd(phydev, MDIO_MMD_AN, BRCM_CL45VEN_EEE_CONTROL, (u32)val);
207 /* Advertise EEE */
208 val = phy_read_mmd(phydev, MDIO_MMD_AN, BCM_CL45VEN_EEE_ADV);
209 if (val < 0)
210 return val;
212 if (enable)
213 val |= (MDIO_EEE_100TX | MDIO_EEE_1000T);
214 else
215 val &= ~(MDIO_EEE_100TX | MDIO_EEE_1000T);
217 phy_write_mmd(phydev, MDIO_MMD_AN, BCM_CL45VEN_EEE_ADV, (u32)val);
219 return 0;
221 EXPORT_SYMBOL_GPL(bcm_phy_set_eee);
223 int bcm_phy_downshift_get(struct phy_device *phydev, u8 *count)
225 int val;
227 val = bcm54xx_auxctl_read(phydev, MII_BCM54XX_AUXCTL_SHDWSEL_MISC);
228 if (val < 0)
229 return val;
231 /* Check if wirespeed is enabled or not */
232 if (!(val & MII_BCM54XX_AUXCTL_SHDWSEL_MISC_WIRESPEED_EN)) {
233 *count = DOWNSHIFT_DEV_DISABLE;
234 return 0;
237 val = bcm_phy_read_shadow(phydev, BCM54XX_SHD_SCR2);
238 if (val < 0)
239 return val;
241 /* Downgrade after one link attempt */
242 if (val & BCM54XX_SHD_SCR2_WSPD_RTRY_DIS) {
243 *count = 1;
244 } else {
245 /* Downgrade after configured retry count */
246 val >>= BCM54XX_SHD_SCR2_WSPD_RTRY_LMT_SHIFT;
247 val &= BCM54XX_SHD_SCR2_WSPD_RTRY_LMT_MASK;
248 *count = val + BCM54XX_SHD_SCR2_WSPD_RTRY_LMT_OFFSET;
251 return 0;
253 EXPORT_SYMBOL_GPL(bcm_phy_downshift_get);
255 int bcm_phy_downshift_set(struct phy_device *phydev, u8 count)
257 int val = 0, ret = 0;
259 /* Range check the number given */
260 if (count - BCM54XX_SHD_SCR2_WSPD_RTRY_LMT_OFFSET >
261 BCM54XX_SHD_SCR2_WSPD_RTRY_LMT_MASK &&
262 count != DOWNSHIFT_DEV_DEFAULT_COUNT) {
263 return -ERANGE;
266 val = bcm54xx_auxctl_read(phydev, MII_BCM54XX_AUXCTL_SHDWSEL_MISC);
267 if (val < 0)
268 return val;
270 /* Se the write enable bit */
271 val |= MII_BCM54XX_AUXCTL_MISC_WREN;
273 if (count == DOWNSHIFT_DEV_DISABLE) {
274 val &= ~MII_BCM54XX_AUXCTL_SHDWSEL_MISC_WIRESPEED_EN;
275 return bcm54xx_auxctl_write(phydev,
276 MII_BCM54XX_AUXCTL_SHDWSEL_MISC,
277 val);
278 } else {
279 val |= MII_BCM54XX_AUXCTL_SHDWSEL_MISC_WIRESPEED_EN;
280 ret = bcm54xx_auxctl_write(phydev,
281 MII_BCM54XX_AUXCTL_SHDWSEL_MISC,
282 val);
283 if (ret < 0)
284 return ret;
287 val = bcm_phy_read_shadow(phydev, BCM54XX_SHD_SCR2);
288 val &= ~(BCM54XX_SHD_SCR2_WSPD_RTRY_LMT_MASK <<
289 BCM54XX_SHD_SCR2_WSPD_RTRY_LMT_SHIFT |
290 BCM54XX_SHD_SCR2_WSPD_RTRY_DIS);
292 switch (count) {
293 case 1:
294 val |= BCM54XX_SHD_SCR2_WSPD_RTRY_DIS;
295 break;
296 case DOWNSHIFT_DEV_DEFAULT_COUNT:
297 val |= 1 << BCM54XX_SHD_SCR2_WSPD_RTRY_LMT_SHIFT;
298 break;
299 default:
300 val |= (count - BCM54XX_SHD_SCR2_WSPD_RTRY_LMT_OFFSET) <<
301 BCM54XX_SHD_SCR2_WSPD_RTRY_LMT_SHIFT;
302 break;
305 return bcm_phy_write_shadow(phydev, BCM54XX_SHD_SCR2, val);
307 EXPORT_SYMBOL_GPL(bcm_phy_downshift_set);
309 struct bcm_phy_hw_stat {
310 const char *string;
311 u8 reg;
312 u8 shift;
313 u8 bits;
316 /* Counters freeze at either 0xffff or 0xff, better than nothing */
317 static const struct bcm_phy_hw_stat bcm_phy_hw_stats[] = {
318 { "phy_receive_errors", MII_BRCM_CORE_BASE12, 0, 16 },
319 { "phy_serdes_ber_errors", MII_BRCM_CORE_BASE13, 8, 8 },
320 { "phy_false_carrier_sense_errors", MII_BRCM_CORE_BASE13, 0, 8 },
321 { "phy_local_rcvr_nok", MII_BRCM_CORE_BASE14, 8, 8 },
322 { "phy_remote_rcv_nok", MII_BRCM_CORE_BASE14, 0, 8 },
325 int bcm_phy_get_sset_count(struct phy_device *phydev)
327 return ARRAY_SIZE(bcm_phy_hw_stats);
329 EXPORT_SYMBOL_GPL(bcm_phy_get_sset_count);
331 void bcm_phy_get_strings(struct phy_device *phydev, u8 *data)
333 unsigned int i;
335 for (i = 0; i < ARRAY_SIZE(bcm_phy_hw_stats); i++)
336 strlcpy(data + i * ETH_GSTRING_LEN,
337 bcm_phy_hw_stats[i].string, ETH_GSTRING_LEN);
339 EXPORT_SYMBOL_GPL(bcm_phy_get_strings);
341 /* Caller is supposed to provide appropriate storage for the library code to
342 * access the shadow copy
344 static u64 bcm_phy_get_stat(struct phy_device *phydev, u64 *shadow,
345 unsigned int i)
347 struct bcm_phy_hw_stat stat = bcm_phy_hw_stats[i];
348 int val;
349 u64 ret;
351 val = phy_read(phydev, stat.reg);
352 if (val < 0) {
353 ret = U64_MAX;
354 } else {
355 val >>= stat.shift;
356 val = val & ((1 << stat.bits) - 1);
357 shadow[i] += val;
358 ret = shadow[i];
361 return ret;
364 void bcm_phy_get_stats(struct phy_device *phydev, u64 *shadow,
365 struct ethtool_stats *stats, u64 *data)
367 unsigned int i;
369 for (i = 0; i < ARRAY_SIZE(bcm_phy_hw_stats); i++)
370 data[i] = bcm_phy_get_stat(phydev, shadow, i);
372 EXPORT_SYMBOL_GPL(bcm_phy_get_stats);
374 MODULE_DESCRIPTION("Broadcom PHY Library");
375 MODULE_LICENSE("GPL v2");
376 MODULE_AUTHOR("Broadcom Corporation");