1 1.15 thorpej /* $NetBSD: mvphy.c,v 1.15 2020/03/15 23:04:50 thorpej 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.15 thorpej __KERNEL_RCSID(0, "$NetBSD: mvphy.c,v 1.15 2020/03/15 23:04:50 thorpej 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.11 msaitoh #define MV_READ(p, phy, r, v) \ 56 1.11 msaitoh (*(p)->mii_pdata->mii_readreg)(device_parent((p)->mii_dev), \ 57 1.11 msaitoh (phy), (r), (v)) 58 1.11 msaitoh #define MV_WRITE(p, phy, r, v) \ 59 1.11 msaitoh (*(p)->mii_pdata->mii_writereg)(device_parent((p)->mii_dev), \ 60 1.11 msaitoh (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.12 christos MII_PHY_DESC(xxMARVELL, E6060), 90 1.12 christos MII_PHY_END, 91 1.1 gdamore }; 92 1.1 gdamore 93 1.1 gdamore /* 94 1.1 gdamore * On AP30/AR5312 the switch is configured in one of two ways: 95 1.1 gdamore * as a ROUTER or as a BRIDGE. The ROUTER config sets up ports 96 1.1 gdamore * 0-3 as LAN ports, port 4 as the WAN port, and port 5 connects 97 1.1 gdamore * to the MAC in the 5312. The BRIDGE config sets up ports 98 1.1 gdamore * 0-4 as LAN ports with port 5 connected to the MAC in the 5312. 99 1.1 gdamore */ 100 1.1 gdamore struct mvPhyConfig { 101 1.1 gdamore uint16_t switchPortAddr;/* switch port associated with PHY */ 102 1.1 gdamore uint16_t vlanSetting; /* VLAN table setting for PHY */ 103 1.1 gdamore uint32_t portControl; /* switch port control setting for PHY */ 104 1.1 gdamore }; 105 1.3 jmcneill static const struct mvPhyConfig dumbConfig[] = { 106 1.3 jmcneill { 0x18, 0x2e, /* PHY port 0 = LAN port 0 */ 107 1.3 jmcneill MV_PORT_CONTROL_PORT_STATE_FORWARDING }, 108 1.3 jmcneill { 0x19, 0x2d, /* PHY port 1 = LAN port 1 */ 109 1.3 jmcneill MV_PORT_CONTROL_PORT_STATE_FORWARDING }, 110 1.3 jmcneill { 0x1a, 0x2b, /* PHY port 2 = LAN port 2 */ 111 1.3 jmcneill MV_PORT_CONTROL_PORT_STATE_FORWARDING }, 112 1.3 jmcneill { 0x1b, 0x27, /* PHY port 3 = LAN port 3 */ 113 1.3 jmcneill MV_PORT_CONTROL_PORT_STATE_FORWARDING }, 114 1.3 jmcneill { 0x1c, 0x25, /* PHY port 4 = LAN port 4 */ 115 1.3 jmcneill MV_PORT_CONTROL_PORT_STATE_FORWARDING }, 116 1.3 jmcneill { 0x1d, 0x1f, /* PHY port 5 = CPU port */ 117 1.3 jmcneill MV_PORT_CONTROL_PORT_STATE_FORWARDING } 118 1.3 jmcneill }; 119 1.11 msaitoh #if 0 /* XXX what are these? */ 120 1.1 gdamore static const struct mvPhyConfig routerConfig[] = { 121 1.1 gdamore { 0x18, 0x2e, /* PHY port 0 = LAN port 0 */ 122 1.1 gdamore MV_PORT_CONTROL_PORT_STATE_FORWARDING }, 123 1.1 gdamore { 0x19, 0x2d, /* PHY port 1 = LAN port 1 */ 124 1.1 gdamore MV_PORT_CONTROL_PORT_STATE_FORWARDING }, 125 1.1 gdamore { 0x1a, 0x2b, /* PHY port 2 = LAN port 2 */ 126 1.1 gdamore MV_PORT_CONTROL_PORT_STATE_FORWARDING }, 127 1.1 gdamore { 0x1b, 0x27, /* PHY port 3 = LAN port 3 */ 128 1.1 gdamore MV_PORT_CONTROL_PORT_STATE_FORWARDING }, 129 1.1 gdamore { 0x1c, 0x1020, /* PHY port 4 = WAN port */ 130 1.1 gdamore MV_PORT_CONTROL_PORT_STATE_FORWARDING }, 131 1.1 gdamore /* NB: 0x0f =>'s send only to LAN ports */ 132 1.1 gdamore { 0x1d, 0x0f, /* PHY port 5 = CPU port */ 133 1.1 gdamore MV_PORT_CONTROL_PORT_STATE_FORWARDING 134 1.1 gdamore #if 0 135 1.1 gdamore | MV_PORT_CONTROL_INGRESS_TRAILER 136 1.1 gdamore | MV_PORT_CONTROL_EGRESS_MODE 137 1.1 gdamore #endif 138 1.1 gdamore } 139 1.1 gdamore }; 140 1.1 gdamore static const struct mvPhyConfig bridgeConfig[] = { 141 1.1 gdamore { 0x18, 0x3e, /* PHY port 0 = LAN port 0 */ 142 1.1 gdamore MV_PORT_CONTROL_PORT_STATE_FORWARDING }, 143 1.1 gdamore { 0x19, 0x3d, /* PHY port 1 = LAN port 1 */ 144 1.1 gdamore MV_PORT_CONTROL_PORT_STATE_FORWARDING }, 145 1.1 gdamore { 0x1a, 0x3b, /* PHY port 2 = LAN port 2 */ 146 1.1 gdamore MV_PORT_CONTROL_PORT_STATE_FORWARDING }, 147 1.1 gdamore { 0x1b, 0x37, /* PHY port 3 = LAN port 3 */ 148 1.1 gdamore MV_PORT_CONTROL_PORT_STATE_FORWARDING }, 149 1.1 gdamore { 0x1c, 0x37, /* PHY port 4 = LAN port 4 */ 150 1.1 gdamore MV_PORT_CONTROL_PORT_STATE_FORWARDING }, 151 1.1 gdamore /* NB: 0x1f =>'s send to all ports */ 152 1.1 gdamore { 0x1d, 0x1f, /* PHY port 5 = CPU port */ 153 1.1 gdamore MV_PORT_CONTROL_PORT_STATE_FORWARDING 154 1.1 gdamore #if 0 155 1.1 gdamore | MV_PORT_CONTROL_INGRESS_TRAILER 156 1.1 gdamore | MV_PORT_CONTROL_EGRESS_MODE 157 1.1 gdamore #endif 158 1.1 gdamore } 159 1.1 gdamore }; 160 1.11 msaitoh #endif 161 1.1 gdamore 162 1.1 gdamore static void mvphy_switchconfig(struct mii_softc *, int); 163 1.1 gdamore static void mvphy_flushatu(struct mii_softc *); 164 1.1 gdamore 165 1.1 gdamore static int 166 1.7 xtraeme mvphymatch(device_t parent, cfdata_t match, void *aux) 167 1.1 gdamore { 168 1.1 gdamore struct mii_attach_args *ma = aux; 169 1.1 gdamore 170 1.1 gdamore if (mii_phy_match(ma, mvphys) != NULL) 171 1.13 msaitoh return 10; 172 1.1 gdamore 173 1.13 msaitoh return 0; 174 1.1 gdamore } 175 1.1 gdamore 176 1.1 gdamore static void 177 1.7 xtraeme mvphyattach(device_t parent, device_t self, void *aux) 178 1.1 gdamore { 179 1.1 gdamore struct mii_softc *sc = device_private(self); 180 1.1 gdamore struct mii_attach_args *ma = aux; 181 1.1 gdamore struct mii_data *mii = ma->mii_data; 182 1.1 gdamore const struct mii_phydesc *mpd; 183 1.1 gdamore 184 1.1 gdamore mpd = mii_phy_match(ma, mvphys); 185 1.1 gdamore aprint_naive(": Media interface\n"); 186 1.1 gdamore aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2)); 187 1.1 gdamore 188 1.7 xtraeme sc->mii_dev = self; 189 1.1 gdamore sc->mii_inst = mii->mii_instance; 190 1.1 gdamore sc->mii_phy = ma->mii_phyno; 191 1.1 gdamore sc->mii_funcs = &mvphy_funcs; 192 1.1 gdamore sc->mii_pdata = mii; 193 1.1 gdamore sc->mii_flags = ma->mii_flags; 194 1.1 gdamore 195 1.15 thorpej mii_lock(mii); 196 1.15 thorpej 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.11 msaitoh PHY_READ(sc, MII_BMSR, &sc->mii_capabilities); 210 1.11 msaitoh sc->mii_capabilities &= ma->mii_capmask; 211 1.14 msaitoh 212 1.15 thorpej mii_unlock(mii); 213 1.15 thorpej 214 1.14 msaitoh mii_phy_add_media(sc); 215 1.1 gdamore } 216 1.1 gdamore 217 1.1 gdamore static int 218 1.1 gdamore mvphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 219 1.1 gdamore { 220 1.1 gdamore struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 221 1.1 gdamore 222 1.15 thorpej KASSERT(mii_locked(mii)); 223 1.15 thorpej 224 1.1 gdamore switch (cmd) { 225 1.1 gdamore case MII_POLLSTAT: 226 1.13 msaitoh /* If we're not polling our PHY instance, just return. */ 227 1.1 gdamore if (IFM_INST(ife->ifm_media) != sc->mii_inst) 228 1.13 msaitoh return 0; 229 1.1 gdamore break; 230 1.1 gdamore 231 1.1 gdamore case MII_MEDIACHG: 232 1.1 gdamore /* 233 1.1 gdamore * If the media indicates a different PHY instance, 234 1.1 gdamore * isolate ourselves. 235 1.1 gdamore */ 236 1.1 gdamore if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 237 1.1 gdamore /* XXX? */ 238 1.13 msaitoh return 0; 239 1.1 gdamore } 240 1.1 gdamore 241 1.13 msaitoh /* If the interface is not up, don't do anything. */ 242 1.1 gdamore if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 243 1.1 gdamore break; 244 1.1 gdamore 245 1.1 gdamore mii_phy_setmedia(sc); 246 1.1 gdamore break; 247 1.1 gdamore 248 1.1 gdamore case MII_TICK: 249 1.13 msaitoh /* If we're not currently selected, just return. */ 250 1.1 gdamore if (IFM_INST(ife->ifm_media) != sc->mii_inst) 251 1.13 msaitoh return 0; 252 1.1 gdamore 253 1.1 gdamore if (mii_phy_tick(sc) == EJUSTRETURN) 254 1.13 msaitoh return 0; 255 1.1 gdamore break; 256 1.1 gdamore 257 1.1 gdamore case MII_DOWN: 258 1.1 gdamore mii_phy_down(sc); 259 1.13 msaitoh return 0; 260 1.1 gdamore } 261 1.1 gdamore 262 1.1 gdamore /* Update the media status. */ 263 1.1 gdamore mii_phy_status(sc); 264 1.1 gdamore 265 1.1 gdamore /* Callback if something changed. */ 266 1.1 gdamore mii_phy_update(sc, cmd); 267 1.13 msaitoh return 0; 268 1.1 gdamore } 269 1.1 gdamore 270 1.1 gdamore static void 271 1.1 gdamore mvphy_status(struct mii_softc *sc) 272 1.1 gdamore { 273 1.1 gdamore struct mii_data *mii = sc->mii_pdata; 274 1.11 msaitoh uint16_t hwstatus; 275 1.1 gdamore 276 1.15 thorpej KASSERT(mii_locked(mii)); 277 1.15 thorpej 278 1.1 gdamore mii->mii_media_status = IFM_AVALID; 279 1.1 gdamore mii->mii_media_active = IFM_ETHER; 280 1.1 gdamore 281 1.11 msaitoh PHY_READ(sc, MII_MV_PHY_SPECIFIC_STATUS, &hwstatus); 282 1.1 gdamore if (hwstatus & MV_STATUS_REAL_TIME_LINK_UP) { 283 1.1 gdamore mii->mii_media_status |= IFM_ACTIVE; 284 1.1 gdamore if (hwstatus & MV_STATUS_RESOLVED_SPEED_100) 285 1.1 gdamore mii->mii_media_active |= IFM_100_TX; 286 1.1 gdamore else 287 1.1 gdamore mii->mii_media_active |= IFM_10_T; 288 1.1 gdamore if (hwstatus & MV_STATUS_RESOLVED_DUPLEX_FULL) 289 1.1 gdamore mii->mii_media_active |= IFM_FDX; 290 1.10 msaitoh else 291 1.10 msaitoh mii->mii_media_active |= IFM_HDX; 292 1.1 gdamore } else { 293 1.1 gdamore mii->mii_media_active |= IFM_NONE; 294 1.1 gdamore /* XXX flush ATU only on link down transition */ 295 1.1 gdamore mvphy_flushatu(sc); 296 1.1 gdamore } 297 1.1 gdamore } 298 1.1 gdamore 299 1.1 gdamore static void 300 1.1 gdamore mvphy_reset(struct mii_softc *sc) 301 1.1 gdamore { 302 1.1 gdamore 303 1.15 thorpej KASSERT(mii_locked(sc->mii_pdata)); 304 1.15 thorpej 305 1.1 gdamore /* XXX handle fixed media config */ 306 1.1 gdamore PHY_WRITE(sc, MII_BMCR, BMCR_RESET | BMCR_AUTOEN); 307 1.1 gdamore mvphy_switchconfig(sc, MV_PORT(sc)); 308 1.1 gdamore } 309 1.1 gdamore 310 1.1 gdamore /* 311 1.1 gdamore * Configure switch for the specified port. 312 1.1 gdamore */ 313 1.1 gdamore static void 314 1.1 gdamore mvphy_switchconfig(struct mii_softc *sc, int port) 315 1.1 gdamore { 316 1.1 gdamore /* XXX router vs bridge */ 317 1.3 jmcneill /*const struct mvPhyConfig *conf = &routerConfig[port];*/ 318 1.3 jmcneill /*const struct mvPhyConfig *conf = &bridgeConfig[port];*/ 319 1.3 jmcneill const struct mvPhyConfig *conf = &dumbConfig[port]; 320 1.1 gdamore 321 1.1 gdamore MV_WRITE(sc, conf->switchPortAddr, MV_PORT_BASED_VLAN_MAP, 322 1.1 gdamore conf->vlanSetting); 323 1.1 gdamore /* XXX administrative control of port enable? */ 324 1.1 gdamore MV_WRITE(sc, conf->switchPortAddr, MV_PORT_CONTROL, conf->portControl); 325 1.13 msaitoh MV_WRITE(sc, conf->switchPortAddr, MV_PORT_ASSOCIATION_VECTOR, 326 1.13 msaitoh 1 << port); 327 1.1 gdamore } 328 1.1 gdamore 329 1.1 gdamore /* 330 1.1 gdamore * Flush the Address Translation Unit (ATU). 331 1.1 gdamore */ 332 1.1 gdamore static void 333 1.1 gdamore mvphy_flushatu(struct mii_softc *sc) 334 1.1 gdamore { 335 1.11 msaitoh int status; 336 1.11 msaitoh uint16_t reg; 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.11 msaitoh 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.9 rjs aprint_error_dev(sc->mii_dev, "timeout waiting for ATU flush\n");*/ 353 1.1 gdamore } 354