1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * drivers/net/ethernet/nxp/lpc_eth.c 4 * 5 * Author: Kevin Wells <kevin.wells@nxp.com> 6 * 7 * Copyright (C) 2010 NXP Semiconductors 8 * Copyright (C) 2012 Roland Stigge <stigge@antcom.de> 9 */ 10 11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 12 13 #include <linux/clk.h> 14 #include <linux/crc32.h> 15 #include <linux/etherdevice.h> 16 #include <linux/module.h> 17 #include <linux/of.h> 18 #include <linux/of_mdio.h> 19 #include <linux/of_net.h> 20 #include <linux/phy.h> 21 #include <linux/platform_device.h> 22 #include <linux/spinlock.h> 23 #include <linux/soc/nxp/lpc32xx-misc.h> 24 25 #define MODNAME "lpc-eth" 26 #define DRV_VERSION "1.00" 27 28 #define ENET_MAXF_SIZE 1536 29 #define ENET_RX_DESC 48 30 #define ENET_TX_DESC 16 31 32 #define NAPI_WEIGHT 16 33 34 /* 35 * Ethernet MAC controller Register offsets 36 */ 37 #define LPC_ENET_MAC1(x) (x + 0x000) 38 #define LPC_ENET_MAC2(x) (x + 0x004) 39 #define LPC_ENET_IPGT(x) (x + 0x008) 40 #define LPC_ENET_IPGR(x) (x + 0x00C) 41 #define LPC_ENET_CLRT(x) (x + 0x010) 42 #define LPC_ENET_MAXF(x) (x + 0x014) 43 #define LPC_ENET_SUPP(x) (x + 0x018) 44 #define LPC_ENET_TEST(x) (x + 0x01C) 45 #define LPC_ENET_MCFG(x) (x + 0x020) 46 #define LPC_ENET_MCMD(x) (x + 0x024) 47 #define LPC_ENET_MADR(x) (x + 0x028) 48 #define LPC_ENET_MWTD(x) (x + 0x02C) 49 #define LPC_ENET_MRDD(x) (x + 0x030) 50 #define LPC_ENET_MIND(x) (x + 0x034) 51 #define LPC_ENET_SA0(x) (x + 0x040) 52 #define LPC_ENET_SA1(x) (x + 0x044) 53 #define LPC_ENET_SA2(x) (x + 0x048) 54 #define LPC_ENET_COMMAND(x) (x + 0x100) 55 #define LPC_ENET_STATUS(x) (x + 0x104) 56 #define LPC_ENET_RXDESCRIPTOR(x) (x + 0x108) 57 #define LPC_ENET_RXSTATUS(x) (x + 0x10C) 58 #define LPC_ENET_RXDESCRIPTORNUMBER(x) (x + 0x110) 59 #define LPC_ENET_RXPRODUCEINDEX(x) (x + 0x114) 60 #define LPC_ENET_RXCONSUMEINDEX(x) (x + 0x118) 61 #define LPC_ENET_TXDESCRIPTOR(x) (x + 0x11C) 62 #define LPC_ENET_TXSTATUS(x) (x + 0x120) 63 #define LPC_ENET_TXDESCRIPTORNUMBER(x) (x + 0x124) 64 #define LPC_ENET_TXPRODUCEINDEX(x) (x + 0x128) 65 #define LPC_ENET_TXCONSUMEINDEX(x) (x + 0x12C) 66 #define LPC_ENET_TSV0(x) (x + 0x158) 67 #define LPC_ENET_TSV1(x) (x + 0x15C) 68 #define LPC_ENET_RSV(x) (x + 0x160) 69 #define LPC_ENET_FLOWCONTROLCOUNTER(x) (x + 0x170) 70 #define LPC_ENET_FLOWCONTROLSTATUS(x) (x + 0x174) 71 #define LPC_ENET_RXFILTER_CTRL(x) (x + 0x200) 72 #define LPC_ENET_RXFILTERWOLSTATUS(x) (x + 0x204) 73 #define LPC_ENET_RXFILTERWOLCLEAR(x) (x + 0x208) 74 #define LPC_ENET_HASHFILTERL(x) (x + 0x210) 75 #define LPC_ENET_HASHFILTERH(x) (x + 0x214) 76 #define LPC_ENET_INTSTATUS(x) (x + 0xFE0) 77 #define LPC_ENET_INTENABLE(x) (x + 0xFE4) 78 #define LPC_ENET_INTCLEAR(x) (x + 0xFE8) 79 #define LPC_ENET_INTSET(x) (x + 0xFEC) 80 #define LPC_ENET_POWERDOWN(x) (x + 0xFF4) 81 82 /* 83 * mac1 register definitions 84 */ 85 #define LPC_MAC1_RECV_ENABLE (1 << 0) 86 #define LPC_MAC1_PASS_ALL_RX_FRAMES (1 << 1) 87 #define LPC_MAC1_RX_FLOW_CONTROL (1 << 2) 88 #define LPC_MAC1_TX_FLOW_CONTROL (1 << 3) 89 #define LPC_MAC1_LOOPBACK (1 << 4) 90 #define LPC_MAC1_RESET_TX (1 << 8) 91 #define LPC_MAC1_RESET_MCS_TX (1 << 9) 92 #define LPC_MAC1_RESET_RX (1 << 10) 93 #define LPC_MAC1_RESET_MCS_RX (1 << 11) 94 #define LPC_MAC1_SIMULATION_RESET (1 << 14) 95 #define LPC_MAC1_SOFT_RESET (1 << 15) 96 97 /* 98 * mac2 register definitions 99 */ 100 #define LPC_MAC2_FULL_DUPLEX (1 << 0) 101 #define LPC_MAC2_FRAME_LENGTH_CHECKING (1 << 1) 102 #define LPC_MAC2_HUGH_LENGTH_CHECKING (1 << 2) 103 #define LPC_MAC2_DELAYED_CRC (1 << 3) 104 #define LPC_MAC2_CRC_ENABLE (1 << 4) 105 #define LPC_MAC2_PAD_CRC_ENABLE (1 << 5) 106 #define LPC_MAC2_VLAN_PAD_ENABLE (1 << 6) 107 #define LPC_MAC2_AUTO_DETECT_PAD_ENABLE (1 << 7) 108 #define LPC_MAC2_PURE_PREAMBLE_ENFORCEMENT (1 << 8) 109 #define LPC_MAC2_LONG_PREAMBLE_ENFORCEMENT (1 << 9) 110 #define LPC_MAC2_NO_BACKOFF (1 << 12) 111 #define LPC_MAC2_BACK_PRESSURE (1 << 13) 112 #define LPC_MAC2_EXCESS_DEFER (1 << 14) 113 114 /* 115 * ipgt register definitions 116 */ 117 #define LPC_IPGT_LOAD(n) ((n) & 0x7F) 118 119 /* 120 * ipgr register definitions 121 */ 122 #define LPC_IPGR_LOAD_PART2(n) ((n) & 0x7F) 123 #define LPC_IPGR_LOAD_PART1(n) (((n) & 0x7F) << 8) 124 125 /* 126 * clrt register definitions 127 */ 128 #define LPC_CLRT_LOAD_RETRY_MAX(n) ((n) & 0xF) 129 #define LPC_CLRT_LOAD_COLLISION_WINDOW(n) (((n) & 0x3F) << 8) 130 131 /* 132 * maxf register definitions 133 */ 134 #define LPC_MAXF_LOAD_MAX_FRAME_LEN(n) ((n) & 0xFFFF) 135 136 /* 137 * supp register definitions 138 */ 139 #define LPC_SUPP_SPEED (1 << 8) 140 #define LPC_SUPP_RESET_RMII (1 << 11) 141 142 /* 143 * test register definitions 144 */ 145 #define LPC_TEST_SHORTCUT_PAUSE_QUANTA (1 << 0) 146 #define LPC_TEST_PAUSE (1 << 1) 147 #define LPC_TEST_BACKPRESSURE (1 << 2) 148 149 /* 150 * mcfg register definitions 151 */ 152 #define LPC_MCFG_SCAN_INCREMENT (1 << 0) 153 #define LPC_MCFG_SUPPRESS_PREAMBLE (1 << 1) 154 #define LPC_MCFG_CLOCK_SELECT(n) (((n) & 0x7) << 2) 155 #define LPC_MCFG_CLOCK_HOST_DIV_4 0 156 #define LPC_MCFG_CLOCK_HOST_DIV_6 2 157 #define LPC_MCFG_CLOCK_HOST_DIV_8 3 158 #define LPC_MCFG_CLOCK_HOST_DIV_10 4 159 #define LPC_MCFG_CLOCK_HOST_DIV_14 5 160 #define LPC_MCFG_CLOCK_HOST_DIV_20 6 161 #define LPC_MCFG_CLOCK_HOST_DIV_28 7 162 #define LPC_MCFG_RESET_MII_MGMT (1 << 15) 163 164 /* 165 * mcmd register definitions 166 */ 167 #define LPC_MCMD_READ (1 << 0) 168 #define LPC_MCMD_SCAN (1 << 1) 169 170 /* 171 * madr register definitions 172 */ 173 #define LPC_MADR_REGISTER_ADDRESS(n) ((n) & 0x1F) 174 #define LPC_MADR_PHY_0ADDRESS(n) (((n) & 0x1F) << 8) 175 176 /* 177 * mwtd register definitions 178 */ 179 #define LPC_MWDT_WRITE(n) ((n) & 0xFFFF) 180 181 /* 182 * mrdd register definitions 183 */ 184 #define LPC_MRDD_READ_MASK 0xFFFF 185 186 /* 187 * mind register definitions 188 */ 189 #define LPC_MIND_BUSY (1 << 0) 190 #define LPC_MIND_SCANNING (1 << 1) 191 #define LPC_MIND_NOT_VALID (1 << 2) 192 #define LPC_MIND_MII_LINK_FAIL (1 << 3) 193 194 /* 195 * command register definitions 196 */ 197 #define LPC_COMMAND_RXENABLE (1 << 0) 198 #define LPC_COMMAND_TXENABLE (1 << 1) 199 #define LPC_COMMAND_REG_RESET (1 << 3) 200 #define LPC_COMMAND_TXRESET (1 << 4) 201 #define LPC_COMMAND_RXRESET (1 << 5) 202 #define LPC_COMMAND_PASSRUNTFRAME (1 << 6) 203 #define LPC_COMMAND_PASSRXFILTER (1 << 7) 204 #define LPC_COMMAND_TXFLOWCONTROL (1 << 8) 205 #define LPC_COMMAND_RMII (1 << 9) 206 #define LPC_COMMAND_FULLDUPLEX (1 << 10) 207 208 /* 209 * status register definitions 210 */ 211 #define LPC_STATUS_RXACTIVE (1 << 0) 212 #define LPC_STATUS_TXACTIVE (1 << 1) 213 214 /* 215 * tsv0 register definitions 216 */ 217 #define LPC_TSV0_CRC_ERROR (1 << 0) 218 #define LPC_TSV0_LENGTH_CHECK_ERROR (1 << 1) 219 #define LPC_TSV0_LENGTH_OUT_OF_RANGE (1 << 2) 220 #define LPC_TSV0_DONE (1 << 3) 221 #define LPC_TSV0_MULTICAST (1 << 4) 222 #define LPC_TSV0_BROADCAST (1 << 5) 223 #define LPC_TSV0_PACKET_DEFER (1 << 6) 224 #define LPC_TSV0_ESCESSIVE_DEFER (1 << 7) 225 #define LPC_TSV0_ESCESSIVE_COLLISION (1 << 8) 226 #define LPC_TSV0_LATE_COLLISION (1 << 9) 227 #define LPC_TSV0_GIANT (1 << 10) 228 #define LPC_TSV0_UNDERRUN (1 << 11) 229 #define LPC_TSV0_TOTAL_BYTES(n) (((n) >> 12) & 0xFFFF) 230 #define LPC_TSV0_CONTROL_FRAME (1 << 28) 231 #define LPC_TSV0_PAUSE (1 << 29) 232 #define LPC_TSV0_BACKPRESSURE (1 << 30) 233 #define LPC_TSV0_VLAN (1 << 31) 234 235 /* 236 * tsv1 register definitions 237 */ 238 #define LPC_TSV1_TRANSMIT_BYTE_COUNT(n) ((n) & 0xFFFF) 239 #define LPC_TSV1_COLLISION_COUNT(n) (((n) >> 16) & 0xF) 240 241 /* 242 * rsv register definitions 243 */ 244 #define LPC_RSV_RECEIVED_BYTE_COUNT(n) ((n) & 0xFFFF) 245 #define LPC_RSV_RXDV_EVENT_IGNORED (1 << 16) 246 #define LPC_RSV_RXDV_EVENT_PREVIOUSLY_SEEN (1 << 17) 247 #define LPC_RSV_CARRIER_EVNT_PREVIOUS_SEEN (1 << 18) 248 #define LPC_RSV_RECEIVE_CODE_VIOLATION (1 << 19) 249 #define LPC_RSV_CRC_ERROR (1 << 20) 250 #define LPC_RSV_LENGTH_CHECK_ERROR (1 << 21) 251 #define LPC_RSV_LENGTH_OUT_OF_RANGE (1 << 22) 252 #define LPC_RSV_RECEIVE_OK (1 << 23) 253 #define LPC_RSV_MULTICAST (1 << 24) 254 #define LPC_RSV_BROADCAST (1 << 25) 255 #define LPC_RSV_DRIBBLE_NIBBLE (1 << 26) 256 #define LPC_RSV_CONTROL_FRAME (1 << 27) 257 #define LPC_RSV_PAUSE (1 << 28) 258 #define LPC_RSV_UNSUPPORTED_OPCODE (1 << 29) 259 #define LPC_RSV_VLAN (1 << 30) 260 261 /* 262 * flowcontrolcounter register definitions 263 */ 264 #define LPC_FCCR_MIRRORCOUNTER(n) ((n) & 0xFFFF) 265 #define LPC_FCCR_PAUSETIMER(n) (((n) >> 16) & 0xFFFF) 266 267 /* 268 * flowcontrolstatus register definitions 269 */ 270 #define LPC_FCCR_MIRRORCOUNTERCURRENT(n) ((n) & 0xFFFF) 271 272 /* 273 * rxfilterctrl, rxfilterwolstatus, and rxfilterwolclear shared 274 * register definitions 275 */ 276 #define LPC_RXFLTRW_ACCEPTUNICAST (1 << 0) 277 #define LPC_RXFLTRW_ACCEPTUBROADCAST (1 << 1) 278 #define LPC_RXFLTRW_ACCEPTUMULTICAST (1 << 2) 279 #define LPC_RXFLTRW_ACCEPTUNICASTHASH (1 << 3) 280 #define LPC_RXFLTRW_ACCEPTUMULTICASTHASH (1 << 4) 281 #define LPC_RXFLTRW_ACCEPTPERFECT (1 << 5) 282 283 /* 284 * rxfilterctrl register definitions 285 */ 286 #define LPC_RXFLTRWSTS_MAGICPACKETENWOL (1 << 12) 287 #define LPC_RXFLTRWSTS_RXFILTERENWOL (1 << 13) 288 289 /* 290 * rxfilterwolstatus/rxfilterwolclear register definitions 291 */ 292 #define LPC_RXFLTRWSTS_RXFILTERWOL (1 << 7) 293 #define LPC_RXFLTRWSTS_MAGICPACKETWOL (1 << 8) 294 295 /* 296 * intstatus, intenable, intclear, and Intset shared register 297 * definitions 298 */ 299 #define LPC_MACINT_RXOVERRUNINTEN (1 << 0) 300 #define LPC_MACINT_RXERRORONINT (1 << 1) 301 #define LPC_MACINT_RXFINISHEDINTEN (1 << 2) 302 #define LPC_MACINT_RXDONEINTEN (1 << 3) 303 #define LPC_MACINT_TXUNDERRUNINTEN (1 << 4) 304 #define LPC_MACINT_TXERRORINTEN (1 << 5) 305 #define LPC_MACINT_TXFINISHEDINTEN (1 << 6) 306 #define LPC_MACINT_TXDONEINTEN (1 << 7) 307 #define LPC_MACINT_SOFTINTEN (1 << 12) 308 #define LPC_MACINT_WAKEUPINTEN (1 << 13) 309 310 /* 311 * powerdown register definitions 312 */ 313 #define LPC_POWERDOWN_MACAHB (1 << 31) 314 315 static phy_interface_t lpc_phy_interface_mode(struct device *dev) 316 { 317 if (dev && dev->of_node) { 318 const char *mode = of_get_property(dev->of_node, 319 "phy-mode", NULL); 320 if (mode && !strcmp(mode, "mii")) 321 return PHY_INTERFACE_MODE_MII; 322 } 323 return PHY_INTERFACE_MODE_RMII; 324 } 325 326 static bool use_iram_for_net(struct device *dev) 327 { 328 if (dev && dev->of_node) 329 return of_property_read_bool(dev->of_node, "use-iram"); 330 return false; 331 } 332 333 /* Receive Status information word */ 334 #define RXSTATUS_SIZE 0x000007FF 335 #define RXSTATUS_CONTROL (1 << 18) 336 #define RXSTATUS_VLAN (1 << 19) 337 #define RXSTATUS_FILTER (1 << 20) 338 #define RXSTATUS_MULTICAST (1 << 21) 339 #define RXSTATUS_BROADCAST (1 << 22) 340 #define RXSTATUS_CRC (1 << 23) 341 #define RXSTATUS_SYMBOL (1 << 24) 342 #define RXSTATUS_LENGTH (1 << 25) 343 #define RXSTATUS_RANGE (1 << 26) 344 #define RXSTATUS_ALIGN (1 << 27) 345 #define RXSTATUS_OVERRUN (1 << 28) 346 #define RXSTATUS_NODESC (1 << 29) 347 #define RXSTATUS_LAST (1 << 30) 348 #define RXSTATUS_ERROR (1 << 31) 349 350 #define RXSTATUS_STATUS_ERROR \ 351 (RXSTATUS_NODESC | RXSTATUS_OVERRUN | RXSTATUS_ALIGN | \ 352 RXSTATUS_RANGE | RXSTATUS_LENGTH | RXSTATUS_SYMBOL | RXSTATUS_CRC) 353 354 /* Receive Descriptor control word */ 355 #define RXDESC_CONTROL_SIZE 0x000007FF 356 #define RXDESC_CONTROL_INT (1 << 31) 357 358 /* Transmit Status information word */ 359 #define TXSTATUS_COLLISIONS_GET(x) (((x) >> 21) & 0xF) 360 #define TXSTATUS_DEFER (1 << 25) 361 #define TXSTATUS_EXCESSDEFER (1 << 26) 362 #define TXSTATUS_EXCESSCOLL (1 << 27) 363 #define TXSTATUS_LATECOLL (1 << 28) 364 #define TXSTATUS_UNDERRUN (1 << 29) 365 #define TXSTATUS_NODESC (1 << 30) 366 #define TXSTATUS_ERROR (1 << 31) 367 368 /* Transmit Descriptor control word */ 369 #define TXDESC_CONTROL_SIZE 0x000007FF 370 #define TXDESC_CONTROL_OVERRIDE (1 << 26) 371 #define TXDESC_CONTROL_HUGE (1 << 27) 372 #define TXDESC_CONTROL_PAD (1 << 28) 373 #define TXDESC_CONTROL_CRC (1 << 29) 374 #define TXDESC_CONTROL_LAST (1 << 30) 375 #define TXDESC_CONTROL_INT (1 << 31) 376 377 /* 378 * Structure of a TX/RX descriptors and RX status 379 */ 380 struct txrx_desc_t { 381 __le32 packet; 382 __le32 control; 383 }; 384 struct rx_status_t { 385 __le32 statusinfo; 386 __le32 statushashcrc; 387 }; 388 389 /* 390 * Device driver data structure 391 */ 392 struct netdata_local { 393 struct platform_device *pdev; 394 struct net_device *ndev; 395 struct device_node *phy_node; 396 spinlock_t lock; 397 void __iomem *net_base; 398 u32 msg_enable; 399 unsigned int skblen[ENET_TX_DESC]; 400 unsigned int last_tx_idx; 401 unsigned int num_used_tx_buffs; 402 struct mii_bus *mii_bus; 403 struct clk *clk; 404 dma_addr_t dma_buff_base_p; 405 void *dma_buff_base_v; 406 size_t dma_buff_size; 407 struct txrx_desc_t *tx_desc_v; 408 u32 *tx_stat_v; 409 void *tx_buff_v; 410 struct txrx_desc_t *rx_desc_v; 411 struct rx_status_t *rx_stat_v; 412 void *rx_buff_v; 413 int link; 414 int speed; 415 int duplex; 416 struct napi_struct napi; 417 }; 418 419 /* 420 * MAC support functions 421 */ 422 static void __lpc_set_mac(struct netdata_local *pldat, u8 *mac) 423 { 424 u32 tmp; 425 426 /* Set station address */ 427 tmp = mac[0] | ((u32)mac[1] << 8); 428 writel(tmp, LPC_ENET_SA2(pldat->net_base)); 429 tmp = mac[2] | ((u32)mac[3] << 8); 430 writel(tmp, LPC_ENET_SA1(pldat->net_base)); 431 tmp = mac[4] | ((u32)mac[5] << 8); 432 writel(tmp, LPC_ENET_SA0(pldat->net_base)); 433 434 netdev_dbg(pldat->ndev, "Ethernet MAC address %pM\n", mac); 435 } 436 437 static void __lpc_get_mac(struct netdata_local *pldat, u8 *mac) 438 { 439 u32 tmp; 440 441 /* Get station address */ 442 tmp = readl(LPC_ENET_SA2(pldat->net_base)); 443 mac[0] = tmp & 0xFF; 444 mac[1] = tmp >> 8; 445 tmp = readl(LPC_ENET_SA1(pldat->net_base)); 446 mac[2] = tmp & 0xFF; 447 mac[3] = tmp >> 8; 448 tmp = readl(LPC_ENET_SA0(pldat->net_base)); 449 mac[4] = tmp & 0xFF; 450 mac[5] = tmp >> 8; 451 } 452 453 static void __lpc_params_setup(struct netdata_local *pldat) 454 { 455 u32 tmp; 456 457 if (pldat->duplex == DUPLEX_FULL) { 458 tmp = readl(LPC_ENET_MAC2(pldat->net_base)); 459 tmp |= LPC_MAC2_FULL_DUPLEX; 460 writel(tmp, LPC_ENET_MAC2(pldat->net_base)); 461 tmp = readl(LPC_ENET_COMMAND(pldat->net_base)); 462 tmp |= LPC_COMMAND_FULLDUPLEX; 463 writel(tmp, LPC_ENET_COMMAND(pldat->net_base)); 464 writel(LPC_IPGT_LOAD(0x15), LPC_ENET_IPGT(pldat->net_base)); 465 } else { 466 tmp = readl(LPC_ENET_MAC2(pldat->net_base)); 467 tmp &= ~LPC_MAC2_FULL_DUPLEX; 468 writel(tmp, LPC_ENET_MAC2(pldat->net_base)); 469 tmp = readl(LPC_ENET_COMMAND(pldat->net_base)); 470 tmp &= ~LPC_COMMAND_FULLDUPLEX; 471 writel(tmp, LPC_ENET_COMMAND(pldat->net_base)); 472 writel(LPC_IPGT_LOAD(0x12), LPC_ENET_IPGT(pldat->net_base)); 473 } 474 475 if (pldat->speed == SPEED_100) 476 writel(LPC_SUPP_SPEED, LPC_ENET_SUPP(pldat->net_base)); 477 else 478 writel(0, LPC_ENET_SUPP(pldat->net_base)); 479 } 480 481 static void __lpc_eth_reset(struct netdata_local *pldat) 482 { 483 /* Reset all MAC logic */ 484 writel((LPC_MAC1_RESET_TX | LPC_MAC1_RESET_MCS_TX | LPC_MAC1_RESET_RX | 485 LPC_MAC1_RESET_MCS_RX | LPC_MAC1_SIMULATION_RESET | 486 LPC_MAC1_SOFT_RESET), LPC_ENET_MAC1(pldat->net_base)); 487 writel((LPC_COMMAND_REG_RESET | LPC_COMMAND_TXRESET | 488 LPC_COMMAND_RXRESET), LPC_ENET_COMMAND(pldat->net_base)); 489 } 490 491 static int __lpc_mii_mngt_reset(struct netdata_local *pldat) 492 { 493 /* Reset MII management hardware */ 494 writel(LPC_MCFG_RESET_MII_MGMT, LPC_ENET_MCFG(pldat->net_base)); 495 496 /* Setup MII clock to slowest rate with a /28 divider */ 497 writel(LPC_MCFG_CLOCK_SELECT(LPC_MCFG_CLOCK_HOST_DIV_28), 498 LPC_ENET_MCFG(pldat->net_base)); 499 500 return 0; 501 } 502 503 static inline phys_addr_t __va_to_pa(void *addr, struct netdata_local *pldat) 504 { 505 phys_addr_t phaddr; 506 507 phaddr = addr - pldat->dma_buff_base_v; 508 phaddr += pldat->dma_buff_base_p; 509 510 return phaddr; 511 } 512 513 static void lpc_eth_enable_int(void __iomem *regbase) 514 { 515 writel((LPC_MACINT_RXDONEINTEN | LPC_MACINT_TXDONEINTEN), 516 LPC_ENET_INTENABLE(regbase)); 517 } 518 519 static void lpc_eth_disable_int(void __iomem *regbase) 520 { 521 writel(0, LPC_ENET_INTENABLE(regbase)); 522 } 523 524 /* Setup TX/RX descriptors */ 525 static void __lpc_txrx_desc_setup(struct netdata_local *pldat) 526 { 527 u32 *ptxstat; 528 void *tbuff; 529 int i; 530 struct txrx_desc_t *ptxrxdesc; 531 struct rx_status_t *prxstat; 532 533 tbuff = PTR_ALIGN(pldat->dma_buff_base_v, 16); 534 535 /* Setup TX descriptors, status, and buffers */ 536 pldat->tx_desc_v = tbuff; 537 tbuff += sizeof(struct txrx_desc_t) * ENET_TX_DESC; 538 539 pldat->tx_stat_v = tbuff; 540 tbuff += sizeof(u32) * ENET_TX_DESC; 541 542 tbuff = PTR_ALIGN(tbuff, 16); 543 pldat->tx_buff_v = tbuff; 544 tbuff += ENET_MAXF_SIZE * ENET_TX_DESC; 545 546 /* Setup RX descriptors, status, and buffers */ 547 pldat->rx_desc_v = tbuff; 548 tbuff += sizeof(struct txrx_desc_t) * ENET_RX_DESC; 549 550 tbuff = PTR_ALIGN(tbuff, 16); 551 pldat->rx_stat_v = tbuff; 552 tbuff += sizeof(struct rx_status_t) * ENET_RX_DESC; 553 554 tbuff = PTR_ALIGN(tbuff, 16); 555 pldat->rx_buff_v = tbuff; 556 tbuff += ENET_MAXF_SIZE * ENET_RX_DESC; 557 558 /* Map the TX descriptors to the TX buffers in hardware */ 559 for (i = 0; i < ENET_TX_DESC; i++) { 560 ptxstat = &pldat->tx_stat_v[i]; 561 ptxrxdesc = &pldat->tx_desc_v[i]; 562 563 ptxrxdesc->packet = __va_to_pa( 564 pldat->tx_buff_v + i * ENET_MAXF_SIZE, pldat); 565 ptxrxdesc->control = 0; 566 *ptxstat = 0; 567 } 568 569 /* Map the RX descriptors to the RX buffers in hardware */ 570 for (i = 0; i < ENET_RX_DESC; i++) { 571 prxstat = &pldat->rx_stat_v[i]; 572 ptxrxdesc = &pldat->rx_desc_v[i]; 573 574 ptxrxdesc->packet = __va_to_pa( 575 pldat->rx_buff_v + i * ENET_MAXF_SIZE, pldat); 576 ptxrxdesc->control = RXDESC_CONTROL_INT | (ENET_MAXF_SIZE - 1); 577 prxstat->statusinfo = 0; 578 prxstat->statushashcrc = 0; 579 } 580 581 /* Setup base addresses in hardware to point to buffers and 582 * descriptors 583 */ 584 writel((ENET_TX_DESC - 1), 585 LPC_ENET_TXDESCRIPTORNUMBER(pldat->net_base)); 586 writel(__va_to_pa(pldat->tx_desc_v, pldat), 587 LPC_ENET_TXDESCRIPTOR(pldat->net_base)); 588 writel(__va_to_pa(pldat->tx_stat_v, pldat), 589 LPC_ENET_TXSTATUS(pldat->net_base)); 590 writel((ENET_RX_DESC - 1), 591 LPC_ENET_RXDESCRIPTORNUMBER(pldat->net_base)); 592 writel(__va_to_pa(pldat->rx_desc_v, pldat), 593 LPC_ENET_RXDESCRIPTOR(pldat->net_base)); 594 writel(__va_to_pa(pldat->rx_stat_v, pldat), 595 LPC_ENET_RXSTATUS(pldat->net_base)); 596 } 597 598 static void __lpc_eth_init(struct netdata_local *pldat) 599 { 600 u32 tmp; 601 602 /* Disable controller and reset */ 603 tmp = readl(LPC_ENET_COMMAND(pldat->net_base)); 604 tmp &= ~LPC_COMMAND_RXENABLE | LPC_COMMAND_TXENABLE; 605 writel(tmp, LPC_ENET_COMMAND(pldat->net_base)); 606 tmp = readl(LPC_ENET_MAC1(pldat->net_base)); 607 tmp &= ~LPC_MAC1_RECV_ENABLE; 608 writel(tmp, LPC_ENET_MAC1(pldat->net_base)); 609 610 /* Initial MAC setup */ 611 writel(LPC_MAC1_PASS_ALL_RX_FRAMES, LPC_ENET_MAC1(pldat->net_base)); 612 writel((LPC_MAC2_PAD_CRC_ENABLE | LPC_MAC2_CRC_ENABLE), 613 LPC_ENET_MAC2(pldat->net_base)); 614 writel(ENET_MAXF_SIZE, LPC_ENET_MAXF(pldat->net_base)); 615 616 /* Collision window, gap */ 617 writel((LPC_CLRT_LOAD_RETRY_MAX(0xF) | 618 LPC_CLRT_LOAD_COLLISION_WINDOW(0x37)), 619 LPC_ENET_CLRT(pldat->net_base)); 620 writel(LPC_IPGR_LOAD_PART2(0x12), LPC_ENET_IPGR(pldat->net_base)); 621 622 if (lpc_phy_interface_mode(&pldat->pdev->dev) == PHY_INTERFACE_MODE_MII) 623 writel(LPC_COMMAND_PASSRUNTFRAME, 624 LPC_ENET_COMMAND(pldat->net_base)); 625 else { 626 writel((LPC_COMMAND_PASSRUNTFRAME | LPC_COMMAND_RMII), 627 LPC_ENET_COMMAND(pldat->net_base)); 628 writel(LPC_SUPP_RESET_RMII, LPC_ENET_SUPP(pldat->net_base)); 629 } 630 631 __lpc_params_setup(pldat); 632 633 /* Setup TX and RX descriptors */ 634 __lpc_txrx_desc_setup(pldat); 635 636 /* Setup packet filtering */ 637 writel((LPC_RXFLTRW_ACCEPTUBROADCAST | LPC_RXFLTRW_ACCEPTPERFECT), 638 LPC_ENET_RXFILTER_CTRL(pldat->net_base)); 639 640 /* Get the next TX buffer output index */ 641 pldat->num_used_tx_buffs = 0; 642 pldat->last_tx_idx = 643 readl(LPC_ENET_TXCONSUMEINDEX(pldat->net_base)); 644 645 /* Clear and enable interrupts */ 646 writel(0xFFFF, LPC_ENET_INTCLEAR(pldat->net_base)); 647 smp_wmb(); 648 lpc_eth_enable_int(pldat->net_base); 649 650 /* Enable controller */ 651 tmp = readl(LPC_ENET_COMMAND(pldat->net_base)); 652 tmp |= LPC_COMMAND_RXENABLE | LPC_COMMAND_TXENABLE; 653 writel(tmp, LPC_ENET_COMMAND(pldat->net_base)); 654 tmp = readl(LPC_ENET_MAC1(pldat->net_base)); 655 tmp |= LPC_MAC1_RECV_ENABLE; 656 writel(tmp, LPC_ENET_MAC1(pldat->net_base)); 657 } 658 659 static void __lpc_eth_shutdown(struct netdata_local *pldat) 660 { 661 /* Reset ethernet and power down PHY */ 662 __lpc_eth_reset(pldat); 663 writel(0, LPC_ENET_MAC1(pldat->net_base)); 664 writel(0, LPC_ENET_MAC2(pldat->net_base)); 665 } 666 667 /* 668 * MAC<--->PHY support functions 669 */ 670 static int lpc_mdio_read(struct mii_bus *bus, int phy_id, int phyreg) 671 { 672 struct netdata_local *pldat = bus->priv; 673 unsigned long timeout = jiffies + msecs_to_jiffies(100); 674 int lps; 675 676 writel(((phy_id << 8) | phyreg), LPC_ENET_MADR(pldat->net_base)); 677 writel(LPC_MCMD_READ, LPC_ENET_MCMD(pldat->net_base)); 678 679 /* Wait for unbusy status */ 680 while (readl(LPC_ENET_MIND(pldat->net_base)) & LPC_MIND_BUSY) { 681 if (time_after(jiffies, timeout)) 682 return -EIO; 683 cpu_relax(); 684 } 685 686 lps = readl(LPC_ENET_MRDD(pldat->net_base)); 687 writel(0, LPC_ENET_MCMD(pldat->net_base)); 688 689 return lps; 690 } 691 692 static int lpc_mdio_write(struct mii_bus *bus, int phy_id, int phyreg, 693 u16 phydata) 694 { 695 struct netdata_local *pldat = bus->priv; 696 unsigned long timeout = jiffies + msecs_to_jiffies(100); 697 698 writel(((phy_id << 8) | phyreg), LPC_ENET_MADR(pldat->net_base)); 699 writel(phydata, LPC_ENET_MWTD(pldat->net_base)); 700 701 /* Wait for completion */ 702 while (readl(LPC_ENET_MIND(pldat->net_base)) & LPC_MIND_BUSY) { 703 if (time_after(jiffies, timeout)) 704 return -EIO; 705 cpu_relax(); 706 } 707 708 return 0; 709 } 710 711 static int lpc_mdio_reset(struct mii_bus *bus) 712 { 713 return __lpc_mii_mngt_reset((struct netdata_local *)bus->priv); 714 } 715 716 static void lpc_handle_link_change(struct net_device *ndev) 717 { 718 struct netdata_local *pldat = netdev_priv(ndev); 719 struct phy_device *phydev = ndev->phydev; 720 unsigned long flags; 721 722 bool status_change = false; 723 724 spin_lock_irqsave(&pldat->lock, flags); 725 726 if (phydev->link) { 727 if ((pldat->speed != phydev->speed) || 728 (pldat->duplex != phydev->duplex)) { 729 pldat->speed = phydev->speed; 730 pldat->duplex = phydev->duplex; 731 status_change = true; 732 } 733 } 734 735 if (phydev->link != pldat->link) { 736 if (!phydev->link) { 737 pldat->speed = 0; 738 pldat->duplex = -1; 739 } 740 pldat->link = phydev->link; 741 742 status_change = true; 743 } 744 745 spin_unlock_irqrestore(&pldat->lock, flags); 746 747 if (status_change) 748 __lpc_params_setup(pldat); 749 } 750 751 static int lpc_mii_probe(struct net_device *ndev) 752 { 753 struct netdata_local *pldat = netdev_priv(ndev); 754 struct phy_device *phydev; 755 756 /* Attach to the PHY */ 757 if (lpc_phy_interface_mode(&pldat->pdev->dev) == PHY_INTERFACE_MODE_MII) 758 netdev_info(ndev, "using MII interface\n"); 759 else 760 netdev_info(ndev, "using RMII interface\n"); 761 762 if (pldat->phy_node) 763 phydev = of_phy_find_device(pldat->phy_node); 764 else 765 phydev = phy_find_first(pldat->mii_bus); 766 if (!phydev) { 767 netdev_err(ndev, "no PHY found\n"); 768 return -ENODEV; 769 } 770 771 phydev = phy_connect(ndev, phydev_name(phydev), 772 &lpc_handle_link_change, 773 lpc_phy_interface_mode(&pldat->pdev->dev)); 774 if (IS_ERR(phydev)) { 775 netdev_err(ndev, "Could not attach to PHY\n"); 776 return PTR_ERR(phydev); 777 } 778 779 phy_set_max_speed(phydev, SPEED_100); 780 781 pldat->link = 0; 782 pldat->speed = 0; 783 pldat->duplex = -1; 784 785 phy_attached_info(phydev); 786 787 return 0; 788 } 789 790 static int lpc_mii_init(struct netdata_local *pldat) 791 { 792 struct device_node *node; 793 int err = -ENXIO; 794 795 pldat->mii_bus = mdiobus_alloc(); 796 if (!pldat->mii_bus) { 797 err = -ENOMEM; 798 goto err_out; 799 } 800 801 /* Setup MII mode */ 802 if (lpc_phy_interface_mode(&pldat->pdev->dev) == PHY_INTERFACE_MODE_MII) 803 writel(LPC_COMMAND_PASSRUNTFRAME, 804 LPC_ENET_COMMAND(pldat->net_base)); 805 else { 806 writel((LPC_COMMAND_PASSRUNTFRAME | LPC_COMMAND_RMII), 807 LPC_ENET_COMMAND(pldat->net_base)); 808 writel(LPC_SUPP_RESET_RMII, LPC_ENET_SUPP(pldat->net_base)); 809 } 810 811 pldat->mii_bus->name = "lpc_mii_bus"; 812 pldat->mii_bus->read = &lpc_mdio_read; 813 pldat->mii_bus->write = &lpc_mdio_write; 814 pldat->mii_bus->reset = &lpc_mdio_reset; 815 snprintf(pldat->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x", 816 pldat->pdev->name, pldat->pdev->id); 817 pldat->mii_bus->priv = pldat; 818 pldat->mii_bus->parent = &pldat->pdev->dev; 819 820 node = of_get_child_by_name(pldat->pdev->dev.of_node, "mdio"); 821 err = of_mdiobus_register(pldat->mii_bus, node); 822 of_node_put(node); 823 if (err) 824 goto err_out_unregister_bus; 825 826 err = lpc_mii_probe(pldat->ndev); 827 if (err) 828 goto err_out_unregister_bus; 829 830 return 0; 831 832 err_out_unregister_bus: 833 mdiobus_unregister(pldat->mii_bus); 834 mdiobus_free(pldat->mii_bus); 835 err_out: 836 return err; 837 } 838 839 static void __lpc_handle_xmit(struct net_device *ndev) 840 { 841 struct netdata_local *pldat = netdev_priv(ndev); 842 u32 txcidx, *ptxstat, txstat; 843 844 txcidx = readl(LPC_ENET_TXCONSUMEINDEX(pldat->net_base)); 845 while (pldat->last_tx_idx != txcidx) { 846 unsigned int skblen = pldat->skblen[pldat->last_tx_idx]; 847 848 /* A buffer is available, get buffer status */ 849 ptxstat = &pldat->tx_stat_v[pldat->last_tx_idx]; 850 txstat = *ptxstat; 851 852 /* Next buffer and decrement used buffer counter */ 853 pldat->num_used_tx_buffs--; 854 pldat->last_tx_idx++; 855 if (pldat->last_tx_idx >= ENET_TX_DESC) 856 pldat->last_tx_idx = 0; 857 858 /* Update collision counter */ 859 ndev->stats.collisions += TXSTATUS_COLLISIONS_GET(txstat); 860 861 /* Any errors occurred? */ 862 if (txstat & TXSTATUS_ERROR) { 863 if (txstat & TXSTATUS_UNDERRUN) { 864 /* FIFO underrun */ 865 ndev->stats.tx_fifo_errors++; 866 } 867 if (txstat & TXSTATUS_LATECOLL) { 868 /* Late collision */ 869 ndev->stats.tx_aborted_errors++; 870 } 871 if (txstat & TXSTATUS_EXCESSCOLL) { 872 /* Excessive collision */ 873 ndev->stats.tx_aborted_errors++; 874 } 875 if (txstat & TXSTATUS_EXCESSDEFER) { 876 /* Defer limit */ 877 ndev->stats.tx_aborted_errors++; 878 } 879 ndev->stats.tx_errors++; 880 } else { 881 /* Update stats */ 882 ndev->stats.tx_packets++; 883 ndev->stats.tx_bytes += skblen; 884 } 885 886 txcidx = readl(LPC_ENET_TXCONSUMEINDEX(pldat->net_base)); 887 } 888 889 if (pldat->num_used_tx_buffs <= ENET_TX_DESC/2) { 890 if (netif_queue_stopped(ndev)) 891 netif_wake_queue(ndev); 892 } 893 } 894 895 static int __lpc_handle_recv(struct net_device *ndev, int budget) 896 { 897 struct netdata_local *pldat = netdev_priv(ndev); 898 struct sk_buff *skb; 899 u32 rxconsidx, len, ethst; 900 struct rx_status_t *prxstat; 901 int rx_done = 0; 902 903 /* Get the current RX buffer indexes */ 904 rxconsidx = readl(LPC_ENET_RXCONSUMEINDEX(pldat->net_base)); 905 while (rx_done < budget && rxconsidx != 906 readl(LPC_ENET_RXPRODUCEINDEX(pldat->net_base))) { 907 /* Get pointer to receive status */ 908 prxstat = &pldat->rx_stat_v[rxconsidx]; 909 len = (prxstat->statusinfo & RXSTATUS_SIZE) + 1; 910 911 /* Status error? */ 912 ethst = prxstat->statusinfo; 913 if ((ethst & (RXSTATUS_ERROR | RXSTATUS_STATUS_ERROR)) == 914 (RXSTATUS_ERROR | RXSTATUS_RANGE)) 915 ethst &= ~RXSTATUS_ERROR; 916 917 if (ethst & RXSTATUS_ERROR) { 918 int si = prxstat->statusinfo; 919 /* Check statuses */ 920 if (si & RXSTATUS_OVERRUN) { 921 /* Overrun error */ 922 ndev->stats.rx_fifo_errors++; 923 } else if (si & RXSTATUS_CRC) { 924 /* CRC error */ 925 ndev->stats.rx_crc_errors++; 926 } else if (si & RXSTATUS_LENGTH) { 927 /* Length error */ 928 ndev->stats.rx_length_errors++; 929 } else if (si & RXSTATUS_ERROR) { 930 /* Other error */ 931 ndev->stats.rx_length_errors++; 932 } 933 ndev->stats.rx_errors++; 934 } else { 935 /* Packet is good */ 936 skb = dev_alloc_skb(len); 937 if (!skb) { 938 ndev->stats.rx_dropped++; 939 } else { 940 /* Copy packet from buffer */ 941 skb_put_data(skb, 942 pldat->rx_buff_v + rxconsidx * ENET_MAXF_SIZE, 943 len); 944 945 /* Pass to upper layer */ 946 skb->protocol = eth_type_trans(skb, ndev); 947 netif_receive_skb(skb); 948 ndev->stats.rx_packets++; 949 ndev->stats.rx_bytes += len; 950 } 951 } 952 953 /* Increment consume index */ 954 rxconsidx = rxconsidx + 1; 955 if (rxconsidx >= ENET_RX_DESC) 956 rxconsidx = 0; 957 writel(rxconsidx, 958 LPC_ENET_RXCONSUMEINDEX(pldat->net_base)); 959 rx_done++; 960 } 961 962 return rx_done; 963 } 964 965 static int lpc_eth_poll(struct napi_struct *napi, int budget) 966 { 967 struct netdata_local *pldat = container_of(napi, 968 struct netdata_local, napi); 969 struct net_device *ndev = pldat->ndev; 970 int rx_done = 0; 971 struct netdev_queue *txq = netdev_get_tx_queue(ndev, 0); 972 973 __netif_tx_lock(txq, smp_processor_id()); 974 __lpc_handle_xmit(ndev); 975 __netif_tx_unlock(txq); 976 rx_done = __lpc_handle_recv(ndev, budget); 977 978 if (rx_done < budget) { 979 napi_complete_done(napi, rx_done); 980 lpc_eth_enable_int(pldat->net_base); 981 } 982 983 return rx_done; 984 } 985 986 static irqreturn_t __lpc_eth_interrupt(int irq, void *dev_id) 987 { 988 struct net_device *ndev = dev_id; 989 struct netdata_local *pldat = netdev_priv(ndev); 990 u32 tmp; 991 992 spin_lock(&pldat->lock); 993 994 tmp = readl(LPC_ENET_INTSTATUS(pldat->net_base)); 995 /* Clear interrupts */ 996 writel(tmp, LPC_ENET_INTCLEAR(pldat->net_base)); 997 998 lpc_eth_disable_int(pldat->net_base); 999 if (likely(napi_schedule_prep(&pldat->napi))) 1000 __napi_schedule(&pldat->napi); 1001 1002 spin_unlock(&pldat->lock); 1003 1004 return IRQ_HANDLED; 1005 } 1006 1007 static int lpc_eth_close(struct net_device *ndev) 1008 { 1009 unsigned long flags; 1010 struct netdata_local *pldat = netdev_priv(ndev); 1011 1012 if (netif_msg_ifdown(pldat)) 1013 dev_dbg(&pldat->pdev->dev, "shutting down %s\n", ndev->name); 1014 1015 napi_disable(&pldat->napi); 1016 netif_stop_queue(ndev); 1017 1018 if (ndev->phydev) 1019 phy_stop(ndev->phydev); 1020 1021 spin_lock_irqsave(&pldat->lock, flags); 1022 __lpc_eth_reset(pldat); 1023 netif_carrier_off(ndev); 1024 writel(0, LPC_ENET_MAC1(pldat->net_base)); 1025 writel(0, LPC_ENET_MAC2(pldat->net_base)); 1026 spin_unlock_irqrestore(&pldat->lock, flags); 1027 1028 clk_disable_unprepare(pldat->clk); 1029 1030 return 0; 1031 } 1032 1033 static netdev_tx_t lpc_eth_hard_start_xmit(struct sk_buff *skb, 1034 struct net_device *ndev) 1035 { 1036 struct netdata_local *pldat = netdev_priv(ndev); 1037 u32 len, txidx; 1038 u32 *ptxstat; 1039 struct txrx_desc_t *ptxrxdesc; 1040 1041 len = skb->len; 1042 1043 spin_lock_irq(&pldat->lock); 1044 1045 if (pldat->num_used_tx_buffs >= (ENET_TX_DESC - 1)) { 1046 /* This function should never be called when there are no 1047 * buffers 1048 */ 1049 netif_stop_queue(ndev); 1050 spin_unlock_irq(&pldat->lock); 1051 WARN(1, "BUG! TX request when no free TX buffers!\n"); 1052 return NETDEV_TX_BUSY; 1053 } 1054 1055 /* Get the next TX descriptor index */ 1056 txidx = readl(LPC_ENET_TXPRODUCEINDEX(pldat->net_base)); 1057 1058 /* Setup control for the transfer */ 1059 ptxstat = &pldat->tx_stat_v[txidx]; 1060 *ptxstat = 0; 1061 ptxrxdesc = &pldat->tx_desc_v[txidx]; 1062 ptxrxdesc->control = 1063 (len - 1) | TXDESC_CONTROL_LAST | TXDESC_CONTROL_INT; 1064 1065 /* Copy data to the DMA buffer */ 1066 memcpy(pldat->tx_buff_v + txidx * ENET_MAXF_SIZE, skb->data, len); 1067 1068 /* Save the buffer and increment the buffer counter */ 1069 pldat->skblen[txidx] = len; 1070 pldat->num_used_tx_buffs++; 1071 1072 /* Start transmit */ 1073 txidx++; 1074 if (txidx >= ENET_TX_DESC) 1075 txidx = 0; 1076 writel(txidx, LPC_ENET_TXPRODUCEINDEX(pldat->net_base)); 1077 1078 /* Stop queue if no more TX buffers */ 1079 if (pldat->num_used_tx_buffs >= (ENET_TX_DESC - 1)) 1080 netif_stop_queue(ndev); 1081 1082 spin_unlock_irq(&pldat->lock); 1083 1084 dev_kfree_skb(skb); 1085 return NETDEV_TX_OK; 1086 } 1087 1088 static int lpc_set_mac_address(struct net_device *ndev, void *p) 1089 { 1090 struct sockaddr *addr = p; 1091 struct netdata_local *pldat = netdev_priv(ndev); 1092 unsigned long flags; 1093 1094 if (!is_valid_ether_addr(addr->sa_data)) 1095 return -EADDRNOTAVAIL; 1096 memcpy(ndev->dev_addr, addr->sa_data, ETH_ALEN); 1097 1098 spin_lock_irqsave(&pldat->lock, flags); 1099 1100 /* Set station address */ 1101 __lpc_set_mac(pldat, ndev->dev_addr); 1102 1103 spin_unlock_irqrestore(&pldat->lock, flags); 1104 1105 return 0; 1106 } 1107 1108 static void lpc_eth_set_multicast_list(struct net_device *ndev) 1109 { 1110 struct netdata_local *pldat = netdev_priv(ndev); 1111 struct netdev_hw_addr_list *mcptr = &ndev->mc; 1112 struct netdev_hw_addr *ha; 1113 u32 tmp32, hash_val, hashlo, hashhi; 1114 unsigned long flags; 1115 1116 spin_lock_irqsave(&pldat->lock, flags); 1117 1118 /* Set station address */ 1119 __lpc_set_mac(pldat, ndev->dev_addr); 1120 1121 tmp32 = LPC_RXFLTRW_ACCEPTUBROADCAST | LPC_RXFLTRW_ACCEPTPERFECT; 1122 1123 if (ndev->flags & IFF_PROMISC) 1124 tmp32 |= LPC_RXFLTRW_ACCEPTUNICAST | 1125 LPC_RXFLTRW_ACCEPTUMULTICAST; 1126 if (ndev->flags & IFF_ALLMULTI) 1127 tmp32 |= LPC_RXFLTRW_ACCEPTUMULTICAST; 1128 1129 if (netdev_hw_addr_list_count(mcptr)) 1130 tmp32 |= LPC_RXFLTRW_ACCEPTUMULTICASTHASH; 1131 1132 writel(tmp32, LPC_ENET_RXFILTER_CTRL(pldat->net_base)); 1133 1134 1135 /* Set initial hash table */ 1136 hashlo = 0x0; 1137 hashhi = 0x0; 1138 1139 /* 64 bits : multicast address in hash table */ 1140 netdev_hw_addr_list_for_each(ha, mcptr) { 1141 hash_val = (ether_crc(6, ha->addr) >> 23) & 0x3F; 1142 1143 if (hash_val >= 32) 1144 hashhi |= 1 << (hash_val - 32); 1145 else 1146 hashlo |= 1 << hash_val; 1147 } 1148 1149 writel(hashlo, LPC_ENET_HASHFILTERL(pldat->net_base)); 1150 writel(hashhi, LPC_ENET_HASHFILTERH(pldat->net_base)); 1151 1152 spin_unlock_irqrestore(&pldat->lock, flags); 1153 } 1154 1155 static int lpc_eth_open(struct net_device *ndev) 1156 { 1157 struct netdata_local *pldat = netdev_priv(ndev); 1158 int ret; 1159 1160 if (netif_msg_ifup(pldat)) 1161 dev_dbg(&pldat->pdev->dev, "enabling %s\n", ndev->name); 1162 1163 ret = clk_prepare_enable(pldat->clk); 1164 if (ret) 1165 return ret; 1166 1167 /* Suspended PHY makes LPC ethernet core block, so resume now */ 1168 phy_resume(ndev->phydev); 1169 1170 /* Reset and initialize */ 1171 __lpc_eth_reset(pldat); 1172 __lpc_eth_init(pldat); 1173 1174 /* schedule a link state check */ 1175 phy_start(ndev->phydev); 1176 netif_start_queue(ndev); 1177 napi_enable(&pldat->napi); 1178 1179 return 0; 1180 } 1181 1182 /* 1183 * Ethtool ops 1184 */ 1185 static void lpc_eth_ethtool_getdrvinfo(struct net_device *ndev, 1186 struct ethtool_drvinfo *info) 1187 { 1188 strlcpy(info->driver, MODNAME, sizeof(info->driver)); 1189 strlcpy(info->version, DRV_VERSION, sizeof(info->version)); 1190 strlcpy(info->bus_info, dev_name(ndev->dev.parent), 1191 sizeof(info->bus_info)); 1192 } 1193 1194 static u32 lpc_eth_ethtool_getmsglevel(struct net_device *ndev) 1195 { 1196 struct netdata_local *pldat = netdev_priv(ndev); 1197 1198 return pldat->msg_enable; 1199 } 1200 1201 static void lpc_eth_ethtool_setmsglevel(struct net_device *ndev, u32 level) 1202 { 1203 struct netdata_local *pldat = netdev_priv(ndev); 1204 1205 pldat->msg_enable = level; 1206 } 1207 1208 static const struct ethtool_ops lpc_eth_ethtool_ops = { 1209 .get_drvinfo = lpc_eth_ethtool_getdrvinfo, 1210 .get_msglevel = lpc_eth_ethtool_getmsglevel, 1211 .set_msglevel = lpc_eth_ethtool_setmsglevel, 1212 .get_link = ethtool_op_get_link, 1213 .get_link_ksettings = phy_ethtool_get_link_ksettings, 1214 .set_link_ksettings = phy_ethtool_set_link_ksettings, 1215 }; 1216 1217 static const struct net_device_ops lpc_netdev_ops = { 1218 .ndo_open = lpc_eth_open, 1219 .ndo_stop = lpc_eth_close, 1220 .ndo_start_xmit = lpc_eth_hard_start_xmit, 1221 .ndo_set_rx_mode = lpc_eth_set_multicast_list, 1222 .ndo_eth_ioctl = phy_do_ioctl_running, 1223 .ndo_set_mac_address = lpc_set_mac_address, 1224 .ndo_validate_addr = eth_validate_addr, 1225 }; 1226 1227 static int lpc_eth_drv_probe(struct platform_device *pdev) 1228 { 1229 struct device *dev = &pdev->dev; 1230 struct device_node *np = dev->of_node; 1231 struct netdata_local *pldat; 1232 struct net_device *ndev; 1233 dma_addr_t dma_handle; 1234 struct resource *res; 1235 int irq, ret; 1236 1237 /* Setup network interface for RMII or MII mode */ 1238 lpc32xx_set_phy_interface_mode(lpc_phy_interface_mode(dev)); 1239 1240 /* Get platform resources */ 1241 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1242 irq = platform_get_irq(pdev, 0); 1243 if (!res || irq < 0) { 1244 dev_err(dev, "error getting resources.\n"); 1245 ret = -ENXIO; 1246 goto err_exit; 1247 } 1248 1249 /* Allocate net driver data structure */ 1250 ndev = alloc_etherdev(sizeof(struct netdata_local)); 1251 if (!ndev) { 1252 dev_err(dev, "could not allocate device.\n"); 1253 ret = -ENOMEM; 1254 goto err_exit; 1255 } 1256 1257 SET_NETDEV_DEV(ndev, dev); 1258 1259 pldat = netdev_priv(ndev); 1260 pldat->pdev = pdev; 1261 pldat->ndev = ndev; 1262 1263 spin_lock_init(&pldat->lock); 1264 1265 /* Save resources */ 1266 ndev->irq = irq; 1267 1268 /* Get clock for the device */ 1269 pldat->clk = clk_get(dev, NULL); 1270 if (IS_ERR(pldat->clk)) { 1271 dev_err(dev, "error getting clock.\n"); 1272 ret = PTR_ERR(pldat->clk); 1273 goto err_out_free_dev; 1274 } 1275 1276 /* Enable network clock */ 1277 ret = clk_prepare_enable(pldat->clk); 1278 if (ret) 1279 goto err_out_clk_put; 1280 1281 /* Map IO space */ 1282 pldat->net_base = ioremap(res->start, resource_size(res)); 1283 if (!pldat->net_base) { 1284 dev_err(dev, "failed to map registers\n"); 1285 ret = -ENOMEM; 1286 goto err_out_disable_clocks; 1287 } 1288 ret = request_irq(ndev->irq, __lpc_eth_interrupt, 0, 1289 ndev->name, ndev); 1290 if (ret) { 1291 dev_err(dev, "error requesting interrupt.\n"); 1292 goto err_out_iounmap; 1293 } 1294 1295 /* Setup driver functions */ 1296 ndev->netdev_ops = &lpc_netdev_ops; 1297 ndev->ethtool_ops = &lpc_eth_ethtool_ops; 1298 ndev->watchdog_timeo = msecs_to_jiffies(2500); 1299 1300 /* Get size of DMA buffers/descriptors region */ 1301 pldat->dma_buff_size = (ENET_TX_DESC + ENET_RX_DESC) * (ENET_MAXF_SIZE + 1302 sizeof(struct txrx_desc_t) + sizeof(struct rx_status_t)); 1303 1304 if (use_iram_for_net(dev)) { 1305 if (pldat->dma_buff_size > 1306 lpc32xx_return_iram(&pldat->dma_buff_base_v, &dma_handle)) { 1307 pldat->dma_buff_base_v = NULL; 1308 pldat->dma_buff_size = 0; 1309 netdev_err(ndev, 1310 "IRAM not big enough for net buffers, using SDRAM instead.\n"); 1311 } 1312 } 1313 1314 if (pldat->dma_buff_base_v == NULL) { 1315 ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32)); 1316 if (ret) 1317 goto err_out_free_irq; 1318 1319 pldat->dma_buff_size = PAGE_ALIGN(pldat->dma_buff_size); 1320 1321 /* Allocate a chunk of memory for the DMA ethernet buffers 1322 * and descriptors 1323 */ 1324 pldat->dma_buff_base_v = 1325 dma_alloc_coherent(dev, 1326 pldat->dma_buff_size, &dma_handle, 1327 GFP_KERNEL); 1328 if (pldat->dma_buff_base_v == NULL) { 1329 ret = -ENOMEM; 1330 goto err_out_free_irq; 1331 } 1332 } 1333 pldat->dma_buff_base_p = dma_handle; 1334 1335 netdev_dbg(ndev, "IO address space :%pR\n", res); 1336 netdev_dbg(ndev, "IO address size :%zd\n", 1337 (size_t)resource_size(res)); 1338 netdev_dbg(ndev, "IO address (mapped) :0x%p\n", 1339 pldat->net_base); 1340 netdev_dbg(ndev, "IRQ number :%d\n", ndev->irq); 1341 netdev_dbg(ndev, "DMA buffer size :%zd\n", pldat->dma_buff_size); 1342 netdev_dbg(ndev, "DMA buffer P address :%pad\n", 1343 &pldat->dma_buff_base_p); 1344 netdev_dbg(ndev, "DMA buffer V address :0x%p\n", 1345 pldat->dma_buff_base_v); 1346 1347 pldat->phy_node = of_parse_phandle(np, "phy-handle", 0); 1348 1349 /* Get MAC address from current HW setting (POR state is all zeros) */ 1350 __lpc_get_mac(pldat, ndev->dev_addr); 1351 1352 if (!is_valid_ether_addr(ndev->dev_addr)) { 1353 of_get_mac_address(np, ndev->dev_addr); 1354 } 1355 if (!is_valid_ether_addr(ndev->dev_addr)) 1356 eth_hw_addr_random(ndev); 1357 1358 /* then shut everything down to save power */ 1359 __lpc_eth_shutdown(pldat); 1360 1361 /* Set default parameters */ 1362 pldat->msg_enable = NETIF_MSG_LINK; 1363 1364 /* Force an MII interface reset and clock setup */ 1365 __lpc_mii_mngt_reset(pldat); 1366 1367 /* Force default PHY interface setup in chip, this will probably be 1368 * changed by the PHY driver 1369 */ 1370 pldat->link = 0; 1371 pldat->speed = 100; 1372 pldat->duplex = DUPLEX_FULL; 1373 __lpc_params_setup(pldat); 1374 1375 netif_napi_add(ndev, &pldat->napi, lpc_eth_poll, NAPI_WEIGHT); 1376 1377 ret = register_netdev(ndev); 1378 if (ret) { 1379 dev_err(dev, "Cannot register net device, aborting.\n"); 1380 goto err_out_dma_unmap; 1381 } 1382 platform_set_drvdata(pdev, ndev); 1383 1384 ret = lpc_mii_init(pldat); 1385 if (ret) 1386 goto err_out_unregister_netdev; 1387 1388 netdev_info(ndev, "LPC mac at 0x%08lx irq %d\n", 1389 (unsigned long)res->start, ndev->irq); 1390 1391 device_init_wakeup(dev, 1); 1392 device_set_wakeup_enable(dev, 0); 1393 1394 return 0; 1395 1396 err_out_unregister_netdev: 1397 unregister_netdev(ndev); 1398 err_out_dma_unmap: 1399 if (!use_iram_for_net(dev) || 1400 pldat->dma_buff_size > lpc32xx_return_iram(NULL, NULL)) 1401 dma_free_coherent(dev, pldat->dma_buff_size, 1402 pldat->dma_buff_base_v, 1403 pldat->dma_buff_base_p); 1404 err_out_free_irq: 1405 free_irq(ndev->irq, ndev); 1406 err_out_iounmap: 1407 iounmap(pldat->net_base); 1408 err_out_disable_clocks: 1409 clk_disable_unprepare(pldat->clk); 1410 err_out_clk_put: 1411 clk_put(pldat->clk); 1412 err_out_free_dev: 1413 free_netdev(ndev); 1414 err_exit: 1415 pr_err("%s: not found (%d).\n", MODNAME, ret); 1416 return ret; 1417 } 1418 1419 static int lpc_eth_drv_remove(struct platform_device *pdev) 1420 { 1421 struct net_device *ndev = platform_get_drvdata(pdev); 1422 struct netdata_local *pldat = netdev_priv(ndev); 1423 1424 unregister_netdev(ndev); 1425 1426 if (!use_iram_for_net(&pldat->pdev->dev) || 1427 pldat->dma_buff_size > lpc32xx_return_iram(NULL, NULL)) 1428 dma_free_coherent(&pldat->pdev->dev, pldat->dma_buff_size, 1429 pldat->dma_buff_base_v, 1430 pldat->dma_buff_base_p); 1431 free_irq(ndev->irq, ndev); 1432 iounmap(pldat->net_base); 1433 mdiobus_unregister(pldat->mii_bus); 1434 mdiobus_free(pldat->mii_bus); 1435 clk_disable_unprepare(pldat->clk); 1436 clk_put(pldat->clk); 1437 free_netdev(ndev); 1438 1439 return 0; 1440 } 1441 1442 #ifdef CONFIG_PM 1443 static int lpc_eth_drv_suspend(struct platform_device *pdev, 1444 pm_message_t state) 1445 { 1446 struct net_device *ndev = platform_get_drvdata(pdev); 1447 struct netdata_local *pldat = netdev_priv(ndev); 1448 1449 if (device_may_wakeup(&pdev->dev)) 1450 enable_irq_wake(ndev->irq); 1451 1452 if (ndev) { 1453 if (netif_running(ndev)) { 1454 netif_device_detach(ndev); 1455 __lpc_eth_shutdown(pldat); 1456 clk_disable_unprepare(pldat->clk); 1457 1458 /* 1459 * Reset again now clock is disable to be sure 1460 * EMC_MDC is down 1461 */ 1462 __lpc_eth_reset(pldat); 1463 } 1464 } 1465 1466 return 0; 1467 } 1468 1469 static int lpc_eth_drv_resume(struct platform_device *pdev) 1470 { 1471 struct net_device *ndev = platform_get_drvdata(pdev); 1472 struct netdata_local *pldat; 1473 1474 if (device_may_wakeup(&pdev->dev)) 1475 disable_irq_wake(ndev->irq); 1476 1477 if (ndev) { 1478 if (netif_running(ndev)) { 1479 pldat = netdev_priv(ndev); 1480 1481 /* Enable interface clock */ 1482 clk_enable(pldat->clk); 1483 1484 /* Reset and initialize */ 1485 __lpc_eth_reset(pldat); 1486 __lpc_eth_init(pldat); 1487 1488 netif_device_attach(ndev); 1489 } 1490 } 1491 1492 return 0; 1493 } 1494 #endif 1495 1496 static const struct of_device_id lpc_eth_match[] = { 1497 { .compatible = "nxp,lpc-eth" }, 1498 { } 1499 }; 1500 MODULE_DEVICE_TABLE(of, lpc_eth_match); 1501 1502 static struct platform_driver lpc_eth_driver = { 1503 .probe = lpc_eth_drv_probe, 1504 .remove = lpc_eth_drv_remove, 1505 #ifdef CONFIG_PM 1506 .suspend = lpc_eth_drv_suspend, 1507 .resume = lpc_eth_drv_resume, 1508 #endif 1509 .driver = { 1510 .name = MODNAME, 1511 .of_match_table = lpc_eth_match, 1512 }, 1513 }; 1514 1515 module_platform_driver(lpc_eth_driver); 1516 1517 MODULE_AUTHOR("Kevin Wells <kevin.wells@nxp.com>"); 1518 MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>"); 1519 MODULE_DESCRIPTION("LPC Ethernet Driver"); 1520 MODULE_LICENSE("GPL"); 1521