pnaphy.c revision 150763
1179404Sobrien/*-
2179404Sobrien * Copyright (c) 2000 Berkeley Software Design, Inc.
3179404Sobrien * Copyright (c) 1997, 1998, 1999, 2000
4179404Sobrien *	Bill Paul <wpaul@osd.bsdi.com>.  All rights reserved.
5179404Sobrien *
6179404Sobrien * Redistribution and use in source and binary forms, with or without
7179404Sobrien * modification, are permitted provided that the following conditions
8179404Sobrien * are met:
9179404Sobrien * 1. Redistributions of source code must retain the above copyright
10179404Sobrien *    notice, this list of conditions and the following disclaimer.
11179404Sobrien * 2. Redistributions in binary form must reproduce the above copyright
12179404Sobrien *    notice, this list of conditions and the following disclaimer in the
13179404Sobrien *    documentation and/or other materials provided with the distribution.
14179404Sobrien * 3. All advertising materials mentioning features or use of this software
15179404Sobrien *    must display the following acknowledgement:
16179404Sobrien *	This product includes software developed by Bill Paul.
17179404Sobrien * 4. Neither the name of the author nor the names of any co-contributors
18179404Sobrien *    may be used to endorse or promote products derived from this software
19179404Sobrien *    without specific prior written permission.
20179404Sobrien *
21179404Sobrien * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
22179404Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23179404Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24179404Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
25179404Sobrien * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26179404Sobrien * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27179404Sobrien * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28179404Sobrien * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29179404Sobrien * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30179404Sobrien * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31179404Sobrien * THE POSSIBILITY OF SUCH DAMAGE.
32179404Sobrien */
33179404Sobrien
34179404Sobrien#include <sys/cdefs.h>
35179404Sobrien__FBSDID("$FreeBSD: head/sys/dev/mii/pnaphy.c 150763 2005-09-30 19:39:27Z imp $");
36179404Sobrien
37179404Sobrien/*
38 * driver for homePNA PHYs
39 * This is really just a stub that allows us to identify homePNA-based
40 * transceicers and display the link status. MII-based homePNA PHYs
41 * only support one media type and no autonegotiation. If we were
42 * really clever, we could tweak some of the vendor-specific registers
43 * to optimize the link.
44 */
45
46#include <sys/param.h>
47#include <sys/systm.h>
48#include <sys/kernel.h>
49#include <sys/socket.h>
50#include <sys/errno.h>
51#include <sys/module.h>
52#include <sys/bus.h>
53
54#include <net/if.h>
55#include <net/if_media.h>
56
57
58#include <dev/mii/mii.h>
59#include <dev/mii/miivar.h>
60#include "miidevs.h"
61
62#include "miibus_if.h"
63
64static int pnaphy_probe(device_t);
65static int pnaphy_attach(device_t);
66
67static device_method_t pnaphy_methods[] = {
68	/* device interface */
69	DEVMETHOD(device_probe,		pnaphy_probe),
70	DEVMETHOD(device_attach,	pnaphy_attach),
71	DEVMETHOD(device_detach,	mii_phy_detach),
72	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
73	{ 0, 0 }
74};
75
76static devclass_t pnaphy_devclass;
77
78static driver_t pnaphy_driver = {
79	"pnaphy",
80	pnaphy_methods,
81	sizeof(struct mii_softc)
82};
83
84DRIVER_MODULE(pnaphy, miibus, pnaphy_driver, pnaphy_devclass, 0, 0);
85
86static int	pnaphy_service(struct mii_softc *, struct mii_data *,int);
87
88static int
89pnaphy_probe(device_t dev)
90{
91
92	struct mii_attach_args	*ma;
93
94	ma = device_get_ivars(dev);
95
96	if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_AMD &&
97	    MII_MODEL(ma->mii_id2) == MII_MODEL_AMD_79c978) {
98		device_set_desc(dev, MII_STR_AMD_79c978);
99		return(0);
100	}
101
102	return(ENXIO);
103}
104
105static int
106pnaphy_attach(device_t dev)
107{
108	struct mii_softc *sc;
109	struct mii_attach_args *ma;
110	struct mii_data *mii;
111	const char *sep = "";
112
113	sc = device_get_softc(dev);
114	ma = device_get_ivars(dev);
115	sc->mii_dev = device_get_parent(dev);
116	mii = device_get_softc(sc->mii_dev);
117	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
118
119	sc->mii_inst = mii->mii_instance;
120	sc->mii_phy = ma->mii_phyno;
121	sc->mii_service = pnaphy_service;
122	sc->mii_pdata = mii;
123
124	mii->mii_instance++;
125
126	sc->mii_flags |= MIIF_NOISOLATE;
127
128#define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
129#define PRINT(s)	printf("%s%s", sep, s); sep = ", "
130
131	mii_phy_reset(sc);
132
133	sc->mii_capabilities =
134	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
135	device_printf(dev, " ");
136	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
137		printf("no media present");
138	else {
139		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_HPNA_1, 0, sc->mii_inst), 0);
140		PRINT("HomePNA");
141	}
142	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
143	    BMCR_ISO);
144
145	printf("\n");
146
147#undef ADD
148#undef PRINT
149
150	MIIBUS_MEDIAINIT(sc->mii_dev);
151
152	return(0);
153}
154
155static int
156pnaphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
157{
158	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
159	int reg;
160
161	switch (cmd) {
162	case MII_POLLSTAT:
163		/*
164		 * If we're not polling our PHY instance, just return.
165		 */
166		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
167			return (0);
168		break;
169
170	case MII_MEDIACHG:
171		/*
172		 * If the media indicates a different PHY instance,
173		 * isolate ourselves.
174		 */
175		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
176			reg = PHY_READ(sc, MII_BMCR);
177			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
178			return (0);
179		}
180
181		/*
182		 * If the interface is not up, don't do anything.
183		 */
184		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
185			break;
186
187		switch (IFM_SUBTYPE(ife->ifm_media)) {
188		case IFM_AUTO:
189		case IFM_10_T:
190		case IFM_100_TX:
191		case IFM_100_T4:
192			return (EINVAL);
193		default:
194			/*
195			 * BMCR data is stored in the ifmedia entry.
196			 */
197			PHY_WRITE(sc, MII_ANAR,
198			    mii_anar(ife->ifm_media));
199			PHY_WRITE(sc, MII_BMCR, ife->ifm_data);
200		}
201		break;
202
203	case MII_TICK:
204		/*
205		 * If we're not currently selected, just return.
206		 */
207		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
208			return (0);
209		if (mii_phy_tick(sc) == EJUSTRETURN)
210			return (0);
211		break;
212	}
213
214	/* Update the media status. */
215	ukphy_status(sc);
216	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_10_T)
217		mii->mii_media_active = IFM_ETHER|IFM_HPNA_1;
218
219	/* Callback if something changed. */
220	mii_phy_update(sc, cmd);
221	return (0);
222}
223