gxlphy.c revision 1.1 1 /* $NetBSD: gxlphy.c,v 1.1 2019/04/21 11:02:32 jmcneill Exp $ */
2
3 /*
4 * Copyright (c) 2019 Jared McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * Amlogic Meson GXL 10/100 internal PHY
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: gxlphy.c,v 1.1 2019/04/21 11:02:32 jmcneill Exp $");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/device.h>
40 #include <sys/socket.h>
41 #include <sys/errno.h>
42
43 #include <net/if.h>
44 #include <net/if_media.h>
45
46 #include <dev/mii/mii.h>
47 #include <dev/mii/miivar.h>
48 #include <dev/mii/miidevs.h>
49
50 #define TSTCNTL 20
51 #define TSTCNTL_READ __BIT(15)
52 #define TSTCNTL_WRITE __BIT(14)
53 #define TSTCNTL_REG_BANK_SEL __BITS(12,11)
54 #define TSTCNTL_TEST_MODE __BIT(10)
55 #define TSTCNTL_READ_ADDR __BITS(9,5)
56 #define TSTCNTL_WRITE_ADDR __BITS(4,0)
57 #define TSTREAD1 21
58 #define TSTWRITE 23
59
60 #define BANK_WOL 1
61 #define BANK_BIST 3
62
63 #define LPI_STATUS 0x0c
64 #define LPI_STATUS_RSV12 __BIT(12)
65
66 #define BIST_PLL_CTRL 0x1b
67 #define BIST_PLL_DIV0 0x1c
68 #define BIST_PLL_DIV1 0x1d
69
70 static int gxlphymatch(device_t, cfdata_t, void *);
71 static void gxlphyattach(device_t, device_t, void *);
72
73 CFATTACH_DECL3_NEW(gxlphy, sizeof(struct mii_softc),
74 gxlphymatch, gxlphyattach, mii_phy_detach, mii_phy_activate, NULL, NULL,
75 DVF_DETACH_SHUTDOWN);
76
77 static int gxlphy_service(struct mii_softc *, struct mii_data *, int);
78 static void gxlphy_status(struct mii_softc *);
79
80 static const struct mii_phy_funcs gxlphy_funcs = {
81 gxlphy_service, gxlphy_status, mii_phy_reset,
82 };
83
84 static const struct mii_phydesc gxlphys[] = {
85 MII_PHY_DESC(xxAMLOGIC, GXL),
86 MII_PHY_END,
87 };
88
89 static void
90 gxl_openbanks(struct mii_softc *sc)
91 {
92 PHY_WRITE(sc, TSTCNTL, 0);
93 PHY_WRITE(sc, TSTCNTL, TSTCNTL_TEST_MODE);
94 PHY_WRITE(sc, TSTCNTL, 0);
95 PHY_WRITE(sc, TSTCNTL, TSTCNTL_TEST_MODE);
96 }
97
98 static void
99 gxl_closebanks(struct mii_softc *sc)
100 {
101 PHY_WRITE(sc, TSTCNTL, 0);
102 }
103
104 static uint16_t
105 gxl_readreg(struct mii_softc *sc, u_int bank, u_int reg)
106 {
107 uint16_t val;
108
109 gxl_openbanks(sc);
110 PHY_WRITE(sc, TSTCNTL,
111 TSTCNTL_READ | TSTCNTL_TEST_MODE |
112 __SHIFTIN(bank, TSTCNTL_REG_BANK_SEL) |
113 __SHIFTIN(reg, TSTCNTL_READ_ADDR));
114 PHY_READ(sc, TSTREAD1, &val);
115 gxl_closebanks(sc);
116
117 return val;
118 }
119
120 static void
121 gxl_writereg(struct mii_softc *sc, u_int bank, u_int reg, uint16_t val)
122 {
123 gxl_openbanks(sc);
124 PHY_WRITE(sc, TSTWRITE, val);
125 PHY_WRITE(sc, TSTCNTL,
126 TSTCNTL_WRITE | TSTCNTL_TEST_MODE |
127 __SHIFTIN(bank, TSTCNTL_REG_BANK_SEL) |
128 __SHIFTIN(reg, TSTCNTL_WRITE_ADDR));
129 gxl_closebanks(sc);
130 }
131
132 static int
133 gxlphymatch(device_t parent, cfdata_t match, void *aux)
134 {
135 struct mii_attach_args *ma = aux;
136
137 if (mii_phy_match(ma, gxlphys) != NULL)
138 return 20;
139
140 return 0;
141 }
142
143 static void
144 gxlphyattach(device_t parent, device_t self, void *aux)
145 {
146 struct mii_softc *sc = device_private(self);
147 struct mii_attach_args *ma = aux;
148 struct mii_data *mii = ma->mii_data;
149 int oui = MII_OUI(ma->mii_id1, ma->mii_id2);
150 int model = MII_MODEL(ma->mii_id2);
151 int rev = MII_REV(ma->mii_id2);
152 const char *descr;
153
154 if ((descr = mii_get_descr(oui, model)) != NULL)
155 aprint_normal(": %s (OUI 0x%06x, model 0x%04x), rev. %d\n",
156 descr, oui, model, rev);
157 else
158 aprint_normal(": OUI 0x%06x, model 0x%04x, rev. %d\n",
159 oui, model, rev);
160 aprint_naive(": Media interface\n");
161
162 sc->mii_dev = self;
163 sc->mii_inst = mii->mii_instance;
164 sc->mii_phy = ma->mii_phyno;
165 sc->mii_mpd_oui = MII_OUI(ma->mii_id1, ma->mii_id2);
166 sc->mii_mpd_model = MII_MODEL(ma->mii_id2);
167 sc->mii_mpd_rev = MII_REV(ma->mii_id2);
168 sc->mii_funcs = &gxlphy_funcs;
169 sc->mii_pdata = mii;
170 sc->mii_flags = ma->mii_flags;
171 sc->mii_anegticks = MII_ANEGTICKS;
172
173 PHY_RESET(sc);
174
175 gxl_writereg(sc, BANK_BIST, BIST_PLL_CTRL, 0x5);
176 gxl_writereg(sc, BANK_BIST, BIST_PLL_DIV1, 0x029a);
177 gxl_writereg(sc, BANK_BIST, BIST_PLL_DIV0, 0xaaaa);
178
179 PHY_READ(sc, MII_BMSR, &sc->mii_capabilities);
180 sc->mii_capabilities &= ma->mii_capmask;
181 if (sc->mii_capabilities & BMSR_EXTSTAT)
182 PHY_READ(sc, MII_EXTSR, &sc->mii_extcapabilities);
183 aprint_normal_dev(self, "");
184 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 &&
185 (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0)
186 aprint_error("no media present");
187 else
188 mii_phy_add_media(sc);
189 aprint_normal("\n");
190 }
191
192 static int
193 gxlphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
194 {
195 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
196 uint16_t reg;
197
198 switch (cmd) {
199 case MII_POLLSTAT:
200 /* If we're not polling our PHY instance, just return. */
201 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
202 return 0;
203 break;
204
205 case MII_MEDIACHG:
206 /*
207 * If the media indicates a different PHY instance,
208 * isolate ourselves.
209 */
210 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
211 PHY_READ(sc, MII_BMCR, ®);
212 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
213 return 0;
214 }
215
216 /* If the interface is not up, don't do anything. */
217 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
218 break;
219
220 mii_phy_setmedia(sc);
221 break;
222
223 case MII_TICK:
224 /* If we're not currently selected, just return. */
225 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
226 return 0;
227
228 if (mii_phy_tick(sc) == EJUSTRETURN)
229 return 0;
230 break;
231
232 case MII_DOWN:
233 mii_phy_down(sc);
234 return 0;
235 }
236
237 /* Update the media status. */
238 mii_phy_status(sc);
239
240 /* Callback if something changed. */
241 mii_phy_update(sc, cmd);
242 return 0;
243 }
244
245 static void
246 gxlphy_status(struct mii_softc *sc)
247 {
248 uint16_t bmcr, bmsr, wol, lpa, aner;
249
250 PHY_READ(sc, MII_BMCR, &bmcr);
251 if ((bmcr & BMCR_AUTOEN) == 0)
252 goto done;
253
254 PHY_READ(sc, MII_BMSR, &bmsr);
255 if ((bmsr & BMSR_ACOMP) == 0)
256 goto done;
257
258 wol = gxl_readreg(sc, BANK_WOL, LPI_STATUS);
259 PHY_READ(sc, MII_ANLPAR, &lpa);
260 PHY_READ(sc, MII_ANER, &aner);
261
262 if ((wol & LPI_STATUS_RSV12) == 0 ||
263 ((aner & ANER_LPAN) != 0 && (lpa & ANLPAR_ACK) == 0)) {
264 device_printf(sc->mii_dev, "LPA corruption - aneg restart\n");
265
266 bmcr &= ~BMCR_ISO;
267 bmcr |= BMCR_AUTOEN;
268 bmcr |= BMCR_STARTNEG;
269 PHY_WRITE(sc, MII_BMCR, bmcr);
270
271 return;
272 }
273
274 done:
275 ukphy_status(sc);
276 }
277