18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/* Copyright (c) 2014 Linaro Ltd.
38c2ecf20Sopenharmony_ci * Copyright (c) 2014 Hisilicon Limited.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/module.h>
78c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
88c2ecf20Sopenharmony_ci#include <linux/etherdevice.h>
98c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
108c2ecf20Sopenharmony_ci#include <linux/of_device.h>
118c2ecf20Sopenharmony_ci#include <linux/of_net.h>
128c2ecf20Sopenharmony_ci#include <linux/of_mdio.h>
138c2ecf20Sopenharmony_ci#include <linux/reset.h>
148c2ecf20Sopenharmony_ci#include <linux/clk.h>
158c2ecf20Sopenharmony_ci#include <linux/circ_buf.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define STATION_ADDR_LOW		0x0000
188c2ecf20Sopenharmony_ci#define STATION_ADDR_HIGH		0x0004
198c2ecf20Sopenharmony_ci#define MAC_DUPLEX_HALF_CTRL		0x0008
208c2ecf20Sopenharmony_ci#define MAX_FRM_SIZE			0x003c
218c2ecf20Sopenharmony_ci#define PORT_MODE			0x0040
228c2ecf20Sopenharmony_ci#define PORT_EN				0x0044
238c2ecf20Sopenharmony_ci#define BITS_TX_EN			BIT(2)
248c2ecf20Sopenharmony_ci#define BITS_RX_EN			BIT(1)
258c2ecf20Sopenharmony_ci#define REC_FILT_CONTROL		0x0064
268c2ecf20Sopenharmony_ci#define BIT_CRC_ERR_PASS		BIT(5)
278c2ecf20Sopenharmony_ci#define BIT_PAUSE_FRM_PASS		BIT(4)
288c2ecf20Sopenharmony_ci#define BIT_VLAN_DROP_EN		BIT(3)
298c2ecf20Sopenharmony_ci#define BIT_BC_DROP_EN			BIT(2)
308c2ecf20Sopenharmony_ci#define BIT_MC_MATCH_EN			BIT(1)
318c2ecf20Sopenharmony_ci#define BIT_UC_MATCH_EN			BIT(0)
328c2ecf20Sopenharmony_ci#define PORT_MC_ADDR_LOW		0x0068
338c2ecf20Sopenharmony_ci#define PORT_MC_ADDR_HIGH		0x006C
348c2ecf20Sopenharmony_ci#define CF_CRC_STRIP			0x01b0
358c2ecf20Sopenharmony_ci#define MODE_CHANGE_EN			0x01b4
368c2ecf20Sopenharmony_ci#define BIT_MODE_CHANGE_EN		BIT(0)
378c2ecf20Sopenharmony_ci#define COL_SLOT_TIME			0x01c0
388c2ecf20Sopenharmony_ci#define RECV_CONTROL			0x01e0
398c2ecf20Sopenharmony_ci#define BIT_STRIP_PAD_EN		BIT(3)
408c2ecf20Sopenharmony_ci#define BIT_RUNT_PKT_EN			BIT(4)
418c2ecf20Sopenharmony_ci#define CONTROL_WORD			0x0214
428c2ecf20Sopenharmony_ci#define MDIO_SINGLE_CMD			0x03c0
438c2ecf20Sopenharmony_ci#define MDIO_SINGLE_DATA		0x03c4
448c2ecf20Sopenharmony_ci#define MDIO_CTRL			0x03cc
458c2ecf20Sopenharmony_ci#define MDIO_RDATA_STATUS		0x03d0
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci#define MDIO_START			BIT(20)
488c2ecf20Sopenharmony_ci#define MDIO_R_VALID			BIT(0)
498c2ecf20Sopenharmony_ci#define MDIO_READ			(BIT(17) | MDIO_START)
508c2ecf20Sopenharmony_ci#define MDIO_WRITE			(BIT(16) | MDIO_START)
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci#define RX_FQ_START_ADDR		0x0500
538c2ecf20Sopenharmony_ci#define RX_FQ_DEPTH			0x0504
548c2ecf20Sopenharmony_ci#define RX_FQ_WR_ADDR			0x0508
558c2ecf20Sopenharmony_ci#define RX_FQ_RD_ADDR			0x050c
568c2ecf20Sopenharmony_ci#define RX_FQ_VLDDESC_CNT		0x0510
578c2ecf20Sopenharmony_ci#define RX_FQ_ALEMPTY_TH		0x0514
588c2ecf20Sopenharmony_ci#define RX_FQ_REG_EN			0x0518
598c2ecf20Sopenharmony_ci#define BITS_RX_FQ_START_ADDR_EN	BIT(2)
608c2ecf20Sopenharmony_ci#define BITS_RX_FQ_DEPTH_EN		BIT(1)
618c2ecf20Sopenharmony_ci#define BITS_RX_FQ_RD_ADDR_EN		BIT(0)
628c2ecf20Sopenharmony_ci#define RX_FQ_ALFULL_TH			0x051c
638c2ecf20Sopenharmony_ci#define RX_BQ_START_ADDR		0x0520
648c2ecf20Sopenharmony_ci#define RX_BQ_DEPTH			0x0524
658c2ecf20Sopenharmony_ci#define RX_BQ_WR_ADDR			0x0528
668c2ecf20Sopenharmony_ci#define RX_BQ_RD_ADDR			0x052c
678c2ecf20Sopenharmony_ci#define RX_BQ_FREE_DESC_CNT		0x0530
688c2ecf20Sopenharmony_ci#define RX_BQ_ALEMPTY_TH		0x0534
698c2ecf20Sopenharmony_ci#define RX_BQ_REG_EN			0x0538
708c2ecf20Sopenharmony_ci#define BITS_RX_BQ_START_ADDR_EN	BIT(2)
718c2ecf20Sopenharmony_ci#define BITS_RX_BQ_DEPTH_EN		BIT(1)
728c2ecf20Sopenharmony_ci#define BITS_RX_BQ_WR_ADDR_EN		BIT(0)
738c2ecf20Sopenharmony_ci#define RX_BQ_ALFULL_TH			0x053c
748c2ecf20Sopenharmony_ci#define TX_BQ_START_ADDR		0x0580
758c2ecf20Sopenharmony_ci#define TX_BQ_DEPTH			0x0584
768c2ecf20Sopenharmony_ci#define TX_BQ_WR_ADDR			0x0588
778c2ecf20Sopenharmony_ci#define TX_BQ_RD_ADDR			0x058c
788c2ecf20Sopenharmony_ci#define TX_BQ_VLDDESC_CNT		0x0590
798c2ecf20Sopenharmony_ci#define TX_BQ_ALEMPTY_TH		0x0594
808c2ecf20Sopenharmony_ci#define TX_BQ_REG_EN			0x0598
818c2ecf20Sopenharmony_ci#define BITS_TX_BQ_START_ADDR_EN	BIT(2)
828c2ecf20Sopenharmony_ci#define BITS_TX_BQ_DEPTH_EN		BIT(1)
838c2ecf20Sopenharmony_ci#define BITS_TX_BQ_RD_ADDR_EN		BIT(0)
848c2ecf20Sopenharmony_ci#define TX_BQ_ALFULL_TH			0x059c
858c2ecf20Sopenharmony_ci#define TX_RQ_START_ADDR		0x05a0
868c2ecf20Sopenharmony_ci#define TX_RQ_DEPTH			0x05a4
878c2ecf20Sopenharmony_ci#define TX_RQ_WR_ADDR			0x05a8
888c2ecf20Sopenharmony_ci#define TX_RQ_RD_ADDR			0x05ac
898c2ecf20Sopenharmony_ci#define TX_RQ_FREE_DESC_CNT		0x05b0
908c2ecf20Sopenharmony_ci#define TX_RQ_ALEMPTY_TH		0x05b4
918c2ecf20Sopenharmony_ci#define TX_RQ_REG_EN			0x05b8
928c2ecf20Sopenharmony_ci#define BITS_TX_RQ_START_ADDR_EN	BIT(2)
938c2ecf20Sopenharmony_ci#define BITS_TX_RQ_DEPTH_EN		BIT(1)
948c2ecf20Sopenharmony_ci#define BITS_TX_RQ_WR_ADDR_EN		BIT(0)
958c2ecf20Sopenharmony_ci#define TX_RQ_ALFULL_TH			0x05bc
968c2ecf20Sopenharmony_ci#define RAW_PMU_INT			0x05c0
978c2ecf20Sopenharmony_ci#define ENA_PMU_INT			0x05c4
988c2ecf20Sopenharmony_ci#define STATUS_PMU_INT			0x05c8
998c2ecf20Sopenharmony_ci#define MAC_FIFO_ERR_IN			BIT(30)
1008c2ecf20Sopenharmony_ci#define TX_RQ_IN_TIMEOUT_INT		BIT(29)
1018c2ecf20Sopenharmony_ci#define RX_BQ_IN_TIMEOUT_INT		BIT(28)
1028c2ecf20Sopenharmony_ci#define TXOUTCFF_FULL_INT		BIT(27)
1038c2ecf20Sopenharmony_ci#define TXOUTCFF_EMPTY_INT		BIT(26)
1048c2ecf20Sopenharmony_ci#define TXCFF_FULL_INT			BIT(25)
1058c2ecf20Sopenharmony_ci#define TXCFF_EMPTY_INT			BIT(24)
1068c2ecf20Sopenharmony_ci#define RXOUTCFF_FULL_INT		BIT(23)
1078c2ecf20Sopenharmony_ci#define RXOUTCFF_EMPTY_INT		BIT(22)
1088c2ecf20Sopenharmony_ci#define RXCFF_FULL_INT			BIT(21)
1098c2ecf20Sopenharmony_ci#define RXCFF_EMPTY_INT			BIT(20)
1108c2ecf20Sopenharmony_ci#define TX_RQ_IN_INT			BIT(19)
1118c2ecf20Sopenharmony_ci#define TX_BQ_OUT_INT			BIT(18)
1128c2ecf20Sopenharmony_ci#define RX_BQ_IN_INT			BIT(17)
1138c2ecf20Sopenharmony_ci#define RX_FQ_OUT_INT			BIT(16)
1148c2ecf20Sopenharmony_ci#define TX_RQ_EMPTY_INT			BIT(15)
1158c2ecf20Sopenharmony_ci#define TX_RQ_FULL_INT			BIT(14)
1168c2ecf20Sopenharmony_ci#define TX_RQ_ALEMPTY_INT		BIT(13)
1178c2ecf20Sopenharmony_ci#define TX_RQ_ALFULL_INT		BIT(12)
1188c2ecf20Sopenharmony_ci#define TX_BQ_EMPTY_INT			BIT(11)
1198c2ecf20Sopenharmony_ci#define TX_BQ_FULL_INT			BIT(10)
1208c2ecf20Sopenharmony_ci#define TX_BQ_ALEMPTY_INT		BIT(9)
1218c2ecf20Sopenharmony_ci#define TX_BQ_ALFULL_INT		BIT(8)
1228c2ecf20Sopenharmony_ci#define RX_BQ_EMPTY_INT			BIT(7)
1238c2ecf20Sopenharmony_ci#define RX_BQ_FULL_INT			BIT(6)
1248c2ecf20Sopenharmony_ci#define RX_BQ_ALEMPTY_INT		BIT(5)
1258c2ecf20Sopenharmony_ci#define RX_BQ_ALFULL_INT		BIT(4)
1268c2ecf20Sopenharmony_ci#define RX_FQ_EMPTY_INT			BIT(3)
1278c2ecf20Sopenharmony_ci#define RX_FQ_FULL_INT			BIT(2)
1288c2ecf20Sopenharmony_ci#define RX_FQ_ALEMPTY_INT		BIT(1)
1298c2ecf20Sopenharmony_ci#define RX_FQ_ALFULL_INT		BIT(0)
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci#define DEF_INT_MASK			(RX_BQ_IN_INT | RX_BQ_IN_TIMEOUT_INT | \
1328c2ecf20Sopenharmony_ci					TX_RQ_IN_INT | TX_RQ_IN_TIMEOUT_INT)
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci#define DESC_WR_RD_ENA			0x05cc
1358c2ecf20Sopenharmony_ci#define IN_QUEUE_TH			0x05d8
1368c2ecf20Sopenharmony_ci#define OUT_QUEUE_TH			0x05dc
1378c2ecf20Sopenharmony_ci#define QUEUE_TX_BQ_SHIFT		16
1388c2ecf20Sopenharmony_ci#define RX_BQ_IN_TIMEOUT_TH		0x05e0
1398c2ecf20Sopenharmony_ci#define TX_RQ_IN_TIMEOUT_TH		0x05e4
1408c2ecf20Sopenharmony_ci#define STOP_CMD			0x05e8
1418c2ecf20Sopenharmony_ci#define BITS_TX_STOP			BIT(1)
1428c2ecf20Sopenharmony_ci#define BITS_RX_STOP			BIT(0)
1438c2ecf20Sopenharmony_ci#define FLUSH_CMD			0x05eC
1448c2ecf20Sopenharmony_ci#define BITS_TX_FLUSH_CMD		BIT(5)
1458c2ecf20Sopenharmony_ci#define BITS_RX_FLUSH_CMD		BIT(4)
1468c2ecf20Sopenharmony_ci#define BITS_TX_FLUSH_FLAG_DOWN		BIT(3)
1478c2ecf20Sopenharmony_ci#define BITS_TX_FLUSH_FLAG_UP		BIT(2)
1488c2ecf20Sopenharmony_ci#define BITS_RX_FLUSH_FLAG_DOWN		BIT(1)
1498c2ecf20Sopenharmony_ci#define BITS_RX_FLUSH_FLAG_UP		BIT(0)
1508c2ecf20Sopenharmony_ci#define RX_CFF_NUM_REG			0x05f0
1518c2ecf20Sopenharmony_ci#define PMU_FSM_REG			0x05f8
1528c2ecf20Sopenharmony_ci#define RX_FIFO_PKT_IN_NUM		0x05fc
1538c2ecf20Sopenharmony_ci#define RX_FIFO_PKT_OUT_NUM		0x0600
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci#define RGMII_SPEED_1000		0x2c
1568c2ecf20Sopenharmony_ci#define RGMII_SPEED_100			0x2f
1578c2ecf20Sopenharmony_ci#define RGMII_SPEED_10			0x2d
1588c2ecf20Sopenharmony_ci#define MII_SPEED_100			0x0f
1598c2ecf20Sopenharmony_ci#define MII_SPEED_10			0x0d
1608c2ecf20Sopenharmony_ci#define GMAC_SPEED_1000			0x05
1618c2ecf20Sopenharmony_ci#define GMAC_SPEED_100			0x01
1628c2ecf20Sopenharmony_ci#define GMAC_SPEED_10			0x00
1638c2ecf20Sopenharmony_ci#define GMAC_FULL_DUPLEX		BIT(4)
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci#define RX_BQ_INT_THRESHOLD		0x01
1668c2ecf20Sopenharmony_ci#define TX_RQ_INT_THRESHOLD		0x01
1678c2ecf20Sopenharmony_ci#define RX_BQ_IN_TIMEOUT		0x10000
1688c2ecf20Sopenharmony_ci#define TX_RQ_IN_TIMEOUT		0x50000
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci#define MAC_MAX_FRAME_SIZE		1600
1718c2ecf20Sopenharmony_ci#define DESC_SIZE			32
1728c2ecf20Sopenharmony_ci#define RX_DESC_NUM			1024
1738c2ecf20Sopenharmony_ci#define TX_DESC_NUM			1024
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci#define DESC_VLD_FREE			0
1768c2ecf20Sopenharmony_ci#define DESC_VLD_BUSY			0x80000000
1778c2ecf20Sopenharmony_ci#define DESC_FL_MID			0
1788c2ecf20Sopenharmony_ci#define DESC_FL_LAST			0x20000000
1798c2ecf20Sopenharmony_ci#define DESC_FL_FIRST			0x40000000
1808c2ecf20Sopenharmony_ci#define DESC_FL_FULL			0x60000000
1818c2ecf20Sopenharmony_ci#define DESC_DATA_LEN_OFF		16
1828c2ecf20Sopenharmony_ci#define DESC_BUFF_LEN_OFF		0
1838c2ecf20Sopenharmony_ci#define DESC_DATA_MASK			0x7ff
1848c2ecf20Sopenharmony_ci#define DESC_SG				BIT(30)
1858c2ecf20Sopenharmony_ci#define DESC_FRAGS_NUM_OFF		11
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci/* DMA descriptor ring helpers */
1888c2ecf20Sopenharmony_ci#define dma_ring_incr(n, s)		(((n) + 1) & ((s) - 1))
1898c2ecf20Sopenharmony_ci#define dma_cnt(n)			((n) >> 5)
1908c2ecf20Sopenharmony_ci#define dma_byte(n)			((n) << 5)
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci#define HW_CAP_TSO			BIT(0)
1938c2ecf20Sopenharmony_ci#define GEMAC_V1			0
1948c2ecf20Sopenharmony_ci#define GEMAC_V2			(GEMAC_V1 | HW_CAP_TSO)
1958c2ecf20Sopenharmony_ci#define HAS_CAP_TSO(hw_cap)		((hw_cap) & HW_CAP_TSO)
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci#define PHY_RESET_DELAYS_PROPERTY	"hisilicon,phy-reset-delays-us"
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_cienum phy_reset_delays {
2008c2ecf20Sopenharmony_ci	PRE_DELAY,
2018c2ecf20Sopenharmony_ci	PULSE,
2028c2ecf20Sopenharmony_ci	POST_DELAY,
2038c2ecf20Sopenharmony_ci	DELAYS_NUM,
2048c2ecf20Sopenharmony_ci};
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_cistruct hix5hd2_desc {
2078c2ecf20Sopenharmony_ci	__le32 buff_addr;
2088c2ecf20Sopenharmony_ci	__le32 cmd;
2098c2ecf20Sopenharmony_ci} __aligned(32);
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_cistruct hix5hd2_desc_sw {
2128c2ecf20Sopenharmony_ci	struct hix5hd2_desc *desc;
2138c2ecf20Sopenharmony_ci	dma_addr_t	phys_addr;
2148c2ecf20Sopenharmony_ci	unsigned int	count;
2158c2ecf20Sopenharmony_ci	unsigned int	size;
2168c2ecf20Sopenharmony_ci};
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_cistruct hix5hd2_sg_desc_ring {
2198c2ecf20Sopenharmony_ci	struct sg_desc *desc;
2208c2ecf20Sopenharmony_ci	dma_addr_t phys_addr;
2218c2ecf20Sopenharmony_ci};
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_cistruct frags_info {
2248c2ecf20Sopenharmony_ci	__le32 addr;
2258c2ecf20Sopenharmony_ci	__le32 size;
2268c2ecf20Sopenharmony_ci};
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci/* hardware supported max skb frags num */
2298c2ecf20Sopenharmony_ci#define SG_MAX_SKB_FRAGS	17
2308c2ecf20Sopenharmony_cistruct sg_desc {
2318c2ecf20Sopenharmony_ci	__le32 total_len;
2328c2ecf20Sopenharmony_ci	__le32 resvd0;
2338c2ecf20Sopenharmony_ci	__le32 linear_addr;
2348c2ecf20Sopenharmony_ci	__le32 linear_len;
2358c2ecf20Sopenharmony_ci	/* reserve one more frags for memory alignment */
2368c2ecf20Sopenharmony_ci	struct frags_info frags[SG_MAX_SKB_FRAGS + 1];
2378c2ecf20Sopenharmony_ci};
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci#define QUEUE_NUMS	4
2408c2ecf20Sopenharmony_cistruct hix5hd2_priv {
2418c2ecf20Sopenharmony_ci	struct hix5hd2_desc_sw pool[QUEUE_NUMS];
2428c2ecf20Sopenharmony_ci#define rx_fq		pool[0]
2438c2ecf20Sopenharmony_ci#define rx_bq		pool[1]
2448c2ecf20Sopenharmony_ci#define tx_bq		pool[2]
2458c2ecf20Sopenharmony_ci#define tx_rq		pool[3]
2468c2ecf20Sopenharmony_ci	struct hix5hd2_sg_desc_ring tx_ring;
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	void __iomem *base;
2498c2ecf20Sopenharmony_ci	void __iomem *ctrl_base;
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	struct sk_buff *tx_skb[TX_DESC_NUM];
2528c2ecf20Sopenharmony_ci	struct sk_buff *rx_skb[RX_DESC_NUM];
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	struct device *dev;
2558c2ecf20Sopenharmony_ci	struct net_device *netdev;
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	struct device_node *phy_node;
2588c2ecf20Sopenharmony_ci	phy_interface_t	phy_mode;
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	unsigned long hw_cap;
2618c2ecf20Sopenharmony_ci	unsigned int speed;
2628c2ecf20Sopenharmony_ci	unsigned int duplex;
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	struct clk *mac_core_clk;
2658c2ecf20Sopenharmony_ci	struct clk *mac_ifc_clk;
2668c2ecf20Sopenharmony_ci	struct reset_control *mac_core_rst;
2678c2ecf20Sopenharmony_ci	struct reset_control *mac_ifc_rst;
2688c2ecf20Sopenharmony_ci	struct reset_control *phy_rst;
2698c2ecf20Sopenharmony_ci	u32 phy_reset_delays[DELAYS_NUM];
2708c2ecf20Sopenharmony_ci	struct mii_bus *bus;
2718c2ecf20Sopenharmony_ci	struct napi_struct napi;
2728c2ecf20Sopenharmony_ci	struct work_struct tx_timeout_task;
2738c2ecf20Sopenharmony_ci};
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_cistatic inline void hix5hd2_mac_interface_reset(struct hix5hd2_priv *priv)
2768c2ecf20Sopenharmony_ci{
2778c2ecf20Sopenharmony_ci	if (!priv->mac_ifc_rst)
2788c2ecf20Sopenharmony_ci		return;
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	reset_control_assert(priv->mac_ifc_rst);
2818c2ecf20Sopenharmony_ci	reset_control_deassert(priv->mac_ifc_rst);
2828c2ecf20Sopenharmony_ci}
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_cistatic void hix5hd2_config_port(struct net_device *dev, u32 speed, u32 duplex)
2858c2ecf20Sopenharmony_ci{
2868c2ecf20Sopenharmony_ci	struct hix5hd2_priv *priv = netdev_priv(dev);
2878c2ecf20Sopenharmony_ci	u32 val;
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	priv->speed = speed;
2908c2ecf20Sopenharmony_ci	priv->duplex = duplex;
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	switch (priv->phy_mode) {
2938c2ecf20Sopenharmony_ci	case PHY_INTERFACE_MODE_RGMII:
2948c2ecf20Sopenharmony_ci		if (speed == SPEED_1000)
2958c2ecf20Sopenharmony_ci			val = RGMII_SPEED_1000;
2968c2ecf20Sopenharmony_ci		else if (speed == SPEED_100)
2978c2ecf20Sopenharmony_ci			val = RGMII_SPEED_100;
2988c2ecf20Sopenharmony_ci		else
2998c2ecf20Sopenharmony_ci			val = RGMII_SPEED_10;
3008c2ecf20Sopenharmony_ci		break;
3018c2ecf20Sopenharmony_ci	case PHY_INTERFACE_MODE_MII:
3028c2ecf20Sopenharmony_ci		if (speed == SPEED_100)
3038c2ecf20Sopenharmony_ci			val = MII_SPEED_100;
3048c2ecf20Sopenharmony_ci		else
3058c2ecf20Sopenharmony_ci			val = MII_SPEED_10;
3068c2ecf20Sopenharmony_ci		break;
3078c2ecf20Sopenharmony_ci	default:
3088c2ecf20Sopenharmony_ci		netdev_warn(dev, "not supported mode\n");
3098c2ecf20Sopenharmony_ci		val = MII_SPEED_10;
3108c2ecf20Sopenharmony_ci		break;
3118c2ecf20Sopenharmony_ci	}
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	if (duplex)
3148c2ecf20Sopenharmony_ci		val |= GMAC_FULL_DUPLEX;
3158c2ecf20Sopenharmony_ci	writel_relaxed(val, priv->ctrl_base);
3168c2ecf20Sopenharmony_ci	hix5hd2_mac_interface_reset(priv);
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	writel_relaxed(BIT_MODE_CHANGE_EN, priv->base + MODE_CHANGE_EN);
3198c2ecf20Sopenharmony_ci	if (speed == SPEED_1000)
3208c2ecf20Sopenharmony_ci		val = GMAC_SPEED_1000;
3218c2ecf20Sopenharmony_ci	else if (speed == SPEED_100)
3228c2ecf20Sopenharmony_ci		val = GMAC_SPEED_100;
3238c2ecf20Sopenharmony_ci	else
3248c2ecf20Sopenharmony_ci		val = GMAC_SPEED_10;
3258c2ecf20Sopenharmony_ci	writel_relaxed(val, priv->base + PORT_MODE);
3268c2ecf20Sopenharmony_ci	writel_relaxed(0, priv->base + MODE_CHANGE_EN);
3278c2ecf20Sopenharmony_ci	writel_relaxed(duplex, priv->base + MAC_DUPLEX_HALF_CTRL);
3288c2ecf20Sopenharmony_ci}
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_cistatic void hix5hd2_set_desc_depth(struct hix5hd2_priv *priv, int rx, int tx)
3318c2ecf20Sopenharmony_ci{
3328c2ecf20Sopenharmony_ci	writel_relaxed(BITS_RX_FQ_DEPTH_EN, priv->base + RX_FQ_REG_EN);
3338c2ecf20Sopenharmony_ci	writel_relaxed(rx << 3, priv->base + RX_FQ_DEPTH);
3348c2ecf20Sopenharmony_ci	writel_relaxed(0, priv->base + RX_FQ_REG_EN);
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	writel_relaxed(BITS_RX_BQ_DEPTH_EN, priv->base + RX_BQ_REG_EN);
3378c2ecf20Sopenharmony_ci	writel_relaxed(rx << 3, priv->base + RX_BQ_DEPTH);
3388c2ecf20Sopenharmony_ci	writel_relaxed(0, priv->base + RX_BQ_REG_EN);
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	writel_relaxed(BITS_TX_BQ_DEPTH_EN, priv->base + TX_BQ_REG_EN);
3418c2ecf20Sopenharmony_ci	writel_relaxed(tx << 3, priv->base + TX_BQ_DEPTH);
3428c2ecf20Sopenharmony_ci	writel_relaxed(0, priv->base + TX_BQ_REG_EN);
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	writel_relaxed(BITS_TX_RQ_DEPTH_EN, priv->base + TX_RQ_REG_EN);
3458c2ecf20Sopenharmony_ci	writel_relaxed(tx << 3, priv->base + TX_RQ_DEPTH);
3468c2ecf20Sopenharmony_ci	writel_relaxed(0, priv->base + TX_RQ_REG_EN);
3478c2ecf20Sopenharmony_ci}
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_cistatic void hix5hd2_set_rx_fq(struct hix5hd2_priv *priv, dma_addr_t phy_addr)
3508c2ecf20Sopenharmony_ci{
3518c2ecf20Sopenharmony_ci	writel_relaxed(BITS_RX_FQ_START_ADDR_EN, priv->base + RX_FQ_REG_EN);
3528c2ecf20Sopenharmony_ci	writel_relaxed(phy_addr, priv->base + RX_FQ_START_ADDR);
3538c2ecf20Sopenharmony_ci	writel_relaxed(0, priv->base + RX_FQ_REG_EN);
3548c2ecf20Sopenharmony_ci}
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_cistatic void hix5hd2_set_rx_bq(struct hix5hd2_priv *priv, dma_addr_t phy_addr)
3578c2ecf20Sopenharmony_ci{
3588c2ecf20Sopenharmony_ci	writel_relaxed(BITS_RX_BQ_START_ADDR_EN, priv->base + RX_BQ_REG_EN);
3598c2ecf20Sopenharmony_ci	writel_relaxed(phy_addr, priv->base + RX_BQ_START_ADDR);
3608c2ecf20Sopenharmony_ci	writel_relaxed(0, priv->base + RX_BQ_REG_EN);
3618c2ecf20Sopenharmony_ci}
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_cistatic void hix5hd2_set_tx_bq(struct hix5hd2_priv *priv, dma_addr_t phy_addr)
3648c2ecf20Sopenharmony_ci{
3658c2ecf20Sopenharmony_ci	writel_relaxed(BITS_TX_BQ_START_ADDR_EN, priv->base + TX_BQ_REG_EN);
3668c2ecf20Sopenharmony_ci	writel_relaxed(phy_addr, priv->base + TX_BQ_START_ADDR);
3678c2ecf20Sopenharmony_ci	writel_relaxed(0, priv->base + TX_BQ_REG_EN);
3688c2ecf20Sopenharmony_ci}
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_cistatic void hix5hd2_set_tx_rq(struct hix5hd2_priv *priv, dma_addr_t phy_addr)
3718c2ecf20Sopenharmony_ci{
3728c2ecf20Sopenharmony_ci	writel_relaxed(BITS_TX_RQ_START_ADDR_EN, priv->base + TX_RQ_REG_EN);
3738c2ecf20Sopenharmony_ci	writel_relaxed(phy_addr, priv->base + TX_RQ_START_ADDR);
3748c2ecf20Sopenharmony_ci	writel_relaxed(0, priv->base + TX_RQ_REG_EN);
3758c2ecf20Sopenharmony_ci}
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_cistatic void hix5hd2_set_desc_addr(struct hix5hd2_priv *priv)
3788c2ecf20Sopenharmony_ci{
3798c2ecf20Sopenharmony_ci	hix5hd2_set_rx_fq(priv, priv->rx_fq.phys_addr);
3808c2ecf20Sopenharmony_ci	hix5hd2_set_rx_bq(priv, priv->rx_bq.phys_addr);
3818c2ecf20Sopenharmony_ci	hix5hd2_set_tx_rq(priv, priv->tx_rq.phys_addr);
3828c2ecf20Sopenharmony_ci	hix5hd2_set_tx_bq(priv, priv->tx_bq.phys_addr);
3838c2ecf20Sopenharmony_ci}
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_cistatic void hix5hd2_hw_init(struct hix5hd2_priv *priv)
3868c2ecf20Sopenharmony_ci{
3878c2ecf20Sopenharmony_ci	u32 val;
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	/* disable and clear all interrupts */
3908c2ecf20Sopenharmony_ci	writel_relaxed(0, priv->base + ENA_PMU_INT);
3918c2ecf20Sopenharmony_ci	writel_relaxed(~0, priv->base + RAW_PMU_INT);
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	writel_relaxed(BIT_CRC_ERR_PASS, priv->base + REC_FILT_CONTROL);
3948c2ecf20Sopenharmony_ci	writel_relaxed(MAC_MAX_FRAME_SIZE, priv->base + CONTROL_WORD);
3958c2ecf20Sopenharmony_ci	writel_relaxed(0, priv->base + COL_SLOT_TIME);
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci	val = RX_BQ_INT_THRESHOLD | TX_RQ_INT_THRESHOLD << QUEUE_TX_BQ_SHIFT;
3988c2ecf20Sopenharmony_ci	writel_relaxed(val, priv->base + IN_QUEUE_TH);
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	writel_relaxed(RX_BQ_IN_TIMEOUT, priv->base + RX_BQ_IN_TIMEOUT_TH);
4018c2ecf20Sopenharmony_ci	writel_relaxed(TX_RQ_IN_TIMEOUT, priv->base + TX_RQ_IN_TIMEOUT_TH);
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci	hix5hd2_set_desc_depth(priv, RX_DESC_NUM, TX_DESC_NUM);
4048c2ecf20Sopenharmony_ci	hix5hd2_set_desc_addr(priv);
4058c2ecf20Sopenharmony_ci}
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_cistatic void hix5hd2_irq_enable(struct hix5hd2_priv *priv)
4088c2ecf20Sopenharmony_ci{
4098c2ecf20Sopenharmony_ci	writel_relaxed(DEF_INT_MASK, priv->base + ENA_PMU_INT);
4108c2ecf20Sopenharmony_ci}
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_cistatic void hix5hd2_irq_disable(struct hix5hd2_priv *priv)
4138c2ecf20Sopenharmony_ci{
4148c2ecf20Sopenharmony_ci	writel_relaxed(0, priv->base + ENA_PMU_INT);
4158c2ecf20Sopenharmony_ci}
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_cistatic void hix5hd2_port_enable(struct hix5hd2_priv *priv)
4188c2ecf20Sopenharmony_ci{
4198c2ecf20Sopenharmony_ci	writel_relaxed(0xf, priv->base + DESC_WR_RD_ENA);
4208c2ecf20Sopenharmony_ci	writel_relaxed(BITS_RX_EN | BITS_TX_EN, priv->base + PORT_EN);
4218c2ecf20Sopenharmony_ci}
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_cistatic void hix5hd2_port_disable(struct hix5hd2_priv *priv)
4248c2ecf20Sopenharmony_ci{
4258c2ecf20Sopenharmony_ci	writel_relaxed(~(u32)(BITS_RX_EN | BITS_TX_EN), priv->base + PORT_EN);
4268c2ecf20Sopenharmony_ci	writel_relaxed(0, priv->base + DESC_WR_RD_ENA);
4278c2ecf20Sopenharmony_ci}
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_cistatic void hix5hd2_hw_set_mac_addr(struct net_device *dev)
4308c2ecf20Sopenharmony_ci{
4318c2ecf20Sopenharmony_ci	struct hix5hd2_priv *priv = netdev_priv(dev);
4328c2ecf20Sopenharmony_ci	unsigned char *mac = dev->dev_addr;
4338c2ecf20Sopenharmony_ci	u32 val;
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci	val = mac[1] | (mac[0] << 8);
4368c2ecf20Sopenharmony_ci	writel_relaxed(val, priv->base + STATION_ADDR_HIGH);
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ci	val = mac[5] | (mac[4] << 8) | (mac[3] << 16) | (mac[2] << 24);
4398c2ecf20Sopenharmony_ci	writel_relaxed(val, priv->base + STATION_ADDR_LOW);
4408c2ecf20Sopenharmony_ci}
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_cistatic int hix5hd2_net_set_mac_address(struct net_device *dev, void *p)
4438c2ecf20Sopenharmony_ci{
4448c2ecf20Sopenharmony_ci	int ret;
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci	ret = eth_mac_addr(dev, p);
4478c2ecf20Sopenharmony_ci	if (!ret)
4488c2ecf20Sopenharmony_ci		hix5hd2_hw_set_mac_addr(dev);
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	return ret;
4518c2ecf20Sopenharmony_ci}
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_cistatic void hix5hd2_adjust_link(struct net_device *dev)
4548c2ecf20Sopenharmony_ci{
4558c2ecf20Sopenharmony_ci	struct hix5hd2_priv *priv = netdev_priv(dev);
4568c2ecf20Sopenharmony_ci	struct phy_device *phy = dev->phydev;
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	if ((priv->speed != phy->speed) || (priv->duplex != phy->duplex)) {
4598c2ecf20Sopenharmony_ci		hix5hd2_config_port(dev, phy->speed, phy->duplex);
4608c2ecf20Sopenharmony_ci		phy_print_status(phy);
4618c2ecf20Sopenharmony_ci	}
4628c2ecf20Sopenharmony_ci}
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_cistatic void hix5hd2_rx_refill(struct hix5hd2_priv *priv)
4658c2ecf20Sopenharmony_ci{
4668c2ecf20Sopenharmony_ci	struct hix5hd2_desc *desc;
4678c2ecf20Sopenharmony_ci	struct sk_buff *skb;
4688c2ecf20Sopenharmony_ci	u32 start, end, num, pos, i;
4698c2ecf20Sopenharmony_ci	u32 len = MAC_MAX_FRAME_SIZE;
4708c2ecf20Sopenharmony_ci	dma_addr_t addr;
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci	/* software write pointer */
4738c2ecf20Sopenharmony_ci	start = dma_cnt(readl_relaxed(priv->base + RX_FQ_WR_ADDR));
4748c2ecf20Sopenharmony_ci	/* logic read pointer */
4758c2ecf20Sopenharmony_ci	end = dma_cnt(readl_relaxed(priv->base + RX_FQ_RD_ADDR));
4768c2ecf20Sopenharmony_ci	num = CIRC_SPACE(start, end, RX_DESC_NUM);
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	for (i = 0, pos = start; i < num; i++) {
4798c2ecf20Sopenharmony_ci		if (priv->rx_skb[pos]) {
4808c2ecf20Sopenharmony_ci			break;
4818c2ecf20Sopenharmony_ci		} else {
4828c2ecf20Sopenharmony_ci			skb = netdev_alloc_skb_ip_align(priv->netdev, len);
4838c2ecf20Sopenharmony_ci			if (unlikely(skb == NULL))
4848c2ecf20Sopenharmony_ci				break;
4858c2ecf20Sopenharmony_ci		}
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci		addr = dma_map_single(priv->dev, skb->data, len, DMA_FROM_DEVICE);
4888c2ecf20Sopenharmony_ci		if (dma_mapping_error(priv->dev, addr)) {
4898c2ecf20Sopenharmony_ci			dev_kfree_skb_any(skb);
4908c2ecf20Sopenharmony_ci			break;
4918c2ecf20Sopenharmony_ci		}
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci		desc = priv->rx_fq.desc + pos;
4948c2ecf20Sopenharmony_ci		desc->buff_addr = cpu_to_le32(addr);
4958c2ecf20Sopenharmony_ci		priv->rx_skb[pos] = skb;
4968c2ecf20Sopenharmony_ci		desc->cmd = cpu_to_le32(DESC_VLD_FREE |
4978c2ecf20Sopenharmony_ci					(len - 1) << DESC_BUFF_LEN_OFF);
4988c2ecf20Sopenharmony_ci		pos = dma_ring_incr(pos, RX_DESC_NUM);
4998c2ecf20Sopenharmony_ci	}
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	/* ensure desc updated */
5028c2ecf20Sopenharmony_ci	wmb();
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci	if (pos != start)
5058c2ecf20Sopenharmony_ci		writel_relaxed(dma_byte(pos), priv->base + RX_FQ_WR_ADDR);
5068c2ecf20Sopenharmony_ci}
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_cistatic int hix5hd2_rx(struct net_device *dev, int limit)
5098c2ecf20Sopenharmony_ci{
5108c2ecf20Sopenharmony_ci	struct hix5hd2_priv *priv = netdev_priv(dev);
5118c2ecf20Sopenharmony_ci	struct sk_buff *skb;
5128c2ecf20Sopenharmony_ci	struct hix5hd2_desc *desc;
5138c2ecf20Sopenharmony_ci	dma_addr_t addr;
5148c2ecf20Sopenharmony_ci	u32 start, end, num, pos, i, len;
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci	/* software read pointer */
5178c2ecf20Sopenharmony_ci	start = dma_cnt(readl_relaxed(priv->base + RX_BQ_RD_ADDR));
5188c2ecf20Sopenharmony_ci	/* logic write pointer */
5198c2ecf20Sopenharmony_ci	end = dma_cnt(readl_relaxed(priv->base + RX_BQ_WR_ADDR));
5208c2ecf20Sopenharmony_ci	num = CIRC_CNT(end, start, RX_DESC_NUM);
5218c2ecf20Sopenharmony_ci	if (num > limit)
5228c2ecf20Sopenharmony_ci		num = limit;
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci	/* ensure get updated desc */
5258c2ecf20Sopenharmony_ci	rmb();
5268c2ecf20Sopenharmony_ci	for (i = 0, pos = start; i < num; i++) {
5278c2ecf20Sopenharmony_ci		skb = priv->rx_skb[pos];
5288c2ecf20Sopenharmony_ci		if (unlikely(!skb)) {
5298c2ecf20Sopenharmony_ci			netdev_err(dev, "inconsistent rx_skb\n");
5308c2ecf20Sopenharmony_ci			break;
5318c2ecf20Sopenharmony_ci		}
5328c2ecf20Sopenharmony_ci		priv->rx_skb[pos] = NULL;
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci		desc = priv->rx_bq.desc + pos;
5358c2ecf20Sopenharmony_ci		len = (le32_to_cpu(desc->cmd) >> DESC_DATA_LEN_OFF) &
5368c2ecf20Sopenharmony_ci		       DESC_DATA_MASK;
5378c2ecf20Sopenharmony_ci		addr = le32_to_cpu(desc->buff_addr);
5388c2ecf20Sopenharmony_ci		dma_unmap_single(priv->dev, addr, MAC_MAX_FRAME_SIZE,
5398c2ecf20Sopenharmony_ci				 DMA_FROM_DEVICE);
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci		skb_put(skb, len);
5428c2ecf20Sopenharmony_ci		if (skb->len > MAC_MAX_FRAME_SIZE) {
5438c2ecf20Sopenharmony_ci			netdev_err(dev, "rcv len err, len = %d\n", skb->len);
5448c2ecf20Sopenharmony_ci			dev->stats.rx_errors++;
5458c2ecf20Sopenharmony_ci			dev->stats.rx_length_errors++;
5468c2ecf20Sopenharmony_ci			dev_kfree_skb_any(skb);
5478c2ecf20Sopenharmony_ci			goto next;
5488c2ecf20Sopenharmony_ci		}
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_ci		skb->protocol = eth_type_trans(skb, dev);
5518c2ecf20Sopenharmony_ci		napi_gro_receive(&priv->napi, skb);
5528c2ecf20Sopenharmony_ci		dev->stats.rx_packets++;
5538c2ecf20Sopenharmony_ci		dev->stats.rx_bytes += len;
5548c2ecf20Sopenharmony_cinext:
5558c2ecf20Sopenharmony_ci		pos = dma_ring_incr(pos, RX_DESC_NUM);
5568c2ecf20Sopenharmony_ci	}
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci	if (pos != start)
5598c2ecf20Sopenharmony_ci		writel_relaxed(dma_byte(pos), priv->base + RX_BQ_RD_ADDR);
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_ci	hix5hd2_rx_refill(priv);
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci	return num;
5648c2ecf20Sopenharmony_ci}
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_cistatic void hix5hd2_clean_sg_desc(struct hix5hd2_priv *priv,
5678c2ecf20Sopenharmony_ci				  struct sk_buff *skb, u32 pos)
5688c2ecf20Sopenharmony_ci{
5698c2ecf20Sopenharmony_ci	struct sg_desc *desc;
5708c2ecf20Sopenharmony_ci	dma_addr_t addr;
5718c2ecf20Sopenharmony_ci	u32 len;
5728c2ecf20Sopenharmony_ci	int i;
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ci	desc = priv->tx_ring.desc + pos;
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ci	addr = le32_to_cpu(desc->linear_addr);
5778c2ecf20Sopenharmony_ci	len = le32_to_cpu(desc->linear_len);
5788c2ecf20Sopenharmony_ci	dma_unmap_single(priv->dev, addr, len, DMA_TO_DEVICE);
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
5818c2ecf20Sopenharmony_ci		addr = le32_to_cpu(desc->frags[i].addr);
5828c2ecf20Sopenharmony_ci		len = le32_to_cpu(desc->frags[i].size);
5838c2ecf20Sopenharmony_ci		dma_unmap_page(priv->dev, addr, len, DMA_TO_DEVICE);
5848c2ecf20Sopenharmony_ci	}
5858c2ecf20Sopenharmony_ci}
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_cistatic void hix5hd2_xmit_reclaim(struct net_device *dev)
5888c2ecf20Sopenharmony_ci{
5898c2ecf20Sopenharmony_ci	struct sk_buff *skb;
5908c2ecf20Sopenharmony_ci	struct hix5hd2_desc *desc;
5918c2ecf20Sopenharmony_ci	struct hix5hd2_priv *priv = netdev_priv(dev);
5928c2ecf20Sopenharmony_ci	unsigned int bytes_compl = 0, pkts_compl = 0;
5938c2ecf20Sopenharmony_ci	u32 start, end, num, pos, i;
5948c2ecf20Sopenharmony_ci	dma_addr_t addr;
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci	netif_tx_lock(dev);
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci	/* software read */
5998c2ecf20Sopenharmony_ci	start = dma_cnt(readl_relaxed(priv->base + TX_RQ_RD_ADDR));
6008c2ecf20Sopenharmony_ci	/* logic write */
6018c2ecf20Sopenharmony_ci	end = dma_cnt(readl_relaxed(priv->base + TX_RQ_WR_ADDR));
6028c2ecf20Sopenharmony_ci	num = CIRC_CNT(end, start, TX_DESC_NUM);
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci	for (i = 0, pos = start; i < num; i++) {
6058c2ecf20Sopenharmony_ci		skb = priv->tx_skb[pos];
6068c2ecf20Sopenharmony_ci		if (unlikely(!skb)) {
6078c2ecf20Sopenharmony_ci			netdev_err(dev, "inconsistent tx_skb\n");
6088c2ecf20Sopenharmony_ci			break;
6098c2ecf20Sopenharmony_ci		}
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_ci		pkts_compl++;
6128c2ecf20Sopenharmony_ci		bytes_compl += skb->len;
6138c2ecf20Sopenharmony_ci		desc = priv->tx_rq.desc + pos;
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci		if (skb_shinfo(skb)->nr_frags) {
6168c2ecf20Sopenharmony_ci			hix5hd2_clean_sg_desc(priv, skb, pos);
6178c2ecf20Sopenharmony_ci		} else {
6188c2ecf20Sopenharmony_ci			addr = le32_to_cpu(desc->buff_addr);
6198c2ecf20Sopenharmony_ci			dma_unmap_single(priv->dev, addr, skb->len,
6208c2ecf20Sopenharmony_ci					 DMA_TO_DEVICE);
6218c2ecf20Sopenharmony_ci		}
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_ci		priv->tx_skb[pos] = NULL;
6248c2ecf20Sopenharmony_ci		dev_consume_skb_any(skb);
6258c2ecf20Sopenharmony_ci		pos = dma_ring_incr(pos, TX_DESC_NUM);
6268c2ecf20Sopenharmony_ci	}
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci	if (pos != start)
6298c2ecf20Sopenharmony_ci		writel_relaxed(dma_byte(pos), priv->base + TX_RQ_RD_ADDR);
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci	netif_tx_unlock(dev);
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_ci	if (pkts_compl || bytes_compl)
6348c2ecf20Sopenharmony_ci		netdev_completed_queue(dev, pkts_compl, bytes_compl);
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci	if (unlikely(netif_queue_stopped(priv->netdev)) && pkts_compl)
6378c2ecf20Sopenharmony_ci		netif_wake_queue(priv->netdev);
6388c2ecf20Sopenharmony_ci}
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_cistatic int hix5hd2_poll(struct napi_struct *napi, int budget)
6418c2ecf20Sopenharmony_ci{
6428c2ecf20Sopenharmony_ci	struct hix5hd2_priv *priv = container_of(napi,
6438c2ecf20Sopenharmony_ci				struct hix5hd2_priv, napi);
6448c2ecf20Sopenharmony_ci	struct net_device *dev = priv->netdev;
6458c2ecf20Sopenharmony_ci	int work_done = 0, task = budget;
6468c2ecf20Sopenharmony_ci	int ints, num;
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci	do {
6498c2ecf20Sopenharmony_ci		hix5hd2_xmit_reclaim(dev);
6508c2ecf20Sopenharmony_ci		num = hix5hd2_rx(dev, task);
6518c2ecf20Sopenharmony_ci		work_done += num;
6528c2ecf20Sopenharmony_ci		task -= num;
6538c2ecf20Sopenharmony_ci		if ((work_done >= budget) || (num == 0))
6548c2ecf20Sopenharmony_ci			break;
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ci		ints = readl_relaxed(priv->base + RAW_PMU_INT);
6578c2ecf20Sopenharmony_ci		writel_relaxed(ints, priv->base + RAW_PMU_INT);
6588c2ecf20Sopenharmony_ci	} while (ints & DEF_INT_MASK);
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_ci	if (work_done < budget) {
6618c2ecf20Sopenharmony_ci		napi_complete_done(napi, work_done);
6628c2ecf20Sopenharmony_ci		hix5hd2_irq_enable(priv);
6638c2ecf20Sopenharmony_ci	}
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci	return work_done;
6668c2ecf20Sopenharmony_ci}
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_cistatic irqreturn_t hix5hd2_interrupt(int irq, void *dev_id)
6698c2ecf20Sopenharmony_ci{
6708c2ecf20Sopenharmony_ci	struct net_device *dev = (struct net_device *)dev_id;
6718c2ecf20Sopenharmony_ci	struct hix5hd2_priv *priv = netdev_priv(dev);
6728c2ecf20Sopenharmony_ci	int ints = readl_relaxed(priv->base + RAW_PMU_INT);
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci	writel_relaxed(ints, priv->base + RAW_PMU_INT);
6758c2ecf20Sopenharmony_ci	if (likely(ints & DEF_INT_MASK)) {
6768c2ecf20Sopenharmony_ci		hix5hd2_irq_disable(priv);
6778c2ecf20Sopenharmony_ci		napi_schedule(&priv->napi);
6788c2ecf20Sopenharmony_ci	}
6798c2ecf20Sopenharmony_ci
6808c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
6818c2ecf20Sopenharmony_ci}
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_cistatic u32 hix5hd2_get_desc_cmd(struct sk_buff *skb, unsigned long hw_cap)
6848c2ecf20Sopenharmony_ci{
6858c2ecf20Sopenharmony_ci	u32 cmd = 0;
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_ci	if (HAS_CAP_TSO(hw_cap)) {
6888c2ecf20Sopenharmony_ci		if (skb_shinfo(skb)->nr_frags)
6898c2ecf20Sopenharmony_ci			cmd |= DESC_SG;
6908c2ecf20Sopenharmony_ci		cmd |= skb_shinfo(skb)->nr_frags << DESC_FRAGS_NUM_OFF;
6918c2ecf20Sopenharmony_ci	} else {
6928c2ecf20Sopenharmony_ci		cmd |= DESC_FL_FULL |
6938c2ecf20Sopenharmony_ci			((skb->len & DESC_DATA_MASK) << DESC_BUFF_LEN_OFF);
6948c2ecf20Sopenharmony_ci	}
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_ci	cmd |= (skb->len & DESC_DATA_MASK) << DESC_DATA_LEN_OFF;
6978c2ecf20Sopenharmony_ci	cmd |= DESC_VLD_BUSY;
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_ci	return cmd;
7008c2ecf20Sopenharmony_ci}
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_cistatic int hix5hd2_fill_sg_desc(struct hix5hd2_priv *priv,
7038c2ecf20Sopenharmony_ci				struct sk_buff *skb, u32 pos)
7048c2ecf20Sopenharmony_ci{
7058c2ecf20Sopenharmony_ci	struct sg_desc *desc;
7068c2ecf20Sopenharmony_ci	dma_addr_t addr;
7078c2ecf20Sopenharmony_ci	int ret;
7088c2ecf20Sopenharmony_ci	int i;
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci	desc = priv->tx_ring.desc + pos;
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci	desc->total_len = cpu_to_le32(skb->len);
7138c2ecf20Sopenharmony_ci	addr = dma_map_single(priv->dev, skb->data, skb_headlen(skb),
7148c2ecf20Sopenharmony_ci			      DMA_TO_DEVICE);
7158c2ecf20Sopenharmony_ci	if (unlikely(dma_mapping_error(priv->dev, addr)))
7168c2ecf20Sopenharmony_ci		return -EINVAL;
7178c2ecf20Sopenharmony_ci	desc->linear_addr = cpu_to_le32(addr);
7188c2ecf20Sopenharmony_ci	desc->linear_len = cpu_to_le32(skb_headlen(skb));
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_ci	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
7218c2ecf20Sopenharmony_ci		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
7228c2ecf20Sopenharmony_ci		int len = skb_frag_size(frag);
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ci		addr = skb_frag_dma_map(priv->dev, frag, 0, len, DMA_TO_DEVICE);
7258c2ecf20Sopenharmony_ci		ret = dma_mapping_error(priv->dev, addr);
7268c2ecf20Sopenharmony_ci		if (unlikely(ret))
7278c2ecf20Sopenharmony_ci			return -EINVAL;
7288c2ecf20Sopenharmony_ci		desc->frags[i].addr = cpu_to_le32(addr);
7298c2ecf20Sopenharmony_ci		desc->frags[i].size = cpu_to_le32(len);
7308c2ecf20Sopenharmony_ci	}
7318c2ecf20Sopenharmony_ci
7328c2ecf20Sopenharmony_ci	return 0;
7338c2ecf20Sopenharmony_ci}
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_cistatic netdev_tx_t hix5hd2_net_xmit(struct sk_buff *skb, struct net_device *dev)
7368c2ecf20Sopenharmony_ci{
7378c2ecf20Sopenharmony_ci	struct hix5hd2_priv *priv = netdev_priv(dev);
7388c2ecf20Sopenharmony_ci	struct hix5hd2_desc *desc;
7398c2ecf20Sopenharmony_ci	dma_addr_t addr;
7408c2ecf20Sopenharmony_ci	u32 pos;
7418c2ecf20Sopenharmony_ci	u32 cmd;
7428c2ecf20Sopenharmony_ci	int ret;
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci	/* software write pointer */
7458c2ecf20Sopenharmony_ci	pos = dma_cnt(readl_relaxed(priv->base + TX_BQ_WR_ADDR));
7468c2ecf20Sopenharmony_ci	if (unlikely(priv->tx_skb[pos])) {
7478c2ecf20Sopenharmony_ci		dev->stats.tx_dropped++;
7488c2ecf20Sopenharmony_ci		dev->stats.tx_fifo_errors++;
7498c2ecf20Sopenharmony_ci		netif_stop_queue(dev);
7508c2ecf20Sopenharmony_ci		return NETDEV_TX_BUSY;
7518c2ecf20Sopenharmony_ci	}
7528c2ecf20Sopenharmony_ci
7538c2ecf20Sopenharmony_ci	desc = priv->tx_bq.desc + pos;
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_ci	cmd = hix5hd2_get_desc_cmd(skb, priv->hw_cap);
7568c2ecf20Sopenharmony_ci	desc->cmd = cpu_to_le32(cmd);
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci	if (skb_shinfo(skb)->nr_frags) {
7598c2ecf20Sopenharmony_ci		ret = hix5hd2_fill_sg_desc(priv, skb, pos);
7608c2ecf20Sopenharmony_ci		if (unlikely(ret)) {
7618c2ecf20Sopenharmony_ci			dev_kfree_skb_any(skb);
7628c2ecf20Sopenharmony_ci			dev->stats.tx_dropped++;
7638c2ecf20Sopenharmony_ci			return NETDEV_TX_OK;
7648c2ecf20Sopenharmony_ci		}
7658c2ecf20Sopenharmony_ci		addr = priv->tx_ring.phys_addr + pos * sizeof(struct sg_desc);
7668c2ecf20Sopenharmony_ci	} else {
7678c2ecf20Sopenharmony_ci		addr = dma_map_single(priv->dev, skb->data, skb->len,
7688c2ecf20Sopenharmony_ci				      DMA_TO_DEVICE);
7698c2ecf20Sopenharmony_ci		if (unlikely(dma_mapping_error(priv->dev, addr))) {
7708c2ecf20Sopenharmony_ci			dev_kfree_skb_any(skb);
7718c2ecf20Sopenharmony_ci			dev->stats.tx_dropped++;
7728c2ecf20Sopenharmony_ci			return NETDEV_TX_OK;
7738c2ecf20Sopenharmony_ci		}
7748c2ecf20Sopenharmony_ci	}
7758c2ecf20Sopenharmony_ci	desc->buff_addr = cpu_to_le32(addr);
7768c2ecf20Sopenharmony_ci
7778c2ecf20Sopenharmony_ci	priv->tx_skb[pos] = skb;
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ci	/* ensure desc updated */
7808c2ecf20Sopenharmony_ci	wmb();
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_ci	pos = dma_ring_incr(pos, TX_DESC_NUM);
7838c2ecf20Sopenharmony_ci	writel_relaxed(dma_byte(pos), priv->base + TX_BQ_WR_ADDR);
7848c2ecf20Sopenharmony_ci
7858c2ecf20Sopenharmony_ci	netif_trans_update(dev);
7868c2ecf20Sopenharmony_ci	dev->stats.tx_packets++;
7878c2ecf20Sopenharmony_ci	dev->stats.tx_bytes += skb->len;
7888c2ecf20Sopenharmony_ci	netdev_sent_queue(dev, skb->len);
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_ci	return NETDEV_TX_OK;
7918c2ecf20Sopenharmony_ci}
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_cistatic void hix5hd2_free_dma_desc_rings(struct hix5hd2_priv *priv)
7948c2ecf20Sopenharmony_ci{
7958c2ecf20Sopenharmony_ci	struct hix5hd2_desc *desc;
7968c2ecf20Sopenharmony_ci	dma_addr_t addr;
7978c2ecf20Sopenharmony_ci	int i;
7988c2ecf20Sopenharmony_ci
7998c2ecf20Sopenharmony_ci	for (i = 0; i < RX_DESC_NUM; i++) {
8008c2ecf20Sopenharmony_ci		struct sk_buff *skb = priv->rx_skb[i];
8018c2ecf20Sopenharmony_ci		if (skb == NULL)
8028c2ecf20Sopenharmony_ci			continue;
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_ci		desc = priv->rx_fq.desc + i;
8058c2ecf20Sopenharmony_ci		addr = le32_to_cpu(desc->buff_addr);
8068c2ecf20Sopenharmony_ci		dma_unmap_single(priv->dev, addr,
8078c2ecf20Sopenharmony_ci				 MAC_MAX_FRAME_SIZE, DMA_FROM_DEVICE);
8088c2ecf20Sopenharmony_ci		dev_kfree_skb_any(skb);
8098c2ecf20Sopenharmony_ci		priv->rx_skb[i] = NULL;
8108c2ecf20Sopenharmony_ci	}
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_ci	for (i = 0; i < TX_DESC_NUM; i++) {
8138c2ecf20Sopenharmony_ci		struct sk_buff *skb = priv->tx_skb[i];
8148c2ecf20Sopenharmony_ci		if (skb == NULL)
8158c2ecf20Sopenharmony_ci			continue;
8168c2ecf20Sopenharmony_ci
8178c2ecf20Sopenharmony_ci		desc = priv->tx_rq.desc + i;
8188c2ecf20Sopenharmony_ci		addr = le32_to_cpu(desc->buff_addr);
8198c2ecf20Sopenharmony_ci		dma_unmap_single(priv->dev, addr, skb->len, DMA_TO_DEVICE);
8208c2ecf20Sopenharmony_ci		dev_kfree_skb_any(skb);
8218c2ecf20Sopenharmony_ci		priv->tx_skb[i] = NULL;
8228c2ecf20Sopenharmony_ci	}
8238c2ecf20Sopenharmony_ci}
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_cistatic int hix5hd2_net_open(struct net_device *dev)
8268c2ecf20Sopenharmony_ci{
8278c2ecf20Sopenharmony_ci	struct hix5hd2_priv *priv = netdev_priv(dev);
8288c2ecf20Sopenharmony_ci	struct phy_device *phy;
8298c2ecf20Sopenharmony_ci	int ret;
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(priv->mac_core_clk);
8328c2ecf20Sopenharmony_ci	if (ret < 0) {
8338c2ecf20Sopenharmony_ci		netdev_err(dev, "failed to enable mac core clk %d\n", ret);
8348c2ecf20Sopenharmony_ci		return ret;
8358c2ecf20Sopenharmony_ci	}
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(priv->mac_ifc_clk);
8388c2ecf20Sopenharmony_ci	if (ret < 0) {
8398c2ecf20Sopenharmony_ci		clk_disable_unprepare(priv->mac_core_clk);
8408c2ecf20Sopenharmony_ci		netdev_err(dev, "failed to enable mac ifc clk %d\n", ret);
8418c2ecf20Sopenharmony_ci		return ret;
8428c2ecf20Sopenharmony_ci	}
8438c2ecf20Sopenharmony_ci
8448c2ecf20Sopenharmony_ci	phy = of_phy_connect(dev, priv->phy_node,
8458c2ecf20Sopenharmony_ci			     &hix5hd2_adjust_link, 0, priv->phy_mode);
8468c2ecf20Sopenharmony_ci	if (!phy) {
8478c2ecf20Sopenharmony_ci		clk_disable_unprepare(priv->mac_ifc_clk);
8488c2ecf20Sopenharmony_ci		clk_disable_unprepare(priv->mac_core_clk);
8498c2ecf20Sopenharmony_ci		return -ENODEV;
8508c2ecf20Sopenharmony_ci	}
8518c2ecf20Sopenharmony_ci
8528c2ecf20Sopenharmony_ci	phy_start(phy);
8538c2ecf20Sopenharmony_ci	hix5hd2_hw_init(priv);
8548c2ecf20Sopenharmony_ci	hix5hd2_rx_refill(priv);
8558c2ecf20Sopenharmony_ci
8568c2ecf20Sopenharmony_ci	netdev_reset_queue(dev);
8578c2ecf20Sopenharmony_ci	netif_start_queue(dev);
8588c2ecf20Sopenharmony_ci	napi_enable(&priv->napi);
8598c2ecf20Sopenharmony_ci
8608c2ecf20Sopenharmony_ci	hix5hd2_port_enable(priv);
8618c2ecf20Sopenharmony_ci	hix5hd2_irq_enable(priv);
8628c2ecf20Sopenharmony_ci
8638c2ecf20Sopenharmony_ci	return 0;
8648c2ecf20Sopenharmony_ci}
8658c2ecf20Sopenharmony_ci
8668c2ecf20Sopenharmony_cistatic int hix5hd2_net_close(struct net_device *dev)
8678c2ecf20Sopenharmony_ci{
8688c2ecf20Sopenharmony_ci	struct hix5hd2_priv *priv = netdev_priv(dev);
8698c2ecf20Sopenharmony_ci
8708c2ecf20Sopenharmony_ci	hix5hd2_port_disable(priv);
8718c2ecf20Sopenharmony_ci	hix5hd2_irq_disable(priv);
8728c2ecf20Sopenharmony_ci	napi_disable(&priv->napi);
8738c2ecf20Sopenharmony_ci	netif_stop_queue(dev);
8748c2ecf20Sopenharmony_ci	hix5hd2_free_dma_desc_rings(priv);
8758c2ecf20Sopenharmony_ci
8768c2ecf20Sopenharmony_ci	if (dev->phydev) {
8778c2ecf20Sopenharmony_ci		phy_stop(dev->phydev);
8788c2ecf20Sopenharmony_ci		phy_disconnect(dev->phydev);
8798c2ecf20Sopenharmony_ci	}
8808c2ecf20Sopenharmony_ci
8818c2ecf20Sopenharmony_ci	clk_disable_unprepare(priv->mac_ifc_clk);
8828c2ecf20Sopenharmony_ci	clk_disable_unprepare(priv->mac_core_clk);
8838c2ecf20Sopenharmony_ci
8848c2ecf20Sopenharmony_ci	return 0;
8858c2ecf20Sopenharmony_ci}
8868c2ecf20Sopenharmony_ci
8878c2ecf20Sopenharmony_cistatic void hix5hd2_tx_timeout_task(struct work_struct *work)
8888c2ecf20Sopenharmony_ci{
8898c2ecf20Sopenharmony_ci	struct hix5hd2_priv *priv;
8908c2ecf20Sopenharmony_ci
8918c2ecf20Sopenharmony_ci	priv = container_of(work, struct hix5hd2_priv, tx_timeout_task);
8928c2ecf20Sopenharmony_ci	hix5hd2_net_close(priv->netdev);
8938c2ecf20Sopenharmony_ci	hix5hd2_net_open(priv->netdev);
8948c2ecf20Sopenharmony_ci}
8958c2ecf20Sopenharmony_ci
8968c2ecf20Sopenharmony_cistatic void hix5hd2_net_timeout(struct net_device *dev, unsigned int txqueue)
8978c2ecf20Sopenharmony_ci{
8988c2ecf20Sopenharmony_ci	struct hix5hd2_priv *priv = netdev_priv(dev);
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_ci	schedule_work(&priv->tx_timeout_task);
9018c2ecf20Sopenharmony_ci}
9028c2ecf20Sopenharmony_ci
9038c2ecf20Sopenharmony_cistatic const struct net_device_ops hix5hd2_netdev_ops = {
9048c2ecf20Sopenharmony_ci	.ndo_open		= hix5hd2_net_open,
9058c2ecf20Sopenharmony_ci	.ndo_stop		= hix5hd2_net_close,
9068c2ecf20Sopenharmony_ci	.ndo_start_xmit		= hix5hd2_net_xmit,
9078c2ecf20Sopenharmony_ci	.ndo_tx_timeout		= hix5hd2_net_timeout,
9088c2ecf20Sopenharmony_ci	.ndo_set_mac_address	= hix5hd2_net_set_mac_address,
9098c2ecf20Sopenharmony_ci};
9108c2ecf20Sopenharmony_ci
9118c2ecf20Sopenharmony_cistatic const struct ethtool_ops hix5hd2_ethtools_ops = {
9128c2ecf20Sopenharmony_ci	.get_link		= ethtool_op_get_link,
9138c2ecf20Sopenharmony_ci	.get_link_ksettings     = phy_ethtool_get_link_ksettings,
9148c2ecf20Sopenharmony_ci	.set_link_ksettings     = phy_ethtool_set_link_ksettings,
9158c2ecf20Sopenharmony_ci};
9168c2ecf20Sopenharmony_ci
9178c2ecf20Sopenharmony_cistatic int hix5hd2_mdio_wait_ready(struct mii_bus *bus)
9188c2ecf20Sopenharmony_ci{
9198c2ecf20Sopenharmony_ci	struct hix5hd2_priv *priv = bus->priv;
9208c2ecf20Sopenharmony_ci	void __iomem *base = priv->base;
9218c2ecf20Sopenharmony_ci	int i, timeout = 10000;
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_ci	for (i = 0; readl_relaxed(base + MDIO_SINGLE_CMD) & MDIO_START; i++) {
9248c2ecf20Sopenharmony_ci		if (i == timeout)
9258c2ecf20Sopenharmony_ci			return -ETIMEDOUT;
9268c2ecf20Sopenharmony_ci		usleep_range(10, 20);
9278c2ecf20Sopenharmony_ci	}
9288c2ecf20Sopenharmony_ci
9298c2ecf20Sopenharmony_ci	return 0;
9308c2ecf20Sopenharmony_ci}
9318c2ecf20Sopenharmony_ci
9328c2ecf20Sopenharmony_cistatic int hix5hd2_mdio_read(struct mii_bus *bus, int phy, int reg)
9338c2ecf20Sopenharmony_ci{
9348c2ecf20Sopenharmony_ci	struct hix5hd2_priv *priv = bus->priv;
9358c2ecf20Sopenharmony_ci	void __iomem *base = priv->base;
9368c2ecf20Sopenharmony_ci	int val, ret;
9378c2ecf20Sopenharmony_ci
9388c2ecf20Sopenharmony_ci	ret = hix5hd2_mdio_wait_ready(bus);
9398c2ecf20Sopenharmony_ci	if (ret < 0)
9408c2ecf20Sopenharmony_ci		goto out;
9418c2ecf20Sopenharmony_ci
9428c2ecf20Sopenharmony_ci	writel_relaxed(MDIO_READ | phy << 8 | reg, base + MDIO_SINGLE_CMD);
9438c2ecf20Sopenharmony_ci	ret = hix5hd2_mdio_wait_ready(bus);
9448c2ecf20Sopenharmony_ci	if (ret < 0)
9458c2ecf20Sopenharmony_ci		goto out;
9468c2ecf20Sopenharmony_ci
9478c2ecf20Sopenharmony_ci	val = readl_relaxed(base + MDIO_RDATA_STATUS);
9488c2ecf20Sopenharmony_ci	if (val & MDIO_R_VALID) {
9498c2ecf20Sopenharmony_ci		dev_err(bus->parent, "SMI bus read not valid\n");
9508c2ecf20Sopenharmony_ci		ret = -ENODEV;
9518c2ecf20Sopenharmony_ci		goto out;
9528c2ecf20Sopenharmony_ci	}
9538c2ecf20Sopenharmony_ci
9548c2ecf20Sopenharmony_ci	val = readl_relaxed(priv->base + MDIO_SINGLE_DATA);
9558c2ecf20Sopenharmony_ci	ret = (val >> 16) & 0xFFFF;
9568c2ecf20Sopenharmony_ciout:
9578c2ecf20Sopenharmony_ci	return ret;
9588c2ecf20Sopenharmony_ci}
9598c2ecf20Sopenharmony_ci
9608c2ecf20Sopenharmony_cistatic int hix5hd2_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
9618c2ecf20Sopenharmony_ci{
9628c2ecf20Sopenharmony_ci	struct hix5hd2_priv *priv = bus->priv;
9638c2ecf20Sopenharmony_ci	void __iomem *base = priv->base;
9648c2ecf20Sopenharmony_ci	int ret;
9658c2ecf20Sopenharmony_ci
9668c2ecf20Sopenharmony_ci	ret = hix5hd2_mdio_wait_ready(bus);
9678c2ecf20Sopenharmony_ci	if (ret < 0)
9688c2ecf20Sopenharmony_ci		goto out;
9698c2ecf20Sopenharmony_ci
9708c2ecf20Sopenharmony_ci	writel_relaxed(val, base + MDIO_SINGLE_DATA);
9718c2ecf20Sopenharmony_ci	writel_relaxed(MDIO_WRITE | phy << 8 | reg, base + MDIO_SINGLE_CMD);
9728c2ecf20Sopenharmony_ci	ret = hix5hd2_mdio_wait_ready(bus);
9738c2ecf20Sopenharmony_ciout:
9748c2ecf20Sopenharmony_ci	return ret;
9758c2ecf20Sopenharmony_ci}
9768c2ecf20Sopenharmony_ci
9778c2ecf20Sopenharmony_cistatic void hix5hd2_destroy_hw_desc_queue(struct hix5hd2_priv *priv)
9788c2ecf20Sopenharmony_ci{
9798c2ecf20Sopenharmony_ci	int i;
9808c2ecf20Sopenharmony_ci
9818c2ecf20Sopenharmony_ci	for (i = 0; i < QUEUE_NUMS; i++) {
9828c2ecf20Sopenharmony_ci		if (priv->pool[i].desc) {
9838c2ecf20Sopenharmony_ci			dma_free_coherent(priv->dev, priv->pool[i].size,
9848c2ecf20Sopenharmony_ci					  priv->pool[i].desc,
9858c2ecf20Sopenharmony_ci					  priv->pool[i].phys_addr);
9868c2ecf20Sopenharmony_ci			priv->pool[i].desc = NULL;
9878c2ecf20Sopenharmony_ci		}
9888c2ecf20Sopenharmony_ci	}
9898c2ecf20Sopenharmony_ci}
9908c2ecf20Sopenharmony_ci
9918c2ecf20Sopenharmony_cistatic int hix5hd2_init_hw_desc_queue(struct hix5hd2_priv *priv)
9928c2ecf20Sopenharmony_ci{
9938c2ecf20Sopenharmony_ci	struct device *dev = priv->dev;
9948c2ecf20Sopenharmony_ci	struct hix5hd2_desc *virt_addr;
9958c2ecf20Sopenharmony_ci	dma_addr_t phys_addr;
9968c2ecf20Sopenharmony_ci	int size, i;
9978c2ecf20Sopenharmony_ci
9988c2ecf20Sopenharmony_ci	priv->rx_fq.count = RX_DESC_NUM;
9998c2ecf20Sopenharmony_ci	priv->rx_bq.count = RX_DESC_NUM;
10008c2ecf20Sopenharmony_ci	priv->tx_bq.count = TX_DESC_NUM;
10018c2ecf20Sopenharmony_ci	priv->tx_rq.count = TX_DESC_NUM;
10028c2ecf20Sopenharmony_ci
10038c2ecf20Sopenharmony_ci	for (i = 0; i < QUEUE_NUMS; i++) {
10048c2ecf20Sopenharmony_ci		size = priv->pool[i].count * sizeof(struct hix5hd2_desc);
10058c2ecf20Sopenharmony_ci		virt_addr = dma_alloc_coherent(dev, size, &phys_addr,
10068c2ecf20Sopenharmony_ci					       GFP_KERNEL);
10078c2ecf20Sopenharmony_ci		if (virt_addr == NULL)
10088c2ecf20Sopenharmony_ci			goto error_free_pool;
10098c2ecf20Sopenharmony_ci
10108c2ecf20Sopenharmony_ci		priv->pool[i].size = size;
10118c2ecf20Sopenharmony_ci		priv->pool[i].desc = virt_addr;
10128c2ecf20Sopenharmony_ci		priv->pool[i].phys_addr = phys_addr;
10138c2ecf20Sopenharmony_ci	}
10148c2ecf20Sopenharmony_ci	return 0;
10158c2ecf20Sopenharmony_ci
10168c2ecf20Sopenharmony_cierror_free_pool:
10178c2ecf20Sopenharmony_ci	hix5hd2_destroy_hw_desc_queue(priv);
10188c2ecf20Sopenharmony_ci
10198c2ecf20Sopenharmony_ci	return -ENOMEM;
10208c2ecf20Sopenharmony_ci}
10218c2ecf20Sopenharmony_ci
10228c2ecf20Sopenharmony_cistatic int hix5hd2_init_sg_desc_queue(struct hix5hd2_priv *priv)
10238c2ecf20Sopenharmony_ci{
10248c2ecf20Sopenharmony_ci	struct sg_desc *desc;
10258c2ecf20Sopenharmony_ci	dma_addr_t phys_addr;
10268c2ecf20Sopenharmony_ci
10278c2ecf20Sopenharmony_ci	desc = dma_alloc_coherent(priv->dev,
10288c2ecf20Sopenharmony_ci				  TX_DESC_NUM * sizeof(struct sg_desc),
10298c2ecf20Sopenharmony_ci				  &phys_addr, GFP_KERNEL);
10308c2ecf20Sopenharmony_ci	if (!desc)
10318c2ecf20Sopenharmony_ci		return -ENOMEM;
10328c2ecf20Sopenharmony_ci
10338c2ecf20Sopenharmony_ci	priv->tx_ring.desc = desc;
10348c2ecf20Sopenharmony_ci	priv->tx_ring.phys_addr = phys_addr;
10358c2ecf20Sopenharmony_ci
10368c2ecf20Sopenharmony_ci	return 0;
10378c2ecf20Sopenharmony_ci}
10388c2ecf20Sopenharmony_ci
10398c2ecf20Sopenharmony_cistatic void hix5hd2_destroy_sg_desc_queue(struct hix5hd2_priv *priv)
10408c2ecf20Sopenharmony_ci{
10418c2ecf20Sopenharmony_ci	if (priv->tx_ring.desc) {
10428c2ecf20Sopenharmony_ci		dma_free_coherent(priv->dev,
10438c2ecf20Sopenharmony_ci				  TX_DESC_NUM * sizeof(struct sg_desc),
10448c2ecf20Sopenharmony_ci				  priv->tx_ring.desc, priv->tx_ring.phys_addr);
10458c2ecf20Sopenharmony_ci		priv->tx_ring.desc = NULL;
10468c2ecf20Sopenharmony_ci	}
10478c2ecf20Sopenharmony_ci}
10488c2ecf20Sopenharmony_ci
10498c2ecf20Sopenharmony_cistatic inline void hix5hd2_mac_core_reset(struct hix5hd2_priv *priv)
10508c2ecf20Sopenharmony_ci{
10518c2ecf20Sopenharmony_ci	if (!priv->mac_core_rst)
10528c2ecf20Sopenharmony_ci		return;
10538c2ecf20Sopenharmony_ci
10548c2ecf20Sopenharmony_ci	reset_control_assert(priv->mac_core_rst);
10558c2ecf20Sopenharmony_ci	reset_control_deassert(priv->mac_core_rst);
10568c2ecf20Sopenharmony_ci}
10578c2ecf20Sopenharmony_ci
10588c2ecf20Sopenharmony_cistatic void hix5hd2_sleep_us(u32 time_us)
10598c2ecf20Sopenharmony_ci{
10608c2ecf20Sopenharmony_ci	u32 time_ms;
10618c2ecf20Sopenharmony_ci
10628c2ecf20Sopenharmony_ci	if (!time_us)
10638c2ecf20Sopenharmony_ci		return;
10648c2ecf20Sopenharmony_ci
10658c2ecf20Sopenharmony_ci	time_ms = DIV_ROUND_UP(time_us, 1000);
10668c2ecf20Sopenharmony_ci	if (time_ms < 20)
10678c2ecf20Sopenharmony_ci		usleep_range(time_us, time_us + 500);
10688c2ecf20Sopenharmony_ci	else
10698c2ecf20Sopenharmony_ci		msleep(time_ms);
10708c2ecf20Sopenharmony_ci}
10718c2ecf20Sopenharmony_ci
10728c2ecf20Sopenharmony_cistatic void hix5hd2_phy_reset(struct hix5hd2_priv *priv)
10738c2ecf20Sopenharmony_ci{
10748c2ecf20Sopenharmony_ci	/* To make sure PHY hardware reset success,
10758c2ecf20Sopenharmony_ci	 * we must keep PHY in deassert state first and
10768c2ecf20Sopenharmony_ci	 * then complete the hardware reset operation
10778c2ecf20Sopenharmony_ci	 */
10788c2ecf20Sopenharmony_ci	reset_control_deassert(priv->phy_rst);
10798c2ecf20Sopenharmony_ci	hix5hd2_sleep_us(priv->phy_reset_delays[PRE_DELAY]);
10808c2ecf20Sopenharmony_ci
10818c2ecf20Sopenharmony_ci	reset_control_assert(priv->phy_rst);
10828c2ecf20Sopenharmony_ci	/* delay some time to ensure reset ok,
10838c2ecf20Sopenharmony_ci	 * this depends on PHY hardware feature
10848c2ecf20Sopenharmony_ci	 */
10858c2ecf20Sopenharmony_ci	hix5hd2_sleep_us(priv->phy_reset_delays[PULSE]);
10868c2ecf20Sopenharmony_ci	reset_control_deassert(priv->phy_rst);
10878c2ecf20Sopenharmony_ci	/* delay some time to ensure later MDIO access */
10888c2ecf20Sopenharmony_ci	hix5hd2_sleep_us(priv->phy_reset_delays[POST_DELAY]);
10898c2ecf20Sopenharmony_ci}
10908c2ecf20Sopenharmony_ci
10918c2ecf20Sopenharmony_cistatic const struct of_device_id hix5hd2_of_match[];
10928c2ecf20Sopenharmony_ci
10938c2ecf20Sopenharmony_cistatic int hix5hd2_dev_probe(struct platform_device *pdev)
10948c2ecf20Sopenharmony_ci{
10958c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
10968c2ecf20Sopenharmony_ci	struct device_node *node = dev->of_node;
10978c2ecf20Sopenharmony_ci	const struct of_device_id *of_id = NULL;
10988c2ecf20Sopenharmony_ci	struct net_device *ndev;
10998c2ecf20Sopenharmony_ci	struct hix5hd2_priv *priv;
11008c2ecf20Sopenharmony_ci	struct mii_bus *bus;
11018c2ecf20Sopenharmony_ci	const char *mac_addr;
11028c2ecf20Sopenharmony_ci	int ret;
11038c2ecf20Sopenharmony_ci
11048c2ecf20Sopenharmony_ci	ndev = alloc_etherdev(sizeof(struct hix5hd2_priv));
11058c2ecf20Sopenharmony_ci	if (!ndev)
11068c2ecf20Sopenharmony_ci		return -ENOMEM;
11078c2ecf20Sopenharmony_ci
11088c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, ndev);
11098c2ecf20Sopenharmony_ci
11108c2ecf20Sopenharmony_ci	priv = netdev_priv(ndev);
11118c2ecf20Sopenharmony_ci	priv->dev = dev;
11128c2ecf20Sopenharmony_ci	priv->netdev = ndev;
11138c2ecf20Sopenharmony_ci
11148c2ecf20Sopenharmony_ci	of_id = of_match_device(hix5hd2_of_match, dev);
11158c2ecf20Sopenharmony_ci	if (!of_id) {
11168c2ecf20Sopenharmony_ci		ret = -EINVAL;
11178c2ecf20Sopenharmony_ci		goto out_free_netdev;
11188c2ecf20Sopenharmony_ci	}
11198c2ecf20Sopenharmony_ci	priv->hw_cap = (unsigned long)of_id->data;
11208c2ecf20Sopenharmony_ci
11218c2ecf20Sopenharmony_ci	priv->base = devm_platform_ioremap_resource(pdev, 0);
11228c2ecf20Sopenharmony_ci	if (IS_ERR(priv->base)) {
11238c2ecf20Sopenharmony_ci		ret = PTR_ERR(priv->base);
11248c2ecf20Sopenharmony_ci		goto out_free_netdev;
11258c2ecf20Sopenharmony_ci	}
11268c2ecf20Sopenharmony_ci
11278c2ecf20Sopenharmony_ci	priv->ctrl_base = devm_platform_ioremap_resource(pdev, 1);
11288c2ecf20Sopenharmony_ci	if (IS_ERR(priv->ctrl_base)) {
11298c2ecf20Sopenharmony_ci		ret = PTR_ERR(priv->ctrl_base);
11308c2ecf20Sopenharmony_ci		goto out_free_netdev;
11318c2ecf20Sopenharmony_ci	}
11328c2ecf20Sopenharmony_ci
11338c2ecf20Sopenharmony_ci	priv->mac_core_clk = devm_clk_get(&pdev->dev, "mac_core");
11348c2ecf20Sopenharmony_ci	if (IS_ERR(priv->mac_core_clk)) {
11358c2ecf20Sopenharmony_ci		netdev_err(ndev, "failed to get mac core clk\n");
11368c2ecf20Sopenharmony_ci		ret = -ENODEV;
11378c2ecf20Sopenharmony_ci		goto out_free_netdev;
11388c2ecf20Sopenharmony_ci	}
11398c2ecf20Sopenharmony_ci
11408c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(priv->mac_core_clk);
11418c2ecf20Sopenharmony_ci	if (ret < 0) {
11428c2ecf20Sopenharmony_ci		netdev_err(ndev, "failed to enable mac core clk %d\n", ret);
11438c2ecf20Sopenharmony_ci		goto out_free_netdev;
11448c2ecf20Sopenharmony_ci	}
11458c2ecf20Sopenharmony_ci
11468c2ecf20Sopenharmony_ci	priv->mac_ifc_clk = devm_clk_get(&pdev->dev, "mac_ifc");
11478c2ecf20Sopenharmony_ci	if (IS_ERR(priv->mac_ifc_clk))
11488c2ecf20Sopenharmony_ci		priv->mac_ifc_clk = NULL;
11498c2ecf20Sopenharmony_ci
11508c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(priv->mac_ifc_clk);
11518c2ecf20Sopenharmony_ci	if (ret < 0) {
11528c2ecf20Sopenharmony_ci		netdev_err(ndev, "failed to enable mac ifc clk %d\n", ret);
11538c2ecf20Sopenharmony_ci		goto out_disable_mac_core_clk;
11548c2ecf20Sopenharmony_ci	}
11558c2ecf20Sopenharmony_ci
11568c2ecf20Sopenharmony_ci	priv->mac_core_rst = devm_reset_control_get(dev, "mac_core");
11578c2ecf20Sopenharmony_ci	if (IS_ERR(priv->mac_core_rst))
11588c2ecf20Sopenharmony_ci		priv->mac_core_rst = NULL;
11598c2ecf20Sopenharmony_ci	hix5hd2_mac_core_reset(priv);
11608c2ecf20Sopenharmony_ci
11618c2ecf20Sopenharmony_ci	priv->mac_ifc_rst = devm_reset_control_get(dev, "mac_ifc");
11628c2ecf20Sopenharmony_ci	if (IS_ERR(priv->mac_ifc_rst))
11638c2ecf20Sopenharmony_ci		priv->mac_ifc_rst = NULL;
11648c2ecf20Sopenharmony_ci
11658c2ecf20Sopenharmony_ci	priv->phy_rst = devm_reset_control_get(dev, "phy");
11668c2ecf20Sopenharmony_ci	if (IS_ERR(priv->phy_rst)) {
11678c2ecf20Sopenharmony_ci		priv->phy_rst = NULL;
11688c2ecf20Sopenharmony_ci	} else {
11698c2ecf20Sopenharmony_ci		ret = of_property_read_u32_array(node,
11708c2ecf20Sopenharmony_ci						 PHY_RESET_DELAYS_PROPERTY,
11718c2ecf20Sopenharmony_ci						 priv->phy_reset_delays,
11728c2ecf20Sopenharmony_ci						 DELAYS_NUM);
11738c2ecf20Sopenharmony_ci		if (ret)
11748c2ecf20Sopenharmony_ci			goto out_disable_clk;
11758c2ecf20Sopenharmony_ci		hix5hd2_phy_reset(priv);
11768c2ecf20Sopenharmony_ci	}
11778c2ecf20Sopenharmony_ci
11788c2ecf20Sopenharmony_ci	bus = mdiobus_alloc();
11798c2ecf20Sopenharmony_ci	if (bus == NULL) {
11808c2ecf20Sopenharmony_ci		ret = -ENOMEM;
11818c2ecf20Sopenharmony_ci		goto out_disable_clk;
11828c2ecf20Sopenharmony_ci	}
11838c2ecf20Sopenharmony_ci
11848c2ecf20Sopenharmony_ci	bus->priv = priv;
11858c2ecf20Sopenharmony_ci	bus->name = "hix5hd2_mii_bus";
11868c2ecf20Sopenharmony_ci	bus->read = hix5hd2_mdio_read;
11878c2ecf20Sopenharmony_ci	bus->write = hix5hd2_mdio_write;
11888c2ecf20Sopenharmony_ci	bus->parent = &pdev->dev;
11898c2ecf20Sopenharmony_ci	snprintf(bus->id, MII_BUS_ID_SIZE, "%s-mii", dev_name(&pdev->dev));
11908c2ecf20Sopenharmony_ci	priv->bus = bus;
11918c2ecf20Sopenharmony_ci
11928c2ecf20Sopenharmony_ci	ret = of_mdiobus_register(bus, node);
11938c2ecf20Sopenharmony_ci	if (ret)
11948c2ecf20Sopenharmony_ci		goto err_free_mdio;
11958c2ecf20Sopenharmony_ci
11968c2ecf20Sopenharmony_ci	ret = of_get_phy_mode(node, &priv->phy_mode);
11978c2ecf20Sopenharmony_ci	if (ret) {
11988c2ecf20Sopenharmony_ci		netdev_err(ndev, "not find phy-mode\n");
11998c2ecf20Sopenharmony_ci		goto err_mdiobus;
12008c2ecf20Sopenharmony_ci	}
12018c2ecf20Sopenharmony_ci
12028c2ecf20Sopenharmony_ci	priv->phy_node = of_parse_phandle(node, "phy-handle", 0);
12038c2ecf20Sopenharmony_ci	if (!priv->phy_node) {
12048c2ecf20Sopenharmony_ci		netdev_err(ndev, "not find phy-handle\n");
12058c2ecf20Sopenharmony_ci		ret = -EINVAL;
12068c2ecf20Sopenharmony_ci		goto err_mdiobus;
12078c2ecf20Sopenharmony_ci	}
12088c2ecf20Sopenharmony_ci
12098c2ecf20Sopenharmony_ci	ndev->irq = platform_get_irq(pdev, 0);
12108c2ecf20Sopenharmony_ci	if (ndev->irq <= 0) {
12118c2ecf20Sopenharmony_ci		netdev_err(ndev, "No irq resource\n");
12128c2ecf20Sopenharmony_ci		ret = -EINVAL;
12138c2ecf20Sopenharmony_ci		goto out_phy_node;
12148c2ecf20Sopenharmony_ci	}
12158c2ecf20Sopenharmony_ci
12168c2ecf20Sopenharmony_ci	ret = devm_request_irq(dev, ndev->irq, hix5hd2_interrupt,
12178c2ecf20Sopenharmony_ci			       0, pdev->name, ndev);
12188c2ecf20Sopenharmony_ci	if (ret) {
12198c2ecf20Sopenharmony_ci		netdev_err(ndev, "devm_request_irq failed\n");
12208c2ecf20Sopenharmony_ci		goto out_phy_node;
12218c2ecf20Sopenharmony_ci	}
12228c2ecf20Sopenharmony_ci
12238c2ecf20Sopenharmony_ci	mac_addr = of_get_mac_address(node);
12248c2ecf20Sopenharmony_ci	if (!IS_ERR(mac_addr))
12258c2ecf20Sopenharmony_ci		ether_addr_copy(ndev->dev_addr, mac_addr);
12268c2ecf20Sopenharmony_ci	if (!is_valid_ether_addr(ndev->dev_addr)) {
12278c2ecf20Sopenharmony_ci		eth_hw_addr_random(ndev);
12288c2ecf20Sopenharmony_ci		netdev_warn(ndev, "using random MAC address %pM\n",
12298c2ecf20Sopenharmony_ci			    ndev->dev_addr);
12308c2ecf20Sopenharmony_ci	}
12318c2ecf20Sopenharmony_ci
12328c2ecf20Sopenharmony_ci	INIT_WORK(&priv->tx_timeout_task, hix5hd2_tx_timeout_task);
12338c2ecf20Sopenharmony_ci	ndev->watchdog_timeo = 6 * HZ;
12348c2ecf20Sopenharmony_ci	ndev->priv_flags |= IFF_UNICAST_FLT;
12358c2ecf20Sopenharmony_ci	ndev->netdev_ops = &hix5hd2_netdev_ops;
12368c2ecf20Sopenharmony_ci	ndev->ethtool_ops = &hix5hd2_ethtools_ops;
12378c2ecf20Sopenharmony_ci	SET_NETDEV_DEV(ndev, dev);
12388c2ecf20Sopenharmony_ci
12398c2ecf20Sopenharmony_ci	if (HAS_CAP_TSO(priv->hw_cap))
12408c2ecf20Sopenharmony_ci		ndev->hw_features |= NETIF_F_SG;
12418c2ecf20Sopenharmony_ci
12428c2ecf20Sopenharmony_ci	ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
12438c2ecf20Sopenharmony_ci	ndev->vlan_features |= ndev->features;
12448c2ecf20Sopenharmony_ci
12458c2ecf20Sopenharmony_ci	ret = hix5hd2_init_hw_desc_queue(priv);
12468c2ecf20Sopenharmony_ci	if (ret)
12478c2ecf20Sopenharmony_ci		goto out_phy_node;
12488c2ecf20Sopenharmony_ci
12498c2ecf20Sopenharmony_ci	netif_napi_add(ndev, &priv->napi, hix5hd2_poll, NAPI_POLL_WEIGHT);
12508c2ecf20Sopenharmony_ci
12518c2ecf20Sopenharmony_ci	if (HAS_CAP_TSO(priv->hw_cap)) {
12528c2ecf20Sopenharmony_ci		ret = hix5hd2_init_sg_desc_queue(priv);
12538c2ecf20Sopenharmony_ci		if (ret)
12548c2ecf20Sopenharmony_ci			goto out_destroy_queue;
12558c2ecf20Sopenharmony_ci	}
12568c2ecf20Sopenharmony_ci
12578c2ecf20Sopenharmony_ci	ret = register_netdev(priv->netdev);
12588c2ecf20Sopenharmony_ci	if (ret) {
12598c2ecf20Sopenharmony_ci		netdev_err(ndev, "register_netdev failed!");
12608c2ecf20Sopenharmony_ci		goto out_destroy_queue;
12618c2ecf20Sopenharmony_ci	}
12628c2ecf20Sopenharmony_ci
12638c2ecf20Sopenharmony_ci	clk_disable_unprepare(priv->mac_ifc_clk);
12648c2ecf20Sopenharmony_ci	clk_disable_unprepare(priv->mac_core_clk);
12658c2ecf20Sopenharmony_ci
12668c2ecf20Sopenharmony_ci	return ret;
12678c2ecf20Sopenharmony_ci
12688c2ecf20Sopenharmony_ciout_destroy_queue:
12698c2ecf20Sopenharmony_ci	if (HAS_CAP_TSO(priv->hw_cap))
12708c2ecf20Sopenharmony_ci		hix5hd2_destroy_sg_desc_queue(priv);
12718c2ecf20Sopenharmony_ci	netif_napi_del(&priv->napi);
12728c2ecf20Sopenharmony_ci	hix5hd2_destroy_hw_desc_queue(priv);
12738c2ecf20Sopenharmony_ciout_phy_node:
12748c2ecf20Sopenharmony_ci	of_node_put(priv->phy_node);
12758c2ecf20Sopenharmony_cierr_mdiobus:
12768c2ecf20Sopenharmony_ci	mdiobus_unregister(bus);
12778c2ecf20Sopenharmony_cierr_free_mdio:
12788c2ecf20Sopenharmony_ci	mdiobus_free(bus);
12798c2ecf20Sopenharmony_ciout_disable_clk:
12808c2ecf20Sopenharmony_ci	clk_disable_unprepare(priv->mac_ifc_clk);
12818c2ecf20Sopenharmony_ciout_disable_mac_core_clk:
12828c2ecf20Sopenharmony_ci	clk_disable_unprepare(priv->mac_core_clk);
12838c2ecf20Sopenharmony_ciout_free_netdev:
12848c2ecf20Sopenharmony_ci	free_netdev(ndev);
12858c2ecf20Sopenharmony_ci
12868c2ecf20Sopenharmony_ci	return ret;
12878c2ecf20Sopenharmony_ci}
12888c2ecf20Sopenharmony_ci
12898c2ecf20Sopenharmony_cistatic int hix5hd2_dev_remove(struct platform_device *pdev)
12908c2ecf20Sopenharmony_ci{
12918c2ecf20Sopenharmony_ci	struct net_device *ndev = platform_get_drvdata(pdev);
12928c2ecf20Sopenharmony_ci	struct hix5hd2_priv *priv = netdev_priv(ndev);
12938c2ecf20Sopenharmony_ci
12948c2ecf20Sopenharmony_ci	netif_napi_del(&priv->napi);
12958c2ecf20Sopenharmony_ci	unregister_netdev(ndev);
12968c2ecf20Sopenharmony_ci	mdiobus_unregister(priv->bus);
12978c2ecf20Sopenharmony_ci	mdiobus_free(priv->bus);
12988c2ecf20Sopenharmony_ci
12998c2ecf20Sopenharmony_ci	if (HAS_CAP_TSO(priv->hw_cap))
13008c2ecf20Sopenharmony_ci		hix5hd2_destroy_sg_desc_queue(priv);
13018c2ecf20Sopenharmony_ci	hix5hd2_destroy_hw_desc_queue(priv);
13028c2ecf20Sopenharmony_ci	of_node_put(priv->phy_node);
13038c2ecf20Sopenharmony_ci	cancel_work_sync(&priv->tx_timeout_task);
13048c2ecf20Sopenharmony_ci	free_netdev(ndev);
13058c2ecf20Sopenharmony_ci
13068c2ecf20Sopenharmony_ci	return 0;
13078c2ecf20Sopenharmony_ci}
13088c2ecf20Sopenharmony_ci
13098c2ecf20Sopenharmony_cistatic const struct of_device_id hix5hd2_of_match[] = {
13108c2ecf20Sopenharmony_ci	{ .compatible = "hisilicon,hisi-gmac-v1", .data = (void *)GEMAC_V1 },
13118c2ecf20Sopenharmony_ci	{ .compatible = "hisilicon,hisi-gmac-v2", .data = (void *)GEMAC_V2 },
13128c2ecf20Sopenharmony_ci	{ .compatible = "hisilicon,hix5hd2-gmac", .data = (void *)GEMAC_V1 },
13138c2ecf20Sopenharmony_ci	{ .compatible = "hisilicon,hi3798cv200-gmac", .data = (void *)GEMAC_V2 },
13148c2ecf20Sopenharmony_ci	{ .compatible = "hisilicon,hi3516a-gmac", .data = (void *)GEMAC_V2 },
13158c2ecf20Sopenharmony_ci	{},
13168c2ecf20Sopenharmony_ci};
13178c2ecf20Sopenharmony_ci
13188c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, hix5hd2_of_match);
13198c2ecf20Sopenharmony_ci
13208c2ecf20Sopenharmony_cistatic struct platform_driver hix5hd2_dev_driver = {
13218c2ecf20Sopenharmony_ci	.driver = {
13228c2ecf20Sopenharmony_ci		.name = "hisi-gmac",
13238c2ecf20Sopenharmony_ci		.of_match_table = hix5hd2_of_match,
13248c2ecf20Sopenharmony_ci	},
13258c2ecf20Sopenharmony_ci	.probe = hix5hd2_dev_probe,
13268c2ecf20Sopenharmony_ci	.remove = hix5hd2_dev_remove,
13278c2ecf20Sopenharmony_ci};
13288c2ecf20Sopenharmony_ci
13298c2ecf20Sopenharmony_cimodule_platform_driver(hix5hd2_dev_driver);
13308c2ecf20Sopenharmony_ci
13318c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("HISILICON Gigabit Ethernet MAC driver");
13328c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
13338c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:hisi-gmac");
1334