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