2 * Copyright (c) 2010-2013 Xilinx, Inc. All rights reserved.
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.
19 #include "netif/xemacpsif.h"
22 /*** IMPORTANT: Define PEEP in xemacpsif.h and sys_arch_raw.c
23 *** to run it on a PEEP board
26 unsigned int link_speed = 100;
28 XEmacPs_Config *xemacps_lookup_config(unsigned mac_base)
30 extern XEmacPs_Config XEmacPs_ConfigTable[];
31 XEmacPs_Config *CfgPtr = NULL;
34 for (i = 0; i < XPAR_XEMACPS_NUM_INSTANCES; i++) {
35 if (XEmacPs_ConfigTable[i].BaseAddress == mac_base) {
36 CfgPtr = &XEmacPs_ConfigTable[i];
44 void init_emacps(xemacpsif_s *xemacps, struct netif *netif)
46 unsigned mac_address = (unsigned)(netif->state);
48 XEmacPs_Config *mac_config;
49 int Status = XST_SUCCESS;
51 /* obtain config of this emac */
52 mac_config = (XEmacPs_Config *)xemacps_lookup_config(mac_address);
54 xemacpsp = &xemacps->emacps;
57 Status = XEmacPs_SetMacAddress(xemacpsp, (void*)(netif->hwaddr), 1);
58 if (Status != XST_SUCCESS) {
59 xil_printf("In %s:Emac Mac Address set failed...\r\n",__func__);
61 XEmacPs_SetMdioDivisor(xemacpsp, MDC_DIV_224);
62 link_speed = Phy_Setup(xemacpsp);
63 XEmacPs_SetOperatingSpeed(xemacpsp, link_speed);
64 /* Setting the operating speed of the MAC needs a delay. */
67 for (wait=0; wait < 20000; wait++);
71 void init_emacps_on_error (xemacpsif_s *xemacps, struct netif *netif)
73 unsigned mac_address = (unsigned)(netif->state);
75 XEmacPs_Config *mac_config;
76 int Status = XST_SUCCESS;
78 /* obtain config of this emac */
79 mac_config = (XEmacPs_Config *)xemacps_lookup_config(mac_address);
81 xemacpsp = &xemacps->emacps;
84 Status = XEmacPs_SetMacAddress(xemacpsp, (void*)(netif->hwaddr), 1);
85 if (Status != XST_SUCCESS) {
86 xil_printf("In %s:Emac Mac Address set failed...\r\n",__func__);
89 XEmacPs_SetOperatingSpeed(xemacpsp, link_speed);
91 /* Setting the operating speed of the MAC needs a delay. */
94 for (wait=0; wait < 20000; wait++);
98 void setup_isr (struct xemac_s *xemac)
100 xemacpsif_s *xemacpsif;
102 xemacpsif = (xemacpsif_s *)(xemac->state);
106 XEmacPs_SetHandler(&xemacpsif->emacps, XEMACPS_HANDLER_DMASEND,
107 (void *) emacps_send_handler,
110 XEmacPs_SetHandler(&xemacpsif->emacps, XEMACPS_HANDLER_DMARECV,
111 (void *) emacps_recv_handler,
114 XEmacPs_SetHandler(&xemacpsif->emacps, XEMACPS_HANDLER_ERROR,
115 (void *) emacps_error_handler,
119 void start_emacps (xemacpsif_s *xemacps)
121 /* start the temac */
122 XEmacPs_Start(&xemacps->emacps);
125 void restart_emacps_transmitter (xemacpsif_s *xemacps) {
127 Reg = XEmacPs_ReadReg(xemacps->emacps.Config.BaseAddress,
128 XEMACPS_NWCTRL_OFFSET);
129 Reg = Reg & (~XEMACPS_NWCTRL_TXEN_MASK);
130 XEmacPs_WriteReg(xemacps->emacps.Config.BaseAddress,
131 XEMACPS_NWCTRL_OFFSET, 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);
140 void emacps_error_handler(void *arg,u8 Direction, u32 ErrorWord)
142 struct xemac_s *xemac;
143 xemacpsif_s *xemacpsif;
144 struct xtopology_t *xtopologyp;
146 XEmacPs_BdRing *rxring;
147 XEmacPs_BdRing *txring;
149 xemac = (struct xemac_s *)(arg);
150 xemacpsif = (xemacpsif_s *)(xemac->state);
151 rxring = &XEmacPs_GetRxRing(&xemacpsif->emacps);
152 txring = &XEmacPs_GetRxRing(&xemacpsif->emacps);
153 xtopologyp = &xtopology[xemac->topology_index];
154 xemacps = &xemacpsif->emacps;
156 if (ErrorWord != 0) {
159 if (ErrorWord & XEMACPS_RXSR_HRESPNOK_MASK) {
160 LWIP_DEBUGF(NETIF_DEBUG, ("Receive DMA error\r\n"));
161 HandleEmacPsError(xemac);
163 if (ErrorWord & XEMACPS_RXSR_RXOVR_MASK) {
164 LWIP_DEBUGF(NETIF_DEBUG, ("Receive over run\r\n"));
165 emacps_recv_handler(arg);
166 setup_rx_bds(rxring);
168 if (ErrorWord & XEMACPS_RXSR_BUFFNA_MASK) {
169 LWIP_DEBUGF(NETIF_DEBUG, ("Receive buffer not available\r\n"));
170 emacps_recv_handler(arg);
171 setup_rx_bds(rxring);
175 if (ErrorWord & XEMACPS_TXSR_HRESPNOK_MASK) {
176 LWIP_DEBUGF(NETIF_DEBUG, ("Transmit DMA error\r\n"));
177 HandleEmacPsError(xemac);
179 if (ErrorWord & XEMACPS_TXSR_URUN_MASK) {
180 LWIP_DEBUGF(NETIF_DEBUG, ("Transmit under run\r\n"));
181 HandleTxErrors(xemac);
183 if (ErrorWord & XEMACPS_TXSR_BUFEXH_MASK) {
184 LWIP_DEBUGF(NETIF_DEBUG, ("Transmit buffer exhausted\r\n"));
185 HandleTxErrors(xemac);
187 if (ErrorWord & XEMACPS_TXSR_RXOVR_MASK) {
188 LWIP_DEBUGF(NETIF_DEBUG, ("Transmit retry excessed limits\r\n"));
189 HandleTxErrors(xemac);
191 if (ErrorWord & XEMACPS_TXSR_FRAMERX_MASK) {
192 LWIP_DEBUGF(NETIF_DEBUG, ("Transmit collision\r\n"));
193 process_sent_bds(txring);