blob: 0ac78d0db9b5bb67e2953393d9fb3bba6a219813 [file] [log] [blame]
Florian Fainellib560a582014-02-13 16:08:45 -08001/*
2 * Broadcom BCM7xxx internal transceivers support.
3 *
4 * Copyright (C) 2014, Broadcom Corporation
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12#include <linux/module.h>
13#include <linux/phy.h>
14#include <linux/delay.h>
15#include <linux/bitops.h>
16#include <linux/brcmphy.h>
Florian Fainellib8f9a022014-08-22 18:55:45 -070017#include <linux/mdio.h>
Florian Fainellib560a582014-02-13 16:08:45 -080018
19/* Broadcom BCM7xxx internal PHY registers */
20#define MII_BCM7XXX_CHANNEL_WIDTH 0x2000
21
22/* 40nm only register definitions */
23#define MII_BCM7XXX_100TX_AUX_CTL 0x10
24#define MII_BCM7XXX_100TX_FALSE_CAR 0x13
25#define MII_BCM7XXX_100TX_DISC 0x14
26#define MII_BCM7XXX_AUX_MODE 0x1d
27#define MII_BCM7XX_64CLK_MDIO BIT(12)
28#define MII_BCM7XXX_CORE_BASE1E 0x1e
29#define MII_BCM7XXX_TEST 0x1f
30#define MII_BCM7XXX_SHD_MODE_2 BIT(2)
31
Florian Fainellia3622f22014-03-24 16:36:47 -070032/* 28nm only register definitions */
33#define MISC_ADDR(base, channel) base, channel
34
35#define DSP_TAP10 MISC_ADDR(0x0a, 0)
36#define PLL_PLLCTRL_1 MISC_ADDR(0x32, 1)
37#define PLL_PLLCTRL_2 MISC_ADDR(0x32, 2)
38#define PLL_PLLCTRL_4 MISC_ADDR(0x33, 0)
39
40#define AFE_RXCONFIG_0 MISC_ADDR(0x38, 0)
41#define AFE_RXCONFIG_1 MISC_ADDR(0x38, 1)
42#define AFE_RX_LP_COUNTER MISC_ADDR(0x38, 3)
43#define AFE_TX_CONFIG MISC_ADDR(0x39, 0)
44#define AFE_HPF_TRIM_OTHERS MISC_ADDR(0x3a, 0)
45
46#define CORE_EXPB0 0xb0
47
Florian Fainellib560a582014-02-13 16:08:45 -080048static void phy_write_exp(struct phy_device *phydev,
49 u16 reg, u16 value)
50{
51 phy_write(phydev, MII_BCM54XX_EXP_SEL, MII_BCM54XX_EXP_SEL_ER | reg);
52 phy_write(phydev, MII_BCM54XX_EXP_DATA, value);
53}
54
55static void phy_write_misc(struct phy_device *phydev,
56 u16 reg, u16 chl, u16 value)
57{
58 int tmp;
59
60 phy_write(phydev, MII_BCM54XX_AUX_CTL, MII_BCM54XX_AUXCTL_SHDWSEL_MISC);
61
62 tmp = phy_read(phydev, MII_BCM54XX_AUX_CTL);
63 tmp |= MII_BCM54XX_AUXCTL_ACTL_SMDSP_ENA;
64 phy_write(phydev, MII_BCM54XX_AUX_CTL, tmp);
65
66 tmp = (chl * MII_BCM7XXX_CHANNEL_WIDTH) | reg;
67 phy_write(phydev, MII_BCM54XX_EXP_SEL, tmp);
68
69 phy_write(phydev, MII_BCM54XX_EXP_DATA, value);
70}
71
Florian Fainelli9c41f2b2014-11-11 14:55:12 -080072static void r_rc_cal_reset(struct phy_device *phydev)
73{
74 /* Reset R_CAL/RC_CAL Engine */
75 phy_write_exp(phydev, 0x00b0, 0x0010);
76
77 /* Disable Reset R_AL/RC_CAL Engine */
78 phy_write_exp(phydev, 0x00b0, 0x0000);
79}
80
Florian Fainelli2a9df742014-11-11 14:55:11 -080081static int bcm7xxx_28nm_b0_afe_config_init(struct phy_device *phydev)
Florian Fainellib560a582014-02-13 16:08:45 -080082{
Florian Fainellib560a582014-02-13 16:08:45 -080083 /* Increase VCO range to prevent unlocking problem of PLL at low
84 * temp
85 */
Florian Fainellia3622f22014-03-24 16:36:47 -070086 phy_write_misc(phydev, PLL_PLLCTRL_1, 0x0048);
Florian Fainellib560a582014-02-13 16:08:45 -080087
88 /* Change Ki to 011 */
Florian Fainellia3622f22014-03-24 16:36:47 -070089 phy_write_misc(phydev, PLL_PLLCTRL_2, 0x021b);
Florian Fainellib560a582014-02-13 16:08:45 -080090
91 /* Disable loading of TVCO buffer to bandgap, set bandgap trim
92 * to 111
93 */
Florian Fainellia3622f22014-03-24 16:36:47 -070094 phy_write_misc(phydev, PLL_PLLCTRL_4, 0x0e20);
Florian Fainellib560a582014-02-13 16:08:45 -080095
96 /* Adjust bias current trim by -3 */
Florian Fainellia3622f22014-03-24 16:36:47 -070097 phy_write_misc(phydev, DSP_TAP10, 0x690b);
Florian Fainellib560a582014-02-13 16:08:45 -080098
99 /* Switch to CORE_BASE1E */
100 phy_write(phydev, MII_BCM7XXX_CORE_BASE1E, 0xd);
101
Florian Fainelli9c41f2b2014-11-11 14:55:12 -0800102 r_rc_cal_reset(phydev);
Florian Fainellib560a582014-02-13 16:08:45 -0800103
Florian Fainelli99185422014-03-24 16:36:48 -0700104 /* write AFE_RXCONFIG_0 */
105 phy_write_misc(phydev, AFE_RXCONFIG_0, 0xeb19);
106
107 /* write AFE_RXCONFIG_1 */
108 phy_write_misc(phydev, AFE_RXCONFIG_1, 0x9a3f);
109
110 /* write AFE_RX_LP_COUNTER */
Florian Fainellia62ea5a2014-03-24 16:36:49 -0700111 phy_write_misc(phydev, AFE_RX_LP_COUNTER, 0x7fc0);
Florian Fainelli99185422014-03-24 16:36:48 -0700112
113 /* write AFE_HPF_TRIM_OTHERS */
114 phy_write_misc(phydev, AFE_HPF_TRIM_OTHERS, 0x000b);
115
116 /* write AFTE_TX_CONFIG */
117 phy_write_misc(phydev, AFE_TX_CONFIG, 0x0800);
118
Florian Fainellib560a582014-02-13 16:08:45 -0800119 return 0;
120}
121
Florian Fainelli9df54dd2014-08-22 18:55:41 -0700122static int bcm7xxx_apd_enable(struct phy_device *phydev)
123{
124 int val;
125
126 /* Enable powering down of the DLL during auto-power down */
127 val = bcm54xx_shadow_read(phydev, BCM54XX_SHD_SCR3);
128 if (val < 0)
129 return val;
130
131 val |= BCM54XX_SHD_SCR3_DLLAPD_DIS;
132 bcm54xx_shadow_write(phydev, BCM54XX_SHD_SCR3, val);
133
134 /* Enable auto-power down */
135 val = bcm54xx_shadow_read(phydev, BCM54XX_SHD_APD);
136 if (val < 0)
137 return val;
138
139 val |= BCM54XX_SHD_APD_EN;
140 return bcm54xx_shadow_write(phydev, BCM54XX_SHD_APD, val);
141}
142
Florian Fainellib8f9a022014-08-22 18:55:45 -0700143static int bcm7xxx_eee_enable(struct phy_device *phydev)
144{
145 int val;
146
147 val = phy_read_mmd_indirect(phydev, BRCM_CL45VEN_EEE_CONTROL,
148 MDIO_MMD_AN, phydev->addr);
149 if (val < 0)
150 return val;
151
152 /* Enable general EEE feature at the PHY level */
153 val |= LPI_FEATURE_EN | LPI_FEATURE_EN_DIG1000X;
154
155 phy_write_mmd_indirect(phydev, BRCM_CL45VEN_EEE_CONTROL,
156 MDIO_MMD_AN, phydev->addr, val);
157
158 /* Advertise supported modes */
159 val = phy_read_mmd_indirect(phydev, MDIO_AN_EEE_ADV,
160 MDIO_MMD_AN, phydev->addr);
161
162 val |= (MDIO_AN_EEE_ADV_100TX | MDIO_AN_EEE_ADV_1000T);
163 phy_write_mmd_indirect(phydev, MDIO_AN_EEE_ADV,
164 MDIO_MMD_AN, phydev->addr, val);
165
166 return 0;
167}
168
Florian Fainellib560a582014-02-13 16:08:45 -0800169static int bcm7xxx_28nm_config_init(struct phy_device *phydev)
170{
Florian Fainellid8ebfed2014-09-19 13:07:56 -0700171 u8 rev = PHY_BRCM_7XXX_REV(phydev->dev_flags);
172 u8 patch = PHY_BRCM_7XXX_PATCH(phydev->dev_flags);
173 int ret = 0;
Florian Fainellib560a582014-02-13 16:08:45 -0800174
Florian Fainelli6ec259c2014-11-11 14:55:10 -0800175 pr_info_once("%s: %s PHY revision: 0x%02x, patch: %d\n",
176 dev_name(&phydev->dev), phydev->drv->name, rev, patch);
Florian Fainellib560a582014-02-13 16:08:45 -0800177
Florian Fainellid8ebfed2014-09-19 13:07:56 -0700178 switch (rev) {
Florian Fainellid8ebfed2014-09-19 13:07:56 -0700179 case 0xb0:
Florian Fainelli2a9df742014-11-11 14:55:11 -0800180 ret = bcm7xxx_28nm_b0_afe_config_init(phydev);
Florian Fainellid8ebfed2014-09-19 13:07:56 -0700181 break;
182 default:
Florian Fainellid8ebfed2014-09-19 13:07:56 -0700183 break;
184 }
185
Florian Fainelli9df54dd2014-08-22 18:55:41 -0700186 if (ret)
187 return ret;
188
Florian Fainellib8f9a022014-08-22 18:55:45 -0700189 ret = bcm7xxx_eee_enable(phydev);
190 if (ret)
191 return ret;
192
Florian Fainelli9df54dd2014-08-22 18:55:41 -0700193 return bcm7xxx_apd_enable(phydev);
Florian Fainellib560a582014-02-13 16:08:45 -0800194}
195
Florian Fainelli4fd14e02014-08-14 16:52:52 -0700196static int bcm7xxx_28nm_resume(struct phy_device *phydev)
197{
198 int ret;
199
200 /* Re-apply workarounds coming out suspend/resume */
201 ret = bcm7xxx_28nm_config_init(phydev);
202 if (ret)
203 return ret;
204
205 /* 28nm Gigabit PHYs come out of reset without any half-duplex
206 * or "hub" compliant advertised mode, fix that. This does not
207 * cause any problems with the PHY library since genphy_config_aneg()
208 * gracefully handles auto-negotiated and forced modes.
209 */
210 return genphy_config_aneg(phydev);
211}
212
Florian Fainellib560a582014-02-13 16:08:45 -0800213static int phy_set_clr_bits(struct phy_device *dev, int location,
214 int set_mask, int clr_mask)
215{
216 int v, ret;
217
218 v = phy_read(dev, location);
219 if (v < 0)
220 return v;
221
222 v &= ~clr_mask;
223 v |= set_mask;
224
225 ret = phy_write(dev, location, v);
226 if (ret < 0)
227 return ret;
228
229 return v;
230}
231
232static int bcm7xxx_config_init(struct phy_device *phydev)
233{
234 int ret;
235
236 /* Enable 64 clock MDIO */
237 phy_write(phydev, MII_BCM7XXX_AUX_MODE, MII_BCM7XX_64CLK_MDIO);
238 phy_read(phydev, MII_BCM7XXX_AUX_MODE);
239
Florian Fainellie18556e2014-09-19 13:07:51 -0700240 /* Workaround only required for 100Mbits/sec capable PHYs */
241 if (phydev->supported & PHY_GBIT_FEATURES)
Florian Fainellib560a582014-02-13 16:08:45 -0800242 return 0;
243
244 /* set shadow mode 2 */
245 ret = phy_set_clr_bits(phydev, MII_BCM7XXX_TEST,
246 MII_BCM7XXX_SHD_MODE_2, MII_BCM7XXX_SHD_MODE_2);
247 if (ret < 0)
248 return ret;
249
250 /* set iddq_clkbias */
251 phy_write(phydev, MII_BCM7XXX_100TX_DISC, 0x0F00);
252 udelay(10);
253
254 /* reset iddq_clkbias */
255 phy_write(phydev, MII_BCM7XXX_100TX_DISC, 0x0C00);
256
257 phy_write(phydev, MII_BCM7XXX_100TX_FALSE_CAR, 0x7555);
258
259 /* reset shadow mode 2 */
260 ret = phy_set_clr_bits(phydev, MII_BCM7XXX_TEST, MII_BCM7XXX_SHD_MODE_2, 0);
261 if (ret < 0)
262 return ret;
263
264 return 0;
265}
266
267/* Workaround for putting the PHY in IDDQ mode, required
Florian Fainelli82c084f2014-08-14 16:52:53 -0700268 * for all BCM7XXX 40nm and 65nm PHYs
Florian Fainellib560a582014-02-13 16:08:45 -0800269 */
270static int bcm7xxx_suspend(struct phy_device *phydev)
271{
272 int ret;
273 const struct bcm7xxx_regs {
274 int reg;
275 u16 value;
276 } bcm7xxx_suspend_cfg[] = {
277 { MII_BCM7XXX_TEST, 0x008b },
278 { MII_BCM7XXX_100TX_AUX_CTL, 0x01c0 },
279 { MII_BCM7XXX_100TX_DISC, 0x7000 },
280 { MII_BCM7XXX_TEST, 0x000f },
281 { MII_BCM7XXX_100TX_AUX_CTL, 0x20d0 },
282 { MII_BCM7XXX_TEST, 0x000b },
283 };
284 unsigned int i;
285
286 for (i = 0; i < ARRAY_SIZE(bcm7xxx_suspend_cfg); i++) {
287 ret = phy_write(phydev,
288 bcm7xxx_suspend_cfg[i].reg,
289 bcm7xxx_suspend_cfg[i].value);
290 if (ret)
291 return ret;
292 }
293
294 return 0;
295}
296
297static int bcm7xxx_dummy_config_init(struct phy_device *phydev)
298{
299 return 0;
300}
301
Florian Fainelli153df3c2014-08-26 13:15:24 -0700302#define BCM7XXX_28NM_GPHY(_oui, _name) \
303{ \
304 .phy_id = (_oui), \
305 .phy_id_mask = 0xfffffff0, \
306 .name = _name, \
307 .features = PHY_GBIT_FEATURES | \
308 SUPPORTED_Pause | SUPPORTED_Asym_Pause, \
309 .flags = PHY_IS_INTERNAL, \
Florian Fainelli2a9df742014-11-11 14:55:11 -0800310 .config_init = bcm7xxx_28nm_config_init, \
Florian Fainelli153df3c2014-08-26 13:15:24 -0700311 .config_aneg = genphy_config_aneg, \
312 .read_status = genphy_read_status, \
313 .resume = bcm7xxx_28nm_resume, \
314 .driver = { .owner = THIS_MODULE }, \
315}
316
Florian Fainellib560a582014-02-13 16:08:45 -0800317static struct phy_driver bcm7xxx_driver[] = {
Florian Fainelli430ad682014-08-26 13:15:27 -0700318 BCM7XXX_28NM_GPHY(PHY_ID_BCM7250, "Broadcom BCM7250"),
319 BCM7XXX_28NM_GPHY(PHY_ID_BCM7364, "Broadcom BCM7364"),
Florian Fainelli153df3c2014-08-26 13:15:24 -0700320 BCM7XXX_28NM_GPHY(PHY_ID_BCM7366, "Broadcom BCM7366"),
321 BCM7XXX_28NM_GPHY(PHY_ID_BCM7439, "Broadcom BCM7439"),
322 BCM7XXX_28NM_GPHY(PHY_ID_BCM7445, "Broadcom BCM7445"),
Florian Fainellib560a582014-02-13 16:08:45 -0800323{
Petri Gyntherd068b022014-10-01 11:58:02 -0700324 .phy_id = PHY_ID_BCM7425,
325 .phy_id_mask = 0xfffffff0,
326 .name = "Broadcom BCM7425",
327 .features = PHY_GBIT_FEATURES |
328 SUPPORTED_Pause | SUPPORTED_Asym_Pause,
329 .flags = 0,
330 .config_init = bcm7xxx_config_init,
331 .config_aneg = genphy_config_aneg,
332 .read_status = genphy_read_status,
333 .suspend = bcm7xxx_suspend,
334 .resume = bcm7xxx_config_init,
335 .driver = { .owner = THIS_MODULE },
336}, {
337 .phy_id = PHY_ID_BCM7429,
338 .phy_id_mask = 0xfffffff0,
339 .name = "Broadcom BCM7429",
340 .features = PHY_GBIT_FEATURES |
341 SUPPORTED_Pause | SUPPORTED_Asym_Pause,
342 .flags = PHY_IS_INTERNAL,
343 .config_init = bcm7xxx_config_init,
344 .config_aneg = genphy_config_aneg,
345 .read_status = genphy_read_status,
346 .suspend = bcm7xxx_suspend,
347 .resume = bcm7xxx_config_init,
348 .driver = { .owner = THIS_MODULE },
349}, {
Florian Fainellib560a582014-02-13 16:08:45 -0800350 .phy_id = PHY_BCM_OUI_4,
351 .phy_id_mask = 0xffff0000,
352 .name = "Broadcom BCM7XXX 40nm",
353 .features = PHY_GBIT_FEATURES |
354 SUPPORTED_Pause | SUPPORTED_Asym_Pause,
355 .flags = PHY_IS_INTERNAL,
356 .config_init = bcm7xxx_config_init,
357 .config_aneg = genphy_config_aneg,
358 .read_status = genphy_read_status,
359 .suspend = bcm7xxx_suspend,
360 .resume = bcm7xxx_config_init,
361 .driver = { .owner = THIS_MODULE },
362}, {
363 .phy_id = PHY_BCM_OUI_5,
364 .phy_id_mask = 0xffffff00,
365 .name = "Broadcom BCM7XXX 65nm",
366 .features = PHY_BASIC_FEATURES |
367 SUPPORTED_Pause | SUPPORTED_Asym_Pause,
368 .flags = PHY_IS_INTERNAL,
369 .config_init = bcm7xxx_dummy_config_init,
370 .config_aneg = genphy_config_aneg,
371 .read_status = genphy_read_status,
372 .suspend = bcm7xxx_suspend,
373 .resume = bcm7xxx_config_init,
374 .driver = { .owner = THIS_MODULE },
375} };
376
377static struct mdio_device_id __maybe_unused bcm7xxx_tbl[] = {
Florian Fainelli430ad682014-08-26 13:15:27 -0700378 { PHY_ID_BCM7250, 0xfffffff0, },
379 { PHY_ID_BCM7364, 0xfffffff0, },
Florian Fainellib560a582014-02-13 16:08:45 -0800380 { PHY_ID_BCM7366, 0xfffffff0, },
Petri Gyntherd068b022014-10-01 11:58:02 -0700381 { PHY_ID_BCM7425, 0xfffffff0, },
382 { PHY_ID_BCM7429, 0xfffffff0, },
Florian Fainellib560a582014-02-13 16:08:45 -0800383 { PHY_ID_BCM7439, 0xfffffff0, },
384 { PHY_ID_BCM7445, 0xfffffff0, },
Florian Fainellib560a582014-02-13 16:08:45 -0800385 { PHY_BCM_OUI_4, 0xffff0000 },
386 { PHY_BCM_OUI_5, 0xffffff00 },
387 { }
388};
389
Johan Hovold50fd7152014-11-11 19:45:59 +0100390module_phy_driver(bcm7xxx_driver);
Florian Fainellib560a582014-02-13 16:08:45 -0800391
392MODULE_DEVICE_TABLE(mdio, bcm7xxx_tbl);
393
394MODULE_DESCRIPTION("Broadcom BCM7xxx internal PHY driver");
395MODULE_LICENSE("GPL");
396MODULE_AUTHOR("Broadcom Corporation");