1 // SPDX-License-Identifier: GPL-2.0+
3 * Freescale Three Speed Ethernet Controller driver
5 * Copyright 2004-2011, 2013 Freescale Semiconductor, Inc.
6 * (C) Copyright 2003, Motorola, Inc.
18 #include <linux/bitops.h>
19 #include <linux/delay.h>
20 #include <linux/errno.h>
22 #include <asm/processor.h>
26 /* Default initializations for TSEC controllers. */
28 static struct tsec_info_struct tsec_info[] = {
30 STD_TSEC_INFO(1), /* TSEC1 */
33 STD_TSEC_INFO(2), /* TSEC2 */
35 #ifdef CONFIG_MPC85XX_FEC
37 .regs = TSEC_GET_REGS(2, 0x2000),
38 .devname = CONFIG_MPC85XX_FEC_NAME,
39 .phyaddr = FEC_PHY_ADDR,
41 .mii_devname = DEFAULT_MII_NAME
45 STD_TSEC_INFO(3), /* TSEC3 */
48 STD_TSEC_INFO(4), /* TSEC4 */
51 #endif /* CONFIG_DM_ETH */
53 #define TBIANA_SETTINGS ( \
54 TBIANA_ASYMMETRIC_PAUSE \
55 | TBIANA_SYMMETRIC_PAUSE \
56 | TBIANA_FULL_DUPLEX \
59 /* By default force the TBI PHY into 1000Mbps full duplex when in SGMII mode */
60 #ifndef CONFIG_TSEC_TBICR_SETTINGS
61 #define CONFIG_TSEC_TBICR_SETTINGS ( \
67 #endif /* CONFIG_TSEC_TBICR_SETTINGS */
69 /* Configure the TBI for SGMII operation */
70 static void tsec_configure_serdes(struct tsec_private *priv)
73 * Access TBI PHY registers at given TSEC register offset as opposed
74 * to the register offset used for external PHY accesses
76 tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa),
77 0, TBI_ANA, TBIANA_SETTINGS);
78 tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa),
79 0, TBI_TBICON, TBICON_CLK_SELECT);
80 tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa),
81 0, TBI_CR, CONFIG_TSEC_TBICR_SETTINGS);
84 /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
85 * and this is the ethernet-crc method needed for TSEC -- and perhaps
86 * some other adapter -- hash tables
88 #define CRCPOLY_LE 0xedb88320
89 static u32 ether_crc(size_t len, unsigned char const *p)
97 for (i = 0; i < 8; i++)
98 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
100 /* an reverse the bits, cuz of way they arrive -- last-first */
101 crc = (crc >> 16) | (crc << 16);
102 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
103 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
104 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
105 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
109 /* CREDITS: linux gianfar driver, slightly adjusted... thanx. */
111 /* Set the appropriate hash bit for the given addr */
114 * The algorithm works like so:
115 * 1) Take the Destination Address (ie the multicast address), and
116 * do a CRC on it (little endian), and reverse the bits of the
118 * 2) Use the 8 most significant bits as a hash into a 256-entry
119 * table. The table is controlled through 8 32-bit registers:
120 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is entry
121 * 255. This means that the 3 most significant bits in the
122 * hash index which gaddr register to use, and the 5 other bits
123 * indicate which bit (assuming an IBM numbering scheme, which
124 * for PowerPC (tm) is usually the case) in the register holds
127 #ifndef CONFIG_DM_ETH
128 static int tsec_mcast_addr(struct eth_device *dev, const u8 *mcast_mac,
131 static int tsec_mcast_addr(struct udevice *dev, const u8 *mcast_mac, int join)
134 struct tsec_private *priv = (struct tsec_private *)dev->priv;
135 struct tsec __iomem *regs = priv->regs;
137 u8 whichbit, whichreg;
139 result = ether_crc(MAC_ADDR_LEN, mcast_mac);
140 whichbit = (result >> 24) & 0x1f; /* the 5 LSB = which bit to set */
141 whichreg = result >> 29; /* the 3 MSB = which reg to set it in */
143 value = BIT(31 - whichbit);
146 setbits_be32(®s->hash.gaddr0 + whichreg, value);
148 clrbits_be32(®s->hash.gaddr0 + whichreg, value);
154 * Initialized required registers to appropriate values, zeroing
155 * those we don't care about (unless zero is bad, in which case,
156 * choose a more appropriate value)
158 static void init_registers(struct tsec __iomem *regs)
161 out_be32(®s->ievent, IEVENT_INIT_CLEAR);
163 out_be32(®s->imask, IMASK_INIT_CLEAR);
165 out_be32(®s->hash.iaddr0, 0);
166 out_be32(®s->hash.iaddr1, 0);
167 out_be32(®s->hash.iaddr2, 0);
168 out_be32(®s->hash.iaddr3, 0);
169 out_be32(®s->hash.iaddr4, 0);
170 out_be32(®s->hash.iaddr5, 0);
171 out_be32(®s->hash.iaddr6, 0);
172 out_be32(®s->hash.iaddr7, 0);
174 out_be32(®s->hash.gaddr0, 0);
175 out_be32(®s->hash.gaddr1, 0);
176 out_be32(®s->hash.gaddr2, 0);
177 out_be32(®s->hash.gaddr3, 0);
178 out_be32(®s->hash.gaddr4, 0);
179 out_be32(®s->hash.gaddr5, 0);
180 out_be32(®s->hash.gaddr6, 0);
181 out_be32(®s->hash.gaddr7, 0);
183 out_be32(®s->rctrl, 0x00000000);
185 /* Init RMON mib registers */
186 memset((void *)®s->rmon, 0, sizeof(regs->rmon));
188 out_be32(®s->rmon.cam1, 0xffffffff);
189 out_be32(®s->rmon.cam2, 0xffffffff);
191 out_be32(®s->mrblr, MRBLR_INIT_SETTINGS);
193 out_be32(®s->minflr, MINFLR_INIT_SETTINGS);
195 out_be32(®s->attr, ATTR_INIT_SETTINGS);
196 out_be32(®s->attreli, ATTRELI_INIT_SETTINGS);
200 * Configure maccfg2 based on negotiated speed and duplex
201 * reported by PHY handling code
203 static void adjust_link(struct tsec_private *priv, struct phy_device *phydev)
205 struct tsec __iomem *regs = priv->regs;
209 printf("%s: No link.\n", phydev->dev->name);
213 /* clear all bits relative with interface mode */
214 ecntrl = in_be32(®s->ecntrl);
215 ecntrl &= ~ECNTRL_R100;
217 maccfg2 = in_be32(®s->maccfg2);
218 maccfg2 &= ~(MACCFG2_IF | MACCFG2_FULL_DUPLEX);
221 maccfg2 |= MACCFG2_FULL_DUPLEX;
223 switch (phydev->speed) {
225 maccfg2 |= MACCFG2_GMII;
229 maccfg2 |= MACCFG2_MII;
232 * Set R100 bit in all modes although
233 * it is only used in RGMII mode
235 if (phydev->speed == 100)
236 ecntrl |= ECNTRL_R100;
239 printf("%s: Speed was bad\n", phydev->dev->name);
243 out_be32(®s->ecntrl, ecntrl);
244 out_be32(®s->maccfg2, maccfg2);
246 printf("Speed: %d, %s duplex%s\n", phydev->speed,
247 (phydev->duplex) ? "full" : "half",
248 (phydev->port == PORT_FIBRE) ? ", fiber mode" : "");
252 * This returns the status bits of the device. The return value
253 * is never checked, and this is what the 8260 driver did, so we
254 * do the same. Presumably, this would be zero if there were no
257 #ifndef CONFIG_DM_ETH
258 static int tsec_send(struct eth_device *dev, void *packet, int length)
260 static int tsec_send(struct udevice *dev, void *packet, int length)
263 struct tsec_private *priv = (struct tsec_private *)dev->priv;
264 struct tsec __iomem *regs = priv->regs;
269 /* Find an empty buffer descriptor */
271 in_be16(&priv->txbd[priv->tx_idx].status) & TXBD_READY;
273 if (i >= TOUT_LOOP) {
274 printf("%s: tsec: tx buffers full\n", dev->name);
279 out_be32(&priv->txbd[priv->tx_idx].bufptr, (u32)packet);
280 out_be16(&priv->txbd[priv->tx_idx].length, length);
281 status = in_be16(&priv->txbd[priv->tx_idx].status);
282 out_be16(&priv->txbd[priv->tx_idx].status, status |
283 (TXBD_READY | TXBD_LAST | TXBD_CRC | TXBD_INTERRUPT));
285 /* Tell the DMA to go */
286 out_be32(®s->tstat, TSTAT_CLEAR_THALT);
288 /* Wait for buffer to be transmitted */
290 in_be16(&priv->txbd[priv->tx_idx].status) & TXBD_READY;
292 if (i >= TOUT_LOOP) {
293 printf("%s: tsec: tx error\n", dev->name);
298 priv->tx_idx = (priv->tx_idx + 1) % TX_BUF_CNT;
299 result = in_be16(&priv->txbd[priv->tx_idx].status) & TXBD_STATS;
304 #ifndef CONFIG_DM_ETH
305 static int tsec_recv(struct eth_device *dev)
307 struct tsec_private *priv = (struct tsec_private *)dev->priv;
308 struct tsec __iomem *regs = priv->regs;
310 while (!(in_be16(&priv->rxbd[priv->rx_idx].status) & RXBD_EMPTY)) {
311 int length = in_be16(&priv->rxbd[priv->rx_idx].length);
312 u16 status = in_be16(&priv->rxbd[priv->rx_idx].status);
313 uchar *packet = net_rx_packets[priv->rx_idx];
315 /* Send the packet up if there were no errors */
316 if (!(status & RXBD_STATS))
317 net_process_received_packet(packet, length - 4);
319 printf("Got error %x\n", (status & RXBD_STATS));
321 out_be16(&priv->rxbd[priv->rx_idx].length, 0);
324 /* Set the wrap bit if this is the last element in the list */
325 if ((priv->rx_idx + 1) == PKTBUFSRX)
327 out_be16(&priv->rxbd[priv->rx_idx].status, status);
329 priv->rx_idx = (priv->rx_idx + 1) % PKTBUFSRX;
332 if (in_be32(®s->ievent) & IEVENT_BSY) {
333 out_be32(®s->ievent, IEVENT_BSY);
334 out_be32(®s->rstat, RSTAT_CLEAR_RHALT);
340 static int tsec_recv(struct udevice *dev, int flags, uchar **packetp)
342 struct tsec_private *priv = (struct tsec_private *)dev->priv;
343 struct tsec __iomem *regs = priv->regs;
346 if (!(in_be16(&priv->rxbd[priv->rx_idx].status) & RXBD_EMPTY)) {
347 int length = in_be16(&priv->rxbd[priv->rx_idx].length);
348 u16 status = in_be16(&priv->rxbd[priv->rx_idx].status);
351 /* Send the packet up if there were no errors */
352 if (!(status & RXBD_STATS)) {
353 buf = in_be32(&priv->rxbd[priv->rx_idx].bufptr);
354 *packetp = (uchar *)buf;
357 printf("Got error %x\n", (status & RXBD_STATS));
361 if (in_be32(®s->ievent) & IEVENT_BSY) {
362 out_be32(®s->ievent, IEVENT_BSY);
363 out_be32(®s->rstat, RSTAT_CLEAR_RHALT);
369 static int tsec_free_pkt(struct udevice *dev, uchar *packet, int length)
371 struct tsec_private *priv = (struct tsec_private *)dev->priv;
374 out_be16(&priv->rxbd[priv->rx_idx].length, 0);
377 /* Set the wrap bit if this is the last element in the list */
378 if ((priv->rx_idx + 1) == PKTBUFSRX)
380 out_be16(&priv->rxbd[priv->rx_idx].status, status);
382 priv->rx_idx = (priv->rx_idx + 1) % PKTBUFSRX;
388 /* Stop the interface */
389 #ifndef CONFIG_DM_ETH
390 static void tsec_halt(struct eth_device *dev)
392 static void tsec_halt(struct udevice *dev)
395 struct tsec_private *priv = (struct tsec_private *)dev->priv;
396 struct tsec __iomem *regs = priv->regs;
398 clrbits_be32(®s->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
399 setbits_be32(®s->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
401 while ((in_be32(®s->ievent) & (IEVENT_GRSC | IEVENT_GTSC))
402 != (IEVENT_GRSC | IEVENT_GTSC))
405 clrbits_be32(®s->maccfg1, MACCFG1_TX_EN | MACCFG1_RX_EN);
407 /* Shut down the PHY, as needed */
408 phy_shutdown(priv->phydev);
411 #ifdef CONFIG_SYS_FSL_ERRATUM_NMG_ETSEC129
413 * When MACCFG1[Rx_EN] is enabled during system boot as part
414 * of the eTSEC port initialization sequence,
415 * the eTSEC Rx logic may not be properly initialized.
417 void redundant_init(struct tsec_private *priv)
419 struct tsec __iomem *regs = priv->regs;
422 static const u8 pkt[] = {
423 0x00, 0x1e, 0x4f, 0x12, 0xcb, 0x2c, 0x00, 0x25,
424 0x64, 0xbb, 0xd1, 0xab, 0x08, 0x00, 0x45, 0x00,
425 0x00, 0x5c, 0xdd, 0x22, 0x00, 0x00, 0x80, 0x01,
426 0x1f, 0x71, 0x0a, 0xc1, 0x14, 0x22, 0x0a, 0xc1,
427 0x14, 0x6a, 0x08, 0x00, 0xef, 0x7e, 0x02, 0x00,
428 0x94, 0x05, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
429 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e,
430 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
431 0x77, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
432 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
433 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
434 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
435 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70,
438 /* Enable promiscuous mode */
439 setbits_be32(®s->rctrl, 0x8);
440 /* Enable loopback mode */
441 setbits_be32(®s->maccfg1, MACCFG1_LOOPBACK);
442 /* Enable transmit and receive */
443 setbits_be32(®s->maccfg1, MACCFG1_RX_EN | MACCFG1_TX_EN);
445 /* Tell the DMA it is clear to go */
446 setbits_be32(®s->dmactrl, DMACTRL_INIT_SETTINGS);
447 out_be32(®s->tstat, TSTAT_CLEAR_THALT);
448 out_be32(®s->rstat, RSTAT_CLEAR_RHALT);
449 clrbits_be32(®s->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
454 tsec_send(priv->dev, (void *)pkt, sizeof(pkt));
456 /* Wait for buffer to be received */
458 in_be16(&priv->rxbd[priv->rx_idx].status) & RXBD_EMPTY;
460 if (t >= 10 * TOUT_LOOP) {
461 printf("%s: tsec: rx error\n", priv->dev->name);
466 if (!memcmp(pkt, net_rx_packets[priv->rx_idx], sizeof(pkt)))
469 out_be16(&priv->rxbd[priv->rx_idx].length, 0);
471 if ((priv->rx_idx + 1) == PKTBUFSRX)
473 out_be16(&priv->rxbd[priv->rx_idx].status, status);
474 priv->rx_idx = (priv->rx_idx + 1) % PKTBUFSRX;
476 if (in_be32(®s->ievent) & IEVENT_BSY) {
477 out_be32(®s->ievent, IEVENT_BSY);
478 out_be32(®s->rstat, RSTAT_CLEAR_RHALT);
481 printf("loopback recv packet error!\n");
482 clrbits_be32(®s->maccfg1, MACCFG1_RX_EN);
484 setbits_be32(®s->maccfg1, MACCFG1_RX_EN);
486 } while ((count++ < 4) && (fail == 1));
489 panic("eTSEC init fail!\n");
490 /* Disable promiscuous mode */
491 clrbits_be32(®s->rctrl, 0x8);
492 /* Disable loopback mode */
493 clrbits_be32(®s->maccfg1, MACCFG1_LOOPBACK);
498 * Set up the buffers and their descriptors, and bring up the
501 static void startup_tsec(struct tsec_private *priv)
503 struct tsec __iomem *regs = priv->regs;
507 /* reset the indices to zero */
510 #ifdef CONFIG_SYS_FSL_ERRATUM_NMG_ETSEC129
514 /* Point to the buffer descriptors */
515 out_be32(®s->tbase, (u32)&priv->txbd[0]);
516 out_be32(®s->rbase, (u32)&priv->rxbd[0]);
518 /* Initialize the Rx Buffer descriptors */
519 for (i = 0; i < PKTBUFSRX; i++) {
520 out_be16(&priv->rxbd[i].status, RXBD_EMPTY);
521 out_be16(&priv->rxbd[i].length, 0);
522 out_be32(&priv->rxbd[i].bufptr, (u32)net_rx_packets[i]);
524 status = in_be16(&priv->rxbd[PKTBUFSRX - 1].status);
525 out_be16(&priv->rxbd[PKTBUFSRX - 1].status, status | RXBD_WRAP);
527 /* Initialize the TX Buffer Descriptors */
528 for (i = 0; i < TX_BUF_CNT; i++) {
529 out_be16(&priv->txbd[i].status, 0);
530 out_be16(&priv->txbd[i].length, 0);
531 out_be32(&priv->txbd[i].bufptr, 0);
533 status = in_be16(&priv->txbd[TX_BUF_CNT - 1].status);
534 out_be16(&priv->txbd[TX_BUF_CNT - 1].status, status | TXBD_WRAP);
536 #ifdef CONFIG_SYS_FSL_ERRATUM_NMG_ETSEC129
538 if ((SVR_MAJ(svr) == 1) || IS_SVR_REV(svr, 2, 0))
539 redundant_init(priv);
541 /* Enable Transmit and Receive */
542 setbits_be32(®s->maccfg1, MACCFG1_RX_EN | MACCFG1_TX_EN);
544 /* Tell the DMA it is clear to go */
545 setbits_be32(®s->dmactrl, DMACTRL_INIT_SETTINGS);
546 out_be32(®s->tstat, TSTAT_CLEAR_THALT);
547 out_be32(®s->rstat, RSTAT_CLEAR_RHALT);
548 clrbits_be32(®s->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
552 * Initializes data structures and registers for the controller,
553 * and brings the interface up. Returns the link status, meaning
554 * that it returns success if the link is up, failure otherwise.
555 * This allows U-Boot to find the first active controller.
557 #ifndef CONFIG_DM_ETH
558 static int tsec_init(struct eth_device *dev, struct bd_info *bd)
560 static int tsec_init(struct udevice *dev)
563 struct tsec_private *priv = (struct tsec_private *)dev->priv;
565 struct eth_pdata *pdata = dev_get_platdata(dev);
567 struct eth_device *pdata = dev;
569 struct tsec __iomem *regs = priv->regs;
573 /* Make sure the controller is stopped */
576 /* Init MACCFG2. Defaults to GMII */
577 out_be32(®s->maccfg2, MACCFG2_INIT_SETTINGS);
580 out_be32(®s->ecntrl, ECNTRL_INIT_SETTINGS);
583 * Copy the station address into the address registers.
584 * For a station address of 0x12345678ABCD in transmission
585 * order (BE), MACnADDR1 is set to 0xCDAB7856 and
586 * MACnADDR2 is set to 0x34120000.
588 tempval = (pdata->enetaddr[5] << 24) | (pdata->enetaddr[4] << 16) |
589 (pdata->enetaddr[3] << 8) | pdata->enetaddr[2];
591 out_be32(®s->macstnaddr1, tempval);
593 tempval = (pdata->enetaddr[1] << 24) | (pdata->enetaddr[0] << 16);
595 out_be32(®s->macstnaddr2, tempval);
597 /* Clear out (for the most part) the other registers */
598 init_registers(regs);
600 /* Ready the device for tx/rx */
603 /* Start up the PHY */
604 ret = phy_startup(priv->phydev);
606 printf("Could not initialize PHY %s\n",
607 priv->phydev->dev->name);
611 adjust_link(priv, priv->phydev);
613 /* If there's no link, fail */
614 return priv->phydev->link ? 0 : -1;
617 static phy_interface_t tsec_get_interface(struct tsec_private *priv)
619 struct tsec __iomem *regs = priv->regs;
622 ecntrl = in_be32(®s->ecntrl);
624 if (ecntrl & ECNTRL_SGMII_MODE)
625 return PHY_INTERFACE_MODE_SGMII;
627 if (ecntrl & ECNTRL_TBI_MODE) {
628 if (ecntrl & ECNTRL_REDUCED_MODE)
629 return PHY_INTERFACE_MODE_RTBI;
631 return PHY_INTERFACE_MODE_TBI;
634 if (ecntrl & ECNTRL_REDUCED_MODE) {
635 phy_interface_t interface;
637 if (ecntrl & ECNTRL_REDUCED_MII_MODE)
638 return PHY_INTERFACE_MODE_RMII;
640 interface = priv->interface;
643 * This isn't autodetected, so it must
644 * be set by the platform code.
646 if (interface == PHY_INTERFACE_MODE_RGMII_ID ||
647 interface == PHY_INTERFACE_MODE_RGMII_TXID ||
648 interface == PHY_INTERFACE_MODE_RGMII_RXID)
651 return PHY_INTERFACE_MODE_RGMII;
654 if (priv->flags & TSEC_GIGABIT)
655 return PHY_INTERFACE_MODE_GMII;
657 return PHY_INTERFACE_MODE_MII;
661 * Discover which PHY is attached to the device, and configure it
662 * properly. If the PHY is not recognized, then return 0
663 * (failure). Otherwise, return 1
665 static int init_phy(struct tsec_private *priv)
667 struct phy_device *phydev;
668 struct tsec __iomem *regs = priv->regs;
669 u32 supported = (SUPPORTED_10baseT_Half |
670 SUPPORTED_10baseT_Full |
671 SUPPORTED_100baseT_Half |
672 SUPPORTED_100baseT_Full);
674 if (priv->flags & TSEC_GIGABIT)
675 supported |= SUPPORTED_1000baseT_Full;
677 /* Assign a Physical address to the TBI */
678 out_be32(®s->tbipa, priv->tbiaddr);
680 priv->interface = tsec_get_interface(priv);
682 if (priv->interface == PHY_INTERFACE_MODE_SGMII)
683 tsec_configure_serdes(priv);
685 #if defined(CONFIG_DM_ETH) && defined(CONFIG_DM_MDIO)
686 if (ofnode_valid(ofnode_find_subnode(priv->dev->node, "fixed-link")))
687 phydev = phy_connect(NULL, 0, priv->dev, priv->interface);
689 phydev = dm_eth_phy_connect(priv->dev);
691 phydev = phy_connect(priv->bus, priv->phyaddr, priv->dev,
697 phydev->supported &= supported;
698 phydev->advertising = phydev->supported;
700 priv->phydev = phydev;
707 #ifndef CONFIG_DM_ETH
709 * Initialize device structure. Returns success if PHY
710 * initialization succeeded (i.e. if it recognizes the PHY)
712 static int tsec_initialize(struct bd_info *bis,
713 struct tsec_info_struct *tsec_info)
715 struct tsec_private *priv;
716 struct eth_device *dev;
719 dev = (struct eth_device *)malloc(sizeof(*dev));
724 memset(dev, 0, sizeof(*dev));
726 priv = (struct tsec_private *)malloc(sizeof(*priv));
733 priv->regs = tsec_info->regs;
734 priv->phyregs_sgmii = tsec_info->miiregs_sgmii;
736 priv->phyaddr = tsec_info->phyaddr;
737 priv->tbiaddr = CONFIG_SYS_TBIPA_VALUE;
738 priv->flags = tsec_info->flags;
740 strcpy(dev->name, tsec_info->devname);
741 priv->interface = tsec_info->interface;
742 priv->bus = miiphy_get_dev_by_name(tsec_info->mii_devname);
746 dev->init = tsec_init;
747 dev->halt = tsec_halt;
748 dev->send = tsec_send;
749 dev->recv = tsec_recv;
750 dev->mcast = tsec_mcast_addr;
752 /* Tell U-Boot to get the addr from the env */
753 for (i = 0; i < 6; i++)
754 dev->enetaddr[i] = 0;
759 setbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
760 udelay(2); /* Soft Reset must be asserted for 3 TX clocks */
761 clrbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
763 /* Try to initialize PHY here, and return */
764 return init_phy(priv);
768 * Initialize all the TSEC devices
770 * Returns the number of TSEC devices that were initialized
772 int tsec_eth_init(struct bd_info *bis, struct tsec_info_struct *tsecs,
778 for (i = 0; i < num; i++) {
779 int ret = tsec_initialize(bis, &tsecs[i]);
788 int tsec_standard_init(struct bd_info *bis)
790 struct fsl_pq_mdio_info info;
792 info.regs = TSEC_GET_MDIO_REGS_BASE(1);
793 info.name = DEFAULT_MII_NAME;
795 fsl_pq_mdio_init(bis, &info);
797 return tsec_eth_init(bis, tsec_info, ARRAY_SIZE(tsec_info));
799 #else /* CONFIG_DM_ETH */
800 int tsec_probe(struct udevice *dev)
802 struct eth_pdata *pdata = dev_get_platdata(dev);
803 struct tsec_private *priv = dev_get_priv(dev);
804 struct ofnode_phandle_args phandle_args;
805 u32 tbiaddr = CONFIG_SYS_TBIPA_VALUE;
806 struct tsec_data *data;
807 const char *phy_mode;
812 data = (struct tsec_data *)dev_get_driver_data(dev);
814 pdata->iobase = (phys_addr_t)dev_read_addr(dev);
815 priv->regs = dev_remap_addr(dev);
817 ret = dev_read_phandle_with_args(dev, "tbi-handle", NULL, 0, 0,
820 ofnode_read_u32(phandle_args.node, "reg", &tbiaddr);
822 parent = ofnode_get_parent(phandle_args.node);
823 if (!ofnode_valid(parent)) {
824 printf("No parent node for TBI PHY?\n");
828 reg = ofnode_get_addr_index(parent, 0);
829 if (reg == FDT_ADDR_T_NONE) {
830 printf("No 'reg' property of MII for TBI PHY\n");
834 priv->phyregs_sgmii = map_physmem(reg + data->mdio_regs_off,
838 priv->tbiaddr = tbiaddr;
840 phy_mode = dev_read_prop(dev, "phy-connection-type", NULL);
842 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
843 if (pdata->phy_interface == -1) {
844 printf("Invalid PHY interface '%s'\n", phy_mode);
847 priv->interface = pdata->phy_interface;
849 /* Initialize flags */
850 priv->flags = TSEC_GIGABIT;
851 if (priv->interface == PHY_INTERFACE_MODE_SGMII)
852 priv->flags |= TSEC_SGMII;
855 setbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
856 udelay(2); /* Soft Reset must be asserted for 3 TX clocks */
857 clrbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
860 priv->bus = miiphy_get_dev_by_name(dev->name);
862 /* Try to initialize PHY here, and return */
863 return !init_phy(priv);
866 int tsec_remove(struct udevice *dev)
868 struct tsec_private *priv = dev->priv;
871 mdio_unregister(priv->bus);
872 mdio_free(priv->bus);
877 static const struct eth_ops tsec_ops = {
881 .free_pkt = tsec_free_pkt,
883 .mcast = tsec_mcast_addr,
886 static struct tsec_data etsec2_data = {
887 .mdio_regs_off = TSEC_MDIO_REGS_OFFSET,
890 static struct tsec_data gianfar_data = {
891 .mdio_regs_off = 0x0,
894 static const struct udevice_id tsec_ids[] = {
895 { .compatible = "fsl,etsec2", .data = (ulong)&etsec2_data },
896 { .compatible = "gianfar", .data = (ulong)&gianfar_data },
900 U_BOOT_DRIVER(eth_tsec) = {
903 .of_match = tsec_ids,
905 .remove = tsec_remove,
907 .priv_auto = sizeof(struct tsec_private),
908 .plat_auto = sizeof(struct eth_pdata),
909 .flags = DM_FLAG_ALLOC_PRIV_DMA,
911 #endif /* CONFIG_DM_ETH */