1 /*
2  * Copyright (C) 2010 - 2019 Xilinx, Inc.
3  * Copyright (C) 2021 WangHuachen.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without modification,
7  * are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice,
10  *    this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  *    this list of conditions and the following disclaimer in the documentation
13  *    and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
20  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
22  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
26  * OF SUCH DAMAGE.
27  *
28  * This file is part of the lwIP TCP/IP stack.
29  *
30  */
31 
32 #ifndef __NETIF_XEMACPSIF_H__
33 #define __NETIF_XEMACPSIF_H__
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 #include "xlwipconfig.h"
40 #include "lwip/netif.h"
41 #include "netif/etharp.h"
42 #include "lwip/sys.h"
43 #include "netif/xadapter.h"
44 
45 #include "xstatus.h"
46 #include "sleep.h"
47 #include "xparameters.h"
48 #include "xparameters_ps.h"    /* defines XPAR values */
49 #include "xil_types.h"
50 #include "xil_assert.h"
51 #include "xil_io.h"
52 // #include "xil_exception.h"
53 // #include "xpseudo_asm.h"
54 #include "xpseudo_asm_gcc.h"
55 #include "xil_cache.h"
56 #include "xil_printf.h"
57 // #include "xscugic.h"
58 #include "xemacps.h"        /* defines XEmacPs API */
59 
60 #include "netif/xpqueue.h"
61 #include "xlwipconfig.h"
62 
63 #if EL1_NONSECURE
64 #include "xil_smc.h"
65 #endif
66 
67 #define ZYNQ_EMACPS_0_BASEADDR 0xE000B000
68 #define ZYNQ_EMACPS_1_BASEADDR 0xE000C000
69 
70 #define ZYNQMP_EMACPS_0_BASEADDR 0xFF0B0000
71 #define ZYNQMP_EMACPS_1_BASEADDR 0xFF0C0000
72 #define ZYNQMP_EMACPS_2_BASEADDR 0xFF0D0000
73 #define ZYNQMP_EMACPS_3_BASEADDR 0xFF0E0000
74 
75 #define CRL_APB_GEM0_REF_CTRL    0xFF5E0050
76 #define CRL_APB_GEM1_REF_CTRL    0xFF5E0054
77 #define CRL_APB_GEM2_REF_CTRL    0xFF5E0058
78 #define CRL_APB_GEM3_REF_CTRL    0xFF5E005C
79 
80 #define CRL_APB_GEM_DIV0_MASK    0x00003F00
81 #define CRL_APB_GEM_DIV0_SHIFT    8
82 #define CRL_APB_GEM_DIV1_MASK    0x003F0000
83 #define CRL_APB_GEM_DIV1_SHIFT    16
84 
85 #define VERSAL_EMACPS_0_BASEADDR 0xFF0C0000
86 #define VERSAL_EMACPS_1_BASEADDR 0xFF0D0000
87 
88 #define VERSAL_CRL_GEM0_REF_CTRL    0xFF5E0118
89 #define VERSAL_CRL_GEM1_REF_CTRL    0xFF5E011C
90 
91 #define VERSAL_CRL_GEM_DIV_MASK        0x0003FF00
92 #define VERSAL_CRL_APB_GEM_DIV_SHIFT    8
93 
94 #if defined (ARMR5) || (__aarch64__) || (ARMA53_32) || (__MICROBLAZE__)
95 #if defined (USE_JUMBO_FRAMES)
96 #define ZYNQMP_USE_JUMBO
97 #endif
98 #endif
99 
100 #define GEM_VERSION_ZYNQMP    7
101 #define GEM_VERSION_VERSAL    0x107
102 
103 #define MAX_FRAME_SIZE_JUMBO (XEMACPS_MTU_JUMBO + XEMACPS_HDR_SIZE + XEMACPS_TRL_SIZE)
104 
105 void     xemacpsif_setmac(u32_t index, u8_t *addr);
106 u8_t*    xemacpsif_getmac(u32_t index);
107 err_t     xemacpsif_init(struct netif *netif);
108 s32_t     xemacpsif_input(struct netif *netif);
109 
110 /* xaxiemacif_hw.c */
111 void     xemacps_error_handler(XEmacPs * Temac);
112 
113 /* structure within each netif, encapsulating all information required for
114  * using a particular temac instance
115  */
116 typedef struct {
117     XEmacPs emacps;
118 
119     /* queue to store overflow packets */
120     pq_queue_t *recv_q;
121     pq_queue_t *send_q;
122 
123     /* pointers to memory holding buffer descriptors (used only with SDMA) */
124     void *rx_bdspace;
125     void *tx_bdspace;
126 
127     unsigned int last_rx_frms_cntr;
128 
129 } xemacpsif_s;
130 
131 extern xemacpsif_s xemacpsif;
132 
133 s32_t    is_tx_space_available(xemacpsif_s *emac);
134 
135 /* xemacpsif_dma.c */
136 
137 void  process_sent_bds(xemacpsif_s *xemacpsif, XEmacPs_BdRing *txring);
138 u32_t phy_setup_emacps (XEmacPs *xemacpsp, u32_t phy_addr);
139 void detect_phy(XEmacPs *xemacpsp);
140 void emacps_send_handler(void *arg);
141 XStatus emacps_sgsend(xemacpsif_s *xemacpsif, struct pbuf *p);
142 void emacps_recv_handler(void *arg);
143 void emacps_error_handler(void *arg,u8 Direction, u32 ErrorWord);
144 void setup_rx_bds(xemacpsif_s *xemacpsif, XEmacPs_BdRing *rxring);
145 void HandleTxErrors(struct xemac_s *xemac);
146 void HandleEmacPsError(struct xemac_s *xemac);
147 XEmacPs_Config *xemacps_lookup_config(unsigned mac_base);
148 void init_emacps(xemacpsif_s *xemacps, struct netif *netif);
149 void setup_isr (struct xemac_s *xemac);
150 XStatus init_dma(struct xemac_s *xemac);
151 void start_emacps (xemacpsif_s *xemacps);
152 void free_txrx_pbufs(xemacpsif_s *xemacpsif);
153 void free_onlytx_pbufs(xemacpsif_s *xemacpsif);
154 void init_emacps_on_error (xemacpsif_s *xemacps, struct netif *netif);
155 void clean_dma_txdescs(struct xemac_s *xemac);
156 void resetrx_on_no_rxdata(xemacpsif_s *xemacpsif);
157 void reset_dma(struct xemac_s *xemac);
158 
159 #ifdef __cplusplus
160 }
161 #endif
162 
163 #endif /* __NETIF_XAXIEMACIF_H__ */
164