18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef _NB8800_H_
38c2ecf20Sopenharmony_ci#define _NB8800_H_
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <linux/types.h>
68c2ecf20Sopenharmony_ci#include <linux/skbuff.h>
78c2ecf20Sopenharmony_ci#include <linux/phy.h>
88c2ecf20Sopenharmony_ci#include <linux/clk.h>
98c2ecf20Sopenharmony_ci#include <linux/bitops.h>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#define RX_DESC_COUNT			256
128c2ecf20Sopenharmony_ci#define TX_DESC_COUNT			256
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#define NB8800_DESC_LOW			4
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#define RX_BUF_SIZE			1552
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#define RX_COPYBREAK			256
198c2ecf20Sopenharmony_ci#define RX_COPYHDR			128
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#define MAX_MDC_CLOCK			2500000
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci/* Stargate Solutions SSN8800 core registers */
248c2ecf20Sopenharmony_ci#define NB8800_TX_CTL1			0x000
258c2ecf20Sopenharmony_ci#define TX_TPD				BIT(5)
268c2ecf20Sopenharmony_ci#define TX_APPEND_FCS			BIT(4)
278c2ecf20Sopenharmony_ci#define TX_PAD_EN			BIT(3)
288c2ecf20Sopenharmony_ci#define TX_RETRY_EN			BIT(2)
298c2ecf20Sopenharmony_ci#define TX_EN				BIT(0)
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define NB8800_TX_CTL2			0x001
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#define NB8800_RX_CTL			0x004
348c2ecf20Sopenharmony_ci#define RX_BC_DISABLE			BIT(7)
358c2ecf20Sopenharmony_ci#define RX_RUNT				BIT(6)
368c2ecf20Sopenharmony_ci#define RX_AF_EN			BIT(5)
378c2ecf20Sopenharmony_ci#define RX_PAUSE_EN			BIT(3)
388c2ecf20Sopenharmony_ci#define RX_SEND_CRC			BIT(2)
398c2ecf20Sopenharmony_ci#define RX_PAD_STRIP			BIT(1)
408c2ecf20Sopenharmony_ci#define RX_EN				BIT(0)
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#define NB8800_RANDOM_SEED		0x008
438c2ecf20Sopenharmony_ci#define NB8800_TX_SDP			0x14
448c2ecf20Sopenharmony_ci#define NB8800_TX_TPDP1			0x18
458c2ecf20Sopenharmony_ci#define NB8800_TX_TPDP2			0x19
468c2ecf20Sopenharmony_ci#define NB8800_SLOT_TIME		0x1c
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#define NB8800_MDIO_CMD			0x020
498c2ecf20Sopenharmony_ci#define MDIO_CMD_GO			BIT(31)
508c2ecf20Sopenharmony_ci#define MDIO_CMD_WR			BIT(26)
518c2ecf20Sopenharmony_ci#define MDIO_CMD_ADDR(x)		((x) << 21)
528c2ecf20Sopenharmony_ci#define MDIO_CMD_REG(x)			((x) << 16)
538c2ecf20Sopenharmony_ci#define MDIO_CMD_DATA(x)		((x) <<	 0)
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci#define NB8800_MDIO_STS			0x024
568c2ecf20Sopenharmony_ci#define MDIO_STS_ERR			BIT(31)
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci#define NB8800_MC_ADDR(i)		(0x028 + (i))
598c2ecf20Sopenharmony_ci#define NB8800_MC_INIT			0x02e
608c2ecf20Sopenharmony_ci#define NB8800_UC_ADDR(i)		(0x03c + (i))
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci#define NB8800_MAC_MODE			0x044
638c2ecf20Sopenharmony_ci#define RGMII_MODE			BIT(7)
648c2ecf20Sopenharmony_ci#define HALF_DUPLEX			BIT(4)
658c2ecf20Sopenharmony_ci#define BURST_EN			BIT(3)
668c2ecf20Sopenharmony_ci#define LOOPBACK_EN			BIT(2)
678c2ecf20Sopenharmony_ci#define GMAC_MODE			BIT(0)
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci#define NB8800_IC_THRESHOLD		0x050
708c2ecf20Sopenharmony_ci#define NB8800_PE_THRESHOLD		0x051
718c2ecf20Sopenharmony_ci#define NB8800_PF_THRESHOLD		0x052
728c2ecf20Sopenharmony_ci#define NB8800_TX_BUFSIZE		0x054
738c2ecf20Sopenharmony_ci#define NB8800_FIFO_CTL			0x056
748c2ecf20Sopenharmony_ci#define NB8800_PQ1			0x060
758c2ecf20Sopenharmony_ci#define NB8800_PQ2			0x061
768c2ecf20Sopenharmony_ci#define NB8800_SRC_ADDR(i)		(0x06a + (i))
778c2ecf20Sopenharmony_ci#define NB8800_STAT_DATA		0x078
788c2ecf20Sopenharmony_ci#define NB8800_STAT_INDEX		0x07c
798c2ecf20Sopenharmony_ci#define NB8800_STAT_CLEAR		0x07d
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci#define NB8800_SLEEP_MODE		0x07e
828c2ecf20Sopenharmony_ci#define SLEEP_MODE			BIT(0)
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci#define NB8800_WAKEUP			0x07f
858c2ecf20Sopenharmony_ci#define WAKEUP				BIT(0)
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci/* Aurora NB8800 host interface registers */
888c2ecf20Sopenharmony_ci#define NB8800_TXC_CR			0x100
898c2ecf20Sopenharmony_ci#define TCR_LK				BIT(12)
908c2ecf20Sopenharmony_ci#define TCR_DS				BIT(11)
918c2ecf20Sopenharmony_ci#define TCR_BTS(x)			(((x) & 0x7) << 8)
928c2ecf20Sopenharmony_ci#define TCR_DIE				BIT(7)
938c2ecf20Sopenharmony_ci#define TCR_TFI(x)			(((x) & 0x7) << 4)
948c2ecf20Sopenharmony_ci#define TCR_LE				BIT(3)
958c2ecf20Sopenharmony_ci#define TCR_RS				BIT(2)
968c2ecf20Sopenharmony_ci#define TCR_DM				BIT(1)
978c2ecf20Sopenharmony_ci#define TCR_EN				BIT(0)
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci#define NB8800_TXC_SR			0x104
1008c2ecf20Sopenharmony_ci#define TSR_DE				BIT(3)
1018c2ecf20Sopenharmony_ci#define TSR_DI				BIT(2)
1028c2ecf20Sopenharmony_ci#define TSR_TO				BIT(1)
1038c2ecf20Sopenharmony_ci#define TSR_TI				BIT(0)
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci#define NB8800_TX_SAR			0x108
1068c2ecf20Sopenharmony_ci#define NB8800_TX_DESC_ADDR		0x10c
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci#define NB8800_TX_REPORT_ADDR		0x110
1098c2ecf20Sopenharmony_ci#define TX_BYTES_TRANSFERRED(x)		(((x) >> 16) & 0xffff)
1108c2ecf20Sopenharmony_ci#define TX_FIRST_DEFERRAL		BIT(7)
1118c2ecf20Sopenharmony_ci#define TX_EARLY_COLLISIONS(x)		(((x) >> 3) & 0xf)
1128c2ecf20Sopenharmony_ci#define TX_LATE_COLLISION		BIT(2)
1138c2ecf20Sopenharmony_ci#define TX_PACKET_DROPPED		BIT(1)
1148c2ecf20Sopenharmony_ci#define TX_FIFO_UNDERRUN		BIT(0)
1158c2ecf20Sopenharmony_ci#define IS_TX_ERROR(r)			((r) & 0x07)
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci#define NB8800_TX_FIFO_SR		0x114
1188c2ecf20Sopenharmony_ci#define NB8800_TX_ITR			0x118
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci#define NB8800_RXC_CR			0x200
1218c2ecf20Sopenharmony_ci#define RCR_FL				BIT(13)
1228c2ecf20Sopenharmony_ci#define RCR_LK				BIT(12)
1238c2ecf20Sopenharmony_ci#define RCR_DS				BIT(11)
1248c2ecf20Sopenharmony_ci#define RCR_BTS(x)			(((x) & 7) << 8)
1258c2ecf20Sopenharmony_ci#define RCR_DIE				BIT(7)
1268c2ecf20Sopenharmony_ci#define RCR_RFI(x)			(((x) & 7) << 4)
1278c2ecf20Sopenharmony_ci#define RCR_LE				BIT(3)
1288c2ecf20Sopenharmony_ci#define RCR_RS				BIT(2)
1298c2ecf20Sopenharmony_ci#define RCR_DM				BIT(1)
1308c2ecf20Sopenharmony_ci#define RCR_EN				BIT(0)
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci#define NB8800_RXC_SR			0x204
1338c2ecf20Sopenharmony_ci#define RSR_DE				BIT(3)
1348c2ecf20Sopenharmony_ci#define RSR_DI				BIT(2)
1358c2ecf20Sopenharmony_ci#define RSR_RO				BIT(1)
1368c2ecf20Sopenharmony_ci#define RSR_RI				BIT(0)
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci#define NB8800_RX_SAR			0x208
1398c2ecf20Sopenharmony_ci#define NB8800_RX_DESC_ADDR		0x20c
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci#define NB8800_RX_REPORT_ADDR		0x210
1428c2ecf20Sopenharmony_ci#define RX_BYTES_TRANSFERRED(x)		(((x) >> 16) & 0xFFFF)
1438c2ecf20Sopenharmony_ci#define RX_MULTICAST_PKT		BIT(9)
1448c2ecf20Sopenharmony_ci#define RX_BROADCAST_PKT		BIT(8)
1458c2ecf20Sopenharmony_ci#define RX_LENGTH_ERR			BIT(7)
1468c2ecf20Sopenharmony_ci#define RX_FCS_ERR			BIT(6)
1478c2ecf20Sopenharmony_ci#define RX_RUNT_PKT			BIT(5)
1488c2ecf20Sopenharmony_ci#define RX_FIFO_OVERRUN			BIT(4)
1498c2ecf20Sopenharmony_ci#define RX_LATE_COLLISION		BIT(3)
1508c2ecf20Sopenharmony_ci#define RX_ALIGNMENT_ERROR		BIT(2)
1518c2ecf20Sopenharmony_ci#define RX_ERROR_MASK			0xfc
1528c2ecf20Sopenharmony_ci#define IS_RX_ERROR(r)			((r) & RX_ERROR_MASK)
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci#define NB8800_RX_FIFO_SR		0x214
1558c2ecf20Sopenharmony_ci#define NB8800_RX_ITR			0x218
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci/* Sigma Designs SMP86xx additional registers */
1588c2ecf20Sopenharmony_ci#define NB8800_TANGOX_PAD_MODE		0x400
1598c2ecf20Sopenharmony_ci#define PAD_MODE_MASK			0x7
1608c2ecf20Sopenharmony_ci#define PAD_MODE_MII			0x0
1618c2ecf20Sopenharmony_ci#define PAD_MODE_RGMII			0x1
1628c2ecf20Sopenharmony_ci#define PAD_MODE_GTX_CLK_INV		BIT(3)
1638c2ecf20Sopenharmony_ci#define PAD_MODE_GTX_CLK_DELAY		BIT(4)
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci#define NB8800_TANGOX_MDIO_CLKDIV	0x420
1668c2ecf20Sopenharmony_ci#define NB8800_TANGOX_RESET		0x424
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci/* Hardware DMA descriptor */
1698c2ecf20Sopenharmony_cistruct nb8800_dma_desc {
1708c2ecf20Sopenharmony_ci	u32				s_addr;	/* start address */
1718c2ecf20Sopenharmony_ci	u32				n_addr;	/* next descriptor address */
1728c2ecf20Sopenharmony_ci	u32				r_addr;	/* report address */
1738c2ecf20Sopenharmony_ci	u32				config;
1748c2ecf20Sopenharmony_ci} __aligned(8);
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci#define DESC_ID				BIT(23)
1778c2ecf20Sopenharmony_ci#define DESC_EOC			BIT(22)
1788c2ecf20Sopenharmony_ci#define DESC_EOF			BIT(21)
1798c2ecf20Sopenharmony_ci#define DESC_LK				BIT(20)
1808c2ecf20Sopenharmony_ci#define DESC_DS				BIT(19)
1818c2ecf20Sopenharmony_ci#define DESC_BTS(x)			(((x) & 0x7) << 16)
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci/* DMA descriptor and associated data for rx.
1848c2ecf20Sopenharmony_ci * Allocated from coherent memory.
1858c2ecf20Sopenharmony_ci */
1868c2ecf20Sopenharmony_cistruct nb8800_rx_desc {
1878c2ecf20Sopenharmony_ci	/* DMA descriptor */
1888c2ecf20Sopenharmony_ci	struct nb8800_dma_desc		desc;
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	/* Status report filled in by hardware */
1918c2ecf20Sopenharmony_ci	u32				report;
1928c2ecf20Sopenharmony_ci};
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci/* Address of buffer on rx ring */
1958c2ecf20Sopenharmony_cistruct nb8800_rx_buf {
1968c2ecf20Sopenharmony_ci	struct page			*page;
1978c2ecf20Sopenharmony_ci	unsigned long			offset;
1988c2ecf20Sopenharmony_ci};
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci/* DMA descriptors and associated data for tx.
2018c2ecf20Sopenharmony_ci * Allocated from coherent memory.
2028c2ecf20Sopenharmony_ci */
2038c2ecf20Sopenharmony_cistruct nb8800_tx_desc {
2048c2ecf20Sopenharmony_ci	/* DMA descriptor.  The second descriptor is used if packet
2058c2ecf20Sopenharmony_ci	 * data is unaligned.
2068c2ecf20Sopenharmony_ci	 */
2078c2ecf20Sopenharmony_ci	struct nb8800_dma_desc		desc[2];
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	/* Status report filled in by hardware */
2108c2ecf20Sopenharmony_ci	u32				report;
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	/* Bounce buffer for initial unaligned part of packet */
2138c2ecf20Sopenharmony_ci	u8				buf[8] __aligned(8);
2148c2ecf20Sopenharmony_ci};
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci/* Packet in tx queue */
2178c2ecf20Sopenharmony_cistruct nb8800_tx_buf {
2188c2ecf20Sopenharmony_ci	/* Currently queued skb */
2198c2ecf20Sopenharmony_ci	struct sk_buff			*skb;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	/* DMA address of the first descriptor */
2228c2ecf20Sopenharmony_ci	dma_addr_t			dma_desc;
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	/* DMA address of packet data */
2258c2ecf20Sopenharmony_ci	dma_addr_t			dma_addr;
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	/* Length of DMA mapping, less than skb->len if alignment
2288c2ecf20Sopenharmony_ci	 * buffer is used.
2298c2ecf20Sopenharmony_ci	 */
2308c2ecf20Sopenharmony_ci	unsigned int			dma_len;
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	/* Number of packets in chain starting here */
2338c2ecf20Sopenharmony_ci	unsigned int			chain_len;
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	/* Packet chain ready to be submitted to hardware */
2368c2ecf20Sopenharmony_ci	bool				ready;
2378c2ecf20Sopenharmony_ci};
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_cistruct nb8800_priv {
2408c2ecf20Sopenharmony_ci	struct napi_struct		napi;
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	void __iomem			*base;
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	/* RX DMA descriptors */
2458c2ecf20Sopenharmony_ci	struct nb8800_rx_desc		*rx_descs;
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	/* RX buffers referenced by DMA descriptors */
2488c2ecf20Sopenharmony_ci	struct nb8800_rx_buf		*rx_bufs;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	/* Current end of chain */
2518c2ecf20Sopenharmony_ci	u32				rx_eoc;
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	/* Value for rx interrupt time register in NAPI interrupt mode */
2548c2ecf20Sopenharmony_ci	u32				rx_itr_irq;
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	/* Value for rx interrupt time register in NAPI poll mode */
2578c2ecf20Sopenharmony_ci	u32				rx_itr_poll;
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	/* Value for config field of rx DMA descriptors */
2608c2ecf20Sopenharmony_ci	u32				rx_dma_config;
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	/* TX DMA descriptors */
2638c2ecf20Sopenharmony_ci	struct nb8800_tx_desc		*tx_descs;
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	/* TX packet queue */
2668c2ecf20Sopenharmony_ci	struct nb8800_tx_buf		*tx_bufs;
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci	/* Number of free tx queue entries */
2698c2ecf20Sopenharmony_ci	atomic_t			tx_free;
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci	/* First free tx queue entry */
2728c2ecf20Sopenharmony_ci	u32				tx_next;
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	/* Next buffer to transmit */
2758c2ecf20Sopenharmony_ci	u32				tx_queue;
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	/* Start of current packet chain */
2788c2ecf20Sopenharmony_ci	struct nb8800_tx_buf		*tx_chain;
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	/* Next buffer to reclaim */
2818c2ecf20Sopenharmony_ci	u32				tx_done;
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	/* Lock for DMA activation */
2848c2ecf20Sopenharmony_ci	spinlock_t			tx_lock;
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	struct mii_bus			*mii_bus;
2878c2ecf20Sopenharmony_ci	struct device_node		*phy_node;
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	/* PHY connection type from DT */
2908c2ecf20Sopenharmony_ci	phy_interface_t			phy_mode;
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	/* Current link status */
2938c2ecf20Sopenharmony_ci	int				speed;
2948c2ecf20Sopenharmony_ci	int				duplex;
2958c2ecf20Sopenharmony_ci	int				link;
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	/* Pause settings */
2988c2ecf20Sopenharmony_ci	bool				pause_aneg;
2998c2ecf20Sopenharmony_ci	bool				pause_rx;
3008c2ecf20Sopenharmony_ci	bool				pause_tx;
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	/* DMA base address of rx descriptors, see rx_descs above */
3038c2ecf20Sopenharmony_ci	dma_addr_t			rx_desc_dma;
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	/* DMA base address of tx descriptors, see tx_descs above */
3068c2ecf20Sopenharmony_ci	dma_addr_t			tx_desc_dma;
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	struct clk			*clk;
3098c2ecf20Sopenharmony_ci};
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_cistruct nb8800_ops {
3128c2ecf20Sopenharmony_ci	int				(*init)(struct net_device *dev);
3138c2ecf20Sopenharmony_ci	int				(*reset)(struct net_device *dev);
3148c2ecf20Sopenharmony_ci};
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci#endif /* _NB8800_H_ */
317