1 /* 2 * (C) Copyright 2016 3 * Author: Amit Singh Tomar, amittomer25@gmail.com 4 * 5 * SPDX-License-Identifier: GPL-2.0+ 6 * 7 * Ethernet driver for H3/A64/A83T based SoC's 8 * 9 * It is derived from the work done by 10 * LABBE Corentin & Chen-Yu Tsai for Linux, THANKS! 11 * 12 */ 13 14 #include <asm/io.h> 15 #include <asm/arch/clock.h> 16 #include <asm/arch/gpio.h> 17 #include <common.h> 18 #include <dm.h> 19 #include <fdt_support.h> 20 #include <linux/err.h> 21 #include <malloc.h> 22 #include <miiphy.h> 23 #include <net.h> 24 25 #define MDIO_CMD_MII_BUSY BIT(0) 26 #define MDIO_CMD_MII_WRITE BIT(1) 27 28 #define MDIO_CMD_MII_PHY_REG_ADDR_MASK 0x000001f0 29 #define MDIO_CMD_MII_PHY_REG_ADDR_SHIFT 4 30 #define MDIO_CMD_MII_PHY_ADDR_MASK 0x0001f000 31 #define MDIO_CMD_MII_PHY_ADDR_SHIFT 12 32 33 #define CONFIG_TX_DESCR_NUM 32 34 #define CONFIG_RX_DESCR_NUM 32 35 #define CONFIG_ETH_BUFSIZE 2048 /* Note must be dma aligned */ 36 37 /* 38 * The datasheet says that each descriptor can transfers up to 4096 bytes 39 * But later, the register documentation reduces that value to 2048, 40 * using 2048 cause strange behaviours and even BSP driver use 2047 41 */ 42 #define CONFIG_ETH_RXSIZE 2044 /* Note must fit in ETH_BUFSIZE */ 43 44 #define TX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_TX_DESCR_NUM) 45 #define RX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_RX_DESCR_NUM) 46 47 #define H3_EPHY_DEFAULT_VALUE 0x58000 48 #define H3_EPHY_DEFAULT_MASK GENMASK(31, 15) 49 #define H3_EPHY_ADDR_SHIFT 20 50 #define REG_PHY_ADDR_MASK GENMASK(4, 0) 51 #define H3_EPHY_LED_POL BIT(17) /* 1: active low, 0: active high */ 52 #define H3_EPHY_SHUTDOWN BIT(16) /* 1: shutdown, 0: power up */ 53 #define H3_EPHY_SELECT BIT(15) /* 1: internal PHY, 0: external PHY */ 54 55 #define SC_RMII_EN BIT(13) 56 #define SC_EPIT BIT(2) /* 1: RGMII, 0: MII */ 57 #define SC_ETCS_MASK GENMASK(1, 0) 58 #define SC_ETCS_EXT_GMII 0x1 59 #define SC_ETCS_INT_GMII 0x2 60 61 #define CONFIG_MDIO_TIMEOUT (3 * CONFIG_SYS_HZ) 62 63 #define AHB_GATE_OFFSET_EPHY 0 64 65 #if defined(CONFIG_MACH_SUN8I_H3) 66 #define SUN8I_GPD8_GMAC 2 67 #else 68 #define SUN8I_GPD8_GMAC 4 69 #endif 70 71 /* H3/A64 EMAC Register's offset */ 72 #define EMAC_CTL0 0x00 73 #define EMAC_CTL1 0x04 74 #define EMAC_INT_STA 0x08 75 #define EMAC_INT_EN 0x0c 76 #define EMAC_TX_CTL0 0x10 77 #define EMAC_TX_CTL1 0x14 78 #define EMAC_TX_FLOW_CTL 0x1c 79 #define EMAC_TX_DMA_DESC 0x20 80 #define EMAC_RX_CTL0 0x24 81 #define EMAC_RX_CTL1 0x28 82 #define EMAC_RX_DMA_DESC 0x34 83 #define EMAC_MII_CMD 0x48 84 #define EMAC_MII_DATA 0x4c 85 #define EMAC_ADDR0_HIGH 0x50 86 #define EMAC_ADDR0_LOW 0x54 87 #define EMAC_TX_DMA_STA 0xb0 88 #define EMAC_TX_CUR_DESC 0xb4 89 #define EMAC_TX_CUR_BUF 0xb8 90 #define EMAC_RX_DMA_STA 0xc0 91 #define EMAC_RX_CUR_DESC 0xc4 92 93 DECLARE_GLOBAL_DATA_PTR; 94 95 enum emac_variant { 96 A83T_EMAC = 1, 97 H3_EMAC, 98 A64_EMAC, 99 }; 100 101 struct emac_dma_desc { 102 u32 status; 103 u32 st; 104 u32 buf_addr; 105 u32 next; 106 } __aligned(ARCH_DMA_MINALIGN); 107 108 struct emac_eth_dev { 109 struct emac_dma_desc rx_chain[CONFIG_TX_DESCR_NUM]; 110 struct emac_dma_desc tx_chain[CONFIG_RX_DESCR_NUM]; 111 char rxbuffer[RX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN); 112 char txbuffer[TX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN); 113 114 u32 interface; 115 u32 phyaddr; 116 u32 link; 117 u32 speed; 118 u32 duplex; 119 u32 phy_configured; 120 u32 tx_currdescnum; 121 u32 rx_currdescnum; 122 u32 addr; 123 u32 tx_slot; 124 bool use_internal_phy; 125 126 enum emac_variant variant; 127 void *mac_reg; 128 phys_addr_t sysctl_reg; 129 struct phy_device *phydev; 130 struct mii_dev *bus; 131 }; 132 133 static int sun8i_mdio_read(struct mii_dev *bus, int addr, int devad, int reg) 134 { 135 struct emac_eth_dev *priv = bus->priv; 136 ulong start; 137 u32 miiaddr = 0; 138 int timeout = CONFIG_MDIO_TIMEOUT; 139 140 miiaddr &= ~MDIO_CMD_MII_WRITE; 141 miiaddr &= ~MDIO_CMD_MII_PHY_REG_ADDR_MASK; 142 miiaddr |= (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) & 143 MDIO_CMD_MII_PHY_REG_ADDR_MASK; 144 145 miiaddr &= ~MDIO_CMD_MII_PHY_ADDR_MASK; 146 147 miiaddr |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) & 148 MDIO_CMD_MII_PHY_ADDR_MASK; 149 150 miiaddr |= MDIO_CMD_MII_BUSY; 151 152 writel(miiaddr, priv->mac_reg + EMAC_MII_CMD); 153 154 start = get_timer(0); 155 while (get_timer(start) < timeout) { 156 if (!(readl(priv->mac_reg + EMAC_MII_CMD) & MDIO_CMD_MII_BUSY)) 157 return readl(priv->mac_reg + EMAC_MII_DATA); 158 udelay(10); 159 }; 160 161 return -1; 162 } 163 164 static int sun8i_mdio_write(struct mii_dev *bus, int addr, int devad, int reg, 165 u16 val) 166 { 167 struct emac_eth_dev *priv = bus->priv; 168 ulong start; 169 u32 miiaddr = 0; 170 int ret = -1, timeout = CONFIG_MDIO_TIMEOUT; 171 172 miiaddr &= ~MDIO_CMD_MII_PHY_REG_ADDR_MASK; 173 miiaddr |= (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) & 174 MDIO_CMD_MII_PHY_REG_ADDR_MASK; 175 176 miiaddr &= ~MDIO_CMD_MII_PHY_ADDR_MASK; 177 miiaddr |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) & 178 MDIO_CMD_MII_PHY_ADDR_MASK; 179 180 miiaddr |= MDIO_CMD_MII_WRITE; 181 miiaddr |= MDIO_CMD_MII_BUSY; 182 183 writel(miiaddr, priv->mac_reg + EMAC_MII_CMD); 184 writel(val, priv->mac_reg + EMAC_MII_DATA); 185 186 start = get_timer(0); 187 while (get_timer(start) < timeout) { 188 if (!(readl(priv->mac_reg + EMAC_MII_CMD) & 189 MDIO_CMD_MII_BUSY)) { 190 ret = 0; 191 break; 192 } 193 udelay(10); 194 }; 195 196 return ret; 197 } 198 199 static int _sun8i_write_hwaddr(struct emac_eth_dev *priv, u8 *mac_id) 200 { 201 u32 macid_lo, macid_hi; 202 203 macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) + 204 (mac_id[3] << 24); 205 macid_hi = mac_id[4] + (mac_id[5] << 8); 206 207 writel(macid_hi, priv->mac_reg + EMAC_ADDR0_HIGH); 208 writel(macid_lo, priv->mac_reg + EMAC_ADDR0_LOW); 209 210 return 0; 211 } 212 213 static void sun8i_adjust_link(struct emac_eth_dev *priv, 214 struct phy_device *phydev) 215 { 216 u32 v; 217 218 v = readl(priv->mac_reg + EMAC_CTL0); 219 220 if (phydev->duplex) 221 v |= BIT(0); 222 else 223 v &= ~BIT(0); 224 225 v &= ~0x0C; 226 227 switch (phydev->speed) { 228 case 1000: 229 break; 230 case 100: 231 v |= BIT(2); 232 v |= BIT(3); 233 break; 234 case 10: 235 v |= BIT(3); 236 break; 237 } 238 writel(v, priv->mac_reg + EMAC_CTL0); 239 } 240 241 static int sun8i_emac_set_syscon_ephy(struct emac_eth_dev *priv, u32 *reg) 242 { 243 if (priv->use_internal_phy) { 244 /* H3 based SoC's that has an Internal 100MBit PHY 245 * needs to be configured and powered up before use 246 */ 247 *reg &= ~H3_EPHY_DEFAULT_MASK; 248 *reg |= H3_EPHY_DEFAULT_VALUE; 249 *reg |= priv->phyaddr << H3_EPHY_ADDR_SHIFT; 250 *reg &= ~H3_EPHY_SHUTDOWN; 251 *reg |= H3_EPHY_SELECT; 252 } else 253 /* This is to select External Gigabit PHY on 254 * the boards with H3 SoC. 255 */ 256 *reg &= ~H3_EPHY_SELECT; 257 258 return 0; 259 } 260 261 static int sun8i_emac_set_syscon(struct emac_eth_dev *priv) 262 { 263 int ret; 264 u32 reg; 265 266 reg = readl(priv->sysctl_reg); 267 268 if (priv->variant == H3_EMAC) { 269 ret = sun8i_emac_set_syscon_ephy(priv, ®); 270 if (ret) 271 return ret; 272 } 273 274 reg &= ~(SC_ETCS_MASK | SC_EPIT); 275 if (priv->variant == H3_EMAC || priv->variant == A64_EMAC) 276 reg &= ~SC_RMII_EN; 277 278 switch (priv->interface) { 279 case PHY_INTERFACE_MODE_MII: 280 /* default */ 281 break; 282 case PHY_INTERFACE_MODE_RGMII: 283 reg |= SC_EPIT | SC_ETCS_INT_GMII; 284 break; 285 case PHY_INTERFACE_MODE_RMII: 286 if (priv->variant == H3_EMAC || 287 priv->variant == A64_EMAC) { 288 reg |= SC_RMII_EN | SC_ETCS_EXT_GMII; 289 break; 290 } 291 /* RMII not supported on A83T */ 292 default: 293 debug("%s: Invalid PHY interface\n", __func__); 294 return -EINVAL; 295 } 296 297 writel(reg, priv->sysctl_reg); 298 299 return 0; 300 } 301 302 static int sun8i_phy_init(struct emac_eth_dev *priv, void *dev) 303 { 304 struct phy_device *phydev; 305 306 phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface); 307 if (!phydev) 308 return -ENODEV; 309 310 phy_connect_dev(phydev, dev); 311 312 priv->phydev = phydev; 313 phy_config(priv->phydev); 314 315 return 0; 316 } 317 318 static void rx_descs_init(struct emac_eth_dev *priv) 319 { 320 struct emac_dma_desc *desc_table_p = &priv->rx_chain[0]; 321 char *rxbuffs = &priv->rxbuffer[0]; 322 struct emac_dma_desc *desc_p; 323 u32 idx; 324 325 /* flush Rx buffers */ 326 flush_dcache_range((uintptr_t)rxbuffs, (ulong)rxbuffs + 327 RX_TOTAL_BUFSIZE); 328 329 for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) { 330 desc_p = &desc_table_p[idx]; 331 desc_p->buf_addr = (uintptr_t)&rxbuffs[idx * CONFIG_ETH_BUFSIZE] 332 ; 333 desc_p->next = (uintptr_t)&desc_table_p[idx + 1]; 334 desc_p->st |= CONFIG_ETH_RXSIZE; 335 desc_p->status = BIT(31); 336 } 337 338 /* Correcting the last pointer of the chain */ 339 desc_p->next = (uintptr_t)&desc_table_p[0]; 340 341 flush_dcache_range((uintptr_t)priv->rx_chain, 342 (uintptr_t)priv->rx_chain + 343 sizeof(priv->rx_chain)); 344 345 writel((uintptr_t)&desc_table_p[0], (priv->mac_reg + EMAC_RX_DMA_DESC)); 346 priv->rx_currdescnum = 0; 347 } 348 349 static void tx_descs_init(struct emac_eth_dev *priv) 350 { 351 struct emac_dma_desc *desc_table_p = &priv->tx_chain[0]; 352 char *txbuffs = &priv->txbuffer[0]; 353 struct emac_dma_desc *desc_p; 354 u32 idx; 355 356 for (idx = 0; idx < CONFIG_TX_DESCR_NUM; idx++) { 357 desc_p = &desc_table_p[idx]; 358 desc_p->buf_addr = (uintptr_t)&txbuffs[idx * CONFIG_ETH_BUFSIZE] 359 ; 360 desc_p->next = (uintptr_t)&desc_table_p[idx + 1]; 361 desc_p->status = (1 << 31); 362 desc_p->st = 0; 363 } 364 365 /* Correcting the last pointer of the chain */ 366 desc_p->next = (uintptr_t)&desc_table_p[0]; 367 368 /* Flush all Tx buffer descriptors */ 369 flush_dcache_range((uintptr_t)priv->tx_chain, 370 (uintptr_t)priv->tx_chain + 371 sizeof(priv->tx_chain)); 372 373 writel((uintptr_t)&desc_table_p[0], priv->mac_reg + EMAC_TX_DMA_DESC); 374 priv->tx_currdescnum = 0; 375 } 376 377 static int _sun8i_emac_eth_init(struct emac_eth_dev *priv, u8 *enetaddr) 378 { 379 u32 reg, v; 380 int timeout = 100; 381 382 reg = readl((priv->mac_reg + EMAC_CTL1)); 383 384 if (!(reg & 0x1)) { 385 /* Soft reset MAC */ 386 setbits_le32((priv->mac_reg + EMAC_CTL1), 0x1); 387 do { 388 reg = readl(priv->mac_reg + EMAC_CTL1); 389 } while ((reg & 0x01) != 0 && (--timeout)); 390 if (!timeout) { 391 printf("%s: Timeout\n", __func__); 392 return -1; 393 } 394 } 395 396 /* Rewrite mac address after reset */ 397 _sun8i_write_hwaddr(priv, enetaddr); 398 399 v = readl(priv->mac_reg + EMAC_TX_CTL1); 400 /* TX_MD Transmission starts after a full frame located in TX DMA FIFO*/ 401 v |= BIT(1); 402 writel(v, priv->mac_reg + EMAC_TX_CTL1); 403 404 v = readl(priv->mac_reg + EMAC_RX_CTL1); 405 /* RX_MD RX DMA reads data from RX DMA FIFO to host memory after a 406 * complete frame has been written to RX DMA FIFO 407 */ 408 v |= BIT(1); 409 writel(v, priv->mac_reg + EMAC_RX_CTL1); 410 411 /* DMA */ 412 writel(8 << 24, priv->mac_reg + EMAC_CTL1); 413 414 /* Initialize rx/tx descriptors */ 415 rx_descs_init(priv); 416 tx_descs_init(priv); 417 418 /* PHY Start Up */ 419 genphy_parse_link(priv->phydev); 420 421 sun8i_adjust_link(priv, priv->phydev); 422 423 /* Start RX DMA */ 424 v = readl(priv->mac_reg + EMAC_RX_CTL1); 425 v |= BIT(30); 426 writel(v, priv->mac_reg + EMAC_RX_CTL1); 427 /* Start TX DMA */ 428 v = readl(priv->mac_reg + EMAC_TX_CTL1); 429 v |= BIT(30); 430 writel(v, priv->mac_reg + EMAC_TX_CTL1); 431 432 /* Enable RX/TX */ 433 setbits_le32(priv->mac_reg + EMAC_RX_CTL0, BIT(31)); 434 setbits_le32(priv->mac_reg + EMAC_TX_CTL0, BIT(31)); 435 436 return 0; 437 } 438 439 static int parse_phy_pins(struct udevice *dev) 440 { 441 int offset; 442 const char *pin_name; 443 int drive, pull, i; 444 445 offset = fdtdec_lookup_phandle(gd->fdt_blob, dev->of_offset, 446 "pinctrl-0"); 447 if (offset < 0) { 448 printf("WARNING: emac: cannot find pinctrl-0 node\n"); 449 return offset; 450 } 451 452 drive = fdt_getprop_u32_default_node(gd->fdt_blob, offset, 0, 453 "allwinner,drive", 4); 454 pull = fdt_getprop_u32_default_node(gd->fdt_blob, offset, 0, 455 "allwinner,pull", 0); 456 for (i = 0; ; i++) { 457 int pin; 458 459 if (fdt_get_string_index(gd->fdt_blob, offset, 460 "allwinner,pins", i, &pin_name)) 461 break; 462 if (pin_name[0] != 'P') 463 continue; 464 pin = (pin_name[1] - 'A') << 5; 465 if (pin >= 26 << 5) 466 continue; 467 pin += simple_strtol(&pin_name[2], NULL, 10); 468 469 sunxi_gpio_set_cfgpin(pin, SUN8I_GPD8_GMAC); 470 sunxi_gpio_set_drv(pin, drive); 471 sunxi_gpio_set_pull(pin, pull); 472 } 473 474 if (!i) { 475 printf("WARNING: emac: cannot find allwinner,pins property\n"); 476 return -2; 477 } 478 479 return 0; 480 } 481 482 static int _sun8i_eth_recv(struct emac_eth_dev *priv, uchar **packetp) 483 { 484 u32 status, desc_num = priv->rx_currdescnum; 485 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num]; 486 int length = -EAGAIN; 487 int good_packet = 1; 488 uintptr_t desc_start = (uintptr_t)desc_p; 489 uintptr_t desc_end = desc_start + 490 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN); 491 492 ulong data_start = (uintptr_t)desc_p->buf_addr; 493 ulong data_end; 494 495 /* Invalidate entire buffer descriptor */ 496 invalidate_dcache_range(desc_start, desc_end); 497 498 status = desc_p->status; 499 500 /* Check for DMA own bit */ 501 if (!(status & BIT(31))) { 502 length = (desc_p->status >> 16) & 0x3FFF; 503 504 if (length < 0x40) { 505 good_packet = 0; 506 debug("RX: Bad Packet (runt)\n"); 507 } 508 509 data_end = data_start + length; 510 /* Invalidate received data */ 511 invalidate_dcache_range(rounddown(data_start, 512 ARCH_DMA_MINALIGN), 513 roundup(data_end, 514 ARCH_DMA_MINALIGN)); 515 if (good_packet) { 516 if (length > CONFIG_ETH_RXSIZE) { 517 printf("Received packet is too big (len=%d)\n", 518 length); 519 return -EMSGSIZE; 520 } 521 *packetp = (uchar *)(ulong)desc_p->buf_addr; 522 return length; 523 } 524 } 525 526 return length; 527 } 528 529 static int _sun8i_emac_eth_send(struct emac_eth_dev *priv, void *packet, 530 int len) 531 { 532 u32 v, desc_num = priv->tx_currdescnum; 533 struct emac_dma_desc *desc_p = &priv->tx_chain[desc_num]; 534 uintptr_t desc_start = (uintptr_t)desc_p; 535 uintptr_t desc_end = desc_start + 536 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN); 537 538 uintptr_t data_start = (uintptr_t)desc_p->buf_addr; 539 uintptr_t data_end = data_start + 540 roundup(len, ARCH_DMA_MINALIGN); 541 542 /* Invalidate entire buffer descriptor */ 543 invalidate_dcache_range(desc_start, desc_end); 544 545 desc_p->st = len; 546 /* Mandatory undocumented bit */ 547 desc_p->st |= BIT(24); 548 549 memcpy((void *)data_start, packet, len); 550 551 /* Flush data to be sent */ 552 flush_dcache_range(data_start, data_end); 553 554 /* frame end */ 555 desc_p->st |= BIT(30); 556 desc_p->st |= BIT(31); 557 558 /*frame begin */ 559 desc_p->st |= BIT(29); 560 desc_p->status = BIT(31); 561 562 /*Descriptors st and status field has changed, so FLUSH it */ 563 flush_dcache_range(desc_start, desc_end); 564 565 /* Move to next Descriptor and wrap around */ 566 if (++desc_num >= CONFIG_TX_DESCR_NUM) 567 desc_num = 0; 568 priv->tx_currdescnum = desc_num; 569 570 /* Start the DMA */ 571 v = readl(priv->mac_reg + EMAC_TX_CTL1); 572 v |= BIT(31);/* mandatory */ 573 v |= BIT(30);/* mandatory */ 574 writel(v, priv->mac_reg + EMAC_TX_CTL1); 575 576 return 0; 577 } 578 579 static int sun8i_eth_write_hwaddr(struct udevice *dev) 580 { 581 struct eth_pdata *pdata = dev_get_platdata(dev); 582 struct emac_eth_dev *priv = dev_get_priv(dev); 583 584 return _sun8i_write_hwaddr(priv, pdata->enetaddr); 585 } 586 587 static void sun8i_emac_board_setup(struct emac_eth_dev *priv) 588 { 589 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE; 590 591 if (priv->use_internal_phy) { 592 /* Set clock gating for ephy */ 593 setbits_le32(&ccm->bus_gate4, BIT(AHB_GATE_OFFSET_EPHY)); 594 595 /* Deassert EPHY */ 596 setbits_le32(&ccm->ahb_reset2_cfg, BIT(AHB_RESET_OFFSET_EPHY)); 597 } 598 599 /* Set clock gating for emac */ 600 setbits_le32(&ccm->ahb_gate0, BIT(AHB_GATE_OFFSET_GMAC)); 601 602 /* De-assert EMAC */ 603 setbits_le32(&ccm->ahb_reset0_cfg, BIT(AHB_RESET_OFFSET_GMAC)); 604 } 605 606 static int sun8i_mdio_init(const char *name, struct emac_eth_dev *priv) 607 { 608 struct mii_dev *bus = mdio_alloc(); 609 610 if (!bus) { 611 debug("Failed to allocate MDIO bus\n"); 612 return -ENOMEM; 613 } 614 615 bus->read = sun8i_mdio_read; 616 bus->write = sun8i_mdio_write; 617 snprintf(bus->name, sizeof(bus->name), name); 618 bus->priv = (void *)priv; 619 620 return mdio_register(bus); 621 } 622 623 static int sun8i_emac_eth_start(struct udevice *dev) 624 { 625 struct eth_pdata *pdata = dev_get_platdata(dev); 626 627 return _sun8i_emac_eth_init(dev->priv, pdata->enetaddr); 628 } 629 630 static int sun8i_emac_eth_send(struct udevice *dev, void *packet, int length) 631 { 632 struct emac_eth_dev *priv = dev_get_priv(dev); 633 634 return _sun8i_emac_eth_send(priv, packet, length); 635 } 636 637 static int sun8i_emac_eth_recv(struct udevice *dev, int flags, uchar **packetp) 638 { 639 struct emac_eth_dev *priv = dev_get_priv(dev); 640 641 return _sun8i_eth_recv(priv, packetp); 642 } 643 644 static int _sun8i_free_pkt(struct emac_eth_dev *priv) 645 { 646 u32 desc_num = priv->rx_currdescnum; 647 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num]; 648 uintptr_t desc_start = (uintptr_t)desc_p; 649 uintptr_t desc_end = desc_start + 650 roundup(sizeof(u32), ARCH_DMA_MINALIGN); 651 652 /* Make the current descriptor valid again */ 653 desc_p->status |= BIT(31); 654 655 /* Flush Status field of descriptor */ 656 flush_dcache_range(desc_start, desc_end); 657 658 /* Move to next desc and wrap-around condition. */ 659 if (++desc_num >= CONFIG_RX_DESCR_NUM) 660 desc_num = 0; 661 priv->rx_currdescnum = desc_num; 662 663 return 0; 664 } 665 666 static int sun8i_eth_free_pkt(struct udevice *dev, uchar *packet, 667 int length) 668 { 669 struct emac_eth_dev *priv = dev_get_priv(dev); 670 671 return _sun8i_free_pkt(priv); 672 } 673 674 static void sun8i_emac_eth_stop(struct udevice *dev) 675 { 676 struct emac_eth_dev *priv = dev_get_priv(dev); 677 678 /* Stop Rx/Tx transmitter */ 679 clrbits_le32(priv->mac_reg + EMAC_RX_CTL0, BIT(31)); 680 clrbits_le32(priv->mac_reg + EMAC_TX_CTL0, BIT(31)); 681 682 /* Stop TX DMA */ 683 clrbits_le32(priv->mac_reg + EMAC_TX_CTL1, BIT(30)); 684 685 phy_shutdown(priv->phydev); 686 } 687 688 static int sun8i_emac_eth_probe(struct udevice *dev) 689 { 690 struct eth_pdata *pdata = dev_get_platdata(dev); 691 struct emac_eth_dev *priv = dev_get_priv(dev); 692 693 priv->mac_reg = (void *)pdata->iobase; 694 695 sun8i_emac_board_setup(priv); 696 sun8i_emac_set_syscon(priv); 697 698 sun8i_mdio_init(dev->name, priv); 699 priv->bus = miiphy_get_dev_by_name(dev->name); 700 701 return sun8i_phy_init(priv, dev); 702 } 703 704 static const struct eth_ops sun8i_emac_eth_ops = { 705 .start = sun8i_emac_eth_start, 706 .write_hwaddr = sun8i_eth_write_hwaddr, 707 .send = sun8i_emac_eth_send, 708 .recv = sun8i_emac_eth_recv, 709 .free_pkt = sun8i_eth_free_pkt, 710 .stop = sun8i_emac_eth_stop, 711 }; 712 713 static int sun8i_emac_eth_ofdata_to_platdata(struct udevice *dev) 714 { 715 struct eth_pdata *pdata = dev_get_platdata(dev); 716 struct emac_eth_dev *priv = dev_get_priv(dev); 717 const char *phy_mode; 718 int offset = 0; 719 720 pdata->iobase = dev_get_addr_name(dev, "emac"); 721 priv->sysctl_reg = dev_get_addr_name(dev, "syscon"); 722 723 pdata->phy_interface = -1; 724 priv->phyaddr = -1; 725 priv->use_internal_phy = false; 726 727 offset = fdtdec_lookup_phandle(gd->fdt_blob, dev->of_offset, 728 "phy"); 729 if (offset > 0) 730 priv->phyaddr = fdtdec_get_int(gd->fdt_blob, offset, "reg", 731 -1); 732 733 phy_mode = fdt_getprop(gd->fdt_blob, dev->of_offset, "phy-mode", NULL); 734 735 if (phy_mode) 736 pdata->phy_interface = phy_get_interface_by_name(phy_mode); 737 printf("phy interface%d\n", pdata->phy_interface); 738 739 if (pdata->phy_interface == -1) { 740 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode); 741 return -EINVAL; 742 } 743 744 priv->variant = dev_get_driver_data(dev); 745 746 if (!priv->variant) { 747 printf("%s: Missing variant '%s'\n", __func__, 748 (char *)priv->variant); 749 return -EINVAL; 750 } 751 752 if (priv->variant == H3_EMAC) { 753 if (fdt_getprop(gd->fdt_blob, dev->of_offset, 754 "allwinner,use-internal-phy", NULL)) 755 priv->use_internal_phy = true; 756 } 757 758 priv->interface = pdata->phy_interface; 759 760 if (!priv->use_internal_phy) 761 parse_phy_pins(dev); 762 763 return 0; 764 } 765 766 static const struct udevice_id sun8i_emac_eth_ids[] = { 767 {.compatible = "allwinner,sun8i-h3-emac", .data = (uintptr_t)H3_EMAC }, 768 {.compatible = "allwinner,sun50i-a64-emac", 769 .data = (uintptr_t)A64_EMAC }, 770 {.compatible = "allwinner,sun8i-a83t-emac", 771 .data = (uintptr_t)A83T_EMAC }, 772 { } 773 }; 774 775 U_BOOT_DRIVER(eth_sun8i_emac) = { 776 .name = "eth_sun8i_emac", 777 .id = UCLASS_ETH, 778 .of_match = sun8i_emac_eth_ids, 779 .ofdata_to_platdata = sun8i_emac_eth_ofdata_to_platdata, 780 .probe = sun8i_emac_eth_probe, 781 .ops = &sun8i_emac_eth_ops, 782 .priv_auto_alloc_size = sizeof(struct emac_eth_dev), 783 .platdata_auto_alloc_size = sizeof(struct eth_pdata), 784 .flags = DM_FLAG_ALLOC_PRIV_DMA, 785 }; 786