blob: 859b72df4fe91597614198518bee5ad1ef3b6e1b [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 Roesec8136d02005-10-18 19:17:12 +0200189#ifndef CONFIG_NETCONSOLE
Stefan Roese326c9712005-08-01 16:41:48 +0200190 hw_p->print_speed = 1; /* print speed message again next time */
Stefan Roesec8136d02005-10-18 19:17:12 +0200191#endif
Stefan Roese326c9712005-08-01 16:41:48 +0200192
wdenk544e9732004-02-06 23:19:44 +0000193 return;
194}
195
196extern int phy_setup_aneg (unsigned char addr);
197extern int miiphy_reset (unsigned char addr);
198
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200199#if defined (CONFIG_440GX)
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200200int ppc_4xx_eth_setup_bridge(int devnum, bd_t * bis)
wdenked2ac4b2004-03-14 18:23:55 +0000201{
202 unsigned long pfc1;
203 unsigned long zmiifer;
204 unsigned long rmiifer;
205
206 mfsdr(sdr_pfc1, pfc1);
207 pfc1 = SDR0_PFC1_EPS_DECODE(pfc1);
208
209 zmiifer = 0;
210 rmiifer = 0;
211
212 switch (pfc1) {
213 case 1:
214 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
215 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(1);
216 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(2);
217 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(3);
218 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
219 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
220 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
221 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
222 break;
223 case 2:
224 zmiifer = ZMII_FER_SMII << ZMII_FER_V(0);
225 zmiifer = ZMII_FER_SMII << ZMII_FER_V(1);
226 zmiifer = ZMII_FER_SMII << ZMII_FER_V(2);
227 zmiifer = ZMII_FER_SMII << ZMII_FER_V(3);
228 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
229 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
230 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
231 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
232 break;
233 case 3:
234 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
235 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
236 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
237 bis->bi_phymode[1] = BI_PHYMODE_NONE;
238 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
239 bis->bi_phymode[3] = BI_PHYMODE_NONE;
240 break;
241 case 4:
242 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(0);
243 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(1);
244 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V (2);
245 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V (3);
246 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
247 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
248 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
249 bis->bi_phymode[3] = BI_PHYMODE_RGMII;
250 break;
251 case 5:
252 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (0);
253 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (1);
254 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (2);
255 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(3);
256 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
257 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
258 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
259 bis->bi_phymode[3] = BI_PHYMODE_RGMII;
260 break;
261 case 6:
262 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (0);
263 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (1);
264 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
wdenked2ac4b2004-03-14 18:23:55 +0000265 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
266 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
267 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
wdenked2ac4b2004-03-14 18:23:55 +0000268 break;
269 case 0:
270 default:
271 zmiifer = ZMII_FER_MII << ZMII_FER_V(devnum);
272 rmiifer = 0x0;
273 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
274 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
275 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
276 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
277 break;
278 }
279
280 /* Ensure we setup mdio for this devnum and ONLY this devnum */
281 zmiifer |= (ZMII_FER_MDI) << ZMII_FER_V(devnum);
282
283 out32 (ZMII_FER, zmiifer);
284 out32 (RGMII_FER, rmiifer);
285
286 return ((int)pfc1);
287
288}
289#endif
290
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200291static int ppc_4xx_eth_init (struct eth_device *dev, bd_t * bis)
wdenk544e9732004-02-06 23:19:44 +0000292{
Stefan Roese5ff4c3f2005-08-15 12:31:23 +0200293 int i, j;
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200294 unsigned long reg = 0;
wdenk544e9732004-02-06 23:19:44 +0000295 unsigned long msr;
296 unsigned long speed;
297 unsigned long duplex;
298 unsigned long failsafe;
299 unsigned mode_reg;
300 unsigned short devnum;
301 unsigned short reg_short;
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200302#if defined(CONFIG_440GX)
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200303 sys_info_t sysinfo;
wdenked2ac4b2004-03-14 18:23:55 +0000304 int ethgroup;
Stefan Roese326c9712005-08-01 16:41:48 +0200305#endif
wdenk544e9732004-02-06 23:19:44 +0000306
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200307 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenk544e9732004-02-06 23:19:44 +0000308
309 /* before doing anything, figure out if we have a MAC address */
310 /* if not, bail */
Stefan Roese03510612005-10-10 17:43:58 +0200311 if (memcmp (dev->enetaddr, "\0\0\0\0\0\0", 6) == 0) {
312 printf("ERROR: ethaddr not set!\n");
wdenk544e9732004-02-06 23:19:44 +0000313 return -1;
Stefan Roese03510612005-10-10 17:43:58 +0200314 }
wdenk544e9732004-02-06 23:19:44 +0000315
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200316#if defined(CONFIG_440GX)
wdenk544e9732004-02-06 23:19:44 +0000317 /* Need to get the OPB frequency so we can access the PHY */
318 get_sys_info (&sysinfo);
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200319#endif
wdenk544e9732004-02-06 23:19:44 +0000320
wdenk544e9732004-02-06 23:19:44 +0000321 msr = mfmsr ();
322 mtmsr (msr & ~(MSR_EE)); /* disable interrupts */
323
324 devnum = hw_p->devnum;
325
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200326#ifdef INFO_4XX_ENET
wdenk544e9732004-02-06 23:19:44 +0000327 /* AS.HARNOIS
328 * We should have :
Wolfgang Denk0cbaf642005-09-25 00:53:22 +0200329 * hw_p->stats.pkts_handled <= hw_p->stats.pkts_rx <= hw_p->stats.pkts_handled+PKTBUFSRX
wdenk544e9732004-02-06 23:19:44 +0000330 * In the most cases hw_p->stats.pkts_handled = hw_p->stats.pkts_rx, but it
331 * is possible that new packets (without relationship with
332 * current transfer) have got the time to arrived before
333 * netloop calls eth_halt
334 */
335 printf ("About preceeding transfer (eth%d):\n"
336 "- Sent packet number %d\n"
337 "- Received packet number %d\n"
338 "- Handled packet number %d\n",
339 hw_p->devnum,
340 hw_p->stats.pkts_tx,
341 hw_p->stats.pkts_rx, hw_p->stats.pkts_handled);
342
343 hw_p->stats.pkts_tx = 0;
344 hw_p->stats.pkts_rx = 0;
345 hw_p->stats.pkts_handled = 0;
346#endif
347
Wolfgang Denk0cbaf642005-09-25 00:53:22 +0200348 hw_p->tx_err_index = 0; /* Transmit Error Index for tx_err_log */
349 hw_p->rx_err_index = 0; /* Receive Error Index for rx_err_log */
wdenk544e9732004-02-06 23:19:44 +0000350
351 hw_p->rx_slot = 0; /* MAL Receive Slot */
352 hw_p->rx_i_index = 0; /* Receive Interrupt Queue Index */
353 hw_p->rx_u_index = 0; /* Receive User Queue Index */
354
355 hw_p->tx_slot = 0; /* MAL Transmit Slot */
356 hw_p->tx_i_index = 0; /* Transmit Interrupt Queue Index */
357 hw_p->tx_u_index = 0; /* Transmit User Queue Index */
358
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200359#if defined(CONFIG_440)
wdenk544e9732004-02-06 23:19:44 +0000360 /* set RMII mode */
361 /* NOTE: 440GX spec states that mode is mutually exclusive */
362 /* NOTE: Therefore, disable all other EMACS, since we handle */
363 /* NOTE: only one emac at a time */
364 reg = 0;
365 out32 (ZMII_FER, 0);
366 udelay (100);
wdenk544e9732004-02-06 23:19:44 +0000367
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200368#if defined(CONFIG_440EP) || defined(CONFIG_440GR)
Wolfgang Denk0cbaf642005-09-25 00:53:22 +0200369 out32 (ZMII_FER, (ZMII_FER_RMII | ZMII_FER_MDI) << ZMII_FER_V (devnum));
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200370#elif defined(CONFIG_440GX)
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200371 ethgroup = ppc_4xx_eth_setup_bridge(devnum, bis);
Stefan Roese74309032005-09-07 16:21:12 +0200372#elif defined(CONFIG_440GP)
373 /* set RMII mode */
374 out32 (ZMII_FER, ZMII_RMII | ZMII_MDI0);
wdenk00fe1612004-03-14 00:07:33 +0000375#else
376 if ((devnum == 0) || (devnum == 1)) {
377 out32 (ZMII_FER, (ZMII_FER_SMII | ZMII_FER_MDI) << ZMII_FER_V (devnum));
378 }
379 else { /* ((devnum == 2) || (devnum == 3)) */
380 out32 (ZMII_FER, ZMII_FER_MDI << ZMII_FER_V (devnum));
wdenk544e9732004-02-06 23:19:44 +0000381 out32 (RGMII_FER, ((RGMII_FER_RGMII << RGMII_FER_V (2)) |
382 (RGMII_FER_RGMII << RGMII_FER_V (3))));
wdenk00fe1612004-03-14 00:07:33 +0000383 }
384#endif
Stefan Roese797d8572005-08-11 17:56:56 +0200385
wdenk00fe1612004-03-14 00:07:33 +0000386 out32 (ZMII_SSR, ZMII_SSR_SP << ZMII_SSR_V(devnum));
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200387#endif /* defined(CONFIG_440) */
388
wdenk00fe1612004-03-14 00:07:33 +0000389 __asm__ volatile ("eieio");
390
391 /* reset emac so we have access to the phy */
392
393 out32 (EMAC_M0 + hw_p->hw_addr, EMAC_M0_SRST);
wdenk544e9732004-02-06 23:19:44 +0000394 __asm__ volatile ("eieio");
395
396 failsafe = 1000;
397 while ((in32 (EMAC_M0 + hw_p->hw_addr) & (EMAC_M0_SRST)) && failsafe) {
398 udelay (1000);
399 failsafe--;
400 }
401
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200402#if defined(CONFIG_440GX)
wdenk544e9732004-02-06 23:19:44 +0000403 /* Whack the M1 register */
404 mode_reg = 0x0;
405 mode_reg &= ~0x00000038;
406 if (sysinfo.freqOPB <= 50000000);
407 else if (sysinfo.freqOPB <= 66666667)
408 mode_reg |= EMAC_M1_OBCI_66;
409 else if (sysinfo.freqOPB <= 83333333)
410 mode_reg |= EMAC_M1_OBCI_83;
411 else if (sysinfo.freqOPB <= 100000000)
412 mode_reg |= EMAC_M1_OBCI_100;
413 else
414 mode_reg |= EMAC_M1_OBCI_GT100;
415
416 out32 (EMAC_M1 + hw_p->hw_addr, mode_reg);
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200417#endif /* defined(CONFIG_440GX) */
wdenk544e9732004-02-06 23:19:44 +0000418
419 /* wait for PHY to complete auto negotiation */
420 reg_short = 0;
421#ifndef CONFIG_CS8952_PHY
422 switch (devnum) {
423 case 0:
424 reg = CONFIG_PHY_ADDR;
425 break;
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200426#if defined (CONFIG_PHY1_ADDR)
wdenk544e9732004-02-06 23:19:44 +0000427 case 1:
428 reg = CONFIG_PHY1_ADDR;
429 break;
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200430#endif
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200431#if defined (CONFIG_440GX)
wdenk544e9732004-02-06 23:19:44 +0000432 case 2:
433 reg = CONFIG_PHY2_ADDR;
434 break;
435 case 3:
436 reg = CONFIG_PHY3_ADDR;
437 break;
438#endif
439 default:
440 reg = CONFIG_PHY_ADDR;
441 break;
442 }
443
wdenk56ed43e2004-02-22 23:46:08 +0000444 bis->bi_phynum[devnum] = reg;
445
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200446#if defined(CONFIG_PHY_RESET)
wdenk97e8bda2004-09-29 22:43:59 +0000447 /*
448 * Reset the phy, only if its the first time through
449 * otherwise, just check the speeds & feeds
450 */
451 if (hw_p->first_init == 0) {
452 miiphy_reset (reg);
wdenk544e9732004-02-06 23:19:44 +0000453
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200454#if defined(CONFIG_440GX)
wdenk00fe1612004-03-14 00:07:33 +0000455#if defined(CONFIG_CIS8201_PHY)
wdenk7ad5e4c2004-04-25 15:41:35 +0000456 /*
Stefan Roese363330b2005-08-04 17:09:16 +0200457 * Cicada 8201 PHY needs to have an extended register whacked
458 * for RGMII mode.
wdenk7ad5e4c2004-04-25 15:41:35 +0000459 */
Stefan Roese363330b2005-08-04 17:09:16 +0200460 if ( ((devnum == 2) || (devnum ==3)) && (4 == ethgroup) ) {
Stefan Roese5ff4c3f2005-08-15 12:31:23 +0200461#if defined(CONFIG_CIS8201_SHORT_ETCH)
462 miiphy_write (reg, 23, 0x1300);
463#else
464 miiphy_write (reg, 23, 0x1000);
465#endif
Stefan Roese363330b2005-08-04 17:09:16 +0200466 /*
467 * Vitesse VSC8201/Cicada CIS8201 errata:
468 * Interoperability problem with Intel 82547EI phys
469 * This work around (provided by Vitesse) changes
470 * the default timer convergence from 8ms to 12ms
471 */
472 miiphy_write (reg, 0x1f, 0x2a30);
473 miiphy_write (reg, 0x08, 0x0200);
474 miiphy_write (reg, 0x1f, 0x52b5);
475 miiphy_write (reg, 0x02, 0x0004);
476 miiphy_write (reg, 0x01, 0x0671);
477 miiphy_write (reg, 0x00, 0x8fae);
478 miiphy_write (reg, 0x1f, 0x2a30);
479 miiphy_write (reg, 0x08, 0x0000);
480 miiphy_write (reg, 0x1f, 0x0000);
481 /* end Vitesse/Cicada errata */
482 }
wdenk00fe1612004-03-14 00:07:33 +0000483#endif
wdenked2ac4b2004-03-14 18:23:55 +0000484#endif
wdenk97e8bda2004-09-29 22:43:59 +0000485 /* Start/Restart autonegotiation */
486 phy_setup_aneg (reg);
487 udelay (1000);
488 }
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200489#endif /* defined(CONFIG_PHY_RESET) */
wdenk544e9732004-02-06 23:19:44 +0000490
491 miiphy_read (reg, PHY_BMSR, &reg_short);
492
493 /*
wdenk00fe1612004-03-14 00:07:33 +0000494 * Wait if PHY is capable of autonegotiation and autonegotiation is not complete
wdenk544e9732004-02-06 23:19:44 +0000495 */
496 if ((reg_short & PHY_BMSR_AUTN_ABLE)
497 && !(reg_short & PHY_BMSR_AUTN_COMP)) {
498 puts ("Waiting for PHY auto negotiation to complete");
499 i = 0;
500 while (!(reg_short & PHY_BMSR_AUTN_COMP)) {
501 /*
502 * Timeout reached ?
503 */
504 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
505 puts (" TIMEOUT !\n");
506 break;
507 }
508
509 if ((i++ % 1000) == 0) {
510 putc ('.');
511 }
512 udelay (1000); /* 1 ms */
513 miiphy_read (reg, PHY_BMSR, &reg_short);
514
515 }
516 puts (" done\n");
517 udelay (500000); /* another 500 ms (results in faster booting) */
518 }
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200519#endif /* #ifndef CONFIG_CS8952_PHY */
520
wdenk544e9732004-02-06 23:19:44 +0000521 speed = miiphy_speed (reg);
522 duplex = miiphy_duplex (reg);
523
524 if (hw_p->print_speed) {
525 hw_p->print_speed = 0;
526 printf ("ENET Speed is %d Mbps - %s duplex connection\n",
527 (int) speed, (duplex == HALF) ? "HALF" : "FULL");
528 }
529
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200530#if defined(CONFIG_440)
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200531#if defined(CONFIG_440EP) || defined(CONFIG_440GR)
Stefan Roese326c9712005-08-01 16:41:48 +0200532 mfsdr(sdr_mfr, reg);
533 if (speed == 100) {
534 reg = (reg & ~SDR0_MFR_ZMII_MODE_MASK) | SDR0_MFR_ZMII_MODE_RMII_100M;
535 } else {
536 reg = (reg & ~SDR0_MFR_ZMII_MODE_MASK) | SDR0_MFR_ZMII_MODE_RMII_10M;
537 }
538 mtsdr(sdr_mfr, reg);
539#endif
Stefan Roese797d8572005-08-11 17:56:56 +0200540
wdenk544e9732004-02-06 23:19:44 +0000541 /* Set ZMII/RGMII speed according to the phy link speed */
542 reg = in32 (ZMII_SSR);
wdenked2ac4b2004-03-14 18:23:55 +0000543 if ( (speed == 100) || (speed == 1000) )
wdenk544e9732004-02-06 23:19:44 +0000544 out32 (ZMII_SSR, reg | (ZMII_SSR_SP << ZMII_SSR_V (devnum)));
545 else
Stefan Roese797d8572005-08-11 17:56:56 +0200546 out32 (ZMII_SSR, reg & (~(ZMII_SSR_SP << ZMII_SSR_V (devnum))));
wdenk544e9732004-02-06 23:19:44 +0000547
548 if ((devnum == 2) || (devnum == 3)) {
549 if (speed == 1000)
550 reg = (RGMII_SSR_SP_1000MBPS << RGMII_SSR_V (devnum));
551 else if (speed == 100)
552 reg = (RGMII_SSR_SP_100MBPS << RGMII_SSR_V (devnum));
553 else
554 reg = (RGMII_SSR_SP_10MBPS << RGMII_SSR_V (devnum));
555
556 out32 (RGMII_SSR, reg);
557 }
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200558#endif /* defined(CONFIG_440) */
wdenk544e9732004-02-06 23:19:44 +0000559
560 /* set the Mal configuration reg */
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200561#if defined(CONFIG_440GX)
Stefan Roese363330b2005-08-04 17:09:16 +0200562 mtdcr (malmcr, MAL_CR_PLBB | MAL_CR_OPBBL | MAL_CR_LEA |
563 MAL_CR_PLBLT_DEFAULT | MAL_CR_EOPIE | 0x00330000);
564#else
565 mtdcr (malmcr, MAL_CR_PLBB | MAL_CR_OPBBL | MAL_CR_LEA | MAL_CR_PLBLT_DEFAULT);
wdenk544e9732004-02-06 23:19:44 +0000566 /* Errata 1.12: MAL_1 -- Disable MAL bursting */
Stefan Roese363330b2005-08-04 17:09:16 +0200567 if (get_pvr() == PVR_440GP_RB) {
568 mtdcr (malmcr, mfdcr(malmcr) & ~MAL_CR_PLBB);
569 }
570#endif
wdenk544e9732004-02-06 23:19:44 +0000571
572 /* Free "old" buffers */
573 if (hw_p->alloc_tx_buf)
574 free (hw_p->alloc_tx_buf);
575 if (hw_p->alloc_rx_buf)
576 free (hw_p->alloc_rx_buf);
577
578 /*
579 * Malloc MAL buffer desciptors, make sure they are
580 * aligned on cache line boundary size
581 * (401/403/IOP480 = 16, 405 = 32)
582 * and doesn't cross cache block boundaries.
583 */
584 hw_p->alloc_tx_buf =
585 (mal_desc_t *) malloc ((sizeof (mal_desc_t) * NUM_TX_BUFF) +
586 ((2 * CFG_CACHELINE_SIZE) - 2));
Stefan Roese5ff4c3f2005-08-15 12:31:23 +0200587 if (NULL == hw_p->alloc_tx_buf)
588 return -1;
wdenk544e9732004-02-06 23:19:44 +0000589 if (((int) hw_p->alloc_tx_buf & CACHELINE_MASK) != 0) {
590 hw_p->tx =
591 (mal_desc_t *) ((int) hw_p->alloc_tx_buf +
592 CFG_CACHELINE_SIZE -
593 ((int) hw_p->
594 alloc_tx_buf & CACHELINE_MASK));
595 } else {
596 hw_p->tx = hw_p->alloc_tx_buf;
597 }
598
599 hw_p->alloc_rx_buf =
600 (mal_desc_t *) malloc ((sizeof (mal_desc_t) * NUM_RX_BUFF) +
601 ((2 * CFG_CACHELINE_SIZE) - 2));
Stefan Roese5ff4c3f2005-08-15 12:31:23 +0200602 if (NULL == hw_p->alloc_rx_buf) {
603 free(hw_p->alloc_tx_buf);
604 hw_p->alloc_tx_buf = NULL;
605 return -1;
606 }
607
wdenk544e9732004-02-06 23:19:44 +0000608 if (((int) hw_p->alloc_rx_buf & CACHELINE_MASK) != 0) {
609 hw_p->rx =
610 (mal_desc_t *) ((int) hw_p->alloc_rx_buf +
611 CFG_CACHELINE_SIZE -
612 ((int) hw_p->
613 alloc_rx_buf & CACHELINE_MASK));
614 } else {
615 hw_p->rx = hw_p->alloc_rx_buf;
616 }
617
618 for (i = 0; i < NUM_TX_BUFF; i++) {
619 hw_p->tx[i].ctrl = 0;
620 hw_p->tx[i].data_len = 0;
Stefan Roese5ff4c3f2005-08-15 12:31:23 +0200621 if (hw_p->first_init == 0) {
wdenk544e9732004-02-06 23:19:44 +0000622 hw_p->txbuf_ptr =
623 (char *) malloc (ENET_MAX_MTU_ALIGNED);
Stefan Roese5ff4c3f2005-08-15 12:31:23 +0200624 if (NULL == hw_p->txbuf_ptr) {
625 free(hw_p->alloc_rx_buf);
626 free(hw_p->alloc_tx_buf);
627 hw_p->alloc_rx_buf = NULL;
628 hw_p->alloc_tx_buf = NULL;
629 for(j = 0; j < i; j++) {
630 free(hw_p->tx[i].data_ptr);
631 hw_p->tx[i].data_ptr = NULL;
632 }
633 }
634 }
wdenk544e9732004-02-06 23:19:44 +0000635 hw_p->tx[i].data_ptr = hw_p->txbuf_ptr;
636 if ((NUM_TX_BUFF - 1) == i)
637 hw_p->tx[i].ctrl |= MAL_TX_CTRL_WRAP;
638 hw_p->tx_run[i] = -1;
639#if 0
640 printf ("TX_BUFF %d @ 0x%08lx\n", i,
641 (ulong) hw_p->tx[i].data_ptr);
642#endif
643 }
644
645 for (i = 0; i < NUM_RX_BUFF; i++) {
646 hw_p->rx[i].ctrl = 0;
647 hw_p->rx[i].data_len = 0;
Wolfgang Denk0cbaf642005-09-25 00:53:22 +0200648 /* rx[i].data_ptr = (char *) &rx_buff[i]; */
wdenk544e9732004-02-06 23:19:44 +0000649 hw_p->rx[i].data_ptr = (char *) NetRxPackets[i];
650 if ((NUM_RX_BUFF - 1) == i)
651 hw_p->rx[i].ctrl |= MAL_RX_CTRL_WRAP;
652 hw_p->rx[i].ctrl |= MAL_RX_CTRL_EMPTY | MAL_RX_CTRL_INTR;
653 hw_p->rx_ready[i] = -1;
654#if 0
655 printf ("RX_BUFF %d @ 0x%08lx\n", i, (ulong) rx[i].data_ptr);
656#endif
657 }
658
659 reg = 0x00000000;
660
661 reg |= dev->enetaddr[0]; /* set high address */
662 reg = reg << 8;
663 reg |= dev->enetaddr[1];
664
665 out32 (EMAC_IAH + hw_p->hw_addr, reg);
666
667 reg = 0x00000000;
668 reg |= dev->enetaddr[2]; /* set low address */
669 reg = reg << 8;
670 reg |= dev->enetaddr[3];
671 reg = reg << 8;
672 reg |= dev->enetaddr[4];
673 reg = reg << 8;
674 reg |= dev->enetaddr[5];
675
676 out32 (EMAC_IAL + hw_p->hw_addr, reg);
677
678 switch (devnum) {
679 case 1:
680 /* setup MAL tx & rx channel pointers */
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200681#if defined (CONFIG_405EP) || defined (CONFIG_440EP) || defined (CONFIG_440GR)
Stefan Roese326c9712005-08-01 16:41:48 +0200682 mtdcr (maltxctp2r, hw_p->tx);
683#else
wdenk544e9732004-02-06 23:19:44 +0000684 mtdcr (maltxctp1r, hw_p->tx);
Stefan Roese326c9712005-08-01 16:41:48 +0200685#endif
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200686#if defined(CONFIG_440)
Stefan Roese326c9712005-08-01 16:41:48 +0200687 mtdcr (maltxbattr, 0x0);
wdenk544e9732004-02-06 23:19:44 +0000688 mtdcr (malrxbattr, 0x0);
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200689#endif
wdenk544e9732004-02-06 23:19:44 +0000690 mtdcr (malrxctp1r, hw_p->rx);
691 /* set RX buffer size */
692 mtdcr (malrcbs1, ENET_MAX_MTU_ALIGNED / 16);
693 break;
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200694#if defined (CONFIG_440GX)
wdenk544e9732004-02-06 23:19:44 +0000695 case 2:
696 /* setup MAL tx & rx channel pointers */
697 mtdcr (maltxbattr, 0x0);
wdenk544e9732004-02-06 23:19:44 +0000698 mtdcr (malrxbattr, 0x0);
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200699 mtdcr (maltxctp2r, hw_p->tx);
wdenk544e9732004-02-06 23:19:44 +0000700 mtdcr (malrxctp2r, hw_p->rx);
701 /* set RX buffer size */
702 mtdcr (malrcbs2, ENET_MAX_MTU_ALIGNED / 16);
703 break;
704 case 3:
705 /* setup MAL tx & rx channel pointers */
706 mtdcr (maltxbattr, 0x0);
707 mtdcr (maltxctp3r, hw_p->tx);
708 mtdcr (malrxbattr, 0x0);
709 mtdcr (malrxctp3r, hw_p->rx);
710 /* set RX buffer size */
711 mtdcr (malrcbs3, ENET_MAX_MTU_ALIGNED / 16);
712 break;
Stefan Roese797d8572005-08-11 17:56:56 +0200713#endif /* CONFIG_440GX */
wdenk544e9732004-02-06 23:19:44 +0000714 case 0:
715 default:
716 /* setup MAL tx & rx channel pointers */
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200717#if defined(CONFIG_440)
wdenk544e9732004-02-06 23:19:44 +0000718 mtdcr (maltxbattr, 0x0);
wdenk544e9732004-02-06 23:19:44 +0000719 mtdcr (malrxbattr, 0x0);
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200720#endif
721 mtdcr (maltxctp0r, hw_p->tx);
wdenk544e9732004-02-06 23:19:44 +0000722 mtdcr (malrxctp0r, hw_p->rx);
723 /* set RX buffer size */
724 mtdcr (malrcbs0, ENET_MAX_MTU_ALIGNED / 16);
725 break;
726 }
727
728 /* Enable MAL transmit and receive channels */
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200729#if defined(CONFIG_405EP) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
Stefan Roese326c9712005-08-01 16:41:48 +0200730 mtdcr (maltxcasr, (MAL_TXRX_CASR >> (hw_p->devnum*2)));
731#else
wdenk544e9732004-02-06 23:19:44 +0000732 mtdcr (maltxcasr, (MAL_TXRX_CASR >> hw_p->devnum));
Stefan Roese326c9712005-08-01 16:41:48 +0200733#endif
wdenk544e9732004-02-06 23:19:44 +0000734 mtdcr (malrxcasr, (MAL_TXRX_CASR >> hw_p->devnum));
735
736 /* set transmit enable & receive enable */
737 out32 (EMAC_M0 + hw_p->hw_addr, EMAC_M0_TXE | EMAC_M0_RXE);
738
739 /* set receive fifo to 4k and tx fifo to 2k */
740 mode_reg = in32 (EMAC_M1 + hw_p->hw_addr);
741 mode_reg |= EMAC_M1_RFS_4K | EMAC_M1_TX_FIFO_2K;
742
743 /* set speed */
wdenked2ac4b2004-03-14 18:23:55 +0000744 if (speed == _1000BASET)
745 mode_reg = mode_reg | EMAC_M1_MF_1000MBPS | EMAC_M1_IST;
746 else if (speed == _100BASET)
wdenk544e9732004-02-06 23:19:44 +0000747 mode_reg = mode_reg | EMAC_M1_MF_100MBPS | EMAC_M1_IST;
748 else
749 mode_reg = mode_reg & ~0x00C00000; /* 10 MBPS */
750 if (duplex == FULL)
751 mode_reg = mode_reg | 0x80000000 | EMAC_M1_IST;
752
753 out32 (EMAC_M1 + hw_p->hw_addr, mode_reg);
754
755 /* Enable broadcast and indvidual address */
756 /* TBS: enabling runts as some misbehaved nics will send runts */
757 out32 (EMAC_RXM + hw_p->hw_addr, EMAC_RMR_BAE | EMAC_RMR_IAE);
758
759 /* we probably need to set the tx mode1 reg? maybe at tx time */
760
761 /* set transmit request threshold register */
762 out32 (EMAC_TRTR + hw_p->hw_addr, 0x18000000); /* 256 byte threshold */
763
Wolfgang Denk0cbaf642005-09-25 00:53:22 +0200764 /* set receive low/high water mark register */
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200765#if defined(CONFIG_440)
wdenk544e9732004-02-06 23:19:44 +0000766 /* 440GP has a 64 byte burst length */
767 out32 (EMAC_RX_HI_LO_WMARK + hw_p->hw_addr, 0x80009000);
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200768#else
769 /* 405s have a 16 byte burst length */
770 out32 (EMAC_RX_HI_LO_WMARK + hw_p->hw_addr, 0x0f002000);
771#endif /* defined(CONFIG_440) */
wdenk544e9732004-02-06 23:19:44 +0000772 out32 (EMAC_TXM1 + hw_p->hw_addr, 0xf8640000);
773
774 /* Set fifo limit entry in tx mode 0 */
775 out32 (EMAC_TXM0 + hw_p->hw_addr, 0x00000003);
776 /* Frame gap set */
777 out32 (EMAC_I_FRAME_GAP_REG + hw_p->hw_addr, 0x00000008);
778
779 /* Set EMAC IER */
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200780 hw_p->emac_ier = EMAC_ISR_PTLE | EMAC_ISR_BFCS | EMAC_ISR_ORE | EMAC_ISR_IRE;
wdenk544e9732004-02-06 23:19:44 +0000781 if (speed == _100BASET)
782 hw_p->emac_ier = hw_p->emac_ier | EMAC_ISR_SYE;
783
784 out32 (EMAC_ISR + hw_p->hw_addr, 0xffffffff); /* clear pending interrupts */
785 out32 (EMAC_IER + hw_p->hw_addr, hw_p->emac_ier);
786
787 if (hw_p->first_init == 0) {
788 /*
789 * Connect interrupt service routines
790 */
wdenk544e9732004-02-06 23:19:44 +0000791 irq_install_handler (VECNUM_ETH0 + (hw_p->devnum * 2),
792 (interrupt_handler_t *) enetInt, dev);
793 }
wdenk544e9732004-02-06 23:19:44 +0000794
795 mtmsr (msr); /* enable interrupts again */
796
797 hw_p->bis = bis;
798 hw_p->first_init = 1;
799
800 return (1);
801}
802
803
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200804static int ppc_4xx_eth_send (struct eth_device *dev, volatile void *ptr,
wdenk544e9732004-02-06 23:19:44 +0000805 int len)
806{
807 struct enet_frame *ef_ptr;
808 ulong time_start, time_now;
809 unsigned long temp_txm0;
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200810 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenk544e9732004-02-06 23:19:44 +0000811
812 ef_ptr = (struct enet_frame *) ptr;
813
814 /*-----------------------------------------------------------------------+
815 * Copy in our address into the frame.
816 *-----------------------------------------------------------------------*/
817 (void) memcpy (ef_ptr->source_addr, dev->enetaddr, ENET_ADDR_LENGTH);
818
819 /*-----------------------------------------------------------------------+
820 * If frame is too long or too short, modify length.
821 *-----------------------------------------------------------------------*/
822 /* TBS: where does the fragment go???? */
823 if (len > ENET_MAX_MTU)
824 len = ENET_MAX_MTU;
825
826 /* memcpy ((void *) &tx_buff[tx_slot], (const void *) ptr, len); */
827 memcpy ((void *) hw_p->txbuf_ptr, (const void *) ptr, len);
828
829 /*-----------------------------------------------------------------------+
830 * set TX Buffer busy, and send it
831 *-----------------------------------------------------------------------*/
832 hw_p->tx[hw_p->tx_slot].ctrl = (MAL_TX_CTRL_LAST |
833 EMAC_TX_CTRL_GFCS | EMAC_TX_CTRL_GP) &
834 ~(EMAC_TX_CTRL_ISA | EMAC_TX_CTRL_RSA);
835 if ((NUM_TX_BUFF - 1) == hw_p->tx_slot)
836 hw_p->tx[hw_p->tx_slot].ctrl |= MAL_TX_CTRL_WRAP;
837
838 hw_p->tx[hw_p->tx_slot].data_len = (short) len;
839 hw_p->tx[hw_p->tx_slot].ctrl |= MAL_TX_CTRL_READY;
840
841 __asm__ volatile ("eieio");
842
843 out32 (EMAC_TXM0 + hw_p->hw_addr,
844 in32 (EMAC_TXM0 + hw_p->hw_addr) | EMAC_TXM0_GNP0);
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200845#ifdef INFO_4XX_ENET
wdenk544e9732004-02-06 23:19:44 +0000846 hw_p->stats.pkts_tx++;
847#endif
848
849 /*-----------------------------------------------------------------------+
850 * poll unitl the packet is sent and then make sure it is OK
851 *-----------------------------------------------------------------------*/
852 time_start = get_timer (0);
853 while (1) {
854 temp_txm0 = in32 (EMAC_TXM0 + hw_p->hw_addr);
855 /* loop until either TINT turns on or 3 seconds elapse */
856 if ((temp_txm0 & EMAC_TXM0_GNP0) != 0) {
857 /* transmit is done, so now check for errors
858 * If there is an error, an interrupt should
859 * happen when we return
860 */
861 time_now = get_timer (0);
862 if ((time_now - time_start) > 3000) {
863 return (-1);
864 }
865 } else {
866 return (len);
867 }
868 }
869}
870
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200871#if defined (CONFIG_440)
wdenk544e9732004-02-06 23:19:44 +0000872
873int enetInt (struct eth_device *dev)
874{
875 int serviced;
876 int rc = -1; /* default to not us */
877 unsigned long mal_isr;
878 unsigned long emac_isr = 0;
879 unsigned long mal_rx_eob;
880 unsigned long my_uic0msr, my_uic1msr;
881
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200882#if defined(CONFIG_440GX)
wdenk544e9732004-02-06 23:19:44 +0000883 unsigned long my_uic2msr;
884#endif
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200885 EMAC_4XX_HW_PST hw_p;
wdenk544e9732004-02-06 23:19:44 +0000886
887 /*
888 * Because the mal is generic, we need to get the current
889 * eth device
890 */
Stefan Roese0c7ffc02005-08-16 18:18:00 +0200891#if defined(CONFIG_NET_MULTI)
892 dev = eth_get_dev();
893#else
894 dev = emac0_dev;
895#endif
wdenk544e9732004-02-06 23:19:44 +0000896
897 hw_p = dev->priv;
898
899
900 /* enter loop that stays in interrupt code until nothing to service */
901 do {
902 serviced = 0;
903
904 my_uic0msr = mfdcr (uic0msr);
905 my_uic1msr = mfdcr (uic1msr);
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200906#if defined(CONFIG_440GX)
wdenk544e9732004-02-06 23:19:44 +0000907 my_uic2msr = mfdcr (uic2msr);
908#endif
909 if (!(my_uic0msr & (UIC_MRE | UIC_MTE))
910 && !(my_uic1msr &
911 (UIC_ETH0 | UIC_ETH1 | UIC_MS | UIC_MTDE |
912 UIC_MRDE))) {
913 /* not for us */
914 return (rc);
915 }
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200916#if defined (CONFIG_440GX)
wdenk544e9732004-02-06 23:19:44 +0000917 if (!(my_uic0msr & (UIC_MRE | UIC_MTE))
918 && !(my_uic2msr & (UIC_ETH2 | UIC_ETH3))) {
919 /* not for us */
920 return (rc);
921 }
922#endif
923 /* get and clear controller status interrupts */
924 /* look at Mal and EMAC interrupts */
925 if ((my_uic0msr & (UIC_MRE | UIC_MTE))
926 || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
927 /* we have a MAL interrupt */
928 mal_isr = mfdcr (malesr);
929 /* look for mal error */
930 if (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE)) {
931 mal_err (dev, mal_isr, my_uic0msr,
932 MAL_UIC_DEF, MAL_UIC_ERR);
933 serviced = 1;
934 rc = 0;
935 }
936 }
937
938 /* port by port dispatch of emac interrupts */
939 if (hw_p->devnum == 0) {
940 if (UIC_ETH0 & my_uic1msr) { /* look for EMAC errors */
941 emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
942 if ((hw_p->emac_ier & emac_isr) != 0) {
943 emac_err (dev, emac_isr);
944 serviced = 1;
945 rc = 0;
946 }
947 }
948 if ((hw_p->emac_ier & emac_isr)
949 || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
950 mtdcr (uic0sr, UIC_MRE | UIC_MTE); /* Clear */
951 mtdcr (uic1sr, UIC_ETH0 | UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
952 return (rc); /* we had errors so get out */
953 }
954 }
955
956 if (hw_p->devnum == 1) {
957 if (UIC_ETH1 & my_uic1msr) { /* look for EMAC errors */
958 emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
959 if ((hw_p->emac_ier & emac_isr) != 0) {
960 emac_err (dev, emac_isr);
961 serviced = 1;
962 rc = 0;
963 }
964 }
965 if ((hw_p->emac_ier & emac_isr)
966 || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
967 mtdcr (uic0sr, UIC_MRE | UIC_MTE); /* Clear */
968 mtdcr (uic1sr, UIC_ETH1 | UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
969 return (rc); /* we had errors so get out */
970 }
971 }
Stefan Roeseb30f2a12005-08-08 12:42:22 +0200972#if defined (CONFIG_440GX)
wdenk544e9732004-02-06 23:19:44 +0000973 if (hw_p->devnum == 2) {
974 if (UIC_ETH2 & my_uic2msr) { /* look for EMAC errors */
975 emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
976 if ((hw_p->emac_ier & emac_isr) != 0) {
977 emac_err (dev, emac_isr);
978 serviced = 1;
979 rc = 0;
980 }
981 }
982 if ((hw_p->emac_ier & emac_isr)
983 || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
984 mtdcr (uic0sr, UIC_MRE | UIC_MTE); /* Clear */
985 mtdcr (uic1sr, UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
986 mtdcr (uic2sr, UIC_ETH2);
987 return (rc); /* we had errors so get out */
988 }
989 }
990
991 if (hw_p->devnum == 3) {
992 if (UIC_ETH3 & my_uic2msr) { /* look for EMAC errors */
993 emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
994 if ((hw_p->emac_ier & emac_isr) != 0) {
995 emac_err (dev, emac_isr);
996 serviced = 1;
997 rc = 0;
998 }
999 }
1000 if ((hw_p->emac_ier & emac_isr)
1001 || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
1002 mtdcr (uic0sr, UIC_MRE | UIC_MTE); /* Clear */
1003 mtdcr (uic1sr, UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
1004 mtdcr (uic2sr, UIC_ETH3);
1005 return (rc); /* we had errors so get out */
1006 }
1007 }
Stefan Roeseb30f2a12005-08-08 12:42:22 +02001008#endif /* CONFIG_440GX */
wdenk544e9732004-02-06 23:19:44 +00001009 /* handle MAX TX EOB interrupt from a tx */
1010 if (my_uic0msr & UIC_MTE) {
1011 mal_rx_eob = mfdcr (maltxeobisr);
1012 mtdcr (maltxeobisr, mal_rx_eob);
1013 mtdcr (uic0sr, UIC_MTE);
1014 }
1015 /* handle MAL RX EOB interupt from a receive */
wdenk7ad5e4c2004-04-25 15:41:35 +00001016 /* check for EOB on valid channels */
wdenk544e9732004-02-06 23:19:44 +00001017 if (my_uic0msr & UIC_MRE) {
1018 mal_rx_eob = mfdcr (malrxeobisr);
Wolfgang Denk0cbaf642005-09-25 00:53:22 +02001019 if ((mal_rx_eob & (0x80000000 >> hw_p->devnum)) != 0) { /* call emac routine for channel x */
wdenk544e9732004-02-06 23:19:44 +00001020 /* clear EOB
1021 mtdcr(malrxeobisr, mal_rx_eob); */
1022 enet_rcv (dev, emac_isr);
1023 /* indicate that we serviced an interrupt */
1024 serviced = 1;
1025 rc = 0;
1026 }
1027 }
1028 mtdcr (uic0sr, UIC_MRE); /* Clear */
1029 mtdcr (uic1sr, UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
1030 switch (hw_p->devnum) {
1031 case 0:
1032 mtdcr (uic1sr, UIC_ETH0);
1033 break;
1034 case 1:
1035 mtdcr (uic1sr, UIC_ETH1);
1036 break;
Stefan Roeseb30f2a12005-08-08 12:42:22 +02001037#if defined (CONFIG_440GX)
wdenk544e9732004-02-06 23:19:44 +00001038 case 2:
1039 mtdcr (uic2sr, UIC_ETH2);
1040 break;
1041 case 3:
1042 mtdcr (uic2sr, UIC_ETH3);
1043 break;
Stefan Roeseb30f2a12005-08-08 12:42:22 +02001044#endif /* CONFIG_440GX */
wdenk544e9732004-02-06 23:19:44 +00001045 default:
1046 break;
1047 }
1048 } while (serviced);
1049
1050 return (rc);
1051}
1052
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001053#else /* CONFIG_440 */
1054
1055int enetInt (struct eth_device *dev)
1056{
1057 int serviced;
1058 int rc = -1; /* default to not us */
1059 unsigned long mal_isr;
1060 unsigned long emac_isr = 0;
1061 unsigned long mal_rx_eob;
1062 unsigned long my_uicmsr;
1063
1064 EMAC_4XX_HW_PST hw_p;
1065
1066 /*
1067 * Because the mal is generic, we need to get the current
1068 * eth device
1069 */
1070#if defined(CONFIG_NET_MULTI)
1071 dev = eth_get_dev();
1072#else
1073 dev = emac0_dev;
1074#endif
1075
1076 hw_p = dev->priv;
1077
1078 /* enter loop that stays in interrupt code until nothing to service */
1079 do {
1080 serviced = 0;
1081
1082 my_uicmsr = mfdcr (uicmsr);
1083
1084 if ((my_uicmsr & (MAL_UIC_DEF | EMAC_UIC_DEF)) == 0) { /* not for us */
1085 return (rc);
1086 }
1087 /* get and clear controller status interrupts */
1088 /* look at Mal and EMAC interrupts */
1089 if ((MAL_UIC_DEF & my_uicmsr) != 0) { /* we have a MAL interrupt */
1090 mal_isr = mfdcr (malesr);
1091 /* look for mal error */
1092 if ((my_uicmsr & MAL_UIC_ERR) != 0) {
1093 mal_err (dev, mal_isr, my_uicmsr, MAL_UIC_DEF, MAL_UIC_ERR);
1094 serviced = 1;
1095 rc = 0;
1096 }
1097 }
1098
1099 /* port by port dispatch of emac interrupts */
1100
1101 if ((SEL_UIC_DEF(hw_p->devnum) & my_uicmsr) != 0) { /* look for EMAC errors */
1102 emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
1103 if ((hw_p->emac_ier & emac_isr) != 0) {
1104 emac_err (dev, emac_isr);
1105 serviced = 1;
1106 rc = 0;
1107 }
1108 }
1109 if (((hw_p->emac_ier & emac_isr) != 0) || ((MAL_UIC_ERR & my_uicmsr) != 0)) {
1110 mtdcr (uicsr, MAL_UIC_DEF | SEL_UIC_DEF(hw_p->devnum)); /* Clear */
1111 return (rc); /* we had errors so get out */
1112 }
1113
1114 /* handle MAX TX EOB interrupt from a tx */
1115 if (my_uicmsr & UIC_MAL_TXEOB) {
1116 mal_rx_eob = mfdcr (maltxeobisr);
1117 mtdcr (maltxeobisr, mal_rx_eob);
1118 mtdcr (uicsr, UIC_MAL_TXEOB);
1119 }
1120 /* handle MAL RX EOB interupt from a receive */
1121 /* check for EOB on valid channels */
1122 if (my_uicmsr & UIC_MAL_RXEOB)
1123 {
1124 mal_rx_eob = mfdcr (malrxeobisr);
Wolfgang Denk0cbaf642005-09-25 00:53:22 +02001125 if ((mal_rx_eob & (0x80000000 >> hw_p->devnum)) != 0) { /* call emac routine for channel x */
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001126 /* clear EOB
1127 mtdcr(malrxeobisr, mal_rx_eob); */
1128 enet_rcv (dev, emac_isr);
1129 /* indicate that we serviced an interrupt */
1130 serviced = 1;
1131 rc = 0;
1132 }
1133 }
1134 mtdcr (uicsr, MAL_UIC_DEF|EMAC_UIC_DEF|EMAC_UIC_DEF1); /* Clear */
1135 }
1136 while (serviced);
1137
1138 return (rc);
1139}
1140
1141#endif /* CONFIG_440 */
1142
wdenk544e9732004-02-06 23:19:44 +00001143/*-----------------------------------------------------------------------------+
1144 * MAL Error Routine
1145 *-----------------------------------------------------------------------------*/
1146static void mal_err (struct eth_device *dev, unsigned long isr,
1147 unsigned long uic, unsigned long maldef,
1148 unsigned long mal_errr)
1149{
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001150 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenk544e9732004-02-06 23:19:44 +00001151
1152 mtdcr (malesr, isr); /* clear interrupt */
1153
1154 /* clear DE interrupt */
1155 mtdcr (maltxdeir, 0xC0000000);
1156 mtdcr (malrxdeir, 0x80000000);
1157
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001158#ifdef INFO_4XX_ENET
Wolfgang Denk0cbaf642005-09-25 00:53:22 +02001159 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 +00001160#endif
1161
1162 eth_init (hw_p->bis); /* start again... */
1163}
1164
1165/*-----------------------------------------------------------------------------+
1166 * EMAC Error Routine
1167 *-----------------------------------------------------------------------------*/
1168static void emac_err (struct eth_device *dev, unsigned long isr)
1169{
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001170 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenk544e9732004-02-06 23:19:44 +00001171
1172 printf ("EMAC%d error occured.... ISR = %lx\n", hw_p->devnum, isr);
1173 out32 (EMAC_ISR + hw_p->hw_addr, isr);
1174}
1175
1176/*-----------------------------------------------------------------------------+
1177 * enet_rcv() handles the ethernet receive data
1178 *-----------------------------------------------------------------------------*/
1179static void enet_rcv (struct eth_device *dev, unsigned long malisr)
1180{
1181 struct enet_frame *ef_ptr;
1182 unsigned long data_len;
1183 unsigned long rx_eob_isr;
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001184 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenk544e9732004-02-06 23:19:44 +00001185
1186 int handled = 0;
1187 int i;
1188 int loop_count = 0;
1189
1190 rx_eob_isr = mfdcr (malrxeobisr);
1191 if ((0x80000000 >> hw_p->devnum) & rx_eob_isr) {
1192 /* clear EOB */
1193 mtdcr (malrxeobisr, rx_eob_isr);
1194
1195 /* EMAC RX done */
1196 while (1) { /* do all */
1197 i = hw_p->rx_slot;
1198
1199 if ((MAL_RX_CTRL_EMPTY & hw_p->rx[i].ctrl)
1200 || (loop_count >= NUM_RX_BUFF))
1201 break;
1202 loop_count++;
1203 hw_p->rx_slot++;
1204 if (NUM_RX_BUFF == hw_p->rx_slot)
1205 hw_p->rx_slot = 0;
1206 handled++;
1207 data_len = (unsigned long) hw_p->rx[i].data_len; /* Get len */
1208 if (data_len) {
1209 if (data_len > ENET_MAX_MTU) /* Check len */
1210 data_len = 0;
1211 else {
1212 if (EMAC_RX_ERRORS & hw_p->rx[i].ctrl) { /* Check Errors */
1213 data_len = 0;
1214 hw_p->stats.rx_err_log[hw_p->
1215 rx_err_index]
1216 = hw_p->rx[i].ctrl;
1217 hw_p->rx_err_index++;
1218 if (hw_p->rx_err_index ==
1219 MAX_ERR_LOG)
1220 hw_p->rx_err_index =
1221 0;
wdenk7ad5e4c2004-04-25 15:41:35 +00001222 } /* emac_erros */
wdenk544e9732004-02-06 23:19:44 +00001223 } /* data_len < max mtu */
wdenk7ad5e4c2004-04-25 15:41:35 +00001224 } /* if data_len */
wdenk544e9732004-02-06 23:19:44 +00001225 if (!data_len) { /* no data */
1226 hw_p->rx[i].ctrl |= MAL_RX_CTRL_EMPTY; /* Free Recv Buffer */
1227
1228 hw_p->stats.data_len_err++; /* Error at Rx */
1229 }
1230
1231 /* !data_len */
1232 /* AS.HARNOIS */
1233 /* Check if user has already eaten buffer */
1234 /* if not => ERROR */
1235 else if (hw_p->rx_ready[hw_p->rx_i_index] != -1) {
1236 if (hw_p->is_receiving)
1237 printf ("ERROR : Receive buffers are full!\n");
1238 break;
1239 } else {
1240 hw_p->stats.rx_frames++;
1241 hw_p->stats.rx += data_len;
1242 ef_ptr = (struct enet_frame *) hw_p->rx[i].
1243 data_ptr;
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001244#ifdef INFO_4XX_ENET
wdenk544e9732004-02-06 23:19:44 +00001245 hw_p->stats.pkts_rx++;
1246#endif
1247 /* AS.HARNOIS
1248 * use ring buffer
1249 */
1250 hw_p->rx_ready[hw_p->rx_i_index] = i;
1251 hw_p->rx_i_index++;
1252 if (NUM_RX_BUFF == hw_p->rx_i_index)
1253 hw_p->rx_i_index = 0;
1254
wdenk544e9732004-02-06 23:19:44 +00001255 /* AS.HARNOIS
1256 * free receive buffer only when
1257 * buffer has been handled (eth_rx)
1258 rx[i].ctrl |= MAL_RX_CTRL_EMPTY;
1259 */
1260 } /* if data_len */
1261 } /* while */
1262 } /* if EMACK_RXCHL */
1263}
1264
1265
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001266static int ppc_4xx_eth_rx (struct eth_device *dev)
wdenk544e9732004-02-06 23:19:44 +00001267{
1268 int length;
1269 int user_index;
1270 unsigned long msr;
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001271 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenk544e9732004-02-06 23:19:44 +00001272
Wolfgang Denk0cbaf642005-09-25 00:53:22 +02001273 hw_p->is_receiving = 1; /* tell driver */
wdenk544e9732004-02-06 23:19:44 +00001274
1275 for (;;) {
1276 /* AS.HARNOIS
1277 * use ring buffer and
1278 * get index from rx buffer desciptor queue
1279 */
1280 user_index = hw_p->rx_ready[hw_p->rx_u_index];
1281 if (user_index == -1) {
1282 length = -1;
1283 break; /* nothing received - leave for() loop */
1284 }
1285
1286 msr = mfmsr ();
1287 mtmsr (msr & ~(MSR_EE));
1288
1289 length = hw_p->rx[user_index].data_len;
1290
1291 /* Pass the packet up to the protocol layers. */
Wolfgang Denk0cbaf642005-09-25 00:53:22 +02001292 /* NetReceive(NetRxPackets[rxIdx], length - 4); */
1293 /* NetReceive(NetRxPackets[i], length); */
wdenk544e9732004-02-06 23:19:44 +00001294 NetReceive (NetRxPackets[user_index], length - 4);
1295 /* Free Recv Buffer */
1296 hw_p->rx[user_index].ctrl |= MAL_RX_CTRL_EMPTY;
1297 /* Free rx buffer descriptor queue */
1298 hw_p->rx_ready[hw_p->rx_u_index] = -1;
1299 hw_p->rx_u_index++;
1300 if (NUM_RX_BUFF == hw_p->rx_u_index)
1301 hw_p->rx_u_index = 0;
1302
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001303#ifdef INFO_4XX_ENET
wdenk544e9732004-02-06 23:19:44 +00001304 hw_p->stats.pkts_handled++;
1305#endif
1306
1307 mtmsr (msr); /* Enable IRQ's */
1308 }
1309
Wolfgang Denk0cbaf642005-09-25 00:53:22 +02001310 hw_p->is_receiving = 0; /* tell driver */
wdenk544e9732004-02-06 23:19:44 +00001311
1312 return length;
1313}
1314
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001315int ppc_4xx_eth_initialize (bd_t * bis)
wdenk544e9732004-02-06 23:19:44 +00001316{
1317 static int virgin = 0;
wdenk544e9732004-02-06 23:19:44 +00001318 struct eth_device *dev;
1319 int eth_num = 0;
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001320 EMAC_4XX_HW_PST hw = NULL;
wdenk544e9732004-02-06 23:19:44 +00001321
Stefan Roeseb30f2a12005-08-08 12:42:22 +02001322#if defined(CONFIG_440GX)
Stefan Roese326c9712005-08-01 16:41:48 +02001323 unsigned long pfc1;
1324
wdenk544e9732004-02-06 23:19:44 +00001325 mfsdr (sdr_pfc1, pfc1);
1326 pfc1 &= ~(0x01e00000);
1327 pfc1 |= 0x01200000;
1328 mtsdr (sdr_pfc1, pfc1);
Stefan Roese326c9712005-08-01 16:41:48 +02001329#endif
wdenk56ed43e2004-02-22 23:46:08 +00001330 /* set phy num and mode */
1331 bis->bi_phynum[0] = CONFIG_PHY_ADDR;
Stefan Roese326c9712005-08-01 16:41:48 +02001332#if defined(CONFIG_PHY1_ADDR)
wdenk56ed43e2004-02-22 23:46:08 +00001333 bis->bi_phynum[1] = CONFIG_PHY1_ADDR;
Stefan Roese326c9712005-08-01 16:41:48 +02001334#endif
Stefan Roeseb30f2a12005-08-08 12:42:22 +02001335#if defined(CONFIG_440GX)
wdenk56ed43e2004-02-22 23:46:08 +00001336 bis->bi_phynum[2] = CONFIG_PHY2_ADDR;
1337 bis->bi_phynum[3] = CONFIG_PHY3_ADDR;
1338 bis->bi_phymode[0] = 0;
1339 bis->bi_phymode[1] = 0;
1340 bis->bi_phymode[2] = 2;
1341 bis->bi_phymode[3] = 2;
wdenk544e9732004-02-06 23:19:44 +00001342
Stefan Roeseb30f2a12005-08-08 12:42:22 +02001343#if defined (CONFIG_440GX)
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001344 ppc_4xx_eth_setup_bridge(0, bis);
wdenk97e8bda2004-09-29 22:43:59 +00001345#endif
Stefan Roese326c9712005-08-01 16:41:48 +02001346#endif
wdenk97e8bda2004-09-29 22:43:59 +00001347
wdenk544e9732004-02-06 23:19:44 +00001348 for (eth_num = 0; eth_num < EMAC_NUM_DEV; eth_num++) {
1349
1350 /* See if we can actually bring up the interface, otherwise, skip it */
1351 switch (eth_num) {
wdenk54070ab2004-12-31 09:32:47 +00001352 default: /* fall through */
wdenk544e9732004-02-06 23:19:44 +00001353 case 0:
wdenk56ed43e2004-02-22 23:46:08 +00001354 if (memcmp (bis->bi_enetaddr, "\0\0\0\0\0\0", 6) == 0) {
1355 bis->bi_phymode[eth_num] = BI_PHYMODE_NONE;
wdenk544e9732004-02-06 23:19:44 +00001356 continue;
wdenk56ed43e2004-02-22 23:46:08 +00001357 }
wdenk544e9732004-02-06 23:19:44 +00001358 break;
wdenk54070ab2004-12-31 09:32:47 +00001359#ifdef CONFIG_HAS_ETH1
wdenk544e9732004-02-06 23:19:44 +00001360 case 1:
wdenk56ed43e2004-02-22 23:46:08 +00001361 if (memcmp (bis->bi_enet1addr, "\0\0\0\0\0\0", 6) == 0) {
1362 bis->bi_phymode[eth_num] = BI_PHYMODE_NONE;
wdenk544e9732004-02-06 23:19:44 +00001363 continue;
wdenk56ed43e2004-02-22 23:46:08 +00001364 }
wdenk544e9732004-02-06 23:19:44 +00001365 break;
wdenk54070ab2004-12-31 09:32:47 +00001366#endif
1367#ifdef CONFIG_HAS_ETH2
wdenk544e9732004-02-06 23:19:44 +00001368 case 2:
wdenk56ed43e2004-02-22 23:46:08 +00001369 if (memcmp (bis->bi_enet2addr, "\0\0\0\0\0\0", 6) == 0) {
1370 bis->bi_phymode[eth_num] = BI_PHYMODE_NONE;
wdenk544e9732004-02-06 23:19:44 +00001371 continue;
wdenk56ed43e2004-02-22 23:46:08 +00001372 }
wdenk544e9732004-02-06 23:19:44 +00001373 break;
wdenk54070ab2004-12-31 09:32:47 +00001374#endif
1375#ifdef CONFIG_HAS_ETH3
wdenk544e9732004-02-06 23:19:44 +00001376 case 3:
wdenk56ed43e2004-02-22 23:46:08 +00001377 if (memcmp (bis->bi_enet3addr, "\0\0\0\0\0\0", 6) == 0) {
1378 bis->bi_phymode[eth_num] = BI_PHYMODE_NONE;
wdenk544e9732004-02-06 23:19:44 +00001379 continue;
wdenk56ed43e2004-02-22 23:46:08 +00001380 }
wdenk544e9732004-02-06 23:19:44 +00001381 break;
wdenk54070ab2004-12-31 09:32:47 +00001382#endif
wdenk544e9732004-02-06 23:19:44 +00001383 }
1384
1385 /* Allocate device structure */
1386 dev = (struct eth_device *) malloc (sizeof (*dev));
1387 if (dev == NULL) {
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001388 printf ("ppc_4xx_eth_initialize: "
wdenkef893942004-02-23 16:11:30 +00001389 "Cannot allocate eth_device %d\n", eth_num);
wdenk544e9732004-02-06 23:19:44 +00001390 return (-1);
1391 }
wdenkd1894de2005-06-20 10:17:34 +00001392 memset(dev, 0, sizeof(*dev));
wdenk544e9732004-02-06 23:19:44 +00001393
1394 /* Allocate our private use data */
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001395 hw = (EMAC_4XX_HW_PST) malloc (sizeof (*hw));
wdenk544e9732004-02-06 23:19:44 +00001396 if (hw == NULL) {
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001397 printf ("ppc_4xx_eth_initialize: "
wdenkef893942004-02-23 16:11:30 +00001398 "Cannot allocate private hw data for eth_device %d",
wdenk544e9732004-02-06 23:19:44 +00001399 eth_num);
1400 free (dev);
1401 return (-1);
1402 }
wdenkd1894de2005-06-20 10:17:34 +00001403 memset(hw, 0, sizeof(*hw));
wdenk544e9732004-02-06 23:19:44 +00001404
1405 switch (eth_num) {
wdenk54070ab2004-12-31 09:32:47 +00001406 default: /* fall through */
wdenk544e9732004-02-06 23:19:44 +00001407 case 0:
1408 hw->hw_addr = 0;
1409 memcpy (dev->enetaddr, bis->bi_enetaddr, 6);
1410 break;
wdenk54070ab2004-12-31 09:32:47 +00001411#ifdef CONFIG_HAS_ETH1
wdenk544e9732004-02-06 23:19:44 +00001412 case 1:
1413 hw->hw_addr = 0x100;
1414 memcpy (dev->enetaddr, bis->bi_enet1addr, 6);
1415 break;
wdenk54070ab2004-12-31 09:32:47 +00001416#endif
1417#ifdef CONFIG_HAS_ETH2
wdenk544e9732004-02-06 23:19:44 +00001418 case 2:
1419 hw->hw_addr = 0x400;
1420 memcpy (dev->enetaddr, bis->bi_enet2addr, 6);
1421 break;
wdenk54070ab2004-12-31 09:32:47 +00001422#endif
1423#ifdef CONFIG_HAS_ETH3
wdenk544e9732004-02-06 23:19:44 +00001424 case 3:
1425 hw->hw_addr = 0x600;
1426 memcpy (dev->enetaddr, bis->bi_enet3addr, 6);
1427 break;
wdenk54070ab2004-12-31 09:32:47 +00001428#endif
wdenk544e9732004-02-06 23:19:44 +00001429 }
1430
1431 hw->devnum = eth_num;
Stefan Roese326c9712005-08-01 16:41:48 +02001432 hw->print_speed = 1;
wdenk544e9732004-02-06 23:19:44 +00001433
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001434 sprintf (dev->name, "ppc_4xx_eth%d", eth_num);
wdenk544e9732004-02-06 23:19:44 +00001435 dev->priv = (void *) hw;
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001436 dev->init = ppc_4xx_eth_init;
1437 dev->halt = ppc_4xx_eth_halt;
1438 dev->send = ppc_4xx_eth_send;
1439 dev->recv = ppc_4xx_eth_rx;
wdenk544e9732004-02-06 23:19:44 +00001440
1441 if (0 == virgin) {
1442 /* set the MAL IER ??? names may change with new spec ??? */
1443 mal_ier =
1444 MAL_IER_DE | MAL_IER_NE | MAL_IER_TE |
1445 MAL_IER_OPBE | MAL_IER_PLBE;
1446 mtdcr (malesr, 0xffffffff); /* clear pending interrupts */
1447 mtdcr (maltxdeir, 0xffffffff); /* clear pending interrupts */
1448 mtdcr (malrxdeir, 0xffffffff); /* clear pending interrupts */
1449 mtdcr (malier, mal_ier);
1450
1451 /* install MAL interrupt handler */
1452 irq_install_handler (VECNUM_MS,
1453 (interrupt_handler_t *) enetInt,
1454 dev);
1455 irq_install_handler (VECNUM_MTE,
1456 (interrupt_handler_t *) enetInt,
1457 dev);
1458 irq_install_handler (VECNUM_MRE,
1459 (interrupt_handler_t *) enetInt,
1460 dev);
1461 irq_install_handler (VECNUM_TXDE,
1462 (interrupt_handler_t *) enetInt,
1463 dev);
1464 irq_install_handler (VECNUM_RXDE,
1465 (interrupt_handler_t *) enetInt,
1466 dev);
1467 virgin = 1;
1468 }
1469
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001470#if defined(CONFIG_NET_MULTI)
wdenk544e9732004-02-06 23:19:44 +00001471 eth_register (dev);
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001472#else
1473 emac0_dev = dev;
1474#endif
wdenk544e9732004-02-06 23:19:44 +00001475
1476 } /* end for each supported device */
1477 return (1);
1478}
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001479
1480
1481#if !defined(CONFIG_NET_MULTI)
1482void eth_halt (void) {
1483 if (emac0_dev) {
1484 ppc_4xx_eth_halt(emac0_dev);
1485 free(emac0_dev);
1486 emac0_dev = NULL;
1487 }
1488}
1489
1490int eth_init (bd_t *bis)
1491{
1492 ppc_4xx_eth_initialize(bis);
Stefan Roese03510612005-10-10 17:43:58 +02001493 if (emac0_dev) {
1494 return ppc_4xx_eth_init(emac0_dev, bis);
1495 } else {
1496 printf("ERROR: ethaddr not set!\n");
1497 return -1;
1498 }
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001499}
1500
1501int eth_send(volatile void *packet, int length)
1502{
Stefan Roese0c7ffc02005-08-16 18:18:00 +02001503 return (ppc_4xx_eth_send(emac0_dev, packet, length));
1504}
1505
1506int eth_rx(void)
1507{
1508 return (ppc_4xx_eth_rx(emac0_dev));
1509}
1510#endif /* !defined(CONFIG_NET_MULTI) */
1511
1512#endif /* #if (CONFIG_COMMANDS & CFG_CMD_NET) */