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