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