blob: d3f1de4359ba1a1166dd043eb87b29f4e418ecd9 [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
145
wdenk544e9732004-02-06 23:19:44 +0000146/*-----------------------------------------------------------------------------+
147 * Prototypes and externals.
148 *-----------------------------------------------------------------------------*/
149static void enet_rcv (struct eth_device *dev, unsigned long malisr);
150
151int enetInt (struct eth_device *dev);
152static void mal_err (struct eth_device *dev, unsigned long isr,
153 unsigned long uic, unsigned long maldef,
154 unsigned long mal_errr);
155static void emac_err (struct eth_device *dev, unsigned long isr);
156
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200157
wdenk544e9732004-02-06 23:19:44 +0000158/*-----------------------------------------------------------------------------+
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200159| ppc_4xx_eth_halt
wdenk544e9732004-02-06 23:19:44 +0000160| Disable MAL channel, and EMACn
wdenk544e9732004-02-06 23:19:44 +0000161+-----------------------------------------------------------------------------*/
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200162static void ppc_4xx_eth_halt (struct eth_device *dev)
wdenk544e9732004-02-06 23:19:44 +0000163{
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200164 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenk544e9732004-02-06 23:19:44 +0000165 uint32_t failsafe = 10000;
166
167 out32 (EMAC_IER + hw_p->hw_addr, 0x00000000); /* disable emac interrupts */
168
169 /* 1st reset MAL channel */
170 /* Note: writing a 0 to a channel has no effect */
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200171#if defined(CONFIG_405EP) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
172 mtdcr (maltxcarr, (MAL_CR_MMSR >> (hw_p->devnum * 2)));
173#else
wdenk544e9732004-02-06 23:19:44 +0000174 mtdcr (maltxcarr, (MAL_CR_MMSR >> hw_p->devnum));
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200175#endif
wdenk544e9732004-02-06 23:19:44 +0000176 mtdcr (malrxcarr, (MAL_CR_MMSR >> hw_p->devnum));
177
178 /* wait for reset */
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200179 while (mfdcr (malrxcasr) & (MAL_CR_MMSR >> hw_p->devnum)) {
wdenk544e9732004-02-06 23:19:44 +0000180 udelay (1000); /* Delay 1 MS so as not to hammer the register */
181 failsafe--;
182 if (failsafe == 0)
183 break;
wdenk544e9732004-02-06 23:19:44 +0000184 }
185
186 /* EMAC RESET */
187 out32 (EMAC_M0 + hw_p->hw_addr, EMAC_M0_SRST);
188
Stefan Roese326c9712005-08-01 16:41:48 +0200189 hw_p->print_speed = 1; /* print speed message again next time */
190
wdenk544e9732004-02-06 23:19:44 +0000191 return;
192}
193
194extern int phy_setup_aneg (unsigned char addr);
195extern int miiphy_reset (unsigned char addr);
196
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200197#if defined (CONFIG_440GX)
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200198int ppc_4xx_eth_setup_bridge(int devnum, bd_t * bis)
wdenked2ac4b2004-03-14 18:23:55 +0000199{
200 unsigned long pfc1;
201 unsigned long zmiifer;
202 unsigned long rmiifer;
203
204 mfsdr(sdr_pfc1, pfc1);
205 pfc1 = SDR0_PFC1_EPS_DECODE(pfc1);
206
207 zmiifer = 0;
208 rmiifer = 0;
209
210 switch (pfc1) {
211 case 1:
212 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
213 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(1);
214 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(2);
215 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(3);
216 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
217 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
218 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
219 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
220 break;
221 case 2:
222 zmiifer = ZMII_FER_SMII << ZMII_FER_V(0);
223 zmiifer = ZMII_FER_SMII << ZMII_FER_V(1);
224 zmiifer = ZMII_FER_SMII << ZMII_FER_V(2);
225 zmiifer = ZMII_FER_SMII << ZMII_FER_V(3);
226 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
227 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
228 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
229 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
230 break;
231 case 3:
232 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
233 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
234 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
235 bis->bi_phymode[1] = BI_PHYMODE_NONE;
236 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
237 bis->bi_phymode[3] = BI_PHYMODE_NONE;
238 break;
239 case 4:
240 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(0);
241 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(1);
242 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V (2);
243 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V (3);
244 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
245 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
246 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
247 bis->bi_phymode[3] = BI_PHYMODE_RGMII;
248 break;
249 case 5:
250 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (0);
251 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (1);
252 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (2);
253 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(3);
254 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
255 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
256 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
257 bis->bi_phymode[3] = BI_PHYMODE_RGMII;
258 break;
259 case 6:
260 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (0);
261 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (1);
262 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
wdenked2ac4b2004-03-14 18:23:55 +0000263 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
264 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
265 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
wdenked2ac4b2004-03-14 18:23:55 +0000266 break;
267 case 0:
268 default:
269 zmiifer = ZMII_FER_MII << ZMII_FER_V(devnum);
270 rmiifer = 0x0;
271 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
272 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
273 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
274 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
275 break;
276 }
277
278 /* Ensure we setup mdio for this devnum and ONLY this devnum */
279 zmiifer |= (ZMII_FER_MDI) << ZMII_FER_V(devnum);
280
281 out32 (ZMII_FER, zmiifer);
282 out32 (RGMII_FER, rmiifer);
283
284 return ((int)pfc1);
285
286}
287#endif
288
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200289static int ppc_4xx_eth_init (struct eth_device *dev, bd_t * bis)
wdenk544e9732004-02-06 23:19:44 +0000290{
Stefan Roese5ff4c3f2005-08-15 12:31:23 +0200291 int i, j;
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200292 unsigned long reg = 0;
wdenk544e9732004-02-06 23:19:44 +0000293 unsigned long msr;
294 unsigned long speed;
295 unsigned long duplex;
296 unsigned long failsafe;
297 unsigned mode_reg;
298 unsigned short devnum;
299 unsigned short reg_short;
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200300#if defined(CONFIG_440GX)
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200301 sys_info_t sysinfo;
wdenked2ac4b2004-03-14 18:23:55 +0000302 int ethgroup;
Stefan Roese326c9712005-08-01 16:41:48 +0200303#endif
wdenk544e9732004-02-06 23:19:44 +0000304
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200305 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenk544e9732004-02-06 23:19:44 +0000306
307 /* before doing anything, figure out if we have a MAC address */
308 /* if not, bail */
Stefan Roese03510612005-10-10 17:43:58 +0200309 if (memcmp (dev->enetaddr, "\0\0\0\0\0\0", 6) == 0) {
310 printf("ERROR: ethaddr not set!\n");
wdenk544e9732004-02-06 23:19:44 +0000311 return -1;
Stefan Roese03510612005-10-10 17:43:58 +0200312 }
wdenk544e9732004-02-06 23:19:44 +0000313
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200314#if defined(CONFIG_440GX)
wdenk544e9732004-02-06 23:19:44 +0000315 /* Need to get the OPB frequency so we can access the PHY */
316 get_sys_info (&sysinfo);
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200317#endif
wdenk544e9732004-02-06 23:19:44 +0000318
wdenk544e9732004-02-06 23:19:44 +0000319 msr = mfmsr ();
320 mtmsr (msr & ~(MSR_EE)); /* disable interrupts */
321
322 devnum = hw_p->devnum;
323
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200324#ifdef INFO_4XX_ENET
wdenk544e9732004-02-06 23:19:44 +0000325 /* AS.HARNOIS
326 * We should have :
Wolfgang Denk0cbaf642005-09-25 00:53:22 +0200327 * hw_p->stats.pkts_handled <= hw_p->stats.pkts_rx <= hw_p->stats.pkts_handled+PKTBUFSRX
wdenk544e9732004-02-06 23:19:44 +0000328 * In the most cases hw_p->stats.pkts_handled = hw_p->stats.pkts_rx, but it
329 * is possible that new packets (without relationship with
330 * current transfer) have got the time to arrived before
331 * netloop calls eth_halt
332 */
333 printf ("About preceeding transfer (eth%d):\n"
334 "- Sent packet number %d\n"
335 "- Received packet number %d\n"
336 "- Handled packet number %d\n",
337 hw_p->devnum,
338 hw_p->stats.pkts_tx,
339 hw_p->stats.pkts_rx, hw_p->stats.pkts_handled);
340
341 hw_p->stats.pkts_tx = 0;
342 hw_p->stats.pkts_rx = 0;
343 hw_p->stats.pkts_handled = 0;
344#endif
345
Wolfgang Denk0cbaf642005-09-25 00:53:22 +0200346 hw_p->tx_err_index = 0; /* Transmit Error Index for tx_err_log */
347 hw_p->rx_err_index = 0; /* Receive Error Index for rx_err_log */
wdenk544e9732004-02-06 23:19:44 +0000348
349 hw_p->rx_slot = 0; /* MAL Receive Slot */
350 hw_p->rx_i_index = 0; /* Receive Interrupt Queue Index */
351 hw_p->rx_u_index = 0; /* Receive User Queue Index */
352
353 hw_p->tx_slot = 0; /* MAL Transmit Slot */
354 hw_p->tx_i_index = 0; /* Transmit Interrupt Queue Index */
355 hw_p->tx_u_index = 0; /* Transmit User Queue Index */
356
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200357#if defined(CONFIG_440)
wdenk544e9732004-02-06 23:19:44 +0000358 /* set RMII mode */
359 /* NOTE: 440GX spec states that mode is mutually exclusive */
360 /* NOTE: Therefore, disable all other EMACS, since we handle */
361 /* NOTE: only one emac at a time */
362 reg = 0;
363 out32 (ZMII_FER, 0);
364 udelay (100);
wdenk544e9732004-02-06 23:19:44 +0000365
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200366#if defined(CONFIG_440EP) || defined(CONFIG_440GR)
Wolfgang Denk0cbaf642005-09-25 00:53:22 +0200367 out32 (ZMII_FER, (ZMII_FER_RMII | ZMII_FER_MDI) << ZMII_FER_V (devnum));
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200368#elif defined(CONFIG_440GX)
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200369 ethgroup = ppc_4xx_eth_setup_bridge(devnum, bis);
Stefan Roese74309032005-09-07 16:21:12 +0200370#elif defined(CONFIG_440GP)
371 /* set RMII mode */
372 out32 (ZMII_FER, ZMII_RMII | ZMII_MDI0);
wdenk00fe1612004-03-14 00:07:33 +0000373#else
374 if ((devnum == 0) || (devnum == 1)) {
375 out32 (ZMII_FER, (ZMII_FER_SMII | ZMII_FER_MDI) << ZMII_FER_V (devnum));
376 }
377 else { /* ((devnum == 2) || (devnum == 3)) */
378 out32 (ZMII_FER, ZMII_FER_MDI << ZMII_FER_V (devnum));
wdenk544e9732004-02-06 23:19:44 +0000379 out32 (RGMII_FER, ((RGMII_FER_RGMII << RGMII_FER_V (2)) |
380 (RGMII_FER_RGMII << RGMII_FER_V (3))));
wdenk00fe1612004-03-14 00:07:33 +0000381 }
382#endif
Stefan Roese797d8572005-08-11 17:56:56 +0200383
wdenk00fe1612004-03-14 00:07:33 +0000384 out32 (ZMII_SSR, ZMII_SSR_SP << ZMII_SSR_V(devnum));
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200385#endif /* defined(CONFIG_440) */
386
wdenk00fe1612004-03-14 00:07:33 +0000387 __asm__ volatile ("eieio");
388
389 /* reset emac so we have access to the phy */
390
391 out32 (EMAC_M0 + hw_p->hw_addr, EMAC_M0_SRST);
wdenk544e9732004-02-06 23:19:44 +0000392 __asm__ volatile ("eieio");
393
394 failsafe = 1000;
395 while ((in32 (EMAC_M0 + hw_p->hw_addr) & (EMAC_M0_SRST)) && failsafe) {
396 udelay (1000);
397 failsafe--;
398 }
399
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200400#if defined(CONFIG_440GX)
wdenk544e9732004-02-06 23:19:44 +0000401 /* Whack the M1 register */
402 mode_reg = 0x0;
403 mode_reg &= ~0x00000038;
404 if (sysinfo.freqOPB <= 50000000);
405 else if (sysinfo.freqOPB <= 66666667)
406 mode_reg |= EMAC_M1_OBCI_66;
407 else if (sysinfo.freqOPB <= 83333333)
408 mode_reg |= EMAC_M1_OBCI_83;
409 else if (sysinfo.freqOPB <= 100000000)
410 mode_reg |= EMAC_M1_OBCI_100;
411 else
412 mode_reg |= EMAC_M1_OBCI_GT100;
413
414 out32 (EMAC_M1 + hw_p->hw_addr, mode_reg);
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200415#endif /* defined(CONFIG_440GX) */
wdenk544e9732004-02-06 23:19:44 +0000416
417 /* wait for PHY to complete auto negotiation */
418 reg_short = 0;
419#ifndef CONFIG_CS8952_PHY
420 switch (devnum) {
421 case 0:
422 reg = CONFIG_PHY_ADDR;
423 break;
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200424#if defined (CONFIG_PHY1_ADDR)
wdenk544e9732004-02-06 23:19:44 +0000425 case 1:
426 reg = CONFIG_PHY1_ADDR;
427 break;
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200428#endif
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200429#if defined (CONFIG_440GX)
wdenk544e9732004-02-06 23:19:44 +0000430 case 2:
431 reg = CONFIG_PHY2_ADDR;
432 break;
433 case 3:
434 reg = CONFIG_PHY3_ADDR;
435 break;
436#endif
437 default:
438 reg = CONFIG_PHY_ADDR;
439 break;
440 }
441
wdenk56ed43e2004-02-22 23:46:08 +0000442 bis->bi_phynum[devnum] = reg;
443
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200444#if defined(CONFIG_PHY_RESET)
wdenk97e8bda2004-09-29 22:43:59 +0000445 /*
446 * Reset the phy, only if its the first time through
447 * otherwise, just check the speeds & feeds
448 */
449 if (hw_p->first_init == 0) {
450 miiphy_reset (reg);
wdenk544e9732004-02-06 23:19:44 +0000451
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200452#if defined(CONFIG_440GX)
wdenk00fe1612004-03-14 00:07:33 +0000453#if defined(CONFIG_CIS8201_PHY)
wdenk7ad5e4c2004-04-25 15:41:35 +0000454 /*
Stefan Roese363330b2005-08-04 17:09:16 +0200455 * Cicada 8201 PHY needs to have an extended register whacked
456 * for RGMII mode.
wdenk7ad5e4c2004-04-25 15:41:35 +0000457 */
Stefan Roese363330b2005-08-04 17:09:16 +0200458 if ( ((devnum == 2) || (devnum ==3)) && (4 == ethgroup) ) {
Stefan Roese5ff4c3f2005-08-15 12:31:23 +0200459#if defined(CONFIG_CIS8201_SHORT_ETCH)
460 miiphy_write (reg, 23, 0x1300);
461#else
462 miiphy_write (reg, 23, 0x1000);
463#endif
Stefan Roese363330b2005-08-04 17:09:16 +0200464 /*
465 * Vitesse VSC8201/Cicada CIS8201 errata:
466 * Interoperability problem with Intel 82547EI phys
467 * This work around (provided by Vitesse) changes
468 * the default timer convergence from 8ms to 12ms
469 */
470 miiphy_write (reg, 0x1f, 0x2a30);
471 miiphy_write (reg, 0x08, 0x0200);
472 miiphy_write (reg, 0x1f, 0x52b5);
473 miiphy_write (reg, 0x02, 0x0004);
474 miiphy_write (reg, 0x01, 0x0671);
475 miiphy_write (reg, 0x00, 0x8fae);
476 miiphy_write (reg, 0x1f, 0x2a30);
477 miiphy_write (reg, 0x08, 0x0000);
478 miiphy_write (reg, 0x1f, 0x0000);
479 /* end Vitesse/Cicada errata */
480 }
wdenk00fe1612004-03-14 00:07:33 +0000481#endif
wdenked2ac4b2004-03-14 18:23:55 +0000482#endif
wdenk97e8bda2004-09-29 22:43:59 +0000483 /* Start/Restart autonegotiation */
484 phy_setup_aneg (reg);
485 udelay (1000);
486 }
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200487#endif /* defined(CONFIG_PHY_RESET) */
wdenk544e9732004-02-06 23:19:44 +0000488
489 miiphy_read (reg, PHY_BMSR, &reg_short);
490
491 /*
wdenk00fe1612004-03-14 00:07:33 +0000492 * Wait if PHY is capable of autonegotiation and autonegotiation is not complete
wdenk544e9732004-02-06 23:19:44 +0000493 */
494 if ((reg_short & PHY_BMSR_AUTN_ABLE)
495 && !(reg_short & PHY_BMSR_AUTN_COMP)) {
496 puts ("Waiting for PHY auto negotiation to complete");
497 i = 0;
498 while (!(reg_short & PHY_BMSR_AUTN_COMP)) {
499 /*
500 * Timeout reached ?
501 */
502 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
503 puts (" TIMEOUT !\n");
504 break;
505 }
506
507 if ((i++ % 1000) == 0) {
508 putc ('.');
509 }
510 udelay (1000); /* 1 ms */
511 miiphy_read (reg, PHY_BMSR, &reg_short);
512
513 }
514 puts (" done\n");
515 udelay (500000); /* another 500 ms (results in faster booting) */
516 }
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200517#endif /* #ifndef CONFIG_CS8952_PHY */
518
wdenk544e9732004-02-06 23:19:44 +0000519 speed = miiphy_speed (reg);
520 duplex = miiphy_duplex (reg);
521
522 if (hw_p->print_speed) {
523 hw_p->print_speed = 0;
524 printf ("ENET Speed is %d Mbps - %s duplex connection\n",
525 (int) speed, (duplex == HALF) ? "HALF" : "FULL");
526 }
527
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200528#if defined(CONFIG_440)
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200529#if defined(CONFIG_440EP) || defined(CONFIG_440GR)
Stefan Roese326c9712005-08-01 16:41:48 +0200530 mfsdr(sdr_mfr, reg);
531 if (speed == 100) {
532 reg = (reg & ~SDR0_MFR_ZMII_MODE_MASK) | SDR0_MFR_ZMII_MODE_RMII_100M;
533 } else {
534 reg = (reg & ~SDR0_MFR_ZMII_MODE_MASK) | SDR0_MFR_ZMII_MODE_RMII_10M;
535 }
536 mtsdr(sdr_mfr, reg);
537#endif
Stefan Roese797d8572005-08-11 17:56:56 +0200538
wdenk544e9732004-02-06 23:19:44 +0000539 /* Set ZMII/RGMII speed according to the phy link speed */
540 reg = in32 (ZMII_SSR);
wdenked2ac4b2004-03-14 18:23:55 +0000541 if ( (speed == 100) || (speed == 1000) )
wdenk544e9732004-02-06 23:19:44 +0000542 out32 (ZMII_SSR, reg | (ZMII_SSR_SP << ZMII_SSR_V (devnum)));
543 else
Stefan Roese797d8572005-08-11 17:56:56 +0200544 out32 (ZMII_SSR, reg & (~(ZMII_SSR_SP << ZMII_SSR_V (devnum))));
wdenk544e9732004-02-06 23:19:44 +0000545
546 if ((devnum == 2) || (devnum == 3)) {
547 if (speed == 1000)
548 reg = (RGMII_SSR_SP_1000MBPS << RGMII_SSR_V (devnum));
549 else if (speed == 100)
550 reg = (RGMII_SSR_SP_100MBPS << RGMII_SSR_V (devnum));
551 else
552 reg = (RGMII_SSR_SP_10MBPS << RGMII_SSR_V (devnum));
553
554 out32 (RGMII_SSR, reg);
555 }
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200556#endif /* defined(CONFIG_440) */
wdenk544e9732004-02-06 23:19:44 +0000557
558 /* set the Mal configuration reg */
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200559#if defined(CONFIG_440GX)
Stefan Roese363330b2005-08-04 17:09:16 +0200560 mtdcr (malmcr, MAL_CR_PLBB | MAL_CR_OPBBL | MAL_CR_LEA |
561 MAL_CR_PLBLT_DEFAULT | MAL_CR_EOPIE | 0x00330000);
562#else
563 mtdcr (malmcr, MAL_CR_PLBB | MAL_CR_OPBBL | MAL_CR_LEA | MAL_CR_PLBLT_DEFAULT);
wdenk544e9732004-02-06 23:19:44 +0000564 /* Errata 1.12: MAL_1 -- Disable MAL bursting */
Stefan Roese363330b2005-08-04 17:09:16 +0200565 if (get_pvr() == PVR_440GP_RB) {
566 mtdcr (malmcr, mfdcr(malmcr) & ~MAL_CR_PLBB);
567 }
568#endif
wdenk544e9732004-02-06 23:19:44 +0000569
570 /* Free "old" buffers */
571 if (hw_p->alloc_tx_buf)
572 free (hw_p->alloc_tx_buf);
573 if (hw_p->alloc_rx_buf)
574 free (hw_p->alloc_rx_buf);
575
576 /*
577 * Malloc MAL buffer desciptors, make sure they are
578 * aligned on cache line boundary size
579 * (401/403/IOP480 = 16, 405 = 32)
580 * and doesn't cross cache block boundaries.
581 */
582 hw_p->alloc_tx_buf =
583 (mal_desc_t *) malloc ((sizeof (mal_desc_t) * NUM_TX_BUFF) +
584 ((2 * CFG_CACHELINE_SIZE) - 2));
Stefan Roese5ff4c3f2005-08-15 12:31:23 +0200585 if (NULL == hw_p->alloc_tx_buf)
586 return -1;
wdenk544e9732004-02-06 23:19:44 +0000587 if (((int) hw_p->alloc_tx_buf & CACHELINE_MASK) != 0) {
588 hw_p->tx =
589 (mal_desc_t *) ((int) hw_p->alloc_tx_buf +
590 CFG_CACHELINE_SIZE -
591 ((int) hw_p->
592 alloc_tx_buf & CACHELINE_MASK));
593 } else {
594 hw_p->tx = hw_p->alloc_tx_buf;
595 }
596
597 hw_p->alloc_rx_buf =
598 (mal_desc_t *) malloc ((sizeof (mal_desc_t) * NUM_RX_BUFF) +
599 ((2 * CFG_CACHELINE_SIZE) - 2));
Stefan Roese5ff4c3f2005-08-15 12:31:23 +0200600 if (NULL == hw_p->alloc_rx_buf) {
601 free(hw_p->alloc_tx_buf);
602 hw_p->alloc_tx_buf = NULL;
603 return -1;
604 }
605
wdenk544e9732004-02-06 23:19:44 +0000606 if (((int) hw_p->alloc_rx_buf & CACHELINE_MASK) != 0) {
607 hw_p->rx =
608 (mal_desc_t *) ((int) hw_p->alloc_rx_buf +
609 CFG_CACHELINE_SIZE -
610 ((int) hw_p->
611 alloc_rx_buf & CACHELINE_MASK));
612 } else {
613 hw_p->rx = hw_p->alloc_rx_buf;
614 }
615
616 for (i = 0; i < NUM_TX_BUFF; i++) {
617 hw_p->tx[i].ctrl = 0;
618 hw_p->tx[i].data_len = 0;
Stefan Roese5ff4c3f2005-08-15 12:31:23 +0200619 if (hw_p->first_init == 0) {
wdenk544e9732004-02-06 23:19:44 +0000620 hw_p->txbuf_ptr =
621 (char *) malloc (ENET_MAX_MTU_ALIGNED);
Stefan Roese5ff4c3f2005-08-15 12:31:23 +0200622 if (NULL == hw_p->txbuf_ptr) {
623 free(hw_p->alloc_rx_buf);
624 free(hw_p->alloc_tx_buf);
625 hw_p->alloc_rx_buf = NULL;
626 hw_p->alloc_tx_buf = NULL;
627 for(j = 0; j < i; j++) {
628 free(hw_p->tx[i].data_ptr);
629 hw_p->tx[i].data_ptr = NULL;
630 }
631 }
632 }
wdenk544e9732004-02-06 23:19:44 +0000633 hw_p->tx[i].data_ptr = hw_p->txbuf_ptr;
634 if ((NUM_TX_BUFF - 1) == i)
635 hw_p->tx[i].ctrl |= MAL_TX_CTRL_WRAP;
636 hw_p->tx_run[i] = -1;
637#if 0
638 printf ("TX_BUFF %d @ 0x%08lx\n", i,
639 (ulong) hw_p->tx[i].data_ptr);
640#endif
641 }
642
643 for (i = 0; i < NUM_RX_BUFF; i++) {
644 hw_p->rx[i].ctrl = 0;
645 hw_p->rx[i].data_len = 0;
Wolfgang Denk0cbaf642005-09-25 00:53:22 +0200646 /* rx[i].data_ptr = (char *) &rx_buff[i]; */
wdenk544e9732004-02-06 23:19:44 +0000647 hw_p->rx[i].data_ptr = (char *) NetRxPackets[i];
648 if ((NUM_RX_BUFF - 1) == i)
649 hw_p->rx[i].ctrl |= MAL_RX_CTRL_WRAP;
650 hw_p->rx[i].ctrl |= MAL_RX_CTRL_EMPTY | MAL_RX_CTRL_INTR;
651 hw_p->rx_ready[i] = -1;
652#if 0
653 printf ("RX_BUFF %d @ 0x%08lx\n", i, (ulong) rx[i].data_ptr);
654#endif
655 }
656
657 reg = 0x00000000;
658
659 reg |= dev->enetaddr[0]; /* set high address */
660 reg = reg << 8;
661 reg |= dev->enetaddr[1];
662
663 out32 (EMAC_IAH + hw_p->hw_addr, reg);
664
665 reg = 0x00000000;
666 reg |= dev->enetaddr[2]; /* set low address */
667 reg = reg << 8;
668 reg |= dev->enetaddr[3];
669 reg = reg << 8;
670 reg |= dev->enetaddr[4];
671 reg = reg << 8;
672 reg |= dev->enetaddr[5];
673
674 out32 (EMAC_IAL + hw_p->hw_addr, reg);
675
676 switch (devnum) {
677 case 1:
678 /* setup MAL tx & rx channel pointers */
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200679#if defined (CONFIG_405EP) || defined (CONFIG_440EP) || defined (CONFIG_440GR)
Stefan Roese326c9712005-08-01 16:41:48 +0200680 mtdcr (maltxctp2r, hw_p->tx);
681#else
wdenk544e9732004-02-06 23:19:44 +0000682 mtdcr (maltxctp1r, hw_p->tx);
Stefan Roese326c9712005-08-01 16:41:48 +0200683#endif
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200684#if defined(CONFIG_440)
Stefan Roese326c9712005-08-01 16:41:48 +0200685 mtdcr (maltxbattr, 0x0);
wdenk544e9732004-02-06 23:19:44 +0000686 mtdcr (malrxbattr, 0x0);
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200687#endif
wdenk544e9732004-02-06 23:19:44 +0000688 mtdcr (malrxctp1r, hw_p->rx);
689 /* set RX buffer size */
690 mtdcr (malrcbs1, ENET_MAX_MTU_ALIGNED / 16);
691 break;
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200692#if defined (CONFIG_440GX)
wdenk544e9732004-02-06 23:19:44 +0000693 case 2:
694 /* setup MAL tx & rx channel pointers */
695 mtdcr (maltxbattr, 0x0);
wdenk544e9732004-02-06 23:19:44 +0000696 mtdcr (malrxbattr, 0x0);
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200697 mtdcr (maltxctp2r, hw_p->tx);
wdenk544e9732004-02-06 23:19:44 +0000698 mtdcr (malrxctp2r, hw_p->rx);
699 /* set RX buffer size */
700 mtdcr (malrcbs2, ENET_MAX_MTU_ALIGNED / 16);
701 break;
702 case 3:
703 /* setup MAL tx & rx channel pointers */
704 mtdcr (maltxbattr, 0x0);
705 mtdcr (maltxctp3r, hw_p->tx);
706 mtdcr (malrxbattr, 0x0);
707 mtdcr (malrxctp3r, hw_p->rx);
708 /* set RX buffer size */
709 mtdcr (malrcbs3, ENET_MAX_MTU_ALIGNED / 16);
710 break;
Stefan Roese797d8572005-08-11 17:56:56 +0200711#endif /* CONFIG_440GX */
wdenk544e9732004-02-06 23:19:44 +0000712 case 0:
713 default:
714 /* setup MAL tx & rx channel pointers */
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200715#if defined(CONFIG_440)
wdenk544e9732004-02-06 23:19:44 +0000716 mtdcr (maltxbattr, 0x0);
wdenk544e9732004-02-06 23:19:44 +0000717 mtdcr (malrxbattr, 0x0);
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200718#endif
719 mtdcr (maltxctp0r, hw_p->tx);
wdenk544e9732004-02-06 23:19:44 +0000720 mtdcr (malrxctp0r, hw_p->rx);
721 /* set RX buffer size */
722 mtdcr (malrcbs0, ENET_MAX_MTU_ALIGNED / 16);
723 break;
724 }
725
726 /* Enable MAL transmit and receive channels */
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200727#if defined(CONFIG_405EP) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
Stefan Roese326c9712005-08-01 16:41:48 +0200728 mtdcr (maltxcasr, (MAL_TXRX_CASR >> (hw_p->devnum*2)));
729#else
wdenk544e9732004-02-06 23:19:44 +0000730 mtdcr (maltxcasr, (MAL_TXRX_CASR >> hw_p->devnum));
Stefan Roese326c9712005-08-01 16:41:48 +0200731#endif
wdenk544e9732004-02-06 23:19:44 +0000732 mtdcr (malrxcasr, (MAL_TXRX_CASR >> hw_p->devnum));
733
734 /* set transmit enable & receive enable */
735 out32 (EMAC_M0 + hw_p->hw_addr, EMAC_M0_TXE | EMAC_M0_RXE);
736
737 /* set receive fifo to 4k and tx fifo to 2k */
738 mode_reg = in32 (EMAC_M1 + hw_p->hw_addr);
739 mode_reg |= EMAC_M1_RFS_4K | EMAC_M1_TX_FIFO_2K;
740
741 /* set speed */
wdenked2ac4b2004-03-14 18:23:55 +0000742 if (speed == _1000BASET)
743 mode_reg = mode_reg | EMAC_M1_MF_1000MBPS | EMAC_M1_IST;
744 else if (speed == _100BASET)
wdenk544e9732004-02-06 23:19:44 +0000745 mode_reg = mode_reg | EMAC_M1_MF_100MBPS | EMAC_M1_IST;
746 else
747 mode_reg = mode_reg & ~0x00C00000; /* 10 MBPS */
748 if (duplex == FULL)
749 mode_reg = mode_reg | 0x80000000 | EMAC_M1_IST;
750
751 out32 (EMAC_M1 + hw_p->hw_addr, mode_reg);
752
753 /* Enable broadcast and indvidual address */
754 /* TBS: enabling runts as some misbehaved nics will send runts */
755 out32 (EMAC_RXM + hw_p->hw_addr, EMAC_RMR_BAE | EMAC_RMR_IAE);
756
757 /* we probably need to set the tx mode1 reg? maybe at tx time */
758
759 /* set transmit request threshold register */
760 out32 (EMAC_TRTR + hw_p->hw_addr, 0x18000000); /* 256 byte threshold */
761
Wolfgang Denk0cbaf642005-09-25 00:53:22 +0200762 /* set receive low/high water mark register */
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200763#if defined(CONFIG_440)
wdenk544e9732004-02-06 23:19:44 +0000764 /* 440GP has a 64 byte burst length */
765 out32 (EMAC_RX_HI_LO_WMARK + hw_p->hw_addr, 0x80009000);
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200766#else
767 /* 405s have a 16 byte burst length */
768 out32 (EMAC_RX_HI_LO_WMARK + hw_p->hw_addr, 0x0f002000);
769#endif /* defined(CONFIG_440) */
wdenk544e9732004-02-06 23:19:44 +0000770 out32 (EMAC_TXM1 + hw_p->hw_addr, 0xf8640000);
771
772 /* Set fifo limit entry in tx mode 0 */
773 out32 (EMAC_TXM0 + hw_p->hw_addr, 0x00000003);
774 /* Frame gap set */
775 out32 (EMAC_I_FRAME_GAP_REG + hw_p->hw_addr, 0x00000008);
776
777 /* Set EMAC IER */
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200778 hw_p->emac_ier = EMAC_ISR_PTLE | EMAC_ISR_BFCS | EMAC_ISR_ORE | EMAC_ISR_IRE;
wdenk544e9732004-02-06 23:19:44 +0000779 if (speed == _100BASET)
780 hw_p->emac_ier = hw_p->emac_ier | EMAC_ISR_SYE;
781
782 out32 (EMAC_ISR + hw_p->hw_addr, 0xffffffff); /* clear pending interrupts */
783 out32 (EMAC_IER + hw_p->hw_addr, hw_p->emac_ier);
784
785 if (hw_p->first_init == 0) {
786 /*
787 * Connect interrupt service routines
788 */
wdenk544e9732004-02-06 23:19:44 +0000789 irq_install_handler (VECNUM_ETH0 + (hw_p->devnum * 2),
790 (interrupt_handler_t *) enetInt, dev);
791 }
wdenk544e9732004-02-06 23:19:44 +0000792
793 mtmsr (msr); /* enable interrupts again */
794
795 hw_p->bis = bis;
796 hw_p->first_init = 1;
797
798 return (1);
799}
800
801
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200802static int ppc_4xx_eth_send (struct eth_device *dev, volatile void *ptr,
wdenk544e9732004-02-06 23:19:44 +0000803 int len)
804{
805 struct enet_frame *ef_ptr;
806 ulong time_start, time_now;
807 unsigned long temp_txm0;
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200808 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenk544e9732004-02-06 23:19:44 +0000809
810 ef_ptr = (struct enet_frame *) ptr;
811
812 /*-----------------------------------------------------------------------+
813 * Copy in our address into the frame.
814 *-----------------------------------------------------------------------*/
815 (void) memcpy (ef_ptr->source_addr, dev->enetaddr, ENET_ADDR_LENGTH);
816
817 /*-----------------------------------------------------------------------+
818 * If frame is too long or too short, modify length.
819 *-----------------------------------------------------------------------*/
820 /* TBS: where does the fragment go???? */
821 if (len > ENET_MAX_MTU)
822 len = ENET_MAX_MTU;
823
824 /* memcpy ((void *) &tx_buff[tx_slot], (const void *) ptr, len); */
825 memcpy ((void *) hw_p->txbuf_ptr, (const void *) ptr, len);
826
827 /*-----------------------------------------------------------------------+
828 * set TX Buffer busy, and send it
829 *-----------------------------------------------------------------------*/
830 hw_p->tx[hw_p->tx_slot].ctrl = (MAL_TX_CTRL_LAST |
831 EMAC_TX_CTRL_GFCS | EMAC_TX_CTRL_GP) &
832 ~(EMAC_TX_CTRL_ISA | EMAC_TX_CTRL_RSA);
833 if ((NUM_TX_BUFF - 1) == hw_p->tx_slot)
834 hw_p->tx[hw_p->tx_slot].ctrl |= MAL_TX_CTRL_WRAP;
835
836 hw_p->tx[hw_p->tx_slot].data_len = (short) len;
837 hw_p->tx[hw_p->tx_slot].ctrl |= MAL_TX_CTRL_READY;
838
839 __asm__ volatile ("eieio");
840
841 out32 (EMAC_TXM0 + hw_p->hw_addr,
842 in32 (EMAC_TXM0 + hw_p->hw_addr) | EMAC_TXM0_GNP0);
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200843#ifdef INFO_4XX_ENET
wdenk544e9732004-02-06 23:19:44 +0000844 hw_p->stats.pkts_tx++;
845#endif
846
847 /*-----------------------------------------------------------------------+
848 * poll unitl the packet is sent and then make sure it is OK
849 *-----------------------------------------------------------------------*/
850 time_start = get_timer (0);
851 while (1) {
852 temp_txm0 = in32 (EMAC_TXM0 + hw_p->hw_addr);
853 /* loop until either TINT turns on or 3 seconds elapse */
854 if ((temp_txm0 & EMAC_TXM0_GNP0) != 0) {
855 /* transmit is done, so now check for errors
856 * If there is an error, an interrupt should
857 * happen when we return
858 */
859 time_now = get_timer (0);
860 if ((time_now - time_start) > 3000) {
861 return (-1);
862 }
863 } else {
864 return (len);
865 }
866 }
867}
868
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200869#if defined (CONFIG_440)
wdenk544e9732004-02-06 23:19:44 +0000870
871int enetInt (struct eth_device *dev)
872{
873 int serviced;
874 int rc = -1; /* default to not us */
875 unsigned long mal_isr;
876 unsigned long emac_isr = 0;
877 unsigned long mal_rx_eob;
878 unsigned long my_uic0msr, my_uic1msr;
879
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200880#if defined(CONFIG_440GX)
wdenk544e9732004-02-06 23:19:44 +0000881 unsigned long my_uic2msr;
882#endif
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200883 EMAC_4XX_HW_PST hw_p;
wdenk544e9732004-02-06 23:19:44 +0000884
885 /*
886 * Because the mal is generic, we need to get the current
887 * eth device
888 */
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200889#if defined(CONFIG_NET_MULTI)
890 dev = eth_get_dev();
891#else
892 dev = emac0_dev;
893#endif
wdenk544e9732004-02-06 23:19:44 +0000894
895 hw_p = dev->priv;
896
897
898 /* enter loop that stays in interrupt code until nothing to service */
899 do {
900 serviced = 0;
901
902 my_uic0msr = mfdcr (uic0msr);
903 my_uic1msr = mfdcr (uic1msr);
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200904#if defined(CONFIG_440GX)
wdenk544e9732004-02-06 23:19:44 +0000905 my_uic2msr = mfdcr (uic2msr);
906#endif
907 if (!(my_uic0msr & (UIC_MRE | UIC_MTE))
908 && !(my_uic1msr &
909 (UIC_ETH0 | UIC_ETH1 | UIC_MS | UIC_MTDE |
910 UIC_MRDE))) {
911 /* not for us */
912 return (rc);
913 }
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200914#if defined (CONFIG_440GX)
wdenk544e9732004-02-06 23:19:44 +0000915 if (!(my_uic0msr & (UIC_MRE | UIC_MTE))
916 && !(my_uic2msr & (UIC_ETH2 | UIC_ETH3))) {
917 /* not for us */
918 return (rc);
919 }
920#endif
921 /* get and clear controller status interrupts */
922 /* look at Mal and EMAC interrupts */
923 if ((my_uic0msr & (UIC_MRE | UIC_MTE))
924 || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
925 /* we have a MAL interrupt */
926 mal_isr = mfdcr (malesr);
927 /* look for mal error */
928 if (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE)) {
929 mal_err (dev, mal_isr, my_uic0msr,
930 MAL_UIC_DEF, MAL_UIC_ERR);
931 serviced = 1;
932 rc = 0;
933 }
934 }
935
936 /* port by port dispatch of emac interrupts */
937 if (hw_p->devnum == 0) {
938 if (UIC_ETH0 & my_uic1msr) { /* look for EMAC errors */
939 emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
940 if ((hw_p->emac_ier & emac_isr) != 0) {
941 emac_err (dev, emac_isr);
942 serviced = 1;
943 rc = 0;
944 }
945 }
946 if ((hw_p->emac_ier & emac_isr)
947 || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
948 mtdcr (uic0sr, UIC_MRE | UIC_MTE); /* Clear */
949 mtdcr (uic1sr, UIC_ETH0 | UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
950 return (rc); /* we had errors so get out */
951 }
952 }
953
954 if (hw_p->devnum == 1) {
955 if (UIC_ETH1 & my_uic1msr) { /* look for EMAC errors */
956 emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
957 if ((hw_p->emac_ier & emac_isr) != 0) {
958 emac_err (dev, emac_isr);
959 serviced = 1;
960 rc = 0;
961 }
962 }
963 if ((hw_p->emac_ier & emac_isr)
964 || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
965 mtdcr (uic0sr, UIC_MRE | UIC_MTE); /* Clear */
966 mtdcr (uic1sr, UIC_ETH1 | UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
967 return (rc); /* we had errors so get out */
968 }
969 }
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200970#if defined (CONFIG_440GX)
wdenk544e9732004-02-06 23:19:44 +0000971 if (hw_p->devnum == 2) {
972 if (UIC_ETH2 & my_uic2msr) { /* look for EMAC errors */
973 emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
974 if ((hw_p->emac_ier & emac_isr) != 0) {
975 emac_err (dev, emac_isr);
976 serviced = 1;
977 rc = 0;
978 }
979 }
980 if ((hw_p->emac_ier & emac_isr)
981 || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
982 mtdcr (uic0sr, UIC_MRE | UIC_MTE); /* Clear */
983 mtdcr (uic1sr, UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
984 mtdcr (uic2sr, UIC_ETH2);
985 return (rc); /* we had errors so get out */
986 }
987 }
988
989 if (hw_p->devnum == 3) {
990 if (UIC_ETH3 & my_uic2msr) { /* look for EMAC errors */
991 emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
992 if ((hw_p->emac_ier & emac_isr) != 0) {
993 emac_err (dev, emac_isr);
994 serviced = 1;
995 rc = 0;
996 }
997 }
998 if ((hw_p->emac_ier & emac_isr)
999 || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
1000 mtdcr (uic0sr, UIC_MRE | UIC_MTE); /* Clear */
1001 mtdcr (uic1sr, UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
1002 mtdcr (uic2sr, UIC_ETH3);
1003 return (rc); /* we had errors so get out */
1004 }
1005 }
Stefan Roeseb30f2a12005-08-08 12:42:22 +02001006#endif /* CONFIG_440GX */
wdenk544e9732004-02-06 23:19:44 +00001007 /* handle MAX TX EOB interrupt from a tx */
1008 if (my_uic0msr & UIC_MTE) {
1009 mal_rx_eob = mfdcr (maltxeobisr);
1010 mtdcr (maltxeobisr, mal_rx_eob);
1011 mtdcr (uic0sr, UIC_MTE);
1012 }
1013 /* handle MAL RX EOB interupt from a receive */
wdenk7ad5e4c2004-04-25 15:41:35 +00001014 /* check for EOB on valid channels */
wdenk544e9732004-02-06 23:19:44 +00001015 if (my_uic0msr & UIC_MRE) {
1016 mal_rx_eob = mfdcr (malrxeobisr);
Wolfgang Denk0cbaf642005-09-25 00:53:22 +02001017 if ((mal_rx_eob & (0x80000000 >> hw_p->devnum)) != 0) { /* call emac routine for channel x */
wdenk544e9732004-02-06 23:19:44 +00001018 /* clear EOB
1019 mtdcr(malrxeobisr, mal_rx_eob); */
1020 enet_rcv (dev, emac_isr);
1021 /* indicate that we serviced an interrupt */
1022 serviced = 1;
1023 rc = 0;
1024 }
1025 }
1026 mtdcr (uic0sr, UIC_MRE); /* Clear */
1027 mtdcr (uic1sr, UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
1028 switch (hw_p->devnum) {
1029 case 0:
1030 mtdcr (uic1sr, UIC_ETH0);
1031 break;
1032 case 1:
1033 mtdcr (uic1sr, UIC_ETH1);
1034 break;
Stefan Roeseb30f2a12005-08-08 12:42:22 +02001035#if defined (CONFIG_440GX)
wdenk544e9732004-02-06 23:19:44 +00001036 case 2:
1037 mtdcr (uic2sr, UIC_ETH2);
1038 break;
1039 case 3:
1040 mtdcr (uic2sr, UIC_ETH3);
1041 break;
Stefan Roeseb30f2a12005-08-08 12:42:22 +02001042#endif /* CONFIG_440GX */
wdenk544e9732004-02-06 23:19:44 +00001043 default:
1044 break;
1045 }
1046 } while (serviced);
1047
1048 return (rc);
1049}
1050
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001051#else /* CONFIG_440 */
1052
1053int enetInt (struct eth_device *dev)
1054{
1055 int serviced;
1056 int rc = -1; /* default to not us */
1057 unsigned long mal_isr;
1058 unsigned long emac_isr = 0;
1059 unsigned long mal_rx_eob;
1060 unsigned long my_uicmsr;
1061
1062 EMAC_4XX_HW_PST hw_p;
1063
1064 /*
1065 * Because the mal is generic, we need to get the current
1066 * eth device
1067 */
1068#if defined(CONFIG_NET_MULTI)
1069 dev = eth_get_dev();
1070#else
1071 dev = emac0_dev;
1072#endif
1073
1074 hw_p = dev->priv;
1075
1076 /* enter loop that stays in interrupt code until nothing to service */
1077 do {
1078 serviced = 0;
1079
1080 my_uicmsr = mfdcr (uicmsr);
1081
1082 if ((my_uicmsr & (MAL_UIC_DEF | EMAC_UIC_DEF)) == 0) { /* not for us */
1083 return (rc);
1084 }
1085 /* get and clear controller status interrupts */
1086 /* look at Mal and EMAC interrupts */
1087 if ((MAL_UIC_DEF & my_uicmsr) != 0) { /* we have a MAL interrupt */
1088 mal_isr = mfdcr (malesr);
1089 /* look for mal error */
1090 if ((my_uicmsr & MAL_UIC_ERR) != 0) {
1091 mal_err (dev, mal_isr, my_uicmsr, MAL_UIC_DEF, MAL_UIC_ERR);
1092 serviced = 1;
1093 rc = 0;
1094 }
1095 }
1096
1097 /* port by port dispatch of emac interrupts */
1098
1099 if ((SEL_UIC_DEF(hw_p->devnum) & my_uicmsr) != 0) { /* look for EMAC errors */
1100 emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
1101 if ((hw_p->emac_ier & emac_isr) != 0) {
1102 emac_err (dev, emac_isr);
1103 serviced = 1;
1104 rc = 0;
1105 }
1106 }
1107 if (((hw_p->emac_ier & emac_isr) != 0) || ((MAL_UIC_ERR & my_uicmsr) != 0)) {
1108 mtdcr (uicsr, MAL_UIC_DEF | SEL_UIC_DEF(hw_p->devnum)); /* Clear */
1109 return (rc); /* we had errors so get out */
1110 }
1111
1112 /* handle MAX TX EOB interrupt from a tx */
1113 if (my_uicmsr & UIC_MAL_TXEOB) {
1114 mal_rx_eob = mfdcr (maltxeobisr);
1115 mtdcr (maltxeobisr, mal_rx_eob);
1116 mtdcr (uicsr, UIC_MAL_TXEOB);
1117 }
1118 /* handle MAL RX EOB interupt from a receive */
1119 /* check for EOB on valid channels */
1120 if (my_uicmsr & UIC_MAL_RXEOB)
1121 {
1122 mal_rx_eob = mfdcr (malrxeobisr);
Wolfgang Denk0cbaf642005-09-25 00:53:22 +02001123 if ((mal_rx_eob & (0x80000000 >> hw_p->devnum)) != 0) { /* call emac routine for channel x */
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001124 /* clear EOB
1125 mtdcr(malrxeobisr, mal_rx_eob); */
1126 enet_rcv (dev, emac_isr);
1127 /* indicate that we serviced an interrupt */
1128 serviced = 1;
1129 rc = 0;
1130 }
1131 }
1132 mtdcr (uicsr, MAL_UIC_DEF|EMAC_UIC_DEF|EMAC_UIC_DEF1); /* Clear */
1133 }
1134 while (serviced);
1135
1136 return (rc);
1137}
1138
1139#endif /* CONFIG_440 */
1140
wdenk544e9732004-02-06 23:19:44 +00001141/*-----------------------------------------------------------------------------+
1142 * MAL Error Routine
1143 *-----------------------------------------------------------------------------*/
1144static void mal_err (struct eth_device *dev, unsigned long isr,
1145 unsigned long uic, unsigned long maldef,
1146 unsigned long mal_errr)
1147{
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001148 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenk544e9732004-02-06 23:19:44 +00001149
1150 mtdcr (malesr, isr); /* clear interrupt */
1151
1152 /* clear DE interrupt */
1153 mtdcr (maltxdeir, 0xC0000000);
1154 mtdcr (malrxdeir, 0x80000000);
1155
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001156#ifdef INFO_4XX_ENET
Wolfgang Denk0cbaf642005-09-25 00:53:22 +02001157 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 +00001158#endif
1159
1160 eth_init (hw_p->bis); /* start again... */
1161}
1162
1163/*-----------------------------------------------------------------------------+
1164 * EMAC Error Routine
1165 *-----------------------------------------------------------------------------*/
1166static void emac_err (struct eth_device *dev, unsigned long isr)
1167{
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001168 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenk544e9732004-02-06 23:19:44 +00001169
1170 printf ("EMAC%d error occured.... ISR = %lx\n", hw_p->devnum, isr);
1171 out32 (EMAC_ISR + hw_p->hw_addr, isr);
1172}
1173
1174/*-----------------------------------------------------------------------------+
1175 * enet_rcv() handles the ethernet receive data
1176 *-----------------------------------------------------------------------------*/
1177static void enet_rcv (struct eth_device *dev, unsigned long malisr)
1178{
1179 struct enet_frame *ef_ptr;
1180 unsigned long data_len;
1181 unsigned long rx_eob_isr;
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001182 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenk544e9732004-02-06 23:19:44 +00001183
1184 int handled = 0;
1185 int i;
1186 int loop_count = 0;
1187
1188 rx_eob_isr = mfdcr (malrxeobisr);
1189 if ((0x80000000 >> hw_p->devnum) & rx_eob_isr) {
1190 /* clear EOB */
1191 mtdcr (malrxeobisr, rx_eob_isr);
1192
1193 /* EMAC RX done */
1194 while (1) { /* do all */
1195 i = hw_p->rx_slot;
1196
1197 if ((MAL_RX_CTRL_EMPTY & hw_p->rx[i].ctrl)
1198 || (loop_count >= NUM_RX_BUFF))
1199 break;
1200 loop_count++;
1201 hw_p->rx_slot++;
1202 if (NUM_RX_BUFF == hw_p->rx_slot)
1203 hw_p->rx_slot = 0;
1204 handled++;
1205 data_len = (unsigned long) hw_p->rx[i].data_len; /* Get len */
1206 if (data_len) {
1207 if (data_len > ENET_MAX_MTU) /* Check len */
1208 data_len = 0;
1209 else {
1210 if (EMAC_RX_ERRORS & hw_p->rx[i].ctrl) { /* Check Errors */
1211 data_len = 0;
1212 hw_p->stats.rx_err_log[hw_p->
1213 rx_err_index]
1214 = hw_p->rx[i].ctrl;
1215 hw_p->rx_err_index++;
1216 if (hw_p->rx_err_index ==
1217 MAX_ERR_LOG)
1218 hw_p->rx_err_index =
1219 0;
wdenk7ad5e4c2004-04-25 15:41:35 +00001220 } /* emac_erros */
wdenk544e9732004-02-06 23:19:44 +00001221 } /* data_len < max mtu */
wdenk7ad5e4c2004-04-25 15:41:35 +00001222 } /* if data_len */
wdenk544e9732004-02-06 23:19:44 +00001223 if (!data_len) { /* no data */
1224 hw_p->rx[i].ctrl |= MAL_RX_CTRL_EMPTY; /* Free Recv Buffer */
1225
1226 hw_p->stats.data_len_err++; /* Error at Rx */
1227 }
1228
1229 /* !data_len */
1230 /* AS.HARNOIS */
1231 /* Check if user has already eaten buffer */
1232 /* if not => ERROR */
1233 else if (hw_p->rx_ready[hw_p->rx_i_index] != -1) {
1234 if (hw_p->is_receiving)
1235 printf ("ERROR : Receive buffers are full!\n");
1236 break;
1237 } else {
1238 hw_p->stats.rx_frames++;
1239 hw_p->stats.rx += data_len;
1240 ef_ptr = (struct enet_frame *) hw_p->rx[i].
1241 data_ptr;
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001242#ifdef INFO_4XX_ENET
wdenk544e9732004-02-06 23:19:44 +00001243 hw_p->stats.pkts_rx++;
1244#endif
1245 /* AS.HARNOIS
1246 * use ring buffer
1247 */
1248 hw_p->rx_ready[hw_p->rx_i_index] = i;
1249 hw_p->rx_i_index++;
1250 if (NUM_RX_BUFF == hw_p->rx_i_index)
1251 hw_p->rx_i_index = 0;
1252
wdenk544e9732004-02-06 23:19:44 +00001253 /* AS.HARNOIS
1254 * free receive buffer only when
1255 * buffer has been handled (eth_rx)
1256 rx[i].ctrl |= MAL_RX_CTRL_EMPTY;
1257 */
1258 } /* if data_len */
1259 } /* while */
1260 } /* if EMACK_RXCHL */
1261}
1262
1263
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001264static int ppc_4xx_eth_rx (struct eth_device *dev)
wdenk544e9732004-02-06 23:19:44 +00001265{
1266 int length;
1267 int user_index;
1268 unsigned long msr;
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001269 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenk544e9732004-02-06 23:19:44 +00001270
Wolfgang Denk0cbaf642005-09-25 00:53:22 +02001271 hw_p->is_receiving = 1; /* tell driver */
wdenk544e9732004-02-06 23:19:44 +00001272
1273 for (;;) {
1274 /* AS.HARNOIS
1275 * use ring buffer and
1276 * get index from rx buffer desciptor queue
1277 */
1278 user_index = hw_p->rx_ready[hw_p->rx_u_index];
1279 if (user_index == -1) {
1280 length = -1;
1281 break; /* nothing received - leave for() loop */
1282 }
1283
1284 msr = mfmsr ();
1285 mtmsr (msr & ~(MSR_EE));
1286
1287 length = hw_p->rx[user_index].data_len;
1288
1289 /* Pass the packet up to the protocol layers. */
Wolfgang Denk0cbaf642005-09-25 00:53:22 +02001290 /* NetReceive(NetRxPackets[rxIdx], length - 4); */
1291 /* NetReceive(NetRxPackets[i], length); */
wdenk544e9732004-02-06 23:19:44 +00001292 NetReceive (NetRxPackets[user_index], length - 4);
1293 /* Free Recv Buffer */
1294 hw_p->rx[user_index].ctrl |= MAL_RX_CTRL_EMPTY;
1295 /* Free rx buffer descriptor queue */
1296 hw_p->rx_ready[hw_p->rx_u_index] = -1;
1297 hw_p->rx_u_index++;
1298 if (NUM_RX_BUFF == hw_p->rx_u_index)
1299 hw_p->rx_u_index = 0;
1300
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001301#ifdef INFO_4XX_ENET
wdenk544e9732004-02-06 23:19:44 +00001302 hw_p->stats.pkts_handled++;
1303#endif
1304
1305 mtmsr (msr); /* Enable IRQ's */
1306 }
1307
Wolfgang Denk0cbaf642005-09-25 00:53:22 +02001308 hw_p->is_receiving = 0; /* tell driver */
wdenk544e9732004-02-06 23:19:44 +00001309
1310 return length;
1311}
1312
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001313int ppc_4xx_eth_initialize (bd_t * bis)
wdenk544e9732004-02-06 23:19:44 +00001314{
1315 static int virgin = 0;
wdenk544e9732004-02-06 23:19:44 +00001316 struct eth_device *dev;
1317 int eth_num = 0;
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001318 EMAC_4XX_HW_PST hw = NULL;
wdenk544e9732004-02-06 23:19:44 +00001319
Stefan Roeseb30f2a12005-08-08 12:42:22 +02001320#if defined(CONFIG_440GX)
Stefan Roese326c9712005-08-01 16:41:48 +02001321 unsigned long pfc1;
1322
wdenk544e9732004-02-06 23:19:44 +00001323 mfsdr (sdr_pfc1, pfc1);
1324 pfc1 &= ~(0x01e00000);
1325 pfc1 |= 0x01200000;
1326 mtsdr (sdr_pfc1, pfc1);
Stefan Roese326c9712005-08-01 16:41:48 +02001327#endif
wdenk56ed43e2004-02-22 23:46:08 +00001328 /* set phy num and mode */
1329 bis->bi_phynum[0] = CONFIG_PHY_ADDR;
Stefan Roese326c9712005-08-01 16:41:48 +02001330#if defined(CONFIG_PHY1_ADDR)
wdenk56ed43e2004-02-22 23:46:08 +00001331 bis->bi_phynum[1] = CONFIG_PHY1_ADDR;
Stefan Roese326c9712005-08-01 16:41:48 +02001332#endif
Stefan Roeseb30f2a12005-08-08 12:42:22 +02001333#if defined(CONFIG_440GX)
wdenk56ed43e2004-02-22 23:46:08 +00001334 bis->bi_phynum[2] = CONFIG_PHY2_ADDR;
1335 bis->bi_phynum[3] = CONFIG_PHY3_ADDR;
1336 bis->bi_phymode[0] = 0;
1337 bis->bi_phymode[1] = 0;
1338 bis->bi_phymode[2] = 2;
1339 bis->bi_phymode[3] = 2;
wdenk544e9732004-02-06 23:19:44 +00001340
Stefan Roeseb30f2a12005-08-08 12:42:22 +02001341#if defined (CONFIG_440GX)
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001342 ppc_4xx_eth_setup_bridge(0, bis);
wdenk97e8bda2004-09-29 22:43:59 +00001343#endif
Stefan Roese326c9712005-08-01 16:41:48 +02001344#endif
wdenk97e8bda2004-09-29 22:43:59 +00001345
wdenk544e9732004-02-06 23:19:44 +00001346 for (eth_num = 0; eth_num < EMAC_NUM_DEV; eth_num++) {
1347
1348 /* See if we can actually bring up the interface, otherwise, skip it */
1349 switch (eth_num) {
wdenk54070ab2004-12-31 09:32:47 +00001350 default: /* fall through */
wdenk544e9732004-02-06 23:19:44 +00001351 case 0:
wdenk56ed43e2004-02-22 23:46:08 +00001352 if (memcmp (bis->bi_enetaddr, "\0\0\0\0\0\0", 6) == 0) {
1353 bis->bi_phymode[eth_num] = BI_PHYMODE_NONE;
wdenk544e9732004-02-06 23:19:44 +00001354 continue;
wdenk56ed43e2004-02-22 23:46:08 +00001355 }
wdenk544e9732004-02-06 23:19:44 +00001356 break;
wdenk54070ab2004-12-31 09:32:47 +00001357#ifdef CONFIG_HAS_ETH1
wdenk544e9732004-02-06 23:19:44 +00001358 case 1:
wdenk56ed43e2004-02-22 23:46:08 +00001359 if (memcmp (bis->bi_enet1addr, "\0\0\0\0\0\0", 6) == 0) {
1360 bis->bi_phymode[eth_num] = BI_PHYMODE_NONE;
wdenk544e9732004-02-06 23:19:44 +00001361 continue;
wdenk56ed43e2004-02-22 23:46:08 +00001362 }
wdenk544e9732004-02-06 23:19:44 +00001363 break;
wdenk54070ab2004-12-31 09:32:47 +00001364#endif
1365#ifdef CONFIG_HAS_ETH2
wdenk544e9732004-02-06 23:19:44 +00001366 case 2:
wdenk56ed43e2004-02-22 23:46:08 +00001367 if (memcmp (bis->bi_enet2addr, "\0\0\0\0\0\0", 6) == 0) {
1368 bis->bi_phymode[eth_num] = BI_PHYMODE_NONE;
wdenk544e9732004-02-06 23:19:44 +00001369 continue;
wdenk56ed43e2004-02-22 23:46:08 +00001370 }
wdenk544e9732004-02-06 23:19:44 +00001371 break;
wdenk54070ab2004-12-31 09:32:47 +00001372#endif
1373#ifdef CONFIG_HAS_ETH3
wdenk544e9732004-02-06 23:19:44 +00001374 case 3:
wdenk56ed43e2004-02-22 23:46:08 +00001375 if (memcmp (bis->bi_enet3addr, "\0\0\0\0\0\0", 6) == 0) {
1376 bis->bi_phymode[eth_num] = BI_PHYMODE_NONE;
wdenk544e9732004-02-06 23:19:44 +00001377 continue;
wdenk56ed43e2004-02-22 23:46:08 +00001378 }
wdenk544e9732004-02-06 23:19:44 +00001379 break;
wdenk54070ab2004-12-31 09:32:47 +00001380#endif
wdenk544e9732004-02-06 23:19:44 +00001381 }
1382
1383 /* Allocate device structure */
1384 dev = (struct eth_device *) malloc (sizeof (*dev));
1385 if (dev == NULL) {
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001386 printf ("ppc_4xx_eth_initialize: "
wdenkef893942004-02-23 16:11:30 +00001387 "Cannot allocate eth_device %d\n", eth_num);
wdenk544e9732004-02-06 23:19:44 +00001388 return (-1);
1389 }
wdenkd1894de2005-06-20 10:17:34 +00001390 memset(dev, 0, sizeof(*dev));
wdenk544e9732004-02-06 23:19:44 +00001391
1392 /* Allocate our private use data */
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001393 hw = (EMAC_4XX_HW_PST) malloc (sizeof (*hw));
wdenk544e9732004-02-06 23:19:44 +00001394 if (hw == NULL) {
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001395 printf ("ppc_4xx_eth_initialize: "
wdenkef893942004-02-23 16:11:30 +00001396 "Cannot allocate private hw data for eth_device %d",
wdenk544e9732004-02-06 23:19:44 +00001397 eth_num);
1398 free (dev);
1399 return (-1);
1400 }
wdenkd1894de2005-06-20 10:17:34 +00001401 memset(hw, 0, sizeof(*hw));
wdenk544e9732004-02-06 23:19:44 +00001402
1403 switch (eth_num) {
wdenk54070ab2004-12-31 09:32:47 +00001404 default: /* fall through */
wdenk544e9732004-02-06 23:19:44 +00001405 case 0:
1406 hw->hw_addr = 0;
1407 memcpy (dev->enetaddr, bis->bi_enetaddr, 6);
1408 break;
wdenk54070ab2004-12-31 09:32:47 +00001409#ifdef CONFIG_HAS_ETH1
wdenk544e9732004-02-06 23:19:44 +00001410 case 1:
1411 hw->hw_addr = 0x100;
1412 memcpy (dev->enetaddr, bis->bi_enet1addr, 6);
1413 break;
wdenk54070ab2004-12-31 09:32:47 +00001414#endif
1415#ifdef CONFIG_HAS_ETH2
wdenk544e9732004-02-06 23:19:44 +00001416 case 2:
1417 hw->hw_addr = 0x400;
1418 memcpy (dev->enetaddr, bis->bi_enet2addr, 6);
1419 break;
wdenk54070ab2004-12-31 09:32:47 +00001420#endif
1421#ifdef CONFIG_HAS_ETH3
wdenk544e9732004-02-06 23:19:44 +00001422 case 3:
1423 hw->hw_addr = 0x600;
1424 memcpy (dev->enetaddr, bis->bi_enet3addr, 6);
1425 break;
wdenk54070ab2004-12-31 09:32:47 +00001426#endif
wdenk544e9732004-02-06 23:19:44 +00001427 }
1428
1429 hw->devnum = eth_num;
Stefan Roese326c9712005-08-01 16:41:48 +02001430 hw->print_speed = 1;
wdenk544e9732004-02-06 23:19:44 +00001431
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001432 sprintf (dev->name, "ppc_4xx_eth%d", eth_num);
wdenk544e9732004-02-06 23:19:44 +00001433 dev->priv = (void *) hw;
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001434 dev->init = ppc_4xx_eth_init;
1435 dev->halt = ppc_4xx_eth_halt;
1436 dev->send = ppc_4xx_eth_send;
1437 dev->recv = ppc_4xx_eth_rx;
wdenk544e9732004-02-06 23:19:44 +00001438
1439 if (0 == virgin) {
1440 /* set the MAL IER ??? names may change with new spec ??? */
1441 mal_ier =
1442 MAL_IER_DE | MAL_IER_NE | MAL_IER_TE |
1443 MAL_IER_OPBE | MAL_IER_PLBE;
1444 mtdcr (malesr, 0xffffffff); /* clear pending interrupts */
1445 mtdcr (maltxdeir, 0xffffffff); /* clear pending interrupts */
1446 mtdcr (malrxdeir, 0xffffffff); /* clear pending interrupts */
1447 mtdcr (malier, mal_ier);
1448
1449 /* install MAL interrupt handler */
1450 irq_install_handler (VECNUM_MS,
1451 (interrupt_handler_t *) enetInt,
1452 dev);
1453 irq_install_handler (VECNUM_MTE,
1454 (interrupt_handler_t *) enetInt,
1455 dev);
1456 irq_install_handler (VECNUM_MRE,
1457 (interrupt_handler_t *) enetInt,
1458 dev);
1459 irq_install_handler (VECNUM_TXDE,
1460 (interrupt_handler_t *) enetInt,
1461 dev);
1462 irq_install_handler (VECNUM_RXDE,
1463 (interrupt_handler_t *) enetInt,
1464 dev);
1465 virgin = 1;
1466 }
1467
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001468#if defined(CONFIG_NET_MULTI)
wdenk544e9732004-02-06 23:19:44 +00001469 eth_register (dev);
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001470#else
1471 emac0_dev = dev;
1472#endif
wdenk544e9732004-02-06 23:19:44 +00001473
1474 } /* end for each supported device */
1475 return (1);
1476}
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001477
1478
1479#if !defined(CONFIG_NET_MULTI)
1480void eth_halt (void) {
1481 if (emac0_dev) {
1482 ppc_4xx_eth_halt(emac0_dev);
1483 free(emac0_dev);
1484 emac0_dev = NULL;
1485 }
1486}
1487
1488int eth_init (bd_t *bis)
1489{
1490 ppc_4xx_eth_initialize(bis);
Stefan Roese03510612005-10-10 17:43:58 +02001491 if (emac0_dev) {
1492 return ppc_4xx_eth_init(emac0_dev, bis);
1493 } else {
1494 printf("ERROR: ethaddr not set!\n");
1495 return -1;
1496 }
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001497}
1498
1499int eth_send(volatile void *packet, int length)
1500{
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001501 return (ppc_4xx_eth_send(emac0_dev, packet, length));
1502}
1503
1504int eth_rx(void)
1505{
1506 return (ppc_4xx_eth_rx(emac0_dev));
1507}
1508#endif /* !defined(CONFIG_NET_MULTI) */
1509
1510#endif /* #if (CONFIG_COMMANDS & CFG_CMD_NET) */