19f93ac8dSLABBE Corentin /* 29f93ac8dSLABBE Corentin * dwmac-sun8i.c - Allwinner sun8i DWMAC specific glue layer 39f93ac8dSLABBE Corentin * 49f93ac8dSLABBE Corentin * Copyright (C) 2017 Corentin Labbe <clabbe.montjoie@gmail.com> 59f93ac8dSLABBE Corentin * 69f93ac8dSLABBE Corentin * This program is free software; you can redistribute it and/or modify 79f93ac8dSLABBE Corentin * it under the terms of the GNU General Public License as published by 89f93ac8dSLABBE Corentin * the Free Software Foundation; either version 2 of the License, or 99f93ac8dSLABBE Corentin * (at your option) any later version. 109f93ac8dSLABBE Corentin * 119f93ac8dSLABBE Corentin * This program is distributed in the hope that it will be useful, 129f93ac8dSLABBE Corentin * but WITHOUT ANY WARRANTY; without even the implied warranty of 139f93ac8dSLABBE Corentin * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 149f93ac8dSLABBE Corentin * GNU General Public License for more details. 159f93ac8dSLABBE Corentin */ 169f93ac8dSLABBE Corentin 179f93ac8dSLABBE Corentin #include <linux/clk.h> 189f93ac8dSLABBE Corentin #include <linux/io.h> 199f93ac8dSLABBE Corentin #include <linux/iopoll.h> 20634db83bSCorentin Labbe #include <linux/mdio-mux.h> 219f93ac8dSLABBE Corentin #include <linux/mfd/syscon.h> 229f93ac8dSLABBE Corentin #include <linux/module.h> 239f93ac8dSLABBE Corentin #include <linux/of_device.h> 249f93ac8dSLABBE Corentin #include <linux/of_mdio.h> 259f93ac8dSLABBE Corentin #include <linux/of_net.h> 269f93ac8dSLABBE Corentin #include <linux/phy.h> 279f93ac8dSLABBE Corentin #include <linux/platform_device.h> 289f93ac8dSLABBE Corentin #include <linux/regulator/consumer.h> 299f93ac8dSLABBE Corentin #include <linux/regmap.h> 309f93ac8dSLABBE Corentin #include <linux/stmmac.h> 319f93ac8dSLABBE Corentin 329f93ac8dSLABBE Corentin #include "stmmac.h" 339f93ac8dSLABBE Corentin #include "stmmac_platform.h" 349f93ac8dSLABBE Corentin 359f93ac8dSLABBE Corentin /* General notes on dwmac-sun8i: 369f93ac8dSLABBE Corentin * Locking: no locking is necessary in this file because all necessary locking 379f93ac8dSLABBE Corentin * is done in the "stmmac files" 389f93ac8dSLABBE Corentin */ 399f93ac8dSLABBE Corentin 409f93ac8dSLABBE Corentin /* struct emac_variant - Descrive dwmac-sun8i hardware variant 419f93ac8dSLABBE Corentin * @default_syscon_value: The default value of the EMAC register in syscon 429f93ac8dSLABBE Corentin * This value is used for disabling properly EMAC 439f93ac8dSLABBE Corentin * and used as a good starting value in case of the 449f93ac8dSLABBE Corentin * boot process(uboot) leave some stuff. 4525ae15fbSChen-Yu Tsai * @syscon_field reg_field for the syscon's gmac register 46634db83bSCorentin Labbe * @soc_has_internal_phy: Does the MAC embed an internal PHY 479f93ac8dSLABBE Corentin * @support_mii: Does the MAC handle MII 489f93ac8dSLABBE Corentin * @support_rmii: Does the MAC handle RMII 499f93ac8dSLABBE Corentin * @support_rgmii: Does the MAC handle RGMII 509f93ac8dSLABBE Corentin */ 519f93ac8dSLABBE Corentin struct emac_variant { 529f93ac8dSLABBE Corentin u32 default_syscon_value; 5325ae15fbSChen-Yu Tsai const struct reg_field *syscon_field; 54634db83bSCorentin Labbe bool soc_has_internal_phy; 559f93ac8dSLABBE Corentin bool support_mii; 569f93ac8dSLABBE Corentin bool support_rmii; 579f93ac8dSLABBE Corentin bool support_rgmii; 589f93ac8dSLABBE Corentin }; 599f93ac8dSLABBE Corentin 609f93ac8dSLABBE Corentin /* struct sunxi_priv_data - hold all sunxi private data 619f93ac8dSLABBE Corentin * @tx_clk: reference to MAC TX clock 629f93ac8dSLABBE Corentin * @ephy_clk: reference to the optional EPHY clock for the internal PHY 639f93ac8dSLABBE Corentin * @regulator: reference to the optional regulator 649f93ac8dSLABBE Corentin * @rst_ephy: reference to the optional EPHY reset for the internal PHY 659f93ac8dSLABBE Corentin * @variant: reference to the current board variant 669f93ac8dSLABBE Corentin * @regmap: regmap for using the syscon 67634db83bSCorentin Labbe * @internal_phy_powered: Does the internal PHY is enabled 68634db83bSCorentin Labbe * @mux_handle: Internal pointer used by mdio-mux lib 699f93ac8dSLABBE Corentin */ 709f93ac8dSLABBE Corentin struct sunxi_priv_data { 719f93ac8dSLABBE Corentin struct clk *tx_clk; 729f93ac8dSLABBE Corentin struct clk *ephy_clk; 739f93ac8dSLABBE Corentin struct regulator *regulator; 749f93ac8dSLABBE Corentin struct reset_control *rst_ephy; 759f93ac8dSLABBE Corentin const struct emac_variant *variant; 7625ae15fbSChen-Yu Tsai struct regmap_field *regmap_field; 77634db83bSCorentin Labbe bool internal_phy_powered; 78634db83bSCorentin Labbe void *mux_handle; 799f93ac8dSLABBE Corentin }; 809f93ac8dSLABBE Corentin 8125ae15fbSChen-Yu Tsai /* EMAC clock register @ 0x30 in the "system control" address range */ 8225ae15fbSChen-Yu Tsai static const struct reg_field sun8i_syscon_reg_field = { 8325ae15fbSChen-Yu Tsai .reg = 0x30, 8425ae15fbSChen-Yu Tsai .lsb = 0, 8525ae15fbSChen-Yu Tsai .msb = 31, 8625ae15fbSChen-Yu Tsai }; 8725ae15fbSChen-Yu Tsai 889f93ac8dSLABBE Corentin static const struct emac_variant emac_variant_h3 = { 899f93ac8dSLABBE Corentin .default_syscon_value = 0x58000, 9025ae15fbSChen-Yu Tsai .syscon_field = &sun8i_syscon_reg_field, 91634db83bSCorentin Labbe .soc_has_internal_phy = true, 929f93ac8dSLABBE Corentin .support_mii = true, 939f93ac8dSLABBE Corentin .support_rmii = true, 949f93ac8dSLABBE Corentin .support_rgmii = true 959f93ac8dSLABBE Corentin }; 969f93ac8dSLABBE Corentin 9757fde47dSIcenowy Zheng static const struct emac_variant emac_variant_v3s = { 9857fde47dSIcenowy Zheng .default_syscon_value = 0x38000, 9925ae15fbSChen-Yu Tsai .syscon_field = &sun8i_syscon_reg_field, 100634db83bSCorentin Labbe .soc_has_internal_phy = true, 10157fde47dSIcenowy Zheng .support_mii = true 10257fde47dSIcenowy Zheng }; 10357fde47dSIcenowy Zheng 1049f93ac8dSLABBE Corentin static const struct emac_variant emac_variant_a83t = { 1059f93ac8dSLABBE Corentin .default_syscon_value = 0, 10625ae15fbSChen-Yu Tsai .syscon_field = &sun8i_syscon_reg_field, 107634db83bSCorentin Labbe .soc_has_internal_phy = false, 1089f93ac8dSLABBE Corentin .support_mii = true, 1099f93ac8dSLABBE Corentin .support_rgmii = true 1109f93ac8dSLABBE Corentin }; 1119f93ac8dSLABBE Corentin 1129f93ac8dSLABBE Corentin static const struct emac_variant emac_variant_a64 = { 1139f93ac8dSLABBE Corentin .default_syscon_value = 0, 11425ae15fbSChen-Yu Tsai .syscon_field = &sun8i_syscon_reg_field, 115634db83bSCorentin Labbe .soc_has_internal_phy = false, 1169f93ac8dSLABBE Corentin .support_mii = true, 1179f93ac8dSLABBE Corentin .support_rmii = true, 1189f93ac8dSLABBE Corentin .support_rgmii = true 1199f93ac8dSLABBE Corentin }; 1209f93ac8dSLABBE Corentin 1219f93ac8dSLABBE Corentin #define EMAC_BASIC_CTL0 0x00 1229f93ac8dSLABBE Corentin #define EMAC_BASIC_CTL1 0x04 1239f93ac8dSLABBE Corentin #define EMAC_INT_STA 0x08 1249f93ac8dSLABBE Corentin #define EMAC_INT_EN 0x0C 1259f93ac8dSLABBE Corentin #define EMAC_TX_CTL0 0x10 1269f93ac8dSLABBE Corentin #define EMAC_TX_CTL1 0x14 1279f93ac8dSLABBE Corentin #define EMAC_TX_FLOW_CTL 0x1C 1289f93ac8dSLABBE Corentin #define EMAC_TX_DESC_LIST 0x20 1299f93ac8dSLABBE Corentin #define EMAC_RX_CTL0 0x24 1309f93ac8dSLABBE Corentin #define EMAC_RX_CTL1 0x28 1319f93ac8dSLABBE Corentin #define EMAC_RX_DESC_LIST 0x34 1329f93ac8dSLABBE Corentin #define EMAC_RX_FRM_FLT 0x38 1339f93ac8dSLABBE Corentin #define EMAC_MDIO_CMD 0x48 1349f93ac8dSLABBE Corentin #define EMAC_MDIO_DATA 0x4C 1359f93ac8dSLABBE Corentin #define EMAC_MACADDR_HI(reg) (0x50 + (reg) * 8) 1369f93ac8dSLABBE Corentin #define EMAC_MACADDR_LO(reg) (0x54 + (reg) * 8) 1379f93ac8dSLABBE Corentin #define EMAC_TX_DMA_STA 0xB0 1389f93ac8dSLABBE Corentin #define EMAC_TX_CUR_DESC 0xB4 1399f93ac8dSLABBE Corentin #define EMAC_TX_CUR_BUF 0xB8 1409f93ac8dSLABBE Corentin #define EMAC_RX_DMA_STA 0xC0 1419f93ac8dSLABBE Corentin #define EMAC_RX_CUR_DESC 0xC4 1429f93ac8dSLABBE Corentin #define EMAC_RX_CUR_BUF 0xC8 1439f93ac8dSLABBE Corentin 1449f93ac8dSLABBE Corentin /* Use in EMAC_BASIC_CTL0 */ 1459f93ac8dSLABBE Corentin #define EMAC_DUPLEX_FULL BIT(0) 1469f93ac8dSLABBE Corentin #define EMAC_LOOPBACK BIT(1) 1479f93ac8dSLABBE Corentin #define EMAC_SPEED_1000 0 1489f93ac8dSLABBE Corentin #define EMAC_SPEED_100 (0x03 << 2) 1499f93ac8dSLABBE Corentin #define EMAC_SPEED_10 (0x02 << 2) 1509f93ac8dSLABBE Corentin 1519f93ac8dSLABBE Corentin /* Use in EMAC_BASIC_CTL1 */ 1529f93ac8dSLABBE Corentin #define EMAC_BURSTLEN_SHIFT 24 1539f93ac8dSLABBE Corentin 1549f93ac8dSLABBE Corentin /* Used in EMAC_RX_FRM_FLT */ 1559f93ac8dSLABBE Corentin #define EMAC_FRM_FLT_RXALL BIT(0) 1569f93ac8dSLABBE Corentin #define EMAC_FRM_FLT_CTL BIT(13) 1579f93ac8dSLABBE Corentin #define EMAC_FRM_FLT_MULTICAST BIT(16) 1589f93ac8dSLABBE Corentin 1599f93ac8dSLABBE Corentin /* Used in RX_CTL1*/ 1609f93ac8dSLABBE Corentin #define EMAC_RX_MD BIT(1) 1619f93ac8dSLABBE Corentin #define EMAC_RX_TH_MASK GENMASK(4, 5) 1629f93ac8dSLABBE Corentin #define EMAC_RX_TH_32 0 1639f93ac8dSLABBE Corentin #define EMAC_RX_TH_64 (0x1 << 4) 1649f93ac8dSLABBE Corentin #define EMAC_RX_TH_96 (0x2 << 4) 1659f93ac8dSLABBE Corentin #define EMAC_RX_TH_128 (0x3 << 4) 1669f93ac8dSLABBE Corentin #define EMAC_RX_DMA_EN BIT(30) 1679f93ac8dSLABBE Corentin #define EMAC_RX_DMA_START BIT(31) 1689f93ac8dSLABBE Corentin 1699f93ac8dSLABBE Corentin /* Used in TX_CTL1*/ 1709f93ac8dSLABBE Corentin #define EMAC_TX_MD BIT(1) 1719f93ac8dSLABBE Corentin #define EMAC_TX_NEXT_FRM BIT(2) 1729f93ac8dSLABBE Corentin #define EMAC_TX_TH_MASK GENMASK(8, 10) 1739f93ac8dSLABBE Corentin #define EMAC_TX_TH_64 0 1749f93ac8dSLABBE Corentin #define EMAC_TX_TH_128 (0x1 << 8) 1759f93ac8dSLABBE Corentin #define EMAC_TX_TH_192 (0x2 << 8) 1769f93ac8dSLABBE Corentin #define EMAC_TX_TH_256 (0x3 << 8) 1779f93ac8dSLABBE Corentin #define EMAC_TX_DMA_EN BIT(30) 1789f93ac8dSLABBE Corentin #define EMAC_TX_DMA_START BIT(31) 1799f93ac8dSLABBE Corentin 1809f93ac8dSLABBE Corentin /* Used in RX_CTL0 */ 1819f93ac8dSLABBE Corentin #define EMAC_RX_RECEIVER_EN BIT(31) 1829f93ac8dSLABBE Corentin #define EMAC_RX_DO_CRC BIT(27) 1839f93ac8dSLABBE Corentin #define EMAC_RX_FLOW_CTL_EN BIT(16) 1849f93ac8dSLABBE Corentin 1859f93ac8dSLABBE Corentin /* Used in TX_CTL0 */ 1869f93ac8dSLABBE Corentin #define EMAC_TX_TRANSMITTER_EN BIT(31) 1879f93ac8dSLABBE Corentin 1889f93ac8dSLABBE Corentin /* Used in EMAC_TX_FLOW_CTL */ 1899f93ac8dSLABBE Corentin #define EMAC_TX_FLOW_CTL_EN BIT(0) 1909f93ac8dSLABBE Corentin 1919f93ac8dSLABBE Corentin /* Used in EMAC_INT_STA */ 1929f93ac8dSLABBE Corentin #define EMAC_TX_INT BIT(0) 1939f93ac8dSLABBE Corentin #define EMAC_TX_DMA_STOP_INT BIT(1) 1949f93ac8dSLABBE Corentin #define EMAC_TX_BUF_UA_INT BIT(2) 1959f93ac8dSLABBE Corentin #define EMAC_TX_TIMEOUT_INT BIT(3) 1969f93ac8dSLABBE Corentin #define EMAC_TX_UNDERFLOW_INT BIT(4) 1979f93ac8dSLABBE Corentin #define EMAC_TX_EARLY_INT BIT(5) 1989f93ac8dSLABBE Corentin #define EMAC_RX_INT BIT(8) 1999f93ac8dSLABBE Corentin #define EMAC_RX_BUF_UA_INT BIT(9) 2009f93ac8dSLABBE Corentin #define EMAC_RX_DMA_STOP_INT BIT(10) 2019f93ac8dSLABBE Corentin #define EMAC_RX_TIMEOUT_INT BIT(11) 2029f93ac8dSLABBE Corentin #define EMAC_RX_OVERFLOW_INT BIT(12) 2039f93ac8dSLABBE Corentin #define EMAC_RX_EARLY_INT BIT(13) 2049f93ac8dSLABBE Corentin #define EMAC_RGMII_STA_INT BIT(16) 2059f93ac8dSLABBE Corentin 2069f93ac8dSLABBE Corentin #define MAC_ADDR_TYPE_DST BIT(31) 2079f93ac8dSLABBE Corentin 2089f93ac8dSLABBE Corentin /* H3 specific bits for EPHY */ 2099f93ac8dSLABBE Corentin #define H3_EPHY_ADDR_SHIFT 20 2101450ba8aSIcenowy Zheng #define H3_EPHY_CLK_SEL BIT(18) /* 1: 24MHz, 0: 25MHz */ 2119f93ac8dSLABBE Corentin #define H3_EPHY_LED_POL BIT(17) /* 1: active low, 0: active high */ 2129f93ac8dSLABBE Corentin #define H3_EPHY_SHUTDOWN BIT(16) /* 1: shutdown, 0: power up */ 2139f93ac8dSLABBE Corentin #define H3_EPHY_SELECT BIT(15) /* 1: internal PHY, 0: external PHY */ 214634db83bSCorentin Labbe #define H3_EPHY_MUX_MASK (H3_EPHY_SHUTDOWN | H3_EPHY_SELECT) 215634db83bSCorentin Labbe #define DWMAC_SUN8I_MDIO_MUX_INTERNAL_ID 1 216634db83bSCorentin Labbe #define DWMAC_SUN8I_MDIO_MUX_EXTERNAL_ID 2 2179f93ac8dSLABBE Corentin 2189f93ac8dSLABBE Corentin /* H3/A64 specific bits */ 2199f93ac8dSLABBE Corentin #define SYSCON_RMII_EN BIT(13) /* 1: enable RMII (overrides EPIT) */ 2209f93ac8dSLABBE Corentin 2219f93ac8dSLABBE Corentin /* Generic system control EMAC_CLK bits */ 2229f93ac8dSLABBE Corentin #define SYSCON_ETXDC_MASK GENMASK(2, 0) 2239f93ac8dSLABBE Corentin #define SYSCON_ETXDC_SHIFT 10 2249f93ac8dSLABBE Corentin #define SYSCON_ERXDC_MASK GENMASK(4, 0) 2259f93ac8dSLABBE Corentin #define SYSCON_ERXDC_SHIFT 5 2269f93ac8dSLABBE Corentin /* EMAC PHY Interface Type */ 2279f93ac8dSLABBE Corentin #define SYSCON_EPIT BIT(2) /* 1: RGMII, 0: MII */ 2289f93ac8dSLABBE Corentin #define SYSCON_ETCS_MASK GENMASK(1, 0) 2299f93ac8dSLABBE Corentin #define SYSCON_ETCS_MII 0x0 2309f93ac8dSLABBE Corentin #define SYSCON_ETCS_EXT_GMII 0x1 2319f93ac8dSLABBE Corentin #define SYSCON_ETCS_INT_GMII 0x2 2329f93ac8dSLABBE Corentin 2339f93ac8dSLABBE Corentin /* sun8i_dwmac_dma_reset() - reset the EMAC 2349f93ac8dSLABBE Corentin * Called from stmmac via stmmac_dma_ops->reset 2359f93ac8dSLABBE Corentin */ 2369f93ac8dSLABBE Corentin static int sun8i_dwmac_dma_reset(void __iomem *ioaddr) 2379f93ac8dSLABBE Corentin { 2389f93ac8dSLABBE Corentin writel(0, ioaddr + EMAC_RX_CTL1); 2399f93ac8dSLABBE Corentin writel(0, ioaddr + EMAC_TX_CTL1); 2409f93ac8dSLABBE Corentin writel(0, ioaddr + EMAC_RX_FRM_FLT); 2419f93ac8dSLABBE Corentin writel(0, ioaddr + EMAC_RX_DESC_LIST); 2429f93ac8dSLABBE Corentin writel(0, ioaddr + EMAC_TX_DESC_LIST); 2439f93ac8dSLABBE Corentin writel(0, ioaddr + EMAC_INT_EN); 2449f93ac8dSLABBE Corentin writel(0x1FFFFFF, ioaddr + EMAC_INT_STA); 2459f93ac8dSLABBE Corentin return 0; 2469f93ac8dSLABBE Corentin } 2479f93ac8dSLABBE Corentin 2489f93ac8dSLABBE Corentin /* sun8i_dwmac_dma_init() - initialize the EMAC 2499f93ac8dSLABBE Corentin * Called from stmmac via stmmac_dma_ops->init 2509f93ac8dSLABBE Corentin */ 2519f93ac8dSLABBE Corentin static void sun8i_dwmac_dma_init(void __iomem *ioaddr, 2529f93ac8dSLABBE Corentin struct stmmac_dma_cfg *dma_cfg, 2539f93ac8dSLABBE Corentin u32 dma_tx, u32 dma_rx, int atds) 2549f93ac8dSLABBE Corentin { 2559f93ac8dSLABBE Corentin /* Write TX and RX descriptors address */ 2569f93ac8dSLABBE Corentin writel(dma_rx, ioaddr + EMAC_RX_DESC_LIST); 2579f93ac8dSLABBE Corentin writel(dma_tx, ioaddr + EMAC_TX_DESC_LIST); 2589f93ac8dSLABBE Corentin 2599f93ac8dSLABBE Corentin writel(EMAC_RX_INT | EMAC_TX_INT, ioaddr + EMAC_INT_EN); 2609f93ac8dSLABBE Corentin writel(0x1FFFFFF, ioaddr + EMAC_INT_STA); 2619f93ac8dSLABBE Corentin } 2629f93ac8dSLABBE Corentin 2639f93ac8dSLABBE Corentin /* sun8i_dwmac_dump_regs() - Dump EMAC address space 2649f93ac8dSLABBE Corentin * Called from stmmac_dma_ops->dump_regs 2659f93ac8dSLABBE Corentin * Used for ethtool 2669f93ac8dSLABBE Corentin */ 2679f93ac8dSLABBE Corentin static void sun8i_dwmac_dump_regs(void __iomem *ioaddr, u32 *reg_space) 2689f93ac8dSLABBE Corentin { 2699f93ac8dSLABBE Corentin int i; 2709f93ac8dSLABBE Corentin 2719f93ac8dSLABBE Corentin for (i = 0; i < 0xC8; i += 4) { 2729f93ac8dSLABBE Corentin if (i == 0x32 || i == 0x3C) 2739f93ac8dSLABBE Corentin continue; 2749f93ac8dSLABBE Corentin reg_space[i / 4] = readl(ioaddr + i); 2759f93ac8dSLABBE Corentin } 2769f93ac8dSLABBE Corentin } 2779f93ac8dSLABBE Corentin 2789f93ac8dSLABBE Corentin /* sun8i_dwmac_dump_mac_regs() - Dump EMAC address space 2799f93ac8dSLABBE Corentin * Called from stmmac_ops->dump_regs 2809f93ac8dSLABBE Corentin * Used for ethtool 2819f93ac8dSLABBE Corentin */ 2829f93ac8dSLABBE Corentin static void sun8i_dwmac_dump_mac_regs(struct mac_device_info *hw, 2839f93ac8dSLABBE Corentin u32 *reg_space) 2849f93ac8dSLABBE Corentin { 2859f93ac8dSLABBE Corentin int i; 2869f93ac8dSLABBE Corentin void __iomem *ioaddr = hw->pcsr; 2879f93ac8dSLABBE Corentin 2889f93ac8dSLABBE Corentin for (i = 0; i < 0xC8; i += 4) { 2899f93ac8dSLABBE Corentin if (i == 0x32 || i == 0x3C) 2909f93ac8dSLABBE Corentin continue; 2919f93ac8dSLABBE Corentin reg_space[i / 4] = readl(ioaddr + i); 2929f93ac8dSLABBE Corentin } 2939f93ac8dSLABBE Corentin } 2949f93ac8dSLABBE Corentin 2959f93ac8dSLABBE Corentin static void sun8i_dwmac_enable_dma_irq(void __iomem *ioaddr, u32 chan) 2969f93ac8dSLABBE Corentin { 2979f93ac8dSLABBE Corentin writel(EMAC_RX_INT | EMAC_TX_INT, ioaddr + EMAC_INT_EN); 2989f93ac8dSLABBE Corentin } 2999f93ac8dSLABBE Corentin 3009f93ac8dSLABBE Corentin static void sun8i_dwmac_disable_dma_irq(void __iomem *ioaddr, u32 chan) 3019f93ac8dSLABBE Corentin { 3029f93ac8dSLABBE Corentin writel(0, ioaddr + EMAC_INT_EN); 3039f93ac8dSLABBE Corentin } 3049f93ac8dSLABBE Corentin 3059f93ac8dSLABBE Corentin static void sun8i_dwmac_dma_start_tx(void __iomem *ioaddr, u32 chan) 3069f93ac8dSLABBE Corentin { 3079f93ac8dSLABBE Corentin u32 v; 3089f93ac8dSLABBE Corentin 3099f93ac8dSLABBE Corentin v = readl(ioaddr + EMAC_TX_CTL1); 3109f93ac8dSLABBE Corentin v |= EMAC_TX_DMA_START; 3119f93ac8dSLABBE Corentin v |= EMAC_TX_DMA_EN; 3129f93ac8dSLABBE Corentin writel(v, ioaddr + EMAC_TX_CTL1); 3139f93ac8dSLABBE Corentin } 3149f93ac8dSLABBE Corentin 3159f93ac8dSLABBE Corentin static void sun8i_dwmac_enable_dma_transmission(void __iomem *ioaddr) 3169f93ac8dSLABBE Corentin { 3179f93ac8dSLABBE Corentin u32 v; 3189f93ac8dSLABBE Corentin 3199f93ac8dSLABBE Corentin v = readl(ioaddr + EMAC_TX_CTL1); 3209f93ac8dSLABBE Corentin v |= EMAC_TX_DMA_START; 3219f93ac8dSLABBE Corentin v |= EMAC_TX_DMA_EN; 3229f93ac8dSLABBE Corentin writel(v, ioaddr + EMAC_TX_CTL1); 3239f93ac8dSLABBE Corentin } 3249f93ac8dSLABBE Corentin 3259f93ac8dSLABBE Corentin static void sun8i_dwmac_dma_stop_tx(void __iomem *ioaddr, u32 chan) 3269f93ac8dSLABBE Corentin { 3279f93ac8dSLABBE Corentin u32 v; 3289f93ac8dSLABBE Corentin 3299f93ac8dSLABBE Corentin v = readl(ioaddr + EMAC_TX_CTL1); 3309f93ac8dSLABBE Corentin v &= ~EMAC_TX_DMA_EN; 3319f93ac8dSLABBE Corentin writel(v, ioaddr + EMAC_TX_CTL1); 3329f93ac8dSLABBE Corentin } 3339f93ac8dSLABBE Corentin 3349f93ac8dSLABBE Corentin static void sun8i_dwmac_dma_start_rx(void __iomem *ioaddr, u32 chan) 3359f93ac8dSLABBE Corentin { 3369f93ac8dSLABBE Corentin u32 v; 3379f93ac8dSLABBE Corentin 3389f93ac8dSLABBE Corentin v = readl(ioaddr + EMAC_RX_CTL1); 3399f93ac8dSLABBE Corentin v |= EMAC_RX_DMA_START; 3409f93ac8dSLABBE Corentin v |= EMAC_RX_DMA_EN; 3419f93ac8dSLABBE Corentin writel(v, ioaddr + EMAC_RX_CTL1); 3429f93ac8dSLABBE Corentin } 3439f93ac8dSLABBE Corentin 3449f93ac8dSLABBE Corentin static void sun8i_dwmac_dma_stop_rx(void __iomem *ioaddr, u32 chan) 3459f93ac8dSLABBE Corentin { 3469f93ac8dSLABBE Corentin u32 v; 3479f93ac8dSLABBE Corentin 3489f93ac8dSLABBE Corentin v = readl(ioaddr + EMAC_RX_CTL1); 3499f93ac8dSLABBE Corentin v &= ~EMAC_RX_DMA_EN; 3509f93ac8dSLABBE Corentin writel(v, ioaddr + EMAC_RX_CTL1); 3519f93ac8dSLABBE Corentin } 3529f93ac8dSLABBE Corentin 3539f93ac8dSLABBE Corentin static int sun8i_dwmac_dma_interrupt(void __iomem *ioaddr, 3549f93ac8dSLABBE Corentin struct stmmac_extra_stats *x, u32 chan) 3559f93ac8dSLABBE Corentin { 3569f93ac8dSLABBE Corentin u32 v; 3579f93ac8dSLABBE Corentin int ret = 0; 3589f93ac8dSLABBE Corentin 3599f93ac8dSLABBE Corentin v = readl(ioaddr + EMAC_INT_STA); 3609f93ac8dSLABBE Corentin 3619f93ac8dSLABBE Corentin if (v & EMAC_TX_INT) { 3629f93ac8dSLABBE Corentin ret |= handle_tx; 3639f93ac8dSLABBE Corentin x->tx_normal_irq_n++; 3649f93ac8dSLABBE Corentin } 3659f93ac8dSLABBE Corentin 3669f93ac8dSLABBE Corentin if (v & EMAC_TX_DMA_STOP_INT) 3679f93ac8dSLABBE Corentin x->tx_process_stopped_irq++; 3689f93ac8dSLABBE Corentin 3699f93ac8dSLABBE Corentin if (v & EMAC_TX_BUF_UA_INT) 3709f93ac8dSLABBE Corentin x->tx_process_stopped_irq++; 3719f93ac8dSLABBE Corentin 3729f93ac8dSLABBE Corentin if (v & EMAC_TX_TIMEOUT_INT) 3739f93ac8dSLABBE Corentin ret |= tx_hard_error; 3749f93ac8dSLABBE Corentin 3759f93ac8dSLABBE Corentin if (v & EMAC_TX_UNDERFLOW_INT) { 3769f93ac8dSLABBE Corentin ret |= tx_hard_error; 3779f93ac8dSLABBE Corentin x->tx_undeflow_irq++; 3789f93ac8dSLABBE Corentin } 3799f93ac8dSLABBE Corentin 3809f93ac8dSLABBE Corentin if (v & EMAC_TX_EARLY_INT) 3819f93ac8dSLABBE Corentin x->tx_early_irq++; 3829f93ac8dSLABBE Corentin 3839f93ac8dSLABBE Corentin if (v & EMAC_RX_INT) { 3849f93ac8dSLABBE Corentin ret |= handle_rx; 3859f93ac8dSLABBE Corentin x->rx_normal_irq_n++; 3869f93ac8dSLABBE Corentin } 3879f93ac8dSLABBE Corentin 3889f93ac8dSLABBE Corentin if (v & EMAC_RX_BUF_UA_INT) 3899f93ac8dSLABBE Corentin x->rx_buf_unav_irq++; 3909f93ac8dSLABBE Corentin 3919f93ac8dSLABBE Corentin if (v & EMAC_RX_DMA_STOP_INT) 3929f93ac8dSLABBE Corentin x->rx_process_stopped_irq++; 3939f93ac8dSLABBE Corentin 3949f93ac8dSLABBE Corentin if (v & EMAC_RX_TIMEOUT_INT) 3959f93ac8dSLABBE Corentin ret |= tx_hard_error; 3969f93ac8dSLABBE Corentin 3979f93ac8dSLABBE Corentin if (v & EMAC_RX_OVERFLOW_INT) { 3989f93ac8dSLABBE Corentin ret |= tx_hard_error; 3999f93ac8dSLABBE Corentin x->rx_overflow_irq++; 4009f93ac8dSLABBE Corentin } 4019f93ac8dSLABBE Corentin 4029f93ac8dSLABBE Corentin if (v & EMAC_RX_EARLY_INT) 4039f93ac8dSLABBE Corentin x->rx_early_irq++; 4049f93ac8dSLABBE Corentin 4059f93ac8dSLABBE Corentin if (v & EMAC_RGMII_STA_INT) 4069f93ac8dSLABBE Corentin x->irq_rgmii_n++; 4079f93ac8dSLABBE Corentin 4089f93ac8dSLABBE Corentin writel(v, ioaddr + EMAC_INT_STA); 4099f93ac8dSLABBE Corentin 4109f93ac8dSLABBE Corentin return ret; 4119f93ac8dSLABBE Corentin } 4129f93ac8dSLABBE Corentin 4139f93ac8dSLABBE Corentin static void sun8i_dwmac_dma_operation_mode(void __iomem *ioaddr, int txmode, 4149f93ac8dSLABBE Corentin int rxmode, int rxfifosz) 4159f93ac8dSLABBE Corentin { 4169f93ac8dSLABBE Corentin u32 v; 4179f93ac8dSLABBE Corentin 4189f93ac8dSLABBE Corentin v = readl(ioaddr + EMAC_TX_CTL1); 4199f93ac8dSLABBE Corentin if (txmode == SF_DMA_MODE) { 4209f93ac8dSLABBE Corentin v |= EMAC_TX_MD; 4219f93ac8dSLABBE Corentin /* Undocumented bit (called TX_NEXT_FRM in BSP), the original 4229f93ac8dSLABBE Corentin * comment is 4239f93ac8dSLABBE Corentin * "Operating on second frame increase the performance 4249f93ac8dSLABBE Corentin * especially when transmit store-and-forward is used." 4259f93ac8dSLABBE Corentin */ 4269f93ac8dSLABBE Corentin v |= EMAC_TX_NEXT_FRM; 4279f93ac8dSLABBE Corentin } else { 4289f93ac8dSLABBE Corentin v &= ~EMAC_TX_MD; 4299f93ac8dSLABBE Corentin v &= ~EMAC_TX_TH_MASK; 4309f93ac8dSLABBE Corentin if (txmode < 64) 4319f93ac8dSLABBE Corentin v |= EMAC_TX_TH_64; 4329f93ac8dSLABBE Corentin else if (txmode < 128) 4339f93ac8dSLABBE Corentin v |= EMAC_TX_TH_128; 4349f93ac8dSLABBE Corentin else if (txmode < 192) 4359f93ac8dSLABBE Corentin v |= EMAC_TX_TH_192; 4369f93ac8dSLABBE Corentin else if (txmode < 256) 4379f93ac8dSLABBE Corentin v |= EMAC_TX_TH_256; 4389f93ac8dSLABBE Corentin } 4399f93ac8dSLABBE Corentin writel(v, ioaddr + EMAC_TX_CTL1); 4409f93ac8dSLABBE Corentin 4419f93ac8dSLABBE Corentin v = readl(ioaddr + EMAC_RX_CTL1); 4429f93ac8dSLABBE Corentin if (rxmode == SF_DMA_MODE) { 4439f93ac8dSLABBE Corentin v |= EMAC_RX_MD; 4449f93ac8dSLABBE Corentin } else { 4459f93ac8dSLABBE Corentin v &= ~EMAC_RX_MD; 4469f93ac8dSLABBE Corentin v &= ~EMAC_RX_TH_MASK; 4479f93ac8dSLABBE Corentin if (rxmode < 32) 4489f93ac8dSLABBE Corentin v |= EMAC_RX_TH_32; 4499f93ac8dSLABBE Corentin else if (rxmode < 64) 4509f93ac8dSLABBE Corentin v |= EMAC_RX_TH_64; 4519f93ac8dSLABBE Corentin else if (rxmode < 96) 4529f93ac8dSLABBE Corentin v |= EMAC_RX_TH_96; 4539f93ac8dSLABBE Corentin else if (rxmode < 128) 4549f93ac8dSLABBE Corentin v |= EMAC_RX_TH_128; 4559f93ac8dSLABBE Corentin } 4569f93ac8dSLABBE Corentin writel(v, ioaddr + EMAC_RX_CTL1); 4579f93ac8dSLABBE Corentin } 4589f93ac8dSLABBE Corentin 4599f93ac8dSLABBE Corentin static const struct stmmac_dma_ops sun8i_dwmac_dma_ops = { 4609f93ac8dSLABBE Corentin .reset = sun8i_dwmac_dma_reset, 4619f93ac8dSLABBE Corentin .init = sun8i_dwmac_dma_init, 4629f93ac8dSLABBE Corentin .dump_regs = sun8i_dwmac_dump_regs, 4639f93ac8dSLABBE Corentin .dma_mode = sun8i_dwmac_dma_operation_mode, 4649f93ac8dSLABBE Corentin .enable_dma_transmission = sun8i_dwmac_enable_dma_transmission, 4659f93ac8dSLABBE Corentin .enable_dma_irq = sun8i_dwmac_enable_dma_irq, 4669f93ac8dSLABBE Corentin .disable_dma_irq = sun8i_dwmac_disable_dma_irq, 4679f93ac8dSLABBE Corentin .start_tx = sun8i_dwmac_dma_start_tx, 4689f93ac8dSLABBE Corentin .stop_tx = sun8i_dwmac_dma_stop_tx, 4699f93ac8dSLABBE Corentin .start_rx = sun8i_dwmac_dma_start_rx, 4709f93ac8dSLABBE Corentin .stop_rx = sun8i_dwmac_dma_stop_rx, 4719f93ac8dSLABBE Corentin .dma_interrupt = sun8i_dwmac_dma_interrupt, 4729f93ac8dSLABBE Corentin }; 4739f93ac8dSLABBE Corentin 4749f93ac8dSLABBE Corentin static int sun8i_dwmac_init(struct platform_device *pdev, void *priv) 4759f93ac8dSLABBE Corentin { 4769f93ac8dSLABBE Corentin struct sunxi_priv_data *gmac = priv; 4779f93ac8dSLABBE Corentin int ret; 4789f93ac8dSLABBE Corentin 4799f93ac8dSLABBE Corentin if (gmac->regulator) { 4809f93ac8dSLABBE Corentin ret = regulator_enable(gmac->regulator); 4819f93ac8dSLABBE Corentin if (ret) { 4829f93ac8dSLABBE Corentin dev_err(&pdev->dev, "Fail to enable regulator\n"); 4839f93ac8dSLABBE Corentin return ret; 4849f93ac8dSLABBE Corentin } 4859f93ac8dSLABBE Corentin } 4869f93ac8dSLABBE Corentin 4879f93ac8dSLABBE Corentin ret = clk_prepare_enable(gmac->tx_clk); 4889f93ac8dSLABBE Corentin if (ret) { 4899f93ac8dSLABBE Corentin if (gmac->regulator) 4909f93ac8dSLABBE Corentin regulator_disable(gmac->regulator); 4919f93ac8dSLABBE Corentin dev_err(&pdev->dev, "Could not enable AHB clock\n"); 4929f93ac8dSLABBE Corentin return ret; 4939f93ac8dSLABBE Corentin } 4949f93ac8dSLABBE Corentin 4959f93ac8dSLABBE Corentin return 0; 4969f93ac8dSLABBE Corentin } 4979f93ac8dSLABBE Corentin 4988cad443eSFlorian Fainelli static void sun8i_dwmac_core_init(struct mac_device_info *hw, 4998cad443eSFlorian Fainelli struct net_device *dev) 5009f93ac8dSLABBE Corentin { 5019f93ac8dSLABBE Corentin void __iomem *ioaddr = hw->pcsr; 5029f93ac8dSLABBE Corentin u32 v; 5039f93ac8dSLABBE Corentin 5049f93ac8dSLABBE Corentin v = (8 << EMAC_BURSTLEN_SHIFT); /* burst len */ 5059f93ac8dSLABBE Corentin writel(v, ioaddr + EMAC_BASIC_CTL1); 5069f93ac8dSLABBE Corentin } 5079f93ac8dSLABBE Corentin 5089f93ac8dSLABBE Corentin static void sun8i_dwmac_set_mac(void __iomem *ioaddr, bool enable) 5099f93ac8dSLABBE Corentin { 5109f93ac8dSLABBE Corentin u32 t, r; 5119f93ac8dSLABBE Corentin 5129f93ac8dSLABBE Corentin t = readl(ioaddr + EMAC_TX_CTL0); 5139f93ac8dSLABBE Corentin r = readl(ioaddr + EMAC_RX_CTL0); 5149f93ac8dSLABBE Corentin if (enable) { 5159f93ac8dSLABBE Corentin t |= EMAC_TX_TRANSMITTER_EN; 5169f93ac8dSLABBE Corentin r |= EMAC_RX_RECEIVER_EN; 5179f93ac8dSLABBE Corentin } else { 5189f93ac8dSLABBE Corentin t &= ~EMAC_TX_TRANSMITTER_EN; 5199f93ac8dSLABBE Corentin r &= ~EMAC_RX_RECEIVER_EN; 5209f93ac8dSLABBE Corentin } 5219f93ac8dSLABBE Corentin writel(t, ioaddr + EMAC_TX_CTL0); 5229f93ac8dSLABBE Corentin writel(r, ioaddr + EMAC_RX_CTL0); 5239f93ac8dSLABBE Corentin } 5249f93ac8dSLABBE Corentin 5259f93ac8dSLABBE Corentin /* Set MAC address at slot reg_n 5269f93ac8dSLABBE Corentin * All slot > 0 need to be enabled with MAC_ADDR_TYPE_DST 5279f93ac8dSLABBE Corentin * If addr is NULL, clear the slot 5289f93ac8dSLABBE Corentin */ 5299f93ac8dSLABBE Corentin static void sun8i_dwmac_set_umac_addr(struct mac_device_info *hw, 5309f93ac8dSLABBE Corentin unsigned char *addr, 5319f93ac8dSLABBE Corentin unsigned int reg_n) 5329f93ac8dSLABBE Corentin { 5339f93ac8dSLABBE Corentin void __iomem *ioaddr = hw->pcsr; 5349f93ac8dSLABBE Corentin u32 v; 5359f93ac8dSLABBE Corentin 5369f93ac8dSLABBE Corentin if (!addr) { 5379f93ac8dSLABBE Corentin writel(0, ioaddr + EMAC_MACADDR_HI(reg_n)); 5389f93ac8dSLABBE Corentin return; 5399f93ac8dSLABBE Corentin } 5409f93ac8dSLABBE Corentin 5419f93ac8dSLABBE Corentin stmmac_set_mac_addr(ioaddr, addr, EMAC_MACADDR_HI(reg_n), 5429f93ac8dSLABBE Corentin EMAC_MACADDR_LO(reg_n)); 5439f93ac8dSLABBE Corentin if (reg_n > 0) { 5449f93ac8dSLABBE Corentin v = readl(ioaddr + EMAC_MACADDR_HI(reg_n)); 5459f93ac8dSLABBE Corentin v |= MAC_ADDR_TYPE_DST; 5469f93ac8dSLABBE Corentin writel(v, ioaddr + EMAC_MACADDR_HI(reg_n)); 5479f93ac8dSLABBE Corentin } 5489f93ac8dSLABBE Corentin } 5499f93ac8dSLABBE Corentin 5509f93ac8dSLABBE Corentin static void sun8i_dwmac_get_umac_addr(struct mac_device_info *hw, 5519f93ac8dSLABBE Corentin unsigned char *addr, 5529f93ac8dSLABBE Corentin unsigned int reg_n) 5539f93ac8dSLABBE Corentin { 5549f93ac8dSLABBE Corentin void __iomem *ioaddr = hw->pcsr; 5559f93ac8dSLABBE Corentin 5569f93ac8dSLABBE Corentin stmmac_get_mac_addr(ioaddr, addr, EMAC_MACADDR_HI(reg_n), 5579f93ac8dSLABBE Corentin EMAC_MACADDR_LO(reg_n)); 5589f93ac8dSLABBE Corentin } 5599f93ac8dSLABBE Corentin 5609f93ac8dSLABBE Corentin /* caution this function must return non 0 to work */ 5619f93ac8dSLABBE Corentin static int sun8i_dwmac_rx_ipc_enable(struct mac_device_info *hw) 5629f93ac8dSLABBE Corentin { 5639f93ac8dSLABBE Corentin void __iomem *ioaddr = hw->pcsr; 5649f93ac8dSLABBE Corentin u32 v; 5659f93ac8dSLABBE Corentin 5669f93ac8dSLABBE Corentin v = readl(ioaddr + EMAC_RX_CTL0); 5679f93ac8dSLABBE Corentin v |= EMAC_RX_DO_CRC; 5689f93ac8dSLABBE Corentin writel(v, ioaddr + EMAC_RX_CTL0); 5699f93ac8dSLABBE Corentin 5709f93ac8dSLABBE Corentin return 1; 5719f93ac8dSLABBE Corentin } 5729f93ac8dSLABBE Corentin 5739f93ac8dSLABBE Corentin static void sun8i_dwmac_set_filter(struct mac_device_info *hw, 5749f93ac8dSLABBE Corentin struct net_device *dev) 5759f93ac8dSLABBE Corentin { 5769f93ac8dSLABBE Corentin void __iomem *ioaddr = hw->pcsr; 5779f93ac8dSLABBE Corentin u32 v; 5789f93ac8dSLABBE Corentin int i = 1; 5799f93ac8dSLABBE Corentin struct netdev_hw_addr *ha; 5809f93ac8dSLABBE Corentin int macaddrs = netdev_uc_count(dev) + netdev_mc_count(dev) + 1; 5819f93ac8dSLABBE Corentin 5829f93ac8dSLABBE Corentin v = EMAC_FRM_FLT_CTL; 5839f93ac8dSLABBE Corentin 5849f93ac8dSLABBE Corentin if (dev->flags & IFF_PROMISC) { 5859f93ac8dSLABBE Corentin v = EMAC_FRM_FLT_RXALL; 5869f93ac8dSLABBE Corentin } else if (dev->flags & IFF_ALLMULTI) { 5879f93ac8dSLABBE Corentin v |= EMAC_FRM_FLT_MULTICAST; 5889f93ac8dSLABBE Corentin } else if (macaddrs <= hw->unicast_filter_entries) { 5899f93ac8dSLABBE Corentin if (!netdev_mc_empty(dev)) { 5909f93ac8dSLABBE Corentin netdev_for_each_mc_addr(ha, dev) { 5919f93ac8dSLABBE Corentin sun8i_dwmac_set_umac_addr(hw, ha->addr, i); 5929f93ac8dSLABBE Corentin i++; 5939f93ac8dSLABBE Corentin } 5949f93ac8dSLABBE Corentin } 5959f93ac8dSLABBE Corentin if (!netdev_uc_empty(dev)) { 5969f93ac8dSLABBE Corentin netdev_for_each_uc_addr(ha, dev) { 5979f93ac8dSLABBE Corentin sun8i_dwmac_set_umac_addr(hw, ha->addr, i); 5989f93ac8dSLABBE Corentin i++; 5999f93ac8dSLABBE Corentin } 6009f93ac8dSLABBE Corentin } 6019f93ac8dSLABBE Corentin } else { 6029f93ac8dSLABBE Corentin netdev_info(dev, "Too many address, switching to promiscuous\n"); 6039f93ac8dSLABBE Corentin v = EMAC_FRM_FLT_RXALL; 6049f93ac8dSLABBE Corentin } 6059f93ac8dSLABBE Corentin 6069f93ac8dSLABBE Corentin /* Disable unused address filter slots */ 6079f93ac8dSLABBE Corentin while (i < hw->unicast_filter_entries) 6089f93ac8dSLABBE Corentin sun8i_dwmac_set_umac_addr(hw, NULL, i++); 6099f93ac8dSLABBE Corentin 6109f93ac8dSLABBE Corentin writel(v, ioaddr + EMAC_RX_FRM_FLT); 6119f93ac8dSLABBE Corentin } 6129f93ac8dSLABBE Corentin 6139f93ac8dSLABBE Corentin static void sun8i_dwmac_flow_ctrl(struct mac_device_info *hw, 6149f93ac8dSLABBE Corentin unsigned int duplex, unsigned int fc, 6159f93ac8dSLABBE Corentin unsigned int pause_time, u32 tx_cnt) 6169f93ac8dSLABBE Corentin { 6179f93ac8dSLABBE Corentin void __iomem *ioaddr = hw->pcsr; 6189f93ac8dSLABBE Corentin u32 v; 6199f93ac8dSLABBE Corentin 6209f93ac8dSLABBE Corentin v = readl(ioaddr + EMAC_RX_CTL0); 6219f93ac8dSLABBE Corentin if (fc == FLOW_AUTO) 6229f93ac8dSLABBE Corentin v |= EMAC_RX_FLOW_CTL_EN; 6239f93ac8dSLABBE Corentin else 6249f93ac8dSLABBE Corentin v &= ~EMAC_RX_FLOW_CTL_EN; 6259f93ac8dSLABBE Corentin writel(v, ioaddr + EMAC_RX_CTL0); 6269f93ac8dSLABBE Corentin 6279f93ac8dSLABBE Corentin v = readl(ioaddr + EMAC_TX_FLOW_CTL); 6289f93ac8dSLABBE Corentin if (fc == FLOW_AUTO) 6299f93ac8dSLABBE Corentin v |= EMAC_TX_FLOW_CTL_EN; 6309f93ac8dSLABBE Corentin else 6319f93ac8dSLABBE Corentin v &= ~EMAC_TX_FLOW_CTL_EN; 6329f93ac8dSLABBE Corentin writel(v, ioaddr + EMAC_TX_FLOW_CTL); 6339f93ac8dSLABBE Corentin } 6349f93ac8dSLABBE Corentin 6359f93ac8dSLABBE Corentin static int sun8i_dwmac_reset(struct stmmac_priv *priv) 6369f93ac8dSLABBE Corentin { 6379f93ac8dSLABBE Corentin u32 v; 6389f93ac8dSLABBE Corentin int err; 6399f93ac8dSLABBE Corentin 6409f93ac8dSLABBE Corentin v = readl(priv->ioaddr + EMAC_BASIC_CTL1); 6419f93ac8dSLABBE Corentin writel(v | 0x01, priv->ioaddr + EMAC_BASIC_CTL1); 6429f93ac8dSLABBE Corentin 6439f93ac8dSLABBE Corentin /* The timeout was previoulsy set to 10ms, but some board (OrangePI0) 6449f93ac8dSLABBE Corentin * need more if no cable plugged. 100ms seems OK 6459f93ac8dSLABBE Corentin */ 6469f93ac8dSLABBE Corentin err = readl_poll_timeout(priv->ioaddr + EMAC_BASIC_CTL1, v, 6479f93ac8dSLABBE Corentin !(v & 0x01), 100, 100000); 6489f93ac8dSLABBE Corentin 6499f93ac8dSLABBE Corentin if (err) { 6509f93ac8dSLABBE Corentin dev_err(priv->device, "EMAC reset timeout\n"); 6519f93ac8dSLABBE Corentin return -EFAULT; 6529f93ac8dSLABBE Corentin } 6539f93ac8dSLABBE Corentin return 0; 6549f93ac8dSLABBE Corentin } 6559f93ac8dSLABBE Corentin 656634db83bSCorentin Labbe /* Search in mdio-mux node for internal PHY node and get its clk/reset */ 657634db83bSCorentin Labbe static int get_ephy_nodes(struct stmmac_priv *priv) 658634db83bSCorentin Labbe { 659634db83bSCorentin Labbe struct sunxi_priv_data *gmac = priv->plat->bsp_priv; 660634db83bSCorentin Labbe struct device_node *mdio_mux, *iphynode; 661634db83bSCorentin Labbe struct device_node *mdio_internal; 662634db83bSCorentin Labbe int ret; 663634db83bSCorentin Labbe 664634db83bSCorentin Labbe mdio_mux = of_get_child_by_name(priv->device->of_node, "mdio-mux"); 665634db83bSCorentin Labbe if (!mdio_mux) { 666634db83bSCorentin Labbe dev_err(priv->device, "Cannot get mdio-mux node\n"); 667634db83bSCorentin Labbe return -ENODEV; 668634db83bSCorentin Labbe } 669634db83bSCorentin Labbe 670634db83bSCorentin Labbe mdio_internal = of_find_compatible_node(mdio_mux, NULL, 671634db83bSCorentin Labbe "allwinner,sun8i-h3-mdio-internal"); 672634db83bSCorentin Labbe if (!mdio_internal) { 673634db83bSCorentin Labbe dev_err(priv->device, "Cannot get internal_mdio node\n"); 674634db83bSCorentin Labbe return -ENODEV; 675634db83bSCorentin Labbe } 676634db83bSCorentin Labbe 677634db83bSCorentin Labbe /* Seek for internal PHY */ 678634db83bSCorentin Labbe for_each_child_of_node(mdio_internal, iphynode) { 679634db83bSCorentin Labbe gmac->ephy_clk = of_clk_get(iphynode, 0); 680634db83bSCorentin Labbe if (IS_ERR(gmac->ephy_clk)) 681634db83bSCorentin Labbe continue; 682634db83bSCorentin Labbe gmac->rst_ephy = of_reset_control_get_exclusive(iphynode, NULL); 683634db83bSCorentin Labbe if (IS_ERR(gmac->rst_ephy)) { 684634db83bSCorentin Labbe ret = PTR_ERR(gmac->rst_ephy); 685634db83bSCorentin Labbe if (ret == -EPROBE_DEFER) 686634db83bSCorentin Labbe return ret; 687634db83bSCorentin Labbe continue; 688634db83bSCorentin Labbe } 689634db83bSCorentin Labbe dev_info(priv->device, "Found internal PHY node\n"); 690634db83bSCorentin Labbe return 0; 691634db83bSCorentin Labbe } 692634db83bSCorentin Labbe return -ENODEV; 693634db83bSCorentin Labbe } 694634db83bSCorentin Labbe 695634db83bSCorentin Labbe static int sun8i_dwmac_power_internal_phy(struct stmmac_priv *priv) 696634db83bSCorentin Labbe { 697634db83bSCorentin Labbe struct sunxi_priv_data *gmac = priv->plat->bsp_priv; 698634db83bSCorentin Labbe int ret; 699634db83bSCorentin Labbe 700634db83bSCorentin Labbe if (gmac->internal_phy_powered) { 701634db83bSCorentin Labbe dev_warn(priv->device, "Internal PHY already powered\n"); 702634db83bSCorentin Labbe return 0; 703634db83bSCorentin Labbe } 704634db83bSCorentin Labbe 705634db83bSCorentin Labbe dev_info(priv->device, "Powering internal PHY\n"); 706634db83bSCorentin Labbe ret = clk_prepare_enable(gmac->ephy_clk); 707634db83bSCorentin Labbe if (ret) { 708634db83bSCorentin Labbe dev_err(priv->device, "Cannot enable internal PHY\n"); 709634db83bSCorentin Labbe return ret; 710634db83bSCorentin Labbe } 711634db83bSCorentin Labbe 712634db83bSCorentin Labbe /* Make sure the EPHY is properly reseted, as U-Boot may leave 713634db83bSCorentin Labbe * it at deasserted state, and thus it may fail to reset EMAC. 714634db83bSCorentin Labbe */ 715634db83bSCorentin Labbe reset_control_assert(gmac->rst_ephy); 716634db83bSCorentin Labbe 717634db83bSCorentin Labbe ret = reset_control_deassert(gmac->rst_ephy); 718634db83bSCorentin Labbe if (ret) { 719634db83bSCorentin Labbe dev_err(priv->device, "Cannot deassert internal phy\n"); 720634db83bSCorentin Labbe clk_disable_unprepare(gmac->ephy_clk); 721634db83bSCorentin Labbe return ret; 722634db83bSCorentin Labbe } 723634db83bSCorentin Labbe 724634db83bSCorentin Labbe gmac->internal_phy_powered = true; 725634db83bSCorentin Labbe 726634db83bSCorentin Labbe return 0; 727634db83bSCorentin Labbe } 728634db83bSCorentin Labbe 729634db83bSCorentin Labbe static int sun8i_dwmac_unpower_internal_phy(struct sunxi_priv_data *gmac) 730634db83bSCorentin Labbe { 731634db83bSCorentin Labbe if (!gmac->internal_phy_powered) 732634db83bSCorentin Labbe return 0; 733634db83bSCorentin Labbe 734634db83bSCorentin Labbe clk_disable_unprepare(gmac->ephy_clk); 735634db83bSCorentin Labbe reset_control_assert(gmac->rst_ephy); 736634db83bSCorentin Labbe gmac->internal_phy_powered = false; 737634db83bSCorentin Labbe return 0; 738634db83bSCorentin Labbe } 739634db83bSCorentin Labbe 740634db83bSCorentin Labbe /* MDIO multiplexing switch function 741634db83bSCorentin Labbe * This function is called by the mdio-mux layer when it thinks the mdio bus 742634db83bSCorentin Labbe * multiplexer needs to switch. 743634db83bSCorentin Labbe * 'current_child' is the current value of the mux register 744634db83bSCorentin Labbe * 'desired_child' is the value of the 'reg' property of the target child MDIO 745634db83bSCorentin Labbe * node. 746634db83bSCorentin Labbe * The first time this function is called, current_child == -1. 747634db83bSCorentin Labbe * If current_child == desired_child, then the mux is already set to the 748634db83bSCorentin Labbe * correct bus. 749634db83bSCorentin Labbe */ 750634db83bSCorentin Labbe static int mdio_mux_syscon_switch_fn(int current_child, int desired_child, 751634db83bSCorentin Labbe void *data) 752634db83bSCorentin Labbe { 753634db83bSCorentin Labbe struct stmmac_priv *priv = data; 754634db83bSCorentin Labbe struct sunxi_priv_data *gmac = priv->plat->bsp_priv; 755634db83bSCorentin Labbe u32 reg, val; 756634db83bSCorentin Labbe int ret = 0; 757634db83bSCorentin Labbe bool need_power_ephy = false; 758634db83bSCorentin Labbe 759634db83bSCorentin Labbe if (current_child ^ desired_child) { 76025ae15fbSChen-Yu Tsai regmap_field_read(gmac->regmap_field, ®); 761634db83bSCorentin Labbe switch (desired_child) { 762634db83bSCorentin Labbe case DWMAC_SUN8I_MDIO_MUX_INTERNAL_ID: 763634db83bSCorentin Labbe dev_info(priv->device, "Switch mux to internal PHY"); 764634db83bSCorentin Labbe val = (reg & ~H3_EPHY_MUX_MASK) | H3_EPHY_SELECT; 765634db83bSCorentin Labbe 766634db83bSCorentin Labbe need_power_ephy = true; 767634db83bSCorentin Labbe break; 768634db83bSCorentin Labbe case DWMAC_SUN8I_MDIO_MUX_EXTERNAL_ID: 769634db83bSCorentin Labbe dev_info(priv->device, "Switch mux to external PHY"); 770634db83bSCorentin Labbe val = (reg & ~H3_EPHY_MUX_MASK) | H3_EPHY_SHUTDOWN; 771634db83bSCorentin Labbe need_power_ephy = false; 772634db83bSCorentin Labbe break; 773634db83bSCorentin Labbe default: 774634db83bSCorentin Labbe dev_err(priv->device, "Invalid child ID %x\n", 775634db83bSCorentin Labbe desired_child); 776634db83bSCorentin Labbe return -EINVAL; 777634db83bSCorentin Labbe } 77825ae15fbSChen-Yu Tsai regmap_field_write(gmac->regmap_field, val); 779634db83bSCorentin Labbe if (need_power_ephy) { 780634db83bSCorentin Labbe ret = sun8i_dwmac_power_internal_phy(priv); 781634db83bSCorentin Labbe if (ret) 782634db83bSCorentin Labbe return ret; 783634db83bSCorentin Labbe } else { 784634db83bSCorentin Labbe sun8i_dwmac_unpower_internal_phy(gmac); 785634db83bSCorentin Labbe } 786634db83bSCorentin Labbe /* After changing syscon value, the MAC need reset or it will 787634db83bSCorentin Labbe * use the last value (and so the last PHY set). 788634db83bSCorentin Labbe */ 789634db83bSCorentin Labbe ret = sun8i_dwmac_reset(priv); 790634db83bSCorentin Labbe } 791634db83bSCorentin Labbe return ret; 792634db83bSCorentin Labbe } 793634db83bSCorentin Labbe 794634db83bSCorentin Labbe static int sun8i_dwmac_register_mdio_mux(struct stmmac_priv *priv) 795634db83bSCorentin Labbe { 796634db83bSCorentin Labbe int ret; 797634db83bSCorentin Labbe struct device_node *mdio_mux; 798634db83bSCorentin Labbe struct sunxi_priv_data *gmac = priv->plat->bsp_priv; 799634db83bSCorentin Labbe 800634db83bSCorentin Labbe mdio_mux = of_get_child_by_name(priv->device->of_node, "mdio-mux"); 801634db83bSCorentin Labbe if (!mdio_mux) 802634db83bSCorentin Labbe return -ENODEV; 803634db83bSCorentin Labbe 804634db83bSCorentin Labbe ret = mdio_mux_init(priv->device, mdio_mux, mdio_mux_syscon_switch_fn, 805634db83bSCorentin Labbe &gmac->mux_handle, priv, priv->mii); 806634db83bSCorentin Labbe return ret; 807634db83bSCorentin Labbe } 808634db83bSCorentin Labbe 8099f93ac8dSLABBE Corentin static int sun8i_dwmac_set_syscon(struct stmmac_priv *priv) 8109f93ac8dSLABBE Corentin { 8119f93ac8dSLABBE Corentin struct sunxi_priv_data *gmac = priv->plat->bsp_priv; 8129f93ac8dSLABBE Corentin struct device_node *node = priv->device->of_node; 813d93b07f8SLABBE Corentin int ret; 8149f93ac8dSLABBE Corentin u32 reg, val; 8159f93ac8dSLABBE Corentin 81625ae15fbSChen-Yu Tsai regmap_field_read(gmac->regmap_field, &val); 8179f93ac8dSLABBE Corentin reg = gmac->variant->default_syscon_value; 8189f93ac8dSLABBE Corentin if (reg != val) 8199f93ac8dSLABBE Corentin dev_warn(priv->device, 8209f93ac8dSLABBE Corentin "Current syscon value is not the default %x (expect %x)\n", 8219f93ac8dSLABBE Corentin val, reg); 8229f93ac8dSLABBE Corentin 823634db83bSCorentin Labbe if (gmac->variant->soc_has_internal_phy) { 8241c08ac0cSCorentin Labbe if (of_property_read_bool(node, "allwinner,leds-active-low")) 8259f93ac8dSLABBE Corentin reg |= H3_EPHY_LED_POL; 8269f93ac8dSLABBE Corentin else 8279f93ac8dSLABBE Corentin reg &= ~H3_EPHY_LED_POL; 8289f93ac8dSLABBE Corentin 8291450ba8aSIcenowy Zheng /* Force EPHY xtal frequency to 24MHz. */ 8301450ba8aSIcenowy Zheng reg |= H3_EPHY_CLK_SEL; 8311450ba8aSIcenowy Zheng 832634db83bSCorentin Labbe ret = of_mdio_parse_addr(priv->device, priv->plat->phy_node); 8339f93ac8dSLABBE Corentin if (ret < 0) { 8349f93ac8dSLABBE Corentin dev_err(priv->device, "Could not parse MDIO addr\n"); 8359f93ac8dSLABBE Corentin return ret; 8369f93ac8dSLABBE Corentin } 8379f93ac8dSLABBE Corentin /* of_mdio_parse_addr returns a valid (0 ~ 31) PHY 8389f93ac8dSLABBE Corentin * address. No need to mask it again. 8399f93ac8dSLABBE Corentin */ 840634db83bSCorentin Labbe reg |= 1 << H3_EPHY_ADDR_SHIFT; 8419f93ac8dSLABBE Corentin } 8429f93ac8dSLABBE Corentin 8439f93ac8dSLABBE Corentin if (!of_property_read_u32(node, "allwinner,tx-delay-ps", &val)) { 8449f93ac8dSLABBE Corentin if (val % 100) { 8459f93ac8dSLABBE Corentin dev_err(priv->device, "tx-delay must be a multiple of 100\n"); 8469f93ac8dSLABBE Corentin return -EINVAL; 8479f93ac8dSLABBE Corentin } 8489f93ac8dSLABBE Corentin val /= 100; 8499f93ac8dSLABBE Corentin dev_dbg(priv->device, "set tx-delay to %x\n", val); 8509f93ac8dSLABBE Corentin if (val <= SYSCON_ETXDC_MASK) { 8519f93ac8dSLABBE Corentin reg &= ~(SYSCON_ETXDC_MASK << SYSCON_ETXDC_SHIFT); 8529f93ac8dSLABBE Corentin reg |= (val << SYSCON_ETXDC_SHIFT); 8539f93ac8dSLABBE Corentin } else { 8549f93ac8dSLABBE Corentin dev_err(priv->device, "Invalid TX clock delay: %d\n", 8559f93ac8dSLABBE Corentin val); 8569f93ac8dSLABBE Corentin return -EINVAL; 8579f93ac8dSLABBE Corentin } 8589f93ac8dSLABBE Corentin } 8599f93ac8dSLABBE Corentin 8609f93ac8dSLABBE Corentin if (!of_property_read_u32(node, "allwinner,rx-delay-ps", &val)) { 8619f93ac8dSLABBE Corentin if (val % 100) { 8629f93ac8dSLABBE Corentin dev_err(priv->device, "rx-delay must be a multiple of 100\n"); 8639f93ac8dSLABBE Corentin return -EINVAL; 8649f93ac8dSLABBE Corentin } 8659f93ac8dSLABBE Corentin val /= 100; 8669f93ac8dSLABBE Corentin dev_dbg(priv->device, "set rx-delay to %x\n", val); 8679f93ac8dSLABBE Corentin if (val <= SYSCON_ERXDC_MASK) { 8689f93ac8dSLABBE Corentin reg &= ~(SYSCON_ERXDC_MASK << SYSCON_ERXDC_SHIFT); 8699f93ac8dSLABBE Corentin reg |= (val << SYSCON_ERXDC_SHIFT); 8709f93ac8dSLABBE Corentin } else { 8719f93ac8dSLABBE Corentin dev_err(priv->device, "Invalid RX clock delay: %d\n", 8729f93ac8dSLABBE Corentin val); 8739f93ac8dSLABBE Corentin return -EINVAL; 8749f93ac8dSLABBE Corentin } 8759f93ac8dSLABBE Corentin } 8769f93ac8dSLABBE Corentin 8779f93ac8dSLABBE Corentin /* Clear interface mode bits */ 8789f93ac8dSLABBE Corentin reg &= ~(SYSCON_ETCS_MASK | SYSCON_EPIT); 8799f93ac8dSLABBE Corentin if (gmac->variant->support_rmii) 8809f93ac8dSLABBE Corentin reg &= ~SYSCON_RMII_EN; 8819f93ac8dSLABBE Corentin 882d93b07f8SLABBE Corentin switch (priv->plat->interface) { 8839f93ac8dSLABBE Corentin case PHY_INTERFACE_MODE_MII: 8849f93ac8dSLABBE Corentin /* default */ 8859f93ac8dSLABBE Corentin break; 8869f93ac8dSLABBE Corentin case PHY_INTERFACE_MODE_RGMII: 8879f93ac8dSLABBE Corentin reg |= SYSCON_EPIT | SYSCON_ETCS_INT_GMII; 8889f93ac8dSLABBE Corentin break; 8899f93ac8dSLABBE Corentin case PHY_INTERFACE_MODE_RMII: 8909f93ac8dSLABBE Corentin reg |= SYSCON_RMII_EN | SYSCON_ETCS_EXT_GMII; 8919f93ac8dSLABBE Corentin break; 8929f93ac8dSLABBE Corentin default: 8939f93ac8dSLABBE Corentin dev_err(priv->device, "Unsupported interface mode: %s", 8949f93ac8dSLABBE Corentin phy_modes(priv->plat->interface)); 8959f93ac8dSLABBE Corentin return -EINVAL; 8969f93ac8dSLABBE Corentin } 8979f93ac8dSLABBE Corentin 89825ae15fbSChen-Yu Tsai regmap_field_write(gmac->regmap_field, reg); 8999f93ac8dSLABBE Corentin 9009f93ac8dSLABBE Corentin return 0; 9019f93ac8dSLABBE Corentin } 9029f93ac8dSLABBE Corentin 9039f93ac8dSLABBE Corentin static void sun8i_dwmac_unset_syscon(struct sunxi_priv_data *gmac) 9049f93ac8dSLABBE Corentin { 9059f93ac8dSLABBE Corentin u32 reg = gmac->variant->default_syscon_value; 9069f93ac8dSLABBE Corentin 90725ae15fbSChen-Yu Tsai regmap_field_write(gmac->regmap_field, reg); 9089f93ac8dSLABBE Corentin } 9099f93ac8dSLABBE Corentin 9109f93ac8dSLABBE Corentin static void sun8i_dwmac_exit(struct platform_device *pdev, void *priv) 9119f93ac8dSLABBE Corentin { 9129f93ac8dSLABBE Corentin struct sunxi_priv_data *gmac = priv; 9139f93ac8dSLABBE Corentin 914634db83bSCorentin Labbe if (gmac->variant->soc_has_internal_phy) { 915634db83bSCorentin Labbe /* sun8i_dwmac_exit could be called with mdiomux uninit */ 916634db83bSCorentin Labbe if (gmac->mux_handle) 917634db83bSCorentin Labbe mdio_mux_uninit(gmac->mux_handle); 918634db83bSCorentin Labbe if (gmac->internal_phy_powered) 919634db83bSCorentin Labbe sun8i_dwmac_unpower_internal_phy(gmac); 920634db83bSCorentin Labbe } 921634db83bSCorentin Labbe 922634db83bSCorentin Labbe sun8i_dwmac_unset_syscon(gmac); 923634db83bSCorentin Labbe 924634db83bSCorentin Labbe reset_control_put(gmac->rst_ephy); 9259f93ac8dSLABBE Corentin 9269f93ac8dSLABBE Corentin clk_disable_unprepare(gmac->tx_clk); 9279f93ac8dSLABBE Corentin 9289f93ac8dSLABBE Corentin if (gmac->regulator) 9299f93ac8dSLABBE Corentin regulator_disable(gmac->regulator); 9309f93ac8dSLABBE Corentin } 9319f93ac8dSLABBE Corentin 9329f93ac8dSLABBE Corentin static const struct stmmac_ops sun8i_dwmac_ops = { 9339f93ac8dSLABBE Corentin .core_init = sun8i_dwmac_core_init, 9349f93ac8dSLABBE Corentin .set_mac = sun8i_dwmac_set_mac, 9359f93ac8dSLABBE Corentin .dump_regs = sun8i_dwmac_dump_mac_regs, 9369f93ac8dSLABBE Corentin .rx_ipc = sun8i_dwmac_rx_ipc_enable, 9379f93ac8dSLABBE Corentin .set_filter = sun8i_dwmac_set_filter, 9389f93ac8dSLABBE Corentin .flow_ctrl = sun8i_dwmac_flow_ctrl, 9399f93ac8dSLABBE Corentin .set_umac_addr = sun8i_dwmac_set_umac_addr, 9409f93ac8dSLABBE Corentin .get_umac_addr = sun8i_dwmac_get_umac_addr, 9419f93ac8dSLABBE Corentin }; 9429f93ac8dSLABBE Corentin 9439f93ac8dSLABBE Corentin static struct mac_device_info *sun8i_dwmac_setup(void *ppriv) 9449f93ac8dSLABBE Corentin { 9459f93ac8dSLABBE Corentin struct mac_device_info *mac; 9469f93ac8dSLABBE Corentin struct stmmac_priv *priv = ppriv; 9479f93ac8dSLABBE Corentin int ret; 9489f93ac8dSLABBE Corentin 9499f93ac8dSLABBE Corentin mac = devm_kzalloc(priv->device, sizeof(*mac), GFP_KERNEL); 9509f93ac8dSLABBE Corentin if (!mac) 9519f93ac8dSLABBE Corentin return NULL; 9529f93ac8dSLABBE Corentin 953634db83bSCorentin Labbe ret = sun8i_dwmac_set_syscon(priv); 9549f93ac8dSLABBE Corentin if (ret) 9559f93ac8dSLABBE Corentin return NULL; 9569f93ac8dSLABBE Corentin 9579f93ac8dSLABBE Corentin mac->pcsr = priv->ioaddr; 9589f93ac8dSLABBE Corentin mac->mac = &sun8i_dwmac_ops; 9599f93ac8dSLABBE Corentin mac->dma = &sun8i_dwmac_dma_ops; 9609f93ac8dSLABBE Corentin 9619f93ac8dSLABBE Corentin /* The loopback bit seems to be re-set when link change 9629f93ac8dSLABBE Corentin * Simply mask it each time 9639f93ac8dSLABBE Corentin * Speed 10/100/1000 are set in BIT(2)/BIT(3) 9649f93ac8dSLABBE Corentin */ 9659f93ac8dSLABBE Corentin mac->link.speed_mask = GENMASK(3, 2) | EMAC_LOOPBACK; 9669f93ac8dSLABBE Corentin mac->link.speed10 = EMAC_SPEED_10; 9679f93ac8dSLABBE Corentin mac->link.speed100 = EMAC_SPEED_100; 9689f93ac8dSLABBE Corentin mac->link.speed1000 = EMAC_SPEED_1000; 9699f93ac8dSLABBE Corentin mac->link.duplex = EMAC_DUPLEX_FULL; 9709f93ac8dSLABBE Corentin mac->mii.addr = EMAC_MDIO_CMD; 9719f93ac8dSLABBE Corentin mac->mii.data = EMAC_MDIO_DATA; 9729f93ac8dSLABBE Corentin mac->mii.reg_shift = 4; 9739f93ac8dSLABBE Corentin mac->mii.reg_mask = GENMASK(8, 4); 9749f93ac8dSLABBE Corentin mac->mii.addr_shift = 12; 9759f93ac8dSLABBE Corentin mac->mii.addr_mask = GENMASK(16, 12); 9769f93ac8dSLABBE Corentin mac->mii.clk_csr_shift = 20; 9779f93ac8dSLABBE Corentin mac->mii.clk_csr_mask = GENMASK(22, 20); 9789f93ac8dSLABBE Corentin mac->unicast_filter_entries = 8; 9799f93ac8dSLABBE Corentin 9809f93ac8dSLABBE Corentin /* Synopsys Id is not available */ 9819f93ac8dSLABBE Corentin priv->synopsys_id = 0; 9829f93ac8dSLABBE Corentin 9839f93ac8dSLABBE Corentin return mac; 9849f93ac8dSLABBE Corentin } 9859f93ac8dSLABBE Corentin 986*49a06caeSChen-Yu Tsai static struct regmap *sun8i_dwmac_get_syscon_from_dev(struct device_node *node) 987*49a06caeSChen-Yu Tsai { 988*49a06caeSChen-Yu Tsai struct device_node *syscon_node; 989*49a06caeSChen-Yu Tsai struct platform_device *syscon_pdev; 990*49a06caeSChen-Yu Tsai struct regmap *regmap = NULL; 991*49a06caeSChen-Yu Tsai 992*49a06caeSChen-Yu Tsai syscon_node = of_parse_phandle(node, "syscon", 0); 993*49a06caeSChen-Yu Tsai if (!syscon_node) 994*49a06caeSChen-Yu Tsai return ERR_PTR(-ENODEV); 995*49a06caeSChen-Yu Tsai 996*49a06caeSChen-Yu Tsai syscon_pdev = of_find_device_by_node(syscon_node); 997*49a06caeSChen-Yu Tsai if (!syscon_pdev) { 998*49a06caeSChen-Yu Tsai /* platform device might not be probed yet */ 999*49a06caeSChen-Yu Tsai regmap = ERR_PTR(-EPROBE_DEFER); 1000*49a06caeSChen-Yu Tsai goto out_put_node; 1001*49a06caeSChen-Yu Tsai } 1002*49a06caeSChen-Yu Tsai 1003*49a06caeSChen-Yu Tsai /* If no regmap is found then the other device driver is at fault */ 1004*49a06caeSChen-Yu Tsai regmap = dev_get_regmap(&syscon_pdev->dev, NULL); 1005*49a06caeSChen-Yu Tsai if (!regmap) 1006*49a06caeSChen-Yu Tsai regmap = ERR_PTR(-EINVAL); 1007*49a06caeSChen-Yu Tsai 1008*49a06caeSChen-Yu Tsai platform_device_put(syscon_pdev); 1009*49a06caeSChen-Yu Tsai out_put_node: 1010*49a06caeSChen-Yu Tsai of_node_put(syscon_node); 1011*49a06caeSChen-Yu Tsai return regmap; 1012*49a06caeSChen-Yu Tsai } 1013*49a06caeSChen-Yu Tsai 10149f93ac8dSLABBE Corentin static int sun8i_dwmac_probe(struct platform_device *pdev) 10159f93ac8dSLABBE Corentin { 10169f93ac8dSLABBE Corentin struct plat_stmmacenet_data *plat_dat; 10179f93ac8dSLABBE Corentin struct stmmac_resources stmmac_res; 10189f93ac8dSLABBE Corentin struct sunxi_priv_data *gmac; 10199f93ac8dSLABBE Corentin struct device *dev = &pdev->dev; 10209f93ac8dSLABBE Corentin int ret; 1021634db83bSCorentin Labbe struct stmmac_priv *priv; 1022634db83bSCorentin Labbe struct net_device *ndev; 102325ae15fbSChen-Yu Tsai struct regmap *regmap; 10249f93ac8dSLABBE Corentin 10259f93ac8dSLABBE Corentin ret = stmmac_get_platform_resources(pdev, &stmmac_res); 10269f93ac8dSLABBE Corentin if (ret) 10279f93ac8dSLABBE Corentin return ret; 10289f93ac8dSLABBE Corentin 10299f93ac8dSLABBE Corentin plat_dat = stmmac_probe_config_dt(pdev, &stmmac_res.mac); 10309f93ac8dSLABBE Corentin if (IS_ERR(plat_dat)) 10319f93ac8dSLABBE Corentin return PTR_ERR(plat_dat); 10329f93ac8dSLABBE Corentin 10339f93ac8dSLABBE Corentin gmac = devm_kzalloc(dev, sizeof(*gmac), GFP_KERNEL); 10349f93ac8dSLABBE Corentin if (!gmac) 10359f93ac8dSLABBE Corentin return -ENOMEM; 10369f93ac8dSLABBE Corentin 10379f93ac8dSLABBE Corentin gmac->variant = of_device_get_match_data(&pdev->dev); 10389f93ac8dSLABBE Corentin if (!gmac->variant) { 10399f93ac8dSLABBE Corentin dev_err(&pdev->dev, "Missing dwmac-sun8i variant\n"); 10409f93ac8dSLABBE Corentin return -EINVAL; 10419f93ac8dSLABBE Corentin } 10429f93ac8dSLABBE Corentin 10439f93ac8dSLABBE Corentin gmac->tx_clk = devm_clk_get(dev, "stmmaceth"); 10449f93ac8dSLABBE Corentin if (IS_ERR(gmac->tx_clk)) { 10459f93ac8dSLABBE Corentin dev_err(dev, "Could not get TX clock\n"); 10469f93ac8dSLABBE Corentin return PTR_ERR(gmac->tx_clk); 10479f93ac8dSLABBE Corentin } 10489f93ac8dSLABBE Corentin 10499f93ac8dSLABBE Corentin /* Optional regulator for PHY */ 10509f93ac8dSLABBE Corentin gmac->regulator = devm_regulator_get_optional(dev, "phy"); 10519f93ac8dSLABBE Corentin if (IS_ERR(gmac->regulator)) { 10529f93ac8dSLABBE Corentin if (PTR_ERR(gmac->regulator) == -EPROBE_DEFER) 10539f93ac8dSLABBE Corentin return -EPROBE_DEFER; 10549f93ac8dSLABBE Corentin dev_info(dev, "No regulator found\n"); 10559f93ac8dSLABBE Corentin gmac->regulator = NULL; 10569f93ac8dSLABBE Corentin } 10579f93ac8dSLABBE Corentin 1058*49a06caeSChen-Yu Tsai /* The "GMAC clock control" register might be located in the 1059*49a06caeSChen-Yu Tsai * CCU address range (on the R40), or the system control address 1060*49a06caeSChen-Yu Tsai * range (on most other sun8i and later SoCs). 1061*49a06caeSChen-Yu Tsai * 1062*49a06caeSChen-Yu Tsai * The former controls most if not all clocks in the SoC. The 1063*49a06caeSChen-Yu Tsai * latter has an SoC identification register, and on some SoCs, 1064*49a06caeSChen-Yu Tsai * controls to map device specific SRAM to either the intended 1065*49a06caeSChen-Yu Tsai * peripheral, or the CPU address space. 1066*49a06caeSChen-Yu Tsai * 1067*49a06caeSChen-Yu Tsai * In either case, there should be a coordinated and restricted 1068*49a06caeSChen-Yu Tsai * method of accessing the register needed here. This is done by 1069*49a06caeSChen-Yu Tsai * having the device export a custom regmap, instead of a generic 1070*49a06caeSChen-Yu Tsai * syscon, which grants all access to all registers. 1071*49a06caeSChen-Yu Tsai * 1072*49a06caeSChen-Yu Tsai * To support old device trees, we fall back to using the syscon 1073*49a06caeSChen-Yu Tsai * interface if possible. 1074*49a06caeSChen-Yu Tsai */ 1075*49a06caeSChen-Yu Tsai regmap = sun8i_dwmac_get_syscon_from_dev(pdev->dev.of_node); 1076*49a06caeSChen-Yu Tsai if (IS_ERR(regmap)) 1077*49a06caeSChen-Yu Tsai regmap = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, 1078*49a06caeSChen-Yu Tsai "syscon"); 107925ae15fbSChen-Yu Tsai if (IS_ERR(regmap)) { 108025ae15fbSChen-Yu Tsai ret = PTR_ERR(regmap); 10819f93ac8dSLABBE Corentin dev_err(&pdev->dev, "Unable to map syscon: %d\n", ret); 10829f93ac8dSLABBE Corentin return ret; 10839f93ac8dSLABBE Corentin } 10849f93ac8dSLABBE Corentin 108525ae15fbSChen-Yu Tsai gmac->regmap_field = devm_regmap_field_alloc(dev, regmap, 108625ae15fbSChen-Yu Tsai *gmac->variant->syscon_field); 108725ae15fbSChen-Yu Tsai if (IS_ERR(gmac->regmap_field)) { 108825ae15fbSChen-Yu Tsai ret = PTR_ERR(gmac->regmap_field); 108925ae15fbSChen-Yu Tsai dev_err(dev, "Unable to map syscon register: %d\n", ret); 109025ae15fbSChen-Yu Tsai return ret; 109125ae15fbSChen-Yu Tsai } 109225ae15fbSChen-Yu Tsai 10939f93ac8dSLABBE Corentin plat_dat->interface = of_get_phy_mode(dev->of_node); 10949f93ac8dSLABBE Corentin 10959f93ac8dSLABBE Corentin /* platform data specifying hardware features and callbacks. 10969f93ac8dSLABBE Corentin * hardware features were copied from Allwinner drivers. 10979f93ac8dSLABBE Corentin */ 10989f93ac8dSLABBE Corentin plat_dat->rx_coe = STMMAC_RX_COE_TYPE2; 10999f93ac8dSLABBE Corentin plat_dat->tx_coe = 1; 11009f93ac8dSLABBE Corentin plat_dat->has_sun8i = true; 11019f93ac8dSLABBE Corentin plat_dat->bsp_priv = gmac; 11029f93ac8dSLABBE Corentin plat_dat->init = sun8i_dwmac_init; 11039f93ac8dSLABBE Corentin plat_dat->exit = sun8i_dwmac_exit; 11049f93ac8dSLABBE Corentin plat_dat->setup = sun8i_dwmac_setup; 11059f93ac8dSLABBE Corentin 11069f93ac8dSLABBE Corentin ret = sun8i_dwmac_init(pdev, plat_dat->bsp_priv); 11079f93ac8dSLABBE Corentin if (ret) 11089f93ac8dSLABBE Corentin return ret; 11099f93ac8dSLABBE Corentin 11109f93ac8dSLABBE Corentin ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res); 11119f93ac8dSLABBE Corentin if (ret) 1112634db83bSCorentin Labbe goto dwmac_exit; 11139f93ac8dSLABBE Corentin 1114634db83bSCorentin Labbe ndev = dev_get_drvdata(&pdev->dev); 1115634db83bSCorentin Labbe priv = netdev_priv(ndev); 1116634db83bSCorentin Labbe /* The mux must be registered after parent MDIO 1117634db83bSCorentin Labbe * so after stmmac_dvr_probe() 1118634db83bSCorentin Labbe */ 1119634db83bSCorentin Labbe if (gmac->variant->soc_has_internal_phy) { 1120634db83bSCorentin Labbe ret = get_ephy_nodes(priv); 1121634db83bSCorentin Labbe if (ret) 1122634db83bSCorentin Labbe goto dwmac_exit; 1123634db83bSCorentin Labbe ret = sun8i_dwmac_register_mdio_mux(priv); 1124634db83bSCorentin Labbe if (ret) { 1125634db83bSCorentin Labbe dev_err(&pdev->dev, "Failed to register mux\n"); 1126634db83bSCorentin Labbe goto dwmac_mux; 1127634db83bSCorentin Labbe } 1128634db83bSCorentin Labbe } else { 1129634db83bSCorentin Labbe ret = sun8i_dwmac_reset(priv); 1130634db83bSCorentin Labbe if (ret) 1131634db83bSCorentin Labbe goto dwmac_exit; 1132634db83bSCorentin Labbe } 1133634db83bSCorentin Labbe 1134634db83bSCorentin Labbe return ret; 1135634db83bSCorentin Labbe dwmac_mux: 1136634db83bSCorentin Labbe sun8i_dwmac_unset_syscon(gmac); 1137634db83bSCorentin Labbe dwmac_exit: 1138634db83bSCorentin Labbe sun8i_dwmac_exit(pdev, plat_dat->bsp_priv); 11399f93ac8dSLABBE Corentin return ret; 11409f93ac8dSLABBE Corentin } 11419f93ac8dSLABBE Corentin 11429f93ac8dSLABBE Corentin static const struct of_device_id sun8i_dwmac_match[] = { 1143a8ff8ccbSCorentin Labbe { .compatible = "allwinner,sun8i-h3-emac", 1144a8ff8ccbSCorentin Labbe .data = &emac_variant_h3 }, 1145a8ff8ccbSCorentin Labbe { .compatible = "allwinner,sun8i-v3s-emac", 1146a8ff8ccbSCorentin Labbe .data = &emac_variant_v3s }, 1147a8ff8ccbSCorentin Labbe { .compatible = "allwinner,sun8i-a83t-emac", 1148a8ff8ccbSCorentin Labbe .data = &emac_variant_a83t }, 1149a8ff8ccbSCorentin Labbe { .compatible = "allwinner,sun50i-a64-emac", 1150a8ff8ccbSCorentin Labbe .data = &emac_variant_a64 }, 11519f93ac8dSLABBE Corentin { } 11529f93ac8dSLABBE Corentin }; 11539f93ac8dSLABBE Corentin MODULE_DEVICE_TABLE(of, sun8i_dwmac_match); 11549f93ac8dSLABBE Corentin 11559f93ac8dSLABBE Corentin static struct platform_driver sun8i_dwmac_driver = { 11569f93ac8dSLABBE Corentin .probe = sun8i_dwmac_probe, 11579f93ac8dSLABBE Corentin .remove = stmmac_pltfr_remove, 11589f93ac8dSLABBE Corentin .driver = { 11599f93ac8dSLABBE Corentin .name = "dwmac-sun8i", 11609f93ac8dSLABBE Corentin .pm = &stmmac_pltfr_pm_ops, 11619f93ac8dSLABBE Corentin .of_match_table = sun8i_dwmac_match, 11629f93ac8dSLABBE Corentin }, 11639f93ac8dSLABBE Corentin }; 11649f93ac8dSLABBE Corentin module_platform_driver(sun8i_dwmac_driver); 11659f93ac8dSLABBE Corentin 11669f93ac8dSLABBE Corentin MODULE_AUTHOR("Corentin Labbe <clabbe.montjoie@gmail.com>"); 11679f93ac8dSLABBE Corentin MODULE_DESCRIPTION("Allwinner sun8i DWMAC specific glue layer"); 11689f93ac8dSLABBE Corentin MODULE_LICENSE("GPL"); 1169