1 /******************************************************************************
3 * Copyright (C) 2010 - 2015 Xilinx, Inc. All rights reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * Use of the Software is limited solely to applications:
16 * (a) running on a Xilinx device, or
17 * (b) that interact with a Xilinx device through a bus or interconnect.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * XILINX BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
24 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * Except as contained in this notice, the name of the Xilinx shall not be used
28 * in advertising or otherwise to promote the sale, use or other dealings in
29 * this Software without prior written authorization from Xilinx.
31 ******************************************************************************/
32 /*****************************************************************************/
37 * The XEmacPs driver. Functions in this file are the minimum required functions
38 * for this driver. See xemacps.h for a detailed description of the driver.
41 * MODIFICATION HISTORY:
43 * Ver Who Date Changes
44 * ----- ---- -------- -------------------------------------------------------
45 * 1.00a wsy 01/10/10 First release
46 * 2.1 srt 07/15/14 Add support for Zynq Ultrascale Mp GEM specification and
48 * 3.00 kvn 02/13/15 Modified code for MISRA-C:2012 compliance.
49 * 3.0 hk 02/20/15 Added support for jumbo frames. Increase AHB burst.
50 * Disable extended mode. Perform all 64 bit changes under
54 ******************************************************************************/
56 /***************************** Include Files *********************************/
60 /************************** Constant Definitions *****************************/
63 /**************************** Type Definitions *******************************/
66 /***************** Macros (Inline Functions) Definitions *********************/
69 /************************** Function Prototypes ******************************/
71 void XEmacPs_StubHandler(void); /* Default handler routine */
73 /************************** Variable Definitions *****************************/
76 /*****************************************************************************/
78 * Initialize a specific XEmacPs instance/driver. The initialization entails:
79 * - Initialize fields of the XEmacPs instance structure
80 * - Reset hardware and apply default options
81 * - Configure the DMA channels
83 * The PHY is setup independently from the device. Use the MII or whatever other
84 * interface may be present for setup.
86 * @param InstancePtr is a pointer to the instance to be worked on.
87 * @param CfgPtr is the device configuration structure containing required
88 * hardware build data.
89 * @param EffectiveAddress is the base address of the device. If address
90 * translation is not utilized, this parameter can be passed in using
91 * CfgPtr->Config.BaseAddress to specify the physical base address.
94 * - XST_SUCCESS if initialization was successful
96 ******************************************************************************/
97 LONG XEmacPs_CfgInitialize(XEmacPs *InstancePtr, XEmacPs_Config * CfgPtr,
98 UINTPTR EffectiveAddress)
100 /* Verify arguments */
101 Xil_AssertNonvoid(InstancePtr != NULL);
102 Xil_AssertNonvoid(CfgPtr != NULL);
104 /* Set device base address and ID */
105 InstancePtr->Config.DeviceId = CfgPtr->DeviceId;
106 InstancePtr->Config.BaseAddress = EffectiveAddress;
108 /* Set callbacks to an initial stub routine */
109 InstancePtr->SendHandler = ((XEmacPs_Handler)((void*)XEmacPs_StubHandler));
110 InstancePtr->RecvHandler = ((XEmacPs_Handler)(void*)XEmacPs_StubHandler);
111 InstancePtr->ErrorHandler = ((XEmacPs_ErrHandler)(void*)XEmacPs_StubHandler);
113 /* Reset the hardware and set default options */
114 InstancePtr->IsReady = XIL_COMPONENT_IS_READY;
115 XEmacPs_Reset(InstancePtr);
117 return (LONG)(XST_SUCCESS);
121 /*****************************************************************************/
123 * Start the Ethernet controller as follows:
124 * - Enable transmitter if XTE_TRANSMIT_ENABLE_OPTION is set
125 * - Enable receiver if XTE_RECEIVER_ENABLE_OPTION is set
126 * - Start the SG DMA send and receive channels and enable the device
129 * @param InstancePtr is a pointer to the instance to be worked on.
134 * Hardware is configured with scatter-gather DMA, the driver expects to start
135 * the scatter-gather channels and expects that the user has previously set up
136 * the buffer descriptor lists.
138 * This function makes use of internal resources that are shared between the
139 * Start, Stop, and Set/ClearOptions functions. So if one task might be setting
140 * device options while another is trying to start the device, the user is
141 * required to provide protection of this shared data (typically using a
144 * This function must not be preempted by an interrupt that may service the
147 ******************************************************************************/
148 void XEmacPs_Start(XEmacPs *InstancePtr)
152 /* Assert bad arguments and conditions */
153 Xil_AssertVoid(InstancePtr != NULL);
154 Xil_AssertVoid(InstancePtr->IsReady == (u32)XIL_COMPONENT_IS_READY);
157 /* When starting the DMA channels, both transmit and receive sides
158 * need an initialized BD list.
160 if (InstancePtr->Version == 2) {
161 Xil_AssertVoid(InstancePtr->RxBdRing.BaseBdAddr != 0);
162 Xil_AssertVoid(InstancePtr->TxBdRing.BaseBdAddr != 0);
163 XEmacPs_WriteReg(InstancePtr->Config.BaseAddress,
164 XEMACPS_RXQBASE_OFFSET,
165 InstancePtr->RxBdRing.BaseBdAddr);
167 XEmacPs_WriteReg(InstancePtr->Config.BaseAddress,
168 XEMACPS_TXQBASE_OFFSET,
169 InstancePtr->TxBdRing.BaseBdAddr);
172 /* clear any existed int status */
173 XEmacPs_WriteReg(InstancePtr->Config.BaseAddress, XEMACPS_ISR_OFFSET,
174 XEMACPS_IXR_ALL_MASK);
176 /* Enable transmitter if not already enabled */
177 if ((InstancePtr->Options & (u32)XEMACPS_TRANSMITTER_ENABLE_OPTION)!=0x00000000U) {
178 Reg = XEmacPs_ReadReg(InstancePtr->Config.BaseAddress,
179 XEMACPS_NWCTRL_OFFSET);
180 if ((!(Reg & XEMACPS_NWCTRL_TXEN_MASK))==TRUE) {
181 XEmacPs_WriteReg(InstancePtr->Config.BaseAddress,
182 XEMACPS_NWCTRL_OFFSET,
183 Reg | (u32)XEMACPS_NWCTRL_TXEN_MASK);
187 /* Enable receiver if not already enabled */
188 if ((InstancePtr->Options & XEMACPS_RECEIVER_ENABLE_OPTION) != 0x00000000U) {
189 Reg = XEmacPs_ReadReg(InstancePtr->Config.BaseAddress,
190 XEMACPS_NWCTRL_OFFSET);
191 if ((!(Reg & XEMACPS_NWCTRL_RXEN_MASK))==TRUE) {
192 XEmacPs_WriteReg(InstancePtr->Config.BaseAddress,
193 XEMACPS_NWCTRL_OFFSET,
194 Reg | (u32)XEMACPS_NWCTRL_RXEN_MASK);
198 /* Enable TX and RX interrupts */
199 XEmacPs_IntEnable(InstancePtr, (XEMACPS_IXR_TX_ERR_MASK |
200 XEMACPS_IXR_RX_ERR_MASK | (u32)XEMACPS_IXR_FRAMERX_MASK |
201 (u32)XEMACPS_IXR_TXCOMPL_MASK));
203 /* Enable TX Q1 Interrupts */
204 if (InstancePtr->Version > 2)
205 XEmacPs_IntQ1Enable(InstancePtr, XEMACPS_INTQ1_IXR_ALL_MASK);
207 /* Mark as started */
208 InstancePtr->IsStarted = XIL_COMPONENT_IS_STARTED;
214 /*****************************************************************************/
216 * Gracefully stop the Ethernet MAC as follows:
217 * - Disable all interrupts from this device
218 * - Stop DMA channels
219 * - Disable the tansmitter and receiver
221 * Device options currently in effect are not changed.
223 * This function will disable all interrupts. Default interrupts settings that
224 * had been enabled will be restored when XEmacPs_Start() is called.
226 * @param InstancePtr is a pointer to the instance to be worked on.
229 * This function makes use of internal resources that are shared between the
230 * Start, Stop, SetOptions, and ClearOptions functions. So if one task might be
231 * setting device options while another is trying to start the device, the user
232 * is required to provide protection of this shared data (typically using a
235 * Stopping the DMA channels causes this function to block until the DMA
236 * operation is complete.
238 ******************************************************************************/
239 void XEmacPs_Stop(XEmacPs *InstancePtr)
243 Xil_AssertVoid(InstancePtr != NULL);
244 Xil_AssertVoid(InstancePtr->IsReady == (u32)XIL_COMPONENT_IS_READY);
246 /* Disable all interrupts */
247 XEmacPs_WriteReg(InstancePtr->Config.BaseAddress, XEMACPS_IDR_OFFSET,
248 XEMACPS_IXR_ALL_MASK);
250 /* Disable the receiver & transmitter */
251 Reg = XEmacPs_ReadReg(InstancePtr->Config.BaseAddress,
252 XEMACPS_NWCTRL_OFFSET);
253 Reg &= (u32)(~XEMACPS_NWCTRL_RXEN_MASK);
254 Reg &= (u32)(~XEMACPS_NWCTRL_TXEN_MASK);
255 XEmacPs_WriteReg(InstancePtr->Config.BaseAddress,
256 XEMACPS_NWCTRL_OFFSET, Reg);
258 /* Mark as stopped */
259 InstancePtr->IsStarted = 0U;
263 /*****************************************************************************/
265 * Perform a graceful reset of the Ethernet MAC. Resets the DMA channels, the
266 * transmitter, and the receiver.
269 * - Stops transmit and receive channels
271 * - Configure transmit and receive buffer size to default
272 * - Clear transmit and receive status register and counters
273 * - Clear all interrupt sources
274 * - Clear phy (if there is any previously detected) address
275 * - Clear MAC addresses (1-4) as well as Type IDs and hash value
277 * All options are placed in their default state. Any frames in the
278 * descriptor lists will remain in the lists. The side effect of doing
279 * this is that after a reset and following a restart of the device, frames
280 * were in the list before the reset may be transmitted or received.
282 * The upper layer software is responsible for re-configuring (if necessary)
283 * and restarting the MAC after the reset. Note also that driver statistics
284 * are not cleared on reset. It is up to the upper layer software to clear the
285 * statistics if needed.
287 * When a reset is required, the driver notifies the upper layer software of
288 * this need through the ErrorHandler callback and specific status codes.
289 * The upper layer software is responsible for calling this Reset function
290 * and then re-configuring the device.
292 * @param InstancePtr is a pointer to the instance to be worked on.
294 ******************************************************************************/
295 void XEmacPs_Reset(XEmacPs *InstancePtr)
299 s8 EmacPs_zero_MAC[6] = { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
301 Xil_AssertVoid(InstancePtr != NULL);
302 Xil_AssertVoid(InstancePtr->IsReady == (u32)XIL_COMPONENT_IS_READY);
304 /* Stop the device and reset hardware */
305 XEmacPs_Stop(InstancePtr);
306 InstancePtr->Options = XEMACPS_DEFAULT_OPTIONS;
308 InstancePtr->Version = XEmacPs_ReadReg(InstancePtr->Config.BaseAddress, 0xFC);
310 InstancePtr->Version = (InstancePtr->Version >> 16) & 0xFFF;
312 InstancePtr->MaxMtuSize = XEMACPS_MTU;
313 InstancePtr->MaxFrameSize = XEMACPS_MTU + XEMACPS_HDR_SIZE +
315 InstancePtr->MaxVlanFrameSize = InstancePtr->MaxFrameSize +
316 XEMACPS_HDR_VLAN_SIZE;
317 InstancePtr->RxBufMask = XEMACPS_RXBUF_LEN_MASK;
319 /* Setup hardware with default values */
320 XEmacPs_WriteReg(InstancePtr->Config.BaseAddress,
321 XEMACPS_NWCTRL_OFFSET,
322 (XEMACPS_NWCTRL_STATCLR_MASK |
323 XEMACPS_NWCTRL_MDEN_MASK) &
324 (u32)(~XEMACPS_NWCTRL_LOOPEN_MASK));
326 XEmacPs_WriteReg(InstancePtr->Config.BaseAddress,
327 XEMACPS_NWCFG_OFFSET,
328 ((u32)XEMACPS_NWCFG_100_MASK |
329 (u32)XEMACPS_NWCFG_FDEN_MASK |
330 (u32)XEMACPS_NWCFG_UCASTHASHEN_MASK));
331 if (InstancePtr->Version > 2) {
332 XEmacPs_WriteReg(InstancePtr->Config.BaseAddress, XEMACPS_NWCFG_OFFSET,
333 (XEmacPs_ReadReg(InstancePtr->Config.BaseAddress, XEMACPS_NWCFG_OFFSET) |
334 XEMACPS_NWCFG_DWIDTH_64_MASK));
337 XEmacPs_WriteReg(InstancePtr->Config.BaseAddress,
338 XEMACPS_DMACR_OFFSET,
339 (((((u32)XEMACPS_RX_BUF_SIZE / (u32)XEMACPS_RX_BUF_UNIT) +
340 (((((u32)XEMACPS_RX_BUF_SIZE %
341 (u32)XEMACPS_RX_BUF_UNIT))!=(u32)0) ? 1U : 0U)) <<
342 (u32)(XEMACPS_DMACR_RXBUF_SHIFT)) &
343 (u32)(XEMACPS_DMACR_RXBUF_MASK)) |
344 (u32)XEMACPS_DMACR_RXSIZE_MASK |
345 (u32)XEMACPS_DMACR_TXSIZE_MASK);
349 /* FIXME: Why Single bursts? */
350 if (InstancePtr->Version > 2) {
351 XEmacPs_WriteReg(InstancePtr->Config.BaseAddress, XEMACPS_DMACR_OFFSET,
352 (XEmacPs_ReadReg(InstancePtr->Config.BaseAddress, XEMACPS_DMACR_OFFSET) |
354 (u32)XEMACPS_DMACR_ADDR_WIDTH_64 |
356 (u32)XEMACPS_DMACR_INCR16_AHB_BURST));
359 XEmacPs_WriteReg(InstancePtr->Config.BaseAddress,
360 XEMACPS_TXSR_OFFSET, 0x0U);
362 XEmacPs_SetQueuePtr(InstancePtr, 0, 0x00U, (u16)XEMACPS_SEND);
363 if (InstancePtr->Version > 2)
364 XEmacPs_SetQueuePtr(InstancePtr, 0, 0x01U, (u16)XEMACPS_SEND);
365 XEmacPs_SetQueuePtr(InstancePtr, 0, 0x00U, (u16)XEMACPS_RECV);
367 XEmacPs_WriteReg(InstancePtr->Config.BaseAddress,
368 XEMACPS_RXSR_OFFSET, 0x0U);
370 XEmacPs_WriteReg(InstancePtr->Config.BaseAddress, XEMACPS_IDR_OFFSET,
371 XEMACPS_IXR_ALL_MASK);
373 Reg = XEmacPs_ReadReg(InstancePtr->Config.BaseAddress,
375 XEmacPs_WriteReg(InstancePtr->Config.BaseAddress, XEMACPS_ISR_OFFSET,
378 XEmacPs_WriteReg(InstancePtr->Config.BaseAddress,
379 XEMACPS_PHYMNTNC_OFFSET, 0x0U);
381 XEmacPs_ClearHash(InstancePtr);
383 for (i = 1U; i < 5U; i++) {
384 (void)XEmacPs_SetMacAddress(InstancePtr, EmacPs_zero_MAC, i);
385 (void)XEmacPs_SetTypeIdCheck(InstancePtr, 0x00000000U, i);
388 /* clear all counters */
389 for (i = 0U; i < (u8)((XEMACPS_LAST_OFFSET - XEMACPS_OCTTXL_OFFSET) / 4U);
391 (void)XEmacPs_ReadReg(InstancePtr->Config.BaseAddress,
392 XEMACPS_OCTTXL_OFFSET + (u32)(((u32)i) * ((u32)4)));
395 /* Disable the receiver */
396 Reg = XEmacPs_ReadReg(InstancePtr->Config.BaseAddress,
397 XEMACPS_NWCTRL_OFFSET);
398 Reg &= (u32)(~XEMACPS_NWCTRL_RXEN_MASK);
399 XEmacPs_WriteReg(InstancePtr->Config.BaseAddress,
400 XEMACPS_NWCTRL_OFFSET, Reg);
402 /* Sync default options with hardware but leave receiver and
403 * transmitter disabled. They get enabled with XEmacPs_Start() if
404 * XEMACPS_TRANSMITTER_ENABLE_OPTION and
405 * XEMACPS_RECEIVER_ENABLE_OPTION are set.
407 (void)XEmacPs_SetOptions(InstancePtr, InstancePtr->Options &
408 ~((u32)XEMACPS_TRANSMITTER_ENABLE_OPTION |
409 (u32)XEMACPS_RECEIVER_ENABLE_OPTION));
411 (void)XEmacPs_ClearOptions(InstancePtr, ~InstancePtr->Options);
415 /******************************************************************************/
417 * This is a stub for the asynchronous callbacks. The stub is here in case the
418 * upper layer forgot to set the handler(s). On initialization, all handlers are
419 * set to this callback. It is considered an error for this handler to be
422 ******************************************************************************/
423 void XEmacPs_StubHandler(void)
425 Xil_AssertVoidAlways();
428 /*****************************************************************************/
430 * This function sets the start address of the transmit/receive buffer queue.
432 * @param InstancePtr is a pointer to the instance to be worked on.
433 * @QPtr Address of the Queue to be written
434 * @QueueNum Buffer Queue Index
435 * @Direction Transmit/Recive
438 * The buffer queue addresses has to be set before starting the transfer, so
439 * this function has to be called in prior to XEmacPs_Start()
441 ******************************************************************************/
442 void XEmacPs_SetQueuePtr(XEmacPs *InstancePtr, UINTPTR QPtr, u8 QueueNum,
445 /* Assert bad arguments and conditions */
446 Xil_AssertVoid(InstancePtr != NULL);
447 Xil_AssertVoid(InstancePtr->IsReady == (u32)XIL_COMPONENT_IS_READY);
449 /* If already started, then there is nothing to do */
450 if (InstancePtr->IsStarted == (u32)XIL_COMPONENT_IS_STARTED) {
454 if (QueueNum == 0x00U) {
455 if (Direction == XEMACPS_SEND) {
456 XEmacPs_WriteReg(InstancePtr->Config.BaseAddress,
457 XEMACPS_TXQBASE_OFFSET,
458 (QPtr & ULONG64_LO_MASK));
460 XEmacPs_WriteReg(InstancePtr->Config.BaseAddress,
461 XEMACPS_RXQBASE_OFFSET,
462 (QPtr & ULONG64_LO_MASK));
466 XEmacPs_WriteReg(InstancePtr->Config.BaseAddress,
467 XEMACPS_TXQ1BASE_OFFSET,
468 (QPtr & ULONG64_LO_MASK));
471 /* Set the MSB of Queue start address */
472 XEmacPs_WriteReg(InstancePtr->Config.BaseAddress,
473 XEMACPS_MSBBUF_QBASE_OFFSET,
474 (u32)((QPtr & (u32)ULONG64_HI_MASK) >> 32U));