blob: ca1139676b23fff4d0f006a821fbaceb1916d30a [file] [log] [blame]
Wolfgang Denk52744b42013-07-28 22:12:45 +02001/*
Wolfgang Denk815c9672013-09-17 11:24:06 +02002 * SPDX-License-Identifier: GPL-2.0 IBM-pibs
Wolfgang Denk52744b42013-07-28 22:12:45 +02003 */
wdenk544e9732004-02-06 23:19:44 +00004/*-----------------------------------------------------------------------------+
5 *
Wolfgang Denk0cbaf642005-09-25 00:53:22 +02006 * File Name: enetemac.c
wdenk544e9732004-02-06 23:19:44 +00007 *
Wolfgang Denk0cbaf642005-09-25 00:53:22 +02008 * Function: Device driver for the ethernet EMAC3 macro on the 405GP.
wdenk544e9732004-02-06 23:19:44 +00009 *
Wolfgang Denk0cbaf642005-09-25 00:53:22 +020010 * Author: Mark Wisner
wdenk544e9732004-02-06 23:19:44 +000011 *
12 * Change Activity-
13 *
Wolfgang Denk0cbaf642005-09-25 00:53:22 +020014 * Date Description of Change BY
15 * --------- --------------------- ---
16 * 05-May-99 Created MKW
17 * 27-Jun-99 Clean up JWB
18 * 16-Jul-99 Added MAL error recovery and better IP packet handling MKW
19 * 29-Jul-99 Added Full duplex support MKW
20 * 06-Aug-99 Changed names for Mal CR reg MKW
21 * 23-Aug-99 Turned off SYE when running at 10Mbs MKW
22 * 24-Aug-99 Marked descriptor empty after call_xlc MKW
23 * 07-Sep-99 Set MAL RX buffer size reg to ENET_MAX_MTU_ALIGNED / 16 MCG
24 * to avoid chaining maximum sized packets. Push starting
25 * RX descriptor address up to the next cache line boundary.
26 * 16-Jan-00 Added support for booting with IP of 0x0 MKW
27 * 15-Mar-00 Updated enetInit() to enable broadcast addresses in the
Niklaus Giger728bd0a2009-10-04 20:04:20 +020028 * EMAC0_RXM register. JWB
Wolfgang Denk0cbaf642005-09-25 00:53:22 +020029 * 12-Mar-01 anne-sophie.harnois@nextream.fr
30 * - Variables are compatible with those already defined in
31 * include/net.h
32 * - Receive buffer descriptor ring is used to send buffers
33 * to the user
34 * - Info print about send/received/handled packet number if
35 * INFO_405_ENET is set
36 * 17-Apr-01 stefan.roese@esd-electronics.com
37 * - MAL reset in "eth_halt" included
38 * - Enet speed and duplex output now in one line
39 * 08-May-01 stefan.roese@esd-electronics.com
40 * - MAL error handling added (eth_init called again)
41 * 13-Nov-01 stefan.roese@esd-electronics.com
Niklaus Giger728bd0a2009-10-04 20:04:20 +020042 * - Set IST bit in EMAC0_MR1 reg upon 100MBit or full duplex
Wolfgang Denk0cbaf642005-09-25 00:53:22 +020043 * 04-Jan-02 stefan.roese@esd-electronics.com
44 * - Wait for PHY auto negotiation to complete added
45 * 06-Feb-02 stefan.roese@esd-electronics.com
46 * - Bug fixed in waiting for auto negotiation to complete
47 * 26-Feb-02 stefan.roese@esd-electronics.com
48 * - rx and tx buffer descriptors now allocated (no fixed address
49 * used anymore)
50 * 17-Jun-02 stefan.roese@esd-electronics.com
51 * - MAL error debug printf 'M' removed (rx de interrupt may
52 * occur upon many incoming packets with only 4 rx buffers).
wdenk544e9732004-02-06 23:19:44 +000053 *-----------------------------------------------------------------------------*
Wolfgang Denk0cbaf642005-09-25 00:53:22 +020054 * 17-Nov-03 travis.sawyer@sandburst.com
55 * - ported from 405gp_enet.c to utilized upto 4 EMAC ports
56 * in the 440GX. This port should work with the 440GP
57 * (2 EMACs) also
58 * 15-Aug-05 sr@denx.de
59 * - merged 405gp_enet.c and 440gx_enet.c to generic 4xx_enet.c
60 now handling all 4xx cpu's.
wdenk544e9732004-02-06 23:19:44 +000061 *-----------------------------------------------------------------------------*/
62
63#include <config.h>
wdenk544e9732004-02-06 23:19:44 +000064#include <common.h>
65#include <net.h>
66#include <asm/processor.h>
Stefan Roese697100952007-10-23 14:03:17 +020067#include <asm/io.h>
Stefan Roese9c2a6472007-10-31 18:01:24 +010068#include <asm/cache.h>
69#include <asm/mmu.h>
wdenk544e9732004-02-06 23:19:44 +000070#include <commproc.h>
Stefan Roese247e9d72010-09-09 19:18:00 +020071#include <asm/ppc4xx.h>
72#include <asm/ppc4xx-emac.h>
73#include <asm/ppc4xx-mal.h>
wdenk544e9732004-02-06 23:19:44 +000074#include <miiphy.h>
75#include <malloc.h>
Stefan Roese0eb592d2011-11-15 08:01:58 +000076#include <linux/compiler.h>
wdenk544e9732004-02-06 23:19:44 +000077
Jon Loeligera5217742007-07-09 18:57:22 -050078#if !(defined(CONFIG_MII) || defined(CONFIG_CMD_MII))
Stefan Roese0c7ffc02005-08-16 18:18:00 +020079#error "CONFIG_MII has to be defined!"
80#endif
wdenk544e9732004-02-06 23:19:44 +000081
Wolfgang Denk0cbaf642005-09-25 00:53:22 +020082#define EMAC_RESET_TIMEOUT 1000 /* 1000 ms reset timeout */
Stefan Roese3852e232007-10-23 14:05:08 +020083#define PHY_AUTONEGOTIATE_TIMEOUT 5000 /* 5000 ms autonegotiate timeout */
wdenk544e9732004-02-06 23:19:44 +000084
wdenk544e9732004-02-06 23:19:44 +000085/* Ethernet Transmit and Receive Buffers */
86/* AS.HARNOIS
87 * In the same way ENET_MAX_MTU and ENET_MAX_MTU_ALIGNED are set from
88 * PKTSIZE and PKTSIZE_ALIGN (include/net.h)
89 */
Wolfgang Denk0cbaf642005-09-25 00:53:22 +020090#define ENET_MAX_MTU PKTSIZE
wdenk544e9732004-02-06 23:19:44 +000091#define ENET_MAX_MTU_ALIGNED PKTSIZE_ALIGN
92
wdenk544e9732004-02-06 23:19:44 +000093/*-----------------------------------------------------------------------------+
94 * Defines for MAL/EMAC interrupt conditions as reported in the UIC (Universal
95 * Interrupt Controller).
96 *-----------------------------------------------------------------------------*/
Stefan Roese01edcea2008-06-26 13:40:57 +020097#define ETH_IRQ_NUM(dev) (VECNUM_ETH0 + ((dev) * VECNUM_ETH1_OFFS))
98
99#if defined(CONFIG_HAS_ETH3)
100#if !defined(CONFIG_440GX)
101#define UIC_ETHx (UIC_MASK(ETH_IRQ_NUM(0)) || UIC_MASK(ETH_IRQ_NUM(1)) || \
102 UIC_MASK(ETH_IRQ_NUM(2)) || UIC_MASK(ETH_IRQ_NUM(3)))
103#else
104/* Unfortunately 440GX spreads EMAC interrupts on multiple UIC's */
105#define UIC_ETHx (UIC_MASK(ETH_IRQ_NUM(0)) || UIC_MASK(ETH_IRQ_NUM(1)))
106#define UIC_ETHxB (UIC_MASK(ETH_IRQ_NUM(2)) || UIC_MASK(ETH_IRQ_NUM(3)))
107#endif /* !defined(CONFIG_440GX) */
108#elif defined(CONFIG_HAS_ETH2)
109#define UIC_ETHx (UIC_MASK(ETH_IRQ_NUM(0)) || UIC_MASK(ETH_IRQ_NUM(1)) || \
110 UIC_MASK(ETH_IRQ_NUM(2)))
111#elif defined(CONFIG_HAS_ETH1)
112#define UIC_ETHx (UIC_MASK(ETH_IRQ_NUM(0)) || UIC_MASK(ETH_IRQ_NUM(1)))
113#else
114#define UIC_ETHx UIC_MASK(ETH_IRQ_NUM(0))
115#endif
116
117/*
118 * Define a default version for UIC_ETHxB for non 440GX so that we can
119 * use common code for all 4xx variants
120 */
121#if !defined(UIC_ETHxB)
122#define UIC_ETHxB 0
123#endif
124
125#define UIC_MAL_SERR UIC_MASK(VECNUM_MAL_SERR)
126#define UIC_MAL_TXDE UIC_MASK(VECNUM_MAL_TXDE)
127#define UIC_MAL_RXDE UIC_MASK(VECNUM_MAL_RXDE)
128#define UIC_MAL_TXEOB UIC_MASK(VECNUM_MAL_TXEOB)
129#define UIC_MAL_RXEOB UIC_MASK(VECNUM_MAL_RXEOB)
130
131#define MAL_UIC_ERR (UIC_MAL_SERR | UIC_MAL_TXDE | UIC_MAL_RXDE)
132#define MAL_UIC_DEF (UIC_MAL_RXEOB | MAL_UIC_ERR)
133
134/*
135 * We have 3 different interrupt types:
136 * - MAL interrupts indicating successful transfer
137 * - MAL error interrupts indicating MAL related errors
138 * - EMAC interrupts indicating EMAC related errors
139 *
140 * All those interrupts can be on different UIC's, but since
141 * now at least all interrupts from one type are on the same
142 * UIC. Only exception is 440GX where the EMAC interrupts are
143 * spread over two UIC's!
144 */
Stefan Roese51d6d5d2008-06-26 17:36:39 +0200145#if defined(CONFIG_440GX)
146#define UIC_BASE_MAL UIC1_DCR_BASE
147#define UIC_BASE_MAL_ERR UIC2_DCR_BASE
148#define UIC_BASE_EMAC UIC2_DCR_BASE
149#define UIC_BASE_EMAC_B UIC3_DCR_BASE
150#else
Stefan Roese01edcea2008-06-26 13:40:57 +0200151#define UIC_BASE_MAL (UIC0_DCR_BASE + (UIC_NR(VECNUM_MAL_TXEOB) * 0x10))
152#define UIC_BASE_MAL_ERR (UIC0_DCR_BASE + (UIC_NR(VECNUM_MAL_SERR) * 0x10))
153#define UIC_BASE_EMAC (UIC0_DCR_BASE + (UIC_NR(ETH_IRQ_NUM(0)) * 0x10))
Stefan Roese01edcea2008-06-26 13:40:57 +0200154#define UIC_BASE_EMAC_B (UIC0_DCR_BASE + (UIC_NR(ETH_IRQ_NUM(0)) * 0x10))
155#endif
wdenk544e9732004-02-06 23:19:44 +0000156
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200157#undef INFO_4XX_ENET
wdenk544e9732004-02-06 23:19:44 +0000158
Wolfgang Denk0cbaf642005-09-25 00:53:22 +0200159#define BI_PHYMODE_NONE 0
160#define BI_PHYMODE_ZMII 1
wdenk56ed43e2004-02-22 23:46:08 +0000161#define BI_PHYMODE_RGMII 2
Stefan Roese42fbddd2006-09-07 11:51:23 +0200162#define BI_PHYMODE_GMII 3
163#define BI_PHYMODE_RTBI 4
164#define BI_PHYMODE_TBI 5
Stefan Roese153b3e22007-10-05 17:10:59 +0200165#if defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roesebdd13d12008-03-11 15:05:26 +0100166 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roese153b3e22007-10-05 17:10:59 +0200167 defined(CONFIG_405EX)
Stefan Roese42fbddd2006-09-07 11:51:23 +0200168#define BI_PHYMODE_SMII 6
169#define BI_PHYMODE_MII 7
Stefan Roesebdd13d12008-03-11 15:05:26 +0100170#if defined(CONFIG_460EX) || defined(CONFIG_460GT)
171#define BI_PHYMODE_RMII 8
172#endif
Stefan Roese42fbddd2006-09-07 11:51:23 +0200173#endif
Victor Gallardo45a06ff2008-09-04 23:49:36 -0700174#define BI_PHYMODE_SGMII 9
wdenk56ed43e2004-02-22 23:46:08 +0000175
Stefan Roese5a128832007-10-05 17:35:10 +0200176#if defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
Stefan Roese153b3e22007-10-05 17:10:59 +0200177 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roesebdd13d12008-03-11 15:05:26 +0100178 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roese153b3e22007-10-05 17:10:59 +0200179 defined(CONFIG_405EX)
Stefan Roese42fbddd2006-09-07 11:51:23 +0200180#define SDR0_MFR_ETH_CLK_SEL_V(n) ((0x01<<27) / (n+1))
181#endif
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200182
Stefan Roesebdd13d12008-03-11 15:05:26 +0100183#if defined(CONFIG_460EX) || defined(CONFIG_460GT)
184#define SDR0_ETH_CFG_CLK_SEL_V(n) (0x01 << (8 + n))
185#endif
186
187#if defined(CONFIG_460EX) || defined(CONFIG_460GT)
188#define MAL_RX_CHAN_MUL 8 /* 460EX/GT uses MAL channel 8 for EMAC1 */
189#else
190#define MAL_RX_CHAN_MUL 1
191#endif
192
Victor Gallardo45a06ff2008-09-04 23:49:36 -0700193/*--------------------------------------------------------------------+
194 * Fixed PHY (PHY-less) support for Ethernet Ports.
195 *--------------------------------------------------------------------*/
196
197/*
198 * Some boards do not have a PHY for each ethernet port. These ports
199 * are known as Fixed PHY (or PHY-less) ports. For such ports, set
200 * the appropriate CONFIG_PHY_ADDR equal to CONFIG_FIXED_PHY and
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200201 * then define CONFIG_SYS_FIXED_PHY_PORTS to define what the speed and
Victor Gallardo45a06ff2008-09-04 23:49:36 -0700202 * duplex should be for these ports in the board configuration
203 * file.
204 *
205 * For Example:
206 * #define CONFIG_FIXED_PHY 0xFFFFFFFF
207 *
208 * #define CONFIG_PHY_ADDR CONFIG_FIXED_PHY
209 * #define CONFIG_PHY1_ADDR 1
210 * #define CONFIG_PHY2_ADDR CONFIG_FIXED_PHY
211 * #define CONFIG_PHY3_ADDR 3
212 *
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200213 * #define CONFIG_SYS_FIXED_PHY_PORT(devnum,speed,duplex) \
Victor Gallardo45a06ff2008-09-04 23:49:36 -0700214 * {devnum, speed, duplex},
215 *
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200216 * #define CONFIG_SYS_FIXED_PHY_PORTS \
217 * CONFIG_SYS_FIXED_PHY_PORT(0,1000,FULL) \
218 * CONFIG_SYS_FIXED_PHY_PORT(2,100,HALF)
Victor Gallardo45a06ff2008-09-04 23:49:36 -0700219 */
220
221#ifndef CONFIG_FIXED_PHY
222#define CONFIG_FIXED_PHY 0xFFFFFFFF /* Fixed PHY (PHY-less) */
223#endif
224
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200225#ifndef CONFIG_SYS_FIXED_PHY_PORTS
226#define CONFIG_SYS_FIXED_PHY_PORTS /* default is an empty array */
Victor Gallardo45a06ff2008-09-04 23:49:36 -0700227#endif
228
229struct fixed_phy_port {
230 unsigned int devnum; /* ethernet port */
231 unsigned int speed; /* specified speed 10,100 or 1000 */
232 unsigned int duplex; /* specified duplex FULL or HALF */
233};
234
235static const struct fixed_phy_port fixed_phy_port[] = {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200236 CONFIG_SYS_FIXED_PHY_PORTS /* defined in board configuration file */
Victor Gallardo45a06ff2008-09-04 23:49:36 -0700237};
238
wdenk544e9732004-02-06 23:19:44 +0000239/*-----------------------------------------------------------------------------+
240 * Global variables. TX and RX descriptors and buffers.
241 *-----------------------------------------------------------------------------*/
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200242
Stefan Roese7f98aec2005-10-20 16:34:28 +0200243/*
244 * Get count of EMAC devices (doesn't have to be the max. possible number
245 * supported by the cpu)
Stefan Roese15668052007-10-23 10:10:08 +0200246 *
247 * CONFIG_BOARD_EMAC_COUNT added so now a "dynamic" way to configure the
248 * EMAC count is possible. As it is needed for the Kilauea/Haleakala
249 * 405EX/405EXr eval board, using the same binary.
Stefan Roese7f98aec2005-10-20 16:34:28 +0200250 */
Stefan Roese15668052007-10-23 10:10:08 +0200251#if defined(CONFIG_BOARD_EMAC_COUNT)
252#define LAST_EMAC_NUM board_emac_count()
253#else /* CONFIG_BOARD_EMAC_COUNT */
Stefan Roese7f98aec2005-10-20 16:34:28 +0200254#if defined(CONFIG_HAS_ETH3)
255#define LAST_EMAC_NUM 4
256#elif defined(CONFIG_HAS_ETH2)
257#define LAST_EMAC_NUM 3
258#elif defined(CONFIG_HAS_ETH1)
259#define LAST_EMAC_NUM 2
260#else
261#define LAST_EMAC_NUM 1
262#endif
Stefan Roese15668052007-10-23 10:10:08 +0200263#endif /* CONFIG_BOARD_EMAC_COUNT */
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200264
Stefan Roese8d982302007-01-18 10:25:34 +0100265/* normal boards start with EMAC0 */
266#if !defined(CONFIG_EMAC_NR_START)
267#define CONFIG_EMAC_NR_START 0
268#endif
269
Stefan Roese9c2a6472007-10-31 18:01:24 +0100270#define MAL_RX_DESC_SIZE 2048
271#define MAL_TX_DESC_SIZE 2048
272#define MAL_ALLOC_SIZE (MAL_TX_DESC_SIZE + MAL_RX_DESC_SIZE)
273
wdenk544e9732004-02-06 23:19:44 +0000274/*-----------------------------------------------------------------------------+
275 * Prototypes and externals.
276 *-----------------------------------------------------------------------------*/
277static void enet_rcv (struct eth_device *dev, unsigned long malisr);
278
279int enetInt (struct eth_device *dev);
280static void mal_err (struct eth_device *dev, unsigned long isr,
281 unsigned long uic, unsigned long maldef,
282 unsigned long mal_errr);
283static void emac_err (struct eth_device *dev, unsigned long isr);
284
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200285extern int phy_setup_aneg (char *devname, unsigned char addr);
Joe Hershberger0c333192016-08-08 11:28:39 -0500286int emac4xx_miiphy_read(struct mii_dev *bus, int addr, int devad, int reg);
287int emac4xx_miiphy_write(struct mii_dev *bus, int addr, int devad, int reg,
288 u16 value);
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200289
Stefan Roese15668052007-10-23 10:10:08 +0200290int board_emac_count(void);
291
Stefan Roesebdd13d12008-03-11 15:05:26 +0100292static void emac_loopback_enable(EMAC_4XX_HW_PST hw_p)
293{
294#if defined(CONFIG_440SPE) || \
295 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
296 defined(CONFIG_405EX)
297 u32 val;
298
Stefan Roese918010a2009-09-09 16:25:29 +0200299 mfsdr(SDR0_MFR, val);
Stefan Roesebdd13d12008-03-11 15:05:26 +0100300 val |= SDR0_MFR_ETH_CLK_SEL_V(hw_p->devnum);
Stefan Roese918010a2009-09-09 16:25:29 +0200301 mtsdr(SDR0_MFR, val);
Stefan Roesebdd13d12008-03-11 15:05:26 +0100302#elif defined(CONFIG_460EX) || defined(CONFIG_460GT)
303 u32 val;
304
305 mfsdr(SDR0_ETH_CFG, val);
306 val |= SDR0_ETH_CFG_CLK_SEL_V(hw_p->devnum);
307 mtsdr(SDR0_ETH_CFG, val);
308#endif
309}
310
311static void emac_loopback_disable(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 Roese918010a2009-09-09 16:25:29 +0200318 mfsdr(SDR0_MFR, val);
Stefan Roesebdd13d12008-03-11 15:05:26 +0100319 val &= ~SDR0_MFR_ETH_CLK_SEL_V(hw_p->devnum);
Stefan Roese918010a2009-09-09 16:25:29 +0200320 mtsdr(SDR0_MFR, val);
Stefan Roesebdd13d12008-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
wdenk544e9732004-02-06 23:19:44 +0000330/*-----------------------------------------------------------------------------+
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200331| ppc_4xx_eth_halt
wdenk544e9732004-02-06 23:19:44 +0000332| Disable MAL channel, and EMACn
wdenk544e9732004-02-06 23:19:44 +0000333+-----------------------------------------------------------------------------*/
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200334static void ppc_4xx_eth_halt (struct eth_device *dev)
wdenk544e9732004-02-06 23:19:44 +0000335{
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200336 EMAC_4XX_HW_PST hw_p = dev->priv;
Stefan Roese40b8c0d2008-03-19 16:35:12 +0100337 u32 val = 10000;
wdenk544e9732004-02-06 23:19:44 +0000338
Niklaus Giger728bd0a2009-10-04 20:04:20 +0200339 out_be32((void *)EMAC0_IER + hw_p->hw_addr, 0x00000000); /* disable emac interrupts */
wdenk544e9732004-02-06 23:19:44 +0000340
341 /* 1st reset MAL channel */
342 /* Note: writing a 0 to a channel has no effect */
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200343#if defined(CONFIG_405EP) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
Stefan Roese918010a2009-09-09 16:25:29 +0200344 mtdcr (MAL0_TXCARR, (MAL_CR_MMSR >> (hw_p->devnum * 2)));
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200345#else
Stefan Roese918010a2009-09-09 16:25:29 +0200346 mtdcr (MAL0_TXCARR, (MAL_CR_MMSR >> hw_p->devnum));
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200347#endif
Stefan Roese918010a2009-09-09 16:25:29 +0200348 mtdcr (MAL0_RXCARR, (MAL_CR_MMSR >> hw_p->devnum));
wdenk544e9732004-02-06 23:19:44 +0000349
350 /* wait for reset */
Stefan Roese918010a2009-09-09 16:25:29 +0200351 while (mfdcr (MAL0_RXCASR) & (MAL_CR_MMSR >> hw_p->devnum)) {
wdenk544e9732004-02-06 23:19:44 +0000352 udelay (1000); /* Delay 1 MS so as not to hammer the register */
Stefan Roese40b8c0d2008-03-19 16:35:12 +0100353 val--;
354 if (val == 0)
wdenk544e9732004-02-06 23:19:44 +0000355 break;
wdenk544e9732004-02-06 23:19:44 +0000356 }
357
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200358 /* provide clocks for EMAC internal loopback */
Stefan Roesebdd13d12008-03-11 15:05:26 +0100359 emac_loopback_enable(hw_p);
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200360
Stefan Roesebdd13d12008-03-11 15:05:26 +0100361 /* EMAC RESET */
Niklaus Giger728bd0a2009-10-04 20:04:20 +0200362 out_be32((void *)EMAC0_MR0 + hw_p->hw_addr, EMAC_MR0_SRST);
wdenk544e9732004-02-06 23:19:44 +0000363
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200364 /* remove clocks for EMAC internal loopback */
Stefan Roesebdd13d12008-03-11 15:05:26 +0100365 emac_loopback_disable(hw_p);
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200366
Stefan Roesec8136d02005-10-18 19:17:12 +0200367#ifndef CONFIG_NETCONSOLE
Stefan Roese326c9712005-08-01 16:41:48 +0200368 hw_p->print_speed = 1; /* print speed message again next time */
Stefan Roesec8136d02005-10-18 19:17:12 +0200369#endif
Stefan Roese326c9712005-08-01 16:41:48 +0200370
Stefan Roese52df4192008-03-19 16:20:49 +0100371#if defined(CONFIG_460EX) || defined(CONFIG_460GT)
372 /* don't bypass the TAHOE0/TAHOE1 cores for Linux */
Stefan Roese40b8c0d2008-03-19 16:35:12 +0100373 mfsdr(SDR0_ETH_CFG, val);
374 val &= ~(SDR0_ETH_CFG_TAHOE0_BYPASS | SDR0_ETH_CFG_TAHOE1_BYPASS);
375 mtsdr(SDR0_ETH_CFG, val);
Stefan Roese52df4192008-03-19 16:20:49 +0100376#endif
377
wdenk544e9732004-02-06 23:19:44 +0000378 return;
379}
380
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200381#if defined (CONFIG_440GX)
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200382int ppc_4xx_eth_setup_bridge(int devnum, bd_t * bis)
wdenked2ac4b2004-03-14 18:23:55 +0000383{
384 unsigned long pfc1;
385 unsigned long zmiifer;
386 unsigned long rmiifer;
387
Stefan Roese918010a2009-09-09 16:25:29 +0200388 mfsdr(SDR0_PFC1, pfc1);
wdenked2ac4b2004-03-14 18:23:55 +0000389 pfc1 = SDR0_PFC1_EPS_DECODE(pfc1);
390
391 zmiifer = 0;
392 rmiifer = 0;
393
394 switch (pfc1) {
395 case 1:
396 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
397 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(1);
398 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(2);
399 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(3);
400 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
401 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
402 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
403 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
404 break;
405 case 2:
Stefan Roese0632d7b2006-11-27 17:43:25 +0100406 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(0);
407 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(1);
408 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(2);
409 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(3);
wdenked2ac4b2004-03-14 18:23:55 +0000410 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
411 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
412 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
413 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
414 break;
415 case 3:
416 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
417 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
418 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
419 bis->bi_phymode[1] = BI_PHYMODE_NONE;
420 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
421 bis->bi_phymode[3] = BI_PHYMODE_NONE;
422 break;
423 case 4:
424 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(0);
425 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(1);
426 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V (2);
427 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V (3);
428 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
429 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
430 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
431 bis->bi_phymode[3] = BI_PHYMODE_RGMII;
432 break;
433 case 5:
434 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (0);
435 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (1);
436 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (2);
437 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(3);
438 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
439 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
440 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
441 bis->bi_phymode[3] = BI_PHYMODE_RGMII;
442 break;
443 case 6:
444 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (0);
445 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (1);
446 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
wdenked2ac4b2004-03-14 18:23:55 +0000447 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
448 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
449 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
wdenked2ac4b2004-03-14 18:23:55 +0000450 break;
451 case 0:
452 default:
453 zmiifer = ZMII_FER_MII << ZMII_FER_V(devnum);
454 rmiifer = 0x0;
455 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
456 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
457 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
458 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
459 break;
460 }
461
462 /* Ensure we setup mdio for this devnum and ONLY this devnum */
463 zmiifer |= (ZMII_FER_MDI) << ZMII_FER_V(devnum);
464
Niklaus Giger728bd0a2009-10-04 20:04:20 +0200465 out_be32((void *)ZMII0_FER, zmiifer);
Stefan Roese9c2a6472007-10-31 18:01:24 +0100466 out_be32((void *)RGMII_FER, rmiifer);
wdenked2ac4b2004-03-14 18:23:55 +0000467
468 return ((int)pfc1);
wdenked2ac4b2004-03-14 18:23:55 +0000469}
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200470#endif /* CONFIG_440_GX */
wdenked2ac4b2004-03-14 18:23:55 +0000471
Stefan Roese42fbddd2006-09-07 11:51:23 +0200472#if defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
473int ppc_4xx_eth_setup_bridge(int devnum, bd_t * bis)
474{
475 unsigned long zmiifer=0x0;
Matthias Fuchs8bdf1682007-04-24 14:03:45 +0200476 unsigned long pfc1;
Stefan Roese42fbddd2006-09-07 11:51:23 +0200477
Stefan Roese918010a2009-09-09 16:25:29 +0200478 mfsdr(SDR0_PFC1, pfc1);
Matthias Fuchs8bdf1682007-04-24 14:03:45 +0200479 pfc1 &= SDR0_PFC1_SELECT_MASK;
480
Wolfgang Denk58c495b2007-05-05 18:23:11 +0200481 switch (pfc1) {
Matthias Fuchs8bdf1682007-04-24 14:03:45 +0200482 case SDR0_PFC1_SELECT_CONFIG_2:
Stefan Roese42fbddd2006-09-07 11:51:23 +0200483 /* 1 x GMII port */
Niklaus Giger728bd0a2009-10-04 20:04:20 +0200484 out_be32((void *)ZMII0_FER, 0x00);
Stefan Roese697100952007-10-23 14:03:17 +0200485 out_be32((void *)RGMII_FER, 0x00000037);
Stefan Roese42fbddd2006-09-07 11:51:23 +0200486 bis->bi_phymode[0] = BI_PHYMODE_GMII;
487 bis->bi_phymode[1] = BI_PHYMODE_NONE;
488 break;
Matthias Fuchs8bdf1682007-04-24 14:03:45 +0200489 case SDR0_PFC1_SELECT_CONFIG_4:
Stefan Roese42fbddd2006-09-07 11:51:23 +0200490 /* 2 x RGMII ports */
Niklaus Giger728bd0a2009-10-04 20:04:20 +0200491 out_be32((void *)ZMII0_FER, 0x00);
Stefan Roese697100952007-10-23 14:03:17 +0200492 out_be32((void *)RGMII_FER, 0x00000055);
Stefan Roese42fbddd2006-09-07 11:51:23 +0200493 bis->bi_phymode[0] = BI_PHYMODE_RGMII;
494 bis->bi_phymode[1] = BI_PHYMODE_RGMII;
495 break;
Matthias Fuchs8bdf1682007-04-24 14:03:45 +0200496 case SDR0_PFC1_SELECT_CONFIG_6:
Stefan Roese42fbddd2006-09-07 11:51:23 +0200497 /* 2 x SMII ports */
Niklaus Giger728bd0a2009-10-04 20:04:20 +0200498 out_be32((void *)ZMII0_FER,
Stefan Roese697100952007-10-23 14:03:17 +0200499 ((ZMII_FER_SMII) << ZMII_FER_V(0)) |
500 ((ZMII_FER_SMII) << ZMII_FER_V(1)));
501 out_be32((void *)RGMII_FER, 0x00000000);
Matthias Fuchs8bdf1682007-04-24 14:03:45 +0200502 bis->bi_phymode[0] = BI_PHYMODE_SMII;
503 bis->bi_phymode[1] = BI_PHYMODE_SMII;
504 break;
505 case SDR0_PFC1_SELECT_CONFIG_1_2:
506 /* only 1 x MII supported */
Niklaus Giger728bd0a2009-10-04 20:04:20 +0200507 out_be32((void *)ZMII0_FER, (ZMII_FER_MII) << ZMII_FER_V(0));
Stefan Roese697100952007-10-23 14:03:17 +0200508 out_be32((void *)RGMII_FER, 0x00000000);
Matthias Fuchs8bdf1682007-04-24 14:03:45 +0200509 bis->bi_phymode[0] = BI_PHYMODE_MII;
510 bis->bi_phymode[1] = BI_PHYMODE_NONE;
Stefan Roese42fbddd2006-09-07 11:51:23 +0200511 break;
512 default:
513 break;
514 }
515
516 /* Ensure we setup mdio for this devnum and ONLY this devnum */
Niklaus Giger728bd0a2009-10-04 20:04:20 +0200517 zmiifer = in_be32((void *)ZMII0_FER);
Stefan Roese42fbddd2006-09-07 11:51:23 +0200518 zmiifer |= (ZMII_FER_MDI) << ZMII_FER_V(devnum);
Niklaus Giger728bd0a2009-10-04 20:04:20 +0200519 out_be32((void *)ZMII0_FER, zmiifer);
Stefan Roese42fbddd2006-09-07 11:51:23 +0200520
521 return ((int)0x0);
522}
523#endif /* CONFIG_440EPX */
524
Stefan Roese153b3e22007-10-05 17:10:59 +0200525#if defined(CONFIG_405EX)
526int ppc_4xx_eth_setup_bridge(int devnum, bd_t * bis)
527{
Grant Erickson0591f912008-07-08 08:35:00 -0700528 u32 rgmiifer = 0;
Stefan Roese153b3e22007-10-05 17:10:59 +0200529
530 /*
Grant Erickson0591f912008-07-08 08:35:00 -0700531 * The 405EX(r)'s RGMII bridge can operate in one of several
532 * modes, only one of which (2 x RGMII) allows the
533 * simultaneous use of both EMACs on the 405EX.
Stefan Roese153b3e22007-10-05 17:10:59 +0200534 */
Grant Erickson0591f912008-07-08 08:35:00 -0700535
536 switch (CONFIG_EMAC_PHY_MODE) {
537
538 case EMAC_PHY_MODE_NONE:
539 /* No ports */
540 rgmiifer |= RGMII_FER_DIS << 0;
541 rgmiifer |= RGMII_FER_DIS << 4;
542 out_be32((void *)RGMII_FER, rgmiifer);
543 bis->bi_phymode[0] = BI_PHYMODE_NONE;
544 bis->bi_phymode[1] = BI_PHYMODE_NONE;
545 break;
546 case EMAC_PHY_MODE_NONE_RGMII:
547 /* 1 x RGMII port on channel 0 */
548 rgmiifer |= RGMII_FER_RGMII << 0;
549 rgmiifer |= RGMII_FER_DIS << 4;
550 out_be32((void *)RGMII_FER, rgmiifer);
551 bis->bi_phymode[0] = BI_PHYMODE_RGMII;
552 bis->bi_phymode[1] = BI_PHYMODE_NONE;
553 break;
554 case EMAC_PHY_MODE_RGMII_NONE:
555 /* 1 x RGMII port on channel 1 */
556 rgmiifer |= RGMII_FER_DIS << 0;
557 rgmiifer |= RGMII_FER_RGMII << 4;
558 out_be32((void *)RGMII_FER, rgmiifer);
559 bis->bi_phymode[0] = BI_PHYMODE_NONE;
560 bis->bi_phymode[1] = BI_PHYMODE_RGMII;
561 break;
562 case EMAC_PHY_MODE_RGMII_RGMII:
Stefan Roese153b3e22007-10-05 17:10:59 +0200563 /* 2 x RGMII ports */
Grant Erickson0591f912008-07-08 08:35:00 -0700564 rgmiifer |= RGMII_FER_RGMII << 0;
565 rgmiifer |= RGMII_FER_RGMII << 4;
566 out_be32((void *)RGMII_FER, rgmiifer);
Stefan Roese153b3e22007-10-05 17:10:59 +0200567 bis->bi_phymode[0] = BI_PHYMODE_RGMII;
568 bis->bi_phymode[1] = BI_PHYMODE_RGMII;
569 break;
Grant Erickson0591f912008-07-08 08:35:00 -0700570 case EMAC_PHY_MODE_NONE_GMII:
571 /* 1 x GMII port on channel 0 */
572 rgmiifer |= RGMII_FER_GMII << 0;
573 rgmiifer |= RGMII_FER_DIS << 4;
574 out_be32((void *)RGMII_FER, rgmiifer);
575 bis->bi_phymode[0] = BI_PHYMODE_GMII;
576 bis->bi_phymode[1] = BI_PHYMODE_NONE;
577 break;
578 case EMAC_PHY_MODE_NONE_MII:
579 /* 1 x MII port on channel 0 */
580 rgmiifer |= RGMII_FER_MII << 0;
581 rgmiifer |= RGMII_FER_DIS << 4;
582 out_be32((void *)RGMII_FER, rgmiifer);
583 bis->bi_phymode[0] = BI_PHYMODE_MII;
584 bis->bi_phymode[1] = BI_PHYMODE_NONE;
585 break;
586 case EMAC_PHY_MODE_GMII_NONE:
587 /* 1 x GMII port on channel 1 */
588 rgmiifer |= RGMII_FER_DIS << 0;
589 rgmiifer |= RGMII_FER_GMII << 4;
590 out_be32((void *)RGMII_FER, rgmiifer);
591 bis->bi_phymode[0] = BI_PHYMODE_NONE;
592 bis->bi_phymode[1] = BI_PHYMODE_GMII;
593 break;
594 case EMAC_PHY_MODE_MII_NONE:
595 /* 1 x MII port on channel 1 */
596 rgmiifer |= RGMII_FER_DIS << 0;
597 rgmiifer |= RGMII_FER_MII << 4;
598 out_be32((void *)RGMII_FER, rgmiifer);
599 bis->bi_phymode[0] = BI_PHYMODE_NONE;
600 bis->bi_phymode[1] = BI_PHYMODE_MII;
Stefan Roese153b3e22007-10-05 17:10:59 +0200601 break;
602 default:
603 break;
604 }
605
606 /* Ensure we setup mdio for this devnum and ONLY this devnum */
Grant Erickson0591f912008-07-08 08:35:00 -0700607 rgmiifer = in_be32((void *)RGMII_FER);
608 rgmiifer |= (1 << (19-devnum));
609 out_be32((void *)RGMII_FER, rgmiifer);
Stefan Roese153b3e22007-10-05 17:10:59 +0200610
611 return ((int)0x0);
612}
613#endif /* CONFIG_405EX */
614
Stefan Roesebdd13d12008-03-11 15:05:26 +0100615#if defined(CONFIG_460EX) || defined(CONFIG_460GT)
616int ppc_4xx_eth_setup_bridge(int devnum, bd_t * bis)
617{
618 u32 eth_cfg;
619 u32 zmiifer; /* ZMII0_FER reg. */
620 u32 rmiifer; /* RGMII0_FER reg. Bridge 0 */
621 u32 rmiifer1; /* RGMII0_FER reg. Bridge 1 */
Stefan Roese52df4192008-03-19 16:20:49 +0100622 int mode;
Stefan Roesebdd13d12008-03-11 15:05:26 +0100623
624 zmiifer = 0;
625 rmiifer = 0;
626 rmiifer1 = 0;
627
Stefan Roese52df4192008-03-19 16:20:49 +0100628#if defined(CONFIG_460EX)
629 mode = 9;
Victor Gallardo45a06ff2008-09-04 23:49:36 -0700630 mfsdr(SDR0_ETH_CFG, eth_cfg);
631 if (((eth_cfg & SDR0_ETH_CFG_SGMII0_ENABLE) > 0) &&
632 ((eth_cfg & SDR0_ETH_CFG_SGMII1_ENABLE) > 0))
633 mode = 11; /* config SGMII */
Stefan Roese52df4192008-03-19 16:20:49 +0100634#else
635 mode = 10;
Victor Gallardo45a06ff2008-09-04 23:49:36 -0700636 mfsdr(SDR0_ETH_CFG, eth_cfg);
637 if (((eth_cfg & SDR0_ETH_CFG_SGMII0_ENABLE) > 0) &&
638 ((eth_cfg & SDR0_ETH_CFG_SGMII1_ENABLE) > 0) &&
639 ((eth_cfg & SDR0_ETH_CFG_SGMII2_ENABLE) > 0))
640 mode = 12; /* config SGMII */
Stefan Roese52df4192008-03-19 16:20:49 +0100641#endif
642
Stefan Roesebdd13d12008-03-11 15:05:26 +0100643 /* TODO:
644 * NOTE: 460GT has 2 RGMII bridge cores:
645 * emac0 ------ RGMII0_BASE
646 * |
647 * emac1 -----+
648 *
649 * emac2 ------ RGMII1_BASE
650 * |
651 * emac3 -----+
652 *
653 * 460EX has 1 RGMII bridge core:
654 * and RGMII1_BASE is disabled
655 * emac0 ------ RGMII0_BASE
656 * |
657 * emac1 -----+
658 */
659
660 /*
661 * Right now only 2*RGMII is supported. Please extend when needed.
662 * sr - 2008-02-19
Victor Gallardo45a06ff2008-09-04 23:49:36 -0700663 * Add SGMII support.
664 * vg - 2008-07-28
Stefan Roesebdd13d12008-03-11 15:05:26 +0100665 */
Stefan Roese52df4192008-03-19 16:20:49 +0100666 switch (mode) {
Stefan Roesebdd13d12008-03-11 15:05:26 +0100667 case 1:
668 /* 1 MII - 460EX */
669 /* GMC0 EMAC4_0, ZMII Bridge */
670 zmiifer |= ZMII_FER_MII << ZMII_FER_V(0);
671 bis->bi_phymode[0] = BI_PHYMODE_MII;
672 bis->bi_phymode[1] = BI_PHYMODE_NONE;
673 bis->bi_phymode[2] = BI_PHYMODE_NONE;
674 bis->bi_phymode[3] = BI_PHYMODE_NONE;
675 break;
676 case 2:
677 /* 2 MII - 460GT */
678 /* GMC0 EMAC4_0, GMC1 EMAC4_2, ZMII Bridge */
679 zmiifer |= ZMII_FER_MII << ZMII_FER_V(0);
680 zmiifer |= ZMII_FER_MII << ZMII_FER_V(2);
681 bis->bi_phymode[0] = BI_PHYMODE_MII;
682 bis->bi_phymode[1] = BI_PHYMODE_NONE;
683 bis->bi_phymode[2] = BI_PHYMODE_MII;
684 bis->bi_phymode[3] = BI_PHYMODE_NONE;
685 break;
686 case 3:
687 /* 2 RMII - 460EX */
688 /* GMC0 EMAC4_0, GMC0 EMAC4_1, ZMII Bridge */
689 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
690 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(1);
691 bis->bi_phymode[0] = BI_PHYMODE_RMII;
692 bis->bi_phymode[1] = BI_PHYMODE_RMII;
693 bis->bi_phymode[2] = BI_PHYMODE_NONE;
694 bis->bi_phymode[3] = BI_PHYMODE_NONE;
695 break;
696 case 4:
697 /* 4 RMII - 460GT */
698 /* GMC0 EMAC4_0, GMC0 EMAC4_1, GMC1 EMAC4_2, GMC1, EMAC4_3 */
699 /* ZMII Bridge */
700 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
701 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(1);
702 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(2);
703 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(3);
704 bis->bi_phymode[0] = BI_PHYMODE_RMII;
705 bis->bi_phymode[1] = BI_PHYMODE_RMII;
706 bis->bi_phymode[2] = BI_PHYMODE_RMII;
707 bis->bi_phymode[3] = BI_PHYMODE_RMII;
708 break;
709 case 5:
710 /* 2 SMII - 460EX */
711 /* GMC0 EMAC4_0, GMC0 EMAC4_1, ZMII Bridge */
712 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(0);
713 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(1);
714 bis->bi_phymode[0] = BI_PHYMODE_SMII;
715 bis->bi_phymode[1] = BI_PHYMODE_SMII;
716 bis->bi_phymode[2] = BI_PHYMODE_NONE;
717 bis->bi_phymode[3] = BI_PHYMODE_NONE;
718 break;
719 case 6:
720 /* 4 SMII - 460GT */
721 /* GMC0 EMAC4_0, GMC0 EMAC4_1, GMC0 EMAC4_3, GMC0 EMAC4_3 */
722 /* ZMII Bridge */
723 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(0);
724 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(1);
725 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(2);
726 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(3);
727 bis->bi_phymode[0] = BI_PHYMODE_SMII;
728 bis->bi_phymode[1] = BI_PHYMODE_SMII;
729 bis->bi_phymode[2] = BI_PHYMODE_SMII;
730 bis->bi_phymode[3] = BI_PHYMODE_SMII;
731 break;
732 case 7:
733 /* This is the default mode that we want for board bringup - Maple */
734 /* 1 GMII - 460EX */
735 /* GMC0 EMAC4_0, RGMII Bridge 0 */
736 rmiifer |= RGMII_FER_MDIO(0);
737
738 if (devnum == 0) {
739 rmiifer |= RGMII_FER_GMII << RGMII_FER_V(2); /* CH0CFG - EMAC0 */
740 bis->bi_phymode[0] = BI_PHYMODE_GMII;
741 bis->bi_phymode[1] = BI_PHYMODE_NONE;
742 bis->bi_phymode[2] = BI_PHYMODE_NONE;
743 bis->bi_phymode[3] = BI_PHYMODE_NONE;
744 } else {
745 rmiifer |= RGMII_FER_GMII << RGMII_FER_V(3); /* CH1CFG - EMAC1 */
746 bis->bi_phymode[0] = BI_PHYMODE_NONE;
747 bis->bi_phymode[1] = BI_PHYMODE_GMII;
748 bis->bi_phymode[2] = BI_PHYMODE_NONE;
749 bis->bi_phymode[3] = BI_PHYMODE_NONE;
750 }
751 break;
752 case 8:
753 /* 2 GMII - 460GT */
754 /* GMC0 EMAC4_0, RGMII Bridge 0 */
755 /* GMC1 EMAC4_2, RGMII Bridge 1 */
756 rmiifer |= RGMII_FER_GMII << RGMII_FER_V(2); /* CH0CFG - EMAC0 */
757 rmiifer1 |= RGMII_FER_GMII << RGMII_FER_V(2); /* CH0CFG - EMAC2 */
758 rmiifer |= RGMII_FER_MDIO(0); /* enable MDIO - EMAC0 */
759 rmiifer1 |= RGMII_FER_MDIO(0); /* enable MDIO - EMAC2 */
760
761 bis->bi_phymode[0] = BI_PHYMODE_GMII;
762 bis->bi_phymode[1] = BI_PHYMODE_NONE;
763 bis->bi_phymode[2] = BI_PHYMODE_GMII;
764 bis->bi_phymode[3] = BI_PHYMODE_NONE;
765 break;
766 case 9:
767 /* 2 RGMII - 460EX */
768 /* GMC0 EMAC4_0, GMC0 EMAC4_1, RGMII Bridge 0 */
769 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
770 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(3);
771 rmiifer |= RGMII_FER_MDIO(0); /* enable MDIO - EMAC0 */
772
773 bis->bi_phymode[0] = BI_PHYMODE_RGMII;
774 bis->bi_phymode[1] = BI_PHYMODE_RGMII;
775 bis->bi_phymode[2] = BI_PHYMODE_NONE;
776 bis->bi_phymode[3] = BI_PHYMODE_NONE;
777 break;
778 case 10:
779 /* 4 RGMII - 460GT */
780 /* GMC0 EMAC4_0, GMC0 EMAC4_1, RGMII Bridge 0 */
781 /* GMC1 EMAC4_2, GMC1 EMAC4_3, RGMII Bridge 1 */
782 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
783 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(3);
784 rmiifer1 |= RGMII_FER_RGMII << RGMII_FER_V(2);
785 rmiifer1 |= RGMII_FER_RGMII << RGMII_FER_V(3);
786 bis->bi_phymode[0] = BI_PHYMODE_RGMII;
787 bis->bi_phymode[1] = BI_PHYMODE_RGMII;
788 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
789 bis->bi_phymode[3] = BI_PHYMODE_RGMII;
790 break;
Victor Gallardo45a06ff2008-09-04 23:49:36 -0700791 case 11:
792 /* 2 SGMII - 460EX */
793 bis->bi_phymode[0] = BI_PHYMODE_SGMII;
794 bis->bi_phymode[1] = BI_PHYMODE_SGMII;
795 bis->bi_phymode[2] = BI_PHYMODE_NONE;
796 bis->bi_phymode[3] = BI_PHYMODE_NONE;
797 break;
798 case 12:
799 /* 3 SGMII - 460GT */
800 bis->bi_phymode[0] = BI_PHYMODE_SGMII;
801 bis->bi_phymode[1] = BI_PHYMODE_SGMII;
802 bis->bi_phymode[2] = BI_PHYMODE_SGMII;
803 bis->bi_phymode[3] = BI_PHYMODE_NONE;
804 break;
Stefan Roesebdd13d12008-03-11 15:05:26 +0100805 default:
806 break;
807 }
808
809 /* Set EMAC for MDIO */
810 mfsdr(SDR0_ETH_CFG, eth_cfg);
811 eth_cfg |= SDR0_ETH_CFG_MDIO_SEL_EMAC0;
812 mtsdr(SDR0_ETH_CFG, eth_cfg);
813
814 out_be32((void *)RGMII_FER, rmiifer);
815#if defined(CONFIG_460GT)
816 out_be32((void *)RGMII_FER + RGMII1_BASE_OFFSET, rmiifer1);
817#endif
818
819 /* bypass the TAHOE0/TAHOE1 cores for U-Boot */
820 mfsdr(SDR0_ETH_CFG, eth_cfg);
821 eth_cfg |= (SDR0_ETH_CFG_TAHOE0_BYPASS | SDR0_ETH_CFG_TAHOE1_BYPASS);
822 mtsdr(SDR0_ETH_CFG, eth_cfg);
823
824 return 0;
825}
826#endif /* CONFIG_460EX || CONFIG_460GT */
827
Stefan Roese9c2a6472007-10-31 18:01:24 +0100828static inline void *malloc_aligned(u32 size, u32 align)
829{
830 return (void *)(((u32)malloc(size + align) + align - 1) &
831 ~(align - 1));
832}
833
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200834static int ppc_4xx_eth_init (struct eth_device *dev, bd_t * bis)
wdenk544e9732004-02-06 23:19:44 +0000835{
Stefan Roese9c2a6472007-10-31 18:01:24 +0100836 int i;
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200837 unsigned long reg = 0;
wdenk544e9732004-02-06 23:19:44 +0000838 unsigned long msr;
839 unsigned long speed;
840 unsigned long duplex;
841 unsigned long failsafe;
842 unsigned mode_reg;
843 unsigned short devnum;
844 unsigned short reg_short;
Stefan Roese42fbddd2006-09-07 11:51:23 +0200845#if defined(CONFIG_440GX) || \
846 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roese153b3e22007-10-05 17:10:59 +0200847 defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
Stefan Roesebdd13d12008-03-11 15:05:26 +0100848 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roese153b3e22007-10-05 17:10:59 +0200849 defined(CONFIG_405EX)
Felix Radenskyd1de78e2009-05-31 20:44:15 +0300850 u32 opbfreq;
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200851 sys_info_t sysinfo;
Alessio Centazzoec530842009-07-11 11:56:06 -0700852#if defined(CONFIG_440GX) || \
Stefan Roese153b3e22007-10-05 17:10:59 +0200853 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roesebdd13d12008-03-11 15:05:26 +0100854 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roese153b3e22007-10-05 17:10:59 +0200855 defined(CONFIG_405EX)
Stefan Roese0eb592d2011-11-15 08:01:58 +0000856 __maybe_unused int ethgroup = -1;
Stefan Roese99644742005-11-29 18:18:21 +0100857#endif
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200858#endif
Stefan Roese9c2a6472007-10-31 18:01:24 +0100859 u32 bd_cached;
860 u32 bd_uncached = 0;
Anatolij Gustschina41f9182008-02-25 20:54:04 +0100861#ifdef CONFIG_4xx_DCACHE
862 static u32 last_used_ea = 0;
863#endif
Stefan Roesed3df15f2008-04-03 14:50:34 +0200864#if defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
865 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
866 defined(CONFIG_405EX)
867 int rgmii_channel;
868#endif
wdenk544e9732004-02-06 23:19:44 +0000869
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200870 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenk544e9732004-02-06 23:19:44 +0000871
872 /* before doing anything, figure out if we have a MAC address */
873 /* if not, bail */
Stefan Roese03510612005-10-10 17:43:58 +0200874 if (memcmp (dev->enetaddr, "\0\0\0\0\0\0", 6) == 0) {
875 printf("ERROR: ethaddr not set!\n");
wdenk544e9732004-02-06 23:19:44 +0000876 return -1;
Stefan Roese03510612005-10-10 17:43:58 +0200877 }
wdenk544e9732004-02-06 23:19:44 +0000878
Stefan Roese42fbddd2006-09-07 11:51:23 +0200879#if defined(CONFIG_440GX) || \
880 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roese153b3e22007-10-05 17:10:59 +0200881 defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
Stefan Roesebdd13d12008-03-11 15:05:26 +0100882 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roese153b3e22007-10-05 17:10:59 +0200883 defined(CONFIG_405EX)
wdenk544e9732004-02-06 23:19:44 +0000884 /* Need to get the OPB frequency so we can access the PHY */
885 get_sys_info (&sysinfo);
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200886#endif
wdenk544e9732004-02-06 23:19:44 +0000887
wdenk544e9732004-02-06 23:19:44 +0000888 msr = mfmsr ();
889 mtmsr (msr & ~(MSR_EE)); /* disable interrupts */
890
891 devnum = hw_p->devnum;
892
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200893#ifdef INFO_4XX_ENET
wdenk544e9732004-02-06 23:19:44 +0000894 /* AS.HARNOIS
895 * We should have :
Wolfgang Denk0cbaf642005-09-25 00:53:22 +0200896 * hw_p->stats.pkts_handled <= hw_p->stats.pkts_rx <= hw_p->stats.pkts_handled+PKTBUFSRX
wdenk544e9732004-02-06 23:19:44 +0000897 * In the most cases hw_p->stats.pkts_handled = hw_p->stats.pkts_rx, but it
898 * is possible that new packets (without relationship with
899 * current transfer) have got the time to arrived before
900 * netloop calls eth_halt
901 */
Robert P. J. Dayc5b1e5d2016-09-07 14:27:59 -0400902 printf ("About preceding transfer (eth%d):\n"
wdenk544e9732004-02-06 23:19:44 +0000903 "- Sent packet number %d\n"
904 "- Received packet number %d\n"
905 "- Handled packet number %d\n",
906 hw_p->devnum,
907 hw_p->stats.pkts_tx,
908 hw_p->stats.pkts_rx, hw_p->stats.pkts_handled);
909
910 hw_p->stats.pkts_tx = 0;
911 hw_p->stats.pkts_rx = 0;
912 hw_p->stats.pkts_handled = 0;
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200913 hw_p->print_speed = 1; /* print speed message again next time */
wdenk544e9732004-02-06 23:19:44 +0000914#endif
915
Wolfgang Denk0cbaf642005-09-25 00:53:22 +0200916 hw_p->tx_err_index = 0; /* Transmit Error Index for tx_err_log */
917 hw_p->rx_err_index = 0; /* Receive Error Index for rx_err_log */
wdenk544e9732004-02-06 23:19:44 +0000918
919 hw_p->rx_slot = 0; /* MAL Receive Slot */
920 hw_p->rx_i_index = 0; /* Receive Interrupt Queue Index */
921 hw_p->rx_u_index = 0; /* Receive User Queue Index */
922
923 hw_p->tx_slot = 0; /* MAL Transmit Slot */
924 hw_p->tx_i_index = 0; /* Transmit Interrupt Queue Index */
925 hw_p->tx_u_index = 0; /* Transmit User Queue Index */
926
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200927#if defined(CONFIG_440) && !defined(CONFIG_440SP) && !defined(CONFIG_440SPE)
wdenk544e9732004-02-06 23:19:44 +0000928 /* set RMII mode */
929 /* NOTE: 440GX spec states that mode is mutually exclusive */
930 /* NOTE: Therefore, disable all other EMACS, since we handle */
931 /* NOTE: only one emac at a time */
932 reg = 0;
Niklaus Giger728bd0a2009-10-04 20:04:20 +0200933 out_be32((void *)ZMII0_FER, 0);
wdenk544e9732004-02-06 23:19:44 +0000934 udelay (100);
wdenk544e9732004-02-06 23:19:44 +0000935
Stefan Roesebdd13d12008-03-11 15:05:26 +0100936#if defined(CONFIG_440GP) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
Niklaus Giger728bd0a2009-10-04 20:04:20 +0200937 out_be32((void *)ZMII0_FER, (ZMII_FER_RMII | ZMII_FER_MDI) << ZMII_FER_V (devnum));
Stefan Roesebdd13d12008-03-11 15:05:26 +0100938#elif defined(CONFIG_440GX) || \
939 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
940 defined(CONFIG_460EX) || defined(CONFIG_460GT)
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200941 ethgroup = ppc_4xx_eth_setup_bridge(devnum, bis);
wdenk00fe1612004-03-14 00:07:33 +0000942#endif
Stefan Roese797d8572005-08-11 17:56:56 +0200943
Niklaus Giger728bd0a2009-10-04 20:04:20 +0200944 out_be32((void *)ZMII0_SSR, ZMII0_SSR_SP << ZMII0_SSR_V(devnum));
Stefan Roese99644742005-11-29 18:18:21 +0100945#endif /* defined(CONFIG_440) && !defined(CONFIG_440SP) */
Stefan Roese153b3e22007-10-05 17:10:59 +0200946#if defined(CONFIG_405EX)
947 ethgroup = ppc_4xx_eth_setup_bridge(devnum, bis);
948#endif
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200949
Stefan Roesebdd13d12008-03-11 15:05:26 +0100950 sync();
wdenk00fe1612004-03-14 00:07:33 +0000951
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200952 /* provide clocks for EMAC internal loopback */
Stefan Roesebdd13d12008-03-11 15:05:26 +0100953 emac_loopback_enable(hw_p);
wdenk00fe1612004-03-14 00:07:33 +0000954
Stefan Roesebdd13d12008-03-11 15:05:26 +0100955 /* EMAC RESET */
Niklaus Giger728bd0a2009-10-04 20:04:20 +0200956 out_be32((void *)EMAC0_MR0 + hw_p->hw_addr, EMAC_MR0_SRST);
wdenk544e9732004-02-06 23:19:44 +0000957
Stefan Roesebdd13d12008-03-11 15:05:26 +0100958 /* remove clocks for EMAC internal loopback */
959 emac_loopback_disable(hw_p);
960
wdenk544e9732004-02-06 23:19:44 +0000961 failsafe = 1000;
Niklaus Giger728bd0a2009-10-04 20:04:20 +0200962 while ((in_be32((void *)EMAC0_MR0 + hw_p->hw_addr) & (EMAC_MR0_SRST)) && failsafe) {
wdenk544e9732004-02-06 23:19:44 +0000963 udelay (1000);
964 failsafe--;
965 }
Stefan Roese42fbddd2006-09-07 11:51:23 +0200966 if (failsafe <= 0)
967 printf("\nProblem resetting EMAC!\n");
wdenk544e9732004-02-06 23:19:44 +0000968
Stefan Roese42fbddd2006-09-07 11:51:23 +0200969#if defined(CONFIG_440GX) || \
970 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roese153b3e22007-10-05 17:10:59 +0200971 defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
Stefan Roesebdd13d12008-03-11 15:05:26 +0100972 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roese153b3e22007-10-05 17:10:59 +0200973 defined(CONFIG_405EX)
wdenk544e9732004-02-06 23:19:44 +0000974 /* Whack the M1 register */
975 mode_reg = 0x0;
976 mode_reg &= ~0x00000038;
Felix Radenskyd1de78e2009-05-31 20:44:15 +0300977 opbfreq = sysinfo.freqOPB / 1000000;
978 if (opbfreq <= 50);
979 else if (opbfreq <= 66)
Niklaus Giger728bd0a2009-10-04 20:04:20 +0200980 mode_reg |= EMAC_MR1_OBCI_66;
Felix Radenskyd1de78e2009-05-31 20:44:15 +0300981 else if (opbfreq <= 83)
Niklaus Giger728bd0a2009-10-04 20:04:20 +0200982 mode_reg |= EMAC_MR1_OBCI_83;
Felix Radenskyd1de78e2009-05-31 20:44:15 +0300983 else if (opbfreq <= 100)
Niklaus Giger728bd0a2009-10-04 20:04:20 +0200984 mode_reg |= EMAC_MR1_OBCI_100;
wdenk544e9732004-02-06 23:19:44 +0000985 else
Niklaus Giger728bd0a2009-10-04 20:04:20 +0200986 mode_reg |= EMAC_MR1_OBCI_GT100;
wdenk544e9732004-02-06 23:19:44 +0000987
Niklaus Giger728bd0a2009-10-04 20:04:20 +0200988 out_be32((void *)EMAC0_MR1 + hw_p->hw_addr, mode_reg);
Stefan Roese99644742005-11-29 18:18:21 +0100989#endif /* defined(CONFIG_440GX) || defined(CONFIG_440SP) */
wdenk544e9732004-02-06 23:19:44 +0000990
Victor Gallardo45a06ff2008-09-04 23:49:36 -0700991#if defined(CONFIG_GPCS_PHY_ADDR) || defined(CONFIG_GPCS_PHY1_ADDR) || \
992 defined(CONFIG_GPCS_PHY2_ADDR) || defined(CONFIG_GPCS_PHY3_ADDR)
993 if (bis->bi_phymode[devnum] == BI_PHYMODE_SGMII) {
994 /*
995 * In SGMII mode, GPCS access is needed for
996 * communication with the internal SGMII SerDes.
997 */
998 switch (devnum) {
999#if defined(CONFIG_GPCS_PHY_ADDR)
1000 case 0:
1001 reg = CONFIG_GPCS_PHY_ADDR;
1002 break;
1003#endif
1004#if defined(CONFIG_GPCS_PHY1_ADDR)
1005 case 1:
1006 reg = CONFIG_GPCS_PHY1_ADDR;
1007 break;
1008#endif
1009#if defined(CONFIG_GPCS_PHY2_ADDR)
1010 case 2:
1011 reg = CONFIG_GPCS_PHY2_ADDR;
1012 break;
1013#endif
1014#if defined(CONFIG_GPCS_PHY3_ADDR)
1015 case 3:
1016 reg = CONFIG_GPCS_PHY3_ADDR;
1017 break;
1018#endif
1019 }
1020
Niklaus Giger728bd0a2009-10-04 20:04:20 +02001021 mode_reg = in_be32((void *)EMAC0_MR1 + hw_p->hw_addr);
1022 mode_reg |= EMAC_MR1_MF_1000GPCS | EMAC_MR1_IPPA_SET(reg);
1023 out_be32((void *)EMAC0_MR1 + hw_p->hw_addr, mode_reg);
Victor Gallardo45a06ff2008-09-04 23:49:36 -07001024
1025 /* Configure GPCS interface to recommended setting for SGMII */
1026 miiphy_reset(dev->name, reg);
1027 miiphy_write(dev->name, reg, 0x04, 0x8120); /* AsymPause, FDX */
1028 miiphy_write(dev->name, reg, 0x07, 0x2801); /* msg_pg, toggle */
1029 miiphy_write(dev->name, reg, 0x00, 0x0140); /* 1Gbps, FDX */
1030 }
1031#endif /* defined(CONFIG_GPCS_PHY_ADDR) */
1032
wdenk544e9732004-02-06 23:19:44 +00001033 /* wait for PHY to complete auto negotiation */
1034 reg_short = 0;
wdenk544e9732004-02-06 23:19:44 +00001035 switch (devnum) {
1036 case 0:
1037 reg = CONFIG_PHY_ADDR;
1038 break;
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001039#if defined (CONFIG_PHY1_ADDR)
wdenk544e9732004-02-06 23:19:44 +00001040 case 1:
1041 reg = CONFIG_PHY1_ADDR;
1042 break;
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001043#endif
Stefan Roese52df4192008-03-19 16:20:49 +01001044#if defined (CONFIG_PHY2_ADDR)
wdenk544e9732004-02-06 23:19:44 +00001045 case 2:
1046 reg = CONFIG_PHY2_ADDR;
1047 break;
Stefan Roese52df4192008-03-19 16:20:49 +01001048#endif
1049#if defined (CONFIG_PHY3_ADDR)
wdenk544e9732004-02-06 23:19:44 +00001050 case 3:
1051 reg = CONFIG_PHY3_ADDR;
1052 break;
1053#endif
1054 default:
1055 reg = CONFIG_PHY_ADDR;
1056 break;
1057 }
1058
wdenk56ed43e2004-02-22 23:46:08 +00001059 bis->bi_phynum[devnum] = reg;
1060
Victor Gallardo45a06ff2008-09-04 23:49:36 -07001061 if (reg == CONFIG_FIXED_PHY)
1062 goto get_speed;
1063
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001064#if defined(CONFIG_PHY_RESET)
wdenk97e8bda2004-09-29 22:43:59 +00001065 /*
1066 * Reset the phy, only if its the first time through
1067 * otherwise, just check the speeds & feeds
1068 */
1069 if (hw_p->first_init == 0) {
Stefan Roese21df1a42006-11-27 14:46:06 +01001070#if defined(CONFIG_M88E1111_PHY)
Stefan Roese42fbddd2006-09-07 11:51:23 +02001071 miiphy_write (dev->name, reg, 0x14, 0x0ce3);
1072 miiphy_write (dev->name, reg, 0x18, 0x4101);
1073 miiphy_write (dev->name, reg, 0x09, 0x0e00);
1074 miiphy_write (dev->name, reg, 0x04, 0x01e1);
Stefan Roese059d6a92010-06-29 09:23:53 +02001075#if defined(CONFIG_M88E1111_DISABLE_FIBER)
1076 miiphy_read(dev->name, reg, 0x1b, &reg_short);
1077 reg_short |= 0x8000;
1078 miiphy_write(dev->name, reg, 0x1b, reg_short);
1079#endif
Stefan Roese42fbddd2006-09-07 11:51:23 +02001080#endif
Victor Gallardo45a06ff2008-09-04 23:49:36 -07001081#if defined(CONFIG_M88E1112_PHY)
1082 if (bis->bi_phymode[devnum] == BI_PHYMODE_SGMII) {
1083 /*
1084 * Marvell 88E1112 PHY needs to have the SGMII MAC
1085 * interace (page 2) properly configured to
1086 * communicate with the 460EX/GT GPCS interface.
1087 */
1088
1089 /* Set access to Page 2 */
1090 miiphy_write(dev->name, reg, 0x16, 0x0002);
1091
1092 miiphy_write(dev->name, reg, 0x00, 0x0040); /* 1Gbps */
1093 miiphy_read(dev->name, reg, 0x1a, &reg_short);
1094 reg_short |= 0x8000; /* bypass Auto-Negotiation */
1095 miiphy_write(dev->name, reg, 0x1a, reg_short);
1096 miiphy_reset(dev->name, reg); /* reset MAC interface */
1097
1098 /* Reset access to Page 0 */
1099 miiphy_write(dev->name, reg, 0x16, 0x0000);
1100 }
1101#endif /* defined(CONFIG_M88E1112_PHY) */
Marian Balakowiczaab8c492005-10-28 22:30:33 +02001102 miiphy_reset (dev->name, reg);
wdenk544e9732004-02-06 23:19:44 +00001103
Stefan Roese42fbddd2006-09-07 11:51:23 +02001104#if defined(CONFIG_440GX) || \
1105 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roesebdd13d12008-03-11 15:05:26 +01001106 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roese153b3e22007-10-05 17:10:59 +02001107 defined(CONFIG_405EX)
Stefan Roese42fbddd2006-09-07 11:51:23 +02001108
wdenk00fe1612004-03-14 00:07:33 +00001109#if defined(CONFIG_CIS8201_PHY)
wdenk7ad5e4c2004-04-25 15:41:35 +00001110 /*
Stefan Roese363330b2005-08-04 17:09:16 +02001111 * Cicada 8201 PHY needs to have an extended register whacked
1112 * for RGMII mode.
wdenk7ad5e4c2004-04-25 15:41:35 +00001113 */
Stefan Roese42fbddd2006-09-07 11:51:23 +02001114 if (((devnum == 2) || (devnum == 3)) && (4 == ethgroup)) {
Stefan Roese5ff4c3f2005-08-15 12:31:23 +02001115#if defined(CONFIG_CIS8201_SHORT_ETCH)
Marian Balakowiczaab8c492005-10-28 22:30:33 +02001116 miiphy_write (dev->name, reg, 23, 0x1300);
Stefan Roese5ff4c3f2005-08-15 12:31:23 +02001117#else
Marian Balakowiczaab8c492005-10-28 22:30:33 +02001118 miiphy_write (dev->name, reg, 23, 0x1000);
Stefan Roese5ff4c3f2005-08-15 12:31:23 +02001119#endif
Stefan Roese363330b2005-08-04 17:09:16 +02001120 /*
1121 * Vitesse VSC8201/Cicada CIS8201 errata:
1122 * Interoperability problem with Intel 82547EI phys
1123 * This work around (provided by Vitesse) changes
1124 * the default timer convergence from 8ms to 12ms
1125 */
Marian Balakowiczaab8c492005-10-28 22:30:33 +02001126 miiphy_write (dev->name, reg, 0x1f, 0x2a30);
1127 miiphy_write (dev->name, reg, 0x08, 0x0200);
1128 miiphy_write (dev->name, reg, 0x1f, 0x52b5);
1129 miiphy_write (dev->name, reg, 0x02, 0x0004);
1130 miiphy_write (dev->name, reg, 0x01, 0x0671);
1131 miiphy_write (dev->name, reg, 0x00, 0x8fae);
1132 miiphy_write (dev->name, reg, 0x1f, 0x2a30);
1133 miiphy_write (dev->name, reg, 0x08, 0x0000);
1134 miiphy_write (dev->name, reg, 0x1f, 0x0000);
Stefan Roese363330b2005-08-04 17:09:16 +02001135 /* end Vitesse/Cicada errata */
1136 }
Stefan Roesef00486d2008-09-05 14:11:40 +02001137#endif /* defined(CONFIG_CIS8201_PHY) */
Stefan Roese8d982302007-01-18 10:25:34 +01001138
1139#if defined(CONFIG_ET1011C_PHY)
1140 /*
1141 * Agere ET1011c PHY needs to have an extended register whacked
1142 * for RGMII mode.
1143 */
1144 if (((devnum == 2) || (devnum ==3)) && (4 == ethgroup)) {
1145 miiphy_read (dev->name, reg, 0x16, &reg_short);
1146 reg_short &= ~(0x7);
1147 reg_short |= 0x6; /* RGMII DLL Delay*/
1148 miiphy_write (dev->name, reg, 0x16, reg_short);
1149
1150 miiphy_read (dev->name, reg, 0x17, &reg_short);
1151 reg_short &= ~(0x40);
1152 miiphy_write (dev->name, reg, 0x17, reg_short);
1153
1154 miiphy_write(dev->name, reg, 0x1c, 0x74f0);
1155 }
Stefan Roesef00486d2008-09-05 14:11:40 +02001156#endif /* defined(CONFIG_ET1011C_PHY) */
Stefan Roese8d982302007-01-18 10:25:34 +01001157
Stefan Roesef00486d2008-09-05 14:11:40 +02001158#endif /* defined(CONFIG_440GX) ... */
wdenk97e8bda2004-09-29 22:43:59 +00001159 /* Start/Restart autonegotiation */
Marian Balakowiczaab8c492005-10-28 22:30:33 +02001160 phy_setup_aneg (dev->name, reg);
wdenk97e8bda2004-09-29 22:43:59 +00001161 udelay (1000);
1162 }
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001163#endif /* defined(CONFIG_PHY_RESET) */
wdenk544e9732004-02-06 23:19:44 +00001164
Mike Frysingerd63ee712010-12-23 15:40:12 -05001165 miiphy_read (dev->name, reg, MII_BMSR, &reg_short);
wdenk544e9732004-02-06 23:19:44 +00001166
1167 /*
wdenk00fe1612004-03-14 00:07:33 +00001168 * Wait if PHY is capable of autonegotiation and autonegotiation is not complete
wdenk544e9732004-02-06 23:19:44 +00001169 */
Mike Frysingerd63ee712010-12-23 15:40:12 -05001170 if ((reg_short & BMSR_ANEGCAPABLE)
1171 && !(reg_short & BMSR_ANEGCOMPLETE)) {
wdenk544e9732004-02-06 23:19:44 +00001172 puts ("Waiting for PHY auto negotiation to complete");
1173 i = 0;
Mike Frysingerd63ee712010-12-23 15:40:12 -05001174 while (!(reg_short & BMSR_ANEGCOMPLETE)) {
wdenk544e9732004-02-06 23:19:44 +00001175 /*
1176 * Timeout reached ?
1177 */
1178 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
1179 puts (" TIMEOUT !\n");
1180 break;
1181 }
1182
1183 if ((i++ % 1000) == 0) {
1184 putc ('.');
1185 }
1186 udelay (1000); /* 1 ms */
Mike Frysingerd63ee712010-12-23 15:40:12 -05001187 miiphy_read (dev->name, reg, MII_BMSR, &reg_short);
wdenk544e9732004-02-06 23:19:44 +00001188 }
1189 puts (" done\n");
1190 udelay (500000); /* another 500 ms (results in faster booting) */
1191 }
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001192
Victor Gallardo45a06ff2008-09-04 23:49:36 -07001193get_speed:
1194 if (reg == CONFIG_FIXED_PHY) {
1195 for (i = 0; i < ARRAY_SIZE(fixed_phy_port); i++) {
1196 if (devnum == fixed_phy_port[i].devnum) {
1197 speed = fixed_phy_port[i].speed;
1198 duplex = fixed_phy_port[i].duplex;
1199 break;
1200 }
1201 }
1202
1203 if (i == ARRAY_SIZE(fixed_phy_port)) {
1204 printf("ERROR: PHY (%s) not configured correctly!\n",
1205 dev->name);
1206 return -1;
1207 }
1208 } else {
1209 speed = miiphy_speed(dev->name, reg);
1210 duplex = miiphy_duplex(dev->name, reg);
1211 }
wdenk544e9732004-02-06 23:19:44 +00001212
1213 if (hw_p->print_speed) {
1214 hw_p->print_speed = 0;
Stefan Roese8d982302007-01-18 10:25:34 +01001215 printf ("ENET Speed is %d Mbps - %s duplex connection (EMAC%d)\n",
1216 (int) speed, (duplex == HALF) ? "HALF" : "FULL",
1217 hw_p->devnum);
wdenk544e9732004-02-06 23:19:44 +00001218 }
1219
Stefan Roesebdd13d12008-03-11 15:05:26 +01001220#if defined(CONFIG_440) && \
1221 !defined(CONFIG_440SP) && !defined(CONFIG_440SPE) && \
1222 !defined(CONFIG_440EPX) && !defined(CONFIG_440GRX) && \
1223 !defined(CONFIG_460EX) && !defined(CONFIG_460GT)
Stefan Roeseb30f2a12005-08-08 12:42:22 +02001224#if defined(CONFIG_440EP) || defined(CONFIG_440GR)
Stefan Roese918010a2009-09-09 16:25:29 +02001225 mfsdr(SDR0_MFR, reg);
Stefan Roese326c9712005-08-01 16:41:48 +02001226 if (speed == 100) {
1227 reg = (reg & ~SDR0_MFR_ZMII_MODE_MASK) | SDR0_MFR_ZMII_MODE_RMII_100M;
1228 } else {
1229 reg = (reg & ~SDR0_MFR_ZMII_MODE_MASK) | SDR0_MFR_ZMII_MODE_RMII_10M;
1230 }
Stefan Roese918010a2009-09-09 16:25:29 +02001231 mtsdr(SDR0_MFR, reg);
Stefan Roese326c9712005-08-01 16:41:48 +02001232#endif
Stefan Roese797d8572005-08-11 17:56:56 +02001233
wdenk544e9732004-02-06 23:19:44 +00001234 /* Set ZMII/RGMII speed according to the phy link speed */
Niklaus Giger728bd0a2009-10-04 20:04:20 +02001235 reg = in_be32((void *)ZMII0_SSR);
wdenked2ac4b2004-03-14 18:23:55 +00001236 if ( (speed == 100) || (speed == 1000) )
Niklaus Giger728bd0a2009-10-04 20:04:20 +02001237 out_be32((void *)ZMII0_SSR, reg | (ZMII0_SSR_SP << ZMII0_SSR_V (devnum)));
wdenk544e9732004-02-06 23:19:44 +00001238 else
Niklaus Giger728bd0a2009-10-04 20:04:20 +02001239 out_be32((void *)ZMII0_SSR, reg & (~(ZMII0_SSR_SP << ZMII0_SSR_V (devnum))));
wdenk544e9732004-02-06 23:19:44 +00001240
1241 if ((devnum == 2) || (devnum == 3)) {
1242 if (speed == 1000)
1243 reg = (RGMII_SSR_SP_1000MBPS << RGMII_SSR_V (devnum));
1244 else if (speed == 100)
1245 reg = (RGMII_SSR_SP_100MBPS << RGMII_SSR_V (devnum));
Stefan Roese42fbddd2006-09-07 11:51:23 +02001246 else if (speed == 10)
wdenk544e9732004-02-06 23:19:44 +00001247 reg = (RGMII_SSR_SP_10MBPS << RGMII_SSR_V (devnum));
Stefan Roese42fbddd2006-09-07 11:51:23 +02001248 else {
1249 printf("Error in RGMII Speed\n");
1250 return -1;
1251 }
Stefan Roese9c2a6472007-10-31 18:01:24 +01001252 out_be32((void *)RGMII_SSR, reg);
wdenk544e9732004-02-06 23:19:44 +00001253 }
Stefan Roese99644742005-11-29 18:18:21 +01001254#endif /* defined(CONFIG_440) && !defined(CONFIG_440SP) */
wdenk544e9732004-02-06 23:19:44 +00001255
Stefan Roese153b3e22007-10-05 17:10:59 +02001256#if defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roesebdd13d12008-03-11 15:05:26 +01001257 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roese153b3e22007-10-05 17:10:59 +02001258 defined(CONFIG_405EX)
Stefan Roesed3df15f2008-04-03 14:50:34 +02001259 if (devnum >= 2)
1260 rgmii_channel = devnum - 2;
1261 else
1262 rgmii_channel = devnum;
1263
Stefan Roese42fbddd2006-09-07 11:51:23 +02001264 if (speed == 1000)
Stefan Roesed3df15f2008-04-03 14:50:34 +02001265 reg = (RGMII_SSR_SP_1000MBPS << RGMII_SSR_V(rgmii_channel));
Stefan Roese42fbddd2006-09-07 11:51:23 +02001266 else if (speed == 100)
Stefan Roesed3df15f2008-04-03 14:50:34 +02001267 reg = (RGMII_SSR_SP_100MBPS << RGMII_SSR_V(rgmii_channel));
Stefan Roese42fbddd2006-09-07 11:51:23 +02001268 else if (speed == 10)
Stefan Roesed3df15f2008-04-03 14:50:34 +02001269 reg = (RGMII_SSR_SP_10MBPS << RGMII_SSR_V(rgmii_channel));
Stefan Roese42fbddd2006-09-07 11:51:23 +02001270 else {
1271 printf("Error in RGMII Speed\n");
1272 return -1;
1273 }
Stefan Roese697100952007-10-23 14:03:17 +02001274 out_be32((void *)RGMII_SSR, reg);
Stefan Roesebdd13d12008-03-11 15:05:26 +01001275#if defined(CONFIG_460GT)
1276 if ((devnum == 2) || (devnum == 3))
1277 out_be32((void *)RGMII_SSR + RGMII1_BASE_OFFSET, reg);
1278#endif
Stefan Roese42fbddd2006-09-07 11:51:23 +02001279#endif
1280
wdenk544e9732004-02-06 23:19:44 +00001281 /* set the Mal configuration reg */
Stefan Roese42fbddd2006-09-07 11:51:23 +02001282#if defined(CONFIG_440GX) || \
1283 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roese153b3e22007-10-05 17:10:59 +02001284 defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
Stefan Roesebdd13d12008-03-11 15:05:26 +01001285 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roese153b3e22007-10-05 17:10:59 +02001286 defined(CONFIG_405EX)
Stefan Roese918010a2009-09-09 16:25:29 +02001287 mtdcr (MAL0_CFG, MAL_CR_PLBB | MAL_CR_OPBBL | MAL_CR_LEA |
Stefan Roese363330b2005-08-04 17:09:16 +02001288 MAL_CR_PLBLT_DEFAULT | MAL_CR_EOPIE | 0x00330000);
1289#else
Stefan Roese918010a2009-09-09 16:25:29 +02001290 mtdcr (MAL0_CFG, MAL_CR_PLBB | MAL_CR_OPBBL | MAL_CR_LEA | MAL_CR_PLBLT_DEFAULT);
wdenk544e9732004-02-06 23:19:44 +00001291 /* Errata 1.12: MAL_1 -- Disable MAL bursting */
Stefan Roese363330b2005-08-04 17:09:16 +02001292 if (get_pvr() == PVR_440GP_RB) {
Stefan Roese918010a2009-09-09 16:25:29 +02001293 mtdcr (MAL0_CFG, mfdcr(MAL0_CFG) & ~MAL_CR_PLBB);
Stefan Roese363330b2005-08-04 17:09:16 +02001294 }
1295#endif
wdenk544e9732004-02-06 23:19:44 +00001296
wdenk544e9732004-02-06 23:19:44 +00001297 /*
1298 * Malloc MAL buffer desciptors, make sure they are
1299 * aligned on cache line boundary size
1300 * (401/403/IOP480 = 16, 405 = 32)
1301 * and doesn't cross cache block boundaries.
1302 */
Stefan Roese9c2a6472007-10-31 18:01:24 +01001303 if (hw_p->first_init == 0) {
1304 debug("*** Allocating descriptor memory ***\n");
wdenk544e9732004-02-06 23:19:44 +00001305
Stefan Roese9c2a6472007-10-31 18:01:24 +01001306 bd_cached = (u32)malloc_aligned(MAL_ALLOC_SIZE, 4096);
1307 if (!bd_cached) {
Stefan Roese251161b2008-07-10 09:58:06 +02001308 printf("%s: Error allocating MAL descriptor buffers!\n", __func__);
Stefan Roese9c2a6472007-10-31 18:01:24 +01001309 return -1;
1310 }
Stefan Roese5ff4c3f2005-08-15 12:31:23 +02001311
Stefan Roese9c2a6472007-10-31 18:01:24 +01001312#ifdef CONFIG_4xx_DCACHE
Matthias Fuchs211105a2007-12-14 11:19:56 +01001313 flush_dcache_range(bd_cached, bd_cached + MAL_ALLOC_SIZE);
Anatolij Gustschina41f9182008-02-25 20:54:04 +01001314 if (!last_used_ea)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001315#if defined(CONFIG_SYS_MEM_TOP_HIDE)
1316 bd_uncached = bis->bi_memsize + CONFIG_SYS_MEM_TOP_HIDE;
Anatolij Gustschin6ed056e2008-04-17 18:18:00 +02001317#else
Anatolij Gustschina41f9182008-02-25 20:54:04 +01001318 bd_uncached = bis->bi_memsize;
Anatolij Gustschin6ed056e2008-04-17 18:18:00 +02001319#endif
Anatolij Gustschina41f9182008-02-25 20:54:04 +01001320 else
1321 bd_uncached = last_used_ea + MAL_ALLOC_SIZE;
1322
1323 last_used_ea = bd_uncached;
Stefan Roese9c2a6472007-10-31 18:01:24 +01001324 program_tlb(bd_cached, bd_uncached, MAL_ALLOC_SIZE,
1325 TLB_WORD2_I_ENABLE);
1326#else
1327 bd_uncached = bd_cached;
1328#endif
1329 hw_p->tx_phys = bd_cached;
1330 hw_p->rx_phys = bd_cached + MAL_TX_DESC_SIZE;
1331 hw_p->tx = (mal_desc_t *)(bd_uncached);
1332 hw_p->rx = (mal_desc_t *)(bd_uncached + MAL_TX_DESC_SIZE);
Marek Vasut041b5df2011-10-21 14:17:13 +00001333 debug("hw_p->tx=%p, hw_p->rx=%p\n", hw_p->tx, hw_p->rx);
wdenk544e9732004-02-06 23:19:44 +00001334 }
1335
1336 for (i = 0; i < NUM_TX_BUFF; i++) {
1337 hw_p->tx[i].ctrl = 0;
1338 hw_p->tx[i].data_len = 0;
Stefan Roese9c2a6472007-10-31 18:01:24 +01001339 if (hw_p->first_init == 0)
1340 hw_p->txbuf_ptr = malloc_aligned(MAL_ALLOC_SIZE,
1341 L1_CACHE_BYTES);
wdenk544e9732004-02-06 23:19:44 +00001342 hw_p->tx[i].data_ptr = hw_p->txbuf_ptr;
1343 if ((NUM_TX_BUFF - 1) == i)
1344 hw_p->tx[i].ctrl |= MAL_TX_CTRL_WRAP;
1345 hw_p->tx_run[i] = -1;
Marek Vasut041b5df2011-10-21 14:17:13 +00001346 debug("TX_BUFF %d @ 0x%08x\n", i, (u32)hw_p->tx[i].data_ptr);
wdenk544e9732004-02-06 23:19:44 +00001347 }
1348
1349 for (i = 0; i < NUM_RX_BUFF; i++) {
1350 hw_p->rx[i].ctrl = 0;
1351 hw_p->rx[i].data_len = 0;
Joe Hershberger9f09a362015-04-08 01:41:06 -05001352 hw_p->rx[i].data_ptr = (char *)net_rx_packets[i];
wdenk544e9732004-02-06 23:19:44 +00001353 if ((NUM_RX_BUFF - 1) == i)
1354 hw_p->rx[i].ctrl |= MAL_RX_CTRL_WRAP;
1355 hw_p->rx[i].ctrl |= MAL_RX_CTRL_EMPTY | MAL_RX_CTRL_INTR;
1356 hw_p->rx_ready[i] = -1;
Marek Vasut041b5df2011-10-21 14:17:13 +00001357 debug("RX_BUFF %d @ 0x%08x\n", i, (u32)hw_p->rx[i].data_ptr);
wdenk544e9732004-02-06 23:19:44 +00001358 }
1359
1360 reg = 0x00000000;
1361
1362 reg |= dev->enetaddr[0]; /* set high address */
1363 reg = reg << 8;
1364 reg |= dev->enetaddr[1];
1365
Niklaus Giger728bd0a2009-10-04 20:04:20 +02001366 out_be32((void *)EMAC0_IAH + hw_p->hw_addr, reg);
wdenk544e9732004-02-06 23:19:44 +00001367
1368 reg = 0x00000000;
1369 reg |= dev->enetaddr[2]; /* set low address */
1370 reg = reg << 8;
1371 reg |= dev->enetaddr[3];
1372 reg = reg << 8;
1373 reg |= dev->enetaddr[4];
1374 reg = reg << 8;
1375 reg |= dev->enetaddr[5];
1376
Niklaus Giger728bd0a2009-10-04 20:04:20 +02001377 out_be32((void *)EMAC0_IAL + hw_p->hw_addr, reg);
wdenk544e9732004-02-06 23:19:44 +00001378
1379 switch (devnum) {
1380 case 1:
1381 /* setup MAL tx & rx channel pointers */
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001382#if defined (CONFIG_405EP) || defined (CONFIG_440EP) || defined (CONFIG_440GR)
Stefan Roese918010a2009-09-09 16:25:29 +02001383 mtdcr (MAL0_TXCTP2R, hw_p->tx_phys);
Stefan Roese326c9712005-08-01 16:41:48 +02001384#else
Stefan Roese918010a2009-09-09 16:25:29 +02001385 mtdcr (MAL0_TXCTP1R, hw_p->tx_phys);
Stefan Roese326c9712005-08-01 16:41:48 +02001386#endif
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001387#if defined(CONFIG_440)
Stefan Roese918010a2009-09-09 16:25:29 +02001388 mtdcr (MAL0_TXBADDR, 0x0);
1389 mtdcr (MAL0_RXBADDR, 0x0);
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001390#endif
Stefan Roesebdd13d12008-03-11 15:05:26 +01001391
1392#if defined(CONFIG_460EX) || defined(CONFIG_460GT)
Stefan Roese918010a2009-09-09 16:25:29 +02001393 mtdcr (MAL0_RXCTP8R, hw_p->rx_phys);
Stefan Roesebdd13d12008-03-11 15:05:26 +01001394 /* set RX buffer size */
Stefan Roese918010a2009-09-09 16:25:29 +02001395 mtdcr (MAL0_RCBS8, ENET_MAX_MTU_ALIGNED / 16);
Stefan Roesebdd13d12008-03-11 15:05:26 +01001396#else
Stefan Roese918010a2009-09-09 16:25:29 +02001397 mtdcr (MAL0_RXCTP1R, hw_p->rx_phys);
wdenk544e9732004-02-06 23:19:44 +00001398 /* set RX buffer size */
Stefan Roese918010a2009-09-09 16:25:29 +02001399 mtdcr (MAL0_RCBS1, ENET_MAX_MTU_ALIGNED / 16);
Stefan Roesebdd13d12008-03-11 15:05:26 +01001400#endif
wdenk544e9732004-02-06 23:19:44 +00001401 break;
Stefan Roeseb30f2a12005-08-08 12:42:22 +02001402#if defined (CONFIG_440GX)
wdenk544e9732004-02-06 23:19:44 +00001403 case 2:
1404 /* setup MAL tx & rx channel pointers */
Stefan Roese918010a2009-09-09 16:25:29 +02001405 mtdcr (MAL0_TXBADDR, 0x0);
1406 mtdcr (MAL0_RXBADDR, 0x0);
1407 mtdcr (MAL0_TXCTP2R, hw_p->tx_phys);
1408 mtdcr (MAL0_RXCTP2R, hw_p->rx_phys);
wdenk544e9732004-02-06 23:19:44 +00001409 /* set RX buffer size */
Stefan Roese918010a2009-09-09 16:25:29 +02001410 mtdcr (MAL0_RCBS2, ENET_MAX_MTU_ALIGNED / 16);
wdenk544e9732004-02-06 23:19:44 +00001411 break;
1412 case 3:
1413 /* setup MAL tx & rx channel pointers */
Stefan Roese918010a2009-09-09 16:25:29 +02001414 mtdcr (MAL0_TXBADDR, 0x0);
1415 mtdcr (MAL0_TXCTP3R, hw_p->tx_phys);
1416 mtdcr (MAL0_RXBADDR, 0x0);
1417 mtdcr (MAL0_RXCTP3R, hw_p->rx_phys);
wdenk544e9732004-02-06 23:19:44 +00001418 /* set RX buffer size */
Stefan Roese918010a2009-09-09 16:25:29 +02001419 mtdcr (MAL0_RCBS3, ENET_MAX_MTU_ALIGNED / 16);
wdenk544e9732004-02-06 23:19:44 +00001420 break;
Stefan Roese797d8572005-08-11 17:56:56 +02001421#endif /* CONFIG_440GX */
Stefan Roese52df4192008-03-19 16:20:49 +01001422#if defined (CONFIG_460GT)
1423 case 2:
1424 /* setup MAL tx & rx channel pointers */
Stefan Roese918010a2009-09-09 16:25:29 +02001425 mtdcr (MAL0_TXBADDR, 0x0);
1426 mtdcr (MAL0_RXBADDR, 0x0);
1427 mtdcr (MAL0_TXCTP2R, hw_p->tx_phys);
1428 mtdcr (MAL0_RXCTP16R, hw_p->rx_phys);
Stefan Roese52df4192008-03-19 16:20:49 +01001429 /* set RX buffer size */
Stefan Roese918010a2009-09-09 16:25:29 +02001430 mtdcr (MAL0_RCBS16, ENET_MAX_MTU_ALIGNED / 16);
Stefan Roese52df4192008-03-19 16:20:49 +01001431 break;
1432 case 3:
1433 /* setup MAL tx & rx channel pointers */
Stefan Roese918010a2009-09-09 16:25:29 +02001434 mtdcr (MAL0_TXBADDR, 0x0);
1435 mtdcr (MAL0_RXBADDR, 0x0);
1436 mtdcr (MAL0_TXCTP3R, hw_p->tx_phys);
1437 mtdcr (MAL0_RXCTP24R, hw_p->rx_phys);
Stefan Roese52df4192008-03-19 16:20:49 +01001438 /* set RX buffer size */
Stefan Roese918010a2009-09-09 16:25:29 +02001439 mtdcr (MAL0_RCBS24, ENET_MAX_MTU_ALIGNED / 16);
Stefan Roese52df4192008-03-19 16:20:49 +01001440 break;
1441#endif /* CONFIG_460GT */
wdenk544e9732004-02-06 23:19:44 +00001442 case 0:
1443 default:
1444 /* setup MAL tx & rx channel pointers */
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001445#if defined(CONFIG_440)
Stefan Roese918010a2009-09-09 16:25:29 +02001446 mtdcr (MAL0_TXBADDR, 0x0);
1447 mtdcr (MAL0_RXBADDR, 0x0);
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001448#endif
Stefan Roese918010a2009-09-09 16:25:29 +02001449 mtdcr (MAL0_TXCTP0R, hw_p->tx_phys);
1450 mtdcr (MAL0_RXCTP0R, hw_p->rx_phys);
wdenk544e9732004-02-06 23:19:44 +00001451 /* set RX buffer size */
Stefan Roese918010a2009-09-09 16:25:29 +02001452 mtdcr (MAL0_RCBS0, ENET_MAX_MTU_ALIGNED / 16);
wdenk544e9732004-02-06 23:19:44 +00001453 break;
1454 }
1455
1456 /* Enable MAL transmit and receive channels */
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001457#if defined(CONFIG_405EP) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
Stefan Roese918010a2009-09-09 16:25:29 +02001458 mtdcr (MAL0_TXCASR, (MAL_TXRX_CASR >> (hw_p->devnum*2)));
Stefan Roese326c9712005-08-01 16:41:48 +02001459#else
Stefan Roese918010a2009-09-09 16:25:29 +02001460 mtdcr (MAL0_TXCASR, (MAL_TXRX_CASR >> hw_p->devnum));
Stefan Roese326c9712005-08-01 16:41:48 +02001461#endif
Stefan Roese918010a2009-09-09 16:25:29 +02001462 mtdcr (MAL0_RXCASR, (MAL_TXRX_CASR >> hw_p->devnum));
wdenk544e9732004-02-06 23:19:44 +00001463
1464 /* set transmit enable & receive enable */
Niklaus Giger728bd0a2009-10-04 20:04:20 +02001465 out_be32((void *)EMAC0_MR0 + hw_p->hw_addr, EMAC_MR0_TXE | EMAC_MR0_RXE);
wdenk544e9732004-02-06 23:19:44 +00001466
Niklaus Giger728bd0a2009-10-04 20:04:20 +02001467 mode_reg = in_be32((void *)EMAC0_MR1 + hw_p->hw_addr);
Stefan Roeseca5ef8c2008-03-01 12:11:40 +01001468
1469 /* set rx-/tx-fifo size */
1470 mode_reg = (mode_reg & ~EMAC_MR1_FIFO_MASK) | EMAC_MR1_FIFO_SIZE;
wdenk544e9732004-02-06 23:19:44 +00001471
1472 /* set speed */
Stefan Roese99644742005-11-29 18:18:21 +01001473 if (speed == _1000BASET) {
Stefan Roese95ca5fa2010-09-11 09:31:43 +02001474#if defined(CONFIG_440SP) || defined(CONFIG_440SPE)
Stefan Roese99644742005-11-29 18:18:21 +01001475 unsigned long pfc1;
Stefan Roese42fbddd2006-09-07 11:51:23 +02001476
Stefan Roese918010a2009-09-09 16:25:29 +02001477 mfsdr (SDR0_PFC1, pfc1);
Stefan Roese99644742005-11-29 18:18:21 +01001478 pfc1 |= SDR0_PFC1_EM_1000;
Stefan Roese918010a2009-09-09 16:25:29 +02001479 mtsdr (SDR0_PFC1, pfc1);
Stefan Roese99644742005-11-29 18:18:21 +01001480#endif
Niklaus Giger728bd0a2009-10-04 20:04:20 +02001481 mode_reg = mode_reg | EMAC_MR1_MF_1000MBPS | EMAC_MR1_IST;
Stefan Roese99644742005-11-29 18:18:21 +01001482 } else if (speed == _100BASET)
Niklaus Giger728bd0a2009-10-04 20:04:20 +02001483 mode_reg = mode_reg | EMAC_MR1_MF_100MBPS | EMAC_MR1_IST;
wdenk544e9732004-02-06 23:19:44 +00001484 else
1485 mode_reg = mode_reg & ~0x00C00000; /* 10 MBPS */
1486 if (duplex == FULL)
Niklaus Giger728bd0a2009-10-04 20:04:20 +02001487 mode_reg = mode_reg | 0x80000000 | EMAC_MR1_IST;
wdenk544e9732004-02-06 23:19:44 +00001488
Niklaus Giger728bd0a2009-10-04 20:04:20 +02001489 out_be32((void *)EMAC0_MR1 + hw_p->hw_addr, mode_reg);
wdenk544e9732004-02-06 23:19:44 +00001490
1491 /* Enable broadcast and indvidual address */
1492 /* TBS: enabling runts as some misbehaved nics will send runts */
Niklaus Giger728bd0a2009-10-04 20:04:20 +02001493 out_be32((void *)EMAC0_RXM + hw_p->hw_addr, EMAC_RMR_BAE | EMAC_RMR_IAE);
wdenk544e9732004-02-06 23:19:44 +00001494
1495 /* we probably need to set the tx mode1 reg? maybe at tx time */
1496
1497 /* set transmit request threshold register */
Niklaus Giger728bd0a2009-10-04 20:04:20 +02001498 out_be32((void *)EMAC0_TRTR + hw_p->hw_addr, 0x18000000); /* 256 byte threshold */
wdenk544e9732004-02-06 23:19:44 +00001499
Wolfgang Denk0cbaf642005-09-25 00:53:22 +02001500 /* set receive low/high water mark register */
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001501#if defined(CONFIG_440)
Marian Balakowicz49d0eee2006-06-30 16:30:46 +02001502 /* 440s has a 64 byte burst length */
Niklaus Giger728bd0a2009-10-04 20:04:20 +02001503 out_be32((void *)EMAC0_RX_HI_LO_WMARK + hw_p->hw_addr, 0x80009000);
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001504#else
1505 /* 405s have a 16 byte burst length */
Niklaus Giger728bd0a2009-10-04 20:04:20 +02001506 out_be32((void *)EMAC0_RX_HI_LO_WMARK + hw_p->hw_addr, 0x0f002000);
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001507#endif /* defined(CONFIG_440) */
Niklaus Giger728bd0a2009-10-04 20:04:20 +02001508 out_be32((void *)EMAC0_TMR1 + hw_p->hw_addr, 0xf8640000);
wdenk544e9732004-02-06 23:19:44 +00001509
1510 /* Set fifo limit entry in tx mode 0 */
Niklaus Giger728bd0a2009-10-04 20:04:20 +02001511 out_be32((void *)EMAC0_TMR0 + hw_p->hw_addr, 0x00000003);
wdenk544e9732004-02-06 23:19:44 +00001512 /* Frame gap set */
Niklaus Giger728bd0a2009-10-04 20:04:20 +02001513 out_be32((void *)EMAC0_I_FRAME_GAP_REG + hw_p->hw_addr, 0x00000008);
wdenk544e9732004-02-06 23:19:44 +00001514
1515 /* Set EMAC IER */
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001516 hw_p->emac_ier = EMAC_ISR_PTLE | EMAC_ISR_BFCS | EMAC_ISR_ORE | EMAC_ISR_IRE;
wdenk544e9732004-02-06 23:19:44 +00001517 if (speed == _100BASET)
1518 hw_p->emac_ier = hw_p->emac_ier | EMAC_ISR_SYE;
1519
Niklaus Giger728bd0a2009-10-04 20:04:20 +02001520 out_be32((void *)EMAC0_ISR + hw_p->hw_addr, 0xffffffff); /* clear pending interrupts */
1521 out_be32((void *)EMAC0_IER + hw_p->hw_addr, hw_p->emac_ier);
wdenk544e9732004-02-06 23:19:44 +00001522
1523 if (hw_p->first_init == 0) {
1524 /*
1525 * Connect interrupt service routines
1526 */
Stefan Roese153b3e22007-10-05 17:10:59 +02001527 irq_install_handler(ETH_IRQ_NUM(hw_p->devnum),
1528 (interrupt_handler_t *) enetInt, dev);
wdenk544e9732004-02-06 23:19:44 +00001529 }
wdenk544e9732004-02-06 23:19:44 +00001530
1531 mtmsr (msr); /* enable interrupts again */
1532
1533 hw_p->bis = bis;
1534 hw_p->first_init = 1;
1535
Stefan Roese8111a0e2008-01-08 18:39:30 +01001536 return 0;
wdenk544e9732004-02-06 23:19:44 +00001537}
1538
1539
Anatolij Gustschindf2893b2012-05-21 10:48:18 +00001540static int ppc_4xx_eth_send(struct eth_device *dev, void *ptr, int len)
wdenk544e9732004-02-06 23:19:44 +00001541{
1542 struct enet_frame *ef_ptr;
1543 ulong time_start, time_now;
1544 unsigned long temp_txm0;
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001545 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenk544e9732004-02-06 23:19:44 +00001546
1547 ef_ptr = (struct enet_frame *) ptr;
1548
1549 /*-----------------------------------------------------------------------+
1550 * Copy in our address into the frame.
1551 *-----------------------------------------------------------------------*/
1552 (void) memcpy (ef_ptr->source_addr, dev->enetaddr, ENET_ADDR_LENGTH);
1553
1554 /*-----------------------------------------------------------------------+
1555 * If frame is too long or too short, modify length.
1556 *-----------------------------------------------------------------------*/
1557 /* TBS: where does the fragment go???? */
1558 if (len > ENET_MAX_MTU)
1559 len = ENET_MAX_MTU;
1560
1561 /* memcpy ((void *) &tx_buff[tx_slot], (const void *) ptr, len); */
1562 memcpy ((void *) hw_p->txbuf_ptr, (const void *) ptr, len);
Matthias Fuchs211105a2007-12-14 11:19:56 +01001563 flush_dcache_range((u32)hw_p->txbuf_ptr, (u32)hw_p->txbuf_ptr + len);
wdenk544e9732004-02-06 23:19:44 +00001564
1565 /*-----------------------------------------------------------------------+
1566 * set TX Buffer busy, and send it
1567 *-----------------------------------------------------------------------*/
1568 hw_p->tx[hw_p->tx_slot].ctrl = (MAL_TX_CTRL_LAST |
1569 EMAC_TX_CTRL_GFCS | EMAC_TX_CTRL_GP) &
1570 ~(EMAC_TX_CTRL_ISA | EMAC_TX_CTRL_RSA);
1571 if ((NUM_TX_BUFF - 1) == hw_p->tx_slot)
1572 hw_p->tx[hw_p->tx_slot].ctrl |= MAL_TX_CTRL_WRAP;
1573
1574 hw_p->tx[hw_p->tx_slot].data_len = (short) len;
1575 hw_p->tx[hw_p->tx_slot].ctrl |= MAL_TX_CTRL_READY;
1576
Stefan Roesebdd13d12008-03-11 15:05:26 +01001577 sync();
wdenk544e9732004-02-06 23:19:44 +00001578
Niklaus Giger728bd0a2009-10-04 20:04:20 +02001579 out_be32((void *)EMAC0_TMR0 + hw_p->hw_addr,
1580 in_be32((void *)EMAC0_TMR0 + hw_p->hw_addr) | EMAC_TMR0_GNP0);
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001581#ifdef INFO_4XX_ENET
wdenk544e9732004-02-06 23:19:44 +00001582 hw_p->stats.pkts_tx++;
1583#endif
1584
1585 /*-----------------------------------------------------------------------+
1586 * poll unitl the packet is sent and then make sure it is OK
1587 *-----------------------------------------------------------------------*/
1588 time_start = get_timer (0);
1589 while (1) {
Niklaus Giger728bd0a2009-10-04 20:04:20 +02001590 temp_txm0 = in_be32((void *)EMAC0_TMR0 + hw_p->hw_addr);
wdenk544e9732004-02-06 23:19:44 +00001591 /* loop until either TINT turns on or 3 seconds elapse */
Niklaus Giger728bd0a2009-10-04 20:04:20 +02001592 if ((temp_txm0 & EMAC_TMR0_GNP0) != 0) {
wdenk544e9732004-02-06 23:19:44 +00001593 /* transmit is done, so now check for errors
1594 * If there is an error, an interrupt should
1595 * happen when we return
1596 */
1597 time_now = get_timer (0);
1598 if ((time_now - time_start) > 3000) {
1599 return (-1);
1600 }
1601 } else {
1602 return (len);
1603 }
1604 }
1605}
1606
wdenk544e9732004-02-06 23:19:44 +00001607int enetInt (struct eth_device *dev)
1608{
1609 int serviced;
1610 int rc = -1; /* default to not us */
Stefan Roese01edcea2008-06-26 13:40:57 +02001611 u32 mal_isr;
1612 u32 emac_isr = 0;
1613 u32 mal_eob;
1614 u32 uic_mal;
1615 u32 uic_mal_err;
1616 u32 uic_emac;
1617 u32 uic_emac_b;
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001618 EMAC_4XX_HW_PST hw_p;
wdenk544e9732004-02-06 23:19:44 +00001619
1620 /*
1621 * Because the mal is generic, we need to get the current
1622 * eth device
1623 */
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001624 dev = eth_get_dev();
wdenk544e9732004-02-06 23:19:44 +00001625
1626 hw_p = dev->priv;
1627
wdenk544e9732004-02-06 23:19:44 +00001628 /* enter loop that stays in interrupt code until nothing to service */
1629 do {
1630 serviced = 0;
1631
Stefan Roese01edcea2008-06-26 13:40:57 +02001632 uic_mal = mfdcr(UIC_BASE_MAL + UIC_MSR);
1633 uic_mal_err = mfdcr(UIC_BASE_MAL_ERR + UIC_MSR);
1634 uic_emac = mfdcr(UIC_BASE_EMAC + UIC_MSR);
1635 uic_emac_b = mfdcr(UIC_BASE_EMAC_B + UIC_MSR);
Stefan Roese42fbddd2006-09-07 11:51:23 +02001636
Stefan Roese01edcea2008-06-26 13:40:57 +02001637 if (!(uic_mal & (UIC_MAL_RXEOB | UIC_MAL_TXEOB))
1638 && !(uic_mal_err & (UIC_MAL_SERR | UIC_MAL_TXDE | UIC_MAL_RXDE))
1639 && !(uic_emac & UIC_ETHx) && !(uic_emac_b & UIC_ETHxB)) {
wdenk544e9732004-02-06 23:19:44 +00001640 /* not for us */
1641 return (rc);
1642 }
Stefan Roese01edcea2008-06-26 13:40:57 +02001643
wdenk544e9732004-02-06 23:19:44 +00001644 /* get and clear controller status interrupts */
Stefan Roese01edcea2008-06-26 13:40:57 +02001645 /* look at MAL and EMAC error interrupts */
1646 if (uic_mal_err & (UIC_MAL_SERR | UIC_MAL_TXDE | UIC_MAL_RXDE)) {
1647 /* we have a MAL error interrupt */
Stefan Roese918010a2009-09-09 16:25:29 +02001648 mal_isr = mfdcr(MAL0_ESR);
Stefan Roese01edcea2008-06-26 13:40:57 +02001649 mal_err(dev, mal_isr, uic_mal_err,
1650 MAL_UIC_DEF, MAL_UIC_ERR);
wdenk544e9732004-02-06 23:19:44 +00001651
Stefan Roese01edcea2008-06-26 13:40:57 +02001652 /* clear MAL error interrupt status bits */
1653 mtdcr(UIC_BASE_MAL_ERR + UIC_SR,
1654 UIC_MAL_SERR | UIC_MAL_TXDE | UIC_MAL_RXDE);
wdenk544e9732004-02-06 23:19:44 +00001655
Stefan Roese01edcea2008-06-26 13:40:57 +02001656 return -1;
wdenk544e9732004-02-06 23:19:44 +00001657 }
1658
Stefan Roese01edcea2008-06-26 13:40:57 +02001659 /* look for EMAC errors */
1660 if ((uic_emac & UIC_ETHx) || (uic_emac_b & UIC_ETHxB)) {
Niklaus Giger728bd0a2009-10-04 20:04:20 +02001661 emac_isr = in_be32((void *)EMAC0_ISR + hw_p->hw_addr);
Stefan Roese01edcea2008-06-26 13:40:57 +02001662 emac_err(dev, emac_isr);
Stefan Roese99644742005-11-29 18:18:21 +01001663
Stefan Roese01edcea2008-06-26 13:40:57 +02001664 /* clear EMAC error interrupt status bits */
1665 mtdcr(UIC_BASE_EMAC + UIC_SR, UIC_ETHx);
1666 mtdcr(UIC_BASE_EMAC_B + UIC_SR, UIC_ETHxB);
Stefan Roese99644742005-11-29 18:18:21 +01001667
Stefan Roese01edcea2008-06-26 13:40:57 +02001668 return -1;
wdenk544e9732004-02-06 23:19:44 +00001669 }
wdenk544e9732004-02-06 23:19:44 +00001670
Stefan Roese01edcea2008-06-26 13:40:57 +02001671 /* handle MAX TX EOB interrupt from a tx */
1672 if (uic_mal & UIC_MAL_TXEOB) {
1673 /* clear MAL interrupt status bits */
Stefan Roese918010a2009-09-09 16:25:29 +02001674 mal_eob = mfdcr(MAL0_TXEOBISR);
1675 mtdcr(MAL0_TXEOBISR, mal_eob);
Stefan Roese01edcea2008-06-26 13:40:57 +02001676 mtdcr(UIC_BASE_MAL + UIC_SR, UIC_MAL_TXEOB);
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001677
Stefan Roese01edcea2008-06-26 13:40:57 +02001678 /* indicate that we serviced an interrupt */
1679 serviced = 1;
1680 rc = 0;
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001681 }
1682
Mike Williamsbf895ad2011-07-22 04:01:30 +00001683 /* handle MAL RX EOB interrupt from a receive */
Stefan Roese01edcea2008-06-26 13:40:57 +02001684 /* check for EOB on valid channels */
1685 if (uic_mal & UIC_MAL_RXEOB) {
Stefan Roese918010a2009-09-09 16:25:29 +02001686 mal_eob = mfdcr(MAL0_RXEOBISR);
Stefan Roese01edcea2008-06-26 13:40:57 +02001687 if (mal_eob &
1688 (0x80000000 >> (hw_p->devnum * MAL_RX_CHAN_MUL))) {
1689 /* push packet to upper layer */
1690 enet_rcv(dev, emac_isr);
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001691
Stefan Roese01edcea2008-06-26 13:40:57 +02001692 /* clear MAL interrupt status bits */
1693 mtdcr(UIC_BASE_MAL + UIC_SR, UIC_MAL_RXEOB);
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001694
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001695 /* indicate that we serviced an interrupt */
1696 serviced = 1;
1697 rc = 0;
1698 }
1699 }
James Cloughee86aff2009-09-10 09:11:50 +02001700#if defined(CONFIG_405EZ)
1701 /*
1702 * On 405EZ the RX-/TX-interrupts are coalesced into
1703 * one IRQ bit in the UIC. We need to acknowledge the
1704 * RX-/TX-interrupts in the SDR0_ICINTSTAT reg as well.
1705 */
1706 mtsdr(SDR0_ICINTSTAT,
1707 SDR_ICRX_STAT | SDR_ICTX0_STAT | SDR_ICTX1_STAT);
1708#endif /* defined(CONFIG_405EZ) */
Stefan Roese01edcea2008-06-26 13:40:57 +02001709 } while (serviced);
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001710
1711 return (rc);
1712}
1713
wdenk544e9732004-02-06 23:19:44 +00001714/*-----------------------------------------------------------------------------+
1715 * MAL Error Routine
1716 *-----------------------------------------------------------------------------*/
1717static void mal_err (struct eth_device *dev, unsigned long isr,
1718 unsigned long uic, unsigned long maldef,
1719 unsigned long mal_errr)
1720{
Stefan Roese918010a2009-09-09 16:25:29 +02001721 mtdcr (MAL0_ESR, isr); /* clear interrupt */
wdenk544e9732004-02-06 23:19:44 +00001722
1723 /* clear DE interrupt */
Stefan Roese918010a2009-09-09 16:25:29 +02001724 mtdcr (MAL0_TXDEIR, 0xC0000000);
1725 mtdcr (MAL0_RXDEIR, 0x80000000);
wdenk544e9732004-02-06 23:19:44 +00001726
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001727#ifdef INFO_4XX_ENET
Vagrant Cascadianedfdb992016-04-30 19:18:00 -07001728 printf("\nMAL error occurred.... ISR = %lx UIC = = %lx MAL_DEF = %lx MAL_ERR= %lx\n",
Joe Hershberger3dbe17e2015-03-22 17:09:06 -05001729 isr, uic, maldef, mal_errr);
wdenk544e9732004-02-06 23:19:44 +00001730#endif
1731
Joe Hershberger3dbe17e2015-03-22 17:09:06 -05001732 eth_init(); /* start again... */
wdenk544e9732004-02-06 23:19:44 +00001733}
1734
1735/*-----------------------------------------------------------------------------+
1736 * EMAC Error Routine
1737 *-----------------------------------------------------------------------------*/
1738static void emac_err (struct eth_device *dev, unsigned long isr)
1739{
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001740 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenk544e9732004-02-06 23:19:44 +00001741
Vagrant Cascadianedfdb992016-04-30 19:18:00 -07001742 printf ("EMAC%d error occurred.... ISR = %lx\n", hw_p->devnum, isr);
Niklaus Giger728bd0a2009-10-04 20:04:20 +02001743 out_be32((void *)EMAC0_ISR + hw_p->hw_addr, isr);
wdenk544e9732004-02-06 23:19:44 +00001744}
1745
1746/*-----------------------------------------------------------------------------+
1747 * enet_rcv() handles the ethernet receive data
1748 *-----------------------------------------------------------------------------*/
1749static void enet_rcv (struct eth_device *dev, unsigned long malisr)
1750{
wdenk544e9732004-02-06 23:19:44 +00001751 unsigned long data_len;
1752 unsigned long rx_eob_isr;
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001753 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenk544e9732004-02-06 23:19:44 +00001754
1755 int handled = 0;
1756 int i;
1757 int loop_count = 0;
1758
Stefan Roese918010a2009-09-09 16:25:29 +02001759 rx_eob_isr = mfdcr (MAL0_RXEOBISR);
Stefan Roesebdd13d12008-03-11 15:05:26 +01001760 if ((0x80000000 >> (hw_p->devnum * MAL_RX_CHAN_MUL)) & rx_eob_isr) {
wdenk544e9732004-02-06 23:19:44 +00001761 /* clear EOB */
Stefan Roese918010a2009-09-09 16:25:29 +02001762 mtdcr (MAL0_RXEOBISR, rx_eob_isr);
wdenk544e9732004-02-06 23:19:44 +00001763
1764 /* EMAC RX done */
1765 while (1) { /* do all */
1766 i = hw_p->rx_slot;
1767
1768 if ((MAL_RX_CTRL_EMPTY & hw_p->rx[i].ctrl)
1769 || (loop_count >= NUM_RX_BUFF))
1770 break;
Stefan Roese09feb382007-07-12 16:32:08 +02001771
wdenk544e9732004-02-06 23:19:44 +00001772 loop_count++;
wdenk544e9732004-02-06 23:19:44 +00001773 handled++;
Stefan Roesebdd13d12008-03-11 15:05:26 +01001774 data_len = (unsigned long) hw_p->rx[i].data_len & 0x0fff; /* Get len */
wdenk544e9732004-02-06 23:19:44 +00001775 if (data_len) {
1776 if (data_len > ENET_MAX_MTU) /* Check len */
1777 data_len = 0;
1778 else {
1779 if (EMAC_RX_ERRORS & hw_p->rx[i].ctrl) { /* Check Errors */
1780 data_len = 0;
1781 hw_p->stats.rx_err_log[hw_p->
1782 rx_err_index]
1783 = hw_p->rx[i].ctrl;
1784 hw_p->rx_err_index++;
1785 if (hw_p->rx_err_index ==
1786 MAX_ERR_LOG)
1787 hw_p->rx_err_index =
1788 0;
wdenk7ad5e4c2004-04-25 15:41:35 +00001789 } /* emac_erros */
wdenk544e9732004-02-06 23:19:44 +00001790 } /* data_len < max mtu */
wdenk7ad5e4c2004-04-25 15:41:35 +00001791 } /* if data_len */
wdenk544e9732004-02-06 23:19:44 +00001792 if (!data_len) { /* no data */
1793 hw_p->rx[i].ctrl |= MAL_RX_CTRL_EMPTY; /* Free Recv Buffer */
1794
1795 hw_p->stats.data_len_err++; /* Error at Rx */
1796 }
1797
1798 /* !data_len */
1799 /* AS.HARNOIS */
1800 /* Check if user has already eaten buffer */
1801 /* if not => ERROR */
1802 else if (hw_p->rx_ready[hw_p->rx_i_index] != -1) {
1803 if (hw_p->is_receiving)
1804 printf ("ERROR : Receive buffers are full!\n");
1805 break;
1806 } else {
1807 hw_p->stats.rx_frames++;
1808 hw_p->stats.rx += data_len;
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001809#ifdef INFO_4XX_ENET
wdenk544e9732004-02-06 23:19:44 +00001810 hw_p->stats.pkts_rx++;
1811#endif
1812 /* AS.HARNOIS
1813 * use ring buffer
1814 */
1815 hw_p->rx_ready[hw_p->rx_i_index] = i;
1816 hw_p->rx_i_index++;
1817 if (NUM_RX_BUFF == hw_p->rx_i_index)
1818 hw_p->rx_i_index = 0;
1819
Stefan Roese09feb382007-07-12 16:32:08 +02001820 hw_p->rx_slot++;
1821 if (NUM_RX_BUFF == hw_p->rx_slot)
1822 hw_p->rx_slot = 0;
1823
wdenk544e9732004-02-06 23:19:44 +00001824 /* AS.HARNOIS
1825 * free receive buffer only when
1826 * buffer has been handled (eth_rx)
1827 rx[i].ctrl |= MAL_RX_CTRL_EMPTY;
1828 */
1829 } /* if data_len */
1830 } /* while */
1831 } /* if EMACK_RXCHL */
1832}
1833
1834
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001835static int ppc_4xx_eth_rx (struct eth_device *dev)
wdenk544e9732004-02-06 23:19:44 +00001836{
1837 int length;
1838 int user_index;
1839 unsigned long msr;
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001840 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenk544e9732004-02-06 23:19:44 +00001841
Wolfgang Denk0cbaf642005-09-25 00:53:22 +02001842 hw_p->is_receiving = 1; /* tell driver */
wdenk544e9732004-02-06 23:19:44 +00001843
1844 for (;;) {
1845 /* AS.HARNOIS
1846 * use ring buffer and
1847 * get index from rx buffer desciptor queue
1848 */
1849 user_index = hw_p->rx_ready[hw_p->rx_u_index];
1850 if (user_index == -1) {
1851 length = -1;
1852 break; /* nothing received - leave for() loop */
1853 }
1854
1855 msr = mfmsr ();
1856 mtmsr (msr & ~(MSR_EE));
1857
Stefan Roesebdd13d12008-03-11 15:05:26 +01001858 length = hw_p->rx[user_index].data_len & 0x0fff;
wdenk544e9732004-02-06 23:19:44 +00001859
Joe Hershberger9f09a362015-04-08 01:41:06 -05001860 /*
1861 * Pass the packet up to the protocol layers.
1862 * net_process_received_packet(net_rx_packets[rxIdx],
1863 * length - 4);
1864 * net_process_received_packet(net_rx_packets[i], length);
1865 */
Stefan Roese9c2a6472007-10-31 18:01:24 +01001866 invalidate_dcache_range((u32)hw_p->rx[user_index].data_ptr,
1867 (u32)hw_p->rx[user_index].data_ptr +
Matthias Fuchs211105a2007-12-14 11:19:56 +01001868 length - 4);
Joe Hershberger9f09a362015-04-08 01:41:06 -05001869 net_process_received_packet(net_rx_packets[user_index],
1870 length - 4);
wdenk544e9732004-02-06 23:19:44 +00001871 /* Free Recv Buffer */
1872 hw_p->rx[user_index].ctrl |= MAL_RX_CTRL_EMPTY;
1873 /* Free rx buffer descriptor queue */
1874 hw_p->rx_ready[hw_p->rx_u_index] = -1;
1875 hw_p->rx_u_index++;
1876 if (NUM_RX_BUFF == hw_p->rx_u_index)
1877 hw_p->rx_u_index = 0;
1878
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001879#ifdef INFO_4XX_ENET
wdenk544e9732004-02-06 23:19:44 +00001880 hw_p->stats.pkts_handled++;
1881#endif
1882
1883 mtmsr (msr); /* Enable IRQ's */
1884 }
1885
Wolfgang Denk0cbaf642005-09-25 00:53:22 +02001886 hw_p->is_receiving = 0; /* tell driver */
wdenk544e9732004-02-06 23:19:44 +00001887
1888 return length;
1889}
1890
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001891int ppc_4xx_eth_initialize (bd_t * bis)
wdenk544e9732004-02-06 23:19:44 +00001892{
1893 static int virgin = 0;
wdenk544e9732004-02-06 23:19:44 +00001894 struct eth_device *dev;
1895 int eth_num = 0;
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001896 EMAC_4XX_HW_PST hw = NULL;
Stefan Roese8d982302007-01-18 10:25:34 +01001897 u8 ethaddr[4 + CONFIG_EMAC_NR_START][6];
1898 u32 hw_addr[4];
Stefan Roese01edcea2008-06-26 13:40:57 +02001899 u32 mal_ier;
wdenk544e9732004-02-06 23:19:44 +00001900
Stefan Roeseb30f2a12005-08-08 12:42:22 +02001901#if defined(CONFIG_440GX)
Stefan Roese326c9712005-08-01 16:41:48 +02001902 unsigned long pfc1;
1903
Stefan Roese918010a2009-09-09 16:25:29 +02001904 mfsdr (SDR0_PFC1, pfc1);
wdenk544e9732004-02-06 23:19:44 +00001905 pfc1 &= ~(0x01e00000);
1906 pfc1 |= 0x01200000;
Stefan Roese918010a2009-09-09 16:25:29 +02001907 mtsdr (SDR0_PFC1, pfc1);
Stefan Roese326c9712005-08-01 16:41:48 +02001908#endif
Marian Balakowicz49d0eee2006-06-30 16:30:46 +02001909
Stefan Roese8d982302007-01-18 10:25:34 +01001910 /* first clear all mac-addresses */
1911 for (eth_num = 0; eth_num < LAST_EMAC_NUM; eth_num++)
1912 memcpy(ethaddr[eth_num], "\0\0\0\0\0\0", 6);
wdenk97e8bda2004-09-29 22:43:59 +00001913
Stefan Roese7f98aec2005-10-20 16:34:28 +02001914 for (eth_num = 0; eth_num < LAST_EMAC_NUM; eth_num++) {
Mike Frysingerb2039652009-02-11 19:01:26 -05001915 int ethaddr_idx = eth_num + CONFIG_EMAC_NR_START;
wdenk544e9732004-02-06 23:19:44 +00001916 switch (eth_num) {
wdenk54070ab2004-12-31 09:32:47 +00001917 default: /* fall through */
wdenk544e9732004-02-06 23:19:44 +00001918 case 0:
Mike Frysingerb2039652009-02-11 19:01:26 -05001919 eth_getenv_enetaddr("ethaddr", ethaddr[ethaddr_idx]);
Stefan Roese8d982302007-01-18 10:25:34 +01001920 hw_addr[eth_num] = 0x0;
wdenk544e9732004-02-06 23:19:44 +00001921 break;
wdenk54070ab2004-12-31 09:32:47 +00001922#ifdef CONFIG_HAS_ETH1
wdenk544e9732004-02-06 23:19:44 +00001923 case 1:
Mike Frysingerb2039652009-02-11 19:01:26 -05001924 eth_getenv_enetaddr("eth1addr", ethaddr[ethaddr_idx]);
Stefan Roese8d982302007-01-18 10:25:34 +01001925 hw_addr[eth_num] = 0x100;
wdenk544e9732004-02-06 23:19:44 +00001926 break;
wdenk54070ab2004-12-31 09:32:47 +00001927#endif
1928#ifdef CONFIG_HAS_ETH2
wdenk544e9732004-02-06 23:19:44 +00001929 case 2:
Mike Frysingerb2039652009-02-11 19:01:26 -05001930 eth_getenv_enetaddr("eth2addr", ethaddr[ethaddr_idx]);
Stefan Roese52df4192008-03-19 16:20:49 +01001931#if defined(CONFIG_460GT)
1932 hw_addr[eth_num] = 0x300;
1933#else
Stefan Roese8d982302007-01-18 10:25:34 +01001934 hw_addr[eth_num] = 0x400;
Stefan Roese52df4192008-03-19 16:20:49 +01001935#endif
wdenk544e9732004-02-06 23:19:44 +00001936 break;
wdenk54070ab2004-12-31 09:32:47 +00001937#endif
1938#ifdef CONFIG_HAS_ETH3
wdenk544e9732004-02-06 23:19:44 +00001939 case 3:
Mike Frysingerb2039652009-02-11 19:01:26 -05001940 eth_getenv_enetaddr("eth3addr", ethaddr[ethaddr_idx]);
Stefan Roese52df4192008-03-19 16:20:49 +01001941#if defined(CONFIG_460GT)
1942 hw_addr[eth_num] = 0x400;
1943#else
Stefan Roese8d982302007-01-18 10:25:34 +01001944 hw_addr[eth_num] = 0x600;
Stefan Roese52df4192008-03-19 16:20:49 +01001945#endif
wdenk544e9732004-02-06 23:19:44 +00001946 break;
wdenk54070ab2004-12-31 09:32:47 +00001947#endif
wdenk544e9732004-02-06 23:19:44 +00001948 }
Stefan Roese8d982302007-01-18 10:25:34 +01001949 }
1950
1951 /* set phy num and mode */
1952 bis->bi_phynum[0] = CONFIG_PHY_ADDR;
1953 bis->bi_phymode[0] = 0;
1954
1955#if defined(CONFIG_PHY1_ADDR)
1956 bis->bi_phynum[1] = CONFIG_PHY1_ADDR;
1957 bis->bi_phymode[1] = 0;
1958#endif
1959#if defined(CONFIG_440GX)
1960 bis->bi_phynum[2] = CONFIG_PHY2_ADDR;
1961 bis->bi_phynum[3] = CONFIG_PHY3_ADDR;
1962 bis->bi_phymode[2] = 2;
1963 bis->bi_phymode[3] = 2;
Stefan Roese153b3e22007-10-05 17:10:59 +02001964#endif
Stefan Roese8d982302007-01-18 10:25:34 +01001965
Stefan Roese153b3e22007-10-05 17:10:59 +02001966#if defined(CONFIG_440GX) || \
1967 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
1968 defined(CONFIG_405EX)
Stefan Roese8d982302007-01-18 10:25:34 +01001969 ppc_4xx_eth_setup_bridge(0, bis);
1970#endif
1971
1972 for (eth_num = 0; eth_num < LAST_EMAC_NUM; eth_num++) {
1973 /*
1974 * See if we can actually bring up the interface,
1975 * otherwise, skip it
1976 */
1977 if (memcmp (ethaddr[eth_num], "\0\0\0\0\0\0", 6) == 0) {
1978 bis->bi_phymode[eth_num] = BI_PHYMODE_NONE;
1979 continue;
1980 }
wdenk544e9732004-02-06 23:19:44 +00001981
1982 /* Allocate device structure */
1983 dev = (struct eth_device *) malloc (sizeof (*dev));
1984 if (dev == NULL) {
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001985 printf ("ppc_4xx_eth_initialize: "
wdenkef893942004-02-23 16:11:30 +00001986 "Cannot allocate eth_device %d\n", eth_num);
wdenk544e9732004-02-06 23:19:44 +00001987 return (-1);
1988 }
wdenkd1894de2005-06-20 10:17:34 +00001989 memset(dev, 0, sizeof(*dev));
wdenk544e9732004-02-06 23:19:44 +00001990
1991 /* Allocate our private use data */
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001992 hw = (EMAC_4XX_HW_PST) malloc (sizeof (*hw));
wdenk544e9732004-02-06 23:19:44 +00001993 if (hw == NULL) {
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001994 printf ("ppc_4xx_eth_initialize: "
wdenkef893942004-02-23 16:11:30 +00001995 "Cannot allocate private hw data for eth_device %d",
wdenk544e9732004-02-06 23:19:44 +00001996 eth_num);
1997 free (dev);
1998 return (-1);
1999 }
wdenkd1894de2005-06-20 10:17:34 +00002000 memset(hw, 0, sizeof(*hw));
wdenk544e9732004-02-06 23:19:44 +00002001
Stefan Roese8d982302007-01-18 10:25:34 +01002002 hw->hw_addr = hw_addr[eth_num];
2003 memcpy (dev->enetaddr, ethaddr[eth_num], 6);
wdenk544e9732004-02-06 23:19:44 +00002004 hw->devnum = eth_num;
Stefan Roese326c9712005-08-01 16:41:48 +02002005 hw->print_speed = 1;
wdenk544e9732004-02-06 23:19:44 +00002006
Stefan Roese8d982302007-01-18 10:25:34 +01002007 sprintf (dev->name, "ppc_4xx_eth%d", eth_num - CONFIG_EMAC_NR_START);
wdenk544e9732004-02-06 23:19:44 +00002008 dev->priv = (void *) hw;
Stefan Roese0c7ffc02005-08-16 18:18:00 +02002009 dev->init = ppc_4xx_eth_init;
2010 dev->halt = ppc_4xx_eth_halt;
2011 dev->send = ppc_4xx_eth_send;
2012 dev->recv = ppc_4xx_eth_rx;
wdenk544e9732004-02-06 23:19:44 +00002013
Stefan Roese747061c2011-07-12 13:26:47 +02002014 eth_register(dev);
2015
2016#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Joe Hershberger1fbcbed2016-08-08 11:28:38 -05002017 int retval;
2018 struct mii_dev *mdiodev = mdio_alloc();
2019 if (!mdiodev)
2020 return -ENOMEM;
2021 strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
2022 mdiodev->read = emac4xx_miiphy_read;
2023 mdiodev->write = emac4xx_miiphy_write;
2024
2025 retval = mdio_register(mdiodev);
2026 if (retval < 0)
2027 return retval;
Stefan Roese747061c2011-07-12 13:26:47 +02002028#endif
2029
wdenk544e9732004-02-06 23:19:44 +00002030 if (0 == virgin) {
2031 /* set the MAL IER ??? names may change with new spec ??? */
Stefan Roese153b3e22007-10-05 17:10:59 +02002032#if defined(CONFIG_440SPE) || \
2033 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roesebdd13d12008-03-11 15:05:26 +01002034 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roese153b3e22007-10-05 17:10:59 +02002035 defined(CONFIG_405EX)
Marian Balakowicz49d0eee2006-06-30 16:30:46 +02002036 mal_ier =
2037 MAL_IER_PT | MAL_IER_PRE | MAL_IER_PWE |
2038 MAL_IER_DE | MAL_IER_OTE | MAL_IER_OE | MAL_IER_PE ;
2039#else
wdenk544e9732004-02-06 23:19:44 +00002040 mal_ier =
2041 MAL_IER_DE | MAL_IER_NE | MAL_IER_TE |
2042 MAL_IER_OPBE | MAL_IER_PLBE;
Marian Balakowicz49d0eee2006-06-30 16:30:46 +02002043#endif
Stefan Roese918010a2009-09-09 16:25:29 +02002044 mtdcr (MAL0_ESR, 0xffffffff); /* clear pending interrupts */
2045 mtdcr (MAL0_TXDEIR, 0xffffffff); /* clear pending interrupts */
2046 mtdcr (MAL0_RXDEIR, 0xffffffff); /* clear pending interrupts */
2047 mtdcr (MAL0_IER, mal_ier);
wdenk544e9732004-02-06 23:19:44 +00002048
2049 /* install MAL interrupt handler */
Stefan Roese01edcea2008-06-26 13:40:57 +02002050 irq_install_handler (VECNUM_MAL_SERR,
wdenk544e9732004-02-06 23:19:44 +00002051 (interrupt_handler_t *) enetInt,
2052 dev);
Stefan Roese01edcea2008-06-26 13:40:57 +02002053 irq_install_handler (VECNUM_MAL_TXEOB,
wdenk544e9732004-02-06 23:19:44 +00002054 (interrupt_handler_t *) enetInt,
2055 dev);
Stefan Roese01edcea2008-06-26 13:40:57 +02002056 irq_install_handler (VECNUM_MAL_RXEOB,
wdenk544e9732004-02-06 23:19:44 +00002057 (interrupt_handler_t *) enetInt,
2058 dev);
Stefan Roese01edcea2008-06-26 13:40:57 +02002059 irq_install_handler (VECNUM_MAL_TXDE,
wdenk544e9732004-02-06 23:19:44 +00002060 (interrupt_handler_t *) enetInt,
2061 dev);
Stefan Roese01edcea2008-06-26 13:40:57 +02002062 irq_install_handler (VECNUM_MAL_RXDE,
wdenk544e9732004-02-06 23:19:44 +00002063 (interrupt_handler_t *) enetInt,
2064 dev);
2065 virgin = 1;
2066 }
wdenk544e9732004-02-06 23:19:44 +00002067 } /* end for each supported device */
Stefan Roese8111a0e2008-01-08 18:39:30 +01002068
2069 return 0;
wdenk544e9732004-02-06 23:19:44 +00002070}