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