blob: 29e256a4ed57a02e448fd87f066697c743daed51 [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>
17
18/* Broadcom BCM7xxx internal PHY registers */
19#define MII_BCM7XXX_CHANNEL_WIDTH 0x2000
20
21/* 40nm only register definitions */
22#define MII_BCM7XXX_100TX_AUX_CTL 0x10
23#define MII_BCM7XXX_100TX_FALSE_CAR 0x13
24#define MII_BCM7XXX_100TX_DISC 0x14
25#define MII_BCM7XXX_AUX_MODE 0x1d
26#define MII_BCM7XX_64CLK_MDIO BIT(12)
27#define MII_BCM7XXX_CORE_BASE1E 0x1e
28#define MII_BCM7XXX_TEST 0x1f
29#define MII_BCM7XXX_SHD_MODE_2 BIT(2)
30
Florian Fainellia3622f22014-03-24 16:36:47 -070031/* 28nm only register definitions */
32#define MISC_ADDR(base, channel) base, channel
33
34#define DSP_TAP10 MISC_ADDR(0x0a, 0)
35#define PLL_PLLCTRL_1 MISC_ADDR(0x32, 1)
36#define PLL_PLLCTRL_2 MISC_ADDR(0x32, 2)
37#define PLL_PLLCTRL_4 MISC_ADDR(0x33, 0)
38
39#define AFE_RXCONFIG_0 MISC_ADDR(0x38, 0)
40#define AFE_RXCONFIG_1 MISC_ADDR(0x38, 1)
41#define AFE_RX_LP_COUNTER MISC_ADDR(0x38, 3)
42#define AFE_TX_CONFIG MISC_ADDR(0x39, 0)
43#define AFE_HPF_TRIM_OTHERS MISC_ADDR(0x3a, 0)
44
45#define CORE_EXPB0 0xb0
46
Florian Fainellib560a582014-02-13 16:08:45 -080047static int bcm7445_config_init(struct phy_device *phydev)
48{
49 int ret;
50 const struct bcm7445_regs {
51 int reg;
52 u16 value;
53 } bcm7445_regs_cfg[] = {
54 /* increases ADC latency by 24ns */
55 { MII_BCM54XX_EXP_SEL, 0x0038 },
56 { MII_BCM54XX_EXP_DATA, 0xAB95 },
57 /* increases internal 1V LDO voltage by 5% */
58 { MII_BCM54XX_EXP_SEL, 0x2038 },
59 { MII_BCM54XX_EXP_DATA, 0xBB22 },
60 /* reduce RX low pass filter corner frequency */
61 { MII_BCM54XX_EXP_SEL, 0x6038 },
62 { MII_BCM54XX_EXP_DATA, 0xFFC5 },
63 /* reduce RX high pass filter corner frequency */
64 { MII_BCM54XX_EXP_SEL, 0x003a },
65 { MII_BCM54XX_EXP_DATA, 0x2002 },
66 };
67 unsigned int i;
68
69 for (i = 0; i < ARRAY_SIZE(bcm7445_regs_cfg); i++) {
70 ret = phy_write(phydev,
71 bcm7445_regs_cfg[i].reg,
72 bcm7445_regs_cfg[i].value);
73 if (ret)
74 return ret;
75 }
76
77 return 0;
78}
79
80static void phy_write_exp(struct phy_device *phydev,
81 u16 reg, u16 value)
82{
83 phy_write(phydev, MII_BCM54XX_EXP_SEL, MII_BCM54XX_EXP_SEL_ER | reg);
84 phy_write(phydev, MII_BCM54XX_EXP_DATA, value);
85}
86
87static void phy_write_misc(struct phy_device *phydev,
88 u16 reg, u16 chl, u16 value)
89{
90 int tmp;
91
92 phy_write(phydev, MII_BCM54XX_AUX_CTL, MII_BCM54XX_AUXCTL_SHDWSEL_MISC);
93
94 tmp = phy_read(phydev, MII_BCM54XX_AUX_CTL);
95 tmp |= MII_BCM54XX_AUXCTL_ACTL_SMDSP_ENA;
96 phy_write(phydev, MII_BCM54XX_AUX_CTL, tmp);
97
98 tmp = (chl * MII_BCM7XXX_CHANNEL_WIDTH) | reg;
99 phy_write(phydev, MII_BCM54XX_EXP_SEL, tmp);
100
101 phy_write(phydev, MII_BCM54XX_EXP_DATA, value);
102}
103
104static int bcm7xxx_28nm_afe_config_init(struct phy_device *phydev)
105{
Florian Fainellib560a582014-02-13 16:08:45 -0800106 /* Increase VCO range to prevent unlocking problem of PLL at low
107 * temp
108 */
Florian Fainellia3622f22014-03-24 16:36:47 -0700109 phy_write_misc(phydev, PLL_PLLCTRL_1, 0x0048);
Florian Fainellib560a582014-02-13 16:08:45 -0800110
111 /* Change Ki to 011 */
Florian Fainellia3622f22014-03-24 16:36:47 -0700112 phy_write_misc(phydev, PLL_PLLCTRL_2, 0x021b);
Florian Fainellib560a582014-02-13 16:08:45 -0800113
114 /* Disable loading of TVCO buffer to bandgap, set bandgap trim
115 * to 111
116 */
Florian Fainellia3622f22014-03-24 16:36:47 -0700117 phy_write_misc(phydev, PLL_PLLCTRL_4, 0x0e20);
Florian Fainellib560a582014-02-13 16:08:45 -0800118
119 /* Adjust bias current trim by -3 */
Florian Fainellia3622f22014-03-24 16:36:47 -0700120 phy_write_misc(phydev, DSP_TAP10, 0x690b);
Florian Fainellib560a582014-02-13 16:08:45 -0800121
122 /* Switch to CORE_BASE1E */
123 phy_write(phydev, MII_BCM7XXX_CORE_BASE1E, 0xd);
124
125 /* Reset R_CAL/RC_CAL Engine */
Florian Fainellia3622f22014-03-24 16:36:47 -0700126 phy_write_exp(phydev, CORE_EXPB0, 0x0010);
Florian Fainellib560a582014-02-13 16:08:45 -0800127
128 /* Disable Reset R_CAL/RC_CAL Engine */
Florian Fainellia3622f22014-03-24 16:36:47 -0700129 phy_write_exp(phydev, CORE_EXPB0, 0x0000);
Florian Fainellib560a582014-02-13 16:08:45 -0800130
Florian Fainelli99185422014-03-24 16:36:48 -0700131 /* write AFE_RXCONFIG_0 */
132 phy_write_misc(phydev, AFE_RXCONFIG_0, 0xeb19);
133
134 /* write AFE_RXCONFIG_1 */
135 phy_write_misc(phydev, AFE_RXCONFIG_1, 0x9a3f);
136
137 /* write AFE_RX_LP_COUNTER */
Florian Fainellia62ea5a2014-03-24 16:36:49 -0700138 phy_write_misc(phydev, AFE_RX_LP_COUNTER, 0x7fc0);
Florian Fainelli99185422014-03-24 16:36:48 -0700139
140 /* write AFE_HPF_TRIM_OTHERS */
141 phy_write_misc(phydev, AFE_HPF_TRIM_OTHERS, 0x000b);
142
143 /* write AFTE_TX_CONFIG */
144 phy_write_misc(phydev, AFE_TX_CONFIG, 0x0800);
145
Florian Fainellib560a582014-02-13 16:08:45 -0800146 return 0;
147}
148
Florian Fainelli9df54dd2014-08-22 18:55:41 -0700149static int bcm7xxx_apd_enable(struct phy_device *phydev)
150{
151 int val;
152
153 /* Enable powering down of the DLL during auto-power down */
154 val = bcm54xx_shadow_read(phydev, BCM54XX_SHD_SCR3);
155 if (val < 0)
156 return val;
157
158 val |= BCM54XX_SHD_SCR3_DLLAPD_DIS;
159 bcm54xx_shadow_write(phydev, BCM54XX_SHD_SCR3, val);
160
161 /* Enable auto-power down */
162 val = bcm54xx_shadow_read(phydev, BCM54XX_SHD_APD);
163 if (val < 0)
164 return val;
165
166 val |= BCM54XX_SHD_APD_EN;
167 return bcm54xx_shadow_write(phydev, BCM54XX_SHD_APD, val);
168}
169
Florian Fainellib560a582014-02-13 16:08:45 -0800170static int bcm7xxx_28nm_config_init(struct phy_device *phydev)
171{
172 int ret;
173
174 ret = bcm7445_config_init(phydev);
175 if (ret)
176 return ret;
177
Florian Fainelli9df54dd2014-08-22 18:55:41 -0700178 ret = bcm7xxx_28nm_afe_config_init(phydev);
179 if (ret)
180 return ret;
181
182 return bcm7xxx_apd_enable(phydev);
Florian Fainellib560a582014-02-13 16:08:45 -0800183}
184
Florian Fainelli4fd14e02014-08-14 16:52:52 -0700185static int bcm7xxx_28nm_resume(struct phy_device *phydev)
186{
187 int ret;
188
189 /* Re-apply workarounds coming out suspend/resume */
190 ret = bcm7xxx_28nm_config_init(phydev);
191 if (ret)
192 return ret;
193
194 /* 28nm Gigabit PHYs come out of reset without any half-duplex
195 * or "hub" compliant advertised mode, fix that. This does not
196 * cause any problems with the PHY library since genphy_config_aneg()
197 * gracefully handles auto-negotiated and forced modes.
198 */
199 return genphy_config_aneg(phydev);
200}
201
Florian Fainellib560a582014-02-13 16:08:45 -0800202static int phy_set_clr_bits(struct phy_device *dev, int location,
203 int set_mask, int clr_mask)
204{
205 int v, ret;
206
207 v = phy_read(dev, location);
208 if (v < 0)
209 return v;
210
211 v &= ~clr_mask;
212 v |= set_mask;
213
214 ret = phy_write(dev, location, v);
215 if (ret < 0)
216 return ret;
217
218 return v;
219}
220
221static int bcm7xxx_config_init(struct phy_device *phydev)
222{
223 int ret;
224
225 /* Enable 64 clock MDIO */
226 phy_write(phydev, MII_BCM7XXX_AUX_MODE, MII_BCM7XX_64CLK_MDIO);
227 phy_read(phydev, MII_BCM7XXX_AUX_MODE);
228
229 /* Workaround only required for 100Mbits/sec */
230 if (!(phydev->dev_flags & PHY_BRCM_100MBPS_WAR))
231 return 0;
232
233 /* set shadow mode 2 */
234 ret = phy_set_clr_bits(phydev, MII_BCM7XXX_TEST,
235 MII_BCM7XXX_SHD_MODE_2, MII_BCM7XXX_SHD_MODE_2);
236 if (ret < 0)
237 return ret;
238
239 /* set iddq_clkbias */
240 phy_write(phydev, MII_BCM7XXX_100TX_DISC, 0x0F00);
241 udelay(10);
242
243 /* reset iddq_clkbias */
244 phy_write(phydev, MII_BCM7XXX_100TX_DISC, 0x0C00);
245
246 phy_write(phydev, MII_BCM7XXX_100TX_FALSE_CAR, 0x7555);
247
248 /* reset shadow mode 2 */
249 ret = phy_set_clr_bits(phydev, MII_BCM7XXX_TEST, MII_BCM7XXX_SHD_MODE_2, 0);
250 if (ret < 0)
251 return ret;
252
253 return 0;
254}
255
256/* Workaround for putting the PHY in IDDQ mode, required
Florian Fainelli82c084f2014-08-14 16:52:53 -0700257 * for all BCM7XXX 40nm and 65nm PHYs
Florian Fainellib560a582014-02-13 16:08:45 -0800258 */
259static int bcm7xxx_suspend(struct phy_device *phydev)
260{
261 int ret;
262 const struct bcm7xxx_regs {
263 int reg;
264 u16 value;
265 } bcm7xxx_suspend_cfg[] = {
266 { MII_BCM7XXX_TEST, 0x008b },
267 { MII_BCM7XXX_100TX_AUX_CTL, 0x01c0 },
268 { MII_BCM7XXX_100TX_DISC, 0x7000 },
269 { MII_BCM7XXX_TEST, 0x000f },
270 { MII_BCM7XXX_100TX_AUX_CTL, 0x20d0 },
271 { MII_BCM7XXX_TEST, 0x000b },
272 };
273 unsigned int i;
274
275 for (i = 0; i < ARRAY_SIZE(bcm7xxx_suspend_cfg); i++) {
276 ret = phy_write(phydev,
277 bcm7xxx_suspend_cfg[i].reg,
278 bcm7xxx_suspend_cfg[i].value);
279 if (ret)
280 return ret;
281 }
282
283 return 0;
284}
285
286static int bcm7xxx_dummy_config_init(struct phy_device *phydev)
287{
288 return 0;
289}
290
291static struct phy_driver bcm7xxx_driver[] = {
292{
293 .phy_id = PHY_ID_BCM7366,
294 .phy_id_mask = 0xfffffff0,
295 .name = "Broadcom BCM7366",
296 .features = PHY_GBIT_FEATURES |
297 SUPPORTED_Pause | SUPPORTED_Asym_Pause,
298 .flags = PHY_IS_INTERNAL,
299 .config_init = bcm7xxx_28nm_afe_config_init,
300 .config_aneg = genphy_config_aneg,
301 .read_status = genphy_read_status,
Florian Fainelli4fd14e02014-08-14 16:52:52 -0700302 .resume = bcm7xxx_28nm_resume,
Florian Fainellib560a582014-02-13 16:08:45 -0800303 .driver = { .owner = THIS_MODULE },
304}, {
305 .phy_id = PHY_ID_BCM7439,
306 .phy_id_mask = 0xfffffff0,
307 .name = "Broadcom BCM7439",
308 .features = PHY_GBIT_FEATURES |
309 SUPPORTED_Pause | SUPPORTED_Asym_Pause,
310 .flags = PHY_IS_INTERNAL,
311 .config_init = bcm7xxx_28nm_afe_config_init,
312 .config_aneg = genphy_config_aneg,
313 .read_status = genphy_read_status,
Florian Fainelli4fd14e02014-08-14 16:52:52 -0700314 .resume = bcm7xxx_28nm_resume,
Florian Fainellib560a582014-02-13 16:08:45 -0800315 .driver = { .owner = THIS_MODULE },
316}, {
317 .phy_id = PHY_ID_BCM7445,
318 .phy_id_mask = 0xfffffff0,
319 .name = "Broadcom BCM7445",
320 .features = PHY_GBIT_FEATURES |
321 SUPPORTED_Pause | SUPPORTED_Asym_Pause,
322 .flags = PHY_IS_INTERNAL,
323 .config_init = bcm7xxx_28nm_config_init,
324 .config_aneg = genphy_config_aneg,
325 .read_status = genphy_read_status,
Florian Fainelli4fd14e02014-08-14 16:52:52 -0700326 .resume = bcm7xxx_28nm_afe_config_init,
Florian Fainellib560a582014-02-13 16:08:45 -0800327 .driver = { .owner = THIS_MODULE },
328}, {
Florian Fainellib560a582014-02-13 16:08:45 -0800329 .phy_id = PHY_BCM_OUI_4,
330 .phy_id_mask = 0xffff0000,
331 .name = "Broadcom BCM7XXX 40nm",
332 .features = PHY_GBIT_FEATURES |
333 SUPPORTED_Pause | SUPPORTED_Asym_Pause,
334 .flags = PHY_IS_INTERNAL,
335 .config_init = bcm7xxx_config_init,
336 .config_aneg = genphy_config_aneg,
337 .read_status = genphy_read_status,
338 .suspend = bcm7xxx_suspend,
339 .resume = bcm7xxx_config_init,
340 .driver = { .owner = THIS_MODULE },
341}, {
342 .phy_id = PHY_BCM_OUI_5,
343 .phy_id_mask = 0xffffff00,
344 .name = "Broadcom BCM7XXX 65nm",
345 .features = PHY_BASIC_FEATURES |
346 SUPPORTED_Pause | SUPPORTED_Asym_Pause,
347 .flags = PHY_IS_INTERNAL,
348 .config_init = bcm7xxx_dummy_config_init,
349 .config_aneg = genphy_config_aneg,
350 .read_status = genphy_read_status,
351 .suspend = bcm7xxx_suspend,
352 .resume = bcm7xxx_config_init,
353 .driver = { .owner = THIS_MODULE },
354} };
355
356static struct mdio_device_id __maybe_unused bcm7xxx_tbl[] = {
357 { PHY_ID_BCM7366, 0xfffffff0, },
358 { PHY_ID_BCM7439, 0xfffffff0, },
359 { PHY_ID_BCM7445, 0xfffffff0, },
Florian Fainellib560a582014-02-13 16:08:45 -0800360 { PHY_BCM_OUI_4, 0xffff0000 },
361 { PHY_BCM_OUI_5, 0xffffff00 },
362 { }
363};
364
365static int __init bcm7xxx_phy_init(void)
366{
367 return phy_drivers_register(bcm7xxx_driver,
368 ARRAY_SIZE(bcm7xxx_driver));
369}
370
371static void __exit bcm7xxx_phy_exit(void)
372{
373 phy_drivers_unregister(bcm7xxx_driver,
374 ARRAY_SIZE(bcm7xxx_driver));
375}
376
377module_init(bcm7xxx_phy_init);
378module_exit(bcm7xxx_phy_exit);
379
380MODULE_DEVICE_TABLE(mdio, bcm7xxx_tbl);
381
382MODULE_DESCRIPTION("Broadcom BCM7xxx internal PHY driver");
383MODULE_LICENSE("GPL");
384MODULE_AUTHOR("Broadcom Corporation");