import of ftp.dlink.com/GPL/DSMG-600_reB/ppclinux.tar.gz
[linux-2.4.21-pre4.git] / drivers / net / xilinx_enet / xemac.c
1 /* $Id: xemac.c,v 1.1.1.1 2005/04/11 02:50:33 jack Exp $ */
2 /******************************************************************************
3 *
4 *     Author: Xilinx, Inc.
5 *     
6 *     
7 *     This program is free software; you can redistribute it and/or modify it
8 *     under the terms of the GNU General Public License as published by the
9 *     Free Software Foundation; either version 2 of the License, or (at your
10 *     option) any later version.
11 *     
12 *     
13 *     XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS" AS A
14 *     COURTESY TO YOU. BY PROVIDING THIS DESIGN, CODE, OR INFORMATION AS
15 *     ONE POSSIBLE IMPLEMENTATION OF THIS FEATURE, APPLICATION OR STANDARD,
16 *     XILINX IS MAKING NO REPRESENTATION THAT THIS IMPLEMENTATION IS FREE
17 *     FROM ANY CLAIMS OF INFRINGEMENT, AND YOU ARE RESPONSIBLE FOR
18 *     OBTAINING ANY RIGHTS YOU MAY REQUIRE FOR YOUR IMPLEMENTATION.
19 *     XILINX EXPRESSLY DISCLAIMS ANY WARRANTY WHATSOEVER WITH RESPECT TO
20 *     THE ADEQUACY OF THE IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO ANY
21 *     WARRANTIES OR REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE FROM
22 *     CLAIMS OF INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY AND
23 *     FITNESS FOR A PARTICULAR PURPOSE.
24 *     
25 *     
26 *     Xilinx products are not intended for use in life support appliances,
27 *     devices, or systems. Use in such applications is expressly prohibited.
28 *     
29 *     
30 *     (c) Copyright 2002 Xilinx Inc.
31 *     All rights reserved.
32 *     
33 *     
34 *     You should have received a copy of the GNU General Public License along
35 *     with this program; if not, write to the Free Software Foundation, Inc.,
36 *     675 Mass Ave, Cambridge, MA 02139, USA.
37 *
38 ******************************************************************************/
39 /*****************************************************************************/
40 /**
41 *
42 * @file xemac.c
43 *
44 * The XEmac driver. Functions in this file are the minimum required functions
45 * for this driver. See xemac.h for a detailed description of the driver.
46 *
47 * <pre>
48 * MODIFICATION HISTORY:
49 *
50 * Ver   Who  Date     Changes
51 * ----- ---- -------- -----------------------------------------------
52 * 1.00a rpm  07/31/01 First release
53 * 1.00b rpm  02/20/02 Repartitioned files and functions
54 * 1.00b rpm  07/23/02 Removed the PHY reset from Initialize()
55 * </pre>
56 ******************************************************************************/
57
58 /***************************** Include Files *********************************/
59
60 #include "xbasic_types.h"
61 #include "xemac_i.h"
62 #include "xio.h"
63 #include "xipif_v1_23_b.h"      /* Uses v1.23b of the IPIF */
64
65 /************************** Constant Definitions *****************************/
66
67 /**************************** Type Definitions *******************************/
68
69 /***************** Macros (Inline Functions) Definitions *********************/
70
71 /************************** Function Prototypes ******************************/
72
73 static XStatus ConfigureDma(XEmac * InstancePtr);
74 static XStatus ConfigureFifo(XEmac * InstancePtr);
75 static void StubFifoHandler(void *CallBackRef);
76 static void StubErrorHandler(void *CallBackRef, XStatus ErrorCode);
77 static void StubSgHandler(void *CallBackRef, XBufDescriptor * BdPtr,
78                           u32 NumBds);
79
80 /************************** Variable Definitions *****************************/
81
82 /*****************************************************************************/
83 /**
84 *
85 * Initialize a specific XEmac instance/driver.  The initialization entails:
86 * - Initialize fields of the XEmac structure
87 * - Clear the Ethernet statistics for this device
88 * - Initialize the IPIF component with its register base address
89 * - Configure the FIFO components with their register base addresses.
90 * - If the device is configured with DMA, configure the DMA channel components
91 *   with their register base addresses. At some later time, memory pools for
92 *   the scatter-gather descriptor lists are to be passed to the driver.
93 * - Reset the Ethernet MAC
94 *
95 * @param InstancePtr is a pointer to the XEmac instance to be worked on.
96 * @param DeviceId is the unique id of the device controlled by this XEmac
97 *        instance.  Passing in a device id associates the generic XEmac
98 *        instance to a specific device, as chosen by the caller or application
99 *        developer.
100 *
101 * @return
102 *
103 * - XST_SUCCESS if initialization was successful
104 * - XST_DEVICE_IS_STARTED if the device has already been started
105 * - XST_DEVICE_NOT_FOUND if device configuration information was not found for
106 *   a device with the supplied device ID.
107 *
108 * @note
109 *
110 * None.
111 *
112 ******************************************************************************/
113 XStatus
114 XEmac_Initialize(XEmac * InstancePtr, u16 DeviceId)
115 {
116         XStatus Result;
117         XEmac_Config *ConfigPtr;        /* configuration information */
118
119         XASSERT_NONVOID(InstancePtr != NULL);
120
121         /*
122          * If the device is started, disallow the initialize and return a status
123          * indicating it is started.  This allows the user to stop the device
124          * and reinitialize, but prevents a user from inadvertently initializing
125          */
126         if (InstancePtr->IsStarted == XCOMPONENT_IS_STARTED) {
127                 return XST_DEVICE_IS_STARTED;
128         }
129
130         /*
131          * Lookup the device configuration in the temporary CROM table. Use this
132          * configuration info down below when initializing this component.
133          */
134         ConfigPtr = XEmac_LookupConfig(DeviceId);
135         if (ConfigPtr == NULL) {
136                 return XST_DEVICE_NOT_FOUND;
137         }
138
139         /*
140          * Set some default values
141          */
142         InstancePtr->IsReady = 0;
143         InstancePtr->IsStarted = 0;
144         InstancePtr->IsDmaSg = ConfigPtr->HasSgDma;
145         InstancePtr->HasMii = ConfigPtr->HasMii;
146         InstancePtr->HasMulticastHash = FALSE;
147
148         /* Always default polled to false, let user configure this mode */
149         InstancePtr->IsPolled = FALSE;
150         InstancePtr->FifoRecvHandler = StubFifoHandler;
151         InstancePtr->FifoSendHandler = StubFifoHandler;
152         InstancePtr->ErrorHandler = StubErrorHandler;
153         InstancePtr->SgRecvHandler = StubSgHandler;
154         InstancePtr->SgSendHandler = StubSgHandler;
155
156         /*
157          * Clear the statistics for this driver
158          */
159         XEmac_mClearStruct((u8 *) & InstancePtr->Stats, sizeof (XEmac_Stats));
160
161         /*
162          * Initialize the device register base addresses
163          */
164         InstancePtr->BaseAddress = ConfigPtr->BaseAddress;
165
166         /*
167          * Configure the send and receive FIFOs in the MAC
168          */
169         Result = ConfigureFifo(InstancePtr);
170         if (Result != XST_SUCCESS) {
171                 return Result;
172         }
173
174         /*
175          * If the device is configured for scatter-gather DMA, configure the send
176          * and receive DMA channels in the MAC. This could return XST_NO_FEATURE.
177          */
178         if (InstancePtr->IsDmaSg) {
179                 Result = ConfigureDma(InstancePtr);
180                 if (Result != XST_SUCCESS) {
181                         return Result;
182                 }
183         }
184
185         /*
186          * Indicate the component is now ready to use. Note that this is done before
187          * we reset the device and the PHY below, which may seem a bit odd. The
188          * choice was made to move it here rather than remove the asserts in various
189          * functions (e.g., Reset() and all functions that it calls).  Applications
190          * that use multiple threads, one to initialize the XEmac driver and one
191          * waiting on the IsReady condition could have a problem with this sequence.
192          */
193         InstancePtr->IsReady = XCOMPONENT_IS_READY;
194
195         /*
196          * Reset the MAC to get it into its initial state. It is expected that
197          * device configuration by the user will take place after this
198          * initialization is done, but before the device is started.
199          */
200         XEmac_Reset(InstancePtr);
201
202         /*
203          * Reset the PHY (assuming it supports the reset_n signal). This is the
204          * only time the driver resets the PHY.
205          *
206          * Removed 07/23/02 : Caused problems on some boards. Leave up to user to
207          *                    do this directly with PhyRead/PhyWrite functions.
208          *
209          * ControlReg = XIo_In32(InstancePtr->BaseAddress + XEM_ECR_OFFSET);
210          * ControlReg &= ~XEM_ECR_PHY_ENABLE_MASK;
211          * XIo_Out32(InstancePtr->BaseAddress + XEM_ECR_OFFSET, ControlReg);
212          * ControlReg |= XEM_ECR_PHY_ENABLE_MASK;
213          * XIo_Out32(InstancePtr->BaseAddress + XEM_ECR_OFFSET, ControlReg);
214          */
215
216         return XST_SUCCESS;
217 }
218
219 /*****************************************************************************/
220 /**
221 *
222 * Start the Ethernet controller as follows:
223 *   - If not in polled mode
224 *       - Set the internal interrupt enable registers appropriately
225 *       - Enable interrupts within the device itself. Note that connection of
226 *         the driver's interrupt handler to the interrupt source (typically
227 *         done using the interrupt controller component) is done by the higher
228 *         layer software.
229 *       - If the device is configured with DMA, start the DMA channels if the
230 *         descriptor lists are not empty
231 *   - Enable the transmitter
232 *   - Enable the receiver
233 *
234 * The PHY is enabled after driver initialization. We assume the upper layer
235 * software has configured it and the EMAC appropriately before this function
236 * is called.
237 *
238 * @param InstancePtr is a pointer to the XEmac instance to be worked on.
239 *
240 * @return
241 *
242 * - XST_SUCCESS if the device was started successfully
243 * - XST_NO_CALLBACK if a callback function has not yet been registered using
244 *   the SetxxxHandler function. This is required if in interrupt mode.
245 * - XST_DEVICE_IS_STARTED if the device is already started
246 * - XST_DMA_SG_NO_LIST if configured for scatter-gather DMA and a descriptor
247 *   list has not yet been created for the send or receive channel.
248 *
249 * @note
250 *
251 * This function makes use of internal resources that are shared between the
252 * Start, Stop, and SetOptions functions. So if one task might be setting device
253 * options while another is trying to start the device, the user is required to
254 * provide protection of this shared data (typically using a semaphore).
255 *
256 ******************************************************************************/
257 XStatus
258 XEmac_Start(XEmac * InstancePtr)
259 {
260         u32 ControlReg;
261         XStatus Result;
262
263         XASSERT_NONVOID(InstancePtr != NULL);
264         XASSERT_NONVOID(InstancePtr->IsReady == XCOMPONENT_IS_READY);
265
266         /*
267          * If it is already started, return a status indicating so
268          */
269         if (InstancePtr->IsStarted == XCOMPONENT_IS_STARTED) {
270                 return XST_DEVICE_IS_STARTED;
271         }
272
273         /*
274          * If not polled, enable interrupts
275          */
276         if (!InstancePtr->IsPolled) {
277                 /*
278                  * Verify that the callbacks have been registered, then enable
279                  * interrupts
280                  */
281                 if (InstancePtr->IsDmaSg) {
282                         if ((InstancePtr->SgRecvHandler == StubSgHandler) ||
283                             (InstancePtr->SgSendHandler == StubSgHandler)) {
284                                 return XST_NO_CALLBACK;
285                         }
286
287                         /* Enable IPIF interrupts */
288                         XIIF_V123B_WRITE_DIER(InstancePtr->BaseAddress,
289                                               XEM_IPIF_DMA_DFT_MASK |
290                                               XIIF_V123B_ERROR_MASK);
291                         XIIF_V123B_WRITE_IIER(InstancePtr->BaseAddress,
292                                               XEM_EIR_DFT_SG_MASK);
293
294                         /* Enable DMA interrupts */
295                         XDmaChannel_SetIntrEnable(&InstancePtr->RecvChannel,
296                                                   XEM_DMA_SG_INTR_MASK);
297                         XDmaChannel_SetIntrEnable(&InstancePtr->SendChannel,
298                                                   XEM_DMA_SG_INTR_MASK);
299                 } else {
300                         if ((InstancePtr->FifoRecvHandler == StubFifoHandler) ||
301                             (InstancePtr->FifoSendHandler == StubFifoHandler)) {
302                                 return XST_NO_CALLBACK;
303                         }
304
305                         /* Enable IPIF interrupts */
306                         XIIF_V123B_WRITE_DIER(InstancePtr->BaseAddress,
307                                               XEM_IPIF_FIFO_DFT_MASK |
308                                               XIIF_V123B_ERROR_MASK);
309                         XIIF_V123B_WRITE_IIER(InstancePtr->BaseAddress,
310                                               XEM_EIR_DFT_FIFO_MASK);
311                 }
312
313                 /* Enable the global IPIF interrupt output */
314                 XIIF_V123B_GINTR_ENABLE(InstancePtr->BaseAddress);
315         }
316
317         /*
318          * Enable the transmitter, and receiver (do a read/modify/write to preserve
319          * current settings). There is no critical section here since this register
320          * is not modified during interrupt context.
321          */
322         ControlReg = XIo_In32(InstancePtr->BaseAddress + XEM_ECR_OFFSET);
323         ControlReg &= ~(XEM_ECR_XMIT_RESET_MASK | XEM_ECR_RECV_RESET_MASK);
324         ControlReg |= (XEM_ECR_XMIT_ENABLE_MASK | XEM_ECR_RECV_ENABLE_MASK);
325
326         XIo_Out32(InstancePtr->BaseAddress + XEM_ECR_OFFSET, ControlReg);
327
328         /*
329          * If configured with scatter-gather DMA and not polled, restart the
330          * DMA channels in case there are buffers ready to be sent or received into.
331          * The DMA SgStart function uses data that can be modified during interrupt
332          * context, so a critical section is required here.
333          */
334         if ((InstancePtr->IsDmaSg) && (!InstancePtr->IsPolled)) {
335                 XIIF_V123B_GINTR_DISABLE(InstancePtr->BaseAddress);
336
337                 /*
338                  * The only error we care about is if the list has not yet been
339                  * created, or on receive, if no buffer descriptors have been
340                  * added yet (the list is empty). Other errors are benign at this point.
341                  */
342                 Result = XDmaChannel_SgStart(&InstancePtr->RecvChannel);
343                 if ((Result == XST_DMA_SG_NO_LIST)
344                     || (Result == XST_DMA_SG_LIST_EMPTY)) {
345                         XIIF_V123B_GINTR_ENABLE(InstancePtr->BaseAddress);
346                         return Result;
347                 }
348
349                 Result = XDmaChannel_SgStart(&InstancePtr->SendChannel);
350                 if (Result == XST_DMA_SG_NO_LIST) {
351                         XIIF_V123B_GINTR_ENABLE(InstancePtr->BaseAddress);
352                         return Result;
353                 }
354
355                 /*
356                  * Indicate the device is started. Be sure to do this before we exit
357                  * the critical section since starting the DMA channel may cause data
358                  * to start flowing and interrupts to occur, but the SgRecv function in
359                  * interrupt context requires the IsStarted flag to be set.
360                  */
361                 /* InstancePtr->IsStarted = XCOMPONENT_IS_STARTED; RPM */
362
363                 XIIF_V123B_GINTR_ENABLE(InstancePtr->BaseAddress);
364         }
365
366         /*
367          * Indicate that the device is started
368          */
369         InstancePtr->IsStarted = XCOMPONENT_IS_STARTED;
370
371         return XST_SUCCESS;
372 }
373
374 /*****************************************************************************/
375 /**
376 *
377 * Stop the Ethernet MAC as follows:
378 *   - If the device is configured with DMA, stop the DMA channels (wait for
379 *     acknowledgment of stop)
380 *   - Disable the transmitter and receiver
381 *   - Disable interrupts if not in polled mode (the higher layer software is
382 *     responsible for disabling interrupts at the interrupt controller)
383 *
384 * The PHY is left enabled after a Stop is called.
385 *
386 * If the device is configured for scatter-gather DMA, the DMA engine stops at
387 * the next buffer descriptor in its list. The remaining descriptors in the list
388 * are not removed, so anything in the list will be transmitted or received when
389 * the device is restarted. The side effect of doing this is that the last
390 * buffer descriptor processed by the DMA engine before stopping may not be the
391 * last descriptor in the Ethernet frame. So when the device is restarted, a
392 * partial frame (i.e., a bad frame) may be transmitted/received. This is only a
393 * concern if a frame can span multiple buffer descriptors, which is dependent
394 * on the size of the network buffers.
395 *
396 * @param InstancePtr is a pointer to the XEmac instance to be worked on.
397 *
398 * @return
399 *
400 * - XST_SUCCESS if the device was stopped successfully
401 * - XST_DEVICE_IS_STOPPED if the device is already stopped
402 *
403 * @note
404 *
405 * This function makes use of internal resources that are shared between the
406 * Start, Stop, and SetOptions functions. So if one task might be setting device
407 * options while another is trying to start the device, the user is required to
408 * provide protection of this shared data (typically using a semaphore).
409 *
410 ******************************************************************************/
411 XStatus
412 XEmac_Stop(XEmac * InstancePtr)
413 {
414         u32 ControlReg;
415
416         XASSERT_NONVOID(InstancePtr != NULL);
417         XASSERT_NONVOID(InstancePtr->IsReady == XCOMPONENT_IS_READY);
418
419         /*
420          * If the device is already stopped, do nothing but return a status
421          * indicating so
422          */
423         if (InstancePtr->IsStarted != XCOMPONENT_IS_STARTED) {
424                 return XST_DEVICE_IS_STOPPED;
425         }
426
427         /*
428          * If configured for scatter-gather DMA, stop the DMA channels. Ignore
429          * the XST_DMA_SG_IS_STOPPED return code. There is a critical section
430          * here between SgStart and SgStop, and SgStart can be called in interrupt
431          * context, so disable interrupts while calling SgStop.
432          */
433         if (InstancePtr->IsDmaSg) {
434                 XBufDescriptor *BdTemp; /* temporary descriptor pointer */
435
436                 XIIF_V123B_GINTR_DISABLE(InstancePtr->BaseAddress);
437
438                 (void) XDmaChannel_SgStop(&InstancePtr->SendChannel, &BdTemp);
439                 (void) XDmaChannel_SgStop(&InstancePtr->RecvChannel, &BdTemp);
440
441                 XIIF_V123B_GINTR_ENABLE(InstancePtr->BaseAddress);
442         }
443
444         /*
445          * Disable the transmitter and receiver. There is no critical section
446          * here since this register is not modified during interrupt context.
447          */
448         ControlReg = XIo_In32(InstancePtr->BaseAddress + XEM_ECR_OFFSET);
449         ControlReg &= ~(XEM_ECR_XMIT_ENABLE_MASK | XEM_ECR_RECV_ENABLE_MASK);
450         XIo_Out32(InstancePtr->BaseAddress + XEM_ECR_OFFSET, ControlReg);
451
452         /*
453          * If not in polled mode, disable interrupts for IPIF (includes MAC and
454          * DMAs)
455          */
456         if (!InstancePtr->IsPolled) {
457                 XIIF_V123B_GINTR_DISABLE(InstancePtr->BaseAddress);
458         }
459
460         InstancePtr->IsStarted = 0;
461
462         return XST_SUCCESS;
463 }
464
465 /*****************************************************************************/
466 /**
467 *
468 * Reset the Ethernet MAC. This is a graceful reset in that the device is stopped
469 * first. Resets the DMA channels, the FIFOs, the transmitter, and the receiver.
470 * The PHY is not reset. Any frames in the scatter-gather descriptor lists will
471 * remain in the lists. The side effect of doing this is that after a reset and
472 * following a restart of the device, frames that were in the list before the
473 * reset may be transmitted or received. Reset must only be called after the
474 * driver has been initialized.
475 *
476 * The configuration after this reset is as follows:
477 *   - Half duplex
478 *   - Disabled transmitter and receiver
479 *   - Enabled PHY (the PHY is not reset)
480 *   - MAC transmitter does pad insertion, FCS insertion, and source address
481 *     overwrite.
482 *   - MAC receiver does not strip padding or FCS
483 *   - Interframe Gap as recommended by IEEE Std. 802.3 (96 bit times)
484 *   - Unicast addressing enabled
485 *   - Broadcast addressing enabled
486 *   - Multicast addressing disabled (addresses are preserved)
487 *   - Promiscuous addressing disabled
488 *   - Default packet threshold and packet wait bound register values for
489 *     scatter-gather DMA operation
490 *   - MAC address of all zeros
491 *
492 * The upper layer software is responsible for re-configuring (if necessary)
493 * and restarting the MAC after the reset. Note that the PHY is not reset. After
494 * its initial reset during initialization of the MAC, PHY control is left to
495 * the upper layer software. Note also that driver statistics are not cleared
496 * on reset. It is up to the upper layer software to clear the statistics if
497 * needed.
498 *
499 * When a reset is required due to an internal error, the driver notifies the
500 * upper layer software of this need through the ErrorHandler callback and
501 * specific status codes.  The upper layer software is responsible for calling
502 * this Reset function and then re-configuring the device.
503 *
504 * @param InstancePtr is a pointer to the XEmac instance to be worked on.
505 *
506 * @return
507 *
508 * None.
509 *
510 * @note
511 *
512 * None.
513 *
514 * @internal
515 *
516 * The reset is accomplished by setting the IPIF reset register.  This takes
517 * care of resetting all hardware blocks, including the MAC.
518 *
519 ******************************************************************************/
520 void
521 XEmac_Reset(XEmac * InstancePtr)
522 {
523         u32 ControlReg;
524
525         XASSERT_VOID(InstancePtr != NULL);
526         XASSERT_VOID(InstancePtr->IsReady == XCOMPONENT_IS_READY);
527
528         /*
529          * Stop the device first
530          */
531         (void) XEmac_Stop(InstancePtr);
532
533         /*
534          * Reset the entire IPIF at once.  If we choose someday to reset each
535          * hardware block separately, the reset should occur in the direction of
536          * data flow. For example, for the send direction the reset order is DMA
537          * first, then FIFO, then the MAC transmitter.
538          */
539         XIIF_V123B_RESET(InstancePtr->BaseAddress);
540
541         /*
542          * For now, configure the receiver to not strip off FCS and padding since
543          * this is not currently supported. In the future, just take the default
544          * and provide the option for the user to change this behavior.
545          */
546         ControlReg = XIo_In32(InstancePtr->BaseAddress + XEM_ECR_OFFSET);
547         ControlReg &= ~(XEM_ECR_RECV_STRIP_ENABLE_MASK);
548         XIo_Out32(InstancePtr->BaseAddress + XEM_ECR_OFFSET, ControlReg);
549
550         if (InstancePtr->IsDmaSg) {
551                 /*
552                  * After reset, configure the scatter-gather DMA packet threshold and
553                  * packet wait bound registers to default values. Ignore the return
554                  * values of these functions since they only return error if the device
555                  * is not stopped.
556                  */
557                 (void) XEmac_SetPktThreshold(InstancePtr, XEM_SEND,
558                                              XEM_SGDMA_DFT_THRESHOLD);
559                 (void) XEmac_SetPktThreshold(InstancePtr, XEM_RECV,
560                                              XEM_SGDMA_DFT_THRESHOLD);
561                 (void) XEmac_SetPktWaitBound(InstancePtr, XEM_SEND,
562                                              XEM_SGDMA_DFT_WAITBOUND);
563                 (void) XEmac_SetPktWaitBound(InstancePtr, XEM_RECV,
564                                              XEM_SGDMA_DFT_WAITBOUND);
565         }
566 }
567
568 /*****************************************************************************/
569 /**
570 *
571 * Set the MAC address for this driver/device.  The address is a 48-bit value.
572 * The device must be stopped before calling this function.
573 *
574 * @param InstancePtr is a pointer to the XEmac instance to be worked on.
575 * @param AddressPtr is a pointer to a 6-byte MAC address.
576 *
577 * @return
578 *
579 * - XST_SUCCESS if the MAC address was set successfully
580 * - XST_DEVICE_IS_STARTED if the device has not yet been stopped
581 *
582 * @note
583 *
584 * None.
585 *
586 ******************************************************************************/
587 XStatus
588 XEmac_SetMacAddress(XEmac * InstancePtr, u8 * AddressPtr)
589 {
590         u32 MacAddr = 0;
591
592         XASSERT_NONVOID(InstancePtr != NULL);
593         XASSERT_NONVOID(AddressPtr != NULL);
594         XASSERT_NONVOID(InstancePtr->IsReady == XCOMPONENT_IS_READY);
595
596         /*
597          * The device must be stopped before setting the MAC address
598          */
599         if (InstancePtr->IsStarted == XCOMPONENT_IS_STARTED) {
600                 return XST_DEVICE_IS_STARTED;
601         }
602
603         /*
604          * Set the device station address high and low registers
605          */
606         MacAddr = (AddressPtr[0] << 8) | AddressPtr[1];
607         XIo_Out32(InstancePtr->BaseAddress + XEM_SAH_OFFSET, MacAddr);
608
609         MacAddr = (AddressPtr[2] << 24) | (AddressPtr[3] << 16) |
610             (AddressPtr[4] << 8) | AddressPtr[5];
611
612         XIo_Out32(InstancePtr->BaseAddress + XEM_SAL_OFFSET, MacAddr);
613
614         return XST_SUCCESS;
615 }
616
617 /*****************************************************************************/
618 /**
619 *
620 * Get the MAC address for this driver/device.
621 *
622 * @param InstancePtr is a pointer to the XEmac instance to be worked on.
623 * @param BufferPtr is an output parameter, and is a pointer to a buffer into
624 *        which the current MAC address will be copied. The buffer must be at
625 *        least 6 bytes.
626 *
627 * @return
628 *
629 * None.
630 *
631 * @note
632 *
633 * None.
634 *
635 ******************************************************************************/
636 void
637 XEmac_GetMacAddress(XEmac * InstancePtr, u8 * BufferPtr)
638 {
639         u32 MacAddrHi;
640         u32 MacAddrLo;
641
642         XASSERT_VOID(InstancePtr != NULL);
643         XASSERT_VOID(BufferPtr != NULL);
644         XASSERT_VOID(InstancePtr->IsReady == XCOMPONENT_IS_READY);
645
646         MacAddrHi = XIo_In32(InstancePtr->BaseAddress + XEM_SAH_OFFSET);
647         MacAddrLo = XIo_In32(InstancePtr->BaseAddress + XEM_SAL_OFFSET);
648
649         BufferPtr[0] = (u8) (MacAddrHi >> 8);
650         BufferPtr[1] = (u8) MacAddrHi;
651         BufferPtr[2] = (u8) (MacAddrLo >> 24);
652         BufferPtr[3] = (u8) (MacAddrLo >> 16);
653         BufferPtr[4] = (u8) (MacAddrLo >> 8);
654         BufferPtr[5] = (u8) MacAddrLo;
655 }
656
657 /******************************************************************************/
658 /**
659 *
660 * Configure DMA (including scatter-gather) capabilities.
661 *
662 * @param InstancePtr is a pointer to the XEmac instance to be worked on.
663 *
664 * @return
665 *
666 * - XST_SUCCESS  if successful initialization of the packet FIFOs
667 *
668 * @note
669 *
670 * None.
671 *
672 ******************************************************************************/
673 static XStatus
674 ConfigureDma(XEmac * InstancePtr)
675 {
676         XStatus Result;
677
678         /*
679          * Initialize the DMA channels with their base addresses. We assume
680          * scatter-gather DMA is the only possible configuration. Descriptor space
681          * will need to be set later by the upper layer.
682          */
683         Result = XDmaChannel_Initialize(&InstancePtr->RecvChannel,
684                                         InstancePtr->BaseAddress +
685                                         XEM_DMA_RECV_OFFSET);
686         if (Result != XST_SUCCESS) {
687                 return Result;
688         }
689
690         Result = XDmaChannel_Initialize(&InstancePtr->SendChannel,
691                                         InstancePtr->BaseAddress +
692                                         XEM_DMA_SEND_OFFSET);
693
694         return Result;
695 }
696
697 /******************************************************************************/
698 /**
699 *
700 * Configure the send and receive FIFO components with their base addresses
701 * and interrupt masks.  Currently the base addresses are defined constants.
702 *
703 * @param InstancePtr is a pointer to the XEmac instance to be worked on.
704 *
705 * @return
706 *
707 * XST_SUCCESS if successful initialization of the packet FIFOs
708 *
709 * @note
710 *
711 * None.
712 *
713 ******************************************************************************/
714 static XStatus
715 ConfigureFifo(XEmac * InstancePtr)
716 {
717         XStatus Result;
718
719         /*
720          * Return status from the packet FIFOs initialization is ignored since
721          * they always return success.
722          */
723         Result = XPacketFifoV100b_Initialize(&InstancePtr->RecvFifo,
724                                              InstancePtr->BaseAddress +
725                                              XEM_PFIFO_RXREG_OFFSET,
726                                              InstancePtr->BaseAddress +
727                                              XEM_PFIFO_RXDATA_OFFSET);
728         if (Result != XST_SUCCESS) {
729                 return Result;
730         }
731
732         Result = XPacketFifoV100b_Initialize(&InstancePtr->SendFifo,
733                                              InstancePtr->BaseAddress +
734                                              XEM_PFIFO_TXREG_OFFSET,
735                                              InstancePtr->BaseAddress +
736                                              XEM_PFIFO_TXDATA_OFFSET);
737         return Result;
738 }
739
740 /******************************************************************************/
741 /**
742 *
743 * This is a stub for the scatter-gather send and recv callbacks. The stub
744 * is here in case the upper layers forget to set the handlers.
745 *
746 * @param CallBackRef is a pointer to the upper layer callback reference
747 * @param BdPtr is a pointer to the first buffer descriptor in a list
748 * @param NumBds is the number of descriptors in the list.
749 *
750 * @return
751 *
752 * None.
753 *
754 * @note
755 *
756 * None.
757 *
758 ******************************************************************************/
759 static void
760 StubSgHandler(void *CallBackRef, XBufDescriptor * BdPtr, u32 NumBds)
761 {
762         XASSERT_VOID(FALSE);
763 }
764
765 /******************************************************************************/
766 /**
767 *
768 * This is a stub for the non-DMA send and recv callbacks.  The stub is here in
769 * case the upper layers forget to set the handlers.
770 *
771 * @param CallBackRef is a pointer to the upper layer callback reference
772 *
773 * @return
774 *
775 * None.
776 *
777 * @note
778 *
779 * None.
780 *
781 ******************************************************************************/
782 static void
783 StubFifoHandler(void *CallBackRef)
784 {
785         XASSERT_VOID(FALSE);
786 }
787
788 /******************************************************************************/
789 /**
790 *
791 * This is a stub for the asynchronous error callback.  The stub is here in
792 * case the upper layers forget to set the handler.
793 *
794 * @param CallBackRef is a pointer to the upper layer callback reference
795 * @param ErrorCode is the Xilinx error code, indicating the cause of the error
796 *
797 * @return
798 *
799 * None.
800 *
801 * @note
802 *
803 * None.
804 *
805 ******************************************************************************/
806 static void
807 StubErrorHandler(void *CallBackRef, XStatus ErrorCode)
808 {
809         XASSERT_VOID(FALSE);
810 }
811
812 /*****************************************************************************/
813 /**
814 *
815 * Lookup the device configuration based on the unique device ID.  The table
816 * EmacConfigTable contains the configuration info for each device in the system.
817 *
818 * @param DeviceId is the unique device ID of the device being looked up.
819 *
820 * @return
821 *
822 * A pointer to the configuration table entry corresponding to the given
823 * device ID, or NULL if no match is found.
824 *
825 * @note
826 *
827 * None.
828 *
829 ******************************************************************************/
830 XEmac_Config *
831 XEmac_LookupConfig(u16 DeviceId)
832 {
833         XEmac_Config *CfgPtr = NULL;
834         int i;
835
836         for (i = 0; i < XPAR_XEMAC_NUM_INSTANCES; i++) {
837                 if (XEmac_ConfigTable[i].DeviceId == DeviceId) {
838                         CfgPtr = &XEmac_ConfigTable[i];
839                         break;
840                 }
841         }
842
843         return CfgPtr;
844 }