rdcphy.c revision 1.3 1 1.3 christos /* $NetBSD: rdcphy.c,v 1.3 2019/02/24 17:22:21 christos Exp $ */
2 1.1 bouyer
3 1.1 bouyer /*-
4 1.1 bouyer * Copyright (c) 2010, Pyun YongHyeon <yongari (at) FreeBSD.org>
5 1.1 bouyer * All rights reserved.
6 1.1 bouyer *
7 1.1 bouyer * Redistribution and use in source and binary forms, with or without
8 1.1 bouyer * modification, are permitted provided that the following conditions
9 1.1 bouyer * are met:
10 1.1 bouyer * 1. Redistributions of source code must retain the above copyright
11 1.1 bouyer * notice unmodified, this list of conditions, and the following
12 1.1 bouyer * disclaimer.
13 1.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 bouyer * notice, this list of conditions and the following disclaimer in the
15 1.1 bouyer * documentation and/or other materials provided with the distribution.
16 1.1 bouyer *
17 1.1 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 1.1 bouyer * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 1.1 bouyer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.1 bouyer * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 1.1 bouyer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1 bouyer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 1.1 bouyer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.1 bouyer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.1 bouyer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1 bouyer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1 bouyer * SUCH DAMAGE.
28 1.1 bouyer */
29 1.1 bouyer
30 1.1 bouyer /* FreeBSD: src/sys/dev/mii/rdcphy.c,v 1.1 2010/12/30 23:50:25 yongari Exp */
31 1.1 bouyer
32 1.1 bouyer /*
33 1.1 bouyer * Driver for the RDC Semiconductor R6040 10/100 PHY.
34 1.1 bouyer */
35 1.1 bouyer #include <sys/cdefs.h>
36 1.3 christos __KERNEL_RCSID(0, "$NetBSD: rdcphy.c,v 1.3 2019/02/24 17:22:21 christos Exp $");
37 1.1 bouyer
38 1.1 bouyer #include <sys/param.h>
39 1.1 bouyer #include <sys/systm.h>
40 1.1 bouyer #include <sys/kernel.h>
41 1.1 bouyer #include <sys/device.h>
42 1.1 bouyer #include <sys/socket.h>
43 1.1 bouyer #include <sys/errno.h>
44 1.1 bouyer
45 1.1 bouyer #include <sys/bus.h>
46 1.1 bouyer
47 1.1 bouyer #include <net/if.h>
48 1.1 bouyer #include <net/if_media.h>
49 1.1 bouyer
50 1.1 bouyer #include <dev/mii/mii.h>
51 1.1 bouyer #include <dev/mii/miivar.h>
52 1.1 bouyer #include <dev/mii/miidevs.h>
53 1.1 bouyer
54 1.1 bouyer #include <dev/mii/rdcphyreg.h>
55 1.1 bouyer
56 1.1 bouyer struct rdcphy_softc {
57 1.1 bouyer struct mii_softc sc_mii;
58 1.1 bouyer int sc_mii_link_tick;
59 1.1 bouyer #define RDCPHY_MANNEG_TICK 3
60 1.1 bouyer };
61 1.1 bouyer
62 1.1 bouyer static int rdcphymatch(device_t, cfdata_t, void *);
63 1.1 bouyer static void rdcphyattach(device_t, device_t, void *);
64 1.1 bouyer
65 1.1 bouyer CFATTACH_DECL_NEW(rdcphy, sizeof(struct rdcphy_softc),
66 1.1 bouyer rdcphymatch, rdcphyattach, mii_phy_detach, mii_phy_activate);
67 1.1 bouyer
68 1.1 bouyer
69 1.1 bouyer static int rdcphy_service(struct mii_softc *, struct mii_data *, int);
70 1.1 bouyer static void rdcphy_status(struct mii_softc *);
71 1.1 bouyer
72 1.1 bouyer static const struct mii_phy_funcs rdcphy_funcs = {
73 1.1 bouyer rdcphy_service, rdcphy_status, mii_phy_reset,
74 1.1 bouyer };
75 1.1 bouyer
76 1.1 bouyer static const struct mii_phydesc rdcphys[] = {
77 1.3 christos MII_PHY_DESC(RDC, R6040),
78 1.3 christos MII_PHY_END,
79 1.1 bouyer };
80 1.1 bouyer
81 1.1 bouyer static int
82 1.1 bouyer rdcphymatch(device_t parent, cfdata_t match, void *aux)
83 1.1 bouyer {
84 1.1 bouyer struct mii_attach_args *ma = aux;
85 1.1 bouyer
86 1.1 bouyer if (mii_phy_match(ma, rdcphys) != NULL)
87 1.1 bouyer return (10);
88 1.1 bouyer
89 1.1 bouyer return (0);
90 1.1 bouyer }
91 1.1 bouyer
92 1.1 bouyer static void
93 1.1 bouyer rdcphyattach(device_t parent, device_t self, void *aux)
94 1.1 bouyer {
95 1.1 bouyer struct rdcphy_softc *rsc = device_private(self);
96 1.1 bouyer struct mii_softc *sc = &rsc->sc_mii;
97 1.1 bouyer struct mii_attach_args *ma = aux;
98 1.1 bouyer struct mii_data *mii = ma->mii_data;
99 1.1 bouyer
100 1.1 bouyer const struct mii_phydesc *mpd;
101 1.1 bouyer
102 1.1 bouyer mpd = mii_phy_match(ma, rdcphys);
103 1.1 bouyer aprint_naive(": Media interface\n");
104 1.1 bouyer aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
105 1.1 bouyer
106 1.1 bouyer sc->mii_dev = self;
107 1.1 bouyer sc->mii_inst = mii->mii_instance;
108 1.1 bouyer sc->mii_phy = ma->mii_phyno;
109 1.1 bouyer sc->mii_funcs = &rdcphy_funcs;
110 1.1 bouyer sc->mii_pdata = mii;
111 1.1 bouyer sc->mii_flags = ma->mii_flags;
112 1.1 bouyer sc->mii_anegticks = MII_ANEGTICKS;
113 1.1 bouyer
114 1.1 bouyer PHY_RESET(sc);
115 1.1 bouyer
116 1.2 msaitoh PHY_READ(sc, MII_BMSR, &sc->mii_capabilities);
117 1.2 msaitoh sc->mii_capabilities &= ma->mii_capmask;
118 1.1 bouyer if (sc->mii_capabilities & BMSR_EXTSTAT)
119 1.2 msaitoh PHY_READ(sc, MII_EXTSR, &sc->mii_extcapabilities);
120 1.1 bouyer aprint_normal_dev(self, "");
121 1.1 bouyer mii_phy_add_media(sc);
122 1.1 bouyer aprint_normal("\n");
123 1.1 bouyer
124 1.1 bouyer if (!pmf_device_register(self, NULL, mii_phy_resume))
125 1.1 bouyer aprint_error_dev(self, "couldn't establish power handler\n");
126 1.1 bouyer
127 1.1 bouyer }
128 1.1 bouyer
129 1.1 bouyer static int
130 1.1 bouyer rdcphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
131 1.1 bouyer {
132 1.1 bouyer struct rdcphy_softc *rsc = (struct rdcphy_softc *)sc;
133 1.1 bouyer struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
134 1.1 bouyer
135 1.1 bouyer switch (cmd) {
136 1.1 bouyer case MII_POLLSTAT:
137 1.1 bouyer break;
138 1.1 bouyer
139 1.1 bouyer case MII_MEDIACHG:
140 1.1 bouyer /*
141 1.1 bouyer * If the interface is not up, don't do anything.
142 1.1 bouyer */
143 1.1 bouyer if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
144 1.1 bouyer break;
145 1.1 bouyer
146 1.1 bouyer mii_phy_setmedia(sc);
147 1.1 bouyer switch (IFM_SUBTYPE(ife->ifm_media)) {
148 1.1 bouyer case IFM_100_TX:
149 1.1 bouyer case IFM_10_T:
150 1.1 bouyer /*
151 1.1 bouyer * Report fake lost link event to parent
152 1.1 bouyer * driver. This will stop MAC of parent
153 1.1 bouyer * driver and make it possible to reconfigure
154 1.1 bouyer * MAC after completion of link establishment.
155 1.1 bouyer * Note, the parent MAC seems to require
156 1.1 bouyer * restarting MAC when underlying any PHY
157 1.1 bouyer * configuration was changed even if the
158 1.1 bouyer * resolved speed/duplex was not changed at
159 1.1 bouyer * all.
160 1.1 bouyer */
161 1.1 bouyer mii->mii_media_status = 0;
162 1.1 bouyer mii->mii_media_active = IFM_ETHER | IFM_NONE;
163 1.1 bouyer rsc->sc_mii_link_tick = RDCPHY_MANNEG_TICK;
164 1.1 bouyer /* Immediately report link down. */
165 1.1 bouyer mii_phy_update(sc, MII_MEDIACHG);
166 1.1 bouyer return (0);
167 1.1 bouyer default:
168 1.1 bouyer break;
169 1.1 bouyer }
170 1.1 bouyer break;
171 1.1 bouyer
172 1.1 bouyer case MII_TICK:
173 1.1 bouyer if (mii_phy_tick(sc) == EJUSTRETURN)
174 1.1 bouyer return (0);
175 1.1 bouyer if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
176 1.1 bouyer /*
177 1.1 bouyer * It seems the PHY hardware does not correctly
178 1.1 bouyer * report link status changes when manual link
179 1.1 bouyer * configuration is in progress. It is also
180 1.1 bouyer * possible for the PHY to complete establishing
181 1.1 bouyer * a link within one second such that mii(4)
182 1.1 bouyer * did not notice the link change. To workaround
183 1.1 bouyer * the issue, emulate lost link event and wait
184 1.1 bouyer * for 3 seconds when manual link configuration
185 1.1 bouyer * is in progress. 3 seconds would be long
186 1.1 bouyer * enough to absorb transient link flips.
187 1.1 bouyer */
188 1.1 bouyer if (rsc->sc_mii_link_tick > 0) {
189 1.1 bouyer rsc->sc_mii_link_tick--;
190 1.1 bouyer return (0);
191 1.1 bouyer }
192 1.1 bouyer }
193 1.1 bouyer break;
194 1.1 bouyer }
195 1.1 bouyer
196 1.1 bouyer /* Update the media status. */
197 1.1 bouyer rdcphy_status(sc);
198 1.1 bouyer
199 1.1 bouyer /* Callback if something changed. */
200 1.1 bouyer mii_phy_update(sc, cmd);
201 1.1 bouyer return (0);
202 1.1 bouyer }
203 1.1 bouyer
204 1.1 bouyer static void
205 1.1 bouyer rdcphy_status(struct mii_softc *sc)
206 1.1 bouyer {
207 1.1 bouyer struct mii_data *mii = sc->mii_pdata;
208 1.2 msaitoh uint16_t bmsr, bmcr, physts;
209 1.1 bouyer
210 1.1 bouyer mii->mii_media_status = IFM_AVALID;
211 1.1 bouyer mii->mii_media_active = IFM_ETHER;
212 1.1 bouyer
213 1.2 msaitoh PHY_READ(sc, MII_BMSR, &bmsr);
214 1.2 msaitoh PHY_READ(sc, MII_BMSR, &bmsr);
215 1.2 msaitoh PHY_READ(sc, MII_RDCPHY_STATUS, &physts);
216 1.1 bouyer
217 1.1 bouyer if ((physts & STATUS_LINK_UP) != 0)
218 1.1 bouyer mii->mii_media_status |= IFM_ACTIVE;
219 1.1 bouyer
220 1.2 msaitoh PHY_READ(sc, MII_BMCR, &bmcr);
221 1.1 bouyer if ((bmcr & BMCR_ISO) != 0) {
222 1.1 bouyer mii->mii_media_active |= IFM_NONE;
223 1.1 bouyer mii->mii_media_status = 0;
224 1.1 bouyer return;
225 1.1 bouyer }
226 1.1 bouyer
227 1.1 bouyer if ((bmcr & BMCR_LOOP) != 0)
228 1.1 bouyer mii->mii_media_active |= IFM_LOOP;
229 1.1 bouyer
230 1.1 bouyer if ((bmcr & BMCR_AUTOEN) != 0) {
231 1.1 bouyer if ((bmsr & BMSR_ACOMP) == 0) {
232 1.1 bouyer /* Erg, still trying, I guess... */
233 1.1 bouyer mii->mii_media_active |= IFM_NONE;
234 1.1 bouyer return;
235 1.1 bouyer }
236 1.1 bouyer }
237 1.1 bouyer
238 1.1 bouyer switch (physts & STATUS_SPEED_MASK) {
239 1.1 bouyer case STATUS_SPEED_100:
240 1.1 bouyer mii->mii_media_active |= IFM_100_TX;
241 1.1 bouyer break;
242 1.1 bouyer case STATUS_SPEED_10:
243 1.1 bouyer mii->mii_media_active |= IFM_10_T;
244 1.1 bouyer break;
245 1.1 bouyer default:
246 1.1 bouyer mii->mii_media_active |= IFM_NONE;
247 1.1 bouyer return;
248 1.1 bouyer }
249 1.1 bouyer if ((physts & STATUS_FULL_DUPLEX) != 0)
250 1.1 bouyer mii->mii_media_active |= IFM_FDX | mii_phy_flowstatus(sc);
251 1.1 bouyer else
252 1.1 bouyer mii->mii_media_active |= IFM_HDX;
253 1.1 bouyer }
254