]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/netarm_eth.c
* Patch by Thomas Elste, 10 Feb 2004:
[karo-tx-uboot.git] / drivers / netarm_eth.c
1 /*
2  * Copyright (C) 2004 IMMS gGmbH <www.imms.de>
3  *
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.
8  *
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.
13  *
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,
17  * MA 02111-1307 USA
18  *
19  * author(s): Thomas Elste, <info@elste.org>
20  *            (some parts derived from uCLinux Netarm Ethernet Driver)
21  */
22
23
24 #include <common.h>
25 #include <command.h>
26 #include <net.h>
27 #include "netarm_eth.h"
28 #include <asm/arch/netarm_registers.h>
29
30 #ifdef CONFIG_DRIVER_NETARMETH
31
32 #if (CONFIG_COMMANDS & CFG_CMD_NET)
33
34 static int na_mii_poll_busy (void);
35
36 static void na_get_mac_addr (void)
37 {
38         unsigned short p[3];
39         char *m_addr;
40         char ethaddr[20];
41
42         m_addr = (char *) p;
43
44         p[0] = (unsigned short) GET_EADDR (NETARM_ETH_SAL_STATION_ADDR_1);
45         p[1] = (unsigned short) GET_EADDR (NETARM_ETH_SAL_STATION_ADDR_2);
46         p[2] = (unsigned short) GET_EADDR (NETARM_ETH_SAL_STATION_ADDR_3);
47
48         sprintf (ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
49                  m_addr[0], m_addr[1],
50                  m_addr[2], m_addr[3], m_addr[4], m_addr[5]);
51
52         printf ("HW-MAC Address:  %s\n", ethaddr);
53
54         /* set env, todo: check if already an adress is set */
55         setenv ("ethaddr", ethaddr);
56 }
57
58
59 static void na_mii_write (int reg, int value)
60 {
61         int mii_addr;
62
63         /* Select register */
64         mii_addr = CFG_ETH_PHY_ADDR + reg;
65         SET_EADDR (NETARM_ETH_MII_ADDR, mii_addr);
66         /* Write value */
67         SET_EADDR (NETARM_ETH_MII_WRITE, value);
68         na_mii_poll_busy ();
69 }
70
71 static unsigned int na_mii_read (int reg)
72 {
73         int mii_addr, val;
74
75         /* Select register */
76         mii_addr = CFG_ETH_PHY_ADDR + reg;
77         SET_EADDR (NETARM_ETH_MII_ADDR, mii_addr);
78         /* do one management cycle */
79         SET_EADDR (NETARM_ETH_MII_CMD,
80                    GET_EADDR (NETARM_ETH_MII_CMD) | NETARM_ETH_MIIC_RSTAT);
81         na_mii_poll_busy ();
82         /* Return read value */
83         val = GET_EADDR (NETARM_ETH_MII_READ);
84         return val;
85 }
86
87 static int na_mii_poll_busy (void)
88 {
89         /* arm simple, non interrupt dependent timer */
90         reset_timer_masked ();
91         while (get_timer_masked () < NA_MII_POLL_BUSY_DELAY) {
92                 if (!(GET_EADDR (NETARM_ETH_MII_IND) & NETARM_ETH_MIII_BUSY)) {
93                         return 1;
94                 }
95         }
96         printf ("na_mii_busy timeout\n");
97         return (0);
98 }
99
100 static int na_mii_identify_phy (void)
101 {
102         int id_reg_a = 0;
103
104         /* get phy id register */
105         id_reg_a = na_mii_read (MII_PHY_ID);
106
107         if (id_reg_a == 0x0043) {
108                 /* This must be an Enable or a Lucent LU3X31 PHY chip */
109                 return 1;
110         } else if (id_reg_a == 0x0013) {
111                 /* it is an Intel LXT971A */
112                 return 1;
113         }
114         return (0);
115 }
116
117 static int na_mii_negotiate (void)
118 {
119         int i = 0;
120
121         /* Enable auto-negotiation */
122         na_mii_write (MII_PHY_AUTONEGADV, 0x01e1);
123         /* FIXME: 0x01E1 is 100Mb half and full duplex, 0x0061 is 10Mb only */
124         /* Restart auto-negotiation */
125         na_mii_write (MII_PHY_CONTROL, 0x1200);
126
127         /* status register is 0xffff after setting the autoneg restart bit */
128         while (na_mii_read (MII_PHY_STATUS) == 0xffff) {
129                 i++;
130         }
131
132         /* na_mii_read uses the timer already, so we can't use it again for
133            timeout checking.
134            Instead we just try some times.
135          */
136         for (i = 0; i < 40000; i++) {
137                 if ((na_mii_read (MII_PHY_STATUS) & 0x0024) == 0x0024) {
138                         return 0;
139                 }
140         }
141         /*
142            printf("*Warning* autonegotiation timeout, status: 0x%x\n",na_mii_read(MII_PHY_STATUS));
143          */
144         return (1);
145 }
146
147 static unsigned int na_mii_check_speed (void)
148 {
149         unsigned int status;
150
151         /* Read Status register */
152         status = na_mii_read (MII_PHY_STATUS);
153         /* Check link status.  If 0, default to 100 Mbps. */
154         if ((status & 0x0004) == 0) {
155                 printf ("*Warning* no link detected, set default speed to 100Mbs\n");
156                 return 1;
157         } else {
158                 if ((na_mii_read (17) & 0x4000) != 0) {
159                         printf ("100Mbs link detected\n");
160                         return 1;
161                 } else {
162                         printf ("10Mbs link detected\n");
163                         return 0;
164                 }
165         }
166         return 0;
167 }
168
169 static int reset_eth (void)
170 {
171         int pt;
172
173         na_get_mac_addr ();
174         pt = na_mii_identify_phy ();
175
176         /* reset the phy */
177         na_mii_write (MII_PHY_CONTROL, 0x8000);
178         reset_timer_masked ();
179         while (get_timer_masked () < NA_MII_NEGOTIATE_DELAY) {
180                 if ((na_mii_read (MII_PHY_STATUS) & 0x8000) == 0) {
181                         break;
182                 }
183         }
184         if (get_timer_masked () >= NA_MII_NEGOTIATE_DELAY)
185                 printf ("phy reset timeout\n");
186
187         /* set the PCS reg */
188         SET_EADDR (NETARM_ETH_PCS_CFG, NETARM_ETH_PCSC_CLKS_25M |
189                    NETARM_ETH_PCSC_ENJAB | NETARM_ETH_PCSC_NOCFR);
190
191         na_mii_negotiate ();
192         na_mii_check_speed ();
193
194         /* Delay 10 millisecond.  (Maybe this should be 1 second.) */
195         udelay (10000);
196
197         /* Turn receive on.
198            Enable statistics register autozero on read.
199            Do not insert MAC address on transmit.
200            Do not enable special test modes.  */
201         SET_EADDR (NETARM_ETH_STL_CFG,
202                    (NETARM_ETH_STLC_AUTOZ | NETARM_ETH_STLC_RXEN));
203
204         /* Set the inter-packet gap delay to 0.96us for MII.
205            The NET+ARM H/W Reference Guide indicates that the Back-to-back IPG
206            Gap Timer Register should be set to 0x15 and the Non Back-to-back IPG
207            Gap Timer Register should be set to 0x00000C12 for the MII PHY. */
208         SET_EADDR (NETARM_ETH_B2B_IPG_GAP_TMR, 0x15);
209         SET_EADDR (NETARM_ETH_NB2B_IPG_GAP_TMR, 0x00000C12);
210
211         /* Add CRC to end of packets.
212            Pad packets to minimum length of 64 bytes.
213            Allow unlimited length transmit packets.
214            Receive all broadcast packets.
215            NOTE:  Multicast addressing is NOT enabled here currently. */
216         SET_EADDR (NETARM_ETH_MAC_CFG,
217                    (NETARM_ETH_MACC_CRCEN |
218                     NETARM_ETH_MACC_PADEN | NETARM_ETH_MACC_HUGEN));
219         SET_EADDR (NETARM_ETH_SAL_FILTER, NETARM_ETH_SALF_BROAD);
220
221         /* enable fifos */
222         SET_EADDR (NETARM_ETH_GEN_CTRL,
223                    (NETARM_ETH_GCR_ERX | NETARM_ETH_GCR_ETX));
224
225         return (0);
226 }
227
228
229 extern int eth_init (bd_t * bd)
230 {
231         reset_eth ();
232         return 0;
233 }
234
235 extern void eth_halt (void)
236 {
237         SET_EADDR (NETARM_ETH_GEN_CTRL, 0);
238 }
239
240 /* Get a data block via Ethernet */
241 extern int eth_rx (void)
242 {
243         int i;
244         unsigned short rxlen;
245         unsigned int *addr;
246         unsigned int rxstatus, lastrxlen;
247         char *pa;
248
249         /* RXBR is 1, data block was received */
250         if ((GET_EADDR (NETARM_ETH_GEN_STAT) & NETARM_ETH_GST_RXBR) == 0)
251                 return 0;
252
253         /* get status register and the length of received block */
254         rxstatus = GET_EADDR (NETARM_ETH_RX_STAT);
255         rxlen = (rxstatus & NETARM_ETH_RXSTAT_SIZE) >> 16;
256
257         if (rxlen == 0)
258                 return 0;
259
260         /* clear RXBR to make fifo available */
261         SET_EADDR (NETARM_ETH_GEN_STAT,
262                    GET_EADDR (NETARM_ETH_GEN_STAT) & ~NETARM_ETH_GST_RXBR);
263
264         /* clear TXBC to make fifo available */
265         /* According to NETARM50 data manual you just have to clear
266            RXBR but that has no effect. Only after clearing TXBC the
267            Fifo becomes readable. */
268         SET_EADDR (NETARM_ETH_GEN_STAT,
269                    GET_EADDR (NETARM_ETH_GEN_STAT) & ~NETARM_ETH_GST_TXBC);
270
271         addr = (unsigned int *) NetRxPackets[0];
272         pa = (char *) NetRxPackets[0];
273
274         /* read the fifo */
275         for (i = 0; i < rxlen / 4; i++) {
276                 *addr = GET_EADDR (NETARM_ETH_FIFO_DAT1);
277                 addr++;
278         }
279
280         if (GET_EADDR (NETARM_ETH_GEN_STAT) & NETARM_ETH_GST_RXREGR) {
281                 /* RXFDB indicates wether the last word is 1,2,3 or 4 bytes long */
282                 lastrxlen =
283                         (GET_EADDR (NETARM_ETH_GEN_STAT) &
284                          NETARM_ETH_GST_RXFDB) >> 28;
285                 *addr = GET_EADDR (NETARM_ETH_FIFO_DAT1);
286                 switch (lastrxlen) {
287                 case 1:
288                         *addr &= 0xff000000;
289                         break;
290                 case 2:
291                         *addr &= 0xffff0000;
292                         break;
293                 case 3:
294                         *addr &= 0xffffff00;
295                         break;
296                 }
297         }
298
299         /* Pass the packet up to the protocol layers. */
300         NetReceive (NetRxPackets[0], rxlen);
301
302         return rxlen;
303 }
304
305 /* Send a data block via Ethernet. */
306 extern int eth_send (volatile void *packet, int length)
307 {
308         int i, length32;
309         char *pa;
310         unsigned int *pa32, lastp = 0, rest;
311
312         pa = (char *) packet;
313         pa32 = (unsigned int *) packet;
314         length32 = length / 4;
315         rest = length % 4;
316
317         /* make sure there's no garbage in the last word */
318         switch (rest) {
319         case 0:
320                 lastp = pa32[length32];
321                 length32--;
322                 break;
323         case 1:
324                 lastp = pa32[length32] & 0x000000ff;
325                 break;
326         case 2:
327                 lastp = pa32[length32] & 0x0000ffff;
328                 break;
329         case 3:
330                 lastp = pa32[length32] & 0x00ffffff;
331                 break;
332         }
333
334         /* write to the fifo */
335         for (i = 0; i < length32; i++)
336                 SET_EADDR (NETARM_ETH_FIFO_DAT1, pa32[i]);
337
338         /* the last word is written to an extra register, this
339            starts the transmission */
340         SET_EADDR (NETARM_ETH_FIFO_DAT2, lastp);
341
342         /* NETARM_ETH_TXSTAT_TXOK should be checked, to know if the transmission
343            went fine. But we can't use the timer for a timeout loop because
344            of it is used already in upper layers. So we just try some times. */
345         i = 0;
346         while (i < 50000) {
347                 if ((GET_EADDR (NETARM_ETH_TX_STAT) & NETARM_ETH_TXSTAT_TXOK)
348                     == NETARM_ETH_TXSTAT_TXOK)
349                         return 0;
350                 i++;
351         }
352
353         printf ("eth_send timeout\n");
354         return 1;
355 }
356
357 #endif /* COMMANDS & CFG_NET */
358
359 #endif /* CONFIG_DRIVER_NETARMETH */