blob: 599f5ceb562dc087e17b758434ef190e6fc7808f [file] [log] [blame]
wdenkba56f622004-02-06 23:19:44 +00001/*-----------------------------------------------------------------------------+
2 *
Wolfgang Denk265817c2005-09-25 00:53:22 +02003 * 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.
wdenkba56f622004-02-06 23:19:44 +00009 *
Wolfgang Denk265817c2005-09-25 00:53:22 +020010 * 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.
wdenkba56f622004-02-06 23:19:44 +000013 *
Wolfgang Denk265817c2005-09-25 00:53:22 +020014 * 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.
wdenkba56f622004-02-06 23:19:44 +000017 *
Wolfgang Denk265817c2005-09-25 00:53:22 +020018 * COPYRIGHT I B M CORPORATION 1995
19 * LICENSED MATERIAL - PROGRAM PROPERTY OF I B M
wdenkba56f622004-02-06 23:19:44 +000020 *-----------------------------------------------------------------------------*/
21/*-----------------------------------------------------------------------------+
22 *
Wolfgang Denk265817c2005-09-25 00:53:22 +020023 * File Name: enetemac.c
wdenkba56f622004-02-06 23:19:44 +000024 *
Wolfgang Denk265817c2005-09-25 00:53:22 +020025 * Function: Device driver for the ethernet EMAC3 macro on the 405GP.
wdenkba56f622004-02-06 23:19:44 +000026 *
Wolfgang Denk265817c2005-09-25 00:53:22 +020027 * Author: Mark Wisner
wdenkba56f622004-02-06 23:19:44 +000028 *
29 * Change Activity-
30 *
Wolfgang Denk265817c2005-09-25 00:53:22 +020031 * 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).
wdenkba56f622004-02-06 23:19:44 +000070 *-----------------------------------------------------------------------------*
Wolfgang Denk265817c2005-09-25 00:53:22 +020071 * 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.
wdenkba56f622004-02-06 23:19:44 +000078 *-----------------------------------------------------------------------------*/
79
80#include <config.h>
wdenkba56f622004-02-06 23:19:44 +000081#include <common.h>
82#include <net.h>
83#include <asm/processor.h>
Stefan Roese2d834762007-10-23 14:03:17 +020084#include <asm/io.h>
Stefan Roeseff768cb2007-10-31 18:01:24 +010085#include <asm/cache.h>
86#include <asm/mmu.h>
wdenkba56f622004-02-06 23:19:44 +000087#include <commproc.h>
Stefan Roesed6c61aa2005-08-16 18:18:00 +020088#include <ppc4xx.h>
89#include <ppc4xx_enet.h>
wdenkba56f622004-02-06 23:19:44 +000090#include <405_mal.h>
91#include <miiphy.h>
92#include <malloc.h>
Matthias Fuchs6e9233d2008-01-08 15:50:49 +010093#include <asm/ppc4xx-intvec.h>
wdenkba56f622004-02-06 23:19:44 +000094
Stefan Roesed6c61aa2005-08-16 18:18:00 +020095/*
Wolfgang Denk0c8721a2005-09-23 11:05:55 +020096 * Only compile for platform with AMCC EMAC ethernet controller and
Stefan Roesed6c61aa2005-08-16 18:18:00 +020097 * network support enabled.
98 * Remark: CONFIG_405 describes Xilinx PPC405 FPGA without EMAC controller!
99 */
Jon Loeliger3a1ed1e2007-07-09 18:57:22 -0500100#if defined(CONFIG_CMD_NET) && !defined(CONFIG_405) && !defined(CONFIG_IOP480)
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200101
Jon Loeliger3a1ed1e2007-07-09 18:57:22 -0500102#if !(defined(CONFIG_MII) || defined(CONFIG_CMD_MII))
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200103#error "CONFIG_MII has to be defined!"
104#endif
wdenkba56f622004-02-06 23:19:44 +0000105
Stefan Roese1e25f952005-10-20 16:34:28 +0200106#if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_NET_MULTI)
107#error "CONFIG_NET_MULTI has to be defined for NetConsole"
108#endif
109
Wolfgang Denk265817c2005-09-25 00:53:22 +0200110#define EMAC_RESET_TIMEOUT 1000 /* 1000 ms reset timeout */
Stefan Roese1338e6a2007-10-23 14:05:08 +0200111#define PHY_AUTONEGOTIATE_TIMEOUT 5000 /* 5000 ms autonegotiate timeout */
wdenkba56f622004-02-06 23:19:44 +0000112
wdenkba56f622004-02-06 23:19:44 +0000113/* Ethernet Transmit and Receive Buffers */
114/* AS.HARNOIS
115 * In the same way ENET_MAX_MTU and ENET_MAX_MTU_ALIGNED are set from
116 * PKTSIZE and PKTSIZE_ALIGN (include/net.h)
117 */
Wolfgang Denk265817c2005-09-25 00:53:22 +0200118#define ENET_MAX_MTU PKTSIZE
wdenkba56f622004-02-06 23:19:44 +0000119#define ENET_MAX_MTU_ALIGNED PKTSIZE_ALIGN
120
wdenkba56f622004-02-06 23:19:44 +0000121/*-----------------------------------------------------------------------------+
122 * Defines for MAL/EMAC interrupt conditions as reported in the UIC (Universal
123 * Interrupt Controller).
124 *-----------------------------------------------------------------------------*/
125#define MAL_UIC_ERR ( UIC_MAL_SERR | UIC_MAL_TXDE | UIC_MAL_RXDE)
126#define MAL_UIC_DEF (UIC_MAL_RXEOB | MAL_UIC_ERR)
127#define EMAC_UIC_DEF UIC_ENET
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200128#define EMAC_UIC_DEF1 UIC_ENET1
129#define SEL_UIC_DEF(p) (p ? UIC_ENET1 : UIC_ENET )
wdenkba56f622004-02-06 23:19:44 +0000130
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200131#undef INFO_4XX_ENET
wdenkba56f622004-02-06 23:19:44 +0000132
Wolfgang Denk265817c2005-09-25 00:53:22 +0200133#define BI_PHYMODE_NONE 0
134#define BI_PHYMODE_ZMII 1
wdenk3c74e322004-02-22 23:46:08 +0000135#define BI_PHYMODE_RGMII 2
Stefan Roese887e2ec2006-09-07 11:51:23 +0200136#define BI_PHYMODE_GMII 3
137#define BI_PHYMODE_RTBI 4
138#define BI_PHYMODE_TBI 5
Stefan Roesedbbd1252007-10-05 17:10:59 +0200139#if defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
140 defined(CONFIG_405EX)
Stefan Roese887e2ec2006-09-07 11:51:23 +0200141#define BI_PHYMODE_SMII 6
142#define BI_PHYMODE_MII 7
143#endif
wdenk3c74e322004-02-22 23:46:08 +0000144
Stefan Roese1941cce2007-10-05 17:35:10 +0200145#if defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200146 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
147 defined(CONFIG_405EX)
Stefan Roese887e2ec2006-09-07 11:51:23 +0200148#define SDR0_MFR_ETH_CLK_SEL_V(n) ((0x01<<27) / (n+1))
149#endif
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200150
wdenkba56f622004-02-06 23:19:44 +0000151/*-----------------------------------------------------------------------------+
152 * Global variables. TX and RX descriptors and buffers.
153 *-----------------------------------------------------------------------------*/
154/* IER globals */
155static uint32_t mal_ier;
156
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200157#if !defined(CONFIG_NET_MULTI)
Stefan Roese4f92ac32005-10-10 17:43:58 +0200158struct eth_device *emac0_dev = NULL;
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200159#endif
160
Stefan Roese1e25f952005-10-20 16:34:28 +0200161/*
162 * Get count of EMAC devices (doesn't have to be the max. possible number
163 * supported by the cpu)
Stefan Roese353f2682007-10-23 10:10:08 +0200164 *
165 * CONFIG_BOARD_EMAC_COUNT added so now a "dynamic" way to configure the
166 * EMAC count is possible. As it is needed for the Kilauea/Haleakala
167 * 405EX/405EXr eval board, using the same binary.
Stefan Roese1e25f952005-10-20 16:34:28 +0200168 */
Stefan Roese353f2682007-10-23 10:10:08 +0200169#if defined(CONFIG_BOARD_EMAC_COUNT)
170#define LAST_EMAC_NUM board_emac_count()
171#else /* CONFIG_BOARD_EMAC_COUNT */
Stefan Roese1e25f952005-10-20 16:34:28 +0200172#if defined(CONFIG_HAS_ETH3)
173#define LAST_EMAC_NUM 4
174#elif defined(CONFIG_HAS_ETH2)
175#define LAST_EMAC_NUM 3
176#elif defined(CONFIG_HAS_ETH1)
177#define LAST_EMAC_NUM 2
178#else
179#define LAST_EMAC_NUM 1
180#endif
Stefan Roese353f2682007-10-23 10:10:08 +0200181#endif /* CONFIG_BOARD_EMAC_COUNT */
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200182
Stefan Roese5fb692c2007-01-18 10:25:34 +0100183/* normal boards start with EMAC0 */
184#if !defined(CONFIG_EMAC_NR_START)
185#define CONFIG_EMAC_NR_START 0
186#endif
187
Stefan Roesedbbd1252007-10-05 17:10:59 +0200188#if defined(CONFIG_405EX) || defined(CONFIG_440EPX)
189#define ETH_IRQ_NUM(dev) (VECNUM_ETH0 + ((dev)))
190#else
191#define ETH_IRQ_NUM(dev) (VECNUM_ETH0 + ((dev) * 2))
192#endif
193
Stefan Roeseff768cb2007-10-31 18:01:24 +0100194#define MAL_RX_DESC_SIZE 2048
195#define MAL_TX_DESC_SIZE 2048
196#define MAL_ALLOC_SIZE (MAL_TX_DESC_SIZE + MAL_RX_DESC_SIZE)
197
wdenkba56f622004-02-06 23:19:44 +0000198/*-----------------------------------------------------------------------------+
199 * Prototypes and externals.
200 *-----------------------------------------------------------------------------*/
201static void enet_rcv (struct eth_device *dev, unsigned long malisr);
202
203int enetInt (struct eth_device *dev);
204static void mal_err (struct eth_device *dev, unsigned long isr,
205 unsigned long uic, unsigned long maldef,
206 unsigned long mal_errr);
207static void emac_err (struct eth_device *dev, unsigned long isr);
208
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200209extern int phy_setup_aneg (char *devname, unsigned char addr);
210extern int emac4xx_miiphy_read (char *devname, unsigned char addr,
211 unsigned char reg, unsigned short *value);
212extern int emac4xx_miiphy_write (char *devname, unsigned char addr,
213 unsigned char reg, unsigned short value);
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200214
Stefan Roese353f2682007-10-23 10:10:08 +0200215int board_emac_count(void);
216
wdenkba56f622004-02-06 23:19:44 +0000217/*-----------------------------------------------------------------------------+
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200218| ppc_4xx_eth_halt
wdenkba56f622004-02-06 23:19:44 +0000219| Disable MAL channel, and EMACn
wdenkba56f622004-02-06 23:19:44 +0000220+-----------------------------------------------------------------------------*/
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200221static void ppc_4xx_eth_halt (struct eth_device *dev)
wdenkba56f622004-02-06 23:19:44 +0000222{
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200223 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenkba56f622004-02-06 23:19:44 +0000224 uint32_t failsafe = 10000;
Stefan Roesedbbd1252007-10-05 17:10:59 +0200225#if defined(CONFIG_440SPE) || \
226 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
227 defined(CONFIG_405EX)
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200228 unsigned long mfr;
229#endif
wdenkba56f622004-02-06 23:19:44 +0000230
Stefan Roese2d834762007-10-23 14:03:17 +0200231 out_be32((void *)EMAC_IER + hw_p->hw_addr, 0x00000000); /* disable emac interrupts */
wdenkba56f622004-02-06 23:19:44 +0000232
233 /* 1st reset MAL channel */
234 /* Note: writing a 0 to a channel has no effect */
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200235#if defined(CONFIG_405EP) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
236 mtdcr (maltxcarr, (MAL_CR_MMSR >> (hw_p->devnum * 2)));
237#else
wdenkba56f622004-02-06 23:19:44 +0000238 mtdcr (maltxcarr, (MAL_CR_MMSR >> hw_p->devnum));
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200239#endif
wdenkba56f622004-02-06 23:19:44 +0000240 mtdcr (malrxcarr, (MAL_CR_MMSR >> hw_p->devnum));
241
242 /* wait for reset */
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200243 while (mfdcr (malrxcasr) & (MAL_CR_MMSR >> hw_p->devnum)) {
wdenkba56f622004-02-06 23:19:44 +0000244 udelay (1000); /* Delay 1 MS so as not to hammer the register */
245 failsafe--;
246 if (failsafe == 0)
247 break;
wdenkba56f622004-02-06 23:19:44 +0000248 }
249
250 /* EMAC RESET */
Stefan Roesedbbd1252007-10-05 17:10:59 +0200251#if defined(CONFIG_440SPE) || \
252 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
253 defined(CONFIG_405EX)
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200254 /* provide clocks for EMAC internal loopback */
255 mfsdr (sdr_mfr, mfr);
Stefan Roese887e2ec2006-09-07 11:51:23 +0200256 mfr |= SDR0_MFR_ETH_CLK_SEL_V(hw_p->devnum);
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200257 mtsdr(sdr_mfr, mfr);
258#endif
259
Stefan Roese2d834762007-10-23 14:03:17 +0200260 out_be32((void *)EMAC_M0 + hw_p->hw_addr, EMAC_M0_SRST);
wdenkba56f622004-02-06 23:19:44 +0000261
Stefan Roesedbbd1252007-10-05 17:10:59 +0200262#if defined(CONFIG_440SPE) || \
263 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
264 defined(CONFIG_405EX)
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200265 /* remove clocks for EMAC internal loopback */
266 mfsdr (sdr_mfr, mfr);
Stefan Roese887e2ec2006-09-07 11:51:23 +0200267 mfr &= ~SDR0_MFR_ETH_CLK_SEL_V(hw_p->devnum);
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200268 mtsdr(sdr_mfr, mfr);
269#endif
270
271
Stefan Roesea93316c2005-10-18 19:17:12 +0200272#ifndef CONFIG_NETCONSOLE
Stefan Roesec157d8e2005-08-01 16:41:48 +0200273 hw_p->print_speed = 1; /* print speed message again next time */
Stefan Roesea93316c2005-10-18 19:17:12 +0200274#endif
Stefan Roesec157d8e2005-08-01 16:41:48 +0200275
wdenkba56f622004-02-06 23:19:44 +0000276 return;
277}
278
Stefan Roese846b0dd2005-08-08 12:42:22 +0200279#if defined (CONFIG_440GX)
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200280int ppc_4xx_eth_setup_bridge(int devnum, bd_t * bis)
wdenk855a4962004-03-14 18:23:55 +0000281{
282 unsigned long pfc1;
283 unsigned long zmiifer;
284 unsigned long rmiifer;
285
286 mfsdr(sdr_pfc1, pfc1);
287 pfc1 = SDR0_PFC1_EPS_DECODE(pfc1);
288
289 zmiifer = 0;
290 rmiifer = 0;
291
292 switch (pfc1) {
293 case 1:
294 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
295 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(1);
296 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(2);
297 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(3);
298 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
299 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
300 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
301 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
302 break;
303 case 2:
Stefan Roesef6e495f2006-11-27 17:43:25 +0100304 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(0);
305 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(1);
306 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(2);
307 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(3);
wdenk855a4962004-03-14 18:23:55 +0000308 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
309 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
310 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
311 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
312 break;
313 case 3:
314 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
315 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
316 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
317 bis->bi_phymode[1] = BI_PHYMODE_NONE;
318 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
319 bis->bi_phymode[3] = BI_PHYMODE_NONE;
320 break;
321 case 4:
322 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(0);
323 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(1);
324 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V (2);
325 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V (3);
326 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
327 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
328 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
329 bis->bi_phymode[3] = BI_PHYMODE_RGMII;
330 break;
331 case 5:
332 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (0);
333 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (1);
334 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (2);
335 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(3);
336 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
337 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
338 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
339 bis->bi_phymode[3] = BI_PHYMODE_RGMII;
340 break;
341 case 6:
342 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (0);
343 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (1);
344 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
wdenk855a4962004-03-14 18:23:55 +0000345 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
346 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
347 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
wdenk855a4962004-03-14 18:23:55 +0000348 break;
349 case 0:
350 default:
351 zmiifer = ZMII_FER_MII << ZMII_FER_V(devnum);
352 rmiifer = 0x0;
353 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
354 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
355 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
356 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
357 break;
358 }
359
360 /* Ensure we setup mdio for this devnum and ONLY this devnum */
361 zmiifer |= (ZMII_FER_MDI) << ZMII_FER_V(devnum);
362
Stefan Roeseff768cb2007-10-31 18:01:24 +0100363 out_be32((void *)ZMII_FER, zmiifer);
364 out_be32((void *)RGMII_FER, rmiifer);
wdenk855a4962004-03-14 18:23:55 +0000365
366 return ((int)pfc1);
wdenk855a4962004-03-14 18:23:55 +0000367}
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200368#endif /* CONFIG_440_GX */
wdenk855a4962004-03-14 18:23:55 +0000369
Stefan Roese887e2ec2006-09-07 11:51:23 +0200370#if defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
371int ppc_4xx_eth_setup_bridge(int devnum, bd_t * bis)
372{
373 unsigned long zmiifer=0x0;
Matthias Fuchs37ed6cd2007-04-24 14:03:45 +0200374 unsigned long pfc1;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200375
Matthias Fuchs37ed6cd2007-04-24 14:03:45 +0200376 mfsdr(sdr_pfc1, pfc1);
377 pfc1 &= SDR0_PFC1_SELECT_MASK;
378
Wolfgang Denk2f152782007-05-05 18:23:11 +0200379 switch (pfc1) {
Matthias Fuchs37ed6cd2007-04-24 14:03:45 +0200380 case SDR0_PFC1_SELECT_CONFIG_2:
Stefan Roese887e2ec2006-09-07 11:51:23 +0200381 /* 1 x GMII port */
Stefan Roese2d834762007-10-23 14:03:17 +0200382 out_be32((void *)ZMII_FER, 0x00);
383 out_be32((void *)RGMII_FER, 0x00000037);
Stefan Roese887e2ec2006-09-07 11:51:23 +0200384 bis->bi_phymode[0] = BI_PHYMODE_GMII;
385 bis->bi_phymode[1] = BI_PHYMODE_NONE;
386 break;
Matthias Fuchs37ed6cd2007-04-24 14:03:45 +0200387 case SDR0_PFC1_SELECT_CONFIG_4:
Stefan Roese887e2ec2006-09-07 11:51:23 +0200388 /* 2 x RGMII ports */
Stefan Roese2d834762007-10-23 14:03:17 +0200389 out_be32((void *)ZMII_FER, 0x00);
390 out_be32((void *)RGMII_FER, 0x00000055);
Stefan Roese887e2ec2006-09-07 11:51:23 +0200391 bis->bi_phymode[0] = BI_PHYMODE_RGMII;
392 bis->bi_phymode[1] = BI_PHYMODE_RGMII;
393 break;
Matthias Fuchs37ed6cd2007-04-24 14:03:45 +0200394 case SDR0_PFC1_SELECT_CONFIG_6:
Stefan Roese887e2ec2006-09-07 11:51:23 +0200395 /* 2 x SMII ports */
Stefan Roese2d834762007-10-23 14:03:17 +0200396 out_be32((void *)ZMII_FER,
397 ((ZMII_FER_SMII) << ZMII_FER_V(0)) |
398 ((ZMII_FER_SMII) << ZMII_FER_V(1)));
399 out_be32((void *)RGMII_FER, 0x00000000);
Matthias Fuchs37ed6cd2007-04-24 14:03:45 +0200400 bis->bi_phymode[0] = BI_PHYMODE_SMII;
401 bis->bi_phymode[1] = BI_PHYMODE_SMII;
402 break;
403 case SDR0_PFC1_SELECT_CONFIG_1_2:
404 /* only 1 x MII supported */
Stefan Roese2d834762007-10-23 14:03:17 +0200405 out_be32((void *)ZMII_FER, (ZMII_FER_MII) << ZMII_FER_V(0));
406 out_be32((void *)RGMII_FER, 0x00000000);
Matthias Fuchs37ed6cd2007-04-24 14:03:45 +0200407 bis->bi_phymode[0] = BI_PHYMODE_MII;
408 bis->bi_phymode[1] = BI_PHYMODE_NONE;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200409 break;
410 default:
411 break;
412 }
413
414 /* Ensure we setup mdio for this devnum and ONLY this devnum */
Stefan Roese2d834762007-10-23 14:03:17 +0200415 zmiifer = in_be32((void *)ZMII_FER);
Stefan Roese887e2ec2006-09-07 11:51:23 +0200416 zmiifer |= (ZMII_FER_MDI) << ZMII_FER_V(devnum);
Stefan Roese2d834762007-10-23 14:03:17 +0200417 out_be32((void *)ZMII_FER, zmiifer);
Stefan Roese887e2ec2006-09-07 11:51:23 +0200418
419 return ((int)0x0);
420}
421#endif /* CONFIG_440EPX */
422
Stefan Roesedbbd1252007-10-05 17:10:59 +0200423#if defined(CONFIG_405EX)
424int ppc_4xx_eth_setup_bridge(int devnum, bd_t * bis)
425{
426 u32 gmiifer = 0;
427
428 /*
429 * Right now only 2*RGMII is supported. Please extend when needed.
430 * sr - 2007-09-19
431 */
432 switch (1) {
433 case 1:
434 /* 2 x RGMII ports */
Stefan Roese2d834762007-10-23 14:03:17 +0200435 out_be32((void *)RGMII_FER, 0x00000055);
Stefan Roesedbbd1252007-10-05 17:10:59 +0200436 bis->bi_phymode[0] = BI_PHYMODE_RGMII;
437 bis->bi_phymode[1] = BI_PHYMODE_RGMII;
438 break;
439 case 2:
440 /* 2 x SMII ports */
441 break;
442 default:
443 break;
444 }
445
446 /* Ensure we setup mdio for this devnum and ONLY this devnum */
Stefan Roese2d834762007-10-23 14:03:17 +0200447 gmiifer = in_be32((void *)RGMII_FER);
Stefan Roesedbbd1252007-10-05 17:10:59 +0200448 gmiifer |= (1 << (19-devnum));
Stefan Roese2d834762007-10-23 14:03:17 +0200449 out_be32((void *)RGMII_FER, gmiifer);
Stefan Roesedbbd1252007-10-05 17:10:59 +0200450
451 return ((int)0x0);
452}
453#endif /* CONFIG_405EX */
454
Stefan Roeseff768cb2007-10-31 18:01:24 +0100455static inline void *malloc_aligned(u32 size, u32 align)
456{
457 return (void *)(((u32)malloc(size + align) + align - 1) &
458 ~(align - 1));
459}
460
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200461static int ppc_4xx_eth_init (struct eth_device *dev, bd_t * bis)
wdenkba56f622004-02-06 23:19:44 +0000462{
Stefan Roeseff768cb2007-10-31 18:01:24 +0100463 int i;
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200464 unsigned long reg = 0;
wdenkba56f622004-02-06 23:19:44 +0000465 unsigned long msr;
466 unsigned long speed;
467 unsigned long duplex;
468 unsigned long failsafe;
469 unsigned mode_reg;
470 unsigned short devnum;
471 unsigned short reg_short;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200472#if defined(CONFIG_440GX) || \
473 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200474 defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
475 defined(CONFIG_405EX)
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200476 sys_info_t sysinfo;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200477#if defined(CONFIG_440GX) || defined(CONFIG_440SPE) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200478 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
479 defined(CONFIG_405EX)
Stefan Roese6e7fb6e2005-11-29 18:18:21 +0100480 int ethgroup = -1;
481#endif
Stefan Roesec157d8e2005-08-01 16:41:48 +0200482#endif
Grzegorz Bernacki2db64782007-10-01 09:51:50 +0200483#if defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200484 defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
485 defined(CONFIG_405EX)
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200486 unsigned long mfr;
487#endif
Stefan Roeseff768cb2007-10-31 18:01:24 +0100488 u32 bd_cached;
489 u32 bd_uncached = 0;
Anatolij Gustschin4fae35a2008-02-25 20:54:04 +0100490#ifdef CONFIG_4xx_DCACHE
491 static u32 last_used_ea = 0;
492#endif
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200493
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200494 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenkba56f622004-02-06 23:19:44 +0000495
496 /* before doing anything, figure out if we have a MAC address */
497 /* if not, bail */
Stefan Roese4f92ac32005-10-10 17:43:58 +0200498 if (memcmp (dev->enetaddr, "\0\0\0\0\0\0", 6) == 0) {
499 printf("ERROR: ethaddr not set!\n");
wdenkba56f622004-02-06 23:19:44 +0000500 return -1;
Stefan Roese4f92ac32005-10-10 17:43:58 +0200501 }
wdenkba56f622004-02-06 23:19:44 +0000502
Stefan Roese887e2ec2006-09-07 11:51:23 +0200503#if defined(CONFIG_440GX) || \
504 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200505 defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
506 defined(CONFIG_405EX)
wdenkba56f622004-02-06 23:19:44 +0000507 /* Need to get the OPB frequency so we can access the PHY */
508 get_sys_info (&sysinfo);
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200509#endif
wdenkba56f622004-02-06 23:19:44 +0000510
wdenkba56f622004-02-06 23:19:44 +0000511 msr = mfmsr ();
512 mtmsr (msr & ~(MSR_EE)); /* disable interrupts */
513
514 devnum = hw_p->devnum;
515
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200516#ifdef INFO_4XX_ENET
wdenkba56f622004-02-06 23:19:44 +0000517 /* AS.HARNOIS
518 * We should have :
Wolfgang Denk265817c2005-09-25 00:53:22 +0200519 * hw_p->stats.pkts_handled <= hw_p->stats.pkts_rx <= hw_p->stats.pkts_handled+PKTBUFSRX
wdenkba56f622004-02-06 23:19:44 +0000520 * In the most cases hw_p->stats.pkts_handled = hw_p->stats.pkts_rx, but it
521 * is possible that new packets (without relationship with
522 * current transfer) have got the time to arrived before
523 * netloop calls eth_halt
524 */
525 printf ("About preceeding transfer (eth%d):\n"
526 "- Sent packet number %d\n"
527 "- Received packet number %d\n"
528 "- Handled packet number %d\n",
529 hw_p->devnum,
530 hw_p->stats.pkts_tx,
531 hw_p->stats.pkts_rx, hw_p->stats.pkts_handled);
532
533 hw_p->stats.pkts_tx = 0;
534 hw_p->stats.pkts_rx = 0;
535 hw_p->stats.pkts_handled = 0;
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200536 hw_p->print_speed = 1; /* print speed message again next time */
wdenkba56f622004-02-06 23:19:44 +0000537#endif
538
Wolfgang Denk265817c2005-09-25 00:53:22 +0200539 hw_p->tx_err_index = 0; /* Transmit Error Index for tx_err_log */
540 hw_p->rx_err_index = 0; /* Receive Error Index for rx_err_log */
wdenkba56f622004-02-06 23:19:44 +0000541
542 hw_p->rx_slot = 0; /* MAL Receive Slot */
543 hw_p->rx_i_index = 0; /* Receive Interrupt Queue Index */
544 hw_p->rx_u_index = 0; /* Receive User Queue Index */
545
546 hw_p->tx_slot = 0; /* MAL Transmit Slot */
547 hw_p->tx_i_index = 0; /* Transmit Interrupt Queue Index */
548 hw_p->tx_u_index = 0; /* Transmit User Queue Index */
549
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200550#if defined(CONFIG_440) && !defined(CONFIG_440SP) && !defined(CONFIG_440SPE)
wdenkba56f622004-02-06 23:19:44 +0000551 /* set RMII mode */
552 /* NOTE: 440GX spec states that mode is mutually exclusive */
553 /* NOTE: Therefore, disable all other EMACS, since we handle */
554 /* NOTE: only one emac at a time */
555 reg = 0;
Stefan Roese2d834762007-10-23 14:03:17 +0200556 out_be32((void *)ZMII_FER, 0);
wdenkba56f622004-02-06 23:19:44 +0000557 udelay (100);
wdenkba56f622004-02-06 23:19:44 +0000558
Stefan Roese846b0dd2005-08-08 12:42:22 +0200559#if defined(CONFIG_440EP) || defined(CONFIG_440GR)
Stefan Roese2d834762007-10-23 14:03:17 +0200560 out_be32((void *)ZMII_FER, (ZMII_FER_RMII | ZMII_FER_MDI) << ZMII_FER_V (devnum));
Stefan Roese887e2ec2006-09-07 11:51:23 +0200561#elif defined(CONFIG_440GX) || defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200562 ethgroup = ppc_4xx_eth_setup_bridge(devnum, bis);
Stefan Roese4a3cd9e2005-09-07 16:21:12 +0200563#elif defined(CONFIG_440GP)
564 /* set RMII mode */
Stefan Roese2d834762007-10-23 14:03:17 +0200565 out_be32((void *)ZMII_FER, ZMII_RMII | ZMII_MDI0);
wdenk0e6d7982004-03-14 00:07:33 +0000566#else
567 if ((devnum == 0) || (devnum == 1)) {
Stefan Roese2d834762007-10-23 14:03:17 +0200568 out_be32((void *)ZMII_FER, (ZMII_FER_SMII | ZMII_FER_MDI) << ZMII_FER_V (devnum));
Stefan Roese1c2ce222006-11-27 14:12:17 +0100569 } else { /* ((devnum == 2) || (devnum == 3)) */
Stefan Roese2d834762007-10-23 14:03:17 +0200570 out_be32((void *)ZMII_FER, ZMII_FER_MDI << ZMII_FER_V (devnum));
571 out_be32((void *)RGMII_FER, ((RGMII_FER_RGMII << RGMII_FER_V (2)) |
572 (RGMII_FER_RGMII << RGMII_FER_V (3))));
wdenk0e6d7982004-03-14 00:07:33 +0000573 }
574#endif
Stefan Roesec57c7982005-08-11 17:56:56 +0200575
Stefan Roese2d834762007-10-23 14:03:17 +0200576 out_be32((void *)ZMII_SSR, ZMII_SSR_SP << ZMII_SSR_V(devnum));
Stefan Roese6e7fb6e2005-11-29 18:18:21 +0100577#endif /* defined(CONFIG_440) && !defined(CONFIG_440SP) */
Stefan Roesedbbd1252007-10-05 17:10:59 +0200578#if defined(CONFIG_405EX)
579 ethgroup = ppc_4xx_eth_setup_bridge(devnum, bis);
580#endif
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200581
wdenk0e6d7982004-03-14 00:07:33 +0000582 __asm__ volatile ("eieio");
583
584 /* reset emac so we have access to the phy */
Stefan Roesedbbd1252007-10-05 17:10:59 +0200585#if defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
586 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
587 defined(CONFIG_405EX)
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200588 /* provide clocks for EMAC internal loopback */
589 mfsdr (sdr_mfr, mfr);
Stefan Roese887e2ec2006-09-07 11:51:23 +0200590 mfr |= SDR0_MFR_ETH_CLK_SEL_V(devnum);
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200591 mtsdr(sdr_mfr, mfr);
592#endif
wdenk0e6d7982004-03-14 00:07:33 +0000593
Stefan Roese2d834762007-10-23 14:03:17 +0200594 out_be32((void *)EMAC_M0 + hw_p->hw_addr, EMAC_M0_SRST);
wdenkba56f622004-02-06 23:19:44 +0000595
596 failsafe = 1000;
Stefan Roese2d834762007-10-23 14:03:17 +0200597 while ((in_be32((void *)EMAC_M0 + hw_p->hw_addr) & (EMAC_M0_SRST)) && failsafe) {
wdenkba56f622004-02-06 23:19:44 +0000598 udelay (1000);
599 failsafe--;
600 }
Stefan Roese887e2ec2006-09-07 11:51:23 +0200601 if (failsafe <= 0)
602 printf("\nProblem resetting EMAC!\n");
wdenkba56f622004-02-06 23:19:44 +0000603
Stefan Roesedbbd1252007-10-05 17:10:59 +0200604#if defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
605 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
606 defined(CONFIG_405EX)
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200607 /* remove clocks for EMAC internal loopback */
608 mfsdr (sdr_mfr, mfr);
Stefan Roese887e2ec2006-09-07 11:51:23 +0200609 mfr &= ~SDR0_MFR_ETH_CLK_SEL_V(devnum);
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200610 mtsdr(sdr_mfr, mfr);
611#endif
612
Stefan Roese887e2ec2006-09-07 11:51:23 +0200613#if defined(CONFIG_440GX) || \
614 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200615 defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
616 defined(CONFIG_405EX)
wdenkba56f622004-02-06 23:19:44 +0000617 /* Whack the M1 register */
618 mode_reg = 0x0;
619 mode_reg &= ~0x00000038;
620 if (sysinfo.freqOPB <= 50000000);
621 else if (sysinfo.freqOPB <= 66666667)
622 mode_reg |= EMAC_M1_OBCI_66;
623 else if (sysinfo.freqOPB <= 83333333)
624 mode_reg |= EMAC_M1_OBCI_83;
625 else if (sysinfo.freqOPB <= 100000000)
626 mode_reg |= EMAC_M1_OBCI_100;
627 else
628 mode_reg |= EMAC_M1_OBCI_GT100;
629
Stefan Roese2d834762007-10-23 14:03:17 +0200630 out_be32((void *)EMAC_M1 + hw_p->hw_addr, mode_reg);
Stefan Roese6e7fb6e2005-11-29 18:18:21 +0100631#endif /* defined(CONFIG_440GX) || defined(CONFIG_440SP) */
wdenkba56f622004-02-06 23:19:44 +0000632
633 /* wait for PHY to complete auto negotiation */
634 reg_short = 0;
635#ifndef CONFIG_CS8952_PHY
636 switch (devnum) {
637 case 0:
638 reg = CONFIG_PHY_ADDR;
639 break;
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200640#if defined (CONFIG_PHY1_ADDR)
wdenkba56f622004-02-06 23:19:44 +0000641 case 1:
642 reg = CONFIG_PHY1_ADDR;
643 break;
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200644#endif
Stefan Roese846b0dd2005-08-08 12:42:22 +0200645#if defined (CONFIG_440GX)
wdenkba56f622004-02-06 23:19:44 +0000646 case 2:
647 reg = CONFIG_PHY2_ADDR;
648 break;
649 case 3:
650 reg = CONFIG_PHY3_ADDR;
651 break;
652#endif
653 default:
654 reg = CONFIG_PHY_ADDR;
655 break;
656 }
657
wdenk3c74e322004-02-22 23:46:08 +0000658 bis->bi_phynum[devnum] = reg;
659
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200660#if defined(CONFIG_PHY_RESET)
wdenka06752e2004-09-29 22:43:59 +0000661 /*
662 * Reset the phy, only if its the first time through
663 * otherwise, just check the speeds & feeds
664 */
665 if (hw_p->first_init == 0) {
Stefan Roeseec0c2ec2006-11-27 14:46:06 +0100666#if defined(CONFIG_M88E1111_PHY)
Stefan Roese887e2ec2006-09-07 11:51:23 +0200667 miiphy_write (dev->name, reg, 0x14, 0x0ce3);
668 miiphy_write (dev->name, reg, 0x18, 0x4101);
669 miiphy_write (dev->name, reg, 0x09, 0x0e00);
670 miiphy_write (dev->name, reg, 0x04, 0x01e1);
671#endif
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200672 miiphy_reset (dev->name, reg);
wdenkba56f622004-02-06 23:19:44 +0000673
Stefan Roese887e2ec2006-09-07 11:51:23 +0200674#if defined(CONFIG_440GX) || \
675 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200676 defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
677 defined(CONFIG_405EX)
Stefan Roese887e2ec2006-09-07 11:51:23 +0200678
wdenk0e6d7982004-03-14 00:07:33 +0000679#if defined(CONFIG_CIS8201_PHY)
wdenkfc1cfcd2004-04-25 15:41:35 +0000680 /*
Stefan Roese17f50f222005-08-04 17:09:16 +0200681 * Cicada 8201 PHY needs to have an extended register whacked
682 * for RGMII mode.
wdenkfc1cfcd2004-04-25 15:41:35 +0000683 */
Stefan Roese887e2ec2006-09-07 11:51:23 +0200684 if (((devnum == 2) || (devnum == 3)) && (4 == ethgroup)) {
Stefan Roeseb79316f2005-08-15 12:31:23 +0200685#if defined(CONFIG_CIS8201_SHORT_ETCH)
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200686 miiphy_write (dev->name, reg, 23, 0x1300);
Stefan Roeseb79316f2005-08-15 12:31:23 +0200687#else
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200688 miiphy_write (dev->name, reg, 23, 0x1000);
Stefan Roeseb79316f2005-08-15 12:31:23 +0200689#endif
Stefan Roese17f50f222005-08-04 17:09:16 +0200690 /*
691 * Vitesse VSC8201/Cicada CIS8201 errata:
692 * Interoperability problem with Intel 82547EI phys
693 * This work around (provided by Vitesse) changes
694 * the default timer convergence from 8ms to 12ms
695 */
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200696 miiphy_write (dev->name, reg, 0x1f, 0x2a30);
697 miiphy_write (dev->name, reg, 0x08, 0x0200);
698 miiphy_write (dev->name, reg, 0x1f, 0x52b5);
699 miiphy_write (dev->name, reg, 0x02, 0x0004);
700 miiphy_write (dev->name, reg, 0x01, 0x0671);
701 miiphy_write (dev->name, reg, 0x00, 0x8fae);
702 miiphy_write (dev->name, reg, 0x1f, 0x2a30);
703 miiphy_write (dev->name, reg, 0x08, 0x0000);
704 miiphy_write (dev->name, reg, 0x1f, 0x0000);
Stefan Roese17f50f222005-08-04 17:09:16 +0200705 /* end Vitesse/Cicada errata */
706 }
wdenk0e6d7982004-03-14 00:07:33 +0000707#endif
Stefan Roese5fb692c2007-01-18 10:25:34 +0100708
709#if defined(CONFIG_ET1011C_PHY)
710 /*
711 * Agere ET1011c PHY needs to have an extended register whacked
712 * for RGMII mode.
713 */
714 if (((devnum == 2) || (devnum ==3)) && (4 == ethgroup)) {
715 miiphy_read (dev->name, reg, 0x16, &reg_short);
716 reg_short &= ~(0x7);
717 reg_short |= 0x6; /* RGMII DLL Delay*/
718 miiphy_write (dev->name, reg, 0x16, reg_short);
719
720 miiphy_read (dev->name, reg, 0x17, &reg_short);
721 reg_short &= ~(0x40);
722 miiphy_write (dev->name, reg, 0x17, reg_short);
723
724 miiphy_write(dev->name, reg, 0x1c, 0x74f0);
725 }
726#endif
727
wdenk855a4962004-03-14 18:23:55 +0000728#endif
wdenka06752e2004-09-29 22:43:59 +0000729 /* Start/Restart autonegotiation */
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200730 phy_setup_aneg (dev->name, reg);
wdenka06752e2004-09-29 22:43:59 +0000731 udelay (1000);
732 }
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200733#endif /* defined(CONFIG_PHY_RESET) */
wdenkba56f622004-02-06 23:19:44 +0000734
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200735 miiphy_read (dev->name, reg, PHY_BMSR, &reg_short);
wdenkba56f622004-02-06 23:19:44 +0000736
737 /*
wdenk0e6d7982004-03-14 00:07:33 +0000738 * Wait if PHY is capable of autonegotiation and autonegotiation is not complete
wdenkba56f622004-02-06 23:19:44 +0000739 */
740 if ((reg_short & PHY_BMSR_AUTN_ABLE)
741 && !(reg_short & PHY_BMSR_AUTN_COMP)) {
742 puts ("Waiting for PHY auto negotiation to complete");
743 i = 0;
744 while (!(reg_short & PHY_BMSR_AUTN_COMP)) {
745 /*
746 * Timeout reached ?
747 */
748 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
749 puts (" TIMEOUT !\n");
750 break;
751 }
752
753 if ((i++ % 1000) == 0) {
754 putc ('.');
755 }
756 udelay (1000); /* 1 ms */
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200757 miiphy_read (dev->name, reg, PHY_BMSR, &reg_short);
wdenkba56f622004-02-06 23:19:44 +0000758
759 }
760 puts (" done\n");
761 udelay (500000); /* another 500 ms (results in faster booting) */
762 }
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200763#endif /* #ifndef CONFIG_CS8952_PHY */
764
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200765 speed = miiphy_speed (dev->name, reg);
766 duplex = miiphy_duplex (dev->name, reg);
wdenkba56f622004-02-06 23:19:44 +0000767
768 if (hw_p->print_speed) {
769 hw_p->print_speed = 0;
Stefan Roese5fb692c2007-01-18 10:25:34 +0100770 printf ("ENET Speed is %d Mbps - %s duplex connection (EMAC%d)\n",
771 (int) speed, (duplex == HALF) ? "HALF" : "FULL",
772 hw_p->devnum);
wdenkba56f622004-02-06 23:19:44 +0000773 }
774
Stefan Roese887e2ec2006-09-07 11:51:23 +0200775#if defined(CONFIG_440) && !defined(CONFIG_440SP) && !defined(CONFIG_440SPE) && \
776 !defined(CONFIG_440EPX) && !defined(CONFIG_440GRX)
Stefan Roese846b0dd2005-08-08 12:42:22 +0200777#if defined(CONFIG_440EP) || defined(CONFIG_440GR)
Stefan Roesec157d8e2005-08-01 16:41:48 +0200778 mfsdr(sdr_mfr, reg);
779 if (speed == 100) {
780 reg = (reg & ~SDR0_MFR_ZMII_MODE_MASK) | SDR0_MFR_ZMII_MODE_RMII_100M;
781 } else {
782 reg = (reg & ~SDR0_MFR_ZMII_MODE_MASK) | SDR0_MFR_ZMII_MODE_RMII_10M;
783 }
784 mtsdr(sdr_mfr, reg);
785#endif
Stefan Roesec57c7982005-08-11 17:56:56 +0200786
wdenkba56f622004-02-06 23:19:44 +0000787 /* Set ZMII/RGMII speed according to the phy link speed */
Stefan Roeseff768cb2007-10-31 18:01:24 +0100788 reg = in_be32((void *)ZMII_SSR);
wdenk855a4962004-03-14 18:23:55 +0000789 if ( (speed == 100) || (speed == 1000) )
Stefan Roeseff768cb2007-10-31 18:01:24 +0100790 out_be32((void *)ZMII_SSR, reg | (ZMII_SSR_SP << ZMII_SSR_V (devnum)));
wdenkba56f622004-02-06 23:19:44 +0000791 else
Stefan Roeseff768cb2007-10-31 18:01:24 +0100792 out_be32((void *)ZMII_SSR, reg & (~(ZMII_SSR_SP << ZMII_SSR_V (devnum))));
wdenkba56f622004-02-06 23:19:44 +0000793
794 if ((devnum == 2) || (devnum == 3)) {
795 if (speed == 1000)
796 reg = (RGMII_SSR_SP_1000MBPS << RGMII_SSR_V (devnum));
797 else if (speed == 100)
798 reg = (RGMII_SSR_SP_100MBPS << RGMII_SSR_V (devnum));
Stefan Roese887e2ec2006-09-07 11:51:23 +0200799 else if (speed == 10)
wdenkba56f622004-02-06 23:19:44 +0000800 reg = (RGMII_SSR_SP_10MBPS << RGMII_SSR_V (devnum));
Stefan Roese887e2ec2006-09-07 11:51:23 +0200801 else {
802 printf("Error in RGMII Speed\n");
803 return -1;
804 }
Stefan Roeseff768cb2007-10-31 18:01:24 +0100805 out_be32((void *)RGMII_SSR, reg);
wdenkba56f622004-02-06 23:19:44 +0000806 }
Stefan Roese6e7fb6e2005-11-29 18:18:21 +0100807#endif /* defined(CONFIG_440) && !defined(CONFIG_440SP) */
wdenkba56f622004-02-06 23:19:44 +0000808
Stefan Roesedbbd1252007-10-05 17:10:59 +0200809#if defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
810 defined(CONFIG_405EX)
Stefan Roese887e2ec2006-09-07 11:51:23 +0200811 if (speed == 1000)
812 reg = (RGMII_SSR_SP_1000MBPS << RGMII_SSR_V (devnum));
813 else if (speed == 100)
814 reg = (RGMII_SSR_SP_100MBPS << RGMII_SSR_V (devnum));
815 else if (speed == 10)
816 reg = (RGMII_SSR_SP_10MBPS << RGMII_SSR_V (devnum));
817 else {
818 printf("Error in RGMII Speed\n");
819 return -1;
820 }
Stefan Roese2d834762007-10-23 14:03:17 +0200821 out_be32((void *)RGMII_SSR, reg);
Stefan Roese887e2ec2006-09-07 11:51:23 +0200822#endif
823
wdenkba56f622004-02-06 23:19:44 +0000824 /* set the Mal configuration reg */
Stefan Roese887e2ec2006-09-07 11:51:23 +0200825#if defined(CONFIG_440GX) || \
826 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200827 defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
828 defined(CONFIG_405EX)
Stefan Roese17f50f222005-08-04 17:09:16 +0200829 mtdcr (malmcr, MAL_CR_PLBB | MAL_CR_OPBBL | MAL_CR_LEA |
830 MAL_CR_PLBLT_DEFAULT | MAL_CR_EOPIE | 0x00330000);
831#else
832 mtdcr (malmcr, MAL_CR_PLBB | MAL_CR_OPBBL | MAL_CR_LEA | MAL_CR_PLBLT_DEFAULT);
wdenkba56f622004-02-06 23:19:44 +0000833 /* Errata 1.12: MAL_1 -- Disable MAL bursting */
Stefan Roese17f50f222005-08-04 17:09:16 +0200834 if (get_pvr() == PVR_440GP_RB) {
835 mtdcr (malmcr, mfdcr(malmcr) & ~MAL_CR_PLBB);
836 }
837#endif
wdenkba56f622004-02-06 23:19:44 +0000838
wdenkba56f622004-02-06 23:19:44 +0000839 /*
840 * Malloc MAL buffer desciptors, make sure they are
841 * aligned on cache line boundary size
842 * (401/403/IOP480 = 16, 405 = 32)
843 * and doesn't cross cache block boundaries.
844 */
Stefan Roeseff768cb2007-10-31 18:01:24 +0100845 if (hw_p->first_init == 0) {
846 debug("*** Allocating descriptor memory ***\n");
wdenkba56f622004-02-06 23:19:44 +0000847
Stefan Roeseff768cb2007-10-31 18:01:24 +0100848 bd_cached = (u32)malloc_aligned(MAL_ALLOC_SIZE, 4096);
849 if (!bd_cached) {
850 printf("%s: Error allocating MAL descriptor buffers!\n");
851 return -1;
852 }
Stefan Roeseb79316f2005-08-15 12:31:23 +0200853
Stefan Roeseff768cb2007-10-31 18:01:24 +0100854#ifdef CONFIG_4xx_DCACHE
Matthias Fuchsba79fde2007-12-14 11:19:56 +0100855 flush_dcache_range(bd_cached, bd_cached + MAL_ALLOC_SIZE);
Anatolij Gustschin4fae35a2008-02-25 20:54:04 +0100856 if (!last_used_ea)
857 bd_uncached = bis->bi_memsize;
858 else
859 bd_uncached = last_used_ea + MAL_ALLOC_SIZE;
860
861 last_used_ea = bd_uncached;
Stefan Roeseff768cb2007-10-31 18:01:24 +0100862 program_tlb(bd_cached, bd_uncached, MAL_ALLOC_SIZE,
863 TLB_WORD2_I_ENABLE);
864#else
865 bd_uncached = bd_cached;
866#endif
867 hw_p->tx_phys = bd_cached;
868 hw_p->rx_phys = bd_cached + MAL_TX_DESC_SIZE;
869 hw_p->tx = (mal_desc_t *)(bd_uncached);
870 hw_p->rx = (mal_desc_t *)(bd_uncached + MAL_TX_DESC_SIZE);
871 debug("hw_p->tx=%08x, hw_p->rx=%08x\n", hw_p->tx, hw_p->rx);
wdenkba56f622004-02-06 23:19:44 +0000872 }
873
874 for (i = 0; i < NUM_TX_BUFF; i++) {
875 hw_p->tx[i].ctrl = 0;
876 hw_p->tx[i].data_len = 0;
Stefan Roeseff768cb2007-10-31 18:01:24 +0100877 if (hw_p->first_init == 0)
878 hw_p->txbuf_ptr = malloc_aligned(MAL_ALLOC_SIZE,
879 L1_CACHE_BYTES);
wdenkba56f622004-02-06 23:19:44 +0000880 hw_p->tx[i].data_ptr = hw_p->txbuf_ptr;
881 if ((NUM_TX_BUFF - 1) == i)
882 hw_p->tx[i].ctrl |= MAL_TX_CTRL_WRAP;
883 hw_p->tx_run[i] = -1;
Stefan Roeseff768cb2007-10-31 18:01:24 +0100884 debug("TX_BUFF %d @ 0x%08lx\n", i, (u32)hw_p->tx[i].data_ptr);
wdenkba56f622004-02-06 23:19:44 +0000885 }
886
887 for (i = 0; i < NUM_RX_BUFF; i++) {
888 hw_p->rx[i].ctrl = 0;
889 hw_p->rx[i].data_len = 0;
Stefan Roeseff768cb2007-10-31 18:01:24 +0100890 hw_p->rx[i].data_ptr = (char *)NetRxPackets[i];
wdenkba56f622004-02-06 23:19:44 +0000891 if ((NUM_RX_BUFF - 1) == i)
892 hw_p->rx[i].ctrl |= MAL_RX_CTRL_WRAP;
893 hw_p->rx[i].ctrl |= MAL_RX_CTRL_EMPTY | MAL_RX_CTRL_INTR;
894 hw_p->rx_ready[i] = -1;
Stefan Roeseff768cb2007-10-31 18:01:24 +0100895 debug("RX_BUFF %d @ 0x%08lx\n", i, (u32)hw_p->rx[i].data_ptr);
wdenkba56f622004-02-06 23:19:44 +0000896 }
897
898 reg = 0x00000000;
899
900 reg |= dev->enetaddr[0]; /* set high address */
901 reg = reg << 8;
902 reg |= dev->enetaddr[1];
903
Stefan Roese2d834762007-10-23 14:03:17 +0200904 out_be32((void *)EMAC_IAH + hw_p->hw_addr, reg);
wdenkba56f622004-02-06 23:19:44 +0000905
906 reg = 0x00000000;
907 reg |= dev->enetaddr[2]; /* set low address */
908 reg = reg << 8;
909 reg |= dev->enetaddr[3];
910 reg = reg << 8;
911 reg |= dev->enetaddr[4];
912 reg = reg << 8;
913 reg |= dev->enetaddr[5];
914
Stefan Roese2d834762007-10-23 14:03:17 +0200915 out_be32((void *)EMAC_IAL + hw_p->hw_addr, reg);
wdenkba56f622004-02-06 23:19:44 +0000916
917 switch (devnum) {
918 case 1:
919 /* setup MAL tx & rx channel pointers */
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200920#if defined (CONFIG_405EP) || defined (CONFIG_440EP) || defined (CONFIG_440GR)
Stefan Roeseff768cb2007-10-31 18:01:24 +0100921 mtdcr (maltxctp2r, hw_p->tx_phys);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200922#else
Stefan Roeseff768cb2007-10-31 18:01:24 +0100923 mtdcr (maltxctp1r, hw_p->tx_phys);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200924#endif
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200925#if defined(CONFIG_440)
Stefan Roesec157d8e2005-08-01 16:41:48 +0200926 mtdcr (maltxbattr, 0x0);
wdenkba56f622004-02-06 23:19:44 +0000927 mtdcr (malrxbattr, 0x0);
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200928#endif
Stefan Roeseff768cb2007-10-31 18:01:24 +0100929 mtdcr (malrxctp1r, hw_p->rx_phys);
wdenkba56f622004-02-06 23:19:44 +0000930 /* set RX buffer size */
931 mtdcr (malrcbs1, ENET_MAX_MTU_ALIGNED / 16);
932 break;
Stefan Roese846b0dd2005-08-08 12:42:22 +0200933#if defined (CONFIG_440GX)
wdenkba56f622004-02-06 23:19:44 +0000934 case 2:
935 /* setup MAL tx & rx channel pointers */
936 mtdcr (maltxbattr, 0x0);
wdenkba56f622004-02-06 23:19:44 +0000937 mtdcr (malrxbattr, 0x0);
Stefan Roeseff768cb2007-10-31 18:01:24 +0100938 mtdcr (maltxctp2r, hw_p->tx_phys);
939 mtdcr (malrxctp2r, hw_p->rx_phys);
wdenkba56f622004-02-06 23:19:44 +0000940 /* set RX buffer size */
941 mtdcr (malrcbs2, ENET_MAX_MTU_ALIGNED / 16);
942 break;
943 case 3:
944 /* setup MAL tx & rx channel pointers */
945 mtdcr (maltxbattr, 0x0);
Stefan Roeseff768cb2007-10-31 18:01:24 +0100946 mtdcr (maltxctp3r, hw_p->tx_phys);
wdenkba56f622004-02-06 23:19:44 +0000947 mtdcr (malrxbattr, 0x0);
Stefan Roeseff768cb2007-10-31 18:01:24 +0100948 mtdcr (malrxctp3r, hw_p->rx_phys);
wdenkba56f622004-02-06 23:19:44 +0000949 /* set RX buffer size */
950 mtdcr (malrcbs3, ENET_MAX_MTU_ALIGNED / 16);
951 break;
Stefan Roesec57c7982005-08-11 17:56:56 +0200952#endif /* CONFIG_440GX */
wdenkba56f622004-02-06 23:19:44 +0000953 case 0:
954 default:
955 /* setup MAL tx & rx channel pointers */
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200956#if defined(CONFIG_440)
wdenkba56f622004-02-06 23:19:44 +0000957 mtdcr (maltxbattr, 0x0);
wdenkba56f622004-02-06 23:19:44 +0000958 mtdcr (malrxbattr, 0x0);
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200959#endif
Stefan Roeseff768cb2007-10-31 18:01:24 +0100960 mtdcr (maltxctp0r, hw_p->tx_phys);
961 mtdcr (malrxctp0r, hw_p->rx_phys);
wdenkba56f622004-02-06 23:19:44 +0000962 /* set RX buffer size */
963 mtdcr (malrcbs0, ENET_MAX_MTU_ALIGNED / 16);
964 break;
965 }
966
967 /* Enable MAL transmit and receive channels */
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200968#if defined(CONFIG_405EP) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
Stefan Roesec157d8e2005-08-01 16:41:48 +0200969 mtdcr (maltxcasr, (MAL_TXRX_CASR >> (hw_p->devnum*2)));
970#else
wdenkba56f622004-02-06 23:19:44 +0000971 mtdcr (maltxcasr, (MAL_TXRX_CASR >> hw_p->devnum));
Stefan Roesec157d8e2005-08-01 16:41:48 +0200972#endif
wdenkba56f622004-02-06 23:19:44 +0000973 mtdcr (malrxcasr, (MAL_TXRX_CASR >> hw_p->devnum));
974
975 /* set transmit enable & receive enable */
Stefan Roese2d834762007-10-23 14:03:17 +0200976 out_be32((void *)EMAC_M0 + hw_p->hw_addr, EMAC_M0_TXE | EMAC_M0_RXE);
wdenkba56f622004-02-06 23:19:44 +0000977
Stefan Roese2d834762007-10-23 14:03:17 +0200978 mode_reg = in_be32((void *)EMAC_M1 + hw_p->hw_addr);
Stefan Roese76957cb2008-03-01 12:11:40 +0100979
980 /* set rx-/tx-fifo size */
981 mode_reg = (mode_reg & ~EMAC_MR1_FIFO_MASK) | EMAC_MR1_FIFO_SIZE;
wdenkba56f622004-02-06 23:19:44 +0000982
983 /* set speed */
Stefan Roese6e7fb6e2005-11-29 18:18:21 +0100984 if (speed == _1000BASET) {
Stefan Roese738815c2007-10-02 11:44:46 +0200985#if defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
986 defined(CONFIG_440SP) || defined(CONFIG_440SPE)
Stefan Roese6e7fb6e2005-11-29 18:18:21 +0100987 unsigned long pfc1;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200988
Stefan Roese6e7fb6e2005-11-29 18:18:21 +0100989 mfsdr (sdr_pfc1, pfc1);
990 pfc1 |= SDR0_PFC1_EM_1000;
991 mtsdr (sdr_pfc1, pfc1);
992#endif
wdenk855a4962004-03-14 18:23:55 +0000993 mode_reg = mode_reg | EMAC_M1_MF_1000MBPS | EMAC_M1_IST;
Stefan Roese6e7fb6e2005-11-29 18:18:21 +0100994 } else if (speed == _100BASET)
wdenkba56f622004-02-06 23:19:44 +0000995 mode_reg = mode_reg | EMAC_M1_MF_100MBPS | EMAC_M1_IST;
996 else
997 mode_reg = mode_reg & ~0x00C00000; /* 10 MBPS */
998 if (duplex == FULL)
999 mode_reg = mode_reg | 0x80000000 | EMAC_M1_IST;
1000
Stefan Roese2d834762007-10-23 14:03:17 +02001001 out_be32((void *)EMAC_M1 + hw_p->hw_addr, mode_reg);
wdenkba56f622004-02-06 23:19:44 +00001002
1003 /* Enable broadcast and indvidual address */
1004 /* TBS: enabling runts as some misbehaved nics will send runts */
Stefan Roese2d834762007-10-23 14:03:17 +02001005 out_be32((void *)EMAC_RXM + hw_p->hw_addr, EMAC_RMR_BAE | EMAC_RMR_IAE);
wdenkba56f622004-02-06 23:19:44 +00001006
1007 /* we probably need to set the tx mode1 reg? maybe at tx time */
1008
1009 /* set transmit request threshold register */
Stefan Roese2d834762007-10-23 14:03:17 +02001010 out_be32((void *)EMAC_TRTR + hw_p->hw_addr, 0x18000000); /* 256 byte threshold */
wdenkba56f622004-02-06 23:19:44 +00001011
Wolfgang Denk265817c2005-09-25 00:53:22 +02001012 /* set receive low/high water mark register */
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001013#if defined(CONFIG_440)
Marian Balakowicz6c5879f2006-06-30 16:30:46 +02001014 /* 440s has a 64 byte burst length */
Stefan Roese2d834762007-10-23 14:03:17 +02001015 out_be32((void *)EMAC_RX_HI_LO_WMARK + hw_p->hw_addr, 0x80009000);
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001016#else
1017 /* 405s have a 16 byte burst length */
Stefan Roese2d834762007-10-23 14:03:17 +02001018 out_be32((void *)EMAC_RX_HI_LO_WMARK + hw_p->hw_addr, 0x0f002000);
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001019#endif /* defined(CONFIG_440) */
Stefan Roese2d834762007-10-23 14:03:17 +02001020 out_be32((void *)EMAC_TXM1 + hw_p->hw_addr, 0xf8640000);
wdenkba56f622004-02-06 23:19:44 +00001021
1022 /* Set fifo limit entry in tx mode 0 */
Stefan Roese2d834762007-10-23 14:03:17 +02001023 out_be32((void *)EMAC_TXM0 + hw_p->hw_addr, 0x00000003);
wdenkba56f622004-02-06 23:19:44 +00001024 /* Frame gap set */
Stefan Roese2d834762007-10-23 14:03:17 +02001025 out_be32((void *)EMAC_I_FRAME_GAP_REG + hw_p->hw_addr, 0x00000008);
wdenkba56f622004-02-06 23:19:44 +00001026
1027 /* Set EMAC IER */
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001028 hw_p->emac_ier = EMAC_ISR_PTLE | EMAC_ISR_BFCS | EMAC_ISR_ORE | EMAC_ISR_IRE;
wdenkba56f622004-02-06 23:19:44 +00001029 if (speed == _100BASET)
1030 hw_p->emac_ier = hw_p->emac_ier | EMAC_ISR_SYE;
1031
Stefan Roese2d834762007-10-23 14:03:17 +02001032 out_be32((void *)EMAC_ISR + hw_p->hw_addr, 0xffffffff); /* clear pending interrupts */
1033 out_be32((void *)EMAC_IER + hw_p->hw_addr, hw_p->emac_ier);
wdenkba56f622004-02-06 23:19:44 +00001034
1035 if (hw_p->first_init == 0) {
1036 /*
1037 * Connect interrupt service routines
1038 */
Stefan Roesedbbd1252007-10-05 17:10:59 +02001039 irq_install_handler(ETH_IRQ_NUM(hw_p->devnum),
1040 (interrupt_handler_t *) enetInt, dev);
wdenkba56f622004-02-06 23:19:44 +00001041 }
wdenkba56f622004-02-06 23:19:44 +00001042
1043 mtmsr (msr); /* enable interrupts again */
1044
1045 hw_p->bis = bis;
1046 hw_p->first_init = 1;
1047
Stefan Roese802b7692008-01-08 18:39:30 +01001048 return 0;
wdenkba56f622004-02-06 23:19:44 +00001049}
1050
1051
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001052static int ppc_4xx_eth_send (struct eth_device *dev, volatile void *ptr,
wdenkba56f622004-02-06 23:19:44 +00001053 int len)
1054{
1055 struct enet_frame *ef_ptr;
1056 ulong time_start, time_now;
1057 unsigned long temp_txm0;
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001058 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenkba56f622004-02-06 23:19:44 +00001059
1060 ef_ptr = (struct enet_frame *) ptr;
1061
1062 /*-----------------------------------------------------------------------+
1063 * Copy in our address into the frame.
1064 *-----------------------------------------------------------------------*/
1065 (void) memcpy (ef_ptr->source_addr, dev->enetaddr, ENET_ADDR_LENGTH);
1066
1067 /*-----------------------------------------------------------------------+
1068 * If frame is too long or too short, modify length.
1069 *-----------------------------------------------------------------------*/
1070 /* TBS: where does the fragment go???? */
1071 if (len > ENET_MAX_MTU)
1072 len = ENET_MAX_MTU;
1073
1074 /* memcpy ((void *) &tx_buff[tx_slot], (const void *) ptr, len); */
1075 memcpy ((void *) hw_p->txbuf_ptr, (const void *) ptr, len);
Matthias Fuchsba79fde2007-12-14 11:19:56 +01001076 flush_dcache_range((u32)hw_p->txbuf_ptr, (u32)hw_p->txbuf_ptr + len);
wdenkba56f622004-02-06 23:19:44 +00001077
1078 /*-----------------------------------------------------------------------+
1079 * set TX Buffer busy, and send it
1080 *-----------------------------------------------------------------------*/
1081 hw_p->tx[hw_p->tx_slot].ctrl = (MAL_TX_CTRL_LAST |
1082 EMAC_TX_CTRL_GFCS | EMAC_TX_CTRL_GP) &
1083 ~(EMAC_TX_CTRL_ISA | EMAC_TX_CTRL_RSA);
1084 if ((NUM_TX_BUFF - 1) == hw_p->tx_slot)
1085 hw_p->tx[hw_p->tx_slot].ctrl |= MAL_TX_CTRL_WRAP;
1086
1087 hw_p->tx[hw_p->tx_slot].data_len = (short) len;
1088 hw_p->tx[hw_p->tx_slot].ctrl |= MAL_TX_CTRL_READY;
1089
1090 __asm__ volatile ("eieio");
1091
Stefan Roese2d834762007-10-23 14:03:17 +02001092 out_be32((void *)EMAC_TXM0 + hw_p->hw_addr,
1093 in_be32((void *)EMAC_TXM0 + hw_p->hw_addr) | EMAC_TXM0_GNP0);
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001094#ifdef INFO_4XX_ENET
wdenkba56f622004-02-06 23:19:44 +00001095 hw_p->stats.pkts_tx++;
1096#endif
1097
1098 /*-----------------------------------------------------------------------+
1099 * poll unitl the packet is sent and then make sure it is OK
1100 *-----------------------------------------------------------------------*/
1101 time_start = get_timer (0);
1102 while (1) {
Stefan Roese2d834762007-10-23 14:03:17 +02001103 temp_txm0 = in_be32((void *)EMAC_TXM0 + hw_p->hw_addr);
wdenkba56f622004-02-06 23:19:44 +00001104 /* loop until either TINT turns on or 3 seconds elapse */
1105 if ((temp_txm0 & EMAC_TXM0_GNP0) != 0) {
1106 /* transmit is done, so now check for errors
1107 * If there is an error, an interrupt should
1108 * happen when we return
1109 */
1110 time_now = get_timer (0);
1111 if ((time_now - time_start) > 3000) {
1112 return (-1);
1113 }
1114 } else {
1115 return (len);
1116 }
1117 }
1118}
1119
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001120
Stefan Roesedbbd1252007-10-05 17:10:59 +02001121#if defined (CONFIG_440) || defined(CONFIG_405EX)
wdenkba56f622004-02-06 23:19:44 +00001122
Marian Balakowicz6c5879f2006-06-30 16:30:46 +02001123#if defined(CONFIG_440SP) || defined(CONFIG_440SPE)
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001124/*
1125 * Hack: On 440SP all enet irq sources are located on UIC1
1126 * Needs some cleanup. --sr
1127 */
1128#define UIC0MSR uic1msr
1129#define UIC0SR uic1sr
1130#else
1131#define UIC0MSR uic0msr
1132#define UIC0SR uic0sr
1133#endif
1134
Stefan Roesedbbd1252007-10-05 17:10:59 +02001135#if defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
1136 defined(CONFIG_405EX)
Stefan Roese887e2ec2006-09-07 11:51:23 +02001137#define UICMSR_ETHX uic0msr
1138#define UICSR_ETHX uic0sr
1139#else
1140#define UICMSR_ETHX uic1msr
1141#define UICSR_ETHX uic1sr
1142#endif
1143
wdenkba56f622004-02-06 23:19:44 +00001144int enetInt (struct eth_device *dev)
1145{
1146 int serviced;
1147 int rc = -1; /* default to not us */
1148 unsigned long mal_isr;
1149 unsigned long emac_isr = 0;
1150 unsigned long mal_rx_eob;
1151 unsigned long my_uic0msr, my_uic1msr;
Stefan Roese887e2ec2006-09-07 11:51:23 +02001152 unsigned long my_uicmsr_ethx;
wdenkba56f622004-02-06 23:19:44 +00001153
Stefan Roese846b0dd2005-08-08 12:42:22 +02001154#if defined(CONFIG_440GX)
wdenkba56f622004-02-06 23:19:44 +00001155 unsigned long my_uic2msr;
1156#endif
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001157 EMAC_4XX_HW_PST hw_p;
wdenkba56f622004-02-06 23:19:44 +00001158
1159 /*
1160 * Because the mal is generic, we need to get the current
1161 * eth device
1162 */
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001163#if defined(CONFIG_NET_MULTI)
1164 dev = eth_get_dev();
1165#else
1166 dev = emac0_dev;
1167#endif
wdenkba56f622004-02-06 23:19:44 +00001168
1169 hw_p = dev->priv;
1170
wdenkba56f622004-02-06 23:19:44 +00001171 /* enter loop that stays in interrupt code until nothing to service */
1172 do {
1173 serviced = 0;
1174
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001175 my_uic0msr = mfdcr (UIC0MSR);
wdenkba56f622004-02-06 23:19:44 +00001176 my_uic1msr = mfdcr (uic1msr);
Stefan Roese846b0dd2005-08-08 12:42:22 +02001177#if defined(CONFIG_440GX)
wdenkba56f622004-02-06 23:19:44 +00001178 my_uic2msr = mfdcr (uic2msr);
1179#endif
Stefan Roese887e2ec2006-09-07 11:51:23 +02001180 my_uicmsr_ethx = mfdcr (UICMSR_ETHX);
1181
wdenkba56f622004-02-06 23:19:44 +00001182 if (!(my_uic0msr & (UIC_MRE | UIC_MTE))
Stefan Roese887e2ec2006-09-07 11:51:23 +02001183 && !(my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))
1184 && !(my_uicmsr_ethx & (UIC_ETH0 | UIC_ETH1))) {
wdenkba56f622004-02-06 23:19:44 +00001185 /* not for us */
1186 return (rc);
1187 }
Stefan Roese846b0dd2005-08-08 12:42:22 +02001188#if defined (CONFIG_440GX)
wdenkba56f622004-02-06 23:19:44 +00001189 if (!(my_uic0msr & (UIC_MRE | UIC_MTE))
1190 && !(my_uic2msr & (UIC_ETH2 | UIC_ETH3))) {
1191 /* not for us */
1192 return (rc);
1193 }
1194#endif
1195 /* get and clear controller status interrupts */
1196 /* look at Mal and EMAC interrupts */
1197 if ((my_uic0msr & (UIC_MRE | UIC_MTE))
1198 || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
1199 /* we have a MAL interrupt */
1200 mal_isr = mfdcr (malesr);
1201 /* look for mal error */
1202 if (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE)) {
Stefan Roese887e2ec2006-09-07 11:51:23 +02001203 mal_err (dev, mal_isr, my_uic1msr, MAL_UIC_DEF, MAL_UIC_ERR);
wdenkba56f622004-02-06 23:19:44 +00001204 serviced = 1;
1205 rc = 0;
1206 }
1207 }
1208
1209 /* port by port dispatch of emac interrupts */
1210 if (hw_p->devnum == 0) {
Stefan Roese887e2ec2006-09-07 11:51:23 +02001211 if (UIC_ETH0 & my_uicmsr_ethx) { /* look for EMAC errors */
Stefan Roese2d834762007-10-23 14:03:17 +02001212 emac_isr = in_be32((void *)EMAC_ISR + hw_p->hw_addr);
wdenkba56f622004-02-06 23:19:44 +00001213 if ((hw_p->emac_ier & emac_isr) != 0) {
1214 emac_err (dev, emac_isr);
1215 serviced = 1;
1216 rc = 0;
1217 }
1218 }
1219 if ((hw_p->emac_ier & emac_isr)
1220 || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001221 mtdcr (UIC0SR, UIC_MRE | UIC_MTE); /* Clear */
Stefan Roese887e2ec2006-09-07 11:51:23 +02001222 mtdcr (uic1sr, UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
1223 mtdcr (UICSR_ETHX, UIC_ETH0); /* Clear */
wdenkba56f622004-02-06 23:19:44 +00001224 return (rc); /* we had errors so get out */
1225 }
1226 }
1227
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001228#if !defined(CONFIG_440SP)
wdenkba56f622004-02-06 23:19:44 +00001229 if (hw_p->devnum == 1) {
Stefan Roese887e2ec2006-09-07 11:51:23 +02001230 if (UIC_ETH1 & my_uicmsr_ethx) { /* look for EMAC errors */
Stefan Roese2d834762007-10-23 14:03:17 +02001231 emac_isr = in_be32((void *)EMAC_ISR + hw_p->hw_addr);
wdenkba56f622004-02-06 23:19:44 +00001232 if ((hw_p->emac_ier & emac_isr) != 0) {
1233 emac_err (dev, emac_isr);
1234 serviced = 1;
1235 rc = 0;
1236 }
1237 }
1238 if ((hw_p->emac_ier & emac_isr)
1239 || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001240 mtdcr (UIC0SR, UIC_MRE | UIC_MTE); /* Clear */
Stefan Roese887e2ec2006-09-07 11:51:23 +02001241 mtdcr (uic1sr, UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
1242 mtdcr (UICSR_ETHX, UIC_ETH1); /* Clear */
wdenkba56f622004-02-06 23:19:44 +00001243 return (rc); /* we had errors so get out */
1244 }
1245 }
Stefan Roese846b0dd2005-08-08 12:42:22 +02001246#if defined (CONFIG_440GX)
wdenkba56f622004-02-06 23:19:44 +00001247 if (hw_p->devnum == 2) {
1248 if (UIC_ETH2 & my_uic2msr) { /* look for EMAC errors */
Stefan Roese2d834762007-10-23 14:03:17 +02001249 emac_isr = in_be32((void *)EMAC_ISR + hw_p->hw_addr);
wdenkba56f622004-02-06 23:19:44 +00001250 if ((hw_p->emac_ier & emac_isr) != 0) {
1251 emac_err (dev, emac_isr);
1252 serviced = 1;
1253 rc = 0;
1254 }
1255 }
1256 if ((hw_p->emac_ier & emac_isr)
1257 || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001258 mtdcr (UIC0SR, UIC_MRE | UIC_MTE); /* Clear */
wdenkba56f622004-02-06 23:19:44 +00001259 mtdcr (uic1sr, UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
1260 mtdcr (uic2sr, UIC_ETH2);
1261 return (rc); /* we had errors so get out */
1262 }
1263 }
1264
1265 if (hw_p->devnum == 3) {
1266 if (UIC_ETH3 & my_uic2msr) { /* look for EMAC errors */
Stefan Roese2d834762007-10-23 14:03:17 +02001267 emac_isr = in_be32((void *)EMAC_ISR + hw_p->hw_addr);
wdenkba56f622004-02-06 23:19:44 +00001268 if ((hw_p->emac_ier & emac_isr) != 0) {
1269 emac_err (dev, emac_isr);
1270 serviced = 1;
1271 rc = 0;
1272 }
1273 }
1274 if ((hw_p->emac_ier & emac_isr)
1275 || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001276 mtdcr (UIC0SR, UIC_MRE | UIC_MTE); /* Clear */
wdenkba56f622004-02-06 23:19:44 +00001277 mtdcr (uic1sr, UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
1278 mtdcr (uic2sr, UIC_ETH3);
1279 return (rc); /* we had errors so get out */
1280 }
1281 }
Stefan Roese846b0dd2005-08-08 12:42:22 +02001282#endif /* CONFIG_440GX */
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001283#endif /* !CONFIG_440SP */
1284
wdenkba56f622004-02-06 23:19:44 +00001285 /* handle MAX TX EOB interrupt from a tx */
1286 if (my_uic0msr & UIC_MTE) {
1287 mal_rx_eob = mfdcr (maltxeobisr);
1288 mtdcr (maltxeobisr, mal_rx_eob);
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001289 mtdcr (UIC0SR, UIC_MTE);
wdenkba56f622004-02-06 23:19:44 +00001290 }
1291 /* handle MAL RX EOB interupt from a receive */
wdenkfc1cfcd2004-04-25 15:41:35 +00001292 /* check for EOB on valid channels */
wdenkba56f622004-02-06 23:19:44 +00001293 if (my_uic0msr & UIC_MRE) {
1294 mal_rx_eob = mfdcr (malrxeobisr);
Wolfgang Denk265817c2005-09-25 00:53:22 +02001295 if ((mal_rx_eob & (0x80000000 >> hw_p->devnum)) != 0) { /* call emac routine for channel x */
wdenkba56f622004-02-06 23:19:44 +00001296 /* clear EOB
1297 mtdcr(malrxeobisr, mal_rx_eob); */
1298 enet_rcv (dev, emac_isr);
1299 /* indicate that we serviced an interrupt */
1300 serviced = 1;
1301 rc = 0;
1302 }
1303 }
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001304
1305 mtdcr (UIC0SR, UIC_MRE); /* Clear */
wdenkba56f622004-02-06 23:19:44 +00001306 mtdcr (uic1sr, UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
1307 switch (hw_p->devnum) {
1308 case 0:
Stefan Roese887e2ec2006-09-07 11:51:23 +02001309 mtdcr (UICSR_ETHX, UIC_ETH0);
wdenkba56f622004-02-06 23:19:44 +00001310 break;
1311 case 1:
Stefan Roese887e2ec2006-09-07 11:51:23 +02001312 mtdcr (UICSR_ETHX, UIC_ETH1);
wdenkba56f622004-02-06 23:19:44 +00001313 break;
Stefan Roese846b0dd2005-08-08 12:42:22 +02001314#if defined (CONFIG_440GX)
wdenkba56f622004-02-06 23:19:44 +00001315 case 2:
1316 mtdcr (uic2sr, UIC_ETH2);
1317 break;
1318 case 3:
1319 mtdcr (uic2sr, UIC_ETH3);
1320 break;
Stefan Roese846b0dd2005-08-08 12:42:22 +02001321#endif /* CONFIG_440GX */
wdenkba56f622004-02-06 23:19:44 +00001322 default:
1323 break;
1324 }
1325 } while (serviced);
1326
1327 return (rc);
1328}
1329
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001330#else /* CONFIG_440 */
1331
1332int enetInt (struct eth_device *dev)
1333{
1334 int serviced;
1335 int rc = -1; /* default to not us */
1336 unsigned long mal_isr;
1337 unsigned long emac_isr = 0;
1338 unsigned long mal_rx_eob;
1339 unsigned long my_uicmsr;
1340
1341 EMAC_4XX_HW_PST hw_p;
1342
1343 /*
1344 * Because the mal is generic, we need to get the current
1345 * eth device
1346 */
1347#if defined(CONFIG_NET_MULTI)
1348 dev = eth_get_dev();
1349#else
1350 dev = emac0_dev;
1351#endif
1352
1353 hw_p = dev->priv;
1354
1355 /* enter loop that stays in interrupt code until nothing to service */
1356 do {
1357 serviced = 0;
1358
1359 my_uicmsr = mfdcr (uicmsr);
1360
1361 if ((my_uicmsr & (MAL_UIC_DEF | EMAC_UIC_DEF)) == 0) { /* not for us */
1362 return (rc);
1363 }
1364 /* get and clear controller status interrupts */
1365 /* look at Mal and EMAC interrupts */
1366 if ((MAL_UIC_DEF & my_uicmsr) != 0) { /* we have a MAL interrupt */
1367 mal_isr = mfdcr (malesr);
1368 /* look for mal error */
1369 if ((my_uicmsr & MAL_UIC_ERR) != 0) {
1370 mal_err (dev, mal_isr, my_uicmsr, MAL_UIC_DEF, MAL_UIC_ERR);
1371 serviced = 1;
1372 rc = 0;
1373 }
1374 }
1375
1376 /* port by port dispatch of emac interrupts */
1377
1378 if ((SEL_UIC_DEF(hw_p->devnum) & my_uicmsr) != 0) { /* look for EMAC errors */
Stefan Roese2d834762007-10-23 14:03:17 +02001379 emac_isr = in_be32((void *)EMAC_ISR + hw_p->hw_addr);
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001380 if ((hw_p->emac_ier & emac_isr) != 0) {
1381 emac_err (dev, emac_isr);
1382 serviced = 1;
1383 rc = 0;
1384 }
1385 }
1386 if (((hw_p->emac_ier & emac_isr) != 0) || ((MAL_UIC_ERR & my_uicmsr) != 0)) {
1387 mtdcr (uicsr, MAL_UIC_DEF | SEL_UIC_DEF(hw_p->devnum)); /* Clear */
1388 return (rc); /* we had errors so get out */
1389 }
1390
1391 /* handle MAX TX EOB interrupt from a tx */
1392 if (my_uicmsr & UIC_MAL_TXEOB) {
1393 mal_rx_eob = mfdcr (maltxeobisr);
1394 mtdcr (maltxeobisr, mal_rx_eob);
1395 mtdcr (uicsr, UIC_MAL_TXEOB);
1396 }
1397 /* handle MAL RX EOB interupt from a receive */
1398 /* check for EOB on valid channels */
1399 if (my_uicmsr & UIC_MAL_RXEOB)
1400 {
1401 mal_rx_eob = mfdcr (malrxeobisr);
Wolfgang Denk265817c2005-09-25 00:53:22 +02001402 if ((mal_rx_eob & (0x80000000 >> hw_p->devnum)) != 0) { /* call emac routine for channel x */
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001403 /* clear EOB
1404 mtdcr(malrxeobisr, mal_rx_eob); */
1405 enet_rcv (dev, emac_isr);
1406 /* indicate that we serviced an interrupt */
1407 serviced = 1;
1408 rc = 0;
1409 }
1410 }
1411 mtdcr (uicsr, MAL_UIC_DEF|EMAC_UIC_DEF|EMAC_UIC_DEF1); /* Clear */
Stefan Roesee01bd212007-03-21 13:38:59 +01001412#if defined(CONFIG_405EZ)
1413 mtsdr (sdricintstat, SDR_ICRX_STAT | SDR_ICTX0_STAT | SDR_ICTX1_STAT);
1414#endif /* defined(CONFIG_405EZ) */
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001415 }
1416 while (serviced);
1417
1418 return (rc);
1419}
1420
1421#endif /* CONFIG_440 */
1422
wdenkba56f622004-02-06 23:19:44 +00001423/*-----------------------------------------------------------------------------+
1424 * MAL Error Routine
1425 *-----------------------------------------------------------------------------*/
1426static void mal_err (struct eth_device *dev, unsigned long isr,
1427 unsigned long uic, unsigned long maldef,
1428 unsigned long mal_errr)
1429{
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001430 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenkba56f622004-02-06 23:19:44 +00001431
1432 mtdcr (malesr, isr); /* clear interrupt */
1433
1434 /* clear DE interrupt */
1435 mtdcr (maltxdeir, 0xC0000000);
1436 mtdcr (malrxdeir, 0x80000000);
1437
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001438#ifdef INFO_4XX_ENET
Wolfgang Denk265817c2005-09-25 00:53:22 +02001439 printf ("\nMAL error occured.... ISR = %lx UIC = = %lx MAL_DEF = %lx MAL_ERR= %lx \n", isr, uic, maldef, mal_errr);
wdenkba56f622004-02-06 23:19:44 +00001440#endif
1441
1442 eth_init (hw_p->bis); /* start again... */
1443}
1444
1445/*-----------------------------------------------------------------------------+
1446 * EMAC Error Routine
1447 *-----------------------------------------------------------------------------*/
1448static void emac_err (struct eth_device *dev, unsigned long isr)
1449{
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001450 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenkba56f622004-02-06 23:19:44 +00001451
1452 printf ("EMAC%d error occured.... ISR = %lx\n", hw_p->devnum, isr);
Stefan Roese2d834762007-10-23 14:03:17 +02001453 out_be32((void *)EMAC_ISR + hw_p->hw_addr, isr);
wdenkba56f622004-02-06 23:19:44 +00001454}
1455
1456/*-----------------------------------------------------------------------------+
1457 * enet_rcv() handles the ethernet receive data
1458 *-----------------------------------------------------------------------------*/
1459static void enet_rcv (struct eth_device *dev, unsigned long malisr)
1460{
1461 struct enet_frame *ef_ptr;
1462 unsigned long data_len;
1463 unsigned long rx_eob_isr;
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001464 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenkba56f622004-02-06 23:19:44 +00001465
1466 int handled = 0;
1467 int i;
1468 int loop_count = 0;
1469
1470 rx_eob_isr = mfdcr (malrxeobisr);
1471 if ((0x80000000 >> hw_p->devnum) & rx_eob_isr) {
1472 /* clear EOB */
1473 mtdcr (malrxeobisr, rx_eob_isr);
1474
1475 /* EMAC RX done */
1476 while (1) { /* do all */
1477 i = hw_p->rx_slot;
1478
1479 if ((MAL_RX_CTRL_EMPTY & hw_p->rx[i].ctrl)
1480 || (loop_count >= NUM_RX_BUFF))
1481 break;
Stefan Roesea2e1c702007-07-12 16:32:08 +02001482
wdenkba56f622004-02-06 23:19:44 +00001483 loop_count++;
wdenkba56f622004-02-06 23:19:44 +00001484 handled++;
1485 data_len = (unsigned long) hw_p->rx[i].data_len; /* Get len */
1486 if (data_len) {
1487 if (data_len > ENET_MAX_MTU) /* Check len */
1488 data_len = 0;
1489 else {
1490 if (EMAC_RX_ERRORS & hw_p->rx[i].ctrl) { /* Check Errors */
1491 data_len = 0;
1492 hw_p->stats.rx_err_log[hw_p->
1493 rx_err_index]
1494 = hw_p->rx[i].ctrl;
1495 hw_p->rx_err_index++;
1496 if (hw_p->rx_err_index ==
1497 MAX_ERR_LOG)
1498 hw_p->rx_err_index =
1499 0;
wdenkfc1cfcd2004-04-25 15:41:35 +00001500 } /* emac_erros */
wdenkba56f622004-02-06 23:19:44 +00001501 } /* data_len < max mtu */
wdenkfc1cfcd2004-04-25 15:41:35 +00001502 } /* if data_len */
wdenkba56f622004-02-06 23:19:44 +00001503 if (!data_len) { /* no data */
1504 hw_p->rx[i].ctrl |= MAL_RX_CTRL_EMPTY; /* Free Recv Buffer */
1505
1506 hw_p->stats.data_len_err++; /* Error at Rx */
1507 }
1508
1509 /* !data_len */
1510 /* AS.HARNOIS */
1511 /* Check if user has already eaten buffer */
1512 /* if not => ERROR */
1513 else if (hw_p->rx_ready[hw_p->rx_i_index] != -1) {
1514 if (hw_p->is_receiving)
1515 printf ("ERROR : Receive buffers are full!\n");
1516 break;
1517 } else {
1518 hw_p->stats.rx_frames++;
1519 hw_p->stats.rx += data_len;
1520 ef_ptr = (struct enet_frame *) hw_p->rx[i].
1521 data_ptr;
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001522#ifdef INFO_4XX_ENET
wdenkba56f622004-02-06 23:19:44 +00001523 hw_p->stats.pkts_rx++;
1524#endif
1525 /* AS.HARNOIS
1526 * use ring buffer
1527 */
1528 hw_p->rx_ready[hw_p->rx_i_index] = i;
1529 hw_p->rx_i_index++;
1530 if (NUM_RX_BUFF == hw_p->rx_i_index)
1531 hw_p->rx_i_index = 0;
1532
Stefan Roesea2e1c702007-07-12 16:32:08 +02001533 hw_p->rx_slot++;
1534 if (NUM_RX_BUFF == hw_p->rx_slot)
1535 hw_p->rx_slot = 0;
1536
wdenkba56f622004-02-06 23:19:44 +00001537 /* AS.HARNOIS
1538 * free receive buffer only when
1539 * buffer has been handled (eth_rx)
1540 rx[i].ctrl |= MAL_RX_CTRL_EMPTY;
1541 */
1542 } /* if data_len */
1543 } /* while */
1544 } /* if EMACK_RXCHL */
1545}
1546
1547
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001548static int ppc_4xx_eth_rx (struct eth_device *dev)
wdenkba56f622004-02-06 23:19:44 +00001549{
1550 int length;
1551 int user_index;
1552 unsigned long msr;
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001553 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenkba56f622004-02-06 23:19:44 +00001554
Wolfgang Denk265817c2005-09-25 00:53:22 +02001555 hw_p->is_receiving = 1; /* tell driver */
wdenkba56f622004-02-06 23:19:44 +00001556
1557 for (;;) {
1558 /* AS.HARNOIS
1559 * use ring buffer and
1560 * get index from rx buffer desciptor queue
1561 */
1562 user_index = hw_p->rx_ready[hw_p->rx_u_index];
1563 if (user_index == -1) {
1564 length = -1;
1565 break; /* nothing received - leave for() loop */
1566 }
1567
1568 msr = mfmsr ();
1569 mtmsr (msr & ~(MSR_EE));
1570
1571 length = hw_p->rx[user_index].data_len;
1572
1573 /* Pass the packet up to the protocol layers. */
Wolfgang Denk265817c2005-09-25 00:53:22 +02001574 /* NetReceive(NetRxPackets[rxIdx], length - 4); */
1575 /* NetReceive(NetRxPackets[i], length); */
Stefan Roeseff768cb2007-10-31 18:01:24 +01001576 invalidate_dcache_range((u32)hw_p->rx[user_index].data_ptr,
1577 (u32)hw_p->rx[user_index].data_ptr +
Matthias Fuchsba79fde2007-12-14 11:19:56 +01001578 length - 4);
wdenkba56f622004-02-06 23:19:44 +00001579 NetReceive (NetRxPackets[user_index], length - 4);
1580 /* Free Recv Buffer */
1581 hw_p->rx[user_index].ctrl |= MAL_RX_CTRL_EMPTY;
1582 /* Free rx buffer descriptor queue */
1583 hw_p->rx_ready[hw_p->rx_u_index] = -1;
1584 hw_p->rx_u_index++;
1585 if (NUM_RX_BUFF == hw_p->rx_u_index)
1586 hw_p->rx_u_index = 0;
1587
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001588#ifdef INFO_4XX_ENET
wdenkba56f622004-02-06 23:19:44 +00001589 hw_p->stats.pkts_handled++;
1590#endif
1591
1592 mtmsr (msr); /* Enable IRQ's */
1593 }
1594
Wolfgang Denk265817c2005-09-25 00:53:22 +02001595 hw_p->is_receiving = 0; /* tell driver */
wdenkba56f622004-02-06 23:19:44 +00001596
1597 return length;
1598}
1599
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001600int ppc_4xx_eth_initialize (bd_t * bis)
wdenkba56f622004-02-06 23:19:44 +00001601{
1602 static int virgin = 0;
wdenkba56f622004-02-06 23:19:44 +00001603 struct eth_device *dev;
1604 int eth_num = 0;
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001605 EMAC_4XX_HW_PST hw = NULL;
Stefan Roese5fb692c2007-01-18 10:25:34 +01001606 u8 ethaddr[4 + CONFIG_EMAC_NR_START][6];
1607 u32 hw_addr[4];
wdenkba56f622004-02-06 23:19:44 +00001608
Stefan Roese846b0dd2005-08-08 12:42:22 +02001609#if defined(CONFIG_440GX)
Stefan Roesec157d8e2005-08-01 16:41:48 +02001610 unsigned long pfc1;
1611
wdenkba56f622004-02-06 23:19:44 +00001612 mfsdr (sdr_pfc1, pfc1);
1613 pfc1 &= ~(0x01e00000);
1614 pfc1 |= 0x01200000;
1615 mtsdr (sdr_pfc1, pfc1);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001616#endif
Stefan Roese5fb692c2007-01-18 10:25:34 +01001617
1618 /* first clear all mac-addresses */
1619 for (eth_num = 0; eth_num < LAST_EMAC_NUM; eth_num++)
1620 memcpy(ethaddr[eth_num], "\0\0\0\0\0\0", 6);
1621
1622 for (eth_num = 0; eth_num < LAST_EMAC_NUM; eth_num++) {
1623 switch (eth_num) {
1624 default: /* fall through */
1625 case 0:
1626 memcpy(ethaddr[eth_num + CONFIG_EMAC_NR_START],
1627 bis->bi_enetaddr, 6);
1628 hw_addr[eth_num] = 0x0;
1629 break;
1630#ifdef CONFIG_HAS_ETH1
1631 case 1:
1632 memcpy(ethaddr[eth_num + CONFIG_EMAC_NR_START],
1633 bis->bi_enet1addr, 6);
1634 hw_addr[eth_num] = 0x100;
1635 break;
1636#endif
1637#ifdef CONFIG_HAS_ETH2
1638 case 2:
1639 memcpy(ethaddr[eth_num + CONFIG_EMAC_NR_START],
1640 bis->bi_enet2addr, 6);
1641 hw_addr[eth_num] = 0x400;
1642 break;
1643#endif
1644#ifdef CONFIG_HAS_ETH3
1645 case 3:
1646 memcpy(ethaddr[eth_num + CONFIG_EMAC_NR_START],
1647 bis->bi_enet3addr, 6);
1648 hw_addr[eth_num] = 0x600;
1649 break;
1650#endif
1651 }
1652 }
1653
wdenk3c74e322004-02-22 23:46:08 +00001654 /* set phy num and mode */
1655 bis->bi_phynum[0] = CONFIG_PHY_ADDR;
Marian Balakowicz6c5879f2006-06-30 16:30:46 +02001656 bis->bi_phymode[0] = 0;
1657
Stefan Roesec157d8e2005-08-01 16:41:48 +02001658#if defined(CONFIG_PHY1_ADDR)
wdenk3c74e322004-02-22 23:46:08 +00001659 bis->bi_phynum[1] = CONFIG_PHY1_ADDR;
Marian Balakowicz6c5879f2006-06-30 16:30:46 +02001660 bis->bi_phymode[1] = 0;
Stefan Roesec157d8e2005-08-01 16:41:48 +02001661#endif
Stefan Roese846b0dd2005-08-08 12:42:22 +02001662#if defined(CONFIG_440GX)
wdenk3c74e322004-02-22 23:46:08 +00001663 bis->bi_phynum[2] = CONFIG_PHY2_ADDR;
1664 bis->bi_phynum[3] = CONFIG_PHY3_ADDR;
wdenk3c74e322004-02-22 23:46:08 +00001665 bis->bi_phymode[2] = 2;
1666 bis->bi_phymode[3] = 2;
Stefan Roesedbbd1252007-10-05 17:10:59 +02001667#endif
wdenkba56f622004-02-06 23:19:44 +00001668
Stefan Roesedbbd1252007-10-05 17:10:59 +02001669#if defined(CONFIG_440GX) || \
1670 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
1671 defined(CONFIG_405EX)
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001672 ppc_4xx_eth_setup_bridge(0, bis);
wdenka06752e2004-09-29 22:43:59 +00001673#endif
1674
Stefan Roese1e25f952005-10-20 16:34:28 +02001675 for (eth_num = 0; eth_num < LAST_EMAC_NUM; eth_num++) {
Stefan Roese5fb692c2007-01-18 10:25:34 +01001676 /*
1677 * See if we can actually bring up the interface,
1678 * otherwise, skip it
1679 */
1680 if (memcmp (ethaddr[eth_num], "\0\0\0\0\0\0", 6) == 0) {
1681 bis->bi_phymode[eth_num] = BI_PHYMODE_NONE;
1682 continue;
wdenkba56f622004-02-06 23:19:44 +00001683 }
1684
1685 /* Allocate device structure */
1686 dev = (struct eth_device *) malloc (sizeof (*dev));
1687 if (dev == NULL) {
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001688 printf ("ppc_4xx_eth_initialize: "
wdenk3f85ce22004-02-23 16:11:30 +00001689 "Cannot allocate eth_device %d\n", eth_num);
wdenkba56f622004-02-06 23:19:44 +00001690 return (-1);
1691 }
wdenkb2532ef2005-06-20 10:17:34 +00001692 memset(dev, 0, sizeof(*dev));
wdenkba56f622004-02-06 23:19:44 +00001693
1694 /* Allocate our private use data */
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001695 hw = (EMAC_4XX_HW_PST) malloc (sizeof (*hw));
wdenkba56f622004-02-06 23:19:44 +00001696 if (hw == NULL) {
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001697 printf ("ppc_4xx_eth_initialize: "
wdenk3f85ce22004-02-23 16:11:30 +00001698 "Cannot allocate private hw data for eth_device %d",
wdenkba56f622004-02-06 23:19:44 +00001699 eth_num);
1700 free (dev);
1701 return (-1);
1702 }
wdenkb2532ef2005-06-20 10:17:34 +00001703 memset(hw, 0, sizeof(*hw));
wdenkba56f622004-02-06 23:19:44 +00001704
Stefan Roese5fb692c2007-01-18 10:25:34 +01001705 hw->hw_addr = hw_addr[eth_num];
1706 memcpy (dev->enetaddr, ethaddr[eth_num], 6);
wdenkba56f622004-02-06 23:19:44 +00001707 hw->devnum = eth_num;
Stefan Roesec157d8e2005-08-01 16:41:48 +02001708 hw->print_speed = 1;
wdenkba56f622004-02-06 23:19:44 +00001709
Stefan Roese5fb692c2007-01-18 10:25:34 +01001710 sprintf (dev->name, "ppc_4xx_eth%d", eth_num - CONFIG_EMAC_NR_START);
wdenkba56f622004-02-06 23:19:44 +00001711 dev->priv = (void *) hw;
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001712 dev->init = ppc_4xx_eth_init;
1713 dev->halt = ppc_4xx_eth_halt;
1714 dev->send = ppc_4xx_eth_send;
1715 dev->recv = ppc_4xx_eth_rx;
wdenkba56f622004-02-06 23:19:44 +00001716
1717 if (0 == virgin) {
1718 /* set the MAL IER ??? names may change with new spec ??? */
Stefan Roesedbbd1252007-10-05 17:10:59 +02001719#if defined(CONFIG_440SPE) || \
1720 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
1721 defined(CONFIG_405EX)
Marian Balakowicz6c5879f2006-06-30 16:30:46 +02001722 mal_ier =
1723 MAL_IER_PT | MAL_IER_PRE | MAL_IER_PWE |
1724 MAL_IER_DE | MAL_IER_OTE | MAL_IER_OE | MAL_IER_PE ;
1725#else
wdenkba56f622004-02-06 23:19:44 +00001726 mal_ier =
1727 MAL_IER_DE | MAL_IER_NE | MAL_IER_TE |
1728 MAL_IER_OPBE | MAL_IER_PLBE;
Marian Balakowicz6c5879f2006-06-30 16:30:46 +02001729#endif
wdenkba56f622004-02-06 23:19:44 +00001730 mtdcr (malesr, 0xffffffff); /* clear pending interrupts */
1731 mtdcr (maltxdeir, 0xffffffff); /* clear pending interrupts */
1732 mtdcr (malrxdeir, 0xffffffff); /* clear pending interrupts */
1733 mtdcr (malier, mal_ier);
1734
1735 /* install MAL interrupt handler */
1736 irq_install_handler (VECNUM_MS,
1737 (interrupt_handler_t *) enetInt,
1738 dev);
1739 irq_install_handler (VECNUM_MTE,
1740 (interrupt_handler_t *) enetInt,
1741 dev);
1742 irq_install_handler (VECNUM_MRE,
1743 (interrupt_handler_t *) enetInt,
1744 dev);
1745 irq_install_handler (VECNUM_TXDE,
1746 (interrupt_handler_t *) enetInt,
1747 dev);
1748 irq_install_handler (VECNUM_RXDE,
1749 (interrupt_handler_t *) enetInt,
1750 dev);
1751 virgin = 1;
1752 }
1753
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001754#if defined(CONFIG_NET_MULTI)
wdenkba56f622004-02-06 23:19:44 +00001755 eth_register (dev);
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001756#else
1757 emac0_dev = dev;
1758#endif
Marian Balakowicz6c5879f2006-06-30 16:30:46 +02001759
1760#if defined(CONFIG_NET_MULTI)
Jon Loeliger3a1ed1e2007-07-09 18:57:22 -05001761#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Marian Balakowicz63ff0042005-10-28 22:30:33 +02001762 miiphy_register (dev->name,
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001763 emac4xx_miiphy_read, emac4xx_miiphy_write);
Marian Balakowicz63ff0042005-10-28 22:30:33 +02001764#endif
Marian Balakowicz6c5879f2006-06-30 16:30:46 +02001765#endif
wdenkba56f622004-02-06 23:19:44 +00001766 } /* end for each supported device */
Stefan Roese802b7692008-01-08 18:39:30 +01001767
1768 return 0;
wdenkba56f622004-02-06 23:19:44 +00001769}
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001770
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001771#if !defined(CONFIG_NET_MULTI)
1772void eth_halt (void) {
1773 if (emac0_dev) {
1774 ppc_4xx_eth_halt(emac0_dev);
1775 free(emac0_dev);
1776 emac0_dev = NULL;
1777 }
1778}
1779
1780int eth_init (bd_t *bis)
1781{
1782 ppc_4xx_eth_initialize(bis);
Stefan Roese4f92ac32005-10-10 17:43:58 +02001783 if (emac0_dev) {
1784 return ppc_4xx_eth_init(emac0_dev, bis);
1785 } else {
1786 printf("ERROR: ethaddr not set!\n");
1787 return -1;
1788 }
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001789}
1790
1791int eth_send(volatile void *packet, int length)
1792{
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001793 return (ppc_4xx_eth_send(emac0_dev, packet, length));
1794}
1795
1796int eth_rx(void)
1797{
1798 return (ppc_4xx_eth_rx(emac0_dev));
1799}
Marian Balakowicz63ff0042005-10-28 22:30:33 +02001800
1801int emac4xx_miiphy_initialize (bd_t * bis)
1802{
Jon Loeliger3a1ed1e2007-07-09 18:57:22 -05001803#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Marian Balakowicz63ff0042005-10-28 22:30:33 +02001804 miiphy_register ("ppc_4xx_eth0",
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001805 emac4xx_miiphy_read, emac4xx_miiphy_write);
Marian Balakowicz63ff0042005-10-28 22:30:33 +02001806#endif
1807
1808 return 0;
1809}
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001810#endif /* !defined(CONFIG_NET_MULTI) */
1811
Jon Loeliger3a1ed1e2007-07-09 18:57:22 -05001812#endif