xref: /freebsd/sys/dev/mii/ip1000phy.c (revision de1add1e)
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 = ma->mii_data;
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 
122 	sc->mii_flags |= MIIF_NOISOLATE;
123 
124 	isc->model = MII_MODEL(ma->mii_id2);
125 	isc->revision = MII_REV(ma->mii_id2);
126 
127 	sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
128 	if (sc->mii_capabilities & BMSR_EXTSTAT)
129 		sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
130 	device_printf(dev, " ");
131 
132 	ip1000phy_reset(sc);
133 	mii_phy_add_media(sc);
134 	printf("\n");
135 
136 	MIIBUS_MEDIAINIT(sc->mii_dev);
137 	return (0);
138 }
139 
140 static int
141 ip1000phy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
142 {
143 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
144 	uint32_t gig, reg, speed;
145 
146 	switch (cmd) {
147 	case MII_POLLSTAT:
148 		break;
149 
150 	case MII_MEDIACHG:
151 		/*
152 		 * If the interface is not up, don't do anything.
153 		 */
154 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0) {
155 			break;
156 		}
157 
158 		ip1000phy_reset(sc);
159 		switch (IFM_SUBTYPE(ife->ifm_media)) {
160 		case IFM_AUTO:
161 			(void)ip1000phy_mii_phy_auto(sc);
162 			goto done;
163 			break;
164 
165 		case IFM_1000_T:
166 			/*
167 			 * XXX
168 			 * Manual 1000baseT setting doesn't seem to work.
169 			 */
170 			speed = IP1000PHY_BMCR_1000;
171 			break;
172 
173 		case IFM_100_TX:
174 			speed = IP1000PHY_BMCR_100;
175 			break;
176 
177 		case IFM_10_T:
178 			speed = IP1000PHY_BMCR_10;
179 			break;
180 
181 		default:
182 			return (EINVAL);
183 		}
184 
185 		if (((ife->ifm_media & IFM_GMASK) & IFM_FDX) != 0) {
186 			speed |= IP1000PHY_BMCR_FDX;
187 			gig = IP1000PHY_1000CR_1000T_FDX;
188 		} else
189 			gig = IP1000PHY_1000CR_1000T;
190 
191 		PHY_WRITE(sc, IP1000PHY_MII_1000CR, 0);
192 		PHY_WRITE(sc, IP1000PHY_MII_BMCR, speed);
193 
194 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_1000_T)
195 			break;
196 
197 		PHY_WRITE(sc, IP1000PHY_MII_1000CR, gig);
198 		PHY_WRITE(sc, IP1000PHY_MII_BMCR, speed);
199 
200 		/*
201 		 * When setting the link manually, one side must
202 		 * be the master and the other the slave. However
203 		 * ifmedia doesn't give us a good way to specify
204 		 * this, so we fake it by using one of the LINK
205 		 * flags. If LINK0 is set, we program the PHY to
206 		 * be a master, otherwise it's a slave.
207 		 */
208 		if ((mii->mii_ifp->if_flags & IFF_LINK0))
209 			PHY_WRITE(sc, IP1000PHY_MII_1000CR, gig |
210 			    IP1000PHY_1000CR_MASTER |
211 			    IP1000PHY_1000CR_MMASTER |
212 			    IP1000PHY_1000CR_MANUAL);
213 		else
214 			PHY_WRITE(sc, IP1000PHY_MII_1000CR, gig |
215 			    IP1000PHY_1000CR_MASTER |
216 			    IP1000PHY_1000CR_MANUAL);
217 
218 done:
219 		break;
220 
221 	case MII_TICK:
222 		/*
223 		 * Is the interface even up?
224 		 */
225 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
226 			return (0);
227 
228 		/*
229 		 * Only used for autonegotiation.
230 		 */
231 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
232 			sc->mii_ticks = 0;
233 			break;
234 		}
235 
236 		/*
237 		 * check for link.
238 		 */
239 		reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
240 		if (reg & BMSR_LINK) {
241 			sc->mii_ticks = 0;
242 			break;
243 		}
244 
245 		/* Announce link loss right after it happens */
246 		if (sc->mii_ticks++ == 0)
247 			break;
248 
249 		/*
250 		 * Only retry autonegotiation every mii_anegticks seconds.
251 		 */
252 		if (sc->mii_ticks <= sc->mii_anegticks)
253 			break;
254 
255 		sc->mii_ticks = 0;
256 		ip1000phy_mii_phy_auto(sc);
257 		break;
258 	}
259 
260 	/* Update the media status. */
261 	ip1000phy_status(sc);
262 
263 	/* Callback if something changed. */
264 	mii_phy_update(sc, cmd);
265 	return (0);
266 }
267 
268 static void
269 ip1000phy_status(struct mii_softc *sc)
270 {
271 	struct ip1000phy_softc *isc;
272 	struct mii_data *mii = sc->mii_pdata;
273 	uint32_t bmsr, bmcr, stat;
274 	uint32_t ar, lpar;
275 
276 	isc = (struct ip1000phy_softc *)sc;
277 
278 	mii->mii_media_status = IFM_AVALID;
279 	mii->mii_media_active = IFM_ETHER;
280 
281 	bmsr = PHY_READ(sc, IP1000PHY_MII_BMSR) |
282 	    PHY_READ(sc, IP1000PHY_MII_BMSR);
283 	if ((bmsr & IP1000PHY_BMSR_LINK) != 0)
284 		mii->mii_media_status |= IFM_ACTIVE;
285 
286 	bmcr = PHY_READ(sc, IP1000PHY_MII_BMCR);
287 	if ((bmcr & IP1000PHY_BMCR_LOOP) != 0)
288 		mii->mii_media_active |= IFM_LOOP;
289 
290 	if ((bmcr & IP1000PHY_BMCR_AUTOEN) != 0) {
291 		if ((bmsr & IP1000PHY_BMSR_ANEGCOMP) == 0) {
292 			/* Erg, still trying, I guess... */
293 			mii->mii_media_active |= IFM_NONE;
294 			return;
295                 }
296         }
297 
298 	if (isc->model == MII_MODEL_ICPLUS_IP1001) {
299 		stat = PHY_READ(sc, IP1000PHY_LSR);
300 		switch (stat & IP1000PHY_LSR_SPEED_MASK) {
301 		case IP1000PHY_LSR_SPEED_10:
302 			mii->mii_media_active |= IFM_10_T;
303 			break;
304 		case IP1000PHY_LSR_SPEED_100:
305 			mii->mii_media_active |= IFM_100_TX;
306 			break;
307 		case IP1000PHY_LSR_SPEED_1000:
308 			mii->mii_media_active |= IFM_1000_T;
309 			break;
310 		default:
311 			mii->mii_media_active |= IFM_NONE;
312 			return;
313 		}
314 		if ((stat & IP1000PHY_LSR_FULL_DUPLEX) != 0)
315 			mii->mii_media_active |= IFM_FDX;
316 		else
317 			mii->mii_media_active |= IFM_HDX;
318 	} else {
319 		stat = PHY_READ(sc, STGE_PhyCtrl);
320 		switch (PC_LinkSpeed(stat)) {
321 		case PC_LinkSpeed_Down:
322 			mii->mii_media_active |= IFM_NONE;
323 			return;
324 		case PC_LinkSpeed_10:
325 			mii->mii_media_active |= IFM_10_T;
326 			break;
327 		case PC_LinkSpeed_100:
328 			mii->mii_media_active |= IFM_100_TX;
329 			break;
330 		case PC_LinkSpeed_1000:
331 			mii->mii_media_active |= IFM_1000_T;
332 			break;
333 		default:
334 			mii->mii_media_active |= IFM_NONE;
335 			return;
336 		}
337 		if ((stat & PC_PhyDuplexStatus) != 0)
338 			mii->mii_media_active |= IFM_FDX;
339 		else
340 			mii->mii_media_active |= IFM_HDX;
341 	}
342 
343 	ar = PHY_READ(sc, IP1000PHY_MII_ANAR);
344 	lpar = PHY_READ(sc, IP1000PHY_MII_ANLPAR);
345 
346 	/*
347 	 * FLAG0 : Rx flow-control
348 	 * FLAG1 : Tx flow-control
349 	 */
350 	if ((ar & IP1000PHY_ANAR_PAUSE) && (lpar & IP1000PHY_ANLPAR_PAUSE))
351 		mii->mii_media_active |= IFM_FLAG0 | IFM_FLAG1;
352 	else if (!(ar & IP1000PHY_ANAR_PAUSE) && (ar & IP1000PHY_ANAR_APAUSE) &&
353 	    (lpar & IP1000PHY_ANLPAR_PAUSE) && (lpar & IP1000PHY_ANLPAR_APAUSE))
354 		mii->mii_media_active |= IFM_FLAG1;
355 	else if ((ar & IP1000PHY_ANAR_PAUSE) && (ar & IP1000PHY_ANAR_APAUSE) &&
356 	    !(lpar & IP1000PHY_ANLPAR_PAUSE) &&
357 	    (lpar & IP1000PHY_ANLPAR_APAUSE)) {
358 		mii->mii_media_active |= IFM_FLAG0;
359 	}
360 
361 	/*
362 	 * FLAG2 : local PHY resolved to MASTER
363 	 */
364 	if ((mii->mii_media_active & IFM_1000_T) != 0) {
365 		stat = PHY_READ(sc, IP1000PHY_MII_1000SR);
366 		if ((stat & IP1000PHY_1000SR_MASTER) != 0)
367 			mii->mii_media_active |= IFM_FLAG2;
368 	}
369 }
370 
371 static int
372 ip1000phy_mii_phy_auto(struct mii_softc *sc)
373 {
374 	struct ip1000phy_softc *isc;
375 	uint32_t reg;
376 
377 	isc = (struct ip1000phy_softc *)sc;
378 	reg = 0;
379 	if (isc->model == MII_MODEL_ICPLUS_IP1001) {
380 		reg = PHY_READ(sc, IP1000PHY_MII_ANAR);
381 		reg |= IP1000PHY_ANAR_NP;
382 	}
383 	reg |= IP1000PHY_ANAR_10T | IP1000PHY_ANAR_10T_FDX |
384 	    IP1000PHY_ANAR_100TX | IP1000PHY_ANAR_100TX_FDX |
385 	    IP1000PHY_ANAR_PAUSE | IP1000PHY_ANAR_APAUSE;
386 	PHY_WRITE(sc, IP1000PHY_MII_ANAR, reg | IP1000PHY_ANAR_CSMA);
387 
388 	reg = IP1000PHY_1000CR_1000T | IP1000PHY_1000CR_1000T_FDX;
389 	reg |= IP1000PHY_1000CR_MASTER;
390 	PHY_WRITE(sc, IP1000PHY_MII_1000CR, reg);
391 	PHY_WRITE(sc, IP1000PHY_MII_BMCR, (IP1000PHY_BMCR_FDX |
392 	    IP1000PHY_BMCR_AUTOEN | IP1000PHY_BMCR_STARTNEG));
393 
394 	return (EJUSTRETURN);
395 }
396 
397 static void
398 ip1000phy_load_dspcode(struct mii_softc *sc)
399 {
400 
401 	PHY_WRITE(sc, 31, 0x0001);
402 	PHY_WRITE(sc, 27, 0x01e0);
403 	PHY_WRITE(sc, 31, 0x0002);
404 	PHY_WRITE(sc, 27, 0xeb8e);
405 	PHY_WRITE(sc, 31, 0x0000);
406 	PHY_WRITE(sc, 30, 0x005e);
407 	PHY_WRITE(sc, 9, 0x0700);
408 
409 	DELAY(50);
410 }
411 
412 static void
413 ip1000phy_reset(struct mii_softc *sc)
414 {
415 	struct ip1000phy_softc *isc;
416 	struct stge_softc *stge_sc;
417 	struct mii_data *mii;
418 	uint32_t reg;
419 
420 	isc = (struct ip1000phy_softc *)sc;
421 	mii_phy_reset(sc);
422 
423 	/* clear autoneg/full-duplex as we don't want it after reset */
424 	reg = PHY_READ(sc, IP1000PHY_MII_BMCR);
425 	reg &= ~(IP1000PHY_BMCR_AUTOEN | IP1000PHY_BMCR_FDX);
426 	PHY_WRITE(sc, MII_BMCR, reg);
427 
428 	mii = sc->mii_pdata;
429 	/*
430 	 * XXX There should be more general way to pass PHY specific
431 	 * data via mii interface.
432 	 */
433 	if (isc->model == MII_MODEL_ICPLUS_IP1000A &&
434 	     strcmp(mii->mii_ifp->if_dname, "stge") == 0) {
435 		stge_sc = mii->mii_ifp->if_softc;
436 		if (stge_sc->sc_rev >= 0x40 && stge_sc->sc_rev <= 0x4e)
437 			ip1000phy_load_dspcode(sc);
438 	}
439 }
440