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