* Patch by André Schwarz, 8 Dec 2003:
[platform/kernel/u-boot.git] / cpu / mips / au1x00_eth.c
1 /* Only eth0 supported for now
2  *
3  * (C) Copyright 2003
4  * Thomas.Lange@corelatus.se
5  *
6  * See file CREDITS for list of people who contributed to this
7  * project.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of
12  * the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22  * MA 02111-1307 USA
23  */
24 #include <config.h>
25
26 #ifdef CONFIG_AU1X00
27
28 #if defined(CFG_DISCOVER_PHY) || (CONFIG_COMMANDS & CFG_CMD_MII)
29 #error "PHY and MII not supported yet"
30 /* We just assume that we are running 100FD for now */
31 /* We all use switches, right? ;-) */
32 #endif
33
34 /* I assume ethernet behaves like au1000 */
35
36 #ifdef CONFIG_AU1000
37 /* Base address differ between cpu:s */
38 #define ETH0_BASE AU1000_ETH0_BASE
39 #define MAC0_ENABLE AU1000_MAC0_ENABLE
40 #else
41 #ifdef CONFIG_AU1100
42 #define ETH0_BASE AU1100_ETH0_BASE
43 #define MAC0_ENABLE AU1100_MAC0_ENABLE
44 #else
45 #ifdef CONFIG_AU1500
46 #define ETH0_BASE AU1500_ETH0_BASE
47 #define MAC0_ENABLE AU1500_MAC0_ENABLE
48 #else
49 #error "No valid cpu set"
50 #endif
51 #endif
52 #endif
53
54 #include <common.h>
55 #include <malloc.h>
56 #include <net.h>
57 #include <command.h>
58 #include <asm/io.h>
59 #include <asm/au1x00.h>
60
61 /* Ethernet Transmit and Receive Buffers */
62 #define DBUF_LENGTH  1520
63 #define PKT_MAXBUF_SIZE         1518
64
65 static char txbuf[DBUF_LENGTH];
66
67 static int next_tx;
68 static int next_rx;
69
70 /* 4 rx and 4 tx fifos */
71 #define NO_OF_FIFOS 4
72
73 typedef struct{
74         u32 status;
75         u32 addr;
76         u32 len; /* Only used for tx */
77         u32 not_used;
78 } mac_fifo_t;
79
80 mac_fifo_t mac_fifo[NO_OF_FIFOS];
81
82 #define MAX_WAIT 1000
83
84 static int au1x00_send(struct eth_device* dev, volatile void *packet, int length){
85         volatile mac_fifo_t *fifo_tx =
86                 (volatile mac_fifo_t*)(MAC0_TX_DMA_ADDR+MAC_TX_BUFF0_STATUS);
87         int i;
88         int res;
89
90         /* tx fifo should always be idle */
91         fifo_tx[next_tx].len = length;
92         fifo_tx[next_tx].addr = (virt_to_phys(packet))|TX_DMA_ENABLE;
93         au_sync();
94
95         udelay(1);
96         i=0;
97         while(!(fifo_tx[next_tx].addr&TX_T_DONE)){
98                 if(i>MAX_WAIT){
99                         printf("TX timeout\n");
100                         break;
101                 }
102                 udelay(1);
103                 i++;
104         }
105
106         /* Clear done bit */
107         fifo_tx[next_tx].addr = 0;
108         fifo_tx[next_tx].len = 0;
109         au_sync();
110
111         res = fifo_tx[next_tx].status;
112
113         next_tx++;
114         if(next_tx>=NO_OF_FIFOS){
115                 next_tx=0;
116         }
117         return(res);
118 }
119
120 static int au1x00_recv(struct eth_device* dev){
121         volatile mac_fifo_t *fifo_rx =
122                 (volatile mac_fifo_t*)(MAC0_RX_DMA_ADDR+MAC_RX_BUFF0_STATUS);
123
124         int length;
125         u32 status;
126
127         for(;;){
128                 if(!(fifo_rx[next_rx].addr&RX_T_DONE)){
129                         /* Nothing has been received */
130                         return(-1);
131                 }
132
133                 status = fifo_rx[next_rx].status;
134
135                 length = status&0x3FFF;
136
137                 if(status&RX_ERROR){
138                         printf("Rx error 0x%x\n", status);
139                 }
140                 else{
141                         /* Pass the packet up to the protocol layers. */
142                         NetReceive(NetRxPackets[next_rx], length - 4);
143                 }
144
145                 fifo_rx[next_rx].addr = (virt_to_phys(NetRxPackets[next_rx]))|RX_DMA_ENABLE;
146
147                 next_rx++;
148                 if(next_rx>=NO_OF_FIFOS){
149                         next_rx=0;
150                 }
151         } /* for */
152
153         return(0); /* Does anyone use this? */
154 }
155
156 static int au1x00_init(struct eth_device* dev, bd_t * bd){
157
158         volatile u32 *macen = (volatile u32*)MAC0_ENABLE;
159         volatile u32 *mac_ctrl = (volatile u32*)(ETH0_BASE+MAC_CONTROL);
160         volatile u32 *mac_addr_high = (volatile u32*)(ETH0_BASE+MAC_ADDRESS_HIGH);
161         volatile u32 *mac_addr_low = (volatile u32*)(ETH0_BASE+MAC_ADDRESS_LOW);
162         volatile u32 *mac_mcast_high = (volatile u32*)(ETH0_BASE+MAC_MCAST_HIGH);
163         volatile u32 *mac_mcast_low = (volatile u32*)(ETH0_BASE+MAC_MCAST_LOW);
164         volatile mac_fifo_t *fifo_tx =
165                 (volatile mac_fifo_t*)(MAC0_TX_DMA_ADDR+MAC_TX_BUFF0_STATUS);
166         volatile mac_fifo_t *fifo_rx =
167                 (volatile mac_fifo_t*)(MAC0_RX_DMA_ADDR+MAC_RX_BUFF0_STATUS);
168         int i;
169
170         next_tx = 0;
171         next_rx = 0;
172
173         /* We have to enable clocks before releasing reset */
174         *macen = MAC_EN_CLOCK_ENABLE;
175         udelay(10);
176
177         /* Enable MAC0 */
178         /* We have to release reset before accessing registers */
179         *macen = MAC_EN_CLOCK_ENABLE|MAC_EN_RESET0|
180                 MAC_EN_RESET1|MAC_EN_RESET2;
181         udelay(10);
182
183         for(i=0;i<NO_OF_FIFOS;i++){
184                 fifo_tx[i].len = 0;
185                 fifo_tx[i].addr = virt_to_phys(&txbuf[0]);
186                 fifo_rx[i].addr = (virt_to_phys(NetRxPackets[i]))|RX_DMA_ENABLE;
187         }
188
189         /* Put mac addr in little endian */
190 #define ea eth_get_dev()->enetaddr
191         *mac_addr_high  =       (ea[5] <<  8) | (ea[4]      ) ;
192         *mac_addr_low   =       (ea[3] << 24) | (ea[2] << 16) |
193                 (ea[1] <<  8) | (ea[0]      ) ;
194 #undef ea
195         *mac_mcast_low = 0;
196         *mac_mcast_high = 0;
197
198         /* Make sure the MAC buffer is in the correct endian mode */
199 #ifdef __LITTLE_ENDIAN
200         *mac_ctrl = MAC_FULL_DUPLEX;
201         udelay(1);
202         *mac_ctrl = MAC_FULL_DUPLEX|MAC_RX_ENABLE|MAC_TX_ENABLE;
203 #else
204         *mac_ctrl = MAC_BIG_ENDIAN|MAC_FULL_DUPLEX;
205         udelay(1);
206         *mac_ctrl = MAC_BIG_ENDIAN|MAC_FULL_DUPLEX|MAC_RX_ENABLE|MAC_TX_ENABLE;
207 #endif
208
209         return(1);
210 }
211
212 static void au1x00_halt(struct eth_device* dev){
213 }
214
215 int au1x00_enet_initialize(bd_t *bis){
216         struct eth_device* dev;
217
218         dev = (struct eth_device*) malloc(sizeof *dev);
219         memset(dev, 0, sizeof *dev);
220
221         sprintf(dev->name, "Au1X00 ETHERNET");
222         dev->iobase = 0;
223         dev->priv   = 0;
224         dev->init   = au1x00_init;
225         dev->halt   = au1x00_halt;
226         dev->send   = au1x00_send;
227         dev->recv   = au1x00_recv;
228
229         eth_register(dev);
230
231         return 1;
232 }
233
234 #endif /* CONFIG_AU1X00 */