162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
262306a36Sopenharmony_ci/* Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
362306a36Sopenharmony_ci */
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci/* EMAC DMA HW engine uses three rings:
662306a36Sopenharmony_ci * Tx:
762306a36Sopenharmony_ci *   TPD: Transmit Packet Descriptor ring.
862306a36Sopenharmony_ci * Rx:
962306a36Sopenharmony_ci *   RFD: Receive Free Descriptor ring.
1062306a36Sopenharmony_ci *     Ring of descriptors with empty buffers to be filled by Rx HW.
1162306a36Sopenharmony_ci *   RRD: Receive Return Descriptor ring.
1262306a36Sopenharmony_ci *     Ring of descriptors with buffers filled with received data.
1362306a36Sopenharmony_ci */
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#ifndef _EMAC_HW_H_
1662306a36Sopenharmony_ci#define _EMAC_HW_H_
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci/* EMAC_CSR register offsets */
1962306a36Sopenharmony_ci#define EMAC_EMAC_WRAPPER_CSR1                                0x000000
2062306a36Sopenharmony_ci#define EMAC_EMAC_WRAPPER_CSR2                                0x000004
2162306a36Sopenharmony_ci#define EMAC_EMAC_WRAPPER_TX_TS_LO                            0x000104
2262306a36Sopenharmony_ci#define EMAC_EMAC_WRAPPER_TX_TS_HI                            0x000108
2362306a36Sopenharmony_ci#define EMAC_EMAC_WRAPPER_TX_TS_INX                           0x00010c
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci/* DMA Order Settings */
2662306a36Sopenharmony_cienum emac_dma_order {
2762306a36Sopenharmony_ci	emac_dma_ord_in = 1,
2862306a36Sopenharmony_ci	emac_dma_ord_enh = 2,
2962306a36Sopenharmony_ci	emac_dma_ord_out = 4
3062306a36Sopenharmony_ci};
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_cienum emac_dma_req_block {
3362306a36Sopenharmony_ci	emac_dma_req_128 = 0,
3462306a36Sopenharmony_ci	emac_dma_req_256 = 1,
3562306a36Sopenharmony_ci	emac_dma_req_512 = 2,
3662306a36Sopenharmony_ci	emac_dma_req_1024 = 3,
3762306a36Sopenharmony_ci	emac_dma_req_2048 = 4,
3862306a36Sopenharmony_ci	emac_dma_req_4096 = 5
3962306a36Sopenharmony_ci};
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci/* Returns the value of bits idx...idx+n_bits */
4262306a36Sopenharmony_ci#define BITS_GET(val, lo, hi) ((le32_to_cpu(val) & GENMASK((hi), (lo))) >> lo)
4362306a36Sopenharmony_ci#define BITS_SET(val, lo, hi, new_val) \
4462306a36Sopenharmony_ci	val = cpu_to_le32((le32_to_cpu(val) & (~GENMASK((hi), (lo)))) |	\
4562306a36Sopenharmony_ci		(((new_val) << (lo)) & GENMASK((hi), (lo))))
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci/* RRD (Receive Return Descriptor) */
4862306a36Sopenharmony_cistruct emac_rrd {
4962306a36Sopenharmony_ci	u32	word[6];
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci/* number of RFD */
5262306a36Sopenharmony_ci#define RRD_NOR(rrd)			BITS_GET((rrd)->word[0], 16, 19)
5362306a36Sopenharmony_ci/* start consumer index of rfd-ring */
5462306a36Sopenharmony_ci#define RRD_SI(rrd)			BITS_GET((rrd)->word[0], 20, 31)
5562306a36Sopenharmony_ci/* vlan-tag (CVID, CFI and PRI) */
5662306a36Sopenharmony_ci#define RRD_CVALN_TAG(rrd)		BITS_GET((rrd)->word[2], 0, 15)
5762306a36Sopenharmony_ci/* length of the packet */
5862306a36Sopenharmony_ci#define RRD_PKT_SIZE(rrd)		BITS_GET((rrd)->word[3], 0, 13)
5962306a36Sopenharmony_ci/* L4(TCP/UDP) checksum failed */
6062306a36Sopenharmony_ci#define RRD_L4F(rrd)			BITS_GET((rrd)->word[3], 14, 14)
6162306a36Sopenharmony_ci/* vlan tagged */
6262306a36Sopenharmony_ci#define RRD_CVTAG(rrd)			BITS_GET((rrd)->word[3], 16, 16)
6362306a36Sopenharmony_ci/* When set, indicates that the descriptor is updated by the IP core.
6462306a36Sopenharmony_ci * When cleared, indicates that the descriptor is invalid.
6562306a36Sopenharmony_ci */
6662306a36Sopenharmony_ci#define RRD_UPDT(rrd)			BITS_GET((rrd)->word[3], 31, 31)
6762306a36Sopenharmony_ci#define RRD_UPDT_SET(rrd, val)		BITS_SET((rrd)->word[3], 31, 31, val)
6862306a36Sopenharmony_ci/* timestamp low */
6962306a36Sopenharmony_ci#define RRD_TS_LOW(rrd)			BITS_GET((rrd)->word[4], 0, 29)
7062306a36Sopenharmony_ci/* timestamp high */
7162306a36Sopenharmony_ci#define RRD_TS_HI(rrd)			le32_to_cpu((rrd)->word[5])
7262306a36Sopenharmony_ci};
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci/* TPD (Transmit Packet Descriptor) */
7562306a36Sopenharmony_cistruct emac_tpd {
7662306a36Sopenharmony_ci	u32				word[4];
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci/* Number of bytes of the transmit packet. (include 4-byte CRC) */
7962306a36Sopenharmony_ci#define TPD_BUF_LEN_SET(tpd, val)	BITS_SET((tpd)->word[0], 0, 15, val)
8062306a36Sopenharmony_ci/* Custom Checksum Offload: When set, ask IP core to offload custom checksum */
8162306a36Sopenharmony_ci#define TPD_CSX_SET(tpd, val)		BITS_SET((tpd)->word[1], 8, 8, val)
8262306a36Sopenharmony_ci/* TCP Large Send Offload: When set, ask IP core to do offload TCP Large Send */
8362306a36Sopenharmony_ci#define TPD_LSO(tpd)			BITS_GET((tpd)->word[1], 12, 12)
8462306a36Sopenharmony_ci#define TPD_LSO_SET(tpd, val)		BITS_SET((tpd)->word[1], 12, 12, val)
8562306a36Sopenharmony_ci/*  Large Send Offload Version: When set, indicates this is an LSOv2
8662306a36Sopenharmony_ci * (for both IPv4 and IPv6). When cleared, indicates this is an LSOv1
8762306a36Sopenharmony_ci * (only for IPv4).
8862306a36Sopenharmony_ci */
8962306a36Sopenharmony_ci#define TPD_LSOV_SET(tpd, val)		BITS_SET((tpd)->word[1], 13, 13, val)
9062306a36Sopenharmony_ci/* IPv4 packet: When set, indicates this is an  IPv4 packet, this bit is only
9162306a36Sopenharmony_ci * for LSOV2 format.
9262306a36Sopenharmony_ci */
9362306a36Sopenharmony_ci#define TPD_IPV4_SET(tpd, val)		BITS_SET((tpd)->word[1], 16, 16, val)
9462306a36Sopenharmony_ci/* 0: Ethernet   frame (DA+SA+TYPE+DATA+CRC)
9562306a36Sopenharmony_ci * 1: IEEE 802.3 frame (DA+SA+LEN+DSAP+SSAP+CTL+ORG+TYPE+DATA+CRC)
9662306a36Sopenharmony_ci */
9762306a36Sopenharmony_ci#define TPD_TYP_SET(tpd, val)		BITS_SET((tpd)->word[1], 17, 17, val)
9862306a36Sopenharmony_ci/* Low-32bit Buffer Address */
9962306a36Sopenharmony_ci#define TPD_BUFFER_ADDR_L_SET(tpd, val)	((tpd)->word[2] = cpu_to_le32(val))
10062306a36Sopenharmony_ci/* CVLAN Tag to be inserted if INS_VLAN_TAG is set, CVLAN TPID based on global
10162306a36Sopenharmony_ci * register configuration.
10262306a36Sopenharmony_ci */
10362306a36Sopenharmony_ci#define TPD_CVLAN_TAG_SET(tpd, val)	BITS_SET((tpd)->word[3], 0, 15, val)
10462306a36Sopenharmony_ci/*  Insert CVlan Tag: When set, ask MAC to insert CVLAN TAG to outgoing packet
10562306a36Sopenharmony_ci */
10662306a36Sopenharmony_ci#define TPD_INSTC_SET(tpd, val)		BITS_SET((tpd)->word[3], 17, 17, val)
10762306a36Sopenharmony_ci/* High-14bit Buffer Address, So, the 64b-bit address is
10862306a36Sopenharmony_ci * {DESC_CTRL_11_TX_DATA_HIADDR[17:0],(register) BUFFER_ADDR_H, BUFFER_ADDR_L}
10962306a36Sopenharmony_ci * Extend TPD_BUFFER_ADDR_H to [31, 18], because we never enable timestamping.
11062306a36Sopenharmony_ci */
11162306a36Sopenharmony_ci#define TPD_BUFFER_ADDR_H_SET(tpd, val)	BITS_SET((tpd)->word[3], 18, 31, val)
11262306a36Sopenharmony_ci/* Format D. Word offset from the 1st byte of this packet to start to calculate
11362306a36Sopenharmony_ci * the custom checksum.
11462306a36Sopenharmony_ci */
11562306a36Sopenharmony_ci#define TPD_PAYLOAD_OFFSET_SET(tpd, val) BITS_SET((tpd)->word[1], 0, 7, val)
11662306a36Sopenharmony_ci/*  Format D. Word offset from the 1st byte of this packet to fill the custom
11762306a36Sopenharmony_ci * checksum to
11862306a36Sopenharmony_ci */
11962306a36Sopenharmony_ci#define TPD_CXSUM_OFFSET_SET(tpd, val)	BITS_SET((tpd)->word[1], 18, 25, val)
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci/* Format C. TCP Header offset from the 1st byte of this packet. (byte unit) */
12262306a36Sopenharmony_ci#define TPD_TCPHDR_OFFSET_SET(tpd, val)	BITS_SET((tpd)->word[1], 0, 7, val)
12362306a36Sopenharmony_ci/* Format C. MSS (Maximum Segment Size) got from the protocol layer. (byte unit)
12462306a36Sopenharmony_ci */
12562306a36Sopenharmony_ci#define TPD_MSS_SET(tpd, val)		BITS_SET((tpd)->word[1], 18, 30, val)
12662306a36Sopenharmony_ci/* packet length in ext tpd */
12762306a36Sopenharmony_ci#define TPD_PKT_LEN_SET(tpd, val)	((tpd)->word[2] = cpu_to_le32(val))
12862306a36Sopenharmony_ci};
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci/* emac_ring_header represents a single, contiguous block of DMA space
13162306a36Sopenharmony_ci * mapped for the three descriptor rings (tpd, rfd, rrd)
13262306a36Sopenharmony_ci */
13362306a36Sopenharmony_cistruct emac_ring_header {
13462306a36Sopenharmony_ci	void			*v_addr;	/* virtual address */
13562306a36Sopenharmony_ci	dma_addr_t		dma_addr;	/* dma address */
13662306a36Sopenharmony_ci	size_t			size;		/* length in bytes */
13762306a36Sopenharmony_ci	size_t			used;
13862306a36Sopenharmony_ci};
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci/* emac_buffer is wrapper around a pointer to a socket buffer
14162306a36Sopenharmony_ci * so a DMA handle can be stored along with the skb
14262306a36Sopenharmony_ci */
14362306a36Sopenharmony_cistruct emac_buffer {
14462306a36Sopenharmony_ci	struct sk_buff		*skb;		/* socket buffer */
14562306a36Sopenharmony_ci	u16			length;		/* rx buffer length */
14662306a36Sopenharmony_ci	dma_addr_t		dma_addr;	/* dma address */
14762306a36Sopenharmony_ci};
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci/* receive free descriptor (rfd) ring */
15062306a36Sopenharmony_cistruct emac_rfd_ring {
15162306a36Sopenharmony_ci	struct emac_buffer	*rfbuff;
15262306a36Sopenharmony_ci	u32			*v_addr;	/* virtual address */
15362306a36Sopenharmony_ci	dma_addr_t		dma_addr;	/* dma address */
15462306a36Sopenharmony_ci	size_t			size;		/* length in bytes */
15562306a36Sopenharmony_ci	unsigned int		count;		/* number of desc in the ring */
15662306a36Sopenharmony_ci	unsigned int		produce_idx;
15762306a36Sopenharmony_ci	unsigned int		process_idx;
15862306a36Sopenharmony_ci	unsigned int		consume_idx;	/* unused */
15962306a36Sopenharmony_ci};
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci/* Receive Return Desciptor (RRD) ring */
16262306a36Sopenharmony_cistruct emac_rrd_ring {
16362306a36Sopenharmony_ci	u32			*v_addr;	/* virtual address */
16462306a36Sopenharmony_ci	dma_addr_t		dma_addr;	/* physical address */
16562306a36Sopenharmony_ci	size_t			size;		/* length in bytes */
16662306a36Sopenharmony_ci	unsigned int		count;		/* number of desc in the ring */
16762306a36Sopenharmony_ci	unsigned int		produce_idx;	/* unused */
16862306a36Sopenharmony_ci	unsigned int		consume_idx;
16962306a36Sopenharmony_ci};
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ci/* Rx queue */
17262306a36Sopenharmony_cistruct emac_rx_queue {
17362306a36Sopenharmony_ci	struct net_device	*netdev;	/* netdev ring belongs to */
17462306a36Sopenharmony_ci	struct emac_rrd_ring	rrd;
17562306a36Sopenharmony_ci	struct emac_rfd_ring	rfd;
17662306a36Sopenharmony_ci	struct napi_struct	napi;
17762306a36Sopenharmony_ci	struct emac_irq		*irq;
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci	u32			intr;
18062306a36Sopenharmony_ci	u32			produce_mask;
18162306a36Sopenharmony_ci	u32			process_mask;
18262306a36Sopenharmony_ci	u32			consume_mask;
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ci	u16			produce_reg;
18562306a36Sopenharmony_ci	u16			process_reg;
18662306a36Sopenharmony_ci	u16			consume_reg;
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ci	u8			produce_shift;
18962306a36Sopenharmony_ci	u8			process_shft;
19062306a36Sopenharmony_ci	u8			consume_shift;
19162306a36Sopenharmony_ci};
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ci/* Transimit Packet Descriptor (tpd) ring */
19462306a36Sopenharmony_cistruct emac_tpd_ring {
19562306a36Sopenharmony_ci	struct emac_buffer	*tpbuff;
19662306a36Sopenharmony_ci	u32			*v_addr;	/* virtual address */
19762306a36Sopenharmony_ci	dma_addr_t		dma_addr;	/* dma address */
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_ci	size_t			size;		/* length in bytes */
20062306a36Sopenharmony_ci	unsigned int		count;		/* number of desc in the ring */
20162306a36Sopenharmony_ci	unsigned int		produce_idx;
20262306a36Sopenharmony_ci	unsigned int		consume_idx;
20362306a36Sopenharmony_ci	unsigned int		last_produce_idx;
20462306a36Sopenharmony_ci};
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ci/* Tx queue */
20762306a36Sopenharmony_cistruct emac_tx_queue {
20862306a36Sopenharmony_ci	struct emac_tpd_ring	tpd;
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ci	u32			produce_mask;
21162306a36Sopenharmony_ci	u32			consume_mask;
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci	u16			max_packets;	/* max packets per interrupt */
21462306a36Sopenharmony_ci	u16			produce_reg;
21562306a36Sopenharmony_ci	u16			consume_reg;
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_ci	u8			produce_shift;
21862306a36Sopenharmony_ci	u8			consume_shift;
21962306a36Sopenharmony_ci};
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_cistruct emac_adapter;
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ciint  emac_mac_up(struct emac_adapter *adpt);
22462306a36Sopenharmony_civoid emac_mac_down(struct emac_adapter *adpt);
22562306a36Sopenharmony_civoid emac_mac_reset(struct emac_adapter *adpt);
22662306a36Sopenharmony_civoid emac_mac_stop(struct emac_adapter *adpt);
22762306a36Sopenharmony_civoid emac_mac_mode_config(struct emac_adapter *adpt);
22862306a36Sopenharmony_civoid emac_mac_rx_process(struct emac_adapter *adpt, struct emac_rx_queue *rx_q,
22962306a36Sopenharmony_ci			 int *num_pkts, int max_pkts);
23062306a36Sopenharmony_cinetdev_tx_t emac_mac_tx_buf_send(struct emac_adapter *adpt,
23162306a36Sopenharmony_ci				 struct emac_tx_queue *tx_q,
23262306a36Sopenharmony_ci				 struct sk_buff *skb);
23362306a36Sopenharmony_civoid emac_mac_tx_process(struct emac_adapter *adpt, struct emac_tx_queue *tx_q);
23462306a36Sopenharmony_civoid emac_mac_rx_tx_ring_init_all(struct platform_device *pdev,
23562306a36Sopenharmony_ci				  struct emac_adapter *adpt);
23662306a36Sopenharmony_ciint  emac_mac_rx_tx_rings_alloc_all(struct emac_adapter *adpt);
23762306a36Sopenharmony_civoid emac_mac_rx_tx_rings_free_all(struct emac_adapter *adpt);
23862306a36Sopenharmony_civoid emac_mac_multicast_addr_clear(struct emac_adapter *adpt);
23962306a36Sopenharmony_civoid emac_mac_multicast_addr_set(struct emac_adapter *adpt, u8 *addr);
24062306a36Sopenharmony_ci
24162306a36Sopenharmony_ci#endif /*_EMAC_HW_H_*/
242