]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/net/4xx_enet.c
Merge branch 'master' of git://git.denx.de/u-boot-mpc83xx
[karo-tx-uboot.git] / drivers / net / 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 <asm/io.h>
85 #include <asm/cache.h>
86 #include <asm/mmu.h>
87 #include <commproc.h>
88 #include <ppc4xx.h>
89 #include <ppc4xx_enet.h>
90 #include <405_mal.h>
91 #include <miiphy.h>
92 #include <malloc.h>
93
94 #if !(defined(CONFIG_MII) || defined(CONFIG_CMD_MII))
95 #error "CONFIG_MII has to be defined!"
96 #endif
97
98 #if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_NET_MULTI)
99 #error "CONFIG_NET_MULTI has to be defined for NetConsole"
100 #endif
101
102 #define EMAC_RESET_TIMEOUT 1000 /* 1000 ms reset timeout */
103 #define PHY_AUTONEGOTIATE_TIMEOUT 5000  /* 5000 ms autonegotiate timeout */
104
105 /* Ethernet Transmit and Receive Buffers */
106 /* AS.HARNOIS
107  * In the same way ENET_MAX_MTU and ENET_MAX_MTU_ALIGNED are set from
108  * PKTSIZE and PKTSIZE_ALIGN (include/net.h)
109  */
110 #define ENET_MAX_MTU           PKTSIZE
111 #define ENET_MAX_MTU_ALIGNED   PKTSIZE_ALIGN
112
113 /*-----------------------------------------------------------------------------+
114  * Defines for MAL/EMAC interrupt conditions as reported in the UIC (Universal
115  * Interrupt Controller).
116  *-----------------------------------------------------------------------------*/
117 #define ETH_IRQ_NUM(dev)        (VECNUM_ETH0 + ((dev) * VECNUM_ETH1_OFFS))
118
119 #if defined(CONFIG_HAS_ETH3)
120 #if !defined(CONFIG_440GX)
121 #define UIC_ETHx        (UIC_MASK(ETH_IRQ_NUM(0)) || UIC_MASK(ETH_IRQ_NUM(1)) || \
122                          UIC_MASK(ETH_IRQ_NUM(2)) || UIC_MASK(ETH_IRQ_NUM(3)))
123 #else
124 /* Unfortunately 440GX spreads EMAC interrupts on multiple UIC's */
125 #define UIC_ETHx        (UIC_MASK(ETH_IRQ_NUM(0)) || UIC_MASK(ETH_IRQ_NUM(1)))
126 #define UIC_ETHxB       (UIC_MASK(ETH_IRQ_NUM(2)) || UIC_MASK(ETH_IRQ_NUM(3)))
127 #endif /* !defined(CONFIG_440GX) */
128 #elif defined(CONFIG_HAS_ETH2)
129 #define UIC_ETHx        (UIC_MASK(ETH_IRQ_NUM(0)) || UIC_MASK(ETH_IRQ_NUM(1)) || \
130                          UIC_MASK(ETH_IRQ_NUM(2)))
131 #elif defined(CONFIG_HAS_ETH1)
132 #define UIC_ETHx        (UIC_MASK(ETH_IRQ_NUM(0)) || UIC_MASK(ETH_IRQ_NUM(1)))
133 #else
134 #define UIC_ETHx        UIC_MASK(ETH_IRQ_NUM(0))
135 #endif
136
137 /*
138  * Define a default version for UIC_ETHxB for non 440GX so that we can
139  * use common code for all 4xx variants
140  */
141 #if !defined(UIC_ETHxB)
142 #define UIC_ETHxB       0
143 #endif
144
145 #define UIC_MAL_SERR    UIC_MASK(VECNUM_MAL_SERR)
146 #define UIC_MAL_TXDE    UIC_MASK(VECNUM_MAL_TXDE)
147 #define UIC_MAL_RXDE    UIC_MASK(VECNUM_MAL_RXDE)
148 #define UIC_MAL_TXEOB   UIC_MASK(VECNUM_MAL_TXEOB)
149 #define UIC_MAL_RXEOB   UIC_MASK(VECNUM_MAL_RXEOB)
150
151 #define MAL_UIC_ERR     (UIC_MAL_SERR | UIC_MAL_TXDE | UIC_MAL_RXDE)
152 #define MAL_UIC_DEF     (UIC_MAL_RXEOB | MAL_UIC_ERR)
153
154 /*
155  * We have 3 different interrupt types:
156  * - MAL interrupts indicating successful transfer
157  * - MAL error interrupts indicating MAL related errors
158  * - EMAC interrupts indicating EMAC related errors
159  *
160  * All those interrupts can be on different UIC's, but since
161  * now at least all interrupts from one type are on the same
162  * UIC. Only exception is 440GX where the EMAC interrupts are
163  * spread over two UIC's!
164  */
165 #if defined(CONFIG_440GX)
166 #define UIC_BASE_MAL    UIC1_DCR_BASE
167 #define UIC_BASE_MAL_ERR UIC2_DCR_BASE
168 #define UIC_BASE_EMAC   UIC2_DCR_BASE
169 #define UIC_BASE_EMAC_B UIC3_DCR_BASE
170 #else
171 #define UIC_BASE_MAL    (UIC0_DCR_BASE + (UIC_NR(VECNUM_MAL_TXEOB) * 0x10))
172 #define UIC_BASE_MAL_ERR (UIC0_DCR_BASE + (UIC_NR(VECNUM_MAL_SERR) * 0x10))
173 #define UIC_BASE_EMAC   (UIC0_DCR_BASE + (UIC_NR(ETH_IRQ_NUM(0)) * 0x10))
174 #define UIC_BASE_EMAC_B (UIC0_DCR_BASE + (UIC_NR(ETH_IRQ_NUM(0)) * 0x10))
175 #endif
176
177 #undef INFO_4XX_ENET
178
179 #define BI_PHYMODE_NONE  0
180 #define BI_PHYMODE_ZMII  1
181 #define BI_PHYMODE_RGMII 2
182 #define BI_PHYMODE_GMII  3
183 #define BI_PHYMODE_RTBI  4
184 #define BI_PHYMODE_TBI   5
185 #if defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
186     defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
187     defined(CONFIG_405EX)
188 #define BI_PHYMODE_SMII  6
189 #define BI_PHYMODE_MII   7
190 #if defined(CONFIG_460EX) || defined(CONFIG_460GT)
191 #define BI_PHYMODE_RMII  8
192 #endif
193 #endif
194 #define BI_PHYMODE_SGMII 9
195
196 #if defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
197     defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
198     defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
199     defined(CONFIG_405EX)
200 #define SDR0_MFR_ETH_CLK_SEL_V(n)       ((0x01<<27) / (n+1))
201 #endif
202
203 #if defined(CONFIG_460EX) || defined(CONFIG_460GT)
204 #define SDR0_ETH_CFG_CLK_SEL_V(n)       (0x01 << (8 + n))
205 #endif
206
207 #if defined(CONFIG_460EX) || defined(CONFIG_460GT)
208 #define MAL_RX_CHAN_MUL 8       /* 460EX/GT uses MAL channel 8 for EMAC1 */
209 #else
210 #define MAL_RX_CHAN_MUL 1
211 #endif
212
213 /*--------------------------------------------------------------------+
214  * Fixed PHY (PHY-less) support for Ethernet Ports.
215  *--------------------------------------------------------------------*/
216
217 /*
218  * Some boards do not have a PHY for each ethernet port. These ports
219  * are known as Fixed PHY (or PHY-less) ports. For such ports, set
220  * the appropriate CONFIG_PHY_ADDR equal to CONFIG_FIXED_PHY and
221  * then define CONFIG_SYS_FIXED_PHY_PORTS to define what the speed and
222  * duplex should be for these ports in the board configuration
223  * file.
224  *
225  * For Example:
226  *     #define CONFIG_FIXED_PHY   0xFFFFFFFF
227  *
228  *     #define CONFIG_PHY_ADDR    CONFIG_FIXED_PHY
229  *     #define CONFIG_PHY1_ADDR   1
230  *     #define CONFIG_PHY2_ADDR   CONFIG_FIXED_PHY
231  *     #define CONFIG_PHY3_ADDR   3
232  *
233  *     #define CONFIG_SYS_FIXED_PHY_PORT(devnum,speed,duplex) \
234  *                     {devnum, speed, duplex},
235  *
236  *     #define CONFIG_SYS_FIXED_PHY_PORTS \
237  *                     CONFIG_SYS_FIXED_PHY_PORT(0,1000,FULL) \
238  *                     CONFIG_SYS_FIXED_PHY_PORT(2,100,HALF)
239  */
240
241 #ifndef CONFIG_FIXED_PHY
242 #define CONFIG_FIXED_PHY        0xFFFFFFFF /* Fixed PHY (PHY-less) */
243 #endif
244
245 #ifndef CONFIG_SYS_FIXED_PHY_PORTS
246 #define CONFIG_SYS_FIXED_PHY_PORTS      /* default is an empty array */
247 #endif
248
249 struct fixed_phy_port {
250         unsigned int devnum;    /* ethernet port */
251         unsigned int speed;     /* specified speed 10,100 or 1000 */
252         unsigned int duplex;    /* specified duplex FULL or HALF */
253 };
254
255 static const struct fixed_phy_port fixed_phy_port[] = {
256         CONFIG_SYS_FIXED_PHY_PORTS      /* defined in board configuration file */
257 };
258
259 /*-----------------------------------------------------------------------------+
260  * Global variables. TX and RX descriptors and buffers.
261  *-----------------------------------------------------------------------------*/
262 #if !defined(CONFIG_NET_MULTI)
263 struct eth_device *emac0_dev = NULL;
264 #endif
265
266 /*
267  * Get count of EMAC devices (doesn't have to be the max. possible number
268  * supported by the cpu)
269  *
270  * CONFIG_BOARD_EMAC_COUNT added so now a "dynamic" way to configure the
271  * EMAC count is possible. As it is needed for the Kilauea/Haleakala
272  * 405EX/405EXr eval board, using the same binary.
273  */
274 #if defined(CONFIG_BOARD_EMAC_COUNT)
275 #define LAST_EMAC_NUM   board_emac_count()
276 #else /* CONFIG_BOARD_EMAC_COUNT */
277 #if defined(CONFIG_HAS_ETH3)
278 #define LAST_EMAC_NUM   4
279 #elif defined(CONFIG_HAS_ETH2)
280 #define LAST_EMAC_NUM   3
281 #elif defined(CONFIG_HAS_ETH1)
282 #define LAST_EMAC_NUM   2
283 #else
284 #define LAST_EMAC_NUM   1
285 #endif
286 #endif /* CONFIG_BOARD_EMAC_COUNT */
287
288 /* normal boards start with EMAC0 */
289 #if !defined(CONFIG_EMAC_NR_START)
290 #define CONFIG_EMAC_NR_START    0
291 #endif
292
293 #define MAL_RX_DESC_SIZE        2048
294 #define MAL_TX_DESC_SIZE        2048
295 #define MAL_ALLOC_SIZE          (MAL_TX_DESC_SIZE + MAL_RX_DESC_SIZE)
296
297 /*-----------------------------------------------------------------------------+
298  * Prototypes and externals.
299  *-----------------------------------------------------------------------------*/
300 static void enet_rcv (struct eth_device *dev, unsigned long malisr);
301
302 int enetInt (struct eth_device *dev);
303 static void mal_err (struct eth_device *dev, unsigned long isr,
304                      unsigned long uic, unsigned long maldef,
305                      unsigned long mal_errr);
306 static void emac_err (struct eth_device *dev, unsigned long isr);
307
308 extern int phy_setup_aneg (char *devname, unsigned char addr);
309 extern int emac4xx_miiphy_read (char *devname, unsigned char addr,
310                 unsigned char reg, unsigned short *value);
311 extern int emac4xx_miiphy_write (char *devname, unsigned char addr,
312                 unsigned char reg, unsigned short value);
313
314 int board_emac_count(void);
315
316 static void emac_loopback_enable(EMAC_4XX_HW_PST hw_p)
317 {
318 #if defined(CONFIG_440SPE) || \
319     defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
320     defined(CONFIG_405EX)
321         u32 val;
322
323         mfsdr(sdr_mfr, val);
324         val |= SDR0_MFR_ETH_CLK_SEL_V(hw_p->devnum);
325         mtsdr(sdr_mfr, val);
326 #elif defined(CONFIG_460EX) || defined(CONFIG_460GT)
327         u32 val;
328
329         mfsdr(SDR0_ETH_CFG, val);
330         val |= SDR0_ETH_CFG_CLK_SEL_V(hw_p->devnum);
331         mtsdr(SDR0_ETH_CFG, val);
332 #endif
333 }
334
335 static void emac_loopback_disable(EMAC_4XX_HW_PST hw_p)
336 {
337 #if defined(CONFIG_440SPE) || \
338     defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
339     defined(CONFIG_405EX)
340         u32 val;
341
342         mfsdr(sdr_mfr, val);
343         val &= ~SDR0_MFR_ETH_CLK_SEL_V(hw_p->devnum);
344         mtsdr(sdr_mfr, val);
345 #elif defined(CONFIG_460EX) || defined(CONFIG_460GT)
346         u32 val;
347
348         mfsdr(SDR0_ETH_CFG, val);
349         val &= ~SDR0_ETH_CFG_CLK_SEL_V(hw_p->devnum);
350         mtsdr(SDR0_ETH_CFG, val);
351 #endif
352 }
353
354 /*-----------------------------------------------------------------------------+
355 | ppc_4xx_eth_halt
356 | Disable MAL channel, and EMACn
357 +-----------------------------------------------------------------------------*/
358 static void ppc_4xx_eth_halt (struct eth_device *dev)
359 {
360         EMAC_4XX_HW_PST hw_p = dev->priv;
361         u32 val = 10000;
362
363         out_be32((void *)EMAC_IER + hw_p->hw_addr, 0x00000000); /* disable emac interrupts */
364
365         /* 1st reset MAL channel */
366         /* Note: writing a 0 to a channel has no effect */
367 #if defined(CONFIG_405EP) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
368         mtdcr (maltxcarr, (MAL_CR_MMSR >> (hw_p->devnum * 2)));
369 #else
370         mtdcr (maltxcarr, (MAL_CR_MMSR >> hw_p->devnum));
371 #endif
372         mtdcr (malrxcarr, (MAL_CR_MMSR >> hw_p->devnum));
373
374         /* wait for reset */
375         while (mfdcr (malrxcasr) & (MAL_CR_MMSR >> hw_p->devnum)) {
376                 udelay (1000);  /* Delay 1 MS so as not to hammer the register */
377                 val--;
378                 if (val == 0)
379                         break;
380         }
381
382         /* provide clocks for EMAC internal loopback  */
383         emac_loopback_enable(hw_p);
384
385         /* EMAC RESET */
386         out_be32((void *)EMAC_M0 + hw_p->hw_addr, EMAC_M0_SRST);
387
388         /* remove clocks for EMAC internal loopback  */
389         emac_loopback_disable(hw_p);
390
391 #ifndef CONFIG_NETCONSOLE
392         hw_p->print_speed = 1;  /* print speed message again next time */
393 #endif
394
395 #if defined(CONFIG_460EX) || defined(CONFIG_460GT)
396         /* don't bypass the TAHOE0/TAHOE1 cores for Linux */
397         mfsdr(SDR0_ETH_CFG, val);
398         val &= ~(SDR0_ETH_CFG_TAHOE0_BYPASS | SDR0_ETH_CFG_TAHOE1_BYPASS);
399         mtsdr(SDR0_ETH_CFG, val);
400 #endif
401
402         return;
403 }
404
405 #if defined (CONFIG_440GX)
406 int ppc_4xx_eth_setup_bridge(int devnum, bd_t * bis)
407 {
408         unsigned long pfc1;
409         unsigned long zmiifer;
410         unsigned long rmiifer;
411
412         mfsdr(sdr_pfc1, pfc1);
413         pfc1 = SDR0_PFC1_EPS_DECODE(pfc1);
414
415         zmiifer = 0;
416         rmiifer = 0;
417
418         switch (pfc1) {
419         case 1:
420                 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
421                 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(1);
422                 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(2);
423                 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(3);
424                 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
425                 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
426                 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
427                 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
428                 break;
429         case 2:
430                 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(0);
431                 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(1);
432                 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(2);
433                 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(3);
434                 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
435                 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
436                 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
437                 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
438                 break;
439         case 3:
440                 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
441                 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
442                 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
443                 bis->bi_phymode[1] = BI_PHYMODE_NONE;
444                 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
445                 bis->bi_phymode[3] = BI_PHYMODE_NONE;
446                 break;
447         case 4:
448                 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(0);
449                 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(1);
450                 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V (2);
451                 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V (3);
452                 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
453                 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
454                 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
455                 bis->bi_phymode[3] = BI_PHYMODE_RGMII;
456                 break;
457         case 5:
458                 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (0);
459                 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (1);
460                 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (2);
461                 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(3);
462                 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
463                 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
464                 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
465                 bis->bi_phymode[3] = BI_PHYMODE_RGMII;
466                 break;
467         case 6:
468                 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (0);
469                 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (1);
470                 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
471                 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
472                 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
473                 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
474                 break;
475         case 0:
476         default:
477                 zmiifer = ZMII_FER_MII << ZMII_FER_V(devnum);
478                 rmiifer = 0x0;
479                 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
480                 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
481                 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
482                 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
483                 break;
484         }
485
486         /* Ensure we setup mdio for this devnum and ONLY this devnum */
487         zmiifer |= (ZMII_FER_MDI) << ZMII_FER_V(devnum);
488
489         out_be32((void *)ZMII_FER, zmiifer);
490         out_be32((void *)RGMII_FER, rmiifer);
491
492         return ((int)pfc1);
493 }
494 #endif  /* CONFIG_440_GX */
495
496 #if defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
497 int ppc_4xx_eth_setup_bridge(int devnum, bd_t * bis)
498 {
499         unsigned long zmiifer=0x0;
500         unsigned long pfc1;
501
502         mfsdr(sdr_pfc1, pfc1);
503         pfc1 &= SDR0_PFC1_SELECT_MASK;
504
505         switch (pfc1) {
506         case SDR0_PFC1_SELECT_CONFIG_2:
507                 /* 1 x GMII port */
508                 out_be32((void *)ZMII_FER, 0x00);
509                 out_be32((void *)RGMII_FER, 0x00000037);
510                 bis->bi_phymode[0] = BI_PHYMODE_GMII;
511                 bis->bi_phymode[1] = BI_PHYMODE_NONE;
512                 break;
513         case SDR0_PFC1_SELECT_CONFIG_4:
514                 /* 2 x RGMII ports */
515                 out_be32((void *)ZMII_FER, 0x00);
516                 out_be32((void *)RGMII_FER, 0x00000055);
517                 bis->bi_phymode[0] = BI_PHYMODE_RGMII;
518                 bis->bi_phymode[1] = BI_PHYMODE_RGMII;
519                 break;
520         case SDR0_PFC1_SELECT_CONFIG_6:
521                 /* 2 x SMII ports */
522                 out_be32((void *)ZMII_FER,
523                          ((ZMII_FER_SMII) << ZMII_FER_V(0)) |
524                          ((ZMII_FER_SMII) << ZMII_FER_V(1)));
525                 out_be32((void *)RGMII_FER, 0x00000000);
526                 bis->bi_phymode[0] = BI_PHYMODE_SMII;
527                 bis->bi_phymode[1] = BI_PHYMODE_SMII;
528                 break;
529         case SDR0_PFC1_SELECT_CONFIG_1_2:
530                 /* only 1 x MII supported */
531                 out_be32((void *)ZMII_FER, (ZMII_FER_MII) << ZMII_FER_V(0));
532                 out_be32((void *)RGMII_FER, 0x00000000);
533                 bis->bi_phymode[0] = BI_PHYMODE_MII;
534                 bis->bi_phymode[1] = BI_PHYMODE_NONE;
535                 break;
536         default:
537                 break;
538         }
539
540         /* Ensure we setup mdio for this devnum and ONLY this devnum */
541         zmiifer = in_be32((void *)ZMII_FER);
542         zmiifer |= (ZMII_FER_MDI) << ZMII_FER_V(devnum);
543         out_be32((void *)ZMII_FER, zmiifer);
544
545         return ((int)0x0);
546 }
547 #endif  /* CONFIG_440EPX */
548
549 #if defined(CONFIG_405EX)
550 int ppc_4xx_eth_setup_bridge(int devnum, bd_t * bis)
551 {
552         u32 rgmiifer = 0;
553
554         /*
555          * The 405EX(r)'s RGMII bridge can operate in one of several
556          * modes, only one of which (2 x RGMII) allows the
557          * simultaneous use of both EMACs on the 405EX.
558          */
559
560         switch (CONFIG_EMAC_PHY_MODE) {
561
562         case EMAC_PHY_MODE_NONE:
563                 /* No ports */
564                 rgmiifer |= RGMII_FER_DIS       << 0;
565                 rgmiifer |= RGMII_FER_DIS       << 4;
566                 out_be32((void *)RGMII_FER, rgmiifer);
567                 bis->bi_phymode[0] = BI_PHYMODE_NONE;
568                 bis->bi_phymode[1] = BI_PHYMODE_NONE;
569                 break;
570         case EMAC_PHY_MODE_NONE_RGMII:
571                 /* 1 x RGMII port on channel 0 */
572                 rgmiifer |= RGMII_FER_RGMII     << 0;
573                 rgmiifer |= RGMII_FER_DIS       << 4;
574                 out_be32((void *)RGMII_FER, rgmiifer);
575                 bis->bi_phymode[0] = BI_PHYMODE_RGMII;
576                 bis->bi_phymode[1] = BI_PHYMODE_NONE;
577                 break;
578         case EMAC_PHY_MODE_RGMII_NONE:
579                 /* 1 x RGMII port on channel 1 */
580                 rgmiifer |= RGMII_FER_DIS       << 0;
581                 rgmiifer |= RGMII_FER_RGMII     << 4;
582                 out_be32((void *)RGMII_FER, rgmiifer);
583                 bis->bi_phymode[0] = BI_PHYMODE_NONE;
584                 bis->bi_phymode[1] = BI_PHYMODE_RGMII;
585                 break;
586         case EMAC_PHY_MODE_RGMII_RGMII:
587                 /* 2 x RGMII ports */
588                 rgmiifer |= RGMII_FER_RGMII     << 0;
589                 rgmiifer |= RGMII_FER_RGMII     << 4;
590                 out_be32((void *)RGMII_FER, rgmiifer);
591                 bis->bi_phymode[0] = BI_PHYMODE_RGMII;
592                 bis->bi_phymode[1] = BI_PHYMODE_RGMII;
593                 break;
594         case EMAC_PHY_MODE_NONE_GMII:
595                 /* 1 x GMII port on channel 0 */
596                 rgmiifer |= RGMII_FER_GMII      << 0;
597                 rgmiifer |= RGMII_FER_DIS       << 4;
598                 out_be32((void *)RGMII_FER, rgmiifer);
599                 bis->bi_phymode[0] = BI_PHYMODE_GMII;
600                 bis->bi_phymode[1] = BI_PHYMODE_NONE;
601                 break;
602         case EMAC_PHY_MODE_NONE_MII:
603                 /* 1 x MII port on channel 0 */
604                 rgmiifer |= RGMII_FER_MII       << 0;
605                 rgmiifer |= RGMII_FER_DIS       << 4;
606                 out_be32((void *)RGMII_FER, rgmiifer);
607                 bis->bi_phymode[0] = BI_PHYMODE_MII;
608                 bis->bi_phymode[1] = BI_PHYMODE_NONE;
609                 break;
610         case EMAC_PHY_MODE_GMII_NONE:
611                 /* 1 x GMII port on channel 1 */
612                 rgmiifer |= RGMII_FER_DIS       << 0;
613                 rgmiifer |= RGMII_FER_GMII      << 4;
614                 out_be32((void *)RGMII_FER, rgmiifer);
615                 bis->bi_phymode[0] = BI_PHYMODE_NONE;
616                 bis->bi_phymode[1] = BI_PHYMODE_GMII;
617                 break;
618         case EMAC_PHY_MODE_MII_NONE:
619                 /* 1 x MII port on channel 1 */
620                 rgmiifer |= RGMII_FER_DIS       << 0;
621                 rgmiifer |= RGMII_FER_MII       << 4;
622                 out_be32((void *)RGMII_FER, rgmiifer);
623                 bis->bi_phymode[0] = BI_PHYMODE_NONE;
624                 bis->bi_phymode[1] = BI_PHYMODE_MII;
625                 break;
626         default:
627                 break;
628         }
629
630         /* Ensure we setup mdio for this devnum and ONLY this devnum */
631         rgmiifer = in_be32((void *)RGMII_FER);
632         rgmiifer |= (1 << (19-devnum));
633         out_be32((void *)RGMII_FER, rgmiifer);
634
635         return ((int)0x0);
636 }
637 #endif  /* CONFIG_405EX */
638
639 #if defined(CONFIG_460EX) || defined(CONFIG_460GT)
640 int ppc_4xx_eth_setup_bridge(int devnum, bd_t * bis)
641 {
642         u32 eth_cfg;
643         u32 zmiifer;            /* ZMII0_FER reg. */
644         u32 rmiifer;            /* RGMII0_FER reg. Bridge 0 */
645         u32 rmiifer1;           /* RGMII0_FER reg. Bridge 1 */
646         int mode;
647
648         zmiifer  = 0;
649         rmiifer  = 0;
650         rmiifer1 = 0;
651
652 #if defined(CONFIG_460EX)
653         mode = 9;
654         mfsdr(SDR0_ETH_CFG, eth_cfg);
655         if (((eth_cfg & SDR0_ETH_CFG_SGMII0_ENABLE) > 0) &&
656             ((eth_cfg & SDR0_ETH_CFG_SGMII1_ENABLE) > 0))
657                 mode = 11; /* config SGMII */
658 #else
659         mode = 10;
660         mfsdr(SDR0_ETH_CFG, eth_cfg);
661         if (((eth_cfg & SDR0_ETH_CFG_SGMII0_ENABLE) > 0) &&
662             ((eth_cfg & SDR0_ETH_CFG_SGMII1_ENABLE) > 0) &&
663             ((eth_cfg & SDR0_ETH_CFG_SGMII2_ENABLE) > 0))
664                 mode = 12; /* config SGMII */
665 #endif
666
667         /* TODO:
668          * NOTE: 460GT has 2 RGMII bridge cores:
669          *              emac0 ------ RGMII0_BASE
670          *                         |
671          *              emac1 -----+
672          *
673          *              emac2 ------ RGMII1_BASE
674          *                         |
675          *              emac3 -----+
676          *
677          *      460EX has 1 RGMII bridge core:
678          *      and RGMII1_BASE is disabled
679          *              emac0 ------ RGMII0_BASE
680          *                         |
681          *              emac1 -----+
682          */
683
684         /*
685          * Right now only 2*RGMII is supported. Please extend when needed.
686          * sr - 2008-02-19
687          * Add SGMII support.
688          * vg - 2008-07-28
689          */
690         switch (mode) {
691         case 1:
692                 /* 1 MII - 460EX */
693                 /* GMC0 EMAC4_0, ZMII Bridge */
694                 zmiifer |= ZMII_FER_MII << ZMII_FER_V(0);
695                 bis->bi_phymode[0] = BI_PHYMODE_MII;
696                 bis->bi_phymode[1] = BI_PHYMODE_NONE;
697                 bis->bi_phymode[2] = BI_PHYMODE_NONE;
698                 bis->bi_phymode[3] = BI_PHYMODE_NONE;
699                 break;
700         case 2:
701                 /* 2 MII - 460GT */
702                 /* GMC0 EMAC4_0, GMC1 EMAC4_2, ZMII Bridge */
703                 zmiifer |= ZMII_FER_MII << ZMII_FER_V(0);
704                 zmiifer |= ZMII_FER_MII << ZMII_FER_V(2);
705                 bis->bi_phymode[0] = BI_PHYMODE_MII;
706                 bis->bi_phymode[1] = BI_PHYMODE_NONE;
707                 bis->bi_phymode[2] = BI_PHYMODE_MII;
708                 bis->bi_phymode[3] = BI_PHYMODE_NONE;
709                 break;
710         case 3:
711                 /* 2 RMII - 460EX */
712                 /* GMC0 EMAC4_0, GMC0 EMAC4_1, ZMII Bridge */
713                 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
714                 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(1);
715                 bis->bi_phymode[0] = BI_PHYMODE_RMII;
716                 bis->bi_phymode[1] = BI_PHYMODE_RMII;
717                 bis->bi_phymode[2] = BI_PHYMODE_NONE;
718                 bis->bi_phymode[3] = BI_PHYMODE_NONE;
719                 break;
720         case 4:
721                 /* 4 RMII - 460GT */
722                 /* GMC0 EMAC4_0, GMC0 EMAC4_1, GMC1 EMAC4_2, GMC1, EMAC4_3 */
723                 /* ZMII Bridge */
724                 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
725                 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(1);
726                 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(2);
727                 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(3);
728                 bis->bi_phymode[0] = BI_PHYMODE_RMII;
729                 bis->bi_phymode[1] = BI_PHYMODE_RMII;
730                 bis->bi_phymode[2] = BI_PHYMODE_RMII;
731                 bis->bi_phymode[3] = BI_PHYMODE_RMII;
732                 break;
733         case 5:
734                 /* 2 SMII - 460EX */
735                 /* GMC0 EMAC4_0, GMC0 EMAC4_1, ZMII Bridge */
736                 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(0);
737                 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(1);
738                 bis->bi_phymode[0] = BI_PHYMODE_SMII;
739                 bis->bi_phymode[1] = BI_PHYMODE_SMII;
740                 bis->bi_phymode[2] = BI_PHYMODE_NONE;
741                 bis->bi_phymode[3] = BI_PHYMODE_NONE;
742                 break;
743         case 6:
744                 /* 4 SMII - 460GT */
745                 /* GMC0 EMAC4_0, GMC0 EMAC4_1, GMC0 EMAC4_3, GMC0 EMAC4_3 */
746                 /* ZMII Bridge */
747                 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(0);
748                 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(1);
749                 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(2);
750                 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(3);
751                 bis->bi_phymode[0] = BI_PHYMODE_SMII;
752                 bis->bi_phymode[1] = BI_PHYMODE_SMII;
753                 bis->bi_phymode[2] = BI_PHYMODE_SMII;
754                 bis->bi_phymode[3] = BI_PHYMODE_SMII;
755                 break;
756         case 7:
757                 /* This is the default mode that we want for board bringup - Maple */
758                 /* 1 GMII - 460EX */
759                 /* GMC0 EMAC4_0, RGMII Bridge 0 */
760                 rmiifer |= RGMII_FER_MDIO(0);
761
762                 if (devnum == 0) {
763                         rmiifer |= RGMII_FER_GMII << RGMII_FER_V(2); /* CH0CFG - EMAC0 */
764                         bis->bi_phymode[0] = BI_PHYMODE_GMII;
765                         bis->bi_phymode[1] = BI_PHYMODE_NONE;
766                         bis->bi_phymode[2] = BI_PHYMODE_NONE;
767                         bis->bi_phymode[3] = BI_PHYMODE_NONE;
768                 } else {
769                         rmiifer |= RGMII_FER_GMII << RGMII_FER_V(3); /* CH1CFG - EMAC1 */
770                         bis->bi_phymode[0] = BI_PHYMODE_NONE;
771                         bis->bi_phymode[1] = BI_PHYMODE_GMII;
772                         bis->bi_phymode[2] = BI_PHYMODE_NONE;
773                         bis->bi_phymode[3] = BI_PHYMODE_NONE;
774                 }
775                 break;
776         case 8:
777                 /* 2 GMII - 460GT */
778                 /* GMC0 EMAC4_0, RGMII Bridge 0 */
779                 /* GMC1 EMAC4_2, RGMII Bridge 1 */
780                 rmiifer |= RGMII_FER_GMII << RGMII_FER_V(2);    /* CH0CFG - EMAC0 */
781                 rmiifer1 |= RGMII_FER_GMII << RGMII_FER_V(2);   /* CH0CFG - EMAC2 */
782                 rmiifer |= RGMII_FER_MDIO(0);                   /* enable MDIO - EMAC0 */
783                 rmiifer1 |= RGMII_FER_MDIO(0);                  /* enable MDIO - EMAC2 */
784
785                 bis->bi_phymode[0] = BI_PHYMODE_GMII;
786                 bis->bi_phymode[1] = BI_PHYMODE_NONE;
787                 bis->bi_phymode[2] = BI_PHYMODE_GMII;
788                 bis->bi_phymode[3] = BI_PHYMODE_NONE;
789                 break;
790         case 9:
791                 /* 2 RGMII - 460EX */
792                 /* GMC0 EMAC4_0, GMC0 EMAC4_1, RGMII Bridge 0 */
793                 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
794                 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(3);
795                 rmiifer |= RGMII_FER_MDIO(0);                   /* enable MDIO - EMAC0 */
796
797                 bis->bi_phymode[0] = BI_PHYMODE_RGMII;
798                 bis->bi_phymode[1] = BI_PHYMODE_RGMII;
799                 bis->bi_phymode[2] = BI_PHYMODE_NONE;
800                 bis->bi_phymode[3] = BI_PHYMODE_NONE;
801                 break;
802         case 10:
803                 /* 4 RGMII - 460GT */
804                 /* GMC0 EMAC4_0, GMC0 EMAC4_1, RGMII Bridge 0 */
805                 /* GMC1 EMAC4_2, GMC1 EMAC4_3, RGMII Bridge 1 */
806                 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
807                 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(3);
808                 rmiifer1 |= RGMII_FER_RGMII << RGMII_FER_V(2);
809                 rmiifer1 |= RGMII_FER_RGMII << RGMII_FER_V(3);
810                 bis->bi_phymode[0] = BI_PHYMODE_RGMII;
811                 bis->bi_phymode[1] = BI_PHYMODE_RGMII;
812                 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
813                 bis->bi_phymode[3] = BI_PHYMODE_RGMII;
814                 break;
815         case 11:
816                 /* 2 SGMII - 460EX */
817                 bis->bi_phymode[0] = BI_PHYMODE_SGMII;
818                 bis->bi_phymode[1] = BI_PHYMODE_SGMII;
819                 bis->bi_phymode[2] = BI_PHYMODE_NONE;
820                 bis->bi_phymode[3] = BI_PHYMODE_NONE;
821                 break;
822         case 12:
823                 /* 3 SGMII - 460GT */
824                 bis->bi_phymode[0] = BI_PHYMODE_SGMII;
825                 bis->bi_phymode[1] = BI_PHYMODE_SGMII;
826                 bis->bi_phymode[2] = BI_PHYMODE_SGMII;
827                 bis->bi_phymode[3] = BI_PHYMODE_NONE;
828                 break;
829         default:
830                 break;
831         }
832
833         /* Set EMAC for MDIO */
834         mfsdr(SDR0_ETH_CFG, eth_cfg);
835         eth_cfg |= SDR0_ETH_CFG_MDIO_SEL_EMAC0;
836         mtsdr(SDR0_ETH_CFG, eth_cfg);
837
838         out_be32((void *)RGMII_FER, rmiifer);
839 #if defined(CONFIG_460GT)
840         out_be32((void *)RGMII_FER + RGMII1_BASE_OFFSET, rmiifer1);
841 #endif
842
843         /* bypass the TAHOE0/TAHOE1 cores for U-Boot */
844         mfsdr(SDR0_ETH_CFG, eth_cfg);
845         eth_cfg |= (SDR0_ETH_CFG_TAHOE0_BYPASS | SDR0_ETH_CFG_TAHOE1_BYPASS);
846         mtsdr(SDR0_ETH_CFG, eth_cfg);
847
848         return 0;
849 }
850 #endif /* CONFIG_460EX || CONFIG_460GT */
851
852 static inline void *malloc_aligned(u32 size, u32 align)
853 {
854         return (void *)(((u32)malloc(size + align) + align - 1) &
855                         ~(align - 1));
856 }
857
858 static int ppc_4xx_eth_init (struct eth_device *dev, bd_t * bis)
859 {
860         int i;
861         unsigned long reg = 0;
862         unsigned long msr;
863         unsigned long speed;
864         unsigned long duplex;
865         unsigned long failsafe;
866         unsigned mode_reg;
867         unsigned short devnum;
868         unsigned short reg_short;
869 #if defined(CONFIG_440GX) || \
870     defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
871     defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
872     defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
873     defined(CONFIG_405EX)
874         u32 opbfreq;
875         sys_info_t sysinfo;
876 #if defined(CONFIG_440GX) || defined(CONFIG_440SPE) || \
877     defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
878     defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
879     defined(CONFIG_405EX)
880         int ethgroup = -1;
881 #endif
882 #endif
883         u32 bd_cached;
884         u32 bd_uncached = 0;
885 #ifdef CONFIG_4xx_DCACHE
886         static u32 last_used_ea = 0;
887 #endif
888 #if defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
889     defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
890     defined(CONFIG_405EX)
891         int rgmii_channel;
892 #endif
893
894         EMAC_4XX_HW_PST hw_p = dev->priv;
895
896         /* before doing anything, figure out if we have a MAC address */
897         /* if not, bail */
898         if (memcmp (dev->enetaddr, "\0\0\0\0\0\0", 6) == 0) {
899                 printf("ERROR: ethaddr not set!\n");
900                 return -1;
901         }
902
903 #if defined(CONFIG_440GX) || \
904     defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
905     defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
906     defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
907     defined(CONFIG_405EX)
908         /* Need to get the OPB frequency so we can access the PHY */
909         get_sys_info (&sysinfo);
910 #endif
911
912         msr = mfmsr ();
913         mtmsr (msr & ~(MSR_EE));        /* disable interrupts */
914
915         devnum = hw_p->devnum;
916
917 #ifdef INFO_4XX_ENET
918         /* AS.HARNOIS
919          * We should have :
920          * hw_p->stats.pkts_handled <=  hw_p->stats.pkts_rx <= hw_p->stats.pkts_handled+PKTBUFSRX
921          * In the most cases hw_p->stats.pkts_handled = hw_p->stats.pkts_rx, but it
922          * is possible that new packets (without relationship with
923          * current transfer) have got the time to arrived before
924          * netloop calls eth_halt
925          */
926         printf ("About preceeding transfer (eth%d):\n"
927                 "- Sent packet number %d\n"
928                 "- Received packet number %d\n"
929                 "- Handled packet number %d\n",
930                 hw_p->devnum,
931                 hw_p->stats.pkts_tx,
932                 hw_p->stats.pkts_rx, hw_p->stats.pkts_handled);
933
934         hw_p->stats.pkts_tx = 0;
935         hw_p->stats.pkts_rx = 0;
936         hw_p->stats.pkts_handled = 0;
937         hw_p->print_speed = 1;  /* print speed message again next time */
938 #endif
939
940         hw_p->tx_err_index = 0; /* Transmit Error Index for tx_err_log */
941         hw_p->rx_err_index = 0; /* Receive Error Index for rx_err_log */
942
943         hw_p->rx_slot = 0;      /* MAL Receive Slot */
944         hw_p->rx_i_index = 0;   /* Receive Interrupt Queue Index */
945         hw_p->rx_u_index = 0;   /* Receive User Queue Index */
946
947         hw_p->tx_slot = 0;      /* MAL Transmit Slot */
948         hw_p->tx_i_index = 0;   /* Transmit Interrupt Queue Index */
949         hw_p->tx_u_index = 0;   /* Transmit User Queue Index */
950
951 #if defined(CONFIG_440) && !defined(CONFIG_440SP) && !defined(CONFIG_440SPE)
952         /* set RMII mode */
953         /* NOTE: 440GX spec states that mode is mutually exclusive */
954         /* NOTE: Therefore, disable all other EMACS, since we handle */
955         /* NOTE: only one emac at a time */
956         reg = 0;
957         out_be32((void *)ZMII_FER, 0);
958         udelay (100);
959
960 #if defined(CONFIG_440GP) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
961         out_be32((void *)ZMII_FER, (ZMII_FER_RMII | ZMII_FER_MDI) << ZMII_FER_V (devnum));
962 #elif defined(CONFIG_440GX) || \
963     defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
964     defined(CONFIG_460EX) || defined(CONFIG_460GT)
965         ethgroup = ppc_4xx_eth_setup_bridge(devnum, bis);
966 #endif
967
968         out_be32((void *)ZMII_SSR, ZMII_SSR_SP << ZMII_SSR_V(devnum));
969 #endif /* defined(CONFIG_440) && !defined(CONFIG_440SP) */
970 #if defined(CONFIG_405EX)
971         ethgroup = ppc_4xx_eth_setup_bridge(devnum, bis);
972 #endif
973
974         sync();
975
976         /* provide clocks for EMAC internal loopback  */
977         emac_loopback_enable(hw_p);
978
979         /* EMAC RESET */
980         out_be32((void *)EMAC_M0 + hw_p->hw_addr, EMAC_M0_SRST);
981
982         /* remove clocks for EMAC internal loopback  */
983         emac_loopback_disable(hw_p);
984
985         failsafe = 1000;
986         while ((in_be32((void *)EMAC_M0 + hw_p->hw_addr) & (EMAC_M0_SRST)) && failsafe) {
987                 udelay (1000);
988                 failsafe--;
989         }
990         if (failsafe <= 0)
991                 printf("\nProblem resetting EMAC!\n");
992
993 #if defined(CONFIG_440GX) || \
994     defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
995     defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
996     defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
997     defined(CONFIG_405EX)
998         /* Whack the M1 register */
999         mode_reg = 0x0;
1000         mode_reg &= ~0x00000038;
1001         opbfreq = sysinfo.freqOPB / 1000000;
1002         if (opbfreq <= 50);
1003         else if (opbfreq <= 66)
1004                 mode_reg |= EMAC_M1_OBCI_66;
1005         else if (opbfreq <= 83)
1006                 mode_reg |= EMAC_M1_OBCI_83;
1007         else if (opbfreq <= 100)
1008                 mode_reg |= EMAC_M1_OBCI_100;
1009         else
1010                 mode_reg |= EMAC_M1_OBCI_GT100;
1011
1012         out_be32((void *)EMAC_M1 + hw_p->hw_addr, mode_reg);
1013 #endif /* defined(CONFIG_440GX) || defined(CONFIG_440SP) */
1014
1015 #if defined(CONFIG_GPCS_PHY_ADDR) || defined(CONFIG_GPCS_PHY1_ADDR) || \
1016     defined(CONFIG_GPCS_PHY2_ADDR) || defined(CONFIG_GPCS_PHY3_ADDR)
1017         if (bis->bi_phymode[devnum] == BI_PHYMODE_SGMII) {
1018                 /*
1019                  * In SGMII mode, GPCS access is needed for
1020                  * communication with the internal SGMII SerDes.
1021                  */
1022                 switch (devnum) {
1023 #if defined(CONFIG_GPCS_PHY_ADDR)
1024                 case 0:
1025                         reg = CONFIG_GPCS_PHY_ADDR;
1026                         break;
1027 #endif
1028 #if defined(CONFIG_GPCS_PHY1_ADDR)
1029                 case 1:
1030                         reg = CONFIG_GPCS_PHY1_ADDR;
1031                         break;
1032 #endif
1033 #if defined(CONFIG_GPCS_PHY2_ADDR)
1034                 case 2:
1035                         reg = CONFIG_GPCS_PHY2_ADDR;
1036                         break;
1037 #endif
1038 #if defined(CONFIG_GPCS_PHY3_ADDR)
1039                 case 3:
1040                         reg = CONFIG_GPCS_PHY3_ADDR;
1041                         break;
1042 #endif
1043                 }
1044
1045                 mode_reg = in_be32((void *)EMAC_M1 + hw_p->hw_addr);
1046                 mode_reg |= EMAC_M1_MF_1000GPCS | EMAC_M1_IPPA_SET(reg);
1047                 out_be32((void *)EMAC_M1 + hw_p->hw_addr, mode_reg);
1048
1049                 /* Configure GPCS interface to recommended setting for SGMII */
1050                 miiphy_reset(dev->name, reg);
1051                 miiphy_write(dev->name, reg, 0x04, 0x8120); /* AsymPause, FDX */
1052                 miiphy_write(dev->name, reg, 0x07, 0x2801); /* msg_pg, toggle */
1053                 miiphy_write(dev->name, reg, 0x00, 0x0140); /* 1Gbps, FDX     */
1054         }
1055 #endif /* defined(CONFIG_GPCS_PHY_ADDR) */
1056
1057         /* wait for PHY to complete auto negotiation */
1058         reg_short = 0;
1059         switch (devnum) {
1060         case 0:
1061                 reg = CONFIG_PHY_ADDR;
1062                 break;
1063 #if defined (CONFIG_PHY1_ADDR)
1064         case 1:
1065                 reg = CONFIG_PHY1_ADDR;
1066                 break;
1067 #endif
1068 #if defined (CONFIG_PHY2_ADDR)
1069         case 2:
1070                 reg = CONFIG_PHY2_ADDR;
1071                 break;
1072 #endif
1073 #if defined (CONFIG_PHY3_ADDR)
1074         case 3:
1075                 reg = CONFIG_PHY3_ADDR;
1076                 break;
1077 #endif
1078         default:
1079                 reg = CONFIG_PHY_ADDR;
1080                 break;
1081         }
1082
1083         bis->bi_phynum[devnum] = reg;
1084
1085         if (reg == CONFIG_FIXED_PHY)
1086                 goto get_speed;
1087
1088 #if defined(CONFIG_PHY_RESET)
1089         /*
1090          * Reset the phy, only if its the first time through
1091          * otherwise, just check the speeds & feeds
1092          */
1093         if (hw_p->first_init == 0) {
1094 #if defined(CONFIG_M88E1111_PHY)
1095                 miiphy_write (dev->name, reg, 0x14, 0x0ce3);
1096                 miiphy_write (dev->name, reg, 0x18, 0x4101);
1097                 miiphy_write (dev->name, reg, 0x09, 0x0e00);
1098                 miiphy_write (dev->name, reg, 0x04, 0x01e1);
1099 #endif
1100 #if defined(CONFIG_M88E1112_PHY)
1101                 if (bis->bi_phymode[devnum] == BI_PHYMODE_SGMII) {
1102                         /*
1103                          * Marvell 88E1112 PHY needs to have the SGMII MAC
1104                          * interace (page 2) properly configured to
1105                          * communicate with the 460EX/GT GPCS interface.
1106                          */
1107
1108                         /* Set access to Page 2 */
1109                         miiphy_write(dev->name, reg, 0x16, 0x0002);
1110
1111                         miiphy_write(dev->name, reg, 0x00, 0x0040); /* 1Gbps */
1112                         miiphy_read(dev->name, reg, 0x1a, &reg_short);
1113                         reg_short |= 0x8000; /* bypass Auto-Negotiation */
1114                         miiphy_write(dev->name, reg, 0x1a, reg_short);
1115                         miiphy_reset(dev->name, reg); /* reset MAC interface */
1116
1117                         /* Reset access to Page 0 */
1118                         miiphy_write(dev->name, reg, 0x16, 0x0000);
1119                 }
1120 #endif /* defined(CONFIG_M88E1112_PHY) */
1121                 miiphy_reset (dev->name, reg);
1122
1123 #if defined(CONFIG_440GX) || \
1124     defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
1125     defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
1126     defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
1127     defined(CONFIG_405EX)
1128
1129 #if defined(CONFIG_CIS8201_PHY)
1130                 /*
1131                  * Cicada 8201 PHY needs to have an extended register whacked
1132                  * for RGMII mode.
1133                  */
1134                 if (((devnum == 2) || (devnum == 3)) && (4 == ethgroup)) {
1135 #if defined(CONFIG_CIS8201_SHORT_ETCH)
1136                         miiphy_write (dev->name, reg, 23, 0x1300);
1137 #else
1138                         miiphy_write (dev->name, reg, 23, 0x1000);
1139 #endif
1140                         /*
1141                          * Vitesse VSC8201/Cicada CIS8201 errata:
1142                          * Interoperability problem with Intel 82547EI phys
1143                          * This work around (provided by Vitesse) changes
1144                          * the default timer convergence from 8ms to 12ms
1145                          */
1146                         miiphy_write (dev->name, reg, 0x1f, 0x2a30);
1147                         miiphy_write (dev->name, reg, 0x08, 0x0200);
1148                         miiphy_write (dev->name, reg, 0x1f, 0x52b5);
1149                         miiphy_write (dev->name, reg, 0x02, 0x0004);
1150                         miiphy_write (dev->name, reg, 0x01, 0x0671);
1151                         miiphy_write (dev->name, reg, 0x00, 0x8fae);
1152                         miiphy_write (dev->name, reg, 0x1f, 0x2a30);
1153                         miiphy_write (dev->name, reg, 0x08, 0x0000);
1154                         miiphy_write (dev->name, reg, 0x1f, 0x0000);
1155                         /* end Vitesse/Cicada errata */
1156                 }
1157 #endif /* defined(CONFIG_CIS8201_PHY) */
1158
1159 #if defined(CONFIG_ET1011C_PHY)
1160                 /*
1161                  * Agere ET1011c PHY needs to have an extended register whacked
1162                  * for RGMII mode.
1163                  */
1164                 if (((devnum == 2) || (devnum ==3)) && (4 == ethgroup)) {
1165                         miiphy_read (dev->name, reg, 0x16, &reg_short);
1166                         reg_short &= ~(0x7);
1167                         reg_short |= 0x6;       /* RGMII DLL Delay*/
1168                         miiphy_write (dev->name, reg, 0x16, reg_short);
1169
1170                         miiphy_read (dev->name, reg, 0x17, &reg_short);
1171                         reg_short &= ~(0x40);
1172                         miiphy_write (dev->name, reg, 0x17, reg_short);
1173
1174                         miiphy_write(dev->name, reg, 0x1c, 0x74f0);
1175                 }
1176 #endif /* defined(CONFIG_ET1011C_PHY) */
1177
1178 #endif /* defined(CONFIG_440GX) ... */
1179                 /* Start/Restart autonegotiation */
1180                 phy_setup_aneg (dev->name, reg);
1181                 udelay (1000);
1182         }
1183 #endif /* defined(CONFIG_PHY_RESET) */
1184
1185         miiphy_read (dev->name, reg, PHY_BMSR, &reg_short);
1186
1187         /*
1188          * Wait if PHY is capable of autonegotiation and autonegotiation is not complete
1189          */
1190         if ((reg_short & PHY_BMSR_AUTN_ABLE)
1191             && !(reg_short & PHY_BMSR_AUTN_COMP)) {
1192                 puts ("Waiting for PHY auto negotiation to complete");
1193                 i = 0;
1194                 while (!(reg_short & PHY_BMSR_AUTN_COMP)) {
1195                         /*
1196                          * Timeout reached ?
1197                          */
1198                         if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
1199                                 puts (" TIMEOUT !\n");
1200                                 break;
1201                         }
1202
1203                         if ((i++ % 1000) == 0) {
1204                                 putc ('.');
1205                         }
1206                         udelay (1000);  /* 1 ms */
1207                         miiphy_read (dev->name, reg, PHY_BMSR, &reg_short);
1208                 }
1209                 puts (" done\n");
1210                 udelay (500000);        /* another 500 ms (results in faster booting) */
1211         }
1212
1213 get_speed:
1214         if (reg == CONFIG_FIXED_PHY) {
1215                 for (i = 0; i < ARRAY_SIZE(fixed_phy_port); i++) {
1216                         if (devnum == fixed_phy_port[i].devnum) {
1217                                 speed = fixed_phy_port[i].speed;
1218                                 duplex = fixed_phy_port[i].duplex;
1219                                 break;
1220                         }
1221                 }
1222
1223                 if (i == ARRAY_SIZE(fixed_phy_port)) {
1224                         printf("ERROR: PHY (%s) not configured correctly!\n",
1225                                 dev->name);
1226                         return -1;
1227                 }
1228         } else {
1229                 speed = miiphy_speed(dev->name, reg);
1230                 duplex = miiphy_duplex(dev->name, reg);
1231         }
1232
1233         if (hw_p->print_speed) {
1234                 hw_p->print_speed = 0;
1235                 printf ("ENET Speed is %d Mbps - %s duplex connection (EMAC%d)\n",
1236                         (int) speed, (duplex == HALF) ? "HALF" : "FULL",
1237                         hw_p->devnum);
1238         }
1239
1240 #if defined(CONFIG_440) && \
1241     !defined(CONFIG_440SP) && !defined(CONFIG_440SPE) && \
1242     !defined(CONFIG_440EPX) && !defined(CONFIG_440GRX) && \
1243     !defined(CONFIG_460EX) && !defined(CONFIG_460GT)
1244 #if defined(CONFIG_440EP) || defined(CONFIG_440GR)
1245         mfsdr(sdr_mfr, reg);
1246         if (speed == 100) {
1247                 reg = (reg & ~SDR0_MFR_ZMII_MODE_MASK) | SDR0_MFR_ZMII_MODE_RMII_100M;
1248         } else {
1249                 reg = (reg & ~SDR0_MFR_ZMII_MODE_MASK) | SDR0_MFR_ZMII_MODE_RMII_10M;
1250         }
1251         mtsdr(sdr_mfr, reg);
1252 #endif
1253
1254         /* Set ZMII/RGMII speed according to the phy link speed */
1255         reg = in_be32((void *)ZMII_SSR);
1256         if ( (speed == 100) || (speed == 1000) )
1257                 out_be32((void *)ZMII_SSR, reg | (ZMII_SSR_SP << ZMII_SSR_V (devnum)));
1258         else
1259                 out_be32((void *)ZMII_SSR, reg & (~(ZMII_SSR_SP << ZMII_SSR_V (devnum))));
1260
1261         if ((devnum == 2) || (devnum == 3)) {
1262                 if (speed == 1000)
1263                         reg = (RGMII_SSR_SP_1000MBPS << RGMII_SSR_V (devnum));
1264                 else if (speed == 100)
1265                         reg = (RGMII_SSR_SP_100MBPS << RGMII_SSR_V (devnum));
1266                 else if (speed == 10)
1267                         reg = (RGMII_SSR_SP_10MBPS << RGMII_SSR_V (devnum));
1268                 else {
1269                         printf("Error in RGMII Speed\n");
1270                         return -1;
1271                 }
1272                 out_be32((void *)RGMII_SSR, reg);
1273         }
1274 #endif /* defined(CONFIG_440) && !defined(CONFIG_440SP) */
1275
1276 #if defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
1277     defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
1278     defined(CONFIG_405EX)
1279         if (devnum >= 2)
1280                 rgmii_channel = devnum - 2;
1281         else
1282                 rgmii_channel = devnum;
1283
1284         if (speed == 1000)
1285                 reg = (RGMII_SSR_SP_1000MBPS << RGMII_SSR_V(rgmii_channel));
1286         else if (speed == 100)
1287                 reg = (RGMII_SSR_SP_100MBPS << RGMII_SSR_V(rgmii_channel));
1288         else if (speed == 10)
1289                 reg = (RGMII_SSR_SP_10MBPS << RGMII_SSR_V(rgmii_channel));
1290         else {
1291                 printf("Error in RGMII Speed\n");
1292                 return -1;
1293         }
1294         out_be32((void *)RGMII_SSR, reg);
1295 #if defined(CONFIG_460GT)
1296         if ((devnum == 2) || (devnum == 3))
1297                 out_be32((void *)RGMII_SSR + RGMII1_BASE_OFFSET, reg);
1298 #endif
1299 #endif
1300
1301         /* set the Mal configuration reg */
1302 #if defined(CONFIG_440GX) || \
1303     defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
1304     defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
1305     defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
1306     defined(CONFIG_405EX)
1307         mtdcr (malmcr, MAL_CR_PLBB | MAL_CR_OPBBL | MAL_CR_LEA |
1308                MAL_CR_PLBLT_DEFAULT | MAL_CR_EOPIE | 0x00330000);
1309 #else
1310         mtdcr (malmcr, MAL_CR_PLBB | MAL_CR_OPBBL | MAL_CR_LEA | MAL_CR_PLBLT_DEFAULT);
1311         /* Errata 1.12: MAL_1 -- Disable MAL bursting */
1312         if (get_pvr() == PVR_440GP_RB) {
1313                 mtdcr (malmcr, mfdcr(malmcr) & ~MAL_CR_PLBB);
1314         }
1315 #endif
1316
1317         /*
1318          * Malloc MAL buffer desciptors, make sure they are
1319          * aligned on cache line boundary size
1320          * (401/403/IOP480 = 16, 405 = 32)
1321          * and doesn't cross cache block boundaries.
1322          */
1323         if (hw_p->first_init == 0) {
1324                 debug("*** Allocating descriptor memory ***\n");
1325
1326                 bd_cached = (u32)malloc_aligned(MAL_ALLOC_SIZE, 4096);
1327                 if (!bd_cached) {
1328                         printf("%s: Error allocating MAL descriptor buffers!\n", __func__);
1329                         return -1;
1330                 }
1331
1332 #ifdef CONFIG_4xx_DCACHE
1333                 flush_dcache_range(bd_cached, bd_cached + MAL_ALLOC_SIZE);
1334                 if (!last_used_ea)
1335 #if defined(CONFIG_SYS_MEM_TOP_HIDE)
1336                         bd_uncached = bis->bi_memsize + CONFIG_SYS_MEM_TOP_HIDE;
1337 #else
1338                         bd_uncached = bis->bi_memsize;
1339 #endif
1340                 else
1341                         bd_uncached = last_used_ea + MAL_ALLOC_SIZE;
1342
1343                 last_used_ea = bd_uncached;
1344                 program_tlb(bd_cached, bd_uncached, MAL_ALLOC_SIZE,
1345                             TLB_WORD2_I_ENABLE);
1346 #else
1347                 bd_uncached = bd_cached;
1348 #endif
1349                 hw_p->tx_phys = bd_cached;
1350                 hw_p->rx_phys = bd_cached + MAL_TX_DESC_SIZE;
1351                 hw_p->tx = (mal_desc_t *)(bd_uncached);
1352                 hw_p->rx = (mal_desc_t *)(bd_uncached + MAL_TX_DESC_SIZE);
1353                 debug("hw_p->tx=%08x, hw_p->rx=%08x\n", hw_p->tx, hw_p->rx);
1354         }
1355
1356         for (i = 0; i < NUM_TX_BUFF; i++) {
1357                 hw_p->tx[i].ctrl = 0;
1358                 hw_p->tx[i].data_len = 0;
1359                 if (hw_p->first_init == 0)
1360                         hw_p->txbuf_ptr = malloc_aligned(MAL_ALLOC_SIZE,
1361                                                          L1_CACHE_BYTES);
1362                 hw_p->tx[i].data_ptr = hw_p->txbuf_ptr;
1363                 if ((NUM_TX_BUFF - 1) == i)
1364                         hw_p->tx[i].ctrl |= MAL_TX_CTRL_WRAP;
1365                 hw_p->tx_run[i] = -1;
1366                 debug("TX_BUFF %d @ 0x%08lx\n", i, (u32)hw_p->tx[i].data_ptr);
1367         }
1368
1369         for (i = 0; i < NUM_RX_BUFF; i++) {
1370                 hw_p->rx[i].ctrl = 0;
1371                 hw_p->rx[i].data_len = 0;
1372                 hw_p->rx[i].data_ptr = (char *)NetRxPackets[i];
1373                 if ((NUM_RX_BUFF - 1) == i)
1374                         hw_p->rx[i].ctrl |= MAL_RX_CTRL_WRAP;
1375                 hw_p->rx[i].ctrl |= MAL_RX_CTRL_EMPTY | MAL_RX_CTRL_INTR;
1376                 hw_p->rx_ready[i] = -1;
1377                 debug("RX_BUFF %d @ 0x%08lx\n", i, (u32)hw_p->rx[i].data_ptr);
1378         }
1379
1380         reg = 0x00000000;
1381
1382         reg |= dev->enetaddr[0];        /* set high address */
1383         reg = reg << 8;
1384         reg |= dev->enetaddr[1];
1385
1386         out_be32((void *)EMAC_IAH + hw_p->hw_addr, reg);
1387
1388         reg = 0x00000000;
1389         reg |= dev->enetaddr[2];        /* set low address  */
1390         reg = reg << 8;
1391         reg |= dev->enetaddr[3];
1392         reg = reg << 8;
1393         reg |= dev->enetaddr[4];
1394         reg = reg << 8;
1395         reg |= dev->enetaddr[5];
1396
1397         out_be32((void *)EMAC_IAL + hw_p->hw_addr, reg);
1398
1399         switch (devnum) {
1400         case 1:
1401                 /* setup MAL tx & rx channel pointers */
1402 #if defined (CONFIG_405EP) || defined (CONFIG_440EP) || defined (CONFIG_440GR)
1403                 mtdcr (maltxctp2r, hw_p->tx_phys);
1404 #else
1405                 mtdcr (maltxctp1r, hw_p->tx_phys);
1406 #endif
1407 #if defined(CONFIG_440)
1408                 mtdcr (maltxbattr, 0x0);
1409                 mtdcr (malrxbattr, 0x0);
1410 #endif
1411
1412 #if defined(CONFIG_460EX) || defined(CONFIG_460GT)
1413                 mtdcr (malrxctp8r, hw_p->rx_phys);
1414                 /* set RX buffer size */
1415                 mtdcr (malrcbs8, ENET_MAX_MTU_ALIGNED / 16);
1416 #else
1417                 mtdcr (malrxctp1r, hw_p->rx_phys);
1418                 /* set RX buffer size */
1419                 mtdcr (malrcbs1, ENET_MAX_MTU_ALIGNED / 16);
1420 #endif
1421                 break;
1422 #if defined (CONFIG_440GX)
1423         case 2:
1424                 /* setup MAL tx & rx channel pointers */
1425                 mtdcr (maltxbattr, 0x0);
1426                 mtdcr (malrxbattr, 0x0);
1427                 mtdcr (maltxctp2r, hw_p->tx_phys);
1428                 mtdcr (malrxctp2r, hw_p->rx_phys);
1429                 /* set RX buffer size */
1430                 mtdcr (malrcbs2, ENET_MAX_MTU_ALIGNED / 16);
1431                 break;
1432         case 3:
1433                 /* setup MAL tx & rx channel pointers */
1434                 mtdcr (maltxbattr, 0x0);
1435                 mtdcr (maltxctp3r, hw_p->tx_phys);
1436                 mtdcr (malrxbattr, 0x0);
1437                 mtdcr (malrxctp3r, hw_p->rx_phys);
1438                 /* set RX buffer size */
1439                 mtdcr (malrcbs3, ENET_MAX_MTU_ALIGNED / 16);
1440                 break;
1441 #endif /* CONFIG_440GX */
1442 #if defined (CONFIG_460GT)
1443         case 2:
1444                 /* setup MAL tx & rx channel pointers */
1445                 mtdcr (maltxbattr, 0x0);
1446                 mtdcr (malrxbattr, 0x0);
1447                 mtdcr (maltxctp2r, hw_p->tx_phys);
1448                 mtdcr (malrxctp16r, hw_p->rx_phys);
1449                 /* set RX buffer size */
1450                 mtdcr (malrcbs16, ENET_MAX_MTU_ALIGNED / 16);
1451                 break;
1452         case 3:
1453                 /* setup MAL tx & rx channel pointers */
1454                 mtdcr (maltxbattr, 0x0);
1455                 mtdcr (malrxbattr, 0x0);
1456                 mtdcr (maltxctp3r, hw_p->tx_phys);
1457                 mtdcr (malrxctp24r, hw_p->rx_phys);
1458                 /* set RX buffer size */
1459                 mtdcr (malrcbs24, ENET_MAX_MTU_ALIGNED / 16);
1460                 break;
1461 #endif /* CONFIG_460GT */
1462         case 0:
1463         default:
1464                 /* setup MAL tx & rx channel pointers */
1465 #if defined(CONFIG_440)
1466                 mtdcr (maltxbattr, 0x0);
1467                 mtdcr (malrxbattr, 0x0);
1468 #endif
1469                 mtdcr (maltxctp0r, hw_p->tx_phys);
1470                 mtdcr (malrxctp0r, hw_p->rx_phys);
1471                 /* set RX buffer size */
1472                 mtdcr (malrcbs0, ENET_MAX_MTU_ALIGNED / 16);
1473                 break;
1474         }
1475
1476         /* Enable MAL transmit and receive channels */
1477 #if defined(CONFIG_405EP) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
1478         mtdcr (maltxcasr, (MAL_TXRX_CASR >> (hw_p->devnum*2)));
1479 #else
1480         mtdcr (maltxcasr, (MAL_TXRX_CASR >> hw_p->devnum));
1481 #endif
1482         mtdcr (malrxcasr, (MAL_TXRX_CASR >> hw_p->devnum));
1483
1484         /* set transmit enable & receive enable */
1485         out_be32((void *)EMAC_M0 + hw_p->hw_addr, EMAC_M0_TXE | EMAC_M0_RXE);
1486
1487         mode_reg = in_be32((void *)EMAC_M1 + hw_p->hw_addr);
1488
1489         /* set rx-/tx-fifo size */
1490         mode_reg = (mode_reg & ~EMAC_MR1_FIFO_MASK) | EMAC_MR1_FIFO_SIZE;
1491
1492         /* set speed */
1493         if (speed == _1000BASET) {
1494 #if defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
1495     defined(CONFIG_440SP) || defined(CONFIG_440SPE)
1496                 unsigned long pfc1;
1497
1498                 mfsdr (sdr_pfc1, pfc1);
1499                 pfc1 |= SDR0_PFC1_EM_1000;
1500                 mtsdr (sdr_pfc1, pfc1);
1501 #endif
1502                 mode_reg = mode_reg | EMAC_M1_MF_1000MBPS | EMAC_M1_IST;
1503         } else if (speed == _100BASET)
1504                 mode_reg = mode_reg | EMAC_M1_MF_100MBPS | EMAC_M1_IST;
1505         else
1506                 mode_reg = mode_reg & ~0x00C00000;      /* 10 MBPS */
1507         if (duplex == FULL)
1508                 mode_reg = mode_reg | 0x80000000 | EMAC_M1_IST;
1509
1510         out_be32((void *)EMAC_M1 + hw_p->hw_addr, mode_reg);
1511
1512         /* Enable broadcast and indvidual address */
1513         /* TBS: enabling runts as some misbehaved nics will send runts */
1514         out_be32((void *)EMAC_RXM + hw_p->hw_addr, EMAC_RMR_BAE | EMAC_RMR_IAE);
1515
1516         /* we probably need to set the tx mode1 reg? maybe at tx time */
1517
1518         /* set transmit request threshold register */
1519         out_be32((void *)EMAC_TRTR + hw_p->hw_addr, 0x18000000);        /* 256 byte threshold */
1520
1521         /* set receive  low/high water mark register */
1522 #if defined(CONFIG_440)
1523         /* 440s has a 64 byte burst length */
1524         out_be32((void *)EMAC_RX_HI_LO_WMARK + hw_p->hw_addr, 0x80009000);
1525 #else
1526         /* 405s have a 16 byte burst length */
1527         out_be32((void *)EMAC_RX_HI_LO_WMARK + hw_p->hw_addr, 0x0f002000);
1528 #endif /* defined(CONFIG_440) */
1529         out_be32((void *)EMAC_TXM1 + hw_p->hw_addr, 0xf8640000);
1530
1531         /* Set fifo limit entry in tx mode 0 */
1532         out_be32((void *)EMAC_TXM0 + hw_p->hw_addr, 0x00000003);
1533         /* Frame gap set */
1534         out_be32((void *)EMAC_I_FRAME_GAP_REG + hw_p->hw_addr, 0x00000008);
1535
1536         /* Set EMAC IER */
1537         hw_p->emac_ier = EMAC_ISR_PTLE | EMAC_ISR_BFCS | EMAC_ISR_ORE | EMAC_ISR_IRE;
1538         if (speed == _100BASET)
1539                 hw_p->emac_ier = hw_p->emac_ier | EMAC_ISR_SYE;
1540
1541         out_be32((void *)EMAC_ISR + hw_p->hw_addr, 0xffffffff); /* clear pending interrupts */
1542         out_be32((void *)EMAC_IER + hw_p->hw_addr, hw_p->emac_ier);
1543
1544         if (hw_p->first_init == 0) {
1545                 /*
1546                  * Connect interrupt service routines
1547                  */
1548                 irq_install_handler(ETH_IRQ_NUM(hw_p->devnum),
1549                                     (interrupt_handler_t *) enetInt, dev);
1550         }
1551
1552         mtmsr (msr);            /* enable interrupts again */
1553
1554         hw_p->bis = bis;
1555         hw_p->first_init = 1;
1556
1557         return 0;
1558 }
1559
1560
1561 static int ppc_4xx_eth_send (struct eth_device *dev, volatile void *ptr,
1562                               int len)
1563 {
1564         struct enet_frame *ef_ptr;
1565         ulong time_start, time_now;
1566         unsigned long temp_txm0;
1567         EMAC_4XX_HW_PST hw_p = dev->priv;
1568
1569         ef_ptr = (struct enet_frame *) ptr;
1570
1571         /*-----------------------------------------------------------------------+
1572          *  Copy in our address into the frame.
1573          *-----------------------------------------------------------------------*/
1574         (void) memcpy (ef_ptr->source_addr, dev->enetaddr, ENET_ADDR_LENGTH);
1575
1576         /*-----------------------------------------------------------------------+
1577          * If frame is too long or too short, modify length.
1578          *-----------------------------------------------------------------------*/
1579         /* TBS: where does the fragment go???? */
1580         if (len > ENET_MAX_MTU)
1581                 len = ENET_MAX_MTU;
1582
1583         /*   memcpy ((void *) &tx_buff[tx_slot], (const void *) ptr, len); */
1584         memcpy ((void *) hw_p->txbuf_ptr, (const void *) ptr, len);
1585         flush_dcache_range((u32)hw_p->txbuf_ptr, (u32)hw_p->txbuf_ptr + len);
1586
1587         /*-----------------------------------------------------------------------+
1588          * set TX Buffer busy, and send it
1589          *-----------------------------------------------------------------------*/
1590         hw_p->tx[hw_p->tx_slot].ctrl = (MAL_TX_CTRL_LAST |
1591                                         EMAC_TX_CTRL_GFCS | EMAC_TX_CTRL_GP) &
1592                 ~(EMAC_TX_CTRL_ISA | EMAC_TX_CTRL_RSA);
1593         if ((NUM_TX_BUFF - 1) == hw_p->tx_slot)
1594                 hw_p->tx[hw_p->tx_slot].ctrl |= MAL_TX_CTRL_WRAP;
1595
1596         hw_p->tx[hw_p->tx_slot].data_len = (short) len;
1597         hw_p->tx[hw_p->tx_slot].ctrl |= MAL_TX_CTRL_READY;
1598
1599         sync();
1600
1601         out_be32((void *)EMAC_TXM0 + hw_p->hw_addr,
1602                  in_be32((void *)EMAC_TXM0 + hw_p->hw_addr) | EMAC_TXM0_GNP0);
1603 #ifdef INFO_4XX_ENET
1604         hw_p->stats.pkts_tx++;
1605 #endif
1606
1607         /*-----------------------------------------------------------------------+
1608          * poll unitl the packet is sent and then make sure it is OK
1609          *-----------------------------------------------------------------------*/
1610         time_start = get_timer (0);
1611         while (1) {
1612                 temp_txm0 = in_be32((void *)EMAC_TXM0 + hw_p->hw_addr);
1613                 /* loop until either TINT turns on or 3 seconds elapse */
1614                 if ((temp_txm0 & EMAC_TXM0_GNP0) != 0) {
1615                         /* transmit is done, so now check for errors
1616                          * If there is an error, an interrupt should
1617                          * happen when we return
1618                          */
1619                         time_now = get_timer (0);
1620                         if ((time_now - time_start) > 3000) {
1621                                 return (-1);
1622                         }
1623                 } else {
1624                         return (len);
1625                 }
1626         }
1627 }
1628
1629 int enetInt (struct eth_device *dev)
1630 {
1631         int serviced;
1632         int rc = -1;            /* default to not us */
1633         u32 mal_isr;
1634         u32 emac_isr = 0;
1635         u32 mal_eob;
1636         u32 uic_mal;
1637         u32 uic_mal_err;
1638         u32 uic_emac;
1639         u32 uic_emac_b;
1640         EMAC_4XX_HW_PST hw_p;
1641
1642         /*
1643          * Because the mal is generic, we need to get the current
1644          * eth device
1645          */
1646 #if defined(CONFIG_NET_MULTI)
1647         dev = eth_get_dev();
1648 #else
1649         dev = emac0_dev;
1650 #endif
1651
1652         hw_p = dev->priv;
1653
1654         /* enter loop that stays in interrupt code until nothing to service */
1655         do {
1656                 serviced = 0;
1657
1658                 uic_mal = mfdcr(UIC_BASE_MAL + UIC_MSR);
1659                 uic_mal_err = mfdcr(UIC_BASE_MAL_ERR + UIC_MSR);
1660                 uic_emac = mfdcr(UIC_BASE_EMAC + UIC_MSR);
1661                 uic_emac_b = mfdcr(UIC_BASE_EMAC_B + UIC_MSR);
1662
1663                 if (!(uic_mal & (UIC_MAL_RXEOB | UIC_MAL_TXEOB))
1664                     && !(uic_mal_err & (UIC_MAL_SERR | UIC_MAL_TXDE | UIC_MAL_RXDE))
1665                     && !(uic_emac & UIC_ETHx) && !(uic_emac_b & UIC_ETHxB)) {
1666                         /* not for us */
1667                         return (rc);
1668                 }
1669
1670                 /* get and clear controller status interrupts */
1671                 /* look at MAL and EMAC error interrupts */
1672                 if (uic_mal_err & (UIC_MAL_SERR | UIC_MAL_TXDE | UIC_MAL_RXDE)) {
1673                         /* we have a MAL error interrupt */
1674                         mal_isr = mfdcr(malesr);
1675                         mal_err(dev, mal_isr, uic_mal_err,
1676                                  MAL_UIC_DEF, MAL_UIC_ERR);
1677
1678                         /* clear MAL error interrupt status bits */
1679                         mtdcr(UIC_BASE_MAL_ERR + UIC_SR,
1680                               UIC_MAL_SERR | UIC_MAL_TXDE | UIC_MAL_RXDE);
1681
1682                         return -1;
1683                 }
1684
1685                 /* look for EMAC errors */
1686                 if ((uic_emac & UIC_ETHx) || (uic_emac_b & UIC_ETHxB)) {
1687                         emac_isr = in_be32((void *)EMAC_ISR + hw_p->hw_addr);
1688                         emac_err(dev, emac_isr);
1689
1690                         /* clear EMAC error interrupt status bits */
1691                         mtdcr(UIC_BASE_EMAC + UIC_SR, UIC_ETHx);
1692                         mtdcr(UIC_BASE_EMAC_B + UIC_SR, UIC_ETHxB);
1693
1694                         return -1;
1695                 }
1696
1697                 /* handle MAX TX EOB interrupt from a tx */
1698                 if (uic_mal & UIC_MAL_TXEOB) {
1699                         /* clear MAL interrupt status bits */
1700                         mal_eob = mfdcr(maltxeobisr);
1701                         mtdcr(maltxeobisr, mal_eob);
1702                         mtdcr(UIC_BASE_MAL + UIC_SR, UIC_MAL_TXEOB);
1703
1704                         /* indicate that we serviced an interrupt */
1705                         serviced = 1;
1706                         rc = 0;
1707                 }
1708
1709                 /* handle MAL RX EOB interupt from a receive */
1710                 /* check for EOB on valid channels           */
1711                 if (uic_mal & UIC_MAL_RXEOB) {
1712                         mal_eob = mfdcr(malrxeobisr);
1713                         if (mal_eob &
1714                             (0x80000000 >> (hw_p->devnum * MAL_RX_CHAN_MUL))) {
1715                                 /* push packet to upper layer */
1716                                 enet_rcv(dev, emac_isr);
1717
1718                                 /* clear MAL interrupt status bits */
1719                                 mtdcr(UIC_BASE_MAL + UIC_SR, UIC_MAL_RXEOB);
1720
1721                                 /* indicate that we serviced an interrupt */
1722                                 serviced = 1;
1723                                 rc = 0;
1724                         }
1725                 }
1726         } while (serviced);
1727
1728         return (rc);
1729 }
1730
1731 /*-----------------------------------------------------------------------------+
1732  *  MAL Error Routine
1733  *-----------------------------------------------------------------------------*/
1734 static void mal_err (struct eth_device *dev, unsigned long isr,
1735                      unsigned long uic, unsigned long maldef,
1736                      unsigned long mal_errr)
1737 {
1738         EMAC_4XX_HW_PST hw_p = dev->priv;
1739
1740         mtdcr (malesr, isr);    /* clear interrupt */
1741
1742         /* clear DE interrupt */
1743         mtdcr (maltxdeir, 0xC0000000);
1744         mtdcr (malrxdeir, 0x80000000);
1745
1746 #ifdef INFO_4XX_ENET
1747         printf ("\nMAL error occured.... ISR = %lx UIC = = %lx  MAL_DEF = %lx  MAL_ERR= %lx \n", isr, uic, maldef, mal_errr);
1748 #endif
1749
1750         eth_init (hw_p->bis);   /* start again... */
1751 }
1752
1753 /*-----------------------------------------------------------------------------+
1754  *  EMAC Error Routine
1755  *-----------------------------------------------------------------------------*/
1756 static void emac_err (struct eth_device *dev, unsigned long isr)
1757 {
1758         EMAC_4XX_HW_PST hw_p = dev->priv;
1759
1760         printf ("EMAC%d error occured.... ISR = %lx\n", hw_p->devnum, isr);
1761         out_be32((void *)EMAC_ISR + hw_p->hw_addr, isr);
1762 }
1763
1764 /*-----------------------------------------------------------------------------+
1765  *  enet_rcv() handles the ethernet receive data
1766  *-----------------------------------------------------------------------------*/
1767 static void enet_rcv (struct eth_device *dev, unsigned long malisr)
1768 {
1769         struct enet_frame *ef_ptr;
1770         unsigned long data_len;
1771         unsigned long rx_eob_isr;
1772         EMAC_4XX_HW_PST hw_p = dev->priv;
1773
1774         int handled = 0;
1775         int i;
1776         int loop_count = 0;
1777
1778         rx_eob_isr = mfdcr (malrxeobisr);
1779         if ((0x80000000 >> (hw_p->devnum * MAL_RX_CHAN_MUL)) & rx_eob_isr) {
1780                 /* clear EOB */
1781                 mtdcr (malrxeobisr, rx_eob_isr);
1782
1783                 /* EMAC RX done */
1784                 while (1) {     /* do all */
1785                         i = hw_p->rx_slot;
1786
1787                         if ((MAL_RX_CTRL_EMPTY & hw_p->rx[i].ctrl)
1788                             || (loop_count >= NUM_RX_BUFF))
1789                                 break;
1790
1791                         loop_count++;
1792                         handled++;
1793                         data_len = (unsigned long) hw_p->rx[i].data_len & 0x0fff;       /* Get len */
1794                         if (data_len) {
1795                                 if (data_len > ENET_MAX_MTU)    /* Check len */
1796                                         data_len = 0;
1797                                 else {
1798                                         if (EMAC_RX_ERRORS & hw_p->rx[i].ctrl) {        /* Check Errors */
1799                                                 data_len = 0;
1800                                                 hw_p->stats.rx_err_log[hw_p->
1801                                                                        rx_err_index]
1802                                                         = hw_p->rx[i].ctrl;
1803                                                 hw_p->rx_err_index++;
1804                                                 if (hw_p->rx_err_index ==
1805                                                     MAX_ERR_LOG)
1806                                                         hw_p->rx_err_index =
1807                                                                 0;
1808                                         }       /* emac_erros */
1809                                 }       /* data_len < max mtu */
1810                         }       /* if data_len */
1811                         if (!data_len) {        /* no data */
1812                                 hw_p->rx[i].ctrl |= MAL_RX_CTRL_EMPTY;  /* Free Recv Buffer */
1813
1814                                 hw_p->stats.data_len_err++;     /* Error at Rx */
1815                         }
1816
1817                         /* !data_len */
1818                         /* AS.HARNOIS */
1819                         /* Check if user has already eaten buffer */
1820                         /* if not => ERROR */
1821                         else if (hw_p->rx_ready[hw_p->rx_i_index] != -1) {
1822                                 if (hw_p->is_receiving)
1823                                         printf ("ERROR : Receive buffers are full!\n");
1824                                 break;
1825                         } else {
1826                                 hw_p->stats.rx_frames++;
1827                                 hw_p->stats.rx += data_len;
1828                                 ef_ptr = (struct enet_frame *) hw_p->rx[i].
1829                                         data_ptr;
1830 #ifdef INFO_4XX_ENET
1831                                 hw_p->stats.pkts_rx++;
1832 #endif
1833                                 /* AS.HARNOIS
1834                                  * use ring buffer
1835                                  */
1836                                 hw_p->rx_ready[hw_p->rx_i_index] = i;
1837                                 hw_p->rx_i_index++;
1838                                 if (NUM_RX_BUFF == hw_p->rx_i_index)
1839                                         hw_p->rx_i_index = 0;
1840
1841                                 hw_p->rx_slot++;
1842                                 if (NUM_RX_BUFF == hw_p->rx_slot)
1843                                         hw_p->rx_slot = 0;
1844
1845                                 /*  AS.HARNOIS
1846                                  * free receive buffer only when
1847                                  * buffer has been handled (eth_rx)
1848                                  rx[i].ctrl |= MAL_RX_CTRL_EMPTY;
1849                                  */
1850                         }       /* if data_len */
1851                 }               /* while */
1852         }                       /* if EMACK_RXCHL */
1853 }
1854
1855
1856 static int ppc_4xx_eth_rx (struct eth_device *dev)
1857 {
1858         int length;
1859         int user_index;
1860         unsigned long msr;
1861         EMAC_4XX_HW_PST hw_p = dev->priv;
1862
1863         hw_p->is_receiving = 1; /* tell driver */
1864
1865         for (;;) {
1866                 /* AS.HARNOIS
1867                  * use ring buffer and
1868                  * get index from rx buffer desciptor queue
1869                  */
1870                 user_index = hw_p->rx_ready[hw_p->rx_u_index];
1871                 if (user_index == -1) {
1872                         length = -1;
1873                         break;  /* nothing received - leave for() loop */
1874                 }
1875
1876                 msr = mfmsr ();
1877                 mtmsr (msr & ~(MSR_EE));
1878
1879                 length = hw_p->rx[user_index].data_len & 0x0fff;
1880
1881                 /* Pass the packet up to the protocol layers. */
1882                 /*       NetReceive(NetRxPackets[rxIdx], length - 4); */
1883                 /*       NetReceive(NetRxPackets[i], length); */
1884                 invalidate_dcache_range((u32)hw_p->rx[user_index].data_ptr,
1885                                         (u32)hw_p->rx[user_index].data_ptr +
1886                                         length - 4);
1887                 NetReceive (NetRxPackets[user_index], length - 4);
1888                 /* Free Recv Buffer */
1889                 hw_p->rx[user_index].ctrl |= MAL_RX_CTRL_EMPTY;
1890                 /* Free rx buffer descriptor queue */
1891                 hw_p->rx_ready[hw_p->rx_u_index] = -1;
1892                 hw_p->rx_u_index++;
1893                 if (NUM_RX_BUFF == hw_p->rx_u_index)
1894                         hw_p->rx_u_index = 0;
1895
1896 #ifdef INFO_4XX_ENET
1897                 hw_p->stats.pkts_handled++;
1898 #endif
1899
1900                 mtmsr (msr);    /* Enable IRQ's */
1901         }
1902
1903         hw_p->is_receiving = 0; /* tell driver */
1904
1905         return length;
1906 }
1907
1908 int ppc_4xx_eth_initialize (bd_t * bis)
1909 {
1910         static int virgin = 0;
1911         struct eth_device *dev;
1912         int eth_num = 0;
1913         EMAC_4XX_HW_PST hw = NULL;
1914         u8 ethaddr[4 + CONFIG_EMAC_NR_START][6];
1915         u32 hw_addr[4];
1916         u32 mal_ier;
1917
1918 #if defined(CONFIG_440GX)
1919         unsigned long pfc1;
1920
1921         mfsdr (sdr_pfc1, pfc1);
1922         pfc1 &= ~(0x01e00000);
1923         pfc1 |= 0x01200000;
1924         mtsdr (sdr_pfc1, pfc1);
1925 #endif
1926
1927         /* first clear all mac-addresses */
1928         for (eth_num = 0; eth_num < LAST_EMAC_NUM; eth_num++)
1929                 memcpy(ethaddr[eth_num], "\0\0\0\0\0\0", 6);
1930
1931         for (eth_num = 0; eth_num < LAST_EMAC_NUM; eth_num++) {
1932                 int ethaddr_idx = eth_num + CONFIG_EMAC_NR_START;
1933                 switch (eth_num) {
1934                 default:                /* fall through */
1935                 case 0:
1936                         eth_getenv_enetaddr("ethaddr", ethaddr[ethaddr_idx]);
1937                         hw_addr[eth_num] = 0x0;
1938                         break;
1939 #ifdef CONFIG_HAS_ETH1
1940                 case 1:
1941                         eth_getenv_enetaddr("eth1addr", ethaddr[ethaddr_idx]);
1942                         hw_addr[eth_num] = 0x100;
1943                         break;
1944 #endif
1945 #ifdef CONFIG_HAS_ETH2
1946                 case 2:
1947                         eth_getenv_enetaddr("eth2addr", ethaddr[ethaddr_idx]);
1948 #if defined(CONFIG_460GT)
1949                         hw_addr[eth_num] = 0x300;
1950 #else
1951                         hw_addr[eth_num] = 0x400;
1952 #endif
1953                         break;
1954 #endif
1955 #ifdef CONFIG_HAS_ETH3
1956                 case 3:
1957                         eth_getenv_enetaddr("eth3addr", ethaddr[ethaddr_idx]);
1958 #if defined(CONFIG_460GT)
1959                         hw_addr[eth_num] = 0x400;
1960 #else
1961                         hw_addr[eth_num] = 0x600;
1962 #endif
1963                         break;
1964 #endif
1965                 }
1966         }
1967
1968         /* set phy num and mode */
1969         bis->bi_phynum[0] = CONFIG_PHY_ADDR;
1970         bis->bi_phymode[0] = 0;
1971
1972 #if defined(CONFIG_PHY1_ADDR)
1973         bis->bi_phynum[1] = CONFIG_PHY1_ADDR;
1974         bis->bi_phymode[1] = 0;
1975 #endif
1976 #if defined(CONFIG_440GX)
1977         bis->bi_phynum[2] = CONFIG_PHY2_ADDR;
1978         bis->bi_phynum[3] = CONFIG_PHY3_ADDR;
1979         bis->bi_phymode[2] = 2;
1980         bis->bi_phymode[3] = 2;
1981 #endif
1982
1983 #if defined(CONFIG_440GX) || \
1984     defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
1985     defined(CONFIG_405EX)
1986         ppc_4xx_eth_setup_bridge(0, bis);
1987 #endif
1988
1989         for (eth_num = 0; eth_num < LAST_EMAC_NUM; eth_num++) {
1990                 /*
1991                  * See if we can actually bring up the interface,
1992                  * otherwise, skip it
1993                  */
1994                 if (memcmp (ethaddr[eth_num], "\0\0\0\0\0\0", 6) == 0) {
1995                         bis->bi_phymode[eth_num] = BI_PHYMODE_NONE;
1996                         continue;
1997                 }
1998
1999                 /* Allocate device structure */
2000                 dev = (struct eth_device *) malloc (sizeof (*dev));
2001                 if (dev == NULL) {
2002                         printf ("ppc_4xx_eth_initialize: "
2003                                 "Cannot allocate eth_device %d\n", eth_num);
2004                         return (-1);
2005                 }
2006                 memset(dev, 0, sizeof(*dev));
2007
2008                 /* Allocate our private use data */
2009                 hw = (EMAC_4XX_HW_PST) malloc (sizeof (*hw));
2010                 if (hw == NULL) {
2011                         printf ("ppc_4xx_eth_initialize: "
2012                                 "Cannot allocate private hw data for eth_device %d",
2013                                 eth_num);
2014                         free (dev);
2015                         return (-1);
2016                 }
2017                 memset(hw, 0, sizeof(*hw));
2018
2019                 hw->hw_addr = hw_addr[eth_num];
2020                 memcpy (dev->enetaddr, ethaddr[eth_num], 6);
2021                 hw->devnum = eth_num;
2022                 hw->print_speed = 1;
2023
2024                 sprintf (dev->name, "ppc_4xx_eth%d", eth_num - CONFIG_EMAC_NR_START);
2025                 dev->priv = (void *) hw;
2026                 dev->init = ppc_4xx_eth_init;
2027                 dev->halt = ppc_4xx_eth_halt;
2028                 dev->send = ppc_4xx_eth_send;
2029                 dev->recv = ppc_4xx_eth_rx;
2030
2031                 if (0 == virgin) {
2032                         /* set the MAL IER ??? names may change with new spec ??? */
2033 #if defined(CONFIG_440SPE) || \
2034     defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
2035     defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
2036     defined(CONFIG_405EX)
2037                         mal_ier =
2038                                 MAL_IER_PT | MAL_IER_PRE | MAL_IER_PWE |
2039                                 MAL_IER_DE | MAL_IER_OTE | MAL_IER_OE | MAL_IER_PE ;
2040 #else
2041                         mal_ier =
2042                                 MAL_IER_DE | MAL_IER_NE | MAL_IER_TE |
2043                                 MAL_IER_OPBE | MAL_IER_PLBE;
2044 #endif
2045                         mtdcr (malesr, 0xffffffff);     /* clear pending interrupts */
2046                         mtdcr (maltxdeir, 0xffffffff);  /* clear pending interrupts */
2047                         mtdcr (malrxdeir, 0xffffffff);  /* clear pending interrupts */
2048                         mtdcr (malier, mal_ier);
2049
2050                         /* install MAL interrupt handler */
2051                         irq_install_handler (VECNUM_MAL_SERR,
2052                                              (interrupt_handler_t *) enetInt,
2053                                              dev);
2054                         irq_install_handler (VECNUM_MAL_TXEOB,
2055                                              (interrupt_handler_t *) enetInt,
2056                                              dev);
2057                         irq_install_handler (VECNUM_MAL_RXEOB,
2058                                              (interrupt_handler_t *) enetInt,
2059                                              dev);
2060                         irq_install_handler (VECNUM_MAL_TXDE,
2061                                              (interrupt_handler_t *) enetInt,
2062                                              dev);
2063                         irq_install_handler (VECNUM_MAL_RXDE,
2064                                              (interrupt_handler_t *) enetInt,
2065                                              dev);
2066                         virgin = 1;
2067                 }
2068
2069 #if defined(CONFIG_NET_MULTI)
2070                 eth_register (dev);
2071 #else
2072                 emac0_dev = dev;
2073 #endif
2074
2075 #if defined(CONFIG_NET_MULTI)
2076 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
2077                 miiphy_register (dev->name,
2078                                  emac4xx_miiphy_read, emac4xx_miiphy_write);
2079 #endif
2080 #endif
2081         }                       /* end for each supported device */
2082
2083         return 0;
2084 }
2085
2086 #if !defined(CONFIG_NET_MULTI)
2087 void eth_halt (void) {
2088         if (emac0_dev) {
2089                 ppc_4xx_eth_halt(emac0_dev);
2090                 free(emac0_dev);
2091                 emac0_dev = NULL;
2092         }
2093 }
2094
2095 int eth_init (bd_t *bis)
2096 {
2097         ppc_4xx_eth_initialize(bis);
2098         if (emac0_dev) {
2099                 return ppc_4xx_eth_init(emac0_dev, bis);
2100         } else {
2101                 printf("ERROR: ethaddr not set!\n");
2102                 return -1;
2103         }
2104 }
2105
2106 int eth_send(volatile void *packet, int length)
2107 {
2108         return (ppc_4xx_eth_send(emac0_dev, packet, length));
2109 }
2110
2111 int eth_rx(void)
2112 {
2113         return (ppc_4xx_eth_rx(emac0_dev));
2114 }
2115
2116 int emac4xx_miiphy_initialize (bd_t * bis)
2117 {
2118 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
2119         miiphy_register ("ppc_4xx_eth0",
2120                          emac4xx_miiphy_read, emac4xx_miiphy_write);
2121 #endif
2122
2123         return 0;
2124 }
2125 #endif /* !defined(CONFIG_NET_MULTI) */