1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34#include <command.h>
35#include <common.h>
36#include <asm/arch/ep93xx.h>
37#include <asm/io.h>
38#include <malloc.h>
39#include <miiphy.h>
40#include <linux/types.h>
41#include "ep93xx_eth.h"
42
43#define GET_PRIV(eth_dev) ((struct ep93xx_priv *)(eth_dev)->priv)
44#define GET_REGS(eth_dev) (GET_PRIV(eth_dev)->regs)
45
46
47static int ep93xx_miiphy_read(const char * const dev, unsigned char const addr,
48 unsigned char const reg, unsigned short * const value);
49static int ep93xx_miiphy_write(const char * const dev, unsigned char const addr,
50 unsigned char const reg, unsigned short const value);
51
52#if defined(EP93XX_MAC_DEBUG)
53
54
55
56static void dump_dev(struct eth_device *dev)
57{
58 struct ep93xx_priv *priv = GET_PRIV(dev);
59 int i;
60
61 printf("\ndump_dev()\n");
62 printf(" rx_dq.base %p\n", priv->rx_dq.base);
63 printf(" rx_dq.current %p\n", priv->rx_dq.current);
64 printf(" rx_dq.end %p\n", priv->rx_dq.end);
65 printf(" rx_sq.base %p\n", priv->rx_sq.base);
66 printf(" rx_sq.current %p\n", priv->rx_sq.current);
67 printf(" rx_sq.end %p\n", priv->rx_sq.end);
68
69 for (i = 0; i < NUMRXDESC; i++)
70 printf(" rx_buffer[%2.d] %p\n", i, NetRxPackets[i]);
71
72 printf(" tx_dq.base %p\n", priv->tx_dq.base);
73 printf(" tx_dq.current %p\n", priv->tx_dq.current);
74 printf(" tx_dq.end %p\n", priv->tx_dq.end);
75 printf(" tx_sq.base %p\n", priv->tx_sq.base);
76 printf(" tx_sq.current %p\n", priv->tx_sq.current);
77 printf(" tx_sq.end %p\n", priv->tx_sq.end);
78}
79
80
81
82
83static void dump_rx_status_queue(struct eth_device *dev)
84{
85 struct ep93xx_priv *priv = GET_PRIV(dev);
86 int i;
87
88 printf("\ndump_rx_status_queue()\n");
89 printf(" descriptor address word1 word2\n");
90 for (i = 0; i < NUMRXDESC; i++) {
91 printf(" [ %p ] %08X %08X\n",
92 priv->rx_sq.base + i,
93 (priv->rx_sq.base + i)->word1,
94 (priv->rx_sq.base + i)->word2);
95 }
96}
97
98
99
100
101static void dump_rx_descriptor_queue(struct eth_device *dev)
102{
103 struct ep93xx_priv *priv = GET_PRIV(dev);
104 int i;
105
106 printf("\ndump_rx_descriptor_queue()\n");
107 printf(" descriptor address word1 word2\n");
108 for (i = 0; i < NUMRXDESC; i++) {
109 printf(" [ %p ] %08X %08X\n",
110 priv->rx_dq.base + i,
111 (priv->rx_dq.base + i)->word1,
112 (priv->rx_dq.base + i)->word2);
113 }
114}
115
116
117
118
119static void dump_tx_descriptor_queue(struct eth_device *dev)
120{
121 struct ep93xx_priv *priv = GET_PRIV(dev);
122 int i;
123
124 printf("\ndump_tx_descriptor_queue()\n");
125 printf(" descriptor address word1 word2\n");
126 for (i = 0; i < NUMTXDESC; i++) {
127 printf(" [ %p ] %08X %08X\n",
128 priv->tx_dq.base + i,
129 (priv->tx_dq.base + i)->word1,
130 (priv->tx_dq.base + i)->word2);
131 }
132}
133
134
135
136
137static void dump_tx_status_queue(struct eth_device *dev)
138{
139 struct ep93xx_priv *priv = GET_PRIV(dev);
140 int i;
141
142 printf("\ndump_tx_status_queue()\n");
143 printf(" descriptor address word1\n");
144 for (i = 0; i < NUMTXDESC; i++) {
145 printf(" [ %p ] %08X\n",
146 priv->rx_sq.base + i,
147 (priv->rx_sq.base + i)->word1);
148 }
149}
150#else
151#define dump_dev(x)
152#define dump_rx_descriptor_queue(x)
153#define dump_rx_status_queue(x)
154#define dump_tx_descriptor_queue(x)
155#define dump_tx_status_queue(x)
156#endif
157
158
159
160
161
162static void ep93xx_mac_reset(struct eth_device *dev)
163{
164 struct mac_regs *mac = GET_REGS(dev);
165 uint32_t value;
166
167 debug("+ep93xx_mac_reset");
168
169 value = readl(&mac->selfctl);
170 value |= SELFCTL_RESET;
171 writel(value, &mac->selfctl);
172
173 while (readl(&mac->selfctl) & SELFCTL_RESET)
174 ;
175
176 debug("-ep93xx_mac_reset");
177}
178
179
180static int ep93xx_eth_open(struct eth_device *dev, bd_t *bd)
181{
182 struct ep93xx_priv *priv = GET_PRIV(dev);
183 struct mac_regs *mac = GET_REGS(dev);
184 uchar *mac_addr = dev->enetaddr;
185 int i;
186
187 debug("+ep93xx_eth_open");
188
189
190 ep93xx_mac_reset(dev);
191
192
193 priv->tx_dq.current = priv->tx_dq.base;
194 priv->tx_dq.end = (priv->tx_dq.base + NUMTXDESC);
195
196 priv->tx_sq.current = priv->tx_sq.base;
197 priv->tx_sq.end = (priv->tx_sq.base + NUMTXDESC);
198
199 priv->rx_dq.current = priv->rx_dq.base;
200 priv->rx_dq.end = (priv->rx_dq.base + NUMRXDESC);
201
202 priv->rx_sq.current = priv->rx_sq.base;
203 priv->rx_sq.end = (priv->rx_sq.base + NUMRXDESC);
204
205
206
207
208
209
210 writel((uint32_t)priv->tx_dq.base, &mac->txdq.badd);
211 writel((uint32_t)priv->tx_dq.base, &mac->txdq.curadd);
212 writel(sizeof(struct tx_descriptor) * NUMTXDESC, &mac->txdq.blen);
213
214 writel((uint32_t)priv->tx_sq.base, &mac->txstsq.badd);
215 writel((uint32_t)priv->tx_sq.base, &mac->txstsq.curadd);
216 writel(sizeof(struct tx_status) * NUMTXDESC, &mac->txstsq.blen);
217
218 writel(0x00040000, &mac->txdthrshld);
219 writel(0x00040000, &mac->txststhrshld);
220
221 writel((TXSTARTMAX << 0) | (PKTSIZE_ALIGN << 16), &mac->maxfrmlen);
222 writel(BMCTL_TXEN, &mac->bmctl);
223
224
225
226
227
228
229 writel((uint32_t)priv->rx_dq.base, &mac->rxdq.badd);
230 writel((uint32_t)priv->rx_dq.base, &mac->rxdq.curadd);
231 writel(sizeof(struct rx_descriptor) * NUMRXDESC, &mac->rxdq.blen);
232
233 writel((uint32_t)priv->rx_sq.base, &mac->rxstsq.badd);
234 writel((uint32_t)priv->rx_sq.base, &mac->rxstsq.curadd);
235 writel(sizeof(struct rx_status) * NUMRXDESC, &mac->rxstsq.blen);
236
237 writel(0x00040000, &mac->rxdthrshld);
238
239 writel(BMCTL_RXEN, &mac->bmctl);
240
241 writel(0x00040000, &mac->rxststhrshld);
242
243
244 while (!(readl(&mac->bmsts) & BMSTS_RXACT))
245 ;
246
247
248
249
250
251
252 for (i = 0; i < NUMRXDESC; i++) {
253
254 (priv->rx_dq.base + i)->word1 = (uint32_t)NetRxPackets[i];
255
256
257 (priv->rx_dq.base + i)->word2 = PKTSIZE_ALIGN;
258 }
259
260 memset(priv->tx_dq.base, 0,
261 (sizeof(struct tx_descriptor) * NUMTXDESC));
262 memset(priv->rx_sq.base, 0,
263 (sizeof(struct rx_status) * NUMRXDESC));
264 memset(priv->tx_sq.base, 0,
265 (sizeof(struct tx_status) * NUMTXDESC));
266
267 writel(NUMRXDESC, &mac->rxdqenq);
268 writel(NUMRXDESC, &mac->rxstsqenq);
269
270
271 writel(AFP_IAPRIMARY, &mac->afp);
272 writel(mac_addr[0] | (mac_addr[1] << 8) |
273 (mac_addr[2] << 16) | (mac_addr[3] << 24),
274 &mac->indad);
275 writel(mac_addr[4] | (mac_addr[5] << 8), &mac->indad_upper);
276
277
278 writel(RXCTL_IA0 | RXCTL_BA | RXCTL_SRXON |
279 RXCTL_RCRCA | RXCTL_MA, &mac->rxctl);
280 writel(TXCTL_STXON, &mac->txctl);
281
282
283 dump_dev(dev);
284 dump_rx_descriptor_queue(dev);
285 dump_rx_status_queue(dev);
286 dump_tx_descriptor_queue(dev);
287 dump_tx_status_queue(dev);
288
289 debug("-ep93xx_eth_open");
290
291 return 1;
292}
293
294
295
296
297
298static void ep93xx_eth_close(struct eth_device *dev)
299{
300 struct mac_regs *mac = GET_REGS(dev);
301
302 debug("+ep93xx_eth_close");
303
304 writel(0x00000000, &mac->rxctl);
305 writel(0x00000000, &mac->txctl);
306
307 debug("-ep93xx_eth_close");
308}
309
310
311
312
313
314static int ep93xx_eth_rcv_packet(struct eth_device *dev)
315{
316 struct mac_regs *mac = GET_REGS(dev);
317 struct ep93xx_priv *priv = GET_PRIV(dev);
318 int len = -1;
319
320 debug("+ep93xx_eth_rcv_packet");
321
322 if (RX_STATUS_RFP(priv->rx_sq.current)) {
323 if (RX_STATUS_RWE(priv->rx_sq.current)) {
324
325
326
327
328
329
330
331
332
333 len = RX_STATUS_FRAME_LEN(priv->rx_sq.current);
334
335 NetReceive((uchar *)priv->rx_dq.current->word1, len);
336
337 debug("reporting %d bytes...\n", len);
338 } else {
339
340 error("packet rx error, status %08X %08X",
341 priv->rx_sq.current->word1,
342 priv->rx_sq.current->word2);
343 dump_rx_descriptor_queue(dev);
344 dump_rx_status_queue(dev);
345 }
346
347
348
349
350
351
352
353 memset((void *)priv->rx_sq.current, 0,
354 sizeof(struct rx_status));
355
356 priv->rx_sq.current++;
357 if (priv->rx_sq.current >= priv->rx_sq.end)
358 priv->rx_sq.current = priv->rx_sq.base;
359
360 priv->rx_dq.current++;
361 if (priv->rx_dq.current >= priv->rx_dq.end)
362 priv->rx_dq.current = priv->rx_dq.base;
363
364
365
366
367
368
369 writel(1, &mac->rxdqenq);
370 writel(1, &mac->rxstsqenq);
371 } else {
372 len = 0;
373 }
374
375 debug("-ep93xx_eth_rcv_packet %d", len);
376 return len;
377}
378
379
380
381
382static int ep93xx_eth_send_packet(struct eth_device *dev,
383 volatile void * const packet, int const length)
384{
385 struct mac_regs *mac = GET_REGS(dev);
386 struct ep93xx_priv *priv = GET_PRIV(dev);
387 int ret = -1;
388
389 debug("+ep93xx_eth_send_packet");
390
391
392 BUG_ON(packet == NULL);
393
394
395
396
397
398
399
400
401 priv->tx_dq.current->word1 = (uint32_t)packet;
402
403
404 priv->tx_dq.current->word2 = length | TX_DESC_EOF;
405
406
407 priv->tx_sq.current->word1 = 0;
408
409
410 writel(1, &mac->txdqenq);
411
412
413 while (!TX_STATUS_TXFP(priv->tx_sq.current))
414 ;
415
416 if (!TX_STATUS_TXWE(priv->tx_sq.current)) {
417 error("packet tx error, status %08X",
418 priv->tx_sq.current->word1);
419 dump_tx_descriptor_queue(dev);
420 dump_tx_status_queue(dev);
421
422
423 goto eth_send_out;
424 }
425
426 ret = 0;
427
428
429eth_send_out:
430 debug("-ep93xx_eth_send_packet %d", ret);
431 return ret;
432}
433
434#if defined(CONFIG_MII)
435int ep93xx_miiphy_initialize(bd_t * const bd)
436{
437 miiphy_register("ep93xx_eth0", ep93xx_miiphy_read, ep93xx_miiphy_write);
438 return 0;
439}
440#endif
441
442
443
444
445
446
447
448int ep93xx_eth_initialize(u8 dev_num, int base_addr)
449{
450 int ret = -1;
451 struct eth_device *dev;
452 struct ep93xx_priv *priv;
453
454 debug("+ep93xx_eth_initialize");
455
456 priv = malloc(sizeof(*priv));
457 if (!priv) {
458 error("malloc() failed");
459 goto eth_init_failed_0;
460 }
461 memset(priv, 0, sizeof(*priv));
462
463 priv->regs = (struct mac_regs *)base_addr;
464
465 priv->tx_dq.base = calloc(NUMTXDESC,
466 sizeof(struct tx_descriptor));
467 if (priv->tx_dq.base == NULL) {
468 error("calloc() failed");
469 goto eth_init_failed_1;
470 }
471
472 priv->tx_sq.base = calloc(NUMTXDESC,
473 sizeof(struct tx_status));
474 if (priv->tx_sq.base == NULL) {
475 error("calloc() failed");
476 goto eth_init_failed_2;
477 }
478
479 priv->rx_dq.base = calloc(NUMRXDESC,
480 sizeof(struct rx_descriptor));
481 if (priv->rx_dq.base == NULL) {
482 error("calloc() failed");
483 goto eth_init_failed_3;
484 }
485
486 priv->rx_sq.base = calloc(NUMRXDESC,
487 sizeof(struct rx_status));
488 if (priv->rx_sq.base == NULL) {
489 error("calloc() failed");
490 goto eth_init_failed_4;
491 }
492
493 dev = malloc(sizeof *dev);
494 if (dev == NULL) {
495 error("malloc() failed");
496 goto eth_init_failed_5;
497 }
498 memset(dev, 0, sizeof *dev);
499
500 dev->iobase = base_addr;
501 dev->priv = priv;
502 dev->init = ep93xx_eth_open;
503 dev->halt = ep93xx_eth_close;
504 dev->send = ep93xx_eth_send_packet;
505 dev->recv = ep93xx_eth_rcv_packet;
506
507 sprintf(dev->name, "ep93xx_eth-%hu", dev_num);
508
509 eth_register(dev);
510
511
512 ret = 1;
513 goto eth_init_done;
514
515eth_init_failed_5:
516 free(priv->rx_sq.base);
517
518
519eth_init_failed_4:
520 free(priv->rx_dq.base);
521
522
523eth_init_failed_3:
524 free(priv->tx_sq.base);
525
526
527eth_init_failed_2:
528 free(priv->tx_dq.base);
529
530
531eth_init_failed_1:
532 free(priv);
533
534
535eth_init_failed_0:
536
537
538eth_init_done:
539 debug("-ep93xx_eth_initialize %d", ret);
540 return ret;
541}
542
543#if defined(CONFIG_MII)
544
545
546
547
548#define MII_ADDRESS_MAX 31
549
550
551
552
553#define MII_REGISTER_MAX 31
554
555
556
557
558static int ep93xx_miiphy_read(const char * const dev, unsigned char const addr,
559 unsigned char const reg, unsigned short * const value)
560{
561 struct mac_regs *mac = (struct mac_regs *)MAC_BASE;
562 int ret = -1;
563 uint32_t self_ctl;
564
565 debug("+ep93xx_miiphy_read");
566
567
568 BUG_ON(dev == NULL);
569 BUG_ON(addr > MII_ADDRESS_MAX);
570 BUG_ON(reg > MII_REGISTER_MAX);
571 BUG_ON(value == NULL);
572
573
574
575
576
577
578 self_ctl = readl(&mac->selfctl);
579#if defined(CONFIG_MII_SUPPRESS_PREAMBLE)
580 writel(self_ctl & ~(1 << 8), &mac->selfctl);
581#endif
582
583 while (readl(&mac->miists) & MIISTS_BUSY)
584 ;
585
586
587
588
589
590 writel(MIICMD_OPCODE_READ | ((uint32_t)addr << 5) | (uint32_t)reg,
591 &mac->miicmd);
592 while (readl(&mac->miists) & MIISTS_BUSY)
593 ;
594
595 *value = (unsigned short)readl(&mac->miidata);
596
597
598 writel(self_ctl, &mac->selfctl);
599
600 ret = 0;
601
602
603 debug("-ep93xx_miiphy_read");
604 return ret;
605}
606
607
608
609
610static int ep93xx_miiphy_write(const char * const dev, unsigned char const addr,
611 unsigned char const reg, unsigned short const value)
612{
613 struct mac_regs *mac = (struct mac_regs *)MAC_BASE;
614 int ret = -1;
615 uint32_t self_ctl;
616
617 debug("+ep93xx_miiphy_write");
618
619
620 BUG_ON(dev == NULL);
621 BUG_ON(addr > MII_ADDRESS_MAX);
622 BUG_ON(reg > MII_REGISTER_MAX);
623
624
625
626
627
628
629 self_ctl = readl(&mac->selfctl);
630#if defined(CONFIG_MII_SUPPRESS_PREAMBLE)
631 writel(self_ctl & ~(1 << 8), &mac->selfctl);
632#endif
633
634 while (readl(&mac->miists) & MIISTS_BUSY)
635 ;
636
637
638 writel((uint32_t)value, &mac->miidata);
639 writel(MIICMD_OPCODE_WRITE | ((uint32_t)addr << 5) | (uint32_t)reg,
640 &mac->miicmd);
641 while (readl(&mac->miists) & MIISTS_BUSY)
642 ;
643
644
645 writel(self_ctl, &mac->selfctl);
646
647 ret = 0;
648
649
650 debug("-ep93xx_miiphy_write");
651 return ret;
652}
653#endif
654