xref: /linux/drivers/net/ethernet/apm/xgene/xgene_enet_ring2.c (revision 58e16d792a6a8c6b750f637a4649967fcac853dc)
1*1ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2ed9b7da0SIyappan Subramanian /* Applied Micro X-Gene SoC Ethernet Driver
3ed9b7da0SIyappan Subramanian  *
4ed9b7da0SIyappan Subramanian  * Copyright (c) 2015, Applied Micro Circuits Corporation
5ed9b7da0SIyappan Subramanian  * Author: Iyappan Subramanian <isubramanian@apm.com>
6ed9b7da0SIyappan Subramanian  */
7ed9b7da0SIyappan Subramanian 
8ed9b7da0SIyappan Subramanian #include "xgene_enet_main.h"
9ed9b7da0SIyappan Subramanian #include "xgene_enet_hw.h"
10ed9b7da0SIyappan Subramanian #include "xgene_enet_ring2.h"
11ed9b7da0SIyappan Subramanian 
xgene_enet_ring_init(struct xgene_enet_desc_ring * ring)12ed9b7da0SIyappan Subramanian static void xgene_enet_ring_init(struct xgene_enet_desc_ring *ring)
13ed9b7da0SIyappan Subramanian {
14ed9b7da0SIyappan Subramanian 	u32 *ring_cfg = ring->state;
15ed9b7da0SIyappan Subramanian 	u64 addr = ring->dma;
16ed9b7da0SIyappan Subramanian 
17ed9b7da0SIyappan Subramanian 	if (xgene_enet_ring_owner(ring->id) == RING_OWNER_CPU) {
18ed9b7da0SIyappan Subramanian 		ring_cfg[0] |= SET_VAL(X2_INTLINE, ring->id & RING_BUFNUM_MASK);
19ed9b7da0SIyappan Subramanian 		ring_cfg[3] |= SET_BIT(X2_DEQINTEN);
20ed9b7da0SIyappan Subramanian 	}
21f126df85SIyappan Subramanian 	ring_cfg[0] |= SET_VAL(X2_CFGCRID, 2);
22ed9b7da0SIyappan Subramanian 
23ed9b7da0SIyappan Subramanian 	addr >>= 8;
24ed9b7da0SIyappan Subramanian 	ring_cfg[2] |= QCOHERENT | SET_VAL(RINGADDRL, addr);
25ed9b7da0SIyappan Subramanian 
26ed9b7da0SIyappan Subramanian 	addr >>= 27;
27ed9b7da0SIyappan Subramanian 	ring_cfg[3] |= SET_VAL(RINGSIZE, ring->cfgsize)
28ed9b7da0SIyappan Subramanian 		    | ACCEPTLERR
29ed9b7da0SIyappan Subramanian 		    | SET_VAL(RINGADDRH, addr);
30ed9b7da0SIyappan Subramanian 	ring_cfg[4] |= SET_VAL(X2_SELTHRSH, 1);
31ed9b7da0SIyappan Subramanian 	ring_cfg[5] |= SET_BIT(X2_QBASE_AM) | SET_BIT(X2_MSG_AM);
32ed9b7da0SIyappan Subramanian }
33ed9b7da0SIyappan Subramanian 
xgene_enet_ring_set_type(struct xgene_enet_desc_ring * ring)34ed9b7da0SIyappan Subramanian static void xgene_enet_ring_set_type(struct xgene_enet_desc_ring *ring)
35ed9b7da0SIyappan Subramanian {
36ed9b7da0SIyappan Subramanian 	u32 *ring_cfg = ring->state;
37ed9b7da0SIyappan Subramanian 	bool is_bufpool;
38ed9b7da0SIyappan Subramanian 	u32 val;
39ed9b7da0SIyappan Subramanian 
40ed9b7da0SIyappan Subramanian 	is_bufpool = xgene_enet_is_bufpool(ring->id);
41ed9b7da0SIyappan Subramanian 	val = (is_bufpool) ? RING_BUFPOOL : RING_REGULAR;
42ed9b7da0SIyappan Subramanian 	ring_cfg[4] |= SET_VAL(X2_RINGTYPE, val);
43ed9b7da0SIyappan Subramanian 	if (is_bufpool)
44ed9b7da0SIyappan Subramanian 		ring_cfg[3] |= SET_VAL(RINGMODE, BUFPOOL_MODE);
45ed9b7da0SIyappan Subramanian }
46ed9b7da0SIyappan Subramanian 
xgene_enet_ring_set_recombbuf(struct xgene_enet_desc_ring * ring)47ed9b7da0SIyappan Subramanian static void xgene_enet_ring_set_recombbuf(struct xgene_enet_desc_ring *ring)
48ed9b7da0SIyappan Subramanian {
49ed9b7da0SIyappan Subramanian 	u32 *ring_cfg = ring->state;
50ed9b7da0SIyappan Subramanian 
51ed9b7da0SIyappan Subramanian 	ring_cfg[3] |= RECOMBBUF;
52ed9b7da0SIyappan Subramanian 	ring_cfg[4] |= SET_VAL(X2_RECOMTIMEOUT, 0x7);
53ed9b7da0SIyappan Subramanian }
54ed9b7da0SIyappan Subramanian 
xgene_enet_ring_wr32(struct xgene_enet_desc_ring * ring,u32 offset,u32 data)55ed9b7da0SIyappan Subramanian static void xgene_enet_ring_wr32(struct xgene_enet_desc_ring *ring,
56ed9b7da0SIyappan Subramanian 				 u32 offset, u32 data)
57ed9b7da0SIyappan Subramanian {
58ed9b7da0SIyappan Subramanian 	struct xgene_enet_pdata *pdata = netdev_priv(ring->ndev);
59ed9b7da0SIyappan Subramanian 
60ed9b7da0SIyappan Subramanian 	iowrite32(data, pdata->ring_csr_addr + offset);
61ed9b7da0SIyappan Subramanian }
62ed9b7da0SIyappan Subramanian 
xgene_enet_write_ring_state(struct xgene_enet_desc_ring * ring)63ed9b7da0SIyappan Subramanian static void xgene_enet_write_ring_state(struct xgene_enet_desc_ring *ring)
64ed9b7da0SIyappan Subramanian {
65ed9b7da0SIyappan Subramanian 	struct xgene_enet_pdata *pdata = netdev_priv(ring->ndev);
66ed9b7da0SIyappan Subramanian 	int i;
67ed9b7da0SIyappan Subramanian 
68ed9b7da0SIyappan Subramanian 	xgene_enet_ring_wr32(ring, CSR_RING_CONFIG, ring->num);
69ed9b7da0SIyappan Subramanian 	for (i = 0; i < pdata->ring_ops->num_ring_config; i++) {
70ed9b7da0SIyappan Subramanian 		xgene_enet_ring_wr32(ring, CSR_RING_WR_BASE + (i * 4),
71ed9b7da0SIyappan Subramanian 				     ring->state[i]);
72ed9b7da0SIyappan Subramanian 	}
73ed9b7da0SIyappan Subramanian }
74ed9b7da0SIyappan Subramanian 
xgene_enet_clr_ring_state(struct xgene_enet_desc_ring * ring)75ed9b7da0SIyappan Subramanian static void xgene_enet_clr_ring_state(struct xgene_enet_desc_ring *ring)
76ed9b7da0SIyappan Subramanian {
77ed9b7da0SIyappan Subramanian 	memset(ring->state, 0, sizeof(ring->state));
78ed9b7da0SIyappan Subramanian 	xgene_enet_write_ring_state(ring);
79ed9b7da0SIyappan Subramanian }
80ed9b7da0SIyappan Subramanian 
xgene_enet_set_ring_state(struct xgene_enet_desc_ring * ring)81ed9b7da0SIyappan Subramanian static void xgene_enet_set_ring_state(struct xgene_enet_desc_ring *ring)
82ed9b7da0SIyappan Subramanian {
83ed9b7da0SIyappan Subramanian 	enum xgene_ring_owner owner;
84ed9b7da0SIyappan Subramanian 
85ed9b7da0SIyappan Subramanian 	xgene_enet_ring_set_type(ring);
86ed9b7da0SIyappan Subramanian 
87ed9b7da0SIyappan Subramanian 	owner = xgene_enet_ring_owner(ring->id);
88ed9b7da0SIyappan Subramanian 	if (owner == RING_OWNER_ETH0 || owner == RING_OWNER_ETH1)
89ed9b7da0SIyappan Subramanian 		xgene_enet_ring_set_recombbuf(ring);
90ed9b7da0SIyappan Subramanian 
91ed9b7da0SIyappan Subramanian 	xgene_enet_ring_init(ring);
92ed9b7da0SIyappan Subramanian 	xgene_enet_write_ring_state(ring);
93ed9b7da0SIyappan Subramanian }
94ed9b7da0SIyappan Subramanian 
xgene_enet_set_ring_id(struct xgene_enet_desc_ring * ring)95ed9b7da0SIyappan Subramanian static void xgene_enet_set_ring_id(struct xgene_enet_desc_ring *ring)
96ed9b7da0SIyappan Subramanian {
97ed9b7da0SIyappan Subramanian 	u32 ring_id_val, ring_id_buf;
98ed9b7da0SIyappan Subramanian 	bool is_bufpool;
99ed9b7da0SIyappan Subramanian 
100ed9b7da0SIyappan Subramanian 	if (xgene_enet_ring_owner(ring->id) == RING_OWNER_CPU)
101ed9b7da0SIyappan Subramanian 		return;
102ed9b7da0SIyappan Subramanian 
103ed9b7da0SIyappan Subramanian 	is_bufpool = xgene_enet_is_bufpool(ring->id);
104ed9b7da0SIyappan Subramanian 
105ed9b7da0SIyappan Subramanian 	ring_id_val = ring->id & GENMASK(9, 0);
106ed9b7da0SIyappan Subramanian 	ring_id_val |= OVERWRITE;
107ed9b7da0SIyappan Subramanian 
108ed9b7da0SIyappan Subramanian 	ring_id_buf = (ring->num << 9) & GENMASK(18, 9);
109ed9b7da0SIyappan Subramanian 	ring_id_buf |= PREFETCH_BUF_EN;
110a9380b0fSIyappan Subramanian 
111ed9b7da0SIyappan Subramanian 	if (is_bufpool)
112ed9b7da0SIyappan Subramanian 		ring_id_buf |= IS_BUFFER_POOL;
113ed9b7da0SIyappan Subramanian 
114ed9b7da0SIyappan Subramanian 	xgene_enet_ring_wr32(ring, CSR_RING_ID, ring_id_val);
115ed9b7da0SIyappan Subramanian 	xgene_enet_ring_wr32(ring, CSR_RING_ID_BUF, ring_id_buf);
116ed9b7da0SIyappan Subramanian }
117ed9b7da0SIyappan Subramanian 
xgene_enet_clr_desc_ring_id(struct xgene_enet_desc_ring * ring)118ed9b7da0SIyappan Subramanian static void xgene_enet_clr_desc_ring_id(struct xgene_enet_desc_ring *ring)
119ed9b7da0SIyappan Subramanian {
120ed9b7da0SIyappan Subramanian 	u32 ring_id;
121ed9b7da0SIyappan Subramanian 
122ed9b7da0SIyappan Subramanian 	ring_id = ring->id | OVERWRITE;
123ed9b7da0SIyappan Subramanian 	xgene_enet_ring_wr32(ring, CSR_RING_ID, ring_id);
124ed9b7da0SIyappan Subramanian 	xgene_enet_ring_wr32(ring, CSR_RING_ID_BUF, 0);
125ed9b7da0SIyappan Subramanian }
126ed9b7da0SIyappan Subramanian 
xgene_enet_setup_ring(struct xgene_enet_desc_ring * ring)127ed9b7da0SIyappan Subramanian static struct xgene_enet_desc_ring *xgene_enet_setup_ring(
128ed9b7da0SIyappan Subramanian 				    struct xgene_enet_desc_ring *ring)
129ed9b7da0SIyappan Subramanian {
130ed9b7da0SIyappan Subramanian 	bool is_bufpool;
131ed9b7da0SIyappan Subramanian 	u32 addr, i;
132ed9b7da0SIyappan Subramanian 
133ed9b7da0SIyappan Subramanian 	xgene_enet_clr_ring_state(ring);
134ed9b7da0SIyappan Subramanian 	xgene_enet_set_ring_state(ring);
135ed9b7da0SIyappan Subramanian 	xgene_enet_set_ring_id(ring);
136ed9b7da0SIyappan Subramanian 
137ed9b7da0SIyappan Subramanian 	ring->slots = xgene_enet_get_numslots(ring->id, ring->size);
138ed9b7da0SIyappan Subramanian 
139ed9b7da0SIyappan Subramanian 	is_bufpool = xgene_enet_is_bufpool(ring->id);
140ed9b7da0SIyappan Subramanian 	if (is_bufpool || xgene_enet_ring_owner(ring->id) != RING_OWNER_CPU)
141ed9b7da0SIyappan Subramanian 		return ring;
142ed9b7da0SIyappan Subramanian 
143ed9b7da0SIyappan Subramanian 	addr = CSR_VMID0_INTR_MBOX + (4 * (ring->id & RING_BUFNUM_MASK));
144ed9b7da0SIyappan Subramanian 	xgene_enet_ring_wr32(ring, addr, ring->irq_mbox_dma >> 10);
145ed9b7da0SIyappan Subramanian 
146ed9b7da0SIyappan Subramanian 	for (i = 0; i < ring->slots; i++)
147ed9b7da0SIyappan Subramanian 		xgene_enet_mark_desc_slot_empty(&ring->raw_desc[i]);
148ed9b7da0SIyappan Subramanian 
149ed9b7da0SIyappan Subramanian 	return ring;
150ed9b7da0SIyappan Subramanian }
151ed9b7da0SIyappan Subramanian 
xgene_enet_clear_ring(struct xgene_enet_desc_ring * ring)152ed9b7da0SIyappan Subramanian static void xgene_enet_clear_ring(struct xgene_enet_desc_ring *ring)
153ed9b7da0SIyappan Subramanian {
154ed9b7da0SIyappan Subramanian 	xgene_enet_clr_desc_ring_id(ring);
155ed9b7da0SIyappan Subramanian 	xgene_enet_clr_ring_state(ring);
156ed9b7da0SIyappan Subramanian }
157ed9b7da0SIyappan Subramanian 
xgene_enet_wr_cmd(struct xgene_enet_desc_ring * ring,int count)158ed9b7da0SIyappan Subramanian static void xgene_enet_wr_cmd(struct xgene_enet_desc_ring *ring, int count)
159ed9b7da0SIyappan Subramanian {
160ed9b7da0SIyappan Subramanian 	u32 data = 0;
161ed9b7da0SIyappan Subramanian 
162ed9b7da0SIyappan Subramanian 	if (xgene_enet_ring_owner(ring->id) == RING_OWNER_CPU) {
163ed9b7da0SIyappan Subramanian 		data = SET_VAL(X2_INTLINE, ring->id & RING_BUFNUM_MASK) |
164ed9b7da0SIyappan Subramanian 		       INTR_CLEAR;
165ed9b7da0SIyappan Subramanian 	}
166ed9b7da0SIyappan Subramanian 	data |= (count & GENMASK(16, 0));
167ed9b7da0SIyappan Subramanian 
168ed9b7da0SIyappan Subramanian 	iowrite32(data, ring->cmd);
169ed9b7da0SIyappan Subramanian }
170ed9b7da0SIyappan Subramanian 
xgene_enet_ring_len(struct xgene_enet_desc_ring * ring)171ed9b7da0SIyappan Subramanian static u32 xgene_enet_ring_len(struct xgene_enet_desc_ring *ring)
172ed9b7da0SIyappan Subramanian {
173ed9b7da0SIyappan Subramanian 	u32 __iomem *cmd_base = ring->cmd_base;
174ed9b7da0SIyappan Subramanian 	u32 ring_state, num_msgs;
175ed9b7da0SIyappan Subramanian 
176ed9b7da0SIyappan Subramanian 	ring_state = ioread32(&cmd_base[1]);
177ed9b7da0SIyappan Subramanian 	num_msgs = GET_VAL(X2_NUMMSGSINQ, ring_state);
178ed9b7da0SIyappan Subramanian 
179ed9b7da0SIyappan Subramanian 	return num_msgs;
180ed9b7da0SIyappan Subramanian }
181ed9b7da0SIyappan Subramanian 
xgene_enet_setup_coalescing(struct xgene_enet_desc_ring * ring)182107dec27SIyappan Subramanian static void xgene_enet_setup_coalescing(struct xgene_enet_desc_ring *ring)
183107dec27SIyappan Subramanian {
184f126df85SIyappan Subramanian 	u32 data = 0x77777777;
185107dec27SIyappan Subramanian 
186107dec27SIyappan Subramanian 	xgene_enet_ring_wr32(ring, CSR_PBM_COAL, 0x8e);
187f126df85SIyappan Subramanian 	xgene_enet_ring_wr32(ring, CSR_PBM_CTICK0, data);
188107dec27SIyappan Subramanian 	xgene_enet_ring_wr32(ring, CSR_PBM_CTICK1, data);
189f126df85SIyappan Subramanian 	xgene_enet_ring_wr32(ring, CSR_PBM_CTICK2, data);
190f126df85SIyappan Subramanian 	xgene_enet_ring_wr32(ring, CSR_PBM_CTICK3, data);
191f126df85SIyappan Subramanian 	xgene_enet_ring_wr32(ring, CSR_THRESHOLD0_SET1, 0x08);
192f126df85SIyappan Subramanian 	xgene_enet_ring_wr32(ring, CSR_THRESHOLD1_SET1, 0x10);
193107dec27SIyappan Subramanian }
194107dec27SIyappan Subramanian 
195ed9b7da0SIyappan Subramanian struct xgene_ring_ops xgene_ring2_ops = {
196ed9b7da0SIyappan Subramanian 	.num_ring_config = X2_NUM_RING_CONFIG,
197ed9b7da0SIyappan Subramanian 	.num_ring_id_shift = 13,
198ed9b7da0SIyappan Subramanian 	.setup = xgene_enet_setup_ring,
199ed9b7da0SIyappan Subramanian 	.clear = xgene_enet_clear_ring,
200ed9b7da0SIyappan Subramanian 	.wr_cmd = xgene_enet_wr_cmd,
201ed9b7da0SIyappan Subramanian 	.len = xgene_enet_ring_len,
202107dec27SIyappan Subramanian 	.coalesce = xgene_enet_setup_coalescing,
203ed9b7da0SIyappan Subramanian };
204