1 /* $OpenBSD: acphy.c,v 1.10 2022/04/06 18:59:29 naddy Exp $ */
2 /* $NetBSD: acphy.c,v 1.13 2003/04/29 01:49:33 thorpej Exp $ */
3
4 /*
5 * Copyright 2001 Wasabi Systems, Inc.
6 * All rights reserved.
7 *
8 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed for the NetBSD Project by
21 * Wasabi Systems, Inc.
22 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
23 * or promote products derived from this software without specific prior
24 * written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Driver for the Altima AC101 PHY.
41 */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/device.h>
46 #include <sys/socket.h>
47 #include <sys/errno.h>
48
49 #include <net/if.h>
50 #include <net/if_var.h>
51 #include <net/if_media.h>
52
53 #include <dev/mii/mii.h>
54 #include <dev/mii/miivar.h>
55 #include <dev/mii/miidevs.h>
56
57 #include <dev/mii/acphyreg.h>
58
59 int acphymatch(struct device *, void *, void *);
60 void acphyattach(struct device *, struct device *, void *);
61
62 const struct cfattach acphy_ca = {
63 sizeof(struct mii_softc), acphymatch, acphyattach, mii_phy_detach
64 };
65
66 struct cfdriver acphy_cd = {
67 NULL, "acphy", DV_DULL
68 };
69
70 int acphy_service(struct mii_softc *, struct mii_data *, int);
71 void acphy_status(struct mii_softc *);
72
73 const struct mii_phy_funcs acphy_funcs = {
74 acphy_service, acphy_status, mii_phy_reset,
75 };
76
77 static const struct mii_phydesc acphys[] = {
78 { MII_OUI_xxALTIMA, MII_MODEL_xxALTIMA_AC101,
79 MII_STR_xxALTIMA_AC101 },
80 { MII_OUI_xxALTIMA, MII_MODEL_xxALTIMA_AC101L,
81 MII_STR_xxALTIMA_AC101L },
82 { MII_OUI_xxALTIMA, MII_MODEL_xxALTIMA_AC_UNKNOWN,
83 MII_STR_xxALTIMA_AC_UNKNOWN },
84
85 { 0, 0,
86 NULL },
87 };
88
89 int
acphymatch(struct device * parent,void * match,void * aux)90 acphymatch(struct device *parent, void *match, void *aux)
91 {
92 struct mii_attach_args *ma = aux;
93
94 if (mii_phy_match(ma, acphys) != NULL)
95 return (10);
96
97 return (0);
98 }
99
100 void
acphyattach(struct device * parent,struct device * self,void * aux)101 acphyattach(struct device *parent, struct device *self, void *aux)
102 {
103 struct mii_softc *sc = (struct mii_softc *)self;
104 struct mii_attach_args *ma = aux;
105 struct mii_data *mii = ma->mii_data;
106 const struct mii_phydesc *mpd;
107
108 mpd = mii_phy_match(ma, acphys);
109 printf(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
110
111 sc->mii_inst = mii->mii_instance;
112 sc->mii_phy = ma->mii_phyno;
113 sc->mii_funcs = &acphy_funcs;
114 sc->mii_pdata = mii;
115 sc->mii_flags = ma->mii_flags;
116 sc->mii_anegticks = MII_ANEGTICKS;
117
118 PHY_RESET(sc);
119
120 /*
121 * XXX Check MCR_FX_SEL to set MIIF_HAVE_FIBER?
122 */
123
124 sc->mii_capabilities =
125 PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
126
127 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
128 if (sc->mii_flags & MIIF_HAVEFIBER) {
129 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_FX, 0, sc->mii_inst),
130 MII_MEDIA_100_TX);
131 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_FX, IFM_FDX, sc->mii_inst),
132 MII_MEDIA_100_TX);
133 }
134 #undef ADD
135
136 if (sc->mii_capabilities & BMSR_MEDIAMASK)
137 mii_phy_add_media(sc);
138 }
139
140 int
acphy_service(struct mii_softc * sc,struct mii_data * mii,int cmd)141 acphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
142 {
143 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
144 int reg;
145
146 switch (cmd) {
147 case MII_POLLSTAT:
148 /*
149 * If we're not polling our PHY instance, just return.
150 */
151 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
152 return (0);
153 break;
154
155 case MII_MEDIACHG:
156 /*
157 * If the media indicates a different PHY instance,
158 * isolate ourselves.
159 */
160 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
161 reg = PHY_READ(sc, MII_BMCR);
162 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
163 return (0);
164 }
165
166 /*
167 * If the interface is not up, don't do anything.
168 */
169 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
170 break;
171
172 mii_phy_setmedia(sc);
173 break;
174
175 case MII_TICK:
176 /*
177 * If we're not currently selected, just return.
178 */
179 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
180 return (0);
181
182 if (mii_phy_tick(sc) == EJUSTRETURN)
183 return (0);
184 break;
185
186 case MII_DOWN:
187 mii_phy_down(sc);
188 return (0);
189 }
190
191 /* Update the media status. */
192 mii_phy_status(sc);
193
194 /* Callback if something changed. */
195 mii_phy_update(sc, cmd);
196 return (0);
197 }
198
199 void
acphy_status(struct mii_softc * sc)200 acphy_status(struct mii_softc *sc)
201 {
202 struct mii_data *mii = sc->mii_pdata;
203 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
204 int bmsr, bmcr, dr;
205
206 mii->mii_media_status = IFM_AVALID;
207 mii->mii_media_active = IFM_ETHER;
208
209 bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
210 dr = PHY_READ(sc, MII_ACPHY_DR);
211
212 if (bmsr & BMSR_LINK)
213 mii->mii_media_status |= IFM_ACTIVE;
214
215 bmcr = PHY_READ(sc, MII_BMCR);
216 if (bmcr & BMCR_ISO) {
217 mii->mii_media_active |= IFM_NONE;
218 mii->mii_media_status = 0;
219 return;
220 }
221
222 if (bmcr & BMCR_LOOP)
223 mii->mii_media_active |= IFM_LOOP;
224
225 if (bmcr & BMCR_AUTOEN) {
226 /*
227 * The media status bits are only valid if autonegotiation
228 * has completed (or it's disabled).
229 */
230 if ((bmsr & BMSR_ACOMP) == 0) {
231 /* Erg, still trying, I guess... */
232 mii->mii_media_active |= IFM_NONE;
233 return;
234 }
235
236 if (dr & DR_SPEED)
237 mii->mii_media_active |= IFM_100_TX;
238 else
239 mii->mii_media_active |= IFM_10_T;
240
241 if (dr & DR_DPLX)
242 mii->mii_media_active |= IFM_FDX;
243 else
244 mii->mii_media_active |= IFM_HDX;
245 } else
246 mii->mii_media_active = ife->ifm_media;
247 }
248