blob: 86dc2d066e8118cc3827460967c59f0c4928dbac [file] [log] [blame]
wdenkba56f622004-02-06 23:19:44 +00001/*-----------------------------------------------------------------------------+
2 *
Wolfgang Denk265817c2005-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.
wdenkba56f622004-02-06 23:19:44 +00009 *
Wolfgang Denk265817c2005-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.
wdenkba56f622004-02-06 23:19:44 +000013 *
Wolfgang Denk265817c2005-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.
wdenkba56f622004-02-06 23:19:44 +000017 *
Wolfgang Denk265817c2005-09-25 00:53:22 +020018 * COPYRIGHT I B M CORPORATION 1995
19 * LICENSED MATERIAL - PROGRAM PROPERTY OF I B M
wdenkba56f622004-02-06 23:19:44 +000020 *-----------------------------------------------------------------------------*/
21/*-----------------------------------------------------------------------------+
22 *
Wolfgang Denk265817c2005-09-25 00:53:22 +020023 * File Name: enetemac.c
wdenkba56f622004-02-06 23:19:44 +000024 *
Wolfgang Denk265817c2005-09-25 00:53:22 +020025 * Function: Device driver for the ethernet EMAC3 macro on the 405GP.
wdenkba56f622004-02-06 23:19:44 +000026 *
Wolfgang Denk265817c2005-09-25 00:53:22 +020027 * Author: Mark Wisner
wdenkba56f622004-02-06 23:19:44 +000028 *
29 * Change Activity-
30 *
Wolfgang Denk265817c2005-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).
wdenkba56f622004-02-06 23:19:44 +000070 *-----------------------------------------------------------------------------*
Wolfgang Denk265817c2005-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.
wdenkba56f622004-02-06 23:19:44 +000078 *-----------------------------------------------------------------------------*/
79
80#include <config.h>
wdenkba56f622004-02-06 23:19:44 +000081#include <common.h>
82#include <net.h>
83#include <asm/processor.h>
wdenkba56f622004-02-06 23:19:44 +000084#include <commproc.h>
Stefan Roesed6c61aa2005-08-16 18:18:00 +020085#include <ppc4xx.h>
86#include <ppc4xx_enet.h>
wdenkba56f622004-02-06 23:19:44 +000087#include <405_mal.h>
88#include <miiphy.h>
89#include <malloc.h>
90#include "vecnum.h"
91
Stefan Roesed6c61aa2005-08-16 18:18:00 +020092/*
Wolfgang Denk0c8721a2005-09-23 11:05:55 +020093 * Only compile for platform with AMCC EMAC ethernet controller and
Stefan Roesed6c61aa2005-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
wdenkba56f622004-02-06 23:19:44 +0000102
Stefan Roese1e25f952005-10-20 16:34:28 +0200103#if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_NET_MULTI)
104#error "CONFIG_NET_MULTI has to be defined for NetConsole"
105#endif
106
Wolfgang Denk265817c2005-09-25 00:53:22 +0200107#define EMAC_RESET_TIMEOUT 1000 /* 1000 ms reset timeout */
wdenkba56f622004-02-06 23:19:44 +0000108#define PHY_AUTONEGOTIATE_TIMEOUT 4000 /* 4000 ms autonegotiate timeout */
109
wdenkba56f622004-02-06 23:19:44 +0000110/* Ethernet Transmit and Receive Buffers */
111/* AS.HARNOIS
112 * In the same way ENET_MAX_MTU and ENET_MAX_MTU_ALIGNED are set from
113 * PKTSIZE and PKTSIZE_ALIGN (include/net.h)
114 */
Wolfgang Denk265817c2005-09-25 00:53:22 +0200115#define ENET_MAX_MTU PKTSIZE
wdenkba56f622004-02-06 23:19:44 +0000116#define ENET_MAX_MTU_ALIGNED PKTSIZE_ALIGN
117
wdenkba56f622004-02-06 23:19:44 +0000118/*-----------------------------------------------------------------------------+
119 * Defines for MAL/EMAC interrupt conditions as reported in the UIC (Universal
120 * Interrupt Controller).
121 *-----------------------------------------------------------------------------*/
122#define MAL_UIC_ERR ( UIC_MAL_SERR | UIC_MAL_TXDE | UIC_MAL_RXDE)
123#define MAL_UIC_DEF (UIC_MAL_RXEOB | MAL_UIC_ERR)
124#define EMAC_UIC_DEF UIC_ENET
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200125#define EMAC_UIC_DEF1 UIC_ENET1
126#define SEL_UIC_DEF(p) (p ? UIC_ENET1 : UIC_ENET )
wdenkba56f622004-02-06 23:19:44 +0000127
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200128#undef INFO_4XX_ENET
wdenkba56f622004-02-06 23:19:44 +0000129
Wolfgang Denk265817c2005-09-25 00:53:22 +0200130#define BI_PHYMODE_NONE 0
131#define BI_PHYMODE_ZMII 1
wdenk3c74e322004-02-22 23:46:08 +0000132#define BI_PHYMODE_RGMII 2
133
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200134
wdenkba56f622004-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 Roesed6c61aa2005-08-16 18:18:00 +0200141#if !defined(CONFIG_NET_MULTI)
Stefan Roese4f92ac32005-10-10 17:43:58 +0200142struct eth_device *emac0_dev = NULL;
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200143#endif
144
Stefan Roese1e25f952005-10-20 16:34:28 +0200145/*
146 * Get count of EMAC devices (doesn't have to be the max. possible number
147 * supported by the cpu)
148 */
149#if defined(CONFIG_HAS_ETH3)
150#define LAST_EMAC_NUM 4
151#elif defined(CONFIG_HAS_ETH2)
152#define LAST_EMAC_NUM 3
153#elif defined(CONFIG_HAS_ETH1)
154#define LAST_EMAC_NUM 2
155#else
156#define LAST_EMAC_NUM 1
157#endif
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200158
wdenkba56f622004-02-06 23:19:44 +0000159/*-----------------------------------------------------------------------------+
160 * Prototypes and externals.
161 *-----------------------------------------------------------------------------*/
162static void enet_rcv (struct eth_device *dev, unsigned long malisr);
163
164int enetInt (struct eth_device *dev);
165static void mal_err (struct eth_device *dev, unsigned long isr,
166 unsigned long uic, unsigned long maldef,
167 unsigned long mal_errr);
168static void emac_err (struct eth_device *dev, unsigned long isr);
169
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200170extern int phy_setup_aneg (char *devname, unsigned char addr);
171extern int emac4xx_miiphy_read (char *devname, unsigned char addr,
172 unsigned char reg, unsigned short *value);
173extern int emac4xx_miiphy_write (char *devname, unsigned char addr,
174 unsigned char reg, unsigned short value);
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200175
wdenkba56f622004-02-06 23:19:44 +0000176/*-----------------------------------------------------------------------------+
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200177| ppc_4xx_eth_halt
wdenkba56f622004-02-06 23:19:44 +0000178| Disable MAL channel, and EMACn
wdenkba56f622004-02-06 23:19:44 +0000179+-----------------------------------------------------------------------------*/
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200180static void ppc_4xx_eth_halt (struct eth_device *dev)
wdenkba56f622004-02-06 23:19:44 +0000181{
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200182 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenkba56f622004-02-06 23:19:44 +0000183 uint32_t failsafe = 10000;
184
185 out32 (EMAC_IER + hw_p->hw_addr, 0x00000000); /* disable emac interrupts */
186
187 /* 1st reset MAL channel */
188 /* Note: writing a 0 to a channel has no effect */
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200189#if defined(CONFIG_405EP) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
190 mtdcr (maltxcarr, (MAL_CR_MMSR >> (hw_p->devnum * 2)));
191#else
wdenkba56f622004-02-06 23:19:44 +0000192 mtdcr (maltxcarr, (MAL_CR_MMSR >> hw_p->devnum));
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200193#endif
wdenkba56f622004-02-06 23:19:44 +0000194 mtdcr (malrxcarr, (MAL_CR_MMSR >> hw_p->devnum));
195
196 /* wait for reset */
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200197 while (mfdcr (malrxcasr) & (MAL_CR_MMSR >> hw_p->devnum)) {
wdenkba56f622004-02-06 23:19:44 +0000198 udelay (1000); /* Delay 1 MS so as not to hammer the register */
199 failsafe--;
200 if (failsafe == 0)
201 break;
wdenkba56f622004-02-06 23:19:44 +0000202 }
203
204 /* EMAC RESET */
205 out32 (EMAC_M0 + hw_p->hw_addr, EMAC_M0_SRST);
206
Stefan Roesea93316c2005-10-18 19:17:12 +0200207#ifndef CONFIG_NETCONSOLE
Stefan Roesec157d8e2005-08-01 16:41:48 +0200208 hw_p->print_speed = 1; /* print speed message again next time */
Stefan Roesea93316c2005-10-18 19:17:12 +0200209#endif
Stefan Roesec157d8e2005-08-01 16:41:48 +0200210
wdenkba56f622004-02-06 23:19:44 +0000211 return;
212}
213
Stefan Roese846b0dd2005-08-08 12:42:22 +0200214#if defined (CONFIG_440GX)
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200215int ppc_4xx_eth_setup_bridge(int devnum, bd_t * bis)
wdenk855a4962004-03-14 18:23:55 +0000216{
217 unsigned long pfc1;
218 unsigned long zmiifer;
219 unsigned long rmiifer;
220
221 mfsdr(sdr_pfc1, pfc1);
222 pfc1 = SDR0_PFC1_EPS_DECODE(pfc1);
223
224 zmiifer = 0;
225 rmiifer = 0;
226
227 switch (pfc1) {
228 case 1:
229 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
230 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(1);
231 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(2);
232 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(3);
233 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
234 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
235 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
236 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
237 break;
238 case 2:
239 zmiifer = ZMII_FER_SMII << ZMII_FER_V(0);
240 zmiifer = ZMII_FER_SMII << ZMII_FER_V(1);
241 zmiifer = ZMII_FER_SMII << ZMII_FER_V(2);
242 zmiifer = ZMII_FER_SMII << ZMII_FER_V(3);
243 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
244 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
245 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
246 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
247 break;
248 case 3:
249 zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
250 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
251 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
252 bis->bi_phymode[1] = BI_PHYMODE_NONE;
253 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
254 bis->bi_phymode[3] = BI_PHYMODE_NONE;
255 break;
256 case 4:
257 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(0);
258 zmiifer |= ZMII_FER_SMII << ZMII_FER_V(1);
259 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V (2);
260 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V (3);
261 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
262 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
263 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
264 bis->bi_phymode[3] = BI_PHYMODE_RGMII;
265 break;
266 case 5:
267 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (0);
268 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (1);
269 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (2);
270 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(3);
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_RGMII;
275 break;
276 case 6:
277 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (0);
278 zmiifer |= ZMII_FER_SMII << ZMII_FER_V (1);
279 rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
wdenk855a4962004-03-14 18:23:55 +0000280 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
281 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
282 bis->bi_phymode[2] = BI_PHYMODE_RGMII;
wdenk855a4962004-03-14 18:23:55 +0000283 break;
284 case 0:
285 default:
286 zmiifer = ZMII_FER_MII << ZMII_FER_V(devnum);
287 rmiifer = 0x0;
288 bis->bi_phymode[0] = BI_PHYMODE_ZMII;
289 bis->bi_phymode[1] = BI_PHYMODE_ZMII;
290 bis->bi_phymode[2] = BI_PHYMODE_ZMII;
291 bis->bi_phymode[3] = BI_PHYMODE_ZMII;
292 break;
293 }
294
295 /* Ensure we setup mdio for this devnum and ONLY this devnum */
296 zmiifer |= (ZMII_FER_MDI) << ZMII_FER_V(devnum);
297
298 out32 (ZMII_FER, zmiifer);
299 out32 (RGMII_FER, rmiifer);
300
301 return ((int)pfc1);
302
303}
304#endif
305
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200306static int ppc_4xx_eth_init (struct eth_device *dev, bd_t * bis)
wdenkba56f622004-02-06 23:19:44 +0000307{
Stefan Roeseb79316f2005-08-15 12:31:23 +0200308 int i, j;
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200309 unsigned long reg = 0;
wdenkba56f622004-02-06 23:19:44 +0000310 unsigned long msr;
311 unsigned long speed;
312 unsigned long duplex;
313 unsigned long failsafe;
314 unsigned mode_reg;
315 unsigned short devnum;
316 unsigned short reg_short;
Stefan Roese6e7fb6e2005-11-29 18:18:21 +0100317#if defined(CONFIG_440GX) || defined(CONFIG_440SP)
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200318 sys_info_t sysinfo;
Stefan Roese6e7fb6e2005-11-29 18:18:21 +0100319#if defined(CONFIG_440GX)
320 int ethgroup = -1;
321#endif
Stefan Roesec157d8e2005-08-01 16:41:48 +0200322#endif
wdenkba56f622004-02-06 23:19:44 +0000323
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200324 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenkba56f622004-02-06 23:19:44 +0000325
326 /* before doing anything, figure out if we have a MAC address */
327 /* if not, bail */
Stefan Roese4f92ac32005-10-10 17:43:58 +0200328 if (memcmp (dev->enetaddr, "\0\0\0\0\0\0", 6) == 0) {
329 printf("ERROR: ethaddr not set!\n");
wdenkba56f622004-02-06 23:19:44 +0000330 return -1;
Stefan Roese4f92ac32005-10-10 17:43:58 +0200331 }
wdenkba56f622004-02-06 23:19:44 +0000332
Stefan Roese6e7fb6e2005-11-29 18:18:21 +0100333#if defined(CONFIG_440GX) || defined(CONFIG_440SP)
wdenkba56f622004-02-06 23:19:44 +0000334 /* Need to get the OPB frequency so we can access the PHY */
335 get_sys_info (&sysinfo);
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200336#endif
wdenkba56f622004-02-06 23:19:44 +0000337
wdenkba56f622004-02-06 23:19:44 +0000338 msr = mfmsr ();
339 mtmsr (msr & ~(MSR_EE)); /* disable interrupts */
340
341 devnum = hw_p->devnum;
342
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200343#ifdef INFO_4XX_ENET
wdenkba56f622004-02-06 23:19:44 +0000344 /* AS.HARNOIS
345 * We should have :
Wolfgang Denk265817c2005-09-25 00:53:22 +0200346 * hw_p->stats.pkts_handled <= hw_p->stats.pkts_rx <= hw_p->stats.pkts_handled+PKTBUFSRX
wdenkba56f622004-02-06 23:19:44 +0000347 * In the most cases hw_p->stats.pkts_handled = hw_p->stats.pkts_rx, but it
348 * is possible that new packets (without relationship with
349 * current transfer) have got the time to arrived before
350 * netloop calls eth_halt
351 */
352 printf ("About preceeding transfer (eth%d):\n"
353 "- Sent packet number %d\n"
354 "- Received packet number %d\n"
355 "- Handled packet number %d\n",
356 hw_p->devnum,
357 hw_p->stats.pkts_tx,
358 hw_p->stats.pkts_rx, hw_p->stats.pkts_handled);
359
360 hw_p->stats.pkts_tx = 0;
361 hw_p->stats.pkts_rx = 0;
362 hw_p->stats.pkts_handled = 0;
363#endif
364
Wolfgang Denk265817c2005-09-25 00:53:22 +0200365 hw_p->tx_err_index = 0; /* Transmit Error Index for tx_err_log */
366 hw_p->rx_err_index = 0; /* Receive Error Index for rx_err_log */
wdenkba56f622004-02-06 23:19:44 +0000367
368 hw_p->rx_slot = 0; /* MAL Receive Slot */
369 hw_p->rx_i_index = 0; /* Receive Interrupt Queue Index */
370 hw_p->rx_u_index = 0; /* Receive User Queue Index */
371
372 hw_p->tx_slot = 0; /* MAL Transmit Slot */
373 hw_p->tx_i_index = 0; /* Transmit Interrupt Queue Index */
374 hw_p->tx_u_index = 0; /* Transmit User Queue Index */
375
Stefan Roese6e7fb6e2005-11-29 18:18:21 +0100376#if defined(CONFIG_440) && !defined(CONFIG_440SP)
wdenkba56f622004-02-06 23:19:44 +0000377 /* set RMII mode */
378 /* NOTE: 440GX spec states that mode is mutually exclusive */
379 /* NOTE: Therefore, disable all other EMACS, since we handle */
380 /* NOTE: only one emac at a time */
381 reg = 0;
382 out32 (ZMII_FER, 0);
383 udelay (100);
wdenkba56f622004-02-06 23:19:44 +0000384
Stefan Roese846b0dd2005-08-08 12:42:22 +0200385#if defined(CONFIG_440EP) || defined(CONFIG_440GR)
Wolfgang Denk265817c2005-09-25 00:53:22 +0200386 out32 (ZMII_FER, (ZMII_FER_RMII | ZMII_FER_MDI) << ZMII_FER_V (devnum));
Stefan Roese846b0dd2005-08-08 12:42:22 +0200387#elif defined(CONFIG_440GX)
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200388 ethgroup = ppc_4xx_eth_setup_bridge(devnum, bis);
Stefan Roese4a3cd9e2005-09-07 16:21:12 +0200389#elif defined(CONFIG_440GP)
390 /* set RMII mode */
391 out32 (ZMII_FER, ZMII_RMII | ZMII_MDI0);
wdenk0e6d7982004-03-14 00:07:33 +0000392#else
393 if ((devnum == 0) || (devnum == 1)) {
394 out32 (ZMII_FER, (ZMII_FER_SMII | ZMII_FER_MDI) << ZMII_FER_V (devnum));
395 }
396 else { /* ((devnum == 2) || (devnum == 3)) */
397 out32 (ZMII_FER, ZMII_FER_MDI << ZMII_FER_V (devnum));
wdenkba56f622004-02-06 23:19:44 +0000398 out32 (RGMII_FER, ((RGMII_FER_RGMII << RGMII_FER_V (2)) |
399 (RGMII_FER_RGMII << RGMII_FER_V (3))));
wdenk0e6d7982004-03-14 00:07:33 +0000400 }
401#endif
Stefan Roesec57c7982005-08-11 17:56:56 +0200402
wdenk0e6d7982004-03-14 00:07:33 +0000403 out32 (ZMII_SSR, ZMII_SSR_SP << ZMII_SSR_V(devnum));
Stefan Roese6e7fb6e2005-11-29 18:18:21 +0100404#endif /* defined(CONFIG_440) && !defined(CONFIG_440SP) */
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200405
wdenk0e6d7982004-03-14 00:07:33 +0000406 __asm__ volatile ("eieio");
407
408 /* reset emac so we have access to the phy */
409
410 out32 (EMAC_M0 + hw_p->hw_addr, EMAC_M0_SRST);
wdenkba56f622004-02-06 23:19:44 +0000411 __asm__ volatile ("eieio");
412
413 failsafe = 1000;
414 while ((in32 (EMAC_M0 + hw_p->hw_addr) & (EMAC_M0_SRST)) && failsafe) {
415 udelay (1000);
416 failsafe--;
417 }
418
Stefan Roese6e7fb6e2005-11-29 18:18:21 +0100419#if defined(CONFIG_440GX) || defined(CONFIG_440SP)
wdenkba56f622004-02-06 23:19:44 +0000420 /* Whack the M1 register */
421 mode_reg = 0x0;
422 mode_reg &= ~0x00000038;
423 if (sysinfo.freqOPB <= 50000000);
424 else if (sysinfo.freqOPB <= 66666667)
425 mode_reg |= EMAC_M1_OBCI_66;
426 else if (sysinfo.freqOPB <= 83333333)
427 mode_reg |= EMAC_M1_OBCI_83;
428 else if (sysinfo.freqOPB <= 100000000)
429 mode_reg |= EMAC_M1_OBCI_100;
430 else
431 mode_reg |= EMAC_M1_OBCI_GT100;
432
433 out32 (EMAC_M1 + hw_p->hw_addr, mode_reg);
Stefan Roese6e7fb6e2005-11-29 18:18:21 +0100434#endif /* defined(CONFIG_440GX) || defined(CONFIG_440SP) */
wdenkba56f622004-02-06 23:19:44 +0000435
436 /* wait for PHY to complete auto negotiation */
437 reg_short = 0;
438#ifndef CONFIG_CS8952_PHY
439 switch (devnum) {
440 case 0:
441 reg = CONFIG_PHY_ADDR;
442 break;
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200443#if defined (CONFIG_PHY1_ADDR)
wdenkba56f622004-02-06 23:19:44 +0000444 case 1:
445 reg = CONFIG_PHY1_ADDR;
446 break;
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200447#endif
Stefan Roese846b0dd2005-08-08 12:42:22 +0200448#if defined (CONFIG_440GX)
wdenkba56f622004-02-06 23:19:44 +0000449 case 2:
450 reg = CONFIG_PHY2_ADDR;
451 break;
452 case 3:
453 reg = CONFIG_PHY3_ADDR;
454 break;
455#endif
456 default:
457 reg = CONFIG_PHY_ADDR;
458 break;
459 }
460
wdenk3c74e322004-02-22 23:46:08 +0000461 bis->bi_phynum[devnum] = reg;
462
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200463#if defined(CONFIG_PHY_RESET)
wdenka06752e2004-09-29 22:43:59 +0000464 /*
465 * Reset the phy, only if its the first time through
466 * otherwise, just check the speeds & feeds
467 */
468 if (hw_p->first_init == 0) {
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200469 miiphy_reset (dev->name, reg);
wdenkba56f622004-02-06 23:19:44 +0000470
Stefan Roese6e7fb6e2005-11-29 18:18:21 +0100471#if defined(CONFIG_440GX) || defined(CONFIG_440SP)
wdenk0e6d7982004-03-14 00:07:33 +0000472#if defined(CONFIG_CIS8201_PHY)
wdenkfc1cfcd2004-04-25 15:41:35 +0000473 /*
Stefan Roese17f50f222005-08-04 17:09:16 +0200474 * Cicada 8201 PHY needs to have an extended register whacked
475 * for RGMII mode.
wdenkfc1cfcd2004-04-25 15:41:35 +0000476 */
Stefan Roese17f50f222005-08-04 17:09:16 +0200477 if ( ((devnum == 2) || (devnum ==3)) && (4 == ethgroup) ) {
Stefan Roeseb79316f2005-08-15 12:31:23 +0200478#if defined(CONFIG_CIS8201_SHORT_ETCH)
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200479 miiphy_write (dev->name, reg, 23, 0x1300);
Stefan Roeseb79316f2005-08-15 12:31:23 +0200480#else
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200481 miiphy_write (dev->name, reg, 23, 0x1000);
Stefan Roeseb79316f2005-08-15 12:31:23 +0200482#endif
Stefan Roese17f50f222005-08-04 17:09:16 +0200483 /*
484 * Vitesse VSC8201/Cicada CIS8201 errata:
485 * Interoperability problem with Intel 82547EI phys
486 * This work around (provided by Vitesse) changes
487 * the default timer convergence from 8ms to 12ms
488 */
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200489 miiphy_write (dev->name, reg, 0x1f, 0x2a30);
490 miiphy_write (dev->name, reg, 0x08, 0x0200);
491 miiphy_write (dev->name, reg, 0x1f, 0x52b5);
492 miiphy_write (dev->name, reg, 0x02, 0x0004);
493 miiphy_write (dev->name, reg, 0x01, 0x0671);
494 miiphy_write (dev->name, reg, 0x00, 0x8fae);
495 miiphy_write (dev->name, reg, 0x1f, 0x2a30);
496 miiphy_write (dev->name, reg, 0x08, 0x0000);
497 miiphy_write (dev->name, reg, 0x1f, 0x0000);
Stefan Roese17f50f222005-08-04 17:09:16 +0200498 /* end Vitesse/Cicada errata */
499 }
wdenk0e6d7982004-03-14 00:07:33 +0000500#endif
wdenk855a4962004-03-14 18:23:55 +0000501#endif
wdenka06752e2004-09-29 22:43:59 +0000502 /* Start/Restart autonegotiation */
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200503 phy_setup_aneg (dev->name, reg);
wdenka06752e2004-09-29 22:43:59 +0000504 udelay (1000);
505 }
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200506#endif /* defined(CONFIG_PHY_RESET) */
wdenkba56f622004-02-06 23:19:44 +0000507
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200508 miiphy_read (dev->name, reg, PHY_BMSR, &reg_short);
wdenkba56f622004-02-06 23:19:44 +0000509
510 /*
wdenk0e6d7982004-03-14 00:07:33 +0000511 * Wait if PHY is capable of autonegotiation and autonegotiation is not complete
wdenkba56f622004-02-06 23:19:44 +0000512 */
513 if ((reg_short & PHY_BMSR_AUTN_ABLE)
514 && !(reg_short & PHY_BMSR_AUTN_COMP)) {
515 puts ("Waiting for PHY auto negotiation to complete");
516 i = 0;
517 while (!(reg_short & PHY_BMSR_AUTN_COMP)) {
518 /*
519 * Timeout reached ?
520 */
521 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
522 puts (" TIMEOUT !\n");
523 break;
524 }
525
526 if ((i++ % 1000) == 0) {
527 putc ('.');
528 }
529 udelay (1000); /* 1 ms */
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200530 miiphy_read (dev->name, reg, PHY_BMSR, &reg_short);
wdenkba56f622004-02-06 23:19:44 +0000531
532 }
533 puts (" done\n");
534 udelay (500000); /* another 500 ms (results in faster booting) */
535 }
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200536#endif /* #ifndef CONFIG_CS8952_PHY */
537
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200538 speed = miiphy_speed (dev->name, reg);
539 duplex = miiphy_duplex (dev->name, reg);
wdenkba56f622004-02-06 23:19:44 +0000540
541 if (hw_p->print_speed) {
542 hw_p->print_speed = 0;
543 printf ("ENET Speed is %d Mbps - %s duplex connection\n",
544 (int) speed, (duplex == HALF) ? "HALF" : "FULL");
545 }
546
Stefan Roese6e7fb6e2005-11-29 18:18:21 +0100547#if defined(CONFIG_440) && !defined(CONFIG_440SP)
Stefan Roese846b0dd2005-08-08 12:42:22 +0200548#if defined(CONFIG_440EP) || defined(CONFIG_440GR)
Stefan Roesec157d8e2005-08-01 16:41:48 +0200549 mfsdr(sdr_mfr, reg);
550 if (speed == 100) {
551 reg = (reg & ~SDR0_MFR_ZMII_MODE_MASK) | SDR0_MFR_ZMII_MODE_RMII_100M;
552 } else {
553 reg = (reg & ~SDR0_MFR_ZMII_MODE_MASK) | SDR0_MFR_ZMII_MODE_RMII_10M;
554 }
555 mtsdr(sdr_mfr, reg);
556#endif
Stefan Roesec57c7982005-08-11 17:56:56 +0200557
wdenkba56f622004-02-06 23:19:44 +0000558 /* Set ZMII/RGMII speed according to the phy link speed */
559 reg = in32 (ZMII_SSR);
wdenk855a4962004-03-14 18:23:55 +0000560 if ( (speed == 100) || (speed == 1000) )
wdenkba56f622004-02-06 23:19:44 +0000561 out32 (ZMII_SSR, reg | (ZMII_SSR_SP << ZMII_SSR_V (devnum)));
562 else
Stefan Roesec57c7982005-08-11 17:56:56 +0200563 out32 (ZMII_SSR, reg & (~(ZMII_SSR_SP << ZMII_SSR_V (devnum))));
wdenkba56f622004-02-06 23:19:44 +0000564
565 if ((devnum == 2) || (devnum == 3)) {
566 if (speed == 1000)
567 reg = (RGMII_SSR_SP_1000MBPS << RGMII_SSR_V (devnum));
568 else if (speed == 100)
569 reg = (RGMII_SSR_SP_100MBPS << RGMII_SSR_V (devnum));
570 else
571 reg = (RGMII_SSR_SP_10MBPS << RGMII_SSR_V (devnum));
572
573 out32 (RGMII_SSR, reg);
574 }
Stefan Roese6e7fb6e2005-11-29 18:18:21 +0100575#endif /* defined(CONFIG_440) && !defined(CONFIG_440SP) */
wdenkba56f622004-02-06 23:19:44 +0000576
577 /* set the Mal configuration reg */
Stefan Roese6e7fb6e2005-11-29 18:18:21 +0100578#if defined(CONFIG_440GX) || defined(CONFIG_440SP)
Stefan Roese17f50f222005-08-04 17:09:16 +0200579 mtdcr (malmcr, MAL_CR_PLBB | MAL_CR_OPBBL | MAL_CR_LEA |
580 MAL_CR_PLBLT_DEFAULT | MAL_CR_EOPIE | 0x00330000);
581#else
582 mtdcr (malmcr, MAL_CR_PLBB | MAL_CR_OPBBL | MAL_CR_LEA | MAL_CR_PLBLT_DEFAULT);
wdenkba56f622004-02-06 23:19:44 +0000583 /* Errata 1.12: MAL_1 -- Disable MAL bursting */
Stefan Roese17f50f222005-08-04 17:09:16 +0200584 if (get_pvr() == PVR_440GP_RB) {
585 mtdcr (malmcr, mfdcr(malmcr) & ~MAL_CR_PLBB);
586 }
587#endif
wdenkba56f622004-02-06 23:19:44 +0000588
589 /* Free "old" buffers */
590 if (hw_p->alloc_tx_buf)
591 free (hw_p->alloc_tx_buf);
592 if (hw_p->alloc_rx_buf)
593 free (hw_p->alloc_rx_buf);
594
595 /*
596 * Malloc MAL buffer desciptors, make sure they are
597 * aligned on cache line boundary size
598 * (401/403/IOP480 = 16, 405 = 32)
599 * and doesn't cross cache block boundaries.
600 */
601 hw_p->alloc_tx_buf =
602 (mal_desc_t *) malloc ((sizeof (mal_desc_t) * NUM_TX_BUFF) +
603 ((2 * CFG_CACHELINE_SIZE) - 2));
Stefan Roeseb79316f2005-08-15 12:31:23 +0200604 if (NULL == hw_p->alloc_tx_buf)
605 return -1;
wdenkba56f622004-02-06 23:19:44 +0000606 if (((int) hw_p->alloc_tx_buf & CACHELINE_MASK) != 0) {
607 hw_p->tx =
608 (mal_desc_t *) ((int) hw_p->alloc_tx_buf +
609 CFG_CACHELINE_SIZE -
610 ((int) hw_p->
611 alloc_tx_buf & CACHELINE_MASK));
612 } else {
613 hw_p->tx = hw_p->alloc_tx_buf;
614 }
615
616 hw_p->alloc_rx_buf =
617 (mal_desc_t *) malloc ((sizeof (mal_desc_t) * NUM_RX_BUFF) +
618 ((2 * CFG_CACHELINE_SIZE) - 2));
Stefan Roeseb79316f2005-08-15 12:31:23 +0200619 if (NULL == hw_p->alloc_rx_buf) {
620 free(hw_p->alloc_tx_buf);
621 hw_p->alloc_tx_buf = NULL;
622 return -1;
623 }
624
wdenkba56f622004-02-06 23:19:44 +0000625 if (((int) hw_p->alloc_rx_buf & CACHELINE_MASK) != 0) {
626 hw_p->rx =
627 (mal_desc_t *) ((int) hw_p->alloc_rx_buf +
628 CFG_CACHELINE_SIZE -
629 ((int) hw_p->
630 alloc_rx_buf & CACHELINE_MASK));
631 } else {
632 hw_p->rx = hw_p->alloc_rx_buf;
633 }
634
635 for (i = 0; i < NUM_TX_BUFF; i++) {
636 hw_p->tx[i].ctrl = 0;
637 hw_p->tx[i].data_len = 0;
Stefan Roeseb79316f2005-08-15 12:31:23 +0200638 if (hw_p->first_init == 0) {
wdenkba56f622004-02-06 23:19:44 +0000639 hw_p->txbuf_ptr =
640 (char *) malloc (ENET_MAX_MTU_ALIGNED);
Stefan Roeseb79316f2005-08-15 12:31:23 +0200641 if (NULL == hw_p->txbuf_ptr) {
642 free(hw_p->alloc_rx_buf);
643 free(hw_p->alloc_tx_buf);
644 hw_p->alloc_rx_buf = NULL;
645 hw_p->alloc_tx_buf = NULL;
646 for(j = 0; j < i; j++) {
647 free(hw_p->tx[i].data_ptr);
648 hw_p->tx[i].data_ptr = NULL;
649 }
650 }
651 }
wdenkba56f622004-02-06 23:19:44 +0000652 hw_p->tx[i].data_ptr = hw_p->txbuf_ptr;
653 if ((NUM_TX_BUFF - 1) == i)
654 hw_p->tx[i].ctrl |= MAL_TX_CTRL_WRAP;
655 hw_p->tx_run[i] = -1;
656#if 0
657 printf ("TX_BUFF %d @ 0x%08lx\n", i,
658 (ulong) hw_p->tx[i].data_ptr);
659#endif
660 }
661
662 for (i = 0; i < NUM_RX_BUFF; i++) {
663 hw_p->rx[i].ctrl = 0;
664 hw_p->rx[i].data_len = 0;
Wolfgang Denk265817c2005-09-25 00:53:22 +0200665 /* rx[i].data_ptr = (char *) &rx_buff[i]; */
wdenkba56f622004-02-06 23:19:44 +0000666 hw_p->rx[i].data_ptr = (char *) NetRxPackets[i];
667 if ((NUM_RX_BUFF - 1) == i)
668 hw_p->rx[i].ctrl |= MAL_RX_CTRL_WRAP;
669 hw_p->rx[i].ctrl |= MAL_RX_CTRL_EMPTY | MAL_RX_CTRL_INTR;
670 hw_p->rx_ready[i] = -1;
671#if 0
672 printf ("RX_BUFF %d @ 0x%08lx\n", i, (ulong) rx[i].data_ptr);
673#endif
674 }
675
676 reg = 0x00000000;
677
678 reg |= dev->enetaddr[0]; /* set high address */
679 reg = reg << 8;
680 reg |= dev->enetaddr[1];
681
682 out32 (EMAC_IAH + hw_p->hw_addr, reg);
683
684 reg = 0x00000000;
685 reg |= dev->enetaddr[2]; /* set low address */
686 reg = reg << 8;
687 reg |= dev->enetaddr[3];
688 reg = reg << 8;
689 reg |= dev->enetaddr[4];
690 reg = reg << 8;
691 reg |= dev->enetaddr[5];
692
693 out32 (EMAC_IAL + hw_p->hw_addr, reg);
694
695 switch (devnum) {
696 case 1:
697 /* setup MAL tx & rx channel pointers */
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200698#if defined (CONFIG_405EP) || defined (CONFIG_440EP) || defined (CONFIG_440GR)
Stefan Roesec157d8e2005-08-01 16:41:48 +0200699 mtdcr (maltxctp2r, hw_p->tx);
700#else
wdenkba56f622004-02-06 23:19:44 +0000701 mtdcr (maltxctp1r, hw_p->tx);
Stefan Roesec157d8e2005-08-01 16:41:48 +0200702#endif
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200703#if defined(CONFIG_440)
Stefan Roesec157d8e2005-08-01 16:41:48 +0200704 mtdcr (maltxbattr, 0x0);
wdenkba56f622004-02-06 23:19:44 +0000705 mtdcr (malrxbattr, 0x0);
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200706#endif
wdenkba56f622004-02-06 23:19:44 +0000707 mtdcr (malrxctp1r, hw_p->rx);
708 /* set RX buffer size */
709 mtdcr (malrcbs1, ENET_MAX_MTU_ALIGNED / 16);
710 break;
Stefan Roese846b0dd2005-08-08 12:42:22 +0200711#if defined (CONFIG_440GX)
wdenkba56f622004-02-06 23:19:44 +0000712 case 2:
713 /* setup MAL tx & rx channel pointers */
714 mtdcr (maltxbattr, 0x0);
wdenkba56f622004-02-06 23:19:44 +0000715 mtdcr (malrxbattr, 0x0);
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200716 mtdcr (maltxctp2r, hw_p->tx);
wdenkba56f622004-02-06 23:19:44 +0000717 mtdcr (malrxctp2r, hw_p->rx);
718 /* set RX buffer size */
719 mtdcr (malrcbs2, ENET_MAX_MTU_ALIGNED / 16);
720 break;
721 case 3:
722 /* setup MAL tx & rx channel pointers */
723 mtdcr (maltxbattr, 0x0);
724 mtdcr (maltxctp3r, hw_p->tx);
725 mtdcr (malrxbattr, 0x0);
726 mtdcr (malrxctp3r, hw_p->rx);
727 /* set RX buffer size */
728 mtdcr (malrcbs3, ENET_MAX_MTU_ALIGNED / 16);
729 break;
Stefan Roesec57c7982005-08-11 17:56:56 +0200730#endif /* CONFIG_440GX */
wdenkba56f622004-02-06 23:19:44 +0000731 case 0:
732 default:
733 /* setup MAL tx & rx channel pointers */
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200734#if defined(CONFIG_440)
wdenkba56f622004-02-06 23:19:44 +0000735 mtdcr (maltxbattr, 0x0);
wdenkba56f622004-02-06 23:19:44 +0000736 mtdcr (malrxbattr, 0x0);
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200737#endif
738 mtdcr (maltxctp0r, hw_p->tx);
wdenkba56f622004-02-06 23:19:44 +0000739 mtdcr (malrxctp0r, hw_p->rx);
740 /* set RX buffer size */
741 mtdcr (malrcbs0, ENET_MAX_MTU_ALIGNED / 16);
742 break;
743 }
744
745 /* Enable MAL transmit and receive channels */
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200746#if defined(CONFIG_405EP) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
Stefan Roesec157d8e2005-08-01 16:41:48 +0200747 mtdcr (maltxcasr, (MAL_TXRX_CASR >> (hw_p->devnum*2)));
748#else
wdenkba56f622004-02-06 23:19:44 +0000749 mtdcr (maltxcasr, (MAL_TXRX_CASR >> hw_p->devnum));
Stefan Roesec157d8e2005-08-01 16:41:48 +0200750#endif
wdenkba56f622004-02-06 23:19:44 +0000751 mtdcr (malrxcasr, (MAL_TXRX_CASR >> hw_p->devnum));
752
753 /* set transmit enable & receive enable */
754 out32 (EMAC_M0 + hw_p->hw_addr, EMAC_M0_TXE | EMAC_M0_RXE);
755
756 /* set receive fifo to 4k and tx fifo to 2k */
757 mode_reg = in32 (EMAC_M1 + hw_p->hw_addr);
758 mode_reg |= EMAC_M1_RFS_4K | EMAC_M1_TX_FIFO_2K;
759
760 /* set speed */
Stefan Roese6e7fb6e2005-11-29 18:18:21 +0100761 if (speed == _1000BASET) {
762#if defined(CONFIG_440SP)
763#define SDR0_PFC1_EM_1000 0x00200000
764 unsigned long pfc1;
765 mfsdr (sdr_pfc1, pfc1);
766 pfc1 |= SDR0_PFC1_EM_1000;
767 mtsdr (sdr_pfc1, pfc1);
768#endif
wdenk855a4962004-03-14 18:23:55 +0000769 mode_reg = mode_reg | EMAC_M1_MF_1000MBPS | EMAC_M1_IST;
Stefan Roese6e7fb6e2005-11-29 18:18:21 +0100770 } else if (speed == _100BASET)
wdenkba56f622004-02-06 23:19:44 +0000771 mode_reg = mode_reg | EMAC_M1_MF_100MBPS | EMAC_M1_IST;
772 else
773 mode_reg = mode_reg & ~0x00C00000; /* 10 MBPS */
774 if (duplex == FULL)
775 mode_reg = mode_reg | 0x80000000 | EMAC_M1_IST;
776
777 out32 (EMAC_M1 + hw_p->hw_addr, mode_reg);
778
779 /* Enable broadcast and indvidual address */
780 /* TBS: enabling runts as some misbehaved nics will send runts */
781 out32 (EMAC_RXM + hw_p->hw_addr, EMAC_RMR_BAE | EMAC_RMR_IAE);
782
783 /* we probably need to set the tx mode1 reg? maybe at tx time */
784
785 /* set transmit request threshold register */
786 out32 (EMAC_TRTR + hw_p->hw_addr, 0x18000000); /* 256 byte threshold */
787
Wolfgang Denk265817c2005-09-25 00:53:22 +0200788 /* set receive low/high water mark register */
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200789#if defined(CONFIG_440)
wdenkba56f622004-02-06 23:19:44 +0000790 /* 440GP has a 64 byte burst length */
791 out32 (EMAC_RX_HI_LO_WMARK + hw_p->hw_addr, 0x80009000);
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200792#else
793 /* 405s have a 16 byte burst length */
794 out32 (EMAC_RX_HI_LO_WMARK + hw_p->hw_addr, 0x0f002000);
795#endif /* defined(CONFIG_440) */
wdenkba56f622004-02-06 23:19:44 +0000796 out32 (EMAC_TXM1 + hw_p->hw_addr, 0xf8640000);
797
798 /* Set fifo limit entry in tx mode 0 */
799 out32 (EMAC_TXM0 + hw_p->hw_addr, 0x00000003);
800 /* Frame gap set */
801 out32 (EMAC_I_FRAME_GAP_REG + hw_p->hw_addr, 0x00000008);
802
803 /* Set EMAC IER */
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200804 hw_p->emac_ier = EMAC_ISR_PTLE | EMAC_ISR_BFCS | EMAC_ISR_ORE | EMAC_ISR_IRE;
wdenkba56f622004-02-06 23:19:44 +0000805 if (speed == _100BASET)
806 hw_p->emac_ier = hw_p->emac_ier | EMAC_ISR_SYE;
807
808 out32 (EMAC_ISR + hw_p->hw_addr, 0xffffffff); /* clear pending interrupts */
809 out32 (EMAC_IER + hw_p->hw_addr, hw_p->emac_ier);
810
811 if (hw_p->first_init == 0) {
812 /*
813 * Connect interrupt service routines
814 */
wdenkba56f622004-02-06 23:19:44 +0000815 irq_install_handler (VECNUM_ETH0 + (hw_p->devnum * 2),
816 (interrupt_handler_t *) enetInt, dev);
817 }
wdenkba56f622004-02-06 23:19:44 +0000818
819 mtmsr (msr); /* enable interrupts again */
820
821 hw_p->bis = bis;
822 hw_p->first_init = 1;
823
824 return (1);
825}
826
827
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200828static int ppc_4xx_eth_send (struct eth_device *dev, volatile void *ptr,
wdenkba56f622004-02-06 23:19:44 +0000829 int len)
830{
831 struct enet_frame *ef_ptr;
832 ulong time_start, time_now;
833 unsigned long temp_txm0;
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200834 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenkba56f622004-02-06 23:19:44 +0000835
836 ef_ptr = (struct enet_frame *) ptr;
837
838 /*-----------------------------------------------------------------------+
839 * Copy in our address into the frame.
840 *-----------------------------------------------------------------------*/
841 (void) memcpy (ef_ptr->source_addr, dev->enetaddr, ENET_ADDR_LENGTH);
842
843 /*-----------------------------------------------------------------------+
844 * If frame is too long or too short, modify length.
845 *-----------------------------------------------------------------------*/
846 /* TBS: where does the fragment go???? */
847 if (len > ENET_MAX_MTU)
848 len = ENET_MAX_MTU;
849
850 /* memcpy ((void *) &tx_buff[tx_slot], (const void *) ptr, len); */
851 memcpy ((void *) hw_p->txbuf_ptr, (const void *) ptr, len);
852
853 /*-----------------------------------------------------------------------+
854 * set TX Buffer busy, and send it
855 *-----------------------------------------------------------------------*/
856 hw_p->tx[hw_p->tx_slot].ctrl = (MAL_TX_CTRL_LAST |
857 EMAC_TX_CTRL_GFCS | EMAC_TX_CTRL_GP) &
858 ~(EMAC_TX_CTRL_ISA | EMAC_TX_CTRL_RSA);
859 if ((NUM_TX_BUFF - 1) == hw_p->tx_slot)
860 hw_p->tx[hw_p->tx_slot].ctrl |= MAL_TX_CTRL_WRAP;
861
862 hw_p->tx[hw_p->tx_slot].data_len = (short) len;
863 hw_p->tx[hw_p->tx_slot].ctrl |= MAL_TX_CTRL_READY;
864
865 __asm__ volatile ("eieio");
866
867 out32 (EMAC_TXM0 + hw_p->hw_addr,
868 in32 (EMAC_TXM0 + hw_p->hw_addr) | EMAC_TXM0_GNP0);
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200869#ifdef INFO_4XX_ENET
wdenkba56f622004-02-06 23:19:44 +0000870 hw_p->stats.pkts_tx++;
871#endif
872
873 /*-----------------------------------------------------------------------+
874 * poll unitl the packet is sent and then make sure it is OK
875 *-----------------------------------------------------------------------*/
876 time_start = get_timer (0);
877 while (1) {
878 temp_txm0 = in32 (EMAC_TXM0 + hw_p->hw_addr);
879 /* loop until either TINT turns on or 3 seconds elapse */
880 if ((temp_txm0 & EMAC_TXM0_GNP0) != 0) {
881 /* transmit is done, so now check for errors
882 * If there is an error, an interrupt should
883 * happen when we return
884 */
885 time_now = get_timer (0);
886 if ((time_now - time_start) > 3000) {
887 return (-1);
888 }
889 } else {
890 return (len);
891 }
892 }
893}
894
Stefan Roese6e7fb6e2005-11-29 18:18:21 +0100895
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200896#if defined (CONFIG_440)
wdenkba56f622004-02-06 23:19:44 +0000897
Stefan Roese6e7fb6e2005-11-29 18:18:21 +0100898#if defined(CONFIG_440SP)
899/*
900 * Hack: On 440SP all enet irq sources are located on UIC1
901 * Needs some cleanup. --sr
902 */
903#define UIC0MSR uic1msr
904#define UIC0SR uic1sr
905#else
906#define UIC0MSR uic0msr
907#define UIC0SR uic0sr
908#endif
909
wdenkba56f622004-02-06 23:19:44 +0000910int enetInt (struct eth_device *dev)
911{
912 int serviced;
913 int rc = -1; /* default to not us */
914 unsigned long mal_isr;
915 unsigned long emac_isr = 0;
916 unsigned long mal_rx_eob;
917 unsigned long my_uic0msr, my_uic1msr;
918
Stefan Roese846b0dd2005-08-08 12:42:22 +0200919#if defined(CONFIG_440GX)
wdenkba56f622004-02-06 23:19:44 +0000920 unsigned long my_uic2msr;
921#endif
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200922 EMAC_4XX_HW_PST hw_p;
wdenkba56f622004-02-06 23:19:44 +0000923
924 /*
925 * Because the mal is generic, we need to get the current
926 * eth device
927 */
Stefan Roesed6c61aa2005-08-16 18:18:00 +0200928#if defined(CONFIG_NET_MULTI)
929 dev = eth_get_dev();
930#else
931 dev = emac0_dev;
932#endif
wdenkba56f622004-02-06 23:19:44 +0000933
934 hw_p = dev->priv;
935
wdenkba56f622004-02-06 23:19:44 +0000936 /* enter loop that stays in interrupt code until nothing to service */
937 do {
938 serviced = 0;
939
Stefan Roese6e7fb6e2005-11-29 18:18:21 +0100940 my_uic0msr = mfdcr (UIC0MSR);
wdenkba56f622004-02-06 23:19:44 +0000941 my_uic1msr = mfdcr (uic1msr);
Stefan Roese846b0dd2005-08-08 12:42:22 +0200942#if defined(CONFIG_440GX)
wdenkba56f622004-02-06 23:19:44 +0000943 my_uic2msr = mfdcr (uic2msr);
944#endif
945 if (!(my_uic0msr & (UIC_MRE | UIC_MTE))
Stefan Roese6e7fb6e2005-11-29 18:18:21 +0100946 && !(my_uic1msr & (UIC_ETH0 | UIC_ETH1 | UIC_MS | UIC_MTDE | UIC_MRDE))) {
wdenkba56f622004-02-06 23:19:44 +0000947 /* not for us */
948 return (rc);
949 }
Stefan Roese846b0dd2005-08-08 12:42:22 +0200950#if defined (CONFIG_440GX)
wdenkba56f622004-02-06 23:19:44 +0000951 if (!(my_uic0msr & (UIC_MRE | UIC_MTE))
952 && !(my_uic2msr & (UIC_ETH2 | UIC_ETH3))) {
953 /* not for us */
954 return (rc);
955 }
956#endif
957 /* get and clear controller status interrupts */
958 /* look at Mal and EMAC interrupts */
959 if ((my_uic0msr & (UIC_MRE | UIC_MTE))
960 || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
961 /* we have a MAL interrupt */
962 mal_isr = mfdcr (malesr);
963 /* look for mal error */
964 if (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE)) {
965 mal_err (dev, mal_isr, my_uic0msr,
966 MAL_UIC_DEF, MAL_UIC_ERR);
967 serviced = 1;
968 rc = 0;
969 }
970 }
971
972 /* port by port dispatch of emac interrupts */
973 if (hw_p->devnum == 0) {
974 if (UIC_ETH0 & my_uic1msr) { /* 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))) {
Stefan Roese6e7fb6e2005-11-29 18:18:21 +0100984 mtdcr (UIC0SR, UIC_MRE | UIC_MTE); /* Clear */
wdenkba56f622004-02-06 23:19:44 +0000985 mtdcr (uic1sr, UIC_ETH0 | UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
986 return (rc); /* we had errors so get out */
987 }
988 }
989
Stefan Roese6e7fb6e2005-11-29 18:18:21 +0100990#if !defined(CONFIG_440SP)
wdenkba56f622004-02-06 23:19:44 +0000991 if (hw_p->devnum == 1) {
992 if (UIC_ETH1 & my_uic1msr) { /* 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))) {
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001002 mtdcr (UIC0SR, UIC_MRE | UIC_MTE); /* Clear */
wdenkba56f622004-02-06 23:19:44 +00001003 mtdcr (uic1sr, UIC_ETH1 | UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
1004 return (rc); /* we had errors so get out */
1005 }
1006 }
Stefan Roese846b0dd2005-08-08 12:42:22 +02001007#if defined (CONFIG_440GX)
wdenkba56f622004-02-06 23:19:44 +00001008 if (hw_p->devnum == 2) {
1009 if (UIC_ETH2 & my_uic2msr) { /* look for EMAC errors */
1010 emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
1011 if ((hw_p->emac_ier & emac_isr) != 0) {
1012 emac_err (dev, emac_isr);
1013 serviced = 1;
1014 rc = 0;
1015 }
1016 }
1017 if ((hw_p->emac_ier & emac_isr)
1018 || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001019 mtdcr (UIC0SR, UIC_MRE | UIC_MTE); /* Clear */
wdenkba56f622004-02-06 23:19:44 +00001020 mtdcr (uic1sr, UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
1021 mtdcr (uic2sr, UIC_ETH2);
1022 return (rc); /* we had errors so get out */
1023 }
1024 }
1025
1026 if (hw_p->devnum == 3) {
1027 if (UIC_ETH3 & my_uic2msr) { /* look for EMAC errors */
1028 emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
1029 if ((hw_p->emac_ier & emac_isr) != 0) {
1030 emac_err (dev, emac_isr);
1031 serviced = 1;
1032 rc = 0;
1033 }
1034 }
1035 if ((hw_p->emac_ier & emac_isr)
1036 || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001037 mtdcr (UIC0SR, UIC_MRE | UIC_MTE); /* Clear */
wdenkba56f622004-02-06 23:19:44 +00001038 mtdcr (uic1sr, UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
1039 mtdcr (uic2sr, UIC_ETH3);
1040 return (rc); /* we had errors so get out */
1041 }
1042 }
Stefan Roese846b0dd2005-08-08 12:42:22 +02001043#endif /* CONFIG_440GX */
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001044#endif /* !CONFIG_440SP */
1045
wdenkba56f622004-02-06 23:19:44 +00001046 /* handle MAX TX EOB interrupt from a tx */
1047 if (my_uic0msr & UIC_MTE) {
1048 mal_rx_eob = mfdcr (maltxeobisr);
1049 mtdcr (maltxeobisr, mal_rx_eob);
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001050 mtdcr (UIC0SR, UIC_MTE);
wdenkba56f622004-02-06 23:19:44 +00001051 }
1052 /* handle MAL RX EOB interupt from a receive */
wdenkfc1cfcd2004-04-25 15:41:35 +00001053 /* check for EOB on valid channels */
wdenkba56f622004-02-06 23:19:44 +00001054 if (my_uic0msr & UIC_MRE) {
1055 mal_rx_eob = mfdcr (malrxeobisr);
Wolfgang Denk265817c2005-09-25 00:53:22 +02001056 if ((mal_rx_eob & (0x80000000 >> hw_p->devnum)) != 0) { /* call emac routine for channel x */
wdenkba56f622004-02-06 23:19:44 +00001057 /* clear EOB
1058 mtdcr(malrxeobisr, mal_rx_eob); */
1059 enet_rcv (dev, emac_isr);
1060 /* indicate that we serviced an interrupt */
1061 serviced = 1;
1062 rc = 0;
1063 }
1064 }
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001065
1066 mtdcr (UIC0SR, UIC_MRE); /* Clear */
wdenkba56f622004-02-06 23:19:44 +00001067 mtdcr (uic1sr, UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
1068 switch (hw_p->devnum) {
1069 case 0:
1070 mtdcr (uic1sr, UIC_ETH0);
1071 break;
1072 case 1:
1073 mtdcr (uic1sr, UIC_ETH1);
1074 break;
Stefan Roese846b0dd2005-08-08 12:42:22 +02001075#if defined (CONFIG_440GX)
wdenkba56f622004-02-06 23:19:44 +00001076 case 2:
1077 mtdcr (uic2sr, UIC_ETH2);
1078 break;
1079 case 3:
1080 mtdcr (uic2sr, UIC_ETH3);
1081 break;
Stefan Roese846b0dd2005-08-08 12:42:22 +02001082#endif /* CONFIG_440GX */
wdenkba56f622004-02-06 23:19:44 +00001083 default:
1084 break;
1085 }
1086 } while (serviced);
1087
1088 return (rc);
1089}
1090
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001091#else /* CONFIG_440 */
1092
1093int enetInt (struct eth_device *dev)
1094{
1095 int serviced;
1096 int rc = -1; /* default to not us */
1097 unsigned long mal_isr;
1098 unsigned long emac_isr = 0;
1099 unsigned long mal_rx_eob;
1100 unsigned long my_uicmsr;
1101
1102 EMAC_4XX_HW_PST hw_p;
1103
1104 /*
1105 * Because the mal is generic, we need to get the current
1106 * eth device
1107 */
1108#if defined(CONFIG_NET_MULTI)
1109 dev = eth_get_dev();
1110#else
1111 dev = emac0_dev;
1112#endif
1113
1114 hw_p = dev->priv;
1115
1116 /* enter loop that stays in interrupt code until nothing to service */
1117 do {
1118 serviced = 0;
1119
1120 my_uicmsr = mfdcr (uicmsr);
1121
1122 if ((my_uicmsr & (MAL_UIC_DEF | EMAC_UIC_DEF)) == 0) { /* not for us */
1123 return (rc);
1124 }
1125 /* get and clear controller status interrupts */
1126 /* look at Mal and EMAC interrupts */
1127 if ((MAL_UIC_DEF & my_uicmsr) != 0) { /* we have a MAL interrupt */
1128 mal_isr = mfdcr (malesr);
1129 /* look for mal error */
1130 if ((my_uicmsr & MAL_UIC_ERR) != 0) {
1131 mal_err (dev, mal_isr, my_uicmsr, MAL_UIC_DEF, MAL_UIC_ERR);
1132 serviced = 1;
1133 rc = 0;
1134 }
1135 }
1136
1137 /* port by port dispatch of emac interrupts */
1138
1139 if ((SEL_UIC_DEF(hw_p->devnum) & my_uicmsr) != 0) { /* look for EMAC errors */
1140 emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
1141 if ((hw_p->emac_ier & emac_isr) != 0) {
1142 emac_err (dev, emac_isr);
1143 serviced = 1;
1144 rc = 0;
1145 }
1146 }
1147 if (((hw_p->emac_ier & emac_isr) != 0) || ((MAL_UIC_ERR & my_uicmsr) != 0)) {
1148 mtdcr (uicsr, MAL_UIC_DEF | SEL_UIC_DEF(hw_p->devnum)); /* Clear */
1149 return (rc); /* we had errors so get out */
1150 }
1151
1152 /* handle MAX TX EOB interrupt from a tx */
1153 if (my_uicmsr & UIC_MAL_TXEOB) {
1154 mal_rx_eob = mfdcr (maltxeobisr);
1155 mtdcr (maltxeobisr, mal_rx_eob);
1156 mtdcr (uicsr, UIC_MAL_TXEOB);
1157 }
1158 /* handle MAL RX EOB interupt from a receive */
1159 /* check for EOB on valid channels */
1160 if (my_uicmsr & UIC_MAL_RXEOB)
1161 {
1162 mal_rx_eob = mfdcr (malrxeobisr);
Wolfgang Denk265817c2005-09-25 00:53:22 +02001163 if ((mal_rx_eob & (0x80000000 >> hw_p->devnum)) != 0) { /* call emac routine for channel x */
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001164 /* clear EOB
1165 mtdcr(malrxeobisr, mal_rx_eob); */
1166 enet_rcv (dev, emac_isr);
1167 /* indicate that we serviced an interrupt */
1168 serviced = 1;
1169 rc = 0;
1170 }
1171 }
1172 mtdcr (uicsr, MAL_UIC_DEF|EMAC_UIC_DEF|EMAC_UIC_DEF1); /* Clear */
1173 }
1174 while (serviced);
1175
1176 return (rc);
1177}
1178
1179#endif /* CONFIG_440 */
1180
wdenkba56f622004-02-06 23:19:44 +00001181/*-----------------------------------------------------------------------------+
1182 * MAL Error Routine
1183 *-----------------------------------------------------------------------------*/
1184static void mal_err (struct eth_device *dev, unsigned long isr,
1185 unsigned long uic, unsigned long maldef,
1186 unsigned long mal_errr)
1187{
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001188 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenkba56f622004-02-06 23:19:44 +00001189
1190 mtdcr (malesr, isr); /* clear interrupt */
1191
1192 /* clear DE interrupt */
1193 mtdcr (maltxdeir, 0xC0000000);
1194 mtdcr (malrxdeir, 0x80000000);
1195
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001196#ifdef INFO_4XX_ENET
Wolfgang Denk265817c2005-09-25 00:53:22 +02001197 printf ("\nMAL error occured.... ISR = %lx UIC = = %lx MAL_DEF = %lx MAL_ERR= %lx \n", isr, uic, maldef, mal_errr);
wdenkba56f622004-02-06 23:19:44 +00001198#endif
1199
1200 eth_init (hw_p->bis); /* start again... */
1201}
1202
1203/*-----------------------------------------------------------------------------+
1204 * EMAC Error Routine
1205 *-----------------------------------------------------------------------------*/
1206static void emac_err (struct eth_device *dev, unsigned long isr)
1207{
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001208 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenkba56f622004-02-06 23:19:44 +00001209
1210 printf ("EMAC%d error occured.... ISR = %lx\n", hw_p->devnum, isr);
1211 out32 (EMAC_ISR + hw_p->hw_addr, isr);
1212}
1213
1214/*-----------------------------------------------------------------------------+
1215 * enet_rcv() handles the ethernet receive data
1216 *-----------------------------------------------------------------------------*/
1217static void enet_rcv (struct eth_device *dev, unsigned long malisr)
1218{
1219 struct enet_frame *ef_ptr;
1220 unsigned long data_len;
1221 unsigned long rx_eob_isr;
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001222 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenkba56f622004-02-06 23:19:44 +00001223
1224 int handled = 0;
1225 int i;
1226 int loop_count = 0;
1227
1228 rx_eob_isr = mfdcr (malrxeobisr);
1229 if ((0x80000000 >> hw_p->devnum) & rx_eob_isr) {
1230 /* clear EOB */
1231 mtdcr (malrxeobisr, rx_eob_isr);
1232
1233 /* EMAC RX done */
1234 while (1) { /* do all */
1235 i = hw_p->rx_slot;
1236
1237 if ((MAL_RX_CTRL_EMPTY & hw_p->rx[i].ctrl)
1238 || (loop_count >= NUM_RX_BUFF))
1239 break;
1240 loop_count++;
1241 hw_p->rx_slot++;
1242 if (NUM_RX_BUFF == hw_p->rx_slot)
1243 hw_p->rx_slot = 0;
1244 handled++;
1245 data_len = (unsigned long) hw_p->rx[i].data_len; /* Get len */
1246 if (data_len) {
1247 if (data_len > ENET_MAX_MTU) /* Check len */
1248 data_len = 0;
1249 else {
1250 if (EMAC_RX_ERRORS & hw_p->rx[i].ctrl) { /* Check Errors */
1251 data_len = 0;
1252 hw_p->stats.rx_err_log[hw_p->
1253 rx_err_index]
1254 = hw_p->rx[i].ctrl;
1255 hw_p->rx_err_index++;
1256 if (hw_p->rx_err_index ==
1257 MAX_ERR_LOG)
1258 hw_p->rx_err_index =
1259 0;
wdenkfc1cfcd2004-04-25 15:41:35 +00001260 } /* emac_erros */
wdenkba56f622004-02-06 23:19:44 +00001261 } /* data_len < max mtu */
wdenkfc1cfcd2004-04-25 15:41:35 +00001262 } /* if data_len */
wdenkba56f622004-02-06 23:19:44 +00001263 if (!data_len) { /* no data */
1264 hw_p->rx[i].ctrl |= MAL_RX_CTRL_EMPTY; /* Free Recv Buffer */
1265
1266 hw_p->stats.data_len_err++; /* Error at Rx */
1267 }
1268
1269 /* !data_len */
1270 /* AS.HARNOIS */
1271 /* Check if user has already eaten buffer */
1272 /* if not => ERROR */
1273 else if (hw_p->rx_ready[hw_p->rx_i_index] != -1) {
1274 if (hw_p->is_receiving)
1275 printf ("ERROR : Receive buffers are full!\n");
1276 break;
1277 } else {
1278 hw_p->stats.rx_frames++;
1279 hw_p->stats.rx += data_len;
1280 ef_ptr = (struct enet_frame *) hw_p->rx[i].
1281 data_ptr;
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001282#ifdef INFO_4XX_ENET
wdenkba56f622004-02-06 23:19:44 +00001283 hw_p->stats.pkts_rx++;
1284#endif
1285 /* AS.HARNOIS
1286 * use ring buffer
1287 */
1288 hw_p->rx_ready[hw_p->rx_i_index] = i;
1289 hw_p->rx_i_index++;
1290 if (NUM_RX_BUFF == hw_p->rx_i_index)
1291 hw_p->rx_i_index = 0;
1292
wdenkba56f622004-02-06 23:19:44 +00001293 /* AS.HARNOIS
1294 * free receive buffer only when
1295 * buffer has been handled (eth_rx)
1296 rx[i].ctrl |= MAL_RX_CTRL_EMPTY;
1297 */
1298 } /* if data_len */
1299 } /* while */
1300 } /* if EMACK_RXCHL */
1301}
1302
1303
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001304static int ppc_4xx_eth_rx (struct eth_device *dev)
wdenkba56f622004-02-06 23:19:44 +00001305{
1306 int length;
1307 int user_index;
1308 unsigned long msr;
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001309 EMAC_4XX_HW_PST hw_p = dev->priv;
wdenkba56f622004-02-06 23:19:44 +00001310
Wolfgang Denk265817c2005-09-25 00:53:22 +02001311 hw_p->is_receiving = 1; /* tell driver */
wdenkba56f622004-02-06 23:19:44 +00001312
1313 for (;;) {
1314 /* AS.HARNOIS
1315 * use ring buffer and
1316 * get index from rx buffer desciptor queue
1317 */
1318 user_index = hw_p->rx_ready[hw_p->rx_u_index];
1319 if (user_index == -1) {
1320 length = -1;
1321 break; /* nothing received - leave for() loop */
1322 }
1323
1324 msr = mfmsr ();
1325 mtmsr (msr & ~(MSR_EE));
1326
1327 length = hw_p->rx[user_index].data_len;
1328
1329 /* Pass the packet up to the protocol layers. */
Wolfgang Denk265817c2005-09-25 00:53:22 +02001330 /* NetReceive(NetRxPackets[rxIdx], length - 4); */
1331 /* NetReceive(NetRxPackets[i], length); */
wdenkba56f622004-02-06 23:19:44 +00001332 NetReceive (NetRxPackets[user_index], length - 4);
1333 /* Free Recv Buffer */
1334 hw_p->rx[user_index].ctrl |= MAL_RX_CTRL_EMPTY;
1335 /* Free rx buffer descriptor queue */
1336 hw_p->rx_ready[hw_p->rx_u_index] = -1;
1337 hw_p->rx_u_index++;
1338 if (NUM_RX_BUFF == hw_p->rx_u_index)
1339 hw_p->rx_u_index = 0;
1340
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001341#ifdef INFO_4XX_ENET
wdenkba56f622004-02-06 23:19:44 +00001342 hw_p->stats.pkts_handled++;
1343#endif
1344
1345 mtmsr (msr); /* Enable IRQ's */
1346 }
1347
Wolfgang Denk265817c2005-09-25 00:53:22 +02001348 hw_p->is_receiving = 0; /* tell driver */
wdenkba56f622004-02-06 23:19:44 +00001349
1350 return length;
1351}
1352
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001353int ppc_4xx_eth_initialize (bd_t * bis)
wdenkba56f622004-02-06 23:19:44 +00001354{
1355 static int virgin = 0;
wdenkba56f622004-02-06 23:19:44 +00001356 struct eth_device *dev;
1357 int eth_num = 0;
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001358 EMAC_4XX_HW_PST hw = NULL;
wdenkba56f622004-02-06 23:19:44 +00001359
Stefan Roese846b0dd2005-08-08 12:42:22 +02001360#if defined(CONFIG_440GX)
Stefan Roesec157d8e2005-08-01 16:41:48 +02001361 unsigned long pfc1;
1362
wdenkba56f622004-02-06 23:19:44 +00001363 mfsdr (sdr_pfc1, pfc1);
1364 pfc1 &= ~(0x01e00000);
1365 pfc1 |= 0x01200000;
1366 mtsdr (sdr_pfc1, pfc1);
Stefan Roesec157d8e2005-08-01 16:41:48 +02001367#endif
wdenk3c74e322004-02-22 23:46:08 +00001368 /* set phy num and mode */
1369 bis->bi_phynum[0] = CONFIG_PHY_ADDR;
Stefan Roesec157d8e2005-08-01 16:41:48 +02001370#if defined(CONFIG_PHY1_ADDR)
wdenk3c74e322004-02-22 23:46:08 +00001371 bis->bi_phynum[1] = CONFIG_PHY1_ADDR;
Stefan Roesec157d8e2005-08-01 16:41:48 +02001372#endif
Stefan Roese846b0dd2005-08-08 12:42:22 +02001373#if defined(CONFIG_440GX)
wdenk3c74e322004-02-22 23:46:08 +00001374 bis->bi_phynum[2] = CONFIG_PHY2_ADDR;
1375 bis->bi_phynum[3] = CONFIG_PHY3_ADDR;
1376 bis->bi_phymode[0] = 0;
1377 bis->bi_phymode[1] = 0;
1378 bis->bi_phymode[2] = 2;
1379 bis->bi_phymode[3] = 2;
wdenkba56f622004-02-06 23:19:44 +00001380
Stefan Roese846b0dd2005-08-08 12:42:22 +02001381#if defined (CONFIG_440GX)
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001382 ppc_4xx_eth_setup_bridge(0, bis);
wdenka06752e2004-09-29 22:43:59 +00001383#endif
Stefan Roesec157d8e2005-08-01 16:41:48 +02001384#endif
wdenka06752e2004-09-29 22:43:59 +00001385
Stefan Roese1e25f952005-10-20 16:34:28 +02001386 for (eth_num = 0; eth_num < LAST_EMAC_NUM; eth_num++) {
wdenkba56f622004-02-06 23:19:44 +00001387
1388 /* See if we can actually bring up the interface, otherwise, skip it */
1389 switch (eth_num) {
wdenke2ffd592004-12-31 09:32:47 +00001390 default: /* fall through */
wdenkba56f622004-02-06 23:19:44 +00001391 case 0:
wdenk3c74e322004-02-22 23:46:08 +00001392 if (memcmp (bis->bi_enetaddr, "\0\0\0\0\0\0", 6) == 0) {
1393 bis->bi_phymode[eth_num] = BI_PHYMODE_NONE;
wdenkba56f622004-02-06 23:19:44 +00001394 continue;
wdenk3c74e322004-02-22 23:46:08 +00001395 }
wdenkba56f622004-02-06 23:19:44 +00001396 break;
wdenke2ffd592004-12-31 09:32:47 +00001397#ifdef CONFIG_HAS_ETH1
wdenkba56f622004-02-06 23:19:44 +00001398 case 1:
wdenk3c74e322004-02-22 23:46:08 +00001399 if (memcmp (bis->bi_enet1addr, "\0\0\0\0\0\0", 6) == 0) {
1400 bis->bi_phymode[eth_num] = BI_PHYMODE_NONE;
wdenkba56f622004-02-06 23:19:44 +00001401 continue;
wdenk3c74e322004-02-22 23:46:08 +00001402 }
wdenkba56f622004-02-06 23:19:44 +00001403 break;
wdenke2ffd592004-12-31 09:32:47 +00001404#endif
1405#ifdef CONFIG_HAS_ETH2
wdenkba56f622004-02-06 23:19:44 +00001406 case 2:
wdenk3c74e322004-02-22 23:46:08 +00001407 if (memcmp (bis->bi_enet2addr, "\0\0\0\0\0\0", 6) == 0) {
1408 bis->bi_phymode[eth_num] = BI_PHYMODE_NONE;
wdenkba56f622004-02-06 23:19:44 +00001409 continue;
wdenk3c74e322004-02-22 23:46:08 +00001410 }
wdenkba56f622004-02-06 23:19:44 +00001411 break;
wdenke2ffd592004-12-31 09:32:47 +00001412#endif
1413#ifdef CONFIG_HAS_ETH3
wdenkba56f622004-02-06 23:19:44 +00001414 case 3:
wdenk3c74e322004-02-22 23:46:08 +00001415 if (memcmp (bis->bi_enet3addr, "\0\0\0\0\0\0", 6) == 0) {
1416 bis->bi_phymode[eth_num] = BI_PHYMODE_NONE;
wdenkba56f622004-02-06 23:19:44 +00001417 continue;
wdenk3c74e322004-02-22 23:46:08 +00001418 }
wdenkba56f622004-02-06 23:19:44 +00001419 break;
wdenke2ffd592004-12-31 09:32:47 +00001420#endif
wdenkba56f622004-02-06 23:19:44 +00001421 }
1422
1423 /* Allocate device structure */
1424 dev = (struct eth_device *) malloc (sizeof (*dev));
1425 if (dev == NULL) {
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001426 printf ("ppc_4xx_eth_initialize: "
wdenk3f85ce22004-02-23 16:11:30 +00001427 "Cannot allocate eth_device %d\n", eth_num);
wdenkba56f622004-02-06 23:19:44 +00001428 return (-1);
1429 }
wdenkb2532ef2005-06-20 10:17:34 +00001430 memset(dev, 0, sizeof(*dev));
wdenkba56f622004-02-06 23:19:44 +00001431
1432 /* Allocate our private use data */
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001433 hw = (EMAC_4XX_HW_PST) malloc (sizeof (*hw));
wdenkba56f622004-02-06 23:19:44 +00001434 if (hw == NULL) {
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001435 printf ("ppc_4xx_eth_initialize: "
wdenk3f85ce22004-02-23 16:11:30 +00001436 "Cannot allocate private hw data for eth_device %d",
wdenkba56f622004-02-06 23:19:44 +00001437 eth_num);
1438 free (dev);
1439 return (-1);
1440 }
wdenkb2532ef2005-06-20 10:17:34 +00001441 memset(hw, 0, sizeof(*hw));
wdenkba56f622004-02-06 23:19:44 +00001442
1443 switch (eth_num) {
wdenke2ffd592004-12-31 09:32:47 +00001444 default: /* fall through */
wdenkba56f622004-02-06 23:19:44 +00001445 case 0:
1446 hw->hw_addr = 0;
1447 memcpy (dev->enetaddr, bis->bi_enetaddr, 6);
1448 break;
wdenke2ffd592004-12-31 09:32:47 +00001449#ifdef CONFIG_HAS_ETH1
wdenkba56f622004-02-06 23:19:44 +00001450 case 1:
1451 hw->hw_addr = 0x100;
1452 memcpy (dev->enetaddr, bis->bi_enet1addr, 6);
1453 break;
wdenke2ffd592004-12-31 09:32:47 +00001454#endif
1455#ifdef CONFIG_HAS_ETH2
wdenkba56f622004-02-06 23:19:44 +00001456 case 2:
1457 hw->hw_addr = 0x400;
1458 memcpy (dev->enetaddr, bis->bi_enet2addr, 6);
1459 break;
wdenke2ffd592004-12-31 09:32:47 +00001460#endif
1461#ifdef CONFIG_HAS_ETH3
wdenkba56f622004-02-06 23:19:44 +00001462 case 3:
1463 hw->hw_addr = 0x600;
1464 memcpy (dev->enetaddr, bis->bi_enet3addr, 6);
1465 break;
wdenke2ffd592004-12-31 09:32:47 +00001466#endif
wdenkba56f622004-02-06 23:19:44 +00001467 }
1468
1469 hw->devnum = eth_num;
Stefan Roesec157d8e2005-08-01 16:41:48 +02001470 hw->print_speed = 1;
wdenkba56f622004-02-06 23:19:44 +00001471
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001472 sprintf (dev->name, "ppc_4xx_eth%d", eth_num);
wdenkba56f622004-02-06 23:19:44 +00001473 dev->priv = (void *) hw;
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001474 dev->init = ppc_4xx_eth_init;
1475 dev->halt = ppc_4xx_eth_halt;
1476 dev->send = ppc_4xx_eth_send;
1477 dev->recv = ppc_4xx_eth_rx;
wdenkba56f622004-02-06 23:19:44 +00001478
1479 if (0 == virgin) {
1480 /* set the MAL IER ??? names may change with new spec ??? */
1481 mal_ier =
1482 MAL_IER_DE | MAL_IER_NE | MAL_IER_TE |
1483 MAL_IER_OPBE | MAL_IER_PLBE;
1484 mtdcr (malesr, 0xffffffff); /* clear pending interrupts */
1485 mtdcr (maltxdeir, 0xffffffff); /* clear pending interrupts */
1486 mtdcr (malrxdeir, 0xffffffff); /* clear pending interrupts */
1487 mtdcr (malier, mal_ier);
1488
1489 /* install MAL interrupt handler */
1490 irq_install_handler (VECNUM_MS,
1491 (interrupt_handler_t *) enetInt,
1492 dev);
1493 irq_install_handler (VECNUM_MTE,
1494 (interrupt_handler_t *) enetInt,
1495 dev);
1496 irq_install_handler (VECNUM_MRE,
1497 (interrupt_handler_t *) enetInt,
1498 dev);
1499 irq_install_handler (VECNUM_TXDE,
1500 (interrupt_handler_t *) enetInt,
1501 dev);
1502 irq_install_handler (VECNUM_RXDE,
1503 (interrupt_handler_t *) enetInt,
1504 dev);
1505 virgin = 1;
1506 }
1507
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001508#if defined(CONFIG_NET_MULTI)
wdenkba56f622004-02-06 23:19:44 +00001509 eth_register (dev);
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001510#else
1511 emac0_dev = dev;
1512#endif
Marian Balakowicz63ff0042005-10-28 22:30:33 +02001513#if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
1514 miiphy_register (dev->name,
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001515 emac4xx_miiphy_read, emac4xx_miiphy_write);
Marian Balakowicz63ff0042005-10-28 22:30:33 +02001516#endif
wdenkba56f622004-02-06 23:19:44 +00001517
1518 } /* end for each supported device */
1519 return (1);
1520}
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001521
1522
1523#if !defined(CONFIG_NET_MULTI)
1524void eth_halt (void) {
1525 if (emac0_dev) {
1526 ppc_4xx_eth_halt(emac0_dev);
1527 free(emac0_dev);
1528 emac0_dev = NULL;
1529 }
1530}
1531
1532int eth_init (bd_t *bis)
1533{
1534 ppc_4xx_eth_initialize(bis);
Stefan Roese4f92ac32005-10-10 17:43:58 +02001535 if (emac0_dev) {
1536 return ppc_4xx_eth_init(emac0_dev, bis);
1537 } else {
1538 printf("ERROR: ethaddr not set!\n");
1539 return -1;
1540 }
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001541}
1542
1543int eth_send(volatile void *packet, int length)
1544{
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001545 return (ppc_4xx_eth_send(emac0_dev, packet, length));
1546}
1547
1548int eth_rx(void)
1549{
1550 return (ppc_4xx_eth_rx(emac0_dev));
1551}
Marian Balakowicz63ff0042005-10-28 22:30:33 +02001552
1553int emac4xx_miiphy_initialize (bd_t * bis)
1554{
1555#if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
1556 miiphy_register ("ppc_4xx_eth0",
Stefan Roese6e7fb6e2005-11-29 18:18:21 +01001557 emac4xx_miiphy_read, emac4xx_miiphy_write);
Marian Balakowicz63ff0042005-10-28 22:30:33 +02001558#endif
1559
1560 return 0;
1561}
Stefan Roesed6c61aa2005-08-16 18:18:00 +02001562#endif /* !defined(CONFIG_NET_MULTI) */
1563
1564#endif /* #if (CONFIG_COMMANDS & CFG_CMD_NET) */