1*7a9a30c5Sthorpej /* $NetBSD: sqphy.c,v 1.56 2020/03/15 23:04:50 thorpej Exp $ */
2b5ebc7ddSthorpej
3b5ebc7ddSthorpej /*-
47914a89bSthorpej * Copyright (c) 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc.
5b5ebc7ddSthorpej * All rights reserved.
6b5ebc7ddSthorpej *
7b5ebc7ddSthorpej * This code is derived from software contributed to The NetBSD Foundation
8b5ebc7ddSthorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9b5ebc7ddSthorpej * NASA Ames Research Center.
10b5ebc7ddSthorpej *
11b5ebc7ddSthorpej * Redistribution and use in source and binary forms, with or without
12b5ebc7ddSthorpej * modification, are permitted provided that the following conditions
13b5ebc7ddSthorpej * are met:
14b5ebc7ddSthorpej * 1. Redistributions of source code must retain the above copyright
15b5ebc7ddSthorpej * notice, this list of conditions and the following disclaimer.
16b5ebc7ddSthorpej * 2. Redistributions in binary form must reproduce the above copyright
17b5ebc7ddSthorpej * notice, this list of conditions and the following disclaimer in the
18b5ebc7ddSthorpej * documentation and/or other materials provided with the distribution.
19b5ebc7ddSthorpej *
20b5ebc7ddSthorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21b5ebc7ddSthorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22b5ebc7ddSthorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23b5ebc7ddSthorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24b5ebc7ddSthorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25b5ebc7ddSthorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26b5ebc7ddSthorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27b5ebc7ddSthorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28b5ebc7ddSthorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29b5ebc7ddSthorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30b5ebc7ddSthorpej * POSSIBILITY OF SUCH DAMAGE.
31b5ebc7ddSthorpej */
32b5ebc7ddSthorpej
33b5ebc7ddSthorpej /*
34b5ebc7ddSthorpej * Copyright (c) 1997 Manuel Bouyer. All rights reserved.
35b5ebc7ddSthorpej *
36b5ebc7ddSthorpej * Redistribution and use in source and binary forms, with or without
37b5ebc7ddSthorpej * modification, are permitted provided that the following conditions
38b5ebc7ddSthorpej * are met:
39b5ebc7ddSthorpej * 1. Redistributions of source code must retain the above copyright
40b5ebc7ddSthorpej * notice, this list of conditions and the following disclaimer.
41b5ebc7ddSthorpej * 2. Redistributions in binary form must reproduce the above copyright
42b5ebc7ddSthorpej * notice, this list of conditions and the following disclaimer in the
43b5ebc7ddSthorpej * documentation and/or other materials provided with the distribution.
44b5ebc7ddSthorpej *
45b5ebc7ddSthorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
46b5ebc7ddSthorpej * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47b5ebc7ddSthorpej * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
48b5ebc7ddSthorpej * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
49b5ebc7ddSthorpej * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50b5ebc7ddSthorpej * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
51b5ebc7ddSthorpej * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
52b5ebc7ddSthorpej * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
53b5ebc7ddSthorpej * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
54b5ebc7ddSthorpej * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55b5ebc7ddSthorpej */
56b5ebc7ddSthorpej
57b5ebc7ddSthorpej /*
587914a89bSthorpej * driver for Seeq 80220/80221, 80223, and 80225 10/100 ethernet PHYs
59b5ebc7ddSthorpej * datasheet from www.seeq.com
60b5ebc7ddSthorpej */
61b5ebc7ddSthorpej
628b7bb912Slukem #include <sys/cdefs.h>
63*7a9a30c5Sthorpej __KERNEL_RCSID(0, "$NetBSD: sqphy.c,v 1.56 2020/03/15 23:04:50 thorpej Exp $");
648b7bb912Slukem
65b5ebc7ddSthorpej #include <sys/param.h>
66b5ebc7ddSthorpej #include <sys/systm.h>
67b5ebc7ddSthorpej #include <sys/kernel.h>
68b5ebc7ddSthorpej #include <sys/device.h>
69b5ebc7ddSthorpej #include <sys/socket.h>
7083f9ab3cSthorpej #include <sys/errno.h>
71b5ebc7ddSthorpej
72b5ebc7ddSthorpej #include <net/if.h>
73b5ebc7ddSthorpej #include <net/if_media.h>
74b5ebc7ddSthorpej
75b5ebc7ddSthorpej #include <dev/mii/mii.h>
76b5ebc7ddSthorpej #include <dev/mii/miivar.h>
77b5ebc7ddSthorpej #include <dev/mii/miidevs.h>
78b5ebc7ddSthorpej
79b5ebc7ddSthorpej #include <dev/mii/sqphyreg.h>
80b5ebc7ddSthorpej
817db0e577Sxtraeme static int sqphymatch(device_t, cfdata_t, void *);
827db0e577Sxtraeme static void sqphyattach(device_t, device_t, void *);
83b5ebc7ddSthorpej
847db0e577Sxtraeme CFATTACH_DECL_NEW(sqphy, sizeof(struct mii_softc),
85c9b3657cSthorpej sqphymatch, sqphyattach, mii_phy_detach, mii_phy_activate);
86b5ebc7ddSthorpej
871efb3da0Sthorpej static int sqphy_service(struct mii_softc *, struct mii_data *, int);
881efb3da0Sthorpej static void sqphy_status(struct mii_softc *);
891efb3da0Sthorpej static void sqphy_84220_reset(struct mii_softc *);
90b5ebc7ddSthorpej
911efb3da0Sthorpej static const struct mii_phy_funcs sqphy_funcs = {
9249014bf5Sthorpej sqphy_service, sqphy_status, mii_phy_reset,
9349014bf5Sthorpej };
9449014bf5Sthorpej
951efb3da0Sthorpej static const struct mii_phy_funcs sqphy_84220_funcs = {
96ce2ec9e6Sscw sqphy_service, sqphy_status, sqphy_84220_reset,
97ce2ec9e6Sscw };
98ce2ec9e6Sscw
991efb3da0Sthorpej static const struct mii_phydesc sqphys[] = {
1007b43da1bSchristos MII_PHY_DESC(SEEQ, 80220),
1017b43da1bSchristos MII_PHY_DESC(SEEQ, 80225),
1027b43da1bSchristos MII_PHY_DESC(SEEQ, 84220),
1037b43da1bSchristos MII_PHY_END,
104424f7a1eSthorpej };
105424f7a1eSthorpej
1061efb3da0Sthorpej static int
sqphymatch(device_t parent,cfdata_t match,void * aux)1077db0e577Sxtraeme sqphymatch(device_t parent, cfdata_t match, void *aux)
108b5ebc7ddSthorpej {
109b5ebc7ddSthorpej struct mii_attach_args *ma = aux;
110b5ebc7ddSthorpej
111424f7a1eSthorpej if (mii_phy_match(ma, sqphys) != NULL)
1128e65e831Smsaitoh return 10;
113b5ebc7ddSthorpej
1148e65e831Smsaitoh return 0;
115b5ebc7ddSthorpej }
116b5ebc7ddSthorpej
1171efb3da0Sthorpej static void
sqphyattach(device_t parent,device_t self,void * aux)1187db0e577Sxtraeme sqphyattach(device_t parent, device_t self, void *aux)
119b5ebc7ddSthorpej {
120838ee1e0Sthorpej struct mii_softc *sc = device_private(self);
121b5ebc7ddSthorpej struct mii_attach_args *ma = aux;
122b5ebc7ddSthorpej struct mii_data *mii = ma->mii_data;
123424f7a1eSthorpej const struct mii_phydesc *mpd;
124b5ebc7ddSthorpej
125424f7a1eSthorpej mpd = mii_phy_match(ma, sqphys);
126c31f87a5Sthorpej aprint_naive(": Media interface\n");
127c31f87a5Sthorpej aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
128b5ebc7ddSthorpej
1297db0e577Sxtraeme sc->mii_dev = self;
1302a17544cSthorpej sc->mii_inst = mii->mii_instance;
1312a17544cSthorpej sc->mii_phy = ma->mii_phyno;
1322a17544cSthorpej sc->mii_pdata = mii;
133b0178985Sthorpej sc->mii_flags = ma->mii_flags;
134b5ebc7ddSthorpej
135ce2ec9e6Sscw switch (MII_MODEL(ma->mii_id2)) {
136ce2ec9e6Sscw case MII_MODEL_SEEQ_84220:
137ce2ec9e6Sscw sc->mii_funcs = &sqphy_84220_funcs;
1388bc54e5bSmsaitoh aprint_normal_dev(self,
1398bc54e5bSmsaitoh "using Seeq 84220 isolate/reset hack\n");
140ce2ec9e6Sscw break;
141ce2ec9e6Sscw
142ce2ec9e6Sscw default:
143ce2ec9e6Sscw sc->mii_funcs = &sqphy_funcs;
144ce2ec9e6Sscw }
145ce2ec9e6Sscw
146*7a9a30c5Sthorpej mii_lock(mii);
147*7a9a30c5Sthorpej
14849014bf5Sthorpej PHY_RESET(sc);
149b5ebc7ddSthorpej
150a5cdd4b4Smsaitoh PHY_READ(sc, MII_BMSR, &sc->mii_capabilities);
151a5cdd4b4Smsaitoh sc->mii_capabilities &= ma->mii_capmask;
152509697f3Smsaitoh
153*7a9a30c5Sthorpej mii_unlock(mii);
154*7a9a30c5Sthorpej
15584dc99fdSthorpej mii_phy_add_media(sc);
156b5ebc7ddSthorpej }
157b5ebc7ddSthorpej
1581efb3da0Sthorpej static int
sqphy_service(struct mii_softc * sc,struct mii_data * mii,int cmd)15989893e42Sthorpej sqphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
160b5ebc7ddSthorpej {
161b5ebc7ddSthorpej struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
162a5cdd4b4Smsaitoh uint16_t reg;
163b5ebc7ddSthorpej
164*7a9a30c5Sthorpej KASSERT(mii_locked(mii));
165*7a9a30c5Sthorpej
166b5ebc7ddSthorpej switch (cmd) {
167b5ebc7ddSthorpej case MII_POLLSTAT:
1688e65e831Smsaitoh /* If we're not polling our PHY instance, just return. */
1692a17544cSthorpej if (IFM_INST(ife->ifm_media) != sc->mii_inst)
1708e65e831Smsaitoh return 0;
171b5ebc7ddSthorpej break;
172b5ebc7ddSthorpej
173b5ebc7ddSthorpej case MII_MEDIACHG:
174b5ebc7ddSthorpej /*
175b5ebc7ddSthorpej * If the media indicates a different PHY instance,
176b5ebc7ddSthorpej * isolate ourselves.
177b5ebc7ddSthorpej */
1782a17544cSthorpej if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
179a5cdd4b4Smsaitoh PHY_READ(sc, MII_BMCR, ®);
1802a17544cSthorpej PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
1818e65e831Smsaitoh return 0;
182b5ebc7ddSthorpej }
183b5ebc7ddSthorpej
1848e65e831Smsaitoh /* If the interface is not up, don't do anything. */
185b5ebc7ddSthorpej if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
186b5ebc7ddSthorpej break;
187b5ebc7ddSthorpej
1888fc600c3Sthorpej mii_phy_setmedia(sc);
189b5ebc7ddSthorpej break;
190b5ebc7ddSthorpej
191b5ebc7ddSthorpej case MII_TICK:
1928e65e831Smsaitoh /* If we're not currently selected, just return. */
1932a17544cSthorpej if (IFM_INST(ife->ifm_media) != sc->mii_inst)
1948e65e831Smsaitoh return 0;
195b5ebc7ddSthorpej
196ad61d101Sthorpej if (mii_phy_tick(sc) == EJUSTRETURN)
1978e65e831Smsaitoh return 0;
198b5ebc7ddSthorpej break;
199bca88a28Sthorpej
200bca88a28Sthorpej case MII_DOWN:
201bca88a28Sthorpej mii_phy_down(sc);
2028e65e831Smsaitoh return 0;
203b5ebc7ddSthorpej }
204b5ebc7ddSthorpej
205b5ebc7ddSthorpej /* Update the media status. */
2068923ca0bSthorpej mii_phy_status(sc);
207b5ebc7ddSthorpej
208b5ebc7ddSthorpej /* Callback if something changed. */
209ad61d101Sthorpej mii_phy_update(sc, cmd);
2108e65e831Smsaitoh return 0;
211b5ebc7ddSthorpej }
212b5ebc7ddSthorpej
2131efb3da0Sthorpej static void
sqphy_status(struct mii_softc * sc)21489893e42Sthorpej sqphy_status(struct mii_softc *sc)
215b5ebc7ddSthorpej {
2162a17544cSthorpej struct mii_data *mii = sc->mii_pdata;
2178fc600c3Sthorpej struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
218a5cdd4b4Smsaitoh uint16_t bmsr, bmcr, status;
219b5ebc7ddSthorpej
220*7a9a30c5Sthorpej KASSERT(mii_locked(mii));
221*7a9a30c5Sthorpej
222b5ebc7ddSthorpej mii->mii_media_status = IFM_AVALID;
223b5ebc7ddSthorpej mii->mii_media_active = IFM_ETHER;
224b5ebc7ddSthorpej
225a5cdd4b4Smsaitoh PHY_READ(sc, MII_BMSR, &bmsr);
226a5cdd4b4Smsaitoh PHY_READ(sc, MII_BMSR, &bmsr);
227b5ebc7ddSthorpej if (bmsr & BMSR_LINK)
228b5ebc7ddSthorpej mii->mii_media_status |= IFM_ACTIVE;
229b5ebc7ddSthorpej
230a5cdd4b4Smsaitoh PHY_READ(sc, MII_BMCR, &bmcr);
231b5ebc7ddSthorpej if (bmcr & BMCR_ISO) {
232b5ebc7ddSthorpej mii->mii_media_active |= IFM_NONE;
233b5ebc7ddSthorpej mii->mii_media_status = 0;
234b5ebc7ddSthorpej return;
235b5ebc7ddSthorpej }
236b5ebc7ddSthorpej
237b5ebc7ddSthorpej if (bmcr & BMCR_LOOP)
238b5ebc7ddSthorpej mii->mii_media_active |= IFM_LOOP;
239b5ebc7ddSthorpej
240aabf2a08Sthorpej if (bmcr & BMCR_AUTOEN) {
241aabf2a08Sthorpej if ((bmsr & BMSR_ACOMP) == 0) {
242b5ebc7ddSthorpej /* Erg, still trying, I guess... */
243b5ebc7ddSthorpej mii->mii_media_active |= IFM_NONE;
244b5ebc7ddSthorpej return;
245b5ebc7ddSthorpej }
2467914a89bSthorpej /*
2478e65e831Smsaitoh * Note: don't get fancy here -- the 80225 only supports the
2488e65e831Smsaitoh * SPD_DET and DPLX_DET bits in the STATUS register.
2497914a89bSthorpej */
250a5cdd4b4Smsaitoh PHY_READ(sc, MII_SQPHY_STATUS, &status);
251b5ebc7ddSthorpej if (status & STATUS_SPD_DET)
252b5ebc7ddSthorpej mii->mii_media_active |= IFM_100_TX;
253b5ebc7ddSthorpej else
254b5ebc7ddSthorpej mii->mii_media_active |= IFM_10_T;
255b5ebc7ddSthorpej if (status & STATUS_DPLX_DET)
256b5ebc7ddSthorpej mii->mii_media_active |= IFM_FDX;
257b211437bSmsaitoh else
258b211437bSmsaitoh mii->mii_media_active |= IFM_HDX;
2592b695df1Sthorpej } else
2608fc600c3Sthorpej mii->mii_media_active = ife->ifm_media;
261b5ebc7ddSthorpej }
262ce2ec9e6Sscw
2631efb3da0Sthorpej static void
sqphy_84220_reset(struct mii_softc * sc)264ce2ec9e6Sscw sqphy_84220_reset(struct mii_softc *sc)
265ce2ec9e6Sscw {
266a5cdd4b4Smsaitoh uint16_t reg;
267ce2ec9e6Sscw
268*7a9a30c5Sthorpej KASSERT(mii_locked(sc->mii_pdata));
269*7a9a30c5Sthorpej
270ce2ec9e6Sscw mii_phy_reset(sc);
271ce2ec9e6Sscw
272ce2ec9e6Sscw /*
273ce2ec9e6Sscw * This PHY sometimes insists on coming out of reset isolated,
274ce2ec9e6Sscw * even when the MDA[0-3] pins are pulled high (to indicate
275ce2ec9e6Sscw * PHY address 0), contrary to the device's datasheet.
276ce2ec9e6Sscw *
277ce2ec9e6Sscw * Morever, simply clearing BMCR_ISO here isn't enough; the
278ce2ec9e6Sscw * change won't stick until about 30mS *after* the PHY has
279ce2ec9e6Sscw * been reset.
280ce2ec9e6Sscw *
281ce2ec9e6Sscw * This sucks.
282ce2ec9e6Sscw */
283a5cdd4b4Smsaitoh if ((sc->mii_inst != 0) && ((sc->mii_flags & MIIF_NOISOLATE) == 0))
284a5cdd4b4Smsaitoh return;
285ce2ec9e6Sscw
286a5cdd4b4Smsaitoh PHY_READ(sc, MII_BMCR, ®);
287a5cdd4b4Smsaitoh while ((reg & BMCR_ISO) != 0) {
288cbc5db83Sscw delay(35000);
289ce2ec9e6Sscw PHY_WRITE(sc, MII_BMCR, reg & ~BMCR_ISO);
290cbc5db83Sscw delay(35000);
291a5cdd4b4Smsaitoh PHY_READ(sc, MII_BMCR, ®);
292ce2ec9e6Sscw }
293ce2ec9e6Sscw }
294