kernel: Use NULL for DRIVER_MODULE()'s evh & arg (which are pointers).
[dragonfly.git] / sys / dev / netif / mii_layer / ip1000phy.c
CommitLineData
1da2c9db
SZ
1/*-
2 * Copyright (c) 2006, Pyun YongHyeon <yongari@FreeBSD.org>
3 * 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 unmodified, this list of conditions, and the following
10 * 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 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD: src/sys/dev/mii/ip1000phy.c,v 1.1 2006/07/25 00:16:09 yongari Exp $
1da2c9db
SZ
28 */
29
30/*
31 * Driver for the IC Plus IP1000A 10/100/1000 PHY.
32 */
33
34#include <sys/param.h>
35#include <sys/bus.h>
36#include <sys/kernel.h>
37#include <sys/module.h>
38#include <sys/socket.h>
39#include <sys/sysctl.h>
40
41#include <net/if.h>
42#include <net/if_arp.h>
43#include <net/if_media.h>
44
45#include <dev/netif/mii_layer/mii.h>
46#include <dev/netif/mii_layer/miivar.h>
47#include "miidevs.h"
48
49#include <dev/netif/mii_layer/ip1000phyreg.h>
50
51#include "miibus_if.h"
52
53#include <dev/netif/stge/if_stgereg.h>
54#include <dev/netif/stge/if_stgevar.h>
55
56static int ip1000phy_probe(device_t);
57static int ip1000phy_attach(device_t);
58static int ip1000phy_service(struct mii_softc *, struct mii_data *, int);
59static void ip1000phy_status(struct mii_softc *);
60static void ip1000phy_reset(struct mii_softc *);
61static int ip1000phy_mii_phy_auto(struct mii_softc *);
62
63static device_method_t ip1000phy_methods[] = {
64 /* device interface */
65 DEVMETHOD(device_probe, ip1000phy_probe),
66 DEVMETHOD(device_attach, ip1000phy_attach),
67 DEVMETHOD(device_detach, ukphy_detach),
68 DEVMETHOD(device_shutdown, bus_generic_shutdown),
69 { 0, 0 }
70};
71
72static devclass_t ip1000phy_devclass;
73static driver_t ip1000phy_driver = {
74 "ip1000phy",
75 ip1000phy_methods,
76 sizeof (struct mii_softc)
77};
78
aa2b9d05 79DRIVER_MODULE(ip1000phy, miibus, ip1000phy_driver, ip1000phy_devclass, NULL, NULL);
1da2c9db
SZ
80
81static const struct mii_phydesc ip1000phys[] = {
82 MII_PHYDESC(ICPLUS, IP1000A),
83 MII_PHYDESC_NULL
84};
85
86static int
87ip1000phy_probe(device_t dev)
88{
89 struct mii_attach_args *ma;
90 const struct mii_phydesc *mpd;
91
92 ma = device_get_ivars(dev);
93 mpd = mii_phy_match(ma, ip1000phys);
94 if (mpd != NULL) {
95 device_set_desc(dev, mpd->mpd_name);
96 return (0);
97 }
98
99 return (ENXIO);
100}
101
102static int
103ip1000phy_attach(device_t dev)
104{
105 struct mii_softc *sc;
106 struct mii_attach_args *ma;
107 struct mii_data *mii;
108
109 sc = device_get_softc(dev);
110 ma = device_get_ivars(dev);
111 mii_softc_init(sc, ma);
112 sc->mii_dev = device_get_parent(dev);
113 mii = device_get_softc(sc->mii_dev);
114 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
115
116 sc->mii_inst = mii->mii_instance;
117 sc->mii_service = ip1000phy_service;
118 sc->mii_pdata = mii;
119
120 sc->mii_anegticks = MII_ANEGTICKS_GIGE;
121 sc->mii_flags |= MIIF_NOISOLATE;
122
123 mii->mii_instance++;
124
125 device_printf(dev, " ");
126
127#define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
128
129 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
130 BMCR_ISO);
131
132 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
133 IP1000PHY_BMCR_10);
e3869ec7 134 kprintf("10baseT, ");
1da2c9db
SZ
135 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
136 IP1000PHY_BMCR_10 | IP1000PHY_BMCR_FDX);
e3869ec7 137 kprintf("10baseT-FDX, ");
1da2c9db
SZ
138 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
139 IP1000PHY_BMCR_100);
e3869ec7 140 kprintf("100baseTX, ");
1da2c9db
SZ
141 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
142 IP1000PHY_BMCR_100 | IP1000PHY_BMCR_FDX);
e3869ec7 143 kprintf("100baseTX-FDX, ");
1da2c9db
SZ
144 /* 1000baseT half-duplex, really supported? */
145 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 0, sc->mii_inst),
146 IP1000PHY_BMCR_1000);
9bbc5585 147 kprintf("1000baseT, ");
1da2c9db
SZ
148 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX, sc->mii_inst),
149 IP1000PHY_BMCR_1000 | IP1000PHY_BMCR_FDX);
9bbc5585 150 kprintf("1000baseT-FDX, ");
1da2c9db 151 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst), 0);
e3869ec7 152 kprintf("auto\n");
1da2c9db
SZ
153#undef ADD
154
155 ip1000phy_reset(sc);
156
157 MIIBUS_MEDIAINIT(sc->mii_dev);
158 return(0);
159}
160
161static int
162ip1000phy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
163{
164 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
165 uint32_t gig, reg, speed;
166
167 switch (cmd) {
168 case MII_POLLSTAT:
169 /*
170 * If we're not polling our PHY instance, just return.
171 */
172 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
173 return (0);
174 break;
175
176 case MII_MEDIACHG:
177 /*
178 * If the media indicates a different PHY instance,
179 * isolate ourselves.
180 */
181 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
182 reg = PHY_READ(sc, IP1000PHY_MII_BMCR);
183 PHY_WRITE(sc, IP1000PHY_MII_BMCR,
184 reg | IP1000PHY_BMCR_ISO);
185 return (0);
186 }
187
188 /*
189 * If the interface is not up, don't do anything.
190 */
191 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
192 break;
193
194 ip1000phy_reset(sc);
195 switch (IFM_SUBTYPE(ife->ifm_media)) {
196 case IFM_AUTO:
197 ip1000phy_mii_phy_auto(sc);
198 goto done;
199
200 case IFM_1000_T:
201 /*
202 * XXX
203 * Manual 1000baseT setting doesn't seem to work.
204 */
205 speed = IP1000PHY_BMCR_1000;
206 break;
207
208 case IFM_100_TX:
209 speed = IP1000PHY_BMCR_100;
210 break;
211
212 case IFM_10_T:
213 speed = IP1000PHY_BMCR_10;
214 break;
215
216 default:
217 return (EINVAL);
218 }
219
220 if (((ife->ifm_media & IFM_GMASK) & IFM_FDX) != 0) {
221 speed |= IP1000PHY_BMCR_FDX;
222 gig = IP1000PHY_1000CR_1000T_FDX;
223 } else {
224 gig = IP1000PHY_1000CR_1000T;
225 }
226
227 PHY_WRITE(sc, IP1000PHY_MII_1000CR, 0);
228 PHY_WRITE(sc, IP1000PHY_MII_BMCR, speed);
229
230 if (IFM_SUBTYPE(ife->ifm_media) != IFM_1000_T)
231 break;
232
233 PHY_WRITE(sc, IP1000PHY_MII_1000CR, gig);
234 PHY_WRITE(sc, IP1000PHY_MII_BMCR, speed);
235
236 /*
237 * When settning the link manually, one side must
238 * be the master and the other the slave. However
239 * ifmedia doesn't give us a good way to specify
240 * this, so we fake it by using one of the LINK
241 * flags. If LINK0 is set, we program the PHY to
242 * be a master, otherwise it's a slave.
243 */
244 if ((mii->mii_ifp->if_flags & IFF_LINK0)) {
245 PHY_WRITE(sc, IP1000PHY_MII_1000CR, gig |
246 IP1000PHY_1000CR_MASTER |
247 IP1000PHY_1000CR_MMASTER |
248 IP1000PHY_1000CR_MANUAL);
249 } else {
250 PHY_WRITE(sc, IP1000PHY_MII_1000CR, gig |
251 IP1000PHY_1000CR_MASTER |
252 IP1000PHY_1000CR_MANUAL);
253 }
254
255done:
256 break;
257
258 case MII_TICK:
259 /*
260 * If we're not currently selected, just return.
261 */
262 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
263 return (0);
264 /*
265 * Is the interface even up?
266 */
267 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
268 return (0);
269
270 /*
271 * Only used for autonegotiation.
272 */
12b5e22d 273 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
1da2c9db 274 break;
1da2c9db
SZ
275
276 /*
277 * check for link.
278 */
279 reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
280 if (reg & BMSR_LINK) {
281 sc->mii_ticks = 0;
282 break;
283 }
284
285 /* Announce link loss right after it happens */
286 if (sc->mii_ticks++ == 0)
287 break;
288
289 /*
290 * Only retry autonegotiation every mii_anegticks seconds.
291 */
292 if (sc->mii_ticks <= sc->mii_anegticks)
293 return (0);
294
295 sc->mii_ticks = 0;
296 ip1000phy_mii_phy_auto(sc);
297 break;
298 }
299
300 /* Update the media status. */
301 ip1000phy_status(sc);
302
303 /* Callback if something changed. */
304 mii_phy_update(sc, cmd);
305 return (0);
306}
307
308static void
309ip1000phy_status(struct mii_softc *sc)
310{
311 struct mii_data *mii = sc->mii_pdata;
312 uint32_t bmsr, bmcr, stat;
313 uint32_t ar, lpar;
314
315 mii->mii_media_status = IFM_AVALID;
316 mii->mii_media_active = IFM_ETHER;
317
318 bmsr = PHY_READ(sc, IP1000PHY_MII_BMSR) |
319 PHY_READ(sc, IP1000PHY_MII_BMSR);
320 if ((bmsr & IP1000PHY_BMSR_LINK) != 0)
321 mii->mii_media_status |= IFM_ACTIVE;
322
323 bmcr = PHY_READ(sc, IP1000PHY_MII_BMCR);
324 if ((bmcr & IP1000PHY_BMCR_LOOP) != 0)
325 mii->mii_media_active |= IFM_LOOP;
326
327 if ((bmcr & IP1000PHY_BMCR_AUTOEN) != 0) {
328 if ((bmsr & IP1000PHY_BMSR_ANEGCOMP) == 0) {
329 /* Erg, still trying, I guess... */
330 mii->mii_media_active |= IFM_NONE;
331 return;
332 }
333 }
334
335 stat = PHY_READ(sc, STGE_PhyCtrl);
336 switch (PC_LinkSpeed(stat)) {
337 case PC_LinkSpeed_Down:
338 mii->mii_media_active |= IFM_NONE;
339 return;
340 case PC_LinkSpeed_10:
341 mii->mii_media_active |= IFM_10_T;
342 break;
343 case PC_LinkSpeed_100:
344 mii->mii_media_active |= IFM_100_TX;
345 break;
346 case PC_LinkSpeed_1000:
347 mii->mii_media_active |= IFM_1000_T;
348 break;
349 }
350 if ((stat & PC_PhyDuplexStatus) != 0)
351 mii->mii_media_active |= IFM_FDX;
352 else
353 mii->mii_media_active |= IFM_HDX;
354
355 ar = PHY_READ(sc, IP1000PHY_MII_ANAR);
356 lpar = PHY_READ(sc, IP1000PHY_MII_ANLPAR);
357
358 /*
359 * FLAG0 : Rx flow-control
360 * FLAG1 : Tx flow-control
361 */
362 if ((ar & IP1000PHY_ANAR_PAUSE) && (lpar & IP1000PHY_ANLPAR_PAUSE))
363 mii->mii_media_active |= IFM_FLAG0 | IFM_FLAG1;
364 else if (!(ar & IP1000PHY_ANAR_PAUSE) && (ar & IP1000PHY_ANAR_APAUSE) &&
365 (lpar & IP1000PHY_ANLPAR_PAUSE) && (lpar & IP1000PHY_ANLPAR_APAUSE))
366 mii->mii_media_active |= IFM_FLAG1;
367 else if ((ar & IP1000PHY_ANAR_PAUSE) && (ar & IP1000PHY_ANAR_APAUSE) &&
368 !(lpar & IP1000PHY_ANLPAR_PAUSE) &&
369 (lpar & IP1000PHY_ANLPAR_APAUSE)) {
370 mii->mii_media_active |= IFM_FLAG0;
371 }
372
373 /*
374 * FLAG2 : local PHY resolved to MASTER
375 */
376 if ((mii->mii_media_active & IFM_1000_T) != 0) {
377 stat = PHY_READ(sc, IP1000PHY_MII_1000SR);
378 if ((stat & IP1000PHY_1000SR_MASTER) != 0)
379 mii->mii_media_active |= IFM_FLAG2;
380 }
381}
382
383static int
384ip1000phy_mii_phy_auto(struct mii_softc *mii)
385{
386 uint32_t reg;
387
388 PHY_WRITE(mii, IP1000PHY_MII_ANAR,
389 IP1000PHY_ANAR_10T | IP1000PHY_ANAR_10T_FDX |
390 IP1000PHY_ANAR_100TX | IP1000PHY_ANAR_100TX_FDX |
391 IP1000PHY_ANAR_PAUSE | IP1000PHY_ANAR_APAUSE);
392 reg = IP1000PHY_1000CR_1000T | IP1000PHY_1000CR_1000T_FDX;
393 reg |= IP1000PHY_1000CR_MASTER;
394 PHY_WRITE(mii, IP1000PHY_MII_1000CR, reg);
395 PHY_WRITE(mii, IP1000PHY_MII_BMCR, (IP1000PHY_BMCR_FDX |
396 IP1000PHY_BMCR_AUTOEN | IP1000PHY_BMCR_STARTNEG));
397
398 return (EJUSTRETURN);
399}
400
401static void
402ip1000phy_load_dspcode(struct mii_softc *sc)
403{
404
405 PHY_WRITE(sc, 31, 0x0001);
406 PHY_WRITE(sc, 27, 0x01e0);
407 PHY_WRITE(sc, 31, 0x0002);
408 PHY_WRITE(sc, 27, 0xeb8e);
409 PHY_WRITE(sc, 31, 0x0000);
410 PHY_WRITE(sc, 30, 0x005e);
411 PHY_WRITE(sc, 9, 0x0700);
412
413 DELAY(50);
414}
415
416static void
417ip1000phy_reset(struct mii_softc *sc)
418{
419 struct stge_softc *stge_sc;
420 struct mii_data *mii;
421 device_t parent;
422 uint32_t reg;
423
424 mii_phy_reset(sc);
425
426 /* clear autoneg/full-duplex as we don't want it after reset */
427 reg = PHY_READ(sc, IP1000PHY_MII_BMCR);
428 reg &= ~(IP1000PHY_BMCR_AUTOEN | IP1000PHY_BMCR_FDX);
429 PHY_WRITE(sc, MII_BMCR, reg);
430
431 mii = sc->mii_pdata;
432 /*
433 * XXX There should be more general way to pass PHY specific
434 * data via mii interface.
435 */
436 parent = device_get_parent(sc->mii_dev);
437 if (strncmp(device_get_name(parent), "stge", 4) == 0) {
438 stge_sc = device_get_softc(parent);
439 if (stge_sc->sc_rev >= 0x40 && stge_sc->sc_rev <= 0x4e)
440 ip1000phy_load_dspcode(sc);
441 }
442}