xref: /freebsd/sys/dev/mii/ip1000phy.c (revision 34e4a32d)
1 /*-
2  * Copyright (c) 2006, Pyun YongHyeon <yongari@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 /*
33  * Driver for the IC Plus IP1000A/IP1001 10/100/1000 PHY.
34  */
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/socket.h>
41 #include <sys/bus.h>
42 
43 #include <net/if.h>
44 #include <net/if_media.h>
45 
46 #include <dev/mii/mii.h>
47 #include <dev/mii/miivar.h>
48 #include "miidevs.h"
49 
50 #include <dev/mii/ip1000phyreg.h>
51 
52 #include "miibus_if.h"
53 
54 #include <machine/bus.h>
55 #include <dev/stge/if_stgereg.h>
56 
57 static int ip1000phy_probe(device_t);
58 static int ip1000phy_attach(device_t);
59 
60 struct ip1000phy_softc {
61 	struct mii_softc mii_sc;
62 	int model;
63 	int revision;
64 };
65 
66 static device_method_t ip1000phy_methods[] = {
67 	/* device interface */
68 	DEVMETHOD(device_probe,		ip1000phy_probe),
69 	DEVMETHOD(device_attach,	ip1000phy_attach),
70 	DEVMETHOD(device_detach,	mii_phy_detach),
71 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
72 	{ 0, 0 }
73 };
74 
75 static devclass_t ip1000phy_devclass;
76 static driver_t ip1000phy_driver = {
77 	"ip1000phy",
78 	ip1000phy_methods,
79 	sizeof (struct mii_softc)
80 };
81 
82 DRIVER_MODULE(ip1000phy, miibus, ip1000phy_driver, ip1000phy_devclass, 0, 0);
83 
84 static int	ip1000phy_service(struct mii_softc *, struct mii_data *, int);
85 static void	ip1000phy_status(struct mii_softc *);
86 static void	ip1000phy_reset(struct mii_softc *);
87 static int	ip1000phy_mii_phy_auto(struct mii_softc *);
88 
89 static const struct mii_phydesc ip1000phys[] = {
90 	MII_PHY_DESC(ICPLUS, IP1000A),
91 	MII_PHY_DESC(ICPLUS, IP1001),
92 	MII_PHY_END
93 };
94 
95 static int
96 ip1000phy_probe(device_t dev)
97 {
98 
99 	return (mii_phy_dev_probe(dev, ip1000phys, BUS_PROBE_DEFAULT));
100 }
101 
102 static int
103 ip1000phy_attach(device_t dev)
104 {
105 	struct ip1000phy_softc *isc;
106 	struct mii_softc *sc;
107 	struct mii_attach_args *ma;
108 	struct mii_data *mii;
109 
110 	isc = device_get_softc(dev);
111 	sc = &isc->mii_sc;
112 	ma = device_get_ivars(dev);
113 	sc->mii_dev = device_get_parent(dev);
114 	mii = device_get_softc(sc->mii_dev);
115 	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
116 
117 	sc->mii_inst = mii->mii_instance;
118 	sc->mii_phy = ma->mii_phyno;
119 	sc->mii_service = ip1000phy_service;
120 	sc->mii_pdata = mii;
121 	sc->mii_flags |= MIIF_NOISOLATE;
122 
123 	mii->mii_instance++;
124 
125 	isc->model = MII_MODEL(ma->mii_id2);
126 	isc->revision = MII_REV(ma->mii_id2);
127 
128 	sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
129 	if (sc->mii_capabilities & BMSR_EXTSTAT)
130 		sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
131 	device_printf(dev, " ");
132 
133 	ip1000phy_reset(sc);
134 	mii_phy_add_media(sc);
135 	printf("\n");
136 
137 	MIIBUS_MEDIAINIT(sc->mii_dev);
138 	return(0);
139 }
140 
141 static int
142 ip1000phy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
143 {
144 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
145 	uint32_t gig, reg, speed;
146 
147 	switch (cmd) {
148 	case MII_POLLSTAT:
149 		/*
150 		 * If we're not polling our PHY instance, just return.
151 		 */
152 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
153 			return (0);
154 		break;
155 
156 	case MII_MEDIACHG:
157 		/*
158 		 * If the media indicates a different PHY instance,
159 		 * isolate ourselves.
160 		 */
161 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
162 			reg = PHY_READ(sc, IP1000PHY_MII_BMCR);
163 			PHY_WRITE(sc, IP1000PHY_MII_BMCR,
164 			    reg | IP1000PHY_BMCR_ISO);
165 			return (0);
166 		}
167 
168 		/*
169 		 * If the interface is not up, don't do anything.
170 		 */
171 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0) {
172 			break;
173 		}
174 
175 		ip1000phy_reset(sc);
176 		switch (IFM_SUBTYPE(ife->ifm_media)) {
177 		case IFM_AUTO:
178 			(void)ip1000phy_mii_phy_auto(sc);
179 			goto done;
180 			break;
181 
182 		case IFM_1000_T:
183 			/*
184 			 * XXX
185 			 * Manual 1000baseT setting doesn't seem to work.
186 			 */
187 			speed = IP1000PHY_BMCR_1000;
188 			break;
189 
190 		case IFM_100_TX:
191 			speed = IP1000PHY_BMCR_100;
192 			break;
193 
194 		case IFM_10_T:
195 			speed = IP1000PHY_BMCR_10;
196 			break;
197 
198 		default:
199 			return (EINVAL);
200 		}
201 
202 		if (((ife->ifm_media & IFM_GMASK) & IFM_FDX) != 0) {
203 			speed |= IP1000PHY_BMCR_FDX;
204 			gig = IP1000PHY_1000CR_1000T_FDX;
205 		} else
206 			gig = IP1000PHY_1000CR_1000T;
207 
208 		PHY_WRITE(sc, IP1000PHY_MII_1000CR, 0);
209 		PHY_WRITE(sc, IP1000PHY_MII_BMCR, speed);
210 
211 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_1000_T)
212 			break;
213 
214 		PHY_WRITE(sc, IP1000PHY_MII_1000CR, gig);
215 		PHY_WRITE(sc, IP1000PHY_MII_BMCR, speed);
216 
217 		/*
218 		 * When setting the link manually, one side must
219 		 * be the master and the other the slave. However
220 		 * ifmedia doesn't give us a good way to specify
221 		 * this, so we fake it by using one of the LINK
222 		 * flags. If LINK0 is set, we program the PHY to
223 		 * be a master, otherwise it's a slave.
224 		 */
225 		if ((mii->mii_ifp->if_flags & IFF_LINK0))
226 			PHY_WRITE(sc, IP1000PHY_MII_1000CR, gig |
227 			    IP1000PHY_1000CR_MASTER |
228 			    IP1000PHY_1000CR_MMASTER |
229 			    IP1000PHY_1000CR_MANUAL);
230 		else
231 			PHY_WRITE(sc, IP1000PHY_MII_1000CR, gig |
232 			    IP1000PHY_1000CR_MASTER |
233 			    IP1000PHY_1000CR_MANUAL);
234 
235 done:
236 		break;
237 
238 	case MII_TICK:
239 		/*
240 		 * If we're not currently selected, just return.
241 		 */
242 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
243 			return (0);
244 		/*
245 		 * Is the interface even up?
246 		 */
247 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
248 			return (0);
249 
250 		/*
251 		 * Only used for autonegotiation.
252 		 */
253 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
254 			sc->mii_ticks = 0;
255 			break;
256 		}
257 
258 		/*
259 		 * check for link.
260 		 */
261 		reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
262 		if (reg & BMSR_LINK) {
263 			sc->mii_ticks = 0;
264 			break;
265 		}
266 
267 		/* Announce link loss right after it happens */
268 		if (sc->mii_ticks++ == 0)
269 			break;
270 
271 		/*
272 		 * Only retry autonegotiation every mii_anegticks seconds.
273 		 */
274 		if (sc->mii_ticks <= sc->mii_anegticks)
275 			break;
276 
277 		sc->mii_ticks = 0;
278 		ip1000phy_mii_phy_auto(sc);
279 		break;
280 	}
281 
282 	/* Update the media status. */
283 	ip1000phy_status(sc);
284 
285 	/* Callback if something changed. */
286 	mii_phy_update(sc, cmd);
287 	return (0);
288 }
289 
290 static void
291 ip1000phy_status(struct mii_softc *sc)
292 {
293 	struct ip1000phy_softc *isc;
294 	struct mii_data *mii = sc->mii_pdata;
295 	uint32_t bmsr, bmcr, stat;
296 	uint32_t ar, lpar;
297 
298 	isc = (struct ip1000phy_softc *)sc;
299 
300 	mii->mii_media_status = IFM_AVALID;
301 	mii->mii_media_active = IFM_ETHER;
302 
303 	bmsr = PHY_READ(sc, IP1000PHY_MII_BMSR) |
304 	    PHY_READ(sc, IP1000PHY_MII_BMSR);
305 	if ((bmsr & IP1000PHY_BMSR_LINK) != 0)
306 		mii->mii_media_status |= IFM_ACTIVE;
307 
308 	bmcr = PHY_READ(sc, IP1000PHY_MII_BMCR);
309 	if ((bmcr & IP1000PHY_BMCR_LOOP) != 0)
310 		mii->mii_media_active |= IFM_LOOP;
311 
312 	if ((bmcr & IP1000PHY_BMCR_AUTOEN) != 0) {
313 		if ((bmsr & IP1000PHY_BMSR_ANEGCOMP) == 0) {
314 			/* Erg, still trying, I guess... */
315 			mii->mii_media_active |= IFM_NONE;
316 			return;
317                 }
318         }
319 
320 	if (isc->model == MII_MODEL_ICPLUS_IP1001) {
321 		stat = PHY_READ(sc, IP1000PHY_LSR);
322 		switch (stat & IP1000PHY_LSR_SPEED_MASK) {
323 		case IP1000PHY_LSR_SPEED_10:
324 			mii->mii_media_active |= IFM_10_T;
325 			break;
326 		case IP1000PHY_LSR_SPEED_100:
327 			mii->mii_media_active |= IFM_100_TX;
328 			break;
329 		case IP1000PHY_LSR_SPEED_1000:
330 			mii->mii_media_active |= IFM_1000_T;
331 			break;
332 		default:
333 			mii->mii_media_active |= IFM_NONE;
334 			return;
335 		}
336 		if ((stat & IP1000PHY_LSR_FULL_DUPLEX) != 0)
337 			mii->mii_media_active |= IFM_FDX;
338 		else
339 			mii->mii_media_active |= IFM_HDX;
340 	} else {
341 		stat = PHY_READ(sc, STGE_PhyCtrl);
342 		switch (PC_LinkSpeed(stat)) {
343 		case PC_LinkSpeed_Down:
344 			mii->mii_media_active |= IFM_NONE;
345 			return;
346 		case PC_LinkSpeed_10:
347 			mii->mii_media_active |= IFM_10_T;
348 			break;
349 		case PC_LinkSpeed_100:
350 			mii->mii_media_active |= IFM_100_TX;
351 			break;
352 		case PC_LinkSpeed_1000:
353 			mii->mii_media_active |= IFM_1000_T;
354 			break;
355 		default:
356 			mii->mii_media_active |= IFM_NONE;
357 			return;
358 		}
359 		if ((stat & PC_PhyDuplexStatus) != 0)
360 			mii->mii_media_active |= IFM_FDX;
361 		else
362 			mii->mii_media_active |= IFM_HDX;
363 	}
364 
365 	ar = PHY_READ(sc, IP1000PHY_MII_ANAR);
366 	lpar = PHY_READ(sc, IP1000PHY_MII_ANLPAR);
367 
368 	/*
369 	 * FLAG0 : Rx flow-control
370 	 * FLAG1 : Tx flow-control
371 	 */
372 	if ((ar & IP1000PHY_ANAR_PAUSE) && (lpar & IP1000PHY_ANLPAR_PAUSE))
373 		mii->mii_media_active |= IFM_FLAG0 | IFM_FLAG1;
374 	else if (!(ar & IP1000PHY_ANAR_PAUSE) && (ar & IP1000PHY_ANAR_APAUSE) &&
375 	    (lpar & IP1000PHY_ANLPAR_PAUSE) && (lpar & IP1000PHY_ANLPAR_APAUSE))
376 		mii->mii_media_active |= IFM_FLAG1;
377 	else if ((ar & IP1000PHY_ANAR_PAUSE) && (ar & IP1000PHY_ANAR_APAUSE) &&
378 	    !(lpar & IP1000PHY_ANLPAR_PAUSE) &&
379 	    (lpar & IP1000PHY_ANLPAR_APAUSE)) {
380 		mii->mii_media_active |= IFM_FLAG0;
381 	}
382 
383 	/*
384 	 * FLAG2 : local PHY resolved to MASTER
385 	 */
386 	if ((mii->mii_media_active & IFM_1000_T) != 0) {
387 		stat = PHY_READ(sc, IP1000PHY_MII_1000SR);
388 		if ((stat & IP1000PHY_1000SR_MASTER) != 0)
389 			mii->mii_media_active |= IFM_FLAG2;
390 	}
391 }
392 
393 static int
394 ip1000phy_mii_phy_auto(struct mii_softc *mii)
395 {
396 	uint32_t reg;
397 
398 	PHY_WRITE(mii, IP1000PHY_MII_ANAR,
399 	    IP1000PHY_ANAR_10T | IP1000PHY_ANAR_10T_FDX |
400 	    IP1000PHY_ANAR_100TX | IP1000PHY_ANAR_100TX_FDX |
401 	    IP1000PHY_ANAR_PAUSE | IP1000PHY_ANAR_APAUSE);
402 	reg = IP1000PHY_1000CR_1000T | IP1000PHY_1000CR_1000T_FDX;
403 	reg |= IP1000PHY_1000CR_MASTER;
404 	PHY_WRITE(mii, IP1000PHY_MII_1000CR, reg);
405 	PHY_WRITE(mii, IP1000PHY_MII_BMCR, (IP1000PHY_BMCR_FDX |
406 	    IP1000PHY_BMCR_AUTOEN | IP1000PHY_BMCR_STARTNEG));
407 
408 	return (EJUSTRETURN);
409 }
410 
411 static void
412 ip1000phy_load_dspcode(struct mii_softc *sc)
413 {
414 
415 	PHY_WRITE(sc, 31, 0x0001);
416 	PHY_WRITE(sc, 27, 0x01e0);
417 	PHY_WRITE(sc, 31, 0x0002);
418 	PHY_WRITE(sc, 27, 0xeb8e);
419 	PHY_WRITE(sc, 31, 0x0000);
420 	PHY_WRITE(sc, 30, 0x005e);
421 	PHY_WRITE(sc, 9, 0x0700);
422 
423 	DELAY(50);
424 }
425 
426 static void
427 ip1000phy_reset(struct mii_softc *sc)
428 {
429 	struct ip1000phy_softc *isc;
430 	struct stge_softc *stge_sc;
431 	struct mii_data *mii;
432 	uint32_t reg;
433 
434 	isc = (struct ip1000phy_softc *)sc;
435 	mii_phy_reset(sc);
436 
437 	/* clear autoneg/full-duplex as we don't want it after reset */
438 	reg = PHY_READ(sc, IP1000PHY_MII_BMCR);
439 	reg &= ~(IP1000PHY_BMCR_AUTOEN | IP1000PHY_BMCR_FDX);
440 	PHY_WRITE(sc, MII_BMCR, reg);
441 
442 	mii = sc->mii_pdata;
443 	/*
444 	 * XXX There should be more general way to pass PHY specific
445 	 * data via mii interface.
446 	 */
447 	if (isc->model == MII_MODEL_ICPLUS_IP1000A &&
448 	     strcmp(mii->mii_ifp->if_dname, "stge") == 0) {
449 		stge_sc = mii->mii_ifp->if_softc;
450 		if (stge_sc->sc_rev >= 0x40 && stge_sc->sc_rev <= 0x4e)
451 			ip1000phy_load_dspcode(sc);
452 	}
453 }
454