]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo/src/lwIP_Demo/lwIP_port/netif/xemacpsif_hw.c
Reorganise Zynq project after spitting lwIP example into a separate configuration.
[freertos] / FreeRTOS / Demo / CORTEX_A9_Zynq_ZC702 / RTOSDemo / src / lwIP_Demo / lwIP_port / netif / xemacpsif_hw.c
1 /*
2  * Copyright (c) 2010-2013 Xilinx, Inc.  All rights reserved.
3  *
4  * Xilinx, Inc.
5  * XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS" AS A
6  * COURTESY TO YOU.  BY PROVIDING THIS DESIGN, CODE, OR INFORMATION AS
7  * ONE POSSIBLE   IMPLEMENTATION OF THIS FEATURE, APPLICATION OR
8  * STANDARD, XILINX IS MAKING NO REPRESENTATION THAT THIS IMPLEMENTATION
9  * IS FREE FROM ANY CLAIMS OF INFRINGEMENT, AND YOU ARE RESPONSIBLE
10  * FOR OBTAINING ANY RIGHTS YOU MAY REQUIRE FOR YOUR IMPLEMENTATION.
11  * XILINX EXPRESSLY DISCLAIMS ANY WARRANTY WHATSOEVER WITH RESPECT TO
12  * THE ADEQUACY OF THE IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO
13  * ANY WARRANTIES OR REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE
14  * FROM CLAIMS OF INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY
15  * AND FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  */
18
19 #include "netif/xemacpsif.h"
20 #include "lwipopts.h"
21
22 /*** IMPORTANT: Define PEEP in xemacpsif.h and sys_arch_raw.c
23  *** to run it on a PEEP board
24  ***/
25
26 unsigned int link_speed = 100;
27
28 XEmacPs_Config *xemacps_lookup_config(unsigned mac_base)
29 {
30         extern XEmacPs_Config XEmacPs_ConfigTable[];
31         XEmacPs_Config *CfgPtr = NULL;
32         int i;
33
34         for (i = 0; i < XPAR_XEMACPS_NUM_INSTANCES; i++) {
35                 if (XEmacPs_ConfigTable[i].BaseAddress == mac_base) {
36                         CfgPtr = &XEmacPs_ConfigTable[i];
37                         break;
38                 }
39         }
40
41         return (CfgPtr);
42 }
43
44 void init_emacps(xemacpsif_s *xemacps, struct netif *netif)
45 {
46         unsigned mac_address = (unsigned)(netif->state);
47         XEmacPs *xemacpsp;
48         XEmacPs_Config *mac_config;
49         int Status = XST_SUCCESS;
50
51         /* obtain config of this emac */
52         mac_config = (XEmacPs_Config *)xemacps_lookup_config(mac_address);
53
54         /* Does not appear to be used. */
55         ( void ) mac_config;
56
57         xemacpsp = &xemacps->emacps;
58
59         /* set mac address */
60         Status = XEmacPs_SetMacAddress(xemacpsp, (void*)(netif->hwaddr), 1);
61         if (Status != XST_SUCCESS) {
62                 xil_printf("In %s:Emac Mac Address set failed...\r\n",__func__);
63         }
64         XEmacPs_SetMdioDivisor(xemacpsp, MDC_DIV_224);
65         link_speed = Phy_Setup(xemacpsp);
66         XEmacPs_SetOperatingSpeed(xemacpsp, link_speed);
67         /* Setting the operating speed of the MAC needs a delay. */
68         {
69                 volatile int wait;
70                 for (wait=0; wait < 20000; wait++);
71         }
72 }
73
74 void init_emacps_on_error (xemacpsif_s *xemacps, struct netif *netif)
75 {
76         unsigned mac_address = (unsigned)(netif->state);
77         XEmacPs *xemacpsp;
78         XEmacPs_Config *mac_config;
79         int Status = XST_SUCCESS;
80
81         /* obtain config of this emac */
82         mac_config = (XEmacPs_Config *)xemacps_lookup_config(mac_address);
83
84         /* Does not appear to be used? */
85         ( void ) mac_config;
86
87         xemacpsp = &xemacps->emacps;
88
89         /* set mac address */
90         Status = XEmacPs_SetMacAddress(xemacpsp, (void*)(netif->hwaddr), 1);
91         if (Status != XST_SUCCESS) {
92                 xil_printf("In %s:Emac Mac Address set failed...\r\n",__func__);
93         }
94
95         XEmacPs_SetOperatingSpeed(xemacpsp, link_speed);
96
97         /* Setting the operating speed of the MAC needs a delay. */
98         {
99                 volatile int wait;
100                 for (wait=0; wait < 20000; wait++);
101         }
102 }
103
104 void setup_isr (struct xemac_s *xemac)
105 {
106         xemacpsif_s   *xemacpsif;
107
108         xemacpsif = (xemacpsif_s *)(xemac->state);
109         /*
110          * Setup callbacks
111          */
112         XEmacPs_SetHandler(&xemacpsif->emacps, XEMACPS_HANDLER_DMASEND,
113                                      (void *) emacps_send_handler,
114                                      (void *) xemac);
115
116         XEmacPs_SetHandler(&xemacpsif->emacps, XEMACPS_HANDLER_DMARECV,
117                                     (void *) emacps_recv_handler,
118                                     (void *) xemac);
119
120         XEmacPs_SetHandler(&xemacpsif->emacps, XEMACPS_HANDLER_ERROR,
121                                     (void *) emacps_error_handler,
122                                     (void *) xemac);
123 }
124
125 void start_emacps (xemacpsif_s *xemacps)
126 {
127         /* start the temac */
128         XEmacPs_Start(&xemacps->emacps);
129 }
130
131 void restart_emacps_transmitter (xemacpsif_s *xemacps) {
132         u32 Reg;
133         Reg = XEmacPs_ReadReg(xemacps->emacps.Config.BaseAddress,
134                                         XEMACPS_NWCTRL_OFFSET);
135         Reg = Reg & (~XEMACPS_NWCTRL_TXEN_MASK);
136         XEmacPs_WriteReg(xemacps->emacps.Config.BaseAddress,
137                                                                                 XEMACPS_NWCTRL_OFFSET, Reg);
138
139         Reg = XEmacPs_ReadReg(xemacps->emacps.Config.BaseAddress,
140                                                 XEMACPS_NWCTRL_OFFSET);
141         Reg = Reg | (XEMACPS_NWCTRL_TXEN_MASK);
142         XEmacPs_WriteReg(xemacps->emacps.Config.BaseAddress,
143                                                                                 XEMACPS_NWCTRL_OFFSET, Reg);
144 }
145
146 void emacps_error_handler(void *arg,u8 Direction, u32 ErrorWord)
147 {
148         struct xemac_s *xemac;
149         xemacpsif_s   *xemacpsif;
150         struct xtopology_t *xtopologyp;
151         XEmacPs *xemacps;
152         XEmacPs_BdRing *rxring;
153         XEmacPs_BdRing *txring;
154
155         xemac = (struct xemac_s *)(arg);
156         xemacpsif = (xemacpsif_s *)(xemac->state);
157         rxring = &XEmacPs_GetRxRing(&xemacpsif->emacps);
158         txring = &XEmacPs_GetRxRing(&xemacpsif->emacps);
159         xtopologyp = &xtopology[xemac->topology_index];
160         xemacps = &xemacpsif->emacps;
161
162         /* Do not appear to be used. */
163         ( void ) xemacps;
164         ( void ) xtopologyp;
165
166         if (ErrorWord != 0) {
167                 switch (Direction) {
168                         case XEMACPS_RECV:
169                         if (ErrorWord & XEMACPS_RXSR_HRESPNOK_MASK) {
170                                 LWIP_DEBUGF(NETIF_DEBUG, ("Receive DMA error\r\n"));
171                                 HandleEmacPsError(xemac);
172                         }
173                         if (ErrorWord & XEMACPS_RXSR_RXOVR_MASK) {
174                                 LWIP_DEBUGF(NETIF_DEBUG, ("Receive over run\r\n"));
175                                 emacps_recv_handler(arg);
176                                 setup_rx_bds(rxring);
177                         }
178                         if (ErrorWord & XEMACPS_RXSR_BUFFNA_MASK) {
179                                 LWIP_DEBUGF(NETIF_DEBUG, ("Receive buffer not available\r\n"));
180                                 emacps_recv_handler(arg);
181                                 setup_rx_bds(rxring);
182                         }
183                         break;
184                         case XEMACPS_SEND:
185                         if (ErrorWord & XEMACPS_TXSR_HRESPNOK_MASK) {
186                                 LWIP_DEBUGF(NETIF_DEBUG, ("Transmit DMA error\r\n"));
187                                 HandleEmacPsError(xemac);
188                         }
189                         if (ErrorWord & XEMACPS_TXSR_URUN_MASK) {
190                                 LWIP_DEBUGF(NETIF_DEBUG, ("Transmit under run\r\n"));
191                                 HandleTxErrors(xemac);
192                         }
193                         if (ErrorWord & XEMACPS_TXSR_BUFEXH_MASK) {
194                                 LWIP_DEBUGF(NETIF_DEBUG, ("Transmit buffer exhausted\r\n"));
195                                 HandleTxErrors(xemac);
196                         }
197                         if (ErrorWord & XEMACPS_TXSR_RXOVR_MASK) {
198                                 LWIP_DEBUGF(NETIF_DEBUG, ("Transmit retry excessed limits\r\n"));
199                                 HandleTxErrors(xemac);
200                         }
201                         if (ErrorWord & XEMACPS_TXSR_FRAMERX_MASK) {
202                                 LWIP_DEBUGF(NETIF_DEBUG, ("Transmit collision\r\n"));
203                                 process_sent_bds(txring);
204                         }
205                         break;
206                 }
207         }
208 }