LibXenon
Bare-metal Xbox 360 homebrew library
Loading...
Searching...
No Matches
enet.c
Go to the documentation of this file.
1//#include "lwipopts.h"
2#include "lwip/opt.h"
3#include "lwip/def.h"
4#include "lwip/mem.h"
5#include "lwip/pbuf.h"
6#include "lwip/sys.h"
7#include "lwip/stats.h"
8#include "lwip/timers.h"
9#include "netif/etharp.h"
10
11#include <time/time.h>
13#include <ppc/cache.h>
14#include <pci/io.h>
16
17#define TX_DESCRIPTOR_NUM 0x10
18#define RX_DESCRIPTOR_NUM 0x10
19#define MTU 1528
20#define MEM(x) (0x80000000|(long)(x))
21
23{
27
31
32 struct eth_addr *ethaddr;
33};
34
35static int enet_open(struct netif *ctx);
36static struct pbuf *enet_linkinput(struct enet_context *context);
37static err_t enet_linkoutput(struct netif *netif, struct pbuf *p);
38//static err_t enet_output(struct netif *netif, struct pbuf *p, ip_addr_t *ipaddr);
39
40// lazy lazy lazy.. just put the pre-shifted version here
41static unsigned char phy_id_bit = (1 << 6);
42
43static unsigned short phy_read(int addr)
44{
45 write32n(0xea001444, __builtin_bswap32((addr << 11) | phy_id_bit | 0x10));
46 do {mdelay(1);} while (__builtin_bswap32(read32n(0xea001444)) & 0x10);
47 return __builtin_bswap32(read32n(0xea001444)) >> 16;
48}
49
50static void phy_write(int addr, unsigned short data)
51{
52 write32n(0xea001444, __builtin_bswap32((addr << 11) | phy_id_bit | 0x30 | (data << 16)));
53 do {mdelay(1);} while (__builtin_bswap32(read32n(0xea001444)) & 0x10);
54}
55
56static void wait_empty_tx(struct enet_context *context)
57{
58// printf(">> CURRENT TX PTR: %08x\n", __builtin_bswap32(read32n(0xea00140c)));
59 while (__builtin_bswap32(context->tx_descriptor_base[context->tx_descriptor_wptr * 4 + 1]) & 0x80000000); // busy
60}
61
62static inline int virt_to_phys(volatile void *ptr)
63{
64 return ((long)ptr) & 0x7FFFFFFF;
65}
66
67static void tx_data(struct enet_context *context, unsigned char *data, int len)
68{
69 wait_empty_tx(context);
70 int descnr = context->tx_descriptor_wptr;
71 volatile uint32_t *descr = context->tx_descriptor_base + descnr * 4;
72
73// printf("using tx descriptor %d\n", descnr);
74
75// printf("really before %08x %08x %08x %08x\n", descr[0], descr[1], descr[2], descr[3]);
76
77 descr[0] = __builtin_bswap32(len);
78 descr[2] = __builtin_bswap32(virt_to_phys(data));
79 descr[3] = __builtin_bswap32(len | ((descnr == TX_DESCRIPTOR_NUM - 1) ? 0x80000000 : 0));
80 descr[1] = __builtin_bswap32(0xc0230000); // ownership, ...
81
82// printf("data at %08x\n", virt_to_phys(data));
83
84 memdcbst((void*)descr, 0x10);
85
86// printf("before %08x %08x %08x %08x\n", descr[0], descr[1], descr[2], descr[3]);
87
88 write32n(0xea001400, read32n(0xea001400) | __builtin_bswap32(0x10)); // Q0_START
89
90// printf("after %08x %08x %08x %08x\n", descr[0], descr[1], descr[2], descr[3]);
91
92 context->tx_descriptor_wptr++;
94
95 write32n(0xea001410, __builtin_bswap32(0x00101c11)); // enable RX
96
97// printf("TX\n");
98}
99
100static void tx_init(struct enet_context *context, void *base)
101{
102 int i;
103
104 context->tx_descriptor_base = base;
105 context->tx_descriptor_wptr = 0;
106
107 for (i=0; i < TX_DESCRIPTOR_NUM; ++i)
108 {
109 /* tx descriptors */
110 context->tx_descriptor_base[i * 4 + 0] = 0;
111 context->tx_descriptor_base[i * 4 + 1] = 0;
112 context->tx_descriptor_base[i * 4 + 2] = 0;
113 if (i != TX_DESCRIPTOR_NUM - 1)
114 context->tx_descriptor_base[i * 4 + 3] = 0;
115 else
116 context->tx_descriptor_base[i * 4 + 3] = __builtin_bswap32(0x80000000);
117 }
118
119 memdcbst((void*)context->tx_descriptor_base, TX_DESCRIPTOR_NUM * 0x10);
120
121 context->tx_buffer_base = base + TX_DESCRIPTOR_NUM * 0x10;
122
123 write32n(0xea001400, __builtin_bswap32(0x00001c00));
124 write32n(0xea001404, __builtin_bswap32(virt_to_phys(context->tx_descriptor_base)));
125 write32n(0xea001400, __builtin_bswap32(0x00011c00));
126 write32n(0xea001404, __builtin_bswap32(virt_to_phys(context->tx_descriptor_base)));
127 write32n(0xea001400, __builtin_bswap32(0x00001c00));
128}
129
130
131static void rx_init(struct enet_context *context, void *base)
132{
133 int i;
134
135 context->rx_descriptor_base = base;
136 context->rx_receive_base = base + RX_DESCRIPTOR_NUM * 0x10;
137 context->rx_descriptor_rptr = 0;
138
139 /* setup rx descriptors */
140 for (i=0; i < RX_DESCRIPTOR_NUM; ++i)
141 {
142 context->rx_descriptor_base[i * 4 + 0] = __builtin_bswap32(0);
143 context->rx_descriptor_base[i * 4 + 1] = __builtin_bswap32(0xc0000000);
144 context->rx_descriptor_base[i * 4 + 2] = __builtin_bswap32(virt_to_phys(context->rx_receive_base + i * MTU));
145 context->rx_descriptor_base[i * 4 + 3] = __builtin_bswap32(MTU | ((i == RX_DESCRIPTOR_NUM - 1) ? 0x80000000 : 0));
146 }
147 memdcbst((void*)context->rx_descriptor_base, RX_DESCRIPTOR_NUM * 0x10);
148
149 write32n(0xea001414, __builtin_bswap32(virt_to_phys(context->rx_descriptor_base))); // RX queue start
150 write32n(0xea001440, 0x1100004);
151 write32n(0xea001450, 0);
152}
153
154
155//static void arp_timer(void *arg)
156//{
157// etharp_tmr();
158// //sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
159//}
160
162{
163 struct enet_context * context;
164
165 context = (struct enet_context *)mem_malloc(sizeof(struct enet_context));
166 if(context == NULL) {
167 printf("enet: Failed to allocate context memory.\n");
168 return ERR_MEM;
169 } else {
170 memset(context, 0, sizeof(struct enet_context));
171
172 // enet_write_mac_address(context, m);
174 }
175
176 netif->state = context;
177 netif->name[0] = 'e';
178 netif->name[1] = 'n';
179 netif->output = etharp_output; //enet_output; //lwip 1.3.0
180 netif->linkoutput = enet_linkoutput;
181 context->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]);
182
183 netif->hwaddr_len = 6;
184 netif->mtu = 1500;
186
187#if LWIP_NETIF_HOSTNAME
188 netif->hostname = "XeLL";
189#endif
190
191 //printf("NETIF at %p\n", netif);
192
193 enet_open(netif);
194 etharp_init();
195 //sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
196 return ERR_OK;
197}
198
199
200static int enet_open(struct netif *netif)
201{
202 int tries=10;
203 struct enet_context *context = (struct enet_context *) netif->state;
204
205 //printf("NIC reset\n");
206 write32n(0xea001424, 0); // disable interrupts
207 write32n(0xea001428, 0x01805508);
208 //printf("reset: %08x\n", read32n(0xea001428));
209 udelay(100);
210 write32n(0xea001428, 0x01005508);
211
212 write32n(0xea001444, __builtin_bswap32(4));
213 udelay(100);
214 write32n(0xea001444, __builtin_bswap32(0));
215 //printf("1478 before: %08x\n", read32n(0xea001478));
216 // Set MTU
217 write32n(0xea001478, 0xf2050000); //printf("1478: %08x\n", read32n(0xea001478));
218
219 // important
220 write32n(0xea001450, 0x60230000);
221
222 // mac 1+2
223 write32n(0xea001478, 0xF2050000 | (netif->hwaddr[0] << 8) | (netif->hwaddr[1] << 0));
224 write32n(0xea00147c, (netif->hwaddr[2] << 24) | (netif->hwaddr[3] << 16) | (netif->hwaddr[4] << 8) | (netif->hwaddr[5] << 0) );
225
226 write32n(0xea001460, 0x380e0000 | (netif->hwaddr[0] << 8) | (netif->hwaddr[1] << 0));
227 write32n(0xea001464, (netif->hwaddr[2] << 24) | (netif->hwaddr[3] << 16) | (netif->hwaddr[4] << 8) | (netif->hwaddr[5] << 0) );
228
229 // multicast hash
230 write32n(0xea001468, 0);
231 write32n(0xea00146c, 0);
232
233 write32n(0xea001400, 0x001c0000);
234 write32n(0xea001410, __builtin_bswap32(0x00101c00));
235
236 write32n(0xea001440, 0x01190004);
237
238 void *base = (void*)memalign(0x10000,0x10000);
239
240 //printf("init tx\n");
241 tx_init(context, (void*)MEM(base));
242 //printf("init rx\n");
243 rx_init(context, (void*)MEM(base + 0x8000));
244
245 write32n(0xea001440, 0x01100004);
246
247#if 1
248 printf("Reinit PHY...\n");
249
250 // get PHY out of reset state
251 xenon_gpio_control(0,0,0x10);
252 xenon_gpio_control(4,0,0x10);
253
254 // detect corona/KSB systems that have the PHY at a different address
255 if (xenon_get_PCIBridgeRevisionID() >= 0x70)
256 phy_id_bit = 0;
257
258 phy_write(0, 0x9000);
259
260 while (phy_read(0) & 0x8000){
261 mdelay(500);
262 tries--;
263 if (tries<=0) break;
264 };
265
266// phy_write(0x10, 0x8058);
267// phy_write(4, 0x5e1);
268// phy_write(0x14, 0x4048);
269
270 int linkstate = phy_read(1);
271 if (!(linkstate & 4))
272 {
273 tries=10;
274
275 printf("Waiting for link...");
276 while (!(phy_read(1) & 4)){
277 mdelay(500);
278 tries--;
279 if (tries<=0) break;
280 };
281 }
282
283 if (phy_read(1) & 4) {
284 printf("link up!\n");
286 } else {
287 printf("link still down.\n");
289 }
290#endif
291
292
293// write32n(0xea001428, 0x01805508);
294 write32n(0xea001428, 0x01005508);
295 write32n(0xea001410, __builtin_bswap32(0x00101c11)); // enable RX
296 write32n(0xea001400, __builtin_bswap32(0x00001c01)); // enable TX
297
298 return 0;
299}
300
301static struct pbuf *enet_linkinput(struct enet_context *context)
302{
303 volatile uint32_t *d = context->rx_descriptor_base + context->rx_descriptor_rptr * 4;
304 memdcbf((void*)d, 0x10);
305 if (__builtin_bswap32(d[1]) & 0x80000000) /* check ownership */
306 {
307// printf("no data!\n");
308 return 0;
309 }
310
311// printf("RX descriptor %d contains data!\n", context->rx_descriptor_rptr);
312// printf("%08x %08x %08x %08x\n",
313// __builtin_bswap32(d[0]),
314// __builtin_bswap32(d[1]),
315// __builtin_bswap32(d[2]),
316// __builtin_bswap32(d[3]));
317
318 int size = __builtin_bswap32(d[0]) & 0xFFFF;
319 void *phys_addr = context->rx_receive_base + context->rx_descriptor_rptr * MTU;
320 memdcbf(phys_addr, size);
321
322 // hexdump(phys_addr, size);
323
324 struct pbuf *p = pbuf_alloc(PBUF_RAW, size, PBUF_POOL), *q;
325
326 if (p != NULL) {
327 int rptr = 0;
328 /* We iterate over the pbuf chain until we have read the entire
329 packet into the pbuf. */
330 for(q = p; q != NULL; q = q->next) {
331 /* Read enough bytes to fill this pbuf in the chain. The
332 available data in the pbuf is given by the q->len
333 variable. */
334 memcpy(q->payload, phys_addr + rptr, q->len);
335 rptr += q->len;
336 }
337#ifdef LINK_STATS
338 lwip_stats.link.recv++;
339#endif /* LINK_STATS */
340 } else {
341#ifdef LINK_STATS
342 lwip_stats.link.memerr++;
343 lwip_stats.link.drop++;
344#endif /* LINK_STATS */
345 }
346
347 /* reclaim descriptor */
348 d[2] = __builtin_bswap32(virt_to_phys(context->rx_receive_base + context->rx_descriptor_rptr * MTU));
349 d[3] = __builtin_bswap32(MTU | ((context->rx_descriptor_rptr == RX_DESCRIPTOR_NUM - 1) ? 0x80000000 : 0));
350 d[0] = __builtin_bswap32(0);
351 d[1] = __builtin_bswap32(0xc0000000);
352 memdcbst((void*)d, 0x10);
353
354 context->rx_descriptor_rptr++;
356
357 return p;
358}
359
360static err_t enet_linkoutput(struct netif *netif, struct pbuf *p)
361{
362 struct pbuf *q;
363 struct enet_context *context = (struct enet_context *) netif->state;
364
365// printf("enet linkoutput\n");
366
367 void *dstptr = context->tx_buffer_base + context->tx_descriptor_wptr * MTU;
368 int offset = 0;
369
370 for(q = p; q != NULL; q = q->next) {
371 memcpy(dstptr + offset, q->payload, q->len);
372 offset += q->len;
373 }
374
375 memdcbst((void*)dstptr, p->tot_len);
376 tx_data(context, dstptr, p->tot_len);
377
378// printf("ok, data transmitted!\n");
379
380 return 0;
381}
382
383/*
384static err_t
385enet_output(struct netif *netif, struct pbuf *p,
386 struct ip_addr_t *ipaddr)
387{
388// printf("ENET OUTPUT\n");
389 //return etharp_output(netif, ipaddr, p);
390 return etharp_output(netif, p, ipaddr); //lwip 1.3.0
391}
392*/
393
394static void
395enet_input(struct netif *netif)
396{
397 struct enet_context *context = (struct enet_context *) netif->state;
398 struct eth_hdr *ethhdr;
399 struct pbuf *p;
400
401 p = enet_linkinput(context);
402
403 if (p == NULL)
404 return;
405
406#if LINK_STATS
407 lwip_stats.link.recv++;
408#endif /* LINK_STATS */
409
410 ethhdr = p->payload;
411
412 /* pass to network layer */
413 if (ethernet_input(p, netif) != ERR_OK) {
414 pbuf_free(p);
415 p = NULL;
416 }
417}
418
419int cnt;
420
421void
423{
424// printf("\r%08x", mfdec());
425 enet_input(netif);
426}
427
428
429void enet_quiesce(void)
430{
431 write32n(0xea001400, 0);
432 write32n(0xea001410, 0);
433}
#define base
#define NULL
Definition: def.h:47
void memdcbf(void *addr, int len)
void memdcbst(void *addr, int len)
void enet_poll(struct netif *netif)
Definition: enet.c:422
#define MTU
Definition: enet.c:19
err_t enet_init(struct netif *netif)
Definition: enet.c:161
#define RX_DESCRIPTOR_NUM
Definition: enet.c:18
void enet_quiesce(void)
Definition: enet.c:429
int cnt
Definition: enet.c:419
#define MEM(x)
Definition: enet.c:20
#define TX_DESCRIPTOR_NUM
Definition: enet.c:17
#define ERR_MEM
Definition: err.h:53
#define ERR_OK
Definition: err.h:52
s8_t err_t
Definition: err.h:47
u32 ptr
Definition: iso9660.c:557
u32 size
Definition: iso9660.c:558
u32 uint32_t
Definition: libfdt_env.h:11
void * mem_malloc(mem_size_t size)
Definition: mem.c:494
void netif_set_link_down(struct netif *netif)
Definition: netif.c:574
void netif_set_link_up(struct netif *netif)
Definition: netif.c:535
#define NETIF_FLAG_ETHARP
Definition: netif.h:88
#define NETIF_FLAG_BROADCAST
Definition: netif.h:72
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
Definition: pbuf.c:207
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:618
@ PBUF_POOL
Definition: pbuf.h:61
@ PBUF_RAW
Definition: pbuf.h:54
void * rx_receive_base
Definition: enet.c:25
struct eth_addr * ethaddr
Definition: enet.c:32
int rx_descriptor_rptr
Definition: enet.c:26
volatile uint32_t * tx_descriptor_base
Definition: enet.c:28
void * tx_buffer_base
Definition: enet.c:30
volatile uint32_t * rx_descriptor_base
Definition: enet.c:24
int tx_descriptor_wptr
Definition: enet.c:29
Definition: netif.h:136
u8_t flags
Definition: netif.h:192
char name[2]
Definition: netif.h:194
void * state
Definition: netif.h:172
netif_output_fn output
Definition: netif.h:151
u8_t hwaddr[NETIF_MAX_HWADDR_LEN]
Definition: netif.h:190
u16_t mtu
Definition: netif.h:186
netif_linkoutput_fn linkoutput
Definition: netif.h:155
u8_t hwaddr_len
Definition: netif.h:188
Definition: pbuf.h:79
u16_t tot_len
Definition: pbuf.h:93
struct pbuf * next
Definition: pbuf.h:81
u16_t len
Definition: pbuf.h:96
void * payload
Definition: pbuf.h:84
void mdelay(int u)
Definition: time.c:17
void udelay(int u)
Definition: time.c:12
unsigned int xenon_get_PCIBridgeRevisionID()
Definition: xb360.c:650
void xenon_config_get_mac_addr(unsigned char *hwaddr)
Definition: xenon_config.c:74
void xenon_gpio_control(uint32_t reg, uint32_t clear, uint32_t set)
Definition: xenon_gpio.c:6
union @15 data