mvphy.c revision 1.7 1 1.7 xtraeme /* $NetBSD: mvphy.c,v 1.7 2008/05/04 17:06:10 xtraeme Exp $ */
2 1.1 gdamore
3 1.1 gdamore /*-
4 1.1 gdamore * Copyright (c) 2006 Sam Leffler, Errno Consulting
5 1.1 gdamore * All rights reserved.
6 1.1 gdamore *
7 1.1 gdamore * Redistribution and use in source and binary forms, with or without
8 1.1 gdamore * modification, are permitted provided that the following conditions
9 1.1 gdamore * are met:
10 1.1 gdamore * 1. Redistributions of source code must retain the above copyright
11 1.1 gdamore * notice, this list of conditions and the following disclaimer.
12 1.1 gdamore * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 gdamore * notice, this list of conditions and the following disclaimer in the
14 1.1 gdamore * documentation and/or other materials provided with the distribution.
15 1.1 gdamore *
16 1.1 gdamore * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 gdamore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 gdamore * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 gdamore * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 gdamore * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 gdamore * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 gdamore * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 gdamore * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 gdamore * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 gdamore * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 gdamore * SUCH DAMAGE.
27 1.1 gdamore */
28 1.1 gdamore
29 1.1 gdamore /*
30 1.1 gdamore * Driver for Marvell 88E6060 10/100 5-port PHY switch.
31 1.1 gdamore */
32 1.1 gdamore
33 1.1 gdamore #include <sys/cdefs.h>
34 1.7 xtraeme __KERNEL_RCSID(0, "$NetBSD: mvphy.c,v 1.7 2008/05/04 17:06:10 xtraeme Exp $");
35 1.1 gdamore
36 1.1 gdamore #include <sys/param.h>
37 1.1 gdamore #include <sys/systm.h>
38 1.1 gdamore #include <sys/kernel.h>
39 1.1 gdamore #include <sys/device.h>
40 1.1 gdamore #include <sys/socket.h>
41 1.1 gdamore #include <sys/errno.h>
42 1.1 gdamore
43 1.1 gdamore #include <net/if.h>
44 1.1 gdamore #include <net/if_media.h>
45 1.1 gdamore
46 1.1 gdamore #include <dev/mii/mii.h>
47 1.1 gdamore #include <dev/mii/miivar.h>
48 1.1 gdamore #include <dev/mii/miidevs.h>
49 1.1 gdamore
50 1.1 gdamore #include <dev/mii/mvphyreg.h>
51 1.1 gdamore
52 1.1 gdamore #define MV_PORT(sc) ((sc)->mii_phy - 16) /* PHY # to switch port */
53 1.1 gdamore #define MV_CPU_PORT 5 /* port # of CPU port */
54 1.1 gdamore
55 1.1 gdamore #define MV_READ(p, phy, r) \
56 1.1 gdamore (*(p)->mii_pdata->mii_readreg)(device_parent(&(p)->mii_dev), \
57 1.1 gdamore phy, (r))
58 1.1 gdamore #define MV_WRITE(p, phy, r, v) \
59 1.1 gdamore (*(p)->mii_pdata->mii_writereg)(device_parent(&(p)->mii_dev), \
60 1.1 gdamore phy, (r), (v))
61 1.1 gdamore
62 1.1 gdamore /* XXX sysctl'able */
63 1.1 gdamore #define MV_ATUCTRL_ATU_SIZE_DEFAULT 2 /* 1024 entry database */
64 1.1 gdamore #define MV_ATUCTRL_AGE_TIME_DEFAULT 19 /* 19 * 16 = 304 seconds */
65 1.1 gdamore
66 1.1 gdamore /*
67 1.1 gdamore * Register manipulation macros that expect bit field defines
68 1.1 gdamore * to follow the convention that an _S suffix is appended for
69 1.1 gdamore * a shift count, while the field mask has no suffix.
70 1.1 gdamore */
71 1.1 gdamore #define SM(_v, _f) (((_v) << _f##_S) & _f)
72 1.1 gdamore #define MS(_v, _f) (((_v) & _f) >> _f##_S)
73 1.1 gdamore
74 1.7 xtraeme static int mvphymatch(device_t, cfdata_t, void *);
75 1.7 xtraeme static void mvphyattach(device_t, device_t, void *);
76 1.1 gdamore
77 1.7 xtraeme CFATTACH_DECL_NEW(mvphy, sizeof(struct mii_softc),
78 1.1 gdamore mvphymatch, mvphyattach, mii_phy_detach, mii_phy_activate);
79 1.1 gdamore
80 1.1 gdamore static int mvphy_service(struct mii_softc *, struct mii_data *, int);
81 1.1 gdamore static void mvphy_status(struct mii_softc *);
82 1.1 gdamore static void mvphy_reset(struct mii_softc *sc);
83 1.1 gdamore
84 1.1 gdamore static const struct mii_phy_funcs mvphy_funcs = {
85 1.1 gdamore mvphy_service, mvphy_status, mvphy_reset,
86 1.1 gdamore };
87 1.1 gdamore
88 1.1 gdamore static const struct mii_phydesc mvphys[] = {
89 1.1 gdamore { MII_OUI_xxMARVELL, MII_MODEL_xxMARVELL_E6060,
90 1.1 gdamore MII_STR_xxMARVELL_E6060 },
91 1.1 gdamore
92 1.1 gdamore { 0, 0,
93 1.1 gdamore NULL },
94 1.1 gdamore };
95 1.1 gdamore
96 1.1 gdamore /*
97 1.1 gdamore * On AP30/AR5312 the switch is configured in one of two ways:
98 1.1 gdamore * as a ROUTER or as a BRIDGE. The ROUTER config sets up ports
99 1.1 gdamore * 0-3 as LAN ports, port 4 as the WAN port, and port 5 connects
100 1.1 gdamore * to the MAC in the 5312. The BRIDGE config sets up ports
101 1.1 gdamore * 0-4 as LAN ports with port 5 connected to the MAC in the 5312.
102 1.1 gdamore */
103 1.1 gdamore struct mvPhyConfig {
104 1.1 gdamore uint16_t switchPortAddr;/* switch port associated with PHY */
105 1.1 gdamore uint16_t vlanSetting; /* VLAN table setting for PHY */
106 1.1 gdamore uint32_t portControl; /* switch port control setting for PHY */
107 1.1 gdamore };
108 1.3 jmcneill static const struct mvPhyConfig dumbConfig[] = {
109 1.3 jmcneill { 0x18, 0x2e, /* PHY port 0 = LAN port 0 */
110 1.3 jmcneill MV_PORT_CONTROL_PORT_STATE_FORWARDING },
111 1.3 jmcneill { 0x19, 0x2d, /* PHY port 1 = LAN port 1 */
112 1.3 jmcneill MV_PORT_CONTROL_PORT_STATE_FORWARDING },
113 1.3 jmcneill { 0x1a, 0x2b, /* PHY port 2 = LAN port 2 */
114 1.3 jmcneill MV_PORT_CONTROL_PORT_STATE_FORWARDING },
115 1.3 jmcneill { 0x1b, 0x27, /* PHY port 3 = LAN port 3 */
116 1.3 jmcneill MV_PORT_CONTROL_PORT_STATE_FORWARDING },
117 1.3 jmcneill { 0x1c, 0x25, /* PHY port 4 = LAN port 4 */
118 1.3 jmcneill MV_PORT_CONTROL_PORT_STATE_FORWARDING },
119 1.3 jmcneill { 0x1d, 0x1f, /* PHY port 5 = CPU port */
120 1.3 jmcneill MV_PORT_CONTROL_PORT_STATE_FORWARDING }
121 1.3 jmcneill };
122 1.1 gdamore static const struct mvPhyConfig routerConfig[] = {
123 1.1 gdamore { 0x18, 0x2e, /* PHY port 0 = LAN port 0 */
124 1.1 gdamore MV_PORT_CONTROL_PORT_STATE_FORWARDING },
125 1.1 gdamore { 0x19, 0x2d, /* PHY port 1 = LAN port 1 */
126 1.1 gdamore MV_PORT_CONTROL_PORT_STATE_FORWARDING },
127 1.1 gdamore { 0x1a, 0x2b, /* PHY port 2 = LAN port 2 */
128 1.1 gdamore MV_PORT_CONTROL_PORT_STATE_FORWARDING },
129 1.1 gdamore { 0x1b, 0x27, /* PHY port 3 = LAN port 3 */
130 1.1 gdamore MV_PORT_CONTROL_PORT_STATE_FORWARDING },
131 1.1 gdamore { 0x1c, 0x1020, /* PHY port 4 = WAN port */
132 1.1 gdamore MV_PORT_CONTROL_PORT_STATE_FORWARDING },
133 1.1 gdamore /* NB: 0x0f =>'s send only to LAN ports */
134 1.1 gdamore { 0x1d, 0x0f, /* PHY port 5 = CPU port */
135 1.1 gdamore MV_PORT_CONTROL_PORT_STATE_FORWARDING
136 1.1 gdamore #if 0
137 1.1 gdamore | MV_PORT_CONTROL_INGRESS_TRAILER
138 1.1 gdamore | MV_PORT_CONTROL_EGRESS_MODE
139 1.1 gdamore #endif
140 1.1 gdamore }
141 1.1 gdamore };
142 1.1 gdamore static const struct mvPhyConfig bridgeConfig[] = {
143 1.1 gdamore { 0x18, 0x3e, /* PHY port 0 = LAN port 0 */
144 1.1 gdamore MV_PORT_CONTROL_PORT_STATE_FORWARDING },
145 1.1 gdamore { 0x19, 0x3d, /* PHY port 1 = LAN port 1 */
146 1.1 gdamore MV_PORT_CONTROL_PORT_STATE_FORWARDING },
147 1.1 gdamore { 0x1a, 0x3b, /* PHY port 2 = LAN port 2 */
148 1.1 gdamore MV_PORT_CONTROL_PORT_STATE_FORWARDING },
149 1.1 gdamore { 0x1b, 0x37, /* PHY port 3 = LAN port 3 */
150 1.1 gdamore MV_PORT_CONTROL_PORT_STATE_FORWARDING },
151 1.1 gdamore { 0x1c, 0x37, /* PHY port 4 = LAN port 4 */
152 1.1 gdamore MV_PORT_CONTROL_PORT_STATE_FORWARDING },
153 1.1 gdamore /* NB: 0x1f =>'s send to all ports */
154 1.1 gdamore { 0x1d, 0x1f, /* PHY port 5 = CPU port */
155 1.1 gdamore MV_PORT_CONTROL_PORT_STATE_FORWARDING
156 1.1 gdamore #if 0
157 1.1 gdamore | MV_PORT_CONTROL_INGRESS_TRAILER
158 1.1 gdamore | MV_PORT_CONTROL_EGRESS_MODE
159 1.1 gdamore #endif
160 1.1 gdamore }
161 1.1 gdamore };
162 1.1 gdamore
163 1.1 gdamore static void mvphy_switchconfig(struct mii_softc *, int);
164 1.1 gdamore static void mvphy_flushatu(struct mii_softc *);
165 1.1 gdamore
166 1.1 gdamore static int
167 1.7 xtraeme mvphymatch(device_t parent, cfdata_t match, void *aux)
168 1.1 gdamore {
169 1.1 gdamore struct mii_attach_args *ma = aux;
170 1.1 gdamore
171 1.1 gdamore if (mii_phy_match(ma, mvphys) != NULL)
172 1.1 gdamore return (10);
173 1.1 gdamore
174 1.1 gdamore return (0);
175 1.1 gdamore }
176 1.1 gdamore
177 1.1 gdamore static void
178 1.7 xtraeme mvphyattach(device_t parent, device_t self, void *aux)
179 1.1 gdamore {
180 1.1 gdamore struct mii_softc *sc = device_private(self);
181 1.1 gdamore struct mii_attach_args *ma = aux;
182 1.1 gdamore struct mii_data *mii = ma->mii_data;
183 1.1 gdamore const struct mii_phydesc *mpd;
184 1.1 gdamore
185 1.1 gdamore mpd = mii_phy_match(ma, mvphys);
186 1.1 gdamore aprint_naive(": Media interface\n");
187 1.1 gdamore aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
188 1.1 gdamore
189 1.7 xtraeme sc->mii_dev = self;
190 1.1 gdamore sc->mii_inst = mii->mii_instance;
191 1.1 gdamore sc->mii_phy = ma->mii_phyno;
192 1.1 gdamore sc->mii_funcs = &mvphy_funcs;
193 1.1 gdamore sc->mii_pdata = mii;
194 1.1 gdamore sc->mii_flags = ma->mii_flags;
195 1.2 christos sc->mii_anegticks = MII_ANEGTICKS;
196 1.1 gdamore
197 1.1 gdamore if (MV_PORT(sc) == 0) { /* NB: only when attaching first PHY */
198 1.1 gdamore /*
199 1.1 gdamore * Set the global switch settings and configure the
200 1.1 gdamore * CPU port since it does not probe as a visible PHY.
201 1.1 gdamore */
202 1.1 gdamore MV_WRITE(sc, MII_MV_SWITCH_GLOBAL_ADDR, MV_ATU_CONTROL,
203 1.1 gdamore SM(MV_ATUCTRL_AGE_TIME_DEFAULT, MV_ATUCTRL_AGE_TIME)
204 1.1 gdamore | SM(MV_ATUCTRL_ATU_SIZE_DEFAULT, MV_ATUCTRL_ATU_SIZE));
205 1.1 gdamore mvphy_switchconfig(sc, MV_CPU_PORT);
206 1.1 gdamore }
207 1.1 gdamore PHY_RESET(sc);
208 1.1 gdamore
209 1.1 gdamore sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
210 1.7 xtraeme aprint_normal_dev(self, "");
211 1.1 gdamore if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
212 1.1 gdamore aprint_error("no media present");
213 1.1 gdamore else
214 1.1 gdamore mii_phy_add_media(sc);
215 1.1 gdamore aprint_normal("\n");
216 1.4 jmcneill
217 1.4 jmcneill if (!pmf_device_register(self, NULL, mii_phy_resume))
218 1.4 jmcneill aprint_error_dev(self, "couldn't establish power handler\n");
219 1.1 gdamore }
220 1.1 gdamore
221 1.1 gdamore static int
222 1.1 gdamore mvphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
223 1.1 gdamore {
224 1.1 gdamore struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
225 1.1 gdamore
226 1.1 gdamore switch (cmd) {
227 1.1 gdamore case MII_POLLSTAT:
228 1.1 gdamore /*
229 1.1 gdamore * If we're not polling our PHY instance, just return.
230 1.1 gdamore */
231 1.1 gdamore if (IFM_INST(ife->ifm_media) != sc->mii_inst)
232 1.1 gdamore return (0);
233 1.1 gdamore break;
234 1.1 gdamore
235 1.1 gdamore case MII_MEDIACHG:
236 1.1 gdamore /*
237 1.1 gdamore * If the media indicates a different PHY instance,
238 1.1 gdamore * isolate ourselves.
239 1.1 gdamore */
240 1.1 gdamore if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
241 1.1 gdamore /* XXX? */
242 1.1 gdamore return (0);
243 1.1 gdamore }
244 1.1 gdamore
245 1.1 gdamore /*
246 1.1 gdamore * If the interface is not up, don't do anything.
247 1.1 gdamore */
248 1.1 gdamore if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
249 1.1 gdamore break;
250 1.1 gdamore
251 1.1 gdamore mii_phy_setmedia(sc);
252 1.1 gdamore break;
253 1.1 gdamore
254 1.1 gdamore case MII_TICK:
255 1.1 gdamore /*
256 1.1 gdamore * If we're not currently selected, just return.
257 1.1 gdamore */
258 1.1 gdamore if (IFM_INST(ife->ifm_media) != sc->mii_inst)
259 1.1 gdamore return (0);
260 1.1 gdamore
261 1.1 gdamore if (mii_phy_tick(sc) == EJUSTRETURN)
262 1.1 gdamore return (0);
263 1.1 gdamore break;
264 1.1 gdamore
265 1.1 gdamore case MII_DOWN:
266 1.1 gdamore mii_phy_down(sc);
267 1.1 gdamore return (0);
268 1.1 gdamore }
269 1.1 gdamore
270 1.1 gdamore /* Update the media status. */
271 1.1 gdamore mii_phy_status(sc);
272 1.1 gdamore
273 1.1 gdamore /* Callback if something changed. */
274 1.1 gdamore mii_phy_update(sc, cmd);
275 1.1 gdamore return (0);
276 1.1 gdamore }
277 1.1 gdamore
278 1.1 gdamore static void
279 1.1 gdamore mvphy_status(struct mii_softc *sc)
280 1.1 gdamore {
281 1.1 gdamore struct mii_data *mii = sc->mii_pdata;
282 1.1 gdamore int hwstatus;
283 1.1 gdamore
284 1.1 gdamore mii->mii_media_status = IFM_AVALID;
285 1.1 gdamore mii->mii_media_active = IFM_ETHER;
286 1.1 gdamore
287 1.1 gdamore hwstatus = PHY_READ(sc, MII_MV_PHY_SPECIFIC_STATUS);
288 1.1 gdamore if (hwstatus & MV_STATUS_REAL_TIME_LINK_UP) {
289 1.1 gdamore mii->mii_media_status |= IFM_ACTIVE;
290 1.1 gdamore if (hwstatus & MV_STATUS_RESOLVED_SPEED_100)
291 1.1 gdamore mii->mii_media_active |= IFM_100_TX;
292 1.1 gdamore else
293 1.1 gdamore mii->mii_media_active |= IFM_10_T;
294 1.1 gdamore if (hwstatus & MV_STATUS_RESOLVED_DUPLEX_FULL)
295 1.1 gdamore mii->mii_media_active |= IFM_FDX;
296 1.1 gdamore } else {
297 1.1 gdamore mii->mii_media_active |= IFM_NONE;
298 1.1 gdamore /* XXX flush ATU only on link down transition */
299 1.1 gdamore mvphy_flushatu(sc);
300 1.1 gdamore }
301 1.1 gdamore }
302 1.1 gdamore
303 1.1 gdamore static void
304 1.1 gdamore mvphy_reset(struct mii_softc *sc)
305 1.1 gdamore {
306 1.1 gdamore
307 1.1 gdamore /* XXX handle fixed media config */
308 1.1 gdamore PHY_WRITE(sc, MII_BMCR, BMCR_RESET | BMCR_AUTOEN);
309 1.1 gdamore mvphy_switchconfig(sc, MV_PORT(sc));
310 1.1 gdamore }
311 1.1 gdamore
312 1.1 gdamore /*
313 1.1 gdamore * Configure switch for the specified port.
314 1.1 gdamore */
315 1.1 gdamore static void
316 1.1 gdamore mvphy_switchconfig(struct mii_softc *sc, int port)
317 1.1 gdamore {
318 1.1 gdamore /* XXX router vs bridge */
319 1.3 jmcneill /*const struct mvPhyConfig *conf = &routerConfig[port];*/
320 1.3 jmcneill /*const struct mvPhyConfig *conf = &bridgeConfig[port];*/
321 1.3 jmcneill const struct mvPhyConfig *conf = &dumbConfig[port];
322 1.1 gdamore
323 1.1 gdamore MV_WRITE(sc, conf->switchPortAddr, MV_PORT_BASED_VLAN_MAP,
324 1.1 gdamore conf->vlanSetting);
325 1.1 gdamore /* XXX administrative control of port enable? */
326 1.1 gdamore MV_WRITE(sc, conf->switchPortAddr, MV_PORT_CONTROL, conf->portControl);
327 1.1 gdamore MV_WRITE(sc, conf->switchPortAddr, MV_PORT_ASSOCIATION_VECTOR, 1<<port);
328 1.1 gdamore }
329 1.1 gdamore
330 1.1 gdamore /*
331 1.1 gdamore * Flush the Address Translation Unit (ATU).
332 1.1 gdamore */
333 1.1 gdamore static void
334 1.1 gdamore mvphy_flushatu(struct mii_softc *sc)
335 1.1 gdamore {
336 1.1 gdamore uint16_t status;
337 1.1 gdamore int i;
338 1.1 gdamore
339 1.1 gdamore /* wait for any previous request to complete */
340 1.1 gdamore /* XXX if busy defer to tick */
341 1.1 gdamore /* XXX timeout */
342 1.1 gdamore for (i = 0; i < 1000; i++) {
343 1.1 gdamore status = MV_READ(sc, MII_MV_SWITCH_GLOBAL_ADDR,
344 1.1 gdamore MV_ATU_OPERATION);
345 1.1 gdamore if (MV_ATU_IS_BUSY(status))
346 1.1 gdamore break;
347 1.1 gdamore }
348 1.1 gdamore if (i != 1000) {
349 1.1 gdamore MV_WRITE(sc, MII_MV_SWITCH_GLOBAL_ADDR, MV_ATU_OPERATION,
350 1.1 gdamore MV_ATU_OP_FLUSH_ALL | MV_ATU_BUSY);
351 1.3 jmcneill } /*else
352 1.1 gdamore printf("%s: timeout waiting for ATU flush\n",
353 1.7 xtraeme device_xname(sc->mii_dev));*/
354 1.1 gdamore }
355