blob: a7711390b08603e74380442b831ab70b56079b64 [file] [log] [blame]
wdenkba56f622004-02-06 23:19:44 +00001/*-----------------------------------------------------------------------------+
Josh Boyer31773492009-08-07 13:53:20 -04002 * This source code is dual-licensed. You may use it under the terms of the
3 * GNU General Public License version 2, or under the license below.
wdenkba56f622004-02-06 23:19:44 +00004 *
Wolfgang Denk265817c2005-09-25 00:53:22 +02005 * This source code has been made available to you by IBM on an AS-IS
6 * basis. Anyone receiving this source is licensed under IBM
7 * copyrights to use it in any way he or she deems fit, including
8 * copying it, modifying it, compiling it, and redistributing it either
9 * with or without modifications. No license under IBM patents or
10 * patent applications is to be implied by the copyright license.
wdenkba56f622004-02-06 23:19:44 +000011 *
Wolfgang Denk265817c2005-09-25 00:53:22 +020012 * Any user of this software should understand that IBM cannot provide
13 * technical support for this software and will not be responsible for
14 * any consequences resulting from the use of this software.
wdenkba56f622004-02-06 23:19:44 +000015 *
Wolfgang Denk265817c2005-09-25 00:53:22 +020016 * Any person who transfers this source code or any derivative work
17 * must include the IBM copyright notice, this paragraph, and the
18 * preceding two paragraphs in the transferred software.
wdenkba56f622004-02-06 23:19:44 +000019 *
Wolfgang Denk265817c2005-09-25 00:53:22 +020020 * COPYRIGHT I B M CORPORATION 1995
21 * LICENSED MATERIAL - PROGRAM PROPERTY OF I B M
wdenkba56f622004-02-06 23:19:44 +000022 *-----------------------------------------------------------------------------*/
23/*-----------------------------------------------------------------------------+
24 *
Wolfgang Denk265817c2005-09-25 00:53:22 +020025 * File Name: enetemac.c
wdenkba56f622004-02-06 23:19:44 +000026 *
Wolfgang Denk265817c2005-09-25 00:53:22 +020027 * Function: Device driver for the ethernet EMAC3 macro on the 405GP.
wdenkba56f622004-02-06 23:19:44 +000028 *
Wolfgang Denk265817c2005-09-25 00:53:22 +020029 * Author: Mark Wisner
wdenkba56f622004-02-06 23:19:44 +000030 *
31 * Change Activity-
32 *
Wolfgang Denk265817c2005-09-25 00:53:22 +020033 * Date Description of Change BY
34 * --------- --------------------- ---
35 * 05-May-99 Created MKW
36 * 27-Jun-99 Clean up JWB
37 * 16-Jul-99 Added MAL error recovery and better IP packet handling MKW
38 * 29-Jul-99 Added Full duplex support MKW
39 * 06-Aug-99 Changed names for Mal CR reg MKW
40 * 23-Aug-99 Turned off SYE when running at 10Mbs MKW
41 * 24-Aug-99 Marked descriptor empty after call_xlc MKW
42 * 07-Sep-99 Set MAL RX buffer size reg to ENET_MAX_MTU_ALIGNED / 16 MCG
43 * to avoid chaining maximum sized packets. Push starting
44 * RX descriptor address up to the next cache line boundary.
45 * 16-Jan-00 Added support for booting with IP of 0x0 MKW
46 * 15-Mar-00 Updated enetInit() to enable broadcast addresses in the
Niklaus Gigerddc922f2009-10-04 20:04:20 +020047 * EMAC0_RXM register. JWB
Wolfgang Denk265817c2005-09-25 00:53:22 +020048 * 12-Mar-01 anne-sophie.harnois@nextream.fr
49 * - Variables are compatible with those already defined in
50 * include/net.h
51 * - Receive buffer descriptor ring is used to send buffers
52 * to the user
53 * - Info print about send/received/handled packet number if
54 * INFO_405_ENET is set
55 * 17-Apr-01 stefan.roese@esd-electronics.com
56 * - MAL reset in "eth_halt" included
57 * - Enet speed and duplex output now in one line
58 * 08-May-01 stefan.roese@esd-electronics.com
59 * - MAL error handling added (eth_init called again)
60 * 13-Nov-01 stefan.roese@esd-electronics.com
Niklaus Gigerddc922f2009-10-04 20:04:20 +020061 * - Set IST bit in EMAC0_MR1 reg upon 100MBit or full duplex
Wolfgang Denk265817c2005-09-25 00:53:22 +020062 * 04-Jan-02 stefan.roese@esd-electronics.com
63 * - Wait for PHY auto negotiation to complete added
64 * 06-Feb-02 stefan.roese@esd-electronics.com
65 * - Bug fixed in waiting for auto negotiation to complete
66 * 26-Feb-02 stefan.roese@esd-electronics.com
67 * - rx and tx buffer descriptors now allocated (no fixed address
68 * used anymore)
69 * 17-Jun-02 stefan.roese@esd-electronics.com
70 * - MAL error debug printf 'M' removed (rx de interrupt may
71 * occur upon many incoming packets with only 4 rx buffers).
wdenkba56f622004-02-06 23:19:44 +000072 *-----------------------------------------------------------------------------*
Wolfgang Denk265817c2005-09-25 00:53:22 +020073 * 17-Nov-03 travis.sawyer@sandburst.com
74 * - ported from 405gp_enet.c to utilized upto 4 EMAC ports
75 * in the 440GX. This port should work with the 440GP
76 * (2 EMACs) also
77 * 15-Aug-05 sr@denx.de
78 * - merged 405gp_enet.c and 440gx_enet.c to generic 4xx_enet.c
79 now handling all 4xx cpu's.
wdenkba56f622004-02-06 23:19:44 +000080 *-----------------------------------------------------------------------------*/
81
82#include <config.h>
wdenkba56f622004-02-06 23:19:44 +000083#include <common.h>
84#include <net.h>
85#include <asm/processor.h>
Stefan Roese2d834762007-10-23 14:03:17 +020086#include <asm/io.h>
Stefan Roeseff768cb2007-10-31 18:01:24 +010087#include <asm/cache.h>
88#include <asm/mmu.h>
wdenkba56f622004-02-06 23:19:44 +000089#include <commproc.h>
Stefan Roeseb36df562010-09-09 19:18:00 +020090#include <asm/ppc4xx.h>
91#include <asm/ppc4xx-emac.h>
92#include <asm/ppc4xx-mal.h>
wdenkba56f622004-02-06 23:19:44 +000093#include <miiphy.h>
94#include <malloc.h>
wdenkba56f622004-02-06 23:19:44 +000095
Jon Loeliger3a1ed1e2007-07-09 18:57:22 -050096#if !(defined(CONFIG_MII) || defined(CONFIG_CMD_MII))
Stefan Roesed6c61aa2005-08-16 18:18:00 +020097#error "CONFIG_MII has to be defined!"
98#endif
wdenkba56f622004-02-06 23:19:44 +000099
Wolfgang Denk265817c2005-09-25 00:53:22 +0200100#define EMAC_RESET_TIMEOUT 1000 /* 1000 ms reset timeout */
Stefan Roese1338e6a2007-10-23 14:05:08 +0200101#define PHY_AUTONEGOTIATE_TIMEOUT 5000 /* 5000 ms autonegotiate timeout */
wdenkba56f622004-02-06 23:19:44 +0000102
wdenkba56f622004-02-06 23:19:44 +0000103/* Ethernet Transmit and Receive Buffers */
104/* AS.HARNOIS
105 * In the same way ENET_MAX_MTU and ENET_MAX_MTU_ALIGNED are set from
106 * PKTSIZE and PKTSIZE_ALIGN (include/net.h)
107 */
Wolfgang Denk265817c2005-09-25 00:53:22 +0200108#define ENET_MAX_MTU PKTSIZE
wdenkba56f622004-02-06 23:19:44 +0000109#define ENET_MAX_MTU_ALIGNED PKTSIZE_ALIGN
110
wdenkba56f622004-02-06 23:19:44 +0000111/*-----------------------------------------------------------------------------+
112 * Defines for MAL/EMAC interrupt conditions as reported in the UIC (Universal
113 * Interrupt Controller).
114 *-----------------------------------------------------------------------------*/
Stefan Roesed1631fe2008-06-26 13:40:57 +0200115#define ETH_IRQ_NUM(dev) (VECNUM_ETH0 + ((dev) * VECNUM_ETH1_OFFS))
116
117#if defined(CONFIG_HAS_ETH3)
118#if !defined(CONFIG_440GX)
119#define UIC_ETHx (UIC_MASK(ETH_IRQ_NUM(0)) || UIC_MASK(ETH_IRQ_NUM(1)) || \
120 UIC_MASK(ETH_IRQ_NUM(2)) || UIC_MASK(ETH_IRQ_NUM(3)))
121#else
122/* Unfortunately 440GX spreads EMAC interrupts on multiple UIC's */
123#define UIC_ETHx (UIC_MASK(ETH_IRQ_NUM(0)) || UIC_MASK(ETH_IRQ_NUM(1)))
124#define UIC_ETHxB (UIC_MASK(ETH_IRQ_NUM(2)) || UIC_MASK(ETH_IRQ_NUM(3)))
125#endif /* !defined(CONFIG_440GX) */
126#elif defined(CONFIG_HAS_ETH2)
127#define UIC_ETHx (UIC_MASK(ETH_IRQ_NUM(0)) || UIC_MASK(ETH_IRQ_NUM(1)) || \
128 UIC_MASK(ETH_IRQ_NUM(2)))
129#elif defined(CONFIG_HAS_ETH1)
130#define UIC_ETHx (UIC_MASK(ETH_IRQ_NUM(0)) || UIC_MASK(ETH_IRQ_NUM(1)))
131#else
132#define UIC_ETHx UIC_MASK(ETH_IRQ_NUM(0))
133#endif
134
135/*
136 * Define a default version for UIC_ETHxB for non 440GX so that we can
137 * use common code for all 4xx variants
138 */
139#if !defined(UIC_ETHxB)
140#define UIC_ETHxB 0
141#endif
142
143#define UIC_MAL_SERR UIC_MASK(VECNUM_MAL_SERR)
144#define UIC_MAL_TXDE UIC_MASK(VECNUM_MAL_TXDE)
145#define UIC_MAL_RXDE UIC_MASK(VECNUM_MAL_RXDE)
146#define UIC_MAL_TXEOB UIC_MASK(VECNUM_MAL_TXEOB)
147#define UIC_MAL_RXEOB UIC_MASK(VECNUM_MAL_RXEOB)
148
149#define MAL_UIC_ERR (UIC_MAL_SERR | UIC_MAL_TXDE | UIC_MAL_RXDE)
150#define MAL_UIC_DEF (UIC_MAL_RXEOB | MAL_UIC_ERR)
151
152/*
153 * We have 3 different interrupt types:
154 * - MAL interrupts indicating successful transfer
155 * - MAL error interrupts indicating MAL related errors
156 * - EMAC interrupts indicating EMAC related errors
157 *
158 * All those interrupts can be on different UIC's, but since
159 * now at least all interrupts from one type are on the same
160 * UIC. Only exception is 440GX where the EMAC interrupts are
161 * spread over two UIC's!
162 */
Stefan Roese5de85142008-06-26 17:36:39 +0200163#if defined(CONFIG_440GX)
164#define UIC_BASE_MAL UIC1_DCR_BASE
165#define UIC_BASE_MAL_ERR UIC2_DCR_BASE
166#define UIC_BASE_EMAC UIC2_DCR_BASE
167#define UIC_BASE_EMAC_B UIC3_DCR_BASE
168#else
Stefan Roesed1631fe2008-06-26 13:40:57 +0200169#define UIC_BASE_MAL (UIC0_DCR_BASE + (UIC_NR(VECNUM_MAL_TXEOB) * 0x10))
170#define UIC_BASE_MAL_ERR (UIC0_DCR_BASE + (UIC_NR(VECNUM_MAL_SERR) * 0x10))
171#define UIC_BASE_EMAC (UIC0_DCR_BASE + (UIC_NR(ETH_IRQ_NUM(0)) * 0x10))
Stefan Roesed1631fe2008-06-26 13:40:57 +0200172#define UIC_BASE_EMAC_B (UIC0_DCR_BASE + (UIC_NR(ETH_IRQ_NUM(0)) * 0x10))
173#endif
wdenkba56f622004-02-06 23:19:44 +0000174
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200175#undef INFO_4XX_ENET
wdenkba56f622004-02-06 23:19:44 +0000176
Wolfgang Denk265817c2005-09-25 00:53:22 +0200177#define BI_PHYMODE_NONE 0
178#define BI_PHYMODE_ZMII 1
wdenk3c74e322004-02-22 23:46:08 +0000179#define BI_PHYMODE_RGMII 2
Stefan Roese887e2ec2006-09-07 11:51:23 +0200180#define BI_PHYMODE_GMII 3
181#define BI_PHYMODE_RTBI 4
182#define BI_PHYMODE_TBI 5
Stefan Roesedbbd1252007-10-05 17:10:59 +0200183#if defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roese8ac41e32008-03-11 15:05:26 +0100184 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200185 defined(CONFIG_405EX)
Stefan Roese887e2ec2006-09-07 11:51:23 +0200186#define BI_PHYMODE_SMII 6
187#define BI_PHYMODE_MII 7
Stefan Roese8ac41e32008-03-11 15:05:26 +0100188#if defined(CONFIG_460EX) || defined(CONFIG_460GT)
189#define BI_PHYMODE_RMII 8
190#endif
Stefan Roese887e2ec2006-09-07 11:51:23 +0200191#endif
Victor Gallardo78d78232008-09-04 23:49:36 -0700192#define BI_PHYMODE_SGMII 9
wdenk3c74e322004-02-22 23:46:08 +0000193
Stefan Roese1941cce2007-10-05 17:35:10 +0200194#if defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200195 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roese8ac41e32008-03-11 15:05:26 +0100196 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200197 defined(CONFIG_405EX)
Stefan Roese887e2ec2006-09-07 11:51:23 +0200198#define SDR0_MFR_ETH_CLK_SEL_V(n) ((0x01<<27) / (n+1))
199#endif
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200200
Stefan Roese8ac41e32008-03-11 15:05:26 +0100201#if defined(CONFIG_460EX) || defined(CONFIG_460GT)
202#define SDR0_ETH_CFG_CLK_SEL_V(n) (0x01 << (8 + n))
203#endif
204
205#if defined(CONFIG_460EX) || defined(CONFIG_460GT)
206#define MAL_RX_CHAN_MUL 8 /* 460EX/GT uses MAL channel 8 for EMAC1 */
207#else
208#define MAL_RX_CHAN_MUL 1
209#endif
210
Victor Gallardo78d78232008-09-04 23:49:36 -0700211/*--------------------------------------------------------------------+
212 * Fixed PHY (PHY-less) support for Ethernet Ports.
213 *--------------------------------------------------------------------*/
214
215/*
216 * Some boards do not have a PHY for each ethernet port. These ports
217 * are known as Fixed PHY (or PHY-less) ports. For such ports, set
218 * the appropriate CONFIG_PHY_ADDR equal to CONFIG_FIXED_PHY and
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200219 * then define CONFIG_SYS_FIXED_PHY_PORTS to define what the speed and
Victor Gallardo78d78232008-09-04 23:49:36 -0700220 * duplex should be for these ports in the board configuration
221 * file.
222 *
223 * For Example:
224 * #define CONFIG_FIXED_PHY 0xFFFFFFFF
225 *
226 * #define CONFIG_PHY_ADDR CONFIG_FIXED_PHY
227 * #define CONFIG_PHY1_ADDR 1
228 * #define CONFIG_PHY2_ADDR CONFIG_FIXED_PHY
229 * #define CONFIG_PHY3_ADDR 3
230 *
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200231 * #define CONFIG_SYS_FIXED_PHY_PORT(devnum,speed,duplex) \
Victor Gallardo78d78232008-09-04 23:49:36 -0700232 * {devnum, speed, duplex},
233 *
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200234 * #define CONFIG_SYS_FIXED_PHY_PORTS \
235 * CONFIG_SYS_FIXED_PHY_PORT(0,1000,FULL) \
236 * CONFIG_SYS_FIXED_PHY_PORT(2,100,HALF)
Victor Gallardo78d78232008-09-04 23:49:36 -0700237 */
238
239#ifndef CONFIG_FIXED_PHY
240#define CONFIG_FIXED_PHY 0xFFFFFFFF /* Fixed PHY (PHY-less) */
241#endif
242
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200243#ifndef CONFIG_SYS_FIXED_PHY_PORTS
244#define CONFIG_SYS_FIXED_PHY_PORTS /* default is an empty array */
Victor Gallardo78d78232008-09-04 23:49:36 -0700245#endif
246
247struct fixed_phy_port {
248 unsigned int devnum; /* ethernet port */
249 unsigned int speed; /* specified speed 10,100 or 1000 */
250 unsigned int duplex; /* specified duplex FULL or HALF */
251};
252
253static const struct fixed_phy_port fixed_phy_port[] = {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200254 CONFIG_SYS_FIXED_PHY_PORTS /* defined in board configuration file */
Victor Gallardo78d78232008-09-04 23:49:36 -0700255};
256
wdenkba56f622004-02-06 23:19:44 +0000257/*-----------------------------------------------------------------------------+
258 * Global variables. TX and RX descriptors and buffers.
259 *-----------------------------------------------------------------------------*/
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200260
Stefan Roese1e25f952005-10-20 16:34:28 +0200261/*
262 * Get count of EMAC devices (doesn't have to be the max. possible number
263 * supported by the cpu)
Stefan Roese353f2682007-10-23 10:10:08 +0200264 *
265 * CONFIG_BOARD_EMAC_COUNT added so now a "dynamic" way to configure the
266 * EMAC count is possible. As it is needed for the Kilauea/Haleakala
267 * 405EX/405EXr eval board, using the same binary.
Stefan Roese1e25f952005-10-20 16:34:28 +0200268 */
Stefan Roese353f2682007-10-23 10:10:08 +0200269#if defined(CONFIG_BOARD_EMAC_COUNT)
270#define LAST_EMAC_NUM board_emac_count()
271#else /* CONFIG_BOARD_EMAC_COUNT */
Stefan Roese1e25f952005-10-20 16:34:28 +0200272#if defined(CONFIG_HAS_ETH3)
273#define LAST_EMAC_NUM 4
274#elif defined(CONFIG_HAS_ETH2)
275#define LAST_EMAC_NUM 3
276#elif defined(CONFIG_HAS_ETH1)
277#define LAST_EMAC_NUM 2
278#else
279#define LAST_EMAC_NUM 1
280#endif
Stefan Roese353f2682007-10-23 10:10:08 +0200281#endif /* CONFIG_BOARD_EMAC_COUNT */
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200282
Stefan Roese5fb692c2007-01-18 10:25:34 +0100283/* normal boards start with EMAC0 */
284#if !defined(CONFIG_EMAC_NR_START)
285#define CONFIG_EMAC_NR_START 0
286#endif
287
Stefan Roeseff768cb2007-10-31 18:01:24 +0100288#define MAL_RX_DESC_SIZE 2048
289#define MAL_TX_DESC_SIZE 2048
290#define MAL_ALLOC_SIZE (MAL_TX_DESC_SIZE + MAL_RX_DESC_SIZE)
291
wdenkba56f622004-02-06 23:19:44 +0000292/*-----------------------------------------------------------------------------+
293 * Prototypes and externals.
294 *-----------------------------------------------------------------------------*/
295static void enet_rcv (struct eth_device *dev, unsigned long malisr);
296
297int enetInt (struct eth_device *dev);
298static void mal_err (struct eth_device *dev, unsigned long isr,
299 unsigned long uic, unsigned long maldef,
300 unsigned long mal_errr);
301static void emac_err (struct eth_device *dev, unsigned long isr);
302
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200303extern int phy_setup_aneg (char *devname, unsigned char addr);
Mike Frysinger5700bb62010-07-27 18:35:08 -0400304extern int emac4xx_miiphy_read (const char *devname, unsigned char addr,
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200305 unsigned char reg, unsigned short *value);
Mike Frysinger5700bb62010-07-27 18:35:08 -0400306extern int emac4xx_miiphy_write (const char *devname, unsigned char addr,
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200307 unsigned char reg, unsigned short value);
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200308
Stefan Roese353f2682007-10-23 10:10:08 +0200309int board_emac_count(void);
310
Stefan Roese8ac41e32008-03-11 15:05:26 +0100311static void emac_loopback_enable(EMAC_4XX_HW_PST hw_p)
312{
313#if defined(CONFIG_440SPE) || \
314 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
315 defined(CONFIG_405EX)
316 u32 val;
317
Stefan Roesed1c3b272009-09-09 16:25:29 +0200318 mfsdr(SDR0_MFR, val);
Stefan Roese8ac41e32008-03-11 15:05:26 +0100319 val |= SDR0_MFR_ETH_CLK_SEL_V(hw_p->devnum);
Stefan Roesed1c3b272009-09-09 16:25:29 +0200320 mtsdr(SDR0_MFR, val);
Stefan Roese8ac41e32008-03-11 15:05:26 +0100321#elif defined(CONFIG_460EX) || defined(CONFIG_460GT)
322 u32 val;
323
324 mfsdr(SDR0_ETH_CFG, val);
325 val |= SDR0_ETH_CFG_CLK_SEL_V(hw_p->devnum);
326 mtsdr(SDR0_ETH_CFG, val);
327#endif
328}
329
330static void emac_loopback_disable(EMAC_4XX_HW_PST hw_p)
331{
332#if defined(CONFIG_440SPE) || \
333 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
334 defined(CONFIG_405EX)
335 u32 val;
336
Stefan Roesed1c3b272009-09-09 16:25:29 +0200337 mfsdr(SDR0_MFR, val);
Stefan Roese8ac41e32008-03-11 15:05:26 +0100338 val &= ~SDR0_MFR_ETH_CLK_SEL_V(hw_p->devnum);
Stefan Roesed1c3b272009-09-09 16:25:29 +0200339 mtsdr(SDR0_MFR, val);
Stefan Roese8ac41e32008-03-11 15:05:26 +0100340#elif defined(CONFIG_460EX) || defined(CONFIG_460GT)
341 u32 val;
342
343 mfsdr(SDR0_ETH_CFG, val);
344 val &= ~SDR0_ETH_CFG_CLK_SEL_V(hw_p->devnum);
345 mtsdr(SDR0_ETH_CFG, val);
346#endif
347}
348
wdenkba56f622004-02-06 23:19:44 +0000349/*-----------------------------------------------------------------------------+
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200350| ppc_4xx_eth_halt
wdenkba56f622004-02-06 23:19:44 +0000351| Disable MAL channel, and EMACn
wdenkba56f622004-02-06 23:19:44 +0000352+-----------------------------------------------------------------------------*/
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200353static void ppc_4xx_eth_halt (struct eth_device *dev)
wdenkba56f622004-02-06 23:19:44 +0000354{
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200355 EMAC_4XX_HW_PST hw_p = dev->priv;
Stefan Roese9ad31982008-03-19 16:35:12 +0100356 u32 val = 10000;
wdenkba56f622004-02-06 23:19:44 +0000357
Niklaus Gigerddc922f2009-10-04 20:04:20 +0200358 out_be32((void *)EMAC0_IER + hw_p->hw_addr, 0x00000000); /* disable emac interrupts */
wdenkba56f622004-02-06 23:19:44 +0000359
360 /* 1st reset MAL channel */
361 /* Note: writing a 0 to a channel has no effect */
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200362#if defined(CONFIG_405EP) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
Stefan Roesed1c3b272009-09-09 16:25:29 +0200363 mtdcr (MAL0_TXCARR, (MAL_CR_MMSR >> (hw_p->devnum * 2)));
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200364#else
Stefan Roesed1c3b272009-09-09 16:25:29 +0200365 mtdcr (MAL0_TXCARR, (MAL_CR_MMSR >> hw_p->devnum));
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200366#endif
Stefan Roesed1c3b272009-09-09 16:25:29 +0200367 mtdcr (MAL0_RXCARR, (MAL_CR_MMSR >> hw_p->devnum));
wdenkba56f622004-02-06 23:19:44 +0000368
369 /* wait for reset */
Stefan Roesed1c3b272009-09-09 16:25:29 +0200370 while (mfdcr (MAL0_RXCASR) & (MAL_CR_MMSR >> hw_p->devnum)) {
wdenkba56f622004-02-06 23:19:44 +0000371 udelay (1000); /* Delay 1 MS so as not to hammer the register */
Stefan Roese9ad31982008-03-19 16:35:12 +0100372 val--;
373 if (val == 0)
wdenkba56f622004-02-06 23:19:44 +0000374 break;
wdenkba56f622004-02-06 23:19:44 +0000375 }
376
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200377 /* provide clocks for EMAC internal loopback */
Stefan Roese8ac41e32008-03-11 15:05:26 +0100378 emac_loopback_enable(hw_p);
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200379
Stefan Roese8ac41e32008-03-11 15:05:26 +0100380 /* EMAC RESET */
Niklaus Gigerddc922f2009-10-04 20:04:20 +0200381 out_be32((void *)EMAC0_MR0 + hw_p->hw_addr, EMAC_MR0_SRST);
wdenkba56f622004-02-06 23:19:44 +0000382
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200383 /* remove clocks for EMAC internal loopback */
Stefan Roese8ac41e32008-03-11 15:05:26 +0100384 emac_loopback_disable(hw_p);
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200385
Stefan Roesea93316c2005-10-18 19:17:12 +0200386#ifndef CONFIG_NETCONSOLE
Stefan Roesec157d8e2005-08-01 16:41:48 +0200387 hw_p->print_speed = 1; /* print speed message again next time */
Stefan Roesea93316c2005-10-18 19:17:12 +0200388#endif
Stefan Roesec157d8e2005-08-01 16:41:48 +0200389
Stefan Roese4c9e8552008-03-19 16:20:49 +0100390#if defined(CONFIG_460EX) || defined(CONFIG_460GT)
391 /* don't bypass the TAHOE0/TAHOE1 cores for Linux */
Stefan Roese9ad31982008-03-19 16:35:12 +0100392 mfsdr(SDR0_ETH_CFG, val);
393 val &= ~(SDR0_ETH_CFG_TAHOE0_BYPASS | SDR0_ETH_CFG_TAHOE1_BYPASS);
394 mtsdr(SDR0_ETH_CFG, val);
Stefan Roese4c9e8552008-03-19 16:20:49 +0100395#endif
396
wdenkba56f622004-02-06 23:19:44 +0000397 return;
398}
399
Stefan Roese846b0dd2005-08-08 12:42:22 +0200400#if defined (CONFIG_440GX)
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200401int ppc_4xx_eth_setup_bridge(int devnum, bd_t * bis)
wdenk855a4962004-03-14 18:23:55 +0000402{
403 unsigned long pfc1;
404 unsigned long zmiifer;
405 unsigned long rmiifer;
406
Stefan Roesed1c3b272009-09-09 16:25:29 +0200407 mfsdr(SDR0_PFC1, pfc1);
wdenk855a4962004-03-14 18:23:55 +0000408 pfc1 = SDR0_PFC1_EPS_DECODE(pfc1);
409
410 zmiifer = 0;
411 rmiifer = 0;
412
413 switch (pfc1) {
414 case 1:
415 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
416 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(1);
417 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(2);
418 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(3);
419 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
420 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
421 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
422 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
423 break;
424 case 2:
Stefan Roesef6e495f2006-11-27 17:43:25 +0100425 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(0);
426 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(1);
427 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(2);
428 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(3);
wdenk855a4962004-03-14 18:23:55 +0000429 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
430 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
431 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
432 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
433 break;
434 case 3:
435 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
436 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
437 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
438 bis->bi_phymode[1] = BI_PHYMODE_NONE;
439 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
440 bis->bi_phymode[3] = BI_PHYMODE_NONE;
441 break;
442 case 4:
443 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(0);
444 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(1);
445 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V (2);
446 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V (3);
447 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
448 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
449 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
450 bis->bi_phymode[3] = BI_PHYMODE_RGMII;
451 break;
452 case 5:
453 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (0);
454 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (1);
455 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (2);
456 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(3);
457 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
458 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
459 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
460 bis->bi_phymode[3] = BI_PHYMODE_RGMII;
461 break;
462 case 6:
463 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (0);
464 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (1);
465 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
wdenk855a4962004-03-14 18:23:55 +0000466 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
467 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
468 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
wdenk855a4962004-03-14 18:23:55 +0000469 break;
470 case 0:
471 default:
472 zmiifer = ZMII_FER_MII << ZMII_FER_V(devnum);
473 rmiifer = 0x0;
474 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
475 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
476 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
477 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
478 break;
479 }
480
481 /* Ensure we setup mdio for this devnum and ONLY this devnum */
482 zmiifer |= (ZMII_FER_MDI) << ZMII_FER_V(devnum);
483
Niklaus Gigerddc922f2009-10-04 20:04:20 +0200484 out_be32((void *)ZMII0_FER, zmiifer);
Stefan Roeseff768cb2007-10-31 18:01:24 +0100485 out_be32((void *)RGMII_FER, rmiifer);
wdenk855a4962004-03-14 18:23:55 +0000486
487 return ((int)pfc1);
wdenk855a4962004-03-14 18:23:55 +0000488}
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200489#endif /* CONFIG_440_GX */
wdenk855a4962004-03-14 18:23:55 +0000490
Stefan Roese887e2ec2006-09-07 11:51:23 +0200491#if defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
492int ppc_4xx_eth_setup_bridge(int devnum, bd_t * bis)
493{
494 unsigned long zmiifer=0x0;
Matthias Fuchs37ed6cd2007-04-24 14:03:45 +0200495 unsigned long pfc1;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200496
Stefan Roesed1c3b272009-09-09 16:25:29 +0200497 mfsdr(SDR0_PFC1, pfc1);
Matthias Fuchs37ed6cd2007-04-24 14:03:45 +0200498 pfc1 &= SDR0_PFC1_SELECT_MASK;
499
Wolfgang Denk2f152782007-05-05 18:23:11 +0200500 switch (pfc1) {
Matthias Fuchs37ed6cd2007-04-24 14:03:45 +0200501 case SDR0_PFC1_SELECT_CONFIG_2:
Stefan Roese887e2ec2006-09-07 11:51:23 +0200502 /* 1 x GMII port */
Niklaus Gigerddc922f2009-10-04 20:04:20 +0200503 out_be32((void *)ZMII0_FER, 0x00);
Stefan Roese2d834762007-10-23 14:03:17 +0200504 out_be32((void *)RGMII_FER, 0x00000037);
Stefan Roese887e2ec2006-09-07 11:51:23 +0200505 bis->bi_phymode[0] = BI_PHYMODE_GMII;
506 bis->bi_phymode[1] = BI_PHYMODE_NONE;
507 break;
Matthias Fuchs37ed6cd2007-04-24 14:03:45 +0200508 case SDR0_PFC1_SELECT_CONFIG_4:
Stefan Roese887e2ec2006-09-07 11:51:23 +0200509 /* 2 x RGMII ports */
Niklaus Gigerddc922f2009-10-04 20:04:20 +0200510 out_be32((void *)ZMII0_FER, 0x00);
Stefan Roese2d834762007-10-23 14:03:17 +0200511 out_be32((void *)RGMII_FER, 0x00000055);
Stefan Roese887e2ec2006-09-07 11:51:23 +0200512 bis->bi_phymode[0] = BI_PHYMODE_RGMII;
513 bis->bi_phymode[1] = BI_PHYMODE_RGMII;
514 break;
Matthias Fuchs37ed6cd2007-04-24 14:03:45 +0200515 case SDR0_PFC1_SELECT_CONFIG_6:
Stefan Roese887e2ec2006-09-07 11:51:23 +0200516 /* 2 x SMII ports */
Niklaus Gigerddc922f2009-10-04 20:04:20 +0200517 out_be32((void *)ZMII0_FER,
Stefan Roese2d834762007-10-23 14:03:17 +0200518 ((ZMII_FER_SMII) << ZMII_FER_V(0)) |
519 ((ZMII_FER_SMII) << ZMII_FER_V(1)));
520 out_be32((void *)RGMII_FER, 0x00000000);
Matthias Fuchs37ed6cd2007-04-24 14:03:45 +0200521 bis->bi_phymode[0] = BI_PHYMODE_SMII;
522 bis->bi_phymode[1] = BI_PHYMODE_SMII;
523 break;
524 case SDR0_PFC1_SELECT_CONFIG_1_2:
525 /* only 1 x MII supported */
Niklaus Gigerddc922f2009-10-04 20:04:20 +0200526 out_be32((void *)ZMII0_FER, (ZMII_FER_MII) << ZMII_FER_V(0));
Stefan Roese2d834762007-10-23 14:03:17 +0200527 out_be32((void *)RGMII_FER, 0x00000000);
Matthias Fuchs37ed6cd2007-04-24 14:03:45 +0200528 bis->bi_phymode[0] = BI_PHYMODE_MII;
529 bis->bi_phymode[1] = BI_PHYMODE_NONE;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200530 break;
531 default:
532 break;
533 }
534
535 /* Ensure we setup mdio for this devnum and ONLY this devnum */
Niklaus Gigerddc922f2009-10-04 20:04:20 +0200536 zmiifer = in_be32((void *)ZMII0_FER);
Stefan Roese887e2ec2006-09-07 11:51:23 +0200537 zmiifer |= (ZMII_FER_MDI) << ZMII_FER_V(devnum);
Niklaus Gigerddc922f2009-10-04 20:04:20 +0200538 out_be32((void *)ZMII0_FER, zmiifer);
Stefan Roese887e2ec2006-09-07 11:51:23 +0200539
540 return ((int)0x0);
541}
542#endif /* CONFIG_440EPX */
543
Stefan Roesedbbd1252007-10-05 17:10:59 +0200544#if defined(CONFIG_405EX)
545int ppc_4xx_eth_setup_bridge(int devnum, bd_t * bis)
546{
Grant Erickson1740c1b2008-07-08 08:35:00 -0700547 u32 rgmiifer = 0;
Stefan Roesedbbd1252007-10-05 17:10:59 +0200548
549 /*
Grant Erickson1740c1b2008-07-08 08:35:00 -0700550 * The 405EX(r)'s RGMII bridge can operate in one of several
551 * modes, only one of which (2 x RGMII) allows the
552 * simultaneous use of both EMACs on the 405EX.
Stefan Roesedbbd1252007-10-05 17:10:59 +0200553 */
Grant Erickson1740c1b2008-07-08 08:35:00 -0700554
555 switch (CONFIG_EMAC_PHY_MODE) {
556
557 case EMAC_PHY_MODE_NONE:
558 /* No ports */
559 rgmiifer |= RGMII_FER_DIS << 0;
560 rgmiifer |= RGMII_FER_DIS << 4;
561 out_be32((void *)RGMII_FER, rgmiifer);
562 bis->bi_phymode[0] = BI_PHYMODE_NONE;
563 bis->bi_phymode[1] = BI_PHYMODE_NONE;
564 break;
565 case EMAC_PHY_MODE_NONE_RGMII:
566 /* 1 x RGMII port on channel 0 */
567 rgmiifer |= RGMII_FER_RGMII << 0;
568 rgmiifer |= RGMII_FER_DIS << 4;
569 out_be32((void *)RGMII_FER, rgmiifer);
570 bis->bi_phymode[0] = BI_PHYMODE_RGMII;
571 bis->bi_phymode[1] = BI_PHYMODE_NONE;
572 break;
573 case EMAC_PHY_MODE_RGMII_NONE:
574 /* 1 x RGMII port on channel 1 */
575 rgmiifer |= RGMII_FER_DIS << 0;
576 rgmiifer |= RGMII_FER_RGMII << 4;
577 out_be32((void *)RGMII_FER, rgmiifer);
578 bis->bi_phymode[0] = BI_PHYMODE_NONE;
579 bis->bi_phymode[1] = BI_PHYMODE_RGMII;
580 break;
581 case EMAC_PHY_MODE_RGMII_RGMII:
Stefan Roesedbbd1252007-10-05 17:10:59 +0200582 /* 2 x RGMII ports */
Grant Erickson1740c1b2008-07-08 08:35:00 -0700583 rgmiifer |= RGMII_FER_RGMII << 0;
584 rgmiifer |= RGMII_FER_RGMII << 4;
585 out_be32((void *)RGMII_FER, rgmiifer);
Stefan Roesedbbd1252007-10-05 17:10:59 +0200586 bis->bi_phymode[0] = BI_PHYMODE_RGMII;
587 bis->bi_phymode[1] = BI_PHYMODE_RGMII;
588 break;
Grant Erickson1740c1b2008-07-08 08:35:00 -0700589 case EMAC_PHY_MODE_NONE_GMII:
590 /* 1 x GMII port on channel 0 */
591 rgmiifer |= RGMII_FER_GMII << 0;
592 rgmiifer |= RGMII_FER_DIS << 4;
593 out_be32((void *)RGMII_FER, rgmiifer);
594 bis->bi_phymode[0] = BI_PHYMODE_GMII;
595 bis->bi_phymode[1] = BI_PHYMODE_NONE;
596 break;
597 case EMAC_PHY_MODE_NONE_MII:
598 /* 1 x MII port on channel 0 */
599 rgmiifer |= RGMII_FER_MII << 0;
600 rgmiifer |= RGMII_FER_DIS << 4;
601 out_be32((void *)RGMII_FER, rgmiifer);
602 bis->bi_phymode[0] = BI_PHYMODE_MII;
603 bis->bi_phymode[1] = BI_PHYMODE_NONE;
604 break;
605 case EMAC_PHY_MODE_GMII_NONE:
606 /* 1 x GMII port on channel 1 */
607 rgmiifer |= RGMII_FER_DIS << 0;
608 rgmiifer |= RGMII_FER_GMII << 4;
609 out_be32((void *)RGMII_FER, rgmiifer);
610 bis->bi_phymode[0] = BI_PHYMODE_NONE;
611 bis->bi_phymode[1] = BI_PHYMODE_GMII;
612 break;
613 case EMAC_PHY_MODE_MII_NONE:
614 /* 1 x MII port on channel 1 */
615 rgmiifer |= RGMII_FER_DIS << 0;
616 rgmiifer |= RGMII_FER_MII << 4;
617 out_be32((void *)RGMII_FER, rgmiifer);
618 bis->bi_phymode[0] = BI_PHYMODE_NONE;
619 bis->bi_phymode[1] = BI_PHYMODE_MII;
Stefan Roesedbbd1252007-10-05 17:10:59 +0200620 break;
621 default:
622 break;
623 }
624
625 /* Ensure we setup mdio for this devnum and ONLY this devnum */
Grant Erickson1740c1b2008-07-08 08:35:00 -0700626 rgmiifer = in_be32((void *)RGMII_FER);
627 rgmiifer |= (1 << (19-devnum));
628 out_be32((void *)RGMII_FER, rgmiifer);
Stefan Roesedbbd1252007-10-05 17:10:59 +0200629
630 return ((int)0x0);
631}
632#endif /* CONFIG_405EX */
633
Stefan Roese8ac41e32008-03-11 15:05:26 +0100634#if defined(CONFIG_460EX) || defined(CONFIG_460GT)
635int ppc_4xx_eth_setup_bridge(int devnum, bd_t * bis)
636{
637 u32 eth_cfg;
638 u32 zmiifer; /* ZMII0_FER reg. */
639 u32 rmiifer; /* RGMII0_FER reg. Bridge 0 */
640 u32 rmiifer1; /* RGMII0_FER reg. Bridge 1 */
Stefan Roese4c9e8552008-03-19 16:20:49 +0100641 int mode;
Stefan Roese8ac41e32008-03-11 15:05:26 +0100642
643 zmiifer = 0;
644 rmiifer = 0;
645 rmiifer1 = 0;
646
Stefan Roese4c9e8552008-03-19 16:20:49 +0100647#if defined(CONFIG_460EX)
648 mode = 9;
Victor Gallardo78d78232008-09-04 23:49:36 -0700649 mfsdr(SDR0_ETH_CFG, eth_cfg);
650 if (((eth_cfg & SDR0_ETH_CFG_SGMII0_ENABLE) > 0) &&
651 ((eth_cfg & SDR0_ETH_CFG_SGMII1_ENABLE) > 0))
652 mode = 11; /* config SGMII */
Stefan Roese4c9e8552008-03-19 16:20:49 +0100653#else
654 mode = 10;
Victor Gallardo78d78232008-09-04 23:49:36 -0700655 mfsdr(SDR0_ETH_CFG, eth_cfg);
656 if (((eth_cfg & SDR0_ETH_CFG_SGMII0_ENABLE) > 0) &&
657 ((eth_cfg & SDR0_ETH_CFG_SGMII1_ENABLE) > 0) &&
658 ((eth_cfg & SDR0_ETH_CFG_SGMII2_ENABLE) > 0))
659 mode = 12; /* config SGMII */
Stefan Roese4c9e8552008-03-19 16:20:49 +0100660#endif
661
Stefan Roese8ac41e32008-03-11 15:05:26 +0100662 /* TODO:
663 * NOTE: 460GT has 2 RGMII bridge cores:
664 * emac0 ------ RGMII0_BASE
665 * |
666 * emac1 -----+
667 *
668 * emac2 ------ RGMII1_BASE
669 * |
670 * emac3 -----+
671 *
672 * 460EX has 1 RGMII bridge core:
673 * and RGMII1_BASE is disabled
674 * emac0 ------ RGMII0_BASE
675 * |
676 * emac1 -----+
677 */
678
679 /*
680 * Right now only 2*RGMII is supported. Please extend when needed.
681 * sr - 2008-02-19
Victor Gallardo78d78232008-09-04 23:49:36 -0700682 * Add SGMII support.
683 * vg - 2008-07-28
Stefan Roese8ac41e32008-03-11 15:05:26 +0100684 */
Stefan Roese4c9e8552008-03-19 16:20:49 +0100685 switch (mode) {
Stefan Roese8ac41e32008-03-11 15:05:26 +0100686 case 1:
687 /* 1 MII - 460EX */
688 /* GMC0 EMAC4_0, ZMII Bridge */
689 zmiifer |= ZMII_FER_MII << ZMII_FER_V(0);
690 bis->bi_phymode[0] = BI_PHYMODE_MII;
691 bis->bi_phymode[1] = BI_PHYMODE_NONE;
692 bis->bi_phymode[2] = BI_PHYMODE_NONE;
693 bis->bi_phymode[3] = BI_PHYMODE_NONE;
694 break;
695 case 2:
696 /* 2 MII - 460GT */
697 /* GMC0 EMAC4_0, GMC1 EMAC4_2, ZMII Bridge */
698 zmiifer |= ZMII_FER_MII << ZMII_FER_V(0);
699 zmiifer |= ZMII_FER_MII << ZMII_FER_V(2);
700 bis->bi_phymode[0] = BI_PHYMODE_MII;
701 bis->bi_phymode[1] = BI_PHYMODE_NONE;
702 bis->bi_phymode[2] = BI_PHYMODE_MII;
703 bis->bi_phymode[3] = BI_PHYMODE_NONE;
704 break;
705 case 3:
706 /* 2 RMII - 460EX */
707 /* GMC0 EMAC4_0, GMC0 EMAC4_1, ZMII Bridge */
708 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
709 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(1);
710 bis->bi_phymode[0] = BI_PHYMODE_RMII;
711 bis->bi_phymode[1] = BI_PHYMODE_RMII;
712 bis->bi_phymode[2] = BI_PHYMODE_NONE;
713 bis->bi_phymode[3] = BI_PHYMODE_NONE;
714 break;
715 case 4:
716 /* 4 RMII - 460GT */
717 /* GMC0 EMAC4_0, GMC0 EMAC4_1, GMC1 EMAC4_2, GMC1, EMAC4_3 */
718 /* ZMII Bridge */
719 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
720 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(1);
721 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(2);
722 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(3);
723 bis->bi_phymode[0] = BI_PHYMODE_RMII;
724 bis->bi_phymode[1] = BI_PHYMODE_RMII;
725 bis->bi_phymode[2] = BI_PHYMODE_RMII;
726 bis->bi_phymode[3] = BI_PHYMODE_RMII;
727 break;
728 case 5:
729 /* 2 SMII - 460EX */
730 /* GMC0 EMAC4_0, GMC0 EMAC4_1, ZMII Bridge */
731 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(0);
732 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(1);
733 bis->bi_phymode[0] = BI_PHYMODE_SMII;
734 bis->bi_phymode[1] = BI_PHYMODE_SMII;
735 bis->bi_phymode[2] = BI_PHYMODE_NONE;
736 bis->bi_phymode[3] = BI_PHYMODE_NONE;
737 break;
738 case 6:
739 /* 4 SMII - 460GT */
740 /* GMC0 EMAC4_0, GMC0 EMAC4_1, GMC0 EMAC4_3, GMC0 EMAC4_3 */
741 /* ZMII Bridge */
742 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(0);
743 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(1);
744 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(2);
745 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(3);
746 bis->bi_phymode[0] = BI_PHYMODE_SMII;
747 bis->bi_phymode[1] = BI_PHYMODE_SMII;
748 bis->bi_phymode[2] = BI_PHYMODE_SMII;
749 bis->bi_phymode[3] = BI_PHYMODE_SMII;
750 break;
751 case 7:
752 /* This is the default mode that we want for board bringup - Maple */
753 /* 1 GMII - 460EX */
754 /* GMC0 EMAC4_0, RGMII Bridge 0 */
755 rmiifer |= RGMII_FER_MDIO(0);
756
757 if (devnum == 0) {
758 rmiifer |= RGMII_FER_GMII << RGMII_FER_V(2); /* CH0CFG - EMAC0 */
759 bis->bi_phymode[0] = BI_PHYMODE_GMII;
760 bis->bi_phymode[1] = BI_PHYMODE_NONE;
761 bis->bi_phymode[2] = BI_PHYMODE_NONE;
762 bis->bi_phymode[3] = BI_PHYMODE_NONE;
763 } else {
764 rmiifer |= RGMII_FER_GMII << RGMII_FER_V(3); /* CH1CFG - EMAC1 */
765 bis->bi_phymode[0] = BI_PHYMODE_NONE;
766 bis->bi_phymode[1] = BI_PHYMODE_GMII;
767 bis->bi_phymode[2] = BI_PHYMODE_NONE;
768 bis->bi_phymode[3] = BI_PHYMODE_NONE;
769 }
770 break;
771 case 8:
772 /* 2 GMII - 460GT */
773 /* GMC0 EMAC4_0, RGMII Bridge 0 */
774 /* GMC1 EMAC4_2, RGMII Bridge 1 */
775 rmiifer |= RGMII_FER_GMII << RGMII_FER_V(2); /* CH0CFG - EMAC0 */
776 rmiifer1 |= RGMII_FER_GMII << RGMII_FER_V(2); /* CH0CFG - EMAC2 */
777 rmiifer |= RGMII_FER_MDIO(0); /* enable MDIO - EMAC0 */
778 rmiifer1 |= RGMII_FER_MDIO(0); /* enable MDIO - EMAC2 */
779
780 bis->bi_phymode[0] = BI_PHYMODE_GMII;
781 bis->bi_phymode[1] = BI_PHYMODE_NONE;
782 bis->bi_phymode[2] = BI_PHYMODE_GMII;
783 bis->bi_phymode[3] = BI_PHYMODE_NONE;
784 break;
785 case 9:
786 /* 2 RGMII - 460EX */
787 /* GMC0 EMAC4_0, GMC0 EMAC4_1, RGMII Bridge 0 */
788 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
789 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(3);
790 rmiifer |= RGMII_FER_MDIO(0); /* enable MDIO - EMAC0 */
791
792 bis->bi_phymode[0] = BI_PHYMODE_RGMII;
793 bis->bi_phymode[1] = BI_PHYMODE_RGMII;
794 bis->bi_phymode[2] = BI_PHYMODE_NONE;
795 bis->bi_phymode[3] = BI_PHYMODE_NONE;
796 break;
797 case 10:
798 /* 4 RGMII - 460GT */
799 /* GMC0 EMAC4_0, GMC0 EMAC4_1, RGMII Bridge 0 */
800 /* GMC1 EMAC4_2, GMC1 EMAC4_3, RGMII Bridge 1 */
801 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
802 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(3);
803 rmiifer1 |= RGMII_FER_RGMII << RGMII_FER_V(2);
804 rmiifer1 |= RGMII_FER_RGMII << RGMII_FER_V(3);
805 bis->bi_phymode[0] = BI_PHYMODE_RGMII;
806 bis->bi_phymode[1] = BI_PHYMODE_RGMII;
807 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
808 bis->bi_phymode[3] = BI_PHYMODE_RGMII;
809 break;
Victor Gallardo78d78232008-09-04 23:49:36 -0700810 case 11:
811 /* 2 SGMII - 460EX */
812 bis->bi_phymode[0] = BI_PHYMODE_SGMII;
813 bis->bi_phymode[1] = BI_PHYMODE_SGMII;
814 bis->bi_phymode[2] = BI_PHYMODE_NONE;
815 bis->bi_phymode[3] = BI_PHYMODE_NONE;
816 break;
817 case 12:
818 /* 3 SGMII - 460GT */
819 bis->bi_phymode[0] = BI_PHYMODE_SGMII;
820 bis->bi_phymode[1] = BI_PHYMODE_SGMII;
821 bis->bi_phymode[2] = BI_PHYMODE_SGMII;
822 bis->bi_phymode[3] = BI_PHYMODE_NONE;
823 break;
Stefan Roese8ac41e32008-03-11 15:05:26 +0100824 default:
825 break;
826 }
827
828 /* Set EMAC for MDIO */
829 mfsdr(SDR0_ETH_CFG, eth_cfg);
830 eth_cfg |= SDR0_ETH_CFG_MDIO_SEL_EMAC0;
831 mtsdr(SDR0_ETH_CFG, eth_cfg);
832
833 out_be32((void *)RGMII_FER, rmiifer);
834#if defined(CONFIG_460GT)
835 out_be32((void *)RGMII_FER + RGMII1_BASE_OFFSET, rmiifer1);
836#endif
837
838 /* bypass the TAHOE0/TAHOE1 cores for U-Boot */
839 mfsdr(SDR0_ETH_CFG, eth_cfg);
840 eth_cfg |= (SDR0_ETH_CFG_TAHOE0_BYPASS | SDR0_ETH_CFG_TAHOE1_BYPASS);
841 mtsdr(SDR0_ETH_CFG, eth_cfg);
842
843 return 0;
844}
845#endif /* CONFIG_460EX || CONFIG_460GT */
846
Stefan Roeseff768cb2007-10-31 18:01:24 +0100847static inline void *malloc_aligned(u32 size, u32 align)
848{
849 return (void *)(((u32)malloc(size + align) + align - 1) &
850 ~(align - 1));
851}
852
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200853static int ppc_4xx_eth_init (struct eth_device *dev, bd_t * bis)
wdenkba56f622004-02-06 23:19:44 +0000854{
Stefan Roeseff768cb2007-10-31 18:01:24 +0100855 int i;
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200856 unsigned long reg = 0;
wdenkba56f622004-02-06 23:19:44 +0000857 unsigned long msr;
858 unsigned long speed;
859 unsigned long duplex;
860 unsigned long failsafe;
861 unsigned mode_reg;
862 unsigned short devnum;
863 unsigned short reg_short;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200864#if defined(CONFIG_440GX) || \
865 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200866 defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
Stefan Roese8ac41e32008-03-11 15:05:26 +0100867 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200868 defined(CONFIG_405EX)
Felix Radensky0c24dec2009-05-31 20:44:15 +0300869 u32 opbfreq;
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200870 sys_info_t sysinfo;
Alessio Centazzo0544c632009-07-11 11:56:06 -0700871#if defined(CONFIG_440GX) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200872 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roese8ac41e32008-03-11 15:05:26 +0100873 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200874 defined(CONFIG_405EX)
Stefan Roese6e7fb6e2005-11-29 18:18:21 +0100875 int ethgroup = -1;
876#endif
Stefan Roesec157d8e2005-08-01 16:41:48 +0200877#endif
Stefan Roeseff768cb2007-10-31 18:01:24 +0100878 u32 bd_cached;
879 u32 bd_uncached = 0;
Anatolij Gustschin4fae35a2008-02-25 20:54:04 +0100880#ifdef CONFIG_4xx_DCACHE
881 static u32 last_used_ea = 0;
882#endif
Stefan Roesee54ec0f2008-04-03 14:50:34 +0200883#if defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
884 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
885 defined(CONFIG_405EX)
886 int rgmii_channel;
887#endif
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200888
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200889 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenkba56f622004-02-06 23:19:44 +0000890
891 /* before doing anything, figure out if we have a MAC address */
892 /* if not, bail */
Stefan Roese4f92ac32005-10-10 17:43:58 +0200893 if (memcmp (dev->enetaddr, "\0\0\0\0\0\0", 6) == 0) {
894 printf("ERROR: ethaddr not set!\n");
wdenkba56f622004-02-06 23:19:44 +0000895 return -1;
Stefan Roese4f92ac32005-10-10 17:43:58 +0200896 }
wdenkba56f622004-02-06 23:19:44 +0000897
Stefan Roese887e2ec2006-09-07 11:51:23 +0200898#if defined(CONFIG_440GX) || \
899 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200900 defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
Stefan Roese8ac41e32008-03-11 15:05:26 +0100901 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200902 defined(CONFIG_405EX)
wdenkba56f622004-02-06 23:19:44 +0000903 /* Need to get the OPB frequency so we can access the PHY */
904 get_sys_info (&sysinfo);
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200905#endif
wdenkba56f622004-02-06 23:19:44 +0000906
wdenkba56f622004-02-06 23:19:44 +0000907 msr = mfmsr ();
908 mtmsr (msr & ~(MSR_EE)); /* disable interrupts */
909
910 devnum = hw_p->devnum;
911
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200912#ifdef INFO_4XX_ENET
wdenkba56f622004-02-06 23:19:44 +0000913 /* AS.HARNOIS
914 * We should have :
Wolfgang Denk265817c2005-09-25 00:53:22 +0200915 * hw_p->stats.pkts_handled <= hw_p->stats.pkts_rx <= hw_p->stats.pkts_handled+PKTBUFSRX
wdenkba56f622004-02-06 23:19:44 +0000916 * In the most cases hw_p->stats.pkts_handled = hw_p->stats.pkts_rx, but it
917 * is possible that new packets (without relationship with
918 * current transfer) have got the time to arrived before
919 * netloop calls eth_halt
920 */
921 printf ("About preceeding transfer (eth%d):\n"
922 "- Sent packet number %d\n"
923 "- Received packet number %d\n"
924 "- Handled packet number %d\n",
925 hw_p->devnum,
926 hw_p->stats.pkts_tx,
927 hw_p->stats.pkts_rx, hw_p->stats.pkts_handled);
928
929 hw_p->stats.pkts_tx = 0;
930 hw_p->stats.pkts_rx = 0;
931 hw_p->stats.pkts_handled = 0;
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200932 hw_p->print_speed = 1; /* print speed message again next time */
wdenkba56f622004-02-06 23:19:44 +0000933#endif
934
Wolfgang Denk265817c2005-09-25 00:53:22 +0200935 hw_p->tx_err_index = 0; /* Transmit Error Index for tx_err_log */
936 hw_p->rx_err_index = 0; /* Receive Error Index for rx_err_log */
wdenkba56f622004-02-06 23:19:44 +0000937
938 hw_p->rx_slot = 0; /* MAL Receive Slot */
939 hw_p->rx_i_index = 0; /* Receive Interrupt Queue Index */
940 hw_p->rx_u_index = 0; /* Receive User Queue Index */
941
942 hw_p->tx_slot = 0; /* MAL Transmit Slot */
943 hw_p->tx_i_index = 0; /* Transmit Interrupt Queue Index */
944 hw_p->tx_u_index = 0; /* Transmit User Queue Index */
945
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200946#if defined(CONFIG_440) && !defined(CONFIG_440SP) && !defined(CONFIG_440SPE)
wdenkba56f622004-02-06 23:19:44 +0000947 /* set RMII mode */
948 /* NOTE: 440GX spec states that mode is mutually exclusive */
949 /* NOTE: Therefore, disable all other EMACS, since we handle */
950 /* NOTE: only one emac at a time */
951 reg = 0;
Niklaus Gigerddc922f2009-10-04 20:04:20 +0200952 out_be32((void *)ZMII0_FER, 0);
wdenkba56f622004-02-06 23:19:44 +0000953 udelay (100);
wdenkba56f622004-02-06 23:19:44 +0000954
Stefan Roese8ac41e32008-03-11 15:05:26 +0100955#if defined(CONFIG_440GP) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
Niklaus Gigerddc922f2009-10-04 20:04:20 +0200956 out_be32((void *)ZMII0_FER, (ZMII_FER_RMII | ZMII_FER_MDI) << ZMII_FER_V (devnum));
Stefan Roese8ac41e32008-03-11 15:05:26 +0100957#elif defined(CONFIG_440GX) || \
958 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
959 defined(CONFIG_460EX) || defined(CONFIG_460GT)
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200960 ethgroup = ppc_4xx_eth_setup_bridge(devnum, bis);
wdenk0e6d7982004-03-14 00:07:33 +0000961#endif
Stefan Roesec57c7982005-08-11 17:56:56 +0200962
Niklaus Gigerddc922f2009-10-04 20:04:20 +0200963 out_be32((void *)ZMII0_SSR, ZMII0_SSR_SP << ZMII0_SSR_V(devnum));
Stefan Roese6e7fb6e2005-11-29 18:18:21 +0100964#endif /* defined(CONFIG_440) && !defined(CONFIG_440SP) */
Stefan Roesedbbd1252007-10-05 17:10:59 +0200965#if defined(CONFIG_405EX)
966 ethgroup = ppc_4xx_eth_setup_bridge(devnum, bis);
967#endif
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200968
Stefan Roese8ac41e32008-03-11 15:05:26 +0100969 sync();
wdenk0e6d7982004-03-14 00:07:33 +0000970
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200971 /* provide clocks for EMAC internal loopback */
Stefan Roese8ac41e32008-03-11 15:05:26 +0100972 emac_loopback_enable(hw_p);
wdenk0e6d7982004-03-14 00:07:33 +0000973
Stefan Roese8ac41e32008-03-11 15:05:26 +0100974 /* EMAC RESET */
Niklaus Gigerddc922f2009-10-04 20:04:20 +0200975 out_be32((void *)EMAC0_MR0 + hw_p->hw_addr, EMAC_MR0_SRST);
wdenkba56f622004-02-06 23:19:44 +0000976
Stefan Roese8ac41e32008-03-11 15:05:26 +0100977 /* remove clocks for EMAC internal loopback */
978 emac_loopback_disable(hw_p);
979
wdenkba56f622004-02-06 23:19:44 +0000980 failsafe = 1000;
Niklaus Gigerddc922f2009-10-04 20:04:20 +0200981 while ((in_be32((void *)EMAC0_MR0 + hw_p->hw_addr) & (EMAC_MR0_SRST)) && failsafe) {
wdenkba56f622004-02-06 23:19:44 +0000982 udelay (1000);
983 failsafe--;
984 }
Stefan Roese887e2ec2006-09-07 11:51:23 +0200985 if (failsafe <= 0)
986 printf("\nProblem resetting EMAC!\n");
wdenkba56f622004-02-06 23:19:44 +0000987
Stefan Roese887e2ec2006-09-07 11:51:23 +0200988#if defined(CONFIG_440GX) || \
989 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200990 defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
Stefan Roese8ac41e32008-03-11 15:05:26 +0100991 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200992 defined(CONFIG_405EX)
wdenkba56f622004-02-06 23:19:44 +0000993 /* Whack the M1 register */
994 mode_reg = 0x0;
995 mode_reg &= ~0x00000038;
Felix Radensky0c24dec2009-05-31 20:44:15 +0300996 opbfreq = sysinfo.freqOPB / 1000000;
997 if (opbfreq <= 50);
998 else if (opbfreq <= 66)
Niklaus Gigerddc922f2009-10-04 20:04:20 +0200999 mode_reg |= EMAC_MR1_OBCI_66;
Felix Radensky0c24dec2009-05-31 20:44:15 +03001000 else if (opbfreq <= 83)
Niklaus Gigerddc922f2009-10-04 20:04:20 +02001001 mode_reg |= EMAC_MR1_OBCI_83;
Felix Radensky0c24dec2009-05-31 20:44:15 +03001002 else if (opbfreq <= 100)
Niklaus Gigerddc922f2009-10-04 20:04:20 +02001003 mode_reg |= EMAC_MR1_OBCI_100;
wdenkba56f622004-02-06 23:19:44 +00001004 else
Niklaus Gigerddc922f2009-10-04 20:04:20 +02001005 mode_reg |= EMAC_MR1_OBCI_GT100;
wdenkba56f622004-02-06 23:19:44 +00001006
Niklaus Gigerddc922f2009-10-04 20:04:20 +02001007 out_be32((void *)EMAC0_MR1 + hw_p->hw_addr, mode_reg);
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001008#endif /* defined(CONFIG_440GX) || defined(CONFIG_440SP) */
wdenkba56f622004-02-06 23:19:44 +00001009
Victor Gallardo78d78232008-09-04 23:49:36 -07001010#if defined(CONFIG_GPCS_PHY_ADDR) || defined(CONFIG_GPCS_PHY1_ADDR) || \
1011 defined(CONFIG_GPCS_PHY2_ADDR) || defined(CONFIG_GPCS_PHY3_ADDR)
1012 if (bis->bi_phymode[devnum] == BI_PHYMODE_SGMII) {
1013 /*
1014 * In SGMII mode, GPCS access is needed for
1015 * communication with the internal SGMII SerDes.
1016 */
1017 switch (devnum) {
1018#if defined(CONFIG_GPCS_PHY_ADDR)
1019 case 0:
1020 reg = CONFIG_GPCS_PHY_ADDR;
1021 break;
1022#endif
1023#if defined(CONFIG_GPCS_PHY1_ADDR)
1024 case 1:
1025 reg = CONFIG_GPCS_PHY1_ADDR;
1026 break;
1027#endif
1028#if defined(CONFIG_GPCS_PHY2_ADDR)
1029 case 2:
1030 reg = CONFIG_GPCS_PHY2_ADDR;
1031 break;
1032#endif
1033#if defined(CONFIG_GPCS_PHY3_ADDR)
1034 case 3:
1035 reg = CONFIG_GPCS_PHY3_ADDR;
1036 break;
1037#endif
1038 }
1039
Niklaus Gigerddc922f2009-10-04 20:04:20 +02001040 mode_reg = in_be32((void *)EMAC0_MR1 + hw_p->hw_addr);
1041 mode_reg |= EMAC_MR1_MF_1000GPCS | EMAC_MR1_IPPA_SET(reg);
1042 out_be32((void *)EMAC0_MR1 + hw_p->hw_addr, mode_reg);
Victor Gallardo78d78232008-09-04 23:49:36 -07001043
1044 /* Configure GPCS interface to recommended setting for SGMII */
1045 miiphy_reset(dev->name, reg);
1046 miiphy_write(dev->name, reg, 0x04, 0x8120); /* AsymPause, FDX */
1047 miiphy_write(dev->name, reg, 0x07, 0x2801); /* msg_pg, toggle */
1048 miiphy_write(dev->name, reg, 0x00, 0x0140); /* 1Gbps, FDX */
1049 }
1050#endif /* defined(CONFIG_GPCS_PHY_ADDR) */
1051
wdenkba56f622004-02-06 23:19:44 +00001052 /* wait for PHY to complete auto negotiation */
1053 reg_short = 0;
wdenkba56f622004-02-06 23:19:44 +00001054 switch (devnum) {
1055 case 0:
1056 reg = CONFIG_PHY_ADDR;
1057 break;
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001058#if defined (CONFIG_PHY1_ADDR)
wdenkba56f622004-02-06 23:19:44 +00001059 case 1:
1060 reg = CONFIG_PHY1_ADDR;
1061 break;
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001062#endif
Stefan Roese4c9e8552008-03-19 16:20:49 +01001063#if defined (CONFIG_PHY2_ADDR)
wdenkba56f622004-02-06 23:19:44 +00001064 case 2:
1065 reg = CONFIG_PHY2_ADDR;
1066 break;
Stefan Roese4c9e8552008-03-19 16:20:49 +01001067#endif
1068#if defined (CONFIG_PHY3_ADDR)
wdenkba56f622004-02-06 23:19:44 +00001069 case 3:
1070 reg = CONFIG_PHY3_ADDR;
1071 break;
1072#endif
1073 default:
1074 reg = CONFIG_PHY_ADDR;
1075 break;
1076 }
1077
wdenk3c74e322004-02-22 23:46:08 +00001078 bis->bi_phynum[devnum] = reg;
1079
Victor Gallardo78d78232008-09-04 23:49:36 -07001080 if (reg == CONFIG_FIXED_PHY)
1081 goto get_speed;
1082
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001083#if defined(CONFIG_PHY_RESET)
wdenka06752e2004-09-29 22:43:59 +00001084 /*
1085 * Reset the phy, only if its the first time through
1086 * otherwise, just check the speeds & feeds
1087 */
1088 if (hw_p->first_init == 0) {
Stefan Roeseec0c2ec2006-11-27 14:46:06 +01001089#if defined(CONFIG_M88E1111_PHY)
Stefan Roese887e2ec2006-09-07 11:51:23 +02001090 miiphy_write (dev->name, reg, 0x14, 0x0ce3);
1091 miiphy_write (dev->name, reg, 0x18, 0x4101);
1092 miiphy_write (dev->name, reg, 0x09, 0x0e00);
1093 miiphy_write (dev->name, reg, 0x04, 0x01e1);
Stefan Roeseca080542010-06-29 09:23:53 +02001094#if defined(CONFIG_M88E1111_DISABLE_FIBER)
1095 miiphy_read(dev->name, reg, 0x1b, &reg_short);
1096 reg_short |= 0x8000;
1097 miiphy_write(dev->name, reg, 0x1b, reg_short);
1098#endif
Stefan Roese887e2ec2006-09-07 11:51:23 +02001099#endif
Victor Gallardo78d78232008-09-04 23:49:36 -07001100#if defined(CONFIG_M88E1112_PHY)
1101 if (bis->bi_phymode[devnum] == BI_PHYMODE_SGMII) {
1102 /*
1103 * Marvell 88E1112 PHY needs to have the SGMII MAC
1104 * interace (page 2) properly configured to
1105 * communicate with the 460EX/GT GPCS interface.
1106 */
1107
1108 /* Set access to Page 2 */
1109 miiphy_write(dev->name, reg, 0x16, 0x0002);
1110
1111 miiphy_write(dev->name, reg, 0x00, 0x0040); /* 1Gbps */
1112 miiphy_read(dev->name, reg, 0x1a, &reg_short);
1113 reg_short |= 0x8000; /* bypass Auto-Negotiation */
1114 miiphy_write(dev->name, reg, 0x1a, reg_short);
1115 miiphy_reset(dev->name, reg); /* reset MAC interface */
1116
1117 /* Reset access to Page 0 */
1118 miiphy_write(dev->name, reg, 0x16, 0x0000);
1119 }
1120#endif /* defined(CONFIG_M88E1112_PHY) */
Marian Balakowicz63ff0042005-10-28 22:30:33 +02001121 miiphy_reset (dev->name, reg);
wdenkba56f622004-02-06 23:19:44 +00001122
Stefan Roese887e2ec2006-09-07 11:51:23 +02001123#if defined(CONFIG_440GX) || \
1124 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roese8ac41e32008-03-11 15:05:26 +01001125 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +02001126 defined(CONFIG_405EX)
Stefan Roese887e2ec2006-09-07 11:51:23 +02001127
wdenk0e6d7982004-03-14 00:07:33 +00001128#if defined(CONFIG_CIS8201_PHY)
wdenkfc1cfcd2004-04-25 15:41:35 +00001129 /*
Stefan Roese17f50f222005-08-04 17:09:16 +02001130 * Cicada 8201 PHY needs to have an extended register whacked
1131 * for RGMII mode.
wdenkfc1cfcd2004-04-25 15:41:35 +00001132 */
Stefan Roese887e2ec2006-09-07 11:51:23 +02001133 if (((devnum == 2) || (devnum == 3)) && (4 == ethgroup)) {
Stefan Roeseb79316f2005-08-15 12:31:23 +02001134#if defined(CONFIG_CIS8201_SHORT_ETCH)
Marian Balakowicz63ff0042005-10-28 22:30:33 +02001135 miiphy_write (dev->name, reg, 23, 0x1300);
Stefan Roeseb79316f2005-08-15 12:31:23 +02001136#else
Marian Balakowicz63ff0042005-10-28 22:30:33 +02001137 miiphy_write (dev->name, reg, 23, 0x1000);
Stefan Roeseb79316f2005-08-15 12:31:23 +02001138#endif
Stefan Roese17f50f222005-08-04 17:09:16 +02001139 /*
1140 * Vitesse VSC8201/Cicada CIS8201 errata:
1141 * Interoperability problem with Intel 82547EI phys
1142 * This work around (provided by Vitesse) changes
1143 * the default timer convergence from 8ms to 12ms
1144 */
Marian Balakowicz63ff0042005-10-28 22:30:33 +02001145 miiphy_write (dev->name, reg, 0x1f, 0x2a30);
1146 miiphy_write (dev->name, reg, 0x08, 0x0200);
1147 miiphy_write (dev->name, reg, 0x1f, 0x52b5);
1148 miiphy_write (dev->name, reg, 0x02, 0x0004);
1149 miiphy_write (dev->name, reg, 0x01, 0x0671);
1150 miiphy_write (dev->name, reg, 0x00, 0x8fae);
1151 miiphy_write (dev->name, reg, 0x1f, 0x2a30);
1152 miiphy_write (dev->name, reg, 0x08, 0x0000);
1153 miiphy_write (dev->name, reg, 0x1f, 0x0000);
Stefan Roese17f50f222005-08-04 17:09:16 +02001154 /* end Vitesse/Cicada errata */
1155 }
Stefan Roese6ca86462008-09-05 14:11:40 +02001156#endif /* defined(CONFIG_CIS8201_PHY) */
Stefan Roese5fb692c2007-01-18 10:25:34 +01001157
1158#if defined(CONFIG_ET1011C_PHY)
1159 /*
1160 * Agere ET1011c PHY needs to have an extended register whacked
1161 * for RGMII mode.
1162 */
1163 if (((devnum == 2) || (devnum ==3)) && (4 == ethgroup)) {
1164 miiphy_read (dev->name, reg, 0x16, &reg_short);
1165 reg_short &= ~(0x7);
1166 reg_short |= 0x6; /* RGMII DLL Delay*/
1167 miiphy_write (dev->name, reg, 0x16, reg_short);
1168
1169 miiphy_read (dev->name, reg, 0x17, &reg_short);
1170 reg_short &= ~(0x40);
1171 miiphy_write (dev->name, reg, 0x17, reg_short);
1172
1173 miiphy_write(dev->name, reg, 0x1c, 0x74f0);
1174 }
Stefan Roese6ca86462008-09-05 14:11:40 +02001175#endif /* defined(CONFIG_ET1011C_PHY) */
Stefan Roese5fb692c2007-01-18 10:25:34 +01001176
Stefan Roese6ca86462008-09-05 14:11:40 +02001177#endif /* defined(CONFIG_440GX) ... */
wdenka06752e2004-09-29 22:43:59 +00001178 /* Start/Restart autonegotiation */
Marian Balakowicz63ff0042005-10-28 22:30:33 +02001179 phy_setup_aneg (dev->name, reg);
wdenka06752e2004-09-29 22:43:59 +00001180 udelay (1000);
1181 }
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001182#endif /* defined(CONFIG_PHY_RESET) */
wdenkba56f622004-02-06 23:19:44 +00001183
Mike Frysinger8ef583a2010-12-23 15:40:12 -05001184 miiphy_read (dev->name, reg, MII_BMSR, &reg_short);
wdenkba56f622004-02-06 23:19:44 +00001185
1186 /*
wdenk0e6d7982004-03-14 00:07:33 +00001187 * Wait if PHY is capable of autonegotiation and autonegotiation is not complete
wdenkba56f622004-02-06 23:19:44 +00001188 */
Mike Frysinger8ef583a2010-12-23 15:40:12 -05001189 if ((reg_short & BMSR_ANEGCAPABLE)
1190 && !(reg_short & BMSR_ANEGCOMPLETE)) {
wdenkba56f622004-02-06 23:19:44 +00001191 puts ("Waiting for PHY auto negotiation to complete");
1192 i = 0;
Mike Frysinger8ef583a2010-12-23 15:40:12 -05001193 while (!(reg_short & BMSR_ANEGCOMPLETE)) {
wdenkba56f622004-02-06 23:19:44 +00001194 /*
1195 * Timeout reached ?
1196 */
1197 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
1198 puts (" TIMEOUT !\n");
1199 break;
1200 }
1201
1202 if ((i++ % 1000) == 0) {
1203 putc ('.');
1204 }
1205 udelay (1000); /* 1 ms */
Mike Frysinger8ef583a2010-12-23 15:40:12 -05001206 miiphy_read (dev->name, reg, MII_BMSR, &reg_short);
wdenkba56f622004-02-06 23:19:44 +00001207 }
1208 puts (" done\n");
1209 udelay (500000); /* another 500 ms (results in faster booting) */
1210 }
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001211
Victor Gallardo78d78232008-09-04 23:49:36 -07001212get_speed:
1213 if (reg == CONFIG_FIXED_PHY) {
1214 for (i = 0; i < ARRAY_SIZE(fixed_phy_port); i++) {
1215 if (devnum == fixed_phy_port[i].devnum) {
1216 speed = fixed_phy_port[i].speed;
1217 duplex = fixed_phy_port[i].duplex;
1218 break;
1219 }
1220 }
1221
1222 if (i == ARRAY_SIZE(fixed_phy_port)) {
1223 printf("ERROR: PHY (%s) not configured correctly!\n",
1224 dev->name);
1225 return -1;
1226 }
1227 } else {
1228 speed = miiphy_speed(dev->name, reg);
1229 duplex = miiphy_duplex(dev->name, reg);
1230 }
wdenkba56f622004-02-06 23:19:44 +00001231
1232 if (hw_p->print_speed) {
1233 hw_p->print_speed = 0;
Stefan Roese5fb692c2007-01-18 10:25:34 +01001234 printf ("ENET Speed is %d Mbps - %s duplex connection (EMAC%d)\n",
1235 (int) speed, (duplex == HALF) ? "HALF" : "FULL",
1236 hw_p->devnum);
wdenkba56f622004-02-06 23:19:44 +00001237 }
1238
Stefan Roese8ac41e32008-03-11 15:05:26 +01001239#if defined(CONFIG_440) && \
1240 !defined(CONFIG_440SP) && !defined(CONFIG_440SPE) && \
1241 !defined(CONFIG_440EPX) && !defined(CONFIG_440GRX) && \
1242 !defined(CONFIG_460EX) && !defined(CONFIG_460GT)
Stefan Roese846b0dd2005-08-08 12:42:22 +02001243#if defined(CONFIG_440EP) || defined(CONFIG_440GR)
Stefan Roesed1c3b272009-09-09 16:25:29 +02001244 mfsdr(SDR0_MFR, reg);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001245 if (speed == 100) {
1246 reg = (reg & ~SDR0_MFR_ZMII_MODE_MASK) | SDR0_MFR_ZMII_MODE_RMII_100M;
1247 } else {
1248 reg = (reg & ~SDR0_MFR_ZMII_MODE_MASK) | SDR0_MFR_ZMII_MODE_RMII_10M;
1249 }
Stefan Roesed1c3b272009-09-09 16:25:29 +02001250 mtsdr(SDR0_MFR, reg);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001251#endif
Stefan Roesec57c7982005-08-11 17:56:56 +02001252
wdenkba56f622004-02-06 23:19:44 +00001253 /* Set ZMII/RGMII speed according to the phy link speed */
Niklaus Gigerddc922f2009-10-04 20:04:20 +02001254 reg = in_be32((void *)ZMII0_SSR);
wdenk855a4962004-03-14 18:23:55 +00001255 if ( (speed == 100) || (speed == 1000) )
Niklaus Gigerddc922f2009-10-04 20:04:20 +02001256 out_be32((void *)ZMII0_SSR, reg | (ZMII0_SSR_SP << ZMII0_SSR_V (devnum)));
wdenkba56f622004-02-06 23:19:44 +00001257 else
Niklaus Gigerddc922f2009-10-04 20:04:20 +02001258 out_be32((void *)ZMII0_SSR, reg & (~(ZMII0_SSR_SP << ZMII0_SSR_V (devnum))));
wdenkba56f622004-02-06 23:19:44 +00001259
1260 if ((devnum == 2) || (devnum == 3)) {
1261 if (speed == 1000)
1262 reg = (RGMII_SSR_SP_1000MBPS << RGMII_SSR_V (devnum));
1263 else if (speed == 100)
1264 reg = (RGMII_SSR_SP_100MBPS << RGMII_SSR_V (devnum));
Stefan Roese887e2ec2006-09-07 11:51:23 +02001265 else if (speed == 10)
wdenkba56f622004-02-06 23:19:44 +00001266 reg = (RGMII_SSR_SP_10MBPS << RGMII_SSR_V (devnum));
Stefan Roese887e2ec2006-09-07 11:51:23 +02001267 else {
1268 printf("Error in RGMII Speed\n");
1269 return -1;
1270 }
Stefan Roeseff768cb2007-10-31 18:01:24 +01001271 out_be32((void *)RGMII_SSR, reg);
wdenkba56f622004-02-06 23:19:44 +00001272 }
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001273#endif /* defined(CONFIG_440) && !defined(CONFIG_440SP) */
wdenkba56f622004-02-06 23:19:44 +00001274
Stefan Roesedbbd1252007-10-05 17:10:59 +02001275#if defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roese8ac41e32008-03-11 15:05:26 +01001276 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +02001277 defined(CONFIG_405EX)
Stefan Roesee54ec0f2008-04-03 14:50:34 +02001278 if (devnum >= 2)
1279 rgmii_channel = devnum - 2;
1280 else
1281 rgmii_channel = devnum;
1282
Stefan Roese887e2ec2006-09-07 11:51:23 +02001283 if (speed == 1000)
Stefan Roesee54ec0f2008-04-03 14:50:34 +02001284 reg = (RGMII_SSR_SP_1000MBPS << RGMII_SSR_V(rgmii_channel));
Stefan Roese887e2ec2006-09-07 11:51:23 +02001285 else if (speed == 100)
Stefan Roesee54ec0f2008-04-03 14:50:34 +02001286 reg = (RGMII_SSR_SP_100MBPS << RGMII_SSR_V(rgmii_channel));
Stefan Roese887e2ec2006-09-07 11:51:23 +02001287 else if (speed == 10)
Stefan Roesee54ec0f2008-04-03 14:50:34 +02001288 reg = (RGMII_SSR_SP_10MBPS << RGMII_SSR_V(rgmii_channel));
Stefan Roese887e2ec2006-09-07 11:51:23 +02001289 else {
1290 printf("Error in RGMII Speed\n");
1291 return -1;
1292 }
Stefan Roese2d834762007-10-23 14:03:17 +02001293 out_be32((void *)RGMII_SSR, reg);
Stefan Roese8ac41e32008-03-11 15:05:26 +01001294#if defined(CONFIG_460GT)
1295 if ((devnum == 2) || (devnum == 3))
1296 out_be32((void *)RGMII_SSR + RGMII1_BASE_OFFSET, reg);
1297#endif
Stefan Roese887e2ec2006-09-07 11:51:23 +02001298#endif
1299
wdenkba56f622004-02-06 23:19:44 +00001300 /* set the Mal configuration reg */
Stefan Roese887e2ec2006-09-07 11:51:23 +02001301#if defined(CONFIG_440GX) || \
1302 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +02001303 defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
Stefan Roese8ac41e32008-03-11 15:05:26 +01001304 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +02001305 defined(CONFIG_405EX)
Stefan Roesed1c3b272009-09-09 16:25:29 +02001306 mtdcr (MAL0_CFG, MAL_CR_PLBB | MAL_CR_OPBBL | MAL_CR_LEA |
Stefan Roese17f50f222005-08-04 17:09:16 +02001307 MAL_CR_PLBLT_DEFAULT | MAL_CR_EOPIE | 0x00330000);
1308#else
Stefan Roesed1c3b272009-09-09 16:25:29 +02001309 mtdcr (MAL0_CFG, MAL_CR_PLBB | MAL_CR_OPBBL | MAL_CR_LEA | MAL_CR_PLBLT_DEFAULT);
wdenkba56f622004-02-06 23:19:44 +00001310 /* Errata 1.12: MAL_1 -- Disable MAL bursting */
Stefan Roese17f50f222005-08-04 17:09:16 +02001311 if (get_pvr() == PVR_440GP_RB) {
Stefan Roesed1c3b272009-09-09 16:25:29 +02001312 mtdcr (MAL0_CFG, mfdcr(MAL0_CFG) & ~MAL_CR_PLBB);
Stefan Roese17f50f222005-08-04 17:09:16 +02001313 }
1314#endif
wdenkba56f622004-02-06 23:19:44 +00001315
wdenkba56f622004-02-06 23:19:44 +00001316 /*
1317 * Malloc MAL buffer desciptors, make sure they are
1318 * aligned on cache line boundary size
1319 * (401/403/IOP480 = 16, 405 = 32)
1320 * and doesn't cross cache block boundaries.
1321 */
Stefan Roeseff768cb2007-10-31 18:01:24 +01001322 if (hw_p->first_init == 0) {
1323 debug("*** Allocating descriptor memory ***\n");
wdenkba56f622004-02-06 23:19:44 +00001324
Stefan Roeseff768cb2007-10-31 18:01:24 +01001325 bd_cached = (u32)malloc_aligned(MAL_ALLOC_SIZE, 4096);
1326 if (!bd_cached) {
Stefan Roeseb0021442008-07-10 09:58:06 +02001327 printf("%s: Error allocating MAL descriptor buffers!\n", __func__);
Stefan Roeseff768cb2007-10-31 18:01:24 +01001328 return -1;
1329 }
Stefan Roeseb79316f2005-08-15 12:31:23 +02001330
Stefan Roeseff768cb2007-10-31 18:01:24 +01001331#ifdef CONFIG_4xx_DCACHE
Matthias Fuchsba79fde2007-12-14 11:19:56 +01001332 flush_dcache_range(bd_cached, bd_cached + MAL_ALLOC_SIZE);
Anatolij Gustschin4fae35a2008-02-25 20:54:04 +01001333 if (!last_used_ea)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001334#if defined(CONFIG_SYS_MEM_TOP_HIDE)
1335 bd_uncached = bis->bi_memsize + CONFIG_SYS_MEM_TOP_HIDE;
Anatolij Gustschin5e3dca52008-04-17 18:18:00 +02001336#else
Anatolij Gustschin4fae35a2008-02-25 20:54:04 +01001337 bd_uncached = bis->bi_memsize;
Anatolij Gustschin5e3dca52008-04-17 18:18:00 +02001338#endif
Anatolij Gustschin4fae35a2008-02-25 20:54:04 +01001339 else
1340 bd_uncached = last_used_ea + MAL_ALLOC_SIZE;
1341
1342 last_used_ea = bd_uncached;
Stefan Roeseff768cb2007-10-31 18:01:24 +01001343 program_tlb(bd_cached, bd_uncached, MAL_ALLOC_SIZE,
1344 TLB_WORD2_I_ENABLE);
1345#else
1346 bd_uncached = bd_cached;
1347#endif
1348 hw_p->tx_phys = bd_cached;
1349 hw_p->rx_phys = bd_cached + MAL_TX_DESC_SIZE;
1350 hw_p->tx = (mal_desc_t *)(bd_uncached);
1351 hw_p->rx = (mal_desc_t *)(bd_uncached + MAL_TX_DESC_SIZE);
Marek Vasutc97e74a2011-10-21 14:17:13 +00001352 debug("hw_p->tx=%p, hw_p->rx=%p\n", hw_p->tx, hw_p->rx);
wdenkba56f622004-02-06 23:19:44 +00001353 }
1354
1355 for (i = 0; i < NUM_TX_BUFF; i++) {
1356 hw_p->tx[i].ctrl = 0;
1357 hw_p->tx[i].data_len = 0;
Stefan Roeseff768cb2007-10-31 18:01:24 +01001358 if (hw_p->first_init == 0)
1359 hw_p->txbuf_ptr = malloc_aligned(MAL_ALLOC_SIZE,
1360 L1_CACHE_BYTES);
wdenkba56f622004-02-06 23:19:44 +00001361 hw_p->tx[i].data_ptr = hw_p->txbuf_ptr;
1362 if ((NUM_TX_BUFF - 1) == i)
1363 hw_p->tx[i].ctrl |= MAL_TX_CTRL_WRAP;
1364 hw_p->tx_run[i] = -1;
Marek Vasutc97e74a2011-10-21 14:17:13 +00001365 debug("TX_BUFF %d @ 0x%08x\n", i, (u32)hw_p->tx[i].data_ptr);
wdenkba56f622004-02-06 23:19:44 +00001366 }
1367
1368 for (i = 0; i < NUM_RX_BUFF; i++) {
1369 hw_p->rx[i].ctrl = 0;
1370 hw_p->rx[i].data_len = 0;
Stefan Roeseff768cb2007-10-31 18:01:24 +01001371 hw_p->rx[i].data_ptr = (char *)NetRxPackets[i];
wdenkba56f622004-02-06 23:19:44 +00001372 if ((NUM_RX_BUFF - 1) == i)
1373 hw_p->rx[i].ctrl |= MAL_RX_CTRL_WRAP;
1374 hw_p->rx[i].ctrl |= MAL_RX_CTRL_EMPTY | MAL_RX_CTRL_INTR;
1375 hw_p->rx_ready[i] = -1;
Marek Vasutc97e74a2011-10-21 14:17:13 +00001376 debug("RX_BUFF %d @ 0x%08x\n", i, (u32)hw_p->rx[i].data_ptr);
wdenkba56f622004-02-06 23:19:44 +00001377 }
1378
1379 reg = 0x00000000;
1380
1381 reg |= dev->enetaddr[0]; /* set high address */
1382 reg = reg << 8;
1383 reg |= dev->enetaddr[1];
1384
Niklaus Gigerddc922f2009-10-04 20:04:20 +02001385 out_be32((void *)EMAC0_IAH + hw_p->hw_addr, reg);
wdenkba56f622004-02-06 23:19:44 +00001386
1387 reg = 0x00000000;
1388 reg |= dev->enetaddr[2]; /* set low address */
1389 reg = reg << 8;
1390 reg |= dev->enetaddr[3];
1391 reg = reg << 8;
1392 reg |= dev->enetaddr[4];
1393 reg = reg << 8;
1394 reg |= dev->enetaddr[5];
1395
Niklaus Gigerddc922f2009-10-04 20:04:20 +02001396 out_be32((void *)EMAC0_IAL + hw_p->hw_addr, reg);
wdenkba56f622004-02-06 23:19:44 +00001397
1398 switch (devnum) {
1399 case 1:
1400 /* setup MAL tx & rx channel pointers */
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001401#if defined (CONFIG_405EP) || defined (CONFIG_440EP) || defined (CONFIG_440GR)
Stefan Roesed1c3b272009-09-09 16:25:29 +02001402 mtdcr (MAL0_TXCTP2R, hw_p->tx_phys);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001403#else
Stefan Roesed1c3b272009-09-09 16:25:29 +02001404 mtdcr (MAL0_TXCTP1R, hw_p->tx_phys);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001405#endif
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001406#if defined(CONFIG_440)
Stefan Roesed1c3b272009-09-09 16:25:29 +02001407 mtdcr (MAL0_TXBADDR, 0x0);
1408 mtdcr (MAL0_RXBADDR, 0x0);
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001409#endif
Stefan Roese8ac41e32008-03-11 15:05:26 +01001410
1411#if defined(CONFIG_460EX) || defined(CONFIG_460GT)
Stefan Roesed1c3b272009-09-09 16:25:29 +02001412 mtdcr (MAL0_RXCTP8R, hw_p->rx_phys);
Stefan Roese8ac41e32008-03-11 15:05:26 +01001413 /* set RX buffer size */
Stefan Roesed1c3b272009-09-09 16:25:29 +02001414 mtdcr (MAL0_RCBS8, ENET_MAX_MTU_ALIGNED / 16);
Stefan Roese8ac41e32008-03-11 15:05:26 +01001415#else
Stefan Roesed1c3b272009-09-09 16:25:29 +02001416 mtdcr (MAL0_RXCTP1R, hw_p->rx_phys);
wdenkba56f622004-02-06 23:19:44 +00001417 /* set RX buffer size */
Stefan Roesed1c3b272009-09-09 16:25:29 +02001418 mtdcr (MAL0_RCBS1, ENET_MAX_MTU_ALIGNED / 16);
Stefan Roese8ac41e32008-03-11 15:05:26 +01001419#endif
wdenkba56f622004-02-06 23:19:44 +00001420 break;
Stefan Roese846b0dd2005-08-08 12:42:22 +02001421#if defined (CONFIG_440GX)
wdenkba56f622004-02-06 23:19:44 +00001422 case 2:
1423 /* setup MAL tx & rx channel pointers */
Stefan Roesed1c3b272009-09-09 16:25:29 +02001424 mtdcr (MAL0_TXBADDR, 0x0);
1425 mtdcr (MAL0_RXBADDR, 0x0);
1426 mtdcr (MAL0_TXCTP2R, hw_p->tx_phys);
1427 mtdcr (MAL0_RXCTP2R, hw_p->rx_phys);
wdenkba56f622004-02-06 23:19:44 +00001428 /* set RX buffer size */
Stefan Roesed1c3b272009-09-09 16:25:29 +02001429 mtdcr (MAL0_RCBS2, ENET_MAX_MTU_ALIGNED / 16);
wdenkba56f622004-02-06 23:19:44 +00001430 break;
1431 case 3:
1432 /* setup MAL tx & rx channel pointers */
Stefan Roesed1c3b272009-09-09 16:25:29 +02001433 mtdcr (MAL0_TXBADDR, 0x0);
1434 mtdcr (MAL0_TXCTP3R, hw_p->tx_phys);
1435 mtdcr (MAL0_RXBADDR, 0x0);
1436 mtdcr (MAL0_RXCTP3R, hw_p->rx_phys);
wdenkba56f622004-02-06 23:19:44 +00001437 /* set RX buffer size */
Stefan Roesed1c3b272009-09-09 16:25:29 +02001438 mtdcr (MAL0_RCBS3, ENET_MAX_MTU_ALIGNED / 16);
wdenkba56f622004-02-06 23:19:44 +00001439 break;
Stefan Roesec57c7982005-08-11 17:56:56 +02001440#endif /* CONFIG_440GX */
Stefan Roese4c9e8552008-03-19 16:20:49 +01001441#if defined (CONFIG_460GT)
1442 case 2:
1443 /* setup MAL tx & rx channel pointers */
Stefan Roesed1c3b272009-09-09 16:25:29 +02001444 mtdcr (MAL0_TXBADDR, 0x0);
1445 mtdcr (MAL0_RXBADDR, 0x0);
1446 mtdcr (MAL0_TXCTP2R, hw_p->tx_phys);
1447 mtdcr (MAL0_RXCTP16R, hw_p->rx_phys);
Stefan Roese4c9e8552008-03-19 16:20:49 +01001448 /* set RX buffer size */
Stefan Roesed1c3b272009-09-09 16:25:29 +02001449 mtdcr (MAL0_RCBS16, ENET_MAX_MTU_ALIGNED / 16);
Stefan Roese4c9e8552008-03-19 16:20:49 +01001450 break;
1451 case 3:
1452 /* setup MAL tx & rx channel pointers */
Stefan Roesed1c3b272009-09-09 16:25:29 +02001453 mtdcr (MAL0_TXBADDR, 0x0);
1454 mtdcr (MAL0_RXBADDR, 0x0);
1455 mtdcr (MAL0_TXCTP3R, hw_p->tx_phys);
1456 mtdcr (MAL0_RXCTP24R, hw_p->rx_phys);
Stefan Roese4c9e8552008-03-19 16:20:49 +01001457 /* set RX buffer size */
Stefan Roesed1c3b272009-09-09 16:25:29 +02001458 mtdcr (MAL0_RCBS24, ENET_MAX_MTU_ALIGNED / 16);
Stefan Roese4c9e8552008-03-19 16:20:49 +01001459 break;
1460#endif /* CONFIG_460GT */
wdenkba56f622004-02-06 23:19:44 +00001461 case 0:
1462 default:
1463 /* setup MAL tx & rx channel pointers */
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001464#if defined(CONFIG_440)
Stefan Roesed1c3b272009-09-09 16:25:29 +02001465 mtdcr (MAL0_TXBADDR, 0x0);
1466 mtdcr (MAL0_RXBADDR, 0x0);
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001467#endif
Stefan Roesed1c3b272009-09-09 16:25:29 +02001468 mtdcr (MAL0_TXCTP0R, hw_p->tx_phys);
1469 mtdcr (MAL0_RXCTP0R, hw_p->rx_phys);
wdenkba56f622004-02-06 23:19:44 +00001470 /* set RX buffer size */
Stefan Roesed1c3b272009-09-09 16:25:29 +02001471 mtdcr (MAL0_RCBS0, ENET_MAX_MTU_ALIGNED / 16);
wdenkba56f622004-02-06 23:19:44 +00001472 break;
1473 }
1474
1475 /* Enable MAL transmit and receive channels */
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001476#if defined(CONFIG_405EP) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
Stefan Roesed1c3b272009-09-09 16:25:29 +02001477 mtdcr (MAL0_TXCASR, (MAL_TXRX_CASR >> (hw_p->devnum*2)));
Stefan Roesec157d8e2005-08-01 16:41:48 +02001478#else
Stefan Roesed1c3b272009-09-09 16:25:29 +02001479 mtdcr (MAL0_TXCASR, (MAL_TXRX_CASR >> hw_p->devnum));
Stefan Roesec157d8e2005-08-01 16:41:48 +02001480#endif
Stefan Roesed1c3b272009-09-09 16:25:29 +02001481 mtdcr (MAL0_RXCASR, (MAL_TXRX_CASR >> hw_p->devnum));
wdenkba56f622004-02-06 23:19:44 +00001482
1483 /* set transmit enable & receive enable */
Niklaus Gigerddc922f2009-10-04 20:04:20 +02001484 out_be32((void *)EMAC0_MR0 + hw_p->hw_addr, EMAC_MR0_TXE | EMAC_MR0_RXE);
wdenkba56f622004-02-06 23:19:44 +00001485
Niklaus Gigerddc922f2009-10-04 20:04:20 +02001486 mode_reg = in_be32((void *)EMAC0_MR1 + hw_p->hw_addr);
Stefan Roese76957cb2008-03-01 12:11:40 +01001487
1488 /* set rx-/tx-fifo size */
1489 mode_reg = (mode_reg & ~EMAC_MR1_FIFO_MASK) | EMAC_MR1_FIFO_SIZE;
wdenkba56f622004-02-06 23:19:44 +00001490
1491 /* set speed */
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001492 if (speed == _1000BASET) {
Stefan Roese5e7abce2010-09-11 09:31:43 +02001493#if defined(CONFIG_440SP) || defined(CONFIG_440SPE)
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001494 unsigned long pfc1;
Stefan Roese887e2ec2006-09-07 11:51:23 +02001495
Stefan Roesed1c3b272009-09-09 16:25:29 +02001496 mfsdr (SDR0_PFC1, pfc1);
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001497 pfc1 |= SDR0_PFC1_EM_1000;
Stefan Roesed1c3b272009-09-09 16:25:29 +02001498 mtsdr (SDR0_PFC1, pfc1);
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001499#endif
Niklaus Gigerddc922f2009-10-04 20:04:20 +02001500 mode_reg = mode_reg | EMAC_MR1_MF_1000MBPS | EMAC_MR1_IST;
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001501 } else if (speed == _100BASET)
Niklaus Gigerddc922f2009-10-04 20:04:20 +02001502 mode_reg = mode_reg | EMAC_MR1_MF_100MBPS | EMAC_MR1_IST;
wdenkba56f622004-02-06 23:19:44 +00001503 else
1504 mode_reg = mode_reg & ~0x00C00000; /* 10 MBPS */
1505 if (duplex == FULL)
Niklaus Gigerddc922f2009-10-04 20:04:20 +02001506 mode_reg = mode_reg | 0x80000000 | EMAC_MR1_IST;
wdenkba56f622004-02-06 23:19:44 +00001507
Niklaus Gigerddc922f2009-10-04 20:04:20 +02001508 out_be32((void *)EMAC0_MR1 + hw_p->hw_addr, mode_reg);
wdenkba56f622004-02-06 23:19:44 +00001509
1510 /* Enable broadcast and indvidual address */
1511 /* TBS: enabling runts as some misbehaved nics will send runts */
Niklaus Gigerddc922f2009-10-04 20:04:20 +02001512 out_be32((void *)EMAC0_RXM + hw_p->hw_addr, EMAC_RMR_BAE | EMAC_RMR_IAE);
wdenkba56f622004-02-06 23:19:44 +00001513
1514 /* we probably need to set the tx mode1 reg? maybe at tx time */
1515
1516 /* set transmit request threshold register */
Niklaus Gigerddc922f2009-10-04 20:04:20 +02001517 out_be32((void *)EMAC0_TRTR + hw_p->hw_addr, 0x18000000); /* 256 byte threshold */
wdenkba56f622004-02-06 23:19:44 +00001518
Wolfgang Denk265817c2005-09-25 00:53:22 +02001519 /* set receive low/high water mark register */
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001520#if defined(CONFIG_440)
Marian Balakowicz6c5879f2006-06-30 16:30:46 +02001521 /* 440s has a 64 byte burst length */
Niklaus Gigerddc922f2009-10-04 20:04:20 +02001522 out_be32((void *)EMAC0_RX_HI_LO_WMARK + hw_p->hw_addr, 0x80009000);
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001523#else
1524 /* 405s have a 16 byte burst length */
Niklaus Gigerddc922f2009-10-04 20:04:20 +02001525 out_be32((void *)EMAC0_RX_HI_LO_WMARK + hw_p->hw_addr, 0x0f002000);
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001526#endif /* defined(CONFIG_440) */
Niklaus Gigerddc922f2009-10-04 20:04:20 +02001527 out_be32((void *)EMAC0_TMR1 + hw_p->hw_addr, 0xf8640000);
wdenkba56f622004-02-06 23:19:44 +00001528
1529 /* Set fifo limit entry in tx mode 0 */
Niklaus Gigerddc922f2009-10-04 20:04:20 +02001530 out_be32((void *)EMAC0_TMR0 + hw_p->hw_addr, 0x00000003);
wdenkba56f622004-02-06 23:19:44 +00001531 /* Frame gap set */
Niklaus Gigerddc922f2009-10-04 20:04:20 +02001532 out_be32((void *)EMAC0_I_FRAME_GAP_REG + hw_p->hw_addr, 0x00000008);
wdenkba56f622004-02-06 23:19:44 +00001533
1534 /* Set EMAC IER */
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001535 hw_p->emac_ier = EMAC_ISR_PTLE | EMAC_ISR_BFCS | EMAC_ISR_ORE | EMAC_ISR_IRE;
wdenkba56f622004-02-06 23:19:44 +00001536 if (speed == _100BASET)
1537 hw_p->emac_ier = hw_p->emac_ier | EMAC_ISR_SYE;
1538
Niklaus Gigerddc922f2009-10-04 20:04:20 +02001539 out_be32((void *)EMAC0_ISR + hw_p->hw_addr, 0xffffffff); /* clear pending interrupts */
1540 out_be32((void *)EMAC0_IER + hw_p->hw_addr, hw_p->emac_ier);
wdenkba56f622004-02-06 23:19:44 +00001541
1542 if (hw_p->first_init == 0) {
1543 /*
1544 * Connect interrupt service routines
1545 */
Stefan Roesedbbd1252007-10-05 17:10:59 +02001546 irq_install_handler(ETH_IRQ_NUM(hw_p->devnum),
1547 (interrupt_handler_t *) enetInt, dev);
wdenkba56f622004-02-06 23:19:44 +00001548 }
wdenkba56f622004-02-06 23:19:44 +00001549
1550 mtmsr (msr); /* enable interrupts again */
1551
1552 hw_p->bis = bis;
1553 hw_p->first_init = 1;
1554
Stefan Roese802b7692008-01-08 18:39:30 +01001555 return 0;
wdenkba56f622004-02-06 23:19:44 +00001556}
1557
1558
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001559static int ppc_4xx_eth_send (struct eth_device *dev, volatile void *ptr,
wdenkba56f622004-02-06 23:19:44 +00001560 int len)
1561{
1562 struct enet_frame *ef_ptr;
1563 ulong time_start, time_now;
1564 unsigned long temp_txm0;
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001565 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenkba56f622004-02-06 23:19:44 +00001566
1567 ef_ptr = (struct enet_frame *) ptr;
1568
1569 /*-----------------------------------------------------------------------+
1570 * Copy in our address into the frame.
1571 *-----------------------------------------------------------------------*/
1572 (void) memcpy (ef_ptr->source_addr, dev->enetaddr, ENET_ADDR_LENGTH);
1573
1574 /*-----------------------------------------------------------------------+
1575 * If frame is too long or too short, modify length.
1576 *-----------------------------------------------------------------------*/
1577 /* TBS: where does the fragment go???? */
1578 if (len > ENET_MAX_MTU)
1579 len = ENET_MAX_MTU;
1580
1581 /* memcpy ((void *) &tx_buff[tx_slot], (const void *) ptr, len); */
1582 memcpy ((void *) hw_p->txbuf_ptr, (const void *) ptr, len);
Matthias Fuchsba79fde2007-12-14 11:19:56 +01001583 flush_dcache_range((u32)hw_p->txbuf_ptr, (u32)hw_p->txbuf_ptr + len);
wdenkba56f622004-02-06 23:19:44 +00001584
1585 /*-----------------------------------------------------------------------+
1586 * set TX Buffer busy, and send it
1587 *-----------------------------------------------------------------------*/
1588 hw_p->tx[hw_p->tx_slot].ctrl = (MAL_TX_CTRL_LAST |
1589 EMAC_TX_CTRL_GFCS | EMAC_TX_CTRL_GP) &
1590 ~(EMAC_TX_CTRL_ISA | EMAC_TX_CTRL_RSA);
1591 if ((NUM_TX_BUFF - 1) == hw_p->tx_slot)
1592 hw_p->tx[hw_p->tx_slot].ctrl |= MAL_TX_CTRL_WRAP;
1593
1594 hw_p->tx[hw_p->tx_slot].data_len = (short) len;
1595 hw_p->tx[hw_p->tx_slot].ctrl |= MAL_TX_CTRL_READY;
1596
Stefan Roese8ac41e32008-03-11 15:05:26 +01001597 sync();
wdenkba56f622004-02-06 23:19:44 +00001598
Niklaus Gigerddc922f2009-10-04 20:04:20 +02001599 out_be32((void *)EMAC0_TMR0 + hw_p->hw_addr,
1600 in_be32((void *)EMAC0_TMR0 + hw_p->hw_addr) | EMAC_TMR0_GNP0);
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001601#ifdef INFO_4XX_ENET
wdenkba56f622004-02-06 23:19:44 +00001602 hw_p->stats.pkts_tx++;
1603#endif
1604
1605 /*-----------------------------------------------------------------------+
1606 * poll unitl the packet is sent and then make sure it is OK
1607 *-----------------------------------------------------------------------*/
1608 time_start = get_timer (0);
1609 while (1) {
Niklaus Gigerddc922f2009-10-04 20:04:20 +02001610 temp_txm0 = in_be32((void *)EMAC0_TMR0 + hw_p->hw_addr);
wdenkba56f622004-02-06 23:19:44 +00001611 /* loop until either TINT turns on or 3 seconds elapse */
Niklaus Gigerddc922f2009-10-04 20:04:20 +02001612 if ((temp_txm0 & EMAC_TMR0_GNP0) != 0) {
wdenkba56f622004-02-06 23:19:44 +00001613 /* transmit is done, so now check for errors
1614 * If there is an error, an interrupt should
1615 * happen when we return
1616 */
1617 time_now = get_timer (0);
1618 if ((time_now - time_start) > 3000) {
1619 return (-1);
1620 }
1621 } else {
1622 return (len);
1623 }
1624 }
1625}
1626
wdenkba56f622004-02-06 23:19:44 +00001627int enetInt (struct eth_device *dev)
1628{
1629 int serviced;
1630 int rc = -1; /* default to not us */
Stefan Roesed1631fe2008-06-26 13:40:57 +02001631 u32 mal_isr;
1632 u32 emac_isr = 0;
1633 u32 mal_eob;
1634 u32 uic_mal;
1635 u32 uic_mal_err;
1636 u32 uic_emac;
1637 u32 uic_emac_b;
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001638 EMAC_4XX_HW_PST hw_p;
wdenkba56f622004-02-06 23:19:44 +00001639
1640 /*
1641 * Because the mal is generic, we need to get the current
1642 * eth device
1643 */
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001644 dev = eth_get_dev();
wdenkba56f622004-02-06 23:19:44 +00001645
1646 hw_p = dev->priv;
1647
wdenkba56f622004-02-06 23:19:44 +00001648 /* enter loop that stays in interrupt code until nothing to service */
1649 do {
1650 serviced = 0;
1651
Stefan Roesed1631fe2008-06-26 13:40:57 +02001652 uic_mal = mfdcr(UIC_BASE_MAL + UIC_MSR);
1653 uic_mal_err = mfdcr(UIC_BASE_MAL_ERR + UIC_MSR);
1654 uic_emac = mfdcr(UIC_BASE_EMAC + UIC_MSR);
1655 uic_emac_b = mfdcr(UIC_BASE_EMAC_B + UIC_MSR);
Stefan Roese887e2ec2006-09-07 11:51:23 +02001656
Stefan Roesed1631fe2008-06-26 13:40:57 +02001657 if (!(uic_mal & (UIC_MAL_RXEOB | UIC_MAL_TXEOB))
1658 && !(uic_mal_err & (UIC_MAL_SERR | UIC_MAL_TXDE | UIC_MAL_RXDE))
1659 && !(uic_emac & UIC_ETHx) && !(uic_emac_b & UIC_ETHxB)) {
wdenkba56f622004-02-06 23:19:44 +00001660 /* not for us */
1661 return (rc);
1662 }
Stefan Roesed1631fe2008-06-26 13:40:57 +02001663
wdenkba56f622004-02-06 23:19:44 +00001664 /* get and clear controller status interrupts */
Stefan Roesed1631fe2008-06-26 13:40:57 +02001665 /* look at MAL and EMAC error interrupts */
1666 if (uic_mal_err & (UIC_MAL_SERR | UIC_MAL_TXDE | UIC_MAL_RXDE)) {
1667 /* we have a MAL error interrupt */
Stefan Roesed1c3b272009-09-09 16:25:29 +02001668 mal_isr = mfdcr(MAL0_ESR);
Stefan Roesed1631fe2008-06-26 13:40:57 +02001669 mal_err(dev, mal_isr, uic_mal_err,
1670 MAL_UIC_DEF, MAL_UIC_ERR);
1671
1672 /* clear MAL error interrupt status bits */
1673 mtdcr(UIC_BASE_MAL_ERR + UIC_SR,
1674 UIC_MAL_SERR | UIC_MAL_TXDE | UIC_MAL_RXDE);
1675
1676 return -1;
wdenkba56f622004-02-06 23:19:44 +00001677 }
1678
Stefan Roesed1631fe2008-06-26 13:40:57 +02001679 /* look for EMAC errors */
1680 if ((uic_emac & UIC_ETHx) || (uic_emac_b & UIC_ETHxB)) {
Niklaus Gigerddc922f2009-10-04 20:04:20 +02001681 emac_isr = in_be32((void *)EMAC0_ISR + hw_p->hw_addr);
Stefan Roesed1631fe2008-06-26 13:40:57 +02001682 emac_err(dev, emac_isr);
wdenkba56f622004-02-06 23:19:44 +00001683
Stefan Roesed1631fe2008-06-26 13:40:57 +02001684 /* clear EMAC error interrupt status bits */
1685 mtdcr(UIC_BASE_EMAC + UIC_SR, UIC_ETHx);
1686 mtdcr(UIC_BASE_EMAC_B + UIC_SR, UIC_ETHxB);
wdenkba56f622004-02-06 23:19:44 +00001687
Stefan Roesed1631fe2008-06-26 13:40:57 +02001688 return -1;
wdenkba56f622004-02-06 23:19:44 +00001689 }
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001690
wdenkba56f622004-02-06 23:19:44 +00001691 /* handle MAX TX EOB interrupt from a tx */
Stefan Roesed1631fe2008-06-26 13:40:57 +02001692 if (uic_mal & UIC_MAL_TXEOB) {
1693 /* clear MAL interrupt status bits */
Stefan Roesed1c3b272009-09-09 16:25:29 +02001694 mal_eob = mfdcr(MAL0_TXEOBISR);
1695 mtdcr(MAL0_TXEOBISR, mal_eob);
Stefan Roesed1631fe2008-06-26 13:40:57 +02001696 mtdcr(UIC_BASE_MAL + UIC_SR, UIC_MAL_TXEOB);
1697
1698 /* indicate that we serviced an interrupt */
1699 serviced = 1;
1700 rc = 0;
wdenkba56f622004-02-06 23:19:44 +00001701 }
Stefan Roesed1631fe2008-06-26 13:40:57 +02001702
Mike Williams16263082011-07-22 04:01:30 +00001703 /* handle MAL RX EOB interrupt from a receive */
Stefan Roesed1631fe2008-06-26 13:40:57 +02001704 /* check for EOB on valid channels */
1705 if (uic_mal & UIC_MAL_RXEOB) {
Stefan Roesed1c3b272009-09-09 16:25:29 +02001706 mal_eob = mfdcr(MAL0_RXEOBISR);
Stefan Roesed1631fe2008-06-26 13:40:57 +02001707 if (mal_eob &
1708 (0x80000000 >> (hw_p->devnum * MAL_RX_CHAN_MUL))) {
1709 /* push packet to upper layer */
1710 enet_rcv(dev, emac_isr);
1711
1712 /* clear MAL interrupt status bits */
1713 mtdcr(UIC_BASE_MAL + UIC_SR, UIC_MAL_RXEOB);
1714
wdenkba56f622004-02-06 23:19:44 +00001715 /* indicate that we serviced an interrupt */
1716 serviced = 1;
1717 rc = 0;
1718 }
1719 }
James Cloughc0b46d82009-09-10 09:11:50 +02001720#if defined(CONFIG_405EZ)
1721 /*
1722 * On 405EZ the RX-/TX-interrupts are coalesced into
1723 * one IRQ bit in the UIC. We need to acknowledge the
1724 * RX-/TX-interrupts in the SDR0_ICINTSTAT reg as well.
1725 */
1726 mtsdr(SDR0_ICINTSTAT,
1727 SDR_ICRX_STAT | SDR_ICTX0_STAT | SDR_ICTX1_STAT);
1728#endif /* defined(CONFIG_405EZ) */
wdenkba56f622004-02-06 23:19:44 +00001729 } while (serviced);
1730
1731 return (rc);
1732}
1733
1734/*-----------------------------------------------------------------------------+
1735 * MAL Error Routine
1736 *-----------------------------------------------------------------------------*/
1737static void mal_err (struct eth_device *dev, unsigned long isr,
1738 unsigned long uic, unsigned long maldef,
1739 unsigned long mal_errr)
1740{
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001741 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenkba56f622004-02-06 23:19:44 +00001742
Stefan Roesed1c3b272009-09-09 16:25:29 +02001743 mtdcr (MAL0_ESR, isr); /* clear interrupt */
wdenkba56f622004-02-06 23:19:44 +00001744
1745 /* clear DE interrupt */
Stefan Roesed1c3b272009-09-09 16:25:29 +02001746 mtdcr (MAL0_TXDEIR, 0xC0000000);
1747 mtdcr (MAL0_RXDEIR, 0x80000000);
wdenkba56f622004-02-06 23:19:44 +00001748
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001749#ifdef INFO_4XX_ENET
Wolfgang Denk265817c2005-09-25 00:53:22 +02001750 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 +00001751#endif
1752
1753 eth_init (hw_p->bis); /* start again... */
1754}
1755
1756/*-----------------------------------------------------------------------------+
1757 * EMAC Error Routine
1758 *-----------------------------------------------------------------------------*/
1759static void emac_err (struct eth_device *dev, unsigned long isr)
1760{
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001761 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenkba56f622004-02-06 23:19:44 +00001762
1763 printf ("EMAC%d error occured.... ISR = %lx\n", hw_p->devnum, isr);
Niklaus Gigerddc922f2009-10-04 20:04:20 +02001764 out_be32((void *)EMAC0_ISR + hw_p->hw_addr, isr);
wdenkba56f622004-02-06 23:19:44 +00001765}
1766
1767/*-----------------------------------------------------------------------------+
1768 * enet_rcv() handles the ethernet receive data
1769 *-----------------------------------------------------------------------------*/
1770static void enet_rcv (struct eth_device *dev, unsigned long malisr)
1771{
wdenkba56f622004-02-06 23:19:44 +00001772 unsigned long data_len;
1773 unsigned long rx_eob_isr;
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001774 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenkba56f622004-02-06 23:19:44 +00001775
1776 int handled = 0;
1777 int i;
1778 int loop_count = 0;
1779
Stefan Roesed1c3b272009-09-09 16:25:29 +02001780 rx_eob_isr = mfdcr (MAL0_RXEOBISR);
Stefan Roese8ac41e32008-03-11 15:05:26 +01001781 if ((0x80000000 >> (hw_p->devnum * MAL_RX_CHAN_MUL)) & rx_eob_isr) {
wdenkba56f622004-02-06 23:19:44 +00001782 /* clear EOB */
Stefan Roesed1c3b272009-09-09 16:25:29 +02001783 mtdcr (MAL0_RXEOBISR, rx_eob_isr);
wdenkba56f622004-02-06 23:19:44 +00001784
1785 /* EMAC RX done */
1786 while (1) { /* do all */
1787 i = hw_p->rx_slot;
1788
1789 if ((MAL_RX_CTRL_EMPTY & hw_p->rx[i].ctrl)
1790 || (loop_count >= NUM_RX_BUFF))
1791 break;
Stefan Roesea2e1c702007-07-12 16:32:08 +02001792
wdenkba56f622004-02-06 23:19:44 +00001793 loop_count++;
wdenkba56f622004-02-06 23:19:44 +00001794 handled++;
Stefan Roese8ac41e32008-03-11 15:05:26 +01001795 data_len = (unsigned long) hw_p->rx[i].data_len & 0x0fff; /* Get len */
wdenkba56f622004-02-06 23:19:44 +00001796 if (data_len) {
1797 if (data_len > ENET_MAX_MTU) /* Check len */
1798 data_len = 0;
1799 else {
1800 if (EMAC_RX_ERRORS & hw_p->rx[i].ctrl) { /* Check Errors */
1801 data_len = 0;
1802 hw_p->stats.rx_err_log[hw_p->
1803 rx_err_index]
1804 = hw_p->rx[i].ctrl;
1805 hw_p->rx_err_index++;
1806 if (hw_p->rx_err_index ==
1807 MAX_ERR_LOG)
1808 hw_p->rx_err_index =
1809 0;
wdenkfc1cfcd2004-04-25 15:41:35 +00001810 } /* emac_erros */
wdenkba56f622004-02-06 23:19:44 +00001811 } /* data_len < max mtu */
wdenkfc1cfcd2004-04-25 15:41:35 +00001812 } /* if data_len */
wdenkba56f622004-02-06 23:19:44 +00001813 if (!data_len) { /* no data */
1814 hw_p->rx[i].ctrl |= MAL_RX_CTRL_EMPTY; /* Free Recv Buffer */
1815
1816 hw_p->stats.data_len_err++; /* Error at Rx */
1817 }
1818
1819 /* !data_len */
1820 /* AS.HARNOIS */
1821 /* Check if user has already eaten buffer */
1822 /* if not => ERROR */
1823 else if (hw_p->rx_ready[hw_p->rx_i_index] != -1) {
1824 if (hw_p->is_receiving)
1825 printf ("ERROR : Receive buffers are full!\n");
1826 break;
1827 } else {
1828 hw_p->stats.rx_frames++;
1829 hw_p->stats.rx += data_len;
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001830#ifdef INFO_4XX_ENET
wdenkba56f622004-02-06 23:19:44 +00001831 hw_p->stats.pkts_rx++;
1832#endif
1833 /* AS.HARNOIS
1834 * use ring buffer
1835 */
1836 hw_p->rx_ready[hw_p->rx_i_index] = i;
1837 hw_p->rx_i_index++;
1838 if (NUM_RX_BUFF == hw_p->rx_i_index)
1839 hw_p->rx_i_index = 0;
1840
Stefan Roesea2e1c702007-07-12 16:32:08 +02001841 hw_p->rx_slot++;
1842 if (NUM_RX_BUFF == hw_p->rx_slot)
1843 hw_p->rx_slot = 0;
1844
wdenkba56f622004-02-06 23:19:44 +00001845 /* AS.HARNOIS
1846 * free receive buffer only when
1847 * buffer has been handled (eth_rx)
1848 rx[i].ctrl |= MAL_RX_CTRL_EMPTY;
1849 */
1850 } /* if data_len */
1851 } /* while */
1852 } /* if EMACK_RXCHL */
1853}
1854
1855
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001856static int ppc_4xx_eth_rx (struct eth_device *dev)
wdenkba56f622004-02-06 23:19:44 +00001857{
1858 int length;
1859 int user_index;
1860 unsigned long msr;
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001861 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenkba56f622004-02-06 23:19:44 +00001862
Wolfgang Denk265817c2005-09-25 00:53:22 +02001863 hw_p->is_receiving = 1; /* tell driver */
wdenkba56f622004-02-06 23:19:44 +00001864
1865 for (;;) {
1866 /* AS.HARNOIS
1867 * use ring buffer and
1868 * get index from rx buffer desciptor queue
1869 */
1870 user_index = hw_p->rx_ready[hw_p->rx_u_index];
1871 if (user_index == -1) {
1872 length = -1;
1873 break; /* nothing received - leave for() loop */
1874 }
1875
1876 msr = mfmsr ();
1877 mtmsr (msr & ~(MSR_EE));
1878
Stefan Roese8ac41e32008-03-11 15:05:26 +01001879 length = hw_p->rx[user_index].data_len & 0x0fff;
wdenkba56f622004-02-06 23:19:44 +00001880
1881 /* Pass the packet up to the protocol layers. */
Wolfgang Denk265817c2005-09-25 00:53:22 +02001882 /* NetReceive(NetRxPackets[rxIdx], length - 4); */
1883 /* NetReceive(NetRxPackets[i], length); */
Stefan Roeseff768cb2007-10-31 18:01:24 +01001884 invalidate_dcache_range((u32)hw_p->rx[user_index].data_ptr,
1885 (u32)hw_p->rx[user_index].data_ptr +
Matthias Fuchsba79fde2007-12-14 11:19:56 +01001886 length - 4);
wdenkba56f622004-02-06 23:19:44 +00001887 NetReceive (NetRxPackets[user_index], length - 4);
1888 /* Free Recv Buffer */
1889 hw_p->rx[user_index].ctrl |= MAL_RX_CTRL_EMPTY;
1890 /* Free rx buffer descriptor queue */
1891 hw_p->rx_ready[hw_p->rx_u_index] = -1;
1892 hw_p->rx_u_index++;
1893 if (NUM_RX_BUFF == hw_p->rx_u_index)
1894 hw_p->rx_u_index = 0;
1895
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001896#ifdef INFO_4XX_ENET
wdenkba56f622004-02-06 23:19:44 +00001897 hw_p->stats.pkts_handled++;
1898#endif
1899
1900 mtmsr (msr); /* Enable IRQ's */
1901 }
1902
Wolfgang Denk265817c2005-09-25 00:53:22 +02001903 hw_p->is_receiving = 0; /* tell driver */
wdenkba56f622004-02-06 23:19:44 +00001904
1905 return length;
1906}
1907
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001908int ppc_4xx_eth_initialize (bd_t * bis)
wdenkba56f622004-02-06 23:19:44 +00001909{
1910 static int virgin = 0;
wdenkba56f622004-02-06 23:19:44 +00001911 struct eth_device *dev;
1912 int eth_num = 0;
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001913 EMAC_4XX_HW_PST hw = NULL;
Stefan Roese5fb692c2007-01-18 10:25:34 +01001914 u8 ethaddr[4 + CONFIG_EMAC_NR_START][6];
1915 u32 hw_addr[4];
Stefan Roesed1631fe2008-06-26 13:40:57 +02001916 u32 mal_ier;
wdenkba56f622004-02-06 23:19:44 +00001917
Stefan Roese846b0dd2005-08-08 12:42:22 +02001918#if defined(CONFIG_440GX)
Stefan Roesec157d8e2005-08-01 16:41:48 +02001919 unsigned long pfc1;
1920
Stefan Roesed1c3b272009-09-09 16:25:29 +02001921 mfsdr (SDR0_PFC1, pfc1);
wdenkba56f622004-02-06 23:19:44 +00001922 pfc1 &= ~(0x01e00000);
1923 pfc1 |= 0x01200000;
Stefan Roesed1c3b272009-09-09 16:25:29 +02001924 mtsdr (SDR0_PFC1, pfc1);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001925#endif
Stefan Roese5fb692c2007-01-18 10:25:34 +01001926
1927 /* first clear all mac-addresses */
1928 for (eth_num = 0; eth_num < LAST_EMAC_NUM; eth_num++)
1929 memcpy(ethaddr[eth_num], "\0\0\0\0\0\0", 6);
1930
1931 for (eth_num = 0; eth_num < LAST_EMAC_NUM; eth_num++) {
Mike Frysingerd3f87142009-02-11 19:01:26 -05001932 int ethaddr_idx = eth_num + CONFIG_EMAC_NR_START;
Stefan Roese5fb692c2007-01-18 10:25:34 +01001933 switch (eth_num) {
1934 default: /* fall through */
1935 case 0:
Mike Frysingerd3f87142009-02-11 19:01:26 -05001936 eth_getenv_enetaddr("ethaddr", ethaddr[ethaddr_idx]);
Stefan Roese5fb692c2007-01-18 10:25:34 +01001937 hw_addr[eth_num] = 0x0;
1938 break;
1939#ifdef CONFIG_HAS_ETH1
1940 case 1:
Mike Frysingerd3f87142009-02-11 19:01:26 -05001941 eth_getenv_enetaddr("eth1addr", ethaddr[ethaddr_idx]);
Stefan Roese5fb692c2007-01-18 10:25:34 +01001942 hw_addr[eth_num] = 0x100;
1943 break;
1944#endif
1945#ifdef CONFIG_HAS_ETH2
1946 case 2:
Mike Frysingerd3f87142009-02-11 19:01:26 -05001947 eth_getenv_enetaddr("eth2addr", ethaddr[ethaddr_idx]);
Stefan Roese4c9e8552008-03-19 16:20:49 +01001948#if defined(CONFIG_460GT)
1949 hw_addr[eth_num] = 0x300;
1950#else
Stefan Roese5fb692c2007-01-18 10:25:34 +01001951 hw_addr[eth_num] = 0x400;
Stefan Roese4c9e8552008-03-19 16:20:49 +01001952#endif
Stefan Roese5fb692c2007-01-18 10:25:34 +01001953 break;
1954#endif
1955#ifdef CONFIG_HAS_ETH3
1956 case 3:
Mike Frysingerd3f87142009-02-11 19:01:26 -05001957 eth_getenv_enetaddr("eth3addr", ethaddr[ethaddr_idx]);
Stefan Roese4c9e8552008-03-19 16:20:49 +01001958#if defined(CONFIG_460GT)
1959 hw_addr[eth_num] = 0x400;
1960#else
Stefan Roese5fb692c2007-01-18 10:25:34 +01001961 hw_addr[eth_num] = 0x600;
Stefan Roese4c9e8552008-03-19 16:20:49 +01001962#endif
Stefan Roese5fb692c2007-01-18 10:25:34 +01001963 break;
1964#endif
1965 }
1966 }
1967
wdenk3c74e322004-02-22 23:46:08 +00001968 /* set phy num and mode */
1969 bis->bi_phynum[0] = CONFIG_PHY_ADDR;
Marian Balakowicz6c5879f2006-06-30 16:30:46 +02001970 bis->bi_phymode[0] = 0;
1971
Stefan Roesec157d8e2005-08-01 16:41:48 +02001972#if defined(CONFIG_PHY1_ADDR)
wdenk3c74e322004-02-22 23:46:08 +00001973 bis->bi_phynum[1] = CONFIG_PHY1_ADDR;
Marian Balakowicz6c5879f2006-06-30 16:30:46 +02001974 bis->bi_phymode[1] = 0;
Stefan Roesec157d8e2005-08-01 16:41:48 +02001975#endif
Stefan Roese846b0dd2005-08-08 12:42:22 +02001976#if defined(CONFIG_440GX)
wdenk3c74e322004-02-22 23:46:08 +00001977 bis->bi_phynum[2] = CONFIG_PHY2_ADDR;
1978 bis->bi_phynum[3] = CONFIG_PHY3_ADDR;
wdenk3c74e322004-02-22 23:46:08 +00001979 bis->bi_phymode[2] = 2;
1980 bis->bi_phymode[3] = 2;
Stefan Roesedbbd1252007-10-05 17:10:59 +02001981#endif
wdenkba56f622004-02-06 23:19:44 +00001982
Stefan Roesedbbd1252007-10-05 17:10:59 +02001983#if defined(CONFIG_440GX) || \
1984 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
1985 defined(CONFIG_405EX)
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001986 ppc_4xx_eth_setup_bridge(0, bis);
wdenka06752e2004-09-29 22:43:59 +00001987#endif
1988
Stefan Roese1e25f952005-10-20 16:34:28 +02001989 for (eth_num = 0; eth_num < LAST_EMAC_NUM; eth_num++) {
Stefan Roese5fb692c2007-01-18 10:25:34 +01001990 /*
1991 * See if we can actually bring up the interface,
1992 * otherwise, skip it
1993 */
1994 if (memcmp (ethaddr[eth_num], "\0\0\0\0\0\0", 6) == 0) {
1995 bis->bi_phymode[eth_num] = BI_PHYMODE_NONE;
1996 continue;
wdenkba56f622004-02-06 23:19:44 +00001997 }
1998
1999 /* Allocate device structure */
2000 dev = (struct eth_device *) malloc (sizeof (*dev));
2001 if (dev == NULL) {
Stefan Roesed6c61aa2005-08-16 18:18:00 +02002002 printf ("ppc_4xx_eth_initialize: "
wdenk3f85ce22004-02-23 16:11:30 +00002003 "Cannot allocate eth_device %d\n", eth_num);
wdenkba56f622004-02-06 23:19:44 +00002004 return (-1);
2005 }
wdenkb2532ef2005-06-20 10:17:34 +00002006 memset(dev, 0, sizeof(*dev));
wdenkba56f622004-02-06 23:19:44 +00002007
2008 /* Allocate our private use data */
Stefan Roesed6c61aa2005-08-16 18:18:00 +02002009 hw = (EMAC_4XX_HW_PST) malloc (sizeof (*hw));
wdenkba56f622004-02-06 23:19:44 +00002010 if (hw == NULL) {
Stefan Roesed6c61aa2005-08-16 18:18:00 +02002011 printf ("ppc_4xx_eth_initialize: "
wdenk3f85ce22004-02-23 16:11:30 +00002012 "Cannot allocate private hw data for eth_device %d",
wdenkba56f622004-02-06 23:19:44 +00002013 eth_num);
2014 free (dev);
2015 return (-1);
2016 }
wdenkb2532ef2005-06-20 10:17:34 +00002017 memset(hw, 0, sizeof(*hw));
wdenkba56f622004-02-06 23:19:44 +00002018
Stefan Roese5fb692c2007-01-18 10:25:34 +01002019 hw->hw_addr = hw_addr[eth_num];
2020 memcpy (dev->enetaddr, ethaddr[eth_num], 6);
wdenkba56f622004-02-06 23:19:44 +00002021 hw->devnum = eth_num;
Stefan Roesec157d8e2005-08-01 16:41:48 +02002022 hw->print_speed = 1;
wdenkba56f622004-02-06 23:19:44 +00002023
Stefan Roese5fb692c2007-01-18 10:25:34 +01002024 sprintf (dev->name, "ppc_4xx_eth%d", eth_num - CONFIG_EMAC_NR_START);
wdenkba56f622004-02-06 23:19:44 +00002025 dev->priv = (void *) hw;
Stefan Roesed6c61aa2005-08-16 18:18:00 +02002026 dev->init = ppc_4xx_eth_init;
2027 dev->halt = ppc_4xx_eth_halt;
2028 dev->send = ppc_4xx_eth_send;
2029 dev->recv = ppc_4xx_eth_rx;
wdenkba56f622004-02-06 23:19:44 +00002030
Stefan Roese3ce04d92011-07-12 13:26:47 +02002031 eth_register(dev);
2032
2033#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
2034 miiphy_register(dev->name,
2035 emac4xx_miiphy_read, emac4xx_miiphy_write);
2036#endif
2037
wdenkba56f622004-02-06 23:19:44 +00002038 if (0 == virgin) {
2039 /* set the MAL IER ??? names may change with new spec ??? */
Stefan Roesedbbd1252007-10-05 17:10:59 +02002040#if defined(CONFIG_440SPE) || \
2041 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roese8ac41e32008-03-11 15:05:26 +01002042 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +02002043 defined(CONFIG_405EX)
Marian Balakowicz6c5879f2006-06-30 16:30:46 +02002044 mal_ier =
2045 MAL_IER_PT | MAL_IER_PRE | MAL_IER_PWE |
2046 MAL_IER_DE | MAL_IER_OTE | MAL_IER_OE | MAL_IER_PE ;
2047#else
wdenkba56f622004-02-06 23:19:44 +00002048 mal_ier =
2049 MAL_IER_DE | MAL_IER_NE | MAL_IER_TE |
2050 MAL_IER_OPBE | MAL_IER_PLBE;
Marian Balakowicz6c5879f2006-06-30 16:30:46 +02002051#endif
Stefan Roesed1c3b272009-09-09 16:25:29 +02002052 mtdcr (MAL0_ESR, 0xffffffff); /* clear pending interrupts */
2053 mtdcr (MAL0_TXDEIR, 0xffffffff); /* clear pending interrupts */
2054 mtdcr (MAL0_RXDEIR, 0xffffffff); /* clear pending interrupts */
2055 mtdcr (MAL0_IER, mal_ier);
wdenkba56f622004-02-06 23:19:44 +00002056
2057 /* install MAL interrupt handler */
Stefan Roesed1631fe2008-06-26 13:40:57 +02002058 irq_install_handler (VECNUM_MAL_SERR,
wdenkba56f622004-02-06 23:19:44 +00002059 (interrupt_handler_t *) enetInt,
2060 dev);
Stefan Roesed1631fe2008-06-26 13:40:57 +02002061 irq_install_handler (VECNUM_MAL_TXEOB,
wdenkba56f622004-02-06 23:19:44 +00002062 (interrupt_handler_t *) enetInt,
2063 dev);
Stefan Roesed1631fe2008-06-26 13:40:57 +02002064 irq_install_handler (VECNUM_MAL_RXEOB,
wdenkba56f622004-02-06 23:19:44 +00002065 (interrupt_handler_t *) enetInt,
2066 dev);
Stefan Roesed1631fe2008-06-26 13:40:57 +02002067 irq_install_handler (VECNUM_MAL_TXDE,
wdenkba56f622004-02-06 23:19:44 +00002068 (interrupt_handler_t *) enetInt,
2069 dev);
Stefan Roesed1631fe2008-06-26 13:40:57 +02002070 irq_install_handler (VECNUM_MAL_RXDE,
wdenkba56f622004-02-06 23:19:44 +00002071 (interrupt_handler_t *) enetInt,
2072 dev);
2073 virgin = 1;
2074 }
wdenkba56f622004-02-06 23:19:44 +00002075 } /* end for each supported device */
Stefan Roese802b7692008-01-08 18:39:30 +01002076
2077 return 0;
wdenkba56f622004-02-06 23:19:44 +00002078}