blob: 67b3a24d2ff0b82b5f10fb9e732c5edf4332ab03 [file] [log] [blame]
wdenk544e9732004-02-06 23:19:44 +00001/*-----------------------------------------------------------------------------+
2 *
Wolfgang Denk0cbaf642005-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.
wdenk544e9732004-02-06 23:19:44 +00009 *
Wolfgang Denk0cbaf642005-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.
wdenk544e9732004-02-06 23:19:44 +000013 *
Wolfgang Denk0cbaf642005-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.
wdenk544e9732004-02-06 23:19:44 +000017 *
Wolfgang Denk0cbaf642005-09-25 00:53:22 +020018 * COPYRIGHT I B M CORPORATION 1995
19 * LICENSED MATERIAL - PROGRAM PROPERTY OF I B M
wdenk544e9732004-02-06 23:19:44 +000020 *-----------------------------------------------------------------------------*/
21/*-----------------------------------------------------------------------------+
22 *
Wolfgang Denk0cbaf642005-09-25 00:53:22 +020023 * File Name: enetemac.c
wdenk544e9732004-02-06 23:19:44 +000024 *
Wolfgang Denk0cbaf642005-09-25 00:53:22 +020025 * Function: Device driver for the ethernet EMAC3 macro on the 405GP.
wdenk544e9732004-02-06 23:19:44 +000026 *
Wolfgang Denk0cbaf642005-09-25 00:53:22 +020027 * Author: Mark Wisner
wdenk544e9732004-02-06 23:19:44 +000028 *
29 * Change Activity-
30 *
Wolfgang Denk0cbaf642005-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).
wdenk544e9732004-02-06 23:19:44 +000070 *-----------------------------------------------------------------------------*
Wolfgang Denk0cbaf642005-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.
wdenk544e9732004-02-06 23:19:44 +000078 *-----------------------------------------------------------------------------*/
79
80#include <config.h>
wdenk544e9732004-02-06 23:19:44 +000081#include <common.h>
82#include <net.h>
83#include <asm/processor.h>
wdenk544e9732004-02-06 23:19:44 +000084#include <commproc.h>
Stefan Roese0c7ffc02005-08-16 18:18:00 +020085#include <ppc4xx.h>
86#include <ppc4xx_enet.h>
wdenk544e9732004-02-06 23:19:44 +000087#include <405_mal.h>
88#include <miiphy.h>
89#include <malloc.h>
90#include "vecnum.h"
91
Stefan Roese0c7ffc02005-08-16 18:18:00 +020092/*
Wolfgang Denk0ee70772005-09-23 11:05:55 +020093 * Only compile for platform with AMCC EMAC ethernet controller and
Stefan Roese0c7ffc02005-08-16 18:18:00 +020094 * network support enabled.
95 * Remark: CONFIG_405 describes Xilinx PPC405 FPGA without EMAC controller!
96 */
Jon Loeligera5217742007-07-09 18:57:22 -050097#if defined(CONFIG_CMD_NET) && !defined(CONFIG_405) && !defined(CONFIG_IOP480)
Stefan Roese0c7ffc02005-08-16 18:18:00 +020098
Jon Loeligera5217742007-07-09 18:57:22 -050099#if !(defined(CONFIG_MII) || defined(CONFIG_CMD_MII))
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200100#error "CONFIG_MII has to be defined!"
101#endif
wdenk544e9732004-02-06 23:19:44 +0000102
Stefan Roese7f98aec2005-10-20 16:34:28 +0200103#if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_NET_MULTI)
104#error "CONFIG_NET_MULTI has to be defined for NetConsole"
105#endif
106
Wolfgang Denk0cbaf642005-09-25 00:53:22 +0200107#define EMAC_RESET_TIMEOUT 1000 /* 1000 ms reset timeout */
wdenk544e9732004-02-06 23:19:44 +0000108#define PHY_AUTONEGOTIATE_TIMEOUT 4000 /* 4000 ms autonegotiate timeout */
109
wdenk544e9732004-02-06 23:19:44 +0000110/* Ethernet Transmit and Receive Buffers */
111/* AS.HARNOIS
112 * In the same way ENET_MAX_MTU and ENET_MAX_MTU_ALIGNED are set from
113 * PKTSIZE and PKTSIZE_ALIGN (include/net.h)
114 */
Wolfgang Denk0cbaf642005-09-25 00:53:22 +0200115#define ENET_MAX_MTU PKTSIZE
wdenk544e9732004-02-06 23:19:44 +0000116#define ENET_MAX_MTU_ALIGNED PKTSIZE_ALIGN
117
wdenk544e9732004-02-06 23:19:44 +0000118/*-----------------------------------------------------------------------------+
119 * Defines for MAL/EMAC interrupt conditions as reported in the UIC (Universal
120 * Interrupt Controller).
121 *-----------------------------------------------------------------------------*/
122#define MAL_UIC_ERR ( UIC_MAL_SERR | UIC_MAL_TXDE | UIC_MAL_RXDE)
123#define MAL_UIC_DEF (UIC_MAL_RXEOB | MAL_UIC_ERR)
124#define EMAC_UIC_DEF UIC_ENET
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200125#define EMAC_UIC_DEF1 UIC_ENET1
126#define SEL_UIC_DEF(p) (p ? UIC_ENET1 : UIC_ENET )
wdenk544e9732004-02-06 23:19:44 +0000127
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200128#undef INFO_4XX_ENET
wdenk544e9732004-02-06 23:19:44 +0000129
Wolfgang Denk0cbaf642005-09-25 00:53:22 +0200130#define BI_PHYMODE_NONE 0
131#define BI_PHYMODE_ZMII 1
wdenk56ed43e2004-02-22 23:46:08 +0000132#define BI_PHYMODE_RGMII 2
Stefan Roese42fbddd2006-09-07 11:51:23 +0200133#define BI_PHYMODE_GMII 3
134#define BI_PHYMODE_RTBI 4
135#define BI_PHYMODE_TBI 5
Stefan Roese153b3e22007-10-05 17:10:59 +0200136#if defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
137 defined(CONFIG_405EX)
Stefan Roese42fbddd2006-09-07 11:51:23 +0200138#define BI_PHYMODE_SMII 6
139#define BI_PHYMODE_MII 7
140#endif
wdenk56ed43e2004-02-22 23:46:08 +0000141
Stefan Roese5a128832007-10-05 17:35:10 +0200142#if defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
Stefan Roese153b3e22007-10-05 17:10:59 +0200143 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
144 defined(CONFIG_405EX)
Stefan Roese42fbddd2006-09-07 11:51:23 +0200145#define SDR0_MFR_ETH_CLK_SEL_V(n) ((0x01<<27) / (n+1))
146#endif
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200147
wdenk544e9732004-02-06 23:19:44 +0000148/*-----------------------------------------------------------------------------+
149 * Global variables. TX and RX descriptors and buffers.
150 *-----------------------------------------------------------------------------*/
151/* IER globals */
152static uint32_t mal_ier;
153
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200154#if !defined(CONFIG_NET_MULTI)
Stefan Roese03510612005-10-10 17:43:58 +0200155struct eth_device *emac0_dev = NULL;
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200156#endif
157
Stefan Roese7f98aec2005-10-20 16:34:28 +0200158/*
159 * Get count of EMAC devices (doesn't have to be the max. possible number
160 * supported by the cpu)
Stefan Roese15668052007-10-23 10:10:08 +0200161 *
162 * CONFIG_BOARD_EMAC_COUNT added so now a "dynamic" way to configure the
163 * EMAC count is possible. As it is needed for the Kilauea/Haleakala
164 * 405EX/405EXr eval board, using the same binary.
Stefan Roese7f98aec2005-10-20 16:34:28 +0200165 */
Stefan Roese15668052007-10-23 10:10:08 +0200166#if defined(CONFIG_BOARD_EMAC_COUNT)
167#define LAST_EMAC_NUM board_emac_count()
168#else /* CONFIG_BOARD_EMAC_COUNT */
Stefan Roese7f98aec2005-10-20 16:34:28 +0200169#if defined(CONFIG_HAS_ETH3)
170#define LAST_EMAC_NUM 4
171#elif defined(CONFIG_HAS_ETH2)
172#define LAST_EMAC_NUM 3
173#elif defined(CONFIG_HAS_ETH1)
174#define LAST_EMAC_NUM 2
175#else
176#define LAST_EMAC_NUM 1
177#endif
Stefan Roese15668052007-10-23 10:10:08 +0200178#endif /* CONFIG_BOARD_EMAC_COUNT */
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200179
Stefan Roese8d982302007-01-18 10:25:34 +0100180/* normal boards start with EMAC0 */
181#if !defined(CONFIG_EMAC_NR_START)
182#define CONFIG_EMAC_NR_START 0
183#endif
184
Stefan Roese153b3e22007-10-05 17:10:59 +0200185#if defined(CONFIG_405EX) || defined(CONFIG_440EPX)
186#define ETH_IRQ_NUM(dev) (VECNUM_ETH0 + ((dev)))
187#else
188#define ETH_IRQ_NUM(dev) (VECNUM_ETH0 + ((dev) * 2))
189#endif
190
wdenk544e9732004-02-06 23:19:44 +0000191/*-----------------------------------------------------------------------------+
192 * Prototypes and externals.
193 *-----------------------------------------------------------------------------*/
194static void enet_rcv (struct eth_device *dev, unsigned long malisr);
195
196int enetInt (struct eth_device *dev);
197static void mal_err (struct eth_device *dev, unsigned long isr,
198 unsigned long uic, unsigned long maldef,
199 unsigned long mal_errr);
200static void emac_err (struct eth_device *dev, unsigned long isr);
201
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200202extern int phy_setup_aneg (char *devname, unsigned char addr);
203extern int emac4xx_miiphy_read (char *devname, unsigned char addr,
204 unsigned char reg, unsigned short *value);
205extern int emac4xx_miiphy_write (char *devname, unsigned char addr,
206 unsigned char reg, unsigned short value);
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200207
Stefan Roese15668052007-10-23 10:10:08 +0200208int board_emac_count(void);
209
wdenk544e9732004-02-06 23:19:44 +0000210/*-----------------------------------------------------------------------------+
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200211| ppc_4xx_eth_halt
wdenk544e9732004-02-06 23:19:44 +0000212| Disable MAL channel, and EMACn
wdenk544e9732004-02-06 23:19:44 +0000213+-----------------------------------------------------------------------------*/
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200214static void ppc_4xx_eth_halt (struct eth_device *dev)
wdenk544e9732004-02-06 23:19:44 +0000215{
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200216 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenk544e9732004-02-06 23:19:44 +0000217 uint32_t failsafe = 10000;
Stefan Roese153b3e22007-10-05 17:10:59 +0200218#if defined(CONFIG_440SPE) || \
219 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
220 defined(CONFIG_405EX)
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200221 unsigned long mfr;
222#endif
wdenk544e9732004-02-06 23:19:44 +0000223
224 out32 (EMAC_IER + hw_p->hw_addr, 0x00000000); /* disable emac interrupts */
225
226 /* 1st reset MAL channel */
227 /* Note: writing a 0 to a channel has no effect */
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200228#if defined(CONFIG_405EP) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
229 mtdcr (maltxcarr, (MAL_CR_MMSR >> (hw_p->devnum * 2)));
230#else
wdenk544e9732004-02-06 23:19:44 +0000231 mtdcr (maltxcarr, (MAL_CR_MMSR >> hw_p->devnum));
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200232#endif
wdenk544e9732004-02-06 23:19:44 +0000233 mtdcr (malrxcarr, (MAL_CR_MMSR >> hw_p->devnum));
234
235 /* wait for reset */
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200236 while (mfdcr (malrxcasr) & (MAL_CR_MMSR >> hw_p->devnum)) {
wdenk544e9732004-02-06 23:19:44 +0000237 udelay (1000); /* Delay 1 MS so as not to hammer the register */
238 failsafe--;
239 if (failsafe == 0)
240 break;
wdenk544e9732004-02-06 23:19:44 +0000241 }
242
243 /* EMAC RESET */
Stefan Roese153b3e22007-10-05 17:10:59 +0200244#if defined(CONFIG_440SPE) || \
245 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
246 defined(CONFIG_405EX)
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200247 /* provide clocks for EMAC internal loopback */
248 mfsdr (sdr_mfr, mfr);
Stefan Roese42fbddd2006-09-07 11:51:23 +0200249 mfr |= SDR0_MFR_ETH_CLK_SEL_V(hw_p->devnum);
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200250 mtsdr(sdr_mfr, mfr);
251#endif
252
wdenk544e9732004-02-06 23:19:44 +0000253 out32 (EMAC_M0 + hw_p->hw_addr, EMAC_M0_SRST);
254
Stefan Roese153b3e22007-10-05 17:10:59 +0200255#if defined(CONFIG_440SPE) || \
256 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
257 defined(CONFIG_405EX)
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200258 /* remove clocks for EMAC internal loopback */
259 mfsdr (sdr_mfr, mfr);
Stefan Roese42fbddd2006-09-07 11:51:23 +0200260 mfr &= ~SDR0_MFR_ETH_CLK_SEL_V(hw_p->devnum);
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200261 mtsdr(sdr_mfr, mfr);
262#endif
263
264
Stefan Roesec8136d02005-10-18 19:17:12 +0200265#ifndef CONFIG_NETCONSOLE
Stefan Roese326c9712005-08-01 16:41:48 +0200266 hw_p->print_speed = 1; /* print speed message again next time */
Stefan Roesec8136d02005-10-18 19:17:12 +0200267#endif
Stefan Roese326c9712005-08-01 16:41:48 +0200268
wdenk544e9732004-02-06 23:19:44 +0000269 return;
270}
271
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200272#if defined (CONFIG_440GX)
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200273int ppc_4xx_eth_setup_bridge(int devnum, bd_t * bis)
wdenked2ac4b2004-03-14 18:23:55 +0000274{
275 unsigned long pfc1;
276 unsigned long zmiifer;
277 unsigned long rmiifer;
278
279 mfsdr(sdr_pfc1, pfc1);
280 pfc1 = SDR0_PFC1_EPS_DECODE(pfc1);
281
282 zmiifer = 0;
283 rmiifer = 0;
284
285 switch (pfc1) {
286 case 1:
287 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
288 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(1);
289 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(2);
290 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(3);
291 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
292 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
293 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
294 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
295 break;
296 case 2:
Stefan Roese0632d7b2006-11-27 17:43:25 +0100297 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(0);
298 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(1);
299 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(2);
300 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(3);
wdenked2ac4b2004-03-14 18:23:55 +0000301 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
302 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
303 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
304 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
305 break;
306 case 3:
307 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
308 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
309 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
310 bis->bi_phymode[1] = BI_PHYMODE_NONE;
311 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
312 bis->bi_phymode[3] = BI_PHYMODE_NONE;
313 break;
314 case 4:
315 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(0);
316 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(1);
317 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V (2);
318 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V (3);
319 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
320 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
321 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
322 bis->bi_phymode[3] = BI_PHYMODE_RGMII;
323 break;
324 case 5:
325 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (0);
326 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (1);
327 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (2);
328 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(3);
329 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
330 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
331 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
332 bis->bi_phymode[3] = BI_PHYMODE_RGMII;
333 break;
334 case 6:
335 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (0);
336 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (1);
337 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
wdenked2ac4b2004-03-14 18:23:55 +0000338 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
339 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
340 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
wdenked2ac4b2004-03-14 18:23:55 +0000341 break;
342 case 0:
343 default:
344 zmiifer = ZMII_FER_MII << ZMII_FER_V(devnum);
345 rmiifer = 0x0;
346 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
347 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
348 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
349 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
350 break;
351 }
352
353 /* Ensure we setup mdio for this devnum and ONLY this devnum */
354 zmiifer |= (ZMII_FER_MDI) << ZMII_FER_V(devnum);
355
356 out32 (ZMII_FER, zmiifer);
357 out32 (RGMII_FER, rmiifer);
358
359 return ((int)pfc1);
wdenked2ac4b2004-03-14 18:23:55 +0000360}
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200361#endif /* CONFIG_440_GX */
wdenked2ac4b2004-03-14 18:23:55 +0000362
Stefan Roese42fbddd2006-09-07 11:51:23 +0200363#if defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
364int ppc_4xx_eth_setup_bridge(int devnum, bd_t * bis)
365{
366 unsigned long zmiifer=0x0;
Matthias Fuchs8bdf1682007-04-24 14:03:45 +0200367 unsigned long pfc1;
Stefan Roese42fbddd2006-09-07 11:51:23 +0200368
Matthias Fuchs8bdf1682007-04-24 14:03:45 +0200369 mfsdr(sdr_pfc1, pfc1);
370 pfc1 &= SDR0_PFC1_SELECT_MASK;
371
Wolfgang Denk58c495b2007-05-05 18:23:11 +0200372 switch (pfc1) {
Matthias Fuchs8bdf1682007-04-24 14:03:45 +0200373 case SDR0_PFC1_SELECT_CONFIG_2:
Stefan Roese42fbddd2006-09-07 11:51:23 +0200374 /* 1 x GMII port */
375 out32 (ZMII_FER, 0x00);
376 out32 (RGMII_FER, 0x00000037);
377 bis->bi_phymode[0] = BI_PHYMODE_GMII;
378 bis->bi_phymode[1] = BI_PHYMODE_NONE;
379 break;
Matthias Fuchs8bdf1682007-04-24 14:03:45 +0200380 case SDR0_PFC1_SELECT_CONFIG_4:
Stefan Roese42fbddd2006-09-07 11:51:23 +0200381 /* 2 x RGMII ports */
382 out32 (ZMII_FER, 0x00);
383 out32 (RGMII_FER, 0x00000055);
384 bis->bi_phymode[0] = BI_PHYMODE_RGMII;
385 bis->bi_phymode[1] = BI_PHYMODE_RGMII;
386 break;
Matthias Fuchs8bdf1682007-04-24 14:03:45 +0200387 case SDR0_PFC1_SELECT_CONFIG_6:
Stefan Roese42fbddd2006-09-07 11:51:23 +0200388 /* 2 x SMII ports */
Wolfgang Denk58c495b2007-05-05 18:23:11 +0200389 out32 (ZMII_FER,
Matthias Fuchs8bdf1682007-04-24 14:03:45 +0200390 ((ZMII_FER_SMII) << ZMII_FER_V(0)) |
391 ((ZMII_FER_SMII) << ZMII_FER_V(1)));
392 out32 (RGMII_FER, 0x00000000);
393 bis->bi_phymode[0] = BI_PHYMODE_SMII;
394 bis->bi_phymode[1] = BI_PHYMODE_SMII;
395 break;
396 case SDR0_PFC1_SELECT_CONFIG_1_2:
397 /* only 1 x MII supported */
398 out32 (ZMII_FER, (ZMII_FER_MII) << ZMII_FER_V(0));
399 out32 (RGMII_FER, 0x00000000);
400 bis->bi_phymode[0] = BI_PHYMODE_MII;
401 bis->bi_phymode[1] = BI_PHYMODE_NONE;
Stefan Roese42fbddd2006-09-07 11:51:23 +0200402 break;
403 default:
404 break;
405 }
406
407 /* Ensure we setup mdio for this devnum and ONLY this devnum */
408 zmiifer = in32 (ZMII_FER);
409 zmiifer |= (ZMII_FER_MDI) << ZMII_FER_V(devnum);
410 out32 (ZMII_FER, zmiifer);
411
412 return ((int)0x0);
413}
414#endif /* CONFIG_440EPX */
415
Stefan Roese153b3e22007-10-05 17:10:59 +0200416#if defined(CONFIG_405EX)
417int ppc_4xx_eth_setup_bridge(int devnum, bd_t * bis)
418{
419 u32 gmiifer = 0;
420
421 /*
422 * Right now only 2*RGMII is supported. Please extend when needed.
423 * sr - 2007-09-19
424 */
425 switch (1) {
426 case 1:
427 /* 2 x RGMII ports */
428 out32 (RGMII_FER, 0x00000055);
429 bis->bi_phymode[0] = BI_PHYMODE_RGMII;
430 bis->bi_phymode[1] = BI_PHYMODE_RGMII;
431 break;
432 case 2:
433 /* 2 x SMII ports */
434 break;
435 default:
436 break;
437 }
438
439 /* Ensure we setup mdio for this devnum and ONLY this devnum */
440 gmiifer = in32(RGMII_FER);
441 gmiifer |= (1 << (19-devnum));
442 out32 (RGMII_FER, gmiifer);
443
444 return ((int)0x0);
445}
446#endif /* CONFIG_405EX */
447
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200448static int ppc_4xx_eth_init (struct eth_device *dev, bd_t * bis)
wdenk544e9732004-02-06 23:19:44 +0000449{
Stefan Roese5ff4c3f2005-08-15 12:31:23 +0200450 int i, j;
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200451 unsigned long reg = 0;
wdenk544e9732004-02-06 23:19:44 +0000452 unsigned long msr;
453 unsigned long speed;
454 unsigned long duplex;
455 unsigned long failsafe;
456 unsigned mode_reg;
457 unsigned short devnum;
458 unsigned short reg_short;
Stefan Roese42fbddd2006-09-07 11:51:23 +0200459#if defined(CONFIG_440GX) || \
460 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roese153b3e22007-10-05 17:10:59 +0200461 defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
462 defined(CONFIG_405EX)
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200463 sys_info_t sysinfo;
Stefan Roese42fbddd2006-09-07 11:51:23 +0200464#if defined(CONFIG_440GX) || defined(CONFIG_440SPE) || \
Stefan Roese153b3e22007-10-05 17:10:59 +0200465 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
466 defined(CONFIG_405EX)
Stefan Roese99644742005-11-29 18:18:21 +0100467 int ethgroup = -1;
468#endif
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200469#endif
Grzegorz Bernacki2462bbd2007-10-01 09:51:50 +0200470#if defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roese153b3e22007-10-05 17:10:59 +0200471 defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
472 defined(CONFIG_405EX)
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200473 unsigned long mfr;
Stefan Roese326c9712005-08-01 16:41:48 +0200474#endif
wdenk544e9732004-02-06 23:19:44 +0000475
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200476 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenk544e9732004-02-06 23:19:44 +0000477
478 /* before doing anything, figure out if we have a MAC address */
479 /* if not, bail */
Stefan Roese03510612005-10-10 17:43:58 +0200480 if (memcmp (dev->enetaddr, "\0\0\0\0\0\0", 6) == 0) {
481 printf("ERROR: ethaddr not set!\n");
wdenk544e9732004-02-06 23:19:44 +0000482 return -1;
Stefan Roese03510612005-10-10 17:43:58 +0200483 }
wdenk544e9732004-02-06 23:19:44 +0000484
Stefan Roese42fbddd2006-09-07 11:51:23 +0200485#if defined(CONFIG_440GX) || \
486 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roese153b3e22007-10-05 17:10:59 +0200487 defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
488 defined(CONFIG_405EX)
wdenk544e9732004-02-06 23:19:44 +0000489 /* Need to get the OPB frequency so we can access the PHY */
490 get_sys_info (&sysinfo);
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200491#endif
wdenk544e9732004-02-06 23:19:44 +0000492
wdenk544e9732004-02-06 23:19:44 +0000493 msr = mfmsr ();
494 mtmsr (msr & ~(MSR_EE)); /* disable interrupts */
495
496 devnum = hw_p->devnum;
497
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200498#ifdef INFO_4XX_ENET
wdenk544e9732004-02-06 23:19:44 +0000499 /* AS.HARNOIS
500 * We should have :
Wolfgang Denk0cbaf642005-09-25 00:53:22 +0200501 * hw_p->stats.pkts_handled <= hw_p->stats.pkts_rx <= hw_p->stats.pkts_handled+PKTBUFSRX
wdenk544e9732004-02-06 23:19:44 +0000502 * In the most cases hw_p->stats.pkts_handled = hw_p->stats.pkts_rx, but it
503 * is possible that new packets (without relationship with
504 * current transfer) have got the time to arrived before
505 * netloop calls eth_halt
506 */
507 printf ("About preceeding transfer (eth%d):\n"
508 "- Sent packet number %d\n"
509 "- Received packet number %d\n"
510 "- Handled packet number %d\n",
511 hw_p->devnum,
512 hw_p->stats.pkts_tx,
513 hw_p->stats.pkts_rx, hw_p->stats.pkts_handled);
514
515 hw_p->stats.pkts_tx = 0;
516 hw_p->stats.pkts_rx = 0;
517 hw_p->stats.pkts_handled = 0;
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200518 hw_p->print_speed = 1; /* print speed message again next time */
wdenk544e9732004-02-06 23:19:44 +0000519#endif
520
Wolfgang Denk0cbaf642005-09-25 00:53:22 +0200521 hw_p->tx_err_index = 0; /* Transmit Error Index for tx_err_log */
522 hw_p->rx_err_index = 0; /* Receive Error Index for rx_err_log */
wdenk544e9732004-02-06 23:19:44 +0000523
524 hw_p->rx_slot = 0; /* MAL Receive Slot */
525 hw_p->rx_i_index = 0; /* Receive Interrupt Queue Index */
526 hw_p->rx_u_index = 0; /* Receive User Queue Index */
527
528 hw_p->tx_slot = 0; /* MAL Transmit Slot */
529 hw_p->tx_i_index = 0; /* Transmit Interrupt Queue Index */
530 hw_p->tx_u_index = 0; /* Transmit User Queue Index */
531
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200532#if defined(CONFIG_440) && !defined(CONFIG_440SP) && !defined(CONFIG_440SPE)
wdenk544e9732004-02-06 23:19:44 +0000533 /* set RMII mode */
534 /* NOTE: 440GX spec states that mode is mutually exclusive */
535 /* NOTE: Therefore, disable all other EMACS, since we handle */
536 /* NOTE: only one emac at a time */
537 reg = 0;
538 out32 (ZMII_FER, 0);
539 udelay (100);
wdenk544e9732004-02-06 23:19:44 +0000540
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200541#if defined(CONFIG_440EP) || defined(CONFIG_440GR)
Wolfgang Denk0cbaf642005-09-25 00:53:22 +0200542 out32 (ZMII_FER, (ZMII_FER_RMII | ZMII_FER_MDI) << ZMII_FER_V (devnum));
Stefan Roese42fbddd2006-09-07 11:51:23 +0200543#elif defined(CONFIG_440GX) || defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200544 ethgroup = ppc_4xx_eth_setup_bridge(devnum, bis);
Stefan Roese74309032005-09-07 16:21:12 +0200545#elif defined(CONFIG_440GP)
546 /* set RMII mode */
547 out32 (ZMII_FER, ZMII_RMII | ZMII_MDI0);
wdenk00fe1612004-03-14 00:07:33 +0000548#else
549 if ((devnum == 0) || (devnum == 1)) {
550 out32 (ZMII_FER, (ZMII_FER_SMII | ZMII_FER_MDI) << ZMII_FER_V (devnum));
Stefan Roese2a4a9432006-11-27 14:12:17 +0100551 } else { /* ((devnum == 2) || (devnum == 3)) */
wdenk00fe1612004-03-14 00:07:33 +0000552 out32 (ZMII_FER, ZMII_FER_MDI << ZMII_FER_V (devnum));
wdenk544e9732004-02-06 23:19:44 +0000553 out32 (RGMII_FER, ((RGMII_FER_RGMII << RGMII_FER_V (2)) |
554 (RGMII_FER_RGMII << RGMII_FER_V (3))));
wdenk00fe1612004-03-14 00:07:33 +0000555 }
556#endif
Stefan Roese797d8572005-08-11 17:56:56 +0200557
wdenk00fe1612004-03-14 00:07:33 +0000558 out32 (ZMII_SSR, ZMII_SSR_SP << ZMII_SSR_V(devnum));
Stefan Roese99644742005-11-29 18:18:21 +0100559#endif /* defined(CONFIG_440) && !defined(CONFIG_440SP) */
Stefan Roese153b3e22007-10-05 17:10:59 +0200560#if defined(CONFIG_405EX)
561 ethgroup = ppc_4xx_eth_setup_bridge(devnum, bis);
562#endif
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200563
wdenk00fe1612004-03-14 00:07:33 +0000564 __asm__ volatile ("eieio");
565
566 /* reset emac so we have access to the phy */
Stefan Roese153b3e22007-10-05 17:10:59 +0200567#if defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
568 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
569 defined(CONFIG_405EX)
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200570 /* provide clocks for EMAC internal loopback */
571 mfsdr (sdr_mfr, mfr);
Stefan Roese42fbddd2006-09-07 11:51:23 +0200572 mfr |= SDR0_MFR_ETH_CLK_SEL_V(devnum);
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200573 mtsdr(sdr_mfr, mfr);
574#endif
wdenk00fe1612004-03-14 00:07:33 +0000575
576 out32 (EMAC_M0 + hw_p->hw_addr, EMAC_M0_SRST);
wdenk544e9732004-02-06 23:19:44 +0000577 __asm__ volatile ("eieio");
578
579 failsafe = 1000;
580 while ((in32 (EMAC_M0 + hw_p->hw_addr) & (EMAC_M0_SRST)) && failsafe) {
581 udelay (1000);
582 failsafe--;
583 }
Stefan Roese42fbddd2006-09-07 11:51:23 +0200584 if (failsafe <= 0)
585 printf("\nProblem resetting EMAC!\n");
wdenk544e9732004-02-06 23:19:44 +0000586
Stefan Roese153b3e22007-10-05 17:10:59 +0200587#if defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
588 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
589 defined(CONFIG_405EX)
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200590 /* remove clocks for EMAC internal loopback */
591 mfsdr (sdr_mfr, mfr);
Stefan Roese42fbddd2006-09-07 11:51:23 +0200592 mfr &= ~SDR0_MFR_ETH_CLK_SEL_V(devnum);
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200593 mtsdr(sdr_mfr, mfr);
594#endif
595
Stefan Roese42fbddd2006-09-07 11:51:23 +0200596#if defined(CONFIG_440GX) || \
597 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roese153b3e22007-10-05 17:10:59 +0200598 defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
599 defined(CONFIG_405EX)
wdenk544e9732004-02-06 23:19:44 +0000600 /* Whack the M1 register */
601 mode_reg = 0x0;
602 mode_reg &= ~0x00000038;
603 if (sysinfo.freqOPB <= 50000000);
604 else if (sysinfo.freqOPB <= 66666667)
605 mode_reg |= EMAC_M1_OBCI_66;
606 else if (sysinfo.freqOPB <= 83333333)
607 mode_reg |= EMAC_M1_OBCI_83;
608 else if (sysinfo.freqOPB <= 100000000)
609 mode_reg |= EMAC_M1_OBCI_100;
610 else
611 mode_reg |= EMAC_M1_OBCI_GT100;
612
613 out32 (EMAC_M1 + hw_p->hw_addr, mode_reg);
Stefan Roese99644742005-11-29 18:18:21 +0100614#endif /* defined(CONFIG_440GX) || defined(CONFIG_440SP) */
wdenk544e9732004-02-06 23:19:44 +0000615
616 /* wait for PHY to complete auto negotiation */
617 reg_short = 0;
618#ifndef CONFIG_CS8952_PHY
619 switch (devnum) {
620 case 0:
621 reg = CONFIG_PHY_ADDR;
622 break;
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200623#if defined (CONFIG_PHY1_ADDR)
wdenk544e9732004-02-06 23:19:44 +0000624 case 1:
625 reg = CONFIG_PHY1_ADDR;
626 break;
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200627#endif
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200628#if defined (CONFIG_440GX)
wdenk544e9732004-02-06 23:19:44 +0000629 case 2:
630 reg = CONFIG_PHY2_ADDR;
631 break;
632 case 3:
633 reg = CONFIG_PHY3_ADDR;
634 break;
635#endif
636 default:
637 reg = CONFIG_PHY_ADDR;
638 break;
639 }
640
wdenk56ed43e2004-02-22 23:46:08 +0000641 bis->bi_phynum[devnum] = reg;
642
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200643#if defined(CONFIG_PHY_RESET)
wdenk97e8bda2004-09-29 22:43:59 +0000644 /*
645 * Reset the phy, only if its the first time through
646 * otherwise, just check the speeds & feeds
647 */
648 if (hw_p->first_init == 0) {
Stefan Roese21df1a42006-11-27 14:46:06 +0100649#if defined(CONFIG_M88E1111_PHY)
Stefan Roese42fbddd2006-09-07 11:51:23 +0200650 miiphy_write (dev->name, reg, 0x14, 0x0ce3);
651 miiphy_write (dev->name, reg, 0x18, 0x4101);
652 miiphy_write (dev->name, reg, 0x09, 0x0e00);
653 miiphy_write (dev->name, reg, 0x04, 0x01e1);
654#endif
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200655 miiphy_reset (dev->name, reg);
wdenk544e9732004-02-06 23:19:44 +0000656
Stefan Roese42fbddd2006-09-07 11:51:23 +0200657#if defined(CONFIG_440GX) || \
658 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roese153b3e22007-10-05 17:10:59 +0200659 defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
660 defined(CONFIG_405EX)
Stefan Roese42fbddd2006-09-07 11:51:23 +0200661
wdenk00fe1612004-03-14 00:07:33 +0000662#if defined(CONFIG_CIS8201_PHY)
wdenk7ad5e4c2004-04-25 15:41:35 +0000663 /*
Stefan Roese363330b2005-08-04 17:09:16 +0200664 * Cicada 8201 PHY needs to have an extended register whacked
665 * for RGMII mode.
wdenk7ad5e4c2004-04-25 15:41:35 +0000666 */
Stefan Roese42fbddd2006-09-07 11:51:23 +0200667 if (((devnum == 2) || (devnum == 3)) && (4 == ethgroup)) {
Stefan Roese5ff4c3f2005-08-15 12:31:23 +0200668#if defined(CONFIG_CIS8201_SHORT_ETCH)
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200669 miiphy_write (dev->name, reg, 23, 0x1300);
Stefan Roese5ff4c3f2005-08-15 12:31:23 +0200670#else
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200671 miiphy_write (dev->name, reg, 23, 0x1000);
Stefan Roese5ff4c3f2005-08-15 12:31:23 +0200672#endif
Stefan Roese363330b2005-08-04 17:09:16 +0200673 /*
674 * Vitesse VSC8201/Cicada CIS8201 errata:
675 * Interoperability problem with Intel 82547EI phys
676 * This work around (provided by Vitesse) changes
677 * the default timer convergence from 8ms to 12ms
678 */
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200679 miiphy_write (dev->name, reg, 0x1f, 0x2a30);
680 miiphy_write (dev->name, reg, 0x08, 0x0200);
681 miiphy_write (dev->name, reg, 0x1f, 0x52b5);
682 miiphy_write (dev->name, reg, 0x02, 0x0004);
683 miiphy_write (dev->name, reg, 0x01, 0x0671);
684 miiphy_write (dev->name, reg, 0x00, 0x8fae);
685 miiphy_write (dev->name, reg, 0x1f, 0x2a30);
686 miiphy_write (dev->name, reg, 0x08, 0x0000);
687 miiphy_write (dev->name, reg, 0x1f, 0x0000);
Stefan Roese363330b2005-08-04 17:09:16 +0200688 /* end Vitesse/Cicada errata */
689 }
wdenk00fe1612004-03-14 00:07:33 +0000690#endif
Stefan Roese8d982302007-01-18 10:25:34 +0100691
692#if defined(CONFIG_ET1011C_PHY)
693 /*
694 * Agere ET1011c PHY needs to have an extended register whacked
695 * for RGMII mode.
696 */
697 if (((devnum == 2) || (devnum ==3)) && (4 == ethgroup)) {
698 miiphy_read (dev->name, reg, 0x16, &reg_short);
699 reg_short &= ~(0x7);
700 reg_short |= 0x6; /* RGMII DLL Delay*/
701 miiphy_write (dev->name, reg, 0x16, reg_short);
702
703 miiphy_read (dev->name, reg, 0x17, &reg_short);
704 reg_short &= ~(0x40);
705 miiphy_write (dev->name, reg, 0x17, reg_short);
706
707 miiphy_write(dev->name, reg, 0x1c, 0x74f0);
708 }
709#endif
710
wdenked2ac4b2004-03-14 18:23:55 +0000711#endif
wdenk97e8bda2004-09-29 22:43:59 +0000712 /* Start/Restart autonegotiation */
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200713 phy_setup_aneg (dev->name, reg);
wdenk97e8bda2004-09-29 22:43:59 +0000714 udelay (1000);
715 }
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200716#endif /* defined(CONFIG_PHY_RESET) */
wdenk544e9732004-02-06 23:19:44 +0000717
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200718 miiphy_read (dev->name, reg, PHY_BMSR, &reg_short);
wdenk544e9732004-02-06 23:19:44 +0000719
720 /*
wdenk00fe1612004-03-14 00:07:33 +0000721 * Wait if PHY is capable of autonegotiation and autonegotiation is not complete
wdenk544e9732004-02-06 23:19:44 +0000722 */
723 if ((reg_short & PHY_BMSR_AUTN_ABLE)
724 && !(reg_short & PHY_BMSR_AUTN_COMP)) {
725 puts ("Waiting for PHY auto negotiation to complete");
726 i = 0;
727 while (!(reg_short & PHY_BMSR_AUTN_COMP)) {
728 /*
729 * Timeout reached ?
730 */
731 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
732 puts (" TIMEOUT !\n");
733 break;
734 }
735
736 if ((i++ % 1000) == 0) {
737 putc ('.');
738 }
739 udelay (1000); /* 1 ms */
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200740 miiphy_read (dev->name, reg, PHY_BMSR, &reg_short);
wdenk544e9732004-02-06 23:19:44 +0000741
742 }
743 puts (" done\n");
744 udelay (500000); /* another 500 ms (results in faster booting) */
745 }
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200746#endif /* #ifndef CONFIG_CS8952_PHY */
747
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200748 speed = miiphy_speed (dev->name, reg);
749 duplex = miiphy_duplex (dev->name, reg);
wdenk544e9732004-02-06 23:19:44 +0000750
751 if (hw_p->print_speed) {
752 hw_p->print_speed = 0;
Stefan Roese8d982302007-01-18 10:25:34 +0100753 printf ("ENET Speed is %d Mbps - %s duplex connection (EMAC%d)\n",
754 (int) speed, (duplex == HALF) ? "HALF" : "FULL",
755 hw_p->devnum);
wdenk544e9732004-02-06 23:19:44 +0000756 }
757
Stefan Roese42fbddd2006-09-07 11:51:23 +0200758#if defined(CONFIG_440) && !defined(CONFIG_440SP) && !defined(CONFIG_440SPE) && \
759 !defined(CONFIG_440EPX) && !defined(CONFIG_440GRX)
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200760#if defined(CONFIG_440EP) || defined(CONFIG_440GR)
Stefan Roese326c9712005-08-01 16:41:48 +0200761 mfsdr(sdr_mfr, reg);
762 if (speed == 100) {
763 reg = (reg & ~SDR0_MFR_ZMII_MODE_MASK) | SDR0_MFR_ZMII_MODE_RMII_100M;
764 } else {
765 reg = (reg & ~SDR0_MFR_ZMII_MODE_MASK) | SDR0_MFR_ZMII_MODE_RMII_10M;
766 }
767 mtsdr(sdr_mfr, reg);
768#endif
Stefan Roese797d8572005-08-11 17:56:56 +0200769
wdenk544e9732004-02-06 23:19:44 +0000770 /* Set ZMII/RGMII speed according to the phy link speed */
771 reg = in32 (ZMII_SSR);
wdenked2ac4b2004-03-14 18:23:55 +0000772 if ( (speed == 100) || (speed == 1000) )
wdenk544e9732004-02-06 23:19:44 +0000773 out32 (ZMII_SSR, reg | (ZMII_SSR_SP << ZMII_SSR_V (devnum)));
774 else
Stefan Roese797d8572005-08-11 17:56:56 +0200775 out32 (ZMII_SSR, reg & (~(ZMII_SSR_SP << ZMII_SSR_V (devnum))));
wdenk544e9732004-02-06 23:19:44 +0000776
777 if ((devnum == 2) || (devnum == 3)) {
778 if (speed == 1000)
779 reg = (RGMII_SSR_SP_1000MBPS << RGMII_SSR_V (devnum));
780 else if (speed == 100)
781 reg = (RGMII_SSR_SP_100MBPS << RGMII_SSR_V (devnum));
Stefan Roese42fbddd2006-09-07 11:51:23 +0200782 else if (speed == 10)
wdenk544e9732004-02-06 23:19:44 +0000783 reg = (RGMII_SSR_SP_10MBPS << RGMII_SSR_V (devnum));
Stefan Roese42fbddd2006-09-07 11:51:23 +0200784 else {
785 printf("Error in RGMII Speed\n");
786 return -1;
787 }
wdenk544e9732004-02-06 23:19:44 +0000788 out32 (RGMII_SSR, reg);
789 }
Stefan Roese99644742005-11-29 18:18:21 +0100790#endif /* defined(CONFIG_440) && !defined(CONFIG_440SP) */
wdenk544e9732004-02-06 23:19:44 +0000791
Stefan Roese153b3e22007-10-05 17:10:59 +0200792#if defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
793 defined(CONFIG_405EX)
Stefan Roese42fbddd2006-09-07 11:51:23 +0200794 if (speed == 1000)
795 reg = (RGMII_SSR_SP_1000MBPS << RGMII_SSR_V (devnum));
796 else if (speed == 100)
797 reg = (RGMII_SSR_SP_100MBPS << RGMII_SSR_V (devnum));
798 else if (speed == 10)
799 reg = (RGMII_SSR_SP_10MBPS << RGMII_SSR_V (devnum));
800 else {
801 printf("Error in RGMII Speed\n");
802 return -1;
803 }
804 out32 (RGMII_SSR, reg);
805#endif
806
wdenk544e9732004-02-06 23:19:44 +0000807 /* set the Mal configuration reg */
Stefan Roese42fbddd2006-09-07 11:51:23 +0200808#if defined(CONFIG_440GX) || \
809 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roese153b3e22007-10-05 17:10:59 +0200810 defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
811 defined(CONFIG_405EX)
Stefan Roese363330b2005-08-04 17:09:16 +0200812 mtdcr (malmcr, MAL_CR_PLBB | MAL_CR_OPBBL | MAL_CR_LEA |
813 MAL_CR_PLBLT_DEFAULT | MAL_CR_EOPIE | 0x00330000);
814#else
815 mtdcr (malmcr, MAL_CR_PLBB | MAL_CR_OPBBL | MAL_CR_LEA | MAL_CR_PLBLT_DEFAULT);
wdenk544e9732004-02-06 23:19:44 +0000816 /* Errata 1.12: MAL_1 -- Disable MAL bursting */
Stefan Roese363330b2005-08-04 17:09:16 +0200817 if (get_pvr() == PVR_440GP_RB) {
818 mtdcr (malmcr, mfdcr(malmcr) & ~MAL_CR_PLBB);
819 }
820#endif
wdenk544e9732004-02-06 23:19:44 +0000821
822 /* Free "old" buffers */
823 if (hw_p->alloc_tx_buf)
824 free (hw_p->alloc_tx_buf);
825 if (hw_p->alloc_rx_buf)
826 free (hw_p->alloc_rx_buf);
827
828 /*
829 * Malloc MAL buffer desciptors, make sure they are
830 * aligned on cache line boundary size
831 * (401/403/IOP480 = 16, 405 = 32)
832 * and doesn't cross cache block boundaries.
833 */
834 hw_p->alloc_tx_buf =
835 (mal_desc_t *) malloc ((sizeof (mal_desc_t) * NUM_TX_BUFF) +
836 ((2 * CFG_CACHELINE_SIZE) - 2));
Stefan Roese5ff4c3f2005-08-15 12:31:23 +0200837 if (NULL == hw_p->alloc_tx_buf)
838 return -1;
wdenk544e9732004-02-06 23:19:44 +0000839 if (((int) hw_p->alloc_tx_buf & CACHELINE_MASK) != 0) {
840 hw_p->tx =
841 (mal_desc_t *) ((int) hw_p->alloc_tx_buf +
842 CFG_CACHELINE_SIZE -
843 ((int) hw_p->
844 alloc_tx_buf & CACHELINE_MASK));
845 } else {
846 hw_p->tx = hw_p->alloc_tx_buf;
847 }
848
849 hw_p->alloc_rx_buf =
850 (mal_desc_t *) malloc ((sizeof (mal_desc_t) * NUM_RX_BUFF) +
851 ((2 * CFG_CACHELINE_SIZE) - 2));
Stefan Roese5ff4c3f2005-08-15 12:31:23 +0200852 if (NULL == hw_p->alloc_rx_buf) {
853 free(hw_p->alloc_tx_buf);
854 hw_p->alloc_tx_buf = NULL;
855 return -1;
856 }
857
wdenk544e9732004-02-06 23:19:44 +0000858 if (((int) hw_p->alloc_rx_buf & CACHELINE_MASK) != 0) {
859 hw_p->rx =
860 (mal_desc_t *) ((int) hw_p->alloc_rx_buf +
861 CFG_CACHELINE_SIZE -
862 ((int) hw_p->
863 alloc_rx_buf & CACHELINE_MASK));
864 } else {
865 hw_p->rx = hw_p->alloc_rx_buf;
866 }
867
868 for (i = 0; i < NUM_TX_BUFF; i++) {
869 hw_p->tx[i].ctrl = 0;
870 hw_p->tx[i].data_len = 0;
Stefan Roese5ff4c3f2005-08-15 12:31:23 +0200871 if (hw_p->first_init == 0) {
wdenk544e9732004-02-06 23:19:44 +0000872 hw_p->txbuf_ptr =
873 (char *) malloc (ENET_MAX_MTU_ALIGNED);
Stefan Roese5ff4c3f2005-08-15 12:31:23 +0200874 if (NULL == hw_p->txbuf_ptr) {
875 free(hw_p->alloc_rx_buf);
876 free(hw_p->alloc_tx_buf);
877 hw_p->alloc_rx_buf = NULL;
878 hw_p->alloc_tx_buf = NULL;
879 for(j = 0; j < i; j++) {
880 free(hw_p->tx[i].data_ptr);
881 hw_p->tx[i].data_ptr = NULL;
882 }
883 }
884 }
wdenk544e9732004-02-06 23:19:44 +0000885 hw_p->tx[i].data_ptr = hw_p->txbuf_ptr;
886 if ((NUM_TX_BUFF - 1) == i)
887 hw_p->tx[i].ctrl |= MAL_TX_CTRL_WRAP;
888 hw_p->tx_run[i] = -1;
889#if 0
890 printf ("TX_BUFF %d @ 0x%08lx\n", i,
891 (ulong) hw_p->tx[i].data_ptr);
892#endif
893 }
894
895 for (i = 0; i < NUM_RX_BUFF; i++) {
896 hw_p->rx[i].ctrl = 0;
897 hw_p->rx[i].data_len = 0;
Wolfgang Denk0cbaf642005-09-25 00:53:22 +0200898 /* rx[i].data_ptr = (char *) &rx_buff[i]; */
wdenk544e9732004-02-06 23:19:44 +0000899 hw_p->rx[i].data_ptr = (char *) NetRxPackets[i];
900 if ((NUM_RX_BUFF - 1) == i)
901 hw_p->rx[i].ctrl |= MAL_RX_CTRL_WRAP;
902 hw_p->rx[i].ctrl |= MAL_RX_CTRL_EMPTY | MAL_RX_CTRL_INTR;
903 hw_p->rx_ready[i] = -1;
904#if 0
Stefan Roese2a4a9432006-11-27 14:12:17 +0100905 printf ("RX_BUFF %d @ 0x%08lx\n", i, (ulong) hw_p->rx[i].data_ptr);
wdenk544e9732004-02-06 23:19:44 +0000906#endif
907 }
908
909 reg = 0x00000000;
910
911 reg |= dev->enetaddr[0]; /* set high address */
912 reg = reg << 8;
913 reg |= dev->enetaddr[1];
914
915 out32 (EMAC_IAH + hw_p->hw_addr, reg);
916
917 reg = 0x00000000;
918 reg |= dev->enetaddr[2]; /* set low address */
919 reg = reg << 8;
920 reg |= dev->enetaddr[3];
921 reg = reg << 8;
922 reg |= dev->enetaddr[4];
923 reg = reg << 8;
924 reg |= dev->enetaddr[5];
925
926 out32 (EMAC_IAL + hw_p->hw_addr, reg);
927
928 switch (devnum) {
929 case 1:
930 /* setup MAL tx & rx channel pointers */
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200931#if defined (CONFIG_405EP) || defined (CONFIG_440EP) || defined (CONFIG_440GR)
Stefan Roese326c9712005-08-01 16:41:48 +0200932 mtdcr (maltxctp2r, hw_p->tx);
933#else
wdenk544e9732004-02-06 23:19:44 +0000934 mtdcr (maltxctp1r, hw_p->tx);
Stefan Roese326c9712005-08-01 16:41:48 +0200935#endif
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200936#if defined(CONFIG_440)
Stefan Roese326c9712005-08-01 16:41:48 +0200937 mtdcr (maltxbattr, 0x0);
wdenk544e9732004-02-06 23:19:44 +0000938 mtdcr (malrxbattr, 0x0);
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200939#endif
wdenk544e9732004-02-06 23:19:44 +0000940 mtdcr (malrxctp1r, hw_p->rx);
941 /* set RX buffer size */
942 mtdcr (malrcbs1, ENET_MAX_MTU_ALIGNED / 16);
943 break;
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200944#if defined (CONFIG_440GX)
wdenk544e9732004-02-06 23:19:44 +0000945 case 2:
946 /* setup MAL tx & rx channel pointers */
947 mtdcr (maltxbattr, 0x0);
wdenk544e9732004-02-06 23:19:44 +0000948 mtdcr (malrxbattr, 0x0);
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200949 mtdcr (maltxctp2r, hw_p->tx);
wdenk544e9732004-02-06 23:19:44 +0000950 mtdcr (malrxctp2r, hw_p->rx);
951 /* set RX buffer size */
952 mtdcr (malrcbs2, ENET_MAX_MTU_ALIGNED / 16);
953 break;
954 case 3:
955 /* setup MAL tx & rx channel pointers */
956 mtdcr (maltxbattr, 0x0);
957 mtdcr (maltxctp3r, hw_p->tx);
958 mtdcr (malrxbattr, 0x0);
959 mtdcr (malrxctp3r, hw_p->rx);
960 /* set RX buffer size */
961 mtdcr (malrcbs3, ENET_MAX_MTU_ALIGNED / 16);
962 break;
Stefan Roese797d8572005-08-11 17:56:56 +0200963#endif /* CONFIG_440GX */
wdenk544e9732004-02-06 23:19:44 +0000964 case 0:
965 default:
966 /* setup MAL tx & rx channel pointers */
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200967#if defined(CONFIG_440)
wdenk544e9732004-02-06 23:19:44 +0000968 mtdcr (maltxbattr, 0x0);
wdenk544e9732004-02-06 23:19:44 +0000969 mtdcr (malrxbattr, 0x0);
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200970#endif
971 mtdcr (maltxctp0r, hw_p->tx);
wdenk544e9732004-02-06 23:19:44 +0000972 mtdcr (malrxctp0r, hw_p->rx);
973 /* set RX buffer size */
974 mtdcr (malrcbs0, ENET_MAX_MTU_ALIGNED / 16);
975 break;
976 }
977
978 /* Enable MAL transmit and receive channels */
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200979#if defined(CONFIG_405EP) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
Stefan Roese326c9712005-08-01 16:41:48 +0200980 mtdcr (maltxcasr, (MAL_TXRX_CASR >> (hw_p->devnum*2)));
981#else
wdenk544e9732004-02-06 23:19:44 +0000982 mtdcr (maltxcasr, (MAL_TXRX_CASR >> hw_p->devnum));
Stefan Roese326c9712005-08-01 16:41:48 +0200983#endif
wdenk544e9732004-02-06 23:19:44 +0000984 mtdcr (malrxcasr, (MAL_TXRX_CASR >> hw_p->devnum));
985
986 /* set transmit enable & receive enable */
987 out32 (EMAC_M0 + hw_p->hw_addr, EMAC_M0_TXE | EMAC_M0_RXE);
988
989 /* set receive fifo to 4k and tx fifo to 2k */
990 mode_reg = in32 (EMAC_M1 + hw_p->hw_addr);
991 mode_reg |= EMAC_M1_RFS_4K | EMAC_M1_TX_FIFO_2K;
992
993 /* set speed */
Stefan Roese99644742005-11-29 18:18:21 +0100994 if (speed == _1000BASET) {
Stefan Roese43867c82007-10-02 11:44:46 +0200995#if defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
996 defined(CONFIG_440SP) || defined(CONFIG_440SPE)
Stefan Roese99644742005-11-29 18:18:21 +0100997 unsigned long pfc1;
Stefan Roese42fbddd2006-09-07 11:51:23 +0200998
Stefan Roese99644742005-11-29 18:18:21 +0100999 mfsdr (sdr_pfc1, pfc1);
1000 pfc1 |= SDR0_PFC1_EM_1000;
1001 mtsdr (sdr_pfc1, pfc1);
1002#endif
wdenked2ac4b2004-03-14 18:23:55 +00001003 mode_reg = mode_reg | EMAC_M1_MF_1000MBPS | EMAC_M1_IST;
Stefan Roese99644742005-11-29 18:18:21 +01001004 } else if (speed == _100BASET)
wdenk544e9732004-02-06 23:19:44 +00001005 mode_reg = mode_reg | EMAC_M1_MF_100MBPS | EMAC_M1_IST;
1006 else
1007 mode_reg = mode_reg & ~0x00C00000; /* 10 MBPS */
1008 if (duplex == FULL)
1009 mode_reg = mode_reg | 0x80000000 | EMAC_M1_IST;
1010
1011 out32 (EMAC_M1 + hw_p->hw_addr, mode_reg);
1012
1013 /* Enable broadcast and indvidual address */
1014 /* TBS: enabling runts as some misbehaved nics will send runts */
1015 out32 (EMAC_RXM + hw_p->hw_addr, EMAC_RMR_BAE | EMAC_RMR_IAE);
1016
1017 /* we probably need to set the tx mode1 reg? maybe at tx time */
1018
1019 /* set transmit request threshold register */
1020 out32 (EMAC_TRTR + hw_p->hw_addr, 0x18000000); /* 256 byte threshold */
1021
Wolfgang Denk0cbaf642005-09-25 00:53:22 +02001022 /* set receive low/high water mark register */
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001023#if defined(CONFIG_440)
Marian Balakowicz49d0eee2006-06-30 16:30:46 +02001024 /* 440s has a 64 byte burst length */
wdenk544e9732004-02-06 23:19:44 +00001025 out32 (EMAC_RX_HI_LO_WMARK + hw_p->hw_addr, 0x80009000);
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001026#else
1027 /* 405s have a 16 byte burst length */
1028 out32 (EMAC_RX_HI_LO_WMARK + hw_p->hw_addr, 0x0f002000);
1029#endif /* defined(CONFIG_440) */
wdenk544e9732004-02-06 23:19:44 +00001030 out32 (EMAC_TXM1 + hw_p->hw_addr, 0xf8640000);
1031
1032 /* Set fifo limit entry in tx mode 0 */
1033 out32 (EMAC_TXM0 + hw_p->hw_addr, 0x00000003);
1034 /* Frame gap set */
1035 out32 (EMAC_I_FRAME_GAP_REG + hw_p->hw_addr, 0x00000008);
1036
1037 /* Set EMAC IER */
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001038 hw_p->emac_ier = EMAC_ISR_PTLE | EMAC_ISR_BFCS | EMAC_ISR_ORE | EMAC_ISR_IRE;
wdenk544e9732004-02-06 23:19:44 +00001039 if (speed == _100BASET)
1040 hw_p->emac_ier = hw_p->emac_ier | EMAC_ISR_SYE;
1041
1042 out32 (EMAC_ISR + hw_p->hw_addr, 0xffffffff); /* clear pending interrupts */
1043 out32 (EMAC_IER + hw_p->hw_addr, hw_p->emac_ier);
1044
1045 if (hw_p->first_init == 0) {
1046 /*
1047 * Connect interrupt service routines
1048 */
Stefan Roese153b3e22007-10-05 17:10:59 +02001049 irq_install_handler(ETH_IRQ_NUM(hw_p->devnum),
1050 (interrupt_handler_t *) enetInt, dev);
wdenk544e9732004-02-06 23:19:44 +00001051 }
wdenk544e9732004-02-06 23:19:44 +00001052
1053 mtmsr (msr); /* enable interrupts again */
1054
1055 hw_p->bis = bis;
1056 hw_p->first_init = 1;
1057
1058 return (1);
1059}
1060
1061
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001062static int ppc_4xx_eth_send (struct eth_device *dev, volatile void *ptr,
wdenk544e9732004-02-06 23:19:44 +00001063 int len)
1064{
1065 struct enet_frame *ef_ptr;
1066 ulong time_start, time_now;
1067 unsigned long temp_txm0;
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001068 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenk544e9732004-02-06 23:19:44 +00001069
1070 ef_ptr = (struct enet_frame *) ptr;
1071
1072 /*-----------------------------------------------------------------------+
1073 * Copy in our address into the frame.
1074 *-----------------------------------------------------------------------*/
1075 (void) memcpy (ef_ptr->source_addr, dev->enetaddr, ENET_ADDR_LENGTH);
1076
1077 /*-----------------------------------------------------------------------+
1078 * If frame is too long or too short, modify length.
1079 *-----------------------------------------------------------------------*/
1080 /* TBS: where does the fragment go???? */
1081 if (len > ENET_MAX_MTU)
1082 len = ENET_MAX_MTU;
1083
1084 /* memcpy ((void *) &tx_buff[tx_slot], (const void *) ptr, len); */
1085 memcpy ((void *) hw_p->txbuf_ptr, (const void *) ptr, len);
1086
1087 /*-----------------------------------------------------------------------+
1088 * set TX Buffer busy, and send it
1089 *-----------------------------------------------------------------------*/
1090 hw_p->tx[hw_p->tx_slot].ctrl = (MAL_TX_CTRL_LAST |
1091 EMAC_TX_CTRL_GFCS | EMAC_TX_CTRL_GP) &
1092 ~(EMAC_TX_CTRL_ISA | EMAC_TX_CTRL_RSA);
1093 if ((NUM_TX_BUFF - 1) == hw_p->tx_slot)
1094 hw_p->tx[hw_p->tx_slot].ctrl |= MAL_TX_CTRL_WRAP;
1095
1096 hw_p->tx[hw_p->tx_slot].data_len = (short) len;
1097 hw_p->tx[hw_p->tx_slot].ctrl |= MAL_TX_CTRL_READY;
1098
1099 __asm__ volatile ("eieio");
1100
1101 out32 (EMAC_TXM0 + hw_p->hw_addr,
1102 in32 (EMAC_TXM0 + hw_p->hw_addr) | EMAC_TXM0_GNP0);
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001103#ifdef INFO_4XX_ENET
wdenk544e9732004-02-06 23:19:44 +00001104 hw_p->stats.pkts_tx++;
1105#endif
1106
1107 /*-----------------------------------------------------------------------+
1108 * poll unitl the packet is sent and then make sure it is OK
1109 *-----------------------------------------------------------------------*/
1110 time_start = get_timer (0);
1111 while (1) {
1112 temp_txm0 = in32 (EMAC_TXM0 + hw_p->hw_addr);
1113 /* loop until either TINT turns on or 3 seconds elapse */
1114 if ((temp_txm0 & EMAC_TXM0_GNP0) != 0) {
1115 /* transmit is done, so now check for errors
1116 * If there is an error, an interrupt should
1117 * happen when we return
1118 */
1119 time_now = get_timer (0);
1120 if ((time_now - time_start) > 3000) {
1121 return (-1);
1122 }
1123 } else {
1124 return (len);
1125 }
1126 }
1127}
1128
Stefan Roese99644742005-11-29 18:18:21 +01001129
Stefan Roese153b3e22007-10-05 17:10:59 +02001130#if defined (CONFIG_440) || defined(CONFIG_405EX)
wdenk544e9732004-02-06 23:19:44 +00001131
Marian Balakowicz49d0eee2006-06-30 16:30:46 +02001132#if defined(CONFIG_440SP) || defined(CONFIG_440SPE)
Stefan Roese99644742005-11-29 18:18:21 +01001133/*
1134 * Hack: On 440SP all enet irq sources are located on UIC1
1135 * Needs some cleanup. --sr
1136 */
1137#define UIC0MSR uic1msr
1138#define UIC0SR uic1sr
1139#else
1140#define UIC0MSR uic0msr
1141#define UIC0SR uic0sr
1142#endif
1143
Stefan Roese153b3e22007-10-05 17:10:59 +02001144#if defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
1145 defined(CONFIG_405EX)
Stefan Roese42fbddd2006-09-07 11:51:23 +02001146#define UICMSR_ETHX uic0msr
1147#define UICSR_ETHX uic0sr
1148#else
1149#define UICMSR_ETHX uic1msr
1150#define UICSR_ETHX uic1sr
1151#endif
1152
wdenk544e9732004-02-06 23:19:44 +00001153int enetInt (struct eth_device *dev)
1154{
1155 int serviced;
1156 int rc = -1; /* default to not us */
1157 unsigned long mal_isr;
1158 unsigned long emac_isr = 0;
1159 unsigned long mal_rx_eob;
1160 unsigned long my_uic0msr, my_uic1msr;
Stefan Roese42fbddd2006-09-07 11:51:23 +02001161 unsigned long my_uicmsr_ethx;
wdenk544e9732004-02-06 23:19:44 +00001162
Stefan Roeseb30f2a12005-08-08 12:42:22 +02001163#if defined(CONFIG_440GX)
wdenk544e9732004-02-06 23:19:44 +00001164 unsigned long my_uic2msr;
1165#endif
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001166 EMAC_4XX_HW_PST hw_p;
wdenk544e9732004-02-06 23:19:44 +00001167
1168 /*
1169 * Because the mal is generic, we need to get the current
1170 * eth device
1171 */
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001172#if defined(CONFIG_NET_MULTI)
1173 dev = eth_get_dev();
1174#else
1175 dev = emac0_dev;
1176#endif
wdenk544e9732004-02-06 23:19:44 +00001177
1178 hw_p = dev->priv;
1179
wdenk544e9732004-02-06 23:19:44 +00001180 /* enter loop that stays in interrupt code until nothing to service */
1181 do {
1182 serviced = 0;
1183
Stefan Roese99644742005-11-29 18:18:21 +01001184 my_uic0msr = mfdcr (UIC0MSR);
wdenk544e9732004-02-06 23:19:44 +00001185 my_uic1msr = mfdcr (uic1msr);
Stefan Roeseb30f2a12005-08-08 12:42:22 +02001186#if defined(CONFIG_440GX)
wdenk544e9732004-02-06 23:19:44 +00001187 my_uic2msr = mfdcr (uic2msr);
1188#endif
Stefan Roese42fbddd2006-09-07 11:51:23 +02001189 my_uicmsr_ethx = mfdcr (UICMSR_ETHX);
1190
wdenk544e9732004-02-06 23:19:44 +00001191 if (!(my_uic0msr & (UIC_MRE | UIC_MTE))
Stefan Roese42fbddd2006-09-07 11:51:23 +02001192 && !(my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))
1193 && !(my_uicmsr_ethx & (UIC_ETH0 | UIC_ETH1))) {
wdenk544e9732004-02-06 23:19:44 +00001194 /* not for us */
1195 return (rc);
1196 }
Stefan Roeseb30f2a12005-08-08 12:42:22 +02001197#if defined (CONFIG_440GX)
wdenk544e9732004-02-06 23:19:44 +00001198 if (!(my_uic0msr & (UIC_MRE | UIC_MTE))
1199 && !(my_uic2msr & (UIC_ETH2 | UIC_ETH3))) {
1200 /* not for us */
1201 return (rc);
1202 }
1203#endif
1204 /* get and clear controller status interrupts */
1205 /* look at Mal and EMAC interrupts */
1206 if ((my_uic0msr & (UIC_MRE | UIC_MTE))
1207 || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
1208 /* we have a MAL interrupt */
1209 mal_isr = mfdcr (malesr);
1210 /* look for mal error */
1211 if (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE)) {
Stefan Roese42fbddd2006-09-07 11:51:23 +02001212 mal_err (dev, mal_isr, my_uic1msr, MAL_UIC_DEF, MAL_UIC_ERR);
wdenk544e9732004-02-06 23:19:44 +00001213 serviced = 1;
1214 rc = 0;
1215 }
1216 }
1217
1218 /* port by port dispatch of emac interrupts */
1219 if (hw_p->devnum == 0) {
Stefan Roese42fbddd2006-09-07 11:51:23 +02001220 if (UIC_ETH0 & my_uicmsr_ethx) { /* look for EMAC errors */
wdenk544e9732004-02-06 23:19:44 +00001221 emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
1222 if ((hw_p->emac_ier & emac_isr) != 0) {
1223 emac_err (dev, emac_isr);
1224 serviced = 1;
1225 rc = 0;
1226 }
1227 }
1228 if ((hw_p->emac_ier & emac_isr)
1229 || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
Stefan Roese99644742005-11-29 18:18:21 +01001230 mtdcr (UIC0SR, UIC_MRE | UIC_MTE); /* Clear */
Stefan Roese42fbddd2006-09-07 11:51:23 +02001231 mtdcr (uic1sr, UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
1232 mtdcr (UICSR_ETHX, UIC_ETH0); /* Clear */
wdenk544e9732004-02-06 23:19:44 +00001233 return (rc); /* we had errors so get out */
1234 }
1235 }
1236
Stefan Roese99644742005-11-29 18:18:21 +01001237#if !defined(CONFIG_440SP)
wdenk544e9732004-02-06 23:19:44 +00001238 if (hw_p->devnum == 1) {
Stefan Roese42fbddd2006-09-07 11:51:23 +02001239 if (UIC_ETH1 & my_uicmsr_ethx) { /* look for EMAC errors */
wdenk544e9732004-02-06 23:19:44 +00001240 emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
1241 if ((hw_p->emac_ier & emac_isr) != 0) {
1242 emac_err (dev, emac_isr);
1243 serviced = 1;
1244 rc = 0;
1245 }
1246 }
1247 if ((hw_p->emac_ier & emac_isr)
1248 || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
Stefan Roese99644742005-11-29 18:18:21 +01001249 mtdcr (UIC0SR, UIC_MRE | UIC_MTE); /* Clear */
Stefan Roese42fbddd2006-09-07 11:51:23 +02001250 mtdcr (uic1sr, UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
1251 mtdcr (UICSR_ETHX, UIC_ETH1); /* Clear */
wdenk544e9732004-02-06 23:19:44 +00001252 return (rc); /* we had errors so get out */
1253 }
1254 }
Stefan Roeseb30f2a12005-08-08 12:42:22 +02001255#if defined (CONFIG_440GX)
wdenk544e9732004-02-06 23:19:44 +00001256 if (hw_p->devnum == 2) {
1257 if (UIC_ETH2 & my_uic2msr) { /* look for EMAC errors */
1258 emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
1259 if ((hw_p->emac_ier & emac_isr) != 0) {
1260 emac_err (dev, emac_isr);
1261 serviced = 1;
1262 rc = 0;
1263 }
1264 }
1265 if ((hw_p->emac_ier & emac_isr)
1266 || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
Stefan Roese99644742005-11-29 18:18:21 +01001267 mtdcr (UIC0SR, UIC_MRE | UIC_MTE); /* Clear */
wdenk544e9732004-02-06 23:19:44 +00001268 mtdcr (uic1sr, UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
1269 mtdcr (uic2sr, UIC_ETH2);
1270 return (rc); /* we had errors so get out */
1271 }
1272 }
1273
1274 if (hw_p->devnum == 3) {
1275 if (UIC_ETH3 & my_uic2msr) { /* look for EMAC errors */
1276 emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
1277 if ((hw_p->emac_ier & emac_isr) != 0) {
1278 emac_err (dev, emac_isr);
1279 serviced = 1;
1280 rc = 0;
1281 }
1282 }
1283 if ((hw_p->emac_ier & emac_isr)
1284 || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
Stefan Roese99644742005-11-29 18:18:21 +01001285 mtdcr (UIC0SR, UIC_MRE | UIC_MTE); /* Clear */
wdenk544e9732004-02-06 23:19:44 +00001286 mtdcr (uic1sr, UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
1287 mtdcr (uic2sr, UIC_ETH3);
1288 return (rc); /* we had errors so get out */
1289 }
1290 }
Stefan Roeseb30f2a12005-08-08 12:42:22 +02001291#endif /* CONFIG_440GX */
Stefan Roese99644742005-11-29 18:18:21 +01001292#endif /* !CONFIG_440SP */
1293
wdenk544e9732004-02-06 23:19:44 +00001294 /* handle MAX TX EOB interrupt from a tx */
1295 if (my_uic0msr & UIC_MTE) {
1296 mal_rx_eob = mfdcr (maltxeobisr);
1297 mtdcr (maltxeobisr, mal_rx_eob);
Stefan Roese99644742005-11-29 18:18:21 +01001298 mtdcr (UIC0SR, UIC_MTE);
wdenk544e9732004-02-06 23:19:44 +00001299 }
1300 /* handle MAL RX EOB interupt from a receive */
wdenk7ad5e4c2004-04-25 15:41:35 +00001301 /* check for EOB on valid channels */
wdenk544e9732004-02-06 23:19:44 +00001302 if (my_uic0msr & UIC_MRE) {
1303 mal_rx_eob = mfdcr (malrxeobisr);
Wolfgang Denk0cbaf642005-09-25 00:53:22 +02001304 if ((mal_rx_eob & (0x80000000 >> hw_p->devnum)) != 0) { /* call emac routine for channel x */
wdenk544e9732004-02-06 23:19:44 +00001305 /* clear EOB
1306 mtdcr(malrxeobisr, mal_rx_eob); */
1307 enet_rcv (dev, emac_isr);
1308 /* indicate that we serviced an interrupt */
1309 serviced = 1;
1310 rc = 0;
1311 }
1312 }
Stefan Roese99644742005-11-29 18:18:21 +01001313
1314 mtdcr (UIC0SR, UIC_MRE); /* Clear */
wdenk544e9732004-02-06 23:19:44 +00001315 mtdcr (uic1sr, UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
1316 switch (hw_p->devnum) {
1317 case 0:
Stefan Roese42fbddd2006-09-07 11:51:23 +02001318 mtdcr (UICSR_ETHX, UIC_ETH0);
wdenk544e9732004-02-06 23:19:44 +00001319 break;
1320 case 1:
Stefan Roese42fbddd2006-09-07 11:51:23 +02001321 mtdcr (UICSR_ETHX, UIC_ETH1);
wdenk544e9732004-02-06 23:19:44 +00001322 break;
Stefan Roeseb30f2a12005-08-08 12:42:22 +02001323#if defined (CONFIG_440GX)
wdenk544e9732004-02-06 23:19:44 +00001324 case 2:
1325 mtdcr (uic2sr, UIC_ETH2);
1326 break;
1327 case 3:
1328 mtdcr (uic2sr, UIC_ETH3);
1329 break;
Stefan Roeseb30f2a12005-08-08 12:42:22 +02001330#endif /* CONFIG_440GX */
wdenk544e9732004-02-06 23:19:44 +00001331 default:
1332 break;
1333 }
1334 } while (serviced);
1335
1336 return (rc);
1337}
1338
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001339#else /* CONFIG_440 */
1340
1341int enetInt (struct eth_device *dev)
1342{
1343 int serviced;
1344 int rc = -1; /* default to not us */
1345 unsigned long mal_isr;
1346 unsigned long emac_isr = 0;
1347 unsigned long mal_rx_eob;
1348 unsigned long my_uicmsr;
1349
1350 EMAC_4XX_HW_PST hw_p;
1351
1352 /*
1353 * Because the mal is generic, we need to get the current
1354 * eth device
1355 */
1356#if defined(CONFIG_NET_MULTI)
1357 dev = eth_get_dev();
1358#else
1359 dev = emac0_dev;
1360#endif
1361
1362 hw_p = dev->priv;
1363
1364 /* enter loop that stays in interrupt code until nothing to service */
1365 do {
1366 serviced = 0;
1367
1368 my_uicmsr = mfdcr (uicmsr);
1369
1370 if ((my_uicmsr & (MAL_UIC_DEF | EMAC_UIC_DEF)) == 0) { /* not for us */
1371 return (rc);
1372 }
1373 /* get and clear controller status interrupts */
1374 /* look at Mal and EMAC interrupts */
1375 if ((MAL_UIC_DEF & my_uicmsr) != 0) { /* we have a MAL interrupt */
1376 mal_isr = mfdcr (malesr);
1377 /* look for mal error */
1378 if ((my_uicmsr & MAL_UIC_ERR) != 0) {
1379 mal_err (dev, mal_isr, my_uicmsr, MAL_UIC_DEF, MAL_UIC_ERR);
1380 serviced = 1;
1381 rc = 0;
1382 }
1383 }
1384
1385 /* port by port dispatch of emac interrupts */
1386
1387 if ((SEL_UIC_DEF(hw_p->devnum) & my_uicmsr) != 0) { /* look for EMAC errors */
1388 emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
1389 if ((hw_p->emac_ier & emac_isr) != 0) {
1390 emac_err (dev, emac_isr);
1391 serviced = 1;
1392 rc = 0;
1393 }
1394 }
1395 if (((hw_p->emac_ier & emac_isr) != 0) || ((MAL_UIC_ERR & my_uicmsr) != 0)) {
1396 mtdcr (uicsr, MAL_UIC_DEF | SEL_UIC_DEF(hw_p->devnum)); /* Clear */
1397 return (rc); /* we had errors so get out */
1398 }
1399
1400 /* handle MAX TX EOB interrupt from a tx */
1401 if (my_uicmsr & UIC_MAL_TXEOB) {
1402 mal_rx_eob = mfdcr (maltxeobisr);
1403 mtdcr (maltxeobisr, mal_rx_eob);
1404 mtdcr (uicsr, UIC_MAL_TXEOB);
1405 }
1406 /* handle MAL RX EOB interupt from a receive */
1407 /* check for EOB on valid channels */
1408 if (my_uicmsr & UIC_MAL_RXEOB)
1409 {
1410 mal_rx_eob = mfdcr (malrxeobisr);
Wolfgang Denk0cbaf642005-09-25 00:53:22 +02001411 if ((mal_rx_eob & (0x80000000 >> hw_p->devnum)) != 0) { /* call emac routine for channel x */
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001412 /* clear EOB
1413 mtdcr(malrxeobisr, mal_rx_eob); */
1414 enet_rcv (dev, emac_isr);
1415 /* indicate that we serviced an interrupt */
1416 serviced = 1;
1417 rc = 0;
1418 }
1419 }
1420 mtdcr (uicsr, MAL_UIC_DEF|EMAC_UIC_DEF|EMAC_UIC_DEF1); /* Clear */
Stefan Roese17ffbc82007-03-21 13:38:59 +01001421#if defined(CONFIG_405EZ)
1422 mtsdr (sdricintstat, SDR_ICRX_STAT | SDR_ICTX0_STAT | SDR_ICTX1_STAT);
1423#endif /* defined(CONFIG_405EZ) */
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001424 }
1425 while (serviced);
1426
1427 return (rc);
1428}
1429
1430#endif /* CONFIG_440 */
1431
wdenk544e9732004-02-06 23:19:44 +00001432/*-----------------------------------------------------------------------------+
1433 * MAL Error Routine
1434 *-----------------------------------------------------------------------------*/
1435static void mal_err (struct eth_device *dev, unsigned long isr,
1436 unsigned long uic, unsigned long maldef,
1437 unsigned long mal_errr)
1438{
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001439 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenk544e9732004-02-06 23:19:44 +00001440
1441 mtdcr (malesr, isr); /* clear interrupt */
1442
1443 /* clear DE interrupt */
1444 mtdcr (maltxdeir, 0xC0000000);
1445 mtdcr (malrxdeir, 0x80000000);
1446
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001447#ifdef INFO_4XX_ENET
Wolfgang Denk0cbaf642005-09-25 00:53:22 +02001448 printf ("\nMAL error occured.... ISR = %lx UIC = = %lx MAL_DEF = %lx MAL_ERR= %lx \n", isr, uic, maldef, mal_errr);
wdenk544e9732004-02-06 23:19:44 +00001449#endif
1450
1451 eth_init (hw_p->bis); /* start again... */
1452}
1453
1454/*-----------------------------------------------------------------------------+
1455 * EMAC Error Routine
1456 *-----------------------------------------------------------------------------*/
1457static void emac_err (struct eth_device *dev, unsigned long isr)
1458{
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001459 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenk544e9732004-02-06 23:19:44 +00001460
1461 printf ("EMAC%d error occured.... ISR = %lx\n", hw_p->devnum, isr);
1462 out32 (EMAC_ISR + hw_p->hw_addr, isr);
1463}
1464
1465/*-----------------------------------------------------------------------------+
1466 * enet_rcv() handles the ethernet receive data
1467 *-----------------------------------------------------------------------------*/
1468static void enet_rcv (struct eth_device *dev, unsigned long malisr)
1469{
1470 struct enet_frame *ef_ptr;
1471 unsigned long data_len;
1472 unsigned long rx_eob_isr;
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001473 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenk544e9732004-02-06 23:19:44 +00001474
1475 int handled = 0;
1476 int i;
1477 int loop_count = 0;
1478
1479 rx_eob_isr = mfdcr (malrxeobisr);
1480 if ((0x80000000 >> hw_p->devnum) & rx_eob_isr) {
1481 /* clear EOB */
1482 mtdcr (malrxeobisr, rx_eob_isr);
1483
1484 /* EMAC RX done */
1485 while (1) { /* do all */
1486 i = hw_p->rx_slot;
1487
1488 if ((MAL_RX_CTRL_EMPTY & hw_p->rx[i].ctrl)
1489 || (loop_count >= NUM_RX_BUFF))
1490 break;
Stefan Roese09feb382007-07-12 16:32:08 +02001491
wdenk544e9732004-02-06 23:19:44 +00001492 loop_count++;
wdenk544e9732004-02-06 23:19:44 +00001493 handled++;
1494 data_len = (unsigned long) hw_p->rx[i].data_len; /* Get len */
1495 if (data_len) {
1496 if (data_len > ENET_MAX_MTU) /* Check len */
1497 data_len = 0;
1498 else {
1499 if (EMAC_RX_ERRORS & hw_p->rx[i].ctrl) { /* Check Errors */
1500 data_len = 0;
1501 hw_p->stats.rx_err_log[hw_p->
1502 rx_err_index]
1503 = hw_p->rx[i].ctrl;
1504 hw_p->rx_err_index++;
1505 if (hw_p->rx_err_index ==
1506 MAX_ERR_LOG)
1507 hw_p->rx_err_index =
1508 0;
wdenk7ad5e4c2004-04-25 15:41:35 +00001509 } /* emac_erros */
wdenk544e9732004-02-06 23:19:44 +00001510 } /* data_len < max mtu */
wdenk7ad5e4c2004-04-25 15:41:35 +00001511 } /* if data_len */
wdenk544e9732004-02-06 23:19:44 +00001512 if (!data_len) { /* no data */
1513 hw_p->rx[i].ctrl |= MAL_RX_CTRL_EMPTY; /* Free Recv Buffer */
1514
1515 hw_p->stats.data_len_err++; /* Error at Rx */
1516 }
1517
1518 /* !data_len */
1519 /* AS.HARNOIS */
1520 /* Check if user has already eaten buffer */
1521 /* if not => ERROR */
1522 else if (hw_p->rx_ready[hw_p->rx_i_index] != -1) {
1523 if (hw_p->is_receiving)
1524 printf ("ERROR : Receive buffers are full!\n");
1525 break;
1526 } else {
1527 hw_p->stats.rx_frames++;
1528 hw_p->stats.rx += data_len;
1529 ef_ptr = (struct enet_frame *) hw_p->rx[i].
1530 data_ptr;
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001531#ifdef INFO_4XX_ENET
wdenk544e9732004-02-06 23:19:44 +00001532 hw_p->stats.pkts_rx++;
1533#endif
1534 /* AS.HARNOIS
1535 * use ring buffer
1536 */
1537 hw_p->rx_ready[hw_p->rx_i_index] = i;
1538 hw_p->rx_i_index++;
1539 if (NUM_RX_BUFF == hw_p->rx_i_index)
1540 hw_p->rx_i_index = 0;
1541
Stefan Roese09feb382007-07-12 16:32:08 +02001542 hw_p->rx_slot++;
1543 if (NUM_RX_BUFF == hw_p->rx_slot)
1544 hw_p->rx_slot = 0;
1545
wdenk544e9732004-02-06 23:19:44 +00001546 /* AS.HARNOIS
1547 * free receive buffer only when
1548 * buffer has been handled (eth_rx)
1549 rx[i].ctrl |= MAL_RX_CTRL_EMPTY;
1550 */
1551 } /* if data_len */
1552 } /* while */
1553 } /* if EMACK_RXCHL */
1554}
1555
1556
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001557static int ppc_4xx_eth_rx (struct eth_device *dev)
wdenk544e9732004-02-06 23:19:44 +00001558{
1559 int length;
1560 int user_index;
1561 unsigned long msr;
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001562 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenk544e9732004-02-06 23:19:44 +00001563
Wolfgang Denk0cbaf642005-09-25 00:53:22 +02001564 hw_p->is_receiving = 1; /* tell driver */
wdenk544e9732004-02-06 23:19:44 +00001565
1566 for (;;) {
1567 /* AS.HARNOIS
1568 * use ring buffer and
1569 * get index from rx buffer desciptor queue
1570 */
1571 user_index = hw_p->rx_ready[hw_p->rx_u_index];
1572 if (user_index == -1) {
1573 length = -1;
1574 break; /* nothing received - leave for() loop */
1575 }
1576
1577 msr = mfmsr ();
1578 mtmsr (msr & ~(MSR_EE));
1579
1580 length = hw_p->rx[user_index].data_len;
1581
1582 /* Pass the packet up to the protocol layers. */
Wolfgang Denk0cbaf642005-09-25 00:53:22 +02001583 /* NetReceive(NetRxPackets[rxIdx], length - 4); */
1584 /* NetReceive(NetRxPackets[i], length); */
wdenk544e9732004-02-06 23:19:44 +00001585 NetReceive (NetRxPackets[user_index], length - 4);
1586 /* Free Recv Buffer */
1587 hw_p->rx[user_index].ctrl |= MAL_RX_CTRL_EMPTY;
1588 /* Free rx buffer descriptor queue */
1589 hw_p->rx_ready[hw_p->rx_u_index] = -1;
1590 hw_p->rx_u_index++;
1591 if (NUM_RX_BUFF == hw_p->rx_u_index)
1592 hw_p->rx_u_index = 0;
1593
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001594#ifdef INFO_4XX_ENET
wdenk544e9732004-02-06 23:19:44 +00001595 hw_p->stats.pkts_handled++;
1596#endif
1597
1598 mtmsr (msr); /* Enable IRQ's */
1599 }
1600
Wolfgang Denk0cbaf642005-09-25 00:53:22 +02001601 hw_p->is_receiving = 0; /* tell driver */
wdenk544e9732004-02-06 23:19:44 +00001602
1603 return length;
1604}
1605
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001606int ppc_4xx_eth_initialize (bd_t * bis)
wdenk544e9732004-02-06 23:19:44 +00001607{
1608 static int virgin = 0;
wdenk544e9732004-02-06 23:19:44 +00001609 struct eth_device *dev;
1610 int eth_num = 0;
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001611 EMAC_4XX_HW_PST hw = NULL;
Stefan Roese8d982302007-01-18 10:25:34 +01001612 u8 ethaddr[4 + CONFIG_EMAC_NR_START][6];
1613 u32 hw_addr[4];
wdenk544e9732004-02-06 23:19:44 +00001614
Stefan Roeseb30f2a12005-08-08 12:42:22 +02001615#if defined(CONFIG_440GX)
Stefan Roese326c9712005-08-01 16:41:48 +02001616 unsigned long pfc1;
1617
wdenk544e9732004-02-06 23:19:44 +00001618 mfsdr (sdr_pfc1, pfc1);
1619 pfc1 &= ~(0x01e00000);
1620 pfc1 |= 0x01200000;
1621 mtsdr (sdr_pfc1, pfc1);
Stefan Roese326c9712005-08-01 16:41:48 +02001622#endif
Marian Balakowicz49d0eee2006-06-30 16:30:46 +02001623
Stefan Roese8d982302007-01-18 10:25:34 +01001624 /* first clear all mac-addresses */
1625 for (eth_num = 0; eth_num < LAST_EMAC_NUM; eth_num++)
1626 memcpy(ethaddr[eth_num], "\0\0\0\0\0\0", 6);
wdenk97e8bda2004-09-29 22:43:59 +00001627
Stefan Roese7f98aec2005-10-20 16:34:28 +02001628 for (eth_num = 0; eth_num < LAST_EMAC_NUM; eth_num++) {
wdenk544e9732004-02-06 23:19:44 +00001629 switch (eth_num) {
wdenk54070ab2004-12-31 09:32:47 +00001630 default: /* fall through */
wdenk544e9732004-02-06 23:19:44 +00001631 case 0:
Stefan Roese8d982302007-01-18 10:25:34 +01001632 memcpy(ethaddr[eth_num + CONFIG_EMAC_NR_START],
1633 bis->bi_enetaddr, 6);
1634 hw_addr[eth_num] = 0x0;
wdenk544e9732004-02-06 23:19:44 +00001635 break;
wdenk54070ab2004-12-31 09:32:47 +00001636#ifdef CONFIG_HAS_ETH1
wdenk544e9732004-02-06 23:19:44 +00001637 case 1:
Stefan Roese8d982302007-01-18 10:25:34 +01001638 memcpy(ethaddr[eth_num + CONFIG_EMAC_NR_START],
1639 bis->bi_enet1addr, 6);
1640 hw_addr[eth_num] = 0x100;
wdenk544e9732004-02-06 23:19:44 +00001641 break;
wdenk54070ab2004-12-31 09:32:47 +00001642#endif
1643#ifdef CONFIG_HAS_ETH2
wdenk544e9732004-02-06 23:19:44 +00001644 case 2:
Stefan Roese8d982302007-01-18 10:25:34 +01001645 memcpy(ethaddr[eth_num + CONFIG_EMAC_NR_START],
1646 bis->bi_enet2addr, 6);
1647 hw_addr[eth_num] = 0x400;
wdenk544e9732004-02-06 23:19:44 +00001648 break;
wdenk54070ab2004-12-31 09:32:47 +00001649#endif
1650#ifdef CONFIG_HAS_ETH3
wdenk544e9732004-02-06 23:19:44 +00001651 case 3:
Stefan Roese8d982302007-01-18 10:25:34 +01001652 memcpy(ethaddr[eth_num + CONFIG_EMAC_NR_START],
1653 bis->bi_enet3addr, 6);
1654 hw_addr[eth_num] = 0x600;
wdenk544e9732004-02-06 23:19:44 +00001655 break;
wdenk54070ab2004-12-31 09:32:47 +00001656#endif
wdenk544e9732004-02-06 23:19:44 +00001657 }
Stefan Roese8d982302007-01-18 10:25:34 +01001658 }
1659
1660 /* set phy num and mode */
1661 bis->bi_phynum[0] = CONFIG_PHY_ADDR;
1662 bis->bi_phymode[0] = 0;
1663
1664#if defined(CONFIG_PHY1_ADDR)
1665 bis->bi_phynum[1] = CONFIG_PHY1_ADDR;
1666 bis->bi_phymode[1] = 0;
1667#endif
1668#if defined(CONFIG_440GX)
1669 bis->bi_phynum[2] = CONFIG_PHY2_ADDR;
1670 bis->bi_phynum[3] = CONFIG_PHY3_ADDR;
1671 bis->bi_phymode[2] = 2;
1672 bis->bi_phymode[3] = 2;
Stefan Roese153b3e22007-10-05 17:10:59 +02001673#endif
Stefan Roese8d982302007-01-18 10:25:34 +01001674
Stefan Roese153b3e22007-10-05 17:10:59 +02001675#if defined(CONFIG_440GX) || \
1676 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
1677 defined(CONFIG_405EX)
Stefan Roese8d982302007-01-18 10:25:34 +01001678 ppc_4xx_eth_setup_bridge(0, bis);
1679#endif
1680
1681 for (eth_num = 0; eth_num < LAST_EMAC_NUM; eth_num++) {
1682 /*
1683 * See if we can actually bring up the interface,
1684 * otherwise, skip it
1685 */
1686 if (memcmp (ethaddr[eth_num], "\0\0\0\0\0\0", 6) == 0) {
1687 bis->bi_phymode[eth_num] = BI_PHYMODE_NONE;
1688 continue;
1689 }
wdenk544e9732004-02-06 23:19:44 +00001690
1691 /* Allocate device structure */
1692 dev = (struct eth_device *) malloc (sizeof (*dev));
1693 if (dev == NULL) {
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001694 printf ("ppc_4xx_eth_initialize: "
wdenkef893942004-02-23 16:11:30 +00001695 "Cannot allocate eth_device %d\n", eth_num);
wdenk544e9732004-02-06 23:19:44 +00001696 return (-1);
1697 }
wdenkd1894de2005-06-20 10:17:34 +00001698 memset(dev, 0, sizeof(*dev));
wdenk544e9732004-02-06 23:19:44 +00001699
1700 /* Allocate our private use data */
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001701 hw = (EMAC_4XX_HW_PST) malloc (sizeof (*hw));
wdenk544e9732004-02-06 23:19:44 +00001702 if (hw == NULL) {
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001703 printf ("ppc_4xx_eth_initialize: "
wdenkef893942004-02-23 16:11:30 +00001704 "Cannot allocate private hw data for eth_device %d",
wdenk544e9732004-02-06 23:19:44 +00001705 eth_num);
1706 free (dev);
1707 return (-1);
1708 }
wdenkd1894de2005-06-20 10:17:34 +00001709 memset(hw, 0, sizeof(*hw));
wdenk544e9732004-02-06 23:19:44 +00001710
Stefan Roese8d982302007-01-18 10:25:34 +01001711 hw->hw_addr = hw_addr[eth_num];
1712 memcpy (dev->enetaddr, ethaddr[eth_num], 6);
wdenk544e9732004-02-06 23:19:44 +00001713 hw->devnum = eth_num;
Stefan Roese326c9712005-08-01 16:41:48 +02001714 hw->print_speed = 1;
wdenk544e9732004-02-06 23:19:44 +00001715
Stefan Roese8d982302007-01-18 10:25:34 +01001716 sprintf (dev->name, "ppc_4xx_eth%d", eth_num - CONFIG_EMAC_NR_START);
wdenk544e9732004-02-06 23:19:44 +00001717 dev->priv = (void *) hw;
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001718 dev->init = ppc_4xx_eth_init;
1719 dev->halt = ppc_4xx_eth_halt;
1720 dev->send = ppc_4xx_eth_send;
1721 dev->recv = ppc_4xx_eth_rx;
wdenk544e9732004-02-06 23:19:44 +00001722
1723 if (0 == virgin) {
1724 /* set the MAL IER ??? names may change with new spec ??? */
Stefan Roese153b3e22007-10-05 17:10:59 +02001725#if defined(CONFIG_440SPE) || \
1726 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
1727 defined(CONFIG_405EX)
Marian Balakowicz49d0eee2006-06-30 16:30:46 +02001728 mal_ier =
1729 MAL_IER_PT | MAL_IER_PRE | MAL_IER_PWE |
1730 MAL_IER_DE | MAL_IER_OTE | MAL_IER_OE | MAL_IER_PE ;
1731#else
wdenk544e9732004-02-06 23:19:44 +00001732 mal_ier =
1733 MAL_IER_DE | MAL_IER_NE | MAL_IER_TE |
1734 MAL_IER_OPBE | MAL_IER_PLBE;
Marian Balakowicz49d0eee2006-06-30 16:30:46 +02001735#endif
wdenk544e9732004-02-06 23:19:44 +00001736 mtdcr (malesr, 0xffffffff); /* clear pending interrupts */
1737 mtdcr (maltxdeir, 0xffffffff); /* clear pending interrupts */
1738 mtdcr (malrxdeir, 0xffffffff); /* clear pending interrupts */
1739 mtdcr (malier, mal_ier);
1740
1741 /* install MAL interrupt handler */
1742 irq_install_handler (VECNUM_MS,
1743 (interrupt_handler_t *) enetInt,
1744 dev);
1745 irq_install_handler (VECNUM_MTE,
1746 (interrupt_handler_t *) enetInt,
1747 dev);
1748 irq_install_handler (VECNUM_MRE,
1749 (interrupt_handler_t *) enetInt,
1750 dev);
1751 irq_install_handler (VECNUM_TXDE,
1752 (interrupt_handler_t *) enetInt,
1753 dev);
1754 irq_install_handler (VECNUM_RXDE,
1755 (interrupt_handler_t *) enetInt,
1756 dev);
1757 virgin = 1;
1758 }
1759
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001760#if defined(CONFIG_NET_MULTI)
wdenk544e9732004-02-06 23:19:44 +00001761 eth_register (dev);
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001762#else
1763 emac0_dev = dev;
1764#endif
Marian Balakowicz49d0eee2006-06-30 16:30:46 +02001765
1766#if defined(CONFIG_NET_MULTI)
Jon Loeligera5217742007-07-09 18:57:22 -05001767#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Marian Balakowiczaab8c492005-10-28 22:30:33 +02001768 miiphy_register (dev->name,
Stefan Roese99644742005-11-29 18:18:21 +01001769 emac4xx_miiphy_read, emac4xx_miiphy_write);
Marian Balakowiczaab8c492005-10-28 22:30:33 +02001770#endif
Marian Balakowicz49d0eee2006-06-30 16:30:46 +02001771#endif
wdenk544e9732004-02-06 23:19:44 +00001772 } /* end for each supported device */
1773 return (1);
1774}
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001775
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001776#if !defined(CONFIG_NET_MULTI)
1777void eth_halt (void) {
1778 if (emac0_dev) {
1779 ppc_4xx_eth_halt(emac0_dev);
1780 free(emac0_dev);
1781 emac0_dev = NULL;
1782 }
1783}
1784
1785int eth_init (bd_t *bis)
1786{
1787 ppc_4xx_eth_initialize(bis);
Stefan Roese03510612005-10-10 17:43:58 +02001788 if (emac0_dev) {
1789 return ppc_4xx_eth_init(emac0_dev, bis);
1790 } else {
1791 printf("ERROR: ethaddr not set!\n");
1792 return -1;
1793 }
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001794}
1795
1796int eth_send(volatile void *packet, int length)
1797{
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001798 return (ppc_4xx_eth_send(emac0_dev, packet, length));
1799}
1800
1801int eth_rx(void)
1802{
1803 return (ppc_4xx_eth_rx(emac0_dev));
1804}
Marian Balakowiczaab8c492005-10-28 22:30:33 +02001805
1806int emac4xx_miiphy_initialize (bd_t * bis)
1807{
Jon Loeligera5217742007-07-09 18:57:22 -05001808#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Marian Balakowiczaab8c492005-10-28 22:30:33 +02001809 miiphy_register ("ppc_4xx_eth0",
Stefan Roese99644742005-11-29 18:18:21 +01001810 emac4xx_miiphy_read, emac4xx_miiphy_write);
Marian Balakowiczaab8c492005-10-28 22:30:33 +02001811#endif
1812
1813 return 0;
1814}
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001815#endif /* !defined(CONFIG_NET_MULTI) */
1816
Jon Loeligera5217742007-07-09 18:57:22 -05001817#endif