1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (c) 2014-2017 Broadcom
4  */
5 
6 #ifndef __BCMGENET_H__
7 #define __BCMGENET_H__
8 
9 #include <linux/skbuff.h>
10 #include <linux/netdevice.h>
11 #include <linux/spinlock.h>
12 #include <linux/clk.h>
13 #include <linux/mii.h>
14 #include <linux/if_vlan.h>
15 #include <linux/phy.h>
16 #include <linux/dim.h>
17 
18 /* total number of Buffer Descriptors, same for Rx/Tx */
19 #define TOTAL_DESC				256
20 
21 /* which ring is descriptor based */
22 #define DESC_INDEX				16
23 
24 /* Body(1500) + EH_SIZE(14) + VLANTAG(4) + BRCMTAG(6) + FCS(4) = 1528.
25  * 1536 is multiple of 256 bytes
26  */
27 #define ENET_BRCM_TAG_LEN	6
28 #define ENET_PAD		8
29 #define ENET_MAX_MTU_SIZE	(ETH_DATA_LEN + ETH_HLEN + VLAN_HLEN + \
30 				 ENET_BRCM_TAG_LEN + ETH_FCS_LEN + ENET_PAD)
31 #define DMA_MAX_BURST_LENGTH    0x10
32 
33 /* misc. configuration */
34 #define CLEAR_ALL_HFB			0xFF
35 #define DMA_FC_THRESH_HI		(TOTAL_DESC >> 4)
36 #define DMA_FC_THRESH_LO		5
37 
38 /* 64B receive/transmit status block */
39 struct status_64 {
40 	u32	length_status;		/* length and peripheral status */
41 	u32	ext_status;		/* Extended status*/
42 	u32	rx_csum;		/* partial rx checksum */
43 	u32	unused1[9];		/* unused */
44 	u32	tx_csum_info;		/* Tx checksum info. */
45 	u32	unused2[3];		/* unused */
46 };
47 
48 /* Rx status bits */
49 #define STATUS_RX_EXT_MASK		0x1FFFFF
50 #define STATUS_RX_CSUM_MASK		0xFFFF
51 #define STATUS_RX_CSUM_OK		0x10000
52 #define STATUS_RX_CSUM_FR		0x20000
53 #define STATUS_RX_PROTO_TCP		0
54 #define STATUS_RX_PROTO_UDP		1
55 #define STATUS_RX_PROTO_ICMP		2
56 #define STATUS_RX_PROTO_OTHER		3
57 #define STATUS_RX_PROTO_MASK		3
58 #define STATUS_RX_PROTO_SHIFT		18
59 #define STATUS_FILTER_INDEX_MASK	0xFFFF
60 /* Tx status bits */
61 #define STATUS_TX_CSUM_START_MASK	0X7FFF
62 #define STATUS_TX_CSUM_START_SHIFT	16
63 #define STATUS_TX_CSUM_PROTO_UDP	0x8000
64 #define STATUS_TX_CSUM_OFFSET_MASK	0x7FFF
65 #define STATUS_TX_CSUM_LV		0x80000000
66 
67 /* DMA Descriptor */
68 #define DMA_DESC_LENGTH_STATUS	0x00	/* in bytes of data in buffer */
69 #define DMA_DESC_ADDRESS_LO	0x04	/* lower bits of PA */
70 #define DMA_DESC_ADDRESS_HI	0x08	/* upper 32 bits of PA, GENETv4+ */
71 
72 /* Rx/Tx common counter group */
73 struct bcmgenet_pkt_counters {
74 	u32	cnt_64;		/* RO Received/Transmited 64 bytes packet */
75 	u32	cnt_127;	/* RO Rx/Tx 127 bytes packet */
76 	u32	cnt_255;	/* RO Rx/Tx 65-255 bytes packet */
77 	u32	cnt_511;	/* RO Rx/Tx 256-511 bytes packet */
78 	u32	cnt_1023;	/* RO Rx/Tx 512-1023 bytes packet */
79 	u32	cnt_1518;	/* RO Rx/Tx 1024-1518 bytes packet */
80 	u32	cnt_mgv;	/* RO Rx/Tx 1519-1522 good VLAN packet */
81 	u32	cnt_2047;	/* RO Rx/Tx 1522-2047 bytes packet*/
82 	u32	cnt_4095;	/* RO Rx/Tx 2048-4095 bytes packet*/
83 	u32	cnt_9216;	/* RO Rx/Tx 4096-9216 bytes packet*/
84 };
85 
86 /* RSV, Receive Status Vector */
87 struct bcmgenet_rx_counters {
88 	struct  bcmgenet_pkt_counters pkt_cnt;
89 	u32	pkt;		/* RO (0x428) Received pkt count*/
90 	u32	bytes;		/* RO Received byte count */
91 	u32	mca;		/* RO # of Received multicast pkt */
92 	u32	bca;		/* RO # of Receive broadcast pkt */
93 	u32	fcs;		/* RO # of Received FCS error  */
94 	u32	cf;		/* RO # of Received control frame pkt*/
95 	u32	pf;		/* RO # of Received pause frame pkt */
96 	u32	uo;		/* RO # of unknown op code pkt */
97 	u32	aln;		/* RO # of alignment error count */
98 	u32	flr;		/* RO # of frame length out of range count */
99 	u32	cde;		/* RO # of code error pkt */
100 	u32	fcr;		/* RO # of carrier sense error pkt */
101 	u32	ovr;		/* RO # of oversize pkt*/
102 	u32	jbr;		/* RO # of jabber count */
103 	u32	mtue;		/* RO # of MTU error pkt*/
104 	u32	pok;		/* RO # of Received good pkt */
105 	u32	uc;		/* RO # of unicast pkt */
106 	u32	ppp;		/* RO # of PPP pkt */
107 	u32	rcrc;		/* RO (0x470),# of CRC match pkt */
108 };
109 
110 /* TSV, Transmit Status Vector */
111 struct bcmgenet_tx_counters {
112 	struct bcmgenet_pkt_counters pkt_cnt;
113 	u32	pkts;		/* RO (0x4a8) Transmited pkt */
114 	u32	mca;		/* RO # of xmited multicast pkt */
115 	u32	bca;		/* RO # of xmited broadcast pkt */
116 	u32	pf;		/* RO # of xmited pause frame count */
117 	u32	cf;		/* RO # of xmited control frame count */
118 	u32	fcs;		/* RO # of xmited FCS error count */
119 	u32	ovr;		/* RO # of xmited oversize pkt */
120 	u32	drf;		/* RO # of xmited deferral pkt */
121 	u32	edf;		/* RO # of xmited Excessive deferral pkt*/
122 	u32	scl;		/* RO # of xmited single collision pkt */
123 	u32	mcl;		/* RO # of xmited multiple collision pkt*/
124 	u32	lcl;		/* RO # of xmited late collision pkt */
125 	u32	ecl;		/* RO # of xmited excessive collision pkt*/
126 	u32	frg;		/* RO # of xmited fragments pkt*/
127 	u32	ncl;		/* RO # of xmited total collision count */
128 	u32	jbr;		/* RO # of xmited jabber count*/
129 	u32	bytes;		/* RO # of xmited byte count */
130 	u32	pok;		/* RO # of xmited good pkt */
131 	u32	uc;		/* RO (0x0x4f0)# of xmited unitcast pkt */
132 };
133 
134 struct bcmgenet_mib_counters {
135 	struct bcmgenet_rx_counters rx;
136 	struct bcmgenet_tx_counters tx;
137 	u32	rx_runt_cnt;
138 	u32	rx_runt_fcs;
139 	u32	rx_runt_fcs_align;
140 	u32	rx_runt_bytes;
141 	u32	rbuf_ovflow_cnt;
142 	u32	rbuf_err_cnt;
143 	u32	mdf_err_cnt;
144 	u32	alloc_rx_buff_failed;
145 	u32	rx_dma_failed;
146 	u32	tx_dma_failed;
147 	u32	tx_realloc_tsb;
148 	u32	tx_realloc_tsb_failed;
149 };
150 
151 #define UMAC_HD_BKP_CTRL		0x004
152 #define	 HD_FC_EN			(1 << 0)
153 #define  HD_FC_BKOFF_OK			(1 << 1)
154 #define  IPG_CONFIG_RX_SHIFT		2
155 #define  IPG_CONFIG_RX_MASK		0x1F
156 
157 #define UMAC_CMD			0x008
158 #define  CMD_TX_EN			(1 << 0)
159 #define  CMD_RX_EN			(1 << 1)
160 #define  UMAC_SPEED_10			0
161 #define  UMAC_SPEED_100			1
162 #define  UMAC_SPEED_1000		2
163 #define  UMAC_SPEED_2500		3
164 #define  CMD_SPEED_SHIFT		2
165 #define  CMD_SPEED_MASK			3
166 #define  CMD_PROMISC			(1 << 4)
167 #define  CMD_PAD_EN			(1 << 5)
168 #define  CMD_CRC_FWD			(1 << 6)
169 #define  CMD_PAUSE_FWD			(1 << 7)
170 #define  CMD_RX_PAUSE_IGNORE		(1 << 8)
171 #define  CMD_TX_ADDR_INS		(1 << 9)
172 #define  CMD_HD_EN			(1 << 10)
173 #define  CMD_SW_RESET			(1 << 13)
174 #define  CMD_LCL_LOOP_EN		(1 << 15)
175 #define  CMD_AUTO_CONFIG		(1 << 22)
176 #define  CMD_CNTL_FRM_EN		(1 << 23)
177 #define  CMD_NO_LEN_CHK			(1 << 24)
178 #define  CMD_RMT_LOOP_EN		(1 << 25)
179 #define  CMD_PRBL_EN			(1 << 27)
180 #define  CMD_TX_PAUSE_IGNORE		(1 << 28)
181 #define  CMD_TX_RX_EN			(1 << 29)
182 #define  CMD_RUNT_FILTER_DIS		(1 << 30)
183 
184 #define UMAC_MAC0			0x00C
185 #define UMAC_MAC1			0x010
186 #define UMAC_MAX_FRAME_LEN		0x014
187 
188 #define UMAC_MODE			0x44
189 #define  MODE_LINK_STATUS		(1 << 5)
190 
191 #define UMAC_EEE_CTRL			0x064
192 #define  EN_LPI_RX_PAUSE		(1 << 0)
193 #define  EN_LPI_TX_PFC			(1 << 1)
194 #define  EN_LPI_TX_PAUSE		(1 << 2)
195 #define  EEE_EN				(1 << 3)
196 #define  RX_FIFO_CHECK			(1 << 4)
197 #define  EEE_TX_CLK_DIS			(1 << 5)
198 #define  DIS_EEE_10M			(1 << 6)
199 #define  LP_IDLE_PREDICTION_MODE	(1 << 7)
200 
201 #define UMAC_EEE_LPI_TIMER		0x068
202 #define UMAC_EEE_WAKE_TIMER		0x06C
203 #define UMAC_EEE_REF_COUNT		0x070
204 #define  EEE_REFERENCE_COUNT_MASK	0xffff
205 
206 #define UMAC_TX_FLUSH			0x334
207 
208 #define UMAC_MIB_START			0x400
209 
210 #define UMAC_MDIO_CMD			0x614
211 #define  MDIO_START_BUSY		(1 << 29)
212 #define  MDIO_READ_FAIL			(1 << 28)
213 #define  MDIO_RD			(2 << 26)
214 #define  MDIO_WR			(1 << 26)
215 #define  MDIO_PMD_SHIFT			21
216 #define  MDIO_PMD_MASK			0x1F
217 #define  MDIO_REG_SHIFT			16
218 #define  MDIO_REG_MASK			0x1F
219 
220 #define UMAC_RBUF_OVFL_CNT_V1		0x61C
221 #define RBUF_OVFL_CNT_V2		0x80
222 #define RBUF_OVFL_CNT_V3PLUS		0x94
223 
224 #define UMAC_MPD_CTRL			0x620
225 #define  MPD_EN				(1 << 0)
226 #define  MPD_PW_EN			(1 << 27)
227 #define  MPD_MSEQ_LEN_SHIFT		16
228 #define  MPD_MSEQ_LEN_MASK		0xFF
229 
230 #define UMAC_MPD_PW_MS			0x624
231 #define UMAC_MPD_PW_LS			0x628
232 #define UMAC_RBUF_ERR_CNT_V1		0x634
233 #define RBUF_ERR_CNT_V2			0x84
234 #define RBUF_ERR_CNT_V3PLUS		0x98
235 #define UMAC_MDF_ERR_CNT		0x638
236 #define UMAC_MDF_CTRL			0x650
237 #define UMAC_MDF_ADDR			0x654
238 #define UMAC_MIB_CTRL			0x580
239 #define  MIB_RESET_RX			(1 << 0)
240 #define  MIB_RESET_RUNT			(1 << 1)
241 #define  MIB_RESET_TX			(1 << 2)
242 
243 #define RBUF_CTRL			0x00
244 #define  RBUF_64B_EN			(1 << 0)
245 #define  RBUF_ALIGN_2B			(1 << 1)
246 #define  RBUF_BAD_DIS			(1 << 2)
247 
248 #define RBUF_STATUS			0x0C
249 #define  RBUF_STATUS_WOL		(1 << 0)
250 #define  RBUF_STATUS_MPD_INTR_ACTIVE	(1 << 1)
251 #define  RBUF_STATUS_ACPI_INTR_ACTIVE	(1 << 2)
252 
253 #define RBUF_CHK_CTRL			0x14
254 #define  RBUF_RXCHK_EN			(1 << 0)
255 #define  RBUF_SKIP_FCS			(1 << 4)
256 #define  RBUF_L3_PARSE_DIS		(1 << 5)
257 
258 #define RBUF_ENERGY_CTRL		0x9c
259 #define  RBUF_EEE_EN			(1 << 0)
260 #define  RBUF_PM_EN			(1 << 1)
261 
262 #define RBUF_TBUF_SIZE_CTRL		0xb4
263 
264 #define RBUF_HFB_CTRL_V1		0x38
265 #define  RBUF_HFB_FILTER_EN_SHIFT	16
266 #define  RBUF_HFB_FILTER_EN_MASK	0xffff0000
267 #define  RBUF_HFB_EN			(1 << 0)
268 #define  RBUF_HFB_256B			(1 << 1)
269 #define  RBUF_ACPI_EN			(1 << 2)
270 
271 #define RBUF_HFB_LEN_V1			0x3C
272 #define  RBUF_FLTR_LEN_MASK		0xFF
273 #define  RBUF_FLTR_LEN_SHIFT		8
274 
275 #define TBUF_CTRL			0x00
276 #define  TBUF_64B_EN			(1 << 0)
277 #define TBUF_BP_MC			0x0C
278 #define TBUF_ENERGY_CTRL		0x14
279 #define  TBUF_EEE_EN			(1 << 0)
280 #define  TBUF_PM_EN			(1 << 1)
281 
282 #define TBUF_CTRL_V1			0x80
283 #define TBUF_BP_MC_V1			0xA0
284 
285 #define HFB_CTRL			0x00
286 #define HFB_FLT_ENABLE_V3PLUS		0x04
287 #define HFB_FLT_LEN_V2			0x04
288 #define HFB_FLT_LEN_V3PLUS		0x1C
289 
290 /* uniMac intrl2 registers */
291 #define INTRL2_CPU_STAT			0x00
292 #define INTRL2_CPU_SET			0x04
293 #define INTRL2_CPU_CLEAR		0x08
294 #define INTRL2_CPU_MASK_STATUS		0x0C
295 #define INTRL2_CPU_MASK_SET		0x10
296 #define INTRL2_CPU_MASK_CLEAR		0x14
297 
298 /* INTRL2 instance 0 definitions */
299 #define UMAC_IRQ_SCB			(1 << 0)
300 #define UMAC_IRQ_EPHY			(1 << 1)
301 #define UMAC_IRQ_PHY_DET_R		(1 << 2)
302 #define UMAC_IRQ_PHY_DET_F		(1 << 3)
303 #define UMAC_IRQ_LINK_UP		(1 << 4)
304 #define UMAC_IRQ_LINK_DOWN		(1 << 5)
305 #define UMAC_IRQ_LINK_EVENT		(UMAC_IRQ_LINK_UP | UMAC_IRQ_LINK_DOWN)
306 #define UMAC_IRQ_UMAC			(1 << 6)
307 #define UMAC_IRQ_UMAC_TSV		(1 << 7)
308 #define UMAC_IRQ_TBUF_UNDERRUN		(1 << 8)
309 #define UMAC_IRQ_RBUF_OVERFLOW		(1 << 9)
310 #define UMAC_IRQ_HFB_SM			(1 << 10)
311 #define UMAC_IRQ_HFB_MM			(1 << 11)
312 #define UMAC_IRQ_MPD_R			(1 << 12)
313 #define UMAC_IRQ_RXDMA_MBDONE		(1 << 13)
314 #define UMAC_IRQ_RXDMA_PDONE		(1 << 14)
315 #define UMAC_IRQ_RXDMA_BDONE		(1 << 15)
316 #define UMAC_IRQ_RXDMA_DONE		UMAC_IRQ_RXDMA_MBDONE
317 #define UMAC_IRQ_TXDMA_MBDONE		(1 << 16)
318 #define UMAC_IRQ_TXDMA_PDONE		(1 << 17)
319 #define UMAC_IRQ_TXDMA_BDONE		(1 << 18)
320 #define UMAC_IRQ_TXDMA_DONE		UMAC_IRQ_TXDMA_MBDONE
321 
322 /* Only valid for GENETv3+ */
323 #define UMAC_IRQ_MDIO_DONE		(1 << 23)
324 #define UMAC_IRQ_MDIO_ERROR		(1 << 24)
325 
326 /* INTRL2 instance 1 definitions */
327 #define UMAC_IRQ1_TX_INTR_MASK		0xFFFF
328 #define UMAC_IRQ1_RX_INTR_MASK		0xFFFF
329 #define UMAC_IRQ1_RX_INTR_SHIFT		16
330 
331 /* Register block offsets */
332 #define GENET_SYS_OFF			0x0000
333 #define GENET_GR_BRIDGE_OFF		0x0040
334 #define GENET_EXT_OFF			0x0080
335 #define GENET_INTRL2_0_OFF		0x0200
336 #define GENET_INTRL2_1_OFF		0x0240
337 #define GENET_RBUF_OFF			0x0300
338 #define GENET_UMAC_OFF			0x0800
339 
340 /* SYS block offsets and register definitions */
341 #define SYS_REV_CTRL			0x00
342 #define SYS_PORT_CTRL			0x04
343 #define  PORT_MODE_INT_EPHY		0
344 #define  PORT_MODE_INT_GPHY		1
345 #define  PORT_MODE_EXT_EPHY		2
346 #define  PORT_MODE_EXT_GPHY		3
347 #define  PORT_MODE_EXT_RVMII_25		(4 | BIT(4))
348 #define  PORT_MODE_EXT_RVMII_50		4
349 #define  LED_ACT_SOURCE_MAC		(1 << 9)
350 
351 #define SYS_RBUF_FLUSH_CTRL		0x08
352 #define SYS_TBUF_FLUSH_CTRL		0x0C
353 #define RBUF_FLUSH_CTRL_V1		0x04
354 
355 /* Ext block register offsets and definitions */
356 #define EXT_EXT_PWR_MGMT		0x00
357 #define  EXT_PWR_DOWN_BIAS		(1 << 0)
358 #define  EXT_PWR_DOWN_DLL		(1 << 1)
359 #define  EXT_PWR_DOWN_PHY		(1 << 2)
360 #define  EXT_PWR_DN_EN_LD		(1 << 3)
361 #define  EXT_ENERGY_DET			(1 << 4)
362 #define  EXT_IDDQ_FROM_PHY		(1 << 5)
363 #define  EXT_IDDQ_GLBL_PWR		(1 << 7)
364 #define  EXT_PHY_RESET			(1 << 8)
365 #define  EXT_ENERGY_DET_MASK		(1 << 12)
366 #define  EXT_PWR_DOWN_PHY_TX		(1 << 16)
367 #define  EXT_PWR_DOWN_PHY_RX		(1 << 17)
368 #define  EXT_PWR_DOWN_PHY_SD		(1 << 18)
369 #define  EXT_PWR_DOWN_PHY_RD		(1 << 19)
370 #define  EXT_PWR_DOWN_PHY_EN		(1 << 20)
371 
372 #define EXT_RGMII_OOB_CTRL		0x0C
373 #define  RGMII_MODE_EN_V123		(1 << 0)
374 #define  RGMII_LINK			(1 << 4)
375 #define  OOB_DISABLE			(1 << 5)
376 #define  RGMII_MODE_EN			(1 << 6)
377 #define  ID_MODE_DIS			(1 << 16)
378 
379 #define EXT_GPHY_CTRL			0x1C
380 #define  EXT_CFG_IDDQ_BIAS		(1 << 0)
381 #define  EXT_CFG_PWR_DOWN		(1 << 1)
382 #define  EXT_CK25_DIS			(1 << 4)
383 #define  EXT_GPHY_RESET			(1 << 5)
384 
385 /* DMA rings size */
386 #define DMA_RING_SIZE			(0x40)
387 #define DMA_RINGS_SIZE			(DMA_RING_SIZE * (DESC_INDEX + 1))
388 
389 /* DMA registers common definitions */
390 #define DMA_RW_POINTER_MASK		0x1FF
391 #define DMA_P_INDEX_DISCARD_CNT_MASK	0xFFFF
392 #define DMA_P_INDEX_DISCARD_CNT_SHIFT	16
393 #define DMA_BUFFER_DONE_CNT_MASK	0xFFFF
394 #define DMA_BUFFER_DONE_CNT_SHIFT	16
395 #define DMA_P_INDEX_MASK		0xFFFF
396 #define DMA_C_INDEX_MASK		0xFFFF
397 
398 /* DMA ring size register */
399 #define DMA_RING_SIZE_MASK		0xFFFF
400 #define DMA_RING_SIZE_SHIFT		16
401 #define DMA_RING_BUFFER_SIZE_MASK	0xFFFF
402 
403 /* DMA interrupt threshold register */
404 #define DMA_INTR_THRESHOLD_MASK		0x01FF
405 
406 /* DMA XON/XOFF register */
407 #define DMA_XON_THREHOLD_MASK		0xFFFF
408 #define DMA_XOFF_THRESHOLD_MASK		0xFFFF
409 #define DMA_XOFF_THRESHOLD_SHIFT	16
410 
411 /* DMA flow period register */
412 #define DMA_FLOW_PERIOD_MASK		0xFFFF
413 #define DMA_MAX_PKT_SIZE_MASK		0xFFFF
414 #define DMA_MAX_PKT_SIZE_SHIFT		16
415 
416 
417 /* DMA control register */
418 #define DMA_EN				(1 << 0)
419 #define DMA_RING_BUF_EN_SHIFT		0x01
420 #define DMA_RING_BUF_EN_MASK		0xFFFF
421 #define DMA_TSB_SWAP_EN			(1 << 20)
422 
423 /* DMA status register */
424 #define DMA_DISABLED			(1 << 0)
425 #define DMA_DESC_RAM_INIT_BUSY		(1 << 1)
426 
427 /* DMA SCB burst size register */
428 #define DMA_SCB_BURST_SIZE_MASK		0x1F
429 
430 /* DMA activity vector register */
431 #define DMA_ACTIVITY_VECTOR_MASK	0x1FFFF
432 
433 /* DMA backpressure mask register */
434 #define DMA_BACKPRESSURE_MASK		0x1FFFF
435 #define DMA_PFC_ENABLE			(1 << 31)
436 
437 /* DMA backpressure status register */
438 #define DMA_BACKPRESSURE_STATUS_MASK	0x1FFFF
439 
440 /* DMA override register */
441 #define DMA_LITTLE_ENDIAN_MODE		(1 << 0)
442 #define DMA_REGISTER_MODE		(1 << 1)
443 
444 /* DMA timeout register */
445 #define DMA_TIMEOUT_MASK		0xFFFF
446 #define DMA_TIMEOUT_VAL			5000	/* micro seconds */
447 
448 /* TDMA rate limiting control register */
449 #define DMA_RATE_LIMIT_EN_MASK		0xFFFF
450 
451 /* TDMA arbitration control register */
452 #define DMA_ARBITER_MODE_MASK		0x03
453 #define DMA_RING_BUF_PRIORITY_MASK	0x1F
454 #define DMA_RING_BUF_PRIORITY_SHIFT	5
455 #define DMA_PRIO_REG_INDEX(q)		((q) / 6)
456 #define DMA_PRIO_REG_SHIFT(q)		(((q) % 6) * DMA_RING_BUF_PRIORITY_SHIFT)
457 #define DMA_RATE_ADJ_MASK		0xFF
458 
459 /* Tx/Rx Dma Descriptor common bits*/
460 #define DMA_BUFLENGTH_MASK		0x0fff
461 #define DMA_BUFLENGTH_SHIFT		16
462 #define DMA_OWN				0x8000
463 #define DMA_EOP				0x4000
464 #define DMA_SOP				0x2000
465 #define DMA_WRAP			0x1000
466 /* Tx specific Dma descriptor bits */
467 #define DMA_TX_UNDERRUN			0x0200
468 #define DMA_TX_APPEND_CRC		0x0040
469 #define DMA_TX_OW_CRC			0x0020
470 #define DMA_TX_DO_CSUM			0x0010
471 #define DMA_TX_QTAG_SHIFT		7
472 
473 /* Rx Specific Dma descriptor bits */
474 #define DMA_RX_CHK_V3PLUS		0x8000
475 #define DMA_RX_CHK_V12			0x1000
476 #define DMA_RX_BRDCAST			0x0040
477 #define DMA_RX_MULT			0x0020
478 #define DMA_RX_LG			0x0010
479 #define DMA_RX_NO			0x0008
480 #define DMA_RX_RXER			0x0004
481 #define DMA_RX_CRC_ERROR		0x0002
482 #define DMA_RX_OV			0x0001
483 #define DMA_RX_FI_MASK			0x001F
484 #define DMA_RX_FI_SHIFT			0x0007
485 #define DMA_DESC_ALLOC_MASK		0x00FF
486 
487 #define DMA_ARBITER_RR			0x00
488 #define DMA_ARBITER_WRR			0x01
489 #define DMA_ARBITER_SP			0x02
490 
491 struct enet_cb {
492 	struct sk_buff      *skb;
493 	void __iomem *bd_addr;
494 	DEFINE_DMA_UNMAP_ADDR(dma_addr);
495 	DEFINE_DMA_UNMAP_LEN(dma_len);
496 };
497 
498 /* power management mode */
499 enum bcmgenet_power_mode {
500 	GENET_POWER_CABLE_SENSE = 0,
501 	GENET_POWER_PASSIVE,
502 	GENET_POWER_WOL_MAGIC,
503 };
504 
505 struct bcmgenet_priv;
506 
507 /* We support both runtime GENET detection and compile-time
508  * to optimize code-paths for a given hardware
509  */
510 enum bcmgenet_version {
511 	GENET_V1 = 1,
512 	GENET_V2,
513 	GENET_V3,
514 	GENET_V4,
515 	GENET_V5
516 };
517 
518 #define GENET_IS_V1(p)	((p)->version == GENET_V1)
519 #define GENET_IS_V2(p)	((p)->version == GENET_V2)
520 #define GENET_IS_V3(p)	((p)->version == GENET_V3)
521 #define GENET_IS_V4(p)	((p)->version == GENET_V4)
522 #define GENET_IS_V5(p)	((p)->version == GENET_V5)
523 
524 /* Hardware flags */
525 #define GENET_HAS_40BITS	(1 << 0)
526 #define GENET_HAS_EXT		(1 << 1)
527 #define GENET_HAS_MDIO_INTR	(1 << 2)
528 #define GENET_HAS_MOCA_LINK_DET	(1 << 3)
529 
530 /* BCMGENET hardware parameters, keep this structure nicely aligned
531  * since it is going to be used in hot paths
532  */
533 struct bcmgenet_hw_params {
534 	u8		tx_queues;
535 	u8		tx_bds_per_q;
536 	u8		rx_queues;
537 	u8		rx_bds_per_q;
538 	u8		bp_in_en_shift;
539 	u32		bp_in_mask;
540 	u8		hfb_filter_cnt;
541 	u8		hfb_filter_size;
542 	u8		qtag_mask;
543 	u16		tbuf_offset;
544 	u32		hfb_offset;
545 	u32		hfb_reg_offset;
546 	u32		rdma_offset;
547 	u32		tdma_offset;
548 	u32		words_per_bd;
549 	u32		flags;
550 };
551 
552 struct bcmgenet_skb_cb {
553 	struct enet_cb *first_cb;	/* First control block of SKB */
554 	struct enet_cb *last_cb;	/* Last control block of SKB */
555 	unsigned int bytes_sent;	/* bytes on the wire (no TSB) */
556 };
557 
558 #define GENET_CB(skb)	((struct bcmgenet_skb_cb *)((skb)->cb))
559 
560 struct bcmgenet_tx_ring {
561 	spinlock_t	lock;		/* ring lock */
562 	struct napi_struct napi;	/* NAPI per tx queue */
563 	unsigned long	packets;
564 	unsigned long	bytes;
565 	unsigned int	index;		/* ring index */
566 	unsigned int	queue;		/* queue index */
567 	struct enet_cb	*cbs;		/* tx ring buffer control block*/
568 	unsigned int	size;		/* size of each tx ring */
569 	unsigned int    clean_ptr;      /* Tx ring clean pointer */
570 	unsigned int	c_index;	/* last consumer index of each ring*/
571 	unsigned int	free_bds;	/* # of free bds for each ring */
572 	unsigned int	write_ptr;	/* Tx ring write pointer SW copy */
573 	unsigned int	prod_index;	/* Tx ring producer index SW copy */
574 	unsigned int	cb_ptr;		/* Tx ring initial CB ptr */
575 	unsigned int	end_ptr;	/* Tx ring end CB ptr */
576 	void (*int_enable)(struct bcmgenet_tx_ring *);
577 	void (*int_disable)(struct bcmgenet_tx_ring *);
578 	struct bcmgenet_priv *priv;
579 };
580 
581 struct bcmgenet_net_dim {
582 	u16		use_dim;
583 	u16		event_ctr;
584 	unsigned long	packets;
585 	unsigned long	bytes;
586 	struct dim	dim;
587 };
588 
589 struct bcmgenet_rx_ring {
590 	struct napi_struct napi;	/* Rx NAPI struct */
591 	unsigned long	bytes;
592 	unsigned long	packets;
593 	unsigned long	errors;
594 	unsigned long	dropped;
595 	unsigned int	index;		/* Rx ring index */
596 	struct enet_cb	*cbs;		/* Rx ring buffer control block */
597 	unsigned int	size;		/* Rx ring size */
598 	unsigned int	c_index;	/* Rx last consumer index */
599 	unsigned int	read_ptr;	/* Rx ring read pointer */
600 	unsigned int	cb_ptr;		/* Rx ring initial CB ptr */
601 	unsigned int	end_ptr;	/* Rx ring end CB ptr */
602 	unsigned int	old_discards;
603 	struct bcmgenet_net_dim dim;
604 	u32		rx_max_coalesced_frames;
605 	u32		rx_coalesce_usecs;
606 	void (*int_enable)(struct bcmgenet_rx_ring *);
607 	void (*int_disable)(struct bcmgenet_rx_ring *);
608 	struct bcmgenet_priv *priv;
609 };
610 
611 /* device context */
612 struct bcmgenet_priv {
613 	void __iomem *base;
614 	enum bcmgenet_version version;
615 	struct net_device *dev;
616 
617 	/* transmit variables */
618 	void __iomem *tx_bds;
619 	struct enet_cb *tx_cbs;
620 	unsigned int num_tx_bds;
621 
622 	struct bcmgenet_tx_ring tx_rings[DESC_INDEX + 1];
623 
624 	/* receive variables */
625 	void __iomem *rx_bds;
626 	struct enet_cb *rx_cbs;
627 	unsigned int num_rx_bds;
628 	unsigned int rx_buf_len;
629 
630 	struct bcmgenet_rx_ring rx_rings[DESC_INDEX + 1];
631 
632 	/* other misc variables */
633 	struct bcmgenet_hw_params *hw_params;
634 
635 	/* MDIO bus variables */
636 	wait_queue_head_t wq;
637 	bool internal_phy;
638 	struct device_node *phy_dn;
639 	struct device_node *mdio_dn;
640 	struct mii_bus *mii_bus;
641 	u16 gphy_rev;
642 	struct clk *clk_eee;
643 	bool clk_eee_enabled;
644 
645 	/* PHY device variables */
646 	int old_link;
647 	int old_speed;
648 	int old_duplex;
649 	int old_pause;
650 	phy_interface_t phy_interface;
651 	int phy_addr;
652 	int ext_phy;
653 
654 	/* Interrupt variables */
655 	struct work_struct bcmgenet_irq_work;
656 	int irq0;
657 	int irq1;
658 	int wol_irq;
659 	bool wol_irq_disabled;
660 
661 	/* shared status */
662 	spinlock_t lock;
663 	unsigned int irq0_stat;
664 
665 	/* HW descriptors/checksum variables */
666 	bool crc_fwd_en;
667 
668 	u32 dma_max_burst_length;
669 
670 	u32 msg_enable;
671 
672 	struct clk *clk;
673 	struct platform_device *pdev;
674 	struct platform_device *mii_pdev;
675 
676 	/* WOL */
677 	struct clk *clk_wol;
678 	u32 wolopts;
679 
680 	struct bcmgenet_mib_counters mib;
681 
682 	struct ethtool_eee eee;
683 };
684 
685 #define GENET_IO_MACRO(name, offset)					\
686 static inline u32 bcmgenet_##name##_readl(struct bcmgenet_priv *priv,	\
687 					u32 off)			\
688 {									\
689 	/* MIPS chips strapped for BE will automagically configure the	\
690 	 * peripheral registers for CPU-native byte order.		\
691 	 */								\
692 	if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN)) \
693 		return __raw_readl(priv->base + offset + off);		\
694 	else								\
695 		return readl_relaxed(priv->base + offset + off);	\
696 }									\
697 static inline void bcmgenet_##name##_writel(struct bcmgenet_priv *priv,	\
698 					u32 val, u32 off)		\
699 {									\
700 	if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN)) \
701 		__raw_writel(val, priv->base + offset + off);		\
702 	else								\
703 		writel_relaxed(val, priv->base + offset + off);		\
704 }
705 
706 GENET_IO_MACRO(ext, GENET_EXT_OFF);
707 GENET_IO_MACRO(umac, GENET_UMAC_OFF);
708 GENET_IO_MACRO(sys, GENET_SYS_OFF);
709 
710 /* interrupt l2 registers accessors */
711 GENET_IO_MACRO(intrl2_0, GENET_INTRL2_0_OFF);
712 GENET_IO_MACRO(intrl2_1, GENET_INTRL2_1_OFF);
713 
714 /* HFB register accessors  */
715 GENET_IO_MACRO(hfb, priv->hw_params->hfb_offset);
716 
717 /* GENET v2+ HFB control and filter len helpers */
718 GENET_IO_MACRO(hfb_reg, priv->hw_params->hfb_reg_offset);
719 
720 /* RBUF register accessors */
721 GENET_IO_MACRO(rbuf, GENET_RBUF_OFF);
722 
723 /* MDIO routines */
724 int bcmgenet_mii_init(struct net_device *dev);
725 int bcmgenet_mii_config(struct net_device *dev, bool init);
726 int bcmgenet_mii_probe(struct net_device *dev);
727 void bcmgenet_mii_exit(struct net_device *dev);
728 void bcmgenet_phy_power_set(struct net_device *dev, bool enable);
729 void bcmgenet_mii_setup(struct net_device *dev);
730 
731 /* Wake-on-LAN routines */
732 void bcmgenet_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol);
733 int bcmgenet_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol);
734 int bcmgenet_wol_power_down_cfg(struct bcmgenet_priv *priv,
735 				enum bcmgenet_power_mode mode);
736 void bcmgenet_wol_power_up_cfg(struct bcmgenet_priv *priv,
737 			       enum bcmgenet_power_mode mode);
738 
739 #endif /* __BCMGENET_H__ */
740