1
2
3
4
5
6
7
8
9
10#include <config.h>
11#include <common.h>
12#include <dm.h>
13#include <malloc.h>
14#include <net.h>
15#include <command.h>
16#include <tsec.h>
17#include <fsl_mdio.h>
18#include <linux/bitops.h>
19#include <linux/delay.h>
20#include <linux/errno.h>
21#include <asm/processor.h>
22#include <asm/io.h>
23
24#ifndef CONFIG_DM_ETH
25
26
27static struct tsec_info_struct tsec_info[] = {
28#ifdef CONFIG_TSEC1
29 STD_TSEC_INFO(1),
30#endif
31#ifdef CONFIG_TSEC2
32 STD_TSEC_INFO(2),
33#endif
34#ifdef CONFIG_MPC85XX_FEC
35 {
36 .regs = TSEC_GET_REGS(2, 0x2000),
37 .devname = CONFIG_MPC85XX_FEC_NAME,
38 .phyaddr = FEC_PHY_ADDR,
39 .flags = FEC_FLAGS,
40 .mii_devname = DEFAULT_MII_NAME
41 },
42#endif
43#ifdef CONFIG_TSEC3
44 STD_TSEC_INFO(3),
45#endif
46#ifdef CONFIG_TSEC4
47 STD_TSEC_INFO(4),
48#endif
49};
50#endif
51
52#define TBIANA_SETTINGS ( \
53 TBIANA_ASYMMETRIC_PAUSE \
54 | TBIANA_SYMMETRIC_PAUSE \
55 | TBIANA_FULL_DUPLEX \
56 )
57
58
59#ifndef CONFIG_TSEC_TBICR_SETTINGS
60#define CONFIG_TSEC_TBICR_SETTINGS ( \
61 TBICR_PHY_RESET \
62 | TBICR_ANEG_ENABLE \
63 | TBICR_FULL_DUPLEX \
64 | TBICR_SPEED1_SET \
65 )
66#endif
67
68
69static void tsec_configure_serdes(struct tsec_private *priv)
70{
71
72
73
74
75 tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa),
76 0, TBI_ANA, TBIANA_SETTINGS);
77 tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa),
78 0, TBI_TBICON, TBICON_CLK_SELECT);
79 tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa),
80 0, TBI_CR, CONFIG_TSEC_TBICR_SETTINGS);
81}
82
83
84
85
86
87#define CRCPOLY_LE 0xedb88320
88static u32 ether_crc(size_t len, unsigned char const *p)
89{
90 int i;
91 u32 crc;
92
93 crc = ~0;
94 while (len--) {
95 crc ^= *p++;
96 for (i = 0; i < 8; i++)
97 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
98 }
99
100 crc = (crc >> 16) | (crc << 16);
101 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
102 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
103 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
104 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
105 return crc;
106}
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126#ifndef CONFIG_DM_ETH
127static int tsec_mcast_addr(struct eth_device *dev, const u8 *mcast_mac,
128 int join)
129#else
130static int tsec_mcast_addr(struct udevice *dev, const u8 *mcast_mac, int join)
131#endif
132{
133 struct tsec_private *priv = (struct tsec_private *)dev->priv;
134 struct tsec __iomem *regs = priv->regs;
135 u32 result, value;
136 u8 whichbit, whichreg;
137
138 result = ether_crc(MAC_ADDR_LEN, mcast_mac);
139 whichbit = (result >> 24) & 0x1f;
140 whichreg = result >> 29;
141
142 value = BIT(31 - whichbit);
143
144 if (join)
145 setbits_be32(®s->hash.gaddr0 + whichreg, value);
146 else
147 clrbits_be32(®s->hash.gaddr0 + whichreg, value);
148
149 return 0;
150}
151
152
153
154
155
156
157static void init_registers(struct tsec __iomem *regs)
158{
159
160 out_be32(®s->ievent, IEVENT_INIT_CLEAR);
161
162 out_be32(®s->imask, IMASK_INIT_CLEAR);
163
164 out_be32(®s->hash.iaddr0, 0);
165 out_be32(®s->hash.iaddr1, 0);
166 out_be32(®s->hash.iaddr2, 0);
167 out_be32(®s->hash.iaddr3, 0);
168 out_be32(®s->hash.iaddr4, 0);
169 out_be32(®s->hash.iaddr5, 0);
170 out_be32(®s->hash.iaddr6, 0);
171 out_be32(®s->hash.iaddr7, 0);
172
173 out_be32(®s->hash.gaddr0, 0);
174 out_be32(®s->hash.gaddr1, 0);
175 out_be32(®s->hash.gaddr2, 0);
176 out_be32(®s->hash.gaddr3, 0);
177 out_be32(®s->hash.gaddr4, 0);
178 out_be32(®s->hash.gaddr5, 0);
179 out_be32(®s->hash.gaddr6, 0);
180 out_be32(®s->hash.gaddr7, 0);
181
182 out_be32(®s->rctrl, 0x00000000);
183
184
185 memset((void *)®s->rmon, 0, sizeof(regs->rmon));
186
187 out_be32(®s->rmon.cam1, 0xffffffff);
188 out_be32(®s->rmon.cam2, 0xffffffff);
189
190 out_be32(®s->mrblr, MRBLR_INIT_SETTINGS);
191
192 out_be32(®s->minflr, MINFLR_INIT_SETTINGS);
193
194 out_be32(®s->attr, ATTR_INIT_SETTINGS);
195 out_be32(®s->attreli, ATTRELI_INIT_SETTINGS);
196}
197
198
199
200
201
202static void adjust_link(struct tsec_private *priv, struct phy_device *phydev)
203{
204 struct tsec __iomem *regs = priv->regs;
205 u32 ecntrl, maccfg2;
206
207 if (!phydev->link) {
208 printf("%s: No link.\n", phydev->dev->name);
209 return;
210 }
211
212
213 ecntrl = in_be32(®s->ecntrl);
214 ecntrl &= ~ECNTRL_R100;
215
216 maccfg2 = in_be32(®s->maccfg2);
217 maccfg2 &= ~(MACCFG2_IF | MACCFG2_FULL_DUPLEX);
218
219 if (phydev->duplex)
220 maccfg2 |= MACCFG2_FULL_DUPLEX;
221
222 switch (phydev->speed) {
223 case 1000:
224 maccfg2 |= MACCFG2_GMII;
225 break;
226 case 100:
227 case 10:
228 maccfg2 |= MACCFG2_MII;
229
230
231
232
233
234 if (phydev->speed == 100)
235 ecntrl |= ECNTRL_R100;
236 break;
237 default:
238 printf("%s: Speed was bad\n", phydev->dev->name);
239 break;
240 }
241
242 out_be32(®s->ecntrl, ecntrl);
243 out_be32(®s->maccfg2, maccfg2);
244
245 printf("Speed: %d, %s duplex%s\n", phydev->speed,
246 (phydev->duplex) ? "full" : "half",
247 (phydev->port == PORT_FIBRE) ? ", fiber mode" : "");
248}
249
250
251
252
253
254
255
256#ifndef CONFIG_DM_ETH
257static int tsec_send(struct eth_device *dev, void *packet, int length)
258#else
259static int tsec_send(struct udevice *dev, void *packet, int length)
260#endif
261{
262 struct tsec_private *priv = (struct tsec_private *)dev->priv;
263 struct tsec __iomem *regs = priv->regs;
264 int result = 0;
265 u16 status;
266 int i;
267
268
269 for (i = 0;
270 in_be16(&priv->txbd[priv->tx_idx].status) & TXBD_READY;
271 i++) {
272 if (i >= TOUT_LOOP) {
273 printf("%s: tsec: tx buffers full\n", dev->name);
274 return result;
275 }
276 }
277
278 out_be32(&priv->txbd[priv->tx_idx].bufptr, (u32)packet);
279 out_be16(&priv->txbd[priv->tx_idx].length, length);
280 status = in_be16(&priv->txbd[priv->tx_idx].status);
281 out_be16(&priv->txbd[priv->tx_idx].status, status |
282 (TXBD_READY | TXBD_LAST | TXBD_CRC | TXBD_INTERRUPT));
283
284
285 out_be32(®s->tstat, TSTAT_CLEAR_THALT);
286
287
288 for (i = 0;
289 in_be16(&priv->txbd[priv->tx_idx].status) & TXBD_READY;
290 i++) {
291 if (i >= TOUT_LOOP) {
292 printf("%s: tsec: tx error\n", dev->name);
293 return result;
294 }
295 }
296
297 priv->tx_idx = (priv->tx_idx + 1) % TX_BUF_CNT;
298 result = in_be16(&priv->txbd[priv->tx_idx].status) & TXBD_STATS;
299
300 return result;
301}
302
303#ifndef CONFIG_DM_ETH
304static int tsec_recv(struct eth_device *dev)
305{
306 struct tsec_private *priv = (struct tsec_private *)dev->priv;
307 struct tsec __iomem *regs = priv->regs;
308
309 while (!(in_be16(&priv->rxbd[priv->rx_idx].status) & RXBD_EMPTY)) {
310 int length = in_be16(&priv->rxbd[priv->rx_idx].length);
311 u16 status = in_be16(&priv->rxbd[priv->rx_idx].status);
312 uchar *packet = net_rx_packets[priv->rx_idx];
313
314
315 if (!(status & RXBD_STATS))
316 net_process_received_packet(packet, length - 4);
317 else
318 printf("Got error %x\n", (status & RXBD_STATS));
319
320 out_be16(&priv->rxbd[priv->rx_idx].length, 0);
321
322 status = RXBD_EMPTY;
323
324 if ((priv->rx_idx + 1) == PKTBUFSRX)
325 status |= RXBD_WRAP;
326 out_be16(&priv->rxbd[priv->rx_idx].status, status);
327
328 priv->rx_idx = (priv->rx_idx + 1) % PKTBUFSRX;
329 }
330
331 if (in_be32(®s->ievent) & IEVENT_BSY) {
332 out_be32(®s->ievent, IEVENT_BSY);
333 out_be32(®s->rstat, RSTAT_CLEAR_RHALT);
334 }
335
336 return -1;
337}
338#else
339static int tsec_recv(struct udevice *dev, int flags, uchar **packetp)
340{
341 struct tsec_private *priv = (struct tsec_private *)dev->priv;
342 struct tsec __iomem *regs = priv->regs;
343 int ret = -1;
344
345 if (!(in_be16(&priv->rxbd[priv->rx_idx].status) & RXBD_EMPTY)) {
346 int length = in_be16(&priv->rxbd[priv->rx_idx].length);
347 u16 status = in_be16(&priv->rxbd[priv->rx_idx].status);
348 u32 buf;
349
350
351 if (!(status & RXBD_STATS)) {
352 buf = in_be32(&priv->rxbd[priv->rx_idx].bufptr);
353 *packetp = (uchar *)buf;
354 ret = length - 4;
355 } else {
356 printf("Got error %x\n", (status & RXBD_STATS));
357 }
358 }
359
360 if (in_be32(®s->ievent) & IEVENT_BSY) {
361 out_be32(®s->ievent, IEVENT_BSY);
362 out_be32(®s->rstat, RSTAT_CLEAR_RHALT);
363 }
364
365 return ret;
366}
367
368static int tsec_free_pkt(struct udevice *dev, uchar *packet, int length)
369{
370 struct tsec_private *priv = (struct tsec_private *)dev->priv;
371 u16 status;
372
373 out_be16(&priv->rxbd[priv->rx_idx].length, 0);
374
375 status = RXBD_EMPTY;
376
377 if ((priv->rx_idx + 1) == PKTBUFSRX)
378 status |= RXBD_WRAP;
379 out_be16(&priv->rxbd[priv->rx_idx].status, status);
380
381 priv->rx_idx = (priv->rx_idx + 1) % PKTBUFSRX;
382
383 return 0;
384}
385#endif
386
387
388#ifndef CONFIG_DM_ETH
389static void tsec_halt(struct eth_device *dev)
390#else
391static void tsec_halt(struct udevice *dev)
392#endif
393{
394 struct tsec_private *priv = (struct tsec_private *)dev->priv;
395 struct tsec __iomem *regs = priv->regs;
396
397 clrbits_be32(®s->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
398 setbits_be32(®s->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
399
400 while ((in_be32(®s->ievent) & (IEVENT_GRSC | IEVENT_GTSC))
401 != (IEVENT_GRSC | IEVENT_GTSC))
402 ;
403
404 clrbits_be32(®s->maccfg1, MACCFG1_TX_EN | MACCFG1_RX_EN);
405
406
407 phy_shutdown(priv->phydev);
408}
409
410#ifdef CONFIG_SYS_FSL_ERRATUM_NMG_ETSEC129
411
412
413
414
415
416void redundant_init(struct tsec_private *priv)
417{
418 struct tsec __iomem *regs = priv->regs;
419 uint t, count = 0;
420 int fail = 1;
421 static const u8 pkt[] = {
422 0x00, 0x1e, 0x4f, 0x12, 0xcb, 0x2c, 0x00, 0x25,
423 0x64, 0xbb, 0xd1, 0xab, 0x08, 0x00, 0x45, 0x00,
424 0x00, 0x5c, 0xdd, 0x22, 0x00, 0x00, 0x80, 0x01,
425 0x1f, 0x71, 0x0a, 0xc1, 0x14, 0x22, 0x0a, 0xc1,
426 0x14, 0x6a, 0x08, 0x00, 0xef, 0x7e, 0x02, 0x00,
427 0x94, 0x05, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
428 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e,
429 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
430 0x77, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
431 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
432 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
433 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
434 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70,
435 0x71, 0x72};
436
437
438 setbits_be32(®s->rctrl, 0x8);
439
440 setbits_be32(®s->maccfg1, MACCFG1_LOOPBACK);
441
442 setbits_be32(®s->maccfg1, MACCFG1_RX_EN | MACCFG1_TX_EN);
443
444
445 setbits_be32(®s->dmactrl, DMACTRL_INIT_SETTINGS);
446 out_be32(®s->tstat, TSTAT_CLEAR_THALT);
447 out_be32(®s->rstat, RSTAT_CLEAR_RHALT);
448 clrbits_be32(®s->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
449
450 do {
451 u16 status;
452
453 tsec_send(priv->dev, (void *)pkt, sizeof(pkt));
454
455
456 for (t = 0;
457 in_be16(&priv->rxbd[priv->rx_idx].status) & RXBD_EMPTY;
458 t++) {
459 if (t >= 10 * TOUT_LOOP) {
460 printf("%s: tsec: rx error\n", priv->dev->name);
461 break;
462 }
463 }
464
465 if (!memcmp(pkt, net_rx_packets[priv->rx_idx], sizeof(pkt)))
466 fail = 0;
467
468 out_be16(&priv->rxbd[priv->rx_idx].length, 0);
469 status = RXBD_EMPTY;
470 if ((priv->rx_idx + 1) == PKTBUFSRX)
471 status |= RXBD_WRAP;
472 out_be16(&priv->rxbd[priv->rx_idx].status, status);
473 priv->rx_idx = (priv->rx_idx + 1) % PKTBUFSRX;
474
475 if (in_be32(®s->ievent) & IEVENT_BSY) {
476 out_be32(®s->ievent, IEVENT_BSY);
477 out_be32(®s->rstat, RSTAT_CLEAR_RHALT);
478 }
479 if (fail) {
480 printf("loopback recv packet error!\n");
481 clrbits_be32(®s->maccfg1, MACCFG1_RX_EN);
482 udelay(1000);
483 setbits_be32(®s->maccfg1, MACCFG1_RX_EN);
484 }
485 } while ((count++ < 4) && (fail == 1));
486
487 if (fail)
488 panic("eTSEC init fail!\n");
489
490 clrbits_be32(®s->rctrl, 0x8);
491
492 clrbits_be32(®s->maccfg1, MACCFG1_LOOPBACK);
493}
494#endif
495
496
497
498
499
500static void startup_tsec(struct tsec_private *priv)
501{
502 struct tsec __iomem *regs = priv->regs;
503 u16 status;
504 int i;
505
506
507 priv->rx_idx = 0;
508 priv->tx_idx = 0;
509#ifdef CONFIG_SYS_FSL_ERRATUM_NMG_ETSEC129
510 uint svr;
511#endif
512
513
514 out_be32(®s->tbase, (u32)&priv->txbd[0]);
515 out_be32(®s->rbase, (u32)&priv->rxbd[0]);
516
517
518 for (i = 0; i < PKTBUFSRX; i++) {
519 out_be16(&priv->rxbd[i].status, RXBD_EMPTY);
520 out_be16(&priv->rxbd[i].length, 0);
521 out_be32(&priv->rxbd[i].bufptr, (u32)net_rx_packets[i]);
522 }
523 status = in_be16(&priv->rxbd[PKTBUFSRX - 1].status);
524 out_be16(&priv->rxbd[PKTBUFSRX - 1].status, status | RXBD_WRAP);
525
526
527 for (i = 0; i < TX_BUF_CNT; i++) {
528 out_be16(&priv->txbd[i].status, 0);
529 out_be16(&priv->txbd[i].length, 0);
530 out_be32(&priv->txbd[i].bufptr, 0);
531 }
532 status = in_be16(&priv->txbd[TX_BUF_CNT - 1].status);
533 out_be16(&priv->txbd[TX_BUF_CNT - 1].status, status | TXBD_WRAP);
534
535#ifdef CONFIG_SYS_FSL_ERRATUM_NMG_ETSEC129
536 svr = get_svr();
537 if ((SVR_MAJ(svr) == 1) || IS_SVR_REV(svr, 2, 0))
538 redundant_init(priv);
539#endif
540
541 setbits_be32(®s->maccfg1, MACCFG1_RX_EN | MACCFG1_TX_EN);
542
543
544 setbits_be32(®s->dmactrl, DMACTRL_INIT_SETTINGS);
545 out_be32(®s->tstat, TSTAT_CLEAR_THALT);
546 out_be32(®s->rstat, RSTAT_CLEAR_RHALT);
547 clrbits_be32(®s->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
548}
549
550
551
552
553
554
555
556#ifndef CONFIG_DM_ETH
557static int tsec_init(struct eth_device *dev, struct bd_info *bd)
558#else
559static int tsec_init(struct udevice *dev)
560#endif
561{
562 struct tsec_private *priv = (struct tsec_private *)dev->priv;
563#ifdef CONFIG_DM_ETH
564 struct eth_pdata *pdata = dev_get_platdata(dev);
565#else
566 struct eth_device *pdata = dev;
567#endif
568 struct tsec __iomem *regs = priv->regs;
569 u32 tempval;
570 int ret;
571
572
573 tsec_halt(dev);
574
575
576 out_be32(®s->maccfg2, MACCFG2_INIT_SETTINGS);
577
578
579 out_be32(®s->ecntrl, ECNTRL_INIT_SETTINGS);
580
581
582
583
584
585
586
587 tempval = (pdata->enetaddr[5] << 24) | (pdata->enetaddr[4] << 16) |
588 (pdata->enetaddr[3] << 8) | pdata->enetaddr[2];
589
590 out_be32(®s->macstnaddr1, tempval);
591
592 tempval = (pdata->enetaddr[1] << 24) | (pdata->enetaddr[0] << 16);
593
594 out_be32(®s->macstnaddr2, tempval);
595
596
597 init_registers(regs);
598
599
600 startup_tsec(priv);
601
602
603 ret = phy_startup(priv->phydev);
604 if (ret) {
605 printf("Could not initialize PHY %s\n",
606 priv->phydev->dev->name);
607 return ret;
608 }
609
610 adjust_link(priv, priv->phydev);
611
612
613 return priv->phydev->link ? 0 : -1;
614}
615
616static phy_interface_t tsec_get_interface(struct tsec_private *priv)
617{
618 struct tsec __iomem *regs = priv->regs;
619 u32 ecntrl;
620
621 ecntrl = in_be32(®s->ecntrl);
622
623 if (ecntrl & ECNTRL_SGMII_MODE)
624 return PHY_INTERFACE_MODE_SGMII;
625
626 if (ecntrl & ECNTRL_TBI_MODE) {
627 if (ecntrl & ECNTRL_REDUCED_MODE)
628 return PHY_INTERFACE_MODE_RTBI;
629 else
630 return PHY_INTERFACE_MODE_TBI;
631 }
632
633 if (ecntrl & ECNTRL_REDUCED_MODE) {
634 phy_interface_t interface;
635
636 if (ecntrl & ECNTRL_REDUCED_MII_MODE)
637 return PHY_INTERFACE_MODE_RMII;
638
639 interface = priv->interface;
640
641
642
643
644
645 if (interface == PHY_INTERFACE_MODE_RGMII_ID ||
646 interface == PHY_INTERFACE_MODE_RGMII_TXID ||
647 interface == PHY_INTERFACE_MODE_RGMII_RXID)
648 return interface;
649
650 return PHY_INTERFACE_MODE_RGMII;
651 }
652
653 if (priv->flags & TSEC_GIGABIT)
654 return PHY_INTERFACE_MODE_GMII;
655
656 return PHY_INTERFACE_MODE_MII;
657}
658
659
660
661
662
663
664static int init_phy(struct tsec_private *priv)
665{
666 struct phy_device *phydev;
667 struct tsec __iomem *regs = priv->regs;
668 u32 supported = (SUPPORTED_10baseT_Half |
669 SUPPORTED_10baseT_Full |
670 SUPPORTED_100baseT_Half |
671 SUPPORTED_100baseT_Full);
672
673 if (priv->flags & TSEC_GIGABIT)
674 supported |= SUPPORTED_1000baseT_Full;
675
676
677 out_be32(®s->tbipa, priv->tbiaddr);
678
679 priv->interface = tsec_get_interface(priv);
680
681 if (priv->interface == PHY_INTERFACE_MODE_SGMII)
682 tsec_configure_serdes(priv);
683
684 phydev = phy_connect(priv->bus, priv->phyaddr, priv->dev,
685 priv->interface);
686 if (!phydev)
687 return 0;
688
689 phydev->supported &= supported;
690 phydev->advertising = phydev->supported;
691
692 priv->phydev = phydev;
693
694 phy_config(phydev);
695
696 return 1;
697}
698
699#ifndef CONFIG_DM_ETH
700
701
702
703
704static int tsec_initialize(struct bd_info *bis,
705 struct tsec_info_struct *tsec_info)
706{
707 struct tsec_private *priv;
708 struct eth_device *dev;
709 int i;
710
711 dev = (struct eth_device *)malloc(sizeof(*dev));
712
713 if (!dev)
714 return 0;
715
716 memset(dev, 0, sizeof(*dev));
717
718 priv = (struct tsec_private *)malloc(sizeof(*priv));
719
720 if (!priv) {
721 free(dev);
722 return 0;
723 }
724
725 priv->regs = tsec_info->regs;
726 priv->phyregs_sgmii = tsec_info->miiregs_sgmii;
727
728 priv->phyaddr = tsec_info->phyaddr;
729 priv->tbiaddr = CONFIG_SYS_TBIPA_VALUE;
730 priv->flags = tsec_info->flags;
731
732 strcpy(dev->name, tsec_info->devname);
733 priv->interface = tsec_info->interface;
734 priv->bus = miiphy_get_dev_by_name(tsec_info->mii_devname);
735 priv->dev = dev;
736 dev->iobase = 0;
737 dev->priv = priv;
738 dev->init = tsec_init;
739 dev->halt = tsec_halt;
740 dev->send = tsec_send;
741 dev->recv = tsec_recv;
742 dev->mcast = tsec_mcast_addr;
743
744
745 for (i = 0; i < 6; i++)
746 dev->enetaddr[i] = 0;
747
748 eth_register(dev);
749
750
751 setbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
752 udelay(2);
753 clrbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
754
755
756 return init_phy(priv);
757}
758
759
760
761
762
763
764int tsec_eth_init(struct bd_info *bis, struct tsec_info_struct *tsecs,
765 int num)
766{
767 int i;
768 int count = 0;
769
770 for (i = 0; i < num; i++) {
771 int ret = tsec_initialize(bis, &tsecs[i]);
772
773 if (ret > 0)
774 count += ret;
775 }
776
777 return count;
778}
779
780int tsec_standard_init(struct bd_info *bis)
781{
782 struct fsl_pq_mdio_info info;
783
784 info.regs = TSEC_GET_MDIO_REGS_BASE(1);
785 info.name = DEFAULT_MII_NAME;
786
787 fsl_pq_mdio_init(bis, &info);
788
789 return tsec_eth_init(bis, tsec_info, ARRAY_SIZE(tsec_info));
790}
791#else
792int tsec_probe(struct udevice *dev)
793{
794 struct eth_pdata *pdata = dev_get_platdata(dev);
795 struct tsec_private *priv = dev_get_priv(dev);
796 struct tsec_mii_mng __iomem *ext_phyregs_mii;
797 struct ofnode_phandle_args phandle_args;
798 u32 tbiaddr = CONFIG_SYS_TBIPA_VALUE;
799 struct fsl_pq_mdio_info mdio_info;
800 const char *phy_mode;
801 fdt_addr_t reg;
802 ofnode parent;
803 int ret;
804
805 pdata->iobase = (phys_addr_t)dev_read_addr(dev);
806 priv->regs = dev_remap_addr(dev);
807
808 if (dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
809 &phandle_args)) {
810 printf("phy-handle does not exist under tsec %s\n", dev->name);
811 return -ENOENT;
812 } else {
813 int reg = ofnode_read_u32_default(phandle_args.node, "reg", 0);
814
815 priv->phyaddr = reg;
816 }
817
818 parent = ofnode_get_parent(phandle_args.node);
819 if (!ofnode_valid(parent)) {
820 printf("No parent node for PHY?\n");
821 return -ENOENT;
822 }
823
824 reg = ofnode_get_addr_index(parent, 0);
825 if (reg == FDT_ADDR_T_NONE) {
826 printf("No 'reg' property of MII for external PHY\n");
827 return -ENOENT;
828 }
829
830 ext_phyregs_mii = map_physmem(reg + TSEC_MDIO_REGS_OFFSET, 0,
831 MAP_NOCACHE);
832
833 ret = dev_read_phandle_with_args(dev, "tbi-handle", NULL, 0, 0,
834 &phandle_args);
835 if (ret == 0) {
836 ofnode_read_u32(phandle_args.node, "reg", &tbiaddr);
837
838 parent = ofnode_get_parent(phandle_args.node);
839 if (!ofnode_valid(parent)) {
840 printf("No parent node for TBI PHY?\n");
841 return -ENOENT;
842 }
843
844 reg = ofnode_get_addr_index(parent, 0);
845 if (reg == FDT_ADDR_T_NONE) {
846 printf("No 'reg' property of MII for TBI PHY\n");
847 return -ENOENT;
848 }
849
850 priv->phyregs_sgmii = map_physmem(reg + TSEC_MDIO_REGS_OFFSET,
851 0, MAP_NOCACHE);
852 }
853
854 priv->tbiaddr = tbiaddr;
855
856 phy_mode = dev_read_prop(dev, "phy-connection-type", NULL);
857 if (phy_mode)
858 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
859 if (pdata->phy_interface == -1) {
860 printf("Invalid PHY interface '%s'\n", phy_mode);
861 return -EINVAL;
862 }
863 priv->interface = pdata->phy_interface;
864
865
866 priv->flags = TSEC_GIGABIT;
867 if (priv->interface == PHY_INTERFACE_MODE_SGMII)
868 priv->flags |= TSEC_SGMII;
869
870 mdio_info.regs = ext_phyregs_mii;
871 mdio_info.name = (char *)dev->name;
872 ret = fsl_pq_mdio_init(NULL, &mdio_info);
873 if (ret)
874 return ret;
875
876
877 setbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
878 udelay(2);
879 clrbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
880
881 priv->dev = dev;
882 priv->bus = miiphy_get_dev_by_name(dev->name);
883
884
885 return !init_phy(priv);
886}
887
888int tsec_remove(struct udevice *dev)
889{
890 struct tsec_private *priv = dev->priv;
891
892 free(priv->phydev);
893 mdio_unregister(priv->bus);
894 mdio_free(priv->bus);
895
896 return 0;
897}
898
899static const struct eth_ops tsec_ops = {
900 .start = tsec_init,
901 .send = tsec_send,
902 .recv = tsec_recv,
903 .free_pkt = tsec_free_pkt,
904 .stop = tsec_halt,
905 .mcast = tsec_mcast_addr,
906};
907
908static const struct udevice_id tsec_ids[] = {
909 { .compatible = "fsl,etsec2" },
910 { }
911};
912
913U_BOOT_DRIVER(eth_tsec) = {
914 .name = "tsec",
915 .id = UCLASS_ETH,
916 .of_match = tsec_ids,
917 .probe = tsec_probe,
918 .remove = tsec_remove,
919 .ops = &tsec_ops,
920 .priv_auto_alloc_size = sizeof(struct tsec_private),
921 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
922 .flags = DM_FLAG_ALLOC_PRIV_DMA,
923};
924#endif
925