1 // SPDX-License-Identifier: GPL-2.0 2 /* Atheros AR71xx built-in ethernet mac driver 3 * 4 * Copyright (C) 2019 Oleksij Rempel <o.rempel@pengutronix.de> 5 * 6 * List of authors contributed to this driver before mainlining: 7 * Alexander Couzens <lynxis@fe80.eu> 8 * Christian Lamparter <chunkeey@gmail.com> 9 * Chuanhong Guo <gch981213@gmail.com> 10 * Daniel F. Dickinson <cshored@thecshore.com> 11 * David Bauer <mail@david-bauer.net> 12 * Felix Fietkau <nbd@nbd.name> 13 * Gabor Juhos <juhosg@freemail.hu> 14 * Hauke Mehrtens <hauke@hauke-m.de> 15 * Johann Neuhauser <johann@it-neuhauser.de> 16 * John Crispin <john@phrozen.org> 17 * Jo-Philipp Wich <jo@mein.io> 18 * Koen Vandeputte <koen.vandeputte@ncentric.com> 19 * Lucian Cristian <lucian.cristian@gmail.com> 20 * Matt Merhar <mattmerhar@protonmail.com> 21 * Milan Krstic <milan.krstic@gmail.com> 22 * Petr Štetiar <ynezz@true.cz> 23 * Rosen Penev <rosenp@gmail.com> 24 * Stephen Walker <stephendwalker+github@gmail.com> 25 * Vittorio Gambaletta <openwrt@vittgam.net> 26 * Weijie Gao <hackpascal@gmail.com> 27 * Imre Kaloz <kaloz@openwrt.org> 28 */ 29 30 #include <linux/if_vlan.h> 31 #include <linux/mfd/syscon.h> 32 #include <linux/of_mdio.h> 33 #include <linux/of_net.h> 34 #include <linux/of_platform.h> 35 #include <linux/phylink.h> 36 #include <linux/regmap.h> 37 #include <linux/reset.h> 38 #include <linux/clk.h> 39 #include <linux/io.h> 40 #include <net/selftests.h> 41 42 /* For our NAPI weight bigger does *NOT* mean better - it means more 43 * D-cache misses and lots more wasted cycles than we'll ever 44 * possibly gain from saving instructions. 45 */ 46 #define AG71XX_NAPI_WEIGHT 32 47 #define AG71XX_OOM_REFILL (1 + HZ / 10) 48 49 #define AG71XX_INT_ERR (AG71XX_INT_RX_BE | AG71XX_INT_TX_BE) 50 #define AG71XX_INT_TX (AG71XX_INT_TX_PS) 51 #define AG71XX_INT_RX (AG71XX_INT_RX_PR | AG71XX_INT_RX_OF) 52 53 #define AG71XX_INT_POLL (AG71XX_INT_RX | AG71XX_INT_TX) 54 #define AG71XX_INT_INIT (AG71XX_INT_ERR | AG71XX_INT_POLL) 55 56 #define AG71XX_TX_MTU_LEN 1540 57 58 #define AG71XX_TX_RING_SPLIT 512 59 #define AG71XX_TX_RING_DS_PER_PKT DIV_ROUND_UP(AG71XX_TX_MTU_LEN, \ 60 AG71XX_TX_RING_SPLIT) 61 #define AG71XX_TX_RING_SIZE_DEFAULT 128 62 #define AG71XX_RX_RING_SIZE_DEFAULT 256 63 64 #define AG71XX_MDIO_RETRY 1000 65 #define AG71XX_MDIO_DELAY 5 66 #define AG71XX_MDIO_MAX_CLK 5000000 67 68 /* Register offsets */ 69 #define AG71XX_REG_MAC_CFG1 0x0000 70 #define MAC_CFG1_TXE BIT(0) /* Tx Enable */ 71 #define MAC_CFG1_STX BIT(1) /* Synchronize Tx Enable */ 72 #define MAC_CFG1_RXE BIT(2) /* Rx Enable */ 73 #define MAC_CFG1_SRX BIT(3) /* Synchronize Rx Enable */ 74 #define MAC_CFG1_TFC BIT(4) /* Tx Flow Control Enable */ 75 #define MAC_CFG1_RFC BIT(5) /* Rx Flow Control Enable */ 76 #define MAC_CFG1_SR BIT(31) /* Soft Reset */ 77 #define MAC_CFG1_INIT (MAC_CFG1_RXE | MAC_CFG1_TXE | \ 78 MAC_CFG1_SRX | MAC_CFG1_STX) 79 80 #define AG71XX_REG_MAC_CFG2 0x0004 81 #define MAC_CFG2_FDX BIT(0) 82 #define MAC_CFG2_PAD_CRC_EN BIT(2) 83 #define MAC_CFG2_LEN_CHECK BIT(4) 84 #define MAC_CFG2_IF_1000 BIT(9) 85 #define MAC_CFG2_IF_10_100 BIT(8) 86 87 #define AG71XX_REG_MAC_MFL 0x0010 88 89 #define AG71XX_REG_MII_CFG 0x0020 90 #define MII_CFG_CLK_DIV_4 0 91 #define MII_CFG_CLK_DIV_6 2 92 #define MII_CFG_CLK_DIV_8 3 93 #define MII_CFG_CLK_DIV_10 4 94 #define MII_CFG_CLK_DIV_14 5 95 #define MII_CFG_CLK_DIV_20 6 96 #define MII_CFG_CLK_DIV_28 7 97 #define MII_CFG_CLK_DIV_34 8 98 #define MII_CFG_CLK_DIV_42 9 99 #define MII_CFG_CLK_DIV_50 10 100 #define MII_CFG_CLK_DIV_58 11 101 #define MII_CFG_CLK_DIV_66 12 102 #define MII_CFG_CLK_DIV_74 13 103 #define MII_CFG_CLK_DIV_82 14 104 #define MII_CFG_CLK_DIV_98 15 105 #define MII_CFG_RESET BIT(31) 106 107 #define AG71XX_REG_MII_CMD 0x0024 108 #define MII_CMD_READ BIT(0) 109 110 #define AG71XX_REG_MII_ADDR 0x0028 111 #define MII_ADDR_SHIFT 8 112 113 #define AG71XX_REG_MII_CTRL 0x002c 114 #define AG71XX_REG_MII_STATUS 0x0030 115 #define AG71XX_REG_MII_IND 0x0034 116 #define MII_IND_BUSY BIT(0) 117 #define MII_IND_INVALID BIT(2) 118 119 #define AG71XX_REG_MAC_IFCTL 0x0038 120 #define MAC_IFCTL_SPEED BIT(16) 121 122 #define AG71XX_REG_MAC_ADDR1 0x0040 123 #define AG71XX_REG_MAC_ADDR2 0x0044 124 #define AG71XX_REG_FIFO_CFG0 0x0048 125 #define FIFO_CFG0_WTM BIT(0) /* Watermark Module */ 126 #define FIFO_CFG0_RXS BIT(1) /* Rx System Module */ 127 #define FIFO_CFG0_RXF BIT(2) /* Rx Fabric Module */ 128 #define FIFO_CFG0_TXS BIT(3) /* Tx System Module */ 129 #define FIFO_CFG0_TXF BIT(4) /* Tx Fabric Module */ 130 #define FIFO_CFG0_ALL (FIFO_CFG0_WTM | FIFO_CFG0_RXS | FIFO_CFG0_RXF \ 131 | FIFO_CFG0_TXS | FIFO_CFG0_TXF) 132 #define FIFO_CFG0_INIT (FIFO_CFG0_ALL << FIFO_CFG0_ENABLE_SHIFT) 133 134 #define FIFO_CFG0_ENABLE_SHIFT 8 135 136 #define AG71XX_REG_FIFO_CFG1 0x004c 137 #define AG71XX_REG_FIFO_CFG2 0x0050 138 #define AG71XX_REG_FIFO_CFG3 0x0054 139 #define AG71XX_REG_FIFO_CFG4 0x0058 140 #define FIFO_CFG4_DE BIT(0) /* Drop Event */ 141 #define FIFO_CFG4_DV BIT(1) /* RX_DV Event */ 142 #define FIFO_CFG4_FC BIT(2) /* False Carrier */ 143 #define FIFO_CFG4_CE BIT(3) /* Code Error */ 144 #define FIFO_CFG4_CR BIT(4) /* CRC error */ 145 #define FIFO_CFG4_LM BIT(5) /* Length Mismatch */ 146 #define FIFO_CFG4_LO BIT(6) /* Length out of range */ 147 #define FIFO_CFG4_OK BIT(7) /* Packet is OK */ 148 #define FIFO_CFG4_MC BIT(8) /* Multicast Packet */ 149 #define FIFO_CFG4_BC BIT(9) /* Broadcast Packet */ 150 #define FIFO_CFG4_DR BIT(10) /* Dribble */ 151 #define FIFO_CFG4_LE BIT(11) /* Long Event */ 152 #define FIFO_CFG4_CF BIT(12) /* Control Frame */ 153 #define FIFO_CFG4_PF BIT(13) /* Pause Frame */ 154 #define FIFO_CFG4_UO BIT(14) /* Unsupported Opcode */ 155 #define FIFO_CFG4_VT BIT(15) /* VLAN tag detected */ 156 #define FIFO_CFG4_FT BIT(16) /* Frame Truncated */ 157 #define FIFO_CFG4_UC BIT(17) /* Unicast Packet */ 158 #define FIFO_CFG4_INIT (FIFO_CFG4_DE | FIFO_CFG4_DV | FIFO_CFG4_FC | \ 159 FIFO_CFG4_CE | FIFO_CFG4_CR | FIFO_CFG4_LM | \ 160 FIFO_CFG4_LO | FIFO_CFG4_OK | FIFO_CFG4_MC | \ 161 FIFO_CFG4_BC | FIFO_CFG4_DR | FIFO_CFG4_LE | \ 162 FIFO_CFG4_CF | FIFO_CFG4_PF | FIFO_CFG4_UO | \ 163 FIFO_CFG4_VT) 164 165 #define AG71XX_REG_FIFO_CFG5 0x005c 166 #define FIFO_CFG5_DE BIT(0) /* Drop Event */ 167 #define FIFO_CFG5_DV BIT(1) /* RX_DV Event */ 168 #define FIFO_CFG5_FC BIT(2) /* False Carrier */ 169 #define FIFO_CFG5_CE BIT(3) /* Code Error */ 170 #define FIFO_CFG5_LM BIT(4) /* Length Mismatch */ 171 #define FIFO_CFG5_LO BIT(5) /* Length Out of Range */ 172 #define FIFO_CFG5_OK BIT(6) /* Packet is OK */ 173 #define FIFO_CFG5_MC BIT(7) /* Multicast Packet */ 174 #define FIFO_CFG5_BC BIT(8) /* Broadcast Packet */ 175 #define FIFO_CFG5_DR BIT(9) /* Dribble */ 176 #define FIFO_CFG5_CF BIT(10) /* Control Frame */ 177 #define FIFO_CFG5_PF BIT(11) /* Pause Frame */ 178 #define FIFO_CFG5_UO BIT(12) /* Unsupported Opcode */ 179 #define FIFO_CFG5_VT BIT(13) /* VLAN tag detected */ 180 #define FIFO_CFG5_LE BIT(14) /* Long Event */ 181 #define FIFO_CFG5_FT BIT(15) /* Frame Truncated */ 182 #define FIFO_CFG5_16 BIT(16) /* unknown */ 183 #define FIFO_CFG5_17 BIT(17) /* unknown */ 184 #define FIFO_CFG5_SF BIT(18) /* Short Frame */ 185 #define FIFO_CFG5_BM BIT(19) /* Byte Mode */ 186 #define FIFO_CFG5_INIT (FIFO_CFG5_DE | FIFO_CFG5_DV | FIFO_CFG5_FC | \ 187 FIFO_CFG5_CE | FIFO_CFG5_LO | FIFO_CFG5_OK | \ 188 FIFO_CFG5_MC | FIFO_CFG5_BC | FIFO_CFG5_DR | \ 189 FIFO_CFG5_CF | FIFO_CFG5_PF | FIFO_CFG5_VT | \ 190 FIFO_CFG5_LE | FIFO_CFG5_FT | FIFO_CFG5_16 | \ 191 FIFO_CFG5_17 | FIFO_CFG5_SF) 192 193 #define AG71XX_REG_TX_CTRL 0x0180 194 #define TX_CTRL_TXE BIT(0) /* Tx Enable */ 195 196 #define AG71XX_REG_TX_DESC 0x0184 197 #define AG71XX_REG_TX_STATUS 0x0188 198 #define TX_STATUS_PS BIT(0) /* Packet Sent */ 199 #define TX_STATUS_UR BIT(1) /* Tx Underrun */ 200 #define TX_STATUS_BE BIT(3) /* Bus Error */ 201 202 #define AG71XX_REG_RX_CTRL 0x018c 203 #define RX_CTRL_RXE BIT(0) /* Rx Enable */ 204 205 #define AG71XX_DMA_RETRY 10 206 #define AG71XX_DMA_DELAY 1 207 208 #define AG71XX_REG_RX_DESC 0x0190 209 #define AG71XX_REG_RX_STATUS 0x0194 210 #define RX_STATUS_PR BIT(0) /* Packet Received */ 211 #define RX_STATUS_OF BIT(2) /* Rx Overflow */ 212 #define RX_STATUS_BE BIT(3) /* Bus Error */ 213 214 #define AG71XX_REG_INT_ENABLE 0x0198 215 #define AG71XX_REG_INT_STATUS 0x019c 216 #define AG71XX_INT_TX_PS BIT(0) 217 #define AG71XX_INT_TX_UR BIT(1) 218 #define AG71XX_INT_TX_BE BIT(3) 219 #define AG71XX_INT_RX_PR BIT(4) 220 #define AG71XX_INT_RX_OF BIT(6) 221 #define AG71XX_INT_RX_BE BIT(7) 222 223 #define AG71XX_REG_FIFO_DEPTH 0x01a8 224 #define AG71XX_REG_RX_SM 0x01b0 225 #define AG71XX_REG_TX_SM 0x01b4 226 227 #define AG71XX_DEFAULT_MSG_ENABLE \ 228 (NETIF_MSG_DRV \ 229 | NETIF_MSG_PROBE \ 230 | NETIF_MSG_LINK \ 231 | NETIF_MSG_TIMER \ 232 | NETIF_MSG_IFDOWN \ 233 | NETIF_MSG_IFUP \ 234 | NETIF_MSG_RX_ERR \ 235 | NETIF_MSG_TX_ERR) 236 237 struct ag71xx_statistic { 238 unsigned short offset; 239 u32 mask; 240 const char name[ETH_GSTRING_LEN]; 241 }; 242 243 static const struct ag71xx_statistic ag71xx_statistics[] = { 244 { 0x0080, GENMASK(17, 0), "Tx/Rx 64 Byte", }, 245 { 0x0084, GENMASK(17, 0), "Tx/Rx 65-127 Byte", }, 246 { 0x0088, GENMASK(17, 0), "Tx/Rx 128-255 Byte", }, 247 { 0x008C, GENMASK(17, 0), "Tx/Rx 256-511 Byte", }, 248 { 0x0090, GENMASK(17, 0), "Tx/Rx 512-1023 Byte", }, 249 { 0x0094, GENMASK(17, 0), "Tx/Rx 1024-1518 Byte", }, 250 { 0x0098, GENMASK(17, 0), "Tx/Rx 1519-1522 Byte VLAN", }, 251 { 0x009C, GENMASK(23, 0), "Rx Byte", }, 252 { 0x00A0, GENMASK(17, 0), "Rx Packet", }, 253 { 0x00A4, GENMASK(11, 0), "Rx FCS Error", }, 254 { 0x00A8, GENMASK(17, 0), "Rx Multicast Packet", }, 255 { 0x00AC, GENMASK(21, 0), "Rx Broadcast Packet", }, 256 { 0x00B0, GENMASK(17, 0), "Rx Control Frame Packet", }, 257 { 0x00B4, GENMASK(11, 0), "Rx Pause Frame Packet", }, 258 { 0x00B8, GENMASK(11, 0), "Rx Unknown OPCode Packet", }, 259 { 0x00BC, GENMASK(11, 0), "Rx Alignment Error", }, 260 { 0x00C0, GENMASK(15, 0), "Rx Frame Length Error", }, 261 { 0x00C4, GENMASK(11, 0), "Rx Code Error", }, 262 { 0x00C8, GENMASK(11, 0), "Rx Carrier Sense Error", }, 263 { 0x00CC, GENMASK(11, 0), "Rx Undersize Packet", }, 264 { 0x00D0, GENMASK(11, 0), "Rx Oversize Packet", }, 265 { 0x00D4, GENMASK(11, 0), "Rx Fragments", }, 266 { 0x00D8, GENMASK(11, 0), "Rx Jabber", }, 267 { 0x00DC, GENMASK(11, 0), "Rx Dropped Packet", }, 268 { 0x00E0, GENMASK(23, 0), "Tx Byte", }, 269 { 0x00E4, GENMASK(17, 0), "Tx Packet", }, 270 { 0x00E8, GENMASK(17, 0), "Tx Multicast Packet", }, 271 { 0x00EC, GENMASK(17, 0), "Tx Broadcast Packet", }, 272 { 0x00F0, GENMASK(11, 0), "Tx Pause Control Frame", }, 273 { 0x00F4, GENMASK(11, 0), "Tx Deferral Packet", }, 274 { 0x00F8, GENMASK(11, 0), "Tx Excessive Deferral Packet", }, 275 { 0x00FC, GENMASK(11, 0), "Tx Single Collision Packet", }, 276 { 0x0100, GENMASK(11, 0), "Tx Multiple Collision", }, 277 { 0x0104, GENMASK(11, 0), "Tx Late Collision Packet", }, 278 { 0x0108, GENMASK(11, 0), "Tx Excessive Collision Packet", }, 279 { 0x010C, GENMASK(12, 0), "Tx Total Collision", }, 280 { 0x0110, GENMASK(11, 0), "Tx Pause Frames Honored", }, 281 { 0x0114, GENMASK(11, 0), "Tx Drop Frame", }, 282 { 0x0118, GENMASK(11, 0), "Tx Jabber Frame", }, 283 { 0x011C, GENMASK(11, 0), "Tx FCS Error", }, 284 { 0x0120, GENMASK(11, 0), "Tx Control Frame", }, 285 { 0x0124, GENMASK(11, 0), "Tx Oversize Frame", }, 286 { 0x0128, GENMASK(11, 0), "Tx Undersize Frame", }, 287 { 0x012C, GENMASK(11, 0), "Tx Fragment", }, 288 }; 289 290 #define DESC_EMPTY BIT(31) 291 #define DESC_MORE BIT(24) 292 #define DESC_PKTLEN_M 0xfff 293 struct ag71xx_desc { 294 u32 data; 295 u32 ctrl; 296 u32 next; 297 u32 pad; 298 } __aligned(4); 299 300 #define AG71XX_DESC_SIZE roundup(sizeof(struct ag71xx_desc), \ 301 L1_CACHE_BYTES) 302 303 struct ag71xx_buf { 304 union { 305 struct { 306 struct sk_buff *skb; 307 unsigned int len; 308 } tx; 309 struct { 310 dma_addr_t dma_addr; 311 void *rx_buf; 312 } rx; 313 }; 314 }; 315 316 struct ag71xx_ring { 317 /* "Hot" fields in the data path. */ 318 unsigned int curr; 319 unsigned int dirty; 320 321 /* "Cold" fields - not used in the data path. */ 322 struct ag71xx_buf *buf; 323 u16 order; 324 u16 desc_split; 325 dma_addr_t descs_dma; 326 u8 *descs_cpu; 327 }; 328 329 enum ag71xx_type { 330 AR7100, 331 AR7240, 332 AR9130, 333 AR9330, 334 AR9340, 335 QCA9530, 336 QCA9550, 337 }; 338 339 struct ag71xx_dcfg { 340 u32 max_frame_len; 341 const u32 *fifodata; 342 u16 desc_pktlen_mask; 343 bool tx_hang_workaround; 344 enum ag71xx_type type; 345 }; 346 347 struct ag71xx { 348 /* Critical data related to the per-packet data path are clustered 349 * early in this structure to help improve the D-cache footprint. 350 */ 351 struct ag71xx_ring rx_ring ____cacheline_aligned; 352 struct ag71xx_ring tx_ring ____cacheline_aligned; 353 354 u16 rx_buf_size; 355 u8 rx_buf_offset; 356 357 struct net_device *ndev; 358 struct platform_device *pdev; 359 struct napi_struct napi; 360 u32 msg_enable; 361 const struct ag71xx_dcfg *dcfg; 362 363 /* From this point onwards we're not looking at per-packet fields. */ 364 void __iomem *mac_base; 365 366 struct ag71xx_desc *stop_desc; 367 dma_addr_t stop_desc_dma; 368 369 phy_interface_t phy_if_mode; 370 struct phylink *phylink; 371 struct phylink_config phylink_config; 372 373 struct delayed_work restart_work; 374 struct timer_list oom_timer; 375 376 struct reset_control *mac_reset; 377 378 u32 fifodata[3]; 379 int mac_idx; 380 381 struct reset_control *mdio_reset; 382 struct mii_bus *mii_bus; 383 struct clk *clk_mdio; 384 struct clk *clk_eth; 385 }; 386 387 static int ag71xx_desc_empty(struct ag71xx_desc *desc) 388 { 389 return (desc->ctrl & DESC_EMPTY) != 0; 390 } 391 392 static struct ag71xx_desc *ag71xx_ring_desc(struct ag71xx_ring *ring, int idx) 393 { 394 return (struct ag71xx_desc *)&ring->descs_cpu[idx * AG71XX_DESC_SIZE]; 395 } 396 397 static int ag71xx_ring_size_order(int size) 398 { 399 return fls(size - 1); 400 } 401 402 static bool ag71xx_is(struct ag71xx *ag, enum ag71xx_type type) 403 { 404 return ag->dcfg->type == type; 405 } 406 407 static void ag71xx_wr(struct ag71xx *ag, unsigned int reg, u32 value) 408 { 409 iowrite32(value, ag->mac_base + reg); 410 /* flush write */ 411 (void)ioread32(ag->mac_base + reg); 412 } 413 414 static u32 ag71xx_rr(struct ag71xx *ag, unsigned int reg) 415 { 416 return ioread32(ag->mac_base + reg); 417 } 418 419 static void ag71xx_sb(struct ag71xx *ag, unsigned int reg, u32 mask) 420 { 421 void __iomem *r; 422 423 r = ag->mac_base + reg; 424 iowrite32(ioread32(r) | mask, r); 425 /* flush write */ 426 (void)ioread32(r); 427 } 428 429 static void ag71xx_cb(struct ag71xx *ag, unsigned int reg, u32 mask) 430 { 431 void __iomem *r; 432 433 r = ag->mac_base + reg; 434 iowrite32(ioread32(r) & ~mask, r); 435 /* flush write */ 436 (void)ioread32(r); 437 } 438 439 static void ag71xx_int_enable(struct ag71xx *ag, u32 ints) 440 { 441 ag71xx_sb(ag, AG71XX_REG_INT_ENABLE, ints); 442 } 443 444 static void ag71xx_int_disable(struct ag71xx *ag, u32 ints) 445 { 446 ag71xx_cb(ag, AG71XX_REG_INT_ENABLE, ints); 447 } 448 449 static void ag71xx_get_drvinfo(struct net_device *ndev, 450 struct ethtool_drvinfo *info) 451 { 452 struct ag71xx *ag = netdev_priv(ndev); 453 454 strlcpy(info->driver, "ag71xx", sizeof(info->driver)); 455 strlcpy(info->bus_info, of_node_full_name(ag->pdev->dev.of_node), 456 sizeof(info->bus_info)); 457 } 458 459 static int ag71xx_get_link_ksettings(struct net_device *ndev, 460 struct ethtool_link_ksettings *kset) 461 { 462 struct ag71xx *ag = netdev_priv(ndev); 463 464 return phylink_ethtool_ksettings_get(ag->phylink, kset); 465 } 466 467 static int ag71xx_set_link_ksettings(struct net_device *ndev, 468 const struct ethtool_link_ksettings *kset) 469 { 470 struct ag71xx *ag = netdev_priv(ndev); 471 472 return phylink_ethtool_ksettings_set(ag->phylink, kset); 473 } 474 475 static int ag71xx_ethtool_nway_reset(struct net_device *ndev) 476 { 477 struct ag71xx *ag = netdev_priv(ndev); 478 479 return phylink_ethtool_nway_reset(ag->phylink); 480 } 481 482 static void ag71xx_ethtool_get_pauseparam(struct net_device *ndev, 483 struct ethtool_pauseparam *pause) 484 { 485 struct ag71xx *ag = netdev_priv(ndev); 486 487 phylink_ethtool_get_pauseparam(ag->phylink, pause); 488 } 489 490 static int ag71xx_ethtool_set_pauseparam(struct net_device *ndev, 491 struct ethtool_pauseparam *pause) 492 { 493 struct ag71xx *ag = netdev_priv(ndev); 494 495 return phylink_ethtool_set_pauseparam(ag->phylink, pause); 496 } 497 498 static void ag71xx_ethtool_get_strings(struct net_device *netdev, u32 sset, 499 u8 *data) 500 { 501 int i; 502 503 switch (sset) { 504 case ETH_SS_STATS: 505 for (i = 0; i < ARRAY_SIZE(ag71xx_statistics); i++) 506 memcpy(data + i * ETH_GSTRING_LEN, 507 ag71xx_statistics[i].name, ETH_GSTRING_LEN); 508 break; 509 case ETH_SS_TEST: 510 net_selftest_get_strings(data); 511 break; 512 } 513 } 514 515 static void ag71xx_ethtool_get_stats(struct net_device *ndev, 516 struct ethtool_stats *stats, u64 *data) 517 { 518 struct ag71xx *ag = netdev_priv(ndev); 519 int i; 520 521 for (i = 0; i < ARRAY_SIZE(ag71xx_statistics); i++) 522 *data++ = ag71xx_rr(ag, ag71xx_statistics[i].offset) 523 & ag71xx_statistics[i].mask; 524 } 525 526 static int ag71xx_ethtool_get_sset_count(struct net_device *ndev, int sset) 527 { 528 switch (sset) { 529 case ETH_SS_STATS: 530 return ARRAY_SIZE(ag71xx_statistics); 531 case ETH_SS_TEST: 532 return net_selftest_get_count(); 533 default: 534 return -EOPNOTSUPP; 535 } 536 } 537 538 static const struct ethtool_ops ag71xx_ethtool_ops = { 539 .get_drvinfo = ag71xx_get_drvinfo, 540 .get_link = ethtool_op_get_link, 541 .get_ts_info = ethtool_op_get_ts_info, 542 .get_link_ksettings = ag71xx_get_link_ksettings, 543 .set_link_ksettings = ag71xx_set_link_ksettings, 544 .nway_reset = ag71xx_ethtool_nway_reset, 545 .get_pauseparam = ag71xx_ethtool_get_pauseparam, 546 .set_pauseparam = ag71xx_ethtool_set_pauseparam, 547 .get_strings = ag71xx_ethtool_get_strings, 548 .get_ethtool_stats = ag71xx_ethtool_get_stats, 549 .get_sset_count = ag71xx_ethtool_get_sset_count, 550 .self_test = net_selftest, 551 }; 552 553 static int ag71xx_mdio_wait_busy(struct ag71xx *ag) 554 { 555 struct net_device *ndev = ag->ndev; 556 int i; 557 558 for (i = 0; i < AG71XX_MDIO_RETRY; i++) { 559 u32 busy; 560 561 udelay(AG71XX_MDIO_DELAY); 562 563 busy = ag71xx_rr(ag, AG71XX_REG_MII_IND); 564 if (!busy) 565 return 0; 566 567 udelay(AG71XX_MDIO_DELAY); 568 } 569 570 netif_err(ag, link, ndev, "MDIO operation timed out\n"); 571 572 return -ETIMEDOUT; 573 } 574 575 static int ag71xx_mdio_mii_read(struct mii_bus *bus, int addr, int reg) 576 { 577 struct ag71xx *ag = bus->priv; 578 int err, val; 579 580 err = ag71xx_mdio_wait_busy(ag); 581 if (err) 582 return err; 583 584 ag71xx_wr(ag, AG71XX_REG_MII_ADDR, 585 ((addr & 0x1f) << MII_ADDR_SHIFT) | (reg & 0xff)); 586 /* enable read mode */ 587 ag71xx_wr(ag, AG71XX_REG_MII_CMD, MII_CMD_READ); 588 589 err = ag71xx_mdio_wait_busy(ag); 590 if (err) 591 return err; 592 593 val = ag71xx_rr(ag, AG71XX_REG_MII_STATUS); 594 /* disable read mode */ 595 ag71xx_wr(ag, AG71XX_REG_MII_CMD, 0); 596 597 netif_dbg(ag, link, ag->ndev, "mii_read: addr=%04x, reg=%04x, value=%04x\n", 598 addr, reg, val); 599 600 return val; 601 } 602 603 static int ag71xx_mdio_mii_write(struct mii_bus *bus, int addr, int reg, 604 u16 val) 605 { 606 struct ag71xx *ag = bus->priv; 607 608 netif_dbg(ag, link, ag->ndev, "mii_write: addr=%04x, reg=%04x, value=%04x\n", 609 addr, reg, val); 610 611 ag71xx_wr(ag, AG71XX_REG_MII_ADDR, 612 ((addr & 0x1f) << MII_ADDR_SHIFT) | (reg & 0xff)); 613 ag71xx_wr(ag, AG71XX_REG_MII_CTRL, val); 614 615 return ag71xx_mdio_wait_busy(ag); 616 } 617 618 static const u32 ar71xx_mdio_div_table[] = { 619 4, 4, 6, 8, 10, 14, 20, 28, 620 }; 621 622 static const u32 ar7240_mdio_div_table[] = { 623 2, 2, 4, 6, 8, 12, 18, 26, 32, 40, 48, 56, 62, 70, 78, 96, 624 }; 625 626 static const u32 ar933x_mdio_div_table[] = { 627 4, 4, 6, 8, 10, 14, 20, 28, 34, 42, 50, 58, 66, 74, 82, 98, 628 }; 629 630 static int ag71xx_mdio_get_divider(struct ag71xx *ag, u32 *div) 631 { 632 unsigned long ref_clock; 633 const u32 *table; 634 int ndivs, i; 635 636 ref_clock = clk_get_rate(ag->clk_mdio); 637 if (!ref_clock) 638 return -EINVAL; 639 640 if (ag71xx_is(ag, AR9330) || ag71xx_is(ag, AR9340)) { 641 table = ar933x_mdio_div_table; 642 ndivs = ARRAY_SIZE(ar933x_mdio_div_table); 643 } else if (ag71xx_is(ag, AR7240)) { 644 table = ar7240_mdio_div_table; 645 ndivs = ARRAY_SIZE(ar7240_mdio_div_table); 646 } else { 647 table = ar71xx_mdio_div_table; 648 ndivs = ARRAY_SIZE(ar71xx_mdio_div_table); 649 } 650 651 for (i = 0; i < ndivs; i++) { 652 unsigned long t; 653 654 t = ref_clock / table[i]; 655 if (t <= AG71XX_MDIO_MAX_CLK) { 656 *div = i; 657 return 0; 658 } 659 } 660 661 return -ENOENT; 662 } 663 664 static int ag71xx_mdio_reset(struct mii_bus *bus) 665 { 666 struct ag71xx *ag = bus->priv; 667 int err; 668 u32 t; 669 670 err = ag71xx_mdio_get_divider(ag, &t); 671 if (err) 672 return err; 673 674 ag71xx_wr(ag, AG71XX_REG_MII_CFG, t | MII_CFG_RESET); 675 usleep_range(100, 200); 676 677 ag71xx_wr(ag, AG71XX_REG_MII_CFG, t); 678 usleep_range(100, 200); 679 680 return 0; 681 } 682 683 static int ag71xx_mdio_probe(struct ag71xx *ag) 684 { 685 struct device *dev = &ag->pdev->dev; 686 struct net_device *ndev = ag->ndev; 687 static struct mii_bus *mii_bus; 688 struct device_node *np, *mnp; 689 int err; 690 691 np = dev->of_node; 692 ag->mii_bus = NULL; 693 694 ag->clk_mdio = devm_clk_get(dev, "mdio"); 695 if (IS_ERR(ag->clk_mdio)) { 696 netif_err(ag, probe, ndev, "Failed to get mdio clk.\n"); 697 return PTR_ERR(ag->clk_mdio); 698 } 699 700 err = clk_prepare_enable(ag->clk_mdio); 701 if (err) { 702 netif_err(ag, probe, ndev, "Failed to enable mdio clk.\n"); 703 return err; 704 } 705 706 mii_bus = devm_mdiobus_alloc(dev); 707 if (!mii_bus) { 708 err = -ENOMEM; 709 goto mdio_err_put_clk; 710 } 711 712 ag->mdio_reset = of_reset_control_get_exclusive(np, "mdio"); 713 if (IS_ERR(ag->mdio_reset)) { 714 netif_err(ag, probe, ndev, "Failed to get reset mdio.\n"); 715 err = PTR_ERR(ag->mdio_reset); 716 goto mdio_err_put_clk; 717 } 718 719 mii_bus->name = "ag71xx_mdio"; 720 mii_bus->read = ag71xx_mdio_mii_read; 721 mii_bus->write = ag71xx_mdio_mii_write; 722 mii_bus->reset = ag71xx_mdio_reset; 723 mii_bus->priv = ag; 724 mii_bus->parent = dev; 725 snprintf(mii_bus->id, MII_BUS_ID_SIZE, "%s.%d", np->name, ag->mac_idx); 726 727 if (!IS_ERR(ag->mdio_reset)) { 728 reset_control_assert(ag->mdio_reset); 729 msleep(100); 730 reset_control_deassert(ag->mdio_reset); 731 msleep(200); 732 } 733 734 mnp = of_get_child_by_name(np, "mdio"); 735 err = of_mdiobus_register(mii_bus, mnp); 736 of_node_put(mnp); 737 if (err) 738 goto mdio_err_put_clk; 739 740 ag->mii_bus = mii_bus; 741 742 return 0; 743 744 mdio_err_put_clk: 745 clk_disable_unprepare(ag->clk_mdio); 746 return err; 747 } 748 749 static void ag71xx_mdio_remove(struct ag71xx *ag) 750 { 751 if (ag->mii_bus) 752 mdiobus_unregister(ag->mii_bus); 753 clk_disable_unprepare(ag->clk_mdio); 754 } 755 756 static void ag71xx_hw_stop(struct ag71xx *ag) 757 { 758 /* disable all interrupts and stop the rx/tx engine */ 759 ag71xx_wr(ag, AG71XX_REG_INT_ENABLE, 0); 760 ag71xx_wr(ag, AG71XX_REG_RX_CTRL, 0); 761 ag71xx_wr(ag, AG71XX_REG_TX_CTRL, 0); 762 } 763 764 static bool ag71xx_check_dma_stuck(struct ag71xx *ag) 765 { 766 unsigned long timestamp; 767 u32 rx_sm, tx_sm, rx_fd; 768 769 timestamp = netdev_get_tx_queue(ag->ndev, 0)->trans_start; 770 if (likely(time_before(jiffies, timestamp + HZ / 10))) 771 return false; 772 773 if (!netif_carrier_ok(ag->ndev)) 774 return false; 775 776 rx_sm = ag71xx_rr(ag, AG71XX_REG_RX_SM); 777 if ((rx_sm & 0x7) == 0x3 && ((rx_sm >> 4) & 0x7) == 0x6) 778 return true; 779 780 tx_sm = ag71xx_rr(ag, AG71XX_REG_TX_SM); 781 rx_fd = ag71xx_rr(ag, AG71XX_REG_FIFO_DEPTH); 782 if (((tx_sm >> 4) & 0x7) == 0 && ((rx_sm & 0x7) == 0) && 783 ((rx_sm >> 4) & 0x7) == 0 && rx_fd == 0) 784 return true; 785 786 return false; 787 } 788 789 static int ag71xx_tx_packets(struct ag71xx *ag, bool flush) 790 { 791 struct ag71xx_ring *ring = &ag->tx_ring; 792 int sent = 0, bytes_compl = 0, n = 0; 793 struct net_device *ndev = ag->ndev; 794 int ring_mask, ring_size; 795 bool dma_stuck = false; 796 797 ring_mask = BIT(ring->order) - 1; 798 ring_size = BIT(ring->order); 799 800 netif_dbg(ag, tx_queued, ndev, "processing TX ring\n"); 801 802 while (ring->dirty + n != ring->curr) { 803 struct ag71xx_desc *desc; 804 struct sk_buff *skb; 805 unsigned int i; 806 807 i = (ring->dirty + n) & ring_mask; 808 desc = ag71xx_ring_desc(ring, i); 809 skb = ring->buf[i].tx.skb; 810 811 if (!flush && !ag71xx_desc_empty(desc)) { 812 if (ag->dcfg->tx_hang_workaround && 813 ag71xx_check_dma_stuck(ag)) { 814 schedule_delayed_work(&ag->restart_work, 815 HZ / 2); 816 dma_stuck = true; 817 } 818 break; 819 } 820 821 if (flush) 822 desc->ctrl |= DESC_EMPTY; 823 824 n++; 825 if (!skb) 826 continue; 827 828 dev_kfree_skb_any(skb); 829 ring->buf[i].tx.skb = NULL; 830 831 bytes_compl += ring->buf[i].tx.len; 832 833 sent++; 834 ring->dirty += n; 835 836 while (n > 0) { 837 ag71xx_wr(ag, AG71XX_REG_TX_STATUS, TX_STATUS_PS); 838 n--; 839 } 840 } 841 842 netif_dbg(ag, tx_done, ndev, "%d packets sent out\n", sent); 843 844 if (!sent) 845 return 0; 846 847 ag->ndev->stats.tx_bytes += bytes_compl; 848 ag->ndev->stats.tx_packets += sent; 849 850 netdev_completed_queue(ag->ndev, sent, bytes_compl); 851 if ((ring->curr - ring->dirty) < (ring_size * 3) / 4) 852 netif_wake_queue(ag->ndev); 853 854 if (!dma_stuck) 855 cancel_delayed_work(&ag->restart_work); 856 857 return sent; 858 } 859 860 static void ag71xx_dma_wait_stop(struct ag71xx *ag) 861 { 862 struct net_device *ndev = ag->ndev; 863 int i; 864 865 for (i = 0; i < AG71XX_DMA_RETRY; i++) { 866 u32 rx, tx; 867 868 mdelay(AG71XX_DMA_DELAY); 869 870 rx = ag71xx_rr(ag, AG71XX_REG_RX_CTRL) & RX_CTRL_RXE; 871 tx = ag71xx_rr(ag, AG71XX_REG_TX_CTRL) & TX_CTRL_TXE; 872 if (!rx && !tx) 873 return; 874 } 875 876 netif_err(ag, hw, ndev, "DMA stop operation timed out\n"); 877 } 878 879 static void ag71xx_dma_reset(struct ag71xx *ag) 880 { 881 struct net_device *ndev = ag->ndev; 882 u32 val; 883 int i; 884 885 /* stop RX and TX */ 886 ag71xx_wr(ag, AG71XX_REG_RX_CTRL, 0); 887 ag71xx_wr(ag, AG71XX_REG_TX_CTRL, 0); 888 889 /* give the hardware some time to really stop all rx/tx activity 890 * clearing the descriptors too early causes random memory corruption 891 */ 892 ag71xx_dma_wait_stop(ag); 893 894 /* clear descriptor addresses */ 895 ag71xx_wr(ag, AG71XX_REG_TX_DESC, ag->stop_desc_dma); 896 ag71xx_wr(ag, AG71XX_REG_RX_DESC, ag->stop_desc_dma); 897 898 /* clear pending RX/TX interrupts */ 899 for (i = 0; i < 256; i++) { 900 ag71xx_wr(ag, AG71XX_REG_RX_STATUS, RX_STATUS_PR); 901 ag71xx_wr(ag, AG71XX_REG_TX_STATUS, TX_STATUS_PS); 902 } 903 904 /* clear pending errors */ 905 ag71xx_wr(ag, AG71XX_REG_RX_STATUS, RX_STATUS_BE | RX_STATUS_OF); 906 ag71xx_wr(ag, AG71XX_REG_TX_STATUS, TX_STATUS_BE | TX_STATUS_UR); 907 908 val = ag71xx_rr(ag, AG71XX_REG_RX_STATUS); 909 if (val) 910 netif_err(ag, hw, ndev, "unable to clear DMA Rx status: %08x\n", 911 val); 912 913 val = ag71xx_rr(ag, AG71XX_REG_TX_STATUS); 914 915 /* mask out reserved bits */ 916 val &= ~0xff000000; 917 918 if (val) 919 netif_err(ag, hw, ndev, "unable to clear DMA Tx status: %08x\n", 920 val); 921 } 922 923 static void ag71xx_hw_setup(struct ag71xx *ag) 924 { 925 u32 init = MAC_CFG1_INIT; 926 927 /* setup MAC configuration registers */ 928 ag71xx_wr(ag, AG71XX_REG_MAC_CFG1, init); 929 930 ag71xx_sb(ag, AG71XX_REG_MAC_CFG2, 931 MAC_CFG2_PAD_CRC_EN | MAC_CFG2_LEN_CHECK); 932 933 /* setup max frame length to zero */ 934 ag71xx_wr(ag, AG71XX_REG_MAC_MFL, 0); 935 936 /* setup FIFO configuration registers */ 937 ag71xx_wr(ag, AG71XX_REG_FIFO_CFG0, FIFO_CFG0_INIT); 938 ag71xx_wr(ag, AG71XX_REG_FIFO_CFG1, ag->fifodata[0]); 939 ag71xx_wr(ag, AG71XX_REG_FIFO_CFG2, ag->fifodata[1]); 940 ag71xx_wr(ag, AG71XX_REG_FIFO_CFG4, FIFO_CFG4_INIT); 941 ag71xx_wr(ag, AG71XX_REG_FIFO_CFG5, FIFO_CFG5_INIT); 942 } 943 944 static unsigned int ag71xx_max_frame_len(unsigned int mtu) 945 { 946 return ETH_HLEN + VLAN_HLEN + mtu + ETH_FCS_LEN; 947 } 948 949 static void ag71xx_hw_set_macaddr(struct ag71xx *ag, unsigned char *mac) 950 { 951 u32 t; 952 953 t = (((u32)mac[5]) << 24) | (((u32)mac[4]) << 16) 954 | (((u32)mac[3]) << 8) | ((u32)mac[2]); 955 956 ag71xx_wr(ag, AG71XX_REG_MAC_ADDR1, t); 957 958 t = (((u32)mac[1]) << 24) | (((u32)mac[0]) << 16); 959 ag71xx_wr(ag, AG71XX_REG_MAC_ADDR2, t); 960 } 961 962 static void ag71xx_fast_reset(struct ag71xx *ag) 963 { 964 struct net_device *dev = ag->ndev; 965 u32 rx_ds; 966 u32 mii_reg; 967 968 ag71xx_hw_stop(ag); 969 970 mii_reg = ag71xx_rr(ag, AG71XX_REG_MII_CFG); 971 rx_ds = ag71xx_rr(ag, AG71XX_REG_RX_DESC); 972 973 ag71xx_tx_packets(ag, true); 974 975 reset_control_assert(ag->mac_reset); 976 usleep_range(10, 20); 977 reset_control_deassert(ag->mac_reset); 978 usleep_range(10, 20); 979 980 ag71xx_dma_reset(ag); 981 ag71xx_hw_setup(ag); 982 ag->tx_ring.curr = 0; 983 ag->tx_ring.dirty = 0; 984 netdev_reset_queue(ag->ndev); 985 986 /* setup max frame length */ 987 ag71xx_wr(ag, AG71XX_REG_MAC_MFL, 988 ag71xx_max_frame_len(ag->ndev->mtu)); 989 990 ag71xx_wr(ag, AG71XX_REG_RX_DESC, rx_ds); 991 ag71xx_wr(ag, AG71XX_REG_TX_DESC, ag->tx_ring.descs_dma); 992 ag71xx_wr(ag, AG71XX_REG_MII_CFG, mii_reg); 993 994 ag71xx_hw_set_macaddr(ag, dev->dev_addr); 995 } 996 997 static void ag71xx_hw_start(struct ag71xx *ag) 998 { 999 /* start RX engine */ 1000 ag71xx_wr(ag, AG71XX_REG_RX_CTRL, RX_CTRL_RXE); 1001 1002 /* enable interrupts */ 1003 ag71xx_wr(ag, AG71XX_REG_INT_ENABLE, AG71XX_INT_INIT); 1004 1005 netif_wake_queue(ag->ndev); 1006 } 1007 1008 static void ag71xx_mac_config(struct phylink_config *config, unsigned int mode, 1009 const struct phylink_link_state *state) 1010 { 1011 struct ag71xx *ag = netdev_priv(to_net_dev(config->dev)); 1012 1013 if (phylink_autoneg_inband(mode)) 1014 return; 1015 1016 if (!ag71xx_is(ag, AR7100) && !ag71xx_is(ag, AR9130)) 1017 ag71xx_fast_reset(ag); 1018 1019 if (ag->tx_ring.desc_split) { 1020 ag->fifodata[2] &= 0xffff; 1021 ag->fifodata[2] |= ((2048 - ag->tx_ring.desc_split) / 4) << 16; 1022 } 1023 1024 ag71xx_wr(ag, AG71XX_REG_FIFO_CFG3, ag->fifodata[2]); 1025 } 1026 1027 static void ag71xx_mac_validate(struct phylink_config *config, 1028 unsigned long *supported, 1029 struct phylink_link_state *state) 1030 { 1031 struct ag71xx *ag = netdev_priv(to_net_dev(config->dev)); 1032 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, }; 1033 1034 switch (state->interface) { 1035 case PHY_INTERFACE_MODE_NA: 1036 break; 1037 case PHY_INTERFACE_MODE_MII: 1038 if ((ag71xx_is(ag, AR9330) && ag->mac_idx == 0) || 1039 ag71xx_is(ag, AR9340) || 1040 ag71xx_is(ag, QCA9530) || 1041 (ag71xx_is(ag, QCA9550) && ag->mac_idx == 1)) 1042 break; 1043 goto unsupported; 1044 case PHY_INTERFACE_MODE_GMII: 1045 if ((ag71xx_is(ag, AR9330) && ag->mac_idx == 1) || 1046 (ag71xx_is(ag, AR9340) && ag->mac_idx == 1) || 1047 (ag71xx_is(ag, QCA9530) && ag->mac_idx == 1)) 1048 break; 1049 goto unsupported; 1050 case PHY_INTERFACE_MODE_SGMII: 1051 if (ag71xx_is(ag, QCA9550) && ag->mac_idx == 0) 1052 break; 1053 goto unsupported; 1054 case PHY_INTERFACE_MODE_RMII: 1055 if (ag71xx_is(ag, AR9340) && ag->mac_idx == 0) 1056 break; 1057 goto unsupported; 1058 case PHY_INTERFACE_MODE_RGMII: 1059 if ((ag71xx_is(ag, AR9340) && ag->mac_idx == 0) || 1060 (ag71xx_is(ag, QCA9550) && ag->mac_idx == 1)) 1061 break; 1062 goto unsupported; 1063 default: 1064 goto unsupported; 1065 } 1066 1067 phylink_set(mask, MII); 1068 1069 phylink_set(mask, Pause); 1070 phylink_set(mask, Asym_Pause); 1071 phylink_set(mask, Autoneg); 1072 phylink_set(mask, 10baseT_Half); 1073 phylink_set(mask, 10baseT_Full); 1074 phylink_set(mask, 100baseT_Half); 1075 phylink_set(mask, 100baseT_Full); 1076 1077 if (state->interface == PHY_INTERFACE_MODE_NA || 1078 state->interface == PHY_INTERFACE_MODE_SGMII || 1079 state->interface == PHY_INTERFACE_MODE_RGMII || 1080 state->interface == PHY_INTERFACE_MODE_GMII) { 1081 phylink_set(mask, 1000baseT_Full); 1082 phylink_set(mask, 1000baseX_Full); 1083 } 1084 1085 linkmode_and(supported, supported, mask); 1086 linkmode_and(state->advertising, state->advertising, mask); 1087 1088 return; 1089 unsupported: 1090 linkmode_zero(supported); 1091 } 1092 1093 static void ag71xx_mac_pcs_get_state(struct phylink_config *config, 1094 struct phylink_link_state *state) 1095 { 1096 state->link = 0; 1097 } 1098 1099 static void ag71xx_mac_an_restart(struct phylink_config *config) 1100 { 1101 /* Not Supported */ 1102 } 1103 1104 static void ag71xx_mac_link_down(struct phylink_config *config, 1105 unsigned int mode, phy_interface_t interface) 1106 { 1107 struct ag71xx *ag = netdev_priv(to_net_dev(config->dev)); 1108 1109 ag71xx_hw_stop(ag); 1110 } 1111 1112 static void ag71xx_mac_link_up(struct phylink_config *config, 1113 struct phy_device *phy, 1114 unsigned int mode, phy_interface_t interface, 1115 int speed, int duplex, 1116 bool tx_pause, bool rx_pause) 1117 { 1118 struct ag71xx *ag = netdev_priv(to_net_dev(config->dev)); 1119 u32 cfg1, cfg2; 1120 u32 ifctl; 1121 u32 fifo5; 1122 1123 cfg2 = ag71xx_rr(ag, AG71XX_REG_MAC_CFG2); 1124 cfg2 &= ~(MAC_CFG2_IF_1000 | MAC_CFG2_IF_10_100 | MAC_CFG2_FDX); 1125 cfg2 |= duplex ? MAC_CFG2_FDX : 0; 1126 1127 ifctl = ag71xx_rr(ag, AG71XX_REG_MAC_IFCTL); 1128 ifctl &= ~(MAC_IFCTL_SPEED); 1129 1130 fifo5 = ag71xx_rr(ag, AG71XX_REG_FIFO_CFG5); 1131 fifo5 &= ~FIFO_CFG5_BM; 1132 1133 switch (speed) { 1134 case SPEED_1000: 1135 cfg2 |= MAC_CFG2_IF_1000; 1136 fifo5 |= FIFO_CFG5_BM; 1137 break; 1138 case SPEED_100: 1139 cfg2 |= MAC_CFG2_IF_10_100; 1140 ifctl |= MAC_IFCTL_SPEED; 1141 break; 1142 case SPEED_10: 1143 cfg2 |= MAC_CFG2_IF_10_100; 1144 break; 1145 default: 1146 return; 1147 } 1148 1149 ag71xx_wr(ag, AG71XX_REG_MAC_CFG2, cfg2); 1150 ag71xx_wr(ag, AG71XX_REG_FIFO_CFG5, fifo5); 1151 ag71xx_wr(ag, AG71XX_REG_MAC_IFCTL, ifctl); 1152 1153 cfg1 = ag71xx_rr(ag, AG71XX_REG_MAC_CFG1); 1154 cfg1 &= ~(MAC_CFG1_TFC | MAC_CFG1_RFC); 1155 if (tx_pause) 1156 cfg1 |= MAC_CFG1_TFC; 1157 1158 if (rx_pause) 1159 cfg1 |= MAC_CFG1_RFC; 1160 ag71xx_wr(ag, AG71XX_REG_MAC_CFG1, cfg1); 1161 1162 ag71xx_hw_start(ag); 1163 } 1164 1165 static const struct phylink_mac_ops ag71xx_phylink_mac_ops = { 1166 .validate = ag71xx_mac_validate, 1167 .mac_pcs_get_state = ag71xx_mac_pcs_get_state, 1168 .mac_an_restart = ag71xx_mac_an_restart, 1169 .mac_config = ag71xx_mac_config, 1170 .mac_link_down = ag71xx_mac_link_down, 1171 .mac_link_up = ag71xx_mac_link_up, 1172 }; 1173 1174 static int ag71xx_phylink_setup(struct ag71xx *ag) 1175 { 1176 struct phylink *phylink; 1177 1178 ag->phylink_config.dev = &ag->ndev->dev; 1179 ag->phylink_config.type = PHYLINK_NETDEV; 1180 1181 phylink = phylink_create(&ag->phylink_config, ag->pdev->dev.fwnode, 1182 ag->phy_if_mode, &ag71xx_phylink_mac_ops); 1183 if (IS_ERR(phylink)) 1184 return PTR_ERR(phylink); 1185 1186 ag->phylink = phylink; 1187 return 0; 1188 } 1189 1190 static void ag71xx_ring_tx_clean(struct ag71xx *ag) 1191 { 1192 struct ag71xx_ring *ring = &ag->tx_ring; 1193 int ring_mask = BIT(ring->order) - 1; 1194 u32 bytes_compl = 0, pkts_compl = 0; 1195 struct net_device *ndev = ag->ndev; 1196 1197 while (ring->curr != ring->dirty) { 1198 struct ag71xx_desc *desc; 1199 u32 i = ring->dirty & ring_mask; 1200 1201 desc = ag71xx_ring_desc(ring, i); 1202 if (!ag71xx_desc_empty(desc)) { 1203 desc->ctrl = 0; 1204 ndev->stats.tx_errors++; 1205 } 1206 1207 if (ring->buf[i].tx.skb) { 1208 bytes_compl += ring->buf[i].tx.len; 1209 pkts_compl++; 1210 dev_kfree_skb_any(ring->buf[i].tx.skb); 1211 } 1212 ring->buf[i].tx.skb = NULL; 1213 ring->dirty++; 1214 } 1215 1216 /* flush descriptors */ 1217 wmb(); 1218 1219 netdev_completed_queue(ndev, pkts_compl, bytes_compl); 1220 } 1221 1222 static void ag71xx_ring_tx_init(struct ag71xx *ag) 1223 { 1224 struct ag71xx_ring *ring = &ag->tx_ring; 1225 int ring_size = BIT(ring->order); 1226 int ring_mask = ring_size - 1; 1227 int i; 1228 1229 for (i = 0; i < ring_size; i++) { 1230 struct ag71xx_desc *desc = ag71xx_ring_desc(ring, i); 1231 1232 desc->next = (u32)(ring->descs_dma + 1233 AG71XX_DESC_SIZE * ((i + 1) & ring_mask)); 1234 1235 desc->ctrl = DESC_EMPTY; 1236 ring->buf[i].tx.skb = NULL; 1237 } 1238 1239 /* flush descriptors */ 1240 wmb(); 1241 1242 ring->curr = 0; 1243 ring->dirty = 0; 1244 netdev_reset_queue(ag->ndev); 1245 } 1246 1247 static void ag71xx_ring_rx_clean(struct ag71xx *ag) 1248 { 1249 struct ag71xx_ring *ring = &ag->rx_ring; 1250 int ring_size = BIT(ring->order); 1251 int i; 1252 1253 if (!ring->buf) 1254 return; 1255 1256 for (i = 0; i < ring_size; i++) 1257 if (ring->buf[i].rx.rx_buf) { 1258 dma_unmap_single(&ag->pdev->dev, 1259 ring->buf[i].rx.dma_addr, 1260 ag->rx_buf_size, DMA_FROM_DEVICE); 1261 skb_free_frag(ring->buf[i].rx.rx_buf); 1262 } 1263 } 1264 1265 static int ag71xx_buffer_size(struct ag71xx *ag) 1266 { 1267 return ag->rx_buf_size + 1268 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 1269 } 1270 1271 static bool ag71xx_fill_rx_buf(struct ag71xx *ag, struct ag71xx_buf *buf, 1272 int offset, 1273 void *(*alloc)(unsigned int size)) 1274 { 1275 struct ag71xx_ring *ring = &ag->rx_ring; 1276 struct ag71xx_desc *desc; 1277 void *data; 1278 1279 desc = ag71xx_ring_desc(ring, buf - &ring->buf[0]); 1280 1281 data = alloc(ag71xx_buffer_size(ag)); 1282 if (!data) 1283 return false; 1284 1285 buf->rx.rx_buf = data; 1286 buf->rx.dma_addr = dma_map_single(&ag->pdev->dev, data, ag->rx_buf_size, 1287 DMA_FROM_DEVICE); 1288 desc->data = (u32)buf->rx.dma_addr + offset; 1289 return true; 1290 } 1291 1292 static int ag71xx_ring_rx_init(struct ag71xx *ag) 1293 { 1294 struct ag71xx_ring *ring = &ag->rx_ring; 1295 struct net_device *ndev = ag->ndev; 1296 int ring_mask = BIT(ring->order) - 1; 1297 int ring_size = BIT(ring->order); 1298 unsigned int i; 1299 int ret; 1300 1301 ret = 0; 1302 for (i = 0; i < ring_size; i++) { 1303 struct ag71xx_desc *desc = ag71xx_ring_desc(ring, i); 1304 1305 desc->next = (u32)(ring->descs_dma + 1306 AG71XX_DESC_SIZE * ((i + 1) & ring_mask)); 1307 1308 netif_dbg(ag, rx_status, ndev, "RX desc at %p, next is %08x\n", 1309 desc, desc->next); 1310 } 1311 1312 for (i = 0; i < ring_size; i++) { 1313 struct ag71xx_desc *desc = ag71xx_ring_desc(ring, i); 1314 1315 if (!ag71xx_fill_rx_buf(ag, &ring->buf[i], ag->rx_buf_offset, 1316 netdev_alloc_frag)) { 1317 ret = -ENOMEM; 1318 break; 1319 } 1320 1321 desc->ctrl = DESC_EMPTY; 1322 } 1323 1324 /* flush descriptors */ 1325 wmb(); 1326 1327 ring->curr = 0; 1328 ring->dirty = 0; 1329 1330 return ret; 1331 } 1332 1333 static int ag71xx_ring_rx_refill(struct ag71xx *ag) 1334 { 1335 struct ag71xx_ring *ring = &ag->rx_ring; 1336 int ring_mask = BIT(ring->order) - 1; 1337 int offset = ag->rx_buf_offset; 1338 unsigned int count; 1339 1340 count = 0; 1341 for (; ring->curr - ring->dirty > 0; ring->dirty++) { 1342 struct ag71xx_desc *desc; 1343 unsigned int i; 1344 1345 i = ring->dirty & ring_mask; 1346 desc = ag71xx_ring_desc(ring, i); 1347 1348 if (!ring->buf[i].rx.rx_buf && 1349 !ag71xx_fill_rx_buf(ag, &ring->buf[i], offset, 1350 napi_alloc_frag)) 1351 break; 1352 1353 desc->ctrl = DESC_EMPTY; 1354 count++; 1355 } 1356 1357 /* flush descriptors */ 1358 wmb(); 1359 1360 netif_dbg(ag, rx_status, ag->ndev, "%u rx descriptors refilled\n", 1361 count); 1362 1363 return count; 1364 } 1365 1366 static int ag71xx_rings_init(struct ag71xx *ag) 1367 { 1368 struct ag71xx_ring *tx = &ag->tx_ring; 1369 struct ag71xx_ring *rx = &ag->rx_ring; 1370 int ring_size, tx_size; 1371 1372 ring_size = BIT(tx->order) + BIT(rx->order); 1373 tx_size = BIT(tx->order); 1374 1375 tx->buf = kcalloc(ring_size, sizeof(*tx->buf), GFP_KERNEL); 1376 if (!tx->buf) 1377 return -ENOMEM; 1378 1379 tx->descs_cpu = dma_alloc_coherent(&ag->pdev->dev, 1380 ring_size * AG71XX_DESC_SIZE, 1381 &tx->descs_dma, GFP_KERNEL); 1382 if (!tx->descs_cpu) { 1383 kfree(tx->buf); 1384 tx->buf = NULL; 1385 return -ENOMEM; 1386 } 1387 1388 rx->buf = &tx->buf[tx_size]; 1389 rx->descs_cpu = ((void *)tx->descs_cpu) + tx_size * AG71XX_DESC_SIZE; 1390 rx->descs_dma = tx->descs_dma + tx_size * AG71XX_DESC_SIZE; 1391 1392 ag71xx_ring_tx_init(ag); 1393 return ag71xx_ring_rx_init(ag); 1394 } 1395 1396 static void ag71xx_rings_free(struct ag71xx *ag) 1397 { 1398 struct ag71xx_ring *tx = &ag->tx_ring; 1399 struct ag71xx_ring *rx = &ag->rx_ring; 1400 int ring_size; 1401 1402 ring_size = BIT(tx->order) + BIT(rx->order); 1403 1404 if (tx->descs_cpu) 1405 dma_free_coherent(&ag->pdev->dev, ring_size * AG71XX_DESC_SIZE, 1406 tx->descs_cpu, tx->descs_dma); 1407 1408 kfree(tx->buf); 1409 1410 tx->descs_cpu = NULL; 1411 rx->descs_cpu = NULL; 1412 tx->buf = NULL; 1413 rx->buf = NULL; 1414 } 1415 1416 static void ag71xx_rings_cleanup(struct ag71xx *ag) 1417 { 1418 ag71xx_ring_rx_clean(ag); 1419 ag71xx_ring_tx_clean(ag); 1420 ag71xx_rings_free(ag); 1421 1422 netdev_reset_queue(ag->ndev); 1423 } 1424 1425 static void ag71xx_hw_init(struct ag71xx *ag) 1426 { 1427 ag71xx_hw_stop(ag); 1428 1429 ag71xx_sb(ag, AG71XX_REG_MAC_CFG1, MAC_CFG1_SR); 1430 usleep_range(20, 30); 1431 1432 reset_control_assert(ag->mac_reset); 1433 msleep(100); 1434 reset_control_deassert(ag->mac_reset); 1435 msleep(200); 1436 1437 ag71xx_hw_setup(ag); 1438 1439 ag71xx_dma_reset(ag); 1440 } 1441 1442 static int ag71xx_hw_enable(struct ag71xx *ag) 1443 { 1444 int ret; 1445 1446 ret = ag71xx_rings_init(ag); 1447 if (ret) 1448 return ret; 1449 1450 napi_enable(&ag->napi); 1451 ag71xx_wr(ag, AG71XX_REG_TX_DESC, ag->tx_ring.descs_dma); 1452 ag71xx_wr(ag, AG71XX_REG_RX_DESC, ag->rx_ring.descs_dma); 1453 netif_start_queue(ag->ndev); 1454 1455 return 0; 1456 } 1457 1458 static void ag71xx_hw_disable(struct ag71xx *ag) 1459 { 1460 netif_stop_queue(ag->ndev); 1461 1462 ag71xx_hw_stop(ag); 1463 ag71xx_dma_reset(ag); 1464 1465 napi_disable(&ag->napi); 1466 del_timer_sync(&ag->oom_timer); 1467 1468 ag71xx_rings_cleanup(ag); 1469 } 1470 1471 static int ag71xx_open(struct net_device *ndev) 1472 { 1473 struct ag71xx *ag = netdev_priv(ndev); 1474 unsigned int max_frame_len; 1475 int ret; 1476 1477 ret = phylink_of_phy_connect(ag->phylink, ag->pdev->dev.of_node, 0); 1478 if (ret) { 1479 netif_err(ag, link, ndev, "phylink_of_phy_connect filed with err: %i\n", 1480 ret); 1481 goto err; 1482 } 1483 1484 max_frame_len = ag71xx_max_frame_len(ndev->mtu); 1485 ag->rx_buf_size = 1486 SKB_DATA_ALIGN(max_frame_len + NET_SKB_PAD + NET_IP_ALIGN); 1487 1488 /* setup max frame length */ 1489 ag71xx_wr(ag, AG71XX_REG_MAC_MFL, max_frame_len); 1490 ag71xx_hw_set_macaddr(ag, ndev->dev_addr); 1491 1492 ret = ag71xx_hw_enable(ag); 1493 if (ret) 1494 goto err; 1495 1496 phylink_start(ag->phylink); 1497 1498 return 0; 1499 1500 err: 1501 ag71xx_rings_cleanup(ag); 1502 return ret; 1503 } 1504 1505 static int ag71xx_stop(struct net_device *ndev) 1506 { 1507 struct ag71xx *ag = netdev_priv(ndev); 1508 1509 phylink_stop(ag->phylink); 1510 phylink_disconnect_phy(ag->phylink); 1511 ag71xx_hw_disable(ag); 1512 1513 return 0; 1514 } 1515 1516 static int ag71xx_fill_dma_desc(struct ag71xx_ring *ring, u32 addr, int len) 1517 { 1518 int i, ring_mask, ndesc, split; 1519 struct ag71xx_desc *desc; 1520 1521 ring_mask = BIT(ring->order) - 1; 1522 ndesc = 0; 1523 split = ring->desc_split; 1524 1525 if (!split) 1526 split = len; 1527 1528 while (len > 0) { 1529 unsigned int cur_len = len; 1530 1531 i = (ring->curr + ndesc) & ring_mask; 1532 desc = ag71xx_ring_desc(ring, i); 1533 1534 if (!ag71xx_desc_empty(desc)) 1535 return -1; 1536 1537 if (cur_len > split) { 1538 cur_len = split; 1539 1540 /* TX will hang if DMA transfers <= 4 bytes, 1541 * make sure next segment is more than 4 bytes long. 1542 */ 1543 if (len <= split + 4) 1544 cur_len -= 4; 1545 } 1546 1547 desc->data = addr; 1548 addr += cur_len; 1549 len -= cur_len; 1550 1551 if (len > 0) 1552 cur_len |= DESC_MORE; 1553 1554 /* prevent early tx attempt of this descriptor */ 1555 if (!ndesc) 1556 cur_len |= DESC_EMPTY; 1557 1558 desc->ctrl = cur_len; 1559 ndesc++; 1560 } 1561 1562 return ndesc; 1563 } 1564 1565 static netdev_tx_t ag71xx_hard_start_xmit(struct sk_buff *skb, 1566 struct net_device *ndev) 1567 { 1568 int i, n, ring_min, ring_mask, ring_size; 1569 struct ag71xx *ag = netdev_priv(ndev); 1570 struct ag71xx_ring *ring; 1571 struct ag71xx_desc *desc; 1572 dma_addr_t dma_addr; 1573 1574 ring = &ag->tx_ring; 1575 ring_mask = BIT(ring->order) - 1; 1576 ring_size = BIT(ring->order); 1577 1578 if (skb->len <= 4) { 1579 netif_dbg(ag, tx_err, ndev, "packet len is too small\n"); 1580 goto err_drop; 1581 } 1582 1583 dma_addr = dma_map_single(&ag->pdev->dev, skb->data, skb->len, 1584 DMA_TO_DEVICE); 1585 1586 i = ring->curr & ring_mask; 1587 desc = ag71xx_ring_desc(ring, i); 1588 1589 /* setup descriptor fields */ 1590 n = ag71xx_fill_dma_desc(ring, (u32)dma_addr, 1591 skb->len & ag->dcfg->desc_pktlen_mask); 1592 if (n < 0) 1593 goto err_drop_unmap; 1594 1595 i = (ring->curr + n - 1) & ring_mask; 1596 ring->buf[i].tx.len = skb->len; 1597 ring->buf[i].tx.skb = skb; 1598 1599 netdev_sent_queue(ndev, skb->len); 1600 1601 skb_tx_timestamp(skb); 1602 1603 desc->ctrl &= ~DESC_EMPTY; 1604 ring->curr += n; 1605 1606 /* flush descriptor */ 1607 wmb(); 1608 1609 ring_min = 2; 1610 if (ring->desc_split) 1611 ring_min *= AG71XX_TX_RING_DS_PER_PKT; 1612 1613 if (ring->curr - ring->dirty >= ring_size - ring_min) { 1614 netif_dbg(ag, tx_err, ndev, "tx queue full\n"); 1615 netif_stop_queue(ndev); 1616 } 1617 1618 netif_dbg(ag, tx_queued, ndev, "packet injected into TX queue\n"); 1619 1620 /* enable TX engine */ 1621 ag71xx_wr(ag, AG71XX_REG_TX_CTRL, TX_CTRL_TXE); 1622 1623 return NETDEV_TX_OK; 1624 1625 err_drop_unmap: 1626 dma_unmap_single(&ag->pdev->dev, dma_addr, skb->len, DMA_TO_DEVICE); 1627 1628 err_drop: 1629 ndev->stats.tx_dropped++; 1630 1631 dev_kfree_skb(skb); 1632 return NETDEV_TX_OK; 1633 } 1634 1635 static void ag71xx_oom_timer_handler(struct timer_list *t) 1636 { 1637 struct ag71xx *ag = from_timer(ag, t, oom_timer); 1638 1639 napi_schedule(&ag->napi); 1640 } 1641 1642 static void ag71xx_tx_timeout(struct net_device *ndev, unsigned int txqueue) 1643 { 1644 struct ag71xx *ag = netdev_priv(ndev); 1645 1646 netif_err(ag, tx_err, ndev, "tx timeout\n"); 1647 1648 schedule_delayed_work(&ag->restart_work, 1); 1649 } 1650 1651 static void ag71xx_restart_work_func(struct work_struct *work) 1652 { 1653 struct ag71xx *ag = container_of(work, struct ag71xx, 1654 restart_work.work); 1655 1656 rtnl_lock(); 1657 ag71xx_hw_disable(ag); 1658 ag71xx_hw_enable(ag); 1659 1660 phylink_stop(ag->phylink); 1661 phylink_start(ag->phylink); 1662 1663 rtnl_unlock(); 1664 } 1665 1666 static int ag71xx_rx_packets(struct ag71xx *ag, int limit) 1667 { 1668 struct net_device *ndev = ag->ndev; 1669 int ring_mask, ring_size, done = 0; 1670 unsigned int pktlen_mask, offset; 1671 struct ag71xx_ring *ring; 1672 struct list_head rx_list; 1673 struct sk_buff *skb; 1674 1675 ring = &ag->rx_ring; 1676 pktlen_mask = ag->dcfg->desc_pktlen_mask; 1677 offset = ag->rx_buf_offset; 1678 ring_mask = BIT(ring->order) - 1; 1679 ring_size = BIT(ring->order); 1680 1681 netif_dbg(ag, rx_status, ndev, "rx packets, limit=%d, curr=%u, dirty=%u\n", 1682 limit, ring->curr, ring->dirty); 1683 1684 INIT_LIST_HEAD(&rx_list); 1685 1686 while (done < limit) { 1687 unsigned int i = ring->curr & ring_mask; 1688 struct ag71xx_desc *desc = ag71xx_ring_desc(ring, i); 1689 int pktlen; 1690 int err = 0; 1691 1692 if (ag71xx_desc_empty(desc)) 1693 break; 1694 1695 if ((ring->dirty + ring_size) == ring->curr) { 1696 WARN_ONCE(1, "RX out of ring"); 1697 break; 1698 } 1699 1700 ag71xx_wr(ag, AG71XX_REG_RX_STATUS, RX_STATUS_PR); 1701 1702 pktlen = desc->ctrl & pktlen_mask; 1703 pktlen -= ETH_FCS_LEN; 1704 1705 dma_unmap_single(&ag->pdev->dev, ring->buf[i].rx.dma_addr, 1706 ag->rx_buf_size, DMA_FROM_DEVICE); 1707 1708 ndev->stats.rx_packets++; 1709 ndev->stats.rx_bytes += pktlen; 1710 1711 skb = build_skb(ring->buf[i].rx.rx_buf, ag71xx_buffer_size(ag)); 1712 if (!skb) { 1713 skb_free_frag(ring->buf[i].rx.rx_buf); 1714 goto next; 1715 } 1716 1717 skb_reserve(skb, offset); 1718 skb_put(skb, pktlen); 1719 1720 if (err) { 1721 ndev->stats.rx_dropped++; 1722 kfree_skb(skb); 1723 } else { 1724 skb->dev = ndev; 1725 skb->ip_summed = CHECKSUM_NONE; 1726 list_add_tail(&skb->list, &rx_list); 1727 } 1728 1729 next: 1730 ring->buf[i].rx.rx_buf = NULL; 1731 done++; 1732 1733 ring->curr++; 1734 } 1735 1736 ag71xx_ring_rx_refill(ag); 1737 1738 list_for_each_entry(skb, &rx_list, list) 1739 skb->protocol = eth_type_trans(skb, ndev); 1740 netif_receive_skb_list(&rx_list); 1741 1742 netif_dbg(ag, rx_status, ndev, "rx finish, curr=%u, dirty=%u, done=%d\n", 1743 ring->curr, ring->dirty, done); 1744 1745 return done; 1746 } 1747 1748 static int ag71xx_poll(struct napi_struct *napi, int limit) 1749 { 1750 struct ag71xx *ag = container_of(napi, struct ag71xx, napi); 1751 struct ag71xx_ring *rx_ring = &ag->rx_ring; 1752 int rx_ring_size = BIT(rx_ring->order); 1753 struct net_device *ndev = ag->ndev; 1754 int tx_done, rx_done; 1755 u32 status; 1756 1757 tx_done = ag71xx_tx_packets(ag, false); 1758 1759 netif_dbg(ag, rx_status, ndev, "processing RX ring\n"); 1760 rx_done = ag71xx_rx_packets(ag, limit); 1761 1762 if (!rx_ring->buf[rx_ring->dirty % rx_ring_size].rx.rx_buf) 1763 goto oom; 1764 1765 status = ag71xx_rr(ag, AG71XX_REG_RX_STATUS); 1766 if (unlikely(status & RX_STATUS_OF)) { 1767 ag71xx_wr(ag, AG71XX_REG_RX_STATUS, RX_STATUS_OF); 1768 ndev->stats.rx_fifo_errors++; 1769 1770 /* restart RX */ 1771 ag71xx_wr(ag, AG71XX_REG_RX_CTRL, RX_CTRL_RXE); 1772 } 1773 1774 if (rx_done < limit) { 1775 if (status & RX_STATUS_PR) 1776 goto more; 1777 1778 status = ag71xx_rr(ag, AG71XX_REG_TX_STATUS); 1779 if (status & TX_STATUS_PS) 1780 goto more; 1781 1782 netif_dbg(ag, rx_status, ndev, "disable polling mode, rx=%d, tx=%d,limit=%d\n", 1783 rx_done, tx_done, limit); 1784 1785 napi_complete(napi); 1786 1787 /* enable interrupts */ 1788 ag71xx_int_enable(ag, AG71XX_INT_POLL); 1789 return rx_done; 1790 } 1791 1792 more: 1793 netif_dbg(ag, rx_status, ndev, "stay in polling mode, rx=%d, tx=%d, limit=%d\n", 1794 rx_done, tx_done, limit); 1795 return limit; 1796 1797 oom: 1798 netif_err(ag, rx_err, ndev, "out of memory\n"); 1799 1800 mod_timer(&ag->oom_timer, jiffies + AG71XX_OOM_REFILL); 1801 napi_complete(napi); 1802 return 0; 1803 } 1804 1805 static irqreturn_t ag71xx_interrupt(int irq, void *dev_id) 1806 { 1807 struct net_device *ndev = dev_id; 1808 struct ag71xx *ag; 1809 u32 status; 1810 1811 ag = netdev_priv(ndev); 1812 status = ag71xx_rr(ag, AG71XX_REG_INT_STATUS); 1813 1814 if (unlikely(!status)) 1815 return IRQ_NONE; 1816 1817 if (unlikely(status & AG71XX_INT_ERR)) { 1818 if (status & AG71XX_INT_TX_BE) { 1819 ag71xx_wr(ag, AG71XX_REG_TX_STATUS, TX_STATUS_BE); 1820 netif_err(ag, intr, ndev, "TX BUS error\n"); 1821 } 1822 if (status & AG71XX_INT_RX_BE) { 1823 ag71xx_wr(ag, AG71XX_REG_RX_STATUS, RX_STATUS_BE); 1824 netif_err(ag, intr, ndev, "RX BUS error\n"); 1825 } 1826 } 1827 1828 if (likely(status & AG71XX_INT_POLL)) { 1829 ag71xx_int_disable(ag, AG71XX_INT_POLL); 1830 netif_dbg(ag, intr, ndev, "enable polling mode\n"); 1831 napi_schedule(&ag->napi); 1832 } 1833 1834 return IRQ_HANDLED; 1835 } 1836 1837 static int ag71xx_change_mtu(struct net_device *ndev, int new_mtu) 1838 { 1839 struct ag71xx *ag = netdev_priv(ndev); 1840 1841 ndev->mtu = new_mtu; 1842 ag71xx_wr(ag, AG71XX_REG_MAC_MFL, 1843 ag71xx_max_frame_len(ndev->mtu)); 1844 1845 return 0; 1846 } 1847 1848 static const struct net_device_ops ag71xx_netdev_ops = { 1849 .ndo_open = ag71xx_open, 1850 .ndo_stop = ag71xx_stop, 1851 .ndo_start_xmit = ag71xx_hard_start_xmit, 1852 .ndo_eth_ioctl = phy_do_ioctl, 1853 .ndo_tx_timeout = ag71xx_tx_timeout, 1854 .ndo_change_mtu = ag71xx_change_mtu, 1855 .ndo_set_mac_address = eth_mac_addr, 1856 .ndo_validate_addr = eth_validate_addr, 1857 }; 1858 1859 static const u32 ar71xx_addr_ar7100[] = { 1860 0x19000000, 0x1a000000, 1861 }; 1862 1863 static int ag71xx_probe(struct platform_device *pdev) 1864 { 1865 struct device_node *np = pdev->dev.of_node; 1866 const struct ag71xx_dcfg *dcfg; 1867 struct net_device *ndev; 1868 struct resource *res; 1869 int tx_size, err, i; 1870 struct ag71xx *ag; 1871 1872 if (!np) 1873 return -ENODEV; 1874 1875 ndev = devm_alloc_etherdev(&pdev->dev, sizeof(*ag)); 1876 if (!ndev) 1877 return -ENOMEM; 1878 1879 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1880 if (!res) 1881 return -EINVAL; 1882 1883 dcfg = of_device_get_match_data(&pdev->dev); 1884 if (!dcfg) 1885 return -EINVAL; 1886 1887 ag = netdev_priv(ndev); 1888 ag->mac_idx = -1; 1889 for (i = 0; i < ARRAY_SIZE(ar71xx_addr_ar7100); i++) { 1890 if (ar71xx_addr_ar7100[i] == res->start) 1891 ag->mac_idx = i; 1892 } 1893 1894 if (ag->mac_idx < 0) { 1895 netif_err(ag, probe, ndev, "unknown mac idx\n"); 1896 return -EINVAL; 1897 } 1898 1899 ag->clk_eth = devm_clk_get(&pdev->dev, "eth"); 1900 if (IS_ERR(ag->clk_eth)) { 1901 netif_err(ag, probe, ndev, "Failed to get eth clk.\n"); 1902 return PTR_ERR(ag->clk_eth); 1903 } 1904 1905 SET_NETDEV_DEV(ndev, &pdev->dev); 1906 1907 ag->pdev = pdev; 1908 ag->ndev = ndev; 1909 ag->dcfg = dcfg; 1910 ag->msg_enable = netif_msg_init(-1, AG71XX_DEFAULT_MSG_ENABLE); 1911 memcpy(ag->fifodata, dcfg->fifodata, sizeof(ag->fifodata)); 1912 1913 ag->mac_reset = devm_reset_control_get(&pdev->dev, "mac"); 1914 if (IS_ERR(ag->mac_reset)) { 1915 netif_err(ag, probe, ndev, "missing mac reset\n"); 1916 err = PTR_ERR(ag->mac_reset); 1917 goto err_free; 1918 } 1919 1920 ag->mac_base = devm_ioremap(&pdev->dev, res->start, resource_size(res)); 1921 if (!ag->mac_base) { 1922 err = -ENOMEM; 1923 goto err_free; 1924 } 1925 1926 ndev->irq = platform_get_irq(pdev, 0); 1927 err = devm_request_irq(&pdev->dev, ndev->irq, ag71xx_interrupt, 1928 0x0, dev_name(&pdev->dev), ndev); 1929 if (err) { 1930 netif_err(ag, probe, ndev, "unable to request IRQ %d\n", 1931 ndev->irq); 1932 goto err_free; 1933 } 1934 1935 ndev->netdev_ops = &ag71xx_netdev_ops; 1936 ndev->ethtool_ops = &ag71xx_ethtool_ops; 1937 1938 INIT_DELAYED_WORK(&ag->restart_work, ag71xx_restart_work_func); 1939 timer_setup(&ag->oom_timer, ag71xx_oom_timer_handler, 0); 1940 1941 tx_size = AG71XX_TX_RING_SIZE_DEFAULT; 1942 ag->rx_ring.order = ag71xx_ring_size_order(AG71XX_RX_RING_SIZE_DEFAULT); 1943 1944 ndev->min_mtu = 68; 1945 ndev->max_mtu = dcfg->max_frame_len - ag71xx_max_frame_len(0); 1946 1947 ag->rx_buf_offset = NET_SKB_PAD; 1948 if (!ag71xx_is(ag, AR7100) && !ag71xx_is(ag, AR9130)) 1949 ag->rx_buf_offset += NET_IP_ALIGN; 1950 1951 if (ag71xx_is(ag, AR7100)) { 1952 ag->tx_ring.desc_split = AG71XX_TX_RING_SPLIT; 1953 tx_size *= AG71XX_TX_RING_DS_PER_PKT; 1954 } 1955 ag->tx_ring.order = ag71xx_ring_size_order(tx_size); 1956 1957 ag->stop_desc = dmam_alloc_coherent(&pdev->dev, 1958 sizeof(struct ag71xx_desc), 1959 &ag->stop_desc_dma, GFP_KERNEL); 1960 if (!ag->stop_desc) { 1961 err = -ENOMEM; 1962 goto err_free; 1963 } 1964 1965 ag->stop_desc->data = 0; 1966 ag->stop_desc->ctrl = 0; 1967 ag->stop_desc->next = (u32)ag->stop_desc_dma; 1968 1969 err = of_get_ethdev_address(np, ndev); 1970 if (err) { 1971 netif_err(ag, probe, ndev, "invalid MAC address, using random address\n"); 1972 eth_hw_addr_random(ndev); 1973 } 1974 1975 err = of_get_phy_mode(np, &ag->phy_if_mode); 1976 if (err) { 1977 netif_err(ag, probe, ndev, "missing phy-mode property in DT\n"); 1978 goto err_free; 1979 } 1980 1981 netif_napi_add(ndev, &ag->napi, ag71xx_poll, AG71XX_NAPI_WEIGHT); 1982 1983 err = clk_prepare_enable(ag->clk_eth); 1984 if (err) { 1985 netif_err(ag, probe, ndev, "Failed to enable eth clk.\n"); 1986 goto err_free; 1987 } 1988 1989 ag71xx_wr(ag, AG71XX_REG_MAC_CFG1, 0); 1990 1991 ag71xx_hw_init(ag); 1992 1993 err = ag71xx_mdio_probe(ag); 1994 if (err) 1995 goto err_put_clk; 1996 1997 platform_set_drvdata(pdev, ndev); 1998 1999 err = ag71xx_phylink_setup(ag); 2000 if (err) { 2001 netif_err(ag, probe, ndev, "failed to setup phylink (%d)\n", err); 2002 goto err_mdio_remove; 2003 } 2004 2005 err = register_netdev(ndev); 2006 if (err) { 2007 netif_err(ag, probe, ndev, "unable to register net device\n"); 2008 platform_set_drvdata(pdev, NULL); 2009 goto err_mdio_remove; 2010 } 2011 2012 netif_info(ag, probe, ndev, "Atheros AG71xx at 0x%08lx, irq %d, mode:%s\n", 2013 (unsigned long)ag->mac_base, ndev->irq, 2014 phy_modes(ag->phy_if_mode)); 2015 2016 return 0; 2017 2018 err_mdio_remove: 2019 ag71xx_mdio_remove(ag); 2020 err_put_clk: 2021 clk_disable_unprepare(ag->clk_eth); 2022 err_free: 2023 free_netdev(ndev); 2024 return err; 2025 } 2026 2027 static int ag71xx_remove(struct platform_device *pdev) 2028 { 2029 struct net_device *ndev = platform_get_drvdata(pdev); 2030 struct ag71xx *ag; 2031 2032 if (!ndev) 2033 return 0; 2034 2035 ag = netdev_priv(ndev); 2036 unregister_netdev(ndev); 2037 ag71xx_mdio_remove(ag); 2038 clk_disable_unprepare(ag->clk_eth); 2039 platform_set_drvdata(pdev, NULL); 2040 2041 return 0; 2042 } 2043 2044 static const u32 ar71xx_fifo_ar7100[] = { 2045 0x0fff0000, 0x00001fff, 0x00780fff, 2046 }; 2047 2048 static const u32 ar71xx_fifo_ar9130[] = { 2049 0x0fff0000, 0x00001fff, 0x008001ff, 2050 }; 2051 2052 static const u32 ar71xx_fifo_ar9330[] = { 2053 0x0010ffff, 0x015500aa, 0x01f00140, 2054 }; 2055 2056 static const struct ag71xx_dcfg ag71xx_dcfg_ar7100 = { 2057 .type = AR7100, 2058 .fifodata = ar71xx_fifo_ar7100, 2059 .max_frame_len = 1540, 2060 .desc_pktlen_mask = SZ_4K - 1, 2061 .tx_hang_workaround = false, 2062 }; 2063 2064 static const struct ag71xx_dcfg ag71xx_dcfg_ar7240 = { 2065 .type = AR7240, 2066 .fifodata = ar71xx_fifo_ar7100, 2067 .max_frame_len = 1540, 2068 .desc_pktlen_mask = SZ_4K - 1, 2069 .tx_hang_workaround = true, 2070 }; 2071 2072 static const struct ag71xx_dcfg ag71xx_dcfg_ar9130 = { 2073 .type = AR9130, 2074 .fifodata = ar71xx_fifo_ar9130, 2075 .max_frame_len = 1540, 2076 .desc_pktlen_mask = SZ_4K - 1, 2077 .tx_hang_workaround = false, 2078 }; 2079 2080 static const struct ag71xx_dcfg ag71xx_dcfg_ar9330 = { 2081 .type = AR9330, 2082 .fifodata = ar71xx_fifo_ar9330, 2083 .max_frame_len = 1540, 2084 .desc_pktlen_mask = SZ_4K - 1, 2085 .tx_hang_workaround = true, 2086 }; 2087 2088 static const struct ag71xx_dcfg ag71xx_dcfg_ar9340 = { 2089 .type = AR9340, 2090 .fifodata = ar71xx_fifo_ar9330, 2091 .max_frame_len = SZ_16K - 1, 2092 .desc_pktlen_mask = SZ_16K - 1, 2093 .tx_hang_workaround = true, 2094 }; 2095 2096 static const struct ag71xx_dcfg ag71xx_dcfg_qca9530 = { 2097 .type = QCA9530, 2098 .fifodata = ar71xx_fifo_ar9330, 2099 .max_frame_len = SZ_16K - 1, 2100 .desc_pktlen_mask = SZ_16K - 1, 2101 .tx_hang_workaround = true, 2102 }; 2103 2104 static const struct ag71xx_dcfg ag71xx_dcfg_qca9550 = { 2105 .type = QCA9550, 2106 .fifodata = ar71xx_fifo_ar9330, 2107 .max_frame_len = 1540, 2108 .desc_pktlen_mask = SZ_16K - 1, 2109 .tx_hang_workaround = true, 2110 }; 2111 2112 static const struct of_device_id ag71xx_match[] = { 2113 { .compatible = "qca,ar7100-eth", .data = &ag71xx_dcfg_ar7100 }, 2114 { .compatible = "qca,ar7240-eth", .data = &ag71xx_dcfg_ar7240 }, 2115 { .compatible = "qca,ar7241-eth", .data = &ag71xx_dcfg_ar7240 }, 2116 { .compatible = "qca,ar7242-eth", .data = &ag71xx_dcfg_ar7240 }, 2117 { .compatible = "qca,ar9130-eth", .data = &ag71xx_dcfg_ar9130 }, 2118 { .compatible = "qca,ar9330-eth", .data = &ag71xx_dcfg_ar9330 }, 2119 { .compatible = "qca,ar9340-eth", .data = &ag71xx_dcfg_ar9340 }, 2120 { .compatible = "qca,qca9530-eth", .data = &ag71xx_dcfg_qca9530 }, 2121 { .compatible = "qca,qca9550-eth", .data = &ag71xx_dcfg_qca9550 }, 2122 { .compatible = "qca,qca9560-eth", .data = &ag71xx_dcfg_qca9550 }, 2123 {} 2124 }; 2125 2126 static struct platform_driver ag71xx_driver = { 2127 .probe = ag71xx_probe, 2128 .remove = ag71xx_remove, 2129 .driver = { 2130 .name = "ag71xx", 2131 .of_match_table = ag71xx_match, 2132 } 2133 }; 2134 2135 module_platform_driver(ag71xx_driver); 2136 MODULE_LICENSE("GPL v2"); 2137