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