2 * Amlogic Meson GXL Internal PHY Driver
4 * Copyright (C) 2015 Amlogic, Inc. All rights reserved.
5 * Copyright (C) 2016 BayLibre, SAS. All rights reserved.
6 * Author: Neil Armstrong <narmstrong@baylibre.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/mii.h>
22 #include <linux/ethtool.h>
23 #include <linux/phy.h>
24 #include <linux/netdevice.h>
25 #include <linux/bitfield.h>
28 #define TSTCNTL_READ BIT(15)
29 #define TSTCNTL_WRITE BIT(14)
30 #define TSTCNTL_REG_BANK_SEL GENMASK(12, 11)
31 #define TSTCNTL_TEST_MODE BIT(10)
32 #define TSTCNTL_READ_ADDRESS GENMASK(9, 5)
33 #define TSTCNTL_WRITE_ADDRESS GENMASK(4, 0)
36 #define INTSRC_FLAG 29
37 #define INTSRC_ANEG_PR BIT(1)
38 #define INTSRC_PARALLEL_FAULT BIT(2)
39 #define INTSRC_ANEG_LP_ACK BIT(3)
40 #define INTSRC_LINK_DOWN BIT(4)
41 #define INTSRC_REMOTE_FAULT BIT(5)
42 #define INTSRC_ANEG_COMPLETE BIT(6)
43 #define INTSRC_MASK 30
45 #define BANK_ANALOG_DSP 0
50 #define LPI_STATUS 0xc
51 #define LPI_STATUS_RSV12 BIT(12)
54 #define FR_PLL_CONTROL 0x1b
55 #define FR_PLL_DIV0 0x1c
56 #define FR_PLL_DIV1 0x1d
58 static int meson_gxl_open_banks(struct phy_device
*phydev
)
62 /* Enable Analog and DSP register Bank access by
63 * toggling TSTCNTL_TEST_MODE bit in the TSTCNTL register
65 ret
= phy_write(phydev
, TSTCNTL
, 0);
68 ret
= phy_write(phydev
, TSTCNTL
, TSTCNTL_TEST_MODE
);
71 ret
= phy_write(phydev
, TSTCNTL
, 0);
74 return phy_write(phydev
, TSTCNTL
, TSTCNTL_TEST_MODE
);
77 static void meson_gxl_close_banks(struct phy_device
*phydev
)
79 phy_write(phydev
, TSTCNTL
, 0);
82 static int meson_gxl_read_reg(struct phy_device
*phydev
,
83 unsigned int bank
, unsigned int reg
)
87 ret
= meson_gxl_open_banks(phydev
);
91 ret
= phy_write(phydev
, TSTCNTL
, TSTCNTL_READ
|
92 FIELD_PREP(TSTCNTL_REG_BANK_SEL
, bank
) |
94 FIELD_PREP(TSTCNTL_READ_ADDRESS
, reg
));
98 ret
= phy_read(phydev
, TSTREAD1
);
100 /* Close the bank access on our way out */
101 meson_gxl_close_banks(phydev
);
105 static int meson_gxl_write_reg(struct phy_device
*phydev
,
106 unsigned int bank
, unsigned int reg
,
111 ret
= meson_gxl_open_banks(phydev
);
115 ret
= phy_write(phydev
, TSTWRITE
, value
);
119 ret
= phy_write(phydev
, TSTCNTL
, TSTCNTL_WRITE
|
120 FIELD_PREP(TSTCNTL_REG_BANK_SEL
, bank
) |
122 FIELD_PREP(TSTCNTL_WRITE_ADDRESS
, reg
));
125 /* Close the bank access on our way out */
126 meson_gxl_close_banks(phydev
);
130 static int meson_gxl_config_init(struct phy_device
*phydev
)
134 /* Enable fractional PLL */
135 ret
= meson_gxl_write_reg(phydev
, BANK_BIST
, FR_PLL_CONTROL
, 0x5);
139 /* Program fraction FR_PLL_DIV1 */
140 ret
= meson_gxl_write_reg(phydev
, BANK_BIST
, FR_PLL_DIV1
, 0x029a);
144 /* Program fraction FR_PLL_DIV1 */
145 ret
= meson_gxl_write_reg(phydev
, BANK_BIST
, FR_PLL_DIV0
, 0xaaaa);
149 return genphy_config_init(phydev
);
152 /* This function is provided to cope with the possible failures of this phy
153 * during aneg process. When aneg fails, the PHY reports that aneg is done
154 * but the value found in MII_LPA is wrong:
155 * - Early failures: MII_LPA is just 0x0001. if MII_EXPANSION reports that
156 * the link partner (LP) supports aneg but the LP never acked our base
157 * code word, it is likely that we never sent it to begin with.
158 * - Late failures: MII_LPA is filled with a value which seems to make sense
159 * but it actually is not what the LP is advertising. It seems that we
160 * can detect this using a magic bit in the WOL bank (reg 12 - bit 12).
161 * If this particular bit is not set when aneg is reported being done,
162 * it means MII_LPA is likely to be wrong.
164 * In both case, forcing a restart of the aneg process solve the problem.
165 * When this failure happens, the first retry is usually successful but,
166 * in some cases, it may take up to 6 retries to get a decent result
168 static int meson_gxl_read_status(struct phy_device
*phydev
)
170 int ret
, wol
, lpa
, exp
;
172 if (phydev
->autoneg
== AUTONEG_ENABLE
) {
173 ret
= genphy_aneg_done(phydev
);
177 goto read_status_continue
;
179 /* Aneg is done, let's check everything is fine */
180 wol
= meson_gxl_read_reg(phydev
, BANK_WOL
, LPI_STATUS
);
184 lpa
= phy_read(phydev
, MII_LPA
);
188 exp
= phy_read(phydev
, MII_EXPANSION
);
192 if (!(wol
& LPI_STATUS_RSV12
) ||
193 ((exp
& EXPANSION_NWAY
) && !(lpa
& LPA_LPACK
))) {
194 /* Looks like aneg failed after all */
195 phydev_dbg(phydev
, "LPA corruption - aneg restart\n");
196 return genphy_restart_aneg(phydev
);
200 read_status_continue
:
201 return genphy_read_status(phydev
);
204 static int meson_gxl_ack_interrupt(struct phy_device
*phydev
)
206 int ret
= phy_read(phydev
, INTSRC_FLAG
);
208 return ret
< 0 ? ret
: 0;
211 static int meson_gxl_config_intr(struct phy_device
*phydev
)
215 if (phydev
->interrupts
== PHY_INTERRUPT_ENABLED
) {
217 | INTSRC_PARALLEL_FAULT
220 | INTSRC_REMOTE_FAULT
221 | INTSRC_ANEG_COMPLETE
;
226 return phy_write(phydev
, INTSRC_MASK
, val
);
229 static struct phy_driver meson_gxl_phy
[] = {
231 .phy_id
= 0x01814400,
232 .phy_id_mask
= 0xfffffff0,
233 .name
= "Meson GXL Internal PHY",
234 .features
= PHY_BASIC_FEATURES
,
235 .flags
= PHY_IS_INTERNAL
| PHY_HAS_INTERRUPT
,
236 .config_init
= meson_gxl_config_init
,
237 .aneg_done
= genphy_aneg_done
,
238 .read_status
= meson_gxl_read_status
,
239 .ack_interrupt
= meson_gxl_ack_interrupt
,
240 .config_intr
= meson_gxl_config_intr
,
241 .suspend
= genphy_suspend
,
242 .resume
= genphy_resume
,
246 static struct mdio_device_id __maybe_unused meson_gxl_tbl
[] = {
247 { 0x01814400, 0xfffffff0 },
251 module_phy_driver(meson_gxl_phy
);
253 MODULE_DEVICE_TABLE(mdio
, meson_gxl_tbl
);
255 MODULE_DESCRIPTION("Amlogic Meson GXL Internal PHY driver");
256 MODULE_AUTHOR("Baoqi wang");
257 MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
258 MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
259 MODULE_LICENSE("GPL");