1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2014-2016 Freescale Semiconductor, Inc.
11 #include <asm/types.h>
17 #include <linux/bug.h>
18 #include <linux/compat.h>
19 #include <fsl-mc/fsl_dpmac.h>
21 #include <fsl-mc/ldpaa_wriop.h>
22 #include "ldpaa_eth.h"
26 static void init_phy(struct udevice *dev)
28 struct ldpaa_eth_priv *priv = dev_get_priv(dev);
30 priv->phy = dm_eth_phy_connect(dev);
35 phy_config(priv->phy);
38 static int init_phy(struct eth_device *dev)
40 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)dev->priv;
41 struct phy_device *phydev = NULL;
43 int phy_addr, phy_num;
46 bus = wriop_get_mdio(priv->dpmac_id);
50 for (phy_num = 0; phy_num < WRIOP_MAX_PHY_NUM; phy_num++) {
51 phy_addr = wriop_get_phy_address(priv->dpmac_id, phy_num);
55 phydev = phy_connect(bus, phy_addr, dev,
56 wriop_get_enet_if(priv->dpmac_id));
58 printf("Failed to connect\n");
62 wriop_set_phy_dev(priv->dpmac_id, phy_num, phydev);
63 ret = phy_config(phydev);
69 for (phy_num = 0; phy_num < WRIOP_MAX_PHY_NUM; phy_num++) {
70 phydev = wriop_get_phy_dev(priv->dpmac_id, phy_num);
75 wriop_set_phy_dev(priv->dpmac_id, phy_num, NULL);
86 #define DPNI_STATS_PER_PAGE 6
88 static const char *dpni_statistics[][DPNI_STATS_PER_PAGE] = {
90 "DPNI_CNT_ING_ALL_FRAMES",
91 "DPNI_CNT_ING_ALL_BYTES",
92 "DPNI_CNT_ING_MCAST_FRAMES",
93 "DPNI_CNT_ING_MCAST_BYTES",
94 "DPNI_CNT_ING_BCAST_FRAMES",
95 "DPNI_CNT_ING_BCAST_BYTES",
97 "DPNI_CNT_EGR_ALL_FRAMES",
98 "DPNI_CNT_EGR_ALL_BYTES",
99 "DPNI_CNT_EGR_MCAST_FRAMES",
100 "DPNI_CNT_EGR_MCAST_BYTES",
101 "DPNI_CNT_EGR_BCAST_FRAMES",
102 "DPNI_CNT_EGR_BCAST_BYTES",
104 "DPNI_CNT_ING_FILTERED_FRAMES",
105 "DPNI_CNT_ING_DISCARDED_FRAMES",
106 "DPNI_CNT_ING_NOBUFFER_DISCARDS",
107 "DPNI_CNT_EGR_DISCARDED_FRAMES",
108 "DPNI_CNT_EGR_CNF_FRAMES",
113 static void print_dpni_stats(const char *strings[],
114 struct dpni_statistics dpni_stats)
119 stat = (uint64_t *)&dpni_stats;
120 for (i = 0; i < DPNI_STATS_PER_PAGE; i++) {
121 if (strcmp(strings[i], "\0") == 0)
123 printf("%s= %llu\n", strings[i], *stat);
128 static void ldpaa_eth_get_dpni_counter(void)
131 unsigned int page = 0;
132 struct dpni_statistics dpni_stats;
134 printf("DPNI counters ..\n");
135 for (page = 0; page < 3; page++) {
136 err = dpni_get_statistics(dflt_mc_io, MC_CMD_NO_FLAGS,
137 dflt_dpni->dpni_handle, page,
140 printf("dpni_get_statistics: failed:");
141 printf("%d for page[%d]\n", err, page);
144 print_dpni_stats(dpni_statistics[page], dpni_stats);
149 static void ldpaa_eth_get_dpmac_counter(struct udevice *dev)
151 struct ldpaa_eth_priv *priv = dev_get_priv(dev);
153 static void ldpaa_eth_get_dpmac_counter(struct eth_device *net_dev)
155 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)net_dev->priv;
160 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS,
165 printf("dpmac_get_counter: DPMAC_CNT_ING_BYTE failed\n");
168 printf("\nDPMAC counters ..\n");
169 printf("DPMAC_CNT_ING_BYTE=%lld\n", value);
171 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS,
173 DPMAC_CNT_ING_FRAME_DISCARD,
176 printf("dpmac_get_counter: DPMAC_CNT_ING_FRAME_DISCARD failed\n");
179 printf("DPMAC_CNT_ING_FRAME_DISCARD=%lld\n", value);
181 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS,
183 DPMAC_CNT_ING_ALIGN_ERR,
186 printf("dpmac_get_counter: DPMAC_CNT_ING_ALIGN_ERR failed\n");
189 printf("DPMAC_CNT_ING_ALIGN_ERR =%lld\n", value);
191 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS,
196 printf("dpmac_get_counter: DPMAC_CNT_ING_BYTE failed\n");
199 printf("DPMAC_CNT_ING_BYTE=%lld\n", value);
201 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS,
203 DPMAC_CNT_ING_ERR_FRAME,
206 printf("dpmac_get_counter: DPMAC_CNT_ING_ERR_FRAME failed\n");
209 printf("DPMAC_CNT_ING_ERR_FRAME=%lld\n", value);
211 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS,
216 printf("dpmac_get_counter: DPMAC_CNT_EGR_BYTE failed\n");
219 printf("DPMAC_CNT_EGR_BYTE =%lld\n", value);
221 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS,
223 DPMAC_CNT_EGR_ERR_FRAME ,
226 printf("dpmac_get_counter: DPMAC_CNT_EGR_ERR_FRAME failed\n");
229 printf("DPMAC_CNT_EGR_ERR_FRAME =%lld\n", value);
233 static void ldpaa_eth_rx(struct ldpaa_eth_priv *priv,
234 const struct dpaa_fd *fd)
239 struct ldpaa_fas *fas;
240 uint32_t status, err;
241 u32 timeo = (CONFIG_SYS_HZ * 2) / 1000;
243 struct qbman_release_desc releasedesc;
244 struct qbman_swp *swp = dflt_dpio->sw_portal;
246 fd_addr = ldpaa_fd_get_addr(fd);
247 fd_offset = ldpaa_fd_get_offset(fd);
248 fd_length = ldpaa_fd_get_len(fd);
250 debug("Rx frame:data addr=0x%p size=0x%x\n", (u64 *)fd_addr, fd_length);
252 if (fd->simple.frc & LDPAA_FD_FRC_FASV) {
253 /* Read the frame annotation status word and check for errors */
254 fas = (struct ldpaa_fas *)
255 ((uint8_t *)(fd_addr) +
256 dflt_dpni->buf_layout.private_data_size);
257 status = le32_to_cpu(fas->status);
258 if (status & LDPAA_ETH_RX_ERR_MASK) {
259 printf("Rx frame error(s): 0x%08x\n",
260 status & LDPAA_ETH_RX_ERR_MASK);
262 } else if (status & LDPAA_ETH_RX_UNSUPP_MASK) {
263 printf("Unsupported feature in bitmask: 0x%08x\n",
264 status & LDPAA_ETH_RX_UNSUPP_MASK);
269 debug("Rx frame: To Upper layer\n");
270 net_process_received_packet((uint8_t *)(fd_addr) + fd_offset,
274 flush_dcache_range(fd_addr, fd_addr + LDPAA_ETH_RX_BUFFER_SIZE);
275 qbman_release_desc_clear(&releasedesc);
276 qbman_release_desc_set_bpid(&releasedesc, dflt_dpbp->dpbp_attr.bpid);
277 time_start = get_timer(0);
279 /* Release buffer into the QBMAN */
280 err = qbman_swp_release(swp, &releasedesc, &fd_addr, 1);
281 } while (get_timer(time_start) < timeo && err == -EBUSY);
284 printf("Rx frame: QBMAN buffer release fails\n");
290 static int ldpaa_eth_pull_dequeue_rx(struct udevice *dev,
291 int flags, uchar **packetp)
293 struct ldpaa_eth_priv *priv = dev_get_priv(dev);
295 static int ldpaa_eth_pull_dequeue_rx(struct eth_device *dev)
297 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)dev->priv;
299 const struct ldpaa_dq *dq;
300 const struct dpaa_fd *fd;
301 int i = 5, err = 0, status;
302 u32 timeo = (CONFIG_SYS_HZ * 2) / 1000;
304 static struct qbman_pull_desc pulldesc;
305 struct qbman_swp *swp = dflt_dpio->sw_portal;
308 qbman_pull_desc_clear(&pulldesc);
309 qbman_pull_desc_set_numframes(&pulldesc, 1);
310 qbman_pull_desc_set_fq(&pulldesc, priv->rx_dflt_fqid);
312 err = qbman_swp_pull(swp, &pulldesc);
314 printf("Dequeue frames error:0x%08x\n", err);
318 time_start = get_timer(0);
321 dq = qbman_swp_dqrr_next(swp);
322 } while (get_timer(time_start) < timeo && !dq);
325 /* Check for valid frame. If not sent a consume
326 * confirmation to QBMAN otherwise give it to NADK
327 * application and then send consume confirmation to
330 status = (uint8_t)ldpaa_dq_flags(dq);
331 if ((status & LDPAA_DQ_STAT_VALIDFRAME) == 0) {
332 debug("Dequeue RX frames:");
333 debug("No frame delivered\n");
335 qbman_swp_dqrr_consume(swp, dq);
339 fd = ldpaa_dq_fd(dq);
341 /* Obtain FD and process it */
342 ldpaa_eth_rx(priv, fd);
343 qbman_swp_dqrr_consume(swp, dq);
347 debug("No DQRR entries\n");
356 static int ldpaa_eth_tx(struct udevice *dev, void *buf, int len)
358 struct ldpaa_eth_priv *priv = dev_get_priv(dev);
360 static int ldpaa_eth_tx(struct eth_device *net_dev, void *buf, int len)
362 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)net_dev->priv;
366 int data_offset, err;
367 u32 timeo = (CONFIG_SYS_HZ * 10) / 1000;
369 struct qbman_swp *swp = dflt_dpio->sw_portal;
370 struct qbman_eq_desc ed;
371 struct qbman_release_desc releasedesc;
373 /* Setup the FD fields */
374 memset(&fd, 0, sizeof(fd));
376 data_offset = priv->tx_data_offset;
379 err = qbman_swp_acquire(dflt_dpio->sw_portal,
380 dflt_dpbp->dpbp_attr.bpid,
382 } while (err == -EBUSY);
385 printf("qbman_swp_acquire() failed\n");
389 debug("TX data: malloc buffer start=0x%p\n", (u64 *)buffer_start);
391 memcpy(((uint8_t *)(buffer_start) + data_offset), buf, len);
393 flush_dcache_range(buffer_start, buffer_start +
394 LDPAA_ETH_RX_BUFFER_SIZE);
396 ldpaa_fd_set_addr(&fd, (u64)buffer_start);
397 ldpaa_fd_set_offset(&fd, (uint16_t)(data_offset));
398 ldpaa_fd_set_bpid(&fd, dflt_dpbp->dpbp_attr.bpid);
399 ldpaa_fd_set_len(&fd, len);
401 fd.simple.ctrl = LDPAA_FD_CTRL_ASAL | LDPAA_FD_CTRL_PTA |
404 qbman_eq_desc_clear(&ed);
405 qbman_eq_desc_set_no_orp(&ed, 0);
406 qbman_eq_desc_set_qd(&ed, priv->tx_qdid, priv->tx_flow_id, 0);
408 time_start = get_timer(0);
410 while (get_timer(time_start) < timeo) {
411 err = qbman_swp_enqueue(swp, &ed,
412 (const struct qbman_fd *)(&fd));
418 printf("error enqueueing Tx frame\n");
425 qbman_release_desc_clear(&releasedesc);
426 qbman_release_desc_set_bpid(&releasedesc, dflt_dpbp->dpbp_attr.bpid);
427 time_start = get_timer(0);
429 /* Release buffer into the QBMAN */
430 err = qbman_swp_release(swp, &releasedesc, &buffer_start, 1);
431 } while (get_timer(time_start) < timeo && err == -EBUSY);
434 printf("TX data: QBMAN buffer release fails\n");
439 static struct phy_device *ldpaa_get_phydev(struct ldpaa_eth_priv *priv)
445 struct phy_device *phydev = NULL;
448 /* start the phy devices one by one and update the dpmac state */
449 for (phy_num = 0; phy_num < WRIOP_MAX_PHY_NUM; phy_num++) {
450 phydev = wriop_get_phy_dev(priv->dpmac_id, phy_num);
460 static int ldpaa_get_dpmac_state(struct ldpaa_eth_priv *priv,
461 struct dpmac_link_state *state)
463 phy_interface_t enet_if;
464 struct phy_device *phydev = NULL;
467 /* let's start off with maximum capabilities */
468 enet_if = wriop_get_enet_if(priv->dpmac_id);
470 case PHY_INTERFACE_MODE_XGMII:
471 state->rate = SPEED_10000;
474 state->rate = SPEED_1000;
479 state->options |= DPMAC_LINK_OPT_AUTONEG;
480 phydev = ldpaa_get_phydev(priv);
483 err = phy_startup(phydev);
485 printf("%s: Could not initialize\n", phydev->dev->name);
487 } else if (phydev->link) {
488 state->rate = min(state->rate, (uint32_t)phydev->speed);
490 state->options |= DPMAC_LINK_OPT_HALF_DUPLEX;
491 if (!phydev->autoneg)
492 state->options &= ~DPMAC_LINK_OPT_AUTONEG;
499 state->options &= ~DPMAC_LINK_OPT_AUTONEG;
511 static int ldpaa_eth_open(struct udevice *dev)
513 struct eth_pdata *plat = dev_get_platdata(dev);
514 struct ldpaa_eth_priv *priv = dev_get_priv(dev);
516 static int ldpaa_eth_open(struct eth_device *net_dev, bd_t *bd)
518 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)net_dev->priv;
520 struct dpmac_link_state dpmac_link_state = { 0 };
522 struct dpni_link_state link_state;
525 struct dpni_queue d_queue;
528 if (eth_is_active(dev))
531 if (net_dev->state == ETH_STATE_ACTIVE)
535 if (get_mc_boot_status() != 0) {
536 printf("ERROR (MC is not booted)\n");
540 if (get_dpl_apply_status() == 0) {
541 printf("ERROR (DPL is deployed. No device available)\n");
545 /* DPMAC initialization */
546 err = ldpaa_dpmac_setup(priv);
548 goto err_dpmac_setup;
550 err = ldpaa_get_dpmac_state(priv, &dpmac_link_state);
554 /* DPMAC binding DPNI */
555 err = ldpaa_dpmac_bind(priv);
559 /* DPNI initialization */
560 err = ldpaa_dpni_setup(priv);
564 err = ldpaa_dpbp_setup();
568 /* DPNI binding DPBP */
569 err = ldpaa_dpni_bind(priv);
574 err = dpni_add_mac_addr(dflt_mc_io, MC_CMD_NO_FLAGS,
575 dflt_dpni->dpni_handle, plat->enetaddr);
577 err = dpni_add_mac_addr(dflt_mc_io, MC_CMD_NO_FLAGS,
578 dflt_dpni->dpni_handle, net_dev->enetaddr);
581 printf("dpni_add_mac_addr() failed\n");
585 err = dpni_enable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
587 printf("dpni_enable() failed\n");
591 err = dpmac_set_link_state(dflt_mc_io, MC_CMD_NO_FLAGS,
592 priv->dpmac_handle, &dpmac_link_state);
594 printf("dpmac_set_link_state() failed\n");
599 printf("DPMAC link status: %d - ", dpmac_link_state.up);
600 dpmac_link_state.up == 0 ? printf("down\n") :
601 dpmac_link_state.up == 1 ? printf("up\n") : printf("error state\n");
603 err = dpni_get_link_state(dflt_mc_io, MC_CMD_NO_FLAGS,
604 dflt_dpni->dpni_handle, &link_state);
606 printf("dpni_get_link_state() failed\n");
610 printf("DPNI link status: %d - ", link_state.up);
611 link_state.up == 0 ? printf("down\n") :
612 link_state.up == 1 ? printf("up\n") : printf("error state\n");
615 memset(&d_queue, 0, sizeof(struct dpni_queue));
616 err = dpni_get_queue(dflt_mc_io, MC_CMD_NO_FLAGS,
617 dflt_dpni->dpni_handle, DPNI_QUEUE_RX,
620 printf("dpni_get_queue failed\n");
624 priv->rx_dflt_fqid = d_queue.fqid;
626 err = dpni_get_qdid(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle,
629 printf("dpni_get_qdid() failed\n");
633 return dpmac_link_state.up;
637 dpni_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
641 dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
644 dpmac_close(dflt_mc_io, MC_CMD_NO_FLAGS, priv->dpmac_handle);
645 dpmac_destroy(dflt_mc_io,
647 MC_CMD_NO_FLAGS, priv->dpmac_id);
653 static void ldpaa_eth_stop(struct udevice *dev)
655 struct ldpaa_eth_priv *priv = dev_get_priv(dev);
657 static void ldpaa_eth_stop(struct eth_device *net_dev)
659 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)net_dev->priv;
661 struct phy_device *phydev = NULL;
665 if (!eth_is_active(dev))
668 if ((net_dev->state == ETH_STATE_PASSIVE) ||
669 (net_dev->state == ETH_STATE_INIT))
674 ldpaa_eth_get_dpni_counter();
676 ldpaa_eth_get_dpmac_counter(dev);
678 ldpaa_eth_get_dpmac_counter(net_dev);
682 err = dprc_disconnect(dflt_mc_io, MC_CMD_NO_FLAGS,
683 dflt_dprc_handle, &dpmac_endpoint);
685 printf("dprc_disconnect() failed dpmac_endpoint\n");
687 err = dpmac_close(dflt_mc_io, MC_CMD_NO_FLAGS, priv->dpmac_handle);
689 printf("dpmac_close() failed\n");
691 err = dpmac_destroy(dflt_mc_io,
696 printf("dpmac_destroy() failed\n");
698 /* Stop Tx and Rx traffic */
699 err = dpni_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
701 printf("dpni_disable() failed\n");
703 phydev = ldpaa_get_phydev(priv);
705 phy_shutdown(phydev);
707 /* Free DPBP handle and reset. */
710 dpni_reset(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
712 printf("dpni_reset() failed\n");
714 dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
716 printf("dpni_close() failed\n");
719 static void ldpaa_dpbp_drain_cnt(int count)
721 uint64_t buf_array[7];
728 ret = qbman_swp_acquire(dflt_dpio->sw_portal,
729 dflt_dpbp->dpbp_attr.bpid,
732 printf("qbman_swp_acquire() failed\n");
735 for (i = 0; i < ret; i++) {
736 addr = (void *)buf_array[i];
737 debug("Free: buffer addr =0x%p\n", addr);
743 static void ldpaa_dpbp_drain(void)
746 for (i = 0; i < LDPAA_ETH_NUM_BUFS; i += 7)
747 ldpaa_dpbp_drain_cnt(7);
750 static int ldpaa_bp_add_7(uint16_t bpid)
752 uint64_t buf_array[7];
755 struct qbman_release_desc rd;
757 for (i = 0; i < 7; i++) {
758 addr = memalign(LDPAA_ETH_BUF_ALIGN, LDPAA_ETH_RX_BUFFER_SIZE);
760 printf("addr allocation failed\n");
763 memset(addr, 0x00, LDPAA_ETH_RX_BUFFER_SIZE);
764 flush_dcache_range((u64)addr,
765 (u64)(addr + LDPAA_ETH_RX_BUFFER_SIZE));
767 buf_array[i] = (uint64_t)addr;
768 debug("Release: buffer addr =0x%p\n", addr);
772 /* In case the portal is busy, retry until successful.
773 * This function is guaranteed to succeed in a reasonable amount
779 qbman_release_desc_clear(&rd);
780 qbman_release_desc_set_bpid(&rd, bpid);
781 } while (qbman_swp_release(dflt_dpio->sw_portal, &rd, buf_array, i));
792 static int ldpaa_dpbp_seed(uint16_t bpid)
797 for (i = 0; i < LDPAA_ETH_NUM_BUFS; i += 7) {
798 count = ldpaa_bp_add_7(bpid);
800 printf("Buffer Seed= %d\n", count);
806 static int ldpaa_dpbp_setup(void)
810 err = dpbp_open(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_attr.id,
811 &dflt_dpbp->dpbp_handle);
813 printf("dpbp_open() failed\n");
817 err = dpbp_enable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
819 printf("dpbp_enable() failed\n");
823 err = dpbp_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS,
824 dflt_dpbp->dpbp_handle,
825 &dflt_dpbp->dpbp_attr);
827 printf("dpbp_get_attributes() failed\n");
831 err = ldpaa_dpbp_seed(dflt_dpbp->dpbp_attr.bpid);
834 printf("Buffer seeding failed for DPBP %d (bpid=%d)\n",
835 dflt_dpbp->dpbp_attr.id, dflt_dpbp->dpbp_attr.bpid);
843 dpbp_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
845 dpbp_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
850 static void ldpaa_dpbp_free(void)
853 dpbp_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
854 dpbp_reset(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
855 dpbp_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
858 static int ldpaa_dpmac_version_check(struct fsl_mc_io *mc_io,
859 struct ldpaa_eth_priv *priv)
862 uint16_t major_ver, minor_ver;
864 error = dpmac_get_api_version(dflt_mc_io, 0,
867 if ((major_ver < DPMAC_VER_MAJOR) ||
868 (major_ver == DPMAC_VER_MAJOR && minor_ver < DPMAC_VER_MINOR)) {
869 printf("DPMAC version mismatch found %u.%u,",
870 major_ver, minor_ver);
871 printf("supported version is %u.%u\n",
872 DPMAC_VER_MAJOR, DPMAC_VER_MINOR);
879 static int ldpaa_dpmac_setup(struct ldpaa_eth_priv *priv)
882 struct dpmac_cfg dpmac_cfg;
884 dpmac_cfg.mac_id = priv->dpmac_id;
886 err = dpmac_create(dflt_mc_io,
888 MC_CMD_NO_FLAGS, &dpmac_cfg,
891 printf("dpmac_create() failed\n");
893 err = ldpaa_dpmac_version_check(dflt_mc_io, priv);
895 printf("ldpaa_dpmac_version_check() failed: %d\n", err);
896 goto err_version_check;
899 err = dpmac_open(dflt_mc_io,
902 &priv->dpmac_handle);
904 printf("dpmac_open() failed: %d\n", err);
912 dpmac_destroy(dflt_mc_io,
914 MC_CMD_NO_FLAGS, priv->dpmac_id);
919 static int ldpaa_dpmac_bind(struct ldpaa_eth_priv *priv)
922 struct dprc_connection_cfg dprc_connection_cfg = {
923 /* If both rates are zero the connection */
924 /* will be configured in "best effort" mode. */
930 struct dprc_endpoint dbg_endpoint;
934 memset(&dpmac_endpoint, 0, sizeof(struct dprc_endpoint));
935 strcpy(dpmac_endpoint.type, "dpmac");
936 dpmac_endpoint.id = priv->dpmac_id;
938 memset(&dpni_endpoint, 0, sizeof(struct dprc_endpoint));
939 strcpy(dpni_endpoint.type, "dpni");
940 dpni_endpoint.id = dflt_dpni->dpni_id;
942 err = dprc_connect(dflt_mc_io, MC_CMD_NO_FLAGS,
946 &dprc_connection_cfg);
948 printf("dprc_connect() failed\n");
951 err = dprc_get_connection(dflt_mc_io, MC_CMD_NO_FLAGS,
952 dflt_dprc_handle, &dpni_endpoint,
953 &dbg_endpoint, &state);
954 printf("%s, DPMAC Type= %s\n", __func__, dbg_endpoint.type);
955 printf("%s, DPMAC ID= %d\n", __func__, dbg_endpoint.id);
956 printf("%s, DPMAC State= %d\n", __func__, state);
958 memset(&dbg_endpoint, 0, sizeof(struct dprc_endpoint));
959 err = dprc_get_connection(dflt_mc_io, MC_CMD_NO_FLAGS,
960 dflt_dprc_handle, &dpmac_endpoint,
961 &dbg_endpoint, &state);
962 printf("%s, DPNI Type= %s\n", __func__, dbg_endpoint.type);
963 printf("%s, DPNI ID= %d\n", __func__, dbg_endpoint.id);
964 printf("%s, DPNI State= %d\n", __func__, state);
969 static int ldpaa_dpni_setup(struct ldpaa_eth_priv *priv)
973 /* and get a handle for the DPNI this interface is associate with */
974 err = dpni_open(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_id,
975 &dflt_dpni->dpni_handle);
977 printf("dpni_open() failed\n");
980 err = dpni_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS,
981 dflt_dpni->dpni_handle,
982 &dflt_dpni->dpni_attrs);
984 printf("dpni_get_attributes() failed (err=%d)\n", err);
988 /* Configure our buffers' layout */
989 dflt_dpni->buf_layout.options = DPNI_BUF_LAYOUT_OPT_PARSER_RESULT |
990 DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
991 DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE |
992 DPNI_BUF_LAYOUT_OPT_DATA_ALIGN;
993 dflt_dpni->buf_layout.pass_parser_result = true;
994 dflt_dpni->buf_layout.pass_frame_status = true;
995 dflt_dpni->buf_layout.private_data_size = LDPAA_ETH_SWA_SIZE;
996 /* HW erratum mandates data alignment in multiples of 256 */
997 dflt_dpni->buf_layout.data_align = LDPAA_ETH_BUF_ALIGN;
1000 err = dpni_set_buffer_layout(dflt_mc_io, MC_CMD_NO_FLAGS,
1001 dflt_dpni->dpni_handle,
1002 &dflt_dpni->buf_layout, DPNI_QUEUE_RX);
1004 printf("dpni_set_buffer_layout() failed");
1005 goto err_buf_layout;
1009 /* remove Rx-only options */
1010 dflt_dpni->buf_layout.options &= ~(DPNI_BUF_LAYOUT_OPT_DATA_ALIGN |
1011 DPNI_BUF_LAYOUT_OPT_PARSER_RESULT);
1012 err = dpni_set_buffer_layout(dflt_mc_io, MC_CMD_NO_FLAGS,
1013 dflt_dpni->dpni_handle,
1014 &dflt_dpni->buf_layout, DPNI_QUEUE_TX);
1016 printf("dpni_set_buffer_layout() failed");
1017 goto err_buf_layout;
1020 /* ... tx-confirm. */
1021 dflt_dpni->buf_layout.options &= ~DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE;
1022 err = dpni_set_buffer_layout(dflt_mc_io, MC_CMD_NO_FLAGS,
1023 dflt_dpni->dpni_handle,
1024 &dflt_dpni->buf_layout,
1025 DPNI_QUEUE_TX_CONFIRM);
1027 printf("dpni_set_buffer_layout() failed");
1028 goto err_buf_layout;
1031 /* Now that we've set our tx buffer layout, retrieve the minimum
1032 * required tx data offset.
1034 err = dpni_get_tx_data_offset(dflt_mc_io, MC_CMD_NO_FLAGS,
1035 dflt_dpni->dpni_handle,
1036 &priv->tx_data_offset);
1038 printf("dpni_get_tx_data_offset() failed\n");
1039 goto err_data_offset;
1042 /* Warn in case TX data offset is not multiple of 64 bytes. */
1043 WARN_ON(priv->tx_data_offset % 64);
1045 /* Accomodate SWA space. */
1046 priv->tx_data_offset += LDPAA_ETH_SWA_SIZE;
1047 debug("priv->tx_data_offset=%d\n", priv->tx_data_offset);
1054 dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
1059 static int ldpaa_dpni_bind(struct ldpaa_eth_priv *priv)
1061 struct dpni_pools_cfg pools_params;
1062 struct dpni_queue tx_queue;
1065 memset(&pools_params, 0, sizeof(pools_params));
1066 pools_params.num_dpbp = 1;
1067 pools_params.pools[0].dpbp_id = (uint16_t)dflt_dpbp->dpbp_attr.id;
1068 pools_params.pools[0].buffer_size = LDPAA_ETH_RX_BUFFER_SIZE;
1069 err = dpni_set_pools(dflt_mc_io, MC_CMD_NO_FLAGS,
1070 dflt_dpni->dpni_handle, &pools_params);
1072 printf("dpni_set_pools() failed\n");
1076 memset(&tx_queue, 0, sizeof(struct dpni_queue));
1078 err = dpni_set_queue(dflt_mc_io, MC_CMD_NO_FLAGS,
1079 dflt_dpni->dpni_handle,
1080 DPNI_QUEUE_TX, 0, 0, &tx_queue);
1083 printf("dpni_set_queue() failed\n");
1087 err = dpni_set_tx_confirmation_mode(dflt_mc_io, MC_CMD_NO_FLAGS,
1088 dflt_dpni->dpni_handle,
1091 printf("dpni_set_tx_confirmation_mode() failed\n");
1098 #ifdef CONFIG_DM_ETH
1099 static int ldpaa_eth_probe(struct udevice *dev)
1101 struct ofnode_phandle_args phandle;
1103 /* Nothing to do if there is no "phy-handle" in the DTS node */
1104 if (dev_read_phandle_with_args(dev, "phy-handle", NULL,
1114 static uint32_t ldpaa_eth_get_dpmac_id(struct udevice *dev)
1116 int port_node = dev_of_offset(dev);
1118 return fdtdec_get_uint(gd->fdt_blob, port_node, "reg", -1);
1121 static const char *ldpaa_eth_get_phy_mode_str(struct udevice *dev)
1123 int port_node = dev_of_offset(dev);
1124 const char *phy_mode_str;
1126 phy_mode_str = fdt_getprop(gd->fdt_blob, port_node,
1127 "phy-connection-type", NULL);
1129 return phy_mode_str;
1131 phy_mode_str = fdt_getprop(gd->fdt_blob, port_node, "phy-mode", NULL);
1132 return phy_mode_str;
1135 static int ldpaa_eth_bind(struct udevice *dev)
1137 const char *phy_mode_str = NULL;
1142 phy_mode_str = ldpaa_eth_get_phy_mode_str(dev);
1144 phy_mode = phy_get_interface_by_name(phy_mode_str);
1145 if (phy_mode == -1) {
1146 dev_err(dev, "incorrect phy mode\n");
1150 dpmac_id = ldpaa_eth_get_dpmac_id(dev);
1151 if (dpmac_id == -1) {
1152 dev_err(dev, "missing reg field from the dpmac node\n");
1156 sprintf(eth_name, "DPMAC%d@%s", dpmac_id, phy_mode_str);
1157 device_set_name(dev, eth_name);
1162 static int ldpaa_eth_ofdata_to_platdata(struct udevice *dev)
1164 struct ldpaa_eth_priv *priv = dev_get_priv(dev);
1165 const char *phy_mode_str;
1167 priv->dpmac_id = ldpaa_eth_get_dpmac_id(dev);
1168 phy_mode_str = ldpaa_eth_get_phy_mode_str(dev);
1169 priv->phy_mode = phy_get_interface_by_name(phy_mode_str);
1174 static const struct eth_ops ldpaa_eth_ops = {
1175 .start = ldpaa_eth_open,
1176 .send = ldpaa_eth_tx,
1177 .recv = ldpaa_eth_pull_dequeue_rx,
1178 .stop = ldpaa_eth_stop,
1181 static const struct udevice_id ldpaa_eth_of_ids[] = {
1182 { .compatible = "fsl,qoriq-mc-dpmac" },
1185 U_BOOT_DRIVER(ldpaa_eth) = {
1186 .name = "ldpaa_eth",
1188 .of_match = ldpaa_eth_of_ids,
1189 .ofdata_to_platdata = ldpaa_eth_ofdata_to_platdata,
1190 .bind = ldpaa_eth_bind,
1191 .probe = ldpaa_eth_probe,
1192 .ops = &ldpaa_eth_ops,
1193 .priv_auto_alloc_size = sizeof(struct ldpaa_eth_priv),
1194 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1199 static int ldpaa_eth_netdev_init(struct eth_device *net_dev,
1200 phy_interface_t enet_if)
1203 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)net_dev->priv;
1205 snprintf(net_dev->name, ETH_NAME_LEN, "DPMAC%d@%s", priv->dpmac_id,
1206 phy_interface_strings[enet_if]);
1208 net_dev->iobase = 0;
1209 net_dev->init = ldpaa_eth_open;
1210 net_dev->halt = ldpaa_eth_stop;
1211 net_dev->send = ldpaa_eth_tx;
1212 net_dev->recv = ldpaa_eth_pull_dequeue_rx;
1214 #ifdef CONFIG_PHYLIB
1215 err = init_phy(net_dev);
1220 err = eth_register(net_dev);
1222 printf("eth_register() = %d\n", err);
1229 int ldpaa_eth_init(int dpmac_id, phy_interface_t enet_if)
1231 struct eth_device *net_dev = NULL;
1232 struct ldpaa_eth_priv *priv = NULL;
1236 net_dev = (struct eth_device *)malloc(sizeof(struct eth_device));
1238 printf("eth_device malloc() failed\n");
1241 memset(net_dev, 0, sizeof(struct eth_device));
1243 /* alloc the ldpaa ethernet private struct */
1244 priv = (struct ldpaa_eth_priv *)malloc(sizeof(struct ldpaa_eth_priv));
1246 printf("ldpaa_eth_priv malloc() failed\n");
1250 memset(priv, 0, sizeof(struct ldpaa_eth_priv));
1252 net_dev->priv = (void *)priv;
1253 priv->net_dev = (struct eth_device *)net_dev;
1254 priv->dpmac_id = dpmac_id;
1255 debug("%s dpmac_id=%d\n", __func__, dpmac_id);
1257 err = ldpaa_eth_netdev_init(net_dev, enet_if);
1259 goto err_netdev_init;
1261 debug("ldpaa ethernet: Probed interface %s\n", net_dev->name);
1266 net_dev->priv = NULL;