blob: d7b16daf394344761617cfae4d2526ca0fbebb19 [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>
wdenkba56f622004-02-06 23:19:44 +000093
Stefan Roesed6c61aa2005-08-16 18:18:00 +020094/*
Wolfgang Denk0c8721a2005-09-23 11:05:55 +020095 * Only compile for platform with AMCC EMAC ethernet controller and
Stefan Roesed6c61aa2005-08-16 18:18:00 +020096 * network support enabled.
97 * Remark: CONFIG_405 describes Xilinx PPC405 FPGA without EMAC controller!
98 */
Jon Loeliger3a1ed1e2007-07-09 18:57:22 -050099#if defined(CONFIG_CMD_NET) && !defined(CONFIG_405) && !defined(CONFIG_IOP480)
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200100
Jon Loeliger3a1ed1e2007-07-09 18:57:22 -0500101#if !(defined(CONFIG_MII) || defined(CONFIG_CMD_MII))
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200102#error "CONFIG_MII has to be defined!"
103#endif
wdenkba56f622004-02-06 23:19:44 +0000104
Stefan Roese1e25f952005-10-20 16:34:28 +0200105#if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_NET_MULTI)
106#error "CONFIG_NET_MULTI has to be defined for NetConsole"
107#endif
108
Wolfgang Denk265817c2005-09-25 00:53:22 +0200109#define EMAC_RESET_TIMEOUT 1000 /* 1000 ms reset timeout */
Stefan Roese1338e6a2007-10-23 14:05:08 +0200110#define PHY_AUTONEGOTIATE_TIMEOUT 5000 /* 5000 ms autonegotiate timeout */
wdenkba56f622004-02-06 23:19:44 +0000111
wdenkba56f622004-02-06 23:19:44 +0000112/* Ethernet Transmit and Receive Buffers */
113/* AS.HARNOIS
114 * In the same way ENET_MAX_MTU and ENET_MAX_MTU_ALIGNED are set from
115 * PKTSIZE and PKTSIZE_ALIGN (include/net.h)
116 */
Wolfgang Denk265817c2005-09-25 00:53:22 +0200117#define ENET_MAX_MTU PKTSIZE
wdenkba56f622004-02-06 23:19:44 +0000118#define ENET_MAX_MTU_ALIGNED PKTSIZE_ALIGN
119
wdenkba56f622004-02-06 23:19:44 +0000120/*-----------------------------------------------------------------------------+
121 * Defines for MAL/EMAC interrupt conditions as reported in the UIC (Universal
122 * Interrupt Controller).
123 *-----------------------------------------------------------------------------*/
Stefan Roesed1631fe2008-06-26 13:40:57 +0200124#define ETH_IRQ_NUM(dev) (VECNUM_ETH0 + ((dev) * VECNUM_ETH1_OFFS))
125
126#if defined(CONFIG_HAS_ETH3)
127#if !defined(CONFIG_440GX)
128#define UIC_ETHx (UIC_MASK(ETH_IRQ_NUM(0)) || UIC_MASK(ETH_IRQ_NUM(1)) || \
129 UIC_MASK(ETH_IRQ_NUM(2)) || UIC_MASK(ETH_IRQ_NUM(3)))
130#else
131/* Unfortunately 440GX spreads EMAC interrupts on multiple UIC's */
132#define UIC_ETHx (UIC_MASK(ETH_IRQ_NUM(0)) || UIC_MASK(ETH_IRQ_NUM(1)))
133#define UIC_ETHxB (UIC_MASK(ETH_IRQ_NUM(2)) || UIC_MASK(ETH_IRQ_NUM(3)))
134#endif /* !defined(CONFIG_440GX) */
135#elif defined(CONFIG_HAS_ETH2)
136#define UIC_ETHx (UIC_MASK(ETH_IRQ_NUM(0)) || UIC_MASK(ETH_IRQ_NUM(1)) || \
137 UIC_MASK(ETH_IRQ_NUM(2)))
138#elif defined(CONFIG_HAS_ETH1)
139#define UIC_ETHx (UIC_MASK(ETH_IRQ_NUM(0)) || UIC_MASK(ETH_IRQ_NUM(1)))
140#else
141#define UIC_ETHx UIC_MASK(ETH_IRQ_NUM(0))
142#endif
143
144/*
145 * Define a default version for UIC_ETHxB for non 440GX so that we can
146 * use common code for all 4xx variants
147 */
148#if !defined(UIC_ETHxB)
149#define UIC_ETHxB 0
150#endif
151
152#define UIC_MAL_SERR UIC_MASK(VECNUM_MAL_SERR)
153#define UIC_MAL_TXDE UIC_MASK(VECNUM_MAL_TXDE)
154#define UIC_MAL_RXDE UIC_MASK(VECNUM_MAL_RXDE)
155#define UIC_MAL_TXEOB UIC_MASK(VECNUM_MAL_TXEOB)
156#define UIC_MAL_RXEOB UIC_MASK(VECNUM_MAL_RXEOB)
157
158#define MAL_UIC_ERR (UIC_MAL_SERR | UIC_MAL_TXDE | UIC_MAL_RXDE)
159#define MAL_UIC_DEF (UIC_MAL_RXEOB | MAL_UIC_ERR)
160
161/*
162 * We have 3 different interrupt types:
163 * - MAL interrupts indicating successful transfer
164 * - MAL error interrupts indicating MAL related errors
165 * - EMAC interrupts indicating EMAC related errors
166 *
167 * All those interrupts can be on different UIC's, but since
168 * now at least all interrupts from one type are on the same
169 * UIC. Only exception is 440GX where the EMAC interrupts are
170 * spread over two UIC's!
171 */
Stefan Roese5de85142008-06-26 17:36:39 +0200172#if defined(CONFIG_440GX)
173#define UIC_BASE_MAL UIC1_DCR_BASE
174#define UIC_BASE_MAL_ERR UIC2_DCR_BASE
175#define UIC_BASE_EMAC UIC2_DCR_BASE
176#define UIC_BASE_EMAC_B UIC3_DCR_BASE
177#else
Stefan Roesed1631fe2008-06-26 13:40:57 +0200178#define UIC_BASE_MAL (UIC0_DCR_BASE + (UIC_NR(VECNUM_MAL_TXEOB) * 0x10))
179#define UIC_BASE_MAL_ERR (UIC0_DCR_BASE + (UIC_NR(VECNUM_MAL_SERR) * 0x10))
180#define UIC_BASE_EMAC (UIC0_DCR_BASE + (UIC_NR(ETH_IRQ_NUM(0)) * 0x10))
Stefan Roesed1631fe2008-06-26 13:40:57 +0200181#define UIC_BASE_EMAC_B (UIC0_DCR_BASE + (UIC_NR(ETH_IRQ_NUM(0)) * 0x10))
182#endif
wdenkba56f622004-02-06 23:19:44 +0000183
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200184#undef INFO_4XX_ENET
wdenkba56f622004-02-06 23:19:44 +0000185
Wolfgang Denk265817c2005-09-25 00:53:22 +0200186#define BI_PHYMODE_NONE 0
187#define BI_PHYMODE_ZMII 1
wdenk3c74e322004-02-22 23:46:08 +0000188#define BI_PHYMODE_RGMII 2
Stefan Roese887e2ec2006-09-07 11:51:23 +0200189#define BI_PHYMODE_GMII 3
190#define BI_PHYMODE_RTBI 4
191#define BI_PHYMODE_TBI 5
Stefan Roesedbbd1252007-10-05 17:10:59 +0200192#if defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roese8ac41e32008-03-11 15:05:26 +0100193 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200194 defined(CONFIG_405EX)
Stefan Roese887e2ec2006-09-07 11:51:23 +0200195#define BI_PHYMODE_SMII 6
196#define BI_PHYMODE_MII 7
Stefan Roese8ac41e32008-03-11 15:05:26 +0100197#if defined(CONFIG_460EX) || defined(CONFIG_460GT)
198#define BI_PHYMODE_RMII 8
199#endif
Stefan Roese887e2ec2006-09-07 11:51:23 +0200200#endif
Victor Gallardo78d78232008-09-04 23:49:36 -0700201#define BI_PHYMODE_SGMII 9
wdenk3c74e322004-02-22 23:46:08 +0000202
Stefan Roese1941cce2007-10-05 17:35:10 +0200203#if defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200204 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roese8ac41e32008-03-11 15:05:26 +0100205 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200206 defined(CONFIG_405EX)
Stefan Roese887e2ec2006-09-07 11:51:23 +0200207#define SDR0_MFR_ETH_CLK_SEL_V(n) ((0x01<<27) / (n+1))
208#endif
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200209
Stefan Roese8ac41e32008-03-11 15:05:26 +0100210#if defined(CONFIG_460EX) || defined(CONFIG_460GT)
211#define SDR0_ETH_CFG_CLK_SEL_V(n) (0x01 << (8 + n))
212#endif
213
214#if defined(CONFIG_460EX) || defined(CONFIG_460GT)
215#define MAL_RX_CHAN_MUL 8 /* 460EX/GT uses MAL channel 8 for EMAC1 */
216#else
217#define MAL_RX_CHAN_MUL 1
218#endif
219
Victor Gallardo78d78232008-09-04 23:49:36 -0700220/*--------------------------------------------------------------------+
221 * Fixed PHY (PHY-less) support for Ethernet Ports.
222 *--------------------------------------------------------------------*/
223
224/*
225 * Some boards do not have a PHY for each ethernet port. These ports
226 * are known as Fixed PHY (or PHY-less) ports. For such ports, set
227 * the appropriate CONFIG_PHY_ADDR equal to CONFIG_FIXED_PHY and
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200228 * then define CONFIG_SYS_FIXED_PHY_PORTS to define what the speed and
Victor Gallardo78d78232008-09-04 23:49:36 -0700229 * duplex should be for these ports in the board configuration
230 * file.
231 *
232 * For Example:
233 * #define CONFIG_FIXED_PHY 0xFFFFFFFF
234 *
235 * #define CONFIG_PHY_ADDR CONFIG_FIXED_PHY
236 * #define CONFIG_PHY1_ADDR 1
237 * #define CONFIG_PHY2_ADDR CONFIG_FIXED_PHY
238 * #define CONFIG_PHY3_ADDR 3
239 *
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200240 * #define CONFIG_SYS_FIXED_PHY_PORT(devnum,speed,duplex) \
Victor Gallardo78d78232008-09-04 23:49:36 -0700241 * {devnum, speed, duplex},
242 *
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200243 * #define CONFIG_SYS_FIXED_PHY_PORTS \
244 * CONFIG_SYS_FIXED_PHY_PORT(0,1000,FULL) \
245 * CONFIG_SYS_FIXED_PHY_PORT(2,100,HALF)
Victor Gallardo78d78232008-09-04 23:49:36 -0700246 */
247
248#ifndef CONFIG_FIXED_PHY
249#define CONFIG_FIXED_PHY 0xFFFFFFFF /* Fixed PHY (PHY-less) */
250#endif
251
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200252#ifndef CONFIG_SYS_FIXED_PHY_PORTS
253#define CONFIG_SYS_FIXED_PHY_PORTS /* default is an empty array */
Victor Gallardo78d78232008-09-04 23:49:36 -0700254#endif
255
256struct fixed_phy_port {
257 unsigned int devnum; /* ethernet port */
258 unsigned int speed; /* specified speed 10,100 or 1000 */
259 unsigned int duplex; /* specified duplex FULL or HALF */
260};
261
262static const struct fixed_phy_port fixed_phy_port[] = {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200263 CONFIG_SYS_FIXED_PHY_PORTS /* defined in board configuration file */
Victor Gallardo78d78232008-09-04 23:49:36 -0700264};
265
wdenkba56f622004-02-06 23:19:44 +0000266/*-----------------------------------------------------------------------------+
267 * Global variables. TX and RX descriptors and buffers.
268 *-----------------------------------------------------------------------------*/
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200269#if !defined(CONFIG_NET_MULTI)
Stefan Roese4f92ac32005-10-10 17:43:58 +0200270struct eth_device *emac0_dev = NULL;
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200271#endif
272
Stefan Roese1e25f952005-10-20 16:34:28 +0200273/*
274 * Get count of EMAC devices (doesn't have to be the max. possible number
275 * supported by the cpu)
Stefan Roese353f2682007-10-23 10:10:08 +0200276 *
277 * CONFIG_BOARD_EMAC_COUNT added so now a "dynamic" way to configure the
278 * EMAC count is possible. As it is needed for the Kilauea/Haleakala
279 * 405EX/405EXr eval board, using the same binary.
Stefan Roese1e25f952005-10-20 16:34:28 +0200280 */
Stefan Roese353f2682007-10-23 10:10:08 +0200281#if defined(CONFIG_BOARD_EMAC_COUNT)
282#define LAST_EMAC_NUM board_emac_count()
283#else /* CONFIG_BOARD_EMAC_COUNT */
Stefan Roese1e25f952005-10-20 16:34:28 +0200284#if defined(CONFIG_HAS_ETH3)
285#define LAST_EMAC_NUM 4
286#elif defined(CONFIG_HAS_ETH2)
287#define LAST_EMAC_NUM 3
288#elif defined(CONFIG_HAS_ETH1)
289#define LAST_EMAC_NUM 2
290#else
291#define LAST_EMAC_NUM 1
292#endif
Stefan Roese353f2682007-10-23 10:10:08 +0200293#endif /* CONFIG_BOARD_EMAC_COUNT */
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200294
Stefan Roese5fb692c2007-01-18 10:25:34 +0100295/* normal boards start with EMAC0 */
296#if !defined(CONFIG_EMAC_NR_START)
297#define CONFIG_EMAC_NR_START 0
298#endif
299
Stefan Roeseff768cb2007-10-31 18:01:24 +0100300#define MAL_RX_DESC_SIZE 2048
301#define MAL_TX_DESC_SIZE 2048
302#define MAL_ALLOC_SIZE (MAL_TX_DESC_SIZE + MAL_RX_DESC_SIZE)
303
wdenkba56f622004-02-06 23:19:44 +0000304/*-----------------------------------------------------------------------------+
305 * Prototypes and externals.
306 *-----------------------------------------------------------------------------*/
307static void enet_rcv (struct eth_device *dev, unsigned long malisr);
308
309int enetInt (struct eth_device *dev);
310static void mal_err (struct eth_device *dev, unsigned long isr,
311 unsigned long uic, unsigned long maldef,
312 unsigned long mal_errr);
313static void emac_err (struct eth_device *dev, unsigned long isr);
314
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200315extern int phy_setup_aneg (char *devname, unsigned char addr);
316extern int emac4xx_miiphy_read (char *devname, unsigned char addr,
317 unsigned char reg, unsigned short *value);
318extern int emac4xx_miiphy_write (char *devname, unsigned char addr,
319 unsigned char reg, unsigned short value);
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200320
Stefan Roese353f2682007-10-23 10:10:08 +0200321int board_emac_count(void);
322
Stefan Roese8ac41e32008-03-11 15:05:26 +0100323static void emac_loopback_enable(EMAC_4XX_HW_PST hw_p)
324{
325#if defined(CONFIG_440SPE) || \
326 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
327 defined(CONFIG_405EX)
328 u32 val;
329
330 mfsdr(sdr_mfr, val);
331 val |= SDR0_MFR_ETH_CLK_SEL_V(hw_p->devnum);
332 mtsdr(sdr_mfr, val);
333#elif defined(CONFIG_460EX) || defined(CONFIG_460GT)
334 u32 val;
335
336 mfsdr(SDR0_ETH_CFG, val);
337 val |= SDR0_ETH_CFG_CLK_SEL_V(hw_p->devnum);
338 mtsdr(SDR0_ETH_CFG, val);
339#endif
340}
341
342static void emac_loopback_disable(EMAC_4XX_HW_PST hw_p)
343{
344#if defined(CONFIG_440SPE) || \
345 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
346 defined(CONFIG_405EX)
347 u32 val;
348
349 mfsdr(sdr_mfr, val);
350 val &= ~SDR0_MFR_ETH_CLK_SEL_V(hw_p->devnum);
351 mtsdr(sdr_mfr, val);
352#elif defined(CONFIG_460EX) || defined(CONFIG_460GT)
353 u32 val;
354
355 mfsdr(SDR0_ETH_CFG, val);
356 val &= ~SDR0_ETH_CFG_CLK_SEL_V(hw_p->devnum);
357 mtsdr(SDR0_ETH_CFG, val);
358#endif
359}
360
wdenkba56f622004-02-06 23:19:44 +0000361/*-----------------------------------------------------------------------------+
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200362| ppc_4xx_eth_halt
wdenkba56f622004-02-06 23:19:44 +0000363| Disable MAL channel, and EMACn
wdenkba56f622004-02-06 23:19:44 +0000364+-----------------------------------------------------------------------------*/
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200365static void ppc_4xx_eth_halt (struct eth_device *dev)
wdenkba56f622004-02-06 23:19:44 +0000366{
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200367 EMAC_4XX_HW_PST hw_p = dev->priv;
Stefan Roese9ad31982008-03-19 16:35:12 +0100368 u32 val = 10000;
wdenkba56f622004-02-06 23:19:44 +0000369
Stefan Roese2d834762007-10-23 14:03:17 +0200370 out_be32((void *)EMAC_IER + hw_p->hw_addr, 0x00000000); /* disable emac interrupts */
wdenkba56f622004-02-06 23:19:44 +0000371
372 /* 1st reset MAL channel */
373 /* Note: writing a 0 to a channel has no effect */
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200374#if defined(CONFIG_405EP) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
375 mtdcr (maltxcarr, (MAL_CR_MMSR >> (hw_p->devnum * 2)));
376#else
wdenkba56f622004-02-06 23:19:44 +0000377 mtdcr (maltxcarr, (MAL_CR_MMSR >> hw_p->devnum));
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200378#endif
wdenkba56f622004-02-06 23:19:44 +0000379 mtdcr (malrxcarr, (MAL_CR_MMSR >> hw_p->devnum));
380
381 /* wait for reset */
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200382 while (mfdcr (malrxcasr) & (MAL_CR_MMSR >> hw_p->devnum)) {
wdenkba56f622004-02-06 23:19:44 +0000383 udelay (1000); /* Delay 1 MS so as not to hammer the register */
Stefan Roese9ad31982008-03-19 16:35:12 +0100384 val--;
385 if (val == 0)
wdenkba56f622004-02-06 23:19:44 +0000386 break;
wdenkba56f622004-02-06 23:19:44 +0000387 }
388
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200389 /* provide clocks for EMAC internal loopback */
Stefan Roese8ac41e32008-03-11 15:05:26 +0100390 emac_loopback_enable(hw_p);
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200391
Stefan Roese8ac41e32008-03-11 15:05:26 +0100392 /* EMAC RESET */
Stefan Roese2d834762007-10-23 14:03:17 +0200393 out_be32((void *)EMAC_M0 + hw_p->hw_addr, EMAC_M0_SRST);
wdenkba56f622004-02-06 23:19:44 +0000394
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200395 /* remove clocks for EMAC internal loopback */
Stefan Roese8ac41e32008-03-11 15:05:26 +0100396 emac_loopback_disable(hw_p);
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200397
Stefan Roesea93316c2005-10-18 19:17:12 +0200398#ifndef CONFIG_NETCONSOLE
Stefan Roesec157d8e2005-08-01 16:41:48 +0200399 hw_p->print_speed = 1; /* print speed message again next time */
Stefan Roesea93316c2005-10-18 19:17:12 +0200400#endif
Stefan Roesec157d8e2005-08-01 16:41:48 +0200401
Stefan Roese4c9e8552008-03-19 16:20:49 +0100402#if defined(CONFIG_460EX) || defined(CONFIG_460GT)
403 /* don't bypass the TAHOE0/TAHOE1 cores for Linux */
Stefan Roese9ad31982008-03-19 16:35:12 +0100404 mfsdr(SDR0_ETH_CFG, val);
405 val &= ~(SDR0_ETH_CFG_TAHOE0_BYPASS | SDR0_ETH_CFG_TAHOE1_BYPASS);
406 mtsdr(SDR0_ETH_CFG, val);
Stefan Roese4c9e8552008-03-19 16:20:49 +0100407#endif
408
wdenkba56f622004-02-06 23:19:44 +0000409 return;
410}
411
Stefan Roese846b0dd2005-08-08 12:42:22 +0200412#if defined (CONFIG_440GX)
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200413int ppc_4xx_eth_setup_bridge(int devnum, bd_t * bis)
wdenk855a4962004-03-14 18:23:55 +0000414{
415 unsigned long pfc1;
416 unsigned long zmiifer;
417 unsigned long rmiifer;
418
419 mfsdr(sdr_pfc1, pfc1);
420 pfc1 = SDR0_PFC1_EPS_DECODE(pfc1);
421
422 zmiifer = 0;
423 rmiifer = 0;
424
425 switch (pfc1) {
426 case 1:
427 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
428 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(1);
429 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(2);
430 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(3);
431 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
432 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
433 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
434 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
435 break;
436 case 2:
Stefan Roesef6e495f2006-11-27 17:43:25 +0100437 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(0);
438 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(1);
439 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(2);
440 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(3);
wdenk855a4962004-03-14 18:23:55 +0000441 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
442 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
443 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
444 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
445 break;
446 case 3:
447 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
448 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
449 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
450 bis->bi_phymode[1] = BI_PHYMODE_NONE;
451 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
452 bis->bi_phymode[3] = BI_PHYMODE_NONE;
453 break;
454 case 4:
455 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(0);
456 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(1);
457 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V (2);
458 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V (3);
459 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
460 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
461 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
462 bis->bi_phymode[3] = BI_PHYMODE_RGMII;
463 break;
464 case 5:
465 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (0);
466 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (1);
467 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (2);
468 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(3);
469 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
470 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
471 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
472 bis->bi_phymode[3] = BI_PHYMODE_RGMII;
473 break;
474 case 6:
475 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (0);
476 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (1);
477 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
wdenk855a4962004-03-14 18:23:55 +0000478 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
479 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
480 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
wdenk855a4962004-03-14 18:23:55 +0000481 break;
482 case 0:
483 default:
484 zmiifer = ZMII_FER_MII << ZMII_FER_V(devnum);
485 rmiifer = 0x0;
486 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
487 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
488 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
489 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
490 break;
491 }
492
493 /* Ensure we setup mdio for this devnum and ONLY this devnum */
494 zmiifer |= (ZMII_FER_MDI) << ZMII_FER_V(devnum);
495
Stefan Roeseff768cb2007-10-31 18:01:24 +0100496 out_be32((void *)ZMII_FER, zmiifer);
497 out_be32((void *)RGMII_FER, rmiifer);
wdenk855a4962004-03-14 18:23:55 +0000498
499 return ((int)pfc1);
wdenk855a4962004-03-14 18:23:55 +0000500}
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200501#endif /* CONFIG_440_GX */
wdenk855a4962004-03-14 18:23:55 +0000502
Stefan Roese887e2ec2006-09-07 11:51:23 +0200503#if defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
504int ppc_4xx_eth_setup_bridge(int devnum, bd_t * bis)
505{
506 unsigned long zmiifer=0x0;
Matthias Fuchs37ed6cd2007-04-24 14:03:45 +0200507 unsigned long pfc1;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200508
Matthias Fuchs37ed6cd2007-04-24 14:03:45 +0200509 mfsdr(sdr_pfc1, pfc1);
510 pfc1 &= SDR0_PFC1_SELECT_MASK;
511
Wolfgang Denk2f152782007-05-05 18:23:11 +0200512 switch (pfc1) {
Matthias Fuchs37ed6cd2007-04-24 14:03:45 +0200513 case SDR0_PFC1_SELECT_CONFIG_2:
Stefan Roese887e2ec2006-09-07 11:51:23 +0200514 /* 1 x GMII port */
Stefan Roese2d834762007-10-23 14:03:17 +0200515 out_be32((void *)ZMII_FER, 0x00);
516 out_be32((void *)RGMII_FER, 0x00000037);
Stefan Roese887e2ec2006-09-07 11:51:23 +0200517 bis->bi_phymode[0] = BI_PHYMODE_GMII;
518 bis->bi_phymode[1] = BI_PHYMODE_NONE;
519 break;
Matthias Fuchs37ed6cd2007-04-24 14:03:45 +0200520 case SDR0_PFC1_SELECT_CONFIG_4:
Stefan Roese887e2ec2006-09-07 11:51:23 +0200521 /* 2 x RGMII ports */
Stefan Roese2d834762007-10-23 14:03:17 +0200522 out_be32((void *)ZMII_FER, 0x00);
523 out_be32((void *)RGMII_FER, 0x00000055);
Stefan Roese887e2ec2006-09-07 11:51:23 +0200524 bis->bi_phymode[0] = BI_PHYMODE_RGMII;
525 bis->bi_phymode[1] = BI_PHYMODE_RGMII;
526 break;
Matthias Fuchs37ed6cd2007-04-24 14:03:45 +0200527 case SDR0_PFC1_SELECT_CONFIG_6:
Stefan Roese887e2ec2006-09-07 11:51:23 +0200528 /* 2 x SMII ports */
Stefan Roese2d834762007-10-23 14:03:17 +0200529 out_be32((void *)ZMII_FER,
530 ((ZMII_FER_SMII) << ZMII_FER_V(0)) |
531 ((ZMII_FER_SMII) << ZMII_FER_V(1)));
532 out_be32((void *)RGMII_FER, 0x00000000);
Matthias Fuchs37ed6cd2007-04-24 14:03:45 +0200533 bis->bi_phymode[0] = BI_PHYMODE_SMII;
534 bis->bi_phymode[1] = BI_PHYMODE_SMII;
535 break;
536 case SDR0_PFC1_SELECT_CONFIG_1_2:
537 /* only 1 x MII supported */
Stefan Roese2d834762007-10-23 14:03:17 +0200538 out_be32((void *)ZMII_FER, (ZMII_FER_MII) << ZMII_FER_V(0));
539 out_be32((void *)RGMII_FER, 0x00000000);
Matthias Fuchs37ed6cd2007-04-24 14:03:45 +0200540 bis->bi_phymode[0] = BI_PHYMODE_MII;
541 bis->bi_phymode[1] = BI_PHYMODE_NONE;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200542 break;
543 default:
544 break;
545 }
546
547 /* Ensure we setup mdio for this devnum and ONLY this devnum */
Stefan Roese2d834762007-10-23 14:03:17 +0200548 zmiifer = in_be32((void *)ZMII_FER);
Stefan Roese887e2ec2006-09-07 11:51:23 +0200549 zmiifer |= (ZMII_FER_MDI) << ZMII_FER_V(devnum);
Stefan Roese2d834762007-10-23 14:03:17 +0200550 out_be32((void *)ZMII_FER, zmiifer);
Stefan Roese887e2ec2006-09-07 11:51:23 +0200551
552 return ((int)0x0);
553}
554#endif /* CONFIG_440EPX */
555
Stefan Roesedbbd1252007-10-05 17:10:59 +0200556#if defined(CONFIG_405EX)
557int ppc_4xx_eth_setup_bridge(int devnum, bd_t * bis)
558{
Grant Erickson1740c1b2008-07-08 08:35:00 -0700559 u32 rgmiifer = 0;
Stefan Roesedbbd1252007-10-05 17:10:59 +0200560
561 /*
Grant Erickson1740c1b2008-07-08 08:35:00 -0700562 * The 405EX(r)'s RGMII bridge can operate in one of several
563 * modes, only one of which (2 x RGMII) allows the
564 * simultaneous use of both EMACs on the 405EX.
Stefan Roesedbbd1252007-10-05 17:10:59 +0200565 */
Grant Erickson1740c1b2008-07-08 08:35:00 -0700566
567 switch (CONFIG_EMAC_PHY_MODE) {
568
569 case EMAC_PHY_MODE_NONE:
570 /* No ports */
571 rgmiifer |= RGMII_FER_DIS << 0;
572 rgmiifer |= RGMII_FER_DIS << 4;
573 out_be32((void *)RGMII_FER, rgmiifer);
574 bis->bi_phymode[0] = BI_PHYMODE_NONE;
575 bis->bi_phymode[1] = BI_PHYMODE_NONE;
576 break;
577 case EMAC_PHY_MODE_NONE_RGMII:
578 /* 1 x RGMII port on channel 0 */
579 rgmiifer |= RGMII_FER_RGMII << 0;
580 rgmiifer |= RGMII_FER_DIS << 4;
581 out_be32((void *)RGMII_FER, rgmiifer);
582 bis->bi_phymode[0] = BI_PHYMODE_RGMII;
583 bis->bi_phymode[1] = BI_PHYMODE_NONE;
584 break;
585 case EMAC_PHY_MODE_RGMII_NONE:
586 /* 1 x RGMII port on channel 1 */
587 rgmiifer |= RGMII_FER_DIS << 0;
588 rgmiifer |= RGMII_FER_RGMII << 4;
589 out_be32((void *)RGMII_FER, rgmiifer);
590 bis->bi_phymode[0] = BI_PHYMODE_NONE;
591 bis->bi_phymode[1] = BI_PHYMODE_RGMII;
592 break;
593 case EMAC_PHY_MODE_RGMII_RGMII:
Stefan Roesedbbd1252007-10-05 17:10:59 +0200594 /* 2 x RGMII ports */
Grant Erickson1740c1b2008-07-08 08:35:00 -0700595 rgmiifer |= RGMII_FER_RGMII << 0;
596 rgmiifer |= RGMII_FER_RGMII << 4;
597 out_be32((void *)RGMII_FER, rgmiifer);
Stefan Roesedbbd1252007-10-05 17:10:59 +0200598 bis->bi_phymode[0] = BI_PHYMODE_RGMII;
599 bis->bi_phymode[1] = BI_PHYMODE_RGMII;
600 break;
Grant Erickson1740c1b2008-07-08 08:35:00 -0700601 case EMAC_PHY_MODE_NONE_GMII:
602 /* 1 x GMII port on channel 0 */
603 rgmiifer |= RGMII_FER_GMII << 0;
604 rgmiifer |= RGMII_FER_DIS << 4;
605 out_be32((void *)RGMII_FER, rgmiifer);
606 bis->bi_phymode[0] = BI_PHYMODE_GMII;
607 bis->bi_phymode[1] = BI_PHYMODE_NONE;
608 break;
609 case EMAC_PHY_MODE_NONE_MII:
610 /* 1 x MII port on channel 0 */
611 rgmiifer |= RGMII_FER_MII << 0;
612 rgmiifer |= RGMII_FER_DIS << 4;
613 out_be32((void *)RGMII_FER, rgmiifer);
614 bis->bi_phymode[0] = BI_PHYMODE_MII;
615 bis->bi_phymode[1] = BI_PHYMODE_NONE;
616 break;
617 case EMAC_PHY_MODE_GMII_NONE:
618 /* 1 x GMII port on channel 1 */
619 rgmiifer |= RGMII_FER_DIS << 0;
620 rgmiifer |= RGMII_FER_GMII << 4;
621 out_be32((void *)RGMII_FER, rgmiifer);
622 bis->bi_phymode[0] = BI_PHYMODE_NONE;
623 bis->bi_phymode[1] = BI_PHYMODE_GMII;
624 break;
625 case EMAC_PHY_MODE_MII_NONE:
626 /* 1 x MII port on channel 1 */
627 rgmiifer |= RGMII_FER_DIS << 0;
628 rgmiifer |= RGMII_FER_MII << 4;
629 out_be32((void *)RGMII_FER, rgmiifer);
630 bis->bi_phymode[0] = BI_PHYMODE_NONE;
631 bis->bi_phymode[1] = BI_PHYMODE_MII;
Stefan Roesedbbd1252007-10-05 17:10:59 +0200632 break;
633 default:
634 break;
635 }
636
637 /* Ensure we setup mdio for this devnum and ONLY this devnum */
Grant Erickson1740c1b2008-07-08 08:35:00 -0700638 rgmiifer = in_be32((void *)RGMII_FER);
639 rgmiifer |= (1 << (19-devnum));
640 out_be32((void *)RGMII_FER, rgmiifer);
Stefan Roesedbbd1252007-10-05 17:10:59 +0200641
642 return ((int)0x0);
643}
644#endif /* CONFIG_405EX */
645
Stefan Roese8ac41e32008-03-11 15:05:26 +0100646#if defined(CONFIG_460EX) || defined(CONFIG_460GT)
647int ppc_4xx_eth_setup_bridge(int devnum, bd_t * bis)
648{
649 u32 eth_cfg;
650 u32 zmiifer; /* ZMII0_FER reg. */
651 u32 rmiifer; /* RGMII0_FER reg. Bridge 0 */
652 u32 rmiifer1; /* RGMII0_FER reg. Bridge 1 */
Stefan Roese4c9e8552008-03-19 16:20:49 +0100653 int mode;
Stefan Roese8ac41e32008-03-11 15:05:26 +0100654
655 zmiifer = 0;
656 rmiifer = 0;
657 rmiifer1 = 0;
658
Stefan Roese4c9e8552008-03-19 16:20:49 +0100659#if defined(CONFIG_460EX)
660 mode = 9;
Victor Gallardo78d78232008-09-04 23:49:36 -0700661 mfsdr(SDR0_ETH_CFG, eth_cfg);
662 if (((eth_cfg & SDR0_ETH_CFG_SGMII0_ENABLE) > 0) &&
663 ((eth_cfg & SDR0_ETH_CFG_SGMII1_ENABLE) > 0))
664 mode = 11; /* config SGMII */
Stefan Roese4c9e8552008-03-19 16:20:49 +0100665#else
666 mode = 10;
Victor Gallardo78d78232008-09-04 23:49:36 -0700667 mfsdr(SDR0_ETH_CFG, eth_cfg);
668 if (((eth_cfg & SDR0_ETH_CFG_SGMII0_ENABLE) > 0) &&
669 ((eth_cfg & SDR0_ETH_CFG_SGMII1_ENABLE) > 0) &&
670 ((eth_cfg & SDR0_ETH_CFG_SGMII2_ENABLE) > 0))
671 mode = 12; /* config SGMII */
Stefan Roese4c9e8552008-03-19 16:20:49 +0100672#endif
673
Stefan Roese8ac41e32008-03-11 15:05:26 +0100674 /* TODO:
675 * NOTE: 460GT has 2 RGMII bridge cores:
676 * emac0 ------ RGMII0_BASE
677 * |
678 * emac1 -----+
679 *
680 * emac2 ------ RGMII1_BASE
681 * |
682 * emac3 -----+
683 *
684 * 460EX has 1 RGMII bridge core:
685 * and RGMII1_BASE is disabled
686 * emac0 ------ RGMII0_BASE
687 * |
688 * emac1 -----+
689 */
690
691 /*
692 * Right now only 2*RGMII is supported. Please extend when needed.
693 * sr - 2008-02-19
Victor Gallardo78d78232008-09-04 23:49:36 -0700694 * Add SGMII support.
695 * vg - 2008-07-28
Stefan Roese8ac41e32008-03-11 15:05:26 +0100696 */
Stefan Roese4c9e8552008-03-19 16:20:49 +0100697 switch (mode) {
Stefan Roese8ac41e32008-03-11 15:05:26 +0100698 case 1:
699 /* 1 MII - 460EX */
700 /* GMC0 EMAC4_0, ZMII Bridge */
701 zmiifer |= ZMII_FER_MII << ZMII_FER_V(0);
702 bis->bi_phymode[0] = BI_PHYMODE_MII;
703 bis->bi_phymode[1] = BI_PHYMODE_NONE;
704 bis->bi_phymode[2] = BI_PHYMODE_NONE;
705 bis->bi_phymode[3] = BI_PHYMODE_NONE;
706 break;
707 case 2:
708 /* 2 MII - 460GT */
709 /* GMC0 EMAC4_0, GMC1 EMAC4_2, ZMII Bridge */
710 zmiifer |= ZMII_FER_MII << ZMII_FER_V(0);
711 zmiifer |= ZMII_FER_MII << ZMII_FER_V(2);
712 bis->bi_phymode[0] = BI_PHYMODE_MII;
713 bis->bi_phymode[1] = BI_PHYMODE_NONE;
714 bis->bi_phymode[2] = BI_PHYMODE_MII;
715 bis->bi_phymode[3] = BI_PHYMODE_NONE;
716 break;
717 case 3:
718 /* 2 RMII - 460EX */
719 /* GMC0 EMAC4_0, GMC0 EMAC4_1, ZMII Bridge */
720 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
721 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(1);
722 bis->bi_phymode[0] = BI_PHYMODE_RMII;
723 bis->bi_phymode[1] = BI_PHYMODE_RMII;
724 bis->bi_phymode[2] = BI_PHYMODE_NONE;
725 bis->bi_phymode[3] = BI_PHYMODE_NONE;
726 break;
727 case 4:
728 /* 4 RMII - 460GT */
729 /* GMC0 EMAC4_0, GMC0 EMAC4_1, GMC1 EMAC4_2, GMC1, EMAC4_3 */
730 /* ZMII Bridge */
731 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
732 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(1);
733 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(2);
734 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(3);
735 bis->bi_phymode[0] = BI_PHYMODE_RMII;
736 bis->bi_phymode[1] = BI_PHYMODE_RMII;
737 bis->bi_phymode[2] = BI_PHYMODE_RMII;
738 bis->bi_phymode[3] = BI_PHYMODE_RMII;
739 break;
740 case 5:
741 /* 2 SMII - 460EX */
742 /* GMC0 EMAC4_0, GMC0 EMAC4_1, ZMII Bridge */
743 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(0);
744 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(1);
745 bis->bi_phymode[0] = BI_PHYMODE_SMII;
746 bis->bi_phymode[1] = BI_PHYMODE_SMII;
747 bis->bi_phymode[2] = BI_PHYMODE_NONE;
748 bis->bi_phymode[3] = BI_PHYMODE_NONE;
749 break;
750 case 6:
751 /* 4 SMII - 460GT */
752 /* GMC0 EMAC4_0, GMC0 EMAC4_1, GMC0 EMAC4_3, GMC0 EMAC4_3 */
753 /* ZMII Bridge */
754 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(0);
755 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(1);
756 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(2);
757 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(3);
758 bis->bi_phymode[0] = BI_PHYMODE_SMII;
759 bis->bi_phymode[1] = BI_PHYMODE_SMII;
760 bis->bi_phymode[2] = BI_PHYMODE_SMII;
761 bis->bi_phymode[3] = BI_PHYMODE_SMII;
762 break;
763 case 7:
764 /* This is the default mode that we want for board bringup - Maple */
765 /* 1 GMII - 460EX */
766 /* GMC0 EMAC4_0, RGMII Bridge 0 */
767 rmiifer |= RGMII_FER_MDIO(0);
768
769 if (devnum == 0) {
770 rmiifer |= RGMII_FER_GMII << RGMII_FER_V(2); /* CH0CFG - EMAC0 */
771 bis->bi_phymode[0] = BI_PHYMODE_GMII;
772 bis->bi_phymode[1] = BI_PHYMODE_NONE;
773 bis->bi_phymode[2] = BI_PHYMODE_NONE;
774 bis->bi_phymode[3] = BI_PHYMODE_NONE;
775 } else {
776 rmiifer |= RGMII_FER_GMII << RGMII_FER_V(3); /* CH1CFG - EMAC1 */
777 bis->bi_phymode[0] = BI_PHYMODE_NONE;
778 bis->bi_phymode[1] = BI_PHYMODE_GMII;
779 bis->bi_phymode[2] = BI_PHYMODE_NONE;
780 bis->bi_phymode[3] = BI_PHYMODE_NONE;
781 }
782 break;
783 case 8:
784 /* 2 GMII - 460GT */
785 /* GMC0 EMAC4_0, RGMII Bridge 0 */
786 /* GMC1 EMAC4_2, RGMII Bridge 1 */
787 rmiifer |= RGMII_FER_GMII << RGMII_FER_V(2); /* CH0CFG - EMAC0 */
788 rmiifer1 |= RGMII_FER_GMII << RGMII_FER_V(2); /* CH0CFG - EMAC2 */
789 rmiifer |= RGMII_FER_MDIO(0); /* enable MDIO - EMAC0 */
790 rmiifer1 |= RGMII_FER_MDIO(0); /* enable MDIO - EMAC2 */
791
792 bis->bi_phymode[0] = BI_PHYMODE_GMII;
793 bis->bi_phymode[1] = BI_PHYMODE_NONE;
794 bis->bi_phymode[2] = BI_PHYMODE_GMII;
795 bis->bi_phymode[3] = BI_PHYMODE_NONE;
796 break;
797 case 9:
798 /* 2 RGMII - 460EX */
799 /* GMC0 EMAC4_0, GMC0 EMAC4_1, RGMII Bridge 0 */
800 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
801 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(3);
802 rmiifer |= RGMII_FER_MDIO(0); /* enable MDIO - EMAC0 */
803
804 bis->bi_phymode[0] = BI_PHYMODE_RGMII;
805 bis->bi_phymode[1] = BI_PHYMODE_RGMII;
806 bis->bi_phymode[2] = BI_PHYMODE_NONE;
807 bis->bi_phymode[3] = BI_PHYMODE_NONE;
808 break;
809 case 10:
810 /* 4 RGMII - 460GT */
811 /* GMC0 EMAC4_0, GMC0 EMAC4_1, RGMII Bridge 0 */
812 /* GMC1 EMAC4_2, GMC1 EMAC4_3, RGMII Bridge 1 */
813 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
814 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(3);
815 rmiifer1 |= RGMII_FER_RGMII << RGMII_FER_V(2);
816 rmiifer1 |= RGMII_FER_RGMII << RGMII_FER_V(3);
817 bis->bi_phymode[0] = BI_PHYMODE_RGMII;
818 bis->bi_phymode[1] = BI_PHYMODE_RGMII;
819 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
820 bis->bi_phymode[3] = BI_PHYMODE_RGMII;
821 break;
Victor Gallardo78d78232008-09-04 23:49:36 -0700822 case 11:
823 /* 2 SGMII - 460EX */
824 bis->bi_phymode[0] = BI_PHYMODE_SGMII;
825 bis->bi_phymode[1] = BI_PHYMODE_SGMII;
826 bis->bi_phymode[2] = BI_PHYMODE_NONE;
827 bis->bi_phymode[3] = BI_PHYMODE_NONE;
828 break;
829 case 12:
830 /* 3 SGMII - 460GT */
831 bis->bi_phymode[0] = BI_PHYMODE_SGMII;
832 bis->bi_phymode[1] = BI_PHYMODE_SGMII;
833 bis->bi_phymode[2] = BI_PHYMODE_SGMII;
834 bis->bi_phymode[3] = BI_PHYMODE_NONE;
835 break;
Stefan Roese8ac41e32008-03-11 15:05:26 +0100836 default:
837 break;
838 }
839
840 /* Set EMAC for MDIO */
841 mfsdr(SDR0_ETH_CFG, eth_cfg);
842 eth_cfg |= SDR0_ETH_CFG_MDIO_SEL_EMAC0;
843 mtsdr(SDR0_ETH_CFG, eth_cfg);
844
845 out_be32((void *)RGMII_FER, rmiifer);
846#if defined(CONFIG_460GT)
847 out_be32((void *)RGMII_FER + RGMII1_BASE_OFFSET, rmiifer1);
848#endif
849
850 /* bypass the TAHOE0/TAHOE1 cores for U-Boot */
851 mfsdr(SDR0_ETH_CFG, eth_cfg);
852 eth_cfg |= (SDR0_ETH_CFG_TAHOE0_BYPASS | SDR0_ETH_CFG_TAHOE1_BYPASS);
853 mtsdr(SDR0_ETH_CFG, eth_cfg);
854
855 return 0;
856}
857#endif /* CONFIG_460EX || CONFIG_460GT */
858
Stefan Roeseff768cb2007-10-31 18:01:24 +0100859static inline void *malloc_aligned(u32 size, u32 align)
860{
861 return (void *)(((u32)malloc(size + align) + align - 1) &
862 ~(align - 1));
863}
864
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200865static int ppc_4xx_eth_init (struct eth_device *dev, bd_t * bis)
wdenkba56f622004-02-06 23:19:44 +0000866{
Stefan Roeseff768cb2007-10-31 18:01:24 +0100867 int i;
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200868 unsigned long reg = 0;
wdenkba56f622004-02-06 23:19:44 +0000869 unsigned long msr;
870 unsigned long speed;
871 unsigned long duplex;
872 unsigned long failsafe;
873 unsigned mode_reg;
874 unsigned short devnum;
875 unsigned short reg_short;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200876#if defined(CONFIG_440GX) || \
877 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200878 defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
Stefan Roese8ac41e32008-03-11 15:05:26 +0100879 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200880 defined(CONFIG_405EX)
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200881 sys_info_t sysinfo;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200882#if defined(CONFIG_440GX) || defined(CONFIG_440SPE) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200883 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roese8ac41e32008-03-11 15:05:26 +0100884 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200885 defined(CONFIG_405EX)
Stefan Roese6e7fb6e2005-11-29 18:18:21 +0100886 int ethgroup = -1;
887#endif
Stefan Roesec157d8e2005-08-01 16:41:48 +0200888#endif
Stefan Roeseff768cb2007-10-31 18:01:24 +0100889 u32 bd_cached;
890 u32 bd_uncached = 0;
Anatolij Gustschin4fae35a2008-02-25 20:54:04 +0100891#ifdef CONFIG_4xx_DCACHE
892 static u32 last_used_ea = 0;
893#endif
Stefan Roesee54ec0f2008-04-03 14:50:34 +0200894#if defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
895 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
896 defined(CONFIG_405EX)
897 int rgmii_channel;
898#endif
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200899
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200900 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenkba56f622004-02-06 23:19:44 +0000901
902 /* before doing anything, figure out if we have a MAC address */
903 /* if not, bail */
Stefan Roese4f92ac32005-10-10 17:43:58 +0200904 if (memcmp (dev->enetaddr, "\0\0\0\0\0\0", 6) == 0) {
905 printf("ERROR: ethaddr not set!\n");
wdenkba56f622004-02-06 23:19:44 +0000906 return -1;
Stefan Roese4f92ac32005-10-10 17:43:58 +0200907 }
wdenkba56f622004-02-06 23:19:44 +0000908
Stefan Roese887e2ec2006-09-07 11:51:23 +0200909#if defined(CONFIG_440GX) || \
910 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200911 defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
Stefan Roese8ac41e32008-03-11 15:05:26 +0100912 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200913 defined(CONFIG_405EX)
wdenkba56f622004-02-06 23:19:44 +0000914 /* Need to get the OPB frequency so we can access the PHY */
915 get_sys_info (&sysinfo);
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200916#endif
wdenkba56f622004-02-06 23:19:44 +0000917
wdenkba56f622004-02-06 23:19:44 +0000918 msr = mfmsr ();
919 mtmsr (msr & ~(MSR_EE)); /* disable interrupts */
920
921 devnum = hw_p->devnum;
922
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200923#ifdef INFO_4XX_ENET
wdenkba56f622004-02-06 23:19:44 +0000924 /* AS.HARNOIS
925 * We should have :
Wolfgang Denk265817c2005-09-25 00:53:22 +0200926 * hw_p->stats.pkts_handled <= hw_p->stats.pkts_rx <= hw_p->stats.pkts_handled+PKTBUFSRX
wdenkba56f622004-02-06 23:19:44 +0000927 * In the most cases hw_p->stats.pkts_handled = hw_p->stats.pkts_rx, but it
928 * is possible that new packets (without relationship with
929 * current transfer) have got the time to arrived before
930 * netloop calls eth_halt
931 */
932 printf ("About preceeding transfer (eth%d):\n"
933 "- Sent packet number %d\n"
934 "- Received packet number %d\n"
935 "- Handled packet number %d\n",
936 hw_p->devnum,
937 hw_p->stats.pkts_tx,
938 hw_p->stats.pkts_rx, hw_p->stats.pkts_handled);
939
940 hw_p->stats.pkts_tx = 0;
941 hw_p->stats.pkts_rx = 0;
942 hw_p->stats.pkts_handled = 0;
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200943 hw_p->print_speed = 1; /* print speed message again next time */
wdenkba56f622004-02-06 23:19:44 +0000944#endif
945
Wolfgang Denk265817c2005-09-25 00:53:22 +0200946 hw_p->tx_err_index = 0; /* Transmit Error Index for tx_err_log */
947 hw_p->rx_err_index = 0; /* Receive Error Index for rx_err_log */
wdenkba56f622004-02-06 23:19:44 +0000948
949 hw_p->rx_slot = 0; /* MAL Receive Slot */
950 hw_p->rx_i_index = 0; /* Receive Interrupt Queue Index */
951 hw_p->rx_u_index = 0; /* Receive User Queue Index */
952
953 hw_p->tx_slot = 0; /* MAL Transmit Slot */
954 hw_p->tx_i_index = 0; /* Transmit Interrupt Queue Index */
955 hw_p->tx_u_index = 0; /* Transmit User Queue Index */
956
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200957#if defined(CONFIG_440) && !defined(CONFIG_440SP) && !defined(CONFIG_440SPE)
wdenkba56f622004-02-06 23:19:44 +0000958 /* set RMII mode */
959 /* NOTE: 440GX spec states that mode is mutually exclusive */
960 /* NOTE: Therefore, disable all other EMACS, since we handle */
961 /* NOTE: only one emac at a time */
962 reg = 0;
Stefan Roese2d834762007-10-23 14:03:17 +0200963 out_be32((void *)ZMII_FER, 0);
wdenkba56f622004-02-06 23:19:44 +0000964 udelay (100);
wdenkba56f622004-02-06 23:19:44 +0000965
Stefan Roese8ac41e32008-03-11 15:05:26 +0100966#if defined(CONFIG_440GP) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
Stefan Roese2d834762007-10-23 14:03:17 +0200967 out_be32((void *)ZMII_FER, (ZMII_FER_RMII | ZMII_FER_MDI) << ZMII_FER_V (devnum));
Stefan Roese8ac41e32008-03-11 15:05:26 +0100968#elif defined(CONFIG_440GX) || \
969 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
970 defined(CONFIG_460EX) || defined(CONFIG_460GT)
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200971 ethgroup = ppc_4xx_eth_setup_bridge(devnum, bis);
wdenk0e6d7982004-03-14 00:07:33 +0000972#endif
Stefan Roesec57c7982005-08-11 17:56:56 +0200973
Stefan Roese2d834762007-10-23 14:03:17 +0200974 out_be32((void *)ZMII_SSR, ZMII_SSR_SP << ZMII_SSR_V(devnum));
Stefan Roese6e7fb6e2005-11-29 18:18:21 +0100975#endif /* defined(CONFIG_440) && !defined(CONFIG_440SP) */
Stefan Roesedbbd1252007-10-05 17:10:59 +0200976#if defined(CONFIG_405EX)
977 ethgroup = ppc_4xx_eth_setup_bridge(devnum, bis);
978#endif
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200979
Stefan Roese8ac41e32008-03-11 15:05:26 +0100980 sync();
wdenk0e6d7982004-03-14 00:07:33 +0000981
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200982 /* provide clocks for EMAC internal loopback */
Stefan Roese8ac41e32008-03-11 15:05:26 +0100983 emac_loopback_enable(hw_p);
wdenk0e6d7982004-03-14 00:07:33 +0000984
Stefan Roese8ac41e32008-03-11 15:05:26 +0100985 /* EMAC RESET */
Stefan Roese2d834762007-10-23 14:03:17 +0200986 out_be32((void *)EMAC_M0 + hw_p->hw_addr, EMAC_M0_SRST);
wdenkba56f622004-02-06 23:19:44 +0000987
Stefan Roese8ac41e32008-03-11 15:05:26 +0100988 /* remove clocks for EMAC internal loopback */
989 emac_loopback_disable(hw_p);
990
wdenkba56f622004-02-06 23:19:44 +0000991 failsafe = 1000;
Stefan Roese2d834762007-10-23 14:03:17 +0200992 while ((in_be32((void *)EMAC_M0 + hw_p->hw_addr) & (EMAC_M0_SRST)) && failsafe) {
wdenkba56f622004-02-06 23:19:44 +0000993 udelay (1000);
994 failsafe--;
995 }
Stefan Roese887e2ec2006-09-07 11:51:23 +0200996 if (failsafe <= 0)
997 printf("\nProblem resetting EMAC!\n");
wdenkba56f622004-02-06 23:19:44 +0000998
Stefan Roese887e2ec2006-09-07 11:51:23 +0200999#if defined(CONFIG_440GX) || \
1000 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +02001001 defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
Stefan Roese8ac41e32008-03-11 15:05:26 +01001002 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +02001003 defined(CONFIG_405EX)
wdenkba56f622004-02-06 23:19:44 +00001004 /* Whack the M1 register */
1005 mode_reg = 0x0;
1006 mode_reg &= ~0x00000038;
1007 if (sysinfo.freqOPB <= 50000000);
1008 else if (sysinfo.freqOPB <= 66666667)
1009 mode_reg |= EMAC_M1_OBCI_66;
1010 else if (sysinfo.freqOPB <= 83333333)
1011 mode_reg |= EMAC_M1_OBCI_83;
1012 else if (sysinfo.freqOPB <= 100000000)
1013 mode_reg |= EMAC_M1_OBCI_100;
1014 else
1015 mode_reg |= EMAC_M1_OBCI_GT100;
1016
Stefan Roese2d834762007-10-23 14:03:17 +02001017 out_be32((void *)EMAC_M1 + hw_p->hw_addr, mode_reg);
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001018#endif /* defined(CONFIG_440GX) || defined(CONFIG_440SP) */
wdenkba56f622004-02-06 23:19:44 +00001019
Victor Gallardo78d78232008-09-04 23:49:36 -07001020#if defined(CONFIG_GPCS_PHY_ADDR) || defined(CONFIG_GPCS_PHY1_ADDR) || \
1021 defined(CONFIG_GPCS_PHY2_ADDR) || defined(CONFIG_GPCS_PHY3_ADDR)
1022 if (bis->bi_phymode[devnum] == BI_PHYMODE_SGMII) {
1023 /*
1024 * In SGMII mode, GPCS access is needed for
1025 * communication with the internal SGMII SerDes.
1026 */
1027 switch (devnum) {
1028#if defined(CONFIG_GPCS_PHY_ADDR)
1029 case 0:
1030 reg = CONFIG_GPCS_PHY_ADDR;
1031 break;
1032#endif
1033#if defined(CONFIG_GPCS_PHY1_ADDR)
1034 case 1:
1035 reg = CONFIG_GPCS_PHY1_ADDR;
1036 break;
1037#endif
1038#if defined(CONFIG_GPCS_PHY2_ADDR)
1039 case 2:
1040 reg = CONFIG_GPCS_PHY2_ADDR;
1041 break;
1042#endif
1043#if defined(CONFIG_GPCS_PHY3_ADDR)
1044 case 3:
1045 reg = CONFIG_GPCS_PHY3_ADDR;
1046 break;
1047#endif
1048 }
1049
1050 mode_reg = in_be32((void *)EMAC_M1 + hw_p->hw_addr);
1051 mode_reg |= EMAC_M1_MF_1000GPCS | EMAC_M1_IPPA_SET(reg);
1052 out_be32((void *)EMAC_M1 + hw_p->hw_addr, mode_reg);
1053
1054 /* Configure GPCS interface to recommended setting for SGMII */
1055 miiphy_reset(dev->name, reg);
1056 miiphy_write(dev->name, reg, 0x04, 0x8120); /* AsymPause, FDX */
1057 miiphy_write(dev->name, reg, 0x07, 0x2801); /* msg_pg, toggle */
1058 miiphy_write(dev->name, reg, 0x00, 0x0140); /* 1Gbps, FDX */
1059 }
1060#endif /* defined(CONFIG_GPCS_PHY_ADDR) */
1061
wdenkba56f622004-02-06 23:19:44 +00001062 /* wait for PHY to complete auto negotiation */
1063 reg_short = 0;
wdenkba56f622004-02-06 23:19:44 +00001064 switch (devnum) {
1065 case 0:
1066 reg = CONFIG_PHY_ADDR;
1067 break;
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001068#if defined (CONFIG_PHY1_ADDR)
wdenkba56f622004-02-06 23:19:44 +00001069 case 1:
1070 reg = CONFIG_PHY1_ADDR;
1071 break;
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001072#endif
Stefan Roese4c9e8552008-03-19 16:20:49 +01001073#if defined (CONFIG_PHY2_ADDR)
wdenkba56f622004-02-06 23:19:44 +00001074 case 2:
1075 reg = CONFIG_PHY2_ADDR;
1076 break;
Stefan Roese4c9e8552008-03-19 16:20:49 +01001077#endif
1078#if defined (CONFIG_PHY3_ADDR)
wdenkba56f622004-02-06 23:19:44 +00001079 case 3:
1080 reg = CONFIG_PHY3_ADDR;
1081 break;
1082#endif
1083 default:
1084 reg = CONFIG_PHY_ADDR;
1085 break;
1086 }
1087
wdenk3c74e322004-02-22 23:46:08 +00001088 bis->bi_phynum[devnum] = reg;
1089
Victor Gallardo78d78232008-09-04 23:49:36 -07001090 if (reg == CONFIG_FIXED_PHY)
1091 goto get_speed;
1092
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001093#if defined(CONFIG_PHY_RESET)
wdenka06752e2004-09-29 22:43:59 +00001094 /*
1095 * Reset the phy, only if its the first time through
1096 * otherwise, just check the speeds & feeds
1097 */
1098 if (hw_p->first_init == 0) {
Stefan Roeseec0c2ec2006-11-27 14:46:06 +01001099#if defined(CONFIG_M88E1111_PHY)
Stefan Roese887e2ec2006-09-07 11:51:23 +02001100 miiphy_write (dev->name, reg, 0x14, 0x0ce3);
1101 miiphy_write (dev->name, reg, 0x18, 0x4101);
1102 miiphy_write (dev->name, reg, 0x09, 0x0e00);
1103 miiphy_write (dev->name, reg, 0x04, 0x01e1);
1104#endif
Victor Gallardo78d78232008-09-04 23:49:36 -07001105#if defined(CONFIG_M88E1112_PHY)
1106 if (bis->bi_phymode[devnum] == BI_PHYMODE_SGMII) {
1107 /*
1108 * Marvell 88E1112 PHY needs to have the SGMII MAC
1109 * interace (page 2) properly configured to
1110 * communicate with the 460EX/GT GPCS interface.
1111 */
1112
1113 /* Set access to Page 2 */
1114 miiphy_write(dev->name, reg, 0x16, 0x0002);
1115
1116 miiphy_write(dev->name, reg, 0x00, 0x0040); /* 1Gbps */
1117 miiphy_read(dev->name, reg, 0x1a, &reg_short);
1118 reg_short |= 0x8000; /* bypass Auto-Negotiation */
1119 miiphy_write(dev->name, reg, 0x1a, reg_short);
1120 miiphy_reset(dev->name, reg); /* reset MAC interface */
1121
1122 /* Reset access to Page 0 */
1123 miiphy_write(dev->name, reg, 0x16, 0x0000);
1124 }
1125#endif /* defined(CONFIG_M88E1112_PHY) */
Marian Balakowicz63ff0042005-10-28 22:30:33 +02001126 miiphy_reset (dev->name, reg);
wdenkba56f622004-02-06 23:19:44 +00001127
Stefan Roese887e2ec2006-09-07 11:51:23 +02001128#if defined(CONFIG_440GX) || \
1129 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +02001130 defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
Stefan Roese8ac41e32008-03-11 15:05:26 +01001131 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +02001132 defined(CONFIG_405EX)
Stefan Roese887e2ec2006-09-07 11:51:23 +02001133
wdenk0e6d7982004-03-14 00:07:33 +00001134#if defined(CONFIG_CIS8201_PHY)
wdenkfc1cfcd2004-04-25 15:41:35 +00001135 /*
Stefan Roese17f50f222005-08-04 17:09:16 +02001136 * Cicada 8201 PHY needs to have an extended register whacked
1137 * for RGMII mode.
wdenkfc1cfcd2004-04-25 15:41:35 +00001138 */
Stefan Roese887e2ec2006-09-07 11:51:23 +02001139 if (((devnum == 2) || (devnum == 3)) && (4 == ethgroup)) {
Stefan Roeseb79316f2005-08-15 12:31:23 +02001140#if defined(CONFIG_CIS8201_SHORT_ETCH)
Marian Balakowicz63ff0042005-10-28 22:30:33 +02001141 miiphy_write (dev->name, reg, 23, 0x1300);
Stefan Roeseb79316f2005-08-15 12:31:23 +02001142#else
Marian Balakowicz63ff0042005-10-28 22:30:33 +02001143 miiphy_write (dev->name, reg, 23, 0x1000);
Stefan Roeseb79316f2005-08-15 12:31:23 +02001144#endif
Stefan Roese17f50f222005-08-04 17:09:16 +02001145 /*
1146 * Vitesse VSC8201/Cicada CIS8201 errata:
1147 * Interoperability problem with Intel 82547EI phys
1148 * This work around (provided by Vitesse) changes
1149 * the default timer convergence from 8ms to 12ms
1150 */
Marian Balakowicz63ff0042005-10-28 22:30:33 +02001151 miiphy_write (dev->name, reg, 0x1f, 0x2a30);
1152 miiphy_write (dev->name, reg, 0x08, 0x0200);
1153 miiphy_write (dev->name, reg, 0x1f, 0x52b5);
1154 miiphy_write (dev->name, reg, 0x02, 0x0004);
1155 miiphy_write (dev->name, reg, 0x01, 0x0671);
1156 miiphy_write (dev->name, reg, 0x00, 0x8fae);
1157 miiphy_write (dev->name, reg, 0x1f, 0x2a30);
1158 miiphy_write (dev->name, reg, 0x08, 0x0000);
1159 miiphy_write (dev->name, reg, 0x1f, 0x0000);
Stefan Roese17f50f222005-08-04 17:09:16 +02001160 /* end Vitesse/Cicada errata */
1161 }
Stefan Roese6ca86462008-09-05 14:11:40 +02001162#endif /* defined(CONFIG_CIS8201_PHY) */
Stefan Roese5fb692c2007-01-18 10:25:34 +01001163
1164#if defined(CONFIG_ET1011C_PHY)
1165 /*
1166 * Agere ET1011c PHY needs to have an extended register whacked
1167 * for RGMII mode.
1168 */
1169 if (((devnum == 2) || (devnum ==3)) && (4 == ethgroup)) {
1170 miiphy_read (dev->name, reg, 0x16, &reg_short);
1171 reg_short &= ~(0x7);
1172 reg_short |= 0x6; /* RGMII DLL Delay*/
1173 miiphy_write (dev->name, reg, 0x16, reg_short);
1174
1175 miiphy_read (dev->name, reg, 0x17, &reg_short);
1176 reg_short &= ~(0x40);
1177 miiphy_write (dev->name, reg, 0x17, reg_short);
1178
1179 miiphy_write(dev->name, reg, 0x1c, 0x74f0);
1180 }
Stefan Roese6ca86462008-09-05 14:11:40 +02001181#endif /* defined(CONFIG_ET1011C_PHY) */
Stefan Roese5fb692c2007-01-18 10:25:34 +01001182
Stefan Roese6ca86462008-09-05 14:11:40 +02001183#endif /* defined(CONFIG_440GX) ... */
wdenka06752e2004-09-29 22:43:59 +00001184 /* Start/Restart autonegotiation */
Marian Balakowicz63ff0042005-10-28 22:30:33 +02001185 phy_setup_aneg (dev->name, reg);
wdenka06752e2004-09-29 22:43:59 +00001186 udelay (1000);
1187 }
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001188#endif /* defined(CONFIG_PHY_RESET) */
wdenkba56f622004-02-06 23:19:44 +00001189
Marian Balakowicz63ff0042005-10-28 22:30:33 +02001190 miiphy_read (dev->name, reg, PHY_BMSR, &reg_short);
wdenkba56f622004-02-06 23:19:44 +00001191
1192 /*
wdenk0e6d7982004-03-14 00:07:33 +00001193 * Wait if PHY is capable of autonegotiation and autonegotiation is not complete
wdenkba56f622004-02-06 23:19:44 +00001194 */
1195 if ((reg_short & PHY_BMSR_AUTN_ABLE)
1196 && !(reg_short & PHY_BMSR_AUTN_COMP)) {
1197 puts ("Waiting for PHY auto negotiation to complete");
1198 i = 0;
1199 while (!(reg_short & PHY_BMSR_AUTN_COMP)) {
1200 /*
1201 * Timeout reached ?
1202 */
1203 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
1204 puts (" TIMEOUT !\n");
1205 break;
1206 }
1207
1208 if ((i++ % 1000) == 0) {
1209 putc ('.');
1210 }
1211 udelay (1000); /* 1 ms */
Marian Balakowicz63ff0042005-10-28 22:30:33 +02001212 miiphy_read (dev->name, reg, PHY_BMSR, &reg_short);
wdenkba56f622004-02-06 23:19:44 +00001213 }
1214 puts (" done\n");
1215 udelay (500000); /* another 500 ms (results in faster booting) */
1216 }
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001217
Victor Gallardo78d78232008-09-04 23:49:36 -07001218get_speed:
1219 if (reg == CONFIG_FIXED_PHY) {
1220 for (i = 0; i < ARRAY_SIZE(fixed_phy_port); i++) {
1221 if (devnum == fixed_phy_port[i].devnum) {
1222 speed = fixed_phy_port[i].speed;
1223 duplex = fixed_phy_port[i].duplex;
1224 break;
1225 }
1226 }
1227
1228 if (i == ARRAY_SIZE(fixed_phy_port)) {
1229 printf("ERROR: PHY (%s) not configured correctly!\n",
1230 dev->name);
1231 return -1;
1232 }
1233 } else {
1234 speed = miiphy_speed(dev->name, reg);
1235 duplex = miiphy_duplex(dev->name, reg);
1236 }
wdenkba56f622004-02-06 23:19:44 +00001237
1238 if (hw_p->print_speed) {
1239 hw_p->print_speed = 0;
Stefan Roese5fb692c2007-01-18 10:25:34 +01001240 printf ("ENET Speed is %d Mbps - %s duplex connection (EMAC%d)\n",
1241 (int) speed, (duplex == HALF) ? "HALF" : "FULL",
1242 hw_p->devnum);
wdenkba56f622004-02-06 23:19:44 +00001243 }
1244
Stefan Roese8ac41e32008-03-11 15:05:26 +01001245#if defined(CONFIG_440) && \
1246 !defined(CONFIG_440SP) && !defined(CONFIG_440SPE) && \
1247 !defined(CONFIG_440EPX) && !defined(CONFIG_440GRX) && \
1248 !defined(CONFIG_460EX) && !defined(CONFIG_460GT)
Stefan Roese846b0dd2005-08-08 12:42:22 +02001249#if defined(CONFIG_440EP) || defined(CONFIG_440GR)
Stefan Roesec157d8e2005-08-01 16:41:48 +02001250 mfsdr(sdr_mfr, reg);
1251 if (speed == 100) {
1252 reg = (reg & ~SDR0_MFR_ZMII_MODE_MASK) | SDR0_MFR_ZMII_MODE_RMII_100M;
1253 } else {
1254 reg = (reg & ~SDR0_MFR_ZMII_MODE_MASK) | SDR0_MFR_ZMII_MODE_RMII_10M;
1255 }
1256 mtsdr(sdr_mfr, reg);
1257#endif
Stefan Roesec57c7982005-08-11 17:56:56 +02001258
wdenkba56f622004-02-06 23:19:44 +00001259 /* Set ZMII/RGMII speed according to the phy link speed */
Stefan Roeseff768cb2007-10-31 18:01:24 +01001260 reg = in_be32((void *)ZMII_SSR);
wdenk855a4962004-03-14 18:23:55 +00001261 if ( (speed == 100) || (speed == 1000) )
Stefan Roeseff768cb2007-10-31 18:01:24 +01001262 out_be32((void *)ZMII_SSR, reg | (ZMII_SSR_SP << ZMII_SSR_V (devnum)));
wdenkba56f622004-02-06 23:19:44 +00001263 else
Stefan Roeseff768cb2007-10-31 18:01:24 +01001264 out_be32((void *)ZMII_SSR, reg & (~(ZMII_SSR_SP << ZMII_SSR_V (devnum))));
wdenkba56f622004-02-06 23:19:44 +00001265
1266 if ((devnum == 2) || (devnum == 3)) {
1267 if (speed == 1000)
1268 reg = (RGMII_SSR_SP_1000MBPS << RGMII_SSR_V (devnum));
1269 else if (speed == 100)
1270 reg = (RGMII_SSR_SP_100MBPS << RGMII_SSR_V (devnum));
Stefan Roese887e2ec2006-09-07 11:51:23 +02001271 else if (speed == 10)
wdenkba56f622004-02-06 23:19:44 +00001272 reg = (RGMII_SSR_SP_10MBPS << RGMII_SSR_V (devnum));
Stefan Roese887e2ec2006-09-07 11:51:23 +02001273 else {
1274 printf("Error in RGMII Speed\n");
1275 return -1;
1276 }
Stefan Roeseff768cb2007-10-31 18:01:24 +01001277 out_be32((void *)RGMII_SSR, reg);
wdenkba56f622004-02-06 23:19:44 +00001278 }
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001279#endif /* defined(CONFIG_440) && !defined(CONFIG_440SP) */
wdenkba56f622004-02-06 23:19:44 +00001280
Stefan Roesedbbd1252007-10-05 17:10:59 +02001281#if defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roese8ac41e32008-03-11 15:05:26 +01001282 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +02001283 defined(CONFIG_405EX)
Stefan Roesee54ec0f2008-04-03 14:50:34 +02001284 if (devnum >= 2)
1285 rgmii_channel = devnum - 2;
1286 else
1287 rgmii_channel = devnum;
1288
Stefan Roese887e2ec2006-09-07 11:51:23 +02001289 if (speed == 1000)
Stefan Roesee54ec0f2008-04-03 14:50:34 +02001290 reg = (RGMII_SSR_SP_1000MBPS << RGMII_SSR_V(rgmii_channel));
Stefan Roese887e2ec2006-09-07 11:51:23 +02001291 else if (speed == 100)
Stefan Roesee54ec0f2008-04-03 14:50:34 +02001292 reg = (RGMII_SSR_SP_100MBPS << RGMII_SSR_V(rgmii_channel));
Stefan Roese887e2ec2006-09-07 11:51:23 +02001293 else if (speed == 10)
Stefan Roesee54ec0f2008-04-03 14:50:34 +02001294 reg = (RGMII_SSR_SP_10MBPS << RGMII_SSR_V(rgmii_channel));
Stefan Roese887e2ec2006-09-07 11:51:23 +02001295 else {
1296 printf("Error in RGMII Speed\n");
1297 return -1;
1298 }
Stefan Roese2d834762007-10-23 14:03:17 +02001299 out_be32((void *)RGMII_SSR, reg);
Stefan Roese8ac41e32008-03-11 15:05:26 +01001300#if defined(CONFIG_460GT)
1301 if ((devnum == 2) || (devnum == 3))
1302 out_be32((void *)RGMII_SSR + RGMII1_BASE_OFFSET, reg);
1303#endif
Stefan Roese887e2ec2006-09-07 11:51:23 +02001304#endif
1305
wdenkba56f622004-02-06 23:19:44 +00001306 /* set the Mal configuration reg */
Stefan Roese887e2ec2006-09-07 11:51:23 +02001307#if defined(CONFIG_440GX) || \
1308 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +02001309 defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
Stefan Roese8ac41e32008-03-11 15:05:26 +01001310 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +02001311 defined(CONFIG_405EX)
Stefan Roese17f50f222005-08-04 17:09:16 +02001312 mtdcr (malmcr, MAL_CR_PLBB | MAL_CR_OPBBL | MAL_CR_LEA |
1313 MAL_CR_PLBLT_DEFAULT | MAL_CR_EOPIE | 0x00330000);
1314#else
1315 mtdcr (malmcr, MAL_CR_PLBB | MAL_CR_OPBBL | MAL_CR_LEA | MAL_CR_PLBLT_DEFAULT);
wdenkba56f622004-02-06 23:19:44 +00001316 /* Errata 1.12: MAL_1 -- Disable MAL bursting */
Stefan Roese17f50f222005-08-04 17:09:16 +02001317 if (get_pvr() == PVR_440GP_RB) {
1318 mtdcr (malmcr, mfdcr(malmcr) & ~MAL_CR_PLBB);
1319 }
1320#endif
wdenkba56f622004-02-06 23:19:44 +00001321
wdenkba56f622004-02-06 23:19:44 +00001322 /*
1323 * Malloc MAL buffer desciptors, make sure they are
1324 * aligned on cache line boundary size
1325 * (401/403/IOP480 = 16, 405 = 32)
1326 * and doesn't cross cache block boundaries.
1327 */
Stefan Roeseff768cb2007-10-31 18:01:24 +01001328 if (hw_p->first_init == 0) {
1329 debug("*** Allocating descriptor memory ***\n");
wdenkba56f622004-02-06 23:19:44 +00001330
Stefan Roeseff768cb2007-10-31 18:01:24 +01001331 bd_cached = (u32)malloc_aligned(MAL_ALLOC_SIZE, 4096);
1332 if (!bd_cached) {
Stefan Roeseb0021442008-07-10 09:58:06 +02001333 printf("%s: Error allocating MAL descriptor buffers!\n", __func__);
Stefan Roeseff768cb2007-10-31 18:01:24 +01001334 return -1;
1335 }
Stefan Roeseb79316f2005-08-15 12:31:23 +02001336
Stefan Roeseff768cb2007-10-31 18:01:24 +01001337#ifdef CONFIG_4xx_DCACHE
Matthias Fuchsba79fde2007-12-14 11:19:56 +01001338 flush_dcache_range(bd_cached, bd_cached + MAL_ALLOC_SIZE);
Anatolij Gustschin4fae35a2008-02-25 20:54:04 +01001339 if (!last_used_ea)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001340#if defined(CONFIG_SYS_MEM_TOP_HIDE)
1341 bd_uncached = bis->bi_memsize + CONFIG_SYS_MEM_TOP_HIDE;
Anatolij Gustschin5e3dca52008-04-17 18:18:00 +02001342#else
Anatolij Gustschin4fae35a2008-02-25 20:54:04 +01001343 bd_uncached = bis->bi_memsize;
Anatolij Gustschin5e3dca52008-04-17 18:18:00 +02001344#endif
Anatolij Gustschin4fae35a2008-02-25 20:54:04 +01001345 else
1346 bd_uncached = last_used_ea + MAL_ALLOC_SIZE;
1347
1348 last_used_ea = bd_uncached;
Stefan Roeseff768cb2007-10-31 18:01:24 +01001349 program_tlb(bd_cached, bd_uncached, MAL_ALLOC_SIZE,
1350 TLB_WORD2_I_ENABLE);
1351#else
1352 bd_uncached = bd_cached;
1353#endif
1354 hw_p->tx_phys = bd_cached;
1355 hw_p->rx_phys = bd_cached + MAL_TX_DESC_SIZE;
1356 hw_p->tx = (mal_desc_t *)(bd_uncached);
1357 hw_p->rx = (mal_desc_t *)(bd_uncached + MAL_TX_DESC_SIZE);
1358 debug("hw_p->tx=%08x, hw_p->rx=%08x\n", hw_p->tx, hw_p->rx);
wdenkba56f622004-02-06 23:19:44 +00001359 }
1360
1361 for (i = 0; i < NUM_TX_BUFF; i++) {
1362 hw_p->tx[i].ctrl = 0;
1363 hw_p->tx[i].data_len = 0;
Stefan Roeseff768cb2007-10-31 18:01:24 +01001364 if (hw_p->first_init == 0)
1365 hw_p->txbuf_ptr = malloc_aligned(MAL_ALLOC_SIZE,
1366 L1_CACHE_BYTES);
wdenkba56f622004-02-06 23:19:44 +00001367 hw_p->tx[i].data_ptr = hw_p->txbuf_ptr;
1368 if ((NUM_TX_BUFF - 1) == i)
1369 hw_p->tx[i].ctrl |= MAL_TX_CTRL_WRAP;
1370 hw_p->tx_run[i] = -1;
Stefan Roeseff768cb2007-10-31 18:01:24 +01001371 debug("TX_BUFF %d @ 0x%08lx\n", i, (u32)hw_p->tx[i].data_ptr);
wdenkba56f622004-02-06 23:19:44 +00001372 }
1373
1374 for (i = 0; i < NUM_RX_BUFF; i++) {
1375 hw_p->rx[i].ctrl = 0;
1376 hw_p->rx[i].data_len = 0;
Stefan Roeseff768cb2007-10-31 18:01:24 +01001377 hw_p->rx[i].data_ptr = (char *)NetRxPackets[i];
wdenkba56f622004-02-06 23:19:44 +00001378 if ((NUM_RX_BUFF - 1) == i)
1379 hw_p->rx[i].ctrl |= MAL_RX_CTRL_WRAP;
1380 hw_p->rx[i].ctrl |= MAL_RX_CTRL_EMPTY | MAL_RX_CTRL_INTR;
1381 hw_p->rx_ready[i] = -1;
Stefan Roeseff768cb2007-10-31 18:01:24 +01001382 debug("RX_BUFF %d @ 0x%08lx\n", i, (u32)hw_p->rx[i].data_ptr);
wdenkba56f622004-02-06 23:19:44 +00001383 }
1384
1385 reg = 0x00000000;
1386
1387 reg |= dev->enetaddr[0]; /* set high address */
1388 reg = reg << 8;
1389 reg |= dev->enetaddr[1];
1390
Stefan Roese2d834762007-10-23 14:03:17 +02001391 out_be32((void *)EMAC_IAH + hw_p->hw_addr, reg);
wdenkba56f622004-02-06 23:19:44 +00001392
1393 reg = 0x00000000;
1394 reg |= dev->enetaddr[2]; /* set low address */
1395 reg = reg << 8;
1396 reg |= dev->enetaddr[3];
1397 reg = reg << 8;
1398 reg |= dev->enetaddr[4];
1399 reg = reg << 8;
1400 reg |= dev->enetaddr[5];
1401
Stefan Roese2d834762007-10-23 14:03:17 +02001402 out_be32((void *)EMAC_IAL + hw_p->hw_addr, reg);
wdenkba56f622004-02-06 23:19:44 +00001403
1404 switch (devnum) {
1405 case 1:
1406 /* setup MAL tx & rx channel pointers */
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001407#if defined (CONFIG_405EP) || defined (CONFIG_440EP) || defined (CONFIG_440GR)
Stefan Roeseff768cb2007-10-31 18:01:24 +01001408 mtdcr (maltxctp2r, hw_p->tx_phys);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001409#else
Stefan Roeseff768cb2007-10-31 18:01:24 +01001410 mtdcr (maltxctp1r, hw_p->tx_phys);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001411#endif
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001412#if defined(CONFIG_440)
Stefan Roesec157d8e2005-08-01 16:41:48 +02001413 mtdcr (maltxbattr, 0x0);
wdenkba56f622004-02-06 23:19:44 +00001414 mtdcr (malrxbattr, 0x0);
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001415#endif
Stefan Roese8ac41e32008-03-11 15:05:26 +01001416
1417#if defined(CONFIG_460EX) || defined(CONFIG_460GT)
Stefan Roese4c9e8552008-03-19 16:20:49 +01001418 mtdcr (malrxctp8r, hw_p->rx_phys);
Stefan Roese8ac41e32008-03-11 15:05:26 +01001419 /* set RX buffer size */
1420 mtdcr (malrcbs8, ENET_MAX_MTU_ALIGNED / 16);
1421#else
Stefan Roeseff768cb2007-10-31 18:01:24 +01001422 mtdcr (malrxctp1r, hw_p->rx_phys);
wdenkba56f622004-02-06 23:19:44 +00001423 /* set RX buffer size */
1424 mtdcr (malrcbs1, ENET_MAX_MTU_ALIGNED / 16);
Stefan Roese8ac41e32008-03-11 15:05:26 +01001425#endif
wdenkba56f622004-02-06 23:19:44 +00001426 break;
Stefan Roese846b0dd2005-08-08 12:42:22 +02001427#if defined (CONFIG_440GX)
wdenkba56f622004-02-06 23:19:44 +00001428 case 2:
1429 /* setup MAL tx & rx channel pointers */
1430 mtdcr (maltxbattr, 0x0);
wdenkba56f622004-02-06 23:19:44 +00001431 mtdcr (malrxbattr, 0x0);
Stefan Roeseff768cb2007-10-31 18:01:24 +01001432 mtdcr (maltxctp2r, hw_p->tx_phys);
1433 mtdcr (malrxctp2r, hw_p->rx_phys);
wdenkba56f622004-02-06 23:19:44 +00001434 /* set RX buffer size */
1435 mtdcr (malrcbs2, ENET_MAX_MTU_ALIGNED / 16);
1436 break;
1437 case 3:
1438 /* setup MAL tx & rx channel pointers */
1439 mtdcr (maltxbattr, 0x0);
Stefan Roeseff768cb2007-10-31 18:01:24 +01001440 mtdcr (maltxctp3r, hw_p->tx_phys);
wdenkba56f622004-02-06 23:19:44 +00001441 mtdcr (malrxbattr, 0x0);
Stefan Roeseff768cb2007-10-31 18:01:24 +01001442 mtdcr (malrxctp3r, hw_p->rx_phys);
wdenkba56f622004-02-06 23:19:44 +00001443 /* set RX buffer size */
1444 mtdcr (malrcbs3, ENET_MAX_MTU_ALIGNED / 16);
1445 break;
Stefan Roesec57c7982005-08-11 17:56:56 +02001446#endif /* CONFIG_440GX */
Stefan Roese4c9e8552008-03-19 16:20:49 +01001447#if defined (CONFIG_460GT)
1448 case 2:
1449 /* setup MAL tx & rx channel pointers */
1450 mtdcr (maltxbattr, 0x0);
1451 mtdcr (malrxbattr, 0x0);
1452 mtdcr (maltxctp2r, hw_p->tx_phys);
1453 mtdcr (malrxctp16r, hw_p->rx_phys);
1454 /* set RX buffer size */
1455 mtdcr (malrcbs16, ENET_MAX_MTU_ALIGNED / 16);
1456 break;
1457 case 3:
1458 /* setup MAL tx & rx channel pointers */
1459 mtdcr (maltxbattr, 0x0);
1460 mtdcr (malrxbattr, 0x0);
1461 mtdcr (maltxctp3r, hw_p->tx_phys);
1462 mtdcr (malrxctp24r, hw_p->rx_phys);
1463 /* set RX buffer size */
1464 mtdcr (malrcbs24, ENET_MAX_MTU_ALIGNED / 16);
1465 break;
1466#endif /* CONFIG_460GT */
wdenkba56f622004-02-06 23:19:44 +00001467 case 0:
1468 default:
1469 /* setup MAL tx & rx channel pointers */
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001470#if defined(CONFIG_440)
wdenkba56f622004-02-06 23:19:44 +00001471 mtdcr (maltxbattr, 0x0);
wdenkba56f622004-02-06 23:19:44 +00001472 mtdcr (malrxbattr, 0x0);
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001473#endif
Stefan Roeseff768cb2007-10-31 18:01:24 +01001474 mtdcr (maltxctp0r, hw_p->tx_phys);
1475 mtdcr (malrxctp0r, hw_p->rx_phys);
wdenkba56f622004-02-06 23:19:44 +00001476 /* set RX buffer size */
1477 mtdcr (malrcbs0, ENET_MAX_MTU_ALIGNED / 16);
1478 break;
1479 }
1480
1481 /* Enable MAL transmit and receive channels */
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001482#if defined(CONFIG_405EP) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
Stefan Roesec157d8e2005-08-01 16:41:48 +02001483 mtdcr (maltxcasr, (MAL_TXRX_CASR >> (hw_p->devnum*2)));
1484#else
wdenkba56f622004-02-06 23:19:44 +00001485 mtdcr (maltxcasr, (MAL_TXRX_CASR >> hw_p->devnum));
Stefan Roesec157d8e2005-08-01 16:41:48 +02001486#endif
wdenkba56f622004-02-06 23:19:44 +00001487 mtdcr (malrxcasr, (MAL_TXRX_CASR >> hw_p->devnum));
1488
1489 /* set transmit enable & receive enable */
Stefan Roese2d834762007-10-23 14:03:17 +02001490 out_be32((void *)EMAC_M0 + hw_p->hw_addr, EMAC_M0_TXE | EMAC_M0_RXE);
wdenkba56f622004-02-06 23:19:44 +00001491
Stefan Roese2d834762007-10-23 14:03:17 +02001492 mode_reg = in_be32((void *)EMAC_M1 + hw_p->hw_addr);
Stefan Roese76957cb2008-03-01 12:11:40 +01001493
1494 /* set rx-/tx-fifo size */
1495 mode_reg = (mode_reg & ~EMAC_MR1_FIFO_MASK) | EMAC_MR1_FIFO_SIZE;
wdenkba56f622004-02-06 23:19:44 +00001496
1497 /* set speed */
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001498 if (speed == _1000BASET) {
Stefan Roese738815c2007-10-02 11:44:46 +02001499#if defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
1500 defined(CONFIG_440SP) || defined(CONFIG_440SPE)
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001501 unsigned long pfc1;
Stefan Roese887e2ec2006-09-07 11:51:23 +02001502
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001503 mfsdr (sdr_pfc1, pfc1);
1504 pfc1 |= SDR0_PFC1_EM_1000;
1505 mtsdr (sdr_pfc1, pfc1);
1506#endif
wdenk855a4962004-03-14 18:23:55 +00001507 mode_reg = mode_reg | EMAC_M1_MF_1000MBPS | EMAC_M1_IST;
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001508 } else if (speed == _100BASET)
wdenkba56f622004-02-06 23:19:44 +00001509 mode_reg = mode_reg | EMAC_M1_MF_100MBPS | EMAC_M1_IST;
1510 else
1511 mode_reg = mode_reg & ~0x00C00000; /* 10 MBPS */
1512 if (duplex == FULL)
1513 mode_reg = mode_reg | 0x80000000 | EMAC_M1_IST;
1514
Stefan Roese2d834762007-10-23 14:03:17 +02001515 out_be32((void *)EMAC_M1 + hw_p->hw_addr, mode_reg);
wdenkba56f622004-02-06 23:19:44 +00001516
1517 /* Enable broadcast and indvidual address */
1518 /* TBS: enabling runts as some misbehaved nics will send runts */
Stefan Roese2d834762007-10-23 14:03:17 +02001519 out_be32((void *)EMAC_RXM + hw_p->hw_addr, EMAC_RMR_BAE | EMAC_RMR_IAE);
wdenkba56f622004-02-06 23:19:44 +00001520
1521 /* we probably need to set the tx mode1 reg? maybe at tx time */
1522
1523 /* set transmit request threshold register */
Stefan Roese2d834762007-10-23 14:03:17 +02001524 out_be32((void *)EMAC_TRTR + hw_p->hw_addr, 0x18000000); /* 256 byte threshold */
wdenkba56f622004-02-06 23:19:44 +00001525
Wolfgang Denk265817c2005-09-25 00:53:22 +02001526 /* set receive low/high water mark register */
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001527#if defined(CONFIG_440)
Marian Balakowicz6c5879f2006-06-30 16:30:46 +02001528 /* 440s has a 64 byte burst length */
Stefan Roese2d834762007-10-23 14:03:17 +02001529 out_be32((void *)EMAC_RX_HI_LO_WMARK + hw_p->hw_addr, 0x80009000);
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001530#else
1531 /* 405s have a 16 byte burst length */
Stefan Roese2d834762007-10-23 14:03:17 +02001532 out_be32((void *)EMAC_RX_HI_LO_WMARK + hw_p->hw_addr, 0x0f002000);
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001533#endif /* defined(CONFIG_440) */
Stefan Roese2d834762007-10-23 14:03:17 +02001534 out_be32((void *)EMAC_TXM1 + hw_p->hw_addr, 0xf8640000);
wdenkba56f622004-02-06 23:19:44 +00001535
1536 /* Set fifo limit entry in tx mode 0 */
Stefan Roese2d834762007-10-23 14:03:17 +02001537 out_be32((void *)EMAC_TXM0 + hw_p->hw_addr, 0x00000003);
wdenkba56f622004-02-06 23:19:44 +00001538 /* Frame gap set */
Stefan Roese2d834762007-10-23 14:03:17 +02001539 out_be32((void *)EMAC_I_FRAME_GAP_REG + hw_p->hw_addr, 0x00000008);
wdenkba56f622004-02-06 23:19:44 +00001540
1541 /* Set EMAC IER */
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001542 hw_p->emac_ier = EMAC_ISR_PTLE | EMAC_ISR_BFCS | EMAC_ISR_ORE | EMAC_ISR_IRE;
wdenkba56f622004-02-06 23:19:44 +00001543 if (speed == _100BASET)
1544 hw_p->emac_ier = hw_p->emac_ier | EMAC_ISR_SYE;
1545
Stefan Roese2d834762007-10-23 14:03:17 +02001546 out_be32((void *)EMAC_ISR + hw_p->hw_addr, 0xffffffff); /* clear pending interrupts */
1547 out_be32((void *)EMAC_IER + hw_p->hw_addr, hw_p->emac_ier);
wdenkba56f622004-02-06 23:19:44 +00001548
1549 if (hw_p->first_init == 0) {
1550 /*
1551 * Connect interrupt service routines
1552 */
Stefan Roesedbbd1252007-10-05 17:10:59 +02001553 irq_install_handler(ETH_IRQ_NUM(hw_p->devnum),
1554 (interrupt_handler_t *) enetInt, dev);
wdenkba56f622004-02-06 23:19:44 +00001555 }
wdenkba56f622004-02-06 23:19:44 +00001556
1557 mtmsr (msr); /* enable interrupts again */
1558
1559 hw_p->bis = bis;
1560 hw_p->first_init = 1;
1561
Stefan Roese802b7692008-01-08 18:39:30 +01001562 return 0;
wdenkba56f622004-02-06 23:19:44 +00001563}
1564
1565
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001566static int ppc_4xx_eth_send (struct eth_device *dev, volatile void *ptr,
wdenkba56f622004-02-06 23:19:44 +00001567 int len)
1568{
1569 struct enet_frame *ef_ptr;
1570 ulong time_start, time_now;
1571 unsigned long temp_txm0;
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001572 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenkba56f622004-02-06 23:19:44 +00001573
1574 ef_ptr = (struct enet_frame *) ptr;
1575
1576 /*-----------------------------------------------------------------------+
1577 * Copy in our address into the frame.
1578 *-----------------------------------------------------------------------*/
1579 (void) memcpy (ef_ptr->source_addr, dev->enetaddr, ENET_ADDR_LENGTH);
1580
1581 /*-----------------------------------------------------------------------+
1582 * If frame is too long or too short, modify length.
1583 *-----------------------------------------------------------------------*/
1584 /* TBS: where does the fragment go???? */
1585 if (len > ENET_MAX_MTU)
1586 len = ENET_MAX_MTU;
1587
1588 /* memcpy ((void *) &tx_buff[tx_slot], (const void *) ptr, len); */
1589 memcpy ((void *) hw_p->txbuf_ptr, (const void *) ptr, len);
Matthias Fuchsba79fde2007-12-14 11:19:56 +01001590 flush_dcache_range((u32)hw_p->txbuf_ptr, (u32)hw_p->txbuf_ptr + len);
wdenkba56f622004-02-06 23:19:44 +00001591
1592 /*-----------------------------------------------------------------------+
1593 * set TX Buffer busy, and send it
1594 *-----------------------------------------------------------------------*/
1595 hw_p->tx[hw_p->tx_slot].ctrl = (MAL_TX_CTRL_LAST |
1596 EMAC_TX_CTRL_GFCS | EMAC_TX_CTRL_GP) &
1597 ~(EMAC_TX_CTRL_ISA | EMAC_TX_CTRL_RSA);
1598 if ((NUM_TX_BUFF - 1) == hw_p->tx_slot)
1599 hw_p->tx[hw_p->tx_slot].ctrl |= MAL_TX_CTRL_WRAP;
1600
1601 hw_p->tx[hw_p->tx_slot].data_len = (short) len;
1602 hw_p->tx[hw_p->tx_slot].ctrl |= MAL_TX_CTRL_READY;
1603
Stefan Roese8ac41e32008-03-11 15:05:26 +01001604 sync();
wdenkba56f622004-02-06 23:19:44 +00001605
Stefan Roese2d834762007-10-23 14:03:17 +02001606 out_be32((void *)EMAC_TXM0 + hw_p->hw_addr,
1607 in_be32((void *)EMAC_TXM0 + hw_p->hw_addr) | EMAC_TXM0_GNP0);
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001608#ifdef INFO_4XX_ENET
wdenkba56f622004-02-06 23:19:44 +00001609 hw_p->stats.pkts_tx++;
1610#endif
1611
1612 /*-----------------------------------------------------------------------+
1613 * poll unitl the packet is sent and then make sure it is OK
1614 *-----------------------------------------------------------------------*/
1615 time_start = get_timer (0);
1616 while (1) {
Stefan Roese2d834762007-10-23 14:03:17 +02001617 temp_txm0 = in_be32((void *)EMAC_TXM0 + hw_p->hw_addr);
wdenkba56f622004-02-06 23:19:44 +00001618 /* loop until either TINT turns on or 3 seconds elapse */
1619 if ((temp_txm0 & EMAC_TXM0_GNP0) != 0) {
1620 /* transmit is done, so now check for errors
1621 * If there is an error, an interrupt should
1622 * happen when we return
1623 */
1624 time_now = get_timer (0);
1625 if ((time_now - time_start) > 3000) {
1626 return (-1);
1627 }
1628 } else {
1629 return (len);
1630 }
1631 }
1632}
1633
wdenkba56f622004-02-06 23:19:44 +00001634int enetInt (struct eth_device *dev)
1635{
1636 int serviced;
1637 int rc = -1; /* default to not us */
Stefan Roesed1631fe2008-06-26 13:40:57 +02001638 u32 mal_isr;
1639 u32 emac_isr = 0;
1640 u32 mal_eob;
1641 u32 uic_mal;
1642 u32 uic_mal_err;
1643 u32 uic_emac;
1644 u32 uic_emac_b;
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001645 EMAC_4XX_HW_PST hw_p;
wdenkba56f622004-02-06 23:19:44 +00001646
1647 /*
1648 * Because the mal is generic, we need to get the current
1649 * eth device
1650 */
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001651#if defined(CONFIG_NET_MULTI)
1652 dev = eth_get_dev();
1653#else
1654 dev = emac0_dev;
1655#endif
wdenkba56f622004-02-06 23:19:44 +00001656
1657 hw_p = dev->priv;
1658
wdenkba56f622004-02-06 23:19:44 +00001659 /* enter loop that stays in interrupt code until nothing to service */
1660 do {
1661 serviced = 0;
1662
Stefan Roesed1631fe2008-06-26 13:40:57 +02001663 uic_mal = mfdcr(UIC_BASE_MAL + UIC_MSR);
1664 uic_mal_err = mfdcr(UIC_BASE_MAL_ERR + UIC_MSR);
1665 uic_emac = mfdcr(UIC_BASE_EMAC + UIC_MSR);
1666 uic_emac_b = mfdcr(UIC_BASE_EMAC_B + UIC_MSR);
Stefan Roese887e2ec2006-09-07 11:51:23 +02001667
Stefan Roesed1631fe2008-06-26 13:40:57 +02001668 if (!(uic_mal & (UIC_MAL_RXEOB | UIC_MAL_TXEOB))
1669 && !(uic_mal_err & (UIC_MAL_SERR | UIC_MAL_TXDE | UIC_MAL_RXDE))
1670 && !(uic_emac & UIC_ETHx) && !(uic_emac_b & UIC_ETHxB)) {
wdenkba56f622004-02-06 23:19:44 +00001671 /* not for us */
1672 return (rc);
1673 }
Stefan Roesed1631fe2008-06-26 13:40:57 +02001674
wdenkba56f622004-02-06 23:19:44 +00001675 /* get and clear controller status interrupts */
Stefan Roesed1631fe2008-06-26 13:40:57 +02001676 /* look at MAL and EMAC error interrupts */
1677 if (uic_mal_err & (UIC_MAL_SERR | UIC_MAL_TXDE | UIC_MAL_RXDE)) {
1678 /* we have a MAL error interrupt */
1679 mal_isr = mfdcr(malesr);
1680 mal_err(dev, mal_isr, uic_mal_err,
1681 MAL_UIC_DEF, MAL_UIC_ERR);
1682
1683 /* clear MAL error interrupt status bits */
1684 mtdcr(UIC_BASE_MAL_ERR + UIC_SR,
1685 UIC_MAL_SERR | UIC_MAL_TXDE | UIC_MAL_RXDE);
1686
1687 return -1;
wdenkba56f622004-02-06 23:19:44 +00001688 }
1689
Stefan Roesed1631fe2008-06-26 13:40:57 +02001690 /* look for EMAC errors */
1691 if ((uic_emac & UIC_ETHx) || (uic_emac_b & UIC_ETHxB)) {
1692 emac_isr = in_be32((void *)EMAC_ISR + hw_p->hw_addr);
1693 emac_err(dev, emac_isr);
wdenkba56f622004-02-06 23:19:44 +00001694
Stefan Roesed1631fe2008-06-26 13:40:57 +02001695 /* clear EMAC error interrupt status bits */
1696 mtdcr(UIC_BASE_EMAC + UIC_SR, UIC_ETHx);
1697 mtdcr(UIC_BASE_EMAC_B + UIC_SR, UIC_ETHxB);
wdenkba56f622004-02-06 23:19:44 +00001698
Stefan Roesed1631fe2008-06-26 13:40:57 +02001699 return -1;
wdenkba56f622004-02-06 23:19:44 +00001700 }
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001701
wdenkba56f622004-02-06 23:19:44 +00001702 /* handle MAX TX EOB interrupt from a tx */
Stefan Roesed1631fe2008-06-26 13:40:57 +02001703 if (uic_mal & UIC_MAL_TXEOB) {
1704 /* clear MAL interrupt status bits */
1705 mal_eob = mfdcr(maltxeobisr);
1706 mtdcr(maltxeobisr, mal_eob);
1707 mtdcr(UIC_BASE_MAL + UIC_SR, UIC_MAL_TXEOB);
1708
1709 /* indicate that we serviced an interrupt */
1710 serviced = 1;
1711 rc = 0;
wdenkba56f622004-02-06 23:19:44 +00001712 }
Stefan Roesed1631fe2008-06-26 13:40:57 +02001713
1714 /* handle MAL RX EOB interupt from a receive */
1715 /* check for EOB on valid channels */
1716 if (uic_mal & UIC_MAL_RXEOB) {
1717 mal_eob = mfdcr(malrxeobisr);
1718 if (mal_eob &
1719 (0x80000000 >> (hw_p->devnum * MAL_RX_CHAN_MUL))) {
1720 /* push packet to upper layer */
1721 enet_rcv(dev, emac_isr);
1722
1723 /* clear MAL interrupt status bits */
1724 mtdcr(UIC_BASE_MAL + UIC_SR, UIC_MAL_RXEOB);
1725
wdenkba56f622004-02-06 23:19:44 +00001726 /* indicate that we serviced an interrupt */
1727 serviced = 1;
1728 rc = 0;
1729 }
1730 }
wdenkba56f622004-02-06 23:19:44 +00001731 } while (serviced);
1732
1733 return (rc);
1734}
1735
1736/*-----------------------------------------------------------------------------+
1737 * MAL Error Routine
1738 *-----------------------------------------------------------------------------*/
1739static void mal_err (struct eth_device *dev, unsigned long isr,
1740 unsigned long uic, unsigned long maldef,
1741 unsigned long mal_errr)
1742{
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001743 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenkba56f622004-02-06 23:19:44 +00001744
1745 mtdcr (malesr, isr); /* clear interrupt */
1746
1747 /* clear DE interrupt */
1748 mtdcr (maltxdeir, 0xC0000000);
1749 mtdcr (malrxdeir, 0x80000000);
1750
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001751#ifdef INFO_4XX_ENET
Wolfgang Denk265817c2005-09-25 00:53:22 +02001752 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 +00001753#endif
1754
1755 eth_init (hw_p->bis); /* start again... */
1756}
1757
1758/*-----------------------------------------------------------------------------+
1759 * EMAC Error Routine
1760 *-----------------------------------------------------------------------------*/
1761static void emac_err (struct eth_device *dev, unsigned long isr)
1762{
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001763 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenkba56f622004-02-06 23:19:44 +00001764
1765 printf ("EMAC%d error occured.... ISR = %lx\n", hw_p->devnum, isr);
Stefan Roese2d834762007-10-23 14:03:17 +02001766 out_be32((void *)EMAC_ISR + hw_p->hw_addr, isr);
wdenkba56f622004-02-06 23:19:44 +00001767}
1768
1769/*-----------------------------------------------------------------------------+
1770 * enet_rcv() handles the ethernet receive data
1771 *-----------------------------------------------------------------------------*/
1772static void enet_rcv (struct eth_device *dev, unsigned long malisr)
1773{
1774 struct enet_frame *ef_ptr;
1775 unsigned long data_len;
1776 unsigned long rx_eob_isr;
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001777 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenkba56f622004-02-06 23:19:44 +00001778
1779 int handled = 0;
1780 int i;
1781 int loop_count = 0;
1782
1783 rx_eob_isr = mfdcr (malrxeobisr);
Stefan Roese8ac41e32008-03-11 15:05:26 +01001784 if ((0x80000000 >> (hw_p->devnum * MAL_RX_CHAN_MUL)) & rx_eob_isr) {
wdenkba56f622004-02-06 23:19:44 +00001785 /* clear EOB */
1786 mtdcr (malrxeobisr, rx_eob_isr);
1787
1788 /* EMAC RX done */
1789 while (1) { /* do all */
1790 i = hw_p->rx_slot;
1791
1792 if ((MAL_RX_CTRL_EMPTY & hw_p->rx[i].ctrl)
1793 || (loop_count >= NUM_RX_BUFF))
1794 break;
Stefan Roesea2e1c702007-07-12 16:32:08 +02001795
wdenkba56f622004-02-06 23:19:44 +00001796 loop_count++;
wdenkba56f622004-02-06 23:19:44 +00001797 handled++;
Stefan Roese8ac41e32008-03-11 15:05:26 +01001798 data_len = (unsigned long) hw_p->rx[i].data_len & 0x0fff; /* Get len */
wdenkba56f622004-02-06 23:19:44 +00001799 if (data_len) {
1800 if (data_len > ENET_MAX_MTU) /* Check len */
1801 data_len = 0;
1802 else {
1803 if (EMAC_RX_ERRORS & hw_p->rx[i].ctrl) { /* Check Errors */
1804 data_len = 0;
1805 hw_p->stats.rx_err_log[hw_p->
1806 rx_err_index]
1807 = hw_p->rx[i].ctrl;
1808 hw_p->rx_err_index++;
1809 if (hw_p->rx_err_index ==
1810 MAX_ERR_LOG)
1811 hw_p->rx_err_index =
1812 0;
wdenkfc1cfcd2004-04-25 15:41:35 +00001813 } /* emac_erros */
wdenkba56f622004-02-06 23:19:44 +00001814 } /* data_len < max mtu */
wdenkfc1cfcd2004-04-25 15:41:35 +00001815 } /* if data_len */
wdenkba56f622004-02-06 23:19:44 +00001816 if (!data_len) { /* no data */
1817 hw_p->rx[i].ctrl |= MAL_RX_CTRL_EMPTY; /* Free Recv Buffer */
1818
1819 hw_p->stats.data_len_err++; /* Error at Rx */
1820 }
1821
1822 /* !data_len */
1823 /* AS.HARNOIS */
1824 /* Check if user has already eaten buffer */
1825 /* if not => ERROR */
1826 else if (hw_p->rx_ready[hw_p->rx_i_index] != -1) {
1827 if (hw_p->is_receiving)
1828 printf ("ERROR : Receive buffers are full!\n");
1829 break;
1830 } else {
1831 hw_p->stats.rx_frames++;
1832 hw_p->stats.rx += data_len;
1833 ef_ptr = (struct enet_frame *) hw_p->rx[i].
1834 data_ptr;
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001835#ifdef INFO_4XX_ENET
wdenkba56f622004-02-06 23:19:44 +00001836 hw_p->stats.pkts_rx++;
1837#endif
1838 /* AS.HARNOIS
1839 * use ring buffer
1840 */
1841 hw_p->rx_ready[hw_p->rx_i_index] = i;
1842 hw_p->rx_i_index++;
1843 if (NUM_RX_BUFF == hw_p->rx_i_index)
1844 hw_p->rx_i_index = 0;
1845
Stefan Roesea2e1c702007-07-12 16:32:08 +02001846 hw_p->rx_slot++;
1847 if (NUM_RX_BUFF == hw_p->rx_slot)
1848 hw_p->rx_slot = 0;
1849
wdenkba56f622004-02-06 23:19:44 +00001850 /* AS.HARNOIS
1851 * free receive buffer only when
1852 * buffer has been handled (eth_rx)
1853 rx[i].ctrl |= MAL_RX_CTRL_EMPTY;
1854 */
1855 } /* if data_len */
1856 } /* while */
1857 } /* if EMACK_RXCHL */
1858}
1859
1860
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001861static int ppc_4xx_eth_rx (struct eth_device *dev)
wdenkba56f622004-02-06 23:19:44 +00001862{
1863 int length;
1864 int user_index;
1865 unsigned long msr;
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001866 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenkba56f622004-02-06 23:19:44 +00001867
Wolfgang Denk265817c2005-09-25 00:53:22 +02001868 hw_p->is_receiving = 1; /* tell driver */
wdenkba56f622004-02-06 23:19:44 +00001869
1870 for (;;) {
1871 /* AS.HARNOIS
1872 * use ring buffer and
1873 * get index from rx buffer desciptor queue
1874 */
1875 user_index = hw_p->rx_ready[hw_p->rx_u_index];
1876 if (user_index == -1) {
1877 length = -1;
1878 break; /* nothing received - leave for() loop */
1879 }
1880
1881 msr = mfmsr ();
1882 mtmsr (msr & ~(MSR_EE));
1883
Stefan Roese8ac41e32008-03-11 15:05:26 +01001884 length = hw_p->rx[user_index].data_len & 0x0fff;
wdenkba56f622004-02-06 23:19:44 +00001885
1886 /* Pass the packet up to the protocol layers. */
Wolfgang Denk265817c2005-09-25 00:53:22 +02001887 /* NetReceive(NetRxPackets[rxIdx], length - 4); */
1888 /* NetReceive(NetRxPackets[i], length); */
Stefan Roeseff768cb2007-10-31 18:01:24 +01001889 invalidate_dcache_range((u32)hw_p->rx[user_index].data_ptr,
1890 (u32)hw_p->rx[user_index].data_ptr +
Matthias Fuchsba79fde2007-12-14 11:19:56 +01001891 length - 4);
wdenkba56f622004-02-06 23:19:44 +00001892 NetReceive (NetRxPackets[user_index], length - 4);
1893 /* Free Recv Buffer */
1894 hw_p->rx[user_index].ctrl |= MAL_RX_CTRL_EMPTY;
1895 /* Free rx buffer descriptor queue */
1896 hw_p->rx_ready[hw_p->rx_u_index] = -1;
1897 hw_p->rx_u_index++;
1898 if (NUM_RX_BUFF == hw_p->rx_u_index)
1899 hw_p->rx_u_index = 0;
1900
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001901#ifdef INFO_4XX_ENET
wdenkba56f622004-02-06 23:19:44 +00001902 hw_p->stats.pkts_handled++;
1903#endif
1904
1905 mtmsr (msr); /* Enable IRQ's */
1906 }
1907
Wolfgang Denk265817c2005-09-25 00:53:22 +02001908 hw_p->is_receiving = 0; /* tell driver */
wdenkba56f622004-02-06 23:19:44 +00001909
1910 return length;
1911}
1912
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001913int ppc_4xx_eth_initialize (bd_t * bis)
wdenkba56f622004-02-06 23:19:44 +00001914{
1915 static int virgin = 0;
wdenkba56f622004-02-06 23:19:44 +00001916 struct eth_device *dev;
1917 int eth_num = 0;
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001918 EMAC_4XX_HW_PST hw = NULL;
Stefan Roese5fb692c2007-01-18 10:25:34 +01001919 u8 ethaddr[4 + CONFIG_EMAC_NR_START][6];
1920 u32 hw_addr[4];
Stefan Roesed1631fe2008-06-26 13:40:57 +02001921 u32 mal_ier;
wdenkba56f622004-02-06 23:19:44 +00001922
Stefan Roese846b0dd2005-08-08 12:42:22 +02001923#if defined(CONFIG_440GX)
Stefan Roesec157d8e2005-08-01 16:41:48 +02001924 unsigned long pfc1;
1925
wdenkba56f622004-02-06 23:19:44 +00001926 mfsdr (sdr_pfc1, pfc1);
1927 pfc1 &= ~(0x01e00000);
1928 pfc1 |= 0x01200000;
1929 mtsdr (sdr_pfc1, pfc1);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001930#endif
Stefan Roese5fb692c2007-01-18 10:25:34 +01001931
1932 /* first clear all mac-addresses */
1933 for (eth_num = 0; eth_num < LAST_EMAC_NUM; eth_num++)
1934 memcpy(ethaddr[eth_num], "\0\0\0\0\0\0", 6);
1935
1936 for (eth_num = 0; eth_num < LAST_EMAC_NUM; eth_num++) {
1937 switch (eth_num) {
1938 default: /* fall through */
1939 case 0:
1940 memcpy(ethaddr[eth_num + CONFIG_EMAC_NR_START],
1941 bis->bi_enetaddr, 6);
1942 hw_addr[eth_num] = 0x0;
1943 break;
1944#ifdef CONFIG_HAS_ETH1
1945 case 1:
1946 memcpy(ethaddr[eth_num + CONFIG_EMAC_NR_START],
1947 bis->bi_enet1addr, 6);
1948 hw_addr[eth_num] = 0x100;
1949 break;
1950#endif
1951#ifdef CONFIG_HAS_ETH2
1952 case 2:
1953 memcpy(ethaddr[eth_num + CONFIG_EMAC_NR_START],
1954 bis->bi_enet2addr, 6);
Stefan Roese4c9e8552008-03-19 16:20:49 +01001955#if defined(CONFIG_460GT)
1956 hw_addr[eth_num] = 0x300;
1957#else
Stefan Roese5fb692c2007-01-18 10:25:34 +01001958 hw_addr[eth_num] = 0x400;
Stefan Roese4c9e8552008-03-19 16:20:49 +01001959#endif
Stefan Roese5fb692c2007-01-18 10:25:34 +01001960 break;
1961#endif
1962#ifdef CONFIG_HAS_ETH3
1963 case 3:
1964 memcpy(ethaddr[eth_num + CONFIG_EMAC_NR_START],
1965 bis->bi_enet3addr, 6);
Stefan Roese4c9e8552008-03-19 16:20:49 +01001966#if defined(CONFIG_460GT)
1967 hw_addr[eth_num] = 0x400;
1968#else
Stefan Roese5fb692c2007-01-18 10:25:34 +01001969 hw_addr[eth_num] = 0x600;
Stefan Roese4c9e8552008-03-19 16:20:49 +01001970#endif
Stefan Roese5fb692c2007-01-18 10:25:34 +01001971 break;
1972#endif
1973 }
1974 }
1975
wdenk3c74e322004-02-22 23:46:08 +00001976 /* set phy num and mode */
1977 bis->bi_phynum[0] = CONFIG_PHY_ADDR;
Marian Balakowicz6c5879f2006-06-30 16:30:46 +02001978 bis->bi_phymode[0] = 0;
1979
Stefan Roesec157d8e2005-08-01 16:41:48 +02001980#if defined(CONFIG_PHY1_ADDR)
wdenk3c74e322004-02-22 23:46:08 +00001981 bis->bi_phynum[1] = CONFIG_PHY1_ADDR;
Marian Balakowicz6c5879f2006-06-30 16:30:46 +02001982 bis->bi_phymode[1] = 0;
Stefan Roesec157d8e2005-08-01 16:41:48 +02001983#endif
Stefan Roese846b0dd2005-08-08 12:42:22 +02001984#if defined(CONFIG_440GX)
wdenk3c74e322004-02-22 23:46:08 +00001985 bis->bi_phynum[2] = CONFIG_PHY2_ADDR;
1986 bis->bi_phynum[3] = CONFIG_PHY3_ADDR;
wdenk3c74e322004-02-22 23:46:08 +00001987 bis->bi_phymode[2] = 2;
1988 bis->bi_phymode[3] = 2;
Stefan Roesedbbd1252007-10-05 17:10:59 +02001989#endif
wdenkba56f622004-02-06 23:19:44 +00001990
Stefan Roesedbbd1252007-10-05 17:10:59 +02001991#if defined(CONFIG_440GX) || \
1992 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
1993 defined(CONFIG_405EX)
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001994 ppc_4xx_eth_setup_bridge(0, bis);
wdenka06752e2004-09-29 22:43:59 +00001995#endif
1996
Stefan Roese1e25f952005-10-20 16:34:28 +02001997 for (eth_num = 0; eth_num < LAST_EMAC_NUM; eth_num++) {
Stefan Roese5fb692c2007-01-18 10:25:34 +01001998 /*
1999 * See if we can actually bring up the interface,
2000 * otherwise, skip it
2001 */
2002 if (memcmp (ethaddr[eth_num], "\0\0\0\0\0\0", 6) == 0) {
2003 bis->bi_phymode[eth_num] = BI_PHYMODE_NONE;
2004 continue;
wdenkba56f622004-02-06 23:19:44 +00002005 }
2006
2007 /* Allocate device structure */
2008 dev = (struct eth_device *) malloc (sizeof (*dev));
2009 if (dev == NULL) {
Stefan Roesed6c61aa2005-08-16 18:18:00 +02002010 printf ("ppc_4xx_eth_initialize: "
wdenk3f85ce22004-02-23 16:11:30 +00002011 "Cannot allocate eth_device %d\n", eth_num);
wdenkba56f622004-02-06 23:19:44 +00002012 return (-1);
2013 }
wdenkb2532ef2005-06-20 10:17:34 +00002014 memset(dev, 0, sizeof(*dev));
wdenkba56f622004-02-06 23:19:44 +00002015
2016 /* Allocate our private use data */
Stefan Roesed6c61aa2005-08-16 18:18:00 +02002017 hw = (EMAC_4XX_HW_PST) malloc (sizeof (*hw));
wdenkba56f622004-02-06 23:19:44 +00002018 if (hw == NULL) {
Stefan Roesed6c61aa2005-08-16 18:18:00 +02002019 printf ("ppc_4xx_eth_initialize: "
wdenk3f85ce22004-02-23 16:11:30 +00002020 "Cannot allocate private hw data for eth_device %d",
wdenkba56f622004-02-06 23:19:44 +00002021 eth_num);
2022 free (dev);
2023 return (-1);
2024 }
wdenkb2532ef2005-06-20 10:17:34 +00002025 memset(hw, 0, sizeof(*hw));
wdenkba56f622004-02-06 23:19:44 +00002026
Stefan Roese5fb692c2007-01-18 10:25:34 +01002027 hw->hw_addr = hw_addr[eth_num];
2028 memcpy (dev->enetaddr, ethaddr[eth_num], 6);
wdenkba56f622004-02-06 23:19:44 +00002029 hw->devnum = eth_num;
Stefan Roesec157d8e2005-08-01 16:41:48 +02002030 hw->print_speed = 1;
wdenkba56f622004-02-06 23:19:44 +00002031
Stefan Roese5fb692c2007-01-18 10:25:34 +01002032 sprintf (dev->name, "ppc_4xx_eth%d", eth_num - CONFIG_EMAC_NR_START);
wdenkba56f622004-02-06 23:19:44 +00002033 dev->priv = (void *) hw;
Stefan Roesed6c61aa2005-08-16 18:18:00 +02002034 dev->init = ppc_4xx_eth_init;
2035 dev->halt = ppc_4xx_eth_halt;
2036 dev->send = ppc_4xx_eth_send;
2037 dev->recv = ppc_4xx_eth_rx;
wdenkba56f622004-02-06 23:19:44 +00002038
2039 if (0 == virgin) {
2040 /* set the MAL IER ??? names may change with new spec ??? */
Stefan Roesedbbd1252007-10-05 17:10:59 +02002041#if defined(CONFIG_440SPE) || \
2042 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roese8ac41e32008-03-11 15:05:26 +01002043 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +02002044 defined(CONFIG_405EX)
Marian Balakowicz6c5879f2006-06-30 16:30:46 +02002045 mal_ier =
2046 MAL_IER_PT | MAL_IER_PRE | MAL_IER_PWE |
2047 MAL_IER_DE | MAL_IER_OTE | MAL_IER_OE | MAL_IER_PE ;
2048#else
wdenkba56f622004-02-06 23:19:44 +00002049 mal_ier =
2050 MAL_IER_DE | MAL_IER_NE | MAL_IER_TE |
2051 MAL_IER_OPBE | MAL_IER_PLBE;
Marian Balakowicz6c5879f2006-06-30 16:30:46 +02002052#endif
wdenkba56f622004-02-06 23:19:44 +00002053 mtdcr (malesr, 0xffffffff); /* clear pending interrupts */
2054 mtdcr (maltxdeir, 0xffffffff); /* clear pending interrupts */
2055 mtdcr (malrxdeir, 0xffffffff); /* clear pending interrupts */
2056 mtdcr (malier, mal_ier);
2057
2058 /* install MAL interrupt handler */
Stefan Roesed1631fe2008-06-26 13:40:57 +02002059 irq_install_handler (VECNUM_MAL_SERR,
wdenkba56f622004-02-06 23:19:44 +00002060 (interrupt_handler_t *) enetInt,
2061 dev);
Stefan Roesed1631fe2008-06-26 13:40:57 +02002062 irq_install_handler (VECNUM_MAL_TXEOB,
wdenkba56f622004-02-06 23:19:44 +00002063 (interrupt_handler_t *) enetInt,
2064 dev);
Stefan Roesed1631fe2008-06-26 13:40:57 +02002065 irq_install_handler (VECNUM_MAL_RXEOB,
wdenkba56f622004-02-06 23:19:44 +00002066 (interrupt_handler_t *) enetInt,
2067 dev);
Stefan Roesed1631fe2008-06-26 13:40:57 +02002068 irq_install_handler (VECNUM_MAL_TXDE,
wdenkba56f622004-02-06 23:19:44 +00002069 (interrupt_handler_t *) enetInt,
2070 dev);
Stefan Roesed1631fe2008-06-26 13:40:57 +02002071 irq_install_handler (VECNUM_MAL_RXDE,
wdenkba56f622004-02-06 23:19:44 +00002072 (interrupt_handler_t *) enetInt,
2073 dev);
2074 virgin = 1;
2075 }
2076
Stefan Roesed6c61aa2005-08-16 18:18:00 +02002077#if defined(CONFIG_NET_MULTI)
wdenkba56f622004-02-06 23:19:44 +00002078 eth_register (dev);
Stefan Roesed6c61aa2005-08-16 18:18:00 +02002079#else
2080 emac0_dev = dev;
2081#endif
Marian Balakowicz6c5879f2006-06-30 16:30:46 +02002082
2083#if defined(CONFIG_NET_MULTI)
Jon Loeliger3a1ed1e2007-07-09 18:57:22 -05002084#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Marian Balakowicz63ff0042005-10-28 22:30:33 +02002085 miiphy_register (dev->name,
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01002086 emac4xx_miiphy_read, emac4xx_miiphy_write);
Marian Balakowicz63ff0042005-10-28 22:30:33 +02002087#endif
Marian Balakowicz6c5879f2006-06-30 16:30:46 +02002088#endif
wdenkba56f622004-02-06 23:19:44 +00002089 } /* end for each supported device */
Stefan Roese802b7692008-01-08 18:39:30 +01002090
2091 return 0;
wdenkba56f622004-02-06 23:19:44 +00002092}
Stefan Roesed6c61aa2005-08-16 18:18:00 +02002093
Stefan Roesed6c61aa2005-08-16 18:18:00 +02002094#if !defined(CONFIG_NET_MULTI)
2095void eth_halt (void) {
2096 if (emac0_dev) {
2097 ppc_4xx_eth_halt(emac0_dev);
2098 free(emac0_dev);
2099 emac0_dev = NULL;
2100 }
2101}
2102
2103int eth_init (bd_t *bis)
2104{
2105 ppc_4xx_eth_initialize(bis);
Stefan Roese4f92ac32005-10-10 17:43:58 +02002106 if (emac0_dev) {
2107 return ppc_4xx_eth_init(emac0_dev, bis);
2108 } else {
2109 printf("ERROR: ethaddr not set!\n");
2110 return -1;
2111 }
Stefan Roesed6c61aa2005-08-16 18:18:00 +02002112}
2113
2114int eth_send(volatile void *packet, int length)
2115{
Stefan Roesed6c61aa2005-08-16 18:18:00 +02002116 return (ppc_4xx_eth_send(emac0_dev, packet, length));
2117}
2118
2119int eth_rx(void)
2120{
2121 return (ppc_4xx_eth_rx(emac0_dev));
2122}
Marian Balakowicz63ff0042005-10-28 22:30:33 +02002123
2124int emac4xx_miiphy_initialize (bd_t * bis)
2125{
Jon Loeliger3a1ed1e2007-07-09 18:57:22 -05002126#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Marian Balakowicz63ff0042005-10-28 22:30:33 +02002127 miiphy_register ("ppc_4xx_eth0",
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01002128 emac4xx_miiphy_read, emac4xx_miiphy_write);
Marian Balakowicz63ff0042005-10-28 22:30:33 +02002129#endif
2130
2131 return 0;
2132}
Stefan Roesed6c61aa2005-08-16 18:18:00 +02002133#endif /* !defined(CONFIG_NET_MULTI) */
2134
Jon Loeliger3a1ed1e2007-07-09 18:57:22 -05002135#endif