2 * Copyright (C) 2004 IMMS gGmbH <www.imms.de>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
19 * author(s): Thomas Elste, <info@elste.org>
20 * (some parts derived from uCLinux Netarm Ethernet Driver)
27 #include "netarm_eth.h"
28 #include <asm/arch/netarm_registers.h>
30 static int na_mii_poll_busy (void);
32 static void na_get_mac_addr (void)
40 p[0] = (unsigned short) GET_EADDR (NETARM_ETH_SAL_STATION_ADDR_1);
41 p[1] = (unsigned short) GET_EADDR (NETARM_ETH_SAL_STATION_ADDR_2);
42 p[2] = (unsigned short) GET_EADDR (NETARM_ETH_SAL_STATION_ADDR_3);
44 sprintf (ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
46 m_addr[2], m_addr[3], m_addr[4], m_addr[5]);
48 printf ("HW-MAC Address: %s\n", ethaddr);
50 /* set env, todo: check if already an adress is set */
51 setenv ("ethaddr", ethaddr);
54 static void na_mii_write (int reg, int value)
59 mii_addr = CONFIG_SYS_ETH_PHY_ADDR + reg;
60 SET_EADDR (NETARM_ETH_MII_ADDR, mii_addr);
62 SET_EADDR (NETARM_ETH_MII_WRITE, value);
66 static unsigned int na_mii_read (int reg)
71 mii_addr = CONFIG_SYS_ETH_PHY_ADDR + reg;
72 SET_EADDR (NETARM_ETH_MII_ADDR, mii_addr);
73 /* do one management cycle */
74 SET_EADDR (NETARM_ETH_MII_CMD,
75 GET_EADDR (NETARM_ETH_MII_CMD) | NETARM_ETH_MIIC_RSTAT);
77 /* Return read value */
78 val = GET_EADDR (NETARM_ETH_MII_READ);
82 static int na_mii_poll_busy (void)
84 /* arm simple, non interrupt dependent timer */
85 reset_timer_masked ();
86 while (get_timer_masked () < NA_MII_POLL_BUSY_DELAY) {
87 if (!(GET_EADDR (NETARM_ETH_MII_IND) & NETARM_ETH_MIII_BUSY)) {
91 printf ("na_mii_busy timeout\n");
95 static int na_mii_identify_phy (void)
99 /* get phy id register */
100 id_reg_a = na_mii_read (MII_PHY_ID);
102 if (id_reg_a == 0x0043) {
103 /* This must be an Enable or a Lucent LU3X31 PHY chip */
105 } else if (id_reg_a == 0x0013) {
106 /* it is an Intel LXT971A */
112 static int na_mii_negotiate (void)
116 /* Enable auto-negotiation */
117 na_mii_write (MII_PHY_AUTONEGADV, 0x01e1);
118 /* FIXME: 0x01E1 is 100Mb half and full duplex, 0x0061 is 10Mb only */
119 /* Restart auto-negotiation */
120 na_mii_write (MII_PHY_CONTROL, 0x1200);
122 /* status register is 0xffff after setting the autoneg restart bit */
123 while (na_mii_read (MII_PHY_STATUS) == 0xffff) {
127 /* na_mii_read uses the timer already, so we can't use it again for
129 Instead we just try some times.
131 for (i = 0; i < 40000; i++) {
132 if ((na_mii_read (MII_PHY_STATUS) & 0x0024) == 0x0024) {
137 printf("*Warning* autonegotiation timeout, status: 0x%x\n",na_mii_read(MII_PHY_STATUS));
142 static unsigned int na_mii_check_speed (void)
146 /* Read Status register */
147 status = na_mii_read (MII_PHY_STATUS);
148 /* Check link status. If 0, default to 100 Mbps. */
149 if ((status & 0x0004) == 0) {
150 printf ("*Warning* no link detected, set default speed to 100Mbs\n");
153 if ((na_mii_read (17) & 0x4000) != 0) {
154 printf ("100Mbs link detected\n");
157 printf ("10Mbs link detected\n");
164 static int reset_eth (void)
169 pt = na_mii_identify_phy ();
172 na_mii_write (MII_PHY_CONTROL, 0x8000);
173 reset_timer_masked ();
174 while (get_timer_masked () < NA_MII_NEGOTIATE_DELAY) {
175 if ((na_mii_read (MII_PHY_STATUS) & 0x8000) == 0) {
179 if (get_timer_masked () >= NA_MII_NEGOTIATE_DELAY)
180 printf ("phy reset timeout\n");
182 /* set the PCS reg */
183 SET_EADDR (NETARM_ETH_PCS_CFG, NETARM_ETH_PCSC_CLKS_25M |
184 NETARM_ETH_PCSC_ENJAB | NETARM_ETH_PCSC_NOCFR);
187 na_mii_check_speed ();
189 /* Delay 10 millisecond. (Maybe this should be 1 second.) */
193 Enable statistics register autozero on read.
194 Do not insert MAC address on transmit.
195 Do not enable special test modes. */
196 SET_EADDR (NETARM_ETH_STL_CFG,
197 (NETARM_ETH_STLC_AUTOZ | NETARM_ETH_STLC_RXEN));
199 /* Set the inter-packet gap delay to 0.96us for MII.
200 The NET+ARM H/W Reference Guide indicates that the Back-to-back IPG
201 Gap Timer Register should be set to 0x15 and the Non Back-to-back IPG
202 Gap Timer Register should be set to 0x00000C12 for the MII PHY. */
203 SET_EADDR (NETARM_ETH_B2B_IPG_GAP_TMR, 0x15);
204 SET_EADDR (NETARM_ETH_NB2B_IPG_GAP_TMR, 0x00000C12);
206 /* Add CRC to end of packets.
207 Pad packets to minimum length of 64 bytes.
208 Allow unlimited length transmit packets.
209 Receive all broadcast packets.
210 NOTE: Multicast addressing is NOT enabled here currently. */
211 SET_EADDR (NETARM_ETH_MAC_CFG,
212 (NETARM_ETH_MACC_CRCEN |
213 NETARM_ETH_MACC_PADEN | NETARM_ETH_MACC_HUGEN));
214 SET_EADDR (NETARM_ETH_SAL_FILTER, NETARM_ETH_SALF_BROAD);
217 SET_EADDR (NETARM_ETH_GEN_CTRL,
218 (NETARM_ETH_GCR_ERX | NETARM_ETH_GCR_ETX));
224 extern int eth_init (bd_t * bd)
230 extern void eth_halt (void)
232 SET_EADDR (NETARM_ETH_GEN_CTRL, 0);
235 /* Get a data block via Ethernet */
236 extern int eth_rx (void)
239 unsigned short rxlen;
241 unsigned int rxstatus, lastrxlen;
244 /* RXBR is 1, data block was received */
245 if ((GET_EADDR (NETARM_ETH_GEN_STAT) & NETARM_ETH_GST_RXBR) == 0)
248 /* get status register and the length of received block */
249 rxstatus = GET_EADDR (NETARM_ETH_RX_STAT);
250 rxlen = (rxstatus & NETARM_ETH_RXSTAT_SIZE) >> 16;
255 /* clear RXBR to make fifo available */
256 SET_EADDR (NETARM_ETH_GEN_STAT,
257 GET_EADDR (NETARM_ETH_GEN_STAT) & ~NETARM_ETH_GST_RXBR);
259 /* clear TXBC to make fifo available */
260 /* According to NETARM50 data manual you just have to clear
261 RXBR but that has no effect. Only after clearing TXBC the
262 Fifo becomes readable. */
263 SET_EADDR (NETARM_ETH_GEN_STAT,
264 GET_EADDR (NETARM_ETH_GEN_STAT) & ~NETARM_ETH_GST_TXBC);
266 addr = (unsigned int *) NetRxPackets[0];
267 pa = (char *) NetRxPackets[0];
270 for (i = 0; i < rxlen / 4; i++) {
271 *addr = GET_EADDR (NETARM_ETH_FIFO_DAT1);
275 if (GET_EADDR (NETARM_ETH_GEN_STAT) & NETARM_ETH_GST_RXREGR) {
276 /* RXFDB indicates wether the last word is 1,2,3 or 4 bytes long */
278 (GET_EADDR (NETARM_ETH_GEN_STAT) &
279 NETARM_ETH_GST_RXFDB) >> 28;
280 *addr = GET_EADDR (NETARM_ETH_FIFO_DAT1);
294 /* Pass the packet up to the protocol layers. */
295 NetReceive (NetRxPackets[0], rxlen);
300 /* Send a data block via Ethernet. */
301 extern int eth_send (volatile void *packet, int length)
305 unsigned int *pa32, lastp = 0, rest;
307 pa = (char *) packet;
308 pa32 = (unsigned int *) packet;
309 length32 = length / 4;
312 /* make sure there's no garbage in the last word */
315 lastp = pa32[length32];
319 lastp = pa32[length32] & 0x000000ff;
322 lastp = pa32[length32] & 0x0000ffff;
325 lastp = pa32[length32] & 0x00ffffff;
329 /* write to the fifo */
330 for (i = 0; i < length32; i++)
331 SET_EADDR (NETARM_ETH_FIFO_DAT1, pa32[i]);
333 /* the last word is written to an extra register, this
334 starts the transmission */
335 SET_EADDR (NETARM_ETH_FIFO_DAT2, lastp);
337 /* NETARM_ETH_TXSTAT_TXOK should be checked, to know if the transmission
338 went fine. But we can't use the timer for a timeout loop because
339 of it is used already in upper layers. So we just try some times. */
342 if ((GET_EADDR (NETARM_ETH_TX_STAT) & NETARM_ETH_TXSTAT_TXOK)
343 == NETARM_ETH_TXSTAT_TXOK)
348 printf ("eth_send timeout\n");