1*7a9a30c5Sthorpej /* $NetBSD: acphy.c,v 1.30 2020/03/15 23:04:50 thorpej Exp $ */
2b6aa6744Sthorpej
3b6aa6744Sthorpej /*
4b6aa6744Sthorpej * Copyright 2001 Wasabi Systems, Inc.
5b6aa6744Sthorpej * All rights reserved.
6b6aa6744Sthorpej *
7b6aa6744Sthorpej * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8b6aa6744Sthorpej *
9b6aa6744Sthorpej * Redistribution and use in source and binary forms, with or without
10b6aa6744Sthorpej * modification, are permitted provided that the following conditions
11b6aa6744Sthorpej * are met:
12b6aa6744Sthorpej * 1. Redistributions of source code must retain the above copyright
13b6aa6744Sthorpej * notice, this list of conditions and the following disclaimer.
14b6aa6744Sthorpej * 2. Redistributions in binary form must reproduce the above copyright
15b6aa6744Sthorpej * notice, this list of conditions and the following disclaimer in the
16b6aa6744Sthorpej * documentation and/or other materials provided with the distribution.
17b6aa6744Sthorpej * 3. All advertising materials mentioning features or use of this software
18b6aa6744Sthorpej * must display the following acknowledgement:
19b6aa6744Sthorpej * This product includes software developed for the NetBSD Project by
20b6aa6744Sthorpej * Wasabi Systems, Inc.
21b6aa6744Sthorpej * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22b6aa6744Sthorpej * or promote products derived from this software without specific prior
23b6aa6744Sthorpej * written permission.
24b6aa6744Sthorpej *
25b6aa6744Sthorpej * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26b6aa6744Sthorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27b6aa6744Sthorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28b6aa6744Sthorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29b6aa6744Sthorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30b6aa6744Sthorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31b6aa6744Sthorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32b6aa6744Sthorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33b6aa6744Sthorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34b6aa6744Sthorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35b6aa6744Sthorpej * POSSIBILITY OF SUCH DAMAGE.
36b6aa6744Sthorpej */
37b6aa6744Sthorpej
38b6aa6744Sthorpej /*
39b6aa6744Sthorpej * Driver for the Altima AC101 PHY.
40b6aa6744Sthorpej */
41b6aa6744Sthorpej
428b7bb912Slukem #include <sys/cdefs.h>
43*7a9a30c5Sthorpej __KERNEL_RCSID(0, "$NetBSD: acphy.c,v 1.30 2020/03/15 23:04:50 thorpej Exp $");
448b7bb912Slukem
45b6aa6744Sthorpej #include <sys/param.h>
46b6aa6744Sthorpej #include <sys/systm.h>
47b6aa6744Sthorpej #include <sys/kernel.h>
48b6aa6744Sthorpej #include <sys/device.h>
49b6aa6744Sthorpej #include <sys/socket.h>
50b6aa6744Sthorpej #include <sys/errno.h>
51b6aa6744Sthorpej
52b6aa6744Sthorpej #include <net/if.h>
53b6aa6744Sthorpej #include <net/if_media.h>
54b6aa6744Sthorpej
55b6aa6744Sthorpej #include <dev/mii/mii.h>
56b6aa6744Sthorpej #include <dev/mii/miivar.h>
57b6aa6744Sthorpej #include <dev/mii/miidevs.h>
58b6aa6744Sthorpej
59b6aa6744Sthorpej #include <dev/mii/acphyreg.h>
60b6aa6744Sthorpej
617db0e577Sxtraeme static int acphymatch(device_t, cfdata_t, void *);
627db0e577Sxtraeme static void acphyattach(device_t, device_t, void *);
63b6aa6744Sthorpej
647db0e577Sxtraeme CFATTACH_DECL_NEW(acphy, sizeof(struct mii_softc),
65c9b3657cSthorpej acphymatch, acphyattach, mii_phy_detach, mii_phy_activate);
66b6aa6744Sthorpej
671efb3da0Sthorpej static int acphy_service(struct mii_softc *, struct mii_data *, int);
681efb3da0Sthorpej static void acphy_status(struct mii_softc *);
69b6aa6744Sthorpej
701efb3da0Sthorpej static const struct mii_phy_funcs acphy_funcs = {
71b6aa6744Sthorpej acphy_service, acphy_status, mii_phy_reset,
72b6aa6744Sthorpej };
73b6aa6744Sthorpej
741efb3da0Sthorpej static const struct mii_phydesc acphys[] = {
757b43da1bSchristos MII_PHY_DESC(ALTIMA, AC101),
767b43da1bSchristos MII_PHY_DESC(ALTIMA, AC101L),
777b43da1bSchristos MII_PHY_DESC(ALTIMA, Am79C874),
787b43da1bSchristos MII_PHY_DESC(ALTIMA, Am79C875),
79385b05e2Saugustss /* XXX This is reported to work, but it's not from any data sheet. */
807b43da1bSchristos MII_PHY_DESC(ALTIMA, ACXXX),
817b43da1bSchristos MII_PHY_END,
82b6aa6744Sthorpej };
83b6aa6744Sthorpej
841efb3da0Sthorpej static int
acphymatch(device_t parent,cfdata_t match,void * aux)857db0e577Sxtraeme acphymatch(device_t parent, cfdata_t match, void *aux)
86b6aa6744Sthorpej {
87b6aa6744Sthorpej struct mii_attach_args *ma = aux;
88b6aa6744Sthorpej
89b6aa6744Sthorpej if (mii_phy_match(ma, acphys) != NULL)
908e65e831Smsaitoh return 10;
91b6aa6744Sthorpej
928e65e831Smsaitoh return 0;
93b6aa6744Sthorpej }
94b6aa6744Sthorpej
951efb3da0Sthorpej static void
acphyattach(device_t parent,device_t self,void * aux)967db0e577Sxtraeme acphyattach(device_t parent, device_t self, void *aux)
97b6aa6744Sthorpej {
98838ee1e0Sthorpej struct mii_softc *sc = device_private(self);
99b6aa6744Sthorpej struct mii_attach_args *ma = aux;
100b6aa6744Sthorpej struct mii_data *mii = ma->mii_data;
101b6aa6744Sthorpej const struct mii_phydesc *mpd;
102b6aa6744Sthorpej
103b6aa6744Sthorpej mpd = mii_phy_match(ma, acphys);
104c31f87a5Sthorpej aprint_naive(": Media interface\n");
105c31f87a5Sthorpej aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
106b6aa6744Sthorpej
1077db0e577Sxtraeme sc->mii_dev = self;
108b6aa6744Sthorpej sc->mii_inst = mii->mii_instance;
109b6aa6744Sthorpej sc->mii_phy = ma->mii_phyno;
110b6aa6744Sthorpej sc->mii_funcs = &acphy_funcs;
111b6aa6744Sthorpej sc->mii_pdata = mii;
112b0178985Sthorpej sc->mii_flags = ma->mii_flags;
113b6aa6744Sthorpej
114*7a9a30c5Sthorpej mii_lock(mii);
115*7a9a30c5Sthorpej
116b6aa6744Sthorpej PHY_RESET(sc);
117b6aa6744Sthorpej
1188e65e831Smsaitoh /* XXX Check MCR_FX_SEL to set MIIF_HAVE_FIBER? */
119b6aa6744Sthorpej
120a5cdd4b4Smsaitoh PHY_READ(sc, MII_BMSR, &sc->mii_capabilities);
121a5cdd4b4Smsaitoh sc->mii_capabilities &= ma->mii_capmask;
122*7a9a30c5Sthorpej
123*7a9a30c5Sthorpej mii_unlock(mii);
124ee04df9aSmatt
125ee04df9aSmatt #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
126ee04df9aSmatt if (sc->mii_flags & MIIF_HAVEFIBER) {
127*7a9a30c5Sthorpej aprint_normal_dev(sc->mii_dev, "");
128*7a9a30c5Sthorpej mii_lock(mii);
129509697f3Smsaitoh sc->mii_anegticks = MII_ANEGTICKS;
130*7a9a30c5Sthorpej mii_unlock(mii);
131ee04df9aSmatt ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_FX, 0, sc->mii_inst),
132ee04df9aSmatt MII_MEDIA_100_TX);
133c31f87a5Sthorpej aprint_normal("100baseFX, ");
134ee04df9aSmatt ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_FX, IFM_FDX, sc->mii_inst),
135ee04df9aSmatt MII_MEDIA_100_TX);
136509697f3Smsaitoh aprint_normal("100baseFX-FDX\n");
137ee04df9aSmatt }
138ee04df9aSmatt #undef ADD
139ee04df9aSmatt
140b6aa6744Sthorpej mii_phy_add_media(sc);
141b6aa6744Sthorpej }
142b6aa6744Sthorpej
1431efb3da0Sthorpej static int
acphy_service(struct mii_softc * sc,struct mii_data * mii,int cmd)14489893e42Sthorpej acphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
145b6aa6744Sthorpej {
146b6aa6744Sthorpej struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
147a5cdd4b4Smsaitoh uint16_t reg;
148b6aa6744Sthorpej
149*7a9a30c5Sthorpej KASSERT(mii_locked(mii));
150*7a9a30c5Sthorpej
151b6aa6744Sthorpej switch (cmd) {
152b6aa6744Sthorpej case MII_POLLSTAT:
1538e65e831Smsaitoh /* If we're not polling our PHY instance, just return. */
154b6aa6744Sthorpej if (IFM_INST(ife->ifm_media) != sc->mii_inst)
1558e65e831Smsaitoh return 0;
156b6aa6744Sthorpej break;
157b6aa6744Sthorpej
158b6aa6744Sthorpej case MII_MEDIACHG:
159b6aa6744Sthorpej /*
160b6aa6744Sthorpej * If the media indicates a different PHY instance,
161b6aa6744Sthorpej * isolate ourselves.
162b6aa6744Sthorpej */
163b6aa6744Sthorpej if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
164a5cdd4b4Smsaitoh PHY_READ(sc, MII_BMCR, ®);
165b6aa6744Sthorpej PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
1668e65e831Smsaitoh return 0;
167b6aa6744Sthorpej }
168b6aa6744Sthorpej
1698e65e831Smsaitoh /* If the interface is not up, don't do anything. */
170b6aa6744Sthorpej if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
171b6aa6744Sthorpej break;
172b6aa6744Sthorpej
173b6aa6744Sthorpej mii_phy_setmedia(sc);
174b6aa6744Sthorpej break;
175b6aa6744Sthorpej
176b6aa6744Sthorpej case MII_TICK:
1778e65e831Smsaitoh /* If we're not currently selected, just return. */
178b6aa6744Sthorpej if (IFM_INST(ife->ifm_media) != sc->mii_inst)
1798e65e831Smsaitoh return 0;
180b6aa6744Sthorpej
181b6aa6744Sthorpej if (mii_phy_tick(sc) == EJUSTRETURN)
1828e65e831Smsaitoh return 0;
183b6aa6744Sthorpej break;
184b6aa6744Sthorpej
185b6aa6744Sthorpej case MII_DOWN:
186b6aa6744Sthorpej mii_phy_down(sc);
1878e65e831Smsaitoh return 0;
188b6aa6744Sthorpej }
189b6aa6744Sthorpej
190b6aa6744Sthorpej /* Update the media status. */
191b6aa6744Sthorpej mii_phy_status(sc);
192b6aa6744Sthorpej
193b6aa6744Sthorpej /* Callback if something changed. */
194b6aa6744Sthorpej mii_phy_update(sc, cmd);
1958e65e831Smsaitoh return 0;
196b6aa6744Sthorpej }
197b6aa6744Sthorpej
1981efb3da0Sthorpej static void
acphy_status(struct mii_softc * sc)19989893e42Sthorpej acphy_status(struct mii_softc *sc)
200b6aa6744Sthorpej {
201b6aa6744Sthorpej struct mii_data *mii = sc->mii_pdata;
202b6aa6744Sthorpej struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
203a5cdd4b4Smsaitoh uint16_t bmsr, bmcr, dr;
204b6aa6744Sthorpej
205*7a9a30c5Sthorpej KASSERT(mii_locked(mii));
206*7a9a30c5Sthorpej
207b6aa6744Sthorpej mii->mii_media_status = IFM_AVALID;
208b6aa6744Sthorpej mii->mii_media_active = IFM_ETHER;
209b6aa6744Sthorpej
210a5cdd4b4Smsaitoh PHY_READ(sc, MII_BMSR, &bmsr);
211a5cdd4b4Smsaitoh PHY_READ(sc, MII_BMSR, &bmsr);
212a5cdd4b4Smsaitoh PHY_READ(sc, MII_ACPHY_DR, &dr);
213b6aa6744Sthorpej
214b6aa6744Sthorpej if (bmsr & BMSR_LINK)
215b6aa6744Sthorpej mii->mii_media_status |= IFM_ACTIVE;
216b6aa6744Sthorpej
217a5cdd4b4Smsaitoh PHY_READ(sc, MII_BMCR, &bmcr);
218b6aa6744Sthorpej if (bmcr & BMCR_ISO) {
219b6aa6744Sthorpej mii->mii_media_active |= IFM_NONE;
220b6aa6744Sthorpej mii->mii_media_status = 0;
221b6aa6744Sthorpej return;
222b6aa6744Sthorpej }
223b6aa6744Sthorpej
224b6aa6744Sthorpej if (bmcr & BMCR_LOOP)
225b6aa6744Sthorpej mii->mii_media_active |= IFM_LOOP;
226b6aa6744Sthorpej
227b6aa6744Sthorpej if (bmcr & BMCR_AUTOEN) {
228b6aa6744Sthorpej /*
229b9abd324Swiz * The media status bits are only valid if autonegotiation
230b6aa6744Sthorpej * has completed (or it's disabled).
231b6aa6744Sthorpej */
232b6aa6744Sthorpej if ((bmsr & BMSR_ACOMP) == 0) {
233b6aa6744Sthorpej /* Erg, still trying, I guess... */
234b6aa6744Sthorpej mii->mii_media_active |= IFM_NONE;
235b6aa6744Sthorpej return;
236b6aa6744Sthorpej }
237b6aa6744Sthorpej
238b6aa6744Sthorpej if (dr & DR_SPEED)
239b6aa6744Sthorpej mii->mii_media_active |= IFM_100_TX;
240b6aa6744Sthorpej else
241b6aa6744Sthorpej mii->mii_media_active |= IFM_10_T;
242b211437bSmsaitoh
243b6aa6744Sthorpej if (dr & DR_DPLX)
244b6aa6744Sthorpej mii->mii_media_active |= IFM_FDX;
245b211437bSmsaitoh else
246b211437bSmsaitoh mii->mii_media_active |= IFM_HDX;
247b6aa6744Sthorpej } else
248b6aa6744Sthorpej mii->mii_media_active = ife->ifm_media;
249b6aa6744Sthorpej }
250