1178825Sdfr/*	OpenBSD: lxtphy.c,v 1.5 2000/08/26 20:04:17 nate Exp 	*/
2233294Sstas/*	NetBSD: lxtphy.c,v 1.19 2000/02/02 23:34:57 thorpej Exp 	*/
3178825Sdfr
4178825Sdfr/*-
5233294Sstas * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
6178825Sdfr * All rights reserved.
7178825Sdfr *
8178825Sdfr * This code is derived from software contributed to The NetBSD Foundation
9233294Sstas * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10178825Sdfr * NASA Ames Research Center.
11178825Sdfr *
12233294Sstas * Redistribution and use in source and binary forms, with or without
13178825Sdfr * modification, are permitted provided that the following conditions
14178825Sdfr * are met:
15178825Sdfr * 1. Redistributions of source code must retain the above copyright
16233294Sstas *    notice, this list of conditions and the following disclaimer.
17178825Sdfr * 2. Redistributions in binary form must reproduce the above copyright
18178825Sdfr *    notice, this list of conditions and the following disclaimer in the
19178825Sdfr *    documentation and/or other materials provided with the distribution.
20233294Sstas *
21178825Sdfr * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22178825Sdfr * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23178825Sdfr * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24178825Sdfr * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25178825Sdfr * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26178825Sdfr * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27178825Sdfr * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28178825Sdfr * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29178825Sdfr * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30178825Sdfr * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31178825Sdfr * POSSIBILITY OF SUCH DAMAGE.
32178825Sdfr */
33178825Sdfr
34178825Sdfr/*-
35178825Sdfr * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
36178825Sdfr *
37178825Sdfr * Redistribution and use in source and binary forms, with or without
38178825Sdfr * modification, are permitted provided that the following conditions
39178825Sdfr * are met:
40178825Sdfr * 1. Redistributions of source code must retain the above copyright
41178825Sdfr *    notice, this list of conditions and the following disclaimer.
42233294Sstas * 2. Redistributions in binary form must reproduce the above copyright
43178825Sdfr *    notice, this list of conditions and the following disclaimer in the
44178825Sdfr *    documentation and/or other materials provided with the distribution.
45178825Sdfr *
46178825Sdfr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
47178825Sdfr * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
48178825Sdfr * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
49178825Sdfr * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
50178825Sdfr * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
51178825Sdfr * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
52178825Sdfr * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
53233294Sstas * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
54233294Sstas * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
55233294Sstas * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56233294Sstas */
57233294Sstas
58233294Sstas#include <sys/cdefs.h>
59233294Sstas__FBSDID("$FreeBSD$");
60233294Sstas
61233294Sstas/*
62233294Sstas * driver for Level One's LXT-970 ethernet 10/100 PHY
63233294Sstas * datasheet from www.level1.com
64233294Sstas */
65233294Sstas
66233294Sstas#include <sys/param.h>
67233294Sstas#include <sys/systm.h>
68233294Sstas#include <sys/kernel.h>
69233294Sstas#include <sys/socket.h>
70233294Sstas#include <sys/errno.h>
71233294Sstas#include <sys/module.h>
72233294Sstas#include <sys/bus.h>
73233294Sstas
74233294Sstas#include <net/if.h>
75233294Sstas#include <net/if_media.h>
76233294Sstas
77233294Sstas#include <dev/mii/mii.h>
78233294Sstas#include <dev/mii/miivar.h>
79233294Sstas#include "miidevs.h"
80233294Sstas
81233294Sstas#include <dev/mii/lxtphyreg.h>
82233294Sstas
83233294Sstas#include "miibus_if.h"
84233294Sstas
85233294Sstasstatic int lxtphy_probe(device_t);
86233294Sstasstatic int lxtphy_attach(device_t);
87233294Sstas
88233294Sstasstatic device_method_t lxtphy_methods[] = {
89233294Sstas	/* device interface */
90233294Sstas	DEVMETHOD(device_probe,		lxtphy_probe),
91233294Sstas	DEVMETHOD(device_attach,	lxtphy_attach),
92233294Sstas	DEVMETHOD(device_detach,	mii_phy_detach),
93233294Sstas	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
94233294Sstas	DEVMETHOD_END
95};
96
97static devclass_t lxtphy_devclass;
98
99static driver_t lxtphy_driver = {
100	"lxtphy",
101	lxtphy_methods,
102	sizeof(struct mii_softc)
103};
104
105DRIVER_MODULE(lxtphy, miibus, lxtphy_driver, lxtphy_devclass, 0, 0);
106
107static int	lxtphy_service(struct mii_softc *, struct mii_data *, int);
108static void	lxtphy_status(struct mii_softc *);
109static void	lxtphy_reset(struct mii_softc *);
110static void	lxtphy_set_tp(struct mii_softc *);
111static void	lxtphy_set_fx(struct mii_softc *);
112
113static const struct mii_phydesc lxtphys[] = {
114	MII_PHY_DESC(xxLEVEL1, LXT970),
115	MII_PHY_END
116};
117
118static const struct mii_phy_funcs lxtphy_funcs = {
119	lxtphy_service,
120	lxtphy_status,
121	lxtphy_reset
122};
123
124static int
125lxtphy_probe(device_t dev)
126{
127
128	return (mii_phy_dev_probe(dev, lxtphys, BUS_PROBE_DEFAULT));
129}
130
131static int
132lxtphy_attach(device_t dev)
133{
134	struct mii_softc *sc;
135
136	sc = device_get_softc(dev);
137
138	mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &lxtphy_funcs, 0);
139
140	PHY_RESET(sc);
141
142	sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & sc->mii_capmask;
143	device_printf(dev, " ");
144
145#define	ADD(m, c)	ifmedia_add(&sc->mii_pdata->mii_media, (m), (c), NULL)
146	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_FX, 0, sc->mii_inst),
147	    MII_MEDIA_100_TX);
148	printf("100baseFX, ");
149	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_FX, IFM_FDX, sc->mii_inst),
150	    MII_MEDIA_100_TX_FDX);
151	printf("100baseFX-FDX, ");
152#undef ADD
153
154	mii_phy_add_media(sc);
155	printf("\n");
156
157	MIIBUS_MEDIAINIT(sc->mii_dev);
158	return (0);
159}
160
161static int
162lxtphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
163{
164	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
165
166	switch (cmd) {
167	case MII_POLLSTAT:
168		break;
169
170	case MII_MEDIACHG:
171		/*
172		 * If the interface is not up, don't do anything.
173		 */
174		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
175			break;
176
177		if (IFM_SUBTYPE(ife->ifm_media) == IFM_100_FX)
178			lxtphy_set_fx(sc);
179		else
180			lxtphy_set_tp(sc);
181
182		mii_phy_setmedia(sc);
183		break;
184
185	case MII_TICK:
186		if (mii_phy_tick(sc) == EJUSTRETURN)
187			return (0);
188		break;
189	}
190
191	/* Update the media status. */
192	PHY_STATUS(sc);
193
194	/* Callback if something changed. */
195	mii_phy_update(sc, cmd);
196	return (0);
197}
198
199static void
200lxtphy_status(struct mii_softc *sc)
201{
202	struct mii_data *mii = sc->mii_pdata;
203	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
204	int bmcr, bmsr, csr;
205
206	mii->mii_media_status = IFM_AVALID;
207	mii->mii_media_active = IFM_ETHER;
208
209	/*
210	 * Get link status from the CSR; we need to read the CSR
211	 * for media type anyhow, and the link status in the CSR
212	 * doens't latch, so fewer register reads are required.
213	 */
214	csr = PHY_READ(sc, MII_LXTPHY_CSR);
215	if (csr & CSR_LINK)
216		mii->mii_media_status |= IFM_ACTIVE;
217
218	bmcr = PHY_READ(sc, MII_BMCR);
219	if (bmcr & BMCR_ISO) {
220		mii->mii_media_active |= IFM_NONE;
221		mii->mii_media_status = 0;
222		return;
223	}
224
225	if (bmcr & BMCR_LOOP)
226		mii->mii_media_active |= IFM_LOOP;
227
228	if (bmcr & BMCR_AUTOEN) {
229		bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
230		if ((bmsr & BMSR_ACOMP) == 0) {
231			/* Erg, still trying, I guess... */
232			mii->mii_media_active |= IFM_NONE;
233			return;
234		}
235		if (csr & CSR_SPEED)
236			mii->mii_media_active |= IFM_100_TX;
237		else
238			mii->mii_media_active |= IFM_10_T;
239		if (csr & CSR_DUPLEX)
240			mii->mii_media_active |=
241			    IFM_FDX | mii_phy_flowstatus(sc);
242		else
243			mii->mii_media_active |= IFM_HDX;
244	} else
245		mii->mii_media_active = ife->ifm_media;
246}
247
248static void
249lxtphy_reset(struct mii_softc *sc)
250{
251
252	mii_phy_reset(sc);
253	PHY_WRITE(sc, MII_LXTPHY_IER,
254	    PHY_READ(sc, MII_LXTPHY_IER) & ~IER_INTEN);
255}
256
257static void
258lxtphy_set_tp(struct mii_softc *sc)
259{
260	int cfg;
261
262	cfg = PHY_READ(sc, MII_LXTPHY_CONFIG);
263	cfg &= ~CONFIG_100BASEFX;
264	PHY_WRITE(sc, MII_LXTPHY_CONFIG, cfg);
265}
266
267static void
268lxtphy_set_fx(struct mii_softc *sc)
269{
270	int cfg;
271
272	cfg = PHY_READ(sc, MII_LXTPHY_CONFIG);
273	cfg |= CONFIG_100BASEFX;
274	PHY_WRITE(sc, MII_LXTPHY_CONFIG, cfg);
275}
276