1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
29f93ac8dSLABBE Corentin /*
39f93ac8dSLABBE Corentin  * dwmac-sun8i.c - Allwinner sun8i DWMAC specific glue layer
49f93ac8dSLABBE Corentin  *
59f93ac8dSLABBE Corentin  * Copyright (C) 2017 Corentin Labbe <clabbe.montjoie@gmail.com>
69f93ac8dSLABBE Corentin  */
79f93ac8dSLABBE Corentin 
89f93ac8dSLABBE Corentin #include <linux/clk.h>
99f93ac8dSLABBE Corentin #include <linux/io.h>
109f93ac8dSLABBE Corentin #include <linux/iopoll.h>
11634db83bSCorentin Labbe #include <linux/mdio-mux.h>
129f93ac8dSLABBE Corentin #include <linux/mfd/syscon.h>
139f93ac8dSLABBE Corentin #include <linux/module.h>
149f93ac8dSLABBE Corentin #include <linux/of_device.h>
159f93ac8dSLABBE Corentin #include <linux/of_mdio.h>
169f93ac8dSLABBE Corentin #include <linux/of_net.h>
179f93ac8dSLABBE Corentin #include <linux/phy.h>
189f93ac8dSLABBE Corentin #include <linux/platform_device.h>
19b76bbb34SJisheng Zhang #include <linux/pm_runtime.h>
209f93ac8dSLABBE Corentin #include <linux/regulator/consumer.h>
219f93ac8dSLABBE Corentin #include <linux/regmap.h>
229f93ac8dSLABBE Corentin #include <linux/stmmac.h>
239f93ac8dSLABBE Corentin 
249f93ac8dSLABBE Corentin #include "stmmac.h"
259f93ac8dSLABBE Corentin #include "stmmac_platform.h"
269f93ac8dSLABBE Corentin 
279f93ac8dSLABBE Corentin /* General notes on dwmac-sun8i:
289f93ac8dSLABBE Corentin  * Locking: no locking is necessary in this file because all necessary locking
299f93ac8dSLABBE Corentin  *		is done in the "stmmac files"
309f93ac8dSLABBE Corentin  */
319f93ac8dSLABBE Corentin 
3256c266dcSCorentin Labbe /* struct emac_variant - Describe dwmac-sun8i hardware variant
339f93ac8dSLABBE Corentin  * @default_syscon_value:	The default value of the EMAC register in syscon
349f93ac8dSLABBE Corentin  *				This value is used for disabling properly EMAC
359f93ac8dSLABBE Corentin  *				and used as a good starting value in case of the
369f93ac8dSLABBE Corentin  *				boot process(uboot) leave some stuff.
3725ae15fbSChen-Yu Tsai  * @syscon_field		reg_field for the syscon's gmac register
38634db83bSCorentin Labbe  * @soc_has_internal_phy:	Does the MAC embed an internal PHY
399f93ac8dSLABBE Corentin  * @support_mii:		Does the MAC handle MII
409f93ac8dSLABBE Corentin  * @support_rmii:		Does the MAC handle RMII
419f93ac8dSLABBE Corentin  * @support_rgmii:		Does the MAC handle RGMII
427b270b72SChen-Yu Tsai  *
437b270b72SChen-Yu Tsai  * @rx_delay_max:		Maximum raw value for RX delay chain
447b270b72SChen-Yu Tsai  * @tx_delay_max:		Maximum raw value for TX delay chain
457b270b72SChen-Yu Tsai  *				These two also indicate the bitmask for
467b270b72SChen-Yu Tsai  *				the RX and TX delay chain registers. A
477b270b72SChen-Yu Tsai  *				value of zero indicates this is not supported.
489f93ac8dSLABBE Corentin  */
499f93ac8dSLABBE Corentin struct emac_variant {
509f93ac8dSLABBE Corentin 	u32 default_syscon_value;
5125ae15fbSChen-Yu Tsai 	const struct reg_field *syscon_field;
52634db83bSCorentin Labbe 	bool soc_has_internal_phy;
539f93ac8dSLABBE Corentin 	bool support_mii;
549f93ac8dSLABBE Corentin 	bool support_rmii;
559f93ac8dSLABBE Corentin 	bool support_rgmii;
567b270b72SChen-Yu Tsai 	u8 rx_delay_max;
577b270b72SChen-Yu Tsai 	u8 tx_delay_max;
589f93ac8dSLABBE Corentin };
599f93ac8dSLABBE Corentin 
609f93ac8dSLABBE Corentin /* struct sunxi_priv_data - hold all sunxi private data
619f93ac8dSLABBE Corentin  * @ephy_clk:	reference to the optional EPHY clock for the internal PHY
629f93ac8dSLABBE Corentin  * @regulator:	reference to the optional regulator
639f93ac8dSLABBE Corentin  * @rst_ephy:	reference to the optional EPHY reset for the internal PHY
649f93ac8dSLABBE Corentin  * @variant:	reference to the current board variant
659f93ac8dSLABBE Corentin  * @regmap:	regmap for using the syscon
66634db83bSCorentin Labbe  * @internal_phy_powered: Does the internal PHY is enabled
67b8239638SSamuel Holland  * @use_internal_phy: Is the internal PHY selected for use
68634db83bSCorentin Labbe  * @mux_handle:	Internal pointer used by mdio-mux lib
699f93ac8dSLABBE Corentin  */
709f93ac8dSLABBE Corentin struct sunxi_priv_data {
719f93ac8dSLABBE Corentin 	struct clk *ephy_clk;
729f93ac8dSLABBE Corentin 	struct regulator *regulator;
739f93ac8dSLABBE Corentin 	struct reset_control *rst_ephy;
749f93ac8dSLABBE Corentin 	const struct emac_variant *variant;
7525ae15fbSChen-Yu Tsai 	struct regmap_field *regmap_field;
76634db83bSCorentin Labbe 	bool internal_phy_powered;
77b8239638SSamuel Holland 	bool use_internal_phy;
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 
889bf5085aSChen-Yu Tsai /* EMAC clock register @ 0x164 in the CCU address range */
899bf5085aSChen-Yu Tsai static const struct reg_field sun8i_ccu_reg_field = {
909bf5085aSChen-Yu Tsai 	.reg = 0x164,
919bf5085aSChen-Yu Tsai 	.lsb = 0,
929bf5085aSChen-Yu Tsai 	.msb = 31,
939bf5085aSChen-Yu Tsai };
949bf5085aSChen-Yu Tsai 
959f93ac8dSLABBE Corentin static const struct emac_variant emac_variant_h3 = {
969f93ac8dSLABBE Corentin 	.default_syscon_value = 0x58000,
9725ae15fbSChen-Yu Tsai 	.syscon_field = &sun8i_syscon_reg_field,
98634db83bSCorentin Labbe 	.soc_has_internal_phy = true,
999f93ac8dSLABBE Corentin 	.support_mii = true,
1009f93ac8dSLABBE Corentin 	.support_rmii = true,
1017b270b72SChen-Yu Tsai 	.support_rgmii = true,
1027b270b72SChen-Yu Tsai 	.rx_delay_max = 31,
1037b270b72SChen-Yu Tsai 	.tx_delay_max = 7,
1049f93ac8dSLABBE Corentin };
1059f93ac8dSLABBE Corentin 
10657fde47dSIcenowy Zheng static const struct emac_variant emac_variant_v3s = {
10757fde47dSIcenowy Zheng 	.default_syscon_value = 0x38000,
10825ae15fbSChen-Yu Tsai 	.syscon_field = &sun8i_syscon_reg_field,
109634db83bSCorentin Labbe 	.soc_has_internal_phy = true,
11057fde47dSIcenowy Zheng 	.support_mii = true
11157fde47dSIcenowy Zheng };
11257fde47dSIcenowy Zheng 
1139f93ac8dSLABBE Corentin static const struct emac_variant emac_variant_a83t = {
1149f93ac8dSLABBE Corentin 	.default_syscon_value = 0,
11525ae15fbSChen-Yu Tsai 	.syscon_field = &sun8i_syscon_reg_field,
116634db83bSCorentin Labbe 	.soc_has_internal_phy = false,
1179f93ac8dSLABBE Corentin 	.support_mii = true,
1187b270b72SChen-Yu Tsai 	.support_rgmii = true,
1197b270b72SChen-Yu Tsai 	.rx_delay_max = 31,
1207b270b72SChen-Yu Tsai 	.tx_delay_max = 7,
1219f93ac8dSLABBE Corentin };
1229f93ac8dSLABBE Corentin 
1239bf5085aSChen-Yu Tsai static const struct emac_variant emac_variant_r40 = {
1249bf5085aSChen-Yu Tsai 	.default_syscon_value = 0,
1259bf5085aSChen-Yu Tsai 	.syscon_field = &sun8i_ccu_reg_field,
1269bf5085aSChen-Yu Tsai 	.support_mii = true,
1279bf5085aSChen-Yu Tsai 	.support_rgmii = true,
1289bf5085aSChen-Yu Tsai 	.rx_delay_max = 7,
1299bf5085aSChen-Yu Tsai };
1309bf5085aSChen-Yu Tsai 
1319f93ac8dSLABBE Corentin static const struct emac_variant emac_variant_a64 = {
1329f93ac8dSLABBE Corentin 	.default_syscon_value = 0,
13325ae15fbSChen-Yu Tsai 	.syscon_field = &sun8i_syscon_reg_field,
134634db83bSCorentin Labbe 	.soc_has_internal_phy = false,
1359f93ac8dSLABBE Corentin 	.support_mii = true,
1369f93ac8dSLABBE Corentin 	.support_rmii = true,
1377b270b72SChen-Yu Tsai 	.support_rgmii = true,
1387b270b72SChen-Yu Tsai 	.rx_delay_max = 31,
1397b270b72SChen-Yu Tsai 	.tx_delay_max = 7,
1409f93ac8dSLABBE Corentin };
1419f93ac8dSLABBE Corentin 
142adadd38cSIcenowy Zheng static const struct emac_variant emac_variant_h6 = {
143adadd38cSIcenowy Zheng 	.default_syscon_value = 0x50000,
144adadd38cSIcenowy Zheng 	.syscon_field = &sun8i_syscon_reg_field,
145adadd38cSIcenowy Zheng 	/* The "Internal PHY" of H6 is not on the die. It's on the
146adadd38cSIcenowy Zheng 	 * co-packaged AC200 chip instead.
147adadd38cSIcenowy Zheng 	 */
148adadd38cSIcenowy Zheng 	.soc_has_internal_phy = false,
149adadd38cSIcenowy Zheng 	.support_mii = true,
150adadd38cSIcenowy Zheng 	.support_rmii = true,
151adadd38cSIcenowy Zheng 	.support_rgmii = true,
152adadd38cSIcenowy Zheng 	.rx_delay_max = 31,
153adadd38cSIcenowy Zheng 	.tx_delay_max = 7,
154adadd38cSIcenowy Zheng };
155adadd38cSIcenowy Zheng 
1569f93ac8dSLABBE Corentin #define EMAC_BASIC_CTL0 0x00
1579f93ac8dSLABBE Corentin #define EMAC_BASIC_CTL1 0x04
1589f93ac8dSLABBE Corentin #define EMAC_INT_STA    0x08
1599f93ac8dSLABBE Corentin #define EMAC_INT_EN     0x0C
1609f93ac8dSLABBE Corentin #define EMAC_TX_CTL0    0x10
1619f93ac8dSLABBE Corentin #define EMAC_TX_CTL1    0x14
1629f93ac8dSLABBE Corentin #define EMAC_TX_FLOW_CTL        0x1C
1639f93ac8dSLABBE Corentin #define EMAC_TX_DESC_LIST 0x20
1649f93ac8dSLABBE Corentin #define EMAC_RX_CTL0    0x24
1659f93ac8dSLABBE Corentin #define EMAC_RX_CTL1    0x28
1669f93ac8dSLABBE Corentin #define EMAC_RX_DESC_LIST 0x34
1679f93ac8dSLABBE Corentin #define EMAC_RX_FRM_FLT 0x38
1689f93ac8dSLABBE Corentin #define EMAC_MDIO_CMD   0x48
1699f93ac8dSLABBE Corentin #define EMAC_MDIO_DATA  0x4C
1709f93ac8dSLABBE Corentin #define EMAC_MACADDR_HI(reg) (0x50 + (reg) * 8)
1719f93ac8dSLABBE Corentin #define EMAC_MACADDR_LO(reg) (0x54 + (reg) * 8)
1729f93ac8dSLABBE Corentin #define EMAC_TX_DMA_STA 0xB0
1739f93ac8dSLABBE Corentin #define EMAC_TX_CUR_DESC        0xB4
1749f93ac8dSLABBE Corentin #define EMAC_TX_CUR_BUF 0xB8
1759f93ac8dSLABBE Corentin #define EMAC_RX_DMA_STA 0xC0
1769f93ac8dSLABBE Corentin #define EMAC_RX_CUR_DESC        0xC4
1779f93ac8dSLABBE Corentin #define EMAC_RX_CUR_BUF 0xC8
1789f93ac8dSLABBE Corentin 
1799f93ac8dSLABBE Corentin /* Use in EMAC_BASIC_CTL0 */
1809f93ac8dSLABBE Corentin #define EMAC_DUPLEX_FULL	BIT(0)
1819f93ac8dSLABBE Corentin #define EMAC_LOOPBACK		BIT(1)
1829f93ac8dSLABBE Corentin #define EMAC_SPEED_1000 0
1839f93ac8dSLABBE Corentin #define EMAC_SPEED_100 (0x03 << 2)
1849f93ac8dSLABBE Corentin #define EMAC_SPEED_10 (0x02 << 2)
1859f93ac8dSLABBE Corentin 
1869f93ac8dSLABBE Corentin /* Use in EMAC_BASIC_CTL1 */
1879f93ac8dSLABBE Corentin #define EMAC_BURSTLEN_SHIFT		24
1889f93ac8dSLABBE Corentin 
1899f93ac8dSLABBE Corentin /* Used in EMAC_RX_FRM_FLT */
1909f93ac8dSLABBE Corentin #define EMAC_FRM_FLT_RXALL              BIT(0)
1919f93ac8dSLABBE Corentin #define EMAC_FRM_FLT_CTL                BIT(13)
1929f93ac8dSLABBE Corentin #define EMAC_FRM_FLT_MULTICAST          BIT(16)
1939f93ac8dSLABBE Corentin 
1949f93ac8dSLABBE Corentin /* Used in RX_CTL1*/
1959f93ac8dSLABBE Corentin #define EMAC_RX_MD              BIT(1)
196aa4c0c90SJoe Perches #define EMAC_RX_TH_MASK		GENMASK(5, 4)
1979f93ac8dSLABBE Corentin #define EMAC_RX_TH_32		0
1989f93ac8dSLABBE Corentin #define EMAC_RX_TH_64		(0x1 << 4)
1999f93ac8dSLABBE Corentin #define EMAC_RX_TH_96		(0x2 << 4)
2009f93ac8dSLABBE Corentin #define EMAC_RX_TH_128		(0x3 << 4)
2019f93ac8dSLABBE Corentin #define EMAC_RX_DMA_EN  BIT(30)
2029f93ac8dSLABBE Corentin #define EMAC_RX_DMA_START       BIT(31)
2039f93ac8dSLABBE Corentin 
2049f93ac8dSLABBE Corentin /* Used in TX_CTL1*/
2059f93ac8dSLABBE Corentin #define EMAC_TX_MD              BIT(1)
2069f93ac8dSLABBE Corentin #define EMAC_TX_NEXT_FRM        BIT(2)
207aa4c0c90SJoe Perches #define EMAC_TX_TH_MASK		GENMASK(10, 8)
2089f93ac8dSLABBE Corentin #define EMAC_TX_TH_64		0
2099f93ac8dSLABBE Corentin #define EMAC_TX_TH_128		(0x1 << 8)
2109f93ac8dSLABBE Corentin #define EMAC_TX_TH_192		(0x2 << 8)
2119f93ac8dSLABBE Corentin #define EMAC_TX_TH_256		(0x3 << 8)
2129f93ac8dSLABBE Corentin #define EMAC_TX_DMA_EN  BIT(30)
2139f93ac8dSLABBE Corentin #define EMAC_TX_DMA_START       BIT(31)
2149f93ac8dSLABBE Corentin 
2159f93ac8dSLABBE Corentin /* Used in RX_CTL0 */
2169f93ac8dSLABBE Corentin #define EMAC_RX_RECEIVER_EN             BIT(31)
2179f93ac8dSLABBE Corentin #define EMAC_RX_DO_CRC BIT(27)
2189f93ac8dSLABBE Corentin #define EMAC_RX_FLOW_CTL_EN             BIT(16)
2199f93ac8dSLABBE Corentin 
2209f93ac8dSLABBE Corentin /* Used in TX_CTL0 */
2219f93ac8dSLABBE Corentin #define EMAC_TX_TRANSMITTER_EN  BIT(31)
2229f93ac8dSLABBE Corentin 
2239f93ac8dSLABBE Corentin /* Used in EMAC_TX_FLOW_CTL */
2249f93ac8dSLABBE Corentin #define EMAC_TX_FLOW_CTL_EN             BIT(0)
2259f93ac8dSLABBE Corentin 
2269f93ac8dSLABBE Corentin /* Used in EMAC_INT_STA */
2279f93ac8dSLABBE Corentin #define EMAC_TX_INT             BIT(0)
2289f93ac8dSLABBE Corentin #define EMAC_TX_DMA_STOP_INT    BIT(1)
2299f93ac8dSLABBE Corentin #define EMAC_TX_BUF_UA_INT      BIT(2)
2309f93ac8dSLABBE Corentin #define EMAC_TX_TIMEOUT_INT     BIT(3)
2319f93ac8dSLABBE Corentin #define EMAC_TX_UNDERFLOW_INT   BIT(4)
2329f93ac8dSLABBE Corentin #define EMAC_TX_EARLY_INT       BIT(5)
2339f93ac8dSLABBE Corentin #define EMAC_RX_INT             BIT(8)
2349f93ac8dSLABBE Corentin #define EMAC_RX_BUF_UA_INT      BIT(9)
2359f93ac8dSLABBE Corentin #define EMAC_RX_DMA_STOP_INT    BIT(10)
2369f93ac8dSLABBE Corentin #define EMAC_RX_TIMEOUT_INT     BIT(11)
2379f93ac8dSLABBE Corentin #define EMAC_RX_OVERFLOW_INT    BIT(12)
2389f93ac8dSLABBE Corentin #define EMAC_RX_EARLY_INT       BIT(13)
2399f93ac8dSLABBE Corentin #define EMAC_RGMII_STA_INT      BIT(16)
2409f93ac8dSLABBE Corentin 
2417e1c520cSOng Boon Leong #define EMAC_INT_MSK_COMMON	EMAC_RGMII_STA_INT
2427e1c520cSOng Boon Leong #define EMAC_INT_MSK_TX		(EMAC_TX_INT | \
2437e1c520cSOng Boon Leong 				 EMAC_TX_DMA_STOP_INT | \
2447e1c520cSOng Boon Leong 				 EMAC_TX_BUF_UA_INT | \
2457e1c520cSOng Boon Leong 				 EMAC_TX_TIMEOUT_INT | \
2467e1c520cSOng Boon Leong 				 EMAC_TX_UNDERFLOW_INT | \
2477e1c520cSOng Boon Leong 				 EMAC_TX_EARLY_INT |\
2487e1c520cSOng Boon Leong 				 EMAC_INT_MSK_COMMON)
2497e1c520cSOng Boon Leong #define EMAC_INT_MSK_RX		(EMAC_RX_INT | \
2507e1c520cSOng Boon Leong 				 EMAC_RX_BUF_UA_INT | \
2517e1c520cSOng Boon Leong 				 EMAC_RX_DMA_STOP_INT | \
2527e1c520cSOng Boon Leong 				 EMAC_RX_TIMEOUT_INT | \
2537e1c520cSOng Boon Leong 				 EMAC_RX_OVERFLOW_INT | \
2547e1c520cSOng Boon Leong 				 EMAC_RX_EARLY_INT | \
2557e1c520cSOng Boon Leong 				 EMAC_INT_MSK_COMMON)
2567e1c520cSOng Boon Leong 
2579f93ac8dSLABBE Corentin #define MAC_ADDR_TYPE_DST BIT(31)
2589f93ac8dSLABBE Corentin 
2599f93ac8dSLABBE Corentin /* H3 specific bits for EPHY */
2609f93ac8dSLABBE Corentin #define H3_EPHY_ADDR_SHIFT	20
2611450ba8aSIcenowy Zheng #define H3_EPHY_CLK_SEL		BIT(18) /* 1: 24MHz, 0: 25MHz */
2629f93ac8dSLABBE Corentin #define H3_EPHY_LED_POL		BIT(17) /* 1: active low, 0: active high */
2639f93ac8dSLABBE Corentin #define H3_EPHY_SHUTDOWN	BIT(16) /* 1: shutdown, 0: power up */
2649f93ac8dSLABBE Corentin #define H3_EPHY_SELECT		BIT(15) /* 1: internal PHY, 0: external PHY */
265634db83bSCorentin Labbe #define H3_EPHY_MUX_MASK	(H3_EPHY_SHUTDOWN | H3_EPHY_SELECT)
266634db83bSCorentin Labbe #define DWMAC_SUN8I_MDIO_MUX_INTERNAL_ID	1
267634db83bSCorentin Labbe #define DWMAC_SUN8I_MDIO_MUX_EXTERNAL_ID	2
2689f93ac8dSLABBE Corentin 
2699f93ac8dSLABBE Corentin /* H3/A64 specific bits */
2709f93ac8dSLABBE Corentin #define SYSCON_RMII_EN		BIT(13) /* 1: enable RMII (overrides EPIT) */
2719f93ac8dSLABBE Corentin 
2729f93ac8dSLABBE Corentin /* Generic system control EMAC_CLK bits */
2739f93ac8dSLABBE Corentin #define SYSCON_ETXDC_SHIFT		10
2749f93ac8dSLABBE Corentin #define SYSCON_ERXDC_SHIFT		5
2759f93ac8dSLABBE Corentin /* EMAC PHY Interface Type */
2769f93ac8dSLABBE Corentin #define SYSCON_EPIT			BIT(2) /* 1: RGMII, 0: MII */
2779f93ac8dSLABBE Corentin #define SYSCON_ETCS_MASK		GENMASK(1, 0)
2789f93ac8dSLABBE Corentin #define SYSCON_ETCS_MII		0x0
2799f93ac8dSLABBE Corentin #define SYSCON_ETCS_EXT_GMII	0x1
2809f93ac8dSLABBE Corentin #define SYSCON_ETCS_INT_GMII	0x2
2819f93ac8dSLABBE Corentin 
2829f93ac8dSLABBE Corentin /* sun8i_dwmac_dma_reset() - reset the EMAC
2839f93ac8dSLABBE Corentin  * Called from stmmac via stmmac_dma_ops->reset
2849f93ac8dSLABBE Corentin  */
2859f93ac8dSLABBE Corentin static int sun8i_dwmac_dma_reset(void __iomem *ioaddr)
2869f93ac8dSLABBE Corentin {
2879f93ac8dSLABBE Corentin 	writel(0, ioaddr + EMAC_RX_CTL1);
2889f93ac8dSLABBE Corentin 	writel(0, ioaddr + EMAC_TX_CTL1);
2899f93ac8dSLABBE Corentin 	writel(0, ioaddr + EMAC_RX_FRM_FLT);
2909f93ac8dSLABBE Corentin 	writel(0, ioaddr + EMAC_RX_DESC_LIST);
2919f93ac8dSLABBE Corentin 	writel(0, ioaddr + EMAC_TX_DESC_LIST);
2929f93ac8dSLABBE Corentin 	writel(0, ioaddr + EMAC_INT_EN);
2939f93ac8dSLABBE Corentin 	writel(0x1FFFFFF, ioaddr + EMAC_INT_STA);
2949f93ac8dSLABBE Corentin 	return 0;
2959f93ac8dSLABBE Corentin }
2969f93ac8dSLABBE Corentin 
2979f93ac8dSLABBE Corentin /* sun8i_dwmac_dma_init() - initialize the EMAC
2989f93ac8dSLABBE Corentin  * Called from stmmac via stmmac_dma_ops->init
2999f93ac8dSLABBE Corentin  */
3009f93ac8dSLABBE Corentin static void sun8i_dwmac_dma_init(void __iomem *ioaddr,
30124aaed0cSJose Abreu 				 struct stmmac_dma_cfg *dma_cfg, int atds)
3029f93ac8dSLABBE Corentin {
3039f93ac8dSLABBE Corentin 	writel(EMAC_RX_INT | EMAC_TX_INT, ioaddr + EMAC_INT_EN);
3049f93ac8dSLABBE Corentin 	writel(0x1FFFFFF, ioaddr + EMAC_INT_STA);
3059f93ac8dSLABBE Corentin }
3069f93ac8dSLABBE Corentin 
30724aaed0cSJose Abreu static void sun8i_dwmac_dma_init_rx(void __iomem *ioaddr,
30824aaed0cSJose Abreu 				    struct stmmac_dma_cfg *dma_cfg,
30906a80a7dSJose Abreu 				    dma_addr_t dma_rx_phy, u32 chan)
31024aaed0cSJose Abreu {
31124aaed0cSJose Abreu 	/* Write RX descriptors address */
31206a80a7dSJose Abreu 	writel(lower_32_bits(dma_rx_phy), ioaddr + EMAC_RX_DESC_LIST);
31324aaed0cSJose Abreu }
31424aaed0cSJose Abreu 
31524aaed0cSJose Abreu static void sun8i_dwmac_dma_init_tx(void __iomem *ioaddr,
31624aaed0cSJose Abreu 				    struct stmmac_dma_cfg *dma_cfg,
31706a80a7dSJose Abreu 				    dma_addr_t dma_tx_phy, u32 chan)
31824aaed0cSJose Abreu {
31924aaed0cSJose Abreu 	/* Write TX descriptors address */
32006a80a7dSJose Abreu 	writel(lower_32_bits(dma_tx_phy), ioaddr + EMAC_TX_DESC_LIST);
32124aaed0cSJose Abreu }
32224aaed0cSJose Abreu 
3239f93ac8dSLABBE Corentin /* sun8i_dwmac_dump_regs() - Dump EMAC address space
3249f93ac8dSLABBE Corentin  * Called from stmmac_dma_ops->dump_regs
3259f93ac8dSLABBE Corentin  * Used for ethtool
3269f93ac8dSLABBE Corentin  */
3279f93ac8dSLABBE Corentin static void sun8i_dwmac_dump_regs(void __iomem *ioaddr, u32 *reg_space)
3289f93ac8dSLABBE Corentin {
3299f93ac8dSLABBE Corentin 	int i;
3309f93ac8dSLABBE Corentin 
3319f93ac8dSLABBE Corentin 	for (i = 0; i < 0xC8; i += 4) {
3329f93ac8dSLABBE Corentin 		if (i == 0x32 || i == 0x3C)
3339f93ac8dSLABBE Corentin 			continue;
3349f93ac8dSLABBE Corentin 		reg_space[i / 4] = readl(ioaddr + i);
3359f93ac8dSLABBE Corentin 	}
3369f93ac8dSLABBE Corentin }
3379f93ac8dSLABBE Corentin 
3389f93ac8dSLABBE Corentin /* sun8i_dwmac_dump_mac_regs() - Dump EMAC address space
3399f93ac8dSLABBE Corentin  * Called from stmmac_ops->dump_regs
3409f93ac8dSLABBE Corentin  * Used for ethtool
3419f93ac8dSLABBE Corentin  */
3429f93ac8dSLABBE Corentin static void sun8i_dwmac_dump_mac_regs(struct mac_device_info *hw,
3439f93ac8dSLABBE Corentin 				      u32 *reg_space)
3449f93ac8dSLABBE Corentin {
3459f93ac8dSLABBE Corentin 	int i;
3469f93ac8dSLABBE Corentin 	void __iomem *ioaddr = hw->pcsr;
3479f93ac8dSLABBE Corentin 
3489f93ac8dSLABBE Corentin 	for (i = 0; i < 0xC8; i += 4) {
3499f93ac8dSLABBE Corentin 		if (i == 0x32 || i == 0x3C)
3509f93ac8dSLABBE Corentin 			continue;
3519f93ac8dSLABBE Corentin 		reg_space[i / 4] = readl(ioaddr + i);
3529f93ac8dSLABBE Corentin 	}
3539f93ac8dSLABBE Corentin }
3549f93ac8dSLABBE Corentin 
355021bd5e3SJose Abreu static void sun8i_dwmac_enable_dma_irq(void __iomem *ioaddr, u32 chan,
356021bd5e3SJose Abreu 				       bool rx, bool tx)
3579f93ac8dSLABBE Corentin {
358021bd5e3SJose Abreu 	u32 value = readl(ioaddr + EMAC_INT_EN);
359021bd5e3SJose Abreu 
360021bd5e3SJose Abreu 	if (rx)
361021bd5e3SJose Abreu 		value |= EMAC_RX_INT;
362021bd5e3SJose Abreu 	if (tx)
363021bd5e3SJose Abreu 		value |= EMAC_TX_INT;
364021bd5e3SJose Abreu 
365021bd5e3SJose Abreu 	writel(value, ioaddr + EMAC_INT_EN);
3669f93ac8dSLABBE Corentin }
3679f93ac8dSLABBE Corentin 
368021bd5e3SJose Abreu static void sun8i_dwmac_disable_dma_irq(void __iomem *ioaddr, u32 chan,
369021bd5e3SJose Abreu 					bool rx, bool tx)
3709f93ac8dSLABBE Corentin {
371021bd5e3SJose Abreu 	u32 value = readl(ioaddr + EMAC_INT_EN);
372021bd5e3SJose Abreu 
373021bd5e3SJose Abreu 	if (rx)
374021bd5e3SJose Abreu 		value &= ~EMAC_RX_INT;
375021bd5e3SJose Abreu 	if (tx)
376021bd5e3SJose Abreu 		value &= ~EMAC_TX_INT;
377021bd5e3SJose Abreu 
378021bd5e3SJose Abreu 	writel(value, ioaddr + EMAC_INT_EN);
3799f93ac8dSLABBE Corentin }
3809f93ac8dSLABBE Corentin 
3819f93ac8dSLABBE Corentin static void sun8i_dwmac_dma_start_tx(void __iomem *ioaddr, u32 chan)
3829f93ac8dSLABBE Corentin {
3839f93ac8dSLABBE Corentin 	u32 v;
3849f93ac8dSLABBE Corentin 
3859f93ac8dSLABBE Corentin 	v = readl(ioaddr + EMAC_TX_CTL1);
3869f93ac8dSLABBE Corentin 	v |= EMAC_TX_DMA_START;
3879f93ac8dSLABBE Corentin 	v |= EMAC_TX_DMA_EN;
3889f93ac8dSLABBE Corentin 	writel(v, ioaddr + EMAC_TX_CTL1);
3899f93ac8dSLABBE Corentin }
3909f93ac8dSLABBE Corentin 
3919f93ac8dSLABBE Corentin static void sun8i_dwmac_enable_dma_transmission(void __iomem *ioaddr)
3929f93ac8dSLABBE Corentin {
3939f93ac8dSLABBE Corentin 	u32 v;
3949f93ac8dSLABBE Corentin 
3959f93ac8dSLABBE Corentin 	v = readl(ioaddr + EMAC_TX_CTL1);
3969f93ac8dSLABBE Corentin 	v |= EMAC_TX_DMA_START;
3979f93ac8dSLABBE Corentin 	v |= EMAC_TX_DMA_EN;
3989f93ac8dSLABBE Corentin 	writel(v, ioaddr + EMAC_TX_CTL1);
3999f93ac8dSLABBE Corentin }
4009f93ac8dSLABBE Corentin 
4019f93ac8dSLABBE Corentin static void sun8i_dwmac_dma_stop_tx(void __iomem *ioaddr, u32 chan)
4029f93ac8dSLABBE Corentin {
4039f93ac8dSLABBE Corentin 	u32 v;
4049f93ac8dSLABBE Corentin 
4059f93ac8dSLABBE Corentin 	v = readl(ioaddr + EMAC_TX_CTL1);
4069f93ac8dSLABBE Corentin 	v &= ~EMAC_TX_DMA_EN;
4079f93ac8dSLABBE Corentin 	writel(v, ioaddr + EMAC_TX_CTL1);
4089f93ac8dSLABBE Corentin }
4099f93ac8dSLABBE Corentin 
4109f93ac8dSLABBE Corentin static void sun8i_dwmac_dma_start_rx(void __iomem *ioaddr, u32 chan)
4119f93ac8dSLABBE Corentin {
4129f93ac8dSLABBE Corentin 	u32 v;
4139f93ac8dSLABBE Corentin 
4149f93ac8dSLABBE Corentin 	v = readl(ioaddr + EMAC_RX_CTL1);
4159f93ac8dSLABBE Corentin 	v |= EMAC_RX_DMA_START;
4169f93ac8dSLABBE Corentin 	v |= EMAC_RX_DMA_EN;
4179f93ac8dSLABBE Corentin 	writel(v, ioaddr + EMAC_RX_CTL1);
4189f93ac8dSLABBE Corentin }
4199f93ac8dSLABBE Corentin 
4209f93ac8dSLABBE Corentin static void sun8i_dwmac_dma_stop_rx(void __iomem *ioaddr, u32 chan)
4219f93ac8dSLABBE Corentin {
4229f93ac8dSLABBE Corentin 	u32 v;
4239f93ac8dSLABBE Corentin 
4249f93ac8dSLABBE Corentin 	v = readl(ioaddr + EMAC_RX_CTL1);
4259f93ac8dSLABBE Corentin 	v &= ~EMAC_RX_DMA_EN;
4269f93ac8dSLABBE Corentin 	writel(v, ioaddr + EMAC_RX_CTL1);
4279f93ac8dSLABBE Corentin }
4289f93ac8dSLABBE Corentin 
4299f93ac8dSLABBE Corentin static int sun8i_dwmac_dma_interrupt(void __iomem *ioaddr,
4307e1c520cSOng Boon Leong 				     struct stmmac_extra_stats *x, u32 chan,
4317e1c520cSOng Boon Leong 				     u32 dir)
4329f93ac8dSLABBE Corentin {
4339f93ac8dSLABBE Corentin 	u32 v;
4349f93ac8dSLABBE Corentin 	int ret = 0;
4359f93ac8dSLABBE Corentin 
4369f93ac8dSLABBE Corentin 	v = readl(ioaddr + EMAC_INT_STA);
4379f93ac8dSLABBE Corentin 
4387e1c520cSOng Boon Leong 	if (dir == DMA_DIR_RX)
4397e1c520cSOng Boon Leong 		v &= EMAC_INT_MSK_RX;
4407e1c520cSOng Boon Leong 	else if (dir == DMA_DIR_TX)
4417e1c520cSOng Boon Leong 		v &= EMAC_INT_MSK_TX;
4427e1c520cSOng Boon Leong 
4439f93ac8dSLABBE Corentin 	if (v & EMAC_TX_INT) {
4449f93ac8dSLABBE Corentin 		ret |= handle_tx;
4459f93ac8dSLABBE Corentin 		x->tx_normal_irq_n++;
4469f93ac8dSLABBE Corentin 	}
4479f93ac8dSLABBE Corentin 
4489f93ac8dSLABBE Corentin 	if (v & EMAC_TX_DMA_STOP_INT)
4499f93ac8dSLABBE Corentin 		x->tx_process_stopped_irq++;
4509f93ac8dSLABBE Corentin 
4519f93ac8dSLABBE Corentin 	if (v & EMAC_TX_BUF_UA_INT)
4529f93ac8dSLABBE Corentin 		x->tx_process_stopped_irq++;
4539f93ac8dSLABBE Corentin 
4549f93ac8dSLABBE Corentin 	if (v & EMAC_TX_TIMEOUT_INT)
4559f93ac8dSLABBE Corentin 		ret |= tx_hard_error;
4569f93ac8dSLABBE Corentin 
4579f93ac8dSLABBE Corentin 	if (v & EMAC_TX_UNDERFLOW_INT) {
4589f93ac8dSLABBE Corentin 		ret |= tx_hard_error;
4599f93ac8dSLABBE Corentin 		x->tx_undeflow_irq++;
4609f93ac8dSLABBE Corentin 	}
4619f93ac8dSLABBE Corentin 
4629f93ac8dSLABBE Corentin 	if (v & EMAC_TX_EARLY_INT)
4639f93ac8dSLABBE Corentin 		x->tx_early_irq++;
4649f93ac8dSLABBE Corentin 
4659f93ac8dSLABBE Corentin 	if (v & EMAC_RX_INT) {
4669f93ac8dSLABBE Corentin 		ret |= handle_rx;
4679f93ac8dSLABBE Corentin 		x->rx_normal_irq_n++;
4689f93ac8dSLABBE Corentin 	}
4699f93ac8dSLABBE Corentin 
4709f93ac8dSLABBE Corentin 	if (v & EMAC_RX_BUF_UA_INT)
4719f93ac8dSLABBE Corentin 		x->rx_buf_unav_irq++;
4729f93ac8dSLABBE Corentin 
4739f93ac8dSLABBE Corentin 	if (v & EMAC_RX_DMA_STOP_INT)
4749f93ac8dSLABBE Corentin 		x->rx_process_stopped_irq++;
4759f93ac8dSLABBE Corentin 
4769f93ac8dSLABBE Corentin 	if (v & EMAC_RX_TIMEOUT_INT)
4779f93ac8dSLABBE Corentin 		ret |= tx_hard_error;
4789f93ac8dSLABBE Corentin 
4799f93ac8dSLABBE Corentin 	if (v & EMAC_RX_OVERFLOW_INT) {
4809f93ac8dSLABBE Corentin 		ret |= tx_hard_error;
4819f93ac8dSLABBE Corentin 		x->rx_overflow_irq++;
4829f93ac8dSLABBE Corentin 	}
4839f93ac8dSLABBE Corentin 
4849f93ac8dSLABBE Corentin 	if (v & EMAC_RX_EARLY_INT)
4859f93ac8dSLABBE Corentin 		x->rx_early_irq++;
4869f93ac8dSLABBE Corentin 
4879f93ac8dSLABBE Corentin 	if (v & EMAC_RGMII_STA_INT)
4889f93ac8dSLABBE Corentin 		x->irq_rgmii_n++;
4899f93ac8dSLABBE Corentin 
4909f93ac8dSLABBE Corentin 	writel(v, ioaddr + EMAC_INT_STA);
4919f93ac8dSLABBE Corentin 
4929f93ac8dSLABBE Corentin 	return ret;
4939f93ac8dSLABBE Corentin }
4949f93ac8dSLABBE Corentin 
495ab0204e3SJose Abreu static void sun8i_dwmac_dma_operation_mode_rx(void __iomem *ioaddr, int mode,
496ab0204e3SJose Abreu 					      u32 channel, int fifosz, u8 qmode)
497ab0204e3SJose Abreu {
498ab0204e3SJose Abreu 	u32 v;
499ab0204e3SJose Abreu 
500ab0204e3SJose Abreu 	v = readl(ioaddr + EMAC_RX_CTL1);
501ab0204e3SJose Abreu 	if (mode == SF_DMA_MODE) {
502ab0204e3SJose Abreu 		v |= EMAC_RX_MD;
503ab0204e3SJose Abreu 	} else {
504ab0204e3SJose Abreu 		v &= ~EMAC_RX_MD;
505ab0204e3SJose Abreu 		v &= ~EMAC_RX_TH_MASK;
506ab0204e3SJose Abreu 		if (mode < 32)
507ab0204e3SJose Abreu 			v |= EMAC_RX_TH_32;
508ab0204e3SJose Abreu 		else if (mode < 64)
509ab0204e3SJose Abreu 			v |= EMAC_RX_TH_64;
510ab0204e3SJose Abreu 		else if (mode < 96)
511ab0204e3SJose Abreu 			v |= EMAC_RX_TH_96;
512ab0204e3SJose Abreu 		else if (mode < 128)
513ab0204e3SJose Abreu 			v |= EMAC_RX_TH_128;
514ab0204e3SJose Abreu 	}
515ab0204e3SJose Abreu 	writel(v, ioaddr + EMAC_RX_CTL1);
516ab0204e3SJose Abreu }
517ab0204e3SJose Abreu 
518ab0204e3SJose Abreu static void sun8i_dwmac_dma_operation_mode_tx(void __iomem *ioaddr, int mode,
519ab0204e3SJose Abreu 					      u32 channel, int fifosz, u8 qmode)
5209f93ac8dSLABBE Corentin {
5219f93ac8dSLABBE Corentin 	u32 v;
5229f93ac8dSLABBE Corentin 
5239f93ac8dSLABBE Corentin 	v = readl(ioaddr + EMAC_TX_CTL1);
524ab0204e3SJose Abreu 	if (mode == SF_DMA_MODE) {
5259f93ac8dSLABBE Corentin 		v |= EMAC_TX_MD;
5269f93ac8dSLABBE Corentin 		/* Undocumented bit (called TX_NEXT_FRM in BSP), the original
5279f93ac8dSLABBE Corentin 		 * comment is
5289f93ac8dSLABBE Corentin 		 * "Operating on second frame increase the performance
5299f93ac8dSLABBE Corentin 		 * especially when transmit store-and-forward is used."
5309f93ac8dSLABBE Corentin 		 */
5319f93ac8dSLABBE Corentin 		v |= EMAC_TX_NEXT_FRM;
5329f93ac8dSLABBE Corentin 	} else {
5339f93ac8dSLABBE Corentin 		v &= ~EMAC_TX_MD;
5349f93ac8dSLABBE Corentin 		v &= ~EMAC_TX_TH_MASK;
535ab0204e3SJose Abreu 		if (mode < 64)
5369f93ac8dSLABBE Corentin 			v |= EMAC_TX_TH_64;
537ab0204e3SJose Abreu 		else if (mode < 128)
5389f93ac8dSLABBE Corentin 			v |= EMAC_TX_TH_128;
539ab0204e3SJose Abreu 		else if (mode < 192)
5409f93ac8dSLABBE Corentin 			v |= EMAC_TX_TH_192;
541ab0204e3SJose Abreu 		else if (mode < 256)
5429f93ac8dSLABBE Corentin 			v |= EMAC_TX_TH_256;
5439f93ac8dSLABBE Corentin 	}
5449f93ac8dSLABBE Corentin 	writel(v, ioaddr + EMAC_TX_CTL1);
5459f93ac8dSLABBE Corentin }
5469f93ac8dSLABBE Corentin 
5479f93ac8dSLABBE Corentin static const struct stmmac_dma_ops sun8i_dwmac_dma_ops = {
5489f93ac8dSLABBE Corentin 	.reset = sun8i_dwmac_dma_reset,
5499f93ac8dSLABBE Corentin 	.init = sun8i_dwmac_dma_init,
55024aaed0cSJose Abreu 	.init_rx_chan = sun8i_dwmac_dma_init_rx,
55124aaed0cSJose Abreu 	.init_tx_chan = sun8i_dwmac_dma_init_tx,
5529f93ac8dSLABBE Corentin 	.dump_regs = sun8i_dwmac_dump_regs,
553ab0204e3SJose Abreu 	.dma_rx_mode = sun8i_dwmac_dma_operation_mode_rx,
554ab0204e3SJose Abreu 	.dma_tx_mode = sun8i_dwmac_dma_operation_mode_tx,
5559f93ac8dSLABBE Corentin 	.enable_dma_transmission = sun8i_dwmac_enable_dma_transmission,
5569f93ac8dSLABBE Corentin 	.enable_dma_irq = sun8i_dwmac_enable_dma_irq,
5579f93ac8dSLABBE Corentin 	.disable_dma_irq = sun8i_dwmac_disable_dma_irq,
5589f93ac8dSLABBE Corentin 	.start_tx = sun8i_dwmac_dma_start_tx,
5599f93ac8dSLABBE Corentin 	.stop_tx = sun8i_dwmac_dma_stop_tx,
5609f93ac8dSLABBE Corentin 	.start_rx = sun8i_dwmac_dma_start_rx,
5619f93ac8dSLABBE Corentin 	.stop_rx = sun8i_dwmac_dma_stop_rx,
5629f93ac8dSLABBE Corentin 	.dma_interrupt = sun8i_dwmac_dma_interrupt,
5639f93ac8dSLABBE Corentin };
5649f93ac8dSLABBE Corentin 
565b8239638SSamuel Holland static int sun8i_dwmac_power_internal_phy(struct stmmac_priv *priv);
566b8239638SSamuel Holland 
5679f93ac8dSLABBE Corentin static int sun8i_dwmac_init(struct platform_device *pdev, void *priv)
5689f93ac8dSLABBE Corentin {
569b8239638SSamuel Holland 	struct net_device *ndev = platform_get_drvdata(pdev);
5709f93ac8dSLABBE Corentin 	struct sunxi_priv_data *gmac = priv;
5719f93ac8dSLABBE Corentin 	int ret;
5729f93ac8dSLABBE Corentin 
5739f93ac8dSLABBE Corentin 	if (gmac->regulator) {
5749f93ac8dSLABBE Corentin 		ret = regulator_enable(gmac->regulator);
5759f93ac8dSLABBE Corentin 		if (ret) {
5769f93ac8dSLABBE Corentin 			dev_err(&pdev->dev, "Fail to enable regulator\n");
5779f93ac8dSLABBE Corentin 			return ret;
5789f93ac8dSLABBE Corentin 		}
5799f93ac8dSLABBE Corentin 	}
5809f93ac8dSLABBE Corentin 
581b8239638SSamuel Holland 	if (gmac->use_internal_phy) {
582b8239638SSamuel Holland 		ret = sun8i_dwmac_power_internal_phy(netdev_priv(ndev));
583b8239638SSamuel Holland 		if (ret)
584b76bbb34SJisheng Zhang 			goto err_disable_regulator;
5859f93ac8dSLABBE Corentin 	}
5869f93ac8dSLABBE Corentin 
5879f93ac8dSLABBE Corentin 	return 0;
588b8239638SSamuel Holland 
589b8239638SSamuel Holland err_disable_regulator:
590b8239638SSamuel Holland 	if (gmac->regulator)
591b8239638SSamuel Holland 		regulator_disable(gmac->regulator);
592b8239638SSamuel Holland 
593b8239638SSamuel Holland 	return ret;
5949f93ac8dSLABBE Corentin }
5959f93ac8dSLABBE Corentin 
5968cad443eSFlorian Fainelli static void sun8i_dwmac_core_init(struct mac_device_info *hw,
5978cad443eSFlorian Fainelli 				  struct net_device *dev)
5989f93ac8dSLABBE Corentin {
5999f93ac8dSLABBE Corentin 	void __iomem *ioaddr = hw->pcsr;
6009f93ac8dSLABBE Corentin 	u32 v;
6019f93ac8dSLABBE Corentin 
6029f93ac8dSLABBE Corentin 	v = (8 << EMAC_BURSTLEN_SHIFT); /* burst len */
6039f93ac8dSLABBE Corentin 	writel(v, ioaddr + EMAC_BASIC_CTL1);
6049f93ac8dSLABBE Corentin }
6059f93ac8dSLABBE Corentin 
6069f93ac8dSLABBE Corentin static void sun8i_dwmac_set_mac(void __iomem *ioaddr, bool enable)
6079f93ac8dSLABBE Corentin {
6089f93ac8dSLABBE Corentin 	u32 t, r;
6099f93ac8dSLABBE Corentin 
6109f93ac8dSLABBE Corentin 	t = readl(ioaddr + EMAC_TX_CTL0);
6119f93ac8dSLABBE Corentin 	r = readl(ioaddr + EMAC_RX_CTL0);
6129f93ac8dSLABBE Corentin 	if (enable) {
6139f93ac8dSLABBE Corentin 		t |= EMAC_TX_TRANSMITTER_EN;
6149f93ac8dSLABBE Corentin 		r |= EMAC_RX_RECEIVER_EN;
6159f93ac8dSLABBE Corentin 	} else {
6169f93ac8dSLABBE Corentin 		t &= ~EMAC_TX_TRANSMITTER_EN;
6179f93ac8dSLABBE Corentin 		r &= ~EMAC_RX_RECEIVER_EN;
6189f93ac8dSLABBE Corentin 	}
6199f93ac8dSLABBE Corentin 	writel(t, ioaddr + EMAC_TX_CTL0);
6209f93ac8dSLABBE Corentin 	writel(r, ioaddr + EMAC_RX_CTL0);
6219f93ac8dSLABBE Corentin }
6229f93ac8dSLABBE Corentin 
6239f93ac8dSLABBE Corentin /* Set MAC address at slot reg_n
6249f93ac8dSLABBE Corentin  * All slot > 0 need to be enabled with MAC_ADDR_TYPE_DST
6259f93ac8dSLABBE Corentin  * If addr is NULL, clear the slot
6269f93ac8dSLABBE Corentin  */
6279f93ac8dSLABBE Corentin static void sun8i_dwmac_set_umac_addr(struct mac_device_info *hw,
62876660757SJakub Kicinski 				      const unsigned char *addr,
6299f93ac8dSLABBE Corentin 				      unsigned int reg_n)
6309f93ac8dSLABBE Corentin {
6319f93ac8dSLABBE Corentin 	void __iomem *ioaddr = hw->pcsr;
6329f93ac8dSLABBE Corentin 	u32 v;
6339f93ac8dSLABBE Corentin 
6349f93ac8dSLABBE Corentin 	if (!addr) {
6359f93ac8dSLABBE Corentin 		writel(0, ioaddr + EMAC_MACADDR_HI(reg_n));
6369f93ac8dSLABBE Corentin 		return;
6379f93ac8dSLABBE Corentin 	}
6389f93ac8dSLABBE Corentin 
6399f93ac8dSLABBE Corentin 	stmmac_set_mac_addr(ioaddr, addr, EMAC_MACADDR_HI(reg_n),
6409f93ac8dSLABBE Corentin 			    EMAC_MACADDR_LO(reg_n));
6419f93ac8dSLABBE Corentin 	if (reg_n > 0) {
6429f93ac8dSLABBE Corentin 		v = readl(ioaddr + EMAC_MACADDR_HI(reg_n));
6439f93ac8dSLABBE Corentin 		v |= MAC_ADDR_TYPE_DST;
6449f93ac8dSLABBE Corentin 		writel(v, ioaddr + EMAC_MACADDR_HI(reg_n));
6459f93ac8dSLABBE Corentin 	}
6469f93ac8dSLABBE Corentin }
6479f93ac8dSLABBE Corentin 
6489f93ac8dSLABBE Corentin static void sun8i_dwmac_get_umac_addr(struct mac_device_info *hw,
6499f93ac8dSLABBE Corentin 				      unsigned char *addr,
6509f93ac8dSLABBE Corentin 				      unsigned int reg_n)
6519f93ac8dSLABBE Corentin {
6529f93ac8dSLABBE Corentin 	void __iomem *ioaddr = hw->pcsr;
6539f93ac8dSLABBE Corentin 
6549f93ac8dSLABBE Corentin 	stmmac_get_mac_addr(ioaddr, addr, EMAC_MACADDR_HI(reg_n),
6559f93ac8dSLABBE Corentin 			    EMAC_MACADDR_LO(reg_n));
6569f93ac8dSLABBE Corentin }
6579f93ac8dSLABBE Corentin 
6589f93ac8dSLABBE Corentin /* caution this function must return non 0 to work */
6599f93ac8dSLABBE Corentin static int sun8i_dwmac_rx_ipc_enable(struct mac_device_info *hw)
6609f93ac8dSLABBE Corentin {
6619f93ac8dSLABBE Corentin 	void __iomem *ioaddr = hw->pcsr;
6629f93ac8dSLABBE Corentin 	u32 v;
6639f93ac8dSLABBE Corentin 
6649f93ac8dSLABBE Corentin 	v = readl(ioaddr + EMAC_RX_CTL0);
6659f93ac8dSLABBE Corentin 	v |= EMAC_RX_DO_CRC;
6669f93ac8dSLABBE Corentin 	writel(v, ioaddr + EMAC_RX_CTL0);
6679f93ac8dSLABBE Corentin 
6689f93ac8dSLABBE Corentin 	return 1;
6699f93ac8dSLABBE Corentin }
6709f93ac8dSLABBE Corentin 
6719f93ac8dSLABBE Corentin static void sun8i_dwmac_set_filter(struct mac_device_info *hw,
6729f93ac8dSLABBE Corentin 				   struct net_device *dev)
6739f93ac8dSLABBE Corentin {
6749f93ac8dSLABBE Corentin 	void __iomem *ioaddr = hw->pcsr;
6759f93ac8dSLABBE Corentin 	u32 v;
6769f93ac8dSLABBE Corentin 	int i = 1;
6779f93ac8dSLABBE Corentin 	struct netdev_hw_addr *ha;
6789f93ac8dSLABBE Corentin 	int macaddrs = netdev_uc_count(dev) + netdev_mc_count(dev) + 1;
6799f93ac8dSLABBE Corentin 
6809f93ac8dSLABBE Corentin 	v = EMAC_FRM_FLT_CTL;
6819f93ac8dSLABBE Corentin 
6829f93ac8dSLABBE Corentin 	if (dev->flags & IFF_PROMISC) {
6839f93ac8dSLABBE Corentin 		v = EMAC_FRM_FLT_RXALL;
6849f93ac8dSLABBE Corentin 	} else if (dev->flags & IFF_ALLMULTI) {
6859f93ac8dSLABBE Corentin 		v |= EMAC_FRM_FLT_MULTICAST;
6869f93ac8dSLABBE Corentin 	} else if (macaddrs <= hw->unicast_filter_entries) {
6879f93ac8dSLABBE Corentin 		if (!netdev_mc_empty(dev)) {
6889f93ac8dSLABBE Corentin 			netdev_for_each_mc_addr(ha, dev) {
6899f93ac8dSLABBE Corentin 				sun8i_dwmac_set_umac_addr(hw, ha->addr, i);
6909f93ac8dSLABBE Corentin 				i++;
6919f93ac8dSLABBE Corentin 			}
6929f93ac8dSLABBE Corentin 		}
6939f93ac8dSLABBE Corentin 		if (!netdev_uc_empty(dev)) {
6949f93ac8dSLABBE Corentin 			netdev_for_each_uc_addr(ha, dev) {
6959f93ac8dSLABBE Corentin 				sun8i_dwmac_set_umac_addr(hw, ha->addr, i);
6969f93ac8dSLABBE Corentin 				i++;
6979f93ac8dSLABBE Corentin 			}
6989f93ac8dSLABBE Corentin 		}
6999f93ac8dSLABBE Corentin 	} else {
70005908d72SMans Rullgard 		if (!(readl(ioaddr + EMAC_RX_FRM_FLT) & EMAC_FRM_FLT_RXALL))
7019f93ac8dSLABBE Corentin 			netdev_info(dev, "Too many address, switching to promiscuous\n");
7029f93ac8dSLABBE Corentin 		v = EMAC_FRM_FLT_RXALL;
7039f93ac8dSLABBE Corentin 	}
7049f93ac8dSLABBE Corentin 
7059f93ac8dSLABBE Corentin 	/* Disable unused address filter slots */
7069f93ac8dSLABBE Corentin 	while (i < hw->unicast_filter_entries)
7079f93ac8dSLABBE Corentin 		sun8i_dwmac_set_umac_addr(hw, NULL, i++);
7089f93ac8dSLABBE Corentin 
7099f93ac8dSLABBE Corentin 	writel(v, ioaddr + EMAC_RX_FRM_FLT);
7109f93ac8dSLABBE Corentin }
7119f93ac8dSLABBE Corentin 
7129f93ac8dSLABBE Corentin static void sun8i_dwmac_flow_ctrl(struct mac_device_info *hw,
7139f93ac8dSLABBE Corentin 				  unsigned int duplex, unsigned int fc,
7149f93ac8dSLABBE Corentin 				  unsigned int pause_time, u32 tx_cnt)
7159f93ac8dSLABBE Corentin {
7169f93ac8dSLABBE Corentin 	void __iomem *ioaddr = hw->pcsr;
7179f93ac8dSLABBE Corentin 	u32 v;
7189f93ac8dSLABBE Corentin 
7199f93ac8dSLABBE Corentin 	v = readl(ioaddr + EMAC_RX_CTL0);
7209f93ac8dSLABBE Corentin 	if (fc == FLOW_AUTO)
7219f93ac8dSLABBE Corentin 		v |= EMAC_RX_FLOW_CTL_EN;
7229f93ac8dSLABBE Corentin 	else
7239f93ac8dSLABBE Corentin 		v &= ~EMAC_RX_FLOW_CTL_EN;
7249f93ac8dSLABBE Corentin 	writel(v, ioaddr + EMAC_RX_CTL0);
7259f93ac8dSLABBE Corentin 
7269f93ac8dSLABBE Corentin 	v = readl(ioaddr + EMAC_TX_FLOW_CTL);
7279f93ac8dSLABBE Corentin 	if (fc == FLOW_AUTO)
7289f93ac8dSLABBE Corentin 		v |= EMAC_TX_FLOW_CTL_EN;
7299f93ac8dSLABBE Corentin 	else
7309f93ac8dSLABBE Corentin 		v &= ~EMAC_TX_FLOW_CTL_EN;
7319f93ac8dSLABBE Corentin 	writel(v, ioaddr + EMAC_TX_FLOW_CTL);
7329f93ac8dSLABBE Corentin }
7339f93ac8dSLABBE Corentin 
7349f93ac8dSLABBE Corentin static int sun8i_dwmac_reset(struct stmmac_priv *priv)
7359f93ac8dSLABBE Corentin {
7369f93ac8dSLABBE Corentin 	u32 v;
7379f93ac8dSLABBE Corentin 	int err;
7389f93ac8dSLABBE Corentin 
7399f93ac8dSLABBE Corentin 	v = readl(priv->ioaddr + EMAC_BASIC_CTL1);
7409f93ac8dSLABBE Corentin 	writel(v | 0x01, priv->ioaddr + EMAC_BASIC_CTL1);
7419f93ac8dSLABBE Corentin 
7429f93ac8dSLABBE Corentin 	/* The timeout was previoulsy set to 10ms, but some board (OrangePI0)
7439f93ac8dSLABBE Corentin 	 * need more if no cable plugged. 100ms seems OK
7449f93ac8dSLABBE Corentin 	 */
7459f93ac8dSLABBE Corentin 	err = readl_poll_timeout(priv->ioaddr + EMAC_BASIC_CTL1, v,
7469f93ac8dSLABBE Corentin 				 !(v & 0x01), 100, 100000);
7479f93ac8dSLABBE Corentin 
7489f93ac8dSLABBE Corentin 	if (err) {
7499f93ac8dSLABBE Corentin 		dev_err(priv->device, "EMAC reset timeout\n");
7509e0db41eSJisheng Zhang 		return err;
7519f93ac8dSLABBE Corentin 	}
7529f93ac8dSLABBE Corentin 	return 0;
7539f93ac8dSLABBE Corentin }
7549f93ac8dSLABBE Corentin 
755634db83bSCorentin Labbe /* Search in mdio-mux node for internal PHY node and get its clk/reset */
756634db83bSCorentin Labbe static int get_ephy_nodes(struct stmmac_priv *priv)
757634db83bSCorentin Labbe {
758634db83bSCorentin Labbe 	struct sunxi_priv_data *gmac = priv->plat->bsp_priv;
759634db83bSCorentin Labbe 	struct device_node *mdio_mux, *iphynode;
760634db83bSCorentin Labbe 	struct device_node *mdio_internal;
761634db83bSCorentin Labbe 	int ret;
762634db83bSCorentin Labbe 
763634db83bSCorentin Labbe 	mdio_mux = of_get_child_by_name(priv->device->of_node, "mdio-mux");
764634db83bSCorentin Labbe 	if (!mdio_mux) {
765634db83bSCorentin Labbe 		dev_err(priv->device, "Cannot get mdio-mux node\n");
766634db83bSCorentin Labbe 		return -ENODEV;
767634db83bSCorentin Labbe 	}
768634db83bSCorentin Labbe 
769ac63043dSJohan Hovold 	mdio_internal = of_get_compatible_child(mdio_mux,
770634db83bSCorentin Labbe 						"allwinner,sun8i-h3-mdio-internal");
771ac63043dSJohan Hovold 	of_node_put(mdio_mux);
772634db83bSCorentin Labbe 	if (!mdio_internal) {
773634db83bSCorentin Labbe 		dev_err(priv->device, "Cannot get internal_mdio node\n");
774634db83bSCorentin Labbe 		return -ENODEV;
775634db83bSCorentin Labbe 	}
776634db83bSCorentin Labbe 
777634db83bSCorentin Labbe 	/* Seek for internal PHY */
778634db83bSCorentin Labbe 	for_each_child_of_node(mdio_internal, iphynode) {
779634db83bSCorentin Labbe 		gmac->ephy_clk = of_clk_get(iphynode, 0);
780634db83bSCorentin Labbe 		if (IS_ERR(gmac->ephy_clk))
781634db83bSCorentin Labbe 			continue;
782634db83bSCorentin Labbe 		gmac->rst_ephy = of_reset_control_get_exclusive(iphynode, NULL);
783634db83bSCorentin Labbe 		if (IS_ERR(gmac->rst_ephy)) {
784634db83bSCorentin Labbe 			ret = PTR_ERR(gmac->rst_ephy);
785ac63043dSJohan Hovold 			if (ret == -EPROBE_DEFER) {
786ac63043dSJohan Hovold 				of_node_put(iphynode);
787ac63043dSJohan Hovold 				of_node_put(mdio_internal);
788634db83bSCorentin Labbe 				return ret;
789ac63043dSJohan Hovold 			}
790634db83bSCorentin Labbe 			continue;
791634db83bSCorentin Labbe 		}
792634db83bSCorentin Labbe 		dev_info(priv->device, "Found internal PHY node\n");
793ac63043dSJohan Hovold 		of_node_put(iphynode);
794ac63043dSJohan Hovold 		of_node_put(mdio_internal);
795634db83bSCorentin Labbe 		return 0;
796634db83bSCorentin Labbe 	}
797ac63043dSJohan Hovold 
798ac63043dSJohan Hovold 	of_node_put(mdio_internal);
799634db83bSCorentin Labbe 	return -ENODEV;
800634db83bSCorentin Labbe }
801634db83bSCorentin Labbe 
802634db83bSCorentin Labbe static int sun8i_dwmac_power_internal_phy(struct stmmac_priv *priv)
803634db83bSCorentin Labbe {
804634db83bSCorentin Labbe 	struct sunxi_priv_data *gmac = priv->plat->bsp_priv;
805634db83bSCorentin Labbe 	int ret;
806634db83bSCorentin Labbe 
807634db83bSCorentin Labbe 	if (gmac->internal_phy_powered) {
808634db83bSCorentin Labbe 		dev_warn(priv->device, "Internal PHY already powered\n");
809634db83bSCorentin Labbe 		return 0;
810634db83bSCorentin Labbe 	}
811634db83bSCorentin Labbe 
812634db83bSCorentin Labbe 	dev_info(priv->device, "Powering internal PHY\n");
813634db83bSCorentin Labbe 	ret = clk_prepare_enable(gmac->ephy_clk);
814634db83bSCorentin Labbe 	if (ret) {
815634db83bSCorentin Labbe 		dev_err(priv->device, "Cannot enable internal PHY\n");
816634db83bSCorentin Labbe 		return ret;
817634db83bSCorentin Labbe 	}
818634db83bSCorentin Labbe 
819634db83bSCorentin Labbe 	/* Make sure the EPHY is properly reseted, as U-Boot may leave
820634db83bSCorentin Labbe 	 * it at deasserted state, and thus it may fail to reset EMAC.
8211c22f546SSamuel Holland 	 *
8221c22f546SSamuel Holland 	 * This assumes the driver has exclusive access to the EPHY reset.
823634db83bSCorentin Labbe 	 */
8241c22f546SSamuel Holland 	ret = reset_control_reset(gmac->rst_ephy);
825634db83bSCorentin Labbe 	if (ret) {
8261c22f546SSamuel Holland 		dev_err(priv->device, "Cannot reset internal PHY\n");
827634db83bSCorentin Labbe 		clk_disable_unprepare(gmac->ephy_clk);
828634db83bSCorentin Labbe 		return ret;
829634db83bSCorentin Labbe 	}
830634db83bSCorentin Labbe 
831634db83bSCorentin Labbe 	gmac->internal_phy_powered = true;
832634db83bSCorentin Labbe 
833634db83bSCorentin Labbe 	return 0;
834634db83bSCorentin Labbe }
835634db83bSCorentin Labbe 
836557ef2dfSSamuel Holland static void sun8i_dwmac_unpower_internal_phy(struct sunxi_priv_data *gmac)
837634db83bSCorentin Labbe {
838634db83bSCorentin Labbe 	if (!gmac->internal_phy_powered)
839557ef2dfSSamuel Holland 		return;
840634db83bSCorentin Labbe 
841634db83bSCorentin Labbe 	clk_disable_unprepare(gmac->ephy_clk);
842634db83bSCorentin Labbe 	reset_control_assert(gmac->rst_ephy);
843634db83bSCorentin Labbe 	gmac->internal_phy_powered = false;
844634db83bSCorentin Labbe }
845634db83bSCorentin Labbe 
846634db83bSCorentin Labbe /* MDIO multiplexing switch function
847634db83bSCorentin Labbe  * This function is called by the mdio-mux layer when it thinks the mdio bus
848634db83bSCorentin Labbe  * multiplexer needs to switch.
849634db83bSCorentin Labbe  * 'current_child' is the current value of the mux register
850634db83bSCorentin Labbe  * 'desired_child' is the value of the 'reg' property of the target child MDIO
851634db83bSCorentin Labbe  * node.
852634db83bSCorentin Labbe  * The first time this function is called, current_child == -1.
853634db83bSCorentin Labbe  * If current_child == desired_child, then the mux is already set to the
854634db83bSCorentin Labbe  * correct bus.
855634db83bSCorentin Labbe  */
856634db83bSCorentin Labbe static int mdio_mux_syscon_switch_fn(int current_child, int desired_child,
857634db83bSCorentin Labbe 				     void *data)
858634db83bSCorentin Labbe {
859634db83bSCorentin Labbe 	struct stmmac_priv *priv = data;
860634db83bSCorentin Labbe 	struct sunxi_priv_data *gmac = priv->plat->bsp_priv;
861634db83bSCorentin Labbe 	u32 reg, val;
862634db83bSCorentin Labbe 	int ret = 0;
863634db83bSCorentin Labbe 
864634db83bSCorentin Labbe 	if (current_child ^ desired_child) {
86525ae15fbSChen-Yu Tsai 		regmap_field_read(gmac->regmap_field, &reg);
866634db83bSCorentin Labbe 		switch (desired_child) {
867634db83bSCorentin Labbe 		case DWMAC_SUN8I_MDIO_MUX_INTERNAL_ID:
868634db83bSCorentin Labbe 			dev_info(priv->device, "Switch mux to internal PHY");
869634db83bSCorentin Labbe 			val = (reg & ~H3_EPHY_MUX_MASK) | H3_EPHY_SELECT;
870b8239638SSamuel Holland 			gmac->use_internal_phy = true;
871634db83bSCorentin Labbe 			break;
872634db83bSCorentin Labbe 		case DWMAC_SUN8I_MDIO_MUX_EXTERNAL_ID:
873634db83bSCorentin Labbe 			dev_info(priv->device, "Switch mux to external PHY");
874634db83bSCorentin Labbe 			val = (reg & ~H3_EPHY_MUX_MASK) | H3_EPHY_SHUTDOWN;
875b8239638SSamuel Holland 			gmac->use_internal_phy = false;
876634db83bSCorentin Labbe 			break;
877634db83bSCorentin Labbe 		default:
878634db83bSCorentin Labbe 			dev_err(priv->device, "Invalid child ID %x\n",
879634db83bSCorentin Labbe 				desired_child);
880634db83bSCorentin Labbe 			return -EINVAL;
881634db83bSCorentin Labbe 		}
88225ae15fbSChen-Yu Tsai 		regmap_field_write(gmac->regmap_field, val);
883b8239638SSamuel Holland 		if (gmac->use_internal_phy) {
884634db83bSCorentin Labbe 			ret = sun8i_dwmac_power_internal_phy(priv);
885634db83bSCorentin Labbe 			if (ret)
886634db83bSCorentin Labbe 				return ret;
887634db83bSCorentin Labbe 		} else {
888634db83bSCorentin Labbe 			sun8i_dwmac_unpower_internal_phy(gmac);
889634db83bSCorentin Labbe 		}
890634db83bSCorentin Labbe 		/* After changing syscon value, the MAC need reset or it will
891634db83bSCorentin Labbe 		 * use the last value (and so the last PHY set).
892634db83bSCorentin Labbe 		 */
893634db83bSCorentin Labbe 		ret = sun8i_dwmac_reset(priv);
894634db83bSCorentin Labbe 	}
895634db83bSCorentin Labbe 	return ret;
896634db83bSCorentin Labbe }
897634db83bSCorentin Labbe 
898634db83bSCorentin Labbe static int sun8i_dwmac_register_mdio_mux(struct stmmac_priv *priv)
899634db83bSCorentin Labbe {
900634db83bSCorentin Labbe 	int ret;
901634db83bSCorentin Labbe 	struct device_node *mdio_mux;
902634db83bSCorentin Labbe 	struct sunxi_priv_data *gmac = priv->plat->bsp_priv;
903634db83bSCorentin Labbe 
904634db83bSCorentin Labbe 	mdio_mux = of_get_child_by_name(priv->device->of_node, "mdio-mux");
905634db83bSCorentin Labbe 	if (!mdio_mux)
906634db83bSCorentin Labbe 		return -ENODEV;
907634db83bSCorentin Labbe 
908634db83bSCorentin Labbe 	ret = mdio_mux_init(priv->device, mdio_mux, mdio_mux_syscon_switch_fn,
909634db83bSCorentin Labbe 			    &gmac->mux_handle, priv, priv->mii);
910*1a15267bSYang Yingliang 	of_node_put(mdio_mux);
911634db83bSCorentin Labbe 	return ret;
912634db83bSCorentin Labbe }
913634db83bSCorentin Labbe 
9149b1e39cfSSamuel Holland static int sun8i_dwmac_set_syscon(struct device *dev,
9159b1e39cfSSamuel Holland 				  struct plat_stmmacenet_data *plat)
9169f93ac8dSLABBE Corentin {
9179b1e39cfSSamuel Holland 	struct sunxi_priv_data *gmac = plat->bsp_priv;
9189b1e39cfSSamuel Holland 	struct device_node *node = dev->of_node;
919d93b07f8SLABBE Corentin 	int ret;
9209f93ac8dSLABBE Corentin 	u32 reg, val;
9219f93ac8dSLABBE Corentin 
922e33b4325SYizhuo 	ret = regmap_field_read(gmac->regmap_field, &val);
923e33b4325SYizhuo 	if (ret) {
9249b1e39cfSSamuel Holland 		dev_err(dev, "Fail to read from regmap field.\n");
925e33b4325SYizhuo 		return ret;
926e33b4325SYizhuo 	}
927e33b4325SYizhuo 
9289f93ac8dSLABBE Corentin 	reg = gmac->variant->default_syscon_value;
9299f93ac8dSLABBE Corentin 	if (reg != val)
9309b1e39cfSSamuel Holland 		dev_warn(dev,
9319f93ac8dSLABBE Corentin 			 "Current syscon value is not the default %x (expect %x)\n",
9329f93ac8dSLABBE Corentin 			 val, reg);
9339f93ac8dSLABBE Corentin 
934634db83bSCorentin Labbe 	if (gmac->variant->soc_has_internal_phy) {
9351c08ac0cSCorentin Labbe 		if (of_property_read_bool(node, "allwinner,leds-active-low"))
9369f93ac8dSLABBE Corentin 			reg |= H3_EPHY_LED_POL;
9379f93ac8dSLABBE Corentin 		else
9389f93ac8dSLABBE Corentin 			reg &= ~H3_EPHY_LED_POL;
9399f93ac8dSLABBE Corentin 
9401450ba8aSIcenowy Zheng 		/* Force EPHY xtal frequency to 24MHz. */
9411450ba8aSIcenowy Zheng 		reg |= H3_EPHY_CLK_SEL;
9421450ba8aSIcenowy Zheng 
9439b1e39cfSSamuel Holland 		ret = of_mdio_parse_addr(dev, plat->phy_node);
9449f93ac8dSLABBE Corentin 		if (ret < 0) {
9459b1e39cfSSamuel Holland 			dev_err(dev, "Could not parse MDIO addr\n");
9469f93ac8dSLABBE Corentin 			return ret;
9479f93ac8dSLABBE Corentin 		}
9489f93ac8dSLABBE Corentin 		/* of_mdio_parse_addr returns a valid (0 ~ 31) PHY
9499f93ac8dSLABBE Corentin 		 * address. No need to mask it again.
9509f93ac8dSLABBE Corentin 		 */
951634db83bSCorentin Labbe 		reg |= 1 << H3_EPHY_ADDR_SHIFT;
9520fec7e72SIcenowy Zheng 	} else {
9530fec7e72SIcenowy Zheng 		/* For SoCs without internal PHY the PHY selection bit should be
9540fec7e72SIcenowy Zheng 		 * set to 0 (external PHY).
9550fec7e72SIcenowy Zheng 		 */
9560fec7e72SIcenowy Zheng 		reg &= ~H3_EPHY_SELECT;
9579f93ac8dSLABBE Corentin 	}
9589f93ac8dSLABBE Corentin 
9599f93ac8dSLABBE Corentin 	if (!of_property_read_u32(node, "allwinner,tx-delay-ps", &val)) {
9609f93ac8dSLABBE Corentin 		if (val % 100) {
9619b1e39cfSSamuel Holland 			dev_err(dev, "tx-delay must be a multiple of 100\n");
9629f93ac8dSLABBE Corentin 			return -EINVAL;
9639f93ac8dSLABBE Corentin 		}
9649f93ac8dSLABBE Corentin 		val /= 100;
9659b1e39cfSSamuel Holland 		dev_dbg(dev, "set tx-delay to %x\n", val);
9667b270b72SChen-Yu Tsai 		if (val <= gmac->variant->tx_delay_max) {
9677b270b72SChen-Yu Tsai 			reg &= ~(gmac->variant->tx_delay_max <<
9687b270b72SChen-Yu Tsai 				 SYSCON_ETXDC_SHIFT);
9699f93ac8dSLABBE Corentin 			reg |= (val << SYSCON_ETXDC_SHIFT);
9709f93ac8dSLABBE Corentin 		} else {
9719b1e39cfSSamuel Holland 			dev_err(dev, "Invalid TX clock delay: %d\n",
9729f93ac8dSLABBE Corentin 				val);
9739f93ac8dSLABBE Corentin 			return -EINVAL;
9749f93ac8dSLABBE Corentin 		}
9759f93ac8dSLABBE Corentin 	}
9769f93ac8dSLABBE Corentin 
9779f93ac8dSLABBE Corentin 	if (!of_property_read_u32(node, "allwinner,rx-delay-ps", &val)) {
9789f93ac8dSLABBE Corentin 		if (val % 100) {
9799b1e39cfSSamuel Holland 			dev_err(dev, "rx-delay must be a multiple of 100\n");
9809f93ac8dSLABBE Corentin 			return -EINVAL;
9819f93ac8dSLABBE Corentin 		}
9829f93ac8dSLABBE Corentin 		val /= 100;
9839b1e39cfSSamuel Holland 		dev_dbg(dev, "set rx-delay to %x\n", val);
9847b270b72SChen-Yu Tsai 		if (val <= gmac->variant->rx_delay_max) {
9857b270b72SChen-Yu Tsai 			reg &= ~(gmac->variant->rx_delay_max <<
9867b270b72SChen-Yu Tsai 				 SYSCON_ERXDC_SHIFT);
9879f93ac8dSLABBE Corentin 			reg |= (val << SYSCON_ERXDC_SHIFT);
9889f93ac8dSLABBE Corentin 		} else {
9899b1e39cfSSamuel Holland 			dev_err(dev, "Invalid RX clock delay: %d\n",
9909f93ac8dSLABBE Corentin 				val);
9919f93ac8dSLABBE Corentin 			return -EINVAL;
9929f93ac8dSLABBE Corentin 		}
9939f93ac8dSLABBE Corentin 	}
9949f93ac8dSLABBE Corentin 
9959f93ac8dSLABBE Corentin 	/* Clear interface mode bits */
9969f93ac8dSLABBE Corentin 	reg &= ~(SYSCON_ETCS_MASK | SYSCON_EPIT);
9979f93ac8dSLABBE Corentin 	if (gmac->variant->support_rmii)
9989f93ac8dSLABBE Corentin 		reg &= ~SYSCON_RMII_EN;
9999f93ac8dSLABBE Corentin 
10009b1e39cfSSamuel Holland 	switch (plat->interface) {
10019f93ac8dSLABBE Corentin 	case PHY_INTERFACE_MODE_MII:
10029f93ac8dSLABBE Corentin 		/* default */
10039f93ac8dSLABBE Corentin 		break;
10049f93ac8dSLABBE Corentin 	case PHY_INTERFACE_MODE_RGMII:
1005f1239d8aSChen-Yu Tsai 	case PHY_INTERFACE_MODE_RGMII_ID:
1006f1239d8aSChen-Yu Tsai 	case PHY_INTERFACE_MODE_RGMII_RXID:
1007f1239d8aSChen-Yu Tsai 	case PHY_INTERFACE_MODE_RGMII_TXID:
10089f93ac8dSLABBE Corentin 		reg |= SYSCON_EPIT | SYSCON_ETCS_INT_GMII;
10099f93ac8dSLABBE Corentin 		break;
10109f93ac8dSLABBE Corentin 	case PHY_INTERFACE_MODE_RMII:
10119f93ac8dSLABBE Corentin 		reg |= SYSCON_RMII_EN | SYSCON_ETCS_EXT_GMII;
10129f93ac8dSLABBE Corentin 		break;
10139f93ac8dSLABBE Corentin 	default:
10149b1e39cfSSamuel Holland 		dev_err(dev, "Unsupported interface mode: %s",
10159b1e39cfSSamuel Holland 			phy_modes(plat->interface));
10169f93ac8dSLABBE Corentin 		return -EINVAL;
10179f93ac8dSLABBE Corentin 	}
10189f93ac8dSLABBE Corentin 
101925ae15fbSChen-Yu Tsai 	regmap_field_write(gmac->regmap_field, reg);
10209f93ac8dSLABBE Corentin 
10219f93ac8dSLABBE Corentin 	return 0;
10229f93ac8dSLABBE Corentin }
10239f93ac8dSLABBE Corentin 
10249f93ac8dSLABBE Corentin static void sun8i_dwmac_unset_syscon(struct sunxi_priv_data *gmac)
10259f93ac8dSLABBE Corentin {
10269f93ac8dSLABBE Corentin 	u32 reg = gmac->variant->default_syscon_value;
10279f93ac8dSLABBE Corentin 
102825ae15fbSChen-Yu Tsai 	regmap_field_write(gmac->regmap_field, reg);
10299f93ac8dSLABBE Corentin }
10309f93ac8dSLABBE Corentin 
10319f93ac8dSLABBE Corentin static void sun8i_dwmac_exit(struct platform_device *pdev, void *priv)
10329f93ac8dSLABBE Corentin {
10339f93ac8dSLABBE Corentin 	struct sunxi_priv_data *gmac = priv;
10349f93ac8dSLABBE Corentin 
1035afac1d34SSamuel Holland 	if (gmac->variant->soc_has_internal_phy)
1036634db83bSCorentin Labbe 		sun8i_dwmac_unpower_internal_phy(gmac);
1037634db83bSCorentin Labbe 
10389f93ac8dSLABBE Corentin 	if (gmac->regulator)
10399f93ac8dSLABBE Corentin 		regulator_disable(gmac->regulator);
10409f93ac8dSLABBE Corentin }
10419f93ac8dSLABBE Corentin 
10428edb1271SCorentin Labbe static void sun8i_dwmac_set_mac_loopback(void __iomem *ioaddr, bool enable)
10438edb1271SCorentin Labbe {
10448edb1271SCorentin Labbe 	u32 value = readl(ioaddr + EMAC_BASIC_CTL0);
10458edb1271SCorentin Labbe 
10468edb1271SCorentin Labbe 	if (enable)
10478edb1271SCorentin Labbe 		value |= EMAC_LOOPBACK;
10488edb1271SCorentin Labbe 	else
10498edb1271SCorentin Labbe 		value &= ~EMAC_LOOPBACK;
10508edb1271SCorentin Labbe 
10518edb1271SCorentin Labbe 	writel(value, ioaddr + EMAC_BASIC_CTL0);
10528edb1271SCorentin Labbe }
10538edb1271SCorentin Labbe 
10549f93ac8dSLABBE Corentin static const struct stmmac_ops sun8i_dwmac_ops = {
10559f93ac8dSLABBE Corentin 	.core_init = sun8i_dwmac_core_init,
10569f93ac8dSLABBE Corentin 	.set_mac = sun8i_dwmac_set_mac,
10579f93ac8dSLABBE Corentin 	.dump_regs = sun8i_dwmac_dump_mac_regs,
10589f93ac8dSLABBE Corentin 	.rx_ipc = sun8i_dwmac_rx_ipc_enable,
10599f93ac8dSLABBE Corentin 	.set_filter = sun8i_dwmac_set_filter,
10609f93ac8dSLABBE Corentin 	.flow_ctrl = sun8i_dwmac_flow_ctrl,
10619f93ac8dSLABBE Corentin 	.set_umac_addr = sun8i_dwmac_set_umac_addr,
10629f93ac8dSLABBE Corentin 	.get_umac_addr = sun8i_dwmac_get_umac_addr,
10638edb1271SCorentin Labbe 	.set_mac_loopback = sun8i_dwmac_set_mac_loopback,
10649f93ac8dSLABBE Corentin };
10659f93ac8dSLABBE Corentin 
10669f93ac8dSLABBE Corentin static struct mac_device_info *sun8i_dwmac_setup(void *ppriv)
10679f93ac8dSLABBE Corentin {
10689f93ac8dSLABBE Corentin 	struct mac_device_info *mac;
10699f93ac8dSLABBE Corentin 	struct stmmac_priv *priv = ppriv;
10709f93ac8dSLABBE Corentin 
10719f93ac8dSLABBE Corentin 	mac = devm_kzalloc(priv->device, sizeof(*mac), GFP_KERNEL);
10729f93ac8dSLABBE Corentin 	if (!mac)
10739f93ac8dSLABBE Corentin 		return NULL;
10749f93ac8dSLABBE Corentin 
10759f93ac8dSLABBE Corentin 	mac->pcsr = priv->ioaddr;
10769f93ac8dSLABBE Corentin 	mac->mac = &sun8i_dwmac_ops;
10779f93ac8dSLABBE Corentin 	mac->dma = &sun8i_dwmac_dma_ops;
10789f93ac8dSLABBE Corentin 
1079d4c26eb6SCorentin Labbe 	priv->dev->priv_flags |= IFF_UNICAST_FLT;
1080d4c26eb6SCorentin Labbe 
10819f93ac8dSLABBE Corentin 	/* The loopback bit seems to be re-set when link change
10829f93ac8dSLABBE Corentin 	 * Simply mask it each time
10839f93ac8dSLABBE Corentin 	 * Speed 10/100/1000 are set in BIT(2)/BIT(3)
10849f93ac8dSLABBE Corentin 	 */
10859f93ac8dSLABBE Corentin 	mac->link.speed_mask = GENMASK(3, 2) | EMAC_LOOPBACK;
10869f93ac8dSLABBE Corentin 	mac->link.speed10 = EMAC_SPEED_10;
10879f93ac8dSLABBE Corentin 	mac->link.speed100 = EMAC_SPEED_100;
10889f93ac8dSLABBE Corentin 	mac->link.speed1000 = EMAC_SPEED_1000;
10899f93ac8dSLABBE Corentin 	mac->link.duplex = EMAC_DUPLEX_FULL;
10909f93ac8dSLABBE Corentin 	mac->mii.addr = EMAC_MDIO_CMD;
10919f93ac8dSLABBE Corentin 	mac->mii.data = EMAC_MDIO_DATA;
10929f93ac8dSLABBE Corentin 	mac->mii.reg_shift = 4;
10939f93ac8dSLABBE Corentin 	mac->mii.reg_mask = GENMASK(8, 4);
10949f93ac8dSLABBE Corentin 	mac->mii.addr_shift = 12;
10959f93ac8dSLABBE Corentin 	mac->mii.addr_mask = GENMASK(16, 12);
10969f93ac8dSLABBE Corentin 	mac->mii.clk_csr_shift = 20;
10979f93ac8dSLABBE Corentin 	mac->mii.clk_csr_mask = GENMASK(22, 20);
10989f93ac8dSLABBE Corentin 	mac->unicast_filter_entries = 8;
10999f93ac8dSLABBE Corentin 
11009f93ac8dSLABBE Corentin 	/* Synopsys Id is not available */
11019f93ac8dSLABBE Corentin 	priv->synopsys_id = 0;
11029f93ac8dSLABBE Corentin 
11039f93ac8dSLABBE Corentin 	return mac;
11049f93ac8dSLABBE Corentin }
11059f93ac8dSLABBE Corentin 
110649a06caeSChen-Yu Tsai static struct regmap *sun8i_dwmac_get_syscon_from_dev(struct device_node *node)
110749a06caeSChen-Yu Tsai {
110849a06caeSChen-Yu Tsai 	struct device_node *syscon_node;
110949a06caeSChen-Yu Tsai 	struct platform_device *syscon_pdev;
111049a06caeSChen-Yu Tsai 	struct regmap *regmap = NULL;
111149a06caeSChen-Yu Tsai 
111249a06caeSChen-Yu Tsai 	syscon_node = of_parse_phandle(node, "syscon", 0);
111349a06caeSChen-Yu Tsai 	if (!syscon_node)
111449a06caeSChen-Yu Tsai 		return ERR_PTR(-ENODEV);
111549a06caeSChen-Yu Tsai 
111649a06caeSChen-Yu Tsai 	syscon_pdev = of_find_device_by_node(syscon_node);
111749a06caeSChen-Yu Tsai 	if (!syscon_pdev) {
111849a06caeSChen-Yu Tsai 		/* platform device might not be probed yet */
111949a06caeSChen-Yu Tsai 		regmap = ERR_PTR(-EPROBE_DEFER);
112049a06caeSChen-Yu Tsai 		goto out_put_node;
112149a06caeSChen-Yu Tsai 	}
112249a06caeSChen-Yu Tsai 
112349a06caeSChen-Yu Tsai 	/* If no regmap is found then the other device driver is at fault */
112449a06caeSChen-Yu Tsai 	regmap = dev_get_regmap(&syscon_pdev->dev, NULL);
112549a06caeSChen-Yu Tsai 	if (!regmap)
112649a06caeSChen-Yu Tsai 		regmap = ERR_PTR(-EINVAL);
112749a06caeSChen-Yu Tsai 
112849a06caeSChen-Yu Tsai 	platform_device_put(syscon_pdev);
112949a06caeSChen-Yu Tsai out_put_node:
113049a06caeSChen-Yu Tsai 	of_node_put(syscon_node);
113149a06caeSChen-Yu Tsai 	return regmap;
113249a06caeSChen-Yu Tsai }
113349a06caeSChen-Yu Tsai 
11349f93ac8dSLABBE Corentin static int sun8i_dwmac_probe(struct platform_device *pdev)
11359f93ac8dSLABBE Corentin {
11369f93ac8dSLABBE Corentin 	struct plat_stmmacenet_data *plat_dat;
11379f93ac8dSLABBE Corentin 	struct stmmac_resources stmmac_res;
11389f93ac8dSLABBE Corentin 	struct sunxi_priv_data *gmac;
11399f93ac8dSLABBE Corentin 	struct device *dev = &pdev->dev;
11400c65b2b9SAndrew Lunn 	phy_interface_t interface;
11419f93ac8dSLABBE Corentin 	int ret;
1142634db83bSCorentin Labbe 	struct stmmac_priv *priv;
1143634db83bSCorentin Labbe 	struct net_device *ndev;
114425ae15fbSChen-Yu Tsai 	struct regmap *regmap;
11459f93ac8dSLABBE Corentin 
11469f93ac8dSLABBE Corentin 	ret = stmmac_get_platform_resources(pdev, &stmmac_res);
11479f93ac8dSLABBE Corentin 	if (ret)
11489f93ac8dSLABBE Corentin 		return ret;
11499f93ac8dSLABBE Corentin 
11509f93ac8dSLABBE Corentin 	gmac = devm_kzalloc(dev, sizeof(*gmac), GFP_KERNEL);
11519f93ac8dSLABBE Corentin 	if (!gmac)
11529f93ac8dSLABBE Corentin 		return -ENOMEM;
11539f93ac8dSLABBE Corentin 
11549f93ac8dSLABBE Corentin 	gmac->variant = of_device_get_match_data(&pdev->dev);
11559f93ac8dSLABBE Corentin 	if (!gmac->variant) {
11569f93ac8dSLABBE Corentin 		dev_err(&pdev->dev, "Missing dwmac-sun8i variant\n");
11579f93ac8dSLABBE Corentin 		return -EINVAL;
11589f93ac8dSLABBE Corentin 	}
11599f93ac8dSLABBE Corentin 
11609f93ac8dSLABBE Corentin 	/* Optional regulator for PHY */
11619f93ac8dSLABBE Corentin 	gmac->regulator = devm_regulator_get_optional(dev, "phy");
11629f93ac8dSLABBE Corentin 	if (IS_ERR(gmac->regulator)) {
11639f93ac8dSLABBE Corentin 		if (PTR_ERR(gmac->regulator) == -EPROBE_DEFER)
11649f93ac8dSLABBE Corentin 			return -EPROBE_DEFER;
11659f93ac8dSLABBE Corentin 		dev_info(dev, "No regulator found\n");
11669f93ac8dSLABBE Corentin 		gmac->regulator = NULL;
11679f93ac8dSLABBE Corentin 	}
11689f93ac8dSLABBE Corentin 
116949a06caeSChen-Yu Tsai 	/* The "GMAC clock control" register might be located in the
117049a06caeSChen-Yu Tsai 	 * CCU address range (on the R40), or the system control address
117149a06caeSChen-Yu Tsai 	 * range (on most other sun8i and later SoCs).
117249a06caeSChen-Yu Tsai 	 *
117349a06caeSChen-Yu Tsai 	 * The former controls most if not all clocks in the SoC. The
117449a06caeSChen-Yu Tsai 	 * latter has an SoC identification register, and on some SoCs,
117549a06caeSChen-Yu Tsai 	 * controls to map device specific SRAM to either the intended
117649a06caeSChen-Yu Tsai 	 * peripheral, or the CPU address space.
117749a06caeSChen-Yu Tsai 	 *
117849a06caeSChen-Yu Tsai 	 * In either case, there should be a coordinated and restricted
117949a06caeSChen-Yu Tsai 	 * method of accessing the register needed here. This is done by
118049a06caeSChen-Yu Tsai 	 * having the device export a custom regmap, instead of a generic
118149a06caeSChen-Yu Tsai 	 * syscon, which grants all access to all registers.
118249a06caeSChen-Yu Tsai 	 *
118349a06caeSChen-Yu Tsai 	 * To support old device trees, we fall back to using the syscon
118449a06caeSChen-Yu Tsai 	 * interface if possible.
118549a06caeSChen-Yu Tsai 	 */
118649a06caeSChen-Yu Tsai 	regmap = sun8i_dwmac_get_syscon_from_dev(pdev->dev.of_node);
118749a06caeSChen-Yu Tsai 	if (IS_ERR(regmap))
118849a06caeSChen-Yu Tsai 		regmap = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
118949a06caeSChen-Yu Tsai 							 "syscon");
119025ae15fbSChen-Yu Tsai 	if (IS_ERR(regmap)) {
119125ae15fbSChen-Yu Tsai 		ret = PTR_ERR(regmap);
11929f93ac8dSLABBE Corentin 		dev_err(&pdev->dev, "Unable to map syscon: %d\n", ret);
11939f93ac8dSLABBE Corentin 		return ret;
11949f93ac8dSLABBE Corentin 	}
11959f93ac8dSLABBE Corentin 
119625ae15fbSChen-Yu Tsai 	gmac->regmap_field = devm_regmap_field_alloc(dev, regmap,
119725ae15fbSChen-Yu Tsai 						     *gmac->variant->syscon_field);
119825ae15fbSChen-Yu Tsai 	if (IS_ERR(gmac->regmap_field)) {
119925ae15fbSChen-Yu Tsai 		ret = PTR_ERR(gmac->regmap_field);
120025ae15fbSChen-Yu Tsai 		dev_err(dev, "Unable to map syscon register: %d\n", ret);
120125ae15fbSChen-Yu Tsai 		return ret;
120225ae15fbSChen-Yu Tsai 	}
120325ae15fbSChen-Yu Tsai 
12040c65b2b9SAndrew Lunn 	ret = of_get_phy_mode(dev->of_node, &interface);
12050c65b2b9SAndrew Lunn 	if (ret)
12064ec850e5SKangjie Lu 		return -EINVAL;
12077eeecc4bSSamuel Holland 
120883216e39SMichael Walle 	plat_dat = stmmac_probe_config_dt(pdev, stmmac_res.mac);
12097eeecc4bSSamuel Holland 	if (IS_ERR(plat_dat))
12107eeecc4bSSamuel Holland 		return PTR_ERR(plat_dat);
12119f93ac8dSLABBE Corentin 
12129f93ac8dSLABBE Corentin 	/* platform data specifying hardware features and callbacks.
12139f93ac8dSLABBE Corentin 	 * hardware features were copied from Allwinner drivers.
12149f93ac8dSLABBE Corentin 	 */
12157eeecc4bSSamuel Holland 	plat_dat->interface = interface;
12169f93ac8dSLABBE Corentin 	plat_dat->rx_coe = STMMAC_RX_COE_TYPE2;
12179f93ac8dSLABBE Corentin 	plat_dat->tx_coe = 1;
12189f93ac8dSLABBE Corentin 	plat_dat->has_sun8i = true;
12199f93ac8dSLABBE Corentin 	plat_dat->bsp_priv = gmac;
12209f93ac8dSLABBE Corentin 	plat_dat->init = sun8i_dwmac_init;
12219f93ac8dSLABBE Corentin 	plat_dat->exit = sun8i_dwmac_exit;
12229f93ac8dSLABBE Corentin 	plat_dat->setup = sun8i_dwmac_setup;
1223014dfa26SCorentin Labbe 	plat_dat->tx_fifo_size = 4096;
1224014dfa26SCorentin Labbe 	plat_dat->rx_fifo_size = 16384;
12259f93ac8dSLABBE Corentin 
12269b1e39cfSSamuel Holland 	ret = sun8i_dwmac_set_syscon(&pdev->dev, plat_dat);
12279f93ac8dSLABBE Corentin 	if (ret)
12287eeecc4bSSamuel Holland 		goto dwmac_deconfig;
12299f93ac8dSLABBE Corentin 
12309b1e39cfSSamuel Holland 	ret = sun8i_dwmac_init(pdev, plat_dat->bsp_priv);
12319b1e39cfSSamuel Holland 	if (ret)
12329b1e39cfSSamuel Holland 		goto dwmac_syscon;
12339b1e39cfSSamuel Holland 
12349f93ac8dSLABBE Corentin 	ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
12359f93ac8dSLABBE Corentin 	if (ret)
1236634db83bSCorentin Labbe 		goto dwmac_exit;
12379f93ac8dSLABBE Corentin 
1238634db83bSCorentin Labbe 	ndev = dev_get_drvdata(&pdev->dev);
1239634db83bSCorentin Labbe 	priv = netdev_priv(ndev);
12402743aa24SSamuel Holland 
1241b76bbb34SJisheng Zhang 	/* the MAC is runtime suspended after stmmac_dvr_probe(), so we
1242b76bbb34SJisheng Zhang 	 * need to ensure the MAC resume back before other operations such
1243b76bbb34SJisheng Zhang 	 * as reset.
1244b76bbb34SJisheng Zhang 	 */
1245b76bbb34SJisheng Zhang 	pm_runtime_get_sync(&pdev->dev);
1246b76bbb34SJisheng Zhang 
1247634db83bSCorentin Labbe 	/* The mux must be registered after parent MDIO
1248634db83bSCorentin Labbe 	 * so after stmmac_dvr_probe()
1249634db83bSCorentin Labbe 	 */
1250634db83bSCorentin Labbe 	if (gmac->variant->soc_has_internal_phy) {
1251634db83bSCorentin Labbe 		ret = get_ephy_nodes(priv);
1252634db83bSCorentin Labbe 		if (ret)
12537eeecc4bSSamuel Holland 			goto dwmac_remove;
1254634db83bSCorentin Labbe 		ret = sun8i_dwmac_register_mdio_mux(priv);
1255634db83bSCorentin Labbe 		if (ret) {
1256634db83bSCorentin Labbe 			dev_err(&pdev->dev, "Failed to register mux\n");
1257634db83bSCorentin Labbe 			goto dwmac_mux;
1258634db83bSCorentin Labbe 		}
1259634db83bSCorentin Labbe 	} else {
1260634db83bSCorentin Labbe 		ret = sun8i_dwmac_reset(priv);
1261634db83bSCorentin Labbe 		if (ret)
12627eeecc4bSSamuel Holland 			goto dwmac_remove;
1263634db83bSCorentin Labbe 	}
1264634db83bSCorentin Labbe 
1265b76bbb34SJisheng Zhang 	pm_runtime_put(&pdev->dev);
1266b76bbb34SJisheng Zhang 
12672743aa24SSamuel Holland 	return 0;
12682743aa24SSamuel Holland 
1269634db83bSCorentin Labbe dwmac_mux:
127052925421SSamuel Holland 	reset_control_put(gmac->rst_ephy);
127152925421SSamuel Holland 	clk_put(gmac->ephy_clk);
12727eeecc4bSSamuel Holland dwmac_remove:
1273b76bbb34SJisheng Zhang 	pm_runtime_put_noidle(&pdev->dev);
12747eeecc4bSSamuel Holland 	stmmac_dvr_remove(&pdev->dev);
1275634db83bSCorentin Labbe dwmac_exit:
12767eeecc4bSSamuel Holland 	sun8i_dwmac_exit(pdev, gmac);
12779b1e39cfSSamuel Holland dwmac_syscon:
12789b1e39cfSSamuel Holland 	sun8i_dwmac_unset_syscon(gmac);
12797eeecc4bSSamuel Holland dwmac_deconfig:
12807eeecc4bSSamuel Holland 	stmmac_remove_config_dt(pdev, plat_dat);
12817eeecc4bSSamuel Holland 
12829f93ac8dSLABBE Corentin 	return ret;
12839f93ac8dSLABBE Corentin }
12849f93ac8dSLABBE Corentin 
128552925421SSamuel Holland static int sun8i_dwmac_remove(struct platform_device *pdev)
128652925421SSamuel Holland {
128752925421SSamuel Holland 	struct net_device *ndev = platform_get_drvdata(pdev);
128852925421SSamuel Holland 	struct stmmac_priv *priv = netdev_priv(ndev);
128952925421SSamuel Holland 	struct sunxi_priv_data *gmac = priv->plat->bsp_priv;
129052925421SSamuel Holland 
129152925421SSamuel Holland 	if (gmac->variant->soc_has_internal_phy) {
129252925421SSamuel Holland 		mdio_mux_uninit(gmac->mux_handle);
129352925421SSamuel Holland 		sun8i_dwmac_unpower_internal_phy(gmac);
129452925421SSamuel Holland 		reset_control_put(gmac->rst_ephy);
129552925421SSamuel Holland 		clk_put(gmac->ephy_clk);
129652925421SSamuel Holland 	}
129752925421SSamuel Holland 
129852925421SSamuel Holland 	stmmac_pltfr_remove(pdev);
12999b1e39cfSSamuel Holland 	sun8i_dwmac_unset_syscon(gmac);
130052925421SSamuel Holland 
130152925421SSamuel Holland 	return 0;
130252925421SSamuel Holland }
130352925421SSamuel Holland 
130496be41d7SSamuel Holland static void sun8i_dwmac_shutdown(struct platform_device *pdev)
130596be41d7SSamuel Holland {
130696be41d7SSamuel Holland 	struct net_device *ndev = platform_get_drvdata(pdev);
130796be41d7SSamuel Holland 	struct stmmac_priv *priv = netdev_priv(ndev);
130896be41d7SSamuel Holland 	struct sunxi_priv_data *gmac = priv->plat->bsp_priv;
130996be41d7SSamuel Holland 
131096be41d7SSamuel Holland 	sun8i_dwmac_exit(pdev, gmac);
131196be41d7SSamuel Holland }
131296be41d7SSamuel Holland 
13139f93ac8dSLABBE Corentin static const struct of_device_id sun8i_dwmac_match[] = {
1314a8ff8ccbSCorentin Labbe 	{ .compatible = "allwinner,sun8i-h3-emac",
1315a8ff8ccbSCorentin Labbe 		.data = &emac_variant_h3 },
1316a8ff8ccbSCorentin Labbe 	{ .compatible = "allwinner,sun8i-v3s-emac",
1317a8ff8ccbSCorentin Labbe 		.data = &emac_variant_v3s },
1318a8ff8ccbSCorentin Labbe 	{ .compatible = "allwinner,sun8i-a83t-emac",
1319a8ff8ccbSCorentin Labbe 		.data = &emac_variant_a83t },
13209bf5085aSChen-Yu Tsai 	{ .compatible = "allwinner,sun8i-r40-gmac",
13219bf5085aSChen-Yu Tsai 		.data = &emac_variant_r40 },
1322a8ff8ccbSCorentin Labbe 	{ .compatible = "allwinner,sun50i-a64-emac",
1323a8ff8ccbSCorentin Labbe 		.data = &emac_variant_a64 },
1324adadd38cSIcenowy Zheng 	{ .compatible = "allwinner,sun50i-h6-emac",
1325adadd38cSIcenowy Zheng 		.data = &emac_variant_h6 },
13269f93ac8dSLABBE Corentin 	{ }
13279f93ac8dSLABBE Corentin };
13289f93ac8dSLABBE Corentin MODULE_DEVICE_TABLE(of, sun8i_dwmac_match);
13299f93ac8dSLABBE Corentin 
13309f93ac8dSLABBE Corentin static struct platform_driver sun8i_dwmac_driver = {
13319f93ac8dSLABBE Corentin 	.probe  = sun8i_dwmac_probe,
133252925421SSamuel Holland 	.remove = sun8i_dwmac_remove,
133396be41d7SSamuel Holland 	.shutdown = sun8i_dwmac_shutdown,
13349f93ac8dSLABBE Corentin 	.driver = {
13359f93ac8dSLABBE Corentin 		.name           = "dwmac-sun8i",
13369f93ac8dSLABBE Corentin 		.pm		= &stmmac_pltfr_pm_ops,
13379f93ac8dSLABBE Corentin 		.of_match_table = sun8i_dwmac_match,
13389f93ac8dSLABBE Corentin 	},
13399f93ac8dSLABBE Corentin };
13409f93ac8dSLABBE Corentin module_platform_driver(sun8i_dwmac_driver);
13419f93ac8dSLABBE Corentin 
13429f93ac8dSLABBE Corentin MODULE_AUTHOR("Corentin Labbe <clabbe.montjoie@gmail.com>");
13439f93ac8dSLABBE Corentin MODULE_DESCRIPTION("Allwinner sun8i DWMAC specific glue layer");
13449f93ac8dSLABBE Corentin MODULE_LICENSE("GPL");
1345