Merge tag 'reset-for-v5.3' of git://git.pengutronix.de/git/pza/linux into arm/drivers
[sfrench/cifs-2.6.git] / drivers / net / ethernet / stmicro / stmmac / common.h
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*******************************************************************************
3   STMMAC Common Header File
4
5   Copyright (C) 2007-2009  STMicroelectronics Ltd
6
7
8   Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
9 *******************************************************************************/
10
11 #ifndef __COMMON_H__
12 #define __COMMON_H__
13
14 #include <linux/etherdevice.h>
15 #include <linux/netdevice.h>
16 #include <linux/stmmac.h>
17 #include <linux/phy.h>
18 #include <linux/module.h>
19 #if IS_ENABLED(CONFIG_VLAN_8021Q)
20 #define STMMAC_VLAN_TAG_USED
21 #include <linux/if_vlan.h>
22 #endif
23
24 #include "descs.h"
25 #include "hwif.h"
26 #include "mmc.h"
27
28 /* Synopsys Core versions */
29 #define DWMAC_CORE_3_40         0x34
30 #define DWMAC_CORE_3_50         0x35
31 #define DWMAC_CORE_4_00         0x40
32 #define DWMAC_CORE_4_10         0x41
33 #define DWMAC_CORE_5_00         0x50
34 #define DWMAC_CORE_5_10         0x51
35 #define DWXGMAC_CORE_2_10       0x21
36
37 #define STMMAC_CHAN0    0       /* Always supported and default for all chips */
38
39 /* These need to be power of two, and >= 4 */
40 #define DMA_TX_SIZE 512
41 #define DMA_RX_SIZE 512
42 #define STMMAC_GET_ENTRY(x, size)       ((x + 1) & (size - 1))
43
44 #undef FRAME_FILTER_DEBUG
45 /* #define FRAME_FILTER_DEBUG */
46
47 /* Extra statistic and debug information exposed by ethtool */
48 struct stmmac_extra_stats {
49         /* Transmit errors */
50         unsigned long tx_underflow ____cacheline_aligned;
51         unsigned long tx_carrier;
52         unsigned long tx_losscarrier;
53         unsigned long vlan_tag;
54         unsigned long tx_deferred;
55         unsigned long tx_vlan;
56         unsigned long tx_jabber;
57         unsigned long tx_frame_flushed;
58         unsigned long tx_payload_error;
59         unsigned long tx_ip_header_error;
60         /* Receive errors */
61         unsigned long rx_desc;
62         unsigned long sa_filter_fail;
63         unsigned long overflow_error;
64         unsigned long ipc_csum_error;
65         unsigned long rx_collision;
66         unsigned long rx_crc_errors;
67         unsigned long dribbling_bit;
68         unsigned long rx_length;
69         unsigned long rx_mii;
70         unsigned long rx_multicast;
71         unsigned long rx_gmac_overflow;
72         unsigned long rx_watchdog;
73         unsigned long da_rx_filter_fail;
74         unsigned long sa_rx_filter_fail;
75         unsigned long rx_missed_cntr;
76         unsigned long rx_overflow_cntr;
77         unsigned long rx_vlan;
78         /* Tx/Rx IRQ error info */
79         unsigned long tx_undeflow_irq;
80         unsigned long tx_process_stopped_irq;
81         unsigned long tx_jabber_irq;
82         unsigned long rx_overflow_irq;
83         unsigned long rx_buf_unav_irq;
84         unsigned long rx_process_stopped_irq;
85         unsigned long rx_watchdog_irq;
86         unsigned long tx_early_irq;
87         unsigned long fatal_bus_error_irq;
88         /* Tx/Rx IRQ Events */
89         unsigned long rx_early_irq;
90         unsigned long threshold;
91         unsigned long tx_pkt_n;
92         unsigned long rx_pkt_n;
93         unsigned long normal_irq_n;
94         unsigned long rx_normal_irq_n;
95         unsigned long napi_poll;
96         unsigned long tx_normal_irq_n;
97         unsigned long tx_clean;
98         unsigned long tx_set_ic_bit;
99         unsigned long irq_receive_pmt_irq_n;
100         /* MMC info */
101         unsigned long mmc_tx_irq_n;
102         unsigned long mmc_rx_irq_n;
103         unsigned long mmc_rx_csum_offload_irq_n;
104         /* EEE */
105         unsigned long irq_tx_path_in_lpi_mode_n;
106         unsigned long irq_tx_path_exit_lpi_mode_n;
107         unsigned long irq_rx_path_in_lpi_mode_n;
108         unsigned long irq_rx_path_exit_lpi_mode_n;
109         unsigned long phy_eee_wakeup_error_n;
110         /* Extended RDES status */
111         unsigned long ip_hdr_err;
112         unsigned long ip_payload_err;
113         unsigned long ip_csum_bypassed;
114         unsigned long ipv4_pkt_rcvd;
115         unsigned long ipv6_pkt_rcvd;
116         unsigned long no_ptp_rx_msg_type_ext;
117         unsigned long ptp_rx_msg_type_sync;
118         unsigned long ptp_rx_msg_type_follow_up;
119         unsigned long ptp_rx_msg_type_delay_req;
120         unsigned long ptp_rx_msg_type_delay_resp;
121         unsigned long ptp_rx_msg_type_pdelay_req;
122         unsigned long ptp_rx_msg_type_pdelay_resp;
123         unsigned long ptp_rx_msg_type_pdelay_follow_up;
124         unsigned long ptp_rx_msg_type_announce;
125         unsigned long ptp_rx_msg_type_management;
126         unsigned long ptp_rx_msg_pkt_reserved_type;
127         unsigned long ptp_frame_type;
128         unsigned long ptp_ver;
129         unsigned long timestamp_dropped;
130         unsigned long av_pkt_rcvd;
131         unsigned long av_tagged_pkt_rcvd;
132         unsigned long vlan_tag_priority_val;
133         unsigned long l3_filter_match;
134         unsigned long l4_filter_match;
135         unsigned long l3_l4_filter_no_match;
136         /* PCS */
137         unsigned long irq_pcs_ane_n;
138         unsigned long irq_pcs_link_n;
139         unsigned long irq_rgmii_n;
140         unsigned long pcs_link;
141         unsigned long pcs_duplex;
142         unsigned long pcs_speed;
143         /* debug register */
144         unsigned long mtl_tx_status_fifo_full;
145         unsigned long mtl_tx_fifo_not_empty;
146         unsigned long mmtl_fifo_ctrl;
147         unsigned long mtl_tx_fifo_read_ctrl_write;
148         unsigned long mtl_tx_fifo_read_ctrl_wait;
149         unsigned long mtl_tx_fifo_read_ctrl_read;
150         unsigned long mtl_tx_fifo_read_ctrl_idle;
151         unsigned long mac_tx_in_pause;
152         unsigned long mac_tx_frame_ctrl_xfer;
153         unsigned long mac_tx_frame_ctrl_idle;
154         unsigned long mac_tx_frame_ctrl_wait;
155         unsigned long mac_tx_frame_ctrl_pause;
156         unsigned long mac_gmii_tx_proto_engine;
157         unsigned long mtl_rx_fifo_fill_level_full;
158         unsigned long mtl_rx_fifo_fill_above_thresh;
159         unsigned long mtl_rx_fifo_fill_below_thresh;
160         unsigned long mtl_rx_fifo_fill_level_empty;
161         unsigned long mtl_rx_fifo_read_ctrl_flush;
162         unsigned long mtl_rx_fifo_read_ctrl_read_data;
163         unsigned long mtl_rx_fifo_read_ctrl_status;
164         unsigned long mtl_rx_fifo_read_ctrl_idle;
165         unsigned long mtl_rx_fifo_ctrl_active;
166         unsigned long mac_rx_frame_ctrl_fifo;
167         unsigned long mac_gmii_rx_proto_engine;
168         /* TSO */
169         unsigned long tx_tso_frames;
170         unsigned long tx_tso_nfrags;
171 };
172
173 /* Safety Feature statistics exposed by ethtool */
174 struct stmmac_safety_stats {
175         unsigned long mac_errors[32];
176         unsigned long mtl_errors[32];
177         unsigned long dma_errors[32];
178 };
179
180 /* Number of fields in Safety Stats */
181 #define STMMAC_SAFETY_FEAT_SIZE \
182         (sizeof(struct stmmac_safety_stats) / sizeof(unsigned long))
183
184 /* CSR Frequency Access Defines*/
185 #define CSR_F_35M       35000000
186 #define CSR_F_60M       60000000
187 #define CSR_F_100M      100000000
188 #define CSR_F_150M      150000000
189 #define CSR_F_250M      250000000
190 #define CSR_F_300M      300000000
191
192 #define MAC_CSR_H_FRQ_MASK      0x20
193
194 #define HASH_TABLE_SIZE 64
195 #define PAUSE_TIME 0xffff
196
197 /* Flow Control defines */
198 #define FLOW_OFF        0
199 #define FLOW_RX         1
200 #define FLOW_TX         2
201 #define FLOW_AUTO       (FLOW_TX | FLOW_RX)
202
203 /* PCS defines */
204 #define STMMAC_PCS_RGMII        (1 << 0)
205 #define STMMAC_PCS_SGMII        (1 << 1)
206 #define STMMAC_PCS_TBI          (1 << 2)
207 #define STMMAC_PCS_RTBI         (1 << 3)
208
209 #define SF_DMA_MODE 1           /* DMA STORE-AND-FORWARD Operation Mode */
210
211 /* DAM HW feature register fields */
212 #define DMA_HW_FEAT_MIISEL      0x00000001      /* 10/100 Mbps Support */
213 #define DMA_HW_FEAT_GMIISEL     0x00000002      /* 1000 Mbps Support */
214 #define DMA_HW_FEAT_HDSEL       0x00000004      /* Half-Duplex Support */
215 #define DMA_HW_FEAT_EXTHASHEN   0x00000008      /* Expanded DA Hash Filter */
216 #define DMA_HW_FEAT_HASHSEL     0x00000010      /* HASH Filter */
217 #define DMA_HW_FEAT_ADDMAC      0x00000020      /* Multiple MAC Addr Reg */
218 #define DMA_HW_FEAT_PCSSEL      0x00000040      /* PCS registers */
219 #define DMA_HW_FEAT_L3L4FLTREN  0x00000080      /* Layer 3 & Layer 4 Feature */
220 #define DMA_HW_FEAT_SMASEL      0x00000100      /* SMA(MDIO) Interface */
221 #define DMA_HW_FEAT_RWKSEL      0x00000200      /* PMT Remote Wakeup */
222 #define DMA_HW_FEAT_MGKSEL      0x00000400      /* PMT Magic Packet */
223 #define DMA_HW_FEAT_MMCSEL      0x00000800      /* RMON Module */
224 #define DMA_HW_FEAT_TSVER1SEL   0x00001000      /* Only IEEE 1588-2002 */
225 #define DMA_HW_FEAT_TSVER2SEL   0x00002000      /* IEEE 1588-2008 PTPv2 */
226 #define DMA_HW_FEAT_EEESEL      0x00004000      /* Energy Efficient Ethernet */
227 #define DMA_HW_FEAT_AVSEL       0x00008000      /* AV Feature */
228 #define DMA_HW_FEAT_TXCOESEL    0x00010000      /* Checksum Offload in Tx */
229 #define DMA_HW_FEAT_RXTYP1COE   0x00020000      /* IP COE (Type 1) in Rx */
230 #define DMA_HW_FEAT_RXTYP2COE   0x00040000      /* IP COE (Type 2) in Rx */
231 #define DMA_HW_FEAT_RXFIFOSIZE  0x00080000      /* Rx FIFO > 2048 Bytes */
232 #define DMA_HW_FEAT_RXCHCNT     0x00300000      /* No. additional Rx Channels */
233 #define DMA_HW_FEAT_TXCHCNT     0x00c00000      /* No. additional Tx Channels */
234 #define DMA_HW_FEAT_ENHDESSEL   0x01000000      /* Alternate Descriptor */
235 /* Timestamping with Internal System Time */
236 #define DMA_HW_FEAT_INTTSEN     0x02000000
237 #define DMA_HW_FEAT_FLEXIPPSEN  0x04000000      /* Flexible PPS Output */
238 #define DMA_HW_FEAT_SAVLANINS   0x08000000      /* Source Addr or VLAN */
239 #define DMA_HW_FEAT_ACTPHYIF    0x70000000      /* Active/selected PHY iface */
240 #define DEFAULT_DMA_PBL         8
241
242 /* PCS status and mask defines */
243 #define PCS_ANE_IRQ             BIT(2)  /* PCS Auto-Negotiation */
244 #define PCS_LINK_IRQ            BIT(1)  /* PCS Link */
245 #define PCS_RGSMIIIS_IRQ        BIT(0)  /* RGMII or SMII Interrupt */
246
247 /* Max/Min RI Watchdog Timer count value */
248 #define MAX_DMA_RIWT            0xff
249 #define MIN_DMA_RIWT            0x20
250 /* Tx coalesce parameters */
251 #define STMMAC_COAL_TX_TIMER    1000
252 #define STMMAC_MAX_COAL_TX_TICK 100000
253 #define STMMAC_TX_MAX_FRAMES    256
254 #define STMMAC_TX_FRAMES        25
255
256 /* Packets types */
257 enum packets_types {
258         PACKET_AVCPQ = 0x1, /* AV Untagged Control packets */
259         PACKET_PTPQ = 0x2, /* PTP Packets */
260         PACKET_DCBCPQ = 0x3, /* DCB Control Packets */
261         PACKET_UPQ = 0x4, /* Untagged Packets */
262         PACKET_MCBCQ = 0x5, /* Multicast & Broadcast Packets */
263 };
264
265 /* Rx IPC status */
266 enum rx_frame_status {
267         good_frame = 0x0,
268         discard_frame = 0x1,
269         csum_none = 0x2,
270         llc_snap = 0x4,
271         dma_own = 0x8,
272         rx_not_ls = 0x10,
273 };
274
275 /* Tx status */
276 enum tx_frame_status {
277         tx_done = 0x0,
278         tx_not_ls = 0x1,
279         tx_err = 0x2,
280         tx_dma_own = 0x4,
281 };
282
283 enum dma_irq_status {
284         tx_hard_error = 0x1,
285         tx_hard_error_bump_tc = 0x2,
286         handle_rx = 0x4,
287         handle_tx = 0x8,
288 };
289
290 /* EEE and LPI defines */
291 #define CORE_IRQ_TX_PATH_IN_LPI_MODE    (1 << 0)
292 #define CORE_IRQ_TX_PATH_EXIT_LPI_MODE  (1 << 1)
293 #define CORE_IRQ_RX_PATH_IN_LPI_MODE    (1 << 2)
294 #define CORE_IRQ_RX_PATH_EXIT_LPI_MODE  (1 << 3)
295
296 #define CORE_IRQ_MTL_RX_OVERFLOW        BIT(8)
297
298 /* Physical Coding Sublayer */
299 struct rgmii_adv {
300         unsigned int pause;
301         unsigned int duplex;
302         unsigned int lp_pause;
303         unsigned int lp_duplex;
304 };
305
306 #define STMMAC_PCS_PAUSE        1
307 #define STMMAC_PCS_ASYM_PAUSE   2
308
309 /* DMA HW capabilities */
310 struct dma_features {
311         unsigned int mbps_10_100;
312         unsigned int mbps_1000;
313         unsigned int half_duplex;
314         unsigned int hash_filter;
315         unsigned int multi_addr;
316         unsigned int pcs;
317         unsigned int sma_mdio;
318         unsigned int pmt_remote_wake_up;
319         unsigned int pmt_magic_frame;
320         unsigned int rmon;
321         /* IEEE 1588-2002 */
322         unsigned int time_stamp;
323         /* IEEE 1588-2008 */
324         unsigned int atime_stamp;
325         /* 802.3az - Energy-Efficient Ethernet (EEE) */
326         unsigned int eee;
327         unsigned int av;
328         unsigned int tsoen;
329         /* TX and RX csum */
330         unsigned int tx_coe;
331         unsigned int rx_coe;
332         unsigned int rx_coe_type1;
333         unsigned int rx_coe_type2;
334         unsigned int rxfifo_over_2048;
335         /* TX and RX number of channels */
336         unsigned int number_rx_channel;
337         unsigned int number_tx_channel;
338         /* TX and RX number of queues */
339         unsigned int number_rx_queues;
340         unsigned int number_tx_queues;
341         /* PPS output */
342         unsigned int pps_out_num;
343         /* Alternate (enhanced) DESC mode */
344         unsigned int enh_desc;
345         /* TX and RX FIFO sizes */
346         unsigned int tx_fifo_size;
347         unsigned int rx_fifo_size;
348         /* Automotive Safety Package */
349         unsigned int asp;
350         /* RX Parser */
351         unsigned int frpsel;
352         unsigned int frpbs;
353         unsigned int frpes;
354 };
355
356 /* GMAC TX FIFO is 8K, Rx FIFO is 16K */
357 #define BUF_SIZE_16KiB 16384
358 /* RX Buffer size must be < 8191 and multiple of 4/8/16 bytes */
359 #define BUF_SIZE_8KiB 8188
360 #define BUF_SIZE_4KiB 4096
361 #define BUF_SIZE_2KiB 2048
362
363 /* Power Down and WOL */
364 #define PMT_NOT_SUPPORTED 0
365 #define PMT_SUPPORTED 1
366
367 /* Common MAC defines */
368 #define MAC_CTRL_REG            0x00000000      /* MAC Control */
369 #define MAC_ENABLE_TX           0x00000008      /* Transmitter Enable */
370 #define MAC_ENABLE_RX           0x00000004      /* Receiver Enable */
371
372 /* Default LPI timers */
373 #define STMMAC_DEFAULT_LIT_LS   0x3E8
374 #define STMMAC_DEFAULT_TWT_LS   0x1E
375
376 #define STMMAC_CHAIN_MODE       0x1
377 #define STMMAC_RING_MODE        0x2
378
379 #define JUMBO_LEN               9000
380
381 extern const struct stmmac_desc_ops enh_desc_ops;
382 extern const struct stmmac_desc_ops ndesc_ops;
383
384 struct mac_device_info;
385
386 extern const struct stmmac_hwtimestamp stmmac_ptp;
387 extern const struct stmmac_mode_ops dwmac4_ring_mode_ops;
388
389 struct mac_link {
390         u32 speed_mask;
391         u32 speed10;
392         u32 speed100;
393         u32 speed1000;
394         u32 speed2500;
395         u32 speed10000;
396         u32 duplex;
397 };
398
399 struct mii_regs {
400         unsigned int addr;      /* MII Address */
401         unsigned int data;      /* MII Data */
402         unsigned int addr_shift;        /* MII address shift */
403         unsigned int reg_shift;         /* MII reg shift */
404         unsigned int addr_mask;         /* MII address mask */
405         unsigned int reg_mask;          /* MII reg mask */
406         unsigned int clk_csr_shift;
407         unsigned int clk_csr_mask;
408 };
409
410 struct mac_device_info {
411         const struct stmmac_ops *mac;
412         const struct stmmac_desc_ops *desc;
413         const struct stmmac_dma_ops *dma;
414         const struct stmmac_mode_ops *mode;
415         const struct stmmac_hwtimestamp *ptp;
416         const struct stmmac_tc_ops *tc;
417         struct mii_regs mii;    /* MII register Addresses */
418         struct mac_link link;
419         void __iomem *pcsr;     /* vpointer to device CSRs */
420         int multicast_filter_bins;
421         int unicast_filter_entries;
422         int mcast_bits_log2;
423         unsigned int rx_csum;
424         unsigned int pcs;
425         unsigned int pmt;
426         unsigned int ps;
427 };
428
429 struct stmmac_rx_routing {
430         u32 reg_mask;
431         u32 reg_shift;
432 };
433
434 int dwmac100_setup(struct stmmac_priv *priv);
435 int dwmac1000_setup(struct stmmac_priv *priv);
436 int dwmac4_setup(struct stmmac_priv *priv);
437 int dwxgmac2_setup(struct stmmac_priv *priv);
438
439 void stmmac_set_mac_addr(void __iomem *ioaddr, u8 addr[6],
440                          unsigned int high, unsigned int low);
441 void stmmac_get_mac_addr(void __iomem *ioaddr, unsigned char *addr,
442                          unsigned int high, unsigned int low);
443 void stmmac_set_mac(void __iomem *ioaddr, bool enable);
444
445 void stmmac_dwmac4_set_mac_addr(void __iomem *ioaddr, u8 addr[6],
446                                 unsigned int high, unsigned int low);
447 void stmmac_dwmac4_get_mac_addr(void __iomem *ioaddr, unsigned char *addr,
448                                 unsigned int high, unsigned int low);
449 void stmmac_dwmac4_set_mac(void __iomem *ioaddr, bool enable);
450
451 void dwmac_dma_flush_tx_fifo(void __iomem *ioaddr);
452
453 extern const struct stmmac_mode_ops ring_mode_ops;
454 extern const struct stmmac_mode_ops chain_mode_ops;
455 extern const struct stmmac_desc_ops dwmac4_desc_ops;
456
457 #endif /* __COMMON_H__ */