1 /*	$OpenBSD: bmtphy.c,v 1.14 2005/05/25 20:05:44 brad Exp $	*/
2 /*	$NetBSD: bmtphy.c,v 1.17 2005/01/17 13:17:45 scw Exp $	*/
3 
4 /*-
5  * Copyright (c) 2001 Theo de Raadt
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * Driver for Broadcom BCM5201/BCM5202 "Mini-Theta" PHYs.  This also
31  * drives the PHY on the 3Com 3c905C.  The 3c905C's PHY is described in
32  * the 3c905C data sheet.
33  */
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/device.h>
39 #include <sys/socket.h>
40 #include <sys/errno.h>
41 
42 #include <net/if.h>
43 #include <net/if_media.h>
44 
45 #include <dev/mii/mii.h>
46 #include <dev/mii/miivar.h>
47 #include <dev/mii/miidevs.h>
48 
49 #include <dev/mii/bmtphyreg.h>
50 
51 int	bmtphymatch(struct device *, void *, void *);
52 void	bmtphyattach(struct device *, struct device *, void *);
53 
54 struct cfattach bmtphy_ca = {
55 	sizeof(struct mii_softc), bmtphymatch, bmtphyattach, mii_phy_detach,
56 	    mii_phy_activate
57 };
58 
59 struct cfdriver bmtphy_cd = {
60 	NULL, "bmtphy", DV_DULL
61 };
62 
63 int	bmtphy_service(struct mii_softc *, struct mii_data *, int);
64 void	bmtphy_status(struct mii_softc *);
65 
66 const struct mii_phy_funcs bmtphy_funcs = {
67 	bmtphy_service, bmtphy_status, mii_phy_reset,
68 };
69 
70 static const struct mii_phydesc bmtphys[] = {
71 	{ MII_OUI_BROADCOM,		MII_MODEL_BROADCOM_3C905B,
72 	  MII_STR_BROADCOM_3C905B },
73 	{ MII_OUI_BROADCOM,		MII_MODEL_BROADCOM_3C905C,
74 	  MII_STR_BROADCOM_3C905C },
75 	{ MII_OUI_BROADCOM,		MII_MODEL_BROADCOM_BCM4401,
76 	  MII_STR_BROADCOM_BCM4401 },
77 	{ MII_OUI_BROADCOM,		MII_MODEL_BROADCOM_BCM5201,
78 	  MII_STR_BROADCOM_BCM5201 },
79 	{ MII_OUI_BROADCOM,		MII_MODEL_BROADCOM_BCM5214,
80 	  MII_STR_BROADCOM_BCM5214 },
81 	{ MII_OUI_BROADCOM,		MII_MODEL_BROADCOM_BCM5220,
82 	  MII_STR_BROADCOM_BCM5220 },
83 	{ MII_OUI_BROADCOM,		MII_MODEL_BROADCOM_BCM5221,
84 	  MII_STR_BROADCOM_BCM5221 },
85 	{ MII_OUI_BROADCOM,		MII_MODEL_BROADCOM_BCM5222,
86 	  MII_STR_BROADCOM_BCM5222 },
87 
88 	{ 0,				0,
89 	  NULL },
90 };
91 
92 int
bmtphymatch(struct device * parent,void * match,void * aux)93 bmtphymatch(struct device *parent, void *match, void *aux)
94 {
95 	struct mii_attach_args *ma = aux;
96 
97 	if (mii_phy_match(ma, bmtphys) != NULL)
98 		return (10);
99 
100 	return (0);
101 }
102 
103 void
bmtphyattach(struct device * parent,struct device * self,void * aux)104 bmtphyattach(struct device *parent, struct device *self, void *aux)
105 {
106 	struct mii_softc *sc = (struct mii_softc *)self;
107 	struct mii_attach_args *ma = aux;
108 	struct mii_data *mii = ma->mii_data;
109 	const struct mii_phydesc *mpd;
110 
111 	mpd = mii_phy_match(ma, bmtphys);
112 	printf(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
113 
114 	sc->mii_inst = mii->mii_instance;
115 	sc->mii_phy = ma->mii_phyno;
116 	sc->mii_funcs = &bmtphy_funcs;
117 	sc->mii_pdata = mii;
118 	sc->mii_flags = ma->mii_flags;
119 	sc->mii_anegticks = MII_ANEGTICKS;
120 
121 	PHY_RESET(sc);
122 
123 	/*
124 	 * XXX Check AUX_STS_FX_MODE to set MIIF_HAVE_FIBER?
125 	 */
126 
127 	sc->mii_capabilities =
128 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
129 	if (sc->mii_capabilities & BMSR_MEDIAMASK)
130 		mii_phy_add_media(sc);
131 }
132 
133 int
bmtphy_service(struct mii_softc * sc,struct mii_data * mii,int cmd)134 bmtphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
135 {
136 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
137 	int reg;
138 
139 	if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0)
140 		return (ENXIO);
141 
142 	switch (cmd) {
143 	case MII_POLLSTAT:
144 		/*
145 		 * If we're not polling our PHY instance, just return.
146 		 */
147 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
148 			return (0);
149 		break;
150 
151 	case MII_MEDIACHG:
152 		/*
153 		 * If the media indicates a different PHY instance,
154 		 * isolate ourselves.
155 		 */
156 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
157 			reg = PHY_READ(sc, MII_BMCR);
158 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
159 			return (0);
160 		}
161 
162 		/*
163 		 * If the interface is not up, don't do anything.
164 		 */
165 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
166 			break;
167 
168 		mii_phy_setmedia(sc);
169 		break;
170 
171 	case MII_TICK:
172 		/*
173 		 * If we're not currently selected, just return.
174 		 */
175 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
176 			return (0);
177 
178 		if (mii_phy_tick(sc) == EJUSTRETURN)
179 			return (0);
180 		break;
181 
182 	case MII_DOWN:
183 		mii_phy_down(sc);
184 		return (0);
185 	}
186 
187 	/* Update the media status. */
188 	mii_phy_status(sc);
189 
190 	/* Callback if something changed. */
191 	mii_phy_update(sc, cmd);
192 	return (0);
193 }
194 
195 void
bmtphy_status(struct mii_softc * sc)196 bmtphy_status(struct mii_softc *sc)
197 {
198 	struct mii_data *mii = sc->mii_pdata;
199 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
200 	int bmsr, bmcr, aux_csr;
201 
202 	mii->mii_media_status = IFM_AVALID;
203 	mii->mii_media_active = IFM_ETHER;
204 
205 	bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
206 	if (bmsr & BMSR_LINK)
207 		mii->mii_media_status |= IFM_ACTIVE;
208 
209 	bmcr = PHY_READ(sc, MII_BMCR);
210 	if (bmcr & BMCR_ISO) {
211 		mii->mii_media_active |= IFM_NONE;
212 		mii->mii_media_status = 0;
213 		return;
214 	}
215 
216 	if (bmcr & BMCR_LOOP)
217 		mii->mii_media_active |= IFM_LOOP;
218 
219 	if (bmcr & BMCR_AUTOEN) {
220 		/*
221 		 * The later are only valid if autonegotiation
222 		 * has completed (or it's disabled).
223 		 */
224 		if ((bmsr & BMSR_ACOMP) == 0) {
225 			/* Erg, still trying, I guess... */
226 			mii->mii_media_active |= IFM_NONE;
227 			return;
228 		}
229 
230 		aux_csr = PHY_READ(sc, MII_BMTPHY_AUX_CSR);
231 		if (aux_csr & AUX_CSR_SPEED)
232 			mii->mii_media_active |= IFM_100_TX;
233 		else
234 			mii->mii_media_active |= IFM_10_T;
235 		if (aux_csr & AUX_CSR_FDX)
236 			mii->mii_media_active |= IFM_FDX;
237 
238 	} else
239 		mii->mii_media_active = ife->ifm_media;
240 }
241