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