1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2009-2012 Freescale Semiconductor, Inc.
13 #include <dm/ofnode.h>
14 #include <linux/compat.h>
15 #include <phy_interface.h>
24 #include <fsl_dtsec.h>
26 #include <fsl_memac.h>
27 #include <linux/delay.h>
32 static struct eth_device *devlist[NUM_FM_PORTS];
33 static int num_controllers;
36 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) && !defined(BITBANGMII)
38 #define TBIANA_SETTINGS (TBIANA_ASYMMETRIC_PAUSE | TBIANA_SYMMETRIC_PAUSE | \
41 #define TBIANA_SGMII_ACK 0x4001
43 #define TBICR_SETTINGS (TBICR_ANEG_ENABLE | TBICR_RESTART_ANEG | \
44 TBICR_FULL_DUPLEX | TBICR_SPEED1_SET)
46 /* Configure the TBI for SGMII operation */
47 static void dtsec_configure_serdes(struct fm_eth *priv)
49 #ifdef CONFIG_SYS_FMAN_V3
52 bool sgmii_2500 = (priv->enet_if ==
53 PHY_INTERFACE_MODE_2500BASEX) ? true : false;
57 bus.priv = priv->mac->phyregs;
59 bus.priv = priv->pcs_mdio;
60 bus.read = memac_mdio_read;
61 bus.write = memac_mdio_write;
62 bus.reset = memac_mdio_reset;
66 /* SGMII IF mode + AN enable only for 1G SGMII, not for 2.5G */
68 value = PHY_SGMII_CR_PHY_RESET |
69 PHY_SGMII_IF_SPEED_GIGABIT |
70 PHY_SGMII_IF_MODE_SGMII;
72 value = PHY_SGMII_IF_MODE_SGMII | PHY_SGMII_IF_MODE_AN;
74 for (j = 0; j <= 3; j++)
75 debug("dump PCS reg %#x: %#x\n", j,
76 memac_mdio_read(&bus, i, MDIO_DEVAD_NONE, j));
78 memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x14, value);
80 /* Dev ability according to SGMII specification */
81 value = PHY_SGMII_DEV_ABILITY_SGMII;
82 memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x4, value);
85 /* Adjust link timer for 2.5G SGMII,
86 * 1.6 ms in units of 3.2 ns:
87 * 1.6ms / 3.2ns = 5 * 10^5 = 0x7a120.
89 memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x13, 0x0007);
90 memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x12, 0xa120);
92 /* Adjust link timer for SGMII,
93 * 1.6 ms in units of 8 ns:
94 * 1.6ms / 8ns = 2 * 10^5 = 0x30d40.
96 memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x13, 0x0003);
97 memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x12, 0x0d40);
101 value = PHY_SGMII_CR_DEF_VAL | PHY_SGMII_CR_RESET_AN;
102 memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0, value);
104 if ((priv->enet_if == PHY_INTERFACE_MODE_QSGMII) && (i < 3)) {
109 struct dtsec *regs = priv->mac->base;
110 struct tsec_mii_mng *phyregs = priv->mac->phyregs;
113 * Access TBI PHY registers at given TSEC register offset as
114 * opposed to the register offset used for external PHY accesses
116 tsec_local_mdio_write(phyregs, in_be32(®s->tbipa), 0, TBI_TBICON,
118 tsec_local_mdio_write(phyregs, in_be32(®s->tbipa), 0, TBI_ANA,
120 tsec_local_mdio_write(phyregs, in_be32(®s->tbipa), 0,
121 TBI_CR, TBICR_SETTINGS);
125 static void dtsec_init_phy(struct fm_eth *fm_eth)
127 #ifndef CONFIG_SYS_FMAN_V3
128 struct dtsec *regs = (struct dtsec *)CONFIG_SYS_FSL_FM1_DTSEC1_ADDR;
130 /* Assign a Physical address to the TBI */
131 out_be32(®s->tbipa, CONFIG_SYS_TBIPA_VALUE);
134 if (fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII ||
135 fm_eth->enet_if == PHY_INTERFACE_MODE_QSGMII ||
136 fm_eth->enet_if == PHY_INTERFACE_MODE_2500BASEX)
137 dtsec_configure_serdes(fm_eth);
140 #ifndef CONFIG_DM_ETH
142 static int tgec_is_fibre(struct fm_eth *fm)
146 sprintf(phyopt, "fsl_fm%d_xaui_phy", fm->fm_index + 1);
148 return hwconfig_arg_cmp(phyopt, "xfi");
151 #endif /* CONFIG_DM_ETH */
154 static u16 muram_readw(u16 *addr)
156 ulong base = (ulong)addr & ~0x3UL;
157 u32 val32 = in_be32((void *)base);
161 byte_pos = (ulong)addr & 0x3UL;
163 ret = (u16)(val32 & 0x0000ffff);
165 ret = (u16)((val32 & 0xffff0000) >> 16);
170 static void muram_writew(u16 *addr, u16 val)
172 ulong base = (ulong)addr & ~0x3UL;
173 u32 org32 = in_be32((void *)base);
177 byte_pos = (ulong)addr & 0x3UL;
179 val32 = (org32 & 0xffff0000) | val;
181 val32 = (org32 & 0x0000ffff) | ((u32)val << 16);
183 out_be32((void *)base, val32);
186 static void bmi_rx_port_disable(struct fm_bmi_rx_port *rx_port)
188 int timeout = 1000000;
190 clrbits_be32(&rx_port->fmbm_rcfg, FMBM_RCFG_EN);
192 /* wait until the rx port is not busy */
193 while ((in_be32(&rx_port->fmbm_rst) & FMBM_RST_BSY) && timeout--)
196 printf("%s - timeout\n", __func__);
199 static void bmi_rx_port_init(struct fm_bmi_rx_port *rx_port)
201 /* set BMI to independent mode, Rx port disable */
202 out_be32(&rx_port->fmbm_rcfg, FMBM_RCFG_IM);
203 /* clear FOF in IM case */
204 out_be32(&rx_port->fmbm_rim, 0);
205 /* Rx frame next engine -RISC */
206 out_be32(&rx_port->fmbm_rfne, NIA_ENG_RISC | NIA_RISC_AC_IM_RX);
207 /* Rx command attribute - no order, MR[3] = 1 */
208 clrbits_be32(&rx_port->fmbm_rfca, FMBM_RFCA_ORDER | FMBM_RFCA_MR_MASK);
209 setbits_be32(&rx_port->fmbm_rfca, FMBM_RFCA_MR(4));
210 /* enable Rx statistic counters */
211 out_be32(&rx_port->fmbm_rstc, FMBM_RSTC_EN);
212 /* disable Rx performance counters */
213 out_be32(&rx_port->fmbm_rpc, 0);
216 static void bmi_tx_port_disable(struct fm_bmi_tx_port *tx_port)
218 int timeout = 1000000;
220 clrbits_be32(&tx_port->fmbm_tcfg, FMBM_TCFG_EN);
222 /* wait until the tx port is not busy */
223 while ((in_be32(&tx_port->fmbm_tst) & FMBM_TST_BSY) && timeout--)
226 printf("%s - timeout\n", __func__);
229 static void bmi_tx_port_init(struct fm_bmi_tx_port *tx_port)
231 /* set BMI to independent mode, Tx port disable */
232 out_be32(&tx_port->fmbm_tcfg, FMBM_TCFG_IM);
233 /* Tx frame next engine -RISC */
234 out_be32(&tx_port->fmbm_tfne, NIA_ENG_RISC | NIA_RISC_AC_IM_TX);
235 out_be32(&tx_port->fmbm_tfene, NIA_ENG_RISC | NIA_RISC_AC_IM_TX);
236 /* Tx command attribute - no order, MR[3] = 1 */
237 clrbits_be32(&tx_port->fmbm_tfca, FMBM_TFCA_ORDER | FMBM_TFCA_MR_MASK);
238 setbits_be32(&tx_port->fmbm_tfca, FMBM_TFCA_MR(4));
239 /* enable Tx statistic counters */
240 out_be32(&tx_port->fmbm_tstc, FMBM_TSTC_EN);
241 /* disable Tx performance counters */
242 out_be32(&tx_port->fmbm_tpc, 0);
245 static int fm_eth_rx_port_parameter_init(struct fm_eth *fm_eth)
247 struct fm_port_global_pram *pram;
248 u32 pram_page_offset;
249 void *rx_bd_ring_base;
251 u32 bd_ring_base_lo, bd_ring_base_hi;
253 struct fm_port_bd *rxbd;
254 struct fm_port_qd *rxqd;
255 struct fm_bmi_rx_port *bmi_rx_port = fm_eth->rx_port;
258 /* alloc global parameter ram at MURAM */
259 pram = (struct fm_port_global_pram *)fm_muram_alloc(fm_eth->fm_index,
260 FM_PRAM_SIZE, FM_PRAM_ALIGN);
262 printf("%s: No muram for Rx global parameter\n", __func__);
266 fm_eth->rx_pram = pram;
268 /* parameter page offset to MURAM */
269 pram_page_offset = (void *)pram - fm_muram_base(fm_eth->fm_index);
271 /* enable global mode- snooping data buffers and BDs */
272 out_be32(&pram->mode, PRAM_MODE_GLOBAL);
274 /* init the Rx queue descriptor pionter */
275 out_be32(&pram->rxqd_ptr, pram_page_offset + 0x20);
277 /* set the max receive buffer length, power of 2 */
278 muram_writew(&pram->mrblr, MAX_RXBUF_LOG2);
280 /* alloc Rx buffer descriptors from main memory */
281 rx_bd_ring_base = malloc(sizeof(struct fm_port_bd)
283 if (!rx_bd_ring_base)
286 memset(rx_bd_ring_base, 0, sizeof(struct fm_port_bd)
289 /* alloc Rx buffer from main memory */
290 rx_buf_pool = malloc(MAX_RXBUF_LEN * RX_BD_RING_SIZE);
292 free(rx_bd_ring_base);
296 memset(rx_buf_pool, 0, MAX_RXBUF_LEN * RX_BD_RING_SIZE);
297 debug("%s: rx_buf_pool = %p\n", __func__, rx_buf_pool);
299 /* save them to fm_eth */
300 fm_eth->rx_bd_ring = rx_bd_ring_base;
301 fm_eth->cur_rxbd = rx_bd_ring_base;
302 fm_eth->rx_buf = rx_buf_pool;
304 /* init Rx BDs ring */
305 rxbd = (struct fm_port_bd *)rx_bd_ring_base;
306 for (i = 0; i < RX_BD_RING_SIZE; i++) {
307 muram_writew(&rxbd->status, RxBD_EMPTY);
308 muram_writew(&rxbd->len, 0);
309 buf_hi = upper_32_bits(virt_to_phys(rx_buf_pool +
311 buf_lo = lower_32_bits(virt_to_phys(rx_buf_pool +
313 muram_writew(&rxbd->buf_ptr_hi, (u16)buf_hi);
314 out_be32(&rxbd->buf_ptr_lo, buf_lo);
318 /* set the Rx queue descriptor */
320 muram_writew(&rxqd->gen, 0);
321 bd_ring_base_hi = upper_32_bits(virt_to_phys(rx_bd_ring_base));
322 bd_ring_base_lo = lower_32_bits(virt_to_phys(rx_bd_ring_base));
323 muram_writew(&rxqd->bd_ring_base_hi, (u16)bd_ring_base_hi);
324 out_be32(&rxqd->bd_ring_base_lo, bd_ring_base_lo);
325 muram_writew(&rxqd->bd_ring_size, sizeof(struct fm_port_bd)
327 muram_writew(&rxqd->offset_in, 0);
328 muram_writew(&rxqd->offset_out, 0);
330 /* set IM parameter ram pointer to Rx Frame Queue ID */
331 out_be32(&bmi_rx_port->fmbm_rfqid, pram_page_offset);
336 static int fm_eth_tx_port_parameter_init(struct fm_eth *fm_eth)
338 struct fm_port_global_pram *pram;
339 u32 pram_page_offset;
340 void *tx_bd_ring_base;
341 u32 bd_ring_base_lo, bd_ring_base_hi;
342 struct fm_port_bd *txbd;
343 struct fm_port_qd *txqd;
344 struct fm_bmi_tx_port *bmi_tx_port = fm_eth->tx_port;
347 /* alloc global parameter ram at MURAM */
348 pram = (struct fm_port_global_pram *)fm_muram_alloc(fm_eth->fm_index,
349 FM_PRAM_SIZE, FM_PRAM_ALIGN);
351 printf("%s: No muram for Tx global parameter\n", __func__);
354 fm_eth->tx_pram = pram;
356 /* parameter page offset to MURAM */
357 pram_page_offset = (void *)pram - fm_muram_base(fm_eth->fm_index);
359 /* enable global mode- snooping data buffers and BDs */
360 out_be32(&pram->mode, PRAM_MODE_GLOBAL);
362 /* init the Tx queue descriptor pionter */
363 out_be32(&pram->txqd_ptr, pram_page_offset + 0x40);
365 /* alloc Tx buffer descriptors from main memory */
366 tx_bd_ring_base = malloc(sizeof(struct fm_port_bd)
368 if (!tx_bd_ring_base)
371 memset(tx_bd_ring_base, 0, sizeof(struct fm_port_bd)
373 /* save it to fm_eth */
374 fm_eth->tx_bd_ring = tx_bd_ring_base;
375 fm_eth->cur_txbd = tx_bd_ring_base;
377 /* init Tx BDs ring */
378 txbd = (struct fm_port_bd *)tx_bd_ring_base;
379 for (i = 0; i < TX_BD_RING_SIZE; i++) {
380 muram_writew(&txbd->status, TxBD_LAST);
381 muram_writew(&txbd->len, 0);
382 muram_writew(&txbd->buf_ptr_hi, 0);
383 out_be32(&txbd->buf_ptr_lo, 0);
387 /* set the Tx queue decriptor */
389 bd_ring_base_hi = upper_32_bits(virt_to_phys(tx_bd_ring_base));
390 bd_ring_base_lo = lower_32_bits(virt_to_phys(tx_bd_ring_base));
391 muram_writew(&txqd->bd_ring_base_hi, (u16)bd_ring_base_hi);
392 out_be32(&txqd->bd_ring_base_lo, bd_ring_base_lo);
393 muram_writew(&txqd->bd_ring_size, sizeof(struct fm_port_bd)
395 muram_writew(&txqd->offset_in, 0);
396 muram_writew(&txqd->offset_out, 0);
398 /* set IM parameter ram pointer to Tx Confirmation Frame Queue ID */
399 out_be32(&bmi_tx_port->fmbm_tcfqid, pram_page_offset);
404 static int fm_eth_init(struct fm_eth *fm_eth)
408 ret = fm_eth_rx_port_parameter_init(fm_eth);
412 ret = fm_eth_tx_port_parameter_init(fm_eth);
419 static int fm_eth_startup(struct fm_eth *fm_eth)
421 struct fsl_enet_mac *mac;
426 /* Rx/TxBDs, Rx/TxQDs, Rx buff and parameter ram init */
427 ret = fm_eth_init(fm_eth);
430 /* setup the MAC controller */
433 /* For some reason we need to set SPEED_100 */
434 if (((fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII) ||
435 (fm_eth->enet_if == PHY_INTERFACE_MODE_2500BASEX) ||
436 (fm_eth->enet_if == PHY_INTERFACE_MODE_QSGMII)) &&
438 mac->set_if_mode(mac, fm_eth->enet_if, SPEED_100);
440 /* init bmi rx port, IM mode and disable */
441 bmi_rx_port_init(fm_eth->rx_port);
442 /* init bmi tx port, IM mode and disable */
443 bmi_tx_port_init(fm_eth->tx_port);
448 static void fmc_tx_port_graceful_stop_enable(struct fm_eth *fm_eth)
450 struct fm_port_global_pram *pram;
452 pram = fm_eth->tx_pram;
453 /* graceful stop transmission of frames */
454 setbits_be32(&pram->mode, PRAM_MODE_GRACEFUL_STOP);
458 static void fmc_tx_port_graceful_stop_disable(struct fm_eth *fm_eth)
460 struct fm_port_global_pram *pram;
462 pram = fm_eth->tx_pram;
463 /* re-enable transmission of frames */
464 clrbits_be32(&pram->mode, PRAM_MODE_GRACEFUL_STOP);
468 #ifndef CONFIG_DM_ETH
469 static int fm_eth_open(struct eth_device *dev, struct bd_info *bd)
471 static int fm_eth_open(struct udevice *dev)
474 #ifndef CONFIG_DM_ETH
475 struct fm_eth *fm_eth = dev->priv;
477 struct eth_pdata *pdata = dev_get_plat(dev);
478 struct fm_eth *fm_eth = dev_get_priv(dev);
480 unsigned char *enetaddr;
481 struct fsl_enet_mac *mac;
488 #ifndef CONFIG_DM_ETH
489 enetaddr = &dev->enetaddr[0];
491 enetaddr = pdata->enetaddr;
494 /* setup the MAC address */
495 if (enetaddr[0] & 0x01) {
496 printf("%s: MacAddress is multicast address\n", __func__);
498 enetaddr[5] = fm_eth->num;
500 mac->set_mac_addr(mac, enetaddr);
502 /* enable bmi Rx port */
503 setbits_be32(&fm_eth->rx_port->fmbm_rcfg, FMBM_RCFG_EN);
504 /* enable MAC rx/tx port */
505 mac->enable_mac(mac);
506 /* enable bmi Tx port */
507 setbits_be32(&fm_eth->tx_port->fmbm_tcfg, FMBM_TCFG_EN);
508 /* re-enable transmission of frame */
509 fmc_tx_port_graceful_stop_disable(fm_eth);
512 if (fm_eth->phydev) {
513 ret = phy_startup(fm_eth->phydev);
515 #ifndef CONFIG_DM_ETH
516 printf("%s: Could not initialize\n",
517 fm_eth->phydev->dev->name);
519 printf("%s: Could not initialize\n", dev->name);
527 fm_eth->phydev->speed = SPEED_1000;
528 fm_eth->phydev->link = 1;
529 fm_eth->phydev->duplex = DUPLEX_FULL;
532 /* set the MAC-PHY mode */
533 mac->set_if_mode(mac, fm_eth->enet_if, fm_eth->phydev->speed);
534 debug("MAC IF mode %d, speed %d, link %d\n", fm_eth->enet_if,
535 fm_eth->phydev->speed, fm_eth->phydev->link);
537 if (!fm_eth->phydev->link)
538 printf("%s: No link.\n", fm_eth->phydev->dev->name);
540 return fm_eth->phydev->link ? 0 : -1;
543 #ifndef CONFIG_DM_ETH
544 static void fm_eth_halt(struct eth_device *dev)
546 static void fm_eth_halt(struct udevice *dev)
549 struct fm_eth *fm_eth;
550 struct fsl_enet_mac *mac;
552 #ifndef CONFIG_DM_ETH
553 fm_eth = (struct fm_eth *)dev->priv;
555 fm_eth = dev_get_priv(dev);
559 /* graceful stop the transmission of frames */
560 fmc_tx_port_graceful_stop_enable(fm_eth);
561 /* disable bmi Tx port */
562 bmi_tx_port_disable(fm_eth->tx_port);
563 /* disable MAC rx/tx port */
564 mac->disable_mac(mac);
565 /* disable bmi Rx port */
566 bmi_rx_port_disable(fm_eth->rx_port);
570 phy_shutdown(fm_eth->phydev);
574 #ifndef CONFIG_DM_ETH
575 static int fm_eth_send(struct eth_device *dev, void *buf, int len)
577 static int fm_eth_send(struct udevice *dev, void *buf, int len)
580 struct fm_eth *fm_eth;
581 struct fm_port_global_pram *pram;
582 struct fm_port_bd *txbd, *txbd_base;
586 #ifndef CONFIG_DM_ETH
587 fm_eth = (struct fm_eth *)dev->priv;
589 fm_eth = dev_get_priv(dev);
591 pram = fm_eth->tx_pram;
592 txbd = fm_eth->cur_txbd;
594 /* find one empty TxBD */
595 for (i = 0; muram_readw(&txbd->status) & TxBD_READY; i++) {
598 printf("%s: Tx buffer not ready, txbd->status = 0x%x\n",
599 dev->name, muram_readw(&txbd->status));
604 muram_writew(&txbd->buf_ptr_hi, (u16)upper_32_bits(virt_to_phys(buf)));
605 out_be32(&txbd->buf_ptr_lo, lower_32_bits(virt_to_phys(buf)));
606 muram_writew(&txbd->len, len);
608 muram_writew(&txbd->status, TxBD_READY | TxBD_LAST);
611 /* update TxQD, let RISC to send the packet */
612 offset_in = muram_readw(&pram->txqd.offset_in);
613 offset_in += sizeof(struct fm_port_bd);
614 if (offset_in >= muram_readw(&pram->txqd.bd_ring_size))
616 muram_writew(&pram->txqd.offset_in, offset_in);
619 /* wait for buffer to be transmitted */
620 for (i = 0; muram_readw(&txbd->status) & TxBD_READY; i++) {
623 printf("%s: Tx error, txbd->status = 0x%x\n",
624 dev->name, muram_readw(&txbd->status));
629 /* advance the TxBD */
631 txbd_base = (struct fm_port_bd *)fm_eth->tx_bd_ring;
632 if (txbd >= (txbd_base + TX_BD_RING_SIZE))
634 /* update current txbd */
635 fm_eth->cur_txbd = (void *)txbd;
640 static struct fm_port_bd *fm_eth_free_one(struct fm_eth *fm_eth,
641 struct fm_port_bd *rxbd)
643 struct fm_port_global_pram *pram;
644 struct fm_port_bd *rxbd_base;
647 pram = fm_eth->rx_pram;
649 /* clear the RxBDs */
650 muram_writew(&rxbd->status, RxBD_EMPTY);
651 muram_writew(&rxbd->len, 0);
656 rxbd_base = (struct fm_port_bd *)fm_eth->rx_bd_ring;
657 if (rxbd >= (rxbd_base + RX_BD_RING_SIZE))
661 offset_out = muram_readw(&pram->rxqd.offset_out);
662 offset_out += sizeof(struct fm_port_bd);
663 if (offset_out >= muram_readw(&pram->rxqd.bd_ring_size))
665 muram_writew(&pram->rxqd.offset_out, offset_out);
671 #ifndef CONFIG_DM_ETH
672 static int fm_eth_recv(struct eth_device *dev)
674 static int fm_eth_recv(struct udevice *dev, int flags, uchar **packetp)
677 struct fm_eth *fm_eth;
678 struct fm_port_bd *rxbd;
684 #ifndef CONFIG_DM_ETH
685 fm_eth = (struct fm_eth *)dev->priv;
687 fm_eth = dev_get_priv(dev);
689 rxbd = fm_eth->cur_rxbd;
690 status = muram_readw(&rxbd->status);
692 while (!(status & RxBD_EMPTY)) {
693 if (!(status & RxBD_ERROR)) {
694 buf_hi = muram_readw(&rxbd->buf_ptr_hi);
695 buf_lo = in_be32(&rxbd->buf_ptr_lo);
696 data = (u8 *)((ulong)(buf_hi << 16) << 16 | buf_lo);
697 len = muram_readw(&rxbd->len);
698 #ifndef CONFIG_DM_ETH
699 net_process_received_packet(data, len);
705 printf("%s: Rx error\n", dev->name);
709 /* free current bd, advance to next one */
710 rxbd = fm_eth_free_one(fm_eth, rxbd);
712 /* read next status */
713 status = muram_readw(&rxbd->status);
715 fm_eth->cur_rxbd = (void *)rxbd;
721 static int fm_eth_free_pkt(struct udevice *dev, uchar *packet, int length)
723 struct fm_eth *fm_eth = (struct fm_eth *)dev_get_priv(dev);
725 fm_eth->cur_rxbd = fm_eth_free_one(fm_eth, fm_eth->cur_rxbd);
729 #endif /* CONFIG_DM_ETH */
731 #ifndef CONFIG_DM_ETH
732 static int fm_eth_init_mac(struct fm_eth *fm_eth, struct ccsr_fman *reg)
734 struct fsl_enet_mac *mac;
736 void *base, *phyregs = NULL;
740 #ifdef CONFIG_SYS_FMAN_V3
741 #ifndef CONFIG_FSL_FM_10GEC_REGULAR_NOTATION
742 if (fm_eth->type == FM_ETH_10G_E) {
743 /* 10GEC1/10GEC2 use mEMAC9/mEMAC10 on T2080/T4240.
744 * 10GEC3/10GEC4 use mEMAC1/mEMAC2 on T2080.
745 * 10GEC1 uses mEMAC1 on T1024.
746 * so it needs to change the num.
748 if (fm_eth->num >= 2)
754 base = ®->memac[num].fm_memac;
755 phyregs = ®->memac[num].fm_memac_mdio;
757 /* Get the mac registers base address */
758 if (fm_eth->type == FM_ETH_1G_E) {
759 base = ®->mac_1g[num].fm_dtesc;
760 phyregs = ®->mac_1g[num].fm_mdio.miimcfg;
762 base = ®->mac_10g[num].fm_10gec;
763 phyregs = ®->mac_10g[num].fm_10gec_mdio;
767 /* alloc mac controller */
768 mac = malloc(sizeof(struct fsl_enet_mac));
771 memset(mac, 0, sizeof(struct fsl_enet_mac));
773 /* save the mac to fm_eth struct */
776 #ifdef CONFIG_SYS_FMAN_V3
777 init_memac(mac, base, phyregs, MAX_RXBUF_LEN);
779 if (fm_eth->type == FM_ETH_1G_E)
780 init_dtsec(mac, base, phyregs, MAX_RXBUF_LEN);
782 init_tgec(mac, base, phyregs, MAX_RXBUF_LEN);
787 #else /* CONFIG_DM_ETH */
788 static int fm_eth_init_mac(struct fm_eth *fm_eth, void *reg)
790 #ifndef CONFIG_SYS_FMAN_V3
794 fm_eth->mac = kzalloc(sizeof(*fm_eth->mac), GFP_KERNEL);
798 #ifndef CONFIG_SYS_FMAN_V3
799 mdio = fman_mdio(fm_eth->dev->parent, fm_eth->mac_type, fm_eth->num);
800 debug("MDIO %d @ %p\n", fm_eth->num, mdio);
803 switch (fm_eth->mac_type) {
804 #ifdef CONFIG_SYS_FMAN_V3
806 init_memac(fm_eth->mac, reg, NULL, MAX_RXBUF_LEN);
810 init_dtsec(fm_eth->mac, reg, mdio, MAX_RXBUF_LEN);
813 init_tgec(fm_eth->mac, reg, mdio, MAX_RXBUF_LEN);
820 #endif /* CONFIG_DM_ETH */
822 static int init_phy(struct fm_eth *fm_eth)
825 u32 supported = PHY_GBIT_FEATURES;
826 #ifndef CONFIG_DM_ETH
827 struct phy_device *phydev = NULL;
830 if (fm_eth->type == FM_ETH_10G_E)
831 supported = PHY_10G_FEATURES;
832 if (fm_eth->enet_if == PHY_INTERFACE_MODE_2500BASEX)
833 supported |= SUPPORTED_2500baseX_Full;
836 if (fm_eth->type == FM_ETH_1G_E)
837 dtsec_init_phy(fm_eth);
841 #ifdef CONFIG_DM_MDIO
842 fm_eth->phydev = dm_eth_phy_connect(fm_eth->dev);
846 fm_eth->phydev->advertising &= supported;
847 fm_eth->phydev->supported &= supported;
849 phy_config(fm_eth->phydev);
851 #else /* CONFIG_DM_ETH */
854 phydev = phy_connect(fm_eth->bus, fm_eth->phyaddr, fm_eth->dev,
857 printf("Failed to connect\n");
864 if (fm_eth->type == FM_ETH_1G_E) {
865 supported = (SUPPORTED_10baseT_Half |
866 SUPPORTED_10baseT_Full |
867 SUPPORTED_100baseT_Half |
868 SUPPORTED_100baseT_Full |
869 SUPPORTED_1000baseT_Full);
871 supported = SUPPORTED_10000baseT_Full;
873 if (tgec_is_fibre(fm_eth))
874 phydev->port = PORT_FIBRE;
877 phydev->supported &= supported;
878 phydev->advertising = phydev->supported;
880 fm_eth->phydev = phydev;
884 #endif /* CONFIG_DM_ETH */
888 #ifndef CONFIG_DM_ETH
889 int fm_eth_initialize(struct ccsr_fman *reg, struct fm_eth_info *info)
891 struct eth_device *dev;
892 struct fm_eth *fm_eth;
893 int i, num = info->num;
896 /* alloc eth device */
897 dev = (struct eth_device *)malloc(sizeof(struct eth_device));
900 memset(dev, 0, sizeof(struct eth_device));
902 /* alloc the FMan ethernet private struct */
903 fm_eth = (struct fm_eth *)malloc(sizeof(struct fm_eth));
906 memset(fm_eth, 0, sizeof(struct fm_eth));
908 /* save off some things we need from the info struct */
909 fm_eth->fm_index = info->index - 1; /* keep as 0 based for muram */
911 fm_eth->type = info->type;
913 fm_eth->rx_port = (void *)®->port[info->rx_port_id - 1].fm_bmi;
914 fm_eth->tx_port = (void *)®->port[info->tx_port_id - 1].fm_bmi;
916 /* set the ethernet max receive length */
917 fm_eth->max_rx_len = MAX_RXBUF_LEN;
919 /* init global mac structure */
920 ret = fm_eth_init_mac(fm_eth, reg);
924 /* keep same as the manual, we call FMAN1, FMAN2, DTSEC1, DTSEC2, etc */
925 if (fm_eth->type == FM_ETH_1G_E)
926 sprintf(dev->name, "FM%d@DTSEC%d", info->index, num + 1);
928 sprintf(dev->name, "FM%d@TGEC%d", info->index, num + 1);
930 devlist[num_controllers++] = dev;
932 dev->priv = (void *)fm_eth;
933 dev->init = fm_eth_open;
934 dev->halt = fm_eth_halt;
935 dev->send = fm_eth_send;
936 dev->recv = fm_eth_recv;
938 fm_eth->bus = info->bus;
939 fm_eth->phyaddr = info->phy_addr;
940 fm_eth->enet_if = info->enet_if;
942 /* startup the FM im */
943 ret = fm_eth_startup(fm_eth);
949 /* clear the ethernet address */
950 for (i = 0; i < 6; i++)
951 dev->enetaddr[i] = 0;
956 #else /* CONFIG_DM_ETH */
958 static int fm_eth_bind(struct udevice *dev)
963 if (ofnode_read_u32(ofnode_get_parent(dev_ofnode(dev)), "cell-index", &fm)) {
964 printf("FMan node property cell-index missing\n");
968 if (dev && dev_read_u32(dev, "cell-index", &num)) {
969 printf("FMan MAC node property cell-index missing\n");
973 sprintf(mac_name, "fm%d-mac%d", fm + 1, num + 1);
974 device_set_name(dev, mac_name);
976 debug("%s - binding %s\n", __func__, mac_name);
981 static struct udevice *fm_get_internal_mdio(struct udevice *dev)
983 struct ofnode_phandle_args phandle = {.node = ofnode_null()};
984 struct udevice *mdiodev;
986 if (dev_read_phandle_with_args(dev, "pcsphy-handle", NULL,
988 !ofnode_valid(phandle.node)) {
989 if (dev_read_phandle_with_args(dev, "tbi-handle", NULL,
991 !ofnode_valid(phandle.node)) {
992 printf("Issue reading pcsphy-handle/tbi-handle for MAC %s\n",
998 if (uclass_get_device_by_ofnode(UCLASS_MDIO,
999 ofnode_get_parent(phandle.node),
1001 printf("can't find MDIO bus for node %s\n",
1002 ofnode_get_name(ofnode_get_parent(phandle.node)));
1005 debug("Found internal MDIO bus %p\n", mdiodev);
1010 static int fm_eth_probe(struct udevice *dev)
1012 struct fm_eth *fm_eth = (struct fm_eth *)dev_get_priv(dev);
1013 struct ofnode_phandle_args args;
1017 debug("%s enter for dev %p fm_eth %p - %s\n", __func__, dev, fm_eth,
1018 (dev) ? dev->name : "-");
1021 printf("%s already probed, exit\n", (dev) ? dev->name : "-");
1026 fm_eth->fm_index = fman_id(dev->parent);
1027 reg = (void *)(uintptr_t)dev_read_addr(dev);
1028 fm_eth->mac_type = dev_get_driver_data(dev);
1029 #ifdef CONFIG_PHYLIB
1030 fm_eth->enet_if = dev_read_phy_mode(dev);
1032 fm_eth->enet_if = PHY_INTERFACE_MODE_SGMII;
1033 printf("%s: warning - unable to determine interface type\n", __func__);
1035 switch (fm_eth->mac_type) {
1036 #ifndef CONFIG_SYS_FMAN_V3
1038 fm_eth->type = FM_ETH_10G_E;
1043 /* default to 1G, 10G is indicated by port property in dts */
1045 fm_eth->type = FM_ETH_1G_E;
1049 if (dev_read_u32(dev, "cell-index", &fm_eth->num)) {
1050 printf("FMan MAC node property cell-index missing\n");
1054 if (dev_read_phandle_with_args(dev, "fsl,fman-ports", NULL,
1056 goto ports_ref_failure;
1057 index = ofnode_read_u32_default(args.node, "cell-index", 0);
1059 goto ports_ref_failure;
1060 fm_eth->rx_port = fman_port(dev->parent, index);
1062 if (ofnode_read_bool(args.node, "fsl,fman-10g-port"))
1063 fm_eth->type = FM_ETH_10G_E;
1065 if (dev_read_phandle_with_args(dev, "fsl,fman-ports", NULL,
1067 goto ports_ref_failure;
1068 index = ofnode_read_u32_default(args.node, "cell-index", 0);
1070 goto ports_ref_failure;
1071 fm_eth->tx_port = fman_port(dev->parent, index);
1073 /* set the ethernet max receive length */
1074 fm_eth->max_rx_len = MAX_RXBUF_LEN;
1076 switch (fm_eth->enet_if) {
1077 case PHY_INTERFACE_MODE_QSGMII:
1078 /* all PCS blocks are accessed on one controller */
1079 if (fm_eth->num != 0)
1081 case PHY_INTERFACE_MODE_SGMII:
1082 case PHY_INTERFACE_MODE_2500BASEX:
1083 fm_eth->pcs_mdio = fm_get_internal_mdio(dev);
1089 /* init global mac structure */
1090 ret = fm_eth_init_mac(fm_eth, reg);
1094 /* startup the FM im */
1095 ret = fm_eth_startup(fm_eth);
1098 ret = init_phy(fm_eth);
1103 printf("Issue reading fsl,fman-ports for MAC %s\n", dev->name);
1107 static int fm_eth_remove(struct udevice *dev)
1112 static const struct eth_ops fm_eth_ops = {
1113 .start = fm_eth_open,
1114 .send = fm_eth_send,
1115 .recv = fm_eth_recv,
1116 .free_pkt = fm_eth_free_pkt,
1117 .stop = fm_eth_halt,
1120 static const struct udevice_id fm_eth_ids[] = {
1121 #ifdef CONFIG_SYS_FMAN_V3
1122 { .compatible = "fsl,fman-memac", .data = FM_MEMAC },
1124 { .compatible = "fsl,fman-dtsec", .data = FM_DTSEC },
1125 { .compatible = "fsl,fman-xgec", .data = FM_TGEC },
1130 U_BOOT_DRIVER(eth_fman) = {
1133 .of_match = fm_eth_ids,
1134 .bind = fm_eth_bind,
1135 .probe = fm_eth_probe,
1136 .remove = fm_eth_remove,
1138 .priv_auto = sizeof(struct fm_eth),
1139 .plat_auto = sizeof(struct eth_pdata),
1140 .flags = DM_FLAG_ALLOC_PRIV_DMA,
1142 #endif /* CONFIG_DM_ETH */