18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci/* Copyright (c) 2014 Linaro Ltd.
48c2ecf20Sopenharmony_ci * Copyright (c) 2014 Hisilicon Limited.
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/module.h>
88c2ecf20Sopenharmony_ci#include <linux/etherdevice.h>
98c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
108c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
118c2ecf20Sopenharmony_ci#include <linux/ktime.h>
128c2ecf20Sopenharmony_ci#include <linux/of_address.h>
138c2ecf20Sopenharmony_ci#include <linux/phy.h>
148c2ecf20Sopenharmony_ci#include <linux/of_mdio.h>
158c2ecf20Sopenharmony_ci#include <linux/of_net.h>
168c2ecf20Sopenharmony_ci#include <linux/mfd/syscon.h>
178c2ecf20Sopenharmony_ci#include <linux/regmap.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#define SC_PPE_RESET_DREQ		0x026C
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#define PPE_CFG_RX_ADDR			0x100
228c2ecf20Sopenharmony_ci#define PPE_CFG_POOL_GRP		0x300
238c2ecf20Sopenharmony_ci#define PPE_CFG_RX_BUF_SIZE		0x400
248c2ecf20Sopenharmony_ci#define PPE_CFG_RX_FIFO_SIZE		0x500
258c2ecf20Sopenharmony_ci#define PPE_CURR_BUF_CNT		0xa200
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#define GE_DUPLEX_TYPE			0x08
288c2ecf20Sopenharmony_ci#define GE_MAX_FRM_SIZE_REG		0x3c
298c2ecf20Sopenharmony_ci#define GE_PORT_MODE			0x40
308c2ecf20Sopenharmony_ci#define GE_PORT_EN			0x44
318c2ecf20Sopenharmony_ci#define GE_SHORT_RUNTS_THR_REG		0x50
328c2ecf20Sopenharmony_ci#define GE_TX_LOCAL_PAGE_REG		0x5c
338c2ecf20Sopenharmony_ci#define GE_TRANSMIT_CONTROL_REG		0x60
348c2ecf20Sopenharmony_ci#define GE_CF_CRC_STRIP_REG		0x1b0
358c2ecf20Sopenharmony_ci#define GE_MODE_CHANGE_REG		0x1b4
368c2ecf20Sopenharmony_ci#define GE_RECV_CONTROL_REG		0x1e0
378c2ecf20Sopenharmony_ci#define GE_STATION_MAC_ADDRESS		0x210
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#define PPE_CFG_BUS_CTRL_REG		0x424
408c2ecf20Sopenharmony_ci#define PPE_CFG_RX_CTRL_REG		0x428
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#if defined(CONFIG_HI13X1_GMAC)
438c2ecf20Sopenharmony_ci#define PPE_CFG_CPU_ADD_ADDR		0x6D0
448c2ecf20Sopenharmony_ci#define PPE_CFG_MAX_FRAME_LEN_REG	0x500
458c2ecf20Sopenharmony_ci#define PPE_CFG_RX_PKT_MODE_REG		0x504
468c2ecf20Sopenharmony_ci#define PPE_CFG_QOS_VMID_GEN		0x520
478c2ecf20Sopenharmony_ci#define PPE_CFG_RX_PKT_INT		0x740
488c2ecf20Sopenharmony_ci#define PPE_INTEN			0x700
498c2ecf20Sopenharmony_ci#define PPE_INTSTS			0x708
508c2ecf20Sopenharmony_ci#define PPE_RINT			0x704
518c2ecf20Sopenharmony_ci#define PPE_CFG_STS_MODE		0x880
528c2ecf20Sopenharmony_ci#else
538c2ecf20Sopenharmony_ci#define PPE_CFG_CPU_ADD_ADDR		0x580
548c2ecf20Sopenharmony_ci#define PPE_CFG_MAX_FRAME_LEN_REG	0x408
558c2ecf20Sopenharmony_ci#define PPE_CFG_RX_PKT_MODE_REG		0x438
568c2ecf20Sopenharmony_ci#define PPE_CFG_QOS_VMID_GEN		0x500
578c2ecf20Sopenharmony_ci#define PPE_CFG_RX_PKT_INT		0x538
588c2ecf20Sopenharmony_ci#define PPE_INTEN			0x600
598c2ecf20Sopenharmony_ci#define PPE_INTSTS			0x608
608c2ecf20Sopenharmony_ci#define PPE_RINT			0x604
618c2ecf20Sopenharmony_ci#define PPE_CFG_STS_MODE		0x700
628c2ecf20Sopenharmony_ci#endif /* CONFIG_HI13X1_GMAC */
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci#define PPE_HIS_RX_PKT_CNT		0x804
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci#define RESET_DREQ_ALL			0xffffffff
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci/* REG_INTERRUPT */
698c2ecf20Sopenharmony_ci#define RCV_INT				BIT(10)
708c2ecf20Sopenharmony_ci#define RCV_NOBUF			BIT(8)
718c2ecf20Sopenharmony_ci#define RCV_DROP			BIT(7)
728c2ecf20Sopenharmony_ci#define TX_DROP				BIT(6)
738c2ecf20Sopenharmony_ci#define DEF_INT_ERR			(RCV_NOBUF | RCV_DROP | TX_DROP)
748c2ecf20Sopenharmony_ci#define DEF_INT_MASK			(RCV_INT | DEF_INT_ERR)
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci/* TX descriptor config */
778c2ecf20Sopenharmony_ci#define TX_FREE_MEM			BIT(0)
788c2ecf20Sopenharmony_ci#define TX_READ_ALLOC_L3		BIT(1)
798c2ecf20Sopenharmony_ci#if defined(CONFIG_HI13X1_GMAC)
808c2ecf20Sopenharmony_ci#define TX_CLEAR_WB			BIT(7)
818c2ecf20Sopenharmony_ci#define TX_RELEASE_TO_PPE		BIT(4)
828c2ecf20Sopenharmony_ci#define TX_FINISH_CACHE_INV		BIT(6)
838c2ecf20Sopenharmony_ci#define TX_POOL_SHIFT			16
848c2ecf20Sopenharmony_ci#else
858c2ecf20Sopenharmony_ci#define TX_CLEAR_WB			BIT(4)
868c2ecf20Sopenharmony_ci#define TX_FINISH_CACHE_INV		BIT(2)
878c2ecf20Sopenharmony_ci#endif
888c2ecf20Sopenharmony_ci#define TX_L3_CHECKSUM			BIT(5)
898c2ecf20Sopenharmony_ci#define TX_LOOP_BACK			BIT(11)
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci/* RX error */
928c2ecf20Sopenharmony_ci#define RX_PKT_DROP			BIT(0)
938c2ecf20Sopenharmony_ci#define RX_L2_ERR			BIT(1)
948c2ecf20Sopenharmony_ci#define RX_PKT_ERR			(RX_PKT_DROP | RX_L2_ERR)
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci#define SGMII_SPEED_1000		0x08
978c2ecf20Sopenharmony_ci#define SGMII_SPEED_100			0x07
988c2ecf20Sopenharmony_ci#define SGMII_SPEED_10			0x06
998c2ecf20Sopenharmony_ci#define MII_SPEED_100			0x01
1008c2ecf20Sopenharmony_ci#define MII_SPEED_10			0x00
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci#define GE_DUPLEX_FULL			BIT(0)
1038c2ecf20Sopenharmony_ci#define GE_DUPLEX_HALF			0x00
1048c2ecf20Sopenharmony_ci#define GE_MODE_CHANGE_EN		BIT(0)
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci#define GE_TX_AUTO_NEG			BIT(5)
1078c2ecf20Sopenharmony_ci#define GE_TX_ADD_CRC			BIT(6)
1088c2ecf20Sopenharmony_ci#define GE_TX_SHORT_PAD_THROUGH		BIT(7)
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci#define GE_RX_STRIP_CRC			BIT(0)
1118c2ecf20Sopenharmony_ci#define GE_RX_STRIP_PAD			BIT(3)
1128c2ecf20Sopenharmony_ci#define GE_RX_PAD_EN			BIT(4)
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci#define GE_AUTO_NEG_CTL			BIT(0)
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci#define GE_RX_INT_THRESHOLD		BIT(6)
1178c2ecf20Sopenharmony_ci#define GE_RX_TIMEOUT			0x04
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci#define GE_RX_PORT_EN			BIT(1)
1208c2ecf20Sopenharmony_ci#define GE_TX_PORT_EN			BIT(2)
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci#define PPE_CFG_RX_PKT_ALIGN		BIT(18)
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci#if defined(CONFIG_HI13X1_GMAC)
1258c2ecf20Sopenharmony_ci#define PPE_CFG_QOS_VMID_GRP_SHIFT	4
1268c2ecf20Sopenharmony_ci#define PPE_CFG_RX_CTRL_ALIGN_SHIFT	7
1278c2ecf20Sopenharmony_ci#define PPE_CFG_STS_RX_PKT_CNT_RC	BIT(0)
1288c2ecf20Sopenharmony_ci#define PPE_CFG_QOS_VMID_MODE		BIT(15)
1298c2ecf20Sopenharmony_ci#define PPE_CFG_BUS_LOCAL_REL		(BIT(9) | BIT(15) | BIT(19) | BIT(23))
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci/* buf unit size is cache_line_size, which is 64, so the shift is 6 */
1328c2ecf20Sopenharmony_ci#define PPE_BUF_SIZE_SHIFT		6
1338c2ecf20Sopenharmony_ci#define PPE_TX_BUF_HOLD			BIT(31)
1348c2ecf20Sopenharmony_ci#define SOC_CACHE_LINE_MASK		0x3F
1358c2ecf20Sopenharmony_ci#else
1368c2ecf20Sopenharmony_ci#define PPE_CFG_QOS_VMID_GRP_SHIFT	8
1378c2ecf20Sopenharmony_ci#define PPE_CFG_RX_CTRL_ALIGN_SHIFT	11
1388c2ecf20Sopenharmony_ci#define PPE_CFG_STS_RX_PKT_CNT_RC	BIT(12)
1398c2ecf20Sopenharmony_ci#define PPE_CFG_QOS_VMID_MODE		BIT(14)
1408c2ecf20Sopenharmony_ci#define PPE_CFG_BUS_LOCAL_REL		BIT(14)
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci/* buf unit size is 1, so the shift is 6 */
1438c2ecf20Sopenharmony_ci#define PPE_BUF_SIZE_SHIFT		0
1448c2ecf20Sopenharmony_ci#define PPE_TX_BUF_HOLD			0
1458c2ecf20Sopenharmony_ci#endif /* CONFIG_HI13X1_GMAC */
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci#define PPE_CFG_RX_FIFO_FSFU		BIT(11)
1488c2ecf20Sopenharmony_ci#define PPE_CFG_RX_DEPTH_SHIFT		16
1498c2ecf20Sopenharmony_ci#define PPE_CFG_RX_START_SHIFT		0
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci#define PPE_CFG_BUS_BIG_ENDIEN		BIT(0)
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci#define RX_DESC_NUM			128
1548c2ecf20Sopenharmony_ci#define TX_DESC_NUM			256
1558c2ecf20Sopenharmony_ci#define TX_NEXT(N)			(((N) + 1) & (TX_DESC_NUM-1))
1568c2ecf20Sopenharmony_ci#define RX_NEXT(N)			(((N) + 1) & (RX_DESC_NUM-1))
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci#define GMAC_PPE_RX_PKT_MAX_LEN		379
1598c2ecf20Sopenharmony_ci#define GMAC_MAX_PKT_LEN		1516
1608c2ecf20Sopenharmony_ci#define GMAC_MIN_PKT_LEN		31
1618c2ecf20Sopenharmony_ci#define RX_BUF_SIZE			1600
1628c2ecf20Sopenharmony_ci#define RESET_TIMEOUT			1000
1638c2ecf20Sopenharmony_ci#define TX_TIMEOUT			(6 * HZ)
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci#define DRV_NAME			"hip04-ether"
1668c2ecf20Sopenharmony_ci#define DRV_VERSION			"v1.0"
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci#define HIP04_MAX_TX_COALESCE_USECS	200
1698c2ecf20Sopenharmony_ci#define HIP04_MIN_TX_COALESCE_USECS	100
1708c2ecf20Sopenharmony_ci#define HIP04_MAX_TX_COALESCE_FRAMES	200
1718c2ecf20Sopenharmony_ci#define HIP04_MIN_TX_COALESCE_FRAMES	100
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_cistruct tx_desc {
1748c2ecf20Sopenharmony_ci#if defined(CONFIG_HI13X1_GMAC)
1758c2ecf20Sopenharmony_ci	u32 reserved1[2];
1768c2ecf20Sopenharmony_ci	u32 send_addr;
1778c2ecf20Sopenharmony_ci	u16 send_size;
1788c2ecf20Sopenharmony_ci	u16 data_offset;
1798c2ecf20Sopenharmony_ci	u32 reserved2[7];
1808c2ecf20Sopenharmony_ci	u32 cfg;
1818c2ecf20Sopenharmony_ci	u32 wb_addr;
1828c2ecf20Sopenharmony_ci	u32 reserved3[3];
1838c2ecf20Sopenharmony_ci#else
1848c2ecf20Sopenharmony_ci	u32 send_addr;
1858c2ecf20Sopenharmony_ci	u32 send_size;
1868c2ecf20Sopenharmony_ci	u32 next_addr;
1878c2ecf20Sopenharmony_ci	u32 cfg;
1888c2ecf20Sopenharmony_ci	u32 wb_addr;
1898c2ecf20Sopenharmony_ci#endif
1908c2ecf20Sopenharmony_ci} __aligned(64);
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_cistruct rx_desc {
1938c2ecf20Sopenharmony_ci#if defined(CONFIG_HI13X1_GMAC)
1948c2ecf20Sopenharmony_ci	u32 reserved1[3];
1958c2ecf20Sopenharmony_ci	u16 pkt_len;
1968c2ecf20Sopenharmony_ci	u16 reserved_16;
1978c2ecf20Sopenharmony_ci	u32 reserved2[6];
1988c2ecf20Sopenharmony_ci	u32 pkt_err;
1998c2ecf20Sopenharmony_ci	u32 reserved3[5];
2008c2ecf20Sopenharmony_ci#else
2018c2ecf20Sopenharmony_ci	u16 reserved_16;
2028c2ecf20Sopenharmony_ci	u16 pkt_len;
2038c2ecf20Sopenharmony_ci	u32 reserve1[3];
2048c2ecf20Sopenharmony_ci	u32 pkt_err;
2058c2ecf20Sopenharmony_ci	u32 reserve2[4];
2068c2ecf20Sopenharmony_ci#endif
2078c2ecf20Sopenharmony_ci};
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_cistruct hip04_priv {
2108c2ecf20Sopenharmony_ci	void __iomem *base;
2118c2ecf20Sopenharmony_ci#if defined(CONFIG_HI13X1_GMAC)
2128c2ecf20Sopenharmony_ci	void __iomem *sysctrl_base;
2138c2ecf20Sopenharmony_ci#endif
2148c2ecf20Sopenharmony_ci	phy_interface_t phy_mode;
2158c2ecf20Sopenharmony_ci	int chan;
2168c2ecf20Sopenharmony_ci	unsigned int port;
2178c2ecf20Sopenharmony_ci	unsigned int group;
2188c2ecf20Sopenharmony_ci	unsigned int speed;
2198c2ecf20Sopenharmony_ci	unsigned int duplex;
2208c2ecf20Sopenharmony_ci	unsigned int reg_inten;
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	struct napi_struct napi;
2238c2ecf20Sopenharmony_ci	struct device *dev;
2248c2ecf20Sopenharmony_ci	struct net_device *ndev;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	struct tx_desc *tx_desc;
2278c2ecf20Sopenharmony_ci	dma_addr_t tx_desc_dma;
2288c2ecf20Sopenharmony_ci	struct sk_buff *tx_skb[TX_DESC_NUM];
2298c2ecf20Sopenharmony_ci	dma_addr_t tx_phys[TX_DESC_NUM];
2308c2ecf20Sopenharmony_ci	unsigned int tx_head;
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	int tx_coalesce_frames;
2338c2ecf20Sopenharmony_ci	int tx_coalesce_usecs;
2348c2ecf20Sopenharmony_ci	struct hrtimer tx_coalesce_timer;
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	unsigned char *rx_buf[RX_DESC_NUM];
2378c2ecf20Sopenharmony_ci	dma_addr_t rx_phys[RX_DESC_NUM];
2388c2ecf20Sopenharmony_ci	unsigned int rx_head;
2398c2ecf20Sopenharmony_ci	unsigned int rx_buf_size;
2408c2ecf20Sopenharmony_ci	unsigned int rx_cnt_remaining;
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	struct device_node *phy_node;
2438c2ecf20Sopenharmony_ci	struct phy_device *phy;
2448c2ecf20Sopenharmony_ci	struct regmap *map;
2458c2ecf20Sopenharmony_ci	struct work_struct tx_timeout_task;
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	/* written only by tx cleanup */
2488c2ecf20Sopenharmony_ci	unsigned int tx_tail ____cacheline_aligned_in_smp;
2498c2ecf20Sopenharmony_ci};
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_cistatic inline unsigned int tx_count(unsigned int head, unsigned int tail)
2528c2ecf20Sopenharmony_ci{
2538c2ecf20Sopenharmony_ci	return (head - tail) % TX_DESC_NUM;
2548c2ecf20Sopenharmony_ci}
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_cistatic void hip04_config_port(struct net_device *ndev, u32 speed, u32 duplex)
2578c2ecf20Sopenharmony_ci{
2588c2ecf20Sopenharmony_ci	struct hip04_priv *priv = netdev_priv(ndev);
2598c2ecf20Sopenharmony_ci	u32 val;
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	priv->speed = speed;
2628c2ecf20Sopenharmony_ci	priv->duplex = duplex;
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	switch (priv->phy_mode) {
2658c2ecf20Sopenharmony_ci	case PHY_INTERFACE_MODE_SGMII:
2668c2ecf20Sopenharmony_ci		if (speed == SPEED_1000)
2678c2ecf20Sopenharmony_ci			val = SGMII_SPEED_1000;
2688c2ecf20Sopenharmony_ci		else if (speed == SPEED_100)
2698c2ecf20Sopenharmony_ci			val = SGMII_SPEED_100;
2708c2ecf20Sopenharmony_ci		else
2718c2ecf20Sopenharmony_ci			val = SGMII_SPEED_10;
2728c2ecf20Sopenharmony_ci		break;
2738c2ecf20Sopenharmony_ci	case PHY_INTERFACE_MODE_MII:
2748c2ecf20Sopenharmony_ci		if (speed == SPEED_100)
2758c2ecf20Sopenharmony_ci			val = MII_SPEED_100;
2768c2ecf20Sopenharmony_ci		else
2778c2ecf20Sopenharmony_ci			val = MII_SPEED_10;
2788c2ecf20Sopenharmony_ci		break;
2798c2ecf20Sopenharmony_ci	default:
2808c2ecf20Sopenharmony_ci		netdev_warn(ndev, "not supported mode\n");
2818c2ecf20Sopenharmony_ci		val = MII_SPEED_10;
2828c2ecf20Sopenharmony_ci		break;
2838c2ecf20Sopenharmony_ci	}
2848c2ecf20Sopenharmony_ci	writel_relaxed(val, priv->base + GE_PORT_MODE);
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	val = duplex ? GE_DUPLEX_FULL : GE_DUPLEX_HALF;
2878c2ecf20Sopenharmony_ci	writel_relaxed(val, priv->base + GE_DUPLEX_TYPE);
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	val = GE_MODE_CHANGE_EN;
2908c2ecf20Sopenharmony_ci	writel_relaxed(val, priv->base + GE_MODE_CHANGE_REG);
2918c2ecf20Sopenharmony_ci}
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_cistatic void hip04_reset_dreq(struct hip04_priv *priv)
2948c2ecf20Sopenharmony_ci{
2958c2ecf20Sopenharmony_ci#if defined(CONFIG_HI13X1_GMAC)
2968c2ecf20Sopenharmony_ci	writel_relaxed(RESET_DREQ_ALL, priv->sysctrl_base + SC_PPE_RESET_DREQ);
2978c2ecf20Sopenharmony_ci#endif
2988c2ecf20Sopenharmony_ci}
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_cistatic void hip04_reset_ppe(struct hip04_priv *priv)
3018c2ecf20Sopenharmony_ci{
3028c2ecf20Sopenharmony_ci	u32 val, tmp, timeout = 0;
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	do {
3058c2ecf20Sopenharmony_ci		regmap_read(priv->map, priv->port * 4 + PPE_CURR_BUF_CNT, &val);
3068c2ecf20Sopenharmony_ci		regmap_read(priv->map, priv->port * 4 + PPE_CFG_RX_ADDR, &tmp);
3078c2ecf20Sopenharmony_ci		if (timeout++ > RESET_TIMEOUT)
3088c2ecf20Sopenharmony_ci			break;
3098c2ecf20Sopenharmony_ci	} while (val & 0xfff);
3108c2ecf20Sopenharmony_ci}
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_cistatic void hip04_config_fifo(struct hip04_priv *priv)
3138c2ecf20Sopenharmony_ci{
3148c2ecf20Sopenharmony_ci	u32 val;
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	val = readl_relaxed(priv->base + PPE_CFG_STS_MODE);
3178c2ecf20Sopenharmony_ci	val |= PPE_CFG_STS_RX_PKT_CNT_RC;
3188c2ecf20Sopenharmony_ci	writel_relaxed(val, priv->base + PPE_CFG_STS_MODE);
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	val = BIT(priv->group);
3218c2ecf20Sopenharmony_ci	regmap_write(priv->map, priv->port * 4 + PPE_CFG_POOL_GRP, val);
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	val = priv->group << PPE_CFG_QOS_VMID_GRP_SHIFT;
3248c2ecf20Sopenharmony_ci	val |= PPE_CFG_QOS_VMID_MODE;
3258c2ecf20Sopenharmony_ci	writel_relaxed(val, priv->base + PPE_CFG_QOS_VMID_GEN);
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	val = RX_BUF_SIZE >> PPE_BUF_SIZE_SHIFT;
3288c2ecf20Sopenharmony_ci	regmap_write(priv->map, priv->port * 4 + PPE_CFG_RX_BUF_SIZE, val);
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	val = RX_DESC_NUM << PPE_CFG_RX_DEPTH_SHIFT;
3318c2ecf20Sopenharmony_ci	val |= PPE_CFG_RX_FIFO_FSFU;
3328c2ecf20Sopenharmony_ci	val |= priv->chan << PPE_CFG_RX_START_SHIFT;
3338c2ecf20Sopenharmony_ci	regmap_write(priv->map, priv->port * 4 + PPE_CFG_RX_FIFO_SIZE, val);
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	val = NET_IP_ALIGN << PPE_CFG_RX_CTRL_ALIGN_SHIFT;
3368c2ecf20Sopenharmony_ci	writel_relaxed(val, priv->base + PPE_CFG_RX_CTRL_REG);
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	val = PPE_CFG_RX_PKT_ALIGN;
3398c2ecf20Sopenharmony_ci	writel_relaxed(val, priv->base + PPE_CFG_RX_PKT_MODE_REG);
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	val = PPE_CFG_BUS_LOCAL_REL | PPE_CFG_BUS_BIG_ENDIEN;
3428c2ecf20Sopenharmony_ci	writel_relaxed(val, priv->base + PPE_CFG_BUS_CTRL_REG);
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	val = GMAC_PPE_RX_PKT_MAX_LEN;
3458c2ecf20Sopenharmony_ci	writel_relaxed(val, priv->base + PPE_CFG_MAX_FRAME_LEN_REG);
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	val = GMAC_MAX_PKT_LEN;
3488c2ecf20Sopenharmony_ci	writel_relaxed(val, priv->base + GE_MAX_FRM_SIZE_REG);
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	val = GMAC_MIN_PKT_LEN;
3518c2ecf20Sopenharmony_ci	writel_relaxed(val, priv->base + GE_SHORT_RUNTS_THR_REG);
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	val = readl_relaxed(priv->base + GE_TRANSMIT_CONTROL_REG);
3548c2ecf20Sopenharmony_ci	val |= GE_TX_AUTO_NEG | GE_TX_ADD_CRC | GE_TX_SHORT_PAD_THROUGH;
3558c2ecf20Sopenharmony_ci	writel_relaxed(val, priv->base + GE_TRANSMIT_CONTROL_REG);
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	val = GE_RX_STRIP_CRC;
3588c2ecf20Sopenharmony_ci	writel_relaxed(val, priv->base + GE_CF_CRC_STRIP_REG);
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	val = readl_relaxed(priv->base + GE_RECV_CONTROL_REG);
3618c2ecf20Sopenharmony_ci	val |= GE_RX_STRIP_PAD | GE_RX_PAD_EN;
3628c2ecf20Sopenharmony_ci	writel_relaxed(val, priv->base + GE_RECV_CONTROL_REG);
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci#ifndef CONFIG_HI13X1_GMAC
3658c2ecf20Sopenharmony_ci	val = GE_AUTO_NEG_CTL;
3668c2ecf20Sopenharmony_ci	writel_relaxed(val, priv->base + GE_TX_LOCAL_PAGE_REG);
3678c2ecf20Sopenharmony_ci#endif
3688c2ecf20Sopenharmony_ci}
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_cistatic void hip04_mac_enable(struct net_device *ndev)
3718c2ecf20Sopenharmony_ci{
3728c2ecf20Sopenharmony_ci	struct hip04_priv *priv = netdev_priv(ndev);
3738c2ecf20Sopenharmony_ci	u32 val;
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	/* enable tx & rx */
3768c2ecf20Sopenharmony_ci	val = readl_relaxed(priv->base + GE_PORT_EN);
3778c2ecf20Sopenharmony_ci	val |= GE_RX_PORT_EN | GE_TX_PORT_EN;
3788c2ecf20Sopenharmony_ci	writel_relaxed(val, priv->base + GE_PORT_EN);
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	/* clear rx int */
3818c2ecf20Sopenharmony_ci	val = RCV_INT;
3828c2ecf20Sopenharmony_ci	writel_relaxed(val, priv->base + PPE_RINT);
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	/* config recv int */
3858c2ecf20Sopenharmony_ci	val = GE_RX_INT_THRESHOLD | GE_RX_TIMEOUT;
3868c2ecf20Sopenharmony_ci	writel_relaxed(val, priv->base + PPE_CFG_RX_PKT_INT);
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	/* enable interrupt */
3898c2ecf20Sopenharmony_ci	priv->reg_inten = DEF_INT_MASK;
3908c2ecf20Sopenharmony_ci	writel_relaxed(priv->reg_inten, priv->base + PPE_INTEN);
3918c2ecf20Sopenharmony_ci}
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_cistatic void hip04_mac_disable(struct net_device *ndev)
3948c2ecf20Sopenharmony_ci{
3958c2ecf20Sopenharmony_ci	struct hip04_priv *priv = netdev_priv(ndev);
3968c2ecf20Sopenharmony_ci	u32 val;
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	/* disable int */
3998c2ecf20Sopenharmony_ci	priv->reg_inten &= ~(DEF_INT_MASK);
4008c2ecf20Sopenharmony_ci	writel_relaxed(priv->reg_inten, priv->base + PPE_INTEN);
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	/* disable tx & rx */
4038c2ecf20Sopenharmony_ci	val = readl_relaxed(priv->base + GE_PORT_EN);
4048c2ecf20Sopenharmony_ci	val &= ~(GE_RX_PORT_EN | GE_TX_PORT_EN);
4058c2ecf20Sopenharmony_ci	writel_relaxed(val, priv->base + GE_PORT_EN);
4068c2ecf20Sopenharmony_ci}
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_cistatic void hip04_set_xmit_desc(struct hip04_priv *priv, dma_addr_t phys)
4098c2ecf20Sopenharmony_ci{
4108c2ecf20Sopenharmony_ci	u32 val;
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	val = phys >> PPE_BUF_SIZE_SHIFT | PPE_TX_BUF_HOLD;
4138c2ecf20Sopenharmony_ci	writel(val, priv->base + PPE_CFG_CPU_ADD_ADDR);
4148c2ecf20Sopenharmony_ci}
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_cistatic void hip04_set_recv_desc(struct hip04_priv *priv, dma_addr_t phys)
4178c2ecf20Sopenharmony_ci{
4188c2ecf20Sopenharmony_ci	u32 val;
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	val = phys >> PPE_BUF_SIZE_SHIFT;
4218c2ecf20Sopenharmony_ci	regmap_write(priv->map, priv->port * 4 + PPE_CFG_RX_ADDR, val);
4228c2ecf20Sopenharmony_ci}
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_cistatic u32 hip04_recv_cnt(struct hip04_priv *priv)
4258c2ecf20Sopenharmony_ci{
4268c2ecf20Sopenharmony_ci	return readl(priv->base + PPE_HIS_RX_PKT_CNT);
4278c2ecf20Sopenharmony_ci}
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_cistatic void hip04_update_mac_address(struct net_device *ndev)
4308c2ecf20Sopenharmony_ci{
4318c2ecf20Sopenharmony_ci	struct hip04_priv *priv = netdev_priv(ndev);
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	writel_relaxed(((ndev->dev_addr[0] << 8) | (ndev->dev_addr[1])),
4348c2ecf20Sopenharmony_ci		       priv->base + GE_STATION_MAC_ADDRESS);
4358c2ecf20Sopenharmony_ci	writel_relaxed(((ndev->dev_addr[2] << 24) | (ndev->dev_addr[3] << 16) |
4368c2ecf20Sopenharmony_ci			(ndev->dev_addr[4] << 8) | (ndev->dev_addr[5])),
4378c2ecf20Sopenharmony_ci		       priv->base + GE_STATION_MAC_ADDRESS + 4);
4388c2ecf20Sopenharmony_ci}
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_cistatic int hip04_set_mac_address(struct net_device *ndev, void *addr)
4418c2ecf20Sopenharmony_ci{
4428c2ecf20Sopenharmony_ci	eth_mac_addr(ndev, addr);
4438c2ecf20Sopenharmony_ci	hip04_update_mac_address(ndev);
4448c2ecf20Sopenharmony_ci	return 0;
4458c2ecf20Sopenharmony_ci}
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_cistatic int hip04_tx_reclaim(struct net_device *ndev, bool force)
4488c2ecf20Sopenharmony_ci{
4498c2ecf20Sopenharmony_ci	struct hip04_priv *priv = netdev_priv(ndev);
4508c2ecf20Sopenharmony_ci	unsigned tx_tail = priv->tx_tail;
4518c2ecf20Sopenharmony_ci	struct tx_desc *desc;
4528c2ecf20Sopenharmony_ci	unsigned int bytes_compl = 0, pkts_compl = 0;
4538c2ecf20Sopenharmony_ci	unsigned int count;
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci	smp_rmb();
4568c2ecf20Sopenharmony_ci	count = tx_count(READ_ONCE(priv->tx_head), tx_tail);
4578c2ecf20Sopenharmony_ci	if (count == 0)
4588c2ecf20Sopenharmony_ci		goto out;
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	while (count) {
4618c2ecf20Sopenharmony_ci		desc = &priv->tx_desc[tx_tail];
4628c2ecf20Sopenharmony_ci		if (desc->send_addr != 0) {
4638c2ecf20Sopenharmony_ci			if (force)
4648c2ecf20Sopenharmony_ci				desc->send_addr = 0;
4658c2ecf20Sopenharmony_ci			else
4668c2ecf20Sopenharmony_ci				break;
4678c2ecf20Sopenharmony_ci		}
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci		if (priv->tx_phys[tx_tail]) {
4708c2ecf20Sopenharmony_ci			dma_unmap_single(priv->dev, priv->tx_phys[tx_tail],
4718c2ecf20Sopenharmony_ci					 priv->tx_skb[tx_tail]->len,
4728c2ecf20Sopenharmony_ci					 DMA_TO_DEVICE);
4738c2ecf20Sopenharmony_ci			priv->tx_phys[tx_tail] = 0;
4748c2ecf20Sopenharmony_ci		}
4758c2ecf20Sopenharmony_ci		pkts_compl++;
4768c2ecf20Sopenharmony_ci		bytes_compl += priv->tx_skb[tx_tail]->len;
4778c2ecf20Sopenharmony_ci		dev_kfree_skb(priv->tx_skb[tx_tail]);
4788c2ecf20Sopenharmony_ci		priv->tx_skb[tx_tail] = NULL;
4798c2ecf20Sopenharmony_ci		tx_tail = TX_NEXT(tx_tail);
4808c2ecf20Sopenharmony_ci		count--;
4818c2ecf20Sopenharmony_ci	}
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	priv->tx_tail = tx_tail;
4848c2ecf20Sopenharmony_ci	smp_wmb(); /* Ensure tx_tail visible to xmit */
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ciout:
4878c2ecf20Sopenharmony_ci	if (pkts_compl || bytes_compl)
4888c2ecf20Sopenharmony_ci		netdev_completed_queue(ndev, pkts_compl, bytes_compl);
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	if (unlikely(netif_queue_stopped(ndev)) && (count < (TX_DESC_NUM - 1)))
4918c2ecf20Sopenharmony_ci		netif_wake_queue(ndev);
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	return count;
4948c2ecf20Sopenharmony_ci}
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_cistatic void hip04_start_tx_timer(struct hip04_priv *priv)
4978c2ecf20Sopenharmony_ci{
4988c2ecf20Sopenharmony_ci	unsigned long ns = priv->tx_coalesce_usecs * NSEC_PER_USEC / 2;
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci	/* allow timer to fire after half the time at the earliest */
5018c2ecf20Sopenharmony_ci	hrtimer_start_range_ns(&priv->tx_coalesce_timer, ns_to_ktime(ns),
5028c2ecf20Sopenharmony_ci			       ns, HRTIMER_MODE_REL);
5038c2ecf20Sopenharmony_ci}
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_cistatic netdev_tx_t
5068c2ecf20Sopenharmony_cihip04_mac_start_xmit(struct sk_buff *skb, struct net_device *ndev)
5078c2ecf20Sopenharmony_ci{
5088c2ecf20Sopenharmony_ci	struct hip04_priv *priv = netdev_priv(ndev);
5098c2ecf20Sopenharmony_ci	struct net_device_stats *stats = &ndev->stats;
5108c2ecf20Sopenharmony_ci	unsigned int tx_head = priv->tx_head, count;
5118c2ecf20Sopenharmony_ci	struct tx_desc *desc = &priv->tx_desc[tx_head];
5128c2ecf20Sopenharmony_ci	dma_addr_t phys;
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci	smp_rmb();
5158c2ecf20Sopenharmony_ci	count = tx_count(tx_head, READ_ONCE(priv->tx_tail));
5168c2ecf20Sopenharmony_ci	if (count == (TX_DESC_NUM - 1)) {
5178c2ecf20Sopenharmony_ci		netif_stop_queue(ndev);
5188c2ecf20Sopenharmony_ci		return NETDEV_TX_BUSY;
5198c2ecf20Sopenharmony_ci	}
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci	phys = dma_map_single(priv->dev, skb->data, skb->len, DMA_TO_DEVICE);
5228c2ecf20Sopenharmony_ci	if (dma_mapping_error(priv->dev, phys)) {
5238c2ecf20Sopenharmony_ci		dev_kfree_skb(skb);
5248c2ecf20Sopenharmony_ci		return NETDEV_TX_OK;
5258c2ecf20Sopenharmony_ci	}
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	priv->tx_skb[tx_head] = skb;
5288c2ecf20Sopenharmony_ci	priv->tx_phys[tx_head] = phys;
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	desc->send_size = (__force u32)cpu_to_be32(skb->len);
5318c2ecf20Sopenharmony_ci#if defined(CONFIG_HI13X1_GMAC)
5328c2ecf20Sopenharmony_ci	desc->cfg = (__force u32)cpu_to_be32(TX_CLEAR_WB | TX_FINISH_CACHE_INV
5338c2ecf20Sopenharmony_ci		| TX_RELEASE_TO_PPE | priv->port << TX_POOL_SHIFT);
5348c2ecf20Sopenharmony_ci	desc->data_offset = (__force u32)cpu_to_be32(phys & SOC_CACHE_LINE_MASK);
5358c2ecf20Sopenharmony_ci	desc->send_addr =  (__force u32)cpu_to_be32(phys & ~SOC_CACHE_LINE_MASK);
5368c2ecf20Sopenharmony_ci#else
5378c2ecf20Sopenharmony_ci	desc->cfg = (__force u32)cpu_to_be32(TX_CLEAR_WB | TX_FINISH_CACHE_INV);
5388c2ecf20Sopenharmony_ci	desc->send_addr = (__force u32)cpu_to_be32(phys);
5398c2ecf20Sopenharmony_ci#endif
5408c2ecf20Sopenharmony_ci	phys = priv->tx_desc_dma + tx_head * sizeof(struct tx_desc);
5418c2ecf20Sopenharmony_ci	desc->wb_addr = (__force u32)cpu_to_be32(phys +
5428c2ecf20Sopenharmony_ci		offsetof(struct tx_desc, send_addr));
5438c2ecf20Sopenharmony_ci	skb_tx_timestamp(skb);
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci	hip04_set_xmit_desc(priv, phys);
5468c2ecf20Sopenharmony_ci	count++;
5478c2ecf20Sopenharmony_ci	netdev_sent_queue(ndev, skb->len);
5488c2ecf20Sopenharmony_ci	priv->tx_head = TX_NEXT(tx_head);
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_ci	stats->tx_bytes += skb->len;
5518c2ecf20Sopenharmony_ci	stats->tx_packets++;
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_ci	/* Ensure tx_head update visible to tx reclaim */
5548c2ecf20Sopenharmony_ci	smp_wmb();
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci	/* queue is getting full, better start cleaning up now */
5578c2ecf20Sopenharmony_ci	if (count >= priv->tx_coalesce_frames) {
5588c2ecf20Sopenharmony_ci		if (napi_schedule_prep(&priv->napi)) {
5598c2ecf20Sopenharmony_ci			/* disable rx interrupt and timer */
5608c2ecf20Sopenharmony_ci			priv->reg_inten &= ~(RCV_INT);
5618c2ecf20Sopenharmony_ci			writel_relaxed(DEF_INT_MASK & ~RCV_INT,
5628c2ecf20Sopenharmony_ci				       priv->base + PPE_INTEN);
5638c2ecf20Sopenharmony_ci			hrtimer_cancel(&priv->tx_coalesce_timer);
5648c2ecf20Sopenharmony_ci			__napi_schedule(&priv->napi);
5658c2ecf20Sopenharmony_ci		}
5668c2ecf20Sopenharmony_ci	} else if (!hrtimer_is_queued(&priv->tx_coalesce_timer)) {
5678c2ecf20Sopenharmony_ci		/* cleanup not pending yet, start a new timer */
5688c2ecf20Sopenharmony_ci		hip04_start_tx_timer(priv);
5698c2ecf20Sopenharmony_ci	}
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci	return NETDEV_TX_OK;
5728c2ecf20Sopenharmony_ci}
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_cistatic int hip04_rx_poll(struct napi_struct *napi, int budget)
5758c2ecf20Sopenharmony_ci{
5768c2ecf20Sopenharmony_ci	struct hip04_priv *priv = container_of(napi, struct hip04_priv, napi);
5778c2ecf20Sopenharmony_ci	struct net_device *ndev = priv->ndev;
5788c2ecf20Sopenharmony_ci	struct net_device_stats *stats = &ndev->stats;
5798c2ecf20Sopenharmony_ci	struct rx_desc *desc;
5808c2ecf20Sopenharmony_ci	struct sk_buff *skb;
5818c2ecf20Sopenharmony_ci	unsigned char *buf;
5828c2ecf20Sopenharmony_ci	bool last = false;
5838c2ecf20Sopenharmony_ci	dma_addr_t phys;
5848c2ecf20Sopenharmony_ci	int rx = 0;
5858c2ecf20Sopenharmony_ci	int tx_remaining;
5868c2ecf20Sopenharmony_ci	u16 len;
5878c2ecf20Sopenharmony_ci	u32 err;
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci	/* clean up tx descriptors */
5908c2ecf20Sopenharmony_ci	tx_remaining = hip04_tx_reclaim(ndev, false);
5918c2ecf20Sopenharmony_ci	priv->rx_cnt_remaining += hip04_recv_cnt(priv);
5928c2ecf20Sopenharmony_ci	while (priv->rx_cnt_remaining && !last) {
5938c2ecf20Sopenharmony_ci		buf = priv->rx_buf[priv->rx_head];
5948c2ecf20Sopenharmony_ci		skb = build_skb(buf, priv->rx_buf_size);
5958c2ecf20Sopenharmony_ci		if (unlikely(!skb)) {
5968c2ecf20Sopenharmony_ci			net_dbg_ratelimited("build_skb failed\n");
5978c2ecf20Sopenharmony_ci			goto refill;
5988c2ecf20Sopenharmony_ci		}
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_ci		dma_unmap_single(priv->dev, priv->rx_phys[priv->rx_head],
6018c2ecf20Sopenharmony_ci				 RX_BUF_SIZE, DMA_FROM_DEVICE);
6028c2ecf20Sopenharmony_ci		priv->rx_phys[priv->rx_head] = 0;
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci		desc = (struct rx_desc *)skb->data;
6058c2ecf20Sopenharmony_ci		len = be16_to_cpu((__force __be16)desc->pkt_len);
6068c2ecf20Sopenharmony_ci		err = be32_to_cpu((__force __be32)desc->pkt_err);
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci		if (0 == len) {
6098c2ecf20Sopenharmony_ci			dev_kfree_skb_any(skb);
6108c2ecf20Sopenharmony_ci			last = true;
6118c2ecf20Sopenharmony_ci		} else if ((err & RX_PKT_ERR) || (len >= GMAC_MAX_PKT_LEN)) {
6128c2ecf20Sopenharmony_ci			dev_kfree_skb_any(skb);
6138c2ecf20Sopenharmony_ci			stats->rx_dropped++;
6148c2ecf20Sopenharmony_ci			stats->rx_errors++;
6158c2ecf20Sopenharmony_ci		} else {
6168c2ecf20Sopenharmony_ci			skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
6178c2ecf20Sopenharmony_ci			skb_put(skb, len);
6188c2ecf20Sopenharmony_ci			skb->protocol = eth_type_trans(skb, ndev);
6198c2ecf20Sopenharmony_ci			napi_gro_receive(&priv->napi, skb);
6208c2ecf20Sopenharmony_ci			stats->rx_packets++;
6218c2ecf20Sopenharmony_ci			stats->rx_bytes += len;
6228c2ecf20Sopenharmony_ci			rx++;
6238c2ecf20Sopenharmony_ci		}
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_cirefill:
6268c2ecf20Sopenharmony_ci		buf = netdev_alloc_frag(priv->rx_buf_size);
6278c2ecf20Sopenharmony_ci		if (!buf)
6288c2ecf20Sopenharmony_ci			goto done;
6298c2ecf20Sopenharmony_ci		phys = dma_map_single(priv->dev, buf,
6308c2ecf20Sopenharmony_ci				      RX_BUF_SIZE, DMA_FROM_DEVICE);
6318c2ecf20Sopenharmony_ci		if (dma_mapping_error(priv->dev, phys))
6328c2ecf20Sopenharmony_ci			goto done;
6338c2ecf20Sopenharmony_ci		priv->rx_buf[priv->rx_head] = buf;
6348c2ecf20Sopenharmony_ci		priv->rx_phys[priv->rx_head] = phys;
6358c2ecf20Sopenharmony_ci		hip04_set_recv_desc(priv, phys);
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci		priv->rx_head = RX_NEXT(priv->rx_head);
6388c2ecf20Sopenharmony_ci		if (rx >= budget) {
6398c2ecf20Sopenharmony_ci			--priv->rx_cnt_remaining;
6408c2ecf20Sopenharmony_ci			goto done;
6418c2ecf20Sopenharmony_ci		}
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci		if (--priv->rx_cnt_remaining == 0)
6448c2ecf20Sopenharmony_ci			priv->rx_cnt_remaining += hip04_recv_cnt(priv);
6458c2ecf20Sopenharmony_ci	}
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_ci	if (!(priv->reg_inten & RCV_INT)) {
6488c2ecf20Sopenharmony_ci		/* enable rx interrupt */
6498c2ecf20Sopenharmony_ci		priv->reg_inten |= RCV_INT;
6508c2ecf20Sopenharmony_ci		writel_relaxed(priv->reg_inten, priv->base + PPE_INTEN);
6518c2ecf20Sopenharmony_ci	}
6528c2ecf20Sopenharmony_ci	napi_complete_done(napi, rx);
6538c2ecf20Sopenharmony_cidone:
6548c2ecf20Sopenharmony_ci	/* start a new timer if necessary */
6558c2ecf20Sopenharmony_ci	if (rx < budget && tx_remaining)
6568c2ecf20Sopenharmony_ci		hip04_start_tx_timer(priv);
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_ci	return rx;
6598c2ecf20Sopenharmony_ci}
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_cistatic irqreturn_t hip04_mac_interrupt(int irq, void *dev_id)
6628c2ecf20Sopenharmony_ci{
6638c2ecf20Sopenharmony_ci	struct net_device *ndev = (struct net_device *)dev_id;
6648c2ecf20Sopenharmony_ci	struct hip04_priv *priv = netdev_priv(ndev);
6658c2ecf20Sopenharmony_ci	struct net_device_stats *stats = &ndev->stats;
6668c2ecf20Sopenharmony_ci	u32 ists = readl_relaxed(priv->base + PPE_INTSTS);
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ci	if (!ists)
6698c2ecf20Sopenharmony_ci		return IRQ_NONE;
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci	writel_relaxed(DEF_INT_MASK, priv->base + PPE_RINT);
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci	if (unlikely(ists & DEF_INT_ERR)) {
6748c2ecf20Sopenharmony_ci		if (ists & (RCV_NOBUF | RCV_DROP)) {
6758c2ecf20Sopenharmony_ci			stats->rx_errors++;
6768c2ecf20Sopenharmony_ci			stats->rx_dropped++;
6778c2ecf20Sopenharmony_ci			netdev_err(ndev, "rx drop\n");
6788c2ecf20Sopenharmony_ci		}
6798c2ecf20Sopenharmony_ci		if (ists & TX_DROP) {
6808c2ecf20Sopenharmony_ci			stats->tx_dropped++;
6818c2ecf20Sopenharmony_ci			netdev_err(ndev, "tx drop\n");
6828c2ecf20Sopenharmony_ci		}
6838c2ecf20Sopenharmony_ci	}
6848c2ecf20Sopenharmony_ci
6858c2ecf20Sopenharmony_ci	if (ists & RCV_INT && napi_schedule_prep(&priv->napi)) {
6868c2ecf20Sopenharmony_ci		/* disable rx interrupt */
6878c2ecf20Sopenharmony_ci		priv->reg_inten &= ~(RCV_INT);
6888c2ecf20Sopenharmony_ci		writel_relaxed(DEF_INT_MASK & ~RCV_INT, priv->base + PPE_INTEN);
6898c2ecf20Sopenharmony_ci		hrtimer_cancel(&priv->tx_coalesce_timer);
6908c2ecf20Sopenharmony_ci		__napi_schedule(&priv->napi);
6918c2ecf20Sopenharmony_ci	}
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
6948c2ecf20Sopenharmony_ci}
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_cistatic enum hrtimer_restart tx_done(struct hrtimer *hrtimer)
6978c2ecf20Sopenharmony_ci{
6988c2ecf20Sopenharmony_ci	struct hip04_priv *priv;
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci	priv = container_of(hrtimer, struct hip04_priv, tx_coalesce_timer);
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci	if (napi_schedule_prep(&priv->napi)) {
7038c2ecf20Sopenharmony_ci		/* disable rx interrupt */
7048c2ecf20Sopenharmony_ci		priv->reg_inten &= ~(RCV_INT);
7058c2ecf20Sopenharmony_ci		writel_relaxed(DEF_INT_MASK & ~RCV_INT, priv->base + PPE_INTEN);
7068c2ecf20Sopenharmony_ci		__napi_schedule(&priv->napi);
7078c2ecf20Sopenharmony_ci	}
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci	return HRTIMER_NORESTART;
7108c2ecf20Sopenharmony_ci}
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_cistatic void hip04_adjust_link(struct net_device *ndev)
7138c2ecf20Sopenharmony_ci{
7148c2ecf20Sopenharmony_ci	struct hip04_priv *priv = netdev_priv(ndev);
7158c2ecf20Sopenharmony_ci	struct phy_device *phy = priv->phy;
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_ci	if ((priv->speed != phy->speed) || (priv->duplex != phy->duplex)) {
7188c2ecf20Sopenharmony_ci		hip04_config_port(ndev, phy->speed, phy->duplex);
7198c2ecf20Sopenharmony_ci		phy_print_status(phy);
7208c2ecf20Sopenharmony_ci	}
7218c2ecf20Sopenharmony_ci}
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_cistatic int hip04_mac_open(struct net_device *ndev)
7248c2ecf20Sopenharmony_ci{
7258c2ecf20Sopenharmony_ci	struct hip04_priv *priv = netdev_priv(ndev);
7268c2ecf20Sopenharmony_ci	int i;
7278c2ecf20Sopenharmony_ci
7288c2ecf20Sopenharmony_ci	priv->rx_head = 0;
7298c2ecf20Sopenharmony_ci	priv->rx_cnt_remaining = 0;
7308c2ecf20Sopenharmony_ci	priv->tx_head = 0;
7318c2ecf20Sopenharmony_ci	priv->tx_tail = 0;
7328c2ecf20Sopenharmony_ci	hip04_reset_ppe(priv);
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_ci	for (i = 0; i < RX_DESC_NUM; i++) {
7358c2ecf20Sopenharmony_ci		dma_addr_t phys;
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci		phys = dma_map_single(priv->dev, priv->rx_buf[i],
7388c2ecf20Sopenharmony_ci				      RX_BUF_SIZE, DMA_FROM_DEVICE);
7398c2ecf20Sopenharmony_ci		if (dma_mapping_error(priv->dev, phys))
7408c2ecf20Sopenharmony_ci			return -EIO;
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ci		priv->rx_phys[i] = phys;
7438c2ecf20Sopenharmony_ci		hip04_set_recv_desc(priv, phys);
7448c2ecf20Sopenharmony_ci	}
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_ci	if (priv->phy)
7478c2ecf20Sopenharmony_ci		phy_start(priv->phy);
7488c2ecf20Sopenharmony_ci
7498c2ecf20Sopenharmony_ci	netdev_reset_queue(ndev);
7508c2ecf20Sopenharmony_ci	netif_start_queue(ndev);
7518c2ecf20Sopenharmony_ci	hip04_mac_enable(ndev);
7528c2ecf20Sopenharmony_ci	napi_enable(&priv->napi);
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_ci	return 0;
7558c2ecf20Sopenharmony_ci}
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_cistatic int hip04_mac_stop(struct net_device *ndev)
7588c2ecf20Sopenharmony_ci{
7598c2ecf20Sopenharmony_ci	struct hip04_priv *priv = netdev_priv(ndev);
7608c2ecf20Sopenharmony_ci	int i;
7618c2ecf20Sopenharmony_ci
7628c2ecf20Sopenharmony_ci	napi_disable(&priv->napi);
7638c2ecf20Sopenharmony_ci	netif_stop_queue(ndev);
7648c2ecf20Sopenharmony_ci	hip04_mac_disable(ndev);
7658c2ecf20Sopenharmony_ci	hip04_tx_reclaim(ndev, true);
7668c2ecf20Sopenharmony_ci	hip04_reset_ppe(priv);
7678c2ecf20Sopenharmony_ci
7688c2ecf20Sopenharmony_ci	if (priv->phy)
7698c2ecf20Sopenharmony_ci		phy_stop(priv->phy);
7708c2ecf20Sopenharmony_ci
7718c2ecf20Sopenharmony_ci	for (i = 0; i < RX_DESC_NUM; i++) {
7728c2ecf20Sopenharmony_ci		if (priv->rx_phys[i]) {
7738c2ecf20Sopenharmony_ci			dma_unmap_single(priv->dev, priv->rx_phys[i],
7748c2ecf20Sopenharmony_ci					 RX_BUF_SIZE, DMA_FROM_DEVICE);
7758c2ecf20Sopenharmony_ci			priv->rx_phys[i] = 0;
7768c2ecf20Sopenharmony_ci		}
7778c2ecf20Sopenharmony_ci	}
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ci	return 0;
7808c2ecf20Sopenharmony_ci}
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_cistatic void hip04_timeout(struct net_device *ndev, unsigned int txqueue)
7838c2ecf20Sopenharmony_ci{
7848c2ecf20Sopenharmony_ci	struct hip04_priv *priv = netdev_priv(ndev);
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_ci	schedule_work(&priv->tx_timeout_task);
7878c2ecf20Sopenharmony_ci}
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_cistatic void hip04_tx_timeout_task(struct work_struct *work)
7908c2ecf20Sopenharmony_ci{
7918c2ecf20Sopenharmony_ci	struct hip04_priv *priv;
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_ci	priv = container_of(work, struct hip04_priv, tx_timeout_task);
7948c2ecf20Sopenharmony_ci	hip04_mac_stop(priv->ndev);
7958c2ecf20Sopenharmony_ci	hip04_mac_open(priv->ndev);
7968c2ecf20Sopenharmony_ci}
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_cistatic int hip04_get_coalesce(struct net_device *netdev,
7998c2ecf20Sopenharmony_ci			      struct ethtool_coalesce *ec)
8008c2ecf20Sopenharmony_ci{
8018c2ecf20Sopenharmony_ci	struct hip04_priv *priv = netdev_priv(netdev);
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_ci	ec->tx_coalesce_usecs = priv->tx_coalesce_usecs;
8048c2ecf20Sopenharmony_ci	ec->tx_max_coalesced_frames = priv->tx_coalesce_frames;
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_ci	return 0;
8078c2ecf20Sopenharmony_ci}
8088c2ecf20Sopenharmony_ci
8098c2ecf20Sopenharmony_cistatic int hip04_set_coalesce(struct net_device *netdev,
8108c2ecf20Sopenharmony_ci			      struct ethtool_coalesce *ec)
8118c2ecf20Sopenharmony_ci{
8128c2ecf20Sopenharmony_ci	struct hip04_priv *priv = netdev_priv(netdev);
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_ci	if ((ec->tx_coalesce_usecs > HIP04_MAX_TX_COALESCE_USECS ||
8158c2ecf20Sopenharmony_ci	     ec->tx_coalesce_usecs < HIP04_MIN_TX_COALESCE_USECS) ||
8168c2ecf20Sopenharmony_ci	    (ec->tx_max_coalesced_frames > HIP04_MAX_TX_COALESCE_FRAMES ||
8178c2ecf20Sopenharmony_ci	     ec->tx_max_coalesced_frames < HIP04_MIN_TX_COALESCE_FRAMES))
8188c2ecf20Sopenharmony_ci		return -EINVAL;
8198c2ecf20Sopenharmony_ci
8208c2ecf20Sopenharmony_ci	priv->tx_coalesce_usecs = ec->tx_coalesce_usecs;
8218c2ecf20Sopenharmony_ci	priv->tx_coalesce_frames = ec->tx_max_coalesced_frames;
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_ci	return 0;
8248c2ecf20Sopenharmony_ci}
8258c2ecf20Sopenharmony_ci
8268c2ecf20Sopenharmony_cistatic void hip04_get_drvinfo(struct net_device *netdev,
8278c2ecf20Sopenharmony_ci			      struct ethtool_drvinfo *drvinfo)
8288c2ecf20Sopenharmony_ci{
8298c2ecf20Sopenharmony_ci	strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
8308c2ecf20Sopenharmony_ci	strlcpy(drvinfo->version, DRV_VERSION, sizeof(drvinfo->version));
8318c2ecf20Sopenharmony_ci}
8328c2ecf20Sopenharmony_ci
8338c2ecf20Sopenharmony_cistatic const struct ethtool_ops hip04_ethtool_ops = {
8348c2ecf20Sopenharmony_ci	.supported_coalesce_params = ETHTOOL_COALESCE_TX_USECS |
8358c2ecf20Sopenharmony_ci				     ETHTOOL_COALESCE_TX_MAX_FRAMES,
8368c2ecf20Sopenharmony_ci	.get_coalesce		= hip04_get_coalesce,
8378c2ecf20Sopenharmony_ci	.set_coalesce		= hip04_set_coalesce,
8388c2ecf20Sopenharmony_ci	.get_drvinfo		= hip04_get_drvinfo,
8398c2ecf20Sopenharmony_ci};
8408c2ecf20Sopenharmony_ci
8418c2ecf20Sopenharmony_cistatic const struct net_device_ops hip04_netdev_ops = {
8428c2ecf20Sopenharmony_ci	.ndo_open		= hip04_mac_open,
8438c2ecf20Sopenharmony_ci	.ndo_stop		= hip04_mac_stop,
8448c2ecf20Sopenharmony_ci	.ndo_start_xmit		= hip04_mac_start_xmit,
8458c2ecf20Sopenharmony_ci	.ndo_set_mac_address	= hip04_set_mac_address,
8468c2ecf20Sopenharmony_ci	.ndo_tx_timeout         = hip04_timeout,
8478c2ecf20Sopenharmony_ci	.ndo_validate_addr	= eth_validate_addr,
8488c2ecf20Sopenharmony_ci};
8498c2ecf20Sopenharmony_ci
8508c2ecf20Sopenharmony_cistatic int hip04_alloc_ring(struct net_device *ndev, struct device *d)
8518c2ecf20Sopenharmony_ci{
8528c2ecf20Sopenharmony_ci	struct hip04_priv *priv = netdev_priv(ndev);
8538c2ecf20Sopenharmony_ci	int i;
8548c2ecf20Sopenharmony_ci
8558c2ecf20Sopenharmony_ci	priv->tx_desc = dma_alloc_coherent(d,
8568c2ecf20Sopenharmony_ci					   TX_DESC_NUM * sizeof(struct tx_desc),
8578c2ecf20Sopenharmony_ci					   &priv->tx_desc_dma, GFP_KERNEL);
8588c2ecf20Sopenharmony_ci	if (!priv->tx_desc)
8598c2ecf20Sopenharmony_ci		return -ENOMEM;
8608c2ecf20Sopenharmony_ci
8618c2ecf20Sopenharmony_ci	priv->rx_buf_size = RX_BUF_SIZE +
8628c2ecf20Sopenharmony_ci			    SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
8638c2ecf20Sopenharmony_ci	for (i = 0; i < RX_DESC_NUM; i++) {
8648c2ecf20Sopenharmony_ci		priv->rx_buf[i] = netdev_alloc_frag(priv->rx_buf_size);
8658c2ecf20Sopenharmony_ci		if (!priv->rx_buf[i])
8668c2ecf20Sopenharmony_ci			return -ENOMEM;
8678c2ecf20Sopenharmony_ci	}
8688c2ecf20Sopenharmony_ci
8698c2ecf20Sopenharmony_ci	return 0;
8708c2ecf20Sopenharmony_ci}
8718c2ecf20Sopenharmony_ci
8728c2ecf20Sopenharmony_cistatic void hip04_free_ring(struct net_device *ndev, struct device *d)
8738c2ecf20Sopenharmony_ci{
8748c2ecf20Sopenharmony_ci	struct hip04_priv *priv = netdev_priv(ndev);
8758c2ecf20Sopenharmony_ci	int i;
8768c2ecf20Sopenharmony_ci
8778c2ecf20Sopenharmony_ci	for (i = 0; i < RX_DESC_NUM; i++)
8788c2ecf20Sopenharmony_ci		if (priv->rx_buf[i])
8798c2ecf20Sopenharmony_ci			skb_free_frag(priv->rx_buf[i]);
8808c2ecf20Sopenharmony_ci
8818c2ecf20Sopenharmony_ci	for (i = 0; i < TX_DESC_NUM; i++)
8828c2ecf20Sopenharmony_ci		if (priv->tx_skb[i])
8838c2ecf20Sopenharmony_ci			dev_kfree_skb_any(priv->tx_skb[i]);
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_ci	dma_free_coherent(d, TX_DESC_NUM * sizeof(struct tx_desc),
8868c2ecf20Sopenharmony_ci			  priv->tx_desc, priv->tx_desc_dma);
8878c2ecf20Sopenharmony_ci}
8888c2ecf20Sopenharmony_ci
8898c2ecf20Sopenharmony_cistatic int hip04_mac_probe(struct platform_device *pdev)
8908c2ecf20Sopenharmony_ci{
8918c2ecf20Sopenharmony_ci	struct device *d = &pdev->dev;
8928c2ecf20Sopenharmony_ci	struct device_node *node = d->of_node;
8938c2ecf20Sopenharmony_ci	struct of_phandle_args arg;
8948c2ecf20Sopenharmony_ci	struct net_device *ndev;
8958c2ecf20Sopenharmony_ci	struct hip04_priv *priv;
8968c2ecf20Sopenharmony_ci	int irq;
8978c2ecf20Sopenharmony_ci	int ret;
8988c2ecf20Sopenharmony_ci
8998c2ecf20Sopenharmony_ci	ndev = alloc_etherdev(sizeof(struct hip04_priv));
9008c2ecf20Sopenharmony_ci	if (!ndev)
9018c2ecf20Sopenharmony_ci		return -ENOMEM;
9028c2ecf20Sopenharmony_ci
9038c2ecf20Sopenharmony_ci	priv = netdev_priv(ndev);
9048c2ecf20Sopenharmony_ci	priv->dev = d;
9058c2ecf20Sopenharmony_ci	priv->ndev = ndev;
9068c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, ndev);
9078c2ecf20Sopenharmony_ci	SET_NETDEV_DEV(ndev, &pdev->dev);
9088c2ecf20Sopenharmony_ci
9098c2ecf20Sopenharmony_ci	priv->base = devm_platform_ioremap_resource(pdev, 0);
9108c2ecf20Sopenharmony_ci	if (IS_ERR(priv->base)) {
9118c2ecf20Sopenharmony_ci		ret = PTR_ERR(priv->base);
9128c2ecf20Sopenharmony_ci		goto init_fail;
9138c2ecf20Sopenharmony_ci	}
9148c2ecf20Sopenharmony_ci
9158c2ecf20Sopenharmony_ci#if defined(CONFIG_HI13X1_GMAC)
9168c2ecf20Sopenharmony_ci	priv->sysctrl_base = devm_platform_ioremap_resource(pdev, 1);
9178c2ecf20Sopenharmony_ci	if (IS_ERR(priv->sysctrl_base)) {
9188c2ecf20Sopenharmony_ci		ret = PTR_ERR(priv->sysctrl_base);
9198c2ecf20Sopenharmony_ci		goto init_fail;
9208c2ecf20Sopenharmony_ci	}
9218c2ecf20Sopenharmony_ci#endif
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_ci	ret = of_parse_phandle_with_fixed_args(node, "port-handle", 3, 0, &arg);
9248c2ecf20Sopenharmony_ci	if (ret < 0) {
9258c2ecf20Sopenharmony_ci		dev_warn(d, "no port-handle\n");
9268c2ecf20Sopenharmony_ci		goto init_fail;
9278c2ecf20Sopenharmony_ci	}
9288c2ecf20Sopenharmony_ci
9298c2ecf20Sopenharmony_ci	priv->port = arg.args[0];
9308c2ecf20Sopenharmony_ci	priv->chan = arg.args[1] * RX_DESC_NUM;
9318c2ecf20Sopenharmony_ci	priv->group = arg.args[2];
9328c2ecf20Sopenharmony_ci
9338c2ecf20Sopenharmony_ci	hrtimer_init(&priv->tx_coalesce_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
9348c2ecf20Sopenharmony_ci
9358c2ecf20Sopenharmony_ci	/* BQL will try to keep the TX queue as short as possible, but it can't
9368c2ecf20Sopenharmony_ci	 * be faster than tx_coalesce_usecs, so we need a fast timeout here,
9378c2ecf20Sopenharmony_ci	 * but also long enough to gather up enough frames to ensure we don't
9388c2ecf20Sopenharmony_ci	 * get more interrupts than necessary.
9398c2ecf20Sopenharmony_ci	 * 200us is enough for 16 frames of 1500 bytes at gigabit ethernet rate
9408c2ecf20Sopenharmony_ci	 */
9418c2ecf20Sopenharmony_ci	priv->tx_coalesce_frames = TX_DESC_NUM * 3 / 4;
9428c2ecf20Sopenharmony_ci	priv->tx_coalesce_usecs = 200;
9438c2ecf20Sopenharmony_ci	priv->tx_coalesce_timer.function = tx_done;
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_ci	priv->map = syscon_node_to_regmap(arg.np);
9468c2ecf20Sopenharmony_ci	if (IS_ERR(priv->map)) {
9478c2ecf20Sopenharmony_ci		dev_warn(d, "no syscon hisilicon,hip04-ppe\n");
9488c2ecf20Sopenharmony_ci		ret = PTR_ERR(priv->map);
9498c2ecf20Sopenharmony_ci		goto init_fail;
9508c2ecf20Sopenharmony_ci	}
9518c2ecf20Sopenharmony_ci
9528c2ecf20Sopenharmony_ci	ret = of_get_phy_mode(node, &priv->phy_mode);
9538c2ecf20Sopenharmony_ci	if (ret) {
9548c2ecf20Sopenharmony_ci		dev_warn(d, "not find phy-mode\n");
9558c2ecf20Sopenharmony_ci		goto init_fail;
9568c2ecf20Sopenharmony_ci	}
9578c2ecf20Sopenharmony_ci
9588c2ecf20Sopenharmony_ci	irq = platform_get_irq(pdev, 0);
9598c2ecf20Sopenharmony_ci	if (irq <= 0) {
9608c2ecf20Sopenharmony_ci		ret = -EINVAL;
9618c2ecf20Sopenharmony_ci		goto init_fail;
9628c2ecf20Sopenharmony_ci	}
9638c2ecf20Sopenharmony_ci
9648c2ecf20Sopenharmony_ci	ret = devm_request_irq(d, irq, hip04_mac_interrupt,
9658c2ecf20Sopenharmony_ci			       0, pdev->name, ndev);
9668c2ecf20Sopenharmony_ci	if (ret) {
9678c2ecf20Sopenharmony_ci		netdev_err(ndev, "devm_request_irq failed\n");
9688c2ecf20Sopenharmony_ci		goto init_fail;
9698c2ecf20Sopenharmony_ci	}
9708c2ecf20Sopenharmony_ci
9718c2ecf20Sopenharmony_ci	priv->phy_node = of_parse_phandle(node, "phy-handle", 0);
9728c2ecf20Sopenharmony_ci	if (priv->phy_node) {
9738c2ecf20Sopenharmony_ci		priv->phy = of_phy_connect(ndev, priv->phy_node,
9748c2ecf20Sopenharmony_ci					   &hip04_adjust_link,
9758c2ecf20Sopenharmony_ci					   0, priv->phy_mode);
9768c2ecf20Sopenharmony_ci		if (!priv->phy) {
9778c2ecf20Sopenharmony_ci			ret = -EPROBE_DEFER;
9788c2ecf20Sopenharmony_ci			goto init_fail;
9798c2ecf20Sopenharmony_ci		}
9808c2ecf20Sopenharmony_ci	}
9818c2ecf20Sopenharmony_ci
9828c2ecf20Sopenharmony_ci	INIT_WORK(&priv->tx_timeout_task, hip04_tx_timeout_task);
9838c2ecf20Sopenharmony_ci
9848c2ecf20Sopenharmony_ci	ndev->netdev_ops = &hip04_netdev_ops;
9858c2ecf20Sopenharmony_ci	ndev->ethtool_ops = &hip04_ethtool_ops;
9868c2ecf20Sopenharmony_ci	ndev->watchdog_timeo = TX_TIMEOUT;
9878c2ecf20Sopenharmony_ci	ndev->priv_flags |= IFF_UNICAST_FLT;
9888c2ecf20Sopenharmony_ci	ndev->irq = irq;
9898c2ecf20Sopenharmony_ci	netif_napi_add(ndev, &priv->napi, hip04_rx_poll, NAPI_POLL_WEIGHT);
9908c2ecf20Sopenharmony_ci
9918c2ecf20Sopenharmony_ci	hip04_reset_dreq(priv);
9928c2ecf20Sopenharmony_ci	hip04_reset_ppe(priv);
9938c2ecf20Sopenharmony_ci	if (priv->phy_mode == PHY_INTERFACE_MODE_MII)
9948c2ecf20Sopenharmony_ci		hip04_config_port(ndev, SPEED_100, DUPLEX_FULL);
9958c2ecf20Sopenharmony_ci
9968c2ecf20Sopenharmony_ci	hip04_config_fifo(priv);
9978c2ecf20Sopenharmony_ci	eth_random_addr(ndev->dev_addr);
9988c2ecf20Sopenharmony_ci	hip04_update_mac_address(ndev);
9998c2ecf20Sopenharmony_ci
10008c2ecf20Sopenharmony_ci	ret = hip04_alloc_ring(ndev, d);
10018c2ecf20Sopenharmony_ci	if (ret) {
10028c2ecf20Sopenharmony_ci		netdev_err(ndev, "alloc ring fail\n");
10038c2ecf20Sopenharmony_ci		goto alloc_fail;
10048c2ecf20Sopenharmony_ci	}
10058c2ecf20Sopenharmony_ci
10068c2ecf20Sopenharmony_ci	ret = register_netdev(ndev);
10078c2ecf20Sopenharmony_ci	if (ret)
10088c2ecf20Sopenharmony_ci		goto alloc_fail;
10098c2ecf20Sopenharmony_ci
10108c2ecf20Sopenharmony_ci	return 0;
10118c2ecf20Sopenharmony_ci
10128c2ecf20Sopenharmony_cialloc_fail:
10138c2ecf20Sopenharmony_ci	hip04_free_ring(ndev, d);
10148c2ecf20Sopenharmony_ciinit_fail:
10158c2ecf20Sopenharmony_ci	of_node_put(priv->phy_node);
10168c2ecf20Sopenharmony_ci	free_netdev(ndev);
10178c2ecf20Sopenharmony_ci	return ret;
10188c2ecf20Sopenharmony_ci}
10198c2ecf20Sopenharmony_ci
10208c2ecf20Sopenharmony_cistatic int hip04_remove(struct platform_device *pdev)
10218c2ecf20Sopenharmony_ci{
10228c2ecf20Sopenharmony_ci	struct net_device *ndev = platform_get_drvdata(pdev);
10238c2ecf20Sopenharmony_ci	struct hip04_priv *priv = netdev_priv(ndev);
10248c2ecf20Sopenharmony_ci	struct device *d = &pdev->dev;
10258c2ecf20Sopenharmony_ci
10268c2ecf20Sopenharmony_ci	if (priv->phy)
10278c2ecf20Sopenharmony_ci		phy_disconnect(priv->phy);
10288c2ecf20Sopenharmony_ci
10298c2ecf20Sopenharmony_ci	hip04_free_ring(ndev, d);
10308c2ecf20Sopenharmony_ci	unregister_netdev(ndev);
10318c2ecf20Sopenharmony_ci	of_node_put(priv->phy_node);
10328c2ecf20Sopenharmony_ci	cancel_work_sync(&priv->tx_timeout_task);
10338c2ecf20Sopenharmony_ci	free_netdev(ndev);
10348c2ecf20Sopenharmony_ci
10358c2ecf20Sopenharmony_ci	return 0;
10368c2ecf20Sopenharmony_ci}
10378c2ecf20Sopenharmony_ci
10388c2ecf20Sopenharmony_cistatic const struct of_device_id hip04_mac_match[] = {
10398c2ecf20Sopenharmony_ci	{ .compatible = "hisilicon,hip04-mac" },
10408c2ecf20Sopenharmony_ci	{ }
10418c2ecf20Sopenharmony_ci};
10428c2ecf20Sopenharmony_ci
10438c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, hip04_mac_match);
10448c2ecf20Sopenharmony_ci
10458c2ecf20Sopenharmony_cistatic struct platform_driver hip04_mac_driver = {
10468c2ecf20Sopenharmony_ci	.probe	= hip04_mac_probe,
10478c2ecf20Sopenharmony_ci	.remove	= hip04_remove,
10488c2ecf20Sopenharmony_ci	.driver	= {
10498c2ecf20Sopenharmony_ci		.name		= DRV_NAME,
10508c2ecf20Sopenharmony_ci		.of_match_table	= hip04_mac_match,
10518c2ecf20Sopenharmony_ci	},
10528c2ecf20Sopenharmony_ci};
10538c2ecf20Sopenharmony_cimodule_platform_driver(hip04_mac_driver);
10548c2ecf20Sopenharmony_ci
10558c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("HISILICON P04 Ethernet driver");
10568c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1057