37411dfeb12633a8771689b61e98688e506f5903
[platform/kernel/u-boot.git] / drivers / net / ns7520_eth.c
1 /***********************************************************************
2  *
3  * Copyright (C) 2005 by Videon Central, Inc.
4  *
5  * $Id$
6  * @Author: Arthur Shipkowski
7  * @Descr: Ethernet driver for the NS7520. Uses polled Ethernet, like
8  *     the older netarmeth driver.  Note that attempting to filter
9  *     broadcast and multicast out in the SAFR register will cause
10  *     bad things due to released errata.
11  * @References: [1] NS7520 Hardware Reference, December 2003
12  *              [2] Intel LXT971 Datasheet #249414 Rev. 02
13  *
14  ***********************************************************************/
15
16 #include <common.h>
17
18 #include <net.h>                /* NetSendPacket */
19 #include <asm/arch/netarm_registers.h>
20 #include <asm/arch/netarm_dma_module.h>
21
22 #include "ns7520_eth.h"         /* for Ethernet and PHY */
23
24 /**
25  * Send an error message to the terminal.
26  */
27 #define ERROR(x) \
28 do { \
29         char *__foo = strrchr(__FILE__, '/'); \
30         \
31         printf("%s: %d: %s(): ", (__foo == NULL ? __FILE__ : (__foo + 1)), \
32                         __LINE__, __FUNCTION__); \
33         printf x; printf("\n"); \
34 } while (0);
35
36 /* some definition to make transistion to linux easier */
37
38 #define NS7520_DRIVER_NAME      "eth"
39 #define KERN_WARNING            "Warning:"
40 #define KERN_ERR                "Error:"
41 #define KERN_INFO               "Info:"
42
43 #if 1
44 # define DEBUG
45 #endif
46
47 #ifdef  DEBUG
48 # define printk                 printf
49
50 # define DEBUG_INIT             0x0001
51 # define DEBUG_MINOR            0x0002
52 # define DEBUG_RX               0x0004
53 # define DEBUG_TX               0x0008
54 # define DEBUG_INT              0x0010
55 # define DEBUG_POLL             0x0020
56 # define DEBUG_LINK             0x0040
57 # define DEBUG_MII              0x0100
58 # define DEBUG_MII_LOW          0x0200
59 # define DEBUG_MEM              0x0400
60 # define DEBUG_ERROR            0x4000
61 # define DEBUG_ERROR_CRIT       0x8000
62
63 static int nDebugLvl = DEBUG_ERROR_CRIT;
64
65 # define DEBUG_ARGS0( FLG, a0 ) if( ( nDebugLvl & (FLG) ) == (FLG) ) \
66                 printf("%s: " a0, __FUNCTION__, 0, 0, 0, 0, 0, 0 )
67 # define DEBUG_ARGS1( FLG, a0, a1 ) if( ( nDebugLvl & (FLG) ) == (FLG)) \
68                 printf("%s: " a0, __FUNCTION__, (int)(a1), 0, 0, 0, 0, 0 )
69 # define DEBUG_ARGS2( FLG, a0, a1, a2 ) if( (nDebugLvl & (FLG)) ==(FLG))\
70                 printf("%s: " a0, __FUNCTION__, (int)(a1), (int)(a2), 0, 0,0,0 )
71 # define DEBUG_ARGS3( FLG, a0, a1, a2, a3 ) if((nDebugLvl &(FLG))==(FLG))\
72                 printf("%s: "a0,__FUNCTION__,(int)(a1),(int)(a2),(int)(a3),0,0,0)
73 # define DEBUG_FN( FLG ) if( (nDebugLvl & (FLG)) == (FLG) ) \
74                 printf("\r%s:line %d\n", (int)__FUNCTION__, __LINE__, 0,0,0,0);
75 # define ASSERT( expr, func ) if( !( expr ) ) { \
76                 printf( "Assertion failed! %s:line %d %s\n", \
77                 (int)__FUNCTION__,__LINE__,(int)(#expr),0,0,0); \
78                 func }
79 #else                           /* DEBUG */
80 # define printk(...)
81 # define DEBUG_ARGS0( FLG, a0 )
82 # define DEBUG_ARGS1( FLG, a0, a1 )
83 # define DEBUG_ARGS2( FLG, a0, a1, a2 )
84 # define DEBUG_ARGS3( FLG, a0, a1, a2, a3 )
85 # define DEBUG_FN( n )
86 # define ASSERT(expr, func)
87 #endif                          /* DEBUG */
88
89 #define NS7520_MII_NEG_DELAY            (5*CFG_HZ)      /* in s */
90 #define TX_TIMEOUT                      (5*CFG_HZ)      /* in s */
91 #define RX_STALL_WORKAROUND_CNT 100
92
93 static int ns7520_eth_reset(void);
94
95 static void ns7520_link_auto_negotiate(void);
96 static void ns7520_link_update_egcr(void);
97 static void ns7520_link_print_changed(void);
98
99 /* the PHY stuff */
100
101 static char ns7520_mii_identify_phy(void);
102 static unsigned short ns7520_mii_read(unsigned short uiRegister);
103 static void ns7520_mii_write(unsigned short uiRegister,
104                              unsigned short uiData);
105 static unsigned int ns7520_mii_get_clock_divisor(unsigned int
106                                                  unMaxMDIOClk);
107 static unsigned int ns7520_mii_poll_busy(void);
108
109 static unsigned int nPhyMaxMdioClock = PHY_MDIO_MAX_CLK;
110 static unsigned int uiLastLinkStatus;
111 static PhyType phyDetected = PHY_NONE;
112
113 /***********************************************************************
114  * @Function: eth_init
115  * @Return: -1 on failure otherwise 0
116  * @Descr: Initializes the ethernet engine and uses either FS Forth's default
117  *         MAC addr or the one in environment
118  ***********************************************************************/
119
120 int eth_init(bd_t * pbis)
121 {
122         unsigned char aucMACAddr[6];
123         char *pcTmp = getenv("ethaddr");
124         char *pcEnd;
125         int i;
126
127         DEBUG_FN(DEBUG_INIT);
128
129         /* no need to check for hardware */
130
131         if (!ns7520_eth_reset())
132                 return -1;
133
134         if (NULL == pcTmp)
135                 return -1;
136
137         for (i = 0; i < 6; i++) {
138                 aucMACAddr[i] =
139                     pcTmp ? simple_strtoul(pcTmp, &pcEnd, 16) : 0;
140                 pcTmp = (*pcTmp) ? pcEnd + 1 : pcEnd;
141         }
142
143         /* configure ethernet address */
144
145         *get_eth_reg_addr(NS7520_ETH_SA1) =
146             aucMACAddr[5] << 8 | aucMACAddr[4];
147         *get_eth_reg_addr(NS7520_ETH_SA2) =
148             aucMACAddr[3] << 8 | aucMACAddr[2];
149         *get_eth_reg_addr(NS7520_ETH_SA3) =
150             aucMACAddr[1] << 8 | aucMACAddr[0];
151
152         /* enable hardware */
153
154         *get_eth_reg_addr(NS7520_ETH_MAC1) = NS7520_ETH_MAC1_RXEN;
155         *get_eth_reg_addr(NS7520_ETH_SUPP) = NS7520_ETH_SUPP_JABBER;
156         *get_eth_reg_addr(NS7520_ETH_MAC1) = NS7520_ETH_MAC1_RXEN;
157
158         /* the linux kernel may give packets < 60 bytes, for example arp */
159         *get_eth_reg_addr(NS7520_ETH_MAC2) = NS7520_ETH_MAC2_CRCEN |
160             NS7520_ETH_MAC2_PADEN | NS7520_ETH_MAC2_HUGE;
161
162         /* Broadcast/multicast allowed; if you don't set this even unicast chokes */
163         /* Based on NS7520 errata documentation */
164         *get_eth_reg_addr(NS7520_ETH_SAFR) =
165             NS7520_ETH_SAFR_BROAD | NS7520_ETH_SAFR_PRM;
166
167         /* enable receive and transmit FIFO, use 10/100 Mbps MII */
168         *get_eth_reg_addr(NS7520_ETH_EGCR) |=
169             NS7520_ETH_EGCR_ETXWM_75 |
170             NS7520_ETH_EGCR_ERX |
171             NS7520_ETH_EGCR_ERXREG |
172             NS7520_ETH_EGCR_ERXBR | NS7520_ETH_EGCR_ETX;
173
174         return 0;
175 }
176
177 /***********************************************************************
178  * @Function: eth_send
179  * @Return: -1 on timeout otherwise 1
180  * @Descr: sends one frame by DMA
181  ***********************************************************************/
182
183 int eth_send(volatile void *pPacket, int nLen)
184 {
185         int i, length32, retval = 1;
186         char *pa;
187         unsigned int *pa32, lastp = 0, rest;
188         unsigned int status;
189
190         pa = (char *) pPacket;
191         pa32 = (unsigned int *) pPacket;
192         length32 = nLen / 4;
193         rest = nLen % 4;
194
195         /* make sure there's no garbage in the last word */
196         switch (rest) {
197         case 0:
198                 lastp = pa32[length32 - 1];
199                 length32--;
200                 break;
201         case 1:
202                 lastp = pa32[length32] & 0x000000ff;
203                 break;
204         case 2:
205                 lastp = pa32[length32] & 0x0000ffff;
206                 break;
207         case 3:
208                 lastp = pa32[length32] & 0x00ffffff;
209                 break;
210         }
211
212         while (((*get_eth_reg_addr(NS7520_ETH_EGSR)) &
213                 NS7520_ETH_EGSR_TXREGE)
214                == 0) {
215         }
216
217         /* write to the fifo */
218         for (i = 0; i < length32; i++)
219                 *get_eth_reg_addr(NS7520_ETH_FIFO) = pa32[i];
220
221         /* the last word is written to an extra register, this
222            starts the transmission */
223         *get_eth_reg_addr(NS7520_ETH_FIFOL) = lastp;
224
225         /* Wait for it to be done */
226         while ((*get_eth_reg_addr(NS7520_ETH_EGSR) & NS7520_ETH_EGSR_TXBC)
227                == 0) {
228         }
229         status = (*get_eth_reg_addr(NS7520_ETH_ETSR));
230         *get_eth_reg_addr(NS7520_ETH_EGSR) = NS7520_ETH_EGSR_TXBC;      /* Clear it now */
231
232         if (status & NS7520_ETH_ETSR_TXOK) {
233                 retval = 0;     /* We're OK! */
234         } else if (status & NS7520_ETH_ETSR_TXDEF) {
235                 printf("Deferred, we'll see.\n");
236                 retval = 0;
237         } else if (status & NS7520_ETH_ETSR_TXAL) {
238                 printf("Late collision error, %d collisions.\n",
239                        (*get_eth_reg_addr(NS7520_ETH_ETSR)) &
240                        NS7520_ETH_ETSR_TXCOLC);
241         } else if (status & NS7520_ETH_ETSR_TXAEC) {
242                 printf("Excessive collisions: %d\n",
243                        (*get_eth_reg_addr(NS7520_ETH_ETSR)) &
244                        NS7520_ETH_ETSR_TXCOLC);
245         } else if (status & NS7520_ETH_ETSR_TXAED) {
246                 printf("Excessive deferral on xmit.\n");
247         } else if (status & NS7520_ETH_ETSR_TXAUR) {
248                 printf("Packet underrun.\n");
249         } else if (status & NS7520_ETH_ETSR_TXAJ) {
250                 printf("Jumbo packet error.\n");
251         } else {
252                 printf("Error: Should never get here.\n");
253         }
254
255         return (retval);
256 }
257
258 /***********************************************************************
259  * @Function: eth_rx
260  * @Return: size of last frame in bytes or 0 if no frame available
261  * @Descr: gives one frame to U-Boot which has been copied by DMA engine already
262  *         to NetRxPackets[ 0 ].
263  ***********************************************************************/
264
265 int eth_rx(void)
266 {
267         int i;
268         unsigned short rxlen;
269         unsigned short totrxlen = 0;
270         unsigned int *addr;
271         unsigned int rxstatus, lastrxlen;
272         char *pa;
273
274         /* If RXBR is 1, data block was received */
275         while (((*get_eth_reg_addr(NS7520_ETH_EGSR)) &
276                 NS7520_ETH_EGSR_RXBR) == NS7520_ETH_EGSR_RXBR) {
277
278                 /* get status register and the length of received block */
279                 rxstatus = *get_eth_reg_addr(NS7520_ETH_ERSR);
280                 rxlen = (rxstatus & NS7520_ETH_ERSR_RXSIZE) >> 16;
281
282                 /* clear RXBR to make fifo available */
283                 *get_eth_reg_addr(NS7520_ETH_EGSR) = NS7520_ETH_EGSR_RXBR;
284
285                 if (rxstatus & NS7520_ETH_ERSR_ROVER) {
286                         printf("Receive overrun, resetting FIFO.\n");
287                         *get_eth_reg_addr(NS7520_ETH_EGCR) &=
288                             ~NS7520_ETH_EGCR_ERX;
289                         udelay(20);
290                         *get_eth_reg_addr(NS7520_ETH_EGCR) |=
291                             NS7520_ETH_EGCR_ERX;
292                 }
293                 if (rxlen == 0) {
294                         printf("Nothing.\n");
295                         return 0;
296                 }
297
298                 addr = (unsigned int *) NetRxPackets[0];
299                 pa = (char *) NetRxPackets[0];
300
301                 /* read the fifo */
302                 for (i = 0; i < rxlen / 4; i++) {
303                         *addr = *get_eth_reg_addr(NS7520_ETH_FIFO);
304                         addr++;
305                 }
306
307                 if ((*get_eth_reg_addr(NS7520_ETH_EGSR)) &
308                     NS7520_ETH_EGSR_RXREGR) {
309                         /* RXFDB indicates wether the last word is 1,2,3 or 4 bytes long */
310                         lastrxlen =
311                             ((*get_eth_reg_addr(NS7520_ETH_EGSR)) &
312                              NS7520_ETH_EGSR_RXFDB_MA) >> 28;
313                         *addr = *get_eth_reg_addr(NS7520_ETH_FIFO);
314                         switch (lastrxlen) {
315                         case 1:
316                                 *addr &= 0xff000000;
317                                 break;
318                         case 2:
319                                 *addr &= 0xffff0000;
320                                 break;
321                         case 3:
322                                 *addr &= 0xffffff00;
323                                 break;
324                         }
325                 }
326
327                 /* Pass the packet up to the protocol layers. */
328                 NetReceive(NetRxPackets[0], rxlen - 4);
329                 totrxlen += rxlen - 4;
330         }
331
332         return totrxlen;
333 }
334
335 /***********************************************************************
336  * @Function: eth_halt
337  * @Return: n/a
338  * @Descr: stops the ethernet engine
339  ***********************************************************************/
340
341 void eth_halt(void)
342 {
343         DEBUG_FN(DEBUG_INIT);
344
345         *get_eth_reg_addr(NS7520_ETH_MAC1) &= ~NS7520_ETH_MAC1_RXEN;
346         *get_eth_reg_addr(NS7520_ETH_EGCR) &= ~(NS7520_ETH_EGCR_ERX |
347                                                 NS7520_ETH_EGCR_ERXDMA |
348                                                 NS7520_ETH_EGCR_ERXREG |
349                                                 NS7520_ETH_EGCR_ERXBR |
350                                                 NS7520_ETH_EGCR_ETX |
351                                                 NS7520_ETH_EGCR_ETXDMA);
352 }
353
354 /***********************************************************************
355  * @Function: ns7520_eth_reset
356  * @Return: 0 on failure otherwise 1
357  * @Descr: resets the ethernet interface and the PHY,
358  *         performs auto negotiation or fixed modes
359  ***********************************************************************/
360
361 static int ns7520_eth_reset(void)
362 {
363         DEBUG_FN(DEBUG_MINOR);
364
365         /* Reset important registers */
366         *get_eth_reg_addr(NS7520_ETH_EGCR) = 0; /* Null it out! */
367         *get_eth_reg_addr(NS7520_ETH_MAC1) &= NS7520_ETH_MAC1_SRST;
368         *get_eth_reg_addr(NS7520_ETH_MAC2) = 0;
369         /* Reset MAC */
370         *get_eth_reg_addr(NS7520_ETH_EGCR) |= NS7520_ETH_EGCR_MAC_RES;
371         udelay(5);
372         *get_eth_reg_addr(NS7520_ETH_EGCR) &= ~NS7520_ETH_EGCR_MAC_RES;
373
374         /* reset and initialize PHY */
375
376         *get_eth_reg_addr(NS7520_ETH_MAC1) &= ~NS7520_ETH_MAC1_SRST;
377
378         /* we don't support hot plugging of PHY, therefore we don't reset
379            phyDetected and nPhyMaxMdioClock here. The risk is if the setting is
380            incorrect the first open
381            may detect the PHY correctly but succeding will fail
382            For reseting the PHY and identifying we have to use the standard
383            MDIO CLOCK value 2.5 MHz only after hardware reset
384            After having identified the PHY we will do faster */
385
386         *get_eth_reg_addr(NS7520_ETH_MCFG) =
387             ns7520_mii_get_clock_divisor(nPhyMaxMdioClock);
388
389         /* reset PHY */
390         ns7520_mii_write(PHY_COMMON_CTRL, PHY_COMMON_CTRL_RESET);
391         ns7520_mii_write(PHY_COMMON_CTRL, 0);
392
393         udelay(3000);           /* [2] p.70 says at least 300us reset recovery time. */
394
395         /* MII clock has been setup to default, ns7520_mii_identify_phy should
396            work for all */
397
398         if (!ns7520_mii_identify_phy()) {
399                 printk(KERN_ERR NS7520_DRIVER_NAME
400                        ": Unsupported PHY, aborting\n");
401                 return 0;
402         }
403
404         /* now take the highest MDIO clock possible after detection */
405         *get_eth_reg_addr(NS7520_ETH_MCFG) =
406             ns7520_mii_get_clock_divisor(nPhyMaxMdioClock);
407
408         /* PHY has been detected, so there can be no abort reason and we can
409            finish initializing ethernet */
410
411         uiLastLinkStatus = 0xff;        /* undefined */
412
413         ns7520_link_auto_negotiate();
414
415         if (phyDetected == PHY_LXT971A)
416                 /* set LED2 to link mode */
417                 ns7520_mii_write(PHY_LXT971_LED_CFG,
418                                  (PHY_LXT971_LED_CFG_LINK_ACT <<
419                                   PHY_LXT971_LED_CFG_SHIFT_LED2) |
420                                  (PHY_LXT971_LED_CFG_TRANSMIT <<
421                                   PHY_LXT971_LED_CFG_SHIFT_LED1));
422
423         return 1;
424 }
425
426 /***********************************************************************
427  * @Function: ns7520_link_auto_negotiate
428  * @Return: void
429  * @Descr: performs auto-negotation of link.
430  ***********************************************************************/
431
432 static void ns7520_link_auto_negotiate(void)
433 {
434         unsigned long ulStartJiffies;
435         unsigned short uiStatus;
436
437         DEBUG_FN(DEBUG_LINK);
438
439         /* run auto-negotation */
440         /* define what we are capable of */
441         ns7520_mii_write(PHY_COMMON_AUTO_ADV,
442                          PHY_COMMON_AUTO_ADV_100BTXFD |
443                          PHY_COMMON_AUTO_ADV_100BTX |
444                          PHY_COMMON_AUTO_ADV_10BTFD |
445                          PHY_COMMON_AUTO_ADV_10BT |
446                          PHY_COMMON_AUTO_ADV_802_3);
447         /* start auto-negotiation */
448         ns7520_mii_write(PHY_COMMON_CTRL,
449                          PHY_COMMON_CTRL_AUTO_NEG |
450                          PHY_COMMON_CTRL_RES_AUTO);
451
452         /* wait for completion */
453
454         ulStartJiffies = get_timer(0);
455         while (get_timer(0) < ulStartJiffies + NS7520_MII_NEG_DELAY) {
456                 uiStatus = ns7520_mii_read(PHY_COMMON_STAT);
457                 if ((uiStatus &
458                      (PHY_COMMON_STAT_AN_COMP | PHY_COMMON_STAT_LNK_STAT))
459                     ==
460                     (PHY_COMMON_STAT_AN_COMP | PHY_COMMON_STAT_LNK_STAT)) {
461                         /* lucky we are, auto-negotiation succeeded */
462                         ns7520_link_print_changed();
463                         ns7520_link_update_egcr();
464                         return;
465                 }
466         }
467
468         DEBUG_ARGS0(DEBUG_LINK, "auto-negotiation timed out\n");
469         /* ignore invalid link settings */
470 }
471
472 /***********************************************************************
473  * @Function: ns7520_link_update_egcr
474  * @Return: void
475  * @Descr: updates the EGCR and MAC2 link status after mode change or
476  *         auto-negotation
477  ***********************************************************************/
478
479 static void ns7520_link_update_egcr(void)
480 {
481         unsigned int unEGCR;
482         unsigned int unMAC2;
483         unsigned int unIPGT;
484
485         DEBUG_FN(DEBUG_LINK);
486
487         unEGCR = *get_eth_reg_addr(NS7520_ETH_EGCR);
488         unMAC2 = *get_eth_reg_addr(NS7520_ETH_MAC2);
489         unIPGT =
490             *get_eth_reg_addr(NS7520_ETH_IPGT) & ~NS7520_ETH_IPGT_IPGT;
491
492         unEGCR &= ~NS7520_ETH_EGCR_EFULLD;
493         unMAC2 &= ~NS7520_ETH_MAC2_FULLD;
494         if ((uiLastLinkStatus & PHY_LXT971_STAT2_DUPLEX_MODE)
495             == PHY_LXT971_STAT2_DUPLEX_MODE) {
496                 unEGCR |= NS7520_ETH_EGCR_EFULLD;
497                 unMAC2 |= NS7520_ETH_MAC2_FULLD;
498                 unIPGT |= 0x15; /* see [1] p. 167 */
499         } else
500                 unIPGT |= 0x12; /* see [1] p. 167 */
501
502         *get_eth_reg_addr(NS7520_ETH_MAC2) = unMAC2;
503         *get_eth_reg_addr(NS7520_ETH_EGCR) = unEGCR;
504         *get_eth_reg_addr(NS7520_ETH_IPGT) = unIPGT;
505 }
506
507 /***********************************************************************
508  * @Function: ns7520_link_print_changed
509  * @Return: void
510  * @Descr: checks whether the link status has changed and if so prints
511  *         the new mode
512  ***********************************************************************/
513
514 static void ns7520_link_print_changed(void)
515 {
516         unsigned short uiStatus;
517         unsigned short uiControl;
518
519         DEBUG_FN(DEBUG_LINK);
520
521         uiControl = ns7520_mii_read(PHY_COMMON_CTRL);
522
523         if ((uiControl & PHY_COMMON_CTRL_AUTO_NEG) ==
524             PHY_COMMON_CTRL_AUTO_NEG) {
525                 /* PHY_COMMON_STAT_LNK_STAT is only set on autonegotiation */
526                 uiStatus = ns7520_mii_read(PHY_COMMON_STAT);
527
528                 if (!(uiStatus & PHY_COMMON_STAT_LNK_STAT)) {
529                         printk(KERN_WARNING NS7520_DRIVER_NAME
530                                ": link down\n");
531                         /* @TODO Linux: carrier_off */
532                 } else {
533                         /* @TODO Linux: carrier_on */
534                         if (phyDetected == PHY_LXT971A) {
535                                 uiStatus =
536                                     ns7520_mii_read(PHY_LXT971_STAT2);
537                                 uiStatus &=
538                                     (PHY_LXT971_STAT2_100BTX |
539                                      PHY_LXT971_STAT2_DUPLEX_MODE |
540                                      PHY_LXT971_STAT2_AUTO_NEG);
541
542                                 /* mask out all uninteresting parts */
543                         }
544                         /* other PHYs must store there link information in
545                            uiStatus as PHY_LXT971 */
546                 }
547         } else {
548                 /* mode has been forced, so uiStatus should be the same as the
549                    last link status, enforce printing */
550                 uiStatus = uiLastLinkStatus;
551                 uiLastLinkStatus = 0xff;
552         }
553
554         if (uiStatus != uiLastLinkStatus) {
555                 /* save current link status */
556                 uiLastLinkStatus = uiStatus;
557
558                 /* print new link status */
559
560                 printk(KERN_INFO NS7520_DRIVER_NAME
561                        ": link mode %i Mbps %s duplex %s\n",
562                        (uiStatus & PHY_LXT971_STAT2_100BTX) ? 100 : 10,
563                        (uiStatus & PHY_LXT971_STAT2_DUPLEX_MODE) ? "full" :
564                        "half",
565                        (uiStatus & PHY_LXT971_STAT2_AUTO_NEG) ? "(auto)" :
566                        "");
567         }
568 }
569
570 /***********************************************************************
571  * the MII low level stuff
572  ***********************************************************************/
573
574 /***********************************************************************
575  * @Function: ns7520_mii_identify_phy
576  * @Return: 1 if supported PHY has been detected otherwise 0
577  * @Descr: checks for supported PHY and prints the IDs.
578  ***********************************************************************/
579
580 static char ns7520_mii_identify_phy(void)
581 {
582         unsigned short uiID1;
583         unsigned short uiID2;
584         unsigned char *szName;
585         char cRes = 0;
586
587         DEBUG_FN(DEBUG_MII);
588
589         phyDetected = (PhyType) uiID1 = ns7520_mii_read(PHY_COMMON_ID1);
590
591         switch (phyDetected) {
592         case PHY_LXT971A:
593                 szName = "LXT971A";
594                 uiID2 = ns7520_mii_read(PHY_COMMON_ID2);
595                 nPhyMaxMdioClock = PHY_LXT971_MDIO_MAX_CLK;
596                 cRes = 1;
597                 break;
598         case PHY_NONE:
599         default:
600                 /* in case uiID1 == 0 && uiID2 == 0 we may have the wrong
601                    address or reset sets the wrong NS7520_ETH_MCFG_CLKS */
602
603                 uiID2 = 0;
604                 szName = "unknown";
605                 nPhyMaxMdioClock = PHY_MDIO_MAX_CLK;
606                 phyDetected = PHY_NONE;
607         }
608
609         printk(KERN_INFO NS7520_DRIVER_NAME
610                ": PHY (0x%x, 0x%x) = %s detected\n", uiID1, uiID2, szName);
611
612         return cRes;
613 }
614
615 /***********************************************************************
616  * @Function: ns7520_mii_read
617  * @Return: the data read from PHY register uiRegister
618  * @Descr: the data read may be invalid if timed out. If so, a message
619  *         is printed but the invalid data is returned.
620  *         The fixed device address is being used.
621  ***********************************************************************/
622
623 static unsigned short ns7520_mii_read(unsigned short uiRegister)
624 {
625         DEBUG_FN(DEBUG_MII_LOW);
626
627         /* write MII register to be read */
628         *get_eth_reg_addr(NS7520_ETH_MADR) =
629             CONFIG_PHY_ADDR << 8 | uiRegister;
630
631         *get_eth_reg_addr(NS7520_ETH_MCMD) = NS7520_ETH_MCMD_READ;
632
633         if (!ns7520_mii_poll_busy())
634                 printk(KERN_WARNING NS7520_DRIVER_NAME
635                        ": MII still busy in read\n");
636         /* continue to read */
637
638         *get_eth_reg_addr(NS7520_ETH_MCMD) = 0;
639
640         return (unsigned short) (*get_eth_reg_addr(NS7520_ETH_MRDD));
641 }
642
643 /***********************************************************************
644  * @Function: ns7520_mii_write
645  * @Return: nothing
646  * @Descr: writes the data to the PHY register. In case of a timeout,
647  *         no special handling is performed but a message printed
648  *         The fixed device address is being used.
649  ***********************************************************************/
650
651 static void ns7520_mii_write(unsigned short uiRegister,
652                              unsigned short uiData)
653 {
654         DEBUG_FN(DEBUG_MII_LOW);
655
656         /* write MII register to be written */
657         *get_eth_reg_addr(NS7520_ETH_MADR) =
658             CONFIG_PHY_ADDR << 8 | uiRegister;
659
660         *get_eth_reg_addr(NS7520_ETH_MWTD) = uiData;
661
662         if (!ns7520_mii_poll_busy()) {
663                 printf(KERN_WARNING NS7520_DRIVER_NAME
664                        ": MII still busy in write\n");
665         }
666 }
667
668 /***********************************************************************
669  * @Function: ns7520_mii_get_clock_divisor
670  * @Return: the clock divisor that should be used in NS7520_ETH_MCFG_CLKS
671  * @Descr: if no clock divisor can be calculated for the
672  *         current SYSCLK and the maximum MDIO Clock, a warning is printed
673  *         and the greatest divisor is taken
674  ***********************************************************************/
675
676 static unsigned int ns7520_mii_get_clock_divisor(unsigned int unMaxMDIOClk)
677 {
678         struct {
679                 unsigned int unSysClkDivisor;
680                 unsigned int unClks;    /* field for NS7520_ETH_MCFG_CLKS */
681         } PHYClockDivisors[] = {
682                 {
683                 4, NS7520_ETH_MCFG_CLKS_4}, {
684                 6, NS7520_ETH_MCFG_CLKS_6}, {
685                 8, NS7520_ETH_MCFG_CLKS_8}, {
686                 10, NS7520_ETH_MCFG_CLKS_10}, {
687                 14, NS7520_ETH_MCFG_CLKS_14}, {
688                 20, NS7520_ETH_MCFG_CLKS_20}, {
689                 28, NS7520_ETH_MCFG_CLKS_28}
690         };
691
692         int nIndexSysClkDiv;
693         int nArraySize =
694             sizeof(PHYClockDivisors) / sizeof(PHYClockDivisors[0]);
695         unsigned int unClks = NS7520_ETH_MCFG_CLKS_28;  /* defaults to
696                                                            greatest div */
697
698         DEBUG_FN(DEBUG_INIT);
699
700         for (nIndexSysClkDiv = 0; nIndexSysClkDiv < nArraySize;
701              nIndexSysClkDiv++) {
702                 /* find first sysclock divisor that isn't higher than 2.5 MHz
703                    clock */
704                 if (NETARM_XTAL_FREQ /
705                     PHYClockDivisors[nIndexSysClkDiv].unSysClkDivisor <=
706                     unMaxMDIOClk) {
707                         unClks = PHYClockDivisors[nIndexSysClkDiv].unClks;
708                         break;
709                 }
710         }
711
712         DEBUG_ARGS2(DEBUG_INIT,
713                     "Taking MDIO Clock bit mask 0x%0x for max clock %i\n",
714                     unClks, unMaxMDIOClk);
715
716         /* return greatest divisor */
717         return unClks;
718 }
719
720 /***********************************************************************
721  * @Function: ns7520_mii_poll_busy
722  * @Return: 0 if timed out otherwise the remaing timeout
723  * @Descr: waits until the MII has completed a command or it times out
724  *         code may be interrupted by hard interrupts.
725  *         It is not checked what happens on multiple actions when
726  *         the first is still being busy and we timeout.
727  ***********************************************************************/
728
729 static unsigned int ns7520_mii_poll_busy(void)
730 {
731         unsigned int unTimeout = 1000;
732
733         DEBUG_FN(DEBUG_MII_LOW);
734
735         while (((*get_eth_reg_addr(NS7520_ETH_MIND) & NS7520_ETH_MIND_BUSY)
736                 == NS7520_ETH_MIND_BUSY) && unTimeout)
737                 unTimeout--;
738
739         return unTimeout;
740 }
741
742 /* ----------------------------------------------------------------------------
743  * Net+ARM ethernet MII functionality.
744  */
745 #if defined(CONFIG_MII)
746
747 /**
748  * Maximum MII address we support
749  */
750 #define MII_ADDRESS_MAX                 (31)
751
752 /**
753  * Maximum MII register address we support
754  */
755 #define MII_REGISTER_MAX                (31)
756
757 /**
758  * Ethernet MII interface return values for public functions.
759  */
760 enum mii_status {
761         MII_STATUS_SUCCESS = 0,
762         MII_STATUS_FAILURE = 1,
763 };
764
765 /**
766  * Read a 16-bit value from an MII register.
767  */
768 extern int ns7520_miiphy_read(char *devname, unsigned char const addr,
769                 unsigned char const reg, unsigned short *const value)
770 {
771         int ret = MII_STATUS_FAILURE;
772
773         /* Parameter checks */
774         if (addr > MII_ADDRESS_MAX) {
775                 ERROR(("invalid addr, 0x%02X", addr));
776                 goto miiphy_read_failed_0;
777         }
778
779         if (reg > MII_REGISTER_MAX) {
780                 ERROR(("invalid reg, 0x%02X", reg));
781                 goto miiphy_read_failed_0;
782         }
783
784         if (value == NULL) {
785                 ERROR(("NULL value"));
786                 goto miiphy_read_failed_0;
787         }
788
789         DEBUG_FN(DEBUG_MII_LOW);
790
791         /* write MII register to be read */
792         *get_eth_reg_addr(NS7520_ETH_MADR) = (addr << 8) | reg;
793
794         *get_eth_reg_addr(NS7520_ETH_MCMD) = NS7520_ETH_MCMD_READ;
795
796         if (!ns7520_mii_poll_busy())
797                 printk(KERN_WARNING NS7520_DRIVER_NAME
798                        ": MII still busy in read\n");
799         /* continue to read */
800
801         *get_eth_reg_addr(NS7520_ETH_MCMD) = 0;
802
803         *value = (*get_eth_reg_addr(NS7520_ETH_MRDD));
804         ret = MII_STATUS_SUCCESS;
805         /* Fall through */
806
807       miiphy_read_failed_0:
808         return (ret);
809 }
810
811 /**
812  * Write a 16-bit value to an MII register.
813  */
814 extern int ns7520_miiphy_write(char *devname, unsigned char const addr,
815                 unsigned char const reg, unsigned short const value)
816 {
817         int ret = MII_STATUS_FAILURE;
818
819         /* Parameter checks */
820         if (addr > MII_ADDRESS_MAX) {
821                 ERROR(("invalid addr, 0x%02X", addr));
822                 goto miiphy_write_failed_0;
823         }
824
825         if (reg > MII_REGISTER_MAX) {
826                 ERROR(("invalid reg, 0x%02X", reg));
827                 goto miiphy_write_failed_0;
828         }
829
830         /* write MII register to be written */
831         *get_eth_reg_addr(NS7520_ETH_MADR) = (addr << 8) | reg;
832
833         *get_eth_reg_addr(NS7520_ETH_MWTD) = value;
834
835         if (!ns7520_mii_poll_busy()) {
836                 printf(KERN_WARNING NS7520_DRIVER_NAME
837                        ": MII still busy in write\n");
838         }
839
840         ret = MII_STATUS_SUCCESS;
841         /* Fall through */
842
843       miiphy_write_failed_0:
844         return (ret);
845 }
846 #endif                          /* defined(CONFIG_MII) */
847
848 int ns7520_miiphy_initialize(bd_t *bis)
849 {
850 #if defined(CONFIG_MII)
851         miiphy_register("ns7520phy", ns7520_miiphy_read, ns7520_miiphy_write);
852 #endif
853         return 0;
854 }