ip1000phy.c revision 160638
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: head/sys/dev/mii/ip1000phy.c 160638 2006-07-25 00:16:09Z yongari $");
31
32/*
33 * Driver for the IC Plus IP1000A 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
44#include <net/if.h>
45#include <net/if_media.h>
46
47#include <dev/mii/mii.h>
48#include <dev/mii/miivar.h>
49#include "miidevs.h"
50
51#include <dev/mii/ip1000phyreg.h>
52
53#include "miibus_if.h"
54
55#include <machine/bus.h>
56#include <dev/stge/if_stgereg.h>
57
58static int ip1000phy_probe(device_t);
59static int ip1000phy_attach(device_t);
60
61static device_method_t ip1000phy_methods[] = {
62	/* device interface */
63	DEVMETHOD(device_probe,		ip1000phy_probe),
64	DEVMETHOD(device_attach,	ip1000phy_attach),
65	DEVMETHOD(device_detach,	mii_phy_detach),
66	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
67	{ 0, 0 }
68};
69
70static devclass_t ip1000phy_devclass;
71static driver_t ip1000phy_driver = {
72	"ip1000phy",
73	ip1000phy_methods,
74	sizeof (struct mii_softc)
75};
76
77DRIVER_MODULE(ip1000phy, miibus, ip1000phy_driver, ip1000phy_devclass, 0, 0);
78
79static int	ip1000phy_service(struct mii_softc *, struct mii_data *, int);
80static void	ip1000phy_status(struct mii_softc *);
81static void	ip1000phy_reset(struct mii_softc *);
82static int	ip1000phy_mii_phy_auto(struct mii_softc *);
83
84static const struct mii_phydesc ip1000phys[] = {
85	MII_PHY_DESC(ICPLUS, IP1000A),
86	MII_PHY_END
87};
88
89static int
90ip1000phy_probe(device_t dev)
91{
92	struct mii_attach_args *ma;
93	const struct mii_phydesc *mpd;
94
95	ma = device_get_ivars(dev);
96	mpd = mii_phy_match(ma, ip1000phys);
97	if (mpd != NULL) {
98		device_set_desc(dev, mpd->mpd_name);
99		return (BUS_PROBE_DEFAULT);
100	}
101
102	return (ENXIO);
103}
104
105static int
106ip1000phy_attach(device_t dev)
107{
108	struct mii_softc *sc;
109	struct mii_attach_args *ma;
110	struct mii_data *mii;
111
112	sc = device_get_softc(dev);
113	ma = device_get_ivars(dev);
114	sc->mii_dev = device_get_parent(dev);
115	mii = device_get_softc(sc->mii_dev);
116	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
117
118	sc->mii_inst = mii->mii_instance;
119	sc->mii_phy = ma->mii_phyno;
120	sc->mii_service = ip1000phy_service;
121	sc->mii_pdata = mii;
122	sc->mii_anegticks = MII_ANEGTICKS_GIGE;
123	sc->mii_flags |= MIIF_NOISOLATE;
124
125	mii->mii_instance++;
126
127	device_printf(dev, " ");
128
129#define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
130
131	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
132	    BMCR_ISO);
133
134	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
135	    IP1000PHY_BMCR_10);
136	printf("10baseT, ");
137	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
138	    IP1000PHY_BMCR_10 | IP1000PHY_BMCR_FDX);
139	printf("10baseT-FDX, ");
140	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
141	    IP1000PHY_BMCR_100);
142	printf("100baseTX, ");
143	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
144	    IP1000PHY_BMCR_100 | IP1000PHY_BMCR_FDX);
145	printf("100baseTX-FDX, ");
146	/* 1000baseT half-duplex, really supported? */
147	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 0, sc->mii_inst),
148	    IP1000PHY_BMCR_1000);
149	printf("1000baseTX, ");
150	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX, sc->mii_inst),
151	    IP1000PHY_BMCR_1000 | IP1000PHY_BMCR_FDX);
152	printf("1000baseTX-FDX, ");
153	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst), 0);
154	printf("auto\n");
155#undef ADD
156
157	ip1000phy_reset(sc);
158
159	MIIBUS_MEDIAINIT(sc->mii_dev);
160	return(0);
161}
162
163static int
164ip1000phy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
165{
166	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
167	uint32_t gig, reg, speed;
168
169	switch (cmd) {
170	case MII_POLLSTAT:
171		/*
172		 * If we're not polling our PHY instance, just return.
173		 */
174		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
175			return (0);
176		break;
177
178	case MII_MEDIACHG:
179		/*
180		 * If the media indicates a different PHY instance,
181		 * isolate ourselves.
182		 */
183		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
184			reg = PHY_READ(sc, IP1000PHY_MII_BMCR);
185			PHY_WRITE(sc, IP1000PHY_MII_BMCR,
186			    reg | IP1000PHY_BMCR_ISO);
187			return (0);
188		}
189
190		/*
191		 * If the interface is not up, don't do anything.
192		 */
193		if ((mii->mii_ifp->if_flags & IFF_UP) == 0) {
194			break;
195		}
196
197		ip1000phy_reset(sc);
198		switch (IFM_SUBTYPE(ife->ifm_media)) {
199		case IFM_AUTO:
200			(void)ip1000phy_mii_phy_auto(sc);
201			goto done;
202			break;
203
204		case IFM_1000_T:
205			/*
206			 * XXX
207			 * Manual 1000baseT setting doesn't seem to work.
208			 */
209			speed = IP1000PHY_BMCR_1000;
210			break;
211
212		case IFM_100_TX:
213			speed = IP1000PHY_BMCR_100;
214			break;
215
216		case IFM_10_T:
217			speed = IP1000PHY_BMCR_10;
218			break;
219
220		default:
221			return (EINVAL);
222		}
223
224		if (((ife->ifm_media & IFM_GMASK) & IFM_FDX) != 0) {
225			speed |= IP1000PHY_BMCR_FDX;
226			gig = IP1000PHY_1000CR_1000T_FDX;
227		} else
228			gig = IP1000PHY_1000CR_1000T;
229
230		PHY_WRITE(sc, IP1000PHY_MII_1000CR, 0);
231		PHY_WRITE(sc, IP1000PHY_MII_BMCR, speed);
232
233		if (IFM_SUBTYPE(ife->ifm_media) != IFM_1000_T)
234			break;
235
236		PHY_WRITE(sc, IP1000PHY_MII_1000CR, gig);
237		PHY_WRITE(sc, IP1000PHY_MII_BMCR, speed);
238
239		/*
240		 * When settning the link manually, one side must
241		 * be the master and the other the slave. However
242		 * ifmedia doesn't give us a good way to specify
243		 * this, so we fake it by using one of the LINK
244		 * flags. If LINK0 is set, we program the PHY to
245		 * be a master, otherwise it's a slave.
246		 */
247		if ((mii->mii_ifp->if_flags & IFF_LINK0))
248			PHY_WRITE(sc, IP1000PHY_MII_1000CR, gig |
249			    IP1000PHY_1000CR_MASTER |
250			    IP1000PHY_1000CR_MMASTER |
251			    IP1000PHY_1000CR_MANUAL);
252		else
253			PHY_WRITE(sc, IP1000PHY_MII_1000CR, gig |
254			    IP1000PHY_1000CR_MASTER |
255			    IP1000PHY_1000CR_MANUAL);
256
257done:
258		break;
259
260	case MII_TICK:
261		/*
262		 * If we're not currently selected, just return.
263		 */
264		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
265			return (0);
266		/*
267		 * Is the interface even up?
268		 */
269		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
270			return (0);
271
272		/*
273		 * Only used for autonegotiation.
274		 */
275		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
276			sc->mii_ticks = 0;
277			break;
278		}
279
280		/*
281		 * check for link.
282		 */
283		reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
284		if (reg & BMSR_LINK) {
285			sc->mii_ticks = 0;
286			break;
287		}
288
289		/* Announce link loss right after it happens */
290		if (sc->mii_ticks++ == 0)
291			break;
292
293		/*
294		 * Only retry autonegotiation every mii_anegticks seconds.
295		 */
296		if (sc->mii_ticks <= sc->mii_anegticks)
297			return (0);
298
299		sc->mii_ticks = 0;
300		ip1000phy_mii_phy_auto(sc);
301		break;
302	}
303
304	/* Update the media status. */
305	ip1000phy_status(sc);
306
307	/* Callback if something changed. */
308	mii_phy_update(sc, cmd);
309	return (0);
310}
311
312static void
313ip1000phy_status(struct mii_softc *sc)
314{
315	struct mii_data *mii = sc->mii_pdata;
316	uint32_t bmsr, bmcr, stat;
317	uint32_t ar, lpar;
318
319	mii->mii_media_status = IFM_AVALID;
320	mii->mii_media_active = IFM_ETHER;
321
322	bmsr = PHY_READ(sc, IP1000PHY_MII_BMSR) |
323	    PHY_READ(sc, IP1000PHY_MII_BMSR);
324	if ((bmsr & IP1000PHY_BMSR_LINK) != 0)
325		mii->mii_media_status |= IFM_ACTIVE;
326
327	bmcr = PHY_READ(sc, IP1000PHY_MII_BMCR);
328	if ((bmcr & IP1000PHY_BMCR_LOOP) != 0)
329		mii->mii_media_active |= IFM_LOOP;
330
331	if ((bmcr & IP1000PHY_BMCR_AUTOEN) != 0) {
332		if ((bmsr & IP1000PHY_BMSR_ANEGCOMP) == 0) {
333			/* Erg, still trying, I guess... */
334			mii->mii_media_active |= IFM_NONE;
335			return;
336                }
337        }
338
339	stat = PHY_READ(sc, STGE_PhyCtrl);
340	switch (PC_LinkSpeed(stat)) {
341	case PC_LinkSpeed_Down:
342		mii->mii_media_active |= IFM_NONE;
343		return;
344	case PC_LinkSpeed_10:
345		mii->mii_media_active |= IFM_10_T;
346		break;
347	case PC_LinkSpeed_100:
348		mii->mii_media_active |= IFM_100_TX;
349		break;
350	case PC_LinkSpeed_1000:
351		mii->mii_media_active |= IFM_1000_T;
352		break;
353	}
354	if ((stat & PC_PhyDuplexStatus) != 0)
355		mii->mii_media_active |= IFM_FDX;
356	else
357		mii->mii_media_active |= IFM_HDX;
358
359	ar = PHY_READ(sc, IP1000PHY_MII_ANAR);
360	lpar = PHY_READ(sc, IP1000PHY_MII_ANLPAR);
361
362	/*
363	 * FLAG0 : Rx flow-control
364	 * FLAG1 : Tx flow-control
365	 */
366	if ((ar & IP1000PHY_ANAR_PAUSE) && (lpar & IP1000PHY_ANLPAR_PAUSE))
367		mii->mii_media_active |= IFM_FLAG0 | IFM_FLAG1;
368	else if (!(ar & IP1000PHY_ANAR_PAUSE) && (ar & IP1000PHY_ANAR_APAUSE) &&
369	    (lpar & IP1000PHY_ANLPAR_PAUSE) && (lpar & IP1000PHY_ANLPAR_APAUSE))
370		mii->mii_media_active |= IFM_FLAG1;
371	else if ((ar & IP1000PHY_ANAR_PAUSE) && (ar & IP1000PHY_ANAR_APAUSE) &&
372	    !(lpar & IP1000PHY_ANLPAR_PAUSE) &&
373	    (lpar & IP1000PHY_ANLPAR_APAUSE)) {
374		mii->mii_media_active |= IFM_FLAG0;
375	}
376
377	/*
378	 * FLAG2 : local PHY resolved to MASTER
379	 */
380	if ((mii->mii_media_active & IFM_1000_T) != 0) {
381		stat = PHY_READ(sc, IP1000PHY_MII_1000SR);
382		if ((stat & IP1000PHY_1000SR_MASTER) != 0)
383			mii->mii_media_active |= IFM_FLAG2;
384	}
385}
386
387static int
388ip1000phy_mii_phy_auto(struct mii_softc *mii)
389{
390	uint32_t reg;
391
392	PHY_WRITE(mii, IP1000PHY_MII_ANAR,
393	    IP1000PHY_ANAR_10T | IP1000PHY_ANAR_10T_FDX |
394	    IP1000PHY_ANAR_100TX | IP1000PHY_ANAR_100TX_FDX |
395	    IP1000PHY_ANAR_PAUSE | IP1000PHY_ANAR_APAUSE);
396	reg = IP1000PHY_1000CR_1000T | IP1000PHY_1000CR_1000T_FDX;
397	reg |= IP1000PHY_1000CR_MASTER;
398	PHY_WRITE(mii, IP1000PHY_MII_1000CR, reg);
399	PHY_WRITE(mii, IP1000PHY_MII_BMCR, (IP1000PHY_BMCR_FDX |
400	    IP1000PHY_BMCR_AUTOEN | IP1000PHY_BMCR_STARTNEG));
401
402	return (EJUSTRETURN);
403}
404
405static void
406ip1000phy_load_dspcode(struct mii_softc *sc)
407{
408
409	PHY_WRITE(sc, 31, 0x0001);
410	PHY_WRITE(sc, 27, 0x01e0);
411	PHY_WRITE(sc, 31, 0x0002);
412	PHY_WRITE(sc, 27, 0xeb8e);
413	PHY_WRITE(sc, 31, 0x0000);
414	PHY_WRITE(sc, 30, 0x005e);
415	PHY_WRITE(sc, 9, 0x0700);
416
417	DELAY(50);
418}
419
420static void
421ip1000phy_reset(struct mii_softc *sc)
422{
423	struct stge_softc *stge_sc;
424	struct mii_data *mii;
425	uint32_t reg;
426
427	mii_phy_reset(sc);
428
429	/* clear autoneg/full-duplex as we don't want it after reset */
430	reg = PHY_READ(sc, IP1000PHY_MII_BMCR);
431	reg &= ~(IP1000PHY_BMCR_AUTOEN | IP1000PHY_BMCR_FDX);
432	PHY_WRITE(sc, MII_BMCR, reg);
433
434	mii = sc->mii_pdata;
435	/*
436	 * XXX There should be more general way to pass PHY specific
437	 * data via mii interface.
438	 */
439	if (strcmp(mii->mii_ifp->if_dname, "stge") == 0) {
440		stge_sc = mii->mii_ifp->if_softc;
441		if (stge_sc->sc_rev >= 0x40 && stge_sc->sc_rev <= 0x4e)
442			ip1000phy_load_dspcode(sc);
443	}
444}
445