blob: 2d2918e085101fd6dd67364e12a88c95e880d1b3 [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 */
97#if (CONFIG_COMMANDS & CFG_CMD_NET) && !defined(CONFIG_405) && !defined(CONFIG_IOP480)
98
99#if !(defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII))
100#error "CONFIG_MII has to be defined!"
101#endif
wdenk544e9732004-02-06 23:19:44 +0000102
Wolfgang Denk0cbaf642005-09-25 00:53:22 +0200103#define EMAC_RESET_TIMEOUT 1000 /* 1000 ms reset timeout */
wdenk544e9732004-02-06 23:19:44 +0000104#define PHY_AUTONEGOTIATE_TIMEOUT 4000 /* 4000 ms autonegotiate timeout */
105
wdenk544e9732004-02-06 23:19:44 +0000106/* Ethernet Transmit and Receive Buffers */
107/* AS.HARNOIS
108 * In the same way ENET_MAX_MTU and ENET_MAX_MTU_ALIGNED are set from
109 * PKTSIZE and PKTSIZE_ALIGN (include/net.h)
110 */
Wolfgang Denk0cbaf642005-09-25 00:53:22 +0200111#define ENET_MAX_MTU PKTSIZE
wdenk544e9732004-02-06 23:19:44 +0000112#define ENET_MAX_MTU_ALIGNED PKTSIZE_ALIGN
113
wdenk544e9732004-02-06 23:19:44 +0000114/* define the number of channels implemented */
Wolfgang Denk0cbaf642005-09-25 00:53:22 +0200115#define EMAC_RXCHL EMAC_NUM_DEV
116#define EMAC_TXCHL EMAC_NUM_DEV
wdenk544e9732004-02-06 23:19:44 +0000117
118/*-----------------------------------------------------------------------------+
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
133
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200134
wdenk544e9732004-02-06 23:19:44 +0000135/*-----------------------------------------------------------------------------+
136 * Global variables. TX and RX descriptors and buffers.
137 *-----------------------------------------------------------------------------*/
138/* IER globals */
139static uint32_t mal_ier;
140
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200141#if !defined(CONFIG_NET_MULTI)
Stefan Roese03510612005-10-10 17:43:58 +0200142struct eth_device *emac0_dev = NULL;
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200143#endif
144
wdenk544e9732004-02-06 23:19:44 +0000145/*-----------------------------------------------------------------------------+
146 * Prototypes and externals.
147 *-----------------------------------------------------------------------------*/
148static void enet_rcv (struct eth_device *dev, unsigned long malisr);
149
150int enetInt (struct eth_device *dev);
151static void mal_err (struct eth_device *dev, unsigned long isr,
152 unsigned long uic, unsigned long maldef,
153 unsigned long mal_errr);
154static void emac_err (struct eth_device *dev, unsigned long isr);
155
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200156extern int phy_setup_aneg (char *devname, unsigned char addr);
157extern int emac4xx_miiphy_read (char *devname, unsigned char addr,
158 unsigned char reg, unsigned short *value);
159extern int emac4xx_miiphy_write (char *devname, unsigned char addr,
160 unsigned char reg, unsigned short value);
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200161
wdenk544e9732004-02-06 23:19:44 +0000162/*-----------------------------------------------------------------------------+
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200163| ppc_4xx_eth_halt
wdenk544e9732004-02-06 23:19:44 +0000164| Disable MAL channel, and EMACn
wdenk544e9732004-02-06 23:19:44 +0000165+-----------------------------------------------------------------------------*/
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200166static void ppc_4xx_eth_halt (struct eth_device *dev)
wdenk544e9732004-02-06 23:19:44 +0000167{
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200168 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenk544e9732004-02-06 23:19:44 +0000169 uint32_t failsafe = 10000;
170
171 out32 (EMAC_IER + hw_p->hw_addr, 0x00000000); /* disable emac interrupts */
172
173 /* 1st reset MAL channel */
174 /* Note: writing a 0 to a channel has no effect */
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200175#if defined(CONFIG_405EP) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
176 mtdcr (maltxcarr, (MAL_CR_MMSR >> (hw_p->devnum * 2)));
177#else
wdenk544e9732004-02-06 23:19:44 +0000178 mtdcr (maltxcarr, (MAL_CR_MMSR >> hw_p->devnum));
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200179#endif
wdenk544e9732004-02-06 23:19:44 +0000180 mtdcr (malrxcarr, (MAL_CR_MMSR >> hw_p->devnum));
181
182 /* wait for reset */
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200183 while (mfdcr (malrxcasr) & (MAL_CR_MMSR >> hw_p->devnum)) {
wdenk544e9732004-02-06 23:19:44 +0000184 udelay (1000); /* Delay 1 MS so as not to hammer the register */
185 failsafe--;
186 if (failsafe == 0)
187 break;
wdenk544e9732004-02-06 23:19:44 +0000188 }
189
190 /* EMAC RESET */
191 out32 (EMAC_M0 + hw_p->hw_addr, EMAC_M0_SRST);
192
Stefan Roese326c9712005-08-01 16:41:48 +0200193 hw_p->print_speed = 1; /* print speed message again next time */
194
wdenk544e9732004-02-06 23:19:44 +0000195 return;
196}
197
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200198#if defined (CONFIG_440GX)
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200199int ppc_4xx_eth_setup_bridge(int devnum, bd_t * bis)
wdenked2ac4b2004-03-14 18:23:55 +0000200{
201 unsigned long pfc1;
202 unsigned long zmiifer;
203 unsigned long rmiifer;
204
205 mfsdr(sdr_pfc1, pfc1);
206 pfc1 = SDR0_PFC1_EPS_DECODE(pfc1);
207
208 zmiifer = 0;
209 rmiifer = 0;
210
211 switch (pfc1) {
212 case 1:
213 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
214 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(1);
215 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(2);
216 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(3);
217 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
218 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
219 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
220 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
221 break;
222 case 2:
223 zmiifer = ZMII_FER_SMII << ZMII_FER_V(0);
224 zmiifer = ZMII_FER_SMII << ZMII_FER_V(1);
225 zmiifer = ZMII_FER_SMII << ZMII_FER_V(2);
226 zmiifer = ZMII_FER_SMII << ZMII_FER_V(3);
227 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
228 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
229 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
230 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
231 break;
232 case 3:
233 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
234 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
235 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
236 bis->bi_phymode[1] = BI_PHYMODE_NONE;
237 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
238 bis->bi_phymode[3] = BI_PHYMODE_NONE;
239 break;
240 case 4:
241 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(0);
242 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(1);
243 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V (2);
244 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V (3);
245 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
246 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
247 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
248 bis->bi_phymode[3] = BI_PHYMODE_RGMII;
249 break;
250 case 5:
251 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (0);
252 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (1);
253 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (2);
254 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(3);
255 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
256 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
257 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
258 bis->bi_phymode[3] = BI_PHYMODE_RGMII;
259 break;
260 case 6:
261 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (0);
262 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (1);
263 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
wdenked2ac4b2004-03-14 18:23:55 +0000264 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
265 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
266 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
wdenked2ac4b2004-03-14 18:23:55 +0000267 break;
268 case 0:
269 default:
270 zmiifer = ZMII_FER_MII << ZMII_FER_V(devnum);
271 rmiifer = 0x0;
272 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
273 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
274 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
275 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
276 break;
277 }
278
279 /* Ensure we setup mdio for this devnum and ONLY this devnum */
280 zmiifer |= (ZMII_FER_MDI) << ZMII_FER_V(devnum);
281
282 out32 (ZMII_FER, zmiifer);
283 out32 (RGMII_FER, rmiifer);
284
285 return ((int)pfc1);
286
287}
288#endif
289
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200290static int ppc_4xx_eth_init (struct eth_device *dev, bd_t * bis)
wdenk544e9732004-02-06 23:19:44 +0000291{
Stefan Roese5ff4c3f2005-08-15 12:31:23 +0200292 int i, j;
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200293 unsigned long reg = 0;
wdenk544e9732004-02-06 23:19:44 +0000294 unsigned long msr;
295 unsigned long speed;
296 unsigned long duplex;
297 unsigned long failsafe;
298 unsigned mode_reg;
299 unsigned short devnum;
300 unsigned short reg_short;
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200301#if defined(CONFIG_440GX)
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200302 sys_info_t sysinfo;
wdenked2ac4b2004-03-14 18:23:55 +0000303 int ethgroup;
Stefan Roese326c9712005-08-01 16:41:48 +0200304#endif
wdenk544e9732004-02-06 23:19:44 +0000305
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200306 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenk544e9732004-02-06 23:19:44 +0000307
308 /* before doing anything, figure out if we have a MAC address */
309 /* if not, bail */
Stefan Roese03510612005-10-10 17:43:58 +0200310 if (memcmp (dev->enetaddr, "\0\0\0\0\0\0", 6) == 0) {
311 printf("ERROR: ethaddr not set!\n");
wdenk544e9732004-02-06 23:19:44 +0000312 return -1;
Stefan Roese03510612005-10-10 17:43:58 +0200313 }
wdenk544e9732004-02-06 23:19:44 +0000314
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200315#if defined(CONFIG_440GX)
wdenk544e9732004-02-06 23:19:44 +0000316 /* Need to get the OPB frequency so we can access the PHY */
317 get_sys_info (&sysinfo);
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200318#endif
wdenk544e9732004-02-06 23:19:44 +0000319
wdenk544e9732004-02-06 23:19:44 +0000320 msr = mfmsr ();
321 mtmsr (msr & ~(MSR_EE)); /* disable interrupts */
322
323 devnum = hw_p->devnum;
324
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200325#ifdef INFO_4XX_ENET
wdenk544e9732004-02-06 23:19:44 +0000326 /* AS.HARNOIS
327 * We should have :
Wolfgang Denk0cbaf642005-09-25 00:53:22 +0200328 * hw_p->stats.pkts_handled <= hw_p->stats.pkts_rx <= hw_p->stats.pkts_handled+PKTBUFSRX
wdenk544e9732004-02-06 23:19:44 +0000329 * In the most cases hw_p->stats.pkts_handled = hw_p->stats.pkts_rx, but it
330 * is possible that new packets (without relationship with
331 * current transfer) have got the time to arrived before
332 * netloop calls eth_halt
333 */
334 printf ("About preceeding transfer (eth%d):\n"
335 "- Sent packet number %d\n"
336 "- Received packet number %d\n"
337 "- Handled packet number %d\n",
338 hw_p->devnum,
339 hw_p->stats.pkts_tx,
340 hw_p->stats.pkts_rx, hw_p->stats.pkts_handled);
341
342 hw_p->stats.pkts_tx = 0;
343 hw_p->stats.pkts_rx = 0;
344 hw_p->stats.pkts_handled = 0;
345#endif
346
Wolfgang Denk0cbaf642005-09-25 00:53:22 +0200347 hw_p->tx_err_index = 0; /* Transmit Error Index for tx_err_log */
348 hw_p->rx_err_index = 0; /* Receive Error Index for rx_err_log */
wdenk544e9732004-02-06 23:19:44 +0000349
350 hw_p->rx_slot = 0; /* MAL Receive Slot */
351 hw_p->rx_i_index = 0; /* Receive Interrupt Queue Index */
352 hw_p->rx_u_index = 0; /* Receive User Queue Index */
353
354 hw_p->tx_slot = 0; /* MAL Transmit Slot */
355 hw_p->tx_i_index = 0; /* Transmit Interrupt Queue Index */
356 hw_p->tx_u_index = 0; /* Transmit User Queue Index */
357
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200358#if defined(CONFIG_440)
wdenk544e9732004-02-06 23:19:44 +0000359 /* set RMII mode */
360 /* NOTE: 440GX spec states that mode is mutually exclusive */
361 /* NOTE: Therefore, disable all other EMACS, since we handle */
362 /* NOTE: only one emac at a time */
363 reg = 0;
364 out32 (ZMII_FER, 0);
365 udelay (100);
wdenk544e9732004-02-06 23:19:44 +0000366
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200367#if defined(CONFIG_440EP) || defined(CONFIG_440GR)
Wolfgang Denk0cbaf642005-09-25 00:53:22 +0200368 out32 (ZMII_FER, (ZMII_FER_RMII | ZMII_FER_MDI) << ZMII_FER_V (devnum));
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200369#elif defined(CONFIG_440GX)
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200370 ethgroup = ppc_4xx_eth_setup_bridge(devnum, bis);
Stefan Roese74309032005-09-07 16:21:12 +0200371#elif defined(CONFIG_440GP)
372 /* set RMII mode */
373 out32 (ZMII_FER, ZMII_RMII | ZMII_MDI0);
wdenk00fe1612004-03-14 00:07:33 +0000374#else
375 if ((devnum == 0) || (devnum == 1)) {
376 out32 (ZMII_FER, (ZMII_FER_SMII | ZMII_FER_MDI) << ZMII_FER_V (devnum));
377 }
378 else { /* ((devnum == 2) || (devnum == 3)) */
379 out32 (ZMII_FER, ZMII_FER_MDI << ZMII_FER_V (devnum));
wdenk544e9732004-02-06 23:19:44 +0000380 out32 (RGMII_FER, ((RGMII_FER_RGMII << RGMII_FER_V (2)) |
381 (RGMII_FER_RGMII << RGMII_FER_V (3))));
wdenk00fe1612004-03-14 00:07:33 +0000382 }
383#endif
Stefan Roese797d8572005-08-11 17:56:56 +0200384
wdenk00fe1612004-03-14 00:07:33 +0000385 out32 (ZMII_SSR, ZMII_SSR_SP << ZMII_SSR_V(devnum));
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200386#endif /* defined(CONFIG_440) */
387
wdenk00fe1612004-03-14 00:07:33 +0000388 __asm__ volatile ("eieio");
389
390 /* reset emac so we have access to the phy */
391
392 out32 (EMAC_M0 + hw_p->hw_addr, EMAC_M0_SRST);
wdenk544e9732004-02-06 23:19:44 +0000393 __asm__ volatile ("eieio");
394
395 failsafe = 1000;
396 while ((in32 (EMAC_M0 + hw_p->hw_addr) & (EMAC_M0_SRST)) && failsafe) {
397 udelay (1000);
398 failsafe--;
399 }
400
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200401#if defined(CONFIG_440GX)
wdenk544e9732004-02-06 23:19:44 +0000402 /* Whack the M1 register */
403 mode_reg = 0x0;
404 mode_reg &= ~0x00000038;
405 if (sysinfo.freqOPB <= 50000000);
406 else if (sysinfo.freqOPB <= 66666667)
407 mode_reg |= EMAC_M1_OBCI_66;
408 else if (sysinfo.freqOPB <= 83333333)
409 mode_reg |= EMAC_M1_OBCI_83;
410 else if (sysinfo.freqOPB <= 100000000)
411 mode_reg |= EMAC_M1_OBCI_100;
412 else
413 mode_reg |= EMAC_M1_OBCI_GT100;
414
415 out32 (EMAC_M1 + hw_p->hw_addr, mode_reg);
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200416#endif /* defined(CONFIG_440GX) */
wdenk544e9732004-02-06 23:19:44 +0000417
418 /* wait for PHY to complete auto negotiation */
419 reg_short = 0;
420#ifndef CONFIG_CS8952_PHY
421 switch (devnum) {
422 case 0:
423 reg = CONFIG_PHY_ADDR;
424 break;
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200425#if defined (CONFIG_PHY1_ADDR)
wdenk544e9732004-02-06 23:19:44 +0000426 case 1:
427 reg = CONFIG_PHY1_ADDR;
428 break;
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200429#endif
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200430#if defined (CONFIG_440GX)
wdenk544e9732004-02-06 23:19:44 +0000431 case 2:
432 reg = CONFIG_PHY2_ADDR;
433 break;
434 case 3:
435 reg = CONFIG_PHY3_ADDR;
436 break;
437#endif
438 default:
439 reg = CONFIG_PHY_ADDR;
440 break;
441 }
442
wdenk56ed43e2004-02-22 23:46:08 +0000443 bis->bi_phynum[devnum] = reg;
444
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200445#if defined(CONFIG_PHY_RESET)
wdenk97e8bda2004-09-29 22:43:59 +0000446 /*
447 * Reset the phy, only if its the first time through
448 * otherwise, just check the speeds & feeds
449 */
450 if (hw_p->first_init == 0) {
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200451 miiphy_reset (dev->name, reg);
wdenk544e9732004-02-06 23:19:44 +0000452
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200453#if defined(CONFIG_440GX)
wdenk00fe1612004-03-14 00:07:33 +0000454#if defined(CONFIG_CIS8201_PHY)
wdenk7ad5e4c2004-04-25 15:41:35 +0000455 /*
Stefan Roese363330b2005-08-04 17:09:16 +0200456 * Cicada 8201 PHY needs to have an extended register whacked
457 * for RGMII mode.
wdenk7ad5e4c2004-04-25 15:41:35 +0000458 */
Stefan Roese363330b2005-08-04 17:09:16 +0200459 if ( ((devnum == 2) || (devnum ==3)) && (4 == ethgroup) ) {
Stefan Roese5ff4c3f2005-08-15 12:31:23 +0200460#if defined(CONFIG_CIS8201_SHORT_ETCH)
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200461 miiphy_write (dev->name, reg, 23, 0x1300);
Stefan Roese5ff4c3f2005-08-15 12:31:23 +0200462#else
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200463 miiphy_write (dev->name, reg, 23, 0x1000);
Stefan Roese5ff4c3f2005-08-15 12:31:23 +0200464#endif
Stefan Roese363330b2005-08-04 17:09:16 +0200465 /*
466 * Vitesse VSC8201/Cicada CIS8201 errata:
467 * Interoperability problem with Intel 82547EI phys
468 * This work around (provided by Vitesse) changes
469 * the default timer convergence from 8ms to 12ms
470 */
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200471 miiphy_write (dev->name, reg, 0x1f, 0x2a30);
472 miiphy_write (dev->name, reg, 0x08, 0x0200);
473 miiphy_write (dev->name, reg, 0x1f, 0x52b5);
474 miiphy_write (dev->name, reg, 0x02, 0x0004);
475 miiphy_write (dev->name, reg, 0x01, 0x0671);
476 miiphy_write (dev->name, reg, 0x00, 0x8fae);
477 miiphy_write (dev->name, reg, 0x1f, 0x2a30);
478 miiphy_write (dev->name, reg, 0x08, 0x0000);
479 miiphy_write (dev->name, reg, 0x1f, 0x0000);
Stefan Roese363330b2005-08-04 17:09:16 +0200480 /* end Vitesse/Cicada errata */
481 }
wdenk00fe1612004-03-14 00:07:33 +0000482#endif
wdenked2ac4b2004-03-14 18:23:55 +0000483#endif
wdenk97e8bda2004-09-29 22:43:59 +0000484 /* Start/Restart autonegotiation */
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200485 phy_setup_aneg (dev->name, reg);
wdenk97e8bda2004-09-29 22:43:59 +0000486 udelay (1000);
487 }
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200488#endif /* defined(CONFIG_PHY_RESET) */
wdenk544e9732004-02-06 23:19:44 +0000489
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200490 miiphy_read (dev->name, reg, PHY_BMSR, &reg_short);
wdenk544e9732004-02-06 23:19:44 +0000491
492 /*
wdenk00fe1612004-03-14 00:07:33 +0000493 * Wait if PHY is capable of autonegotiation and autonegotiation is not complete
wdenk544e9732004-02-06 23:19:44 +0000494 */
495 if ((reg_short & PHY_BMSR_AUTN_ABLE)
496 && !(reg_short & PHY_BMSR_AUTN_COMP)) {
497 puts ("Waiting for PHY auto negotiation to complete");
498 i = 0;
499 while (!(reg_short & PHY_BMSR_AUTN_COMP)) {
500 /*
501 * Timeout reached ?
502 */
503 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
504 puts (" TIMEOUT !\n");
505 break;
506 }
507
508 if ((i++ % 1000) == 0) {
509 putc ('.');
510 }
511 udelay (1000); /* 1 ms */
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200512 miiphy_read (dev->name, reg, PHY_BMSR, &reg_short);
wdenk544e9732004-02-06 23:19:44 +0000513
514 }
515 puts (" done\n");
516 udelay (500000); /* another 500 ms (results in faster booting) */
517 }
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200518#endif /* #ifndef CONFIG_CS8952_PHY */
519
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200520 speed = miiphy_speed (dev->name, reg);
521 duplex = miiphy_duplex (dev->name, reg);
wdenk544e9732004-02-06 23:19:44 +0000522
523 if (hw_p->print_speed) {
524 hw_p->print_speed = 0;
525 printf ("ENET Speed is %d Mbps - %s duplex connection\n",
526 (int) speed, (duplex == HALF) ? "HALF" : "FULL");
527 }
528
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200529#if defined(CONFIG_440)
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200530#if defined(CONFIG_440EP) || defined(CONFIG_440GR)
Stefan Roese326c9712005-08-01 16:41:48 +0200531 mfsdr(sdr_mfr, reg);
532 if (speed == 100) {
533 reg = (reg & ~SDR0_MFR_ZMII_MODE_MASK) | SDR0_MFR_ZMII_MODE_RMII_100M;
534 } else {
535 reg = (reg & ~SDR0_MFR_ZMII_MODE_MASK) | SDR0_MFR_ZMII_MODE_RMII_10M;
536 }
537 mtsdr(sdr_mfr, reg);
538#endif
Stefan Roese797d8572005-08-11 17:56:56 +0200539
wdenk544e9732004-02-06 23:19:44 +0000540 /* Set ZMII/RGMII speed according to the phy link speed */
541 reg = in32 (ZMII_SSR);
wdenked2ac4b2004-03-14 18:23:55 +0000542 if ( (speed == 100) || (speed == 1000) )
wdenk544e9732004-02-06 23:19:44 +0000543 out32 (ZMII_SSR, reg | (ZMII_SSR_SP << ZMII_SSR_V (devnum)));
544 else
Stefan Roese797d8572005-08-11 17:56:56 +0200545 out32 (ZMII_SSR, reg & (~(ZMII_SSR_SP << ZMII_SSR_V (devnum))));
wdenk544e9732004-02-06 23:19:44 +0000546
547 if ((devnum == 2) || (devnum == 3)) {
548 if (speed == 1000)
549 reg = (RGMII_SSR_SP_1000MBPS << RGMII_SSR_V (devnum));
550 else if (speed == 100)
551 reg = (RGMII_SSR_SP_100MBPS << RGMII_SSR_V (devnum));
552 else
553 reg = (RGMII_SSR_SP_10MBPS << RGMII_SSR_V (devnum));
554
555 out32 (RGMII_SSR, reg);
556 }
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200557#endif /* defined(CONFIG_440) */
wdenk544e9732004-02-06 23:19:44 +0000558
559 /* set the Mal configuration reg */
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200560#if defined(CONFIG_440GX)
Stefan Roese363330b2005-08-04 17:09:16 +0200561 mtdcr (malmcr, MAL_CR_PLBB | MAL_CR_OPBBL | MAL_CR_LEA |
562 MAL_CR_PLBLT_DEFAULT | MAL_CR_EOPIE | 0x00330000);
563#else
564 mtdcr (malmcr, MAL_CR_PLBB | MAL_CR_OPBBL | MAL_CR_LEA | MAL_CR_PLBLT_DEFAULT);
wdenk544e9732004-02-06 23:19:44 +0000565 /* Errata 1.12: MAL_1 -- Disable MAL bursting */
Stefan Roese363330b2005-08-04 17:09:16 +0200566 if (get_pvr() == PVR_440GP_RB) {
567 mtdcr (malmcr, mfdcr(malmcr) & ~MAL_CR_PLBB);
568 }
569#endif
wdenk544e9732004-02-06 23:19:44 +0000570
571 /* Free "old" buffers */
572 if (hw_p->alloc_tx_buf)
573 free (hw_p->alloc_tx_buf);
574 if (hw_p->alloc_rx_buf)
575 free (hw_p->alloc_rx_buf);
576
577 /*
578 * Malloc MAL buffer desciptors, make sure they are
579 * aligned on cache line boundary size
580 * (401/403/IOP480 = 16, 405 = 32)
581 * and doesn't cross cache block boundaries.
582 */
583 hw_p->alloc_tx_buf =
584 (mal_desc_t *) malloc ((sizeof (mal_desc_t) * NUM_TX_BUFF) +
585 ((2 * CFG_CACHELINE_SIZE) - 2));
Stefan Roese5ff4c3f2005-08-15 12:31:23 +0200586 if (NULL == hw_p->alloc_tx_buf)
587 return -1;
wdenk544e9732004-02-06 23:19:44 +0000588 if (((int) hw_p->alloc_tx_buf & CACHELINE_MASK) != 0) {
589 hw_p->tx =
590 (mal_desc_t *) ((int) hw_p->alloc_tx_buf +
591 CFG_CACHELINE_SIZE -
592 ((int) hw_p->
593 alloc_tx_buf & CACHELINE_MASK));
594 } else {
595 hw_p->tx = hw_p->alloc_tx_buf;
596 }
597
598 hw_p->alloc_rx_buf =
599 (mal_desc_t *) malloc ((sizeof (mal_desc_t) * NUM_RX_BUFF) +
600 ((2 * CFG_CACHELINE_SIZE) - 2));
Stefan Roese5ff4c3f2005-08-15 12:31:23 +0200601 if (NULL == hw_p->alloc_rx_buf) {
602 free(hw_p->alloc_tx_buf);
603 hw_p->alloc_tx_buf = NULL;
604 return -1;
605 }
606
wdenk544e9732004-02-06 23:19:44 +0000607 if (((int) hw_p->alloc_rx_buf & CACHELINE_MASK) != 0) {
608 hw_p->rx =
609 (mal_desc_t *) ((int) hw_p->alloc_rx_buf +
610 CFG_CACHELINE_SIZE -
611 ((int) hw_p->
612 alloc_rx_buf & CACHELINE_MASK));
613 } else {
614 hw_p->rx = hw_p->alloc_rx_buf;
615 }
616
617 for (i = 0; i < NUM_TX_BUFF; i++) {
618 hw_p->tx[i].ctrl = 0;
619 hw_p->tx[i].data_len = 0;
Stefan Roese5ff4c3f2005-08-15 12:31:23 +0200620 if (hw_p->first_init == 0) {
wdenk544e9732004-02-06 23:19:44 +0000621 hw_p->txbuf_ptr =
622 (char *) malloc (ENET_MAX_MTU_ALIGNED);
Stefan Roese5ff4c3f2005-08-15 12:31:23 +0200623 if (NULL == hw_p->txbuf_ptr) {
624 free(hw_p->alloc_rx_buf);
625 free(hw_p->alloc_tx_buf);
626 hw_p->alloc_rx_buf = NULL;
627 hw_p->alloc_tx_buf = NULL;
628 for(j = 0; j < i; j++) {
629 free(hw_p->tx[i].data_ptr);
630 hw_p->tx[i].data_ptr = NULL;
631 }
632 }
633 }
wdenk544e9732004-02-06 23:19:44 +0000634 hw_p->tx[i].data_ptr = hw_p->txbuf_ptr;
635 if ((NUM_TX_BUFF - 1) == i)
636 hw_p->tx[i].ctrl |= MAL_TX_CTRL_WRAP;
637 hw_p->tx_run[i] = -1;
638#if 0
639 printf ("TX_BUFF %d @ 0x%08lx\n", i,
640 (ulong) hw_p->tx[i].data_ptr);
641#endif
642 }
643
644 for (i = 0; i < NUM_RX_BUFF; i++) {
645 hw_p->rx[i].ctrl = 0;
646 hw_p->rx[i].data_len = 0;
Wolfgang Denk0cbaf642005-09-25 00:53:22 +0200647 /* rx[i].data_ptr = (char *) &rx_buff[i]; */
wdenk544e9732004-02-06 23:19:44 +0000648 hw_p->rx[i].data_ptr = (char *) NetRxPackets[i];
649 if ((NUM_RX_BUFF - 1) == i)
650 hw_p->rx[i].ctrl |= MAL_RX_CTRL_WRAP;
651 hw_p->rx[i].ctrl |= MAL_RX_CTRL_EMPTY | MAL_RX_CTRL_INTR;
652 hw_p->rx_ready[i] = -1;
653#if 0
654 printf ("RX_BUFF %d @ 0x%08lx\n", i, (ulong) rx[i].data_ptr);
655#endif
656 }
657
658 reg = 0x00000000;
659
660 reg |= dev->enetaddr[0]; /* set high address */
661 reg = reg << 8;
662 reg |= dev->enetaddr[1];
663
664 out32 (EMAC_IAH + hw_p->hw_addr, reg);
665
666 reg = 0x00000000;
667 reg |= dev->enetaddr[2]; /* set low address */
668 reg = reg << 8;
669 reg |= dev->enetaddr[3];
670 reg = reg << 8;
671 reg |= dev->enetaddr[4];
672 reg = reg << 8;
673 reg |= dev->enetaddr[5];
674
675 out32 (EMAC_IAL + hw_p->hw_addr, reg);
676
677 switch (devnum) {
678 case 1:
679 /* setup MAL tx & rx channel pointers */
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200680#if defined (CONFIG_405EP) || defined (CONFIG_440EP) || defined (CONFIG_440GR)
Stefan Roese326c9712005-08-01 16:41:48 +0200681 mtdcr (maltxctp2r, hw_p->tx);
682#else
wdenk544e9732004-02-06 23:19:44 +0000683 mtdcr (maltxctp1r, hw_p->tx);
Stefan Roese326c9712005-08-01 16:41:48 +0200684#endif
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200685#if defined(CONFIG_440)
Stefan Roese326c9712005-08-01 16:41:48 +0200686 mtdcr (maltxbattr, 0x0);
wdenk544e9732004-02-06 23:19:44 +0000687 mtdcr (malrxbattr, 0x0);
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200688#endif
wdenk544e9732004-02-06 23:19:44 +0000689 mtdcr (malrxctp1r, hw_p->rx);
690 /* set RX buffer size */
691 mtdcr (malrcbs1, ENET_MAX_MTU_ALIGNED / 16);
692 break;
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200693#if defined (CONFIG_440GX)
wdenk544e9732004-02-06 23:19:44 +0000694 case 2:
695 /* setup MAL tx & rx channel pointers */
696 mtdcr (maltxbattr, 0x0);
wdenk544e9732004-02-06 23:19:44 +0000697 mtdcr (malrxbattr, 0x0);
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200698 mtdcr (maltxctp2r, hw_p->tx);
wdenk544e9732004-02-06 23:19:44 +0000699 mtdcr (malrxctp2r, hw_p->rx);
700 /* set RX buffer size */
701 mtdcr (malrcbs2, ENET_MAX_MTU_ALIGNED / 16);
702 break;
703 case 3:
704 /* setup MAL tx & rx channel pointers */
705 mtdcr (maltxbattr, 0x0);
706 mtdcr (maltxctp3r, hw_p->tx);
707 mtdcr (malrxbattr, 0x0);
708 mtdcr (malrxctp3r, hw_p->rx);
709 /* set RX buffer size */
710 mtdcr (malrcbs3, ENET_MAX_MTU_ALIGNED / 16);
711 break;
Stefan Roese797d8572005-08-11 17:56:56 +0200712#endif /* CONFIG_440GX */
wdenk544e9732004-02-06 23:19:44 +0000713 case 0:
714 default:
715 /* setup MAL tx & rx channel pointers */
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200716#if defined(CONFIG_440)
wdenk544e9732004-02-06 23:19:44 +0000717 mtdcr (maltxbattr, 0x0);
wdenk544e9732004-02-06 23:19:44 +0000718 mtdcr (malrxbattr, 0x0);
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200719#endif
720 mtdcr (maltxctp0r, hw_p->tx);
wdenk544e9732004-02-06 23:19:44 +0000721 mtdcr (malrxctp0r, hw_p->rx);
722 /* set RX buffer size */
723 mtdcr (malrcbs0, ENET_MAX_MTU_ALIGNED / 16);
724 break;
725 }
726
727 /* Enable MAL transmit and receive channels */
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200728#if defined(CONFIG_405EP) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
Stefan Roese326c9712005-08-01 16:41:48 +0200729 mtdcr (maltxcasr, (MAL_TXRX_CASR >> (hw_p->devnum*2)));
730#else
wdenk544e9732004-02-06 23:19:44 +0000731 mtdcr (maltxcasr, (MAL_TXRX_CASR >> hw_p->devnum));
Stefan Roese326c9712005-08-01 16:41:48 +0200732#endif
wdenk544e9732004-02-06 23:19:44 +0000733 mtdcr (malrxcasr, (MAL_TXRX_CASR >> hw_p->devnum));
734
735 /* set transmit enable & receive enable */
736 out32 (EMAC_M0 + hw_p->hw_addr, EMAC_M0_TXE | EMAC_M0_RXE);
737
738 /* set receive fifo to 4k and tx fifo to 2k */
739 mode_reg = in32 (EMAC_M1 + hw_p->hw_addr);
740 mode_reg |= EMAC_M1_RFS_4K | EMAC_M1_TX_FIFO_2K;
741
742 /* set speed */
wdenked2ac4b2004-03-14 18:23:55 +0000743 if (speed == _1000BASET)
744 mode_reg = mode_reg | EMAC_M1_MF_1000MBPS | EMAC_M1_IST;
745 else if (speed == _100BASET)
wdenk544e9732004-02-06 23:19:44 +0000746 mode_reg = mode_reg | EMAC_M1_MF_100MBPS | EMAC_M1_IST;
747 else
748 mode_reg = mode_reg & ~0x00C00000; /* 10 MBPS */
749 if (duplex == FULL)
750 mode_reg = mode_reg | 0x80000000 | EMAC_M1_IST;
751
752 out32 (EMAC_M1 + hw_p->hw_addr, mode_reg);
753
754 /* Enable broadcast and indvidual address */
755 /* TBS: enabling runts as some misbehaved nics will send runts */
756 out32 (EMAC_RXM + hw_p->hw_addr, EMAC_RMR_BAE | EMAC_RMR_IAE);
757
758 /* we probably need to set the tx mode1 reg? maybe at tx time */
759
760 /* set transmit request threshold register */
761 out32 (EMAC_TRTR + hw_p->hw_addr, 0x18000000); /* 256 byte threshold */
762
Wolfgang Denk0cbaf642005-09-25 00:53:22 +0200763 /* set receive low/high water mark register */
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200764#if defined(CONFIG_440)
wdenk544e9732004-02-06 23:19:44 +0000765 /* 440GP has a 64 byte burst length */
766 out32 (EMAC_RX_HI_LO_WMARK + hw_p->hw_addr, 0x80009000);
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200767#else
768 /* 405s have a 16 byte burst length */
769 out32 (EMAC_RX_HI_LO_WMARK + hw_p->hw_addr, 0x0f002000);
770#endif /* defined(CONFIG_440) */
wdenk544e9732004-02-06 23:19:44 +0000771 out32 (EMAC_TXM1 + hw_p->hw_addr, 0xf8640000);
772
773 /* Set fifo limit entry in tx mode 0 */
774 out32 (EMAC_TXM0 + hw_p->hw_addr, 0x00000003);
775 /* Frame gap set */
776 out32 (EMAC_I_FRAME_GAP_REG + hw_p->hw_addr, 0x00000008);
777
778 /* Set EMAC IER */
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200779 hw_p->emac_ier = EMAC_ISR_PTLE | EMAC_ISR_BFCS | EMAC_ISR_ORE | EMAC_ISR_IRE;
wdenk544e9732004-02-06 23:19:44 +0000780 if (speed == _100BASET)
781 hw_p->emac_ier = hw_p->emac_ier | EMAC_ISR_SYE;
782
783 out32 (EMAC_ISR + hw_p->hw_addr, 0xffffffff); /* clear pending interrupts */
784 out32 (EMAC_IER + hw_p->hw_addr, hw_p->emac_ier);
785
786 if (hw_p->first_init == 0) {
787 /*
788 * Connect interrupt service routines
789 */
wdenk544e9732004-02-06 23:19:44 +0000790 irq_install_handler (VECNUM_ETH0 + (hw_p->devnum * 2),
791 (interrupt_handler_t *) enetInt, dev);
792 }
wdenk544e9732004-02-06 23:19:44 +0000793
794 mtmsr (msr); /* enable interrupts again */
795
796 hw_p->bis = bis;
797 hw_p->first_init = 1;
798
799 return (1);
800}
801
802
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200803static int ppc_4xx_eth_send (struct eth_device *dev, volatile void *ptr,
wdenk544e9732004-02-06 23:19:44 +0000804 int len)
805{
806 struct enet_frame *ef_ptr;
807 ulong time_start, time_now;
808 unsigned long temp_txm0;
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200809 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenk544e9732004-02-06 23:19:44 +0000810
811 ef_ptr = (struct enet_frame *) ptr;
812
813 /*-----------------------------------------------------------------------+
814 * Copy in our address into the frame.
815 *-----------------------------------------------------------------------*/
816 (void) memcpy (ef_ptr->source_addr, dev->enetaddr, ENET_ADDR_LENGTH);
817
818 /*-----------------------------------------------------------------------+
819 * If frame is too long or too short, modify length.
820 *-----------------------------------------------------------------------*/
821 /* TBS: where does the fragment go???? */
822 if (len > ENET_MAX_MTU)
823 len = ENET_MAX_MTU;
824
825 /* memcpy ((void *) &tx_buff[tx_slot], (const void *) ptr, len); */
826 memcpy ((void *) hw_p->txbuf_ptr, (const void *) ptr, len);
827
828 /*-----------------------------------------------------------------------+
829 * set TX Buffer busy, and send it
830 *-----------------------------------------------------------------------*/
831 hw_p->tx[hw_p->tx_slot].ctrl = (MAL_TX_CTRL_LAST |
832 EMAC_TX_CTRL_GFCS | EMAC_TX_CTRL_GP) &
833 ~(EMAC_TX_CTRL_ISA | EMAC_TX_CTRL_RSA);
834 if ((NUM_TX_BUFF - 1) == hw_p->tx_slot)
835 hw_p->tx[hw_p->tx_slot].ctrl |= MAL_TX_CTRL_WRAP;
836
837 hw_p->tx[hw_p->tx_slot].data_len = (short) len;
838 hw_p->tx[hw_p->tx_slot].ctrl |= MAL_TX_CTRL_READY;
839
840 __asm__ volatile ("eieio");
841
842 out32 (EMAC_TXM0 + hw_p->hw_addr,
843 in32 (EMAC_TXM0 + hw_p->hw_addr) | EMAC_TXM0_GNP0);
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200844#ifdef INFO_4XX_ENET
wdenk544e9732004-02-06 23:19:44 +0000845 hw_p->stats.pkts_tx++;
846#endif
847
848 /*-----------------------------------------------------------------------+
849 * poll unitl the packet is sent and then make sure it is OK
850 *-----------------------------------------------------------------------*/
851 time_start = get_timer (0);
852 while (1) {
853 temp_txm0 = in32 (EMAC_TXM0 + hw_p->hw_addr);
854 /* loop until either TINT turns on or 3 seconds elapse */
855 if ((temp_txm0 & EMAC_TXM0_GNP0) != 0) {
856 /* transmit is done, so now check for errors
857 * If there is an error, an interrupt should
858 * happen when we return
859 */
860 time_now = get_timer (0);
861 if ((time_now - time_start) > 3000) {
862 return (-1);
863 }
864 } else {
865 return (len);
866 }
867 }
868}
869
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200870#if defined (CONFIG_440)
wdenk544e9732004-02-06 23:19:44 +0000871
872int enetInt (struct eth_device *dev)
873{
874 int serviced;
875 int rc = -1; /* default to not us */
876 unsigned long mal_isr;
877 unsigned long emac_isr = 0;
878 unsigned long mal_rx_eob;
879 unsigned long my_uic0msr, my_uic1msr;
880
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200881#if defined(CONFIG_440GX)
wdenk544e9732004-02-06 23:19:44 +0000882 unsigned long my_uic2msr;
883#endif
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200884 EMAC_4XX_HW_PST hw_p;
wdenk544e9732004-02-06 23:19:44 +0000885
886 /*
887 * Because the mal is generic, we need to get the current
888 * eth device
889 */
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200890#if defined(CONFIG_NET_MULTI)
891 dev = eth_get_dev();
892#else
893 dev = emac0_dev;
894#endif
wdenk544e9732004-02-06 23:19:44 +0000895
896 hw_p = dev->priv;
897
898
899 /* enter loop that stays in interrupt code until nothing to service */
900 do {
901 serviced = 0;
902
903 my_uic0msr = mfdcr (uic0msr);
904 my_uic1msr = mfdcr (uic1msr);
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200905#if defined(CONFIG_440GX)
wdenk544e9732004-02-06 23:19:44 +0000906 my_uic2msr = mfdcr (uic2msr);
907#endif
908 if (!(my_uic0msr & (UIC_MRE | UIC_MTE))
909 && !(my_uic1msr &
910 (UIC_ETH0 | UIC_ETH1 | UIC_MS | UIC_MTDE |
911 UIC_MRDE))) {
912 /* not for us */
913 return (rc);
914 }
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200915#if defined (CONFIG_440GX)
wdenk544e9732004-02-06 23:19:44 +0000916 if (!(my_uic0msr & (UIC_MRE | UIC_MTE))
917 && !(my_uic2msr & (UIC_ETH2 | UIC_ETH3))) {
918 /* not for us */
919 return (rc);
920 }
921#endif
922 /* get and clear controller status interrupts */
923 /* look at Mal and EMAC interrupts */
924 if ((my_uic0msr & (UIC_MRE | UIC_MTE))
925 || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
926 /* we have a MAL interrupt */
927 mal_isr = mfdcr (malesr);
928 /* look for mal error */
929 if (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE)) {
930 mal_err (dev, mal_isr, my_uic0msr,
931 MAL_UIC_DEF, MAL_UIC_ERR);
932 serviced = 1;
933 rc = 0;
934 }
935 }
936
937 /* port by port dispatch of emac interrupts */
938 if (hw_p->devnum == 0) {
939 if (UIC_ETH0 & my_uic1msr) { /* look for EMAC errors */
940 emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
941 if ((hw_p->emac_ier & emac_isr) != 0) {
942 emac_err (dev, emac_isr);
943 serviced = 1;
944 rc = 0;
945 }
946 }
947 if ((hw_p->emac_ier & emac_isr)
948 || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
949 mtdcr (uic0sr, UIC_MRE | UIC_MTE); /* Clear */
950 mtdcr (uic1sr, UIC_ETH0 | UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
951 return (rc); /* we had errors so get out */
952 }
953 }
954
955 if (hw_p->devnum == 1) {
956 if (UIC_ETH1 & my_uic1msr) { /* look for EMAC errors */
957 emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
958 if ((hw_p->emac_ier & emac_isr) != 0) {
959 emac_err (dev, emac_isr);
960 serviced = 1;
961 rc = 0;
962 }
963 }
964 if ((hw_p->emac_ier & emac_isr)
965 || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
966 mtdcr (uic0sr, UIC_MRE | UIC_MTE); /* Clear */
967 mtdcr (uic1sr, UIC_ETH1 | UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
968 return (rc); /* we had errors so get out */
969 }
970 }
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200971#if defined (CONFIG_440GX)
wdenk544e9732004-02-06 23:19:44 +0000972 if (hw_p->devnum == 2) {
973 if (UIC_ETH2 & my_uic2msr) { /* look for EMAC errors */
974 emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
975 if ((hw_p->emac_ier & emac_isr) != 0) {
976 emac_err (dev, emac_isr);
977 serviced = 1;
978 rc = 0;
979 }
980 }
981 if ((hw_p->emac_ier & emac_isr)
982 || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
983 mtdcr (uic0sr, UIC_MRE | UIC_MTE); /* Clear */
984 mtdcr (uic1sr, UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
985 mtdcr (uic2sr, UIC_ETH2);
986 return (rc); /* we had errors so get out */
987 }
988 }
989
990 if (hw_p->devnum == 3) {
991 if (UIC_ETH3 & my_uic2msr) { /* look for EMAC errors */
992 emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
993 if ((hw_p->emac_ier & emac_isr) != 0) {
994 emac_err (dev, emac_isr);
995 serviced = 1;
996 rc = 0;
997 }
998 }
999 if ((hw_p->emac_ier & emac_isr)
1000 || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
1001 mtdcr (uic0sr, UIC_MRE | UIC_MTE); /* Clear */
1002 mtdcr (uic1sr, UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
1003 mtdcr (uic2sr, UIC_ETH3);
1004 return (rc); /* we had errors so get out */
1005 }
1006 }
Stefan Roeseb30f2a12005-08-08 12:42:22 +02001007#endif /* CONFIG_440GX */
wdenk544e9732004-02-06 23:19:44 +00001008 /* handle MAX TX EOB interrupt from a tx */
1009 if (my_uic0msr & UIC_MTE) {
1010 mal_rx_eob = mfdcr (maltxeobisr);
1011 mtdcr (maltxeobisr, mal_rx_eob);
1012 mtdcr (uic0sr, UIC_MTE);
1013 }
1014 /* handle MAL RX EOB interupt from a receive */
wdenk7ad5e4c2004-04-25 15:41:35 +00001015 /* check for EOB on valid channels */
wdenk544e9732004-02-06 23:19:44 +00001016 if (my_uic0msr & UIC_MRE) {
1017 mal_rx_eob = mfdcr (malrxeobisr);
Wolfgang Denk0cbaf642005-09-25 00:53:22 +02001018 if ((mal_rx_eob & (0x80000000 >> hw_p->devnum)) != 0) { /* call emac routine for channel x */
wdenk544e9732004-02-06 23:19:44 +00001019 /* clear EOB
1020 mtdcr(malrxeobisr, mal_rx_eob); */
1021 enet_rcv (dev, emac_isr);
1022 /* indicate that we serviced an interrupt */
1023 serviced = 1;
1024 rc = 0;
1025 }
1026 }
1027 mtdcr (uic0sr, UIC_MRE); /* Clear */
1028 mtdcr (uic1sr, UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
1029 switch (hw_p->devnum) {
1030 case 0:
1031 mtdcr (uic1sr, UIC_ETH0);
1032 break;
1033 case 1:
1034 mtdcr (uic1sr, UIC_ETH1);
1035 break;
Stefan Roeseb30f2a12005-08-08 12:42:22 +02001036#if defined (CONFIG_440GX)
wdenk544e9732004-02-06 23:19:44 +00001037 case 2:
1038 mtdcr (uic2sr, UIC_ETH2);
1039 break;
1040 case 3:
1041 mtdcr (uic2sr, UIC_ETH3);
1042 break;
Stefan Roeseb30f2a12005-08-08 12:42:22 +02001043#endif /* CONFIG_440GX */
wdenk544e9732004-02-06 23:19:44 +00001044 default:
1045 break;
1046 }
1047 } while (serviced);
1048
1049 return (rc);
1050}
1051
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001052#else /* CONFIG_440 */
1053
1054int enetInt (struct eth_device *dev)
1055{
1056 int serviced;
1057 int rc = -1; /* default to not us */
1058 unsigned long mal_isr;
1059 unsigned long emac_isr = 0;
1060 unsigned long mal_rx_eob;
1061 unsigned long my_uicmsr;
1062
1063 EMAC_4XX_HW_PST hw_p;
1064
1065 /*
1066 * Because the mal is generic, we need to get the current
1067 * eth device
1068 */
1069#if defined(CONFIG_NET_MULTI)
1070 dev = eth_get_dev();
1071#else
1072 dev = emac0_dev;
1073#endif
1074
1075 hw_p = dev->priv;
1076
1077 /* enter loop that stays in interrupt code until nothing to service */
1078 do {
1079 serviced = 0;
1080
1081 my_uicmsr = mfdcr (uicmsr);
1082
1083 if ((my_uicmsr & (MAL_UIC_DEF | EMAC_UIC_DEF)) == 0) { /* not for us */
1084 return (rc);
1085 }
1086 /* get and clear controller status interrupts */
1087 /* look at Mal and EMAC interrupts */
1088 if ((MAL_UIC_DEF & my_uicmsr) != 0) { /* we have a MAL interrupt */
1089 mal_isr = mfdcr (malesr);
1090 /* look for mal error */
1091 if ((my_uicmsr & MAL_UIC_ERR) != 0) {
1092 mal_err (dev, mal_isr, my_uicmsr, MAL_UIC_DEF, MAL_UIC_ERR);
1093 serviced = 1;
1094 rc = 0;
1095 }
1096 }
1097
1098 /* port by port dispatch of emac interrupts */
1099
1100 if ((SEL_UIC_DEF(hw_p->devnum) & my_uicmsr) != 0) { /* look for EMAC errors */
1101 emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
1102 if ((hw_p->emac_ier & emac_isr) != 0) {
1103 emac_err (dev, emac_isr);
1104 serviced = 1;
1105 rc = 0;
1106 }
1107 }
1108 if (((hw_p->emac_ier & emac_isr) != 0) || ((MAL_UIC_ERR & my_uicmsr) != 0)) {
1109 mtdcr (uicsr, MAL_UIC_DEF | SEL_UIC_DEF(hw_p->devnum)); /* Clear */
1110 return (rc); /* we had errors so get out */
1111 }
1112
1113 /* handle MAX TX EOB interrupt from a tx */
1114 if (my_uicmsr & UIC_MAL_TXEOB) {
1115 mal_rx_eob = mfdcr (maltxeobisr);
1116 mtdcr (maltxeobisr, mal_rx_eob);
1117 mtdcr (uicsr, UIC_MAL_TXEOB);
1118 }
1119 /* handle MAL RX EOB interupt from a receive */
1120 /* check for EOB on valid channels */
1121 if (my_uicmsr & UIC_MAL_RXEOB)
1122 {
1123 mal_rx_eob = mfdcr (malrxeobisr);
Wolfgang Denk0cbaf642005-09-25 00:53:22 +02001124 if ((mal_rx_eob & (0x80000000 >> hw_p->devnum)) != 0) { /* call emac routine for channel x */
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001125 /* clear EOB
1126 mtdcr(malrxeobisr, mal_rx_eob); */
1127 enet_rcv (dev, emac_isr);
1128 /* indicate that we serviced an interrupt */
1129 serviced = 1;
1130 rc = 0;
1131 }
1132 }
1133 mtdcr (uicsr, MAL_UIC_DEF|EMAC_UIC_DEF|EMAC_UIC_DEF1); /* Clear */
1134 }
1135 while (serviced);
1136
1137 return (rc);
1138}
1139
1140#endif /* CONFIG_440 */
1141
wdenk544e9732004-02-06 23:19:44 +00001142/*-----------------------------------------------------------------------------+
1143 * MAL Error Routine
1144 *-----------------------------------------------------------------------------*/
1145static void mal_err (struct eth_device *dev, unsigned long isr,
1146 unsigned long uic, unsigned long maldef,
1147 unsigned long mal_errr)
1148{
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001149 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenk544e9732004-02-06 23:19:44 +00001150
1151 mtdcr (malesr, isr); /* clear interrupt */
1152
1153 /* clear DE interrupt */
1154 mtdcr (maltxdeir, 0xC0000000);
1155 mtdcr (malrxdeir, 0x80000000);
1156
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001157#ifdef INFO_4XX_ENET
Wolfgang Denk0cbaf642005-09-25 00:53:22 +02001158 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 +00001159#endif
1160
1161 eth_init (hw_p->bis); /* start again... */
1162}
1163
1164/*-----------------------------------------------------------------------------+
1165 * EMAC Error Routine
1166 *-----------------------------------------------------------------------------*/
1167static void emac_err (struct eth_device *dev, unsigned long isr)
1168{
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001169 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenk544e9732004-02-06 23:19:44 +00001170
1171 printf ("EMAC%d error occured.... ISR = %lx\n", hw_p->devnum, isr);
1172 out32 (EMAC_ISR + hw_p->hw_addr, isr);
1173}
1174
1175/*-----------------------------------------------------------------------------+
1176 * enet_rcv() handles the ethernet receive data
1177 *-----------------------------------------------------------------------------*/
1178static void enet_rcv (struct eth_device *dev, unsigned long malisr)
1179{
1180 struct enet_frame *ef_ptr;
1181 unsigned long data_len;
1182 unsigned long rx_eob_isr;
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001183 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenk544e9732004-02-06 23:19:44 +00001184
1185 int handled = 0;
1186 int i;
1187 int loop_count = 0;
1188
1189 rx_eob_isr = mfdcr (malrxeobisr);
1190 if ((0x80000000 >> hw_p->devnum) & rx_eob_isr) {
1191 /* clear EOB */
1192 mtdcr (malrxeobisr, rx_eob_isr);
1193
1194 /* EMAC RX done */
1195 while (1) { /* do all */
1196 i = hw_p->rx_slot;
1197
1198 if ((MAL_RX_CTRL_EMPTY & hw_p->rx[i].ctrl)
1199 || (loop_count >= NUM_RX_BUFF))
1200 break;
1201 loop_count++;
1202 hw_p->rx_slot++;
1203 if (NUM_RX_BUFF == hw_p->rx_slot)
1204 hw_p->rx_slot = 0;
1205 handled++;
1206 data_len = (unsigned long) hw_p->rx[i].data_len; /* Get len */
1207 if (data_len) {
1208 if (data_len > ENET_MAX_MTU) /* Check len */
1209 data_len = 0;
1210 else {
1211 if (EMAC_RX_ERRORS & hw_p->rx[i].ctrl) { /* Check Errors */
1212 data_len = 0;
1213 hw_p->stats.rx_err_log[hw_p->
1214 rx_err_index]
1215 = hw_p->rx[i].ctrl;
1216 hw_p->rx_err_index++;
1217 if (hw_p->rx_err_index ==
1218 MAX_ERR_LOG)
1219 hw_p->rx_err_index =
1220 0;
wdenk7ad5e4c2004-04-25 15:41:35 +00001221 } /* emac_erros */
wdenk544e9732004-02-06 23:19:44 +00001222 } /* data_len < max mtu */
wdenk7ad5e4c2004-04-25 15:41:35 +00001223 } /* if data_len */
wdenk544e9732004-02-06 23:19:44 +00001224 if (!data_len) { /* no data */
1225 hw_p->rx[i].ctrl |= MAL_RX_CTRL_EMPTY; /* Free Recv Buffer */
1226
1227 hw_p->stats.data_len_err++; /* Error at Rx */
1228 }
1229
1230 /* !data_len */
1231 /* AS.HARNOIS */
1232 /* Check if user has already eaten buffer */
1233 /* if not => ERROR */
1234 else if (hw_p->rx_ready[hw_p->rx_i_index] != -1) {
1235 if (hw_p->is_receiving)
1236 printf ("ERROR : Receive buffers are full!\n");
1237 break;
1238 } else {
1239 hw_p->stats.rx_frames++;
1240 hw_p->stats.rx += data_len;
1241 ef_ptr = (struct enet_frame *) hw_p->rx[i].
1242 data_ptr;
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001243#ifdef INFO_4XX_ENET
wdenk544e9732004-02-06 23:19:44 +00001244 hw_p->stats.pkts_rx++;
1245#endif
1246 /* AS.HARNOIS
1247 * use ring buffer
1248 */
1249 hw_p->rx_ready[hw_p->rx_i_index] = i;
1250 hw_p->rx_i_index++;
1251 if (NUM_RX_BUFF == hw_p->rx_i_index)
1252 hw_p->rx_i_index = 0;
1253
wdenk544e9732004-02-06 23:19:44 +00001254 /* AS.HARNOIS
1255 * free receive buffer only when
1256 * buffer has been handled (eth_rx)
1257 rx[i].ctrl |= MAL_RX_CTRL_EMPTY;
1258 */
1259 } /* if data_len */
1260 } /* while */
1261 } /* if EMACK_RXCHL */
1262}
1263
1264
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001265static int ppc_4xx_eth_rx (struct eth_device *dev)
wdenk544e9732004-02-06 23:19:44 +00001266{
1267 int length;
1268 int user_index;
1269 unsigned long msr;
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001270 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenk544e9732004-02-06 23:19:44 +00001271
Wolfgang Denk0cbaf642005-09-25 00:53:22 +02001272 hw_p->is_receiving = 1; /* tell driver */
wdenk544e9732004-02-06 23:19:44 +00001273
1274 for (;;) {
1275 /* AS.HARNOIS
1276 * use ring buffer and
1277 * get index from rx buffer desciptor queue
1278 */
1279 user_index = hw_p->rx_ready[hw_p->rx_u_index];
1280 if (user_index == -1) {
1281 length = -1;
1282 break; /* nothing received - leave for() loop */
1283 }
1284
1285 msr = mfmsr ();
1286 mtmsr (msr & ~(MSR_EE));
1287
1288 length = hw_p->rx[user_index].data_len;
1289
1290 /* Pass the packet up to the protocol layers. */
Wolfgang Denk0cbaf642005-09-25 00:53:22 +02001291 /* NetReceive(NetRxPackets[rxIdx], length - 4); */
1292 /* NetReceive(NetRxPackets[i], length); */
wdenk544e9732004-02-06 23:19:44 +00001293 NetReceive (NetRxPackets[user_index], length - 4);
1294 /* Free Recv Buffer */
1295 hw_p->rx[user_index].ctrl |= MAL_RX_CTRL_EMPTY;
1296 /* Free rx buffer descriptor queue */
1297 hw_p->rx_ready[hw_p->rx_u_index] = -1;
1298 hw_p->rx_u_index++;
1299 if (NUM_RX_BUFF == hw_p->rx_u_index)
1300 hw_p->rx_u_index = 0;
1301
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001302#ifdef INFO_4XX_ENET
wdenk544e9732004-02-06 23:19:44 +00001303 hw_p->stats.pkts_handled++;
1304#endif
1305
1306 mtmsr (msr); /* Enable IRQ's */
1307 }
1308
Wolfgang Denk0cbaf642005-09-25 00:53:22 +02001309 hw_p->is_receiving = 0; /* tell driver */
wdenk544e9732004-02-06 23:19:44 +00001310
1311 return length;
1312}
1313
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001314int ppc_4xx_eth_initialize (bd_t * bis)
wdenk544e9732004-02-06 23:19:44 +00001315{
1316 static int virgin = 0;
wdenk544e9732004-02-06 23:19:44 +00001317 struct eth_device *dev;
1318 int eth_num = 0;
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001319 EMAC_4XX_HW_PST hw = NULL;
wdenk544e9732004-02-06 23:19:44 +00001320
Stefan Roeseb30f2a12005-08-08 12:42:22 +02001321#if defined(CONFIG_440GX)
Stefan Roese326c9712005-08-01 16:41:48 +02001322 unsigned long pfc1;
1323
wdenk544e9732004-02-06 23:19:44 +00001324 mfsdr (sdr_pfc1, pfc1);
1325 pfc1 &= ~(0x01e00000);
1326 pfc1 |= 0x01200000;
1327 mtsdr (sdr_pfc1, pfc1);
Stefan Roese326c9712005-08-01 16:41:48 +02001328#endif
wdenk56ed43e2004-02-22 23:46:08 +00001329 /* set phy num and mode */
1330 bis->bi_phynum[0] = CONFIG_PHY_ADDR;
Stefan Roese326c9712005-08-01 16:41:48 +02001331#if defined(CONFIG_PHY1_ADDR)
wdenk56ed43e2004-02-22 23:46:08 +00001332 bis->bi_phynum[1] = CONFIG_PHY1_ADDR;
Stefan Roese326c9712005-08-01 16:41:48 +02001333#endif
Stefan Roeseb30f2a12005-08-08 12:42:22 +02001334#if defined(CONFIG_440GX)
wdenk56ed43e2004-02-22 23:46:08 +00001335 bis->bi_phynum[2] = CONFIG_PHY2_ADDR;
1336 bis->bi_phynum[3] = CONFIG_PHY3_ADDR;
1337 bis->bi_phymode[0] = 0;
1338 bis->bi_phymode[1] = 0;
1339 bis->bi_phymode[2] = 2;
1340 bis->bi_phymode[3] = 2;
wdenk544e9732004-02-06 23:19:44 +00001341
Stefan Roeseb30f2a12005-08-08 12:42:22 +02001342#if defined (CONFIG_440GX)
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001343 ppc_4xx_eth_setup_bridge(0, bis);
wdenk97e8bda2004-09-29 22:43:59 +00001344#endif
Stefan Roese326c9712005-08-01 16:41:48 +02001345#endif
wdenk97e8bda2004-09-29 22:43:59 +00001346
wdenk544e9732004-02-06 23:19:44 +00001347 for (eth_num = 0; eth_num < EMAC_NUM_DEV; eth_num++) {
1348
1349 /* See if we can actually bring up the interface, otherwise, skip it */
1350 switch (eth_num) {
wdenk54070ab2004-12-31 09:32:47 +00001351 default: /* fall through */
wdenk544e9732004-02-06 23:19:44 +00001352 case 0:
wdenk56ed43e2004-02-22 23:46:08 +00001353 if (memcmp (bis->bi_enetaddr, "\0\0\0\0\0\0", 6) == 0) {
1354 bis->bi_phymode[eth_num] = BI_PHYMODE_NONE;
wdenk544e9732004-02-06 23:19:44 +00001355 continue;
wdenk56ed43e2004-02-22 23:46:08 +00001356 }
wdenk544e9732004-02-06 23:19:44 +00001357 break;
wdenk54070ab2004-12-31 09:32:47 +00001358#ifdef CONFIG_HAS_ETH1
wdenk544e9732004-02-06 23:19:44 +00001359 case 1:
wdenk56ed43e2004-02-22 23:46:08 +00001360 if (memcmp (bis->bi_enet1addr, "\0\0\0\0\0\0", 6) == 0) {
1361 bis->bi_phymode[eth_num] = BI_PHYMODE_NONE;
wdenk544e9732004-02-06 23:19:44 +00001362 continue;
wdenk56ed43e2004-02-22 23:46:08 +00001363 }
wdenk544e9732004-02-06 23:19:44 +00001364 break;
wdenk54070ab2004-12-31 09:32:47 +00001365#endif
1366#ifdef CONFIG_HAS_ETH2
wdenk544e9732004-02-06 23:19:44 +00001367 case 2:
wdenk56ed43e2004-02-22 23:46:08 +00001368 if (memcmp (bis->bi_enet2addr, "\0\0\0\0\0\0", 6) == 0) {
1369 bis->bi_phymode[eth_num] = BI_PHYMODE_NONE;
wdenk544e9732004-02-06 23:19:44 +00001370 continue;
wdenk56ed43e2004-02-22 23:46:08 +00001371 }
wdenk544e9732004-02-06 23:19:44 +00001372 break;
wdenk54070ab2004-12-31 09:32:47 +00001373#endif
1374#ifdef CONFIG_HAS_ETH3
wdenk544e9732004-02-06 23:19:44 +00001375 case 3:
wdenk56ed43e2004-02-22 23:46:08 +00001376 if (memcmp (bis->bi_enet3addr, "\0\0\0\0\0\0", 6) == 0) {
1377 bis->bi_phymode[eth_num] = BI_PHYMODE_NONE;
wdenk544e9732004-02-06 23:19:44 +00001378 continue;
wdenk56ed43e2004-02-22 23:46:08 +00001379 }
wdenk544e9732004-02-06 23:19:44 +00001380 break;
wdenk54070ab2004-12-31 09:32:47 +00001381#endif
wdenk544e9732004-02-06 23:19:44 +00001382 }
1383
1384 /* Allocate device structure */
1385 dev = (struct eth_device *) malloc (sizeof (*dev));
1386 if (dev == NULL) {
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001387 printf ("ppc_4xx_eth_initialize: "
wdenkef893942004-02-23 16:11:30 +00001388 "Cannot allocate eth_device %d\n", eth_num);
wdenk544e9732004-02-06 23:19:44 +00001389 return (-1);
1390 }
wdenkd1894de2005-06-20 10:17:34 +00001391 memset(dev, 0, sizeof(*dev));
wdenk544e9732004-02-06 23:19:44 +00001392
1393 /* Allocate our private use data */
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001394 hw = (EMAC_4XX_HW_PST) malloc (sizeof (*hw));
wdenk544e9732004-02-06 23:19:44 +00001395 if (hw == NULL) {
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001396 printf ("ppc_4xx_eth_initialize: "
wdenkef893942004-02-23 16:11:30 +00001397 "Cannot allocate private hw data for eth_device %d",
wdenk544e9732004-02-06 23:19:44 +00001398 eth_num);
1399 free (dev);
1400 return (-1);
1401 }
wdenkd1894de2005-06-20 10:17:34 +00001402 memset(hw, 0, sizeof(*hw));
wdenk544e9732004-02-06 23:19:44 +00001403
1404 switch (eth_num) {
wdenk54070ab2004-12-31 09:32:47 +00001405 default: /* fall through */
wdenk544e9732004-02-06 23:19:44 +00001406 case 0:
1407 hw->hw_addr = 0;
1408 memcpy (dev->enetaddr, bis->bi_enetaddr, 6);
1409 break;
wdenk54070ab2004-12-31 09:32:47 +00001410#ifdef CONFIG_HAS_ETH1
wdenk544e9732004-02-06 23:19:44 +00001411 case 1:
1412 hw->hw_addr = 0x100;
1413 memcpy (dev->enetaddr, bis->bi_enet1addr, 6);
1414 break;
wdenk54070ab2004-12-31 09:32:47 +00001415#endif
1416#ifdef CONFIG_HAS_ETH2
wdenk544e9732004-02-06 23:19:44 +00001417 case 2:
1418 hw->hw_addr = 0x400;
1419 memcpy (dev->enetaddr, bis->bi_enet2addr, 6);
1420 break;
wdenk54070ab2004-12-31 09:32:47 +00001421#endif
1422#ifdef CONFIG_HAS_ETH3
wdenk544e9732004-02-06 23:19:44 +00001423 case 3:
1424 hw->hw_addr = 0x600;
1425 memcpy (dev->enetaddr, bis->bi_enet3addr, 6);
1426 break;
wdenk54070ab2004-12-31 09:32:47 +00001427#endif
wdenk544e9732004-02-06 23:19:44 +00001428 }
1429
1430 hw->devnum = eth_num;
Stefan Roese326c9712005-08-01 16:41:48 +02001431 hw->print_speed = 1;
wdenk544e9732004-02-06 23:19:44 +00001432
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001433 sprintf (dev->name, "ppc_4xx_eth%d", eth_num);
wdenk544e9732004-02-06 23:19:44 +00001434 dev->priv = (void *) hw;
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001435 dev->init = ppc_4xx_eth_init;
1436 dev->halt = ppc_4xx_eth_halt;
1437 dev->send = ppc_4xx_eth_send;
1438 dev->recv = ppc_4xx_eth_rx;
wdenk544e9732004-02-06 23:19:44 +00001439
1440 if (0 == virgin) {
1441 /* set the MAL IER ??? names may change with new spec ??? */
1442 mal_ier =
1443 MAL_IER_DE | MAL_IER_NE | MAL_IER_TE |
1444 MAL_IER_OPBE | MAL_IER_PLBE;
1445 mtdcr (malesr, 0xffffffff); /* clear pending interrupts */
1446 mtdcr (maltxdeir, 0xffffffff); /* clear pending interrupts */
1447 mtdcr (malrxdeir, 0xffffffff); /* clear pending interrupts */
1448 mtdcr (malier, mal_ier);
1449
1450 /* install MAL interrupt handler */
1451 irq_install_handler (VECNUM_MS,
1452 (interrupt_handler_t *) enetInt,
1453 dev);
1454 irq_install_handler (VECNUM_MTE,
1455 (interrupt_handler_t *) enetInt,
1456 dev);
1457 irq_install_handler (VECNUM_MRE,
1458 (interrupt_handler_t *) enetInt,
1459 dev);
1460 irq_install_handler (VECNUM_TXDE,
1461 (interrupt_handler_t *) enetInt,
1462 dev);
1463 irq_install_handler (VECNUM_RXDE,
1464 (interrupt_handler_t *) enetInt,
1465 dev);
1466 virgin = 1;
1467 }
1468
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001469#if defined(CONFIG_NET_MULTI)
wdenk544e9732004-02-06 23:19:44 +00001470 eth_register (dev);
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001471#else
1472 emac0_dev = dev;
1473#endif
Marian Balakowiczaab8c492005-10-28 22:30:33 +02001474#if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
1475 miiphy_register (dev->name,
1476 emac4xx_miiphy_read, emac4xx_miiphy_write);
1477#endif
wdenk544e9732004-02-06 23:19:44 +00001478
1479 } /* end for each supported device */
1480 return (1);
1481}
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001482
1483
1484#if !defined(CONFIG_NET_MULTI)
1485void eth_halt (void) {
1486 if (emac0_dev) {
1487 ppc_4xx_eth_halt(emac0_dev);
1488 free(emac0_dev);
1489 emac0_dev = NULL;
1490 }
1491}
1492
1493int eth_init (bd_t *bis)
1494{
1495 ppc_4xx_eth_initialize(bis);
Stefan Roese03510612005-10-10 17:43:58 +02001496 if (emac0_dev) {
1497 return ppc_4xx_eth_init(emac0_dev, bis);
1498 } else {
1499 printf("ERROR: ethaddr not set!\n");
1500 return -1;
1501 }
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001502}
1503
1504int eth_send(volatile void *packet, int length)
1505{
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001506 return (ppc_4xx_eth_send(emac0_dev, packet, length));
1507}
1508
1509int eth_rx(void)
1510{
1511 return (ppc_4xx_eth_rx(emac0_dev));
1512}
Marian Balakowiczaab8c492005-10-28 22:30:33 +02001513
1514int emac4xx_miiphy_initialize (bd_t * bis)
1515{
1516#if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
1517 miiphy_register ("ppc_4xx_eth0",
1518 emac4xx_miiphy_read, emac4xx_miiphy_write);
1519#endif
1520
1521 return 0;
1522}
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001523#endif /* !defined(CONFIG_NET_MULTI) */
1524
1525#endif /* #if (CONFIG_COMMANDS & CFG_CMD_NET) */