1/*
2 * Copyright (c) 1997, 1998, 1999
3 *	Bill Paul <wpaul@ee.columbia.edu>.  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, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD$");
35
36/*
37 * Pseudo-driver for media selection on the Lite-On PNIC 82c168
38 * chip.  The NWAY support on this chip is horribly broken, so we
39 * only support manual mode selection.  This is lame, but getting
40 * NWAY to work right is amazingly difficult.
41 */
42
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/kernel.h>
46#include <sys/socket.h>
47#include <sys/errno.h>
48#include <sys/lock.h>
49#include <sys/module.h>
50#include <sys/mutex.h>
51#include <sys/bus.h>
52
53#include <net/if.h>
54#include <net/if_arp.h>
55#include <net/if_media.h>
56
57#include <dev/mii/mii.h>
58#include <dev/mii/miivar.h>
59#include "miidevs.h"
60
61#include <machine/bus.h>
62#include <machine/resource.h>
63
64#include <dev/dc/if_dcreg.h>
65
66#include "miibus_if.h"
67
68static int pnphy_probe(device_t);
69static int pnphy_attach(device_t);
70
71static device_method_t pnphy_methods[] = {
72	/* device interface */
73	DEVMETHOD(device_probe,		pnphy_probe),
74	DEVMETHOD(device_attach,	pnphy_attach),
75	DEVMETHOD(device_detach,	mii_phy_detach),
76	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
77	DEVMETHOD_END
78};
79
80static devclass_t pnphy_devclass;
81
82static driver_t pnphy_driver = {
83	"pnphy",
84	pnphy_methods,
85	sizeof(struct mii_softc)
86};
87
88DRIVER_MODULE(pnphy, miibus, pnphy_driver, pnphy_devclass, 0, 0);
89
90static int	pnphy_service(struct mii_softc *, struct mii_data *, int);
91static void	pnphy_status(struct mii_softc *);
92static void	pnphy_reset(struct mii_softc *);
93
94static const struct mii_phy_funcs pnphy_funcs = {
95	pnphy_service,
96	pnphy_status,
97	pnphy_reset
98};
99
100static int
101pnphy_probe(device_t dev)
102{
103	struct mii_attach_args *ma;
104
105	ma = device_get_ivars(dev);
106
107	/*
108	 * The dc driver will report the 82c168 vendor and device
109	 * ID to let us know that it wants us to attach.
110	 */
111	if (ma->mii_id1 != DC_VENDORID_LO ||
112	    ma->mii_id2 != DC_DEVICEID_82C168)
113		return (ENXIO);
114
115	device_set_desc(dev, "PNIC 82c168 media interface");
116
117	return (BUS_PROBE_DEFAULT);
118}
119
120static int
121pnphy_attach(device_t dev)
122{
123	struct mii_softc *sc;
124
125	sc = device_get_softc(dev);
126
127	mii_phy_dev_attach(dev, MIIF_NOISOLATE | MIIF_NOMANPAUSE,
128	    &pnphy_funcs, 0);
129
130	sc->mii_capabilities =
131	    BMSR_100TXFDX | BMSR_100TXHDX | BMSR_10TFDX | BMSR_10THDX;
132	sc->mii_capabilities &= sc->mii_capmask;
133	device_printf(dev, " ");
134	mii_phy_add_media(sc);
135	printf("\n");
136
137	MIIBUS_MEDIAINIT(sc->mii_dev);
138	return (0);
139}
140
141static int
142pnphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
143{
144	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
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		 * Note that auto-negotiation is broken on this chip.
159		 */
160		switch (IFM_SUBTYPE(ife->ifm_media)) {
161		case IFM_100_TX:
162			mii->mii_media_active = IFM_ETHER | IFM_100_TX;
163			if ((ife->ifm_media & IFM_FDX) != 0)
164				mii->mii_media_active |= IFM_FDX;
165			MIIBUS_STATCHG(sc->mii_dev);
166			return (0);
167		case IFM_10_T:
168			mii->mii_media_active = IFM_ETHER | IFM_10_T;
169			if ((ife->ifm_media & IFM_FDX) != 0)
170				mii->mii_media_active |= IFM_FDX;
171			MIIBUS_STATCHG(sc->mii_dev);
172			return (0);
173		default:
174			return (EINVAL);
175		}
176		break;
177
178	case MII_TICK:
179		/*
180		 * Is the interface even up?
181		 */
182		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
183			return (0);
184
185		break;
186	}
187
188	/* Update the media status. */
189	PHY_STATUS(sc);
190
191	/* Callback if something changed. */
192	mii_phy_update(sc, cmd);
193	return (0);
194}
195
196static void
197pnphy_status(struct mii_softc *sc)
198{
199	struct mii_data *mii = sc->mii_pdata;
200	int reg;
201	struct dc_softc		*dc_sc;
202
203	dc_sc = mii->mii_ifp->if_softc;
204
205	mii->mii_media_status = IFM_AVALID;
206	mii->mii_media_active = IFM_ETHER;
207
208	reg = CSR_READ_4(dc_sc, DC_ISR);
209	if (!(reg & DC_ISR_LINKFAIL))
210		mii->mii_media_status |= IFM_ACTIVE;
211	reg = CSR_READ_4(dc_sc, DC_NETCFG);
212	if (reg & DC_NETCFG_SPEEDSEL)
213		mii->mii_media_active |= IFM_10_T;
214	else
215		mii->mii_media_active |= IFM_100_TX;
216	if (reg & DC_NETCFG_FULLDUPLEX)
217		mii->mii_media_active |= IFM_FDX;
218	else
219		mii->mii_media_active |= IFM_HDX;
220}
221
222static void
223pnphy_reset(struct mii_softc *sc __unused)
224{
225
226}
227