blob: 47d8abc1775e68740f0e9591cf705b1d34f38138 [file] [log] [blame]
wdenkba56f622004-02-06 23:19:44 +00001/*-----------------------------------------------------------------------------+
2 *
Wolfgang Denk265817c2005-09-25 00:53:22 +02003 * This source code has been made available to you by IBM on an AS-IS
4 * basis. Anyone receiving this source is licensed under IBM
5 * copyrights to use it in any way he or she deems fit, including
6 * copying it, modifying it, compiling it, and redistributing it either
7 * with or without modifications. No license under IBM patents or
8 * patent applications is to be implied by the copyright license.
wdenkba56f622004-02-06 23:19:44 +00009 *
Wolfgang Denk265817c2005-09-25 00:53:22 +020010 * Any user of this software should understand that IBM cannot provide
11 * technical support for this software and will not be responsible for
12 * any consequences resulting from the use of this software.
wdenkba56f622004-02-06 23:19:44 +000013 *
Wolfgang Denk265817c2005-09-25 00:53:22 +020014 * Any person who transfers this source code or any derivative work
15 * must include the IBM copyright notice, this paragraph, and the
16 * preceding two paragraphs in the transferred software.
wdenkba56f622004-02-06 23:19:44 +000017 *
Wolfgang Denk265817c2005-09-25 00:53:22 +020018 * COPYRIGHT I B M CORPORATION 1995
19 * LICENSED MATERIAL - PROGRAM PROPERTY OF I B M
wdenkba56f622004-02-06 23:19:44 +000020 *-----------------------------------------------------------------------------*/
21/*-----------------------------------------------------------------------------+
22 *
Wolfgang Denk265817c2005-09-25 00:53:22 +020023 * File Name: enetemac.c
wdenkba56f622004-02-06 23:19:44 +000024 *
Wolfgang Denk265817c2005-09-25 00:53:22 +020025 * Function: Device driver for the ethernet EMAC3 macro on the 405GP.
wdenkba56f622004-02-06 23:19:44 +000026 *
Wolfgang Denk265817c2005-09-25 00:53:22 +020027 * Author: Mark Wisner
wdenkba56f622004-02-06 23:19:44 +000028 *
29 * Change Activity-
30 *
Wolfgang Denk265817c2005-09-25 00:53:22 +020031 * Date Description of Change BY
32 * --------- --------------------- ---
33 * 05-May-99 Created MKW
34 * 27-Jun-99 Clean up JWB
35 * 16-Jul-99 Added MAL error recovery and better IP packet handling MKW
36 * 29-Jul-99 Added Full duplex support MKW
37 * 06-Aug-99 Changed names for Mal CR reg MKW
38 * 23-Aug-99 Turned off SYE when running at 10Mbs MKW
39 * 24-Aug-99 Marked descriptor empty after call_xlc MKW
40 * 07-Sep-99 Set MAL RX buffer size reg to ENET_MAX_MTU_ALIGNED / 16 MCG
41 * to avoid chaining maximum sized packets. Push starting
42 * RX descriptor address up to the next cache line boundary.
43 * 16-Jan-00 Added support for booting with IP of 0x0 MKW
44 * 15-Mar-00 Updated enetInit() to enable broadcast addresses in the
45 * EMAC_RXM register. JWB
46 * 12-Mar-01 anne-sophie.harnois@nextream.fr
47 * - Variables are compatible with those already defined in
48 * include/net.h
49 * - Receive buffer descriptor ring is used to send buffers
50 * to the user
51 * - Info print about send/received/handled packet number if
52 * INFO_405_ENET is set
53 * 17-Apr-01 stefan.roese@esd-electronics.com
54 * - MAL reset in "eth_halt" included
55 * - Enet speed and duplex output now in one line
56 * 08-May-01 stefan.roese@esd-electronics.com
57 * - MAL error handling added (eth_init called again)
58 * 13-Nov-01 stefan.roese@esd-electronics.com
59 * - Set IST bit in EMAC_M1 reg upon 100MBit or full duplex
60 * 04-Jan-02 stefan.roese@esd-electronics.com
61 * - Wait for PHY auto negotiation to complete added
62 * 06-Feb-02 stefan.roese@esd-electronics.com
63 * - Bug fixed in waiting for auto negotiation to complete
64 * 26-Feb-02 stefan.roese@esd-electronics.com
65 * - rx and tx buffer descriptors now allocated (no fixed address
66 * used anymore)
67 * 17-Jun-02 stefan.roese@esd-electronics.com
68 * - MAL error debug printf 'M' removed (rx de interrupt may
69 * occur upon many incoming packets with only 4 rx buffers).
wdenkba56f622004-02-06 23:19:44 +000070 *-----------------------------------------------------------------------------*
Wolfgang Denk265817c2005-09-25 00:53:22 +020071 * 17-Nov-03 travis.sawyer@sandburst.com
72 * - ported from 405gp_enet.c to utilized upto 4 EMAC ports
73 * in the 440GX. This port should work with the 440GP
74 * (2 EMACs) also
75 * 15-Aug-05 sr@denx.de
76 * - merged 405gp_enet.c and 440gx_enet.c to generic 4xx_enet.c
77 now handling all 4xx cpu's.
wdenkba56f622004-02-06 23:19:44 +000078 *-----------------------------------------------------------------------------*/
79
80#include <config.h>
wdenkba56f622004-02-06 23:19:44 +000081#include <common.h>
82#include <net.h>
83#include <asm/processor.h>
Stefan Roese2d834762007-10-23 14:03:17 +020084#include <asm/io.h>
Stefan Roeseff768cb2007-10-31 18:01:24 +010085#include <asm/cache.h>
86#include <asm/mmu.h>
wdenkba56f622004-02-06 23:19:44 +000087#include <commproc.h>
Stefan Roesed6c61aa2005-08-16 18:18:00 +020088#include <ppc4xx.h>
89#include <ppc4xx_enet.h>
wdenkba56f622004-02-06 23:19:44 +000090#include <405_mal.h>
91#include <miiphy.h>
92#include <malloc.h>
wdenkba56f622004-02-06 23:19:44 +000093
Stefan Roesed6c61aa2005-08-16 18:18:00 +020094/*
Wolfgang Denk0c8721a2005-09-23 11:05:55 +020095 * Only compile for platform with AMCC EMAC ethernet controller and
Stefan Roesed6c61aa2005-08-16 18:18:00 +020096 * network support enabled.
97 * Remark: CONFIG_405 describes Xilinx PPC405 FPGA without EMAC controller!
98 */
Jon Loeliger3a1ed1e2007-07-09 18:57:22 -050099#if defined(CONFIG_CMD_NET) && !defined(CONFIG_405) && !defined(CONFIG_IOP480)
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200100
Jon Loeliger3a1ed1e2007-07-09 18:57:22 -0500101#if !(defined(CONFIG_MII) || defined(CONFIG_CMD_MII))
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200102#error "CONFIG_MII has to be defined!"
103#endif
wdenkba56f622004-02-06 23:19:44 +0000104
Stefan Roese1e25f952005-10-20 16:34:28 +0200105#if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_NET_MULTI)
106#error "CONFIG_NET_MULTI has to be defined for NetConsole"
107#endif
108
Wolfgang Denk265817c2005-09-25 00:53:22 +0200109#define EMAC_RESET_TIMEOUT 1000 /* 1000 ms reset timeout */
Stefan Roese1338e6a2007-10-23 14:05:08 +0200110#define PHY_AUTONEGOTIATE_TIMEOUT 5000 /* 5000 ms autonegotiate timeout */
wdenkba56f622004-02-06 23:19:44 +0000111
wdenkba56f622004-02-06 23:19:44 +0000112/* Ethernet Transmit and Receive Buffers */
113/* AS.HARNOIS
114 * In the same way ENET_MAX_MTU and ENET_MAX_MTU_ALIGNED are set from
115 * PKTSIZE and PKTSIZE_ALIGN (include/net.h)
116 */
Wolfgang Denk265817c2005-09-25 00:53:22 +0200117#define ENET_MAX_MTU PKTSIZE
wdenkba56f622004-02-06 23:19:44 +0000118#define ENET_MAX_MTU_ALIGNED PKTSIZE_ALIGN
119
wdenkba56f622004-02-06 23:19:44 +0000120/*-----------------------------------------------------------------------------+
121 * Defines for MAL/EMAC interrupt conditions as reported in the UIC (Universal
122 * Interrupt Controller).
123 *-----------------------------------------------------------------------------*/
124#define MAL_UIC_ERR ( UIC_MAL_SERR | UIC_MAL_TXDE | UIC_MAL_RXDE)
125#define MAL_UIC_DEF (UIC_MAL_RXEOB | MAL_UIC_ERR)
126#define EMAC_UIC_DEF UIC_ENET
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200127#define EMAC_UIC_DEF1 UIC_ENET1
128#define SEL_UIC_DEF(p) (p ? UIC_ENET1 : UIC_ENET )
wdenkba56f622004-02-06 23:19:44 +0000129
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200130#undef INFO_4XX_ENET
wdenkba56f622004-02-06 23:19:44 +0000131
Wolfgang Denk265817c2005-09-25 00:53:22 +0200132#define BI_PHYMODE_NONE 0
133#define BI_PHYMODE_ZMII 1
wdenk3c74e322004-02-22 23:46:08 +0000134#define BI_PHYMODE_RGMII 2
Stefan Roese887e2ec2006-09-07 11:51:23 +0200135#define BI_PHYMODE_GMII 3
136#define BI_PHYMODE_RTBI 4
137#define BI_PHYMODE_TBI 5
Stefan Roesedbbd1252007-10-05 17:10:59 +0200138#if defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roese8ac41e32008-03-11 15:05:26 +0100139 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200140 defined(CONFIG_405EX)
Stefan Roese887e2ec2006-09-07 11:51:23 +0200141#define BI_PHYMODE_SMII 6
142#define BI_PHYMODE_MII 7
Stefan Roese8ac41e32008-03-11 15:05:26 +0100143#if defined(CONFIG_460EX) || defined(CONFIG_460GT)
144#define BI_PHYMODE_RMII 8
145#endif
Stefan Roese887e2ec2006-09-07 11:51:23 +0200146#endif
wdenk3c74e322004-02-22 23:46:08 +0000147
Stefan Roese1941cce2007-10-05 17:35:10 +0200148#if defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200149 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roese8ac41e32008-03-11 15:05:26 +0100150 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200151 defined(CONFIG_405EX)
Stefan Roese887e2ec2006-09-07 11:51:23 +0200152#define SDR0_MFR_ETH_CLK_SEL_V(n) ((0x01<<27) / (n+1))
153#endif
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200154
Stefan Roese8ac41e32008-03-11 15:05:26 +0100155#if defined(CONFIG_460EX) || defined(CONFIG_460GT)
156#define SDR0_ETH_CFG_CLK_SEL_V(n) (0x01 << (8 + n))
157#endif
158
159#if defined(CONFIG_460EX) || defined(CONFIG_460GT)
160#define MAL_RX_CHAN_MUL 8 /* 460EX/GT uses MAL channel 8 for EMAC1 */
161#else
162#define MAL_RX_CHAN_MUL 1
163#endif
164
wdenkba56f622004-02-06 23:19:44 +0000165/*-----------------------------------------------------------------------------+
166 * Global variables. TX and RX descriptors and buffers.
167 *-----------------------------------------------------------------------------*/
168/* IER globals */
169static uint32_t mal_ier;
170
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200171#if !defined(CONFIG_NET_MULTI)
Stefan Roese4f92ac32005-10-10 17:43:58 +0200172struct eth_device *emac0_dev = NULL;
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200173#endif
174
Stefan Roese1e25f952005-10-20 16:34:28 +0200175/*
176 * Get count of EMAC devices (doesn't have to be the max. possible number
177 * supported by the cpu)
Stefan Roese353f2682007-10-23 10:10:08 +0200178 *
179 * CONFIG_BOARD_EMAC_COUNT added so now a "dynamic" way to configure the
180 * EMAC count is possible. As it is needed for the Kilauea/Haleakala
181 * 405EX/405EXr eval board, using the same binary.
Stefan Roese1e25f952005-10-20 16:34:28 +0200182 */
Stefan Roese353f2682007-10-23 10:10:08 +0200183#if defined(CONFIG_BOARD_EMAC_COUNT)
184#define LAST_EMAC_NUM board_emac_count()
185#else /* CONFIG_BOARD_EMAC_COUNT */
Stefan Roese1e25f952005-10-20 16:34:28 +0200186#if defined(CONFIG_HAS_ETH3)
187#define LAST_EMAC_NUM 4
188#elif defined(CONFIG_HAS_ETH2)
189#define LAST_EMAC_NUM 3
190#elif defined(CONFIG_HAS_ETH1)
191#define LAST_EMAC_NUM 2
192#else
193#define LAST_EMAC_NUM 1
194#endif
Stefan Roese353f2682007-10-23 10:10:08 +0200195#endif /* CONFIG_BOARD_EMAC_COUNT */
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200196
Stefan Roese5fb692c2007-01-18 10:25:34 +0100197/* normal boards start with EMAC0 */
198#if !defined(CONFIG_EMAC_NR_START)
199#define CONFIG_EMAC_NR_START 0
200#endif
201
Stefan Roesedbbd1252007-10-05 17:10:59 +0200202#if defined(CONFIG_405EX) || defined(CONFIG_440EPX)
203#define ETH_IRQ_NUM(dev) (VECNUM_ETH0 + ((dev)))
204#else
205#define ETH_IRQ_NUM(dev) (VECNUM_ETH0 + ((dev) * 2))
206#endif
207
Stefan Roeseff768cb2007-10-31 18:01:24 +0100208#define MAL_RX_DESC_SIZE 2048
209#define MAL_TX_DESC_SIZE 2048
210#define MAL_ALLOC_SIZE (MAL_TX_DESC_SIZE + MAL_RX_DESC_SIZE)
211
wdenkba56f622004-02-06 23:19:44 +0000212/*-----------------------------------------------------------------------------+
213 * Prototypes and externals.
214 *-----------------------------------------------------------------------------*/
215static void enet_rcv (struct eth_device *dev, unsigned long malisr);
216
217int enetInt (struct eth_device *dev);
218static void mal_err (struct eth_device *dev, unsigned long isr,
219 unsigned long uic, unsigned long maldef,
220 unsigned long mal_errr);
221static void emac_err (struct eth_device *dev, unsigned long isr);
222
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200223extern int phy_setup_aneg (char *devname, unsigned char addr);
224extern int emac4xx_miiphy_read (char *devname, unsigned char addr,
225 unsigned char reg, unsigned short *value);
226extern int emac4xx_miiphy_write (char *devname, unsigned char addr,
227 unsigned char reg, unsigned short value);
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200228
Stefan Roese353f2682007-10-23 10:10:08 +0200229int board_emac_count(void);
230
Stefan Roese8ac41e32008-03-11 15:05:26 +0100231static void emac_loopback_enable(EMAC_4XX_HW_PST hw_p)
232{
233#if defined(CONFIG_440SPE) || \
234 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
235 defined(CONFIG_405EX)
236 u32 val;
237
238 mfsdr(sdr_mfr, val);
239 val |= SDR0_MFR_ETH_CLK_SEL_V(hw_p->devnum);
240 mtsdr(sdr_mfr, val);
241#elif defined(CONFIG_460EX) || defined(CONFIG_460GT)
242 u32 val;
243
244 mfsdr(SDR0_ETH_CFG, val);
245 val |= SDR0_ETH_CFG_CLK_SEL_V(hw_p->devnum);
246 mtsdr(SDR0_ETH_CFG, val);
247#endif
248}
249
250static void emac_loopback_disable(EMAC_4XX_HW_PST hw_p)
251{
252#if defined(CONFIG_440SPE) || \
253 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
254 defined(CONFIG_405EX)
255 u32 val;
256
257 mfsdr(sdr_mfr, val);
258 val &= ~SDR0_MFR_ETH_CLK_SEL_V(hw_p->devnum);
259 mtsdr(sdr_mfr, val);
260#elif defined(CONFIG_460EX) || defined(CONFIG_460GT)
261 u32 val;
262
263 mfsdr(SDR0_ETH_CFG, val);
264 val &= ~SDR0_ETH_CFG_CLK_SEL_V(hw_p->devnum);
265 mtsdr(SDR0_ETH_CFG, val);
266#endif
267}
268
wdenkba56f622004-02-06 23:19:44 +0000269/*-----------------------------------------------------------------------------+
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200270| ppc_4xx_eth_halt
wdenkba56f622004-02-06 23:19:44 +0000271| Disable MAL channel, and EMACn
wdenkba56f622004-02-06 23:19:44 +0000272+-----------------------------------------------------------------------------*/
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200273static void ppc_4xx_eth_halt (struct eth_device *dev)
wdenkba56f622004-02-06 23:19:44 +0000274{
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200275 EMAC_4XX_HW_PST hw_p = dev->priv;
Stefan Roese9ad31982008-03-19 16:35:12 +0100276 u32 val = 10000;
wdenkba56f622004-02-06 23:19:44 +0000277
Stefan Roese2d834762007-10-23 14:03:17 +0200278 out_be32((void *)EMAC_IER + hw_p->hw_addr, 0x00000000); /* disable emac interrupts */
wdenkba56f622004-02-06 23:19:44 +0000279
280 /* 1st reset MAL channel */
281 /* Note: writing a 0 to a channel has no effect */
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200282#if defined(CONFIG_405EP) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
283 mtdcr (maltxcarr, (MAL_CR_MMSR >> (hw_p->devnum * 2)));
284#else
wdenkba56f622004-02-06 23:19:44 +0000285 mtdcr (maltxcarr, (MAL_CR_MMSR >> hw_p->devnum));
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200286#endif
wdenkba56f622004-02-06 23:19:44 +0000287 mtdcr (malrxcarr, (MAL_CR_MMSR >> hw_p->devnum));
288
289 /* wait for reset */
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200290 while (mfdcr (malrxcasr) & (MAL_CR_MMSR >> hw_p->devnum)) {
wdenkba56f622004-02-06 23:19:44 +0000291 udelay (1000); /* Delay 1 MS so as not to hammer the register */
Stefan Roese9ad31982008-03-19 16:35:12 +0100292 val--;
293 if (val == 0)
wdenkba56f622004-02-06 23:19:44 +0000294 break;
wdenkba56f622004-02-06 23:19:44 +0000295 }
296
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200297 /* provide clocks for EMAC internal loopback */
Stefan Roese8ac41e32008-03-11 15:05:26 +0100298 emac_loopback_enable(hw_p);
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200299
Stefan Roese8ac41e32008-03-11 15:05:26 +0100300 /* EMAC RESET */
Stefan Roese2d834762007-10-23 14:03:17 +0200301 out_be32((void *)EMAC_M0 + hw_p->hw_addr, EMAC_M0_SRST);
wdenkba56f622004-02-06 23:19:44 +0000302
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200303 /* remove clocks for EMAC internal loopback */
Stefan Roese8ac41e32008-03-11 15:05:26 +0100304 emac_loopback_disable(hw_p);
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200305
Stefan Roesea93316c2005-10-18 19:17:12 +0200306#ifndef CONFIG_NETCONSOLE
Stefan Roesec157d8e2005-08-01 16:41:48 +0200307 hw_p->print_speed = 1; /* print speed message again next time */
Stefan Roesea93316c2005-10-18 19:17:12 +0200308#endif
Stefan Roesec157d8e2005-08-01 16:41:48 +0200309
Stefan Roese4c9e8552008-03-19 16:20:49 +0100310#if defined(CONFIG_460EX) || defined(CONFIG_460GT)
311 /* don't bypass the TAHOE0/TAHOE1 cores for Linux */
Stefan Roese9ad31982008-03-19 16:35:12 +0100312 mfsdr(SDR0_ETH_CFG, val);
313 val &= ~(SDR0_ETH_CFG_TAHOE0_BYPASS | SDR0_ETH_CFG_TAHOE1_BYPASS);
314 mtsdr(SDR0_ETH_CFG, val);
Stefan Roese4c9e8552008-03-19 16:20:49 +0100315#endif
316
wdenkba56f622004-02-06 23:19:44 +0000317 return;
318}
319
Stefan Roese846b0dd2005-08-08 12:42:22 +0200320#if defined (CONFIG_440GX)
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200321int ppc_4xx_eth_setup_bridge(int devnum, bd_t * bis)
wdenk855a4962004-03-14 18:23:55 +0000322{
323 unsigned long pfc1;
324 unsigned long zmiifer;
325 unsigned long rmiifer;
326
327 mfsdr(sdr_pfc1, pfc1);
328 pfc1 = SDR0_PFC1_EPS_DECODE(pfc1);
329
330 zmiifer = 0;
331 rmiifer = 0;
332
333 switch (pfc1) {
334 case 1:
335 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
336 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(1);
337 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(2);
338 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(3);
339 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
340 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
341 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
342 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
343 break;
344 case 2:
Stefan Roesef6e495f2006-11-27 17:43:25 +0100345 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(0);
346 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(1);
347 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(2);
348 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(3);
wdenk855a4962004-03-14 18:23:55 +0000349 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
350 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
351 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
352 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
353 break;
354 case 3:
355 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
356 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
357 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
358 bis->bi_phymode[1] = BI_PHYMODE_NONE;
359 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
360 bis->bi_phymode[3] = BI_PHYMODE_NONE;
361 break;
362 case 4:
363 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(0);
364 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(1);
365 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V (2);
366 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V (3);
367 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
368 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
369 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
370 bis->bi_phymode[3] = BI_PHYMODE_RGMII;
371 break;
372 case 5:
373 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (0);
374 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (1);
375 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (2);
376 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(3);
377 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
378 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
379 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
380 bis->bi_phymode[3] = BI_PHYMODE_RGMII;
381 break;
382 case 6:
383 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (0);
384 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (1);
385 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
wdenk855a4962004-03-14 18:23:55 +0000386 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
387 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
388 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
wdenk855a4962004-03-14 18:23:55 +0000389 break;
390 case 0:
391 default:
392 zmiifer = ZMII_FER_MII << ZMII_FER_V(devnum);
393 rmiifer = 0x0;
394 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
395 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
396 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
397 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
398 break;
399 }
400
401 /* Ensure we setup mdio for this devnum and ONLY this devnum */
402 zmiifer |= (ZMII_FER_MDI) << ZMII_FER_V(devnum);
403
Stefan Roeseff768cb2007-10-31 18:01:24 +0100404 out_be32((void *)ZMII_FER, zmiifer);
405 out_be32((void *)RGMII_FER, rmiifer);
wdenk855a4962004-03-14 18:23:55 +0000406
407 return ((int)pfc1);
wdenk855a4962004-03-14 18:23:55 +0000408}
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200409#endif /* CONFIG_440_GX */
wdenk855a4962004-03-14 18:23:55 +0000410
Stefan Roese887e2ec2006-09-07 11:51:23 +0200411#if defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
412int ppc_4xx_eth_setup_bridge(int devnum, bd_t * bis)
413{
414 unsigned long zmiifer=0x0;
Matthias Fuchs37ed6cd2007-04-24 14:03:45 +0200415 unsigned long pfc1;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200416
Matthias Fuchs37ed6cd2007-04-24 14:03:45 +0200417 mfsdr(sdr_pfc1, pfc1);
418 pfc1 &= SDR0_PFC1_SELECT_MASK;
419
Wolfgang Denk2f152782007-05-05 18:23:11 +0200420 switch (pfc1) {
Matthias Fuchs37ed6cd2007-04-24 14:03:45 +0200421 case SDR0_PFC1_SELECT_CONFIG_2:
Stefan Roese887e2ec2006-09-07 11:51:23 +0200422 /* 1 x GMII port */
Stefan Roese2d834762007-10-23 14:03:17 +0200423 out_be32((void *)ZMII_FER, 0x00);
424 out_be32((void *)RGMII_FER, 0x00000037);
Stefan Roese887e2ec2006-09-07 11:51:23 +0200425 bis->bi_phymode[0] = BI_PHYMODE_GMII;
426 bis->bi_phymode[1] = BI_PHYMODE_NONE;
427 break;
Matthias Fuchs37ed6cd2007-04-24 14:03:45 +0200428 case SDR0_PFC1_SELECT_CONFIG_4:
Stefan Roese887e2ec2006-09-07 11:51:23 +0200429 /* 2 x RGMII ports */
Stefan Roese2d834762007-10-23 14:03:17 +0200430 out_be32((void *)ZMII_FER, 0x00);
431 out_be32((void *)RGMII_FER, 0x00000055);
Stefan Roese887e2ec2006-09-07 11:51:23 +0200432 bis->bi_phymode[0] = BI_PHYMODE_RGMII;
433 bis->bi_phymode[1] = BI_PHYMODE_RGMII;
434 break;
Matthias Fuchs37ed6cd2007-04-24 14:03:45 +0200435 case SDR0_PFC1_SELECT_CONFIG_6:
Stefan Roese887e2ec2006-09-07 11:51:23 +0200436 /* 2 x SMII ports */
Stefan Roese2d834762007-10-23 14:03:17 +0200437 out_be32((void *)ZMII_FER,
438 ((ZMII_FER_SMII) << ZMII_FER_V(0)) |
439 ((ZMII_FER_SMII) << ZMII_FER_V(1)));
440 out_be32((void *)RGMII_FER, 0x00000000);
Matthias Fuchs37ed6cd2007-04-24 14:03:45 +0200441 bis->bi_phymode[0] = BI_PHYMODE_SMII;
442 bis->bi_phymode[1] = BI_PHYMODE_SMII;
443 break;
444 case SDR0_PFC1_SELECT_CONFIG_1_2:
445 /* only 1 x MII supported */
Stefan Roese2d834762007-10-23 14:03:17 +0200446 out_be32((void *)ZMII_FER, (ZMII_FER_MII) << ZMII_FER_V(0));
447 out_be32((void *)RGMII_FER, 0x00000000);
Matthias Fuchs37ed6cd2007-04-24 14:03:45 +0200448 bis->bi_phymode[0] = BI_PHYMODE_MII;
449 bis->bi_phymode[1] = BI_PHYMODE_NONE;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200450 break;
451 default:
452 break;
453 }
454
455 /* Ensure we setup mdio for this devnum and ONLY this devnum */
Stefan Roese2d834762007-10-23 14:03:17 +0200456 zmiifer = in_be32((void *)ZMII_FER);
Stefan Roese887e2ec2006-09-07 11:51:23 +0200457 zmiifer |= (ZMII_FER_MDI) << ZMII_FER_V(devnum);
Stefan Roese2d834762007-10-23 14:03:17 +0200458 out_be32((void *)ZMII_FER, zmiifer);
Stefan Roese887e2ec2006-09-07 11:51:23 +0200459
460 return ((int)0x0);
461}
462#endif /* CONFIG_440EPX */
463
Stefan Roesedbbd1252007-10-05 17:10:59 +0200464#if defined(CONFIG_405EX)
465int ppc_4xx_eth_setup_bridge(int devnum, bd_t * bis)
466{
Grant Erickson1740c1b2008-07-08 08:35:00 -0700467 u32 rgmiifer = 0;
Stefan Roesedbbd1252007-10-05 17:10:59 +0200468
469 /*
Grant Erickson1740c1b2008-07-08 08:35:00 -0700470 * The 405EX(r)'s RGMII bridge can operate in one of several
471 * modes, only one of which (2 x RGMII) allows the
472 * simultaneous use of both EMACs on the 405EX.
Stefan Roesedbbd1252007-10-05 17:10:59 +0200473 */
Grant Erickson1740c1b2008-07-08 08:35:00 -0700474
475 switch (CONFIG_EMAC_PHY_MODE) {
476
477 case EMAC_PHY_MODE_NONE:
478 /* No ports */
479 rgmiifer |= RGMII_FER_DIS << 0;
480 rgmiifer |= RGMII_FER_DIS << 4;
481 out_be32((void *)RGMII_FER, rgmiifer);
482 bis->bi_phymode[0] = BI_PHYMODE_NONE;
483 bis->bi_phymode[1] = BI_PHYMODE_NONE;
484 break;
485 case EMAC_PHY_MODE_NONE_RGMII:
486 /* 1 x RGMII port on channel 0 */
487 rgmiifer |= RGMII_FER_RGMII << 0;
488 rgmiifer |= RGMII_FER_DIS << 4;
489 out_be32((void *)RGMII_FER, rgmiifer);
490 bis->bi_phymode[0] = BI_PHYMODE_RGMII;
491 bis->bi_phymode[1] = BI_PHYMODE_NONE;
492 break;
493 case EMAC_PHY_MODE_RGMII_NONE:
494 /* 1 x RGMII port on channel 1 */
495 rgmiifer |= RGMII_FER_DIS << 0;
496 rgmiifer |= RGMII_FER_RGMII << 4;
497 out_be32((void *)RGMII_FER, rgmiifer);
498 bis->bi_phymode[0] = BI_PHYMODE_NONE;
499 bis->bi_phymode[1] = BI_PHYMODE_RGMII;
500 break;
501 case EMAC_PHY_MODE_RGMII_RGMII:
Stefan Roesedbbd1252007-10-05 17:10:59 +0200502 /* 2 x RGMII ports */
Grant Erickson1740c1b2008-07-08 08:35:00 -0700503 rgmiifer |= RGMII_FER_RGMII << 0;
504 rgmiifer |= RGMII_FER_RGMII << 4;
505 out_be32((void *)RGMII_FER, rgmiifer);
Stefan Roesedbbd1252007-10-05 17:10:59 +0200506 bis->bi_phymode[0] = BI_PHYMODE_RGMII;
507 bis->bi_phymode[1] = BI_PHYMODE_RGMII;
508 break;
Grant Erickson1740c1b2008-07-08 08:35:00 -0700509 case EMAC_PHY_MODE_NONE_GMII:
510 /* 1 x GMII port on channel 0 */
511 rgmiifer |= RGMII_FER_GMII << 0;
512 rgmiifer |= RGMII_FER_DIS << 4;
513 out_be32((void *)RGMII_FER, rgmiifer);
514 bis->bi_phymode[0] = BI_PHYMODE_GMII;
515 bis->bi_phymode[1] = BI_PHYMODE_NONE;
516 break;
517 case EMAC_PHY_MODE_NONE_MII:
518 /* 1 x MII port on channel 0 */
519 rgmiifer |= RGMII_FER_MII << 0;
520 rgmiifer |= RGMII_FER_DIS << 4;
521 out_be32((void *)RGMII_FER, rgmiifer);
522 bis->bi_phymode[0] = BI_PHYMODE_MII;
523 bis->bi_phymode[1] = BI_PHYMODE_NONE;
524 break;
525 case EMAC_PHY_MODE_GMII_NONE:
526 /* 1 x GMII port on channel 1 */
527 rgmiifer |= RGMII_FER_DIS << 0;
528 rgmiifer |= RGMII_FER_GMII << 4;
529 out_be32((void *)RGMII_FER, rgmiifer);
530 bis->bi_phymode[0] = BI_PHYMODE_NONE;
531 bis->bi_phymode[1] = BI_PHYMODE_GMII;
532 break;
533 case EMAC_PHY_MODE_MII_NONE:
534 /* 1 x MII port on channel 1 */
535 rgmiifer |= RGMII_FER_DIS << 0;
536 rgmiifer |= RGMII_FER_MII << 4;
537 out_be32((void *)RGMII_FER, rgmiifer);
538 bis->bi_phymode[0] = BI_PHYMODE_NONE;
539 bis->bi_phymode[1] = BI_PHYMODE_MII;
Stefan Roesedbbd1252007-10-05 17:10:59 +0200540 break;
541 default:
542 break;
543 }
544
545 /* Ensure we setup mdio for this devnum and ONLY this devnum */
Grant Erickson1740c1b2008-07-08 08:35:00 -0700546 rgmiifer = in_be32((void *)RGMII_FER);
547 rgmiifer |= (1 << (19-devnum));
548 out_be32((void *)RGMII_FER, rgmiifer);
Stefan Roesedbbd1252007-10-05 17:10:59 +0200549
550 return ((int)0x0);
551}
552#endif /* CONFIG_405EX */
553
Stefan Roese8ac41e32008-03-11 15:05:26 +0100554#if defined(CONFIG_460EX) || defined(CONFIG_460GT)
555int ppc_4xx_eth_setup_bridge(int devnum, bd_t * bis)
556{
557 u32 eth_cfg;
558 u32 zmiifer; /* ZMII0_FER reg. */
559 u32 rmiifer; /* RGMII0_FER reg. Bridge 0 */
560 u32 rmiifer1; /* RGMII0_FER reg. Bridge 1 */
Stefan Roese4c9e8552008-03-19 16:20:49 +0100561 int mode;
Stefan Roese8ac41e32008-03-11 15:05:26 +0100562
563 zmiifer = 0;
564 rmiifer = 0;
565 rmiifer1 = 0;
566
Stefan Roese4c9e8552008-03-19 16:20:49 +0100567#if defined(CONFIG_460EX)
568 mode = 9;
569#else
570 mode = 10;
571#endif
572
Stefan Roese8ac41e32008-03-11 15:05:26 +0100573 /* TODO:
574 * NOTE: 460GT has 2 RGMII bridge cores:
575 * emac0 ------ RGMII0_BASE
576 * |
577 * emac1 -----+
578 *
579 * emac2 ------ RGMII1_BASE
580 * |
581 * emac3 -----+
582 *
583 * 460EX has 1 RGMII bridge core:
584 * and RGMII1_BASE is disabled
585 * emac0 ------ RGMII0_BASE
586 * |
587 * emac1 -----+
588 */
589
590 /*
591 * Right now only 2*RGMII is supported. Please extend when needed.
592 * sr - 2008-02-19
593 */
Stefan Roese4c9e8552008-03-19 16:20:49 +0100594 switch (mode) {
Stefan Roese8ac41e32008-03-11 15:05:26 +0100595 case 1:
596 /* 1 MII - 460EX */
597 /* GMC0 EMAC4_0, ZMII Bridge */
598 zmiifer |= ZMII_FER_MII << ZMII_FER_V(0);
599 bis->bi_phymode[0] = BI_PHYMODE_MII;
600 bis->bi_phymode[1] = BI_PHYMODE_NONE;
601 bis->bi_phymode[2] = BI_PHYMODE_NONE;
602 bis->bi_phymode[3] = BI_PHYMODE_NONE;
603 break;
604 case 2:
605 /* 2 MII - 460GT */
606 /* GMC0 EMAC4_0, GMC1 EMAC4_2, ZMII Bridge */
607 zmiifer |= ZMII_FER_MII << ZMII_FER_V(0);
608 zmiifer |= ZMII_FER_MII << ZMII_FER_V(2);
609 bis->bi_phymode[0] = BI_PHYMODE_MII;
610 bis->bi_phymode[1] = BI_PHYMODE_NONE;
611 bis->bi_phymode[2] = BI_PHYMODE_MII;
612 bis->bi_phymode[3] = BI_PHYMODE_NONE;
613 break;
614 case 3:
615 /* 2 RMII - 460EX */
616 /* GMC0 EMAC4_0, GMC0 EMAC4_1, ZMII Bridge */
617 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
618 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(1);
619 bis->bi_phymode[0] = BI_PHYMODE_RMII;
620 bis->bi_phymode[1] = BI_PHYMODE_RMII;
621 bis->bi_phymode[2] = BI_PHYMODE_NONE;
622 bis->bi_phymode[3] = BI_PHYMODE_NONE;
623 break;
624 case 4:
625 /* 4 RMII - 460GT */
626 /* GMC0 EMAC4_0, GMC0 EMAC4_1, GMC1 EMAC4_2, GMC1, EMAC4_3 */
627 /* ZMII Bridge */
628 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
629 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(1);
630 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(2);
631 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(3);
632 bis->bi_phymode[0] = BI_PHYMODE_RMII;
633 bis->bi_phymode[1] = BI_PHYMODE_RMII;
634 bis->bi_phymode[2] = BI_PHYMODE_RMII;
635 bis->bi_phymode[3] = BI_PHYMODE_RMII;
636 break;
637 case 5:
638 /* 2 SMII - 460EX */
639 /* GMC0 EMAC4_0, GMC0 EMAC4_1, ZMII Bridge */
640 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(0);
641 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(1);
642 bis->bi_phymode[0] = BI_PHYMODE_SMII;
643 bis->bi_phymode[1] = BI_PHYMODE_SMII;
644 bis->bi_phymode[2] = BI_PHYMODE_NONE;
645 bis->bi_phymode[3] = BI_PHYMODE_NONE;
646 break;
647 case 6:
648 /* 4 SMII - 460GT */
649 /* GMC0 EMAC4_0, GMC0 EMAC4_1, GMC0 EMAC4_3, GMC0 EMAC4_3 */
650 /* ZMII Bridge */
651 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(0);
652 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(1);
653 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(2);
654 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(3);
655 bis->bi_phymode[0] = BI_PHYMODE_SMII;
656 bis->bi_phymode[1] = BI_PHYMODE_SMII;
657 bis->bi_phymode[2] = BI_PHYMODE_SMII;
658 bis->bi_phymode[3] = BI_PHYMODE_SMII;
659 break;
660 case 7:
661 /* This is the default mode that we want for board bringup - Maple */
662 /* 1 GMII - 460EX */
663 /* GMC0 EMAC4_0, RGMII Bridge 0 */
664 rmiifer |= RGMII_FER_MDIO(0);
665
666 if (devnum == 0) {
667 rmiifer |= RGMII_FER_GMII << RGMII_FER_V(2); /* CH0CFG - EMAC0 */
668 bis->bi_phymode[0] = BI_PHYMODE_GMII;
669 bis->bi_phymode[1] = BI_PHYMODE_NONE;
670 bis->bi_phymode[2] = BI_PHYMODE_NONE;
671 bis->bi_phymode[3] = BI_PHYMODE_NONE;
672 } else {
673 rmiifer |= RGMII_FER_GMII << RGMII_FER_V(3); /* CH1CFG - EMAC1 */
674 bis->bi_phymode[0] = BI_PHYMODE_NONE;
675 bis->bi_phymode[1] = BI_PHYMODE_GMII;
676 bis->bi_phymode[2] = BI_PHYMODE_NONE;
677 bis->bi_phymode[3] = BI_PHYMODE_NONE;
678 }
679 break;
680 case 8:
681 /* 2 GMII - 460GT */
682 /* GMC0 EMAC4_0, RGMII Bridge 0 */
683 /* GMC1 EMAC4_2, RGMII Bridge 1 */
684 rmiifer |= RGMII_FER_GMII << RGMII_FER_V(2); /* CH0CFG - EMAC0 */
685 rmiifer1 |= RGMII_FER_GMII << RGMII_FER_V(2); /* CH0CFG - EMAC2 */
686 rmiifer |= RGMII_FER_MDIO(0); /* enable MDIO - EMAC0 */
687 rmiifer1 |= RGMII_FER_MDIO(0); /* enable MDIO - EMAC2 */
688
689 bis->bi_phymode[0] = BI_PHYMODE_GMII;
690 bis->bi_phymode[1] = BI_PHYMODE_NONE;
691 bis->bi_phymode[2] = BI_PHYMODE_GMII;
692 bis->bi_phymode[3] = BI_PHYMODE_NONE;
693 break;
694 case 9:
695 /* 2 RGMII - 460EX */
696 /* GMC0 EMAC4_0, GMC0 EMAC4_1, RGMII Bridge 0 */
697 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
698 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(3);
699 rmiifer |= RGMII_FER_MDIO(0); /* enable MDIO - EMAC0 */
700
701 bis->bi_phymode[0] = BI_PHYMODE_RGMII;
702 bis->bi_phymode[1] = BI_PHYMODE_RGMII;
703 bis->bi_phymode[2] = BI_PHYMODE_NONE;
704 bis->bi_phymode[3] = BI_PHYMODE_NONE;
705 break;
706 case 10:
707 /* 4 RGMII - 460GT */
708 /* GMC0 EMAC4_0, GMC0 EMAC4_1, RGMII Bridge 0 */
709 /* GMC1 EMAC4_2, GMC1 EMAC4_3, RGMII Bridge 1 */
710 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
711 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(3);
712 rmiifer1 |= RGMII_FER_RGMII << RGMII_FER_V(2);
713 rmiifer1 |= RGMII_FER_RGMII << RGMII_FER_V(3);
714 bis->bi_phymode[0] = BI_PHYMODE_RGMII;
715 bis->bi_phymode[1] = BI_PHYMODE_RGMII;
716 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
717 bis->bi_phymode[3] = BI_PHYMODE_RGMII;
718 break;
719 default:
720 break;
721 }
722
723 /* Set EMAC for MDIO */
724 mfsdr(SDR0_ETH_CFG, eth_cfg);
725 eth_cfg |= SDR0_ETH_CFG_MDIO_SEL_EMAC0;
726 mtsdr(SDR0_ETH_CFG, eth_cfg);
727
728 out_be32((void *)RGMII_FER, rmiifer);
729#if defined(CONFIG_460GT)
730 out_be32((void *)RGMII_FER + RGMII1_BASE_OFFSET, rmiifer1);
731#endif
732
733 /* bypass the TAHOE0/TAHOE1 cores for U-Boot */
734 mfsdr(SDR0_ETH_CFG, eth_cfg);
735 eth_cfg |= (SDR0_ETH_CFG_TAHOE0_BYPASS | SDR0_ETH_CFG_TAHOE1_BYPASS);
736 mtsdr(SDR0_ETH_CFG, eth_cfg);
737
738 return 0;
739}
740#endif /* CONFIG_460EX || CONFIG_460GT */
741
Stefan Roeseff768cb2007-10-31 18:01:24 +0100742static inline void *malloc_aligned(u32 size, u32 align)
743{
744 return (void *)(((u32)malloc(size + align) + align - 1) &
745 ~(align - 1));
746}
747
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200748static int ppc_4xx_eth_init (struct eth_device *dev, bd_t * bis)
wdenkba56f622004-02-06 23:19:44 +0000749{
Stefan Roeseff768cb2007-10-31 18:01:24 +0100750 int i;
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200751 unsigned long reg = 0;
wdenkba56f622004-02-06 23:19:44 +0000752 unsigned long msr;
753 unsigned long speed;
754 unsigned long duplex;
755 unsigned long failsafe;
756 unsigned mode_reg;
757 unsigned short devnum;
758 unsigned short reg_short;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200759#if defined(CONFIG_440GX) || \
760 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200761 defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
Stefan Roese8ac41e32008-03-11 15:05:26 +0100762 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200763 defined(CONFIG_405EX)
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200764 sys_info_t sysinfo;
Stefan Roese887e2ec2006-09-07 11:51:23 +0200765#if defined(CONFIG_440GX) || defined(CONFIG_440SPE) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200766 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roese8ac41e32008-03-11 15:05:26 +0100767 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200768 defined(CONFIG_405EX)
Stefan Roese6e7fb6e2005-11-29 18:18:21 +0100769 int ethgroup = -1;
770#endif
Stefan Roesec157d8e2005-08-01 16:41:48 +0200771#endif
Stefan Roeseff768cb2007-10-31 18:01:24 +0100772 u32 bd_cached;
773 u32 bd_uncached = 0;
Anatolij Gustschin4fae35a2008-02-25 20:54:04 +0100774#ifdef CONFIG_4xx_DCACHE
775 static u32 last_used_ea = 0;
776#endif
Stefan Roesee54ec0f2008-04-03 14:50:34 +0200777#if defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
778 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
779 defined(CONFIG_405EX)
780 int rgmii_channel;
781#endif
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200782
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200783 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenkba56f622004-02-06 23:19:44 +0000784
785 /* before doing anything, figure out if we have a MAC address */
786 /* if not, bail */
Stefan Roese4f92ac32005-10-10 17:43:58 +0200787 if (memcmp (dev->enetaddr, "\0\0\0\0\0\0", 6) == 0) {
788 printf("ERROR: ethaddr not set!\n");
wdenkba56f622004-02-06 23:19:44 +0000789 return -1;
Stefan Roese4f92ac32005-10-10 17:43:58 +0200790 }
wdenkba56f622004-02-06 23:19:44 +0000791
Stefan Roese887e2ec2006-09-07 11:51:23 +0200792#if defined(CONFIG_440GX) || \
793 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200794 defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
Stefan Roese8ac41e32008-03-11 15:05:26 +0100795 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200796 defined(CONFIG_405EX)
wdenkba56f622004-02-06 23:19:44 +0000797 /* Need to get the OPB frequency so we can access the PHY */
798 get_sys_info (&sysinfo);
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200799#endif
wdenkba56f622004-02-06 23:19:44 +0000800
wdenkba56f622004-02-06 23:19:44 +0000801 msr = mfmsr ();
802 mtmsr (msr & ~(MSR_EE)); /* disable interrupts */
803
804 devnum = hw_p->devnum;
805
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200806#ifdef INFO_4XX_ENET
wdenkba56f622004-02-06 23:19:44 +0000807 /* AS.HARNOIS
808 * We should have :
Wolfgang Denk265817c2005-09-25 00:53:22 +0200809 * hw_p->stats.pkts_handled <= hw_p->stats.pkts_rx <= hw_p->stats.pkts_handled+PKTBUFSRX
wdenkba56f622004-02-06 23:19:44 +0000810 * In the most cases hw_p->stats.pkts_handled = hw_p->stats.pkts_rx, but it
811 * is possible that new packets (without relationship with
812 * current transfer) have got the time to arrived before
813 * netloop calls eth_halt
814 */
815 printf ("About preceeding transfer (eth%d):\n"
816 "- Sent packet number %d\n"
817 "- Received packet number %d\n"
818 "- Handled packet number %d\n",
819 hw_p->devnum,
820 hw_p->stats.pkts_tx,
821 hw_p->stats.pkts_rx, hw_p->stats.pkts_handled);
822
823 hw_p->stats.pkts_tx = 0;
824 hw_p->stats.pkts_rx = 0;
825 hw_p->stats.pkts_handled = 0;
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200826 hw_p->print_speed = 1; /* print speed message again next time */
wdenkba56f622004-02-06 23:19:44 +0000827#endif
828
Wolfgang Denk265817c2005-09-25 00:53:22 +0200829 hw_p->tx_err_index = 0; /* Transmit Error Index for tx_err_log */
830 hw_p->rx_err_index = 0; /* Receive Error Index for rx_err_log */
wdenkba56f622004-02-06 23:19:44 +0000831
832 hw_p->rx_slot = 0; /* MAL Receive Slot */
833 hw_p->rx_i_index = 0; /* Receive Interrupt Queue Index */
834 hw_p->rx_u_index = 0; /* Receive User Queue Index */
835
836 hw_p->tx_slot = 0; /* MAL Transmit Slot */
837 hw_p->tx_i_index = 0; /* Transmit Interrupt Queue Index */
838 hw_p->tx_u_index = 0; /* Transmit User Queue Index */
839
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200840#if defined(CONFIG_440) && !defined(CONFIG_440SP) && !defined(CONFIG_440SPE)
wdenkba56f622004-02-06 23:19:44 +0000841 /* set RMII mode */
842 /* NOTE: 440GX spec states that mode is mutually exclusive */
843 /* NOTE: Therefore, disable all other EMACS, since we handle */
844 /* NOTE: only one emac at a time */
845 reg = 0;
Stefan Roese2d834762007-10-23 14:03:17 +0200846 out_be32((void *)ZMII_FER, 0);
wdenkba56f622004-02-06 23:19:44 +0000847 udelay (100);
wdenkba56f622004-02-06 23:19:44 +0000848
Stefan Roese8ac41e32008-03-11 15:05:26 +0100849#if defined(CONFIG_440GP) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
Stefan Roese2d834762007-10-23 14:03:17 +0200850 out_be32((void *)ZMII_FER, (ZMII_FER_RMII | ZMII_FER_MDI) << ZMII_FER_V (devnum));
Stefan Roese8ac41e32008-03-11 15:05:26 +0100851#elif defined(CONFIG_440GX) || \
852 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
853 defined(CONFIG_460EX) || defined(CONFIG_460GT)
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200854 ethgroup = ppc_4xx_eth_setup_bridge(devnum, bis);
wdenk0e6d7982004-03-14 00:07:33 +0000855#endif
Stefan Roesec57c7982005-08-11 17:56:56 +0200856
Stefan Roese2d834762007-10-23 14:03:17 +0200857 out_be32((void *)ZMII_SSR, ZMII_SSR_SP << ZMII_SSR_V(devnum));
Stefan Roese6e7fb6e2005-11-29 18:18:21 +0100858#endif /* defined(CONFIG_440) && !defined(CONFIG_440SP) */
Stefan Roesedbbd1252007-10-05 17:10:59 +0200859#if defined(CONFIG_405EX)
860 ethgroup = ppc_4xx_eth_setup_bridge(devnum, bis);
861#endif
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200862
Stefan Roese8ac41e32008-03-11 15:05:26 +0100863 sync();
wdenk0e6d7982004-03-14 00:07:33 +0000864
Marian Balakowicz6c5879f2006-06-30 16:30:46 +0200865 /* provide clocks for EMAC internal loopback */
Stefan Roese8ac41e32008-03-11 15:05:26 +0100866 emac_loopback_enable(hw_p);
wdenk0e6d7982004-03-14 00:07:33 +0000867
Stefan Roese8ac41e32008-03-11 15:05:26 +0100868 /* EMAC RESET */
Stefan Roese2d834762007-10-23 14:03:17 +0200869 out_be32((void *)EMAC_M0 + hw_p->hw_addr, EMAC_M0_SRST);
wdenkba56f622004-02-06 23:19:44 +0000870
Stefan Roese8ac41e32008-03-11 15:05:26 +0100871 /* remove clocks for EMAC internal loopback */
872 emac_loopback_disable(hw_p);
873
wdenkba56f622004-02-06 23:19:44 +0000874 failsafe = 1000;
Stefan Roese2d834762007-10-23 14:03:17 +0200875 while ((in_be32((void *)EMAC_M0 + hw_p->hw_addr) & (EMAC_M0_SRST)) && failsafe) {
wdenkba56f622004-02-06 23:19:44 +0000876 udelay (1000);
877 failsafe--;
878 }
Stefan Roese887e2ec2006-09-07 11:51:23 +0200879 if (failsafe <= 0)
880 printf("\nProblem resetting EMAC!\n");
wdenkba56f622004-02-06 23:19:44 +0000881
Stefan Roese887e2ec2006-09-07 11:51:23 +0200882#if defined(CONFIG_440GX) || \
883 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200884 defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
Stefan Roese8ac41e32008-03-11 15:05:26 +0100885 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200886 defined(CONFIG_405EX)
wdenkba56f622004-02-06 23:19:44 +0000887 /* Whack the M1 register */
888 mode_reg = 0x0;
889 mode_reg &= ~0x00000038;
890 if (sysinfo.freqOPB <= 50000000);
891 else if (sysinfo.freqOPB <= 66666667)
892 mode_reg |= EMAC_M1_OBCI_66;
893 else if (sysinfo.freqOPB <= 83333333)
894 mode_reg |= EMAC_M1_OBCI_83;
895 else if (sysinfo.freqOPB <= 100000000)
896 mode_reg |= EMAC_M1_OBCI_100;
897 else
898 mode_reg |= EMAC_M1_OBCI_GT100;
899
Stefan Roese2d834762007-10-23 14:03:17 +0200900 out_be32((void *)EMAC_M1 + hw_p->hw_addr, mode_reg);
Stefan Roese6e7fb6e2005-11-29 18:18:21 +0100901#endif /* defined(CONFIG_440GX) || defined(CONFIG_440SP) */
wdenkba56f622004-02-06 23:19:44 +0000902
903 /* wait for PHY to complete auto negotiation */
904 reg_short = 0;
905#ifndef CONFIG_CS8952_PHY
906 switch (devnum) {
907 case 0:
908 reg = CONFIG_PHY_ADDR;
909 break;
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200910#if defined (CONFIG_PHY1_ADDR)
wdenkba56f622004-02-06 23:19:44 +0000911 case 1:
912 reg = CONFIG_PHY1_ADDR;
913 break;
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200914#endif
Stefan Roese4c9e8552008-03-19 16:20:49 +0100915#if defined (CONFIG_PHY2_ADDR)
wdenkba56f622004-02-06 23:19:44 +0000916 case 2:
917 reg = CONFIG_PHY2_ADDR;
918 break;
Stefan Roese4c9e8552008-03-19 16:20:49 +0100919#endif
920#if defined (CONFIG_PHY3_ADDR)
wdenkba56f622004-02-06 23:19:44 +0000921 case 3:
922 reg = CONFIG_PHY3_ADDR;
923 break;
924#endif
925 default:
926 reg = CONFIG_PHY_ADDR;
927 break;
928 }
929
wdenk3c74e322004-02-22 23:46:08 +0000930 bis->bi_phynum[devnum] = reg;
931
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200932#if defined(CONFIG_PHY_RESET)
wdenka06752e2004-09-29 22:43:59 +0000933 /*
934 * Reset the phy, only if its the first time through
935 * otherwise, just check the speeds & feeds
936 */
937 if (hw_p->first_init == 0) {
Stefan Roeseec0c2ec2006-11-27 14:46:06 +0100938#if defined(CONFIG_M88E1111_PHY)
Stefan Roese887e2ec2006-09-07 11:51:23 +0200939 miiphy_write (dev->name, reg, 0x14, 0x0ce3);
940 miiphy_write (dev->name, reg, 0x18, 0x4101);
941 miiphy_write (dev->name, reg, 0x09, 0x0e00);
942 miiphy_write (dev->name, reg, 0x04, 0x01e1);
943#endif
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200944 miiphy_reset (dev->name, reg);
wdenkba56f622004-02-06 23:19:44 +0000945
Stefan Roese887e2ec2006-09-07 11:51:23 +0200946#if defined(CONFIG_440GX) || \
947 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200948 defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
Stefan Roese8ac41e32008-03-11 15:05:26 +0100949 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +0200950 defined(CONFIG_405EX)
Stefan Roese887e2ec2006-09-07 11:51:23 +0200951
wdenk0e6d7982004-03-14 00:07:33 +0000952#if defined(CONFIG_CIS8201_PHY)
wdenkfc1cfcd2004-04-25 15:41:35 +0000953 /*
Stefan Roese17f50f222005-08-04 17:09:16 +0200954 * Cicada 8201 PHY needs to have an extended register whacked
955 * for RGMII mode.
wdenkfc1cfcd2004-04-25 15:41:35 +0000956 */
Stefan Roese887e2ec2006-09-07 11:51:23 +0200957 if (((devnum == 2) || (devnum == 3)) && (4 == ethgroup)) {
Stefan Roeseb79316f2005-08-15 12:31:23 +0200958#if defined(CONFIG_CIS8201_SHORT_ETCH)
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200959 miiphy_write (dev->name, reg, 23, 0x1300);
Stefan Roeseb79316f2005-08-15 12:31:23 +0200960#else
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200961 miiphy_write (dev->name, reg, 23, 0x1000);
Stefan Roeseb79316f2005-08-15 12:31:23 +0200962#endif
Stefan Roese17f50f222005-08-04 17:09:16 +0200963 /*
964 * Vitesse VSC8201/Cicada CIS8201 errata:
965 * Interoperability problem with Intel 82547EI phys
966 * This work around (provided by Vitesse) changes
967 * the default timer convergence from 8ms to 12ms
968 */
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200969 miiphy_write (dev->name, reg, 0x1f, 0x2a30);
970 miiphy_write (dev->name, reg, 0x08, 0x0200);
971 miiphy_write (dev->name, reg, 0x1f, 0x52b5);
972 miiphy_write (dev->name, reg, 0x02, 0x0004);
973 miiphy_write (dev->name, reg, 0x01, 0x0671);
974 miiphy_write (dev->name, reg, 0x00, 0x8fae);
975 miiphy_write (dev->name, reg, 0x1f, 0x2a30);
976 miiphy_write (dev->name, reg, 0x08, 0x0000);
977 miiphy_write (dev->name, reg, 0x1f, 0x0000);
Stefan Roese17f50f222005-08-04 17:09:16 +0200978 /* end Vitesse/Cicada errata */
979 }
wdenk0e6d7982004-03-14 00:07:33 +0000980#endif
Stefan Roese5fb692c2007-01-18 10:25:34 +0100981
982#if defined(CONFIG_ET1011C_PHY)
983 /*
984 * Agere ET1011c PHY needs to have an extended register whacked
985 * for RGMII mode.
986 */
987 if (((devnum == 2) || (devnum ==3)) && (4 == ethgroup)) {
988 miiphy_read (dev->name, reg, 0x16, &reg_short);
989 reg_short &= ~(0x7);
990 reg_short |= 0x6; /* RGMII DLL Delay*/
991 miiphy_write (dev->name, reg, 0x16, reg_short);
992
993 miiphy_read (dev->name, reg, 0x17, &reg_short);
994 reg_short &= ~(0x40);
995 miiphy_write (dev->name, reg, 0x17, reg_short);
996
997 miiphy_write(dev->name, reg, 0x1c, 0x74f0);
998 }
999#endif
1000
wdenk855a4962004-03-14 18:23:55 +00001001#endif
wdenka06752e2004-09-29 22:43:59 +00001002 /* Start/Restart autonegotiation */
Marian Balakowicz63ff0042005-10-28 22:30:33 +02001003 phy_setup_aneg (dev->name, reg);
wdenka06752e2004-09-29 22:43:59 +00001004 udelay (1000);
1005 }
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001006#endif /* defined(CONFIG_PHY_RESET) */
wdenkba56f622004-02-06 23:19:44 +00001007
Marian Balakowicz63ff0042005-10-28 22:30:33 +02001008 miiphy_read (dev->name, reg, PHY_BMSR, &reg_short);
wdenkba56f622004-02-06 23:19:44 +00001009
1010 /*
wdenk0e6d7982004-03-14 00:07:33 +00001011 * Wait if PHY is capable of autonegotiation and autonegotiation is not complete
wdenkba56f622004-02-06 23:19:44 +00001012 */
1013 if ((reg_short & PHY_BMSR_AUTN_ABLE)
1014 && !(reg_short & PHY_BMSR_AUTN_COMP)) {
1015 puts ("Waiting for PHY auto negotiation to complete");
1016 i = 0;
1017 while (!(reg_short & PHY_BMSR_AUTN_COMP)) {
1018 /*
1019 * Timeout reached ?
1020 */
1021 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
1022 puts (" TIMEOUT !\n");
1023 break;
1024 }
1025
1026 if ((i++ % 1000) == 0) {
1027 putc ('.');
1028 }
1029 udelay (1000); /* 1 ms */
Marian Balakowicz63ff0042005-10-28 22:30:33 +02001030 miiphy_read (dev->name, reg, PHY_BMSR, &reg_short);
wdenkba56f622004-02-06 23:19:44 +00001031
1032 }
1033 puts (" done\n");
1034 udelay (500000); /* another 500 ms (results in faster booting) */
1035 }
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001036#endif /* #ifndef CONFIG_CS8952_PHY */
1037
Marian Balakowicz63ff0042005-10-28 22:30:33 +02001038 speed = miiphy_speed (dev->name, reg);
1039 duplex = miiphy_duplex (dev->name, reg);
wdenkba56f622004-02-06 23:19:44 +00001040
1041 if (hw_p->print_speed) {
1042 hw_p->print_speed = 0;
Stefan Roese5fb692c2007-01-18 10:25:34 +01001043 printf ("ENET Speed is %d Mbps - %s duplex connection (EMAC%d)\n",
1044 (int) speed, (duplex == HALF) ? "HALF" : "FULL",
1045 hw_p->devnum);
wdenkba56f622004-02-06 23:19:44 +00001046 }
1047
Stefan Roese8ac41e32008-03-11 15:05:26 +01001048#if defined(CONFIG_440) && \
1049 !defined(CONFIG_440SP) && !defined(CONFIG_440SPE) && \
1050 !defined(CONFIG_440EPX) && !defined(CONFIG_440GRX) && \
1051 !defined(CONFIG_460EX) && !defined(CONFIG_460GT)
Stefan Roese846b0dd2005-08-08 12:42:22 +02001052#if defined(CONFIG_440EP) || defined(CONFIG_440GR)
Stefan Roesec157d8e2005-08-01 16:41:48 +02001053 mfsdr(sdr_mfr, reg);
1054 if (speed == 100) {
1055 reg = (reg & ~SDR0_MFR_ZMII_MODE_MASK) | SDR0_MFR_ZMII_MODE_RMII_100M;
1056 } else {
1057 reg = (reg & ~SDR0_MFR_ZMII_MODE_MASK) | SDR0_MFR_ZMII_MODE_RMII_10M;
1058 }
1059 mtsdr(sdr_mfr, reg);
1060#endif
Stefan Roesec57c7982005-08-11 17:56:56 +02001061
wdenkba56f622004-02-06 23:19:44 +00001062 /* Set ZMII/RGMII speed according to the phy link speed */
Stefan Roeseff768cb2007-10-31 18:01:24 +01001063 reg = in_be32((void *)ZMII_SSR);
wdenk855a4962004-03-14 18:23:55 +00001064 if ( (speed == 100) || (speed == 1000) )
Stefan Roeseff768cb2007-10-31 18:01:24 +01001065 out_be32((void *)ZMII_SSR, reg | (ZMII_SSR_SP << ZMII_SSR_V (devnum)));
wdenkba56f622004-02-06 23:19:44 +00001066 else
Stefan Roeseff768cb2007-10-31 18:01:24 +01001067 out_be32((void *)ZMII_SSR, reg & (~(ZMII_SSR_SP << ZMII_SSR_V (devnum))));
wdenkba56f622004-02-06 23:19:44 +00001068
1069 if ((devnum == 2) || (devnum == 3)) {
1070 if (speed == 1000)
1071 reg = (RGMII_SSR_SP_1000MBPS << RGMII_SSR_V (devnum));
1072 else if (speed == 100)
1073 reg = (RGMII_SSR_SP_100MBPS << RGMII_SSR_V (devnum));
Stefan Roese887e2ec2006-09-07 11:51:23 +02001074 else if (speed == 10)
wdenkba56f622004-02-06 23:19:44 +00001075 reg = (RGMII_SSR_SP_10MBPS << RGMII_SSR_V (devnum));
Stefan Roese887e2ec2006-09-07 11:51:23 +02001076 else {
1077 printf("Error in RGMII Speed\n");
1078 return -1;
1079 }
Stefan Roeseff768cb2007-10-31 18:01:24 +01001080 out_be32((void *)RGMII_SSR, reg);
wdenkba56f622004-02-06 23:19:44 +00001081 }
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001082#endif /* defined(CONFIG_440) && !defined(CONFIG_440SP) */
wdenkba56f622004-02-06 23:19:44 +00001083
Stefan Roesedbbd1252007-10-05 17:10:59 +02001084#if defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roese8ac41e32008-03-11 15:05:26 +01001085 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +02001086 defined(CONFIG_405EX)
Stefan Roesee54ec0f2008-04-03 14:50:34 +02001087 if (devnum >= 2)
1088 rgmii_channel = devnum - 2;
1089 else
1090 rgmii_channel = devnum;
1091
Stefan Roese887e2ec2006-09-07 11:51:23 +02001092 if (speed == 1000)
Stefan Roesee54ec0f2008-04-03 14:50:34 +02001093 reg = (RGMII_SSR_SP_1000MBPS << RGMII_SSR_V(rgmii_channel));
Stefan Roese887e2ec2006-09-07 11:51:23 +02001094 else if (speed == 100)
Stefan Roesee54ec0f2008-04-03 14:50:34 +02001095 reg = (RGMII_SSR_SP_100MBPS << RGMII_SSR_V(rgmii_channel));
Stefan Roese887e2ec2006-09-07 11:51:23 +02001096 else if (speed == 10)
Stefan Roesee54ec0f2008-04-03 14:50:34 +02001097 reg = (RGMII_SSR_SP_10MBPS << RGMII_SSR_V(rgmii_channel));
Stefan Roese887e2ec2006-09-07 11:51:23 +02001098 else {
1099 printf("Error in RGMII Speed\n");
1100 return -1;
1101 }
Stefan Roese2d834762007-10-23 14:03:17 +02001102 out_be32((void *)RGMII_SSR, reg);
Stefan Roese8ac41e32008-03-11 15:05:26 +01001103#if defined(CONFIG_460GT)
1104 if ((devnum == 2) || (devnum == 3))
1105 out_be32((void *)RGMII_SSR + RGMII1_BASE_OFFSET, reg);
1106#endif
Stefan Roese887e2ec2006-09-07 11:51:23 +02001107#endif
1108
wdenkba56f622004-02-06 23:19:44 +00001109 /* set the Mal configuration reg */
Stefan Roese887e2ec2006-09-07 11:51:23 +02001110#if defined(CONFIG_440GX) || \
1111 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +02001112 defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
Stefan Roese8ac41e32008-03-11 15:05:26 +01001113 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +02001114 defined(CONFIG_405EX)
Stefan Roese17f50f222005-08-04 17:09:16 +02001115 mtdcr (malmcr, MAL_CR_PLBB | MAL_CR_OPBBL | MAL_CR_LEA |
1116 MAL_CR_PLBLT_DEFAULT | MAL_CR_EOPIE | 0x00330000);
1117#else
1118 mtdcr (malmcr, MAL_CR_PLBB | MAL_CR_OPBBL | MAL_CR_LEA | MAL_CR_PLBLT_DEFAULT);
wdenkba56f622004-02-06 23:19:44 +00001119 /* Errata 1.12: MAL_1 -- Disable MAL bursting */
Stefan Roese17f50f222005-08-04 17:09:16 +02001120 if (get_pvr() == PVR_440GP_RB) {
1121 mtdcr (malmcr, mfdcr(malmcr) & ~MAL_CR_PLBB);
1122 }
1123#endif
wdenkba56f622004-02-06 23:19:44 +00001124
wdenkba56f622004-02-06 23:19:44 +00001125 /*
1126 * Malloc MAL buffer desciptors, make sure they are
1127 * aligned on cache line boundary size
1128 * (401/403/IOP480 = 16, 405 = 32)
1129 * and doesn't cross cache block boundaries.
1130 */
Stefan Roeseff768cb2007-10-31 18:01:24 +01001131 if (hw_p->first_init == 0) {
1132 debug("*** Allocating descriptor memory ***\n");
wdenkba56f622004-02-06 23:19:44 +00001133
Stefan Roeseff768cb2007-10-31 18:01:24 +01001134 bd_cached = (u32)malloc_aligned(MAL_ALLOC_SIZE, 4096);
1135 if (!bd_cached) {
Stefan Roeseb0021442008-07-10 09:58:06 +02001136 printf("%s: Error allocating MAL descriptor buffers!\n", __func__);
Stefan Roeseff768cb2007-10-31 18:01:24 +01001137 return -1;
1138 }
Stefan Roeseb79316f2005-08-15 12:31:23 +02001139
Stefan Roeseff768cb2007-10-31 18:01:24 +01001140#ifdef CONFIG_4xx_DCACHE
Matthias Fuchsba79fde2007-12-14 11:19:56 +01001141 flush_dcache_range(bd_cached, bd_cached + MAL_ALLOC_SIZE);
Anatolij Gustschin4fae35a2008-02-25 20:54:04 +01001142 if (!last_used_ea)
Anatolij Gustschin5e3dca52008-04-17 18:18:00 +02001143#if defined(CFG_MEM_TOP_HIDE)
1144 bd_uncached = bis->bi_memsize + CFG_MEM_TOP_HIDE;
1145#else
Anatolij Gustschin4fae35a2008-02-25 20:54:04 +01001146 bd_uncached = bis->bi_memsize;
Anatolij Gustschin5e3dca52008-04-17 18:18:00 +02001147#endif
Anatolij Gustschin4fae35a2008-02-25 20:54:04 +01001148 else
1149 bd_uncached = last_used_ea + MAL_ALLOC_SIZE;
1150
1151 last_used_ea = bd_uncached;
Stefan Roeseff768cb2007-10-31 18:01:24 +01001152 program_tlb(bd_cached, bd_uncached, MAL_ALLOC_SIZE,
1153 TLB_WORD2_I_ENABLE);
1154#else
1155 bd_uncached = bd_cached;
1156#endif
1157 hw_p->tx_phys = bd_cached;
1158 hw_p->rx_phys = bd_cached + MAL_TX_DESC_SIZE;
1159 hw_p->tx = (mal_desc_t *)(bd_uncached);
1160 hw_p->rx = (mal_desc_t *)(bd_uncached + MAL_TX_DESC_SIZE);
1161 debug("hw_p->tx=%08x, hw_p->rx=%08x\n", hw_p->tx, hw_p->rx);
wdenkba56f622004-02-06 23:19:44 +00001162 }
1163
1164 for (i = 0; i < NUM_TX_BUFF; i++) {
1165 hw_p->tx[i].ctrl = 0;
1166 hw_p->tx[i].data_len = 0;
Stefan Roeseff768cb2007-10-31 18:01:24 +01001167 if (hw_p->first_init == 0)
1168 hw_p->txbuf_ptr = malloc_aligned(MAL_ALLOC_SIZE,
1169 L1_CACHE_BYTES);
wdenkba56f622004-02-06 23:19:44 +00001170 hw_p->tx[i].data_ptr = hw_p->txbuf_ptr;
1171 if ((NUM_TX_BUFF - 1) == i)
1172 hw_p->tx[i].ctrl |= MAL_TX_CTRL_WRAP;
1173 hw_p->tx_run[i] = -1;
Stefan Roeseff768cb2007-10-31 18:01:24 +01001174 debug("TX_BUFF %d @ 0x%08lx\n", i, (u32)hw_p->tx[i].data_ptr);
wdenkba56f622004-02-06 23:19:44 +00001175 }
1176
1177 for (i = 0; i < NUM_RX_BUFF; i++) {
1178 hw_p->rx[i].ctrl = 0;
1179 hw_p->rx[i].data_len = 0;
Stefan Roeseff768cb2007-10-31 18:01:24 +01001180 hw_p->rx[i].data_ptr = (char *)NetRxPackets[i];
wdenkba56f622004-02-06 23:19:44 +00001181 if ((NUM_RX_BUFF - 1) == i)
1182 hw_p->rx[i].ctrl |= MAL_RX_CTRL_WRAP;
1183 hw_p->rx[i].ctrl |= MAL_RX_CTRL_EMPTY | MAL_RX_CTRL_INTR;
1184 hw_p->rx_ready[i] = -1;
Stefan Roeseff768cb2007-10-31 18:01:24 +01001185 debug("RX_BUFF %d @ 0x%08lx\n", i, (u32)hw_p->rx[i].data_ptr);
wdenkba56f622004-02-06 23:19:44 +00001186 }
1187
1188 reg = 0x00000000;
1189
1190 reg |= dev->enetaddr[0]; /* set high address */
1191 reg = reg << 8;
1192 reg |= dev->enetaddr[1];
1193
Stefan Roese2d834762007-10-23 14:03:17 +02001194 out_be32((void *)EMAC_IAH + hw_p->hw_addr, reg);
wdenkba56f622004-02-06 23:19:44 +00001195
1196 reg = 0x00000000;
1197 reg |= dev->enetaddr[2]; /* set low address */
1198 reg = reg << 8;
1199 reg |= dev->enetaddr[3];
1200 reg = reg << 8;
1201 reg |= dev->enetaddr[4];
1202 reg = reg << 8;
1203 reg |= dev->enetaddr[5];
1204
Stefan Roese2d834762007-10-23 14:03:17 +02001205 out_be32((void *)EMAC_IAL + hw_p->hw_addr, reg);
wdenkba56f622004-02-06 23:19:44 +00001206
1207 switch (devnum) {
1208 case 1:
1209 /* setup MAL tx & rx channel pointers */
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001210#if defined (CONFIG_405EP) || defined (CONFIG_440EP) || defined (CONFIG_440GR)
Stefan Roeseff768cb2007-10-31 18:01:24 +01001211 mtdcr (maltxctp2r, hw_p->tx_phys);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001212#else
Stefan Roeseff768cb2007-10-31 18:01:24 +01001213 mtdcr (maltxctp1r, hw_p->tx_phys);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001214#endif
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001215#if defined(CONFIG_440)
Stefan Roesec157d8e2005-08-01 16:41:48 +02001216 mtdcr (maltxbattr, 0x0);
wdenkba56f622004-02-06 23:19:44 +00001217 mtdcr (malrxbattr, 0x0);
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001218#endif
Stefan Roese8ac41e32008-03-11 15:05:26 +01001219
1220#if defined(CONFIG_460EX) || defined(CONFIG_460GT)
Stefan Roese4c9e8552008-03-19 16:20:49 +01001221 mtdcr (malrxctp8r, hw_p->rx_phys);
Stefan Roese8ac41e32008-03-11 15:05:26 +01001222 /* set RX buffer size */
1223 mtdcr (malrcbs8, ENET_MAX_MTU_ALIGNED / 16);
1224#else
Stefan Roeseff768cb2007-10-31 18:01:24 +01001225 mtdcr (malrxctp1r, hw_p->rx_phys);
wdenkba56f622004-02-06 23:19:44 +00001226 /* set RX buffer size */
1227 mtdcr (malrcbs1, ENET_MAX_MTU_ALIGNED / 16);
Stefan Roese8ac41e32008-03-11 15:05:26 +01001228#endif
wdenkba56f622004-02-06 23:19:44 +00001229 break;
Stefan Roese846b0dd2005-08-08 12:42:22 +02001230#if defined (CONFIG_440GX)
wdenkba56f622004-02-06 23:19:44 +00001231 case 2:
1232 /* setup MAL tx & rx channel pointers */
1233 mtdcr (maltxbattr, 0x0);
wdenkba56f622004-02-06 23:19:44 +00001234 mtdcr (malrxbattr, 0x0);
Stefan Roeseff768cb2007-10-31 18:01:24 +01001235 mtdcr (maltxctp2r, hw_p->tx_phys);
1236 mtdcr (malrxctp2r, hw_p->rx_phys);
wdenkba56f622004-02-06 23:19:44 +00001237 /* set RX buffer size */
1238 mtdcr (malrcbs2, ENET_MAX_MTU_ALIGNED / 16);
1239 break;
1240 case 3:
1241 /* setup MAL tx & rx channel pointers */
1242 mtdcr (maltxbattr, 0x0);
Stefan Roeseff768cb2007-10-31 18:01:24 +01001243 mtdcr (maltxctp3r, hw_p->tx_phys);
wdenkba56f622004-02-06 23:19:44 +00001244 mtdcr (malrxbattr, 0x0);
Stefan Roeseff768cb2007-10-31 18:01:24 +01001245 mtdcr (malrxctp3r, hw_p->rx_phys);
wdenkba56f622004-02-06 23:19:44 +00001246 /* set RX buffer size */
1247 mtdcr (malrcbs3, ENET_MAX_MTU_ALIGNED / 16);
1248 break;
Stefan Roesec57c7982005-08-11 17:56:56 +02001249#endif /* CONFIG_440GX */
Stefan Roese4c9e8552008-03-19 16:20:49 +01001250#if defined (CONFIG_460GT)
1251 case 2:
1252 /* setup MAL tx & rx channel pointers */
1253 mtdcr (maltxbattr, 0x0);
1254 mtdcr (malrxbattr, 0x0);
1255 mtdcr (maltxctp2r, hw_p->tx_phys);
1256 mtdcr (malrxctp16r, hw_p->rx_phys);
1257 /* set RX buffer size */
1258 mtdcr (malrcbs16, ENET_MAX_MTU_ALIGNED / 16);
1259 break;
1260 case 3:
1261 /* setup MAL tx & rx channel pointers */
1262 mtdcr (maltxbattr, 0x0);
1263 mtdcr (malrxbattr, 0x0);
1264 mtdcr (maltxctp3r, hw_p->tx_phys);
1265 mtdcr (malrxctp24r, hw_p->rx_phys);
1266 /* set RX buffer size */
1267 mtdcr (malrcbs24, ENET_MAX_MTU_ALIGNED / 16);
1268 break;
1269#endif /* CONFIG_460GT */
wdenkba56f622004-02-06 23:19:44 +00001270 case 0:
1271 default:
1272 /* setup MAL tx & rx channel pointers */
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001273#if defined(CONFIG_440)
wdenkba56f622004-02-06 23:19:44 +00001274 mtdcr (maltxbattr, 0x0);
wdenkba56f622004-02-06 23:19:44 +00001275 mtdcr (malrxbattr, 0x0);
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001276#endif
Stefan Roeseff768cb2007-10-31 18:01:24 +01001277 mtdcr (maltxctp0r, hw_p->tx_phys);
1278 mtdcr (malrxctp0r, hw_p->rx_phys);
wdenkba56f622004-02-06 23:19:44 +00001279 /* set RX buffer size */
1280 mtdcr (malrcbs0, ENET_MAX_MTU_ALIGNED / 16);
1281 break;
1282 }
1283
1284 /* Enable MAL transmit and receive channels */
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001285#if defined(CONFIG_405EP) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
Stefan Roesec157d8e2005-08-01 16:41:48 +02001286 mtdcr (maltxcasr, (MAL_TXRX_CASR >> (hw_p->devnum*2)));
1287#else
wdenkba56f622004-02-06 23:19:44 +00001288 mtdcr (maltxcasr, (MAL_TXRX_CASR >> hw_p->devnum));
Stefan Roesec157d8e2005-08-01 16:41:48 +02001289#endif
wdenkba56f622004-02-06 23:19:44 +00001290 mtdcr (malrxcasr, (MAL_TXRX_CASR >> hw_p->devnum));
1291
1292 /* set transmit enable & receive enable */
Stefan Roese2d834762007-10-23 14:03:17 +02001293 out_be32((void *)EMAC_M0 + hw_p->hw_addr, EMAC_M0_TXE | EMAC_M0_RXE);
wdenkba56f622004-02-06 23:19:44 +00001294
Stefan Roese2d834762007-10-23 14:03:17 +02001295 mode_reg = in_be32((void *)EMAC_M1 + hw_p->hw_addr);
Stefan Roese76957cb2008-03-01 12:11:40 +01001296
1297 /* set rx-/tx-fifo size */
1298 mode_reg = (mode_reg & ~EMAC_MR1_FIFO_MASK) | EMAC_MR1_FIFO_SIZE;
wdenkba56f622004-02-06 23:19:44 +00001299
1300 /* set speed */
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001301 if (speed == _1000BASET) {
Stefan Roese738815c2007-10-02 11:44:46 +02001302#if defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
1303 defined(CONFIG_440SP) || defined(CONFIG_440SPE)
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001304 unsigned long pfc1;
Stefan Roese887e2ec2006-09-07 11:51:23 +02001305
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001306 mfsdr (sdr_pfc1, pfc1);
1307 pfc1 |= SDR0_PFC1_EM_1000;
1308 mtsdr (sdr_pfc1, pfc1);
1309#endif
wdenk855a4962004-03-14 18:23:55 +00001310 mode_reg = mode_reg | EMAC_M1_MF_1000MBPS | EMAC_M1_IST;
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001311 } else if (speed == _100BASET)
wdenkba56f622004-02-06 23:19:44 +00001312 mode_reg = mode_reg | EMAC_M1_MF_100MBPS | EMAC_M1_IST;
1313 else
1314 mode_reg = mode_reg & ~0x00C00000; /* 10 MBPS */
1315 if (duplex == FULL)
1316 mode_reg = mode_reg | 0x80000000 | EMAC_M1_IST;
1317
Stefan Roese2d834762007-10-23 14:03:17 +02001318 out_be32((void *)EMAC_M1 + hw_p->hw_addr, mode_reg);
wdenkba56f622004-02-06 23:19:44 +00001319
1320 /* Enable broadcast and indvidual address */
1321 /* TBS: enabling runts as some misbehaved nics will send runts */
Stefan Roese2d834762007-10-23 14:03:17 +02001322 out_be32((void *)EMAC_RXM + hw_p->hw_addr, EMAC_RMR_BAE | EMAC_RMR_IAE);
wdenkba56f622004-02-06 23:19:44 +00001323
1324 /* we probably need to set the tx mode1 reg? maybe at tx time */
1325
1326 /* set transmit request threshold register */
Stefan Roese2d834762007-10-23 14:03:17 +02001327 out_be32((void *)EMAC_TRTR + hw_p->hw_addr, 0x18000000); /* 256 byte threshold */
wdenkba56f622004-02-06 23:19:44 +00001328
Wolfgang Denk265817c2005-09-25 00:53:22 +02001329 /* set receive low/high water mark register */
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001330#if defined(CONFIG_440)
Marian Balakowicz6c5879f2006-06-30 16:30:46 +02001331 /* 440s has a 64 byte burst length */
Stefan Roese2d834762007-10-23 14:03:17 +02001332 out_be32((void *)EMAC_RX_HI_LO_WMARK + hw_p->hw_addr, 0x80009000);
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001333#else
1334 /* 405s have a 16 byte burst length */
Stefan Roese2d834762007-10-23 14:03:17 +02001335 out_be32((void *)EMAC_RX_HI_LO_WMARK + hw_p->hw_addr, 0x0f002000);
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001336#endif /* defined(CONFIG_440) */
Stefan Roese2d834762007-10-23 14:03:17 +02001337 out_be32((void *)EMAC_TXM1 + hw_p->hw_addr, 0xf8640000);
wdenkba56f622004-02-06 23:19:44 +00001338
1339 /* Set fifo limit entry in tx mode 0 */
Stefan Roese2d834762007-10-23 14:03:17 +02001340 out_be32((void *)EMAC_TXM0 + hw_p->hw_addr, 0x00000003);
wdenkba56f622004-02-06 23:19:44 +00001341 /* Frame gap set */
Stefan Roese2d834762007-10-23 14:03:17 +02001342 out_be32((void *)EMAC_I_FRAME_GAP_REG + hw_p->hw_addr, 0x00000008);
wdenkba56f622004-02-06 23:19:44 +00001343
1344 /* Set EMAC IER */
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001345 hw_p->emac_ier = EMAC_ISR_PTLE | EMAC_ISR_BFCS | EMAC_ISR_ORE | EMAC_ISR_IRE;
wdenkba56f622004-02-06 23:19:44 +00001346 if (speed == _100BASET)
1347 hw_p->emac_ier = hw_p->emac_ier | EMAC_ISR_SYE;
1348
Stefan Roese2d834762007-10-23 14:03:17 +02001349 out_be32((void *)EMAC_ISR + hw_p->hw_addr, 0xffffffff); /* clear pending interrupts */
1350 out_be32((void *)EMAC_IER + hw_p->hw_addr, hw_p->emac_ier);
wdenkba56f622004-02-06 23:19:44 +00001351
1352 if (hw_p->first_init == 0) {
1353 /*
1354 * Connect interrupt service routines
1355 */
Stefan Roesedbbd1252007-10-05 17:10:59 +02001356 irq_install_handler(ETH_IRQ_NUM(hw_p->devnum),
1357 (interrupt_handler_t *) enetInt, dev);
wdenkba56f622004-02-06 23:19:44 +00001358 }
wdenkba56f622004-02-06 23:19:44 +00001359
1360 mtmsr (msr); /* enable interrupts again */
1361
1362 hw_p->bis = bis;
1363 hw_p->first_init = 1;
1364
Stefan Roese802b7692008-01-08 18:39:30 +01001365 return 0;
wdenkba56f622004-02-06 23:19:44 +00001366}
1367
1368
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001369static int ppc_4xx_eth_send (struct eth_device *dev, volatile void *ptr,
wdenkba56f622004-02-06 23:19:44 +00001370 int len)
1371{
1372 struct enet_frame *ef_ptr;
1373 ulong time_start, time_now;
1374 unsigned long temp_txm0;
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001375 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenkba56f622004-02-06 23:19:44 +00001376
1377 ef_ptr = (struct enet_frame *) ptr;
1378
1379 /*-----------------------------------------------------------------------+
1380 * Copy in our address into the frame.
1381 *-----------------------------------------------------------------------*/
1382 (void) memcpy (ef_ptr->source_addr, dev->enetaddr, ENET_ADDR_LENGTH);
1383
1384 /*-----------------------------------------------------------------------+
1385 * If frame is too long or too short, modify length.
1386 *-----------------------------------------------------------------------*/
1387 /* TBS: where does the fragment go???? */
1388 if (len > ENET_MAX_MTU)
1389 len = ENET_MAX_MTU;
1390
1391 /* memcpy ((void *) &tx_buff[tx_slot], (const void *) ptr, len); */
1392 memcpy ((void *) hw_p->txbuf_ptr, (const void *) ptr, len);
Matthias Fuchsba79fde2007-12-14 11:19:56 +01001393 flush_dcache_range((u32)hw_p->txbuf_ptr, (u32)hw_p->txbuf_ptr + len);
wdenkba56f622004-02-06 23:19:44 +00001394
1395 /*-----------------------------------------------------------------------+
1396 * set TX Buffer busy, and send it
1397 *-----------------------------------------------------------------------*/
1398 hw_p->tx[hw_p->tx_slot].ctrl = (MAL_TX_CTRL_LAST |
1399 EMAC_TX_CTRL_GFCS | EMAC_TX_CTRL_GP) &
1400 ~(EMAC_TX_CTRL_ISA | EMAC_TX_CTRL_RSA);
1401 if ((NUM_TX_BUFF - 1) == hw_p->tx_slot)
1402 hw_p->tx[hw_p->tx_slot].ctrl |= MAL_TX_CTRL_WRAP;
1403
1404 hw_p->tx[hw_p->tx_slot].data_len = (short) len;
1405 hw_p->tx[hw_p->tx_slot].ctrl |= MAL_TX_CTRL_READY;
1406
Stefan Roese8ac41e32008-03-11 15:05:26 +01001407 sync();
wdenkba56f622004-02-06 23:19:44 +00001408
Stefan Roese2d834762007-10-23 14:03:17 +02001409 out_be32((void *)EMAC_TXM0 + hw_p->hw_addr,
1410 in_be32((void *)EMAC_TXM0 + hw_p->hw_addr) | EMAC_TXM0_GNP0);
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001411#ifdef INFO_4XX_ENET
wdenkba56f622004-02-06 23:19:44 +00001412 hw_p->stats.pkts_tx++;
1413#endif
1414
1415 /*-----------------------------------------------------------------------+
1416 * poll unitl the packet is sent and then make sure it is OK
1417 *-----------------------------------------------------------------------*/
1418 time_start = get_timer (0);
1419 while (1) {
Stefan Roese2d834762007-10-23 14:03:17 +02001420 temp_txm0 = in_be32((void *)EMAC_TXM0 + hw_p->hw_addr);
wdenkba56f622004-02-06 23:19:44 +00001421 /* loop until either TINT turns on or 3 seconds elapse */
1422 if ((temp_txm0 & EMAC_TXM0_GNP0) != 0) {
1423 /* transmit is done, so now check for errors
1424 * If there is an error, an interrupt should
1425 * happen when we return
1426 */
1427 time_now = get_timer (0);
1428 if ((time_now - time_start) > 3000) {
1429 return (-1);
1430 }
1431 } else {
1432 return (len);
1433 }
1434 }
1435}
1436
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001437
Stefan Roesedbbd1252007-10-05 17:10:59 +02001438#if defined (CONFIG_440) || defined(CONFIG_405EX)
wdenkba56f622004-02-06 23:19:44 +00001439
Marian Balakowicz6c5879f2006-06-30 16:30:46 +02001440#if defined(CONFIG_440SP) || defined(CONFIG_440SPE)
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001441/*
1442 * Hack: On 440SP all enet irq sources are located on UIC1
1443 * Needs some cleanup. --sr
1444 */
1445#define UIC0MSR uic1msr
1446#define UIC0SR uic1sr
Stefan Roese8ac41e32008-03-11 15:05:26 +01001447#define UIC1MSR uic1msr
1448#define UIC1SR uic1sr
1449#elif defined(CONFIG_460EX) || defined(CONFIG_460GT)
1450/*
1451 * Hack: On 460EX/GT all enet irq sources are located on UIC2
1452 * Needs some cleanup. --ag
1453 */
1454#define UIC0MSR uic2msr
1455#define UIC0SR uic2sr
1456#define UIC1MSR uic2msr
1457#define UIC1SR uic2sr
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001458#else
1459#define UIC0MSR uic0msr
1460#define UIC0SR uic0sr
Stefan Roese8ac41e32008-03-11 15:05:26 +01001461#define UIC1MSR uic1msr
1462#define UIC1SR uic1sr
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001463#endif
1464
Stefan Roesedbbd1252007-10-05 17:10:59 +02001465#if defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
1466 defined(CONFIG_405EX)
Stefan Roese887e2ec2006-09-07 11:51:23 +02001467#define UICMSR_ETHX uic0msr
1468#define UICSR_ETHX uic0sr
Stefan Roese8ac41e32008-03-11 15:05:26 +01001469#elif defined(CONFIG_460EX) || defined(CONFIG_460GT)
1470#define UICMSR_ETHX uic2msr
1471#define UICSR_ETHX uic2sr
Stefan Roese887e2ec2006-09-07 11:51:23 +02001472#else
1473#define UICMSR_ETHX uic1msr
1474#define UICSR_ETHX uic1sr
1475#endif
1476
wdenkba56f622004-02-06 23:19:44 +00001477int enetInt (struct eth_device *dev)
1478{
1479 int serviced;
1480 int rc = -1; /* default to not us */
1481 unsigned long mal_isr;
1482 unsigned long emac_isr = 0;
1483 unsigned long mal_rx_eob;
1484 unsigned long my_uic0msr, my_uic1msr;
Stefan Roese887e2ec2006-09-07 11:51:23 +02001485 unsigned long my_uicmsr_ethx;
wdenkba56f622004-02-06 23:19:44 +00001486
Stefan Roese846b0dd2005-08-08 12:42:22 +02001487#if defined(CONFIG_440GX)
wdenkba56f622004-02-06 23:19:44 +00001488 unsigned long my_uic2msr;
1489#endif
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001490 EMAC_4XX_HW_PST hw_p;
wdenkba56f622004-02-06 23:19:44 +00001491
1492 /*
1493 * Because the mal is generic, we need to get the current
1494 * eth device
1495 */
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001496#if defined(CONFIG_NET_MULTI)
1497 dev = eth_get_dev();
1498#else
1499 dev = emac0_dev;
1500#endif
wdenkba56f622004-02-06 23:19:44 +00001501
1502 hw_p = dev->priv;
1503
wdenkba56f622004-02-06 23:19:44 +00001504 /* enter loop that stays in interrupt code until nothing to service */
1505 do {
1506 serviced = 0;
1507
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001508 my_uic0msr = mfdcr (UIC0MSR);
Stefan Roese8ac41e32008-03-11 15:05:26 +01001509 my_uic1msr = mfdcr (UIC1MSR);
Stefan Roese846b0dd2005-08-08 12:42:22 +02001510#if defined(CONFIG_440GX)
wdenkba56f622004-02-06 23:19:44 +00001511 my_uic2msr = mfdcr (uic2msr);
1512#endif
Stefan Roese887e2ec2006-09-07 11:51:23 +02001513 my_uicmsr_ethx = mfdcr (UICMSR_ETHX);
1514
wdenkba56f622004-02-06 23:19:44 +00001515 if (!(my_uic0msr & (UIC_MRE | UIC_MTE))
Stefan Roese887e2ec2006-09-07 11:51:23 +02001516 && !(my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))
1517 && !(my_uicmsr_ethx & (UIC_ETH0 | UIC_ETH1))) {
wdenkba56f622004-02-06 23:19:44 +00001518 /* not for us */
1519 return (rc);
1520 }
Stefan Roese846b0dd2005-08-08 12:42:22 +02001521#if defined (CONFIG_440GX)
wdenkba56f622004-02-06 23:19:44 +00001522 if (!(my_uic0msr & (UIC_MRE | UIC_MTE))
1523 && !(my_uic2msr & (UIC_ETH2 | UIC_ETH3))) {
1524 /* not for us */
1525 return (rc);
1526 }
1527#endif
1528 /* get and clear controller status interrupts */
1529 /* look at Mal and EMAC interrupts */
1530 if ((my_uic0msr & (UIC_MRE | UIC_MTE))
1531 || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
1532 /* we have a MAL interrupt */
1533 mal_isr = mfdcr (malesr);
1534 /* look for mal error */
1535 if (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE)) {
Stefan Roese887e2ec2006-09-07 11:51:23 +02001536 mal_err (dev, mal_isr, my_uic1msr, MAL_UIC_DEF, MAL_UIC_ERR);
wdenkba56f622004-02-06 23:19:44 +00001537 serviced = 1;
1538 rc = 0;
1539 }
1540 }
1541
1542 /* port by port dispatch of emac interrupts */
1543 if (hw_p->devnum == 0) {
Stefan Roese887e2ec2006-09-07 11:51:23 +02001544 if (UIC_ETH0 & my_uicmsr_ethx) { /* look for EMAC errors */
Stefan Roese2d834762007-10-23 14:03:17 +02001545 emac_isr = in_be32((void *)EMAC_ISR + hw_p->hw_addr);
wdenkba56f622004-02-06 23:19:44 +00001546 if ((hw_p->emac_ier & emac_isr) != 0) {
1547 emac_err (dev, emac_isr);
1548 serviced = 1;
1549 rc = 0;
1550 }
1551 }
1552 if ((hw_p->emac_ier & emac_isr)
1553 || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001554 mtdcr (UIC0SR, UIC_MRE | UIC_MTE); /* Clear */
Stefan Roese8ac41e32008-03-11 15:05:26 +01001555 mtdcr (UIC1SR, UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
Stefan Roese887e2ec2006-09-07 11:51:23 +02001556 mtdcr (UICSR_ETHX, UIC_ETH0); /* Clear */
wdenkba56f622004-02-06 23:19:44 +00001557 return (rc); /* we had errors so get out */
1558 }
1559 }
1560
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001561#if !defined(CONFIG_440SP)
wdenkba56f622004-02-06 23:19:44 +00001562 if (hw_p->devnum == 1) {
Stefan Roese887e2ec2006-09-07 11:51:23 +02001563 if (UIC_ETH1 & my_uicmsr_ethx) { /* look for EMAC errors */
Stefan Roese2d834762007-10-23 14:03:17 +02001564 emac_isr = in_be32((void *)EMAC_ISR + hw_p->hw_addr);
wdenkba56f622004-02-06 23:19:44 +00001565 if ((hw_p->emac_ier & emac_isr) != 0) {
1566 emac_err (dev, emac_isr);
1567 serviced = 1;
1568 rc = 0;
1569 }
1570 }
1571 if ((hw_p->emac_ier & emac_isr)
1572 || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001573 mtdcr (UIC0SR, UIC_MRE | UIC_MTE); /* Clear */
Stefan Roese8ac41e32008-03-11 15:05:26 +01001574 mtdcr (UIC1SR, UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
Stefan Roese887e2ec2006-09-07 11:51:23 +02001575 mtdcr (UICSR_ETHX, UIC_ETH1); /* Clear */
wdenkba56f622004-02-06 23:19:44 +00001576 return (rc); /* we had errors so get out */
1577 }
1578 }
Stefan Roese846b0dd2005-08-08 12:42:22 +02001579#if defined (CONFIG_440GX)
wdenkba56f622004-02-06 23:19:44 +00001580 if (hw_p->devnum == 2) {
1581 if (UIC_ETH2 & my_uic2msr) { /* look for EMAC errors */
Stefan Roese2d834762007-10-23 14:03:17 +02001582 emac_isr = in_be32((void *)EMAC_ISR + hw_p->hw_addr);
wdenkba56f622004-02-06 23:19:44 +00001583 if ((hw_p->emac_ier & emac_isr) != 0) {
1584 emac_err (dev, emac_isr);
1585 serviced = 1;
1586 rc = 0;
1587 }
1588 }
1589 if ((hw_p->emac_ier & emac_isr)
1590 || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001591 mtdcr (UIC0SR, UIC_MRE | UIC_MTE); /* Clear */
Stefan Roese8ac41e32008-03-11 15:05:26 +01001592 mtdcr (UIC1SR, UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
wdenkba56f622004-02-06 23:19:44 +00001593 mtdcr (uic2sr, UIC_ETH2);
1594 return (rc); /* we had errors so get out */
1595 }
1596 }
1597
1598 if (hw_p->devnum == 3) {
1599 if (UIC_ETH3 & my_uic2msr) { /* look for EMAC errors */
Stefan Roese2d834762007-10-23 14:03:17 +02001600 emac_isr = in_be32((void *)EMAC_ISR + hw_p->hw_addr);
wdenkba56f622004-02-06 23:19:44 +00001601 if ((hw_p->emac_ier & emac_isr) != 0) {
1602 emac_err (dev, emac_isr);
1603 serviced = 1;
1604 rc = 0;
1605 }
1606 }
1607 if ((hw_p->emac_ier & emac_isr)
1608 || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001609 mtdcr (UIC0SR, UIC_MRE | UIC_MTE); /* Clear */
Stefan Roese8ac41e32008-03-11 15:05:26 +01001610 mtdcr (UIC1SR, UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
wdenkba56f622004-02-06 23:19:44 +00001611 mtdcr (uic2sr, UIC_ETH3);
1612 return (rc); /* we had errors so get out */
1613 }
1614 }
Stefan Roese846b0dd2005-08-08 12:42:22 +02001615#endif /* CONFIG_440GX */
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001616#endif /* !CONFIG_440SP */
1617
wdenkba56f622004-02-06 23:19:44 +00001618 /* handle MAX TX EOB interrupt from a tx */
1619 if (my_uic0msr & UIC_MTE) {
1620 mal_rx_eob = mfdcr (maltxeobisr);
1621 mtdcr (maltxeobisr, mal_rx_eob);
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001622 mtdcr (UIC0SR, UIC_MTE);
wdenkba56f622004-02-06 23:19:44 +00001623 }
1624 /* handle MAL RX EOB interupt from a receive */
wdenkfc1cfcd2004-04-25 15:41:35 +00001625 /* check for EOB on valid channels */
wdenkba56f622004-02-06 23:19:44 +00001626 if (my_uic0msr & UIC_MRE) {
1627 mal_rx_eob = mfdcr (malrxeobisr);
Stefan Roese8ac41e32008-03-11 15:05:26 +01001628 if ((mal_rx_eob &
1629 (0x80000000 >> (hw_p->devnum * MAL_RX_CHAN_MUL)))
1630 != 0) { /* call emac routine for channel x */
wdenkba56f622004-02-06 23:19:44 +00001631 /* clear EOB
1632 mtdcr(malrxeobisr, mal_rx_eob); */
1633 enet_rcv (dev, emac_isr);
1634 /* indicate that we serviced an interrupt */
1635 serviced = 1;
1636 rc = 0;
1637 }
1638 }
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001639
1640 mtdcr (UIC0SR, UIC_MRE); /* Clear */
Stefan Roese8ac41e32008-03-11 15:05:26 +01001641 mtdcr (UIC1SR, UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
wdenkba56f622004-02-06 23:19:44 +00001642 switch (hw_p->devnum) {
1643 case 0:
Stefan Roese887e2ec2006-09-07 11:51:23 +02001644 mtdcr (UICSR_ETHX, UIC_ETH0);
wdenkba56f622004-02-06 23:19:44 +00001645 break;
1646 case 1:
Stefan Roese887e2ec2006-09-07 11:51:23 +02001647 mtdcr (UICSR_ETHX, UIC_ETH1);
wdenkba56f622004-02-06 23:19:44 +00001648 break;
Stefan Roese846b0dd2005-08-08 12:42:22 +02001649#if defined (CONFIG_440GX)
wdenkba56f622004-02-06 23:19:44 +00001650 case 2:
1651 mtdcr (uic2sr, UIC_ETH2);
1652 break;
1653 case 3:
1654 mtdcr (uic2sr, UIC_ETH3);
1655 break;
Stefan Roese846b0dd2005-08-08 12:42:22 +02001656#endif /* CONFIG_440GX */
wdenkba56f622004-02-06 23:19:44 +00001657 default:
1658 break;
1659 }
1660 } while (serviced);
1661
1662 return (rc);
1663}
1664
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001665#else /* CONFIG_440 */
1666
1667int enetInt (struct eth_device *dev)
1668{
1669 int serviced;
1670 int rc = -1; /* default to not us */
1671 unsigned long mal_isr;
1672 unsigned long emac_isr = 0;
1673 unsigned long mal_rx_eob;
1674 unsigned long my_uicmsr;
1675
1676 EMAC_4XX_HW_PST hw_p;
1677
1678 /*
1679 * Because the mal is generic, we need to get the current
1680 * eth device
1681 */
1682#if defined(CONFIG_NET_MULTI)
1683 dev = eth_get_dev();
1684#else
1685 dev = emac0_dev;
1686#endif
1687
1688 hw_p = dev->priv;
1689
1690 /* enter loop that stays in interrupt code until nothing to service */
1691 do {
1692 serviced = 0;
1693
1694 my_uicmsr = mfdcr (uicmsr);
1695
1696 if ((my_uicmsr & (MAL_UIC_DEF | EMAC_UIC_DEF)) == 0) { /* not for us */
1697 return (rc);
1698 }
1699 /* get and clear controller status interrupts */
1700 /* look at Mal and EMAC interrupts */
1701 if ((MAL_UIC_DEF & my_uicmsr) != 0) { /* we have a MAL interrupt */
1702 mal_isr = mfdcr (malesr);
1703 /* look for mal error */
1704 if ((my_uicmsr & MAL_UIC_ERR) != 0) {
1705 mal_err (dev, mal_isr, my_uicmsr, MAL_UIC_DEF, MAL_UIC_ERR);
1706 serviced = 1;
1707 rc = 0;
1708 }
1709 }
1710
1711 /* port by port dispatch of emac interrupts */
1712
1713 if ((SEL_UIC_DEF(hw_p->devnum) & my_uicmsr) != 0) { /* look for EMAC errors */
Stefan Roese2d834762007-10-23 14:03:17 +02001714 emac_isr = in_be32((void *)EMAC_ISR + hw_p->hw_addr);
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001715 if ((hw_p->emac_ier & emac_isr) != 0) {
1716 emac_err (dev, emac_isr);
1717 serviced = 1;
1718 rc = 0;
1719 }
1720 }
1721 if (((hw_p->emac_ier & emac_isr) != 0) || ((MAL_UIC_ERR & my_uicmsr) != 0)) {
1722 mtdcr (uicsr, MAL_UIC_DEF | SEL_UIC_DEF(hw_p->devnum)); /* Clear */
1723 return (rc); /* we had errors so get out */
1724 }
1725
1726 /* handle MAX TX EOB interrupt from a tx */
1727 if (my_uicmsr & UIC_MAL_TXEOB) {
1728 mal_rx_eob = mfdcr (maltxeobisr);
1729 mtdcr (maltxeobisr, mal_rx_eob);
1730 mtdcr (uicsr, UIC_MAL_TXEOB);
1731 }
1732 /* handle MAL RX EOB interupt from a receive */
1733 /* check for EOB on valid channels */
1734 if (my_uicmsr & UIC_MAL_RXEOB)
1735 {
1736 mal_rx_eob = mfdcr (malrxeobisr);
Wolfgang Denk265817c2005-09-25 00:53:22 +02001737 if ((mal_rx_eob & (0x80000000 >> hw_p->devnum)) != 0) { /* call emac routine for channel x */
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001738 /* clear EOB
1739 mtdcr(malrxeobisr, mal_rx_eob); */
1740 enet_rcv (dev, emac_isr);
1741 /* indicate that we serviced an interrupt */
1742 serviced = 1;
1743 rc = 0;
1744 }
1745 }
1746 mtdcr (uicsr, MAL_UIC_DEF|EMAC_UIC_DEF|EMAC_UIC_DEF1); /* Clear */
Stefan Roesee01bd212007-03-21 13:38:59 +01001747#if defined(CONFIG_405EZ)
1748 mtsdr (sdricintstat, SDR_ICRX_STAT | SDR_ICTX0_STAT | SDR_ICTX1_STAT);
1749#endif /* defined(CONFIG_405EZ) */
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001750 }
1751 while (serviced);
1752
1753 return (rc);
1754}
1755
1756#endif /* CONFIG_440 */
1757
wdenkba56f622004-02-06 23:19:44 +00001758/*-----------------------------------------------------------------------------+
1759 * MAL Error Routine
1760 *-----------------------------------------------------------------------------*/
1761static void mal_err (struct eth_device *dev, unsigned long isr,
1762 unsigned long uic, unsigned long maldef,
1763 unsigned long mal_errr)
1764{
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001765 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenkba56f622004-02-06 23:19:44 +00001766
1767 mtdcr (malesr, isr); /* clear interrupt */
1768
1769 /* clear DE interrupt */
1770 mtdcr (maltxdeir, 0xC0000000);
1771 mtdcr (malrxdeir, 0x80000000);
1772
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001773#ifdef INFO_4XX_ENET
Wolfgang Denk265817c2005-09-25 00:53:22 +02001774 printf ("\nMAL error occured.... ISR = %lx UIC = = %lx MAL_DEF = %lx MAL_ERR= %lx \n", isr, uic, maldef, mal_errr);
wdenkba56f622004-02-06 23:19:44 +00001775#endif
1776
1777 eth_init (hw_p->bis); /* start again... */
1778}
1779
1780/*-----------------------------------------------------------------------------+
1781 * EMAC Error Routine
1782 *-----------------------------------------------------------------------------*/
1783static void emac_err (struct eth_device *dev, unsigned long isr)
1784{
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001785 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenkba56f622004-02-06 23:19:44 +00001786
1787 printf ("EMAC%d error occured.... ISR = %lx\n", hw_p->devnum, isr);
Stefan Roese2d834762007-10-23 14:03:17 +02001788 out_be32((void *)EMAC_ISR + hw_p->hw_addr, isr);
wdenkba56f622004-02-06 23:19:44 +00001789}
1790
1791/*-----------------------------------------------------------------------------+
1792 * enet_rcv() handles the ethernet receive data
1793 *-----------------------------------------------------------------------------*/
1794static void enet_rcv (struct eth_device *dev, unsigned long malisr)
1795{
1796 struct enet_frame *ef_ptr;
1797 unsigned long data_len;
1798 unsigned long rx_eob_isr;
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001799 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenkba56f622004-02-06 23:19:44 +00001800
1801 int handled = 0;
1802 int i;
1803 int loop_count = 0;
1804
1805 rx_eob_isr = mfdcr (malrxeobisr);
Stefan Roese8ac41e32008-03-11 15:05:26 +01001806 if ((0x80000000 >> (hw_p->devnum * MAL_RX_CHAN_MUL)) & rx_eob_isr) {
wdenkba56f622004-02-06 23:19:44 +00001807 /* clear EOB */
1808 mtdcr (malrxeobisr, rx_eob_isr);
1809
1810 /* EMAC RX done */
1811 while (1) { /* do all */
1812 i = hw_p->rx_slot;
1813
1814 if ((MAL_RX_CTRL_EMPTY & hw_p->rx[i].ctrl)
1815 || (loop_count >= NUM_RX_BUFF))
1816 break;
Stefan Roesea2e1c702007-07-12 16:32:08 +02001817
wdenkba56f622004-02-06 23:19:44 +00001818 loop_count++;
wdenkba56f622004-02-06 23:19:44 +00001819 handled++;
Stefan Roese8ac41e32008-03-11 15:05:26 +01001820 data_len = (unsigned long) hw_p->rx[i].data_len & 0x0fff; /* Get len */
wdenkba56f622004-02-06 23:19:44 +00001821 if (data_len) {
1822 if (data_len > ENET_MAX_MTU) /* Check len */
1823 data_len = 0;
1824 else {
1825 if (EMAC_RX_ERRORS & hw_p->rx[i].ctrl) { /* Check Errors */
1826 data_len = 0;
1827 hw_p->stats.rx_err_log[hw_p->
1828 rx_err_index]
1829 = hw_p->rx[i].ctrl;
1830 hw_p->rx_err_index++;
1831 if (hw_p->rx_err_index ==
1832 MAX_ERR_LOG)
1833 hw_p->rx_err_index =
1834 0;
wdenkfc1cfcd2004-04-25 15:41:35 +00001835 } /* emac_erros */
wdenkba56f622004-02-06 23:19:44 +00001836 } /* data_len < max mtu */
wdenkfc1cfcd2004-04-25 15:41:35 +00001837 } /* if data_len */
wdenkba56f622004-02-06 23:19:44 +00001838 if (!data_len) { /* no data */
1839 hw_p->rx[i].ctrl |= MAL_RX_CTRL_EMPTY; /* Free Recv Buffer */
1840
1841 hw_p->stats.data_len_err++; /* Error at Rx */
1842 }
1843
1844 /* !data_len */
1845 /* AS.HARNOIS */
1846 /* Check if user has already eaten buffer */
1847 /* if not => ERROR */
1848 else if (hw_p->rx_ready[hw_p->rx_i_index] != -1) {
1849 if (hw_p->is_receiving)
1850 printf ("ERROR : Receive buffers are full!\n");
1851 break;
1852 } else {
1853 hw_p->stats.rx_frames++;
1854 hw_p->stats.rx += data_len;
1855 ef_ptr = (struct enet_frame *) hw_p->rx[i].
1856 data_ptr;
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001857#ifdef INFO_4XX_ENET
wdenkba56f622004-02-06 23:19:44 +00001858 hw_p->stats.pkts_rx++;
1859#endif
1860 /* AS.HARNOIS
1861 * use ring buffer
1862 */
1863 hw_p->rx_ready[hw_p->rx_i_index] = i;
1864 hw_p->rx_i_index++;
1865 if (NUM_RX_BUFF == hw_p->rx_i_index)
1866 hw_p->rx_i_index = 0;
1867
Stefan Roesea2e1c702007-07-12 16:32:08 +02001868 hw_p->rx_slot++;
1869 if (NUM_RX_BUFF == hw_p->rx_slot)
1870 hw_p->rx_slot = 0;
1871
wdenkba56f622004-02-06 23:19:44 +00001872 /* AS.HARNOIS
1873 * free receive buffer only when
1874 * buffer has been handled (eth_rx)
1875 rx[i].ctrl |= MAL_RX_CTRL_EMPTY;
1876 */
1877 } /* if data_len */
1878 } /* while */
1879 } /* if EMACK_RXCHL */
1880}
1881
1882
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001883static int ppc_4xx_eth_rx (struct eth_device *dev)
wdenkba56f622004-02-06 23:19:44 +00001884{
1885 int length;
1886 int user_index;
1887 unsigned long msr;
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001888 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenkba56f622004-02-06 23:19:44 +00001889
Wolfgang Denk265817c2005-09-25 00:53:22 +02001890 hw_p->is_receiving = 1; /* tell driver */
wdenkba56f622004-02-06 23:19:44 +00001891
1892 for (;;) {
1893 /* AS.HARNOIS
1894 * use ring buffer and
1895 * get index from rx buffer desciptor queue
1896 */
1897 user_index = hw_p->rx_ready[hw_p->rx_u_index];
1898 if (user_index == -1) {
1899 length = -1;
1900 break; /* nothing received - leave for() loop */
1901 }
1902
1903 msr = mfmsr ();
1904 mtmsr (msr & ~(MSR_EE));
1905
Stefan Roese8ac41e32008-03-11 15:05:26 +01001906 length = hw_p->rx[user_index].data_len & 0x0fff;
wdenkba56f622004-02-06 23:19:44 +00001907
1908 /* Pass the packet up to the protocol layers. */
Wolfgang Denk265817c2005-09-25 00:53:22 +02001909 /* NetReceive(NetRxPackets[rxIdx], length - 4); */
1910 /* NetReceive(NetRxPackets[i], length); */
Stefan Roeseff768cb2007-10-31 18:01:24 +01001911 invalidate_dcache_range((u32)hw_p->rx[user_index].data_ptr,
1912 (u32)hw_p->rx[user_index].data_ptr +
Matthias Fuchsba79fde2007-12-14 11:19:56 +01001913 length - 4);
wdenkba56f622004-02-06 23:19:44 +00001914 NetReceive (NetRxPackets[user_index], length - 4);
1915 /* Free Recv Buffer */
1916 hw_p->rx[user_index].ctrl |= MAL_RX_CTRL_EMPTY;
1917 /* Free rx buffer descriptor queue */
1918 hw_p->rx_ready[hw_p->rx_u_index] = -1;
1919 hw_p->rx_u_index++;
1920 if (NUM_RX_BUFF == hw_p->rx_u_index)
1921 hw_p->rx_u_index = 0;
1922
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001923#ifdef INFO_4XX_ENET
wdenkba56f622004-02-06 23:19:44 +00001924 hw_p->stats.pkts_handled++;
1925#endif
1926
1927 mtmsr (msr); /* Enable IRQ's */
1928 }
1929
Wolfgang Denk265817c2005-09-25 00:53:22 +02001930 hw_p->is_receiving = 0; /* tell driver */
wdenkba56f622004-02-06 23:19:44 +00001931
1932 return length;
1933}
1934
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001935int ppc_4xx_eth_initialize (bd_t * bis)
wdenkba56f622004-02-06 23:19:44 +00001936{
1937 static int virgin = 0;
wdenkba56f622004-02-06 23:19:44 +00001938 struct eth_device *dev;
1939 int eth_num = 0;
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001940 EMAC_4XX_HW_PST hw = NULL;
Stefan Roese5fb692c2007-01-18 10:25:34 +01001941 u8 ethaddr[4 + CONFIG_EMAC_NR_START][6];
1942 u32 hw_addr[4];
wdenkba56f622004-02-06 23:19:44 +00001943
Stefan Roese846b0dd2005-08-08 12:42:22 +02001944#if defined(CONFIG_440GX)
Stefan Roesec157d8e2005-08-01 16:41:48 +02001945 unsigned long pfc1;
1946
wdenkba56f622004-02-06 23:19:44 +00001947 mfsdr (sdr_pfc1, pfc1);
1948 pfc1 &= ~(0x01e00000);
1949 pfc1 |= 0x01200000;
1950 mtsdr (sdr_pfc1, pfc1);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001951#endif
Stefan Roese5fb692c2007-01-18 10:25:34 +01001952
1953 /* first clear all mac-addresses */
1954 for (eth_num = 0; eth_num < LAST_EMAC_NUM; eth_num++)
1955 memcpy(ethaddr[eth_num], "\0\0\0\0\0\0", 6);
1956
1957 for (eth_num = 0; eth_num < LAST_EMAC_NUM; eth_num++) {
1958 switch (eth_num) {
1959 default: /* fall through */
1960 case 0:
1961 memcpy(ethaddr[eth_num + CONFIG_EMAC_NR_START],
1962 bis->bi_enetaddr, 6);
1963 hw_addr[eth_num] = 0x0;
1964 break;
1965#ifdef CONFIG_HAS_ETH1
1966 case 1:
1967 memcpy(ethaddr[eth_num + CONFIG_EMAC_NR_START],
1968 bis->bi_enet1addr, 6);
1969 hw_addr[eth_num] = 0x100;
1970 break;
1971#endif
1972#ifdef CONFIG_HAS_ETH2
1973 case 2:
1974 memcpy(ethaddr[eth_num + CONFIG_EMAC_NR_START],
1975 bis->bi_enet2addr, 6);
Stefan Roese4c9e8552008-03-19 16:20:49 +01001976#if defined(CONFIG_460GT)
1977 hw_addr[eth_num] = 0x300;
1978#else
Stefan Roese5fb692c2007-01-18 10:25:34 +01001979 hw_addr[eth_num] = 0x400;
Stefan Roese4c9e8552008-03-19 16:20:49 +01001980#endif
Stefan Roese5fb692c2007-01-18 10:25:34 +01001981 break;
1982#endif
1983#ifdef CONFIG_HAS_ETH3
1984 case 3:
1985 memcpy(ethaddr[eth_num + CONFIG_EMAC_NR_START],
1986 bis->bi_enet3addr, 6);
Stefan Roese4c9e8552008-03-19 16:20:49 +01001987#if defined(CONFIG_460GT)
1988 hw_addr[eth_num] = 0x400;
1989#else
Stefan Roese5fb692c2007-01-18 10:25:34 +01001990 hw_addr[eth_num] = 0x600;
Stefan Roese4c9e8552008-03-19 16:20:49 +01001991#endif
Stefan Roese5fb692c2007-01-18 10:25:34 +01001992 break;
1993#endif
1994 }
1995 }
1996
wdenk3c74e322004-02-22 23:46:08 +00001997 /* set phy num and mode */
1998 bis->bi_phynum[0] = CONFIG_PHY_ADDR;
Marian Balakowicz6c5879f2006-06-30 16:30:46 +02001999 bis->bi_phymode[0] = 0;
2000
Stefan Roesec157d8e2005-08-01 16:41:48 +02002001#if defined(CONFIG_PHY1_ADDR)
wdenk3c74e322004-02-22 23:46:08 +00002002 bis->bi_phynum[1] = CONFIG_PHY1_ADDR;
Marian Balakowicz6c5879f2006-06-30 16:30:46 +02002003 bis->bi_phymode[1] = 0;
Stefan Roesec157d8e2005-08-01 16:41:48 +02002004#endif
Stefan Roese846b0dd2005-08-08 12:42:22 +02002005#if defined(CONFIG_440GX)
wdenk3c74e322004-02-22 23:46:08 +00002006 bis->bi_phynum[2] = CONFIG_PHY2_ADDR;
2007 bis->bi_phynum[3] = CONFIG_PHY3_ADDR;
wdenk3c74e322004-02-22 23:46:08 +00002008 bis->bi_phymode[2] = 2;
2009 bis->bi_phymode[3] = 2;
Stefan Roesedbbd1252007-10-05 17:10:59 +02002010#endif
wdenkba56f622004-02-06 23:19:44 +00002011
Stefan Roesedbbd1252007-10-05 17:10:59 +02002012#if defined(CONFIG_440GX) || \
2013 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
2014 defined(CONFIG_405EX)
Stefan Roesed6c61aa2005-08-16 18:18:00 +02002015 ppc_4xx_eth_setup_bridge(0, bis);
wdenka06752e2004-09-29 22:43:59 +00002016#endif
2017
Stefan Roese1e25f952005-10-20 16:34:28 +02002018 for (eth_num = 0; eth_num < LAST_EMAC_NUM; eth_num++) {
Stefan Roese5fb692c2007-01-18 10:25:34 +01002019 /*
2020 * See if we can actually bring up the interface,
2021 * otherwise, skip it
2022 */
2023 if (memcmp (ethaddr[eth_num], "\0\0\0\0\0\0", 6) == 0) {
2024 bis->bi_phymode[eth_num] = BI_PHYMODE_NONE;
2025 continue;
wdenkba56f622004-02-06 23:19:44 +00002026 }
2027
2028 /* Allocate device structure */
2029 dev = (struct eth_device *) malloc (sizeof (*dev));
2030 if (dev == NULL) {
Stefan Roesed6c61aa2005-08-16 18:18:00 +02002031 printf ("ppc_4xx_eth_initialize: "
wdenk3f85ce22004-02-23 16:11:30 +00002032 "Cannot allocate eth_device %d\n", eth_num);
wdenkba56f622004-02-06 23:19:44 +00002033 return (-1);
2034 }
wdenkb2532ef2005-06-20 10:17:34 +00002035 memset(dev, 0, sizeof(*dev));
wdenkba56f622004-02-06 23:19:44 +00002036
2037 /* Allocate our private use data */
Stefan Roesed6c61aa2005-08-16 18:18:00 +02002038 hw = (EMAC_4XX_HW_PST) malloc (sizeof (*hw));
wdenkba56f622004-02-06 23:19:44 +00002039 if (hw == NULL) {
Stefan Roesed6c61aa2005-08-16 18:18:00 +02002040 printf ("ppc_4xx_eth_initialize: "
wdenk3f85ce22004-02-23 16:11:30 +00002041 "Cannot allocate private hw data for eth_device %d",
wdenkba56f622004-02-06 23:19:44 +00002042 eth_num);
2043 free (dev);
2044 return (-1);
2045 }
wdenkb2532ef2005-06-20 10:17:34 +00002046 memset(hw, 0, sizeof(*hw));
wdenkba56f622004-02-06 23:19:44 +00002047
Stefan Roese5fb692c2007-01-18 10:25:34 +01002048 hw->hw_addr = hw_addr[eth_num];
2049 memcpy (dev->enetaddr, ethaddr[eth_num], 6);
wdenkba56f622004-02-06 23:19:44 +00002050 hw->devnum = eth_num;
Stefan Roesec157d8e2005-08-01 16:41:48 +02002051 hw->print_speed = 1;
wdenkba56f622004-02-06 23:19:44 +00002052
Stefan Roese5fb692c2007-01-18 10:25:34 +01002053 sprintf (dev->name, "ppc_4xx_eth%d", eth_num - CONFIG_EMAC_NR_START);
wdenkba56f622004-02-06 23:19:44 +00002054 dev->priv = (void *) hw;
Stefan Roesed6c61aa2005-08-16 18:18:00 +02002055 dev->init = ppc_4xx_eth_init;
2056 dev->halt = ppc_4xx_eth_halt;
2057 dev->send = ppc_4xx_eth_send;
2058 dev->recv = ppc_4xx_eth_rx;
wdenkba56f622004-02-06 23:19:44 +00002059
2060 if (0 == virgin) {
2061 /* set the MAL IER ??? names may change with new spec ??? */
Stefan Roesedbbd1252007-10-05 17:10:59 +02002062#if defined(CONFIG_440SPE) || \
2063 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roese8ac41e32008-03-11 15:05:26 +01002064 defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
Stefan Roesedbbd1252007-10-05 17:10:59 +02002065 defined(CONFIG_405EX)
Marian Balakowicz6c5879f2006-06-30 16:30:46 +02002066 mal_ier =
2067 MAL_IER_PT | MAL_IER_PRE | MAL_IER_PWE |
2068 MAL_IER_DE | MAL_IER_OTE | MAL_IER_OE | MAL_IER_PE ;
2069#else
wdenkba56f622004-02-06 23:19:44 +00002070 mal_ier =
2071 MAL_IER_DE | MAL_IER_NE | MAL_IER_TE |
2072 MAL_IER_OPBE | MAL_IER_PLBE;
Marian Balakowicz6c5879f2006-06-30 16:30:46 +02002073#endif
wdenkba56f622004-02-06 23:19:44 +00002074 mtdcr (malesr, 0xffffffff); /* clear pending interrupts */
2075 mtdcr (maltxdeir, 0xffffffff); /* clear pending interrupts */
2076 mtdcr (malrxdeir, 0xffffffff); /* clear pending interrupts */
2077 mtdcr (malier, mal_ier);
2078
2079 /* install MAL interrupt handler */
2080 irq_install_handler (VECNUM_MS,
2081 (interrupt_handler_t *) enetInt,
2082 dev);
2083 irq_install_handler (VECNUM_MTE,
2084 (interrupt_handler_t *) enetInt,
2085 dev);
2086 irq_install_handler (VECNUM_MRE,
2087 (interrupt_handler_t *) enetInt,
2088 dev);
2089 irq_install_handler (VECNUM_TXDE,
2090 (interrupt_handler_t *) enetInt,
2091 dev);
2092 irq_install_handler (VECNUM_RXDE,
2093 (interrupt_handler_t *) enetInt,
2094 dev);
2095 virgin = 1;
2096 }
2097
Stefan Roesed6c61aa2005-08-16 18:18:00 +02002098#if defined(CONFIG_NET_MULTI)
wdenkba56f622004-02-06 23:19:44 +00002099 eth_register (dev);
Stefan Roesed6c61aa2005-08-16 18:18:00 +02002100#else
2101 emac0_dev = dev;
2102#endif
Marian Balakowicz6c5879f2006-06-30 16:30:46 +02002103
2104#if defined(CONFIG_NET_MULTI)
Jon Loeliger3a1ed1e2007-07-09 18:57:22 -05002105#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Marian Balakowicz63ff0042005-10-28 22:30:33 +02002106 miiphy_register (dev->name,
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01002107 emac4xx_miiphy_read, emac4xx_miiphy_write);
Marian Balakowicz63ff0042005-10-28 22:30:33 +02002108#endif
Marian Balakowicz6c5879f2006-06-30 16:30:46 +02002109#endif
wdenkba56f622004-02-06 23:19:44 +00002110 } /* end for each supported device */
Stefan Roese802b7692008-01-08 18:39:30 +01002111
2112 return 0;
wdenkba56f622004-02-06 23:19:44 +00002113}
Stefan Roesed6c61aa2005-08-16 18:18:00 +02002114
Stefan Roesed6c61aa2005-08-16 18:18:00 +02002115#if !defined(CONFIG_NET_MULTI)
2116void eth_halt (void) {
2117 if (emac0_dev) {
2118 ppc_4xx_eth_halt(emac0_dev);
2119 free(emac0_dev);
2120 emac0_dev = NULL;
2121 }
2122}
2123
2124int eth_init (bd_t *bis)
2125{
2126 ppc_4xx_eth_initialize(bis);
Stefan Roese4f92ac32005-10-10 17:43:58 +02002127 if (emac0_dev) {
2128 return ppc_4xx_eth_init(emac0_dev, bis);
2129 } else {
2130 printf("ERROR: ethaddr not set!\n");
2131 return -1;
2132 }
Stefan Roesed6c61aa2005-08-16 18:18:00 +02002133}
2134
2135int eth_send(volatile void *packet, int length)
2136{
Stefan Roesed6c61aa2005-08-16 18:18:00 +02002137 return (ppc_4xx_eth_send(emac0_dev, packet, length));
2138}
2139
2140int eth_rx(void)
2141{
2142 return (ppc_4xx_eth_rx(emac0_dev));
2143}
Marian Balakowicz63ff0042005-10-28 22:30:33 +02002144
2145int emac4xx_miiphy_initialize (bd_t * bis)
2146{
Jon Loeliger3a1ed1e2007-07-09 18:57:22 -05002147#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Marian Balakowicz63ff0042005-10-28 22:30:33 +02002148 miiphy_register ("ppc_4xx_eth0",
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01002149 emac4xx_miiphy_read, emac4xx_miiphy_write);
Marian Balakowicz63ff0042005-10-28 22:30:33 +02002150#endif
2151
2152 return 0;
2153}
Stefan Roesed6c61aa2005-08-16 18:18:00 +02002154#endif /* !defined(CONFIG_NET_MULTI) */
2155
Jon Loeliger3a1ed1e2007-07-09 18:57:22 -05002156#endif