1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2014-2016 Freescale Semiconductor, Inc.
9 #include <dm/device_compat.h>
10 #include <fsl-mc/fsl_dpmac.h>
11 #include <fsl-mc/ldpaa_wriop.h>
19 #include <asm/types.h>
20 #include <linux/bug.h>
21 #include <linux/compat.h>
22 #include <linux/delay.h>
23 #include <asm/global_data.h>
24 #include "ldpaa_eth.h"
28 static void init_phy(struct udevice *dev)
30 struct ldpaa_eth_priv *priv = dev_get_priv(dev);
32 priv->phy = dm_eth_phy_connect(dev);
37 phy_config(priv->phy);
40 static int init_phy(struct eth_device *dev)
42 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)dev->priv;
43 struct phy_device *phydev = NULL;
45 int phy_addr, phy_num;
48 bus = wriop_get_mdio(priv->dpmac_id);
52 for (phy_num = 0; phy_num < WRIOP_MAX_PHY_NUM; phy_num++) {
53 phy_addr = wriop_get_phy_address(priv->dpmac_id, phy_num);
57 phydev = phy_connect(bus, phy_addr, dev,
58 wriop_get_enet_if(priv->dpmac_id));
60 printf("Failed to connect\n");
64 wriop_set_phy_dev(priv->dpmac_id, phy_num, phydev);
65 ret = phy_config(phydev);
71 for (phy_num = 0; phy_num < WRIOP_MAX_PHY_NUM; phy_num++) {
72 phydev = wriop_get_phy_dev(priv->dpmac_id, phy_num);
77 wriop_set_phy_dev(priv->dpmac_id, phy_num, NULL);
88 #define DPNI_STATS_PER_PAGE 6
90 static const char *dpni_statistics[][DPNI_STATS_PER_PAGE] = {
92 "DPNI_CNT_ING_ALL_FRAMES",
93 "DPNI_CNT_ING_ALL_BYTES",
94 "DPNI_CNT_ING_MCAST_FRAMES",
95 "DPNI_CNT_ING_MCAST_BYTES",
96 "DPNI_CNT_ING_BCAST_FRAMES",
97 "DPNI_CNT_ING_BCAST_BYTES",
99 "DPNI_CNT_EGR_ALL_FRAMES",
100 "DPNI_CNT_EGR_ALL_BYTES",
101 "DPNI_CNT_EGR_MCAST_FRAMES",
102 "DPNI_CNT_EGR_MCAST_BYTES",
103 "DPNI_CNT_EGR_BCAST_FRAMES",
104 "DPNI_CNT_EGR_BCAST_BYTES",
106 "DPNI_CNT_ING_FILTERED_FRAMES",
107 "DPNI_CNT_ING_DISCARDED_FRAMES",
108 "DPNI_CNT_ING_NOBUFFER_DISCARDS",
109 "DPNI_CNT_EGR_DISCARDED_FRAMES",
110 "DPNI_CNT_EGR_CNF_FRAMES",
115 static void print_dpni_stats(const char *strings[],
116 struct dpni_statistics dpni_stats)
121 stat = (uint64_t *)&dpni_stats;
122 for (i = 0; i < DPNI_STATS_PER_PAGE; i++) {
123 if (strcmp(strings[i], "\0") == 0)
125 printf("%s= %llu\n", strings[i], *stat);
130 static void ldpaa_eth_get_dpni_counter(void)
133 unsigned int page = 0;
134 struct dpni_statistics dpni_stats;
136 printf("DPNI counters ..\n");
137 for (page = 0; page < 3; page++) {
138 err = dpni_get_statistics(dflt_mc_io, MC_CMD_NO_FLAGS,
139 dflt_dpni->dpni_handle, page,
142 printf("dpni_get_statistics: failed:");
143 printf("%d for page[%d]\n", err, page);
146 print_dpni_stats(dpni_statistics[page], dpni_stats);
151 static void ldpaa_eth_get_dpmac_counter(struct udevice *dev)
153 struct ldpaa_eth_priv *priv = dev_get_priv(dev);
155 static void ldpaa_eth_get_dpmac_counter(struct eth_device *net_dev)
157 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)net_dev->priv;
162 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS,
167 printf("dpmac_get_counter: DPMAC_CNT_ING_BYTE failed\n");
170 printf("\nDPMAC counters ..\n");
171 printf("DPMAC_CNT_ING_BYTE=%lld\n", value);
173 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS,
175 DPMAC_CNT_ING_FRAME_DISCARD,
178 printf("dpmac_get_counter: DPMAC_CNT_ING_FRAME_DISCARD failed\n");
181 printf("DPMAC_CNT_ING_FRAME_DISCARD=%lld\n", value);
183 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS,
185 DPMAC_CNT_ING_ALIGN_ERR,
188 printf("dpmac_get_counter: DPMAC_CNT_ING_ALIGN_ERR failed\n");
191 printf("DPMAC_CNT_ING_ALIGN_ERR =%lld\n", value);
193 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS,
198 printf("dpmac_get_counter: DPMAC_CNT_ING_BYTE failed\n");
201 printf("DPMAC_CNT_ING_BYTE=%lld\n", value);
203 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS,
205 DPMAC_CNT_ING_ERR_FRAME,
208 printf("dpmac_get_counter: DPMAC_CNT_ING_ERR_FRAME failed\n");
211 printf("DPMAC_CNT_ING_ERR_FRAME=%lld\n", value);
213 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS,
218 printf("dpmac_get_counter: DPMAC_CNT_EGR_BYTE failed\n");
221 printf("DPMAC_CNT_EGR_BYTE =%lld\n", value);
223 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS,
225 DPMAC_CNT_EGR_ERR_FRAME ,
228 printf("dpmac_get_counter: DPMAC_CNT_EGR_ERR_FRAME failed\n");
231 printf("DPMAC_CNT_EGR_ERR_FRAME =%lld\n", value);
235 static void ldpaa_eth_rx(struct ldpaa_eth_priv *priv,
236 const struct dpaa_fd *fd)
241 struct ldpaa_fas *fas;
242 uint32_t status, err;
243 u32 timeo = (CONFIG_SYS_HZ * 2) / 1000;
245 struct qbman_release_desc releasedesc;
246 struct qbman_swp *swp = dflt_dpio->sw_portal;
248 fd_addr = ldpaa_fd_get_addr(fd);
249 fd_offset = ldpaa_fd_get_offset(fd);
250 fd_length = ldpaa_fd_get_len(fd);
252 debug("Rx frame:data addr=0x%p size=0x%x\n", (u64 *)fd_addr, fd_length);
254 if (fd->simple.frc & LDPAA_FD_FRC_FASV) {
255 /* Read the frame annotation status word and check for errors */
256 fas = (struct ldpaa_fas *)
257 ((uint8_t *)(fd_addr) +
258 dflt_dpni->buf_layout.private_data_size);
259 status = le32_to_cpu(fas->status);
260 if (status & LDPAA_ETH_RX_ERR_MASK) {
261 printf("Rx frame error(s): 0x%08x\n",
262 status & LDPAA_ETH_RX_ERR_MASK);
264 } else if (status & LDPAA_ETH_RX_UNSUPP_MASK) {
265 printf("Unsupported feature in bitmask: 0x%08x\n",
266 status & LDPAA_ETH_RX_UNSUPP_MASK);
271 debug("Rx frame: To Upper layer\n");
272 net_process_received_packet((uint8_t *)(fd_addr) + fd_offset,
276 flush_dcache_range(fd_addr, fd_addr + LDPAA_ETH_RX_BUFFER_SIZE);
277 qbman_release_desc_clear(&releasedesc);
278 qbman_release_desc_set_bpid(&releasedesc, dflt_dpbp->dpbp_attr.bpid);
279 time_start = get_timer(0);
281 /* Release buffer into the QBMAN */
282 err = qbman_swp_release(swp, &releasedesc, &fd_addr, 1);
283 } while (get_timer(time_start) < timeo && err == -EBUSY);
286 printf("Rx frame: QBMAN buffer release fails\n");
292 static int ldpaa_eth_pull_dequeue_rx(struct udevice *dev,
293 int flags, uchar **packetp)
295 struct ldpaa_eth_priv *priv = dev_get_priv(dev);
297 static int ldpaa_eth_pull_dequeue_rx(struct eth_device *dev)
299 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)dev->priv;
301 const struct ldpaa_dq *dq;
302 const struct dpaa_fd *fd;
303 int i = 5, err = 0, status;
304 u32 timeo = (CONFIG_SYS_HZ * 2) / 1000;
306 static struct qbman_pull_desc pulldesc;
307 struct qbman_swp *swp = dflt_dpio->sw_portal;
310 qbman_pull_desc_clear(&pulldesc);
311 qbman_pull_desc_set_numframes(&pulldesc, 1);
312 qbman_pull_desc_set_fq(&pulldesc, priv->rx_dflt_fqid);
314 err = qbman_swp_pull(swp, &pulldesc);
316 printf("Dequeue frames error:0x%08x\n", err);
320 time_start = get_timer(0);
323 dq = qbman_swp_dqrr_next(swp);
324 } while (get_timer(time_start) < timeo && !dq);
327 /* Check for valid frame. If not sent a consume
328 * confirmation to QBMAN otherwise give it to NADK
329 * application and then send consume confirmation to
332 status = (uint8_t)ldpaa_dq_flags(dq);
333 if ((status & LDPAA_DQ_STAT_VALIDFRAME) == 0) {
334 debug("Dequeue RX frames:");
335 debug("No frame delivered\n");
337 qbman_swp_dqrr_consume(swp, dq);
341 fd = ldpaa_dq_fd(dq);
343 /* Obtain FD and process it */
344 ldpaa_eth_rx(priv, fd);
345 qbman_swp_dqrr_consume(swp, dq);
349 debug("No DQRR entries\n");
358 static int ldpaa_eth_tx(struct udevice *dev, void *buf, int len)
360 struct ldpaa_eth_priv *priv = dev_get_priv(dev);
362 static int ldpaa_eth_tx(struct eth_device *net_dev, void *buf, int len)
364 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)net_dev->priv;
368 int data_offset, err;
369 u32 timeo = (CONFIG_SYS_HZ * 10) / 1000;
371 struct qbman_swp *swp = dflt_dpio->sw_portal;
372 struct qbman_eq_desc ed;
373 struct qbman_release_desc releasedesc;
375 /* Setup the FD fields */
376 memset(&fd, 0, sizeof(fd));
378 data_offset = priv->tx_data_offset;
381 err = qbman_swp_acquire(dflt_dpio->sw_portal,
382 dflt_dpbp->dpbp_attr.bpid,
384 } while (err == -EBUSY);
387 printf("qbman_swp_acquire() failed\n");
391 debug("TX data: malloc buffer start=0x%p\n", (u64 *)buffer_start);
393 memcpy(((uint8_t *)(buffer_start) + data_offset), buf, len);
395 flush_dcache_range(buffer_start, buffer_start +
396 LDPAA_ETH_RX_BUFFER_SIZE);
398 ldpaa_fd_set_addr(&fd, (u64)buffer_start);
399 ldpaa_fd_set_offset(&fd, (uint16_t)(data_offset));
400 ldpaa_fd_set_bpid(&fd, dflt_dpbp->dpbp_attr.bpid);
401 ldpaa_fd_set_len(&fd, len);
403 fd.simple.ctrl = LDPAA_FD_CTRL_ASAL | LDPAA_FD_CTRL_PTA |
406 qbman_eq_desc_clear(&ed);
407 qbman_eq_desc_set_no_orp(&ed, 0);
408 qbman_eq_desc_set_qd(&ed, priv->tx_qdid, priv->tx_flow_id, 0);
410 time_start = get_timer(0);
412 while (get_timer(time_start) < timeo) {
413 err = qbman_swp_enqueue(swp, &ed,
414 (const struct qbman_fd *)(&fd));
420 printf("error enqueueing Tx frame\n");
427 qbman_release_desc_clear(&releasedesc);
428 qbman_release_desc_set_bpid(&releasedesc, dflt_dpbp->dpbp_attr.bpid);
429 time_start = get_timer(0);
431 /* Release buffer into the QBMAN */
432 err = qbman_swp_release(swp, &releasedesc, &buffer_start, 1);
433 } while (get_timer(time_start) < timeo && err == -EBUSY);
436 printf("TX data: QBMAN buffer release fails\n");
441 static struct phy_device *ldpaa_get_phydev(struct ldpaa_eth_priv *priv)
447 struct phy_device *phydev = NULL;
450 /* start the phy devices one by one and update the dpmac state */
451 for (phy_num = 0; phy_num < WRIOP_MAX_PHY_NUM; phy_num++) {
452 phydev = wriop_get_phy_dev(priv->dpmac_id, phy_num);
462 static int ldpaa_get_dpmac_state(struct ldpaa_eth_priv *priv,
463 struct dpmac_link_state *state)
465 phy_interface_t enet_if;
466 struct phy_device *phydev = NULL;
469 /* let's start off with maximum capabilities */
470 enet_if = wriop_get_enet_if(priv->dpmac_id);
472 case PHY_INTERFACE_MODE_XGMII:
473 state->rate = SPEED_10000;
476 state->rate = SPEED_1000;
481 state->options |= DPMAC_LINK_OPT_AUTONEG;
482 phydev = ldpaa_get_phydev(priv);
485 err = phy_startup(phydev);
487 printf("%s: Could not initialize\n", phydev->dev->name);
489 } else if (phydev->link) {
490 state->rate = min(state->rate, (uint32_t)phydev->speed);
492 state->options |= DPMAC_LINK_OPT_HALF_DUPLEX;
493 if (!phydev->autoneg)
494 state->options &= ~DPMAC_LINK_OPT_AUTONEG;
501 state->options &= ~DPMAC_LINK_OPT_AUTONEG;
513 static int ldpaa_eth_open(struct udevice *dev)
515 struct eth_pdata *plat = dev_get_plat(dev);
516 struct ldpaa_eth_priv *priv = dev_get_priv(dev);
518 static int ldpaa_eth_open(struct eth_device *net_dev, struct bd_info *bd)
520 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)net_dev->priv;
522 struct dpmac_link_state dpmac_link_state = { 0 };
524 struct dpni_link_state link_state;
527 struct dpni_queue d_queue;
530 if (eth_is_active(dev))
533 if (net_dev->state == ETH_STATE_ACTIVE)
537 if (get_mc_boot_status() != 0) {
538 printf("ERROR (MC is not booted)\n");
542 if (get_dpl_apply_status() == 0) {
543 printf("ERROR (DPL is deployed. No device available)\n");
547 /* DPMAC initialization */
548 err = ldpaa_dpmac_setup(priv);
550 goto err_dpmac_setup;
552 err = ldpaa_get_dpmac_state(priv, &dpmac_link_state);
556 /* DPMAC binding DPNI */
557 err = ldpaa_dpmac_bind(priv);
561 /* DPNI initialization */
562 err = ldpaa_dpni_setup(priv);
566 err = ldpaa_dpbp_setup();
570 /* DPNI binding DPBP */
571 err = ldpaa_dpni_bind(priv);
576 err = dpni_add_mac_addr(dflt_mc_io, MC_CMD_NO_FLAGS,
577 dflt_dpni->dpni_handle, plat->enetaddr);
579 err = dpni_add_mac_addr(dflt_mc_io, MC_CMD_NO_FLAGS,
580 dflt_dpni->dpni_handle, net_dev->enetaddr);
583 printf("dpni_add_mac_addr() failed\n");
587 err = dpni_enable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
589 printf("dpni_enable() failed\n");
593 err = dpmac_set_link_state(dflt_mc_io, MC_CMD_NO_FLAGS,
594 priv->dpmac_handle, &dpmac_link_state);
596 printf("dpmac_set_link_state() failed\n");
601 printf("DPMAC link status: %d - ", dpmac_link_state.up);
602 dpmac_link_state.up == 0 ? printf("down\n") :
603 dpmac_link_state.up == 1 ? printf("up\n") : printf("error state\n");
605 err = dpni_get_link_state(dflt_mc_io, MC_CMD_NO_FLAGS,
606 dflt_dpni->dpni_handle, &link_state);
608 printf("dpni_get_link_state() failed\n");
612 printf("DPNI link status: %d - ", link_state.up);
613 link_state.up == 0 ? printf("down\n") :
614 link_state.up == 1 ? printf("up\n") : printf("error state\n");
617 memset(&d_queue, 0, sizeof(struct dpni_queue));
618 err = dpni_get_queue(dflt_mc_io, MC_CMD_NO_FLAGS,
619 dflt_dpni->dpni_handle, DPNI_QUEUE_RX,
622 printf("dpni_get_queue failed\n");
626 priv->rx_dflt_fqid = d_queue.fqid;
628 err = dpni_get_qdid(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle,
631 printf("dpni_get_qdid() failed\n");
635 return dpmac_link_state.up;
639 dpni_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
643 dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
646 dpmac_close(dflt_mc_io, MC_CMD_NO_FLAGS, priv->dpmac_handle);
647 dpmac_destroy(dflt_mc_io,
649 MC_CMD_NO_FLAGS, priv->dpmac_id);
655 static void ldpaa_eth_stop(struct udevice *dev)
657 struct ldpaa_eth_priv *priv = dev_get_priv(dev);
659 static void ldpaa_eth_stop(struct eth_device *net_dev)
661 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)net_dev->priv;
663 struct phy_device *phydev = NULL;
667 if (!eth_is_active(dev))
670 if ((net_dev->state == ETH_STATE_PASSIVE) ||
671 (net_dev->state == ETH_STATE_INIT))
676 ldpaa_eth_get_dpni_counter();
678 ldpaa_eth_get_dpmac_counter(dev);
680 ldpaa_eth_get_dpmac_counter(net_dev);
684 err = dprc_disconnect(dflt_mc_io, MC_CMD_NO_FLAGS,
685 dflt_dprc_handle, &dpmac_endpoint);
687 printf("dprc_disconnect() failed dpmac_endpoint\n");
689 err = dpmac_close(dflt_mc_io, MC_CMD_NO_FLAGS, priv->dpmac_handle);
691 printf("dpmac_close() failed\n");
693 err = dpmac_destroy(dflt_mc_io,
698 printf("dpmac_destroy() failed\n");
700 /* Stop Tx and Rx traffic */
701 err = dpni_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
703 printf("dpni_disable() failed\n");
705 phydev = ldpaa_get_phydev(priv);
707 phy_shutdown(phydev);
709 /* Free DPBP handle and reset. */
712 dpni_reset(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
714 printf("dpni_reset() failed\n");
716 dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
718 printf("dpni_close() failed\n");
721 static void ldpaa_dpbp_drain_cnt(int count)
723 uint64_t buf_array[7];
730 ret = qbman_swp_acquire(dflt_dpio->sw_portal,
731 dflt_dpbp->dpbp_attr.bpid,
734 printf("qbman_swp_acquire() failed\n");
737 for (i = 0; i < ret; i++) {
738 addr = (void *)buf_array[i];
739 debug("Free: buffer addr =0x%p\n", addr);
745 static void ldpaa_dpbp_drain(void)
748 for (i = 0; i < LDPAA_ETH_NUM_BUFS; i += 7)
749 ldpaa_dpbp_drain_cnt(7);
752 static int ldpaa_bp_add_7(uint16_t bpid)
754 uint64_t buf_array[7];
757 struct qbman_release_desc rd;
759 for (i = 0; i < 7; i++) {
760 addr = memalign(LDPAA_ETH_BUF_ALIGN, LDPAA_ETH_RX_BUFFER_SIZE);
762 printf("addr allocation failed\n");
765 memset(addr, 0x00, LDPAA_ETH_RX_BUFFER_SIZE);
766 flush_dcache_range((u64)addr,
767 (u64)(addr + LDPAA_ETH_RX_BUFFER_SIZE));
769 buf_array[i] = (uint64_t)addr;
770 debug("Release: buffer addr =0x%p\n", addr);
774 /* In case the portal is busy, retry until successful.
775 * This function is guaranteed to succeed in a reasonable amount
781 qbman_release_desc_clear(&rd);
782 qbman_release_desc_set_bpid(&rd, bpid);
783 } while (qbman_swp_release(dflt_dpio->sw_portal, &rd, buf_array, i));
794 static int ldpaa_dpbp_seed(uint16_t bpid)
799 for (i = 0; i < LDPAA_ETH_NUM_BUFS; i += 7) {
800 count = ldpaa_bp_add_7(bpid);
802 printf("Buffer Seed= %d\n", count);
808 static int ldpaa_dpbp_setup(void)
812 err = dpbp_open(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_attr.id,
813 &dflt_dpbp->dpbp_handle);
815 printf("dpbp_open() failed\n");
819 err = dpbp_enable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
821 printf("dpbp_enable() failed\n");
825 err = dpbp_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS,
826 dflt_dpbp->dpbp_handle,
827 &dflt_dpbp->dpbp_attr);
829 printf("dpbp_get_attributes() failed\n");
833 err = ldpaa_dpbp_seed(dflt_dpbp->dpbp_attr.bpid);
836 printf("Buffer seeding failed for DPBP %d (bpid=%d)\n",
837 dflt_dpbp->dpbp_attr.id, dflt_dpbp->dpbp_attr.bpid);
845 dpbp_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
847 dpbp_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
852 static void ldpaa_dpbp_free(void)
855 dpbp_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
856 dpbp_reset(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
857 dpbp_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
860 static int ldpaa_dpmac_version_check(struct fsl_mc_io *mc_io,
861 struct ldpaa_eth_priv *priv)
864 uint16_t major_ver, minor_ver;
866 error = dpmac_get_api_version(dflt_mc_io, 0,
869 if ((major_ver < DPMAC_VER_MAJOR) ||
870 (major_ver == DPMAC_VER_MAJOR && minor_ver < DPMAC_VER_MINOR)) {
871 printf("DPMAC version mismatch found %u.%u,",
872 major_ver, minor_ver);
873 printf("supported version is %u.%u\n",
874 DPMAC_VER_MAJOR, DPMAC_VER_MINOR);
881 static int ldpaa_dpmac_setup(struct ldpaa_eth_priv *priv)
884 struct dpmac_cfg dpmac_cfg;
886 dpmac_cfg.mac_id = priv->dpmac_id;
888 err = dpmac_create(dflt_mc_io,
890 MC_CMD_NO_FLAGS, &dpmac_cfg,
893 printf("dpmac_create() failed\n");
895 err = ldpaa_dpmac_version_check(dflt_mc_io, priv);
897 printf("ldpaa_dpmac_version_check() failed: %d\n", err);
898 goto err_version_check;
901 err = dpmac_open(dflt_mc_io,
904 &priv->dpmac_handle);
906 printf("dpmac_open() failed: %d\n", err);
914 dpmac_destroy(dflt_mc_io,
916 MC_CMD_NO_FLAGS, priv->dpmac_id);
921 static int ldpaa_dpmac_bind(struct ldpaa_eth_priv *priv)
924 struct dprc_connection_cfg dprc_connection_cfg = {
925 /* If both rates are zero the connection */
926 /* will be configured in "best effort" mode. */
932 struct dprc_endpoint dbg_endpoint;
936 memset(&dpmac_endpoint, 0, sizeof(struct dprc_endpoint));
937 strcpy(dpmac_endpoint.type, "dpmac");
938 dpmac_endpoint.id = priv->dpmac_id;
940 memset(&dpni_endpoint, 0, sizeof(struct dprc_endpoint));
941 strcpy(dpni_endpoint.type, "dpni");
942 dpni_endpoint.id = dflt_dpni->dpni_id;
944 err = dprc_connect(dflt_mc_io, MC_CMD_NO_FLAGS,
948 &dprc_connection_cfg);
950 printf("dprc_connect() failed\n");
953 err = dprc_get_connection(dflt_mc_io, MC_CMD_NO_FLAGS,
954 dflt_dprc_handle, &dpni_endpoint,
955 &dbg_endpoint, &state);
956 printf("%s, DPMAC Type= %s\n", __func__, dbg_endpoint.type);
957 printf("%s, DPMAC ID= %d\n", __func__, dbg_endpoint.id);
958 printf("%s, DPMAC State= %d\n", __func__, state);
960 memset(&dbg_endpoint, 0, sizeof(struct dprc_endpoint));
961 err = dprc_get_connection(dflt_mc_io, MC_CMD_NO_FLAGS,
962 dflt_dprc_handle, &dpmac_endpoint,
963 &dbg_endpoint, &state);
964 printf("%s, DPNI Type= %s\n", __func__, dbg_endpoint.type);
965 printf("%s, DPNI ID= %d\n", __func__, dbg_endpoint.id);
966 printf("%s, DPNI State= %d\n", __func__, state);
971 static int ldpaa_dpni_setup(struct ldpaa_eth_priv *priv)
975 /* and get a handle for the DPNI this interface is associate with */
976 err = dpni_open(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_id,
977 &dflt_dpni->dpni_handle);
979 printf("dpni_open() failed\n");
982 err = dpni_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS,
983 dflt_dpni->dpni_handle,
984 &dflt_dpni->dpni_attrs);
986 printf("dpni_get_attributes() failed (err=%d)\n", err);
990 /* Configure our buffers' layout */
991 dflt_dpni->buf_layout.options = DPNI_BUF_LAYOUT_OPT_PARSER_RESULT |
992 DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
993 DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE |
994 DPNI_BUF_LAYOUT_OPT_DATA_ALIGN;
995 dflt_dpni->buf_layout.pass_parser_result = true;
996 dflt_dpni->buf_layout.pass_frame_status = true;
997 dflt_dpni->buf_layout.private_data_size = LDPAA_ETH_SWA_SIZE;
998 /* HW erratum mandates data alignment in multiples of 256 */
999 dflt_dpni->buf_layout.data_align = LDPAA_ETH_BUF_ALIGN;
1002 err = dpni_set_buffer_layout(dflt_mc_io, MC_CMD_NO_FLAGS,
1003 dflt_dpni->dpni_handle,
1004 &dflt_dpni->buf_layout, DPNI_QUEUE_RX);
1006 printf("dpni_set_buffer_layout() failed");
1007 goto err_buf_layout;
1011 /* remove Rx-only options */
1012 dflt_dpni->buf_layout.options &= ~(DPNI_BUF_LAYOUT_OPT_DATA_ALIGN |
1013 DPNI_BUF_LAYOUT_OPT_PARSER_RESULT);
1014 err = dpni_set_buffer_layout(dflt_mc_io, MC_CMD_NO_FLAGS,
1015 dflt_dpni->dpni_handle,
1016 &dflt_dpni->buf_layout, DPNI_QUEUE_TX);
1018 printf("dpni_set_buffer_layout() failed");
1019 goto err_buf_layout;
1022 /* ... tx-confirm. */
1023 dflt_dpni->buf_layout.options &= ~DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE;
1024 err = dpni_set_buffer_layout(dflt_mc_io, MC_CMD_NO_FLAGS,
1025 dflt_dpni->dpni_handle,
1026 &dflt_dpni->buf_layout,
1027 DPNI_QUEUE_TX_CONFIRM);
1029 printf("dpni_set_buffer_layout() failed");
1030 goto err_buf_layout;
1033 /* Now that we've set our tx buffer layout, retrieve the minimum
1034 * required tx data offset.
1036 err = dpni_get_tx_data_offset(dflt_mc_io, MC_CMD_NO_FLAGS,
1037 dflt_dpni->dpni_handle,
1038 &priv->tx_data_offset);
1040 printf("dpni_get_tx_data_offset() failed\n");
1041 goto err_data_offset;
1044 /* Warn in case TX data offset is not multiple of 64 bytes. */
1045 WARN_ON(priv->tx_data_offset % 64);
1047 /* Accomodate SWA space. */
1048 priv->tx_data_offset += LDPAA_ETH_SWA_SIZE;
1049 debug("priv->tx_data_offset=%d\n", priv->tx_data_offset);
1056 dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
1061 static int ldpaa_dpni_bind(struct ldpaa_eth_priv *priv)
1063 struct dpni_pools_cfg pools_params;
1064 struct dpni_queue tx_queue;
1067 memset(&pools_params, 0, sizeof(pools_params));
1068 pools_params.num_dpbp = 1;
1069 pools_params.pools[0].dpbp_id = (uint16_t)dflt_dpbp->dpbp_attr.id;
1070 pools_params.pools[0].buffer_size = LDPAA_ETH_RX_BUFFER_SIZE;
1071 err = dpni_set_pools(dflt_mc_io, MC_CMD_NO_FLAGS,
1072 dflt_dpni->dpni_handle, &pools_params);
1074 printf("dpni_set_pools() failed\n");
1078 memset(&tx_queue, 0, sizeof(struct dpni_queue));
1080 err = dpni_set_queue(dflt_mc_io, MC_CMD_NO_FLAGS,
1081 dflt_dpni->dpni_handle,
1082 DPNI_QUEUE_TX, 0, 0, &tx_queue);
1085 printf("dpni_set_queue() failed\n");
1089 err = dpni_set_tx_confirmation_mode(dflt_mc_io, MC_CMD_NO_FLAGS,
1090 dflt_dpni->dpni_handle,
1093 printf("dpni_set_tx_confirmation_mode() failed\n");
1100 #ifdef CONFIG_DM_ETH
1101 static int ldpaa_eth_probe(struct udevice *dev)
1103 struct ofnode_phandle_args phandle;
1105 /* Nothing to do if there is no "phy-handle" in the DTS node */
1106 if (dev_read_phandle_with_args(dev, "phy-handle", NULL,
1116 static uint32_t ldpaa_eth_get_dpmac_id(struct udevice *dev)
1118 int port_node = dev_of_offset(dev);
1120 return fdtdec_get_uint(gd->fdt_blob, port_node, "reg", -1);
1123 static int ldpaa_eth_bind(struct udevice *dev)
1129 phy_mode = dev_read_phy_mode(dev);
1130 if (phy_mode == PHY_INTERFACE_MODE_NONE) {
1131 dev_err(dev, "incorrect phy mode\n");
1135 dpmac_id = ldpaa_eth_get_dpmac_id(dev);
1136 if (dpmac_id == -1) {
1137 dev_err(dev, "missing reg field from the dpmac node\n");
1141 sprintf(eth_name, "DPMAC%d@%s", dpmac_id,
1142 phy_string_for_interface(phy_mode));
1143 device_set_name(dev, eth_name);
1148 static int ldpaa_eth_of_to_plat(struct udevice *dev)
1150 struct ldpaa_eth_priv *priv = dev_get_priv(dev);
1152 priv->dpmac_id = ldpaa_eth_get_dpmac_id(dev);
1153 priv->phy_mode = dev_read_phy_mode(dev);
1158 static const struct eth_ops ldpaa_eth_ops = {
1159 .start = ldpaa_eth_open,
1160 .send = ldpaa_eth_tx,
1161 .recv = ldpaa_eth_pull_dequeue_rx,
1162 .stop = ldpaa_eth_stop,
1165 static const struct udevice_id ldpaa_eth_of_ids[] = {
1166 { .compatible = "fsl,qoriq-mc-dpmac" },
1169 U_BOOT_DRIVER(ldpaa_eth) = {
1170 .name = "ldpaa_eth",
1172 .of_match = ldpaa_eth_of_ids,
1173 .of_to_plat = ldpaa_eth_of_to_plat,
1174 .bind = ldpaa_eth_bind,
1175 .probe = ldpaa_eth_probe,
1176 .ops = &ldpaa_eth_ops,
1177 .priv_auto = sizeof(struct ldpaa_eth_priv),
1178 .plat_auto = sizeof(struct eth_pdata),
1183 static int ldpaa_eth_netdev_init(struct eth_device *net_dev,
1184 phy_interface_t enet_if)
1187 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)net_dev->priv;
1189 snprintf(net_dev->name, ETH_NAME_LEN, "DPMAC%d@%s", priv->dpmac_id,
1190 phy_interface_strings[enet_if]);
1192 net_dev->iobase = 0;
1193 net_dev->init = ldpaa_eth_open;
1194 net_dev->halt = ldpaa_eth_stop;
1195 net_dev->send = ldpaa_eth_tx;
1196 net_dev->recv = ldpaa_eth_pull_dequeue_rx;
1198 #ifdef CONFIG_PHYLIB
1199 err = init_phy(net_dev);
1204 err = eth_register(net_dev);
1206 printf("eth_register() = %d\n", err);
1213 int ldpaa_eth_init(int dpmac_id, phy_interface_t enet_if)
1215 struct eth_device *net_dev = NULL;
1216 struct ldpaa_eth_priv *priv = NULL;
1220 net_dev = (struct eth_device *)malloc(sizeof(struct eth_device));
1222 printf("eth_device malloc() failed\n");
1225 memset(net_dev, 0, sizeof(struct eth_device));
1227 /* alloc the ldpaa ethernet private struct */
1228 priv = (struct ldpaa_eth_priv *)malloc(sizeof(struct ldpaa_eth_priv));
1230 printf("ldpaa_eth_priv malloc() failed\n");
1234 memset(priv, 0, sizeof(struct ldpaa_eth_priv));
1236 net_dev->priv = (void *)priv;
1237 priv->net_dev = (struct eth_device *)net_dev;
1238 priv->dpmac_id = dpmac_id;
1239 debug("%s dpmac_id=%d\n", __func__, dpmac_id);
1241 err = ldpaa_eth_netdev_init(net_dev, enet_if);
1243 goto err_netdev_init;
1245 debug("ldpaa ethernet: Probed interface %s\n", net_dev->name);
1250 net_dev->priv = NULL;