1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Lantiq / Intel GSWIP switch driver for VRX200 SoCs 4 * 5 * Copyright (C) 2010 Lantiq Deutschland 6 * Copyright (C) 2012 John Crispin <john@phrozen.org> 7 * Copyright (C) 2017 - 2018 Hauke Mehrtens <hauke@hauke-m.de> 8 */ 9 10 #include <linux/clk.h> 11 #include <linux/etherdevice.h> 12 #include <linux/firmware.h> 13 #include <linux/if_bridge.h> 14 #include <linux/if_vlan.h> 15 #include <linux/iopoll.h> 16 #include <linux/mfd/syscon.h> 17 #include <linux/module.h> 18 #include <linux/of_mdio.h> 19 #include <linux/of_net.h> 20 #include <linux/of_platform.h> 21 #include <linux/phy.h> 22 #include <linux/phylink.h> 23 #include <linux/platform_device.h> 24 #include <linux/regmap.h> 25 #include <linux/reset.h> 26 #include <net/dsa.h> 27 #include <dt-bindings/mips/lantiq_rcu_gphy.h> 28 29 #include "lantiq_pce.h" 30 31 /* GSWIP MDIO Registers */ 32 #define GSWIP_MDIO_GLOB 0x00 33 #define GSWIP_MDIO_GLOB_ENABLE BIT(15) 34 #define GSWIP_MDIO_CTRL 0x08 35 #define GSWIP_MDIO_CTRL_BUSY BIT(12) 36 #define GSWIP_MDIO_CTRL_RD BIT(11) 37 #define GSWIP_MDIO_CTRL_WR BIT(10) 38 #define GSWIP_MDIO_CTRL_PHYAD_MASK 0x1f 39 #define GSWIP_MDIO_CTRL_PHYAD_SHIFT 5 40 #define GSWIP_MDIO_CTRL_REGAD_MASK 0x1f 41 #define GSWIP_MDIO_READ 0x09 42 #define GSWIP_MDIO_WRITE 0x0A 43 #define GSWIP_MDIO_MDC_CFG0 0x0B 44 #define GSWIP_MDIO_MDC_CFG1 0x0C 45 #define GSWIP_MDIO_PHYp(p) (0x15 - (p)) 46 #define GSWIP_MDIO_PHY_LINK_MASK 0x6000 47 #define GSWIP_MDIO_PHY_LINK_AUTO 0x0000 48 #define GSWIP_MDIO_PHY_LINK_DOWN 0x4000 49 #define GSWIP_MDIO_PHY_LINK_UP 0x2000 50 #define GSWIP_MDIO_PHY_SPEED_MASK 0x1800 51 #define GSWIP_MDIO_PHY_SPEED_AUTO 0x1800 52 #define GSWIP_MDIO_PHY_SPEED_M10 0x0000 53 #define GSWIP_MDIO_PHY_SPEED_M100 0x0800 54 #define GSWIP_MDIO_PHY_SPEED_G1 0x1000 55 #define GSWIP_MDIO_PHY_FDUP_MASK 0x0600 56 #define GSWIP_MDIO_PHY_FDUP_AUTO 0x0000 57 #define GSWIP_MDIO_PHY_FDUP_EN 0x0200 58 #define GSWIP_MDIO_PHY_FDUP_DIS 0x0600 59 #define GSWIP_MDIO_PHY_FCONTX_MASK 0x0180 60 #define GSWIP_MDIO_PHY_FCONTX_AUTO 0x0000 61 #define GSWIP_MDIO_PHY_FCONTX_EN 0x0100 62 #define GSWIP_MDIO_PHY_FCONTX_DIS 0x0180 63 #define GSWIP_MDIO_PHY_FCONRX_MASK 0x0060 64 #define GSWIP_MDIO_PHY_FCONRX_AUTO 0x0000 65 #define GSWIP_MDIO_PHY_FCONRX_EN 0x0020 66 #define GSWIP_MDIO_PHY_FCONRX_DIS 0x0060 67 #define GSWIP_MDIO_PHY_ADDR_MASK 0x001f 68 #define GSWIP_MDIO_PHY_MASK (GSWIP_MDIO_PHY_ADDR_MASK | \ 69 GSWIP_MDIO_PHY_FCONRX_MASK | \ 70 GSWIP_MDIO_PHY_FCONTX_MASK | \ 71 GSWIP_MDIO_PHY_LINK_MASK | \ 72 GSWIP_MDIO_PHY_SPEED_MASK | \ 73 GSWIP_MDIO_PHY_FDUP_MASK) 74 75 /* GSWIP MII Registers */ 76 #define GSWIP_MII_CFG0 0x00 77 #define GSWIP_MII_CFG1 0x02 78 #define GSWIP_MII_CFG5 0x04 79 #define GSWIP_MII_CFG_EN BIT(14) 80 #define GSWIP_MII_CFG_LDCLKDIS BIT(12) 81 #define GSWIP_MII_CFG_MODE_MIIP 0x0 82 #define GSWIP_MII_CFG_MODE_MIIM 0x1 83 #define GSWIP_MII_CFG_MODE_RMIIP 0x2 84 #define GSWIP_MII_CFG_MODE_RMIIM 0x3 85 #define GSWIP_MII_CFG_MODE_RGMII 0x4 86 #define GSWIP_MII_CFG_MODE_MASK 0xf 87 #define GSWIP_MII_CFG_RATE_M2P5 0x00 88 #define GSWIP_MII_CFG_RATE_M25 0x10 89 #define GSWIP_MII_CFG_RATE_M125 0x20 90 #define GSWIP_MII_CFG_RATE_M50 0x30 91 #define GSWIP_MII_CFG_RATE_AUTO 0x40 92 #define GSWIP_MII_CFG_RATE_MASK 0x70 93 #define GSWIP_MII_PCDU0 0x01 94 #define GSWIP_MII_PCDU1 0x03 95 #define GSWIP_MII_PCDU5 0x05 96 #define GSWIP_MII_PCDU_TXDLY_MASK GENMASK(2, 0) 97 #define GSWIP_MII_PCDU_RXDLY_MASK GENMASK(9, 7) 98 99 /* GSWIP Core Registers */ 100 #define GSWIP_SWRES 0x000 101 #define GSWIP_SWRES_R1 BIT(1) /* GSWIP Software reset */ 102 #define GSWIP_SWRES_R0 BIT(0) /* GSWIP Hardware reset */ 103 #define GSWIP_VERSION 0x013 104 #define GSWIP_VERSION_REV_SHIFT 0 105 #define GSWIP_VERSION_REV_MASK GENMASK(7, 0) 106 #define GSWIP_VERSION_MOD_SHIFT 8 107 #define GSWIP_VERSION_MOD_MASK GENMASK(15, 8) 108 #define GSWIP_VERSION_2_0 0x100 109 #define GSWIP_VERSION_2_1 0x021 110 #define GSWIP_VERSION_2_2 0x122 111 #define GSWIP_VERSION_2_2_ETC 0x022 112 113 #define GSWIP_BM_RAM_VAL(x) (0x043 - (x)) 114 #define GSWIP_BM_RAM_ADDR 0x044 115 #define GSWIP_BM_RAM_CTRL 0x045 116 #define GSWIP_BM_RAM_CTRL_BAS BIT(15) 117 #define GSWIP_BM_RAM_CTRL_OPMOD BIT(5) 118 #define GSWIP_BM_RAM_CTRL_ADDR_MASK GENMASK(4, 0) 119 #define GSWIP_BM_QUEUE_GCTRL 0x04A 120 #define GSWIP_BM_QUEUE_GCTRL_GL_MOD BIT(10) 121 /* buffer management Port Configuration Register */ 122 #define GSWIP_BM_PCFGp(p) (0x080 + ((p) * 2)) 123 #define GSWIP_BM_PCFG_CNTEN BIT(0) /* RMON Counter Enable */ 124 #define GSWIP_BM_PCFG_IGCNT BIT(1) /* Ingres Special Tag RMON count */ 125 /* buffer management Port Control Register */ 126 #define GSWIP_BM_RMON_CTRLp(p) (0x81 + ((p) * 2)) 127 #define GSWIP_BM_CTRL_RMON_RAM1_RES BIT(0) /* Software Reset for RMON RAM 1 */ 128 #define GSWIP_BM_CTRL_RMON_RAM2_RES BIT(1) /* Software Reset for RMON RAM 2 */ 129 130 /* PCE */ 131 #define GSWIP_PCE_TBL_KEY(x) (0x447 - (x)) 132 #define GSWIP_PCE_TBL_MASK 0x448 133 #define GSWIP_PCE_TBL_VAL(x) (0x44D - (x)) 134 #define GSWIP_PCE_TBL_ADDR 0x44E 135 #define GSWIP_PCE_TBL_CTRL 0x44F 136 #define GSWIP_PCE_TBL_CTRL_BAS BIT(15) 137 #define GSWIP_PCE_TBL_CTRL_TYPE BIT(13) 138 #define GSWIP_PCE_TBL_CTRL_VLD BIT(12) 139 #define GSWIP_PCE_TBL_CTRL_KEYFORM BIT(11) 140 #define GSWIP_PCE_TBL_CTRL_GMAP_MASK GENMASK(10, 7) 141 #define GSWIP_PCE_TBL_CTRL_OPMOD_MASK GENMASK(6, 5) 142 #define GSWIP_PCE_TBL_CTRL_OPMOD_ADRD 0x00 143 #define GSWIP_PCE_TBL_CTRL_OPMOD_ADWR 0x20 144 #define GSWIP_PCE_TBL_CTRL_OPMOD_KSRD 0x40 145 #define GSWIP_PCE_TBL_CTRL_OPMOD_KSWR 0x60 146 #define GSWIP_PCE_TBL_CTRL_ADDR_MASK GENMASK(4, 0) 147 #define GSWIP_PCE_PMAP1 0x453 /* Monitoring port map */ 148 #define GSWIP_PCE_PMAP2 0x454 /* Default Multicast port map */ 149 #define GSWIP_PCE_PMAP3 0x455 /* Default Unknown Unicast port map */ 150 #define GSWIP_PCE_GCTRL_0 0x456 151 #define GSWIP_PCE_GCTRL_0_MC_VALID BIT(3) 152 #define GSWIP_PCE_GCTRL_0_VLAN BIT(14) /* VLAN aware Switching */ 153 #define GSWIP_PCE_GCTRL_1 0x457 154 #define GSWIP_PCE_GCTRL_1_MAC_GLOCK BIT(2) /* MAC Address table lock */ 155 #define GSWIP_PCE_GCTRL_1_MAC_GLOCK_MOD BIT(3) /* Mac address table lock forwarding mode */ 156 #define GSWIP_PCE_PCTRL_0p(p) (0x480 + ((p) * 0xA)) 157 #define GSWIP_PCE_PCTRL_0_INGRESS BIT(11) 158 #define GSWIP_PCE_PCTRL_0_PSTATE_LISTEN 0x0 159 #define GSWIP_PCE_PCTRL_0_PSTATE_RX 0x1 160 #define GSWIP_PCE_PCTRL_0_PSTATE_TX 0x2 161 #define GSWIP_PCE_PCTRL_0_PSTATE_LEARNING 0x3 162 #define GSWIP_PCE_PCTRL_0_PSTATE_FORWARDING 0x7 163 #define GSWIP_PCE_PCTRL_0_PSTATE_MASK GENMASK(2, 0) 164 165 #define GSWIP_MAC_FLEN 0x8C5 166 #define GSWIP_MAC_CTRL_2p(p) (0x905 + ((p) * 0xC)) 167 #define GSWIP_MAC_CTRL_2_MLEN BIT(3) /* Maximum Untagged Frame Lnegth */ 168 169 /* Ethernet Switch Fetch DMA Port Control Register */ 170 #define GSWIP_FDMA_PCTRLp(p) (0xA80 + ((p) * 0x6)) 171 #define GSWIP_FDMA_PCTRL_EN BIT(0) /* FDMA Port Enable */ 172 #define GSWIP_FDMA_PCTRL_STEN BIT(1) /* Special Tag Insertion Enable */ 173 #define GSWIP_FDMA_PCTRL_VLANMOD_MASK GENMASK(4, 3) /* VLAN Modification Control */ 174 #define GSWIP_FDMA_PCTRL_VLANMOD_SHIFT 3 /* VLAN Modification Control */ 175 #define GSWIP_FDMA_PCTRL_VLANMOD_DIS (0x0 << GSWIP_FDMA_PCTRL_VLANMOD_SHIFT) 176 #define GSWIP_FDMA_PCTRL_VLANMOD_PRIO (0x1 << GSWIP_FDMA_PCTRL_VLANMOD_SHIFT) 177 #define GSWIP_FDMA_PCTRL_VLANMOD_ID (0x2 << GSWIP_FDMA_PCTRL_VLANMOD_SHIFT) 178 #define GSWIP_FDMA_PCTRL_VLANMOD_BOTH (0x3 << GSWIP_FDMA_PCTRL_VLANMOD_SHIFT) 179 180 /* Ethernet Switch Store DMA Port Control Register */ 181 #define GSWIP_SDMA_PCTRLp(p) (0xBC0 + ((p) * 0x6)) 182 #define GSWIP_SDMA_PCTRL_EN BIT(0) /* SDMA Port Enable */ 183 #define GSWIP_SDMA_PCTRL_FCEN BIT(1) /* Flow Control Enable */ 184 #define GSWIP_SDMA_PCTRL_PAUFWD BIT(1) /* Pause Frame Forwarding */ 185 186 #define XRX200_GPHY_FW_ALIGN (16 * 1024) 187 188 struct gswip_hw_info { 189 int max_ports; 190 int cpu_port; 191 }; 192 193 struct xway_gphy_match_data { 194 char *fe_firmware_name; 195 char *ge_firmware_name; 196 }; 197 198 struct gswip_gphy_fw { 199 struct clk *clk_gate; 200 struct reset_control *reset; 201 u32 fw_addr_offset; 202 char *fw_name; 203 }; 204 205 struct gswip_priv { 206 __iomem void *gswip; 207 __iomem void *mdio; 208 __iomem void *mii; 209 const struct gswip_hw_info *hw_info; 210 const struct xway_gphy_match_data *gphy_fw_name_cfg; 211 struct dsa_switch *ds; 212 struct device *dev; 213 struct regmap *rcu_regmap; 214 int num_gphy_fw; 215 struct gswip_gphy_fw *gphy_fw; 216 }; 217 218 struct gswip_rmon_cnt_desc { 219 unsigned int size; 220 unsigned int offset; 221 const char *name; 222 }; 223 224 #define MIB_DESC(_size, _offset, _name) {.size = _size, .offset = _offset, .name = _name} 225 226 static const struct gswip_rmon_cnt_desc gswip_rmon_cnt[] = { 227 /** Receive Packet Count (only packets that are accepted and not discarded). */ 228 MIB_DESC(1, 0x1F, "RxGoodPkts"), 229 MIB_DESC(1, 0x23, "RxUnicastPkts"), 230 MIB_DESC(1, 0x22, "RxMulticastPkts"), 231 MIB_DESC(1, 0x21, "RxFCSErrorPkts"), 232 MIB_DESC(1, 0x1D, "RxUnderSizeGoodPkts"), 233 MIB_DESC(1, 0x1E, "RxUnderSizeErrorPkts"), 234 MIB_DESC(1, 0x1B, "RxOversizeGoodPkts"), 235 MIB_DESC(1, 0x1C, "RxOversizeErrorPkts"), 236 MIB_DESC(1, 0x20, "RxGoodPausePkts"), 237 MIB_DESC(1, 0x1A, "RxAlignErrorPkts"), 238 MIB_DESC(1, 0x12, "Rx64BytePkts"), 239 MIB_DESC(1, 0x13, "Rx127BytePkts"), 240 MIB_DESC(1, 0x14, "Rx255BytePkts"), 241 MIB_DESC(1, 0x15, "Rx511BytePkts"), 242 MIB_DESC(1, 0x16, "Rx1023BytePkts"), 243 /** Receive Size 1024-1522 (or more, if configured) Packet Count. */ 244 MIB_DESC(1, 0x17, "RxMaxBytePkts"), 245 MIB_DESC(1, 0x18, "RxDroppedPkts"), 246 MIB_DESC(1, 0x19, "RxFilteredPkts"), 247 MIB_DESC(2, 0x24, "RxGoodBytes"), 248 MIB_DESC(2, 0x26, "RxBadBytes"), 249 MIB_DESC(1, 0x11, "TxAcmDroppedPkts"), 250 MIB_DESC(1, 0x0C, "TxGoodPkts"), 251 MIB_DESC(1, 0x06, "TxUnicastPkts"), 252 MIB_DESC(1, 0x07, "TxMulticastPkts"), 253 MIB_DESC(1, 0x00, "Tx64BytePkts"), 254 MIB_DESC(1, 0x01, "Tx127BytePkts"), 255 MIB_DESC(1, 0x02, "Tx255BytePkts"), 256 MIB_DESC(1, 0x03, "Tx511BytePkts"), 257 MIB_DESC(1, 0x04, "Tx1023BytePkts"), 258 /** Transmit Size 1024-1522 (or more, if configured) Packet Count. */ 259 MIB_DESC(1, 0x05, "TxMaxBytePkts"), 260 MIB_DESC(1, 0x08, "TxSingleCollCount"), 261 MIB_DESC(1, 0x09, "TxMultCollCount"), 262 MIB_DESC(1, 0x0A, "TxLateCollCount"), 263 MIB_DESC(1, 0x0B, "TxExcessCollCount"), 264 MIB_DESC(1, 0x0D, "TxPauseCount"), 265 MIB_DESC(1, 0x10, "TxDroppedPkts"), 266 MIB_DESC(2, 0x0E, "TxGoodBytes"), 267 }; 268 269 static u32 gswip_switch_r(struct gswip_priv *priv, u32 offset) 270 { 271 return __raw_readl(priv->gswip + (offset * 4)); 272 } 273 274 static void gswip_switch_w(struct gswip_priv *priv, u32 val, u32 offset) 275 { 276 __raw_writel(val, priv->gswip + (offset * 4)); 277 } 278 279 static void gswip_switch_mask(struct gswip_priv *priv, u32 clear, u32 set, 280 u32 offset) 281 { 282 u32 val = gswip_switch_r(priv, offset); 283 284 val &= ~(clear); 285 val |= set; 286 gswip_switch_w(priv, val, offset); 287 } 288 289 static u32 gswip_switch_r_timeout(struct gswip_priv *priv, u32 offset, 290 u32 cleared) 291 { 292 u32 val; 293 294 return readx_poll_timeout(__raw_readl, priv->gswip + (offset * 4), val, 295 (val & cleared) == 0, 20, 50000); 296 } 297 298 static u32 gswip_mdio_r(struct gswip_priv *priv, u32 offset) 299 { 300 return __raw_readl(priv->mdio + (offset * 4)); 301 } 302 303 static void gswip_mdio_w(struct gswip_priv *priv, u32 val, u32 offset) 304 { 305 __raw_writel(val, priv->mdio + (offset * 4)); 306 } 307 308 static void gswip_mdio_mask(struct gswip_priv *priv, u32 clear, u32 set, 309 u32 offset) 310 { 311 u32 val = gswip_mdio_r(priv, offset); 312 313 val &= ~(clear); 314 val |= set; 315 gswip_mdio_w(priv, val, offset); 316 } 317 318 static u32 gswip_mii_r(struct gswip_priv *priv, u32 offset) 319 { 320 return __raw_readl(priv->mii + (offset * 4)); 321 } 322 323 static void gswip_mii_w(struct gswip_priv *priv, u32 val, u32 offset) 324 { 325 __raw_writel(val, priv->mii + (offset * 4)); 326 } 327 328 static void gswip_mii_mask(struct gswip_priv *priv, u32 clear, u32 set, 329 u32 offset) 330 { 331 u32 val = gswip_mii_r(priv, offset); 332 333 val &= ~(clear); 334 val |= set; 335 gswip_mii_w(priv, val, offset); 336 } 337 338 static void gswip_mii_mask_cfg(struct gswip_priv *priv, u32 clear, u32 set, 339 int port) 340 { 341 switch (port) { 342 case 0: 343 gswip_mii_mask(priv, clear, set, GSWIP_MII_CFG0); 344 break; 345 case 1: 346 gswip_mii_mask(priv, clear, set, GSWIP_MII_CFG1); 347 break; 348 case 5: 349 gswip_mii_mask(priv, clear, set, GSWIP_MII_CFG5); 350 break; 351 } 352 } 353 354 static void gswip_mii_mask_pcdu(struct gswip_priv *priv, u32 clear, u32 set, 355 int port) 356 { 357 switch (port) { 358 case 0: 359 gswip_mii_mask(priv, clear, set, GSWIP_MII_PCDU0); 360 break; 361 case 1: 362 gswip_mii_mask(priv, clear, set, GSWIP_MII_PCDU1); 363 break; 364 case 5: 365 gswip_mii_mask(priv, clear, set, GSWIP_MII_PCDU5); 366 break; 367 } 368 } 369 370 static int gswip_mdio_poll(struct gswip_priv *priv) 371 { 372 int cnt = 100; 373 374 while (likely(cnt--)) { 375 u32 ctrl = gswip_mdio_r(priv, GSWIP_MDIO_CTRL); 376 377 if ((ctrl & GSWIP_MDIO_CTRL_BUSY) == 0) 378 return 0; 379 usleep_range(20, 40); 380 } 381 382 return -ETIMEDOUT; 383 } 384 385 static int gswip_mdio_wr(struct mii_bus *bus, int addr, int reg, u16 val) 386 { 387 struct gswip_priv *priv = bus->priv; 388 int err; 389 390 err = gswip_mdio_poll(priv); 391 if (err) { 392 dev_err(&bus->dev, "waiting for MDIO bus busy timed out\n"); 393 return err; 394 } 395 396 gswip_mdio_w(priv, val, GSWIP_MDIO_WRITE); 397 gswip_mdio_w(priv, GSWIP_MDIO_CTRL_BUSY | GSWIP_MDIO_CTRL_WR | 398 ((addr & GSWIP_MDIO_CTRL_PHYAD_MASK) << GSWIP_MDIO_CTRL_PHYAD_SHIFT) | 399 (reg & GSWIP_MDIO_CTRL_REGAD_MASK), 400 GSWIP_MDIO_CTRL); 401 402 return 0; 403 } 404 405 static int gswip_mdio_rd(struct mii_bus *bus, int addr, int reg) 406 { 407 struct gswip_priv *priv = bus->priv; 408 int err; 409 410 err = gswip_mdio_poll(priv); 411 if (err) { 412 dev_err(&bus->dev, "waiting for MDIO bus busy timed out\n"); 413 return err; 414 } 415 416 gswip_mdio_w(priv, GSWIP_MDIO_CTRL_BUSY | GSWIP_MDIO_CTRL_RD | 417 ((addr & GSWIP_MDIO_CTRL_PHYAD_MASK) << GSWIP_MDIO_CTRL_PHYAD_SHIFT) | 418 (reg & GSWIP_MDIO_CTRL_REGAD_MASK), 419 GSWIP_MDIO_CTRL); 420 421 err = gswip_mdio_poll(priv); 422 if (err) { 423 dev_err(&bus->dev, "waiting for MDIO bus busy timed out\n"); 424 return err; 425 } 426 427 return gswip_mdio_r(priv, GSWIP_MDIO_READ); 428 } 429 430 static int gswip_mdio(struct gswip_priv *priv, struct device_node *mdio_np) 431 { 432 struct dsa_switch *ds = priv->ds; 433 434 ds->slave_mii_bus = devm_mdiobus_alloc(priv->dev); 435 if (!ds->slave_mii_bus) 436 return -ENOMEM; 437 438 ds->slave_mii_bus->priv = priv; 439 ds->slave_mii_bus->read = gswip_mdio_rd; 440 ds->slave_mii_bus->write = gswip_mdio_wr; 441 ds->slave_mii_bus->name = "lantiq,xrx200-mdio"; 442 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "%s-mii", 443 dev_name(priv->dev)); 444 ds->slave_mii_bus->parent = priv->dev; 445 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask; 446 447 return of_mdiobus_register(ds->slave_mii_bus, mdio_np); 448 } 449 450 static int gswip_port_enable(struct dsa_switch *ds, int port, 451 struct phy_device *phydev) 452 { 453 struct gswip_priv *priv = ds->priv; 454 455 /* RMON Counter Enable for port */ 456 gswip_switch_w(priv, GSWIP_BM_PCFG_CNTEN, GSWIP_BM_PCFGp(port)); 457 458 /* enable port fetch/store dma & VLAN Modification */ 459 gswip_switch_mask(priv, 0, GSWIP_FDMA_PCTRL_EN | 460 GSWIP_FDMA_PCTRL_VLANMOD_BOTH, 461 GSWIP_FDMA_PCTRLp(port)); 462 gswip_switch_mask(priv, 0, GSWIP_SDMA_PCTRL_EN, 463 GSWIP_SDMA_PCTRLp(port)); 464 gswip_switch_mask(priv, 0, GSWIP_PCE_PCTRL_0_INGRESS, 465 GSWIP_PCE_PCTRL_0p(port)); 466 467 if (!dsa_is_cpu_port(ds, port)) { 468 u32 macconf = GSWIP_MDIO_PHY_LINK_AUTO | 469 GSWIP_MDIO_PHY_SPEED_AUTO | 470 GSWIP_MDIO_PHY_FDUP_AUTO | 471 GSWIP_MDIO_PHY_FCONTX_AUTO | 472 GSWIP_MDIO_PHY_FCONRX_AUTO | 473 (phydev->mdio.addr & GSWIP_MDIO_PHY_ADDR_MASK); 474 475 gswip_mdio_w(priv, macconf, GSWIP_MDIO_PHYp(port)); 476 /* Activate MDIO auto polling */ 477 gswip_mdio_mask(priv, 0, BIT(port), GSWIP_MDIO_MDC_CFG0); 478 } 479 480 return 0; 481 } 482 483 static void gswip_port_disable(struct dsa_switch *ds, int port, 484 struct phy_device *phy) 485 { 486 struct gswip_priv *priv = ds->priv; 487 488 if (!dsa_is_cpu_port(ds, port)) { 489 gswip_mdio_mask(priv, GSWIP_MDIO_PHY_LINK_DOWN, 490 GSWIP_MDIO_PHY_LINK_MASK, 491 GSWIP_MDIO_PHYp(port)); 492 /* Deactivate MDIO auto polling */ 493 gswip_mdio_mask(priv, BIT(port), 0, GSWIP_MDIO_MDC_CFG0); 494 } 495 496 gswip_switch_mask(priv, GSWIP_FDMA_PCTRL_EN, 0, 497 GSWIP_FDMA_PCTRLp(port)); 498 gswip_switch_mask(priv, GSWIP_SDMA_PCTRL_EN, 0, 499 GSWIP_SDMA_PCTRLp(port)); 500 } 501 502 static int gswip_pce_load_microcode(struct gswip_priv *priv) 503 { 504 int i; 505 int err; 506 507 gswip_switch_mask(priv, GSWIP_PCE_TBL_CTRL_ADDR_MASK | 508 GSWIP_PCE_TBL_CTRL_OPMOD_MASK, 509 GSWIP_PCE_TBL_CTRL_OPMOD_ADWR, GSWIP_PCE_TBL_CTRL); 510 gswip_switch_w(priv, 0, GSWIP_PCE_TBL_MASK); 511 512 for (i = 0; i < ARRAY_SIZE(gswip_pce_microcode); i++) { 513 gswip_switch_w(priv, i, GSWIP_PCE_TBL_ADDR); 514 gswip_switch_w(priv, gswip_pce_microcode[i].val_0, 515 GSWIP_PCE_TBL_VAL(0)); 516 gswip_switch_w(priv, gswip_pce_microcode[i].val_1, 517 GSWIP_PCE_TBL_VAL(1)); 518 gswip_switch_w(priv, gswip_pce_microcode[i].val_2, 519 GSWIP_PCE_TBL_VAL(2)); 520 gswip_switch_w(priv, gswip_pce_microcode[i].val_3, 521 GSWIP_PCE_TBL_VAL(3)); 522 523 /* start the table access: */ 524 gswip_switch_mask(priv, 0, GSWIP_PCE_TBL_CTRL_BAS, 525 GSWIP_PCE_TBL_CTRL); 526 err = gswip_switch_r_timeout(priv, GSWIP_PCE_TBL_CTRL, 527 GSWIP_PCE_TBL_CTRL_BAS); 528 if (err) 529 return err; 530 } 531 532 /* tell the switch that the microcode is loaded */ 533 gswip_switch_mask(priv, 0, GSWIP_PCE_GCTRL_0_MC_VALID, 534 GSWIP_PCE_GCTRL_0); 535 536 return 0; 537 } 538 539 static int gswip_setup(struct dsa_switch *ds) 540 { 541 struct gswip_priv *priv = ds->priv; 542 unsigned int cpu_port = priv->hw_info->cpu_port; 543 int i; 544 int err; 545 546 gswip_switch_w(priv, GSWIP_SWRES_R0, GSWIP_SWRES); 547 usleep_range(5000, 10000); 548 gswip_switch_w(priv, 0, GSWIP_SWRES); 549 550 /* disable port fetch/store dma on all ports */ 551 for (i = 0; i < priv->hw_info->max_ports; i++) 552 gswip_port_disable(ds, i, NULL); 553 554 /* enable Switch */ 555 gswip_mdio_mask(priv, 0, GSWIP_MDIO_GLOB_ENABLE, GSWIP_MDIO_GLOB); 556 557 err = gswip_pce_load_microcode(priv); 558 if (err) { 559 dev_err(priv->dev, "writing PCE microcode failed, %i", err); 560 return err; 561 } 562 563 /* Default unknown Broadcast/Multicast/Unicast port maps */ 564 gswip_switch_w(priv, BIT(cpu_port), GSWIP_PCE_PMAP1); 565 gswip_switch_w(priv, BIT(cpu_port), GSWIP_PCE_PMAP2); 566 gswip_switch_w(priv, BIT(cpu_port), GSWIP_PCE_PMAP3); 567 568 /* disable PHY auto polling */ 569 gswip_mdio_w(priv, 0x0, GSWIP_MDIO_MDC_CFG0); 570 /* Configure the MDIO Clock 2.5 MHz */ 571 gswip_mdio_mask(priv, 0xff, 0x09, GSWIP_MDIO_MDC_CFG1); 572 573 /* Disable the xMII link */ 574 gswip_mii_mask_cfg(priv, GSWIP_MII_CFG_EN, 0, 0); 575 gswip_mii_mask_cfg(priv, GSWIP_MII_CFG_EN, 0, 1); 576 gswip_mii_mask_cfg(priv, GSWIP_MII_CFG_EN, 0, 5); 577 578 /* enable special tag insertion on cpu port */ 579 gswip_switch_mask(priv, 0, GSWIP_FDMA_PCTRL_STEN, 580 GSWIP_FDMA_PCTRLp(cpu_port)); 581 582 gswip_switch_mask(priv, 0, GSWIP_MAC_CTRL_2_MLEN, 583 GSWIP_MAC_CTRL_2p(cpu_port)); 584 gswip_switch_w(priv, VLAN_ETH_FRAME_LEN + 8, GSWIP_MAC_FLEN); 585 gswip_switch_mask(priv, 0, GSWIP_BM_QUEUE_GCTRL_GL_MOD, 586 GSWIP_BM_QUEUE_GCTRL); 587 588 /* VLAN aware Switching */ 589 gswip_switch_mask(priv, 0, GSWIP_PCE_GCTRL_0_VLAN, GSWIP_PCE_GCTRL_0); 590 591 /* Mac Address Table Lock */ 592 gswip_switch_mask(priv, 0, GSWIP_PCE_GCTRL_1_MAC_GLOCK | 593 GSWIP_PCE_GCTRL_1_MAC_GLOCK_MOD, 594 GSWIP_PCE_GCTRL_1); 595 596 gswip_port_enable(ds, cpu_port, NULL); 597 return 0; 598 } 599 600 static enum dsa_tag_protocol gswip_get_tag_protocol(struct dsa_switch *ds, 601 int port) 602 { 603 return DSA_TAG_PROTO_GSWIP; 604 } 605 606 static void gswip_phylink_validate(struct dsa_switch *ds, int port, 607 unsigned long *supported, 608 struct phylink_link_state *state) 609 { 610 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, }; 611 612 switch (port) { 613 case 0: 614 case 1: 615 if (!phy_interface_mode_is_rgmii(state->interface) && 616 state->interface != PHY_INTERFACE_MODE_MII && 617 state->interface != PHY_INTERFACE_MODE_REVMII && 618 state->interface != PHY_INTERFACE_MODE_RMII) { 619 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS); 620 dev_err(ds->dev, 621 "Unsupported interface: %d\n", state->interface); 622 return; 623 } 624 break; 625 case 2: 626 case 3: 627 case 4: 628 if (state->interface != PHY_INTERFACE_MODE_INTERNAL) { 629 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS); 630 dev_err(ds->dev, 631 "Unsupported interface: %d\n", state->interface); 632 return; 633 } 634 break; 635 case 5: 636 if (!phy_interface_mode_is_rgmii(state->interface) && 637 state->interface != PHY_INTERFACE_MODE_INTERNAL) { 638 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS); 639 dev_err(ds->dev, 640 "Unsupported interface: %d\n", state->interface); 641 return; 642 } 643 break; 644 } 645 646 /* Allow all the expected bits */ 647 phylink_set(mask, Autoneg); 648 phylink_set_port_modes(mask); 649 phylink_set(mask, Pause); 650 phylink_set(mask, Asym_Pause); 651 652 /* With the exclusion of MII and Reverse MII, we support Gigabit, 653 * including Half duplex 654 */ 655 if (state->interface != PHY_INTERFACE_MODE_MII && 656 state->interface != PHY_INTERFACE_MODE_REVMII) { 657 phylink_set(mask, 1000baseT_Full); 658 phylink_set(mask, 1000baseT_Half); 659 } 660 661 phylink_set(mask, 10baseT_Half); 662 phylink_set(mask, 10baseT_Full); 663 phylink_set(mask, 100baseT_Half); 664 phylink_set(mask, 100baseT_Full); 665 666 bitmap_and(supported, supported, mask, 667 __ETHTOOL_LINK_MODE_MASK_NBITS); 668 bitmap_and(state->advertising, state->advertising, mask, 669 __ETHTOOL_LINK_MODE_MASK_NBITS); 670 } 671 672 static void gswip_phylink_mac_config(struct dsa_switch *ds, int port, 673 unsigned int mode, 674 const struct phylink_link_state *state) 675 { 676 struct gswip_priv *priv = ds->priv; 677 u32 miicfg = 0; 678 679 miicfg |= GSWIP_MII_CFG_LDCLKDIS; 680 681 switch (state->interface) { 682 case PHY_INTERFACE_MODE_MII: 683 case PHY_INTERFACE_MODE_INTERNAL: 684 miicfg |= GSWIP_MII_CFG_MODE_MIIM; 685 break; 686 case PHY_INTERFACE_MODE_REVMII: 687 miicfg |= GSWIP_MII_CFG_MODE_MIIP; 688 break; 689 case PHY_INTERFACE_MODE_RMII: 690 miicfg |= GSWIP_MII_CFG_MODE_RMIIM; 691 break; 692 case PHY_INTERFACE_MODE_RGMII: 693 case PHY_INTERFACE_MODE_RGMII_ID: 694 case PHY_INTERFACE_MODE_RGMII_RXID: 695 case PHY_INTERFACE_MODE_RGMII_TXID: 696 miicfg |= GSWIP_MII_CFG_MODE_RGMII; 697 break; 698 default: 699 dev_err(ds->dev, 700 "Unsupported interface: %d\n", state->interface); 701 return; 702 } 703 gswip_mii_mask_cfg(priv, GSWIP_MII_CFG_MODE_MASK, miicfg, port); 704 705 switch (state->interface) { 706 case PHY_INTERFACE_MODE_RGMII_ID: 707 gswip_mii_mask_pcdu(priv, GSWIP_MII_PCDU_TXDLY_MASK | 708 GSWIP_MII_PCDU_RXDLY_MASK, 0, port); 709 break; 710 case PHY_INTERFACE_MODE_RGMII_RXID: 711 gswip_mii_mask_pcdu(priv, GSWIP_MII_PCDU_RXDLY_MASK, 0, port); 712 break; 713 case PHY_INTERFACE_MODE_RGMII_TXID: 714 gswip_mii_mask_pcdu(priv, GSWIP_MII_PCDU_TXDLY_MASK, 0, port); 715 break; 716 default: 717 break; 718 } 719 } 720 721 static void gswip_phylink_mac_link_down(struct dsa_switch *ds, int port, 722 unsigned int mode, 723 phy_interface_t interface) 724 { 725 struct gswip_priv *priv = ds->priv; 726 727 gswip_mii_mask_cfg(priv, GSWIP_MII_CFG_EN, 0, port); 728 } 729 730 static void gswip_phylink_mac_link_up(struct dsa_switch *ds, int port, 731 unsigned int mode, 732 phy_interface_t interface, 733 struct phy_device *phydev) 734 { 735 struct gswip_priv *priv = ds->priv; 736 737 /* Enable the xMII interface only for the external PHY */ 738 if (interface != PHY_INTERFACE_MODE_INTERNAL) 739 gswip_mii_mask_cfg(priv, 0, GSWIP_MII_CFG_EN, port); 740 } 741 742 static void gswip_get_strings(struct dsa_switch *ds, int port, u32 stringset, 743 uint8_t *data) 744 { 745 int i; 746 747 if (stringset != ETH_SS_STATS) 748 return; 749 750 for (i = 0; i < ARRAY_SIZE(gswip_rmon_cnt); i++) 751 strncpy(data + i * ETH_GSTRING_LEN, gswip_rmon_cnt[i].name, 752 ETH_GSTRING_LEN); 753 } 754 755 static u32 gswip_bcm_ram_entry_read(struct gswip_priv *priv, u32 table, 756 u32 index) 757 { 758 u32 result; 759 int err; 760 761 gswip_switch_w(priv, index, GSWIP_BM_RAM_ADDR); 762 gswip_switch_mask(priv, GSWIP_BM_RAM_CTRL_ADDR_MASK | 763 GSWIP_BM_RAM_CTRL_OPMOD, 764 table | GSWIP_BM_RAM_CTRL_BAS, 765 GSWIP_BM_RAM_CTRL); 766 767 err = gswip_switch_r_timeout(priv, GSWIP_BM_RAM_CTRL, 768 GSWIP_BM_RAM_CTRL_BAS); 769 if (err) { 770 dev_err(priv->dev, "timeout while reading table: %u, index: %u", 771 table, index); 772 return 0; 773 } 774 775 result = gswip_switch_r(priv, GSWIP_BM_RAM_VAL(0)); 776 result |= gswip_switch_r(priv, GSWIP_BM_RAM_VAL(1)) << 16; 777 778 return result; 779 } 780 781 static void gswip_get_ethtool_stats(struct dsa_switch *ds, int port, 782 uint64_t *data) 783 { 784 struct gswip_priv *priv = ds->priv; 785 const struct gswip_rmon_cnt_desc *rmon_cnt; 786 int i; 787 u64 high; 788 789 for (i = 0; i < ARRAY_SIZE(gswip_rmon_cnt); i++) { 790 rmon_cnt = &gswip_rmon_cnt[i]; 791 792 data[i] = gswip_bcm_ram_entry_read(priv, port, 793 rmon_cnt->offset); 794 if (rmon_cnt->size == 2) { 795 high = gswip_bcm_ram_entry_read(priv, port, 796 rmon_cnt->offset + 1); 797 data[i] |= high << 32; 798 } 799 } 800 } 801 802 static int gswip_get_sset_count(struct dsa_switch *ds, int port, int sset) 803 { 804 if (sset != ETH_SS_STATS) 805 return 0; 806 807 return ARRAY_SIZE(gswip_rmon_cnt); 808 } 809 810 static const struct dsa_switch_ops gswip_switch_ops = { 811 .get_tag_protocol = gswip_get_tag_protocol, 812 .setup = gswip_setup, 813 .port_enable = gswip_port_enable, 814 .port_disable = gswip_port_disable, 815 .phylink_validate = gswip_phylink_validate, 816 .phylink_mac_config = gswip_phylink_mac_config, 817 .phylink_mac_link_down = gswip_phylink_mac_link_down, 818 .phylink_mac_link_up = gswip_phylink_mac_link_up, 819 .get_strings = gswip_get_strings, 820 .get_ethtool_stats = gswip_get_ethtool_stats, 821 .get_sset_count = gswip_get_sset_count, 822 }; 823 824 static const struct xway_gphy_match_data xrx200a1x_gphy_data = { 825 .fe_firmware_name = "lantiq/xrx200_phy22f_a14.bin", 826 .ge_firmware_name = "lantiq/xrx200_phy11g_a14.bin", 827 }; 828 829 static const struct xway_gphy_match_data xrx200a2x_gphy_data = { 830 .fe_firmware_name = "lantiq/xrx200_phy22f_a22.bin", 831 .ge_firmware_name = "lantiq/xrx200_phy11g_a22.bin", 832 }; 833 834 static const struct xway_gphy_match_data xrx300_gphy_data = { 835 .fe_firmware_name = "lantiq/xrx300_phy22f_a21.bin", 836 .ge_firmware_name = "lantiq/xrx300_phy11g_a21.bin", 837 }; 838 839 static const struct of_device_id xway_gphy_match[] = { 840 { .compatible = "lantiq,xrx200-gphy-fw", .data = NULL }, 841 { .compatible = "lantiq,xrx200a1x-gphy-fw", .data = &xrx200a1x_gphy_data }, 842 { .compatible = "lantiq,xrx200a2x-gphy-fw", .data = &xrx200a2x_gphy_data }, 843 { .compatible = "lantiq,xrx300-gphy-fw", .data = &xrx300_gphy_data }, 844 { .compatible = "lantiq,xrx330-gphy-fw", .data = &xrx300_gphy_data }, 845 {}, 846 }; 847 848 static int gswip_gphy_fw_load(struct gswip_priv *priv, struct gswip_gphy_fw *gphy_fw) 849 { 850 struct device *dev = priv->dev; 851 const struct firmware *fw; 852 void *fw_addr; 853 dma_addr_t dma_addr; 854 dma_addr_t dev_addr; 855 size_t size; 856 int ret; 857 858 ret = clk_prepare_enable(gphy_fw->clk_gate); 859 if (ret) 860 return ret; 861 862 reset_control_assert(gphy_fw->reset); 863 864 ret = request_firmware(&fw, gphy_fw->fw_name, dev); 865 if (ret) { 866 dev_err(dev, "failed to load firmware: %s, error: %i\n", 867 gphy_fw->fw_name, ret); 868 return ret; 869 } 870 871 /* GPHY cores need the firmware code in a persistent and contiguous 872 * memory area with a 16 kB boundary aligned start address. 873 */ 874 size = fw->size + XRX200_GPHY_FW_ALIGN; 875 876 fw_addr = dmam_alloc_coherent(dev, size, &dma_addr, GFP_KERNEL); 877 if (fw_addr) { 878 fw_addr = PTR_ALIGN(fw_addr, XRX200_GPHY_FW_ALIGN); 879 dev_addr = ALIGN(dma_addr, XRX200_GPHY_FW_ALIGN); 880 memcpy(fw_addr, fw->data, fw->size); 881 } else { 882 dev_err(dev, "failed to alloc firmware memory\n"); 883 release_firmware(fw); 884 return -ENOMEM; 885 } 886 887 release_firmware(fw); 888 889 ret = regmap_write(priv->rcu_regmap, gphy_fw->fw_addr_offset, dev_addr); 890 if (ret) 891 return ret; 892 893 reset_control_deassert(gphy_fw->reset); 894 895 return ret; 896 } 897 898 static int gswip_gphy_fw_probe(struct gswip_priv *priv, 899 struct gswip_gphy_fw *gphy_fw, 900 struct device_node *gphy_fw_np, int i) 901 { 902 struct device *dev = priv->dev; 903 u32 gphy_mode; 904 int ret; 905 char gphyname[10]; 906 907 snprintf(gphyname, sizeof(gphyname), "gphy%d", i); 908 909 gphy_fw->clk_gate = devm_clk_get(dev, gphyname); 910 if (IS_ERR(gphy_fw->clk_gate)) { 911 dev_err(dev, "Failed to lookup gate clock\n"); 912 return PTR_ERR(gphy_fw->clk_gate); 913 } 914 915 ret = of_property_read_u32(gphy_fw_np, "reg", &gphy_fw->fw_addr_offset); 916 if (ret) 917 return ret; 918 919 ret = of_property_read_u32(gphy_fw_np, "lantiq,gphy-mode", &gphy_mode); 920 /* Default to GE mode */ 921 if (ret) 922 gphy_mode = GPHY_MODE_GE; 923 924 switch (gphy_mode) { 925 case GPHY_MODE_FE: 926 gphy_fw->fw_name = priv->gphy_fw_name_cfg->fe_firmware_name; 927 break; 928 case GPHY_MODE_GE: 929 gphy_fw->fw_name = priv->gphy_fw_name_cfg->ge_firmware_name; 930 break; 931 default: 932 dev_err(dev, "Unknown GPHY mode %d\n", gphy_mode); 933 return -EINVAL; 934 } 935 936 gphy_fw->reset = of_reset_control_array_get_exclusive(gphy_fw_np); 937 if (IS_ERR(priv->gphy_fw)) { 938 if (PTR_ERR(priv->gphy_fw) != -EPROBE_DEFER) 939 dev_err(dev, "Failed to lookup gphy reset\n"); 940 return PTR_ERR(priv->gphy_fw); 941 } 942 943 return gswip_gphy_fw_load(priv, gphy_fw); 944 } 945 946 static void gswip_gphy_fw_remove(struct gswip_priv *priv, 947 struct gswip_gphy_fw *gphy_fw) 948 { 949 int ret; 950 951 /* check if the device was fully probed */ 952 if (!gphy_fw->fw_name) 953 return; 954 955 ret = regmap_write(priv->rcu_regmap, gphy_fw->fw_addr_offset, 0); 956 if (ret) 957 dev_err(priv->dev, "can not reset GPHY FW pointer"); 958 959 clk_disable_unprepare(gphy_fw->clk_gate); 960 961 reset_control_put(gphy_fw->reset); 962 } 963 964 static int gswip_gphy_fw_list(struct gswip_priv *priv, 965 struct device_node *gphy_fw_list_np, u32 version) 966 { 967 struct device *dev = priv->dev; 968 struct device_node *gphy_fw_np; 969 const struct of_device_id *match; 970 int err; 971 int i = 0; 972 973 /* The The VRX200 rev 1.1 uses the GSWIP 2.0 and needs the older 974 * GPHY firmware. The VRX200 rev 1.2 uses the GSWIP 2.1 and also 975 * needs a different GPHY firmware. 976 */ 977 if (of_device_is_compatible(gphy_fw_list_np, "lantiq,xrx200-gphy-fw")) { 978 switch (version) { 979 case GSWIP_VERSION_2_0: 980 priv->gphy_fw_name_cfg = &xrx200a1x_gphy_data; 981 break; 982 case GSWIP_VERSION_2_1: 983 priv->gphy_fw_name_cfg = &xrx200a2x_gphy_data; 984 break; 985 default: 986 dev_err(dev, "unknown GSWIP version: 0x%x", version); 987 return -ENOENT; 988 } 989 } 990 991 match = of_match_node(xway_gphy_match, gphy_fw_list_np); 992 if (match && match->data) 993 priv->gphy_fw_name_cfg = match->data; 994 995 if (!priv->gphy_fw_name_cfg) { 996 dev_err(dev, "GPHY compatible type not supported"); 997 return -ENOENT; 998 } 999 1000 priv->num_gphy_fw = of_get_available_child_count(gphy_fw_list_np); 1001 if (!priv->num_gphy_fw) 1002 return -ENOENT; 1003 1004 priv->rcu_regmap = syscon_regmap_lookup_by_phandle(gphy_fw_list_np, 1005 "lantiq,rcu"); 1006 if (IS_ERR(priv->rcu_regmap)) 1007 return PTR_ERR(priv->rcu_regmap); 1008 1009 priv->gphy_fw = devm_kmalloc_array(dev, priv->num_gphy_fw, 1010 sizeof(*priv->gphy_fw), 1011 GFP_KERNEL | __GFP_ZERO); 1012 if (!priv->gphy_fw) 1013 return -ENOMEM; 1014 1015 for_each_available_child_of_node(gphy_fw_list_np, gphy_fw_np) { 1016 err = gswip_gphy_fw_probe(priv, &priv->gphy_fw[i], 1017 gphy_fw_np, i); 1018 if (err) 1019 goto remove_gphy; 1020 i++; 1021 } 1022 1023 return 0; 1024 1025 remove_gphy: 1026 for (i = 0; i < priv->num_gphy_fw; i++) 1027 gswip_gphy_fw_remove(priv, &priv->gphy_fw[i]); 1028 return err; 1029 } 1030 1031 static int gswip_probe(struct platform_device *pdev) 1032 { 1033 struct gswip_priv *priv; 1034 struct resource *gswip_res, *mdio_res, *mii_res; 1035 struct device_node *mdio_np, *gphy_fw_np; 1036 struct device *dev = &pdev->dev; 1037 int err; 1038 int i; 1039 u32 version; 1040 1041 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 1042 if (!priv) 1043 return -ENOMEM; 1044 1045 gswip_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1046 priv->gswip = devm_ioremap_resource(dev, gswip_res); 1047 if (!priv->gswip) 1048 return -ENOMEM; 1049 1050 mdio_res = platform_get_resource(pdev, IORESOURCE_MEM, 1); 1051 priv->mdio = devm_ioremap_resource(dev, mdio_res); 1052 if (!priv->mdio) 1053 return -ENOMEM; 1054 1055 mii_res = platform_get_resource(pdev, IORESOURCE_MEM, 2); 1056 priv->mii = devm_ioremap_resource(dev, mii_res); 1057 if (!priv->mii) 1058 return -ENOMEM; 1059 1060 priv->hw_info = of_device_get_match_data(dev); 1061 if (!priv->hw_info) 1062 return -EINVAL; 1063 1064 priv->ds = dsa_switch_alloc(dev, priv->hw_info->max_ports); 1065 if (!priv->ds) 1066 return -ENOMEM; 1067 1068 priv->ds->priv = priv; 1069 priv->ds->ops = &gswip_switch_ops; 1070 priv->dev = dev; 1071 version = gswip_switch_r(priv, GSWIP_VERSION); 1072 1073 /* bring up the mdio bus */ 1074 gphy_fw_np = of_find_compatible_node(pdev->dev.of_node, NULL, 1075 "lantiq,gphy-fw"); 1076 if (gphy_fw_np) { 1077 err = gswip_gphy_fw_list(priv, gphy_fw_np, version); 1078 if (err) { 1079 dev_err(dev, "gphy fw probe failed\n"); 1080 return err; 1081 } 1082 } 1083 1084 /* bring up the mdio bus */ 1085 mdio_np = of_find_compatible_node(pdev->dev.of_node, NULL, 1086 "lantiq,xrx200-mdio"); 1087 if (mdio_np) { 1088 err = gswip_mdio(priv, mdio_np); 1089 if (err) { 1090 dev_err(dev, "mdio probe failed\n"); 1091 goto gphy_fw; 1092 } 1093 } 1094 1095 err = dsa_register_switch(priv->ds); 1096 if (err) { 1097 dev_err(dev, "dsa switch register failed: %i\n", err); 1098 goto mdio_bus; 1099 } 1100 if (priv->ds->dst->cpu_dp->index != priv->hw_info->cpu_port) { 1101 dev_err(dev, "wrong CPU port defined, HW only supports port: %i", 1102 priv->hw_info->cpu_port); 1103 err = -EINVAL; 1104 goto mdio_bus; 1105 } 1106 1107 platform_set_drvdata(pdev, priv); 1108 1109 dev_info(dev, "probed GSWIP version %lx mod %lx\n", 1110 (version & GSWIP_VERSION_REV_MASK) >> GSWIP_VERSION_REV_SHIFT, 1111 (version & GSWIP_VERSION_MOD_MASK) >> GSWIP_VERSION_MOD_SHIFT); 1112 return 0; 1113 1114 mdio_bus: 1115 if (mdio_np) 1116 mdiobus_unregister(priv->ds->slave_mii_bus); 1117 gphy_fw: 1118 for (i = 0; i < priv->num_gphy_fw; i++) 1119 gswip_gphy_fw_remove(priv, &priv->gphy_fw[i]); 1120 return err; 1121 } 1122 1123 static int gswip_remove(struct platform_device *pdev) 1124 { 1125 struct gswip_priv *priv = platform_get_drvdata(pdev); 1126 int i; 1127 1128 if (!priv) 1129 return 0; 1130 1131 /* disable the switch */ 1132 gswip_mdio_mask(priv, GSWIP_MDIO_GLOB_ENABLE, 0, GSWIP_MDIO_GLOB); 1133 1134 dsa_unregister_switch(priv->ds); 1135 1136 if (priv->ds->slave_mii_bus) 1137 mdiobus_unregister(priv->ds->slave_mii_bus); 1138 1139 for (i = 0; i < priv->num_gphy_fw; i++) 1140 gswip_gphy_fw_remove(priv, &priv->gphy_fw[i]); 1141 1142 return 0; 1143 } 1144 1145 static const struct gswip_hw_info gswip_xrx200 = { 1146 .max_ports = 7, 1147 .cpu_port = 6, 1148 }; 1149 1150 static const struct of_device_id gswip_of_match[] = { 1151 { .compatible = "lantiq,xrx200-gswip", .data = &gswip_xrx200 }, 1152 {}, 1153 }; 1154 MODULE_DEVICE_TABLE(of, gswip_of_match); 1155 1156 static struct platform_driver gswip_driver = { 1157 .probe = gswip_probe, 1158 .remove = gswip_remove, 1159 .driver = { 1160 .name = "gswip", 1161 .of_match_table = gswip_of_match, 1162 }, 1163 }; 1164 1165 module_platform_driver(gswip_driver); 1166 1167 MODULE_AUTHOR("Hauke Mehrtens <hauke@hauke-m.de>"); 1168 MODULE_DESCRIPTION("Lantiq / Intel GSWIP driver"); 1169 MODULE_LICENSE("GPL v2"); 1170