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