xref: /netbsd/sys/dev/mii/pnaphy.c (revision bf9ec67e)
1 /*	$NetBSD: pnaphy.c,v 1.4 2002/03/25 20:51:25 thorpej Exp $	*/
2 
3 /*
4  * Copyright 2001 Wasabi Systems, Inc.
5  * All rights reserved.
6  *
7  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed for the NetBSD Project by
20  *	Wasabi Systems, Inc.
21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22  *    or promote products derived from this software without specific prior
23  *    written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 /*
39  * Driver for generic HomePNA 1.0 PHYs.
40  *
41  * HomePNA 1.0 PHYs are pretty simple -- they support exactly one
42  * media type, and don't do autonegotiation.
43  *
44  * HomePNA PHYs often have vendor-specific registers for tuning
45  * the network connection, but we don't deal with any of that
46  * at all.
47  */
48 
49 #include <sys/cdefs.h>
50 __KERNEL_RCSID(0, "$NetBSD: pnaphy.c,v 1.4 2002/03/25 20:51:25 thorpej Exp $");
51 
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/device.h>
56 #include <sys/malloc.h>
57 #include <sys/socket.h>
58 #include <sys/errno.h>
59 
60 #include <net/if.h>
61 #include <net/if_media.h>
62 
63 #include <dev/mii/mii.h>
64 #include <dev/mii/miivar.h>
65 #include <dev/mii/miidevs.h>
66 
67 int	pnaphymatch(struct device *, struct cfdata *, void *);
68 void	pnaphyattach(struct device *, struct device *, void *);
69 
70 struct cfattach pnaphy_ca = {
71 	sizeof(struct mii_softc), pnaphymatch, pnaphyattach,
72 	    mii_phy_detach, mii_phy_activate
73 };
74 
75 int	pnaphy_service(struct mii_softc *, struct mii_data *, int);
76 void	pnaphy_status(struct mii_softc *);
77 
78 const struct mii_phy_funcs pnaphy_funcs = {
79 	pnaphy_service, pnaphy_status, mii_phy_reset,
80 };
81 
82 const struct mii_phydesc pnaphys[] = {
83 	{ MII_OUI_yyAMD,		MII_MODEL_yyAMD_79c901home,
84 	  MII_STR_yyAMD_79c901home },
85 
86 	{ 0,				0,
87 	  NULL },
88 };
89 
90 int
91 pnaphymatch(struct device *parent, struct cfdata *match, void *aux)
92 {
93 	struct mii_attach_args *ma = aux;
94 
95 	if (mii_phy_match(ma, pnaphys) != NULL) {
96 		/*
97 		 * Match higher than ukphy, but lower than a specific
98 		 * driver would match.
99 		 */
100 		return (2);
101 	}
102 
103 	return (0);
104 }
105 
106 void
107 pnaphyattach(struct device *parent, struct device *self, void *aux)
108 {
109 	struct mii_softc *sc = (struct mii_softc *)self;
110 	struct mii_attach_args *ma = aux;
111 	struct mii_data *mii = ma->mii_data;
112 	const struct mii_phydesc *mpd;
113 
114 	mpd = mii_phy_match(ma, pnaphys);
115 	printf(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
116 
117 	sc->mii_inst = mii->mii_instance;
118 	sc->mii_phy = ma->mii_phyno;
119 	sc->mii_funcs = &pnaphy_funcs;
120 	sc->mii_pdata = mii;
121 	sc->mii_flags = ma->mii_flags;
122 	sc->mii_anegticks = 5;
123 
124 	PHY_RESET(sc);
125 
126 	sc->mii_capabilities =
127 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
128 	printf("%s: ", sc->mii_dev.dv_xname);
129 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
130 		printf("no media present");
131 	else
132 		mii_phy_add_media(sc);
133 	printf("\n");
134 }
135 
136 int
137 pnaphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
138 {
139 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
140 	int reg;
141 
142 	switch (cmd) {
143 	case MII_POLLSTAT:
144 		/*
145 		 * If we're not polling our PHY instance, just return.
146 		 */
147 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
148 			return (0);
149 		break;
150 
151 	case MII_MEDIACHG:
152 		/*
153 		 * If the media indicates a different PHY instance,
154 		 * isolate ourselves.
155 		 */
156 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
157 			reg = PHY_READ(sc, MII_BMCR);
158 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
159 			return (0);
160 		}
161 
162 		/*
163 		 * If the interface is not up, don't do anything.
164 		 */
165 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
166 			break;
167 
168 		mii_phy_setmedia(sc);
169 		break;
170 
171 	case MII_TICK:
172 		/*
173 		 * If we're not currently selected, just return.
174 		 */
175 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
176 			return (0);
177 
178 		if (mii_phy_tick(sc) == EJUSTRETURN)
179 			return (0);
180 		break;
181 
182 	case MII_DOWN:
183 		mii_phy_down(sc);
184 		return (0);
185 	}
186 
187 	/* Update the media status. */
188 	mii_phy_status(sc);
189 
190 	/* Callback if something changed. */
191 	mii_phy_update(sc, cmd);
192 	return (0);
193 }
194 
195 void
196 pnaphy_status(struct mii_softc *sc)
197 {
198 	struct mii_data *mii = sc->mii_pdata;
199 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
200 	int bmsr, bmcr;
201 
202 	mii->mii_media_status = IFM_AVALID;
203 	mii->mii_media_active = IFM_ETHER;
204 
205 	bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
206 
207 	if (bmsr & BMSR_LINK)
208 		mii->mii_media_status |= IFM_ACTIVE;
209 
210 	bmcr = PHY_READ(sc, MII_BMCR);
211 	if (bmcr & BMCR_ISO) {
212 		mii->mii_media_active |= IFM_NONE;
213 		mii->mii_media_status = 0;
214 		return;
215 	}
216 
217 	if (bmcr & BMCR_LOOP)
218 		mii->mii_media_active |= IFM_LOOP;
219 
220 	/*
221 	 * On HomePNA PHYs, the current media is always the selected
222 	 * media.
223 	 *
224 	 * XXX May not be the case w/ HomePNA 2.
225 	 */
226 	mii->mii_media_active = ife->ifm_media;
227 }
228