xref: /dragonfly/sys/dev/netif/mii_layer/pnaphy.c (revision 0db87cb7)
1 /*
2  * Copyright (c) 2000 Berkeley Software Design, Inc.
3  * Copyright (c) 1997, 1998, 1999, 2000
4  *	Bill Paul <wpaul@osd.bsdi.com>.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following 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  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Bill Paul.
17  * 4. Neither the name of the author nor the names of any co-contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31  * THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * $FreeBSD: src/sys/dev/mii/pnaphy.c,v 1.1.2.3 2002/11/08 21:53:49 semenu Exp $
34  */
35 
36 /*
37  * driver for homePNA PHYs
38  * This is really just a stub that allows us to identify homePNA-based
39  * transceicers and display the link status. MII-based homePNA PHYs
40  * only support one media type and no autonegotiation. If we were
41  * really clever, we could tweak some of the vendor-specific registers
42  * to optimize the link.
43  */
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/socket.h>
49 #include <sys/errno.h>
50 #include <sys/module.h>
51 #include <sys/bus.h>
52 
53 #include <net/if.h>
54 #include <net/if_media.h>
55 
56 #include <machine/clock.h>
57 
58 #include "mii.h"
59 #include "miivar.h"
60 #include "miidevs.h"
61 
62 #include "miibus_if.h"
63 
64 static int pnaphy_probe		(device_t);
65 static int pnaphy_attach	(device_t);
66 
67 static device_method_t pnaphy_methods[] = {
68 	/* device interface */
69 	DEVMETHOD(device_probe,		pnaphy_probe),
70 	DEVMETHOD(device_attach,	pnaphy_attach),
71 	DEVMETHOD(device_detach,	ukphy_detach),
72 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
73 	DEVMETHOD_END
74 };
75 
76 static const struct mii_phydesc pnaphys[] = {
77 	MII_PHYDESC(AMD,	79c978),
78 	MII_PHYDESC_NULL
79 };
80 
81 static devclass_t pnaphy_devclass;
82 
83 static driver_t pnaphy_driver = {
84 	"pnaphy",
85 	pnaphy_methods,
86 	sizeof(struct mii_softc)
87 };
88 
89 DRIVER_MODULE(pnaphy, miibus, pnaphy_driver, pnaphy_devclass, NULL, NULL);
90 
91 static int	pnaphy_service(struct mii_softc *, struct mii_data *, int);
92 
93 static int
94 pnaphy_probe(device_t dev)
95 {
96 	struct mii_attach_args *ma = device_get_ivars(dev);
97 	const struct mii_phydesc *mpd;
98 
99 	mpd = mii_phy_match(ma, pnaphys);
100 	if (mpd != NULL) {
101 		device_set_desc(dev, mpd->mpd_name);
102 		return (0);
103 	}
104 	return(ENXIO);
105 }
106 
107 static int
108 pnaphy_attach(device_t dev)
109 {
110 	struct mii_softc *sc;
111 	struct mii_attach_args *ma;
112 	struct mii_data *mii;
113 
114 	sc = device_get_softc(dev);
115 	ma = device_get_ivars(dev);
116 	mii_softc_init(sc, ma);
117 	sc->mii_dev = device_get_parent(dev);
118 	mii = device_get_softc(sc->mii_dev);
119 	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
120 
121 	sc->mii_inst = mii->mii_instance;
122 	sc->mii_service = pnaphy_service;
123 	sc->mii_pdata = mii;
124 
125 	mii->mii_instance++;
126 
127 	sc->mii_flags |= MIIF_NOISOLATE |
128 			 MIIF_IS_HPNA;		/* force HomePNA */
129 
130 	mii_phy_reset(sc);
131 
132 	sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
133 
134 	device_printf(dev, " ");
135 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
136 		kprintf("no media present");
137 	else
138 		mii_phy_add_media(sc);
139 	kprintf("\n");
140 
141 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
142 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
143 	    MII_MEDIA_NONE);
144 #undef ADD
145 
146 	MIIBUS_MEDIAINIT(sc->mii_dev);
147 	return(0);
148 }
149 
150 static int
151 pnaphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
152 {
153 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
154 	int reg;
155 
156 	switch (cmd) {
157 	case MII_POLLSTAT:
158 		/*
159 		 * If we're not polling our PHY instance, just return.
160 		 */
161 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
162 			return (0);
163 		break;
164 
165 	case MII_MEDIACHG:
166 		/*
167 		 * If the media indicates a different PHY instance,
168 		 * isolate ourselves.
169 		 */
170 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
171 			reg = PHY_READ(sc, MII_BMCR);
172 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
173 			return (0);
174 		}
175 
176 		/*
177 		 * If the interface is not up, don't do anything.
178 		 */
179 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
180 			break;
181 
182 		switch (IFM_SUBTYPE(ife->ifm_media)) {
183 		case IFM_AUTO:
184 		case IFM_10_T:
185 		case IFM_100_TX:
186 		case IFM_100_T4:
187 			return (EINVAL);
188 		default:
189 			mii_phy_set_media(sc);
190 		}
191 		break;
192 
193 	case MII_TICK:
194 		/*
195 		 * If we're not currently selected, just return.
196 		 */
197 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
198 			return (0);
199 
200 		if (mii_phy_tick(sc) == EJUSTRETURN)
201 			return (0);
202 		break;
203 	}
204 
205 	/* Update the media status. */
206 	ukphy_status(sc);
207 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_10_T)
208 		mii->mii_media_active = IFM_ETHER | IFM_HPNA_1;
209 
210 	/* Callback if something changed. */
211 	mii_phy_update(sc, cmd);
212 	return (0);
213 }
214