Add initial support for the ALPR board from Prodrive
[oweals/u-boot.git] / cpu / ppc4xx / 4xx_enet.c
1 /*-----------------------------------------------------------------------------+
2  *
3  *       This source code has been made available to you by IBM on an AS-IS
4  *       basis.  Anyone receiving this source is licensed under IBM
5  *       copyrights to use it in any way he or she deems fit, including
6  *       copying it, modifying it, compiling it, and redistributing it either
7  *       with or without modifications.  No license under IBM patents or
8  *       patent applications is to be implied by the copyright license.
9  *
10  *       Any user of this software should understand that IBM cannot provide
11  *       technical support for this software and will not be responsible for
12  *       any consequences resulting from the use of this software.
13  *
14  *       Any person who transfers this source code or any derivative work
15  *       must include the IBM copyright notice, this paragraph, and the
16  *       preceding two paragraphs in the transferred software.
17  *
18  *       COPYRIGHT   I B M   CORPORATION 1995
19  *       LICENSED MATERIAL  -  PROGRAM PROPERTY OF I B M
20  *-----------------------------------------------------------------------------*/
21 /*-----------------------------------------------------------------------------+
22  *
23  *  File Name:  enetemac.c
24  *
25  *  Function:   Device driver for the ethernet EMAC3 macro on the 405GP.
26  *
27  *  Author:     Mark Wisner
28  *
29  *  Change Activity-
30  *
31  *  Date        Description of Change                                       BY
32  *  ---------   ---------------------                                       ---
33  *  05-May-99   Created                                                     MKW
34  *  27-Jun-99   Clean up                                                    JWB
35  *  16-Jul-99   Added MAL error recovery and better IP packet handling      MKW
36  *  29-Jul-99   Added Full duplex support                                   MKW
37  *  06-Aug-99   Changed names for Mal CR reg                                MKW
38  *  23-Aug-99   Turned off SYE when running at 10Mbs                        MKW
39  *  24-Aug-99   Marked descriptor empty after call_xlc                      MKW
40  *  07-Sep-99   Set MAL RX buffer size reg to ENET_MAX_MTU_ALIGNED / 16     MCG
41  *              to avoid chaining maximum sized packets. Push starting
42  *              RX descriptor address up to the next cache line boundary.
43  *  16-Jan-00   Added support for booting with IP of 0x0                    MKW
44  *  15-Mar-00   Updated enetInit() to enable broadcast addresses in the
45  *              EMAC_RXM register.                                          JWB
46  *  12-Mar-01   anne-sophie.harnois@nextream.fr
47  *               - Variables are compatible with those already defined in
48  *                include/net.h
49  *              - Receive buffer descriptor ring is used to send buffers
50  *                to the user
51  *              - Info print about send/received/handled packet number if
52  *                INFO_405_ENET is set
53  *  17-Apr-01   stefan.roese@esd-electronics.com
54  *              - MAL reset in "eth_halt" included
55  *              - Enet speed and duplex output now in one line
56  *  08-May-01   stefan.roese@esd-electronics.com
57  *              - MAL error handling added (eth_init called again)
58  *  13-Nov-01   stefan.roese@esd-electronics.com
59  *              - Set IST bit in EMAC_M1 reg upon 100MBit or full duplex
60  *  04-Jan-02   stefan.roese@esd-electronics.com
61  *              - Wait for PHY auto negotiation to complete added
62  *  06-Feb-02   stefan.roese@esd-electronics.com
63  *              - Bug fixed in waiting for auto negotiation to complete
64  *  26-Feb-02   stefan.roese@esd-electronics.com
65  *              - rx and tx buffer descriptors now allocated (no fixed address
66  *                used anymore)
67  *  17-Jun-02   stefan.roese@esd-electronics.com
68  *              - MAL error debug printf 'M' removed (rx de interrupt may
69  *                occur upon many incoming packets with only 4 rx buffers).
70  *-----------------------------------------------------------------------------*
71  *  17-Nov-03   travis.sawyer@sandburst.com
72  *              - ported from 405gp_enet.c to utilized upto 4 EMAC ports
73  *                in the 440GX.  This port should work with the 440GP
74  *                (2 EMACs) also
75  *  15-Aug-05   sr@denx.de
76  *              - merged 405gp_enet.c and 440gx_enet.c to generic 4xx_enet.c
77                   now handling all 4xx cpu's.
78  *-----------------------------------------------------------------------------*/
79
80 #include <config.h>
81 #include <common.h>
82 #include <net.h>
83 #include <asm/processor.h>
84 #include <commproc.h>
85 #include <ppc4xx.h>
86 #include <ppc4xx_enet.h>
87 #include <405_mal.h>
88 #include <miiphy.h>
89 #include <malloc.h>
90 #include "vecnum.h"
91
92 /*
93  * Only compile for platform with AMCC EMAC ethernet controller and
94  * network support enabled.
95  * Remark: CONFIG_405 describes Xilinx PPC405 FPGA without EMAC controller!
96  */
97 #if (CONFIG_COMMANDS & CFG_CMD_NET) && !defined(CONFIG_405) && !defined(CONFIG_IOP480)
98
99 #if !(defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII))
100 #error "CONFIG_MII has to be defined!"
101 #endif
102
103 #if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_NET_MULTI)
104 #error "CONFIG_NET_MULTI has to be defined for NetConsole"
105 #endif
106
107 #define EMAC_RESET_TIMEOUT 1000 /* 1000 ms reset timeout */
108 #define PHY_AUTONEGOTIATE_TIMEOUT 4000  /* 4000 ms autonegotiate timeout */
109
110 /* Ethernet Transmit and Receive Buffers */
111 /* AS.HARNOIS
112  * In the same way ENET_MAX_MTU and ENET_MAX_MTU_ALIGNED are set from
113  * PKTSIZE and PKTSIZE_ALIGN (include/net.h)
114  */
115 #define ENET_MAX_MTU           PKTSIZE
116 #define ENET_MAX_MTU_ALIGNED   PKTSIZE_ALIGN
117
118 /*-----------------------------------------------------------------------------+
119  * Defines for MAL/EMAC interrupt conditions as reported in the UIC (Universal
120  * Interrupt Controller).
121  *-----------------------------------------------------------------------------*/
122 #define MAL_UIC_ERR ( UIC_MAL_SERR | UIC_MAL_TXDE  | UIC_MAL_RXDE)
123 #define MAL_UIC_DEF  (UIC_MAL_RXEOB | MAL_UIC_ERR)
124 #define EMAC_UIC_DEF UIC_ENET
125 #define EMAC_UIC_DEF1 UIC_ENET1
126 #define SEL_UIC_DEF(p) (p ? UIC_ENET1 : UIC_ENET )
127
128 #undef INFO_4XX_ENET
129
130 #define BI_PHYMODE_NONE  0
131 #define BI_PHYMODE_ZMII  1
132 #define BI_PHYMODE_RGMII 2
133
134
135 /*-----------------------------------------------------------------------------+
136  * Global variables. TX and RX descriptors and buffers.
137  *-----------------------------------------------------------------------------*/
138 /* IER globals */
139 static uint32_t mal_ier;
140
141 #if !defined(CONFIG_NET_MULTI)
142 struct eth_device *emac0_dev = NULL;
143 #endif
144
145 /*
146  * Get count of EMAC devices (doesn't have to be the max. possible number
147  * supported by the cpu)
148  */
149 #if defined(CONFIG_HAS_ETH3)
150 #define LAST_EMAC_NUM   4
151 #elif defined(CONFIG_HAS_ETH2)
152 #define LAST_EMAC_NUM   3
153 #elif defined(CONFIG_HAS_ETH1)
154 #define LAST_EMAC_NUM   2
155 #else
156 #define LAST_EMAC_NUM   1
157 #endif
158
159 /*-----------------------------------------------------------------------------+
160  * Prototypes and externals.
161  *-----------------------------------------------------------------------------*/
162 static void enet_rcv (struct eth_device *dev, unsigned long malisr);
163
164 int enetInt (struct eth_device *dev);
165 static void mal_err (struct eth_device *dev, unsigned long isr,
166                      unsigned long uic, unsigned long maldef,
167                      unsigned long mal_errr);
168 static void emac_err (struct eth_device *dev, unsigned long isr);
169
170 extern int phy_setup_aneg (char *devname, unsigned char addr);
171 extern int emac4xx_miiphy_read (char *devname, unsigned char addr,
172                 unsigned char reg, unsigned short *value);
173 extern int emac4xx_miiphy_write (char *devname, unsigned char addr,
174                 unsigned char reg, unsigned short value);
175
176 /*-----------------------------------------------------------------------------+
177 | ppc_4xx_eth_halt
178 | Disable MAL channel, and EMACn
179 +-----------------------------------------------------------------------------*/
180 static void ppc_4xx_eth_halt (struct eth_device *dev)
181 {
182         EMAC_4XX_HW_PST hw_p = dev->priv;
183         uint32_t failsafe = 10000;
184 #if defined(CONFIG_440SPE)
185         unsigned long mfr;
186 #endif
187
188         out32 (EMAC_IER + hw_p->hw_addr, 0x00000000);   /* disable emac interrupts */
189
190         /* 1st reset MAL channel */
191         /* Note: writing a 0 to a channel has no effect */
192 #if defined(CONFIG_405EP) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
193         mtdcr (maltxcarr, (MAL_CR_MMSR >> (hw_p->devnum * 2)));
194 #else
195         mtdcr (maltxcarr, (MAL_CR_MMSR >> hw_p->devnum));
196 #endif
197         mtdcr (malrxcarr, (MAL_CR_MMSR >> hw_p->devnum));
198
199         /* wait for reset */
200         while (mfdcr (malrxcasr) & (MAL_CR_MMSR >> hw_p->devnum)) {
201                 udelay (1000);  /* Delay 1 MS so as not to hammer the register */
202                 failsafe--;
203                 if (failsafe == 0)
204                         break;
205         }
206
207         /* EMAC RESET */
208 #if defined(CONFIG_440SPE)
209         /* provide clocks for EMAC internal loopback  */
210         mfsdr (sdr_mfr, mfr);
211         mfr |= 0x08000000;
212         mtsdr(sdr_mfr, mfr);
213 #endif
214
215         out32 (EMAC_M0 + hw_p->hw_addr, EMAC_M0_SRST);
216
217 #if defined(CONFIG_440SPE)
218         /* remove clocks for EMAC internal loopback  */
219         mfsdr (sdr_mfr, mfr);
220         mfr &= ~0x08000000;
221         mtsdr(sdr_mfr, mfr);
222 #endif
223
224
225 #ifndef CONFIG_NETCONSOLE
226         hw_p->print_speed = 1;  /* print speed message again next time */
227 #endif
228
229         return;
230 }
231
232 #if defined (CONFIG_440GX)
233 int ppc_4xx_eth_setup_bridge(int devnum, bd_t * bis)
234 {
235         unsigned long pfc1;
236         unsigned long zmiifer;
237         unsigned long rmiifer;
238
239         mfsdr(sdr_pfc1, pfc1);
240         pfc1 = SDR0_PFC1_EPS_DECODE(pfc1);
241
242         zmiifer = 0;
243         rmiifer = 0;
244
245         switch (pfc1) {
246         case 1:
247                 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
248                 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(1);
249                 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(2);
250                 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(3);
251                 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
252                 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
253                 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
254                 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
255                 break;
256         case 2:
257                 zmiifer = ZMII_FER_SMII << ZMII_FER_V(0);
258                 zmiifer = ZMII_FER_SMII << ZMII_FER_V(1);
259                 zmiifer = ZMII_FER_SMII << ZMII_FER_V(2);
260                 zmiifer = ZMII_FER_SMII << ZMII_FER_V(3);
261                 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
262                 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
263                 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
264                 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
265                 break;
266         case 3:
267                 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
268                 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
269                 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
270                 bis->bi_phymode[1] = BI_PHYMODE_NONE;
271                 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
272                 bis->bi_phymode[3] = BI_PHYMODE_NONE;
273                 break;
274         case 4:
275                 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(0);
276                 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(1);
277                 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V (2);
278                 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V (3);
279                 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
280                 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
281                 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
282                 bis->bi_phymode[3] = BI_PHYMODE_RGMII;
283                 break;
284         case 5:
285                 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (0);
286                 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (1);
287                 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (2);
288                 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(3);
289                 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
290                 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
291                 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
292                 bis->bi_phymode[3] = BI_PHYMODE_RGMII;
293                 break;
294         case 6:
295                 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (0);
296                 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (1);
297                 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
298                 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
299                 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
300                 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
301                 break;
302         case 0:
303         default:
304                 zmiifer = ZMII_FER_MII << ZMII_FER_V(devnum);
305                 rmiifer = 0x0;
306                 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
307                 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
308                 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
309                 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
310                 break;
311         }
312
313         /* Ensure we setup mdio for this devnum and ONLY this devnum */
314         zmiifer |= (ZMII_FER_MDI) << ZMII_FER_V(devnum);
315
316         out32 (ZMII_FER, zmiifer);
317         out32 (RGMII_FER, rmiifer);
318
319         return ((int)pfc1);
320
321 }
322 #endif  /* CONFIG_440_GX */
323
324 static int ppc_4xx_eth_init (struct eth_device *dev, bd_t * bis)
325 {
326         int i, j;
327         unsigned long reg = 0;
328         unsigned long msr;
329         unsigned long speed;
330         unsigned long duplex;
331         unsigned long failsafe;
332         unsigned mode_reg;
333         unsigned short devnum;
334         unsigned short reg_short;
335 #if defined(CONFIG_440GX) || defined(CONFIG_440SP) || defined(CONFIG_440SPE)
336         sys_info_t sysinfo;
337 #if defined(CONFIG_440GX) || defined(CONFIG_440SPE)
338         int ethgroup = -1;
339 #endif
340 #endif
341 #if defined(CONFIG_440SPE)
342         unsigned long mfr;
343 #endif
344
345
346         EMAC_4XX_HW_PST hw_p = dev->priv;
347
348         /* before doing anything, figure out if we have a MAC address */
349         /* if not, bail */
350         if (memcmp (dev->enetaddr, "\0\0\0\0\0\0", 6) == 0) {
351                 printf("ERROR: ethaddr not set!\n");
352                 return -1;
353         }
354
355 #if defined(CONFIG_440GX) || defined(CONFIG_440SP) || defined(CONFIG_440SPE)
356         /* Need to get the OPB frequency so we can access the PHY */
357         get_sys_info (&sysinfo);
358 #endif
359
360         msr = mfmsr ();
361         mtmsr (msr & ~(MSR_EE));        /* disable interrupts */
362
363         devnum = hw_p->devnum;
364
365 #ifdef INFO_4XX_ENET
366         /* AS.HARNOIS
367          * We should have :
368          * hw_p->stats.pkts_handled <=  hw_p->stats.pkts_rx <= hw_p->stats.pkts_handled+PKTBUFSRX
369          * In the most cases hw_p->stats.pkts_handled = hw_p->stats.pkts_rx, but it
370          * is possible that new packets (without relationship with
371          * current transfer) have got the time to arrived before
372          * netloop calls eth_halt
373          */
374         printf ("About preceeding transfer (eth%d):\n"
375                 "- Sent packet number %d\n"
376                 "- Received packet number %d\n"
377                 "- Handled packet number %d\n",
378                 hw_p->devnum,
379                 hw_p->stats.pkts_tx,
380                 hw_p->stats.pkts_rx, hw_p->stats.pkts_handled);
381
382         hw_p->stats.pkts_tx = 0;
383         hw_p->stats.pkts_rx = 0;
384         hw_p->stats.pkts_handled = 0;
385         hw_p->print_speed = 1;  /* print speed message again next time */
386 #endif
387
388         hw_p->tx_err_index = 0; /* Transmit Error Index for tx_err_log */
389         hw_p->rx_err_index = 0; /* Receive Error Index for rx_err_log */
390
391         hw_p->rx_slot = 0;      /* MAL Receive Slot */
392         hw_p->rx_i_index = 0;   /* Receive Interrupt Queue Index */
393         hw_p->rx_u_index = 0;   /* Receive User Queue Index */
394
395         hw_p->tx_slot = 0;      /* MAL Transmit Slot */
396         hw_p->tx_i_index = 0;   /* Transmit Interrupt Queue Index */
397         hw_p->tx_u_index = 0;   /* Transmit User Queue Index */
398
399 #if defined(CONFIG_440) && !defined(CONFIG_440SP) && !defined(CONFIG_440SPE)
400         /* set RMII mode */
401         /* NOTE: 440GX spec states that mode is mutually exclusive */
402         /* NOTE: Therefore, disable all other EMACS, since we handle */
403         /* NOTE: only one emac at a time */
404         reg = 0;
405         out32 (ZMII_FER, 0);
406         udelay (100);
407
408 #if defined(CONFIG_440EP) || defined(CONFIG_440GR)
409         out32 (ZMII_FER, (ZMII_FER_RMII | ZMII_FER_MDI) << ZMII_FER_V (devnum));
410 #elif defined(CONFIG_440GX)
411         ethgroup = ppc_4xx_eth_setup_bridge(devnum, bis);
412 #elif defined(CONFIG_440GP)
413         /* set RMII mode */
414         out32 (ZMII_FER, ZMII_RMII | ZMII_MDI0);
415 #else
416         if ((devnum == 0) || (devnum == 1)) {
417                 out32 (ZMII_FER, (ZMII_FER_SMII | ZMII_FER_MDI) << ZMII_FER_V (devnum));
418         }
419         else { /* ((devnum == 2) || (devnum == 3)) */
420                 out32 (ZMII_FER, ZMII_FER_MDI << ZMII_FER_V (devnum));
421                 out32 (RGMII_FER, ((RGMII_FER_RGMII << RGMII_FER_V (2)) |
422                                    (RGMII_FER_RGMII << RGMII_FER_V (3))));
423         }
424 #endif
425
426         out32 (ZMII_SSR, ZMII_SSR_SP << ZMII_SSR_V(devnum));
427 #endif /* defined(CONFIG_440) && !defined(CONFIG_440SP) */
428
429         __asm__ volatile ("eieio");
430
431         /* reset emac so we have access to the phy */
432 #if defined(CONFIG_440SPE)
433         /* provide clocks for EMAC internal loopback  */
434         mfsdr (sdr_mfr, mfr);
435         mfr |= 0x08000000;
436         mtsdr(sdr_mfr, mfr);
437 #endif
438
439         out32 (EMAC_M0 + hw_p->hw_addr, EMAC_M0_SRST);
440         __asm__ volatile ("eieio");
441
442         failsafe = 1000;
443         while ((in32 (EMAC_M0 + hw_p->hw_addr) & (EMAC_M0_SRST)) && failsafe) {
444                 udelay (1000);
445                 failsafe--;
446         }
447
448 #if defined(CONFIG_440SPE)
449         /* remove clocks for EMAC internal loopback  */
450         mfsdr (sdr_mfr, mfr);
451         mfr &= ~0x08000000;
452         mtsdr(sdr_mfr, mfr);
453 #endif
454
455 #if defined(CONFIG_440GX) || defined(CONFIG_440SP) || defined(CONFIG_440SPE)
456         /* Whack the M1 register */
457         mode_reg = 0x0;
458         mode_reg &= ~0x00000038;
459         if (sysinfo.freqOPB <= 50000000);
460         else if (sysinfo.freqOPB <= 66666667)
461                 mode_reg |= EMAC_M1_OBCI_66;
462         else if (sysinfo.freqOPB <= 83333333)
463                 mode_reg |= EMAC_M1_OBCI_83;
464         else if (sysinfo.freqOPB <= 100000000)
465                 mode_reg |= EMAC_M1_OBCI_100;
466         else
467                 mode_reg |= EMAC_M1_OBCI_GT100;
468
469         out32 (EMAC_M1 + hw_p->hw_addr, mode_reg);
470 #endif /* defined(CONFIG_440GX) || defined(CONFIG_440SP) */
471
472         /* wait for PHY to complete auto negotiation */
473         reg_short = 0;
474 #ifndef CONFIG_CS8952_PHY
475         switch (devnum) {
476         case 0:
477                 reg = CONFIG_PHY_ADDR;
478                 break;
479 #if defined (CONFIG_PHY1_ADDR)
480         case 1:
481                 reg = CONFIG_PHY1_ADDR;
482                 break;
483 #endif
484 #if defined (CONFIG_440GX)
485         case 2:
486                 reg = CONFIG_PHY2_ADDR;
487                 break;
488         case 3:
489                 reg = CONFIG_PHY3_ADDR;
490                 break;
491 #endif
492         default:
493                 reg = CONFIG_PHY_ADDR;
494                 break;
495         }
496
497         bis->bi_phynum[devnum] = reg;
498
499 #if defined(CONFIG_PHY_RESET)
500         /*
501          * Reset the phy, only if its the first time through
502          * otherwise, just check the speeds & feeds
503          */
504         if (hw_p->first_init == 0) {
505 #if defined(CONFIG_88E1111_CLK_DELAY)
506                 /*
507                  * On some boards (e.g. ALPR) the Marvell 88E1111 PHY needs
508                  * the "RGMII transmit timing control" and "RGMII receive
509                  * timing control" bits set, so that Gbit communication works
510                  * without problems.
511                  * Also set the "Transmitter disable" to 1 to enable the
512                  * transmitter.
513                  * After setting these bits a soft-reset must occur for this
514                  * change to become active.
515                  */
516                 miiphy_read (dev->name, reg, 0x14, &reg_short);
517                 reg_short |= (1 << 7) | (1 << 1) | (1 << 0);
518                 miiphy_write (dev->name, reg, 0x14, reg_short);
519 #endif
520                 miiphy_reset (dev->name, reg);
521
522 #if defined(CONFIG_440GX) || defined(CONFIG_440SP) || defined(CONFIG_440SPE)
523 #if defined(CONFIG_CIS8201_PHY)
524                 /*
525                  * Cicada 8201 PHY needs to have an extended register whacked
526                  * for RGMII mode.
527                  */
528                 if ( ((devnum == 2) || (devnum ==3)) && (4 == ethgroup) ) {
529 #if defined(CONFIG_CIS8201_SHORT_ETCH)
530                         miiphy_write (dev->name, reg, 23, 0x1300);
531 #else
532                         miiphy_write (dev->name, reg, 23, 0x1000);
533 #endif
534                         /*
535                          * Vitesse VSC8201/Cicada CIS8201 errata:
536                          * Interoperability problem with Intel 82547EI phys
537                          * This work around (provided by Vitesse) changes
538                          * the default timer convergence from 8ms to 12ms
539                          */
540                         miiphy_write (dev->name, reg, 0x1f, 0x2a30);
541                         miiphy_write (dev->name, reg, 0x08, 0x0200);
542                         miiphy_write (dev->name, reg, 0x1f, 0x52b5);
543                         miiphy_write (dev->name, reg, 0x02, 0x0004);
544                         miiphy_write (dev->name, reg, 0x01, 0x0671);
545                         miiphy_write (dev->name, reg, 0x00, 0x8fae);
546                         miiphy_write (dev->name, reg, 0x1f, 0x2a30);
547                         miiphy_write (dev->name, reg, 0x08, 0x0000);
548                         miiphy_write (dev->name, reg, 0x1f, 0x0000);
549                         /* end Vitesse/Cicada errata */
550                 }
551 #endif
552 #endif
553                 /* Start/Restart autonegotiation */
554                 phy_setup_aneg (dev->name, reg);
555                 udelay (1000);
556         }
557 #endif /* defined(CONFIG_PHY_RESET) */
558
559         miiphy_read (dev->name, reg, PHY_BMSR, &reg_short);
560
561         /*
562          * Wait if PHY is capable of autonegotiation and autonegotiation is not complete
563          */
564         if ((reg_short & PHY_BMSR_AUTN_ABLE)
565             && !(reg_short & PHY_BMSR_AUTN_COMP)) {
566                 puts ("Waiting for PHY auto negotiation to complete");
567                 i = 0;
568                 while (!(reg_short & PHY_BMSR_AUTN_COMP)) {
569                         /*
570                          * Timeout reached ?
571                          */
572                         if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
573                                 puts (" TIMEOUT !\n");
574                                 break;
575                         }
576
577                         if ((i++ % 1000) == 0) {
578                                 putc ('.');
579                         }
580                         udelay (1000);  /* 1 ms */
581                         miiphy_read (dev->name, reg, PHY_BMSR, &reg_short);
582
583                 }
584                 puts (" done\n");
585                 udelay (500000);        /* another 500 ms (results in faster booting) */
586         }
587 #endif /* #ifndef CONFIG_CS8952_PHY */
588
589         speed = miiphy_speed (dev->name, reg);
590         duplex = miiphy_duplex (dev->name, reg);
591
592         if (hw_p->print_speed) {
593                 hw_p->print_speed = 0;
594                 printf ("ENET Speed is %d Mbps - %s duplex connection\n",
595                         (int) speed, (duplex == HALF) ? "HALF" : "FULL");
596         }
597
598 #if defined(CONFIG_440) && !defined(CONFIG_440SP) && !defined(CONFIG_440SPE)
599 #if defined(CONFIG_440EP) || defined(CONFIG_440GR)
600         mfsdr(sdr_mfr, reg);
601         if (speed == 100) {
602                 reg = (reg & ~SDR0_MFR_ZMII_MODE_MASK) | SDR0_MFR_ZMII_MODE_RMII_100M;
603         } else {
604                 reg = (reg & ~SDR0_MFR_ZMII_MODE_MASK) | SDR0_MFR_ZMII_MODE_RMII_10M;
605         }
606         mtsdr(sdr_mfr, reg);
607 #endif
608
609         /* Set ZMII/RGMII speed according to the phy link speed */
610         reg = in32 (ZMII_SSR);
611         if ( (speed == 100) || (speed == 1000) )
612                 out32 (ZMII_SSR, reg | (ZMII_SSR_SP << ZMII_SSR_V (devnum)));
613         else
614                 out32 (ZMII_SSR, reg & (~(ZMII_SSR_SP << ZMII_SSR_V (devnum))));
615
616         if ((devnum == 2) || (devnum == 3)) {
617                 if (speed == 1000)
618                         reg = (RGMII_SSR_SP_1000MBPS << RGMII_SSR_V (devnum));
619                 else if (speed == 100)
620                         reg = (RGMII_SSR_SP_100MBPS << RGMII_SSR_V (devnum));
621                 else
622                         reg = (RGMII_SSR_SP_10MBPS << RGMII_SSR_V (devnum));
623
624                 out32 (RGMII_SSR, reg);
625         }
626 #endif /* defined(CONFIG_440) && !defined(CONFIG_440SP) */
627
628         /* set the Mal configuration reg */
629 #if defined(CONFIG_440GX) || defined(CONFIG_440SP) || defined(CONFIG_440SPE)
630         mtdcr (malmcr, MAL_CR_PLBB | MAL_CR_OPBBL | MAL_CR_LEA |
631                MAL_CR_PLBLT_DEFAULT | MAL_CR_EOPIE | 0x00330000);
632 #else
633         mtdcr (malmcr, MAL_CR_PLBB | MAL_CR_OPBBL | MAL_CR_LEA | MAL_CR_PLBLT_DEFAULT);
634         /* Errata 1.12: MAL_1 -- Disable MAL bursting */
635         if (get_pvr() == PVR_440GP_RB) {
636                 mtdcr (malmcr, mfdcr(malmcr) & ~MAL_CR_PLBB);
637         }
638 #endif
639
640         /* Free "old" buffers */
641         if (hw_p->alloc_tx_buf)
642                 free (hw_p->alloc_tx_buf);
643         if (hw_p->alloc_rx_buf)
644                 free (hw_p->alloc_rx_buf);
645
646         /*
647          * Malloc MAL buffer desciptors, make sure they are
648          * aligned on cache line boundary size
649          * (401/403/IOP480 = 16, 405 = 32)
650          * and doesn't cross cache block boundaries.
651          */
652         hw_p->alloc_tx_buf =
653                 (mal_desc_t *) malloc ((sizeof (mal_desc_t) * NUM_TX_BUFF) +
654                                        ((2 * CFG_CACHELINE_SIZE) - 2));
655         if (NULL == hw_p->alloc_tx_buf)
656                 return -1;
657         if (((int) hw_p->alloc_tx_buf & CACHELINE_MASK) != 0) {
658                 hw_p->tx =
659                         (mal_desc_t *) ((int) hw_p->alloc_tx_buf +
660                                         CFG_CACHELINE_SIZE -
661                                         ((int) hw_p->
662                                          alloc_tx_buf & CACHELINE_MASK));
663         } else {
664                 hw_p->tx = hw_p->alloc_tx_buf;
665         }
666
667         hw_p->alloc_rx_buf =
668                 (mal_desc_t *) malloc ((sizeof (mal_desc_t) * NUM_RX_BUFF) +
669                                        ((2 * CFG_CACHELINE_SIZE) - 2));
670         if (NULL == hw_p->alloc_rx_buf) {
671                 free(hw_p->alloc_tx_buf);
672                 hw_p->alloc_tx_buf = NULL;
673                 return -1;
674         }
675
676         if (((int) hw_p->alloc_rx_buf & CACHELINE_MASK) != 0) {
677                 hw_p->rx =
678                         (mal_desc_t *) ((int) hw_p->alloc_rx_buf +
679                                         CFG_CACHELINE_SIZE -
680                                         ((int) hw_p->
681                                          alloc_rx_buf & CACHELINE_MASK));
682         } else {
683                 hw_p->rx = hw_p->alloc_rx_buf;
684         }
685
686         for (i = 0; i < NUM_TX_BUFF; i++) {
687                 hw_p->tx[i].ctrl = 0;
688                 hw_p->tx[i].data_len = 0;
689                 if (hw_p->first_init == 0) {
690                         hw_p->txbuf_ptr =
691                                 (char *) malloc (ENET_MAX_MTU_ALIGNED);
692                         if (NULL == hw_p->txbuf_ptr) {
693                                 free(hw_p->alloc_rx_buf);
694                                 free(hw_p->alloc_tx_buf);
695                                 hw_p->alloc_rx_buf = NULL;
696                                 hw_p->alloc_tx_buf = NULL;
697                                 for(j = 0; j < i; j++) {
698                                         free(hw_p->tx[i].data_ptr);
699                                         hw_p->tx[i].data_ptr = NULL;
700                                 }
701                         }
702                 }
703                 hw_p->tx[i].data_ptr = hw_p->txbuf_ptr;
704                 if ((NUM_TX_BUFF - 1) == i)
705                         hw_p->tx[i].ctrl |= MAL_TX_CTRL_WRAP;
706                 hw_p->tx_run[i] = -1;
707 #if 0
708                 printf ("TX_BUFF %d @ 0x%08lx\n", i,
709                         (ulong) hw_p->tx[i].data_ptr);
710 #endif
711         }
712
713         for (i = 0; i < NUM_RX_BUFF; i++) {
714                 hw_p->rx[i].ctrl = 0;
715                 hw_p->rx[i].data_len = 0;
716                 /*       rx[i].data_ptr = (char *) &rx_buff[i]; */
717                 hw_p->rx[i].data_ptr = (char *) NetRxPackets[i];
718                 if ((NUM_RX_BUFF - 1) == i)
719                         hw_p->rx[i].ctrl |= MAL_RX_CTRL_WRAP;
720                 hw_p->rx[i].ctrl |= MAL_RX_CTRL_EMPTY | MAL_RX_CTRL_INTR;
721                 hw_p->rx_ready[i] = -1;
722 #if 0
723                 printf ("RX_BUFF %d @ 0x%08lx\n", i, (ulong) rx[i].data_ptr);
724 #endif
725         }
726
727         reg = 0x00000000;
728
729         reg |= dev->enetaddr[0];        /* set high address */
730         reg = reg << 8;
731         reg |= dev->enetaddr[1];
732
733         out32 (EMAC_IAH + hw_p->hw_addr, reg);
734
735         reg = 0x00000000;
736         reg |= dev->enetaddr[2];        /* set low address  */
737         reg = reg << 8;
738         reg |= dev->enetaddr[3];
739         reg = reg << 8;
740         reg |= dev->enetaddr[4];
741         reg = reg << 8;
742         reg |= dev->enetaddr[5];
743
744         out32 (EMAC_IAL + hw_p->hw_addr, reg);
745
746         switch (devnum) {
747         case 1:
748                 /* setup MAL tx & rx channel pointers */
749 #if defined (CONFIG_405EP) || defined (CONFIG_440EP) || defined (CONFIG_440GR)
750                 mtdcr (maltxctp2r, hw_p->tx);
751 #else
752                 mtdcr (maltxctp1r, hw_p->tx);
753 #endif
754 #if defined(CONFIG_440)
755                 mtdcr (maltxbattr, 0x0);
756                 mtdcr (malrxbattr, 0x0);
757 #endif
758                 mtdcr (malrxctp1r, hw_p->rx);
759                 /* set RX buffer size */
760                 mtdcr (malrcbs1, ENET_MAX_MTU_ALIGNED / 16);
761                 break;
762 #if defined (CONFIG_440GX)
763         case 2:
764                 /* setup MAL tx & rx channel pointers */
765                 mtdcr (maltxbattr, 0x0);
766                 mtdcr (malrxbattr, 0x0);
767                 mtdcr (maltxctp2r, hw_p->tx);
768                 mtdcr (malrxctp2r, hw_p->rx);
769                 /* set RX buffer size */
770                 mtdcr (malrcbs2, ENET_MAX_MTU_ALIGNED / 16);
771                 break;
772         case 3:
773                 /* setup MAL tx & rx channel pointers */
774                 mtdcr (maltxbattr, 0x0);
775                 mtdcr (maltxctp3r, hw_p->tx);
776                 mtdcr (malrxbattr, 0x0);
777                 mtdcr (malrxctp3r, hw_p->rx);
778                 /* set RX buffer size */
779                 mtdcr (malrcbs3, ENET_MAX_MTU_ALIGNED / 16);
780                 break;
781 #endif /* CONFIG_440GX */
782         case 0:
783         default:
784                 /* setup MAL tx & rx channel pointers */
785 #if defined(CONFIG_440)
786                 mtdcr (maltxbattr, 0x0);
787                 mtdcr (malrxbattr, 0x0);
788 #endif
789                 mtdcr (maltxctp0r, hw_p->tx);
790                 mtdcr (malrxctp0r, hw_p->rx);
791                 /* set RX buffer size */
792                 mtdcr (malrcbs0, ENET_MAX_MTU_ALIGNED / 16);
793                 break;
794         }
795
796         /* Enable MAL transmit and receive channels */
797 #if defined(CONFIG_405EP) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
798         mtdcr (maltxcasr, (MAL_TXRX_CASR >> (hw_p->devnum*2)));
799 #else
800         mtdcr (maltxcasr, (MAL_TXRX_CASR >> hw_p->devnum));
801 #endif
802         mtdcr (malrxcasr, (MAL_TXRX_CASR >> hw_p->devnum));
803
804         /* set transmit enable & receive enable */
805         out32 (EMAC_M0 + hw_p->hw_addr, EMAC_M0_TXE | EMAC_M0_RXE);
806
807         /* set receive fifo to 4k and tx fifo to 2k */
808         mode_reg = in32 (EMAC_M1 + hw_p->hw_addr);
809         mode_reg |= EMAC_M1_RFS_4K | EMAC_M1_TX_FIFO_2K;
810
811         /* set speed */
812         if (speed == _1000BASET) {
813 #if defined(CONFIG_440SP) || defined(CONFIG_440SPE)
814                 unsigned long pfc1;
815                 mfsdr (sdr_pfc1, pfc1);
816                 pfc1 |= SDR0_PFC1_EM_1000;
817                 mtsdr (sdr_pfc1, pfc1);
818 #endif
819                 mode_reg = mode_reg | EMAC_M1_MF_1000MBPS | EMAC_M1_IST;
820         } else if (speed == _100BASET)
821                 mode_reg = mode_reg | EMAC_M1_MF_100MBPS | EMAC_M1_IST;
822         else
823                 mode_reg = mode_reg & ~0x00C00000;      /* 10 MBPS */
824         if (duplex == FULL)
825                 mode_reg = mode_reg | 0x80000000 | EMAC_M1_IST;
826
827         out32 (EMAC_M1 + hw_p->hw_addr, mode_reg);
828
829         /* Enable broadcast and indvidual address */
830         /* TBS: enabling runts as some misbehaved nics will send runts */
831         out32 (EMAC_RXM + hw_p->hw_addr, EMAC_RMR_BAE | EMAC_RMR_IAE);
832
833         /* we probably need to set the tx mode1 reg? maybe at tx time */
834
835         /* set transmit request threshold register */
836         out32 (EMAC_TRTR + hw_p->hw_addr, 0x18000000);  /* 256 byte threshold */
837
838         /* set receive  low/high water mark register */
839 #if defined(CONFIG_440)
840         /* 440s has a 64 byte burst length */
841         out32 (EMAC_RX_HI_LO_WMARK + hw_p->hw_addr, 0x80009000);
842 #else
843         /* 405s have a 16 byte burst length */
844         out32 (EMAC_RX_HI_LO_WMARK + hw_p->hw_addr, 0x0f002000);
845 #endif /* defined(CONFIG_440) */
846         out32 (EMAC_TXM1 + hw_p->hw_addr, 0xf8640000);
847
848         /* Set fifo limit entry in tx mode 0 */
849         out32 (EMAC_TXM0 + hw_p->hw_addr, 0x00000003);
850         /* Frame gap set */
851         out32 (EMAC_I_FRAME_GAP_REG + hw_p->hw_addr, 0x00000008);
852
853         /* Set EMAC IER */
854         hw_p->emac_ier = EMAC_ISR_PTLE | EMAC_ISR_BFCS | EMAC_ISR_ORE | EMAC_ISR_IRE;
855         if (speed == _100BASET)
856                 hw_p->emac_ier = hw_p->emac_ier | EMAC_ISR_SYE;
857
858         out32 (EMAC_ISR + hw_p->hw_addr, 0xffffffff);   /* clear pending interrupts */
859         out32 (EMAC_IER + hw_p->hw_addr, hw_p->emac_ier);
860
861         if (hw_p->first_init == 0) {
862                 /*
863                  * Connect interrupt service routines
864                  */
865                 irq_install_handler (VECNUM_ETH0 + (hw_p->devnum * 2),
866                                      (interrupt_handler_t *) enetInt, dev);
867         }
868
869         mtmsr (msr);            /* enable interrupts again */
870
871         hw_p->bis = bis;
872         hw_p->first_init = 1;
873
874         return (1);
875 }
876
877
878 static int ppc_4xx_eth_send (struct eth_device *dev, volatile void *ptr,
879                               int len)
880 {
881         struct enet_frame *ef_ptr;
882         ulong time_start, time_now;
883         unsigned long temp_txm0;
884         EMAC_4XX_HW_PST hw_p = dev->priv;
885
886         ef_ptr = (struct enet_frame *) ptr;
887
888         /*-----------------------------------------------------------------------+
889          *  Copy in our address into the frame.
890          *-----------------------------------------------------------------------*/
891         (void) memcpy (ef_ptr->source_addr, dev->enetaddr, ENET_ADDR_LENGTH);
892
893         /*-----------------------------------------------------------------------+
894          * If frame is too long or too short, modify length.
895          *-----------------------------------------------------------------------*/
896         /* TBS: where does the fragment go???? */
897         if (len > ENET_MAX_MTU)
898                 len = ENET_MAX_MTU;
899
900         /*   memcpy ((void *) &tx_buff[tx_slot], (const void *) ptr, len); */
901         memcpy ((void *) hw_p->txbuf_ptr, (const void *) ptr, len);
902
903         /*-----------------------------------------------------------------------+
904          * set TX Buffer busy, and send it
905          *-----------------------------------------------------------------------*/
906         hw_p->tx[hw_p->tx_slot].ctrl = (MAL_TX_CTRL_LAST |
907                                         EMAC_TX_CTRL_GFCS | EMAC_TX_CTRL_GP) &
908                 ~(EMAC_TX_CTRL_ISA | EMAC_TX_CTRL_RSA);
909         if ((NUM_TX_BUFF - 1) == hw_p->tx_slot)
910                 hw_p->tx[hw_p->tx_slot].ctrl |= MAL_TX_CTRL_WRAP;
911
912         hw_p->tx[hw_p->tx_slot].data_len = (short) len;
913         hw_p->tx[hw_p->tx_slot].ctrl |= MAL_TX_CTRL_READY;
914
915         __asm__ volatile ("eieio");
916
917         out32 (EMAC_TXM0 + hw_p->hw_addr,
918                in32 (EMAC_TXM0 + hw_p->hw_addr) | EMAC_TXM0_GNP0);
919 #ifdef INFO_4XX_ENET
920         hw_p->stats.pkts_tx++;
921 #endif
922
923         /*-----------------------------------------------------------------------+
924          * poll unitl the packet is sent and then make sure it is OK
925          *-----------------------------------------------------------------------*/
926         time_start = get_timer (0);
927         while (1) {
928                 temp_txm0 = in32 (EMAC_TXM0 + hw_p->hw_addr);
929                 /* loop until either TINT turns on or 3 seconds elapse */
930                 if ((temp_txm0 & EMAC_TXM0_GNP0) != 0) {
931                         /* transmit is done, so now check for errors
932                          * If there is an error, an interrupt should
933                          * happen when we return
934                          */
935                         time_now = get_timer (0);
936                         if ((time_now - time_start) > 3000) {
937                                 return (-1);
938                         }
939                 } else {
940                         return (len);
941                 }
942         }
943 }
944
945
946 #if defined (CONFIG_440)
947
948 #if defined(CONFIG_440SP) || defined(CONFIG_440SPE)
949 /*
950  * Hack: On 440SP all enet irq sources are located on UIC1
951  * Needs some cleanup. --sr
952  */
953 #define UIC0MSR         uic1msr
954 #define UIC0SR          uic1sr
955 #else
956 #define UIC0MSR         uic0msr
957 #define UIC0SR          uic0sr
958 #endif
959
960 int enetInt (struct eth_device *dev)
961 {
962         int serviced;
963         int rc = -1;            /* default to not us */
964         unsigned long mal_isr;
965         unsigned long emac_isr = 0;
966         unsigned long mal_rx_eob;
967         unsigned long my_uic0msr, my_uic1msr;
968
969 #if defined(CONFIG_440GX)
970         unsigned long my_uic2msr;
971 #endif
972         EMAC_4XX_HW_PST hw_p;
973
974         /*
975          * Because the mal is generic, we need to get the current
976          * eth device
977          */
978 #if defined(CONFIG_NET_MULTI)
979         dev = eth_get_dev();
980 #else
981         dev = emac0_dev;
982 #endif
983
984         hw_p = dev->priv;
985
986         /* enter loop that stays in interrupt code until nothing to service */
987         do {
988                 serviced = 0;
989
990                 my_uic0msr = mfdcr (UIC0MSR);
991                 my_uic1msr = mfdcr (uic1msr);
992 #if defined(CONFIG_440GX)
993                 my_uic2msr = mfdcr (uic2msr);
994 #endif
995                 if (!(my_uic0msr & (UIC_MRE | UIC_MTE))
996                     && !(my_uic1msr & (UIC_ETH0 | UIC_ETH1 | UIC_MS | UIC_MTDE | UIC_MRDE))) {
997                         /* not for us */
998                         return (rc);
999                 }
1000 #if defined (CONFIG_440GX)
1001                 if (!(my_uic0msr & (UIC_MRE | UIC_MTE))
1002                     && !(my_uic2msr & (UIC_ETH2 | UIC_ETH3))) {
1003                         /* not for us */
1004                         return (rc);
1005                 }
1006 #endif
1007                 /* get and clear controller status interrupts */
1008                 /* look at Mal and EMAC interrupts */
1009                 if ((my_uic0msr & (UIC_MRE | UIC_MTE))
1010                     || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
1011                         /* we have a MAL interrupt */
1012                         mal_isr = mfdcr (malesr);
1013                         /* look for mal error */
1014                         if (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE)) {
1015                                 mal_err (dev, mal_isr, my_uic0msr,
1016                                          MAL_UIC_DEF, MAL_UIC_ERR);
1017                                 serviced = 1;
1018                                 rc = 0;
1019                         }
1020                 }
1021
1022                 /* port by port dispatch of emac interrupts */
1023                 if (hw_p->devnum == 0) {
1024                         if (UIC_ETH0 & my_uic1msr) {    /* look for EMAC errors */
1025                                 emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
1026                                 if ((hw_p->emac_ier & emac_isr) != 0) {
1027                                         emac_err (dev, emac_isr);
1028                                         serviced = 1;
1029                                         rc = 0;
1030                                 }
1031                         }
1032                         if ((hw_p->emac_ier & emac_isr)
1033                             || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
1034                                 mtdcr (UIC0SR, UIC_MRE | UIC_MTE);      /* Clear */
1035                                 mtdcr (uic1sr, UIC_ETH0 | UIC_MS | UIC_MTDE | UIC_MRDE);        /* Clear */
1036                                 return (rc);    /* we had errors so get out */
1037                         }
1038                 }
1039
1040 #if !defined(CONFIG_440SP)
1041                 if (hw_p->devnum == 1) {
1042                         if (UIC_ETH1 & my_uic1msr) {    /* look for EMAC errors */
1043                                 emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
1044                                 if ((hw_p->emac_ier & emac_isr) != 0) {
1045                                         emac_err (dev, emac_isr);
1046                                         serviced = 1;
1047                                         rc = 0;
1048                                 }
1049                         }
1050                         if ((hw_p->emac_ier & emac_isr)
1051                             || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
1052                                 mtdcr (UIC0SR, UIC_MRE | UIC_MTE);      /* Clear */
1053                                 mtdcr (uic1sr, UIC_ETH1 | UIC_MS | UIC_MTDE | UIC_MRDE);        /* Clear */
1054                                 return (rc);    /* we had errors so get out */
1055                         }
1056                 }
1057 #if defined (CONFIG_440GX)
1058                 if (hw_p->devnum == 2) {
1059                         if (UIC_ETH2 & my_uic2msr) {    /* look for EMAC errors */
1060                                 emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
1061                                 if ((hw_p->emac_ier & emac_isr) != 0) {
1062                                         emac_err (dev, emac_isr);
1063                                         serviced = 1;
1064                                         rc = 0;
1065                                 }
1066                         }
1067                         if ((hw_p->emac_ier & emac_isr)
1068                             || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
1069                                 mtdcr (UIC0SR, UIC_MRE | UIC_MTE);      /* Clear */
1070                                 mtdcr (uic1sr, UIC_MS | UIC_MTDE | UIC_MRDE);   /* Clear */
1071                                 mtdcr (uic2sr, UIC_ETH2);
1072                                 return (rc);    /* we had errors so get out */
1073                         }
1074                 }
1075
1076                 if (hw_p->devnum == 3) {
1077                         if (UIC_ETH3 & my_uic2msr) {    /* look for EMAC errors */
1078                                 emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
1079                                 if ((hw_p->emac_ier & emac_isr) != 0) {
1080                                         emac_err (dev, emac_isr);
1081                                         serviced = 1;
1082                                         rc = 0;
1083                                 }
1084                         }
1085                         if ((hw_p->emac_ier & emac_isr)
1086                             || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
1087                                 mtdcr (UIC0SR, UIC_MRE | UIC_MTE);      /* Clear */
1088                                 mtdcr (uic1sr, UIC_MS | UIC_MTDE | UIC_MRDE);   /* Clear */
1089                                 mtdcr (uic2sr, UIC_ETH3);
1090                                 return (rc);    /* we had errors so get out */
1091                         }
1092                 }
1093 #endif /* CONFIG_440GX */
1094 #endif /* !CONFIG_440SP */
1095
1096                 /* handle MAX TX EOB interrupt from a tx */
1097                 if (my_uic0msr & UIC_MTE) {
1098                         mal_rx_eob = mfdcr (maltxeobisr);
1099                         mtdcr (maltxeobisr, mal_rx_eob);
1100                         mtdcr (UIC0SR, UIC_MTE);
1101                 }
1102                 /* handle MAL RX EOB  interupt from a receive */
1103                 /* check for EOB on valid channels            */
1104                 if (my_uic0msr & UIC_MRE) {
1105                         mal_rx_eob = mfdcr (malrxeobisr);
1106                         if ((mal_rx_eob & (0x80000000 >> hw_p->devnum)) != 0) { /* call emac routine for channel x */
1107                                 /* clear EOB
1108                                    mtdcr(malrxeobisr, mal_rx_eob); */
1109                                 enet_rcv (dev, emac_isr);
1110                                 /* indicate that we serviced an interrupt */
1111                                 serviced = 1;
1112                                 rc = 0;
1113                         }
1114                 }
1115
1116                 mtdcr (UIC0SR, UIC_MRE);        /* Clear */
1117                 mtdcr (uic1sr, UIC_MS | UIC_MTDE | UIC_MRDE);   /* Clear */
1118                 switch (hw_p->devnum) {
1119                 case 0:
1120                         mtdcr (uic1sr, UIC_ETH0);
1121                         break;
1122                 case 1:
1123                         mtdcr (uic1sr, UIC_ETH1);
1124                         break;
1125 #if defined (CONFIG_440GX)
1126                 case 2:
1127                         mtdcr (uic2sr, UIC_ETH2);
1128                         break;
1129                 case 3:
1130                         mtdcr (uic2sr, UIC_ETH3);
1131                         break;
1132 #endif /* CONFIG_440GX */
1133                 default:
1134                         break;
1135                 }
1136         } while (serviced);
1137
1138         return (rc);
1139 }
1140
1141 #else /* CONFIG_440 */
1142
1143 int enetInt (struct eth_device *dev)
1144 {
1145         int serviced;
1146         int rc = -1;            /* default to not us */
1147         unsigned long mal_isr;
1148         unsigned long emac_isr = 0;
1149         unsigned long mal_rx_eob;
1150         unsigned long my_uicmsr;
1151
1152         EMAC_4XX_HW_PST hw_p;
1153
1154         /*
1155          * Because the mal is generic, we need to get the current
1156          * eth device
1157          */
1158 #if defined(CONFIG_NET_MULTI)
1159         dev = eth_get_dev();
1160 #else
1161         dev = emac0_dev;
1162 #endif
1163
1164         hw_p = dev->priv;
1165
1166         /* enter loop that stays in interrupt code until nothing to service */
1167         do {
1168                 serviced = 0;
1169
1170                 my_uicmsr = mfdcr (uicmsr);
1171
1172                 if ((my_uicmsr & (MAL_UIC_DEF | EMAC_UIC_DEF)) == 0) {  /* not for us */
1173                         return (rc);
1174                 }
1175                 /* get and clear controller status interrupts */
1176                 /* look at Mal and EMAC interrupts */
1177                 if ((MAL_UIC_DEF & my_uicmsr) != 0) {   /* we have a MAL interrupt */
1178                         mal_isr = mfdcr (malesr);
1179                         /* look for mal error */
1180                         if ((my_uicmsr & MAL_UIC_ERR) != 0) {
1181                                 mal_err (dev, mal_isr, my_uicmsr, MAL_UIC_DEF, MAL_UIC_ERR);
1182                                 serviced = 1;
1183                                 rc = 0;
1184                         }
1185                 }
1186
1187                 /* port by port dispatch of emac interrupts */
1188
1189                 if ((SEL_UIC_DEF(hw_p->devnum) & my_uicmsr) != 0) {     /* look for EMAC errors */
1190                         emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
1191                         if ((hw_p->emac_ier & emac_isr) != 0) {
1192                                 emac_err (dev, emac_isr);
1193                                 serviced = 1;
1194                                 rc = 0;
1195                         }
1196                 }
1197                 if (((hw_p->emac_ier & emac_isr) != 0) || ((MAL_UIC_ERR & my_uicmsr) != 0)) {
1198                         mtdcr (uicsr, MAL_UIC_DEF | SEL_UIC_DEF(hw_p->devnum)); /* Clear */
1199                         return (rc);            /* we had errors so get out */
1200                 }
1201
1202                 /* handle MAX TX EOB interrupt from a tx */
1203                 if (my_uicmsr & UIC_MAL_TXEOB) {
1204                         mal_rx_eob = mfdcr (maltxeobisr);
1205                         mtdcr (maltxeobisr, mal_rx_eob);
1206                         mtdcr (uicsr, UIC_MAL_TXEOB);
1207                 }
1208                 /* handle MAL RX EOB  interupt from a receive */
1209                 /* check for EOB on valid channels            */
1210                 if (my_uicmsr & UIC_MAL_RXEOB)
1211                 {
1212                         mal_rx_eob = mfdcr (malrxeobisr);
1213                         if ((mal_rx_eob & (0x80000000 >> hw_p->devnum)) != 0) { /* call emac routine for channel x */
1214                                 /* clear EOB
1215                                  mtdcr(malrxeobisr, mal_rx_eob); */
1216                                 enet_rcv (dev, emac_isr);
1217                                 /* indicate that we serviced an interrupt */
1218                                 serviced = 1;
1219                                 rc = 0;
1220                         }
1221                 }
1222                 mtdcr (uicsr, MAL_UIC_DEF|EMAC_UIC_DEF|EMAC_UIC_DEF1);  /* Clear */
1223         }
1224         while (serviced);
1225
1226         return (rc);
1227 }
1228
1229 #endif /* CONFIG_440 */
1230
1231 /*-----------------------------------------------------------------------------+
1232  *  MAL Error Routine
1233  *-----------------------------------------------------------------------------*/
1234 static void mal_err (struct eth_device *dev, unsigned long isr,
1235                      unsigned long uic, unsigned long maldef,
1236                      unsigned long mal_errr)
1237 {
1238         EMAC_4XX_HW_PST hw_p = dev->priv;
1239
1240         mtdcr (malesr, isr);    /* clear interrupt */
1241
1242         /* clear DE interrupt */
1243         mtdcr (maltxdeir, 0xC0000000);
1244         mtdcr (malrxdeir, 0x80000000);
1245
1246 #ifdef INFO_4XX_ENET
1247         printf ("\nMAL error occured.... ISR = %lx UIC = = %lx  MAL_DEF = %lx  MAL_ERR= %lx \n", isr, uic, maldef, mal_errr);
1248 #endif
1249
1250         eth_init (hw_p->bis);   /* start again... */
1251 }
1252
1253 /*-----------------------------------------------------------------------------+
1254  *  EMAC Error Routine
1255  *-----------------------------------------------------------------------------*/
1256 static void emac_err (struct eth_device *dev, unsigned long isr)
1257 {
1258         EMAC_4XX_HW_PST hw_p = dev->priv;
1259
1260         printf ("EMAC%d error occured.... ISR = %lx\n", hw_p->devnum, isr);
1261         out32 (EMAC_ISR + hw_p->hw_addr, isr);
1262 }
1263
1264 /*-----------------------------------------------------------------------------+
1265  *  enet_rcv() handles the ethernet receive data
1266  *-----------------------------------------------------------------------------*/
1267 static void enet_rcv (struct eth_device *dev, unsigned long malisr)
1268 {
1269         struct enet_frame *ef_ptr;
1270         unsigned long data_len;
1271         unsigned long rx_eob_isr;
1272         EMAC_4XX_HW_PST hw_p = dev->priv;
1273
1274         int handled = 0;
1275         int i;
1276         int loop_count = 0;
1277
1278         rx_eob_isr = mfdcr (malrxeobisr);
1279         if ((0x80000000 >> hw_p->devnum) & rx_eob_isr) {
1280                 /* clear EOB */
1281                 mtdcr (malrxeobisr, rx_eob_isr);
1282
1283                 /* EMAC RX done */
1284                 while (1) {     /* do all */
1285                         i = hw_p->rx_slot;
1286
1287                         if ((MAL_RX_CTRL_EMPTY & hw_p->rx[i].ctrl)
1288                             || (loop_count >= NUM_RX_BUFF))
1289                                 break;
1290                         loop_count++;
1291                         hw_p->rx_slot++;
1292                         if (NUM_RX_BUFF == hw_p->rx_slot)
1293                                 hw_p->rx_slot = 0;
1294                         handled++;
1295                         data_len = (unsigned long) hw_p->rx[i].data_len;        /* Get len */
1296                         if (data_len) {
1297                                 if (data_len > ENET_MAX_MTU)    /* Check len */
1298                                         data_len = 0;
1299                                 else {
1300                                         if (EMAC_RX_ERRORS & hw_p->rx[i].ctrl) {        /* Check Errors */
1301                                                 data_len = 0;
1302                                                 hw_p->stats.rx_err_log[hw_p->
1303                                                                        rx_err_index]
1304                                                         = hw_p->rx[i].ctrl;
1305                                                 hw_p->rx_err_index++;
1306                                                 if (hw_p->rx_err_index ==
1307                                                     MAX_ERR_LOG)
1308                                                         hw_p->rx_err_index =
1309                                                                 0;
1310                                         }       /* emac_erros */
1311                                 }       /* data_len < max mtu */
1312                         }       /* if data_len */
1313                         if (!data_len) {        /* no data */
1314                                 hw_p->rx[i].ctrl |= MAL_RX_CTRL_EMPTY;  /* Free Recv Buffer */
1315
1316                                 hw_p->stats.data_len_err++;     /* Error at Rx */
1317                         }
1318
1319                         /* !data_len */
1320                         /* AS.HARNOIS */
1321                         /* Check if user has already eaten buffer */
1322                         /* if not => ERROR */
1323                         else if (hw_p->rx_ready[hw_p->rx_i_index] != -1) {
1324                                 if (hw_p->is_receiving)
1325                                         printf ("ERROR : Receive buffers are full!\n");
1326                                 break;
1327                         } else {
1328                                 hw_p->stats.rx_frames++;
1329                                 hw_p->stats.rx += data_len;
1330                                 ef_ptr = (struct enet_frame *) hw_p->rx[i].
1331                                         data_ptr;
1332 #ifdef INFO_4XX_ENET
1333                                 hw_p->stats.pkts_rx++;
1334 #endif
1335                                 /* AS.HARNOIS
1336                                  * use ring buffer
1337                                  */
1338                                 hw_p->rx_ready[hw_p->rx_i_index] = i;
1339                                 hw_p->rx_i_index++;
1340                                 if (NUM_RX_BUFF == hw_p->rx_i_index)
1341                                         hw_p->rx_i_index = 0;
1342
1343                                 /*  AS.HARNOIS
1344                                  * free receive buffer only when
1345                                  * buffer has been handled (eth_rx)
1346                                  rx[i].ctrl |= MAL_RX_CTRL_EMPTY;
1347                                  */
1348                         }       /* if data_len */
1349                 }               /* while */
1350         }                       /* if EMACK_RXCHL */
1351 }
1352
1353
1354 static int ppc_4xx_eth_rx (struct eth_device *dev)
1355 {
1356         int length;
1357         int user_index;
1358         unsigned long msr;
1359         EMAC_4XX_HW_PST hw_p = dev->priv;
1360
1361         hw_p->is_receiving = 1; /* tell driver */
1362
1363         for (;;) {
1364                 /* AS.HARNOIS
1365                  * use ring buffer and
1366                  * get index from rx buffer desciptor queue
1367                  */
1368                 user_index = hw_p->rx_ready[hw_p->rx_u_index];
1369                 if (user_index == -1) {
1370                         length = -1;
1371                         break;  /* nothing received - leave for() loop */
1372                 }
1373
1374                 msr = mfmsr ();
1375                 mtmsr (msr & ~(MSR_EE));
1376
1377                 length = hw_p->rx[user_index].data_len;
1378
1379                 /* Pass the packet up to the protocol layers. */
1380                 /*       NetReceive(NetRxPackets[rxIdx], length - 4); */
1381                 /*       NetReceive(NetRxPackets[i], length); */
1382                 NetReceive (NetRxPackets[user_index], length - 4);
1383                 /* Free Recv Buffer */
1384                 hw_p->rx[user_index].ctrl |= MAL_RX_CTRL_EMPTY;
1385                 /* Free rx buffer descriptor queue */
1386                 hw_p->rx_ready[hw_p->rx_u_index] = -1;
1387                 hw_p->rx_u_index++;
1388                 if (NUM_RX_BUFF == hw_p->rx_u_index)
1389                         hw_p->rx_u_index = 0;
1390
1391 #ifdef INFO_4XX_ENET
1392                 hw_p->stats.pkts_handled++;
1393 #endif
1394
1395                 mtmsr (msr);    /* Enable IRQ's */
1396         }
1397
1398         hw_p->is_receiving = 0; /* tell driver */
1399
1400         return length;
1401 }
1402
1403 int ppc_4xx_eth_initialize (bd_t * bis)
1404 {
1405         static int virgin = 0;
1406         struct eth_device *dev;
1407         int eth_num = 0;
1408         EMAC_4XX_HW_PST hw = NULL;
1409
1410 #if defined(CONFIG_440GX)
1411         unsigned long pfc1;
1412
1413         mfsdr (sdr_pfc1, pfc1);
1414         pfc1 &= ~(0x01e00000);
1415         pfc1 |= 0x01200000;
1416         mtsdr (sdr_pfc1, pfc1);
1417 #endif
1418         /* set phy num and mode */
1419         bis->bi_phynum[0] = CONFIG_PHY_ADDR;
1420         bis->bi_phymode[0] = 0;
1421
1422 #if defined(CONFIG_PHY1_ADDR)
1423         bis->bi_phynum[1] = CONFIG_PHY1_ADDR;
1424         bis->bi_phymode[1] = 0;
1425 #endif
1426 #if defined(CONFIG_440GX)
1427         bis->bi_phynum[2] = CONFIG_PHY2_ADDR;
1428         bis->bi_phynum[3] = CONFIG_PHY3_ADDR;
1429         bis->bi_phymode[2] = 2;
1430         bis->bi_phymode[3] = 2;
1431
1432         ppc_4xx_eth_setup_bridge(0, bis);
1433 #endif
1434
1435         for (eth_num = 0; eth_num < LAST_EMAC_NUM; eth_num++) {
1436
1437                 /* See if we can actually bring up the interface, otherwise, skip it */
1438                 switch (eth_num) {
1439                 default:                /* fall through */
1440                 case 0:
1441                         if (memcmp (bis->bi_enetaddr, "\0\0\0\0\0\0", 6) == 0) {
1442                                 bis->bi_phymode[eth_num] = BI_PHYMODE_NONE;
1443                                 continue;
1444                         }
1445                         break;
1446 #ifdef CONFIG_HAS_ETH1
1447                 case 1:
1448                         if (memcmp (bis->bi_enet1addr, "\0\0\0\0\0\0", 6) == 0) {
1449                                 bis->bi_phymode[eth_num] = BI_PHYMODE_NONE;
1450                                 continue;
1451                         }
1452                         break;
1453 #endif
1454 #ifdef CONFIG_HAS_ETH2
1455                 case 2:
1456                         if (memcmp (bis->bi_enet2addr, "\0\0\0\0\0\0", 6) == 0) {
1457                                 bis->bi_phymode[eth_num] = BI_PHYMODE_NONE;
1458                                 continue;
1459                         }
1460                         break;
1461 #endif
1462 #ifdef CONFIG_HAS_ETH3
1463                 case 3:
1464                         if (memcmp (bis->bi_enet3addr, "\0\0\0\0\0\0", 6) == 0) {
1465                                 bis->bi_phymode[eth_num] = BI_PHYMODE_NONE;
1466                                 continue;
1467                         }
1468                         break;
1469 #endif
1470                 }
1471
1472                 /* Allocate device structure */
1473                 dev = (struct eth_device *) malloc (sizeof (*dev));
1474                 if (dev == NULL) {
1475                         printf ("ppc_4xx_eth_initialize: "
1476                                 "Cannot allocate eth_device %d\n", eth_num);
1477                         return (-1);
1478                 }
1479                 memset(dev, 0, sizeof(*dev));
1480
1481                 /* Allocate our private use data */
1482                 hw = (EMAC_4XX_HW_PST) malloc (sizeof (*hw));
1483                 if (hw == NULL) {
1484                         printf ("ppc_4xx_eth_initialize: "
1485                                 "Cannot allocate private hw data for eth_device %d",
1486                                 eth_num);
1487                         free (dev);
1488                         return (-1);
1489                 }
1490                 memset(hw, 0, sizeof(*hw));
1491
1492                 switch (eth_num) {
1493                 default:                /* fall through */
1494                 case 0:
1495                         hw->hw_addr = 0;
1496                         memcpy (dev->enetaddr, bis->bi_enetaddr, 6);
1497                         break;
1498 #ifdef CONFIG_HAS_ETH1
1499                 case 1:
1500                         hw->hw_addr = 0x100;
1501                         memcpy (dev->enetaddr, bis->bi_enet1addr, 6);
1502                         break;
1503 #endif
1504 #ifdef CONFIG_HAS_ETH2
1505                 case 2:
1506                         hw->hw_addr = 0x400;
1507                         memcpy (dev->enetaddr, bis->bi_enet2addr, 6);
1508                         break;
1509 #endif
1510 #ifdef CONFIG_HAS_ETH3
1511                 case 3:
1512                         hw->hw_addr = 0x600;
1513                         memcpy (dev->enetaddr, bis->bi_enet3addr, 6);
1514                         break;
1515 #endif
1516                 }
1517
1518                 hw->devnum = eth_num;
1519                 hw->print_speed = 1;
1520
1521                 sprintf (dev->name, "ppc_4xx_eth%d", eth_num);
1522                 dev->priv = (void *) hw;
1523                 dev->init = ppc_4xx_eth_init;
1524                 dev->halt = ppc_4xx_eth_halt;
1525                 dev->send = ppc_4xx_eth_send;
1526                 dev->recv = ppc_4xx_eth_rx;
1527
1528                 if (0 == virgin) {
1529                         /* set the MAL IER ??? names may change with new spec ??? */
1530 #if defined(CONFIG_440SPE)
1531                         mal_ier =
1532                                 MAL_IER_PT | MAL_IER_PRE | MAL_IER_PWE |
1533                                 MAL_IER_DE | MAL_IER_OTE | MAL_IER_OE | MAL_IER_PE ;
1534 #else
1535                         mal_ier =
1536                                 MAL_IER_DE | MAL_IER_NE | MAL_IER_TE |
1537                                 MAL_IER_OPBE | MAL_IER_PLBE;
1538 #endif
1539                         mtdcr (malesr, 0xffffffff);     /* clear pending interrupts */
1540                         mtdcr (maltxdeir, 0xffffffff);  /* clear pending interrupts */
1541                         mtdcr (malrxdeir, 0xffffffff);  /* clear pending interrupts */
1542                         mtdcr (malier, mal_ier);
1543
1544                         /* install MAL interrupt handler */
1545                         irq_install_handler (VECNUM_MS,
1546                                              (interrupt_handler_t *) enetInt,
1547                                              dev);
1548                         irq_install_handler (VECNUM_MTE,
1549                                              (interrupt_handler_t *) enetInt,
1550                                              dev);
1551                         irq_install_handler (VECNUM_MRE,
1552                                              (interrupt_handler_t *) enetInt,
1553                                              dev);
1554                         irq_install_handler (VECNUM_TXDE,
1555                                              (interrupt_handler_t *) enetInt,
1556                                              dev);
1557                         irq_install_handler (VECNUM_RXDE,
1558                                              (interrupt_handler_t *) enetInt,
1559                                              dev);
1560                         virgin = 1;
1561                 }
1562
1563 #if defined(CONFIG_NET_MULTI)
1564                 eth_register (dev);
1565 #else
1566                 emac0_dev = dev;
1567 #endif
1568
1569 #if defined(CONFIG_NET_MULTI)
1570 #if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
1571                 miiphy_register (dev->name,
1572                                  emac4xx_miiphy_read, emac4xx_miiphy_write);
1573 #endif
1574 #endif
1575         }                       /* end for each supported device */
1576         return (1);
1577 }
1578
1579
1580 #if !defined(CONFIG_NET_MULTI)
1581 void eth_halt (void) {
1582         if (emac0_dev) {
1583                 ppc_4xx_eth_halt(emac0_dev);
1584                 free(emac0_dev);
1585                 emac0_dev = NULL;
1586         }
1587 }
1588
1589 int eth_init (bd_t *bis)
1590 {
1591         ppc_4xx_eth_initialize(bis);
1592         if (emac0_dev) {
1593                 return ppc_4xx_eth_init(emac0_dev, bis);
1594         } else {
1595                 printf("ERROR: ethaddr not set!\n");
1596                 return -1;
1597         }
1598 }
1599
1600 int eth_send(volatile void *packet, int length)
1601 {
1602         return (ppc_4xx_eth_send(emac0_dev, packet, length));
1603 }
1604
1605 int eth_rx(void)
1606 {
1607         return (ppc_4xx_eth_rx(emac0_dev));
1608 }
1609
1610 int emac4xx_miiphy_initialize (bd_t * bis)
1611 {
1612 #if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
1613         miiphy_register ("ppc_4xx_eth0",
1614                          emac4xx_miiphy_read, emac4xx_miiphy_write);
1615 #endif
1616
1617         return 0;
1618 }
1619 #endif /* !defined(CONFIG_NET_MULTI) */
1620
1621 #endif /* #if (CONFIG_COMMANDS & CFG_CMD_NET) */