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>
20 #include <linux/delay.h>
22 #include <fsl-mc/ldpaa_wriop.h>
23 #include "ldpaa_eth.h"
27 static void init_phy(struct udevice *dev)
29 struct ldpaa_eth_priv *priv = dev_get_priv(dev);
31 priv->phy = dm_eth_phy_connect(dev);
36 phy_config(priv->phy);
39 static int init_phy(struct eth_device *dev)
41 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)dev->priv;
42 struct phy_device *phydev = NULL;
44 int phy_addr, phy_num;
47 bus = wriop_get_mdio(priv->dpmac_id);
51 for (phy_num = 0; phy_num < WRIOP_MAX_PHY_NUM; phy_num++) {
52 phy_addr = wriop_get_phy_address(priv->dpmac_id, phy_num);
56 phydev = phy_connect(bus, phy_addr, dev,
57 wriop_get_enet_if(priv->dpmac_id));
59 printf("Failed to connect\n");
63 wriop_set_phy_dev(priv->dpmac_id, phy_num, phydev);
64 ret = phy_config(phydev);
70 for (phy_num = 0; phy_num < WRIOP_MAX_PHY_NUM; phy_num++) {
71 phydev = wriop_get_phy_dev(priv->dpmac_id, phy_num);
76 wriop_set_phy_dev(priv->dpmac_id, phy_num, NULL);
87 #define DPNI_STATS_PER_PAGE 6
89 static const char *dpni_statistics[][DPNI_STATS_PER_PAGE] = {
91 "DPNI_CNT_ING_ALL_FRAMES",
92 "DPNI_CNT_ING_ALL_BYTES",
93 "DPNI_CNT_ING_MCAST_FRAMES",
94 "DPNI_CNT_ING_MCAST_BYTES",
95 "DPNI_CNT_ING_BCAST_FRAMES",
96 "DPNI_CNT_ING_BCAST_BYTES",
98 "DPNI_CNT_EGR_ALL_FRAMES",
99 "DPNI_CNT_EGR_ALL_BYTES",
100 "DPNI_CNT_EGR_MCAST_FRAMES",
101 "DPNI_CNT_EGR_MCAST_BYTES",
102 "DPNI_CNT_EGR_BCAST_FRAMES",
103 "DPNI_CNT_EGR_BCAST_BYTES",
105 "DPNI_CNT_ING_FILTERED_FRAMES",
106 "DPNI_CNT_ING_DISCARDED_FRAMES",
107 "DPNI_CNT_ING_NOBUFFER_DISCARDS",
108 "DPNI_CNT_EGR_DISCARDED_FRAMES",
109 "DPNI_CNT_EGR_CNF_FRAMES",
114 static void print_dpni_stats(const char *strings[],
115 struct dpni_statistics dpni_stats)
120 stat = (uint64_t *)&dpni_stats;
121 for (i = 0; i < DPNI_STATS_PER_PAGE; i++) {
122 if (strcmp(strings[i], "\0") == 0)
124 printf("%s= %llu\n", strings[i], *stat);
129 static void ldpaa_eth_get_dpni_counter(void)
132 unsigned int page = 0;
133 struct dpni_statistics dpni_stats;
135 printf("DPNI counters ..\n");
136 for (page = 0; page < 3; page++) {
137 err = dpni_get_statistics(dflt_mc_io, MC_CMD_NO_FLAGS,
138 dflt_dpni->dpni_handle, page,
141 printf("dpni_get_statistics: failed:");
142 printf("%d for page[%d]\n", err, page);
145 print_dpni_stats(dpni_statistics[page], dpni_stats);
150 static void ldpaa_eth_get_dpmac_counter(struct udevice *dev)
152 struct ldpaa_eth_priv *priv = dev_get_priv(dev);
154 static void ldpaa_eth_get_dpmac_counter(struct eth_device *net_dev)
156 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)net_dev->priv;
161 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS,
166 printf("dpmac_get_counter: DPMAC_CNT_ING_BYTE failed\n");
169 printf("\nDPMAC counters ..\n");
170 printf("DPMAC_CNT_ING_BYTE=%lld\n", value);
172 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS,
174 DPMAC_CNT_ING_FRAME_DISCARD,
177 printf("dpmac_get_counter: DPMAC_CNT_ING_FRAME_DISCARD failed\n");
180 printf("DPMAC_CNT_ING_FRAME_DISCARD=%lld\n", value);
182 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS,
184 DPMAC_CNT_ING_ALIGN_ERR,
187 printf("dpmac_get_counter: DPMAC_CNT_ING_ALIGN_ERR failed\n");
190 printf("DPMAC_CNT_ING_ALIGN_ERR =%lld\n", value);
192 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS,
197 printf("dpmac_get_counter: DPMAC_CNT_ING_BYTE failed\n");
200 printf("DPMAC_CNT_ING_BYTE=%lld\n", value);
202 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS,
204 DPMAC_CNT_ING_ERR_FRAME,
207 printf("dpmac_get_counter: DPMAC_CNT_ING_ERR_FRAME failed\n");
210 printf("DPMAC_CNT_ING_ERR_FRAME=%lld\n", value);
212 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS,
217 printf("dpmac_get_counter: DPMAC_CNT_EGR_BYTE failed\n");
220 printf("DPMAC_CNT_EGR_BYTE =%lld\n", value);
222 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS,
224 DPMAC_CNT_EGR_ERR_FRAME ,
227 printf("dpmac_get_counter: DPMAC_CNT_EGR_ERR_FRAME failed\n");
230 printf("DPMAC_CNT_EGR_ERR_FRAME =%lld\n", value);
234 static void ldpaa_eth_rx(struct ldpaa_eth_priv *priv,
235 const struct dpaa_fd *fd)
240 struct ldpaa_fas *fas;
241 uint32_t status, err;
242 u32 timeo = (CONFIG_SYS_HZ * 2) / 1000;
244 struct qbman_release_desc releasedesc;
245 struct qbman_swp *swp = dflt_dpio->sw_portal;
247 fd_addr = ldpaa_fd_get_addr(fd);
248 fd_offset = ldpaa_fd_get_offset(fd);
249 fd_length = ldpaa_fd_get_len(fd);
251 debug("Rx frame:data addr=0x%p size=0x%x\n", (u64 *)fd_addr, fd_length);
253 if (fd->simple.frc & LDPAA_FD_FRC_FASV) {
254 /* Read the frame annotation status word and check for errors */
255 fas = (struct ldpaa_fas *)
256 ((uint8_t *)(fd_addr) +
257 dflt_dpni->buf_layout.private_data_size);
258 status = le32_to_cpu(fas->status);
259 if (status & LDPAA_ETH_RX_ERR_MASK) {
260 printf("Rx frame error(s): 0x%08x\n",
261 status & LDPAA_ETH_RX_ERR_MASK);
263 } else if (status & LDPAA_ETH_RX_UNSUPP_MASK) {
264 printf("Unsupported feature in bitmask: 0x%08x\n",
265 status & LDPAA_ETH_RX_UNSUPP_MASK);
270 debug("Rx frame: To Upper layer\n");
271 net_process_received_packet((uint8_t *)(fd_addr) + fd_offset,
275 flush_dcache_range(fd_addr, fd_addr + LDPAA_ETH_RX_BUFFER_SIZE);
276 qbman_release_desc_clear(&releasedesc);
277 qbman_release_desc_set_bpid(&releasedesc, dflt_dpbp->dpbp_attr.bpid);
278 time_start = get_timer(0);
280 /* Release buffer into the QBMAN */
281 err = qbman_swp_release(swp, &releasedesc, &fd_addr, 1);
282 } while (get_timer(time_start) < timeo && err == -EBUSY);
285 printf("Rx frame: QBMAN buffer release fails\n");
291 static int ldpaa_eth_pull_dequeue_rx(struct udevice *dev,
292 int flags, uchar **packetp)
294 struct ldpaa_eth_priv *priv = dev_get_priv(dev);
296 static int ldpaa_eth_pull_dequeue_rx(struct eth_device *dev)
298 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)dev->priv;
300 const struct ldpaa_dq *dq;
301 const struct dpaa_fd *fd;
302 int i = 5, err = 0, status;
303 u32 timeo = (CONFIG_SYS_HZ * 2) / 1000;
305 static struct qbman_pull_desc pulldesc;
306 struct qbman_swp *swp = dflt_dpio->sw_portal;
309 qbman_pull_desc_clear(&pulldesc);
310 qbman_pull_desc_set_numframes(&pulldesc, 1);
311 qbman_pull_desc_set_fq(&pulldesc, priv->rx_dflt_fqid);
313 err = qbman_swp_pull(swp, &pulldesc);
315 printf("Dequeue frames error:0x%08x\n", err);
319 time_start = get_timer(0);
322 dq = qbman_swp_dqrr_next(swp);
323 } while (get_timer(time_start) < timeo && !dq);
326 /* Check for valid frame. If not sent a consume
327 * confirmation to QBMAN otherwise give it to NADK
328 * application and then send consume confirmation to
331 status = (uint8_t)ldpaa_dq_flags(dq);
332 if ((status & LDPAA_DQ_STAT_VALIDFRAME) == 0) {
333 debug("Dequeue RX frames:");
334 debug("No frame delivered\n");
336 qbman_swp_dqrr_consume(swp, dq);
340 fd = ldpaa_dq_fd(dq);
342 /* Obtain FD and process it */
343 ldpaa_eth_rx(priv, fd);
344 qbman_swp_dqrr_consume(swp, dq);
348 debug("No DQRR entries\n");
357 static int ldpaa_eth_tx(struct udevice *dev, void *buf, int len)
359 struct ldpaa_eth_priv *priv = dev_get_priv(dev);
361 static int ldpaa_eth_tx(struct eth_device *net_dev, void *buf, int len)
363 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)net_dev->priv;
367 int data_offset, err;
368 u32 timeo = (CONFIG_SYS_HZ * 10) / 1000;
370 struct qbman_swp *swp = dflt_dpio->sw_portal;
371 struct qbman_eq_desc ed;
372 struct qbman_release_desc releasedesc;
374 /* Setup the FD fields */
375 memset(&fd, 0, sizeof(fd));
377 data_offset = priv->tx_data_offset;
380 err = qbman_swp_acquire(dflt_dpio->sw_portal,
381 dflt_dpbp->dpbp_attr.bpid,
383 } while (err == -EBUSY);
386 printf("qbman_swp_acquire() failed\n");
390 debug("TX data: malloc buffer start=0x%p\n", (u64 *)buffer_start);
392 memcpy(((uint8_t *)(buffer_start) + data_offset), buf, len);
394 flush_dcache_range(buffer_start, buffer_start +
395 LDPAA_ETH_RX_BUFFER_SIZE);
397 ldpaa_fd_set_addr(&fd, (u64)buffer_start);
398 ldpaa_fd_set_offset(&fd, (uint16_t)(data_offset));
399 ldpaa_fd_set_bpid(&fd, dflt_dpbp->dpbp_attr.bpid);
400 ldpaa_fd_set_len(&fd, len);
402 fd.simple.ctrl = LDPAA_FD_CTRL_ASAL | LDPAA_FD_CTRL_PTA |
405 qbman_eq_desc_clear(&ed);
406 qbman_eq_desc_set_no_orp(&ed, 0);
407 qbman_eq_desc_set_qd(&ed, priv->tx_qdid, priv->tx_flow_id, 0);
409 time_start = get_timer(0);
411 while (get_timer(time_start) < timeo) {
412 err = qbman_swp_enqueue(swp, &ed,
413 (const struct qbman_fd *)(&fd));
419 printf("error enqueueing Tx frame\n");
426 qbman_release_desc_clear(&releasedesc);
427 qbman_release_desc_set_bpid(&releasedesc, dflt_dpbp->dpbp_attr.bpid);
428 time_start = get_timer(0);
430 /* Release buffer into the QBMAN */
431 err = qbman_swp_release(swp, &releasedesc, &buffer_start, 1);
432 } while (get_timer(time_start) < timeo && err == -EBUSY);
435 printf("TX data: QBMAN buffer release fails\n");
440 static struct phy_device *ldpaa_get_phydev(struct ldpaa_eth_priv *priv)
446 struct phy_device *phydev = NULL;
449 /* start the phy devices one by one and update the dpmac state */
450 for (phy_num = 0; phy_num < WRIOP_MAX_PHY_NUM; phy_num++) {
451 phydev = wriop_get_phy_dev(priv->dpmac_id, phy_num);
461 static int ldpaa_get_dpmac_state(struct ldpaa_eth_priv *priv,
462 struct dpmac_link_state *state)
464 phy_interface_t enet_if;
465 struct phy_device *phydev = NULL;
468 /* let's start off with maximum capabilities */
469 enet_if = wriop_get_enet_if(priv->dpmac_id);
471 case PHY_INTERFACE_MODE_XGMII:
472 state->rate = SPEED_10000;
475 state->rate = SPEED_1000;
480 state->options |= DPMAC_LINK_OPT_AUTONEG;
481 phydev = ldpaa_get_phydev(priv);
484 err = phy_startup(phydev);
486 printf("%s: Could not initialize\n", phydev->dev->name);
488 } else if (phydev->link) {
489 state->rate = min(state->rate, (uint32_t)phydev->speed);
491 state->options |= DPMAC_LINK_OPT_HALF_DUPLEX;
492 if (!phydev->autoneg)
493 state->options &= ~DPMAC_LINK_OPT_AUTONEG;
500 state->options &= ~DPMAC_LINK_OPT_AUTONEG;
512 static int ldpaa_eth_open(struct udevice *dev)
514 struct eth_pdata *plat = dev_get_platdata(dev);
515 struct ldpaa_eth_priv *priv = dev_get_priv(dev);
517 static int ldpaa_eth_open(struct eth_device *net_dev, struct bd_info *bd)
519 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)net_dev->priv;
521 struct dpmac_link_state dpmac_link_state = { 0 };
523 struct dpni_link_state link_state;
526 struct dpni_queue d_queue;
529 if (eth_is_active(dev))
532 if (net_dev->state == ETH_STATE_ACTIVE)
536 if (get_mc_boot_status() != 0) {
537 printf("ERROR (MC is not booted)\n");
541 if (get_dpl_apply_status() == 0) {
542 printf("ERROR (DPL is deployed. No device available)\n");
546 /* DPMAC initialization */
547 err = ldpaa_dpmac_setup(priv);
549 goto err_dpmac_setup;
551 err = ldpaa_get_dpmac_state(priv, &dpmac_link_state);
555 /* DPMAC binding DPNI */
556 err = ldpaa_dpmac_bind(priv);
560 /* DPNI initialization */
561 err = ldpaa_dpni_setup(priv);
565 err = ldpaa_dpbp_setup();
569 /* DPNI binding DPBP */
570 err = ldpaa_dpni_bind(priv);
575 err = dpni_add_mac_addr(dflt_mc_io, MC_CMD_NO_FLAGS,
576 dflt_dpni->dpni_handle, plat->enetaddr);
578 err = dpni_add_mac_addr(dflt_mc_io, MC_CMD_NO_FLAGS,
579 dflt_dpni->dpni_handle, net_dev->enetaddr);
582 printf("dpni_add_mac_addr() failed\n");
586 err = dpni_enable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
588 printf("dpni_enable() failed\n");
592 err = dpmac_set_link_state(dflt_mc_io, MC_CMD_NO_FLAGS,
593 priv->dpmac_handle, &dpmac_link_state);
595 printf("dpmac_set_link_state() failed\n");
600 printf("DPMAC link status: %d - ", dpmac_link_state.up);
601 dpmac_link_state.up == 0 ? printf("down\n") :
602 dpmac_link_state.up == 1 ? printf("up\n") : printf("error state\n");
604 err = dpni_get_link_state(dflt_mc_io, MC_CMD_NO_FLAGS,
605 dflt_dpni->dpni_handle, &link_state);
607 printf("dpni_get_link_state() failed\n");
611 printf("DPNI link status: %d - ", link_state.up);
612 link_state.up == 0 ? printf("down\n") :
613 link_state.up == 1 ? printf("up\n") : printf("error state\n");
616 memset(&d_queue, 0, sizeof(struct dpni_queue));
617 err = dpni_get_queue(dflt_mc_io, MC_CMD_NO_FLAGS,
618 dflt_dpni->dpni_handle, DPNI_QUEUE_RX,
621 printf("dpni_get_queue failed\n");
625 priv->rx_dflt_fqid = d_queue.fqid;
627 err = dpni_get_qdid(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle,
630 printf("dpni_get_qdid() failed\n");
634 return dpmac_link_state.up;
638 dpni_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
642 dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
645 dpmac_close(dflt_mc_io, MC_CMD_NO_FLAGS, priv->dpmac_handle);
646 dpmac_destroy(dflt_mc_io,
648 MC_CMD_NO_FLAGS, priv->dpmac_id);
654 static void ldpaa_eth_stop(struct udevice *dev)
656 struct ldpaa_eth_priv *priv = dev_get_priv(dev);
658 static void ldpaa_eth_stop(struct eth_device *net_dev)
660 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)net_dev->priv;
662 struct phy_device *phydev = NULL;
666 if (!eth_is_active(dev))
669 if ((net_dev->state == ETH_STATE_PASSIVE) ||
670 (net_dev->state == ETH_STATE_INIT))
675 ldpaa_eth_get_dpni_counter();
677 ldpaa_eth_get_dpmac_counter(dev);
679 ldpaa_eth_get_dpmac_counter(net_dev);
683 err = dprc_disconnect(dflt_mc_io, MC_CMD_NO_FLAGS,
684 dflt_dprc_handle, &dpmac_endpoint);
686 printf("dprc_disconnect() failed dpmac_endpoint\n");
688 err = dpmac_close(dflt_mc_io, MC_CMD_NO_FLAGS, priv->dpmac_handle);
690 printf("dpmac_close() failed\n");
692 err = dpmac_destroy(dflt_mc_io,
697 printf("dpmac_destroy() failed\n");
699 /* Stop Tx and Rx traffic */
700 err = dpni_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
702 printf("dpni_disable() failed\n");
704 phydev = ldpaa_get_phydev(priv);
706 phy_shutdown(phydev);
708 /* Free DPBP handle and reset. */
711 dpni_reset(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
713 printf("dpni_reset() failed\n");
715 dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
717 printf("dpni_close() failed\n");
720 static void ldpaa_dpbp_drain_cnt(int count)
722 uint64_t buf_array[7];
729 ret = qbman_swp_acquire(dflt_dpio->sw_portal,
730 dflt_dpbp->dpbp_attr.bpid,
733 printf("qbman_swp_acquire() failed\n");
736 for (i = 0; i < ret; i++) {
737 addr = (void *)buf_array[i];
738 debug("Free: buffer addr =0x%p\n", addr);
744 static void ldpaa_dpbp_drain(void)
747 for (i = 0; i < LDPAA_ETH_NUM_BUFS; i += 7)
748 ldpaa_dpbp_drain_cnt(7);
751 static int ldpaa_bp_add_7(uint16_t bpid)
753 uint64_t buf_array[7];
756 struct qbman_release_desc rd;
758 for (i = 0; i < 7; i++) {
759 addr = memalign(LDPAA_ETH_BUF_ALIGN, LDPAA_ETH_RX_BUFFER_SIZE);
761 printf("addr allocation failed\n");
764 memset(addr, 0x00, LDPAA_ETH_RX_BUFFER_SIZE);
765 flush_dcache_range((u64)addr,
766 (u64)(addr + LDPAA_ETH_RX_BUFFER_SIZE));
768 buf_array[i] = (uint64_t)addr;
769 debug("Release: buffer addr =0x%p\n", addr);
773 /* In case the portal is busy, retry until successful.
774 * This function is guaranteed to succeed in a reasonable amount
780 qbman_release_desc_clear(&rd);
781 qbman_release_desc_set_bpid(&rd, bpid);
782 } while (qbman_swp_release(dflt_dpio->sw_portal, &rd, buf_array, i));
793 static int ldpaa_dpbp_seed(uint16_t bpid)
798 for (i = 0; i < LDPAA_ETH_NUM_BUFS; i += 7) {
799 count = ldpaa_bp_add_7(bpid);
801 printf("Buffer Seed= %d\n", count);
807 static int ldpaa_dpbp_setup(void)
811 err = dpbp_open(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_attr.id,
812 &dflt_dpbp->dpbp_handle);
814 printf("dpbp_open() failed\n");
818 err = dpbp_enable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
820 printf("dpbp_enable() failed\n");
824 err = dpbp_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS,
825 dflt_dpbp->dpbp_handle,
826 &dflt_dpbp->dpbp_attr);
828 printf("dpbp_get_attributes() failed\n");
832 err = ldpaa_dpbp_seed(dflt_dpbp->dpbp_attr.bpid);
835 printf("Buffer seeding failed for DPBP %d (bpid=%d)\n",
836 dflt_dpbp->dpbp_attr.id, dflt_dpbp->dpbp_attr.bpid);
844 dpbp_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
846 dpbp_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
851 static void ldpaa_dpbp_free(void)
854 dpbp_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
855 dpbp_reset(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
856 dpbp_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
859 static int ldpaa_dpmac_version_check(struct fsl_mc_io *mc_io,
860 struct ldpaa_eth_priv *priv)
863 uint16_t major_ver, minor_ver;
865 error = dpmac_get_api_version(dflt_mc_io, 0,
868 if ((major_ver < DPMAC_VER_MAJOR) ||
869 (major_ver == DPMAC_VER_MAJOR && minor_ver < DPMAC_VER_MINOR)) {
870 printf("DPMAC version mismatch found %u.%u,",
871 major_ver, minor_ver);
872 printf("supported version is %u.%u\n",
873 DPMAC_VER_MAJOR, DPMAC_VER_MINOR);
880 static int ldpaa_dpmac_setup(struct ldpaa_eth_priv *priv)
883 struct dpmac_cfg dpmac_cfg;
885 dpmac_cfg.mac_id = priv->dpmac_id;
887 err = dpmac_create(dflt_mc_io,
889 MC_CMD_NO_FLAGS, &dpmac_cfg,
892 printf("dpmac_create() failed\n");
894 err = ldpaa_dpmac_version_check(dflt_mc_io, priv);
896 printf("ldpaa_dpmac_version_check() failed: %d\n", err);
897 goto err_version_check;
900 err = dpmac_open(dflt_mc_io,
903 &priv->dpmac_handle);
905 printf("dpmac_open() failed: %d\n", err);
913 dpmac_destroy(dflt_mc_io,
915 MC_CMD_NO_FLAGS, priv->dpmac_id);
920 static int ldpaa_dpmac_bind(struct ldpaa_eth_priv *priv)
923 struct dprc_connection_cfg dprc_connection_cfg = {
924 /* If both rates are zero the connection */
925 /* will be configured in "best effort" mode. */
931 struct dprc_endpoint dbg_endpoint;
935 memset(&dpmac_endpoint, 0, sizeof(struct dprc_endpoint));
936 strcpy(dpmac_endpoint.type, "dpmac");
937 dpmac_endpoint.id = priv->dpmac_id;
939 memset(&dpni_endpoint, 0, sizeof(struct dprc_endpoint));
940 strcpy(dpni_endpoint.type, "dpni");
941 dpni_endpoint.id = dflt_dpni->dpni_id;
943 err = dprc_connect(dflt_mc_io, MC_CMD_NO_FLAGS,
947 &dprc_connection_cfg);
949 printf("dprc_connect() failed\n");
952 err = dprc_get_connection(dflt_mc_io, MC_CMD_NO_FLAGS,
953 dflt_dprc_handle, &dpni_endpoint,
954 &dbg_endpoint, &state);
955 printf("%s, DPMAC Type= %s\n", __func__, dbg_endpoint.type);
956 printf("%s, DPMAC ID= %d\n", __func__, dbg_endpoint.id);
957 printf("%s, DPMAC State= %d\n", __func__, state);
959 memset(&dbg_endpoint, 0, sizeof(struct dprc_endpoint));
960 err = dprc_get_connection(dflt_mc_io, MC_CMD_NO_FLAGS,
961 dflt_dprc_handle, &dpmac_endpoint,
962 &dbg_endpoint, &state);
963 printf("%s, DPNI Type= %s\n", __func__, dbg_endpoint.type);
964 printf("%s, DPNI ID= %d\n", __func__, dbg_endpoint.id);
965 printf("%s, DPNI State= %d\n", __func__, state);
970 static int ldpaa_dpni_setup(struct ldpaa_eth_priv *priv)
974 /* and get a handle for the DPNI this interface is associate with */
975 err = dpni_open(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_id,
976 &dflt_dpni->dpni_handle);
978 printf("dpni_open() failed\n");
981 err = dpni_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS,
982 dflt_dpni->dpni_handle,
983 &dflt_dpni->dpni_attrs);
985 printf("dpni_get_attributes() failed (err=%d)\n", err);
989 /* Configure our buffers' layout */
990 dflt_dpni->buf_layout.options = DPNI_BUF_LAYOUT_OPT_PARSER_RESULT |
991 DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
992 DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE |
993 DPNI_BUF_LAYOUT_OPT_DATA_ALIGN;
994 dflt_dpni->buf_layout.pass_parser_result = true;
995 dflt_dpni->buf_layout.pass_frame_status = true;
996 dflt_dpni->buf_layout.private_data_size = LDPAA_ETH_SWA_SIZE;
997 /* HW erratum mandates data alignment in multiples of 256 */
998 dflt_dpni->buf_layout.data_align = LDPAA_ETH_BUF_ALIGN;
1001 err = dpni_set_buffer_layout(dflt_mc_io, MC_CMD_NO_FLAGS,
1002 dflt_dpni->dpni_handle,
1003 &dflt_dpni->buf_layout, DPNI_QUEUE_RX);
1005 printf("dpni_set_buffer_layout() failed");
1006 goto err_buf_layout;
1010 /* remove Rx-only options */
1011 dflt_dpni->buf_layout.options &= ~(DPNI_BUF_LAYOUT_OPT_DATA_ALIGN |
1012 DPNI_BUF_LAYOUT_OPT_PARSER_RESULT);
1013 err = dpni_set_buffer_layout(dflt_mc_io, MC_CMD_NO_FLAGS,
1014 dflt_dpni->dpni_handle,
1015 &dflt_dpni->buf_layout, DPNI_QUEUE_TX);
1017 printf("dpni_set_buffer_layout() failed");
1018 goto err_buf_layout;
1021 /* ... tx-confirm. */
1022 dflt_dpni->buf_layout.options &= ~DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE;
1023 err = dpni_set_buffer_layout(dflt_mc_io, MC_CMD_NO_FLAGS,
1024 dflt_dpni->dpni_handle,
1025 &dflt_dpni->buf_layout,
1026 DPNI_QUEUE_TX_CONFIRM);
1028 printf("dpni_set_buffer_layout() failed");
1029 goto err_buf_layout;
1032 /* Now that we've set our tx buffer layout, retrieve the minimum
1033 * required tx data offset.
1035 err = dpni_get_tx_data_offset(dflt_mc_io, MC_CMD_NO_FLAGS,
1036 dflt_dpni->dpni_handle,
1037 &priv->tx_data_offset);
1039 printf("dpni_get_tx_data_offset() failed\n");
1040 goto err_data_offset;
1043 /* Warn in case TX data offset is not multiple of 64 bytes. */
1044 WARN_ON(priv->tx_data_offset % 64);
1046 /* Accomodate SWA space. */
1047 priv->tx_data_offset += LDPAA_ETH_SWA_SIZE;
1048 debug("priv->tx_data_offset=%d\n", priv->tx_data_offset);
1055 dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
1060 static int ldpaa_dpni_bind(struct ldpaa_eth_priv *priv)
1062 struct dpni_pools_cfg pools_params;
1063 struct dpni_queue tx_queue;
1066 memset(&pools_params, 0, sizeof(pools_params));
1067 pools_params.num_dpbp = 1;
1068 pools_params.pools[0].dpbp_id = (uint16_t)dflt_dpbp->dpbp_attr.id;
1069 pools_params.pools[0].buffer_size = LDPAA_ETH_RX_BUFFER_SIZE;
1070 err = dpni_set_pools(dflt_mc_io, MC_CMD_NO_FLAGS,
1071 dflt_dpni->dpni_handle, &pools_params);
1073 printf("dpni_set_pools() failed\n");
1077 memset(&tx_queue, 0, sizeof(struct dpni_queue));
1079 err = dpni_set_queue(dflt_mc_io, MC_CMD_NO_FLAGS,
1080 dflt_dpni->dpni_handle,
1081 DPNI_QUEUE_TX, 0, 0, &tx_queue);
1084 printf("dpni_set_queue() failed\n");
1088 err = dpni_set_tx_confirmation_mode(dflt_mc_io, MC_CMD_NO_FLAGS,
1089 dflt_dpni->dpni_handle,
1092 printf("dpni_set_tx_confirmation_mode() failed\n");
1099 #ifdef CONFIG_DM_ETH
1100 static int ldpaa_eth_probe(struct udevice *dev)
1102 struct ofnode_phandle_args phandle;
1104 /* Nothing to do if there is no "phy-handle" in the DTS node */
1105 if (dev_read_phandle_with_args(dev, "phy-handle", NULL,
1115 static uint32_t ldpaa_eth_get_dpmac_id(struct udevice *dev)
1117 int port_node = dev_of_offset(dev);
1119 return fdtdec_get_uint(gd->fdt_blob, port_node, "reg", -1);
1122 static const char *ldpaa_eth_get_phy_mode_str(struct udevice *dev)
1124 int port_node = dev_of_offset(dev);
1125 const char *phy_mode_str;
1127 phy_mode_str = fdt_getprop(gd->fdt_blob, port_node,
1128 "phy-connection-type", NULL);
1130 return phy_mode_str;
1132 phy_mode_str = fdt_getprop(gd->fdt_blob, port_node, "phy-mode", NULL);
1133 return phy_mode_str;
1136 static int ldpaa_eth_bind(struct udevice *dev)
1138 const char *phy_mode_str = NULL;
1143 phy_mode_str = ldpaa_eth_get_phy_mode_str(dev);
1145 phy_mode = phy_get_interface_by_name(phy_mode_str);
1146 if (phy_mode == -1) {
1147 dev_err(dev, "incorrect phy mode\n");
1151 dpmac_id = ldpaa_eth_get_dpmac_id(dev);
1152 if (dpmac_id == -1) {
1153 dev_err(dev, "missing reg field from the dpmac node\n");
1157 sprintf(eth_name, "DPMAC%d@%s", dpmac_id, phy_mode_str);
1158 device_set_name(dev, eth_name);
1163 static int ldpaa_eth_ofdata_to_platdata(struct udevice *dev)
1165 struct ldpaa_eth_priv *priv = dev_get_priv(dev);
1166 const char *phy_mode_str;
1168 priv->dpmac_id = ldpaa_eth_get_dpmac_id(dev);
1169 phy_mode_str = ldpaa_eth_get_phy_mode_str(dev);
1170 priv->phy_mode = phy_get_interface_by_name(phy_mode_str);
1175 static const struct eth_ops ldpaa_eth_ops = {
1176 .start = ldpaa_eth_open,
1177 .send = ldpaa_eth_tx,
1178 .recv = ldpaa_eth_pull_dequeue_rx,
1179 .stop = ldpaa_eth_stop,
1182 static const struct udevice_id ldpaa_eth_of_ids[] = {
1183 { .compatible = "fsl,qoriq-mc-dpmac" },
1186 U_BOOT_DRIVER(ldpaa_eth) = {
1187 .name = "ldpaa_eth",
1189 .of_match = ldpaa_eth_of_ids,
1190 .ofdata_to_platdata = ldpaa_eth_ofdata_to_platdata,
1191 .bind = ldpaa_eth_bind,
1192 .probe = ldpaa_eth_probe,
1193 .ops = &ldpaa_eth_ops,
1194 .priv_auto_alloc_size = sizeof(struct ldpaa_eth_priv),
1195 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1200 static int ldpaa_eth_netdev_init(struct eth_device *net_dev,
1201 phy_interface_t enet_if)
1204 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)net_dev->priv;
1206 snprintf(net_dev->name, ETH_NAME_LEN, "DPMAC%d@%s", priv->dpmac_id,
1207 phy_interface_strings[enet_if]);
1209 net_dev->iobase = 0;
1210 net_dev->init = ldpaa_eth_open;
1211 net_dev->halt = ldpaa_eth_stop;
1212 net_dev->send = ldpaa_eth_tx;
1213 net_dev->recv = ldpaa_eth_pull_dequeue_rx;
1215 #ifdef CONFIG_PHYLIB
1216 err = init_phy(net_dev);
1221 err = eth_register(net_dev);
1223 printf("eth_register() = %d\n", err);
1230 int ldpaa_eth_init(int dpmac_id, phy_interface_t enet_if)
1232 struct eth_device *net_dev = NULL;
1233 struct ldpaa_eth_priv *priv = NULL;
1237 net_dev = (struct eth_device *)malloc(sizeof(struct eth_device));
1239 printf("eth_device malloc() failed\n");
1242 memset(net_dev, 0, sizeof(struct eth_device));
1244 /* alloc the ldpaa ethernet private struct */
1245 priv = (struct ldpaa_eth_priv *)malloc(sizeof(struct ldpaa_eth_priv));
1247 printf("ldpaa_eth_priv malloc() failed\n");
1251 memset(priv, 0, sizeof(struct ldpaa_eth_priv));
1253 net_dev->priv = (void *)priv;
1254 priv->net_dev = (struct eth_device *)net_dev;
1255 priv->dpmac_id = dpmac_id;
1256 debug("%s dpmac_id=%d\n", __func__, dpmac_id);
1258 err = ldpaa_eth_netdev_init(net_dev, enet_if);
1260 goto err_netdev_init;
1262 debug("ldpaa ethernet: Probed interface %s\n", net_dev->name);
1267 net_dev->priv = NULL;