blob: 6ab04e73d5ab574f426e0fbffb37b1e512b302b1 [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>
Stefan Roese697100952007-10-23 14:03:17 +020084#include <asm/io.h>
wdenk544e9732004-02-06 23:19:44 +000085#include <commproc.h>
Stefan Roese0c7ffc02005-08-16 18:18:00 +020086#include <ppc4xx.h>
87#include <ppc4xx_enet.h>
wdenk544e9732004-02-06 23:19:44 +000088#include <405_mal.h>
89#include <miiphy.h>
90#include <malloc.h>
91#include "vecnum.h"
92
Stefan Roese0c7ffc02005-08-16 18:18:00 +020093/*
Wolfgang Denk0ee70772005-09-23 11:05:55 +020094 * Only compile for platform with AMCC EMAC ethernet controller and
Stefan Roese0c7ffc02005-08-16 18:18:00 +020095 * network support enabled.
96 * Remark: CONFIG_405 describes Xilinx PPC405 FPGA without EMAC controller!
97 */
Jon Loeligera5217742007-07-09 18:57:22 -050098#if defined(CONFIG_CMD_NET) && !defined(CONFIG_405) && !defined(CONFIG_IOP480)
Stefan Roese0c7ffc02005-08-16 18:18:00 +020099
Jon Loeligera5217742007-07-09 18:57:22 -0500100#if !(defined(CONFIG_MII) || defined(CONFIG_CMD_MII))
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200101#error "CONFIG_MII has to be defined!"
102#endif
wdenk544e9732004-02-06 23:19:44 +0000103
Stefan Roese7f98aec2005-10-20 16:34:28 +0200104#if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_NET_MULTI)
105#error "CONFIG_NET_MULTI has to be defined for NetConsole"
106#endif
107
Wolfgang Denk0cbaf642005-09-25 00:53:22 +0200108#define EMAC_RESET_TIMEOUT 1000 /* 1000 ms reset timeout */
wdenk544e9732004-02-06 23:19:44 +0000109#define PHY_AUTONEGOTIATE_TIMEOUT 4000 /* 4000 ms autonegotiate timeout */
110
wdenk544e9732004-02-06 23:19:44 +0000111/* Ethernet Transmit and Receive Buffers */
112/* AS.HARNOIS
113 * In the same way ENET_MAX_MTU and ENET_MAX_MTU_ALIGNED are set from
114 * PKTSIZE and PKTSIZE_ALIGN (include/net.h)
115 */
Wolfgang Denk0cbaf642005-09-25 00:53:22 +0200116#define ENET_MAX_MTU PKTSIZE
wdenk544e9732004-02-06 23:19:44 +0000117#define ENET_MAX_MTU_ALIGNED PKTSIZE_ALIGN
118
wdenk544e9732004-02-06 23:19:44 +0000119/*-----------------------------------------------------------------------------+
120 * Defines for MAL/EMAC interrupt conditions as reported in the UIC (Universal
121 * Interrupt Controller).
122 *-----------------------------------------------------------------------------*/
123#define MAL_UIC_ERR ( UIC_MAL_SERR | UIC_MAL_TXDE | UIC_MAL_RXDE)
124#define MAL_UIC_DEF (UIC_MAL_RXEOB | MAL_UIC_ERR)
125#define EMAC_UIC_DEF UIC_ENET
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200126#define EMAC_UIC_DEF1 UIC_ENET1
127#define SEL_UIC_DEF(p) (p ? UIC_ENET1 : UIC_ENET )
wdenk544e9732004-02-06 23:19:44 +0000128
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200129#undef INFO_4XX_ENET
wdenk544e9732004-02-06 23:19:44 +0000130
Wolfgang Denk0cbaf642005-09-25 00:53:22 +0200131#define BI_PHYMODE_NONE 0
132#define BI_PHYMODE_ZMII 1
wdenk56ed43e2004-02-22 23:46:08 +0000133#define BI_PHYMODE_RGMII 2
Stefan Roese42fbddd2006-09-07 11:51:23 +0200134#define BI_PHYMODE_GMII 3
135#define BI_PHYMODE_RTBI 4
136#define BI_PHYMODE_TBI 5
Stefan Roese153b3e22007-10-05 17:10:59 +0200137#if defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
138 defined(CONFIG_405EX)
Stefan Roese42fbddd2006-09-07 11:51:23 +0200139#define BI_PHYMODE_SMII 6
140#define BI_PHYMODE_MII 7
141#endif
wdenk56ed43e2004-02-22 23:46:08 +0000142
Stefan Roese5a128832007-10-05 17:35:10 +0200143#if defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
Stefan Roese153b3e22007-10-05 17:10:59 +0200144 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
145 defined(CONFIG_405EX)
Stefan Roese42fbddd2006-09-07 11:51:23 +0200146#define SDR0_MFR_ETH_CLK_SEL_V(n) ((0x01<<27) / (n+1))
147#endif
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200148
wdenk544e9732004-02-06 23:19:44 +0000149/*-----------------------------------------------------------------------------+
150 * Global variables. TX and RX descriptors and buffers.
151 *-----------------------------------------------------------------------------*/
152/* IER globals */
153static uint32_t mal_ier;
154
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200155#if !defined(CONFIG_NET_MULTI)
Stefan Roese03510612005-10-10 17:43:58 +0200156struct eth_device *emac0_dev = NULL;
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200157#endif
158
Stefan Roese7f98aec2005-10-20 16:34:28 +0200159/*
160 * Get count of EMAC devices (doesn't have to be the max. possible number
161 * supported by the cpu)
Stefan Roese15668052007-10-23 10:10:08 +0200162 *
163 * CONFIG_BOARD_EMAC_COUNT added so now a "dynamic" way to configure the
164 * EMAC count is possible. As it is needed for the Kilauea/Haleakala
165 * 405EX/405EXr eval board, using the same binary.
Stefan Roese7f98aec2005-10-20 16:34:28 +0200166 */
Stefan Roese15668052007-10-23 10:10:08 +0200167#if defined(CONFIG_BOARD_EMAC_COUNT)
168#define LAST_EMAC_NUM board_emac_count()
169#else /* CONFIG_BOARD_EMAC_COUNT */
Stefan Roese7f98aec2005-10-20 16:34:28 +0200170#if defined(CONFIG_HAS_ETH3)
171#define LAST_EMAC_NUM 4
172#elif defined(CONFIG_HAS_ETH2)
173#define LAST_EMAC_NUM 3
174#elif defined(CONFIG_HAS_ETH1)
175#define LAST_EMAC_NUM 2
176#else
177#define LAST_EMAC_NUM 1
178#endif
Stefan Roese15668052007-10-23 10:10:08 +0200179#endif /* CONFIG_BOARD_EMAC_COUNT */
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200180
Stefan Roese8d982302007-01-18 10:25:34 +0100181/* normal boards start with EMAC0 */
182#if !defined(CONFIG_EMAC_NR_START)
183#define CONFIG_EMAC_NR_START 0
184#endif
185
Stefan Roese153b3e22007-10-05 17:10:59 +0200186#if defined(CONFIG_405EX) || defined(CONFIG_440EPX)
187#define ETH_IRQ_NUM(dev) (VECNUM_ETH0 + ((dev)))
188#else
189#define ETH_IRQ_NUM(dev) (VECNUM_ETH0 + ((dev) * 2))
190#endif
191
wdenk544e9732004-02-06 23:19:44 +0000192/*-----------------------------------------------------------------------------+
193 * Prototypes and externals.
194 *-----------------------------------------------------------------------------*/
195static void enet_rcv (struct eth_device *dev, unsigned long malisr);
196
197int enetInt (struct eth_device *dev);
198static void mal_err (struct eth_device *dev, unsigned long isr,
199 unsigned long uic, unsigned long maldef,
200 unsigned long mal_errr);
201static void emac_err (struct eth_device *dev, unsigned long isr);
202
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200203extern int phy_setup_aneg (char *devname, unsigned char addr);
204extern int emac4xx_miiphy_read (char *devname, unsigned char addr,
205 unsigned char reg, unsigned short *value);
206extern int emac4xx_miiphy_write (char *devname, unsigned char addr,
207 unsigned char reg, unsigned short value);
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200208
Stefan Roese15668052007-10-23 10:10:08 +0200209int board_emac_count(void);
210
wdenk544e9732004-02-06 23:19:44 +0000211/*-----------------------------------------------------------------------------+
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200212| ppc_4xx_eth_halt
wdenk544e9732004-02-06 23:19:44 +0000213| Disable MAL channel, and EMACn
wdenk544e9732004-02-06 23:19:44 +0000214+-----------------------------------------------------------------------------*/
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200215static void ppc_4xx_eth_halt (struct eth_device *dev)
wdenk544e9732004-02-06 23:19:44 +0000216{
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200217 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenk544e9732004-02-06 23:19:44 +0000218 uint32_t failsafe = 10000;
Stefan Roese153b3e22007-10-05 17:10:59 +0200219#if defined(CONFIG_440SPE) || \
220 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
221 defined(CONFIG_405EX)
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200222 unsigned long mfr;
223#endif
wdenk544e9732004-02-06 23:19:44 +0000224
Stefan Roese697100952007-10-23 14:03:17 +0200225 out_be32((void *)EMAC_IER + hw_p->hw_addr, 0x00000000); /* disable emac interrupts */
wdenk544e9732004-02-06 23:19:44 +0000226
227 /* 1st reset MAL channel */
228 /* Note: writing a 0 to a channel has no effect */
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200229#if defined(CONFIG_405EP) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
230 mtdcr (maltxcarr, (MAL_CR_MMSR >> (hw_p->devnum * 2)));
231#else
wdenk544e9732004-02-06 23:19:44 +0000232 mtdcr (maltxcarr, (MAL_CR_MMSR >> hw_p->devnum));
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200233#endif
wdenk544e9732004-02-06 23:19:44 +0000234 mtdcr (malrxcarr, (MAL_CR_MMSR >> hw_p->devnum));
235
236 /* wait for reset */
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200237 while (mfdcr (malrxcasr) & (MAL_CR_MMSR >> hw_p->devnum)) {
wdenk544e9732004-02-06 23:19:44 +0000238 udelay (1000); /* Delay 1 MS so as not to hammer the register */
239 failsafe--;
240 if (failsafe == 0)
241 break;
wdenk544e9732004-02-06 23:19:44 +0000242 }
243
244 /* EMAC RESET */
Stefan Roese153b3e22007-10-05 17:10:59 +0200245#if defined(CONFIG_440SPE) || \
246 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
247 defined(CONFIG_405EX)
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200248 /* provide clocks for EMAC internal loopback */
249 mfsdr (sdr_mfr, mfr);
Stefan Roese42fbddd2006-09-07 11:51:23 +0200250 mfr |= SDR0_MFR_ETH_CLK_SEL_V(hw_p->devnum);
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200251 mtsdr(sdr_mfr, mfr);
252#endif
253
Stefan Roese697100952007-10-23 14:03:17 +0200254 out_be32((void *)EMAC_M0 + hw_p->hw_addr, EMAC_M0_SRST);
wdenk544e9732004-02-06 23:19:44 +0000255
Stefan Roese153b3e22007-10-05 17:10:59 +0200256#if defined(CONFIG_440SPE) || \
257 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
258 defined(CONFIG_405EX)
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200259 /* remove clocks for EMAC internal loopback */
260 mfsdr (sdr_mfr, mfr);
Stefan Roese42fbddd2006-09-07 11:51:23 +0200261 mfr &= ~SDR0_MFR_ETH_CLK_SEL_V(hw_p->devnum);
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200262 mtsdr(sdr_mfr, mfr);
263#endif
264
265
Stefan Roesec8136d02005-10-18 19:17:12 +0200266#ifndef CONFIG_NETCONSOLE
Stefan Roese326c9712005-08-01 16:41:48 +0200267 hw_p->print_speed = 1; /* print speed message again next time */
Stefan Roesec8136d02005-10-18 19:17:12 +0200268#endif
Stefan Roese326c9712005-08-01 16:41:48 +0200269
wdenk544e9732004-02-06 23:19:44 +0000270 return;
271}
272
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200273#if defined (CONFIG_440GX)
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200274int ppc_4xx_eth_setup_bridge(int devnum, bd_t * bis)
wdenked2ac4b2004-03-14 18:23:55 +0000275{
276 unsigned long pfc1;
277 unsigned long zmiifer;
278 unsigned long rmiifer;
279
280 mfsdr(sdr_pfc1, pfc1);
281 pfc1 = SDR0_PFC1_EPS_DECODE(pfc1);
282
283 zmiifer = 0;
284 rmiifer = 0;
285
286 switch (pfc1) {
287 case 1:
288 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
289 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(1);
290 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(2);
291 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(3);
292 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
293 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
294 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
295 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
296 break;
297 case 2:
Stefan Roese0632d7b2006-11-27 17:43:25 +0100298 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(0);
299 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(1);
300 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(2);
301 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(3);
wdenked2ac4b2004-03-14 18:23:55 +0000302 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
303 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
304 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
305 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
306 break;
307 case 3:
308 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
309 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
310 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
311 bis->bi_phymode[1] = BI_PHYMODE_NONE;
312 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
313 bis->bi_phymode[3] = BI_PHYMODE_NONE;
314 break;
315 case 4:
316 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(0);
317 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(1);
318 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V (2);
319 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V (3);
320 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
321 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
322 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
323 bis->bi_phymode[3] = BI_PHYMODE_RGMII;
324 break;
325 case 5:
326 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (0);
327 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (1);
328 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (2);
329 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(3);
330 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
331 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
332 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
333 bis->bi_phymode[3] = BI_PHYMODE_RGMII;
334 break;
335 case 6:
336 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (0);
337 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (1);
338 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
wdenked2ac4b2004-03-14 18:23:55 +0000339 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
340 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
341 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
wdenked2ac4b2004-03-14 18:23:55 +0000342 break;
343 case 0:
344 default:
345 zmiifer = ZMII_FER_MII << ZMII_FER_V(devnum);
346 rmiifer = 0x0;
347 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
348 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
349 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
350 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
351 break;
352 }
353
354 /* Ensure we setup mdio for this devnum and ONLY this devnum */
355 zmiifer |= (ZMII_FER_MDI) << ZMII_FER_V(devnum);
356
Stefan Roese697100952007-10-23 14:03:17 +0200357 out_be32(ZMII_FER, zmiifer);
358 out_be32(RGMII_FER, rmiifer);
wdenked2ac4b2004-03-14 18:23:55 +0000359
360 return ((int)pfc1);
wdenked2ac4b2004-03-14 18:23:55 +0000361}
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200362#endif /* CONFIG_440_GX */
wdenked2ac4b2004-03-14 18:23:55 +0000363
Stefan Roese42fbddd2006-09-07 11:51:23 +0200364#if defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
365int ppc_4xx_eth_setup_bridge(int devnum, bd_t * bis)
366{
367 unsigned long zmiifer=0x0;
Matthias Fuchs8bdf1682007-04-24 14:03:45 +0200368 unsigned long pfc1;
Stefan Roese42fbddd2006-09-07 11:51:23 +0200369
Matthias Fuchs8bdf1682007-04-24 14:03:45 +0200370 mfsdr(sdr_pfc1, pfc1);
371 pfc1 &= SDR0_PFC1_SELECT_MASK;
372
Wolfgang Denk58c495b2007-05-05 18:23:11 +0200373 switch (pfc1) {
Matthias Fuchs8bdf1682007-04-24 14:03:45 +0200374 case SDR0_PFC1_SELECT_CONFIG_2:
Stefan Roese42fbddd2006-09-07 11:51:23 +0200375 /* 1 x GMII port */
Stefan Roese697100952007-10-23 14:03:17 +0200376 out_be32((void *)ZMII_FER, 0x00);
377 out_be32((void *)RGMII_FER, 0x00000037);
Stefan Roese42fbddd2006-09-07 11:51:23 +0200378 bis->bi_phymode[0] = BI_PHYMODE_GMII;
379 bis->bi_phymode[1] = BI_PHYMODE_NONE;
380 break;
Matthias Fuchs8bdf1682007-04-24 14:03:45 +0200381 case SDR0_PFC1_SELECT_CONFIG_4:
Stefan Roese42fbddd2006-09-07 11:51:23 +0200382 /* 2 x RGMII ports */
Stefan Roese697100952007-10-23 14:03:17 +0200383 out_be32((void *)ZMII_FER, 0x00);
384 out_be32((void *)RGMII_FER, 0x00000055);
Stefan Roese42fbddd2006-09-07 11:51:23 +0200385 bis->bi_phymode[0] = BI_PHYMODE_RGMII;
386 bis->bi_phymode[1] = BI_PHYMODE_RGMII;
387 break;
Matthias Fuchs8bdf1682007-04-24 14:03:45 +0200388 case SDR0_PFC1_SELECT_CONFIG_6:
Stefan Roese42fbddd2006-09-07 11:51:23 +0200389 /* 2 x SMII ports */
Stefan Roese697100952007-10-23 14:03:17 +0200390 out_be32((void *)ZMII_FER,
391 ((ZMII_FER_SMII) << ZMII_FER_V(0)) |
392 ((ZMII_FER_SMII) << ZMII_FER_V(1)));
393 out_be32((void *)RGMII_FER, 0x00000000);
Matthias Fuchs8bdf1682007-04-24 14:03:45 +0200394 bis->bi_phymode[0] = BI_PHYMODE_SMII;
395 bis->bi_phymode[1] = BI_PHYMODE_SMII;
396 break;
397 case SDR0_PFC1_SELECT_CONFIG_1_2:
398 /* only 1 x MII supported */
Stefan Roese697100952007-10-23 14:03:17 +0200399 out_be32((void *)ZMII_FER, (ZMII_FER_MII) << ZMII_FER_V(0));
400 out_be32((void *)RGMII_FER, 0x00000000);
Matthias Fuchs8bdf1682007-04-24 14:03:45 +0200401 bis->bi_phymode[0] = BI_PHYMODE_MII;
402 bis->bi_phymode[1] = BI_PHYMODE_NONE;
Stefan Roese42fbddd2006-09-07 11:51:23 +0200403 break;
404 default:
405 break;
406 }
407
408 /* Ensure we setup mdio for this devnum and ONLY this devnum */
Stefan Roese697100952007-10-23 14:03:17 +0200409 zmiifer = in_be32((void *)ZMII_FER);
Stefan Roese42fbddd2006-09-07 11:51:23 +0200410 zmiifer |= (ZMII_FER_MDI) << ZMII_FER_V(devnum);
Stefan Roese697100952007-10-23 14:03:17 +0200411 out_be32((void *)ZMII_FER, zmiifer);
Stefan Roese42fbddd2006-09-07 11:51:23 +0200412
413 return ((int)0x0);
414}
415#endif /* CONFIG_440EPX */
416
Stefan Roese153b3e22007-10-05 17:10:59 +0200417#if defined(CONFIG_405EX)
418int ppc_4xx_eth_setup_bridge(int devnum, bd_t * bis)
419{
420 u32 gmiifer = 0;
421
422 /*
423 * Right now only 2*RGMII is supported. Please extend when needed.
424 * sr - 2007-09-19
425 */
426 switch (1) {
427 case 1:
428 /* 2 x RGMII ports */
Stefan Roese697100952007-10-23 14:03:17 +0200429 out_be32((void *)RGMII_FER, 0x00000055);
Stefan Roese153b3e22007-10-05 17:10:59 +0200430 bis->bi_phymode[0] = BI_PHYMODE_RGMII;
431 bis->bi_phymode[1] = BI_PHYMODE_RGMII;
432 break;
433 case 2:
434 /* 2 x SMII ports */
435 break;
436 default:
437 break;
438 }
439
440 /* Ensure we setup mdio for this devnum and ONLY this devnum */
Stefan Roese697100952007-10-23 14:03:17 +0200441 gmiifer = in_be32((void *)RGMII_FER);
Stefan Roese153b3e22007-10-05 17:10:59 +0200442 gmiifer |= (1 << (19-devnum));
Stefan Roese697100952007-10-23 14:03:17 +0200443 out_be32((void *)RGMII_FER, gmiifer);
Stefan Roese153b3e22007-10-05 17:10:59 +0200444
445 return ((int)0x0);
446}
447#endif /* CONFIG_405EX */
448
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200449static int ppc_4xx_eth_init (struct eth_device *dev, bd_t * bis)
wdenk544e9732004-02-06 23:19:44 +0000450{
Stefan Roese5ff4c3f2005-08-15 12:31:23 +0200451 int i, j;
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200452 unsigned long reg = 0;
wdenk544e9732004-02-06 23:19:44 +0000453 unsigned long msr;
454 unsigned long speed;
455 unsigned long duplex;
456 unsigned long failsafe;
457 unsigned mode_reg;
458 unsigned short devnum;
459 unsigned short reg_short;
Stefan Roese42fbddd2006-09-07 11:51:23 +0200460#if defined(CONFIG_440GX) || \
461 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roese153b3e22007-10-05 17:10:59 +0200462 defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
463 defined(CONFIG_405EX)
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200464 sys_info_t sysinfo;
Stefan Roese42fbddd2006-09-07 11:51:23 +0200465#if defined(CONFIG_440GX) || defined(CONFIG_440SPE) || \
Stefan Roese153b3e22007-10-05 17:10:59 +0200466 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
467 defined(CONFIG_405EX)
Stefan Roese99644742005-11-29 18:18:21 +0100468 int ethgroup = -1;
469#endif
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200470#endif
Grzegorz Bernacki2462bbd2007-10-01 09:51:50 +0200471#if defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roese153b3e22007-10-05 17:10:59 +0200472 defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
473 defined(CONFIG_405EX)
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200474 unsigned long mfr;
Stefan Roese326c9712005-08-01 16:41:48 +0200475#endif
wdenk544e9732004-02-06 23:19:44 +0000476
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200477 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenk544e9732004-02-06 23:19:44 +0000478
479 /* before doing anything, figure out if we have a MAC address */
480 /* if not, bail */
Stefan Roese03510612005-10-10 17:43:58 +0200481 if (memcmp (dev->enetaddr, "\0\0\0\0\0\0", 6) == 0) {
482 printf("ERROR: ethaddr not set!\n");
wdenk544e9732004-02-06 23:19:44 +0000483 return -1;
Stefan Roese03510612005-10-10 17:43:58 +0200484 }
wdenk544e9732004-02-06 23:19:44 +0000485
Stefan Roese42fbddd2006-09-07 11:51:23 +0200486#if defined(CONFIG_440GX) || \
487 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
Stefan Roese153b3e22007-10-05 17:10:59 +0200488 defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
489 defined(CONFIG_405EX)
wdenk544e9732004-02-06 23:19:44 +0000490 /* Need to get the OPB frequency so we can access the PHY */
491 get_sys_info (&sysinfo);
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200492#endif
wdenk544e9732004-02-06 23:19:44 +0000493
wdenk544e9732004-02-06 23:19:44 +0000494 msr = mfmsr ();
495 mtmsr (msr & ~(MSR_EE)); /* disable interrupts */
496
497 devnum = hw_p->devnum;
498
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200499#ifdef INFO_4XX_ENET
wdenk544e9732004-02-06 23:19:44 +0000500 /* AS.HARNOIS
501 * We should have :
Wolfgang Denk0cbaf642005-09-25 00:53:22 +0200502 * hw_p->stats.pkts_handled <= hw_p->stats.pkts_rx <= hw_p->stats.pkts_handled+PKTBUFSRX
wdenk544e9732004-02-06 23:19:44 +0000503 * In the most cases hw_p->stats.pkts_handled = hw_p->stats.pkts_rx, but it
504 * is possible that new packets (without relationship with
505 * current transfer) have got the time to arrived before
506 * netloop calls eth_halt
507 */
508 printf ("About preceeding transfer (eth%d):\n"
509 "- Sent packet number %d\n"
510 "- Received packet number %d\n"
511 "- Handled packet number %d\n",
512 hw_p->devnum,
513 hw_p->stats.pkts_tx,
514 hw_p->stats.pkts_rx, hw_p->stats.pkts_handled);
515
516 hw_p->stats.pkts_tx = 0;
517 hw_p->stats.pkts_rx = 0;
518 hw_p->stats.pkts_handled = 0;
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200519 hw_p->print_speed = 1; /* print speed message again next time */
wdenk544e9732004-02-06 23:19:44 +0000520#endif
521
Wolfgang Denk0cbaf642005-09-25 00:53:22 +0200522 hw_p->tx_err_index = 0; /* Transmit Error Index for tx_err_log */
523 hw_p->rx_err_index = 0; /* Receive Error Index for rx_err_log */
wdenk544e9732004-02-06 23:19:44 +0000524
525 hw_p->rx_slot = 0; /* MAL Receive Slot */
526 hw_p->rx_i_index = 0; /* Receive Interrupt Queue Index */
527 hw_p->rx_u_index = 0; /* Receive User Queue Index */
528
529 hw_p->tx_slot = 0; /* MAL Transmit Slot */
530 hw_p->tx_i_index = 0; /* Transmit Interrupt Queue Index */
531 hw_p->tx_u_index = 0; /* Transmit User Queue Index */
532
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200533#if defined(CONFIG_440) && !defined(CONFIG_440SP) && !defined(CONFIG_440SPE)
wdenk544e9732004-02-06 23:19:44 +0000534 /* set RMII mode */
535 /* NOTE: 440GX spec states that mode is mutually exclusive */
536 /* NOTE: Therefore, disable all other EMACS, since we handle */
537 /* NOTE: only one emac at a time */
538 reg = 0;
Stefan Roese697100952007-10-23 14:03:17 +0200539 out_be32((void *)ZMII_FER, 0);
wdenk544e9732004-02-06 23:19:44 +0000540 udelay (100);
wdenk544e9732004-02-06 23:19:44 +0000541
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200542#if defined(CONFIG_440EP) || defined(CONFIG_440GR)
Stefan Roese697100952007-10-23 14:03:17 +0200543 out_be32((void *)ZMII_FER, (ZMII_FER_RMII | ZMII_FER_MDI) << ZMII_FER_V (devnum));
Stefan Roese42fbddd2006-09-07 11:51:23 +0200544#elif defined(CONFIG_440GX) || defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200545 ethgroup = ppc_4xx_eth_setup_bridge(devnum, bis);
Stefan Roese74309032005-09-07 16:21:12 +0200546#elif defined(CONFIG_440GP)
547 /* set RMII mode */
Stefan Roese697100952007-10-23 14:03:17 +0200548 out_be32((void *)ZMII_FER, ZMII_RMII | ZMII_MDI0);
wdenk00fe1612004-03-14 00:07:33 +0000549#else
550 if ((devnum == 0) || (devnum == 1)) {
Stefan Roese697100952007-10-23 14:03:17 +0200551 out_be32((void *)ZMII_FER, (ZMII_FER_SMII | ZMII_FER_MDI) << ZMII_FER_V (devnum));
Stefan Roese2a4a9432006-11-27 14:12:17 +0100552 } else { /* ((devnum == 2) || (devnum == 3)) */
Stefan Roese697100952007-10-23 14:03:17 +0200553 out_be32((void *)ZMII_FER, ZMII_FER_MDI << ZMII_FER_V (devnum));
554 out_be32((void *)RGMII_FER, ((RGMII_FER_RGMII << RGMII_FER_V (2)) |
555 (RGMII_FER_RGMII << RGMII_FER_V (3))));
wdenk00fe1612004-03-14 00:07:33 +0000556 }
557#endif
Stefan Roese797d8572005-08-11 17:56:56 +0200558
Stefan Roese697100952007-10-23 14:03:17 +0200559 out_be32((void *)ZMII_SSR, ZMII_SSR_SP << ZMII_SSR_V(devnum));
Stefan Roese99644742005-11-29 18:18:21 +0100560#endif /* defined(CONFIG_440) && !defined(CONFIG_440SP) */
Stefan Roese153b3e22007-10-05 17:10:59 +0200561#if defined(CONFIG_405EX)
562 ethgroup = ppc_4xx_eth_setup_bridge(devnum, bis);
563#endif
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200564
wdenk00fe1612004-03-14 00:07:33 +0000565 __asm__ volatile ("eieio");
566
567 /* reset emac so we have access to the phy */
Stefan Roese153b3e22007-10-05 17:10:59 +0200568#if defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
569 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
570 defined(CONFIG_405EX)
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200571 /* provide clocks for EMAC internal loopback */
572 mfsdr (sdr_mfr, mfr);
Stefan Roese42fbddd2006-09-07 11:51:23 +0200573 mfr |= SDR0_MFR_ETH_CLK_SEL_V(devnum);
Marian Balakowicz49d0eee2006-06-30 16:30:46 +0200574 mtsdr(sdr_mfr, mfr);
575#endif
wdenk00fe1612004-03-14 00:07:33 +0000576
Stefan Roese697100952007-10-23 14:03:17 +0200577 out_be32((void *)EMAC_M0 + hw_p->hw_addr, EMAC_M0_SRST);
wdenk544e9732004-02-06 23:19:44 +0000578
579 failsafe = 1000;
Stefan Roese697100952007-10-23 14:03:17 +0200580 while ((in_be32((void *)EMAC_M0 + hw_p->hw_addr) & (EMAC_M0_SRST)) && failsafe) {
wdenk544e9732004-02-06 23:19:44 +0000581 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
Stefan Roese697100952007-10-23 14:03:17 +0200613 out_be32((void *)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 */
Stefan Roese697100952007-10-23 14:03:17 +0200771 reg = in_be32(ZMII_SSR);
wdenked2ac4b2004-03-14 18:23:55 +0000772 if ( (speed == 100) || (speed == 1000) )
Stefan Roese697100952007-10-23 14:03:17 +0200773 out_be32(ZMII_SSR, reg | (ZMII_SSR_SP << ZMII_SSR_V (devnum)));
wdenk544e9732004-02-06 23:19:44 +0000774 else
Stefan Roese697100952007-10-23 14:03:17 +0200775 out_be32(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 }
Stefan Roese697100952007-10-23 14:03:17 +0200788 out_be32(RGMII_SSR, reg);
wdenk544e9732004-02-06 23:19:44 +0000789 }
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 }
Stefan Roese697100952007-10-23 14:03:17 +0200804 out_be32((void *)RGMII_SSR, reg);
Stefan Roese42fbddd2006-09-07 11:51:23 +0200805#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
Stefan Roese697100952007-10-23 14:03:17 +0200915 out_be32((void *)EMAC_IAH + hw_p->hw_addr, reg);
wdenk544e9732004-02-06 23:19:44 +0000916
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
Stefan Roese697100952007-10-23 14:03:17 +0200926 out_be32((void *)EMAC_IAL + hw_p->hw_addr, reg);
wdenk544e9732004-02-06 23:19:44 +0000927
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 */
Stefan Roese697100952007-10-23 14:03:17 +0200987 out_be32((void *)EMAC_M0 + hw_p->hw_addr, EMAC_M0_TXE | EMAC_M0_RXE);
wdenk544e9732004-02-06 23:19:44 +0000988
989 /* set receive fifo to 4k and tx fifo to 2k */
Stefan Roese697100952007-10-23 14:03:17 +0200990 mode_reg = in_be32((void *)EMAC_M1 + hw_p->hw_addr);
wdenk544e9732004-02-06 23:19:44 +0000991 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
Stefan Roese697100952007-10-23 14:03:17 +02001011 out_be32((void *)EMAC_M1 + hw_p->hw_addr, mode_reg);
wdenk544e9732004-02-06 23:19:44 +00001012
1013 /* Enable broadcast and indvidual address */
1014 /* TBS: enabling runts as some misbehaved nics will send runts */
Stefan Roese697100952007-10-23 14:03:17 +02001015 out_be32((void *)EMAC_RXM + hw_p->hw_addr, EMAC_RMR_BAE | EMAC_RMR_IAE);
wdenk544e9732004-02-06 23:19:44 +00001016
1017 /* we probably need to set the tx mode1 reg? maybe at tx time */
1018
1019 /* set transmit request threshold register */
Stefan Roese697100952007-10-23 14:03:17 +02001020 out_be32((void *)EMAC_TRTR + hw_p->hw_addr, 0x18000000); /* 256 byte threshold */
wdenk544e9732004-02-06 23:19:44 +00001021
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 */
Stefan Roese697100952007-10-23 14:03:17 +02001025 out_be32((void *)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 */
Stefan Roese697100952007-10-23 14:03:17 +02001028 out_be32((void *)EMAC_RX_HI_LO_WMARK + hw_p->hw_addr, 0x0f002000);
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001029#endif /* defined(CONFIG_440) */
Stefan Roese697100952007-10-23 14:03:17 +02001030 out_be32((void *)EMAC_TXM1 + hw_p->hw_addr, 0xf8640000);
wdenk544e9732004-02-06 23:19:44 +00001031
1032 /* Set fifo limit entry in tx mode 0 */
Stefan Roese697100952007-10-23 14:03:17 +02001033 out_be32((void *)EMAC_TXM0 + hw_p->hw_addr, 0x00000003);
wdenk544e9732004-02-06 23:19:44 +00001034 /* Frame gap set */
Stefan Roese697100952007-10-23 14:03:17 +02001035 out_be32((void *)EMAC_I_FRAME_GAP_REG + hw_p->hw_addr, 0x00000008);
wdenk544e9732004-02-06 23:19:44 +00001036
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
Stefan Roese697100952007-10-23 14:03:17 +02001042 out_be32((void *)EMAC_ISR + hw_p->hw_addr, 0xffffffff); /* clear pending interrupts */
1043 out_be32((void *)EMAC_IER + hw_p->hw_addr, hw_p->emac_ier);
wdenk544e9732004-02-06 23:19:44 +00001044
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
Stefan Roese697100952007-10-23 14:03:17 +02001101 out_be32((void *)EMAC_TXM0 + hw_p->hw_addr,
1102 in_be32((void *)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) {
Stefan Roese697100952007-10-23 14:03:17 +02001112 temp_txm0 = in_be32((void *)EMAC_TXM0 + hw_p->hw_addr);
wdenk544e9732004-02-06 23:19:44 +00001113 /* 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 */
Stefan Roese697100952007-10-23 14:03:17 +02001221 emac_isr = in_be32((void *)EMAC_ISR + hw_p->hw_addr);
wdenk544e9732004-02-06 23:19:44 +00001222 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 */
Stefan Roese697100952007-10-23 14:03:17 +02001240 emac_isr = in_be32((void *)EMAC_ISR + hw_p->hw_addr);
wdenk544e9732004-02-06 23:19:44 +00001241 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 */
Stefan Roese697100952007-10-23 14:03:17 +02001258 emac_isr = in_be32((void *)EMAC_ISR + hw_p->hw_addr);
wdenk544e9732004-02-06 23:19:44 +00001259 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 */
Stefan Roese697100952007-10-23 14:03:17 +02001276 emac_isr = in_be32((void *)EMAC_ISR + hw_p->hw_addr);
wdenk544e9732004-02-06 23:19:44 +00001277 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 */
Stefan Roese697100952007-10-23 14:03:17 +02001388 emac_isr = in_be32((void *)EMAC_ISR + hw_p->hw_addr);
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001389 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);
Stefan Roese697100952007-10-23 14:03:17 +02001462 out_be32((void *)EMAC_ISR + hw_p->hw_addr, isr);
wdenk544e9732004-02-06 23:19:44 +00001463}
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