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 * $FreeBSD: src/sys/dev/mii/pnphy.c,v 1.1.2.1 2002/11/08 21:53:49 semenu Exp $
33 */
34
35 /*
36 * Pseudo-driver for media selection on the Lite-On PNIC 82c168
37 * chip. The NWAY support on this chip is horribly broken, so we
38 * only support manual mode selection. This is lame, but getting
39 * NWAY to work right is amazingly difficult.
40 */
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/socket.h>
46 #include <sys/errno.h>
47 #include <sys/module.h>
48 #include <sys/bus.h>
49
50 #include <net/if.h>
51 #include <net/if_arp.h>
52 #include <net/if_media.h>
53 #include <net/if_poll.h>
54
55 #include "mii.h"
56 #include "miivar.h"
57 #include "miidevs.h"
58
59 #include <machine/clock.h>
60
61 #include "../dc/if_dcreg.h"
62
63 #include "miibus_if.h"
64
65 #define DC_SETBIT(sc, reg, x) \
66 CSR_WRITE_4(sc, reg, \
67 CSR_READ_4(sc, reg) | x)
68
69 #define DC_CLRBIT(sc, reg, x) \
70 CSR_WRITE_4(sc, reg, \
71 CSR_READ_4(sc, reg) & ~x)
72
73 static int pnphy_probe (device_t);
74 static int pnphy_attach (device_t);
75
76 static device_method_t pnphy_methods[] = {
77 /* device interface */
78 DEVMETHOD(device_probe, pnphy_probe),
79 DEVMETHOD(device_attach, pnphy_attach),
80 DEVMETHOD(device_detach, ukphy_detach),
81 DEVMETHOD(device_shutdown, bus_generic_shutdown),
82 DEVMETHOD_END
83 };
84
85 static devclass_t pnphy_devclass;
86
87 static driver_t pnphy_driver = {
88 "pnphy",
89 pnphy_methods,
90 sizeof(struct mii_softc)
91 };
92
93 DRIVER_MODULE(pnphy, miibus, pnphy_driver, pnphy_devclass, NULL, NULL);
94
95 static int pnphy_service(struct mii_softc *, struct mii_data *, int);
96 static void pnphy_status(struct mii_softc *);
97
98 static int
pnphy_probe(device_t dev)99 pnphy_probe(device_t dev)
100 {
101 struct mii_attach_args *ma;
102
103 ma = device_get_ivars(dev);
104
105 /*
106 * The dc driver will report the 82c168 vendor and device
107 * ID to let us know that it wants us to attach.
108 */
109 if (ma->mii_id1 != DC_VENDORID_LO ||
110 ma->mii_id2 != DC_DEVICEID_82C168)
111 return(ENXIO);
112
113 device_set_desc(dev, "PNIC 82c168 media interface");
114
115 return (0);
116 }
117
118 static int
pnphy_attach(device_t dev)119 pnphy_attach(device_t dev)
120 {
121 struct mii_softc *sc;
122 struct mii_attach_args *ma;
123 struct mii_data *mii;
124
125 sc = device_get_softc(dev);
126 ma = device_get_ivars(dev);
127 mii_softc_init(sc, ma);
128 sc->mii_dev = device_get_parent(dev);
129 mii = device_get_softc(sc->mii_dev);
130 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
131
132 sc->mii_inst = mii->mii_instance;
133 sc->mii_service = pnphy_service;
134 sc->mii_pdata = mii;
135
136 sc->mii_flags |= MIIF_NOISOLATE;
137 mii->mii_instance++;
138
139 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
140
141 sc->mii_capabilities =
142 BMSR_100TXFDX|BMSR_100TXHDX|BMSR_10TFDX|BMSR_10THDX;
143 sc->mii_capabilities &= ma->mii_capmask;
144 device_printf(dev, " ");
145 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
146 kprintf("no media present");
147 else
148 mii_phy_add_media(sc);
149 kprintf("\n");
150
151 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
152 MII_MEDIA_NONE);
153
154 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst),
155 MII_MEDIA_100_TX);
156
157 #undef ADD
158
159 MIIBUS_MEDIAINIT(sc->mii_dev);
160 return(0);
161 }
162
163 static int
pnphy_service(struct mii_softc * sc,struct mii_data * mii,int cmd)164 pnphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
165 {
166 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
167
168 switch (cmd) {
169 case MII_POLLSTAT:
170 /*
171 * If we're not polling our PHY instance, just return.
172 */
173 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
174 return (0);
175 }
176 break;
177
178 case MII_MEDIACHG:
179 /*
180 * If the media indicates a different PHY instance,
181 * isolate ourselves.
182 */
183 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
184 return (0);
185
186 /*
187 * If the interface is not up, don't do anything.
188 */
189 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
190 break;
191
192 sc->mii_flags = 0;
193
194 switch (IFM_SUBTYPE(ife->ifm_media)) {
195 case IFM_AUTO:
196 /* NWAY is busted on this chip */
197 case IFM_100_T4:
198 /*
199 * XXX Not supported as a manual setting right now.
200 */
201 return (EINVAL);
202 case IFM_100_TX:
203 mii->mii_media_active = IFM_ETHER|IFM_100_TX;
204 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX)
205 mii->mii_media_active |= IFM_FDX;
206 MIIBUS_STATCHG(sc->mii_dev);
207 return(0);
208 break;
209 case IFM_10_T:
210 mii->mii_media_active = IFM_ETHER|IFM_10_T;
211 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX)
212 mii->mii_media_active |= IFM_FDX;
213 MIIBUS_STATCHG(sc->mii_dev);
214 return(0);
215 break;
216 default:
217 return(EINVAL);
218 break;
219 }
220 break;
221
222 case MII_TICK:
223 /*
224 * If we're not currently selected, just return.
225 */
226 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
227 return (0);
228
229 /*
230 * Is the interface even up?
231 */
232 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
233 return (0);
234
235 /*
236 * Only used for autonegotiation.
237 */
238 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
239 break;
240
241 /*
242 * This PHY's autonegotiation doesn't need to be kicked.
243 */
244 return(0);
245 }
246
247 /* Update the media status. */
248 pnphy_status(sc);
249
250 /* Callback if something changed. */
251 mii_phy_update(sc, cmd);
252 return (0);
253 }
254
255 static void
pnphy_status(struct mii_softc * sc)256 pnphy_status(struct mii_softc *sc)
257 {
258 struct mii_data *mii = sc->mii_pdata;
259 struct dc_softc *dc_sc = mii->mii_ifp->if_softc;
260 int reg;
261
262 mii->mii_media_status = IFM_AVALID;
263 mii->mii_media_active = IFM_ETHER;
264
265 reg = CSR_READ_4(dc_sc, DC_ISR);
266
267 if (!(reg & DC_ISR_LINKFAIL))
268 mii->mii_media_status |= IFM_ACTIVE;
269
270 if (CSR_READ_4(dc_sc, DC_NETCFG) & DC_NETCFG_SPEEDSEL)
271 mii->mii_media_active |= IFM_10_T;
272 else
273 mii->mii_media_active |= IFM_100_TX;
274 if (CSR_READ_4(dc_sc, DC_NETCFG) & DC_NETCFG_FULLDUPLEX)
275 mii->mii_media_active |= IFM_FDX;
276 }
277