Lines Matching +full:mt8516 +full:- +full:eth
1 // SPDX-License-Identifier: GPL-2.0-only
12 #include <linux/dma-mapping.h>
77 /* Flow-Control Configuration Register */
190 * reuse the same structure for both TX and RX - the layout is the same, only
268 return priv->ndev->dev.parent; in mtk_star_get_dev()
282 ring->descs = descs; in mtk_star_ring_init()
283 ring->head = 0; in mtk_star_ring_init()
284 ring->tail = 0; in mtk_star_ring_init()
290 struct mtk_star_ring_desc *desc = &ring->descs[ring->tail]; in mtk_star_ring_pop_tail()
293 status = READ_ONCE(desc->status); in mtk_star_ring_pop_tail()
297 return -1; in mtk_star_ring_pop_tail()
299 desc_data->len = status & MTK_STAR_DESC_MSK_LEN; in mtk_star_ring_pop_tail()
300 desc_data->flags = status & ~MTK_STAR_DESC_MSK_LEN; in mtk_star_ring_pop_tail()
301 desc_data->dma_addr = ring->dma_addrs[ring->tail]; in mtk_star_ring_pop_tail()
302 desc_data->skb = ring->skbs[ring->tail]; in mtk_star_ring_pop_tail()
304 ring->dma_addrs[ring->tail] = 0; in mtk_star_ring_pop_tail()
305 ring->skbs[ring->tail] = NULL; in mtk_star_ring_pop_tail()
309 WRITE_ONCE(desc->data_ptr, 0); in mtk_star_ring_pop_tail()
310 WRITE_ONCE(desc->status, status); in mtk_star_ring_pop_tail()
312 ring->tail = (ring->tail + 1) % MTK_STAR_RING_NUM_DESCS; in mtk_star_ring_pop_tail()
321 struct mtk_star_ring_desc *desc = &ring->descs[ring->head]; in mtk_star_ring_push_head()
324 status = READ_ONCE(desc->status); in mtk_star_ring_push_head()
326 ring->skbs[ring->head] = desc_data->skb; in mtk_star_ring_push_head()
327 ring->dma_addrs[ring->head] = desc_data->dma_addr; in mtk_star_ring_push_head()
329 status |= desc_data->len; in mtk_star_ring_push_head()
333 WRITE_ONCE(desc->data_ptr, desc_data->dma_addr); in mtk_star_ring_push_head()
334 WRITE_ONCE(desc->status, status); in mtk_star_ring_push_head()
338 WRITE_ONCE(desc->status, status); in mtk_star_ring_push_head()
340 ring->head = (ring->head + 1) % MTK_STAR_RING_NUM_DESCS; in mtk_star_ring_push_head()
363 return abs(ring->head - ring->tail); in mtk_star_ring_num_used_descs()
382 return dma_map_single(dev, skb_tail_pointer(skb) - 2, in mtk_star_dma_map_rx()
391 dma_unmap_single(dev, desc_data->dma_addr, in mtk_star_dma_unmap_rx()
392 skb_tailroom(desc_data->skb), DMA_FROM_DEVICE); in mtk_star_dma_unmap_rx()
400 return dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE); in mtk_star_dma_map_tx()
408 return dma_unmap_single(dev, desc_data->dma_addr, in mtk_star_dma_unmap_tx()
409 skb_headlen(desc_data->skb), DMA_TO_DEVICE); in mtk_star_dma_unmap_tx()
414 regmap_clear_bits(priv->regs, MTK_STAR_REG_MAC_CFG, in mtk_star_nic_disable_pd()
425 regmap_write(priv->regs, MTK_STAR_REG_INT_MASK, ~val); in mtk_star_intr_enable()
430 regmap_write(priv->regs, MTK_STAR_REG_INT_MASK, ~0); in mtk_star_intr_disable()
437 regmap_read(priv->regs, MTK_STAR_REG_INT_STS, &val); in mtk_star_intr_read()
447 regmap_write(priv->regs, MTK_STAR_REG_INT_STS, val); in mtk_star_intr_ack_all()
458 priv->descs_base = (struct mtk_star_ring_desc *)priv->ring_base; in mtk_star_dma_init()
461 desc = &priv->descs_base[i]; in mtk_star_dma_init()
464 desc->status = MTK_STAR_DESC_BIT_COWN; in mtk_star_dma_init()
465 if ((i == MTK_STAR_NUM_TX_DESCS - 1) || in mtk_star_dma_init()
466 (i == MTK_STAR_NUM_DESCS_TOTAL - 1)) in mtk_star_dma_init()
467 desc->status |= MTK_STAR_DESC_BIT_EOR; in mtk_star_dma_init()
470 mtk_star_ring_init(&priv->tx_ring, priv->descs_base); in mtk_star_dma_init()
471 mtk_star_ring_init(&priv->rx_ring, in mtk_star_dma_init()
472 priv->descs_base + MTK_STAR_NUM_TX_DESCS); in mtk_star_dma_init()
475 val = (unsigned int)priv->dma_addr; in mtk_star_dma_init()
476 regmap_write(priv->regs, MTK_STAR_REG_TX_BASE_ADDR, val); in mtk_star_dma_init()
477 regmap_write(priv->regs, MTK_STAR_REG_TX_DPTR, val); in mtk_star_dma_init()
480 regmap_write(priv->regs, MTK_STAR_REG_RX_BASE_ADDR, val); in mtk_star_dma_init()
481 regmap_write(priv->regs, MTK_STAR_REG_RX_DPTR, val); in mtk_star_dma_init()
486 regmap_set_bits(priv->regs, MTK_STAR_REG_TX_DMA_CTRL, in mtk_star_dma_start()
488 regmap_set_bits(priv->regs, MTK_STAR_REG_RX_DMA_CTRL, in mtk_star_dma_start()
494 regmap_write(priv->regs, MTK_STAR_REG_TX_DMA_CTRL, in mtk_star_dma_stop()
496 regmap_write(priv->regs, MTK_STAR_REG_RX_DMA_CTRL, in mtk_star_dma_stop()
508 priv->descs_base[i].status |= MTK_STAR_DESC_BIT_COWN; in mtk_star_dma_disable()
513 regmap_set_bits(priv->regs, MTK_STAR_REG_RX_DMA_CTRL, in mtk_star_dma_resume_rx()
519 regmap_set_bits(priv->regs, MTK_STAR_REG_TX_DMA_CTRL, in mtk_star_dma_resume_tx()
526 u8 *mac_addr = ndev->dev_addr; in mtk_star_set_mac_addr()
533 regmap_write(priv->regs, MTK_STAR_REG_MY_MAC_H, high); in mtk_star_set_mac_addr()
534 regmap_write(priv->regs, MTK_STAR_REG_MY_MAC_L, low); in mtk_star_set_mac_addr()
573 regmap_read(priv->regs, counter_regs[i], &val); in mtk_star_reset_counters()
581 regmap_read(priv->regs, reg, &val); in mtk_star_update_stat()
590 struct rtnl_link_stats64 *stats = &priv->stats; in mtk_star_update_stats()
593 mtk_star_update_stat(priv, MTK_STAR_REG_C_RXOKPKT, &stats->rx_packets); in mtk_star_update_stats()
594 mtk_star_update_stat(priv, MTK_STAR_REG_C_TXOKPKT, &stats->tx_packets); in mtk_star_update_stats()
595 mtk_star_update_stat(priv, MTK_STAR_REG_C_RXOKBYTE, &stats->rx_bytes); in mtk_star_update_stats()
596 mtk_star_update_stat(priv, MTK_STAR_REG_C_TXOKBYTE, &stats->tx_bytes); in mtk_star_update_stats()
599 mtk_star_update_stat(priv, MTK_STAR_REG_C_RX_MULTI, &stats->multicast); in mtk_star_update_stats()
600 mtk_star_update_stat(priv, MTK_STAR_REG_C_TX_MULTI, &stats->multicast); in mtk_star_update_stats()
604 &stats->collisions); in mtk_star_update_stats()
606 &stats->collisions); in mtk_star_update_stats()
607 mtk_star_update_stat(priv, MTK_STAR_REG_C_RXRUNT, &stats->collisions); in mtk_star_update_stats()
611 &stats->rx_length_errors); in mtk_star_update_stats()
613 &stats->rx_over_errors); in mtk_star_update_stats()
614 mtk_star_update_stat(priv, MTK_STAR_REG_C_RXCRC, &stats->rx_crc_errors); in mtk_star_update_stats()
616 &stats->rx_frame_errors); in mtk_star_update_stats()
618 &stats->rx_fifo_errors); in mtk_star_update_stats()
620 mtk_star_update_stat(priv, MTK_STAR_REG_C_RX_RERR, &stats->rx_errors); in mtk_star_update_stats()
621 stats->rx_errors += stats->rx_length_errors; in mtk_star_update_stats()
622 stats->rx_errors += stats->rx_over_errors; in mtk_star_update_stats()
623 stats->rx_errors += stats->rx_crc_errors; in mtk_star_update_stats()
624 stats->rx_errors += stats->rx_frame_errors; in mtk_star_update_stats()
625 stats->rx_errors += stats->rx_fifo_errors; in mtk_star_update_stats()
639 if (tail & (MTK_STAR_SKB_ALIGNMENT - 1)) { in mtk_star_alloc_skb()
640 offset = tail & (MTK_STAR_SKB_ALIGNMENT - 1); in mtk_star_alloc_skb()
641 skb_reserve(skb, MTK_STAR_SKB_ALIGNMENT - offset); in mtk_star_alloc_skb()
644 /* Ensure 16-byte alignment of the skb pointer: eth_type_trans() will in mtk_star_alloc_skb()
655 struct mtk_star_ring *ring = &priv->rx_ring; in mtk_star_prepare_rx_skbs()
665 return -ENOMEM; in mtk_star_prepare_rx_skbs()
670 return -ENOMEM; in mtk_star_prepare_rx_skbs()
673 desc = &ring->descs[i]; in mtk_star_prepare_rx_skbs()
674 desc->data_ptr = dma_addr; in mtk_star_prepare_rx_skbs()
675 desc->status |= skb_tailroom(skb) & MTK_STAR_DESC_MSK_LEN; in mtk_star_prepare_rx_skbs()
676 desc->status &= ~MTK_STAR_DESC_BIT_COWN; in mtk_star_prepare_rx_skbs()
677 ring->skbs[i] = skb; in mtk_star_prepare_rx_skbs()
678 ring->dma_addrs[i] = dma_addr; in mtk_star_prepare_rx_skbs()
693 if (!ring->dma_addrs[i]) in mtk_star_ring_free_skbs()
696 desc_data.dma_addr = ring->dma_addrs[i]; in mtk_star_ring_free_skbs()
697 desc_data.skb = ring->skbs[i]; in mtk_star_ring_free_skbs()
706 struct mtk_star_ring *ring = &priv->rx_ring; in mtk_star_free_rx_skbs()
713 struct mtk_star_ring *ring = &priv->tx_ring; in mtk_star_free_tx_skbs()
720 * FIXME: The interrupt handling should be more fine-grained with each
736 napi_schedule(&priv->napi); in mtk_star_handle_irq()
742 /* Wait for the completion of any previous command - CMD_START bit must be
749 return regmap_read_poll_timeout_atomic(priv->regs, in mtk_star_hash_wait_cmd_start()
761 ret = regmap_read_poll_timeout_atomic(priv->regs, in mtk_star_hash_wait_ok()
769 if (!regmap_test_bits(priv->regs, MTK_STAR_REG_HASH_CTRL, in mtk_star_hash_wait_ok()
771 return -EIO; in mtk_star_hash_wait_ok()
791 regmap_write(priv->regs, MTK_STAR_REG_HASH_CTRL, val); in mtk_star_set_hashbit()
804 regmap_set_bits(priv->regs, MTK_STAR_REG_HASH_CTRL, in mtk_star_reset_hash_table()
806 regmap_set_bits(priv->regs, MTK_STAR_REG_TEST1, in mtk_star_reset_hash_table()
816 if (priv->speed == SPEED_1000) in mtk_star_phy_config()
818 else if (priv->speed == SPEED_100) in mtk_star_phy_config()
827 /* Only full-duplex supported for now. */ in mtk_star_phy_config()
830 regmap_write(priv->regs, MTK_STAR_REG_PHY_CTRL1, val); in mtk_star_phy_config()
832 if (priv->pause) { in mtk_star_phy_config()
840 regmap_update_bits(priv->regs, MTK_STAR_REG_FC_CFG, in mtk_star_phy_config()
844 if (priv->pause) { in mtk_star_phy_config()
851 regmap_update_bits(priv->regs, MTK_STAR_REG_EXT_CFG, in mtk_star_phy_config()
858 struct phy_device *phydev = priv->phydev; in mtk_star_adjust_link()
861 if (phydev->link) { in mtk_star_adjust_link()
862 if (!priv->link) { in mtk_star_adjust_link()
863 priv->link = phydev->link; in mtk_star_adjust_link()
867 if (priv->speed != phydev->speed) { in mtk_star_adjust_link()
868 priv->speed = phydev->speed; in mtk_star_adjust_link()
872 if (priv->pause != phydev->pause) { in mtk_star_adjust_link()
873 priv->pause = phydev->pause; in mtk_star_adjust_link()
877 if (priv->link) { in mtk_star_adjust_link()
878 priv->link = phydev->link; in mtk_star_adjust_link()
884 if (phydev->link) in mtk_star_adjust_link()
887 phy_print_status(ndev->phydev); in mtk_star_adjust_link()
899 regmap_write(priv->regs, MTK_STAR_REG_SYS_CONF, val); in mtk_star_init_config()
900 regmap_update_bits(priv->regs, MTK_STAR_REG_MAC_CLK_CONF, in mtk_star_init_config()
907 regmap_update_bits(priv->pericfg, MTK_PERICFG_REG_NIC_CFG_CON, in mtk_star_set_mode_rmii()
930 regmap_write(priv->regs, MTK_STAR_REG_MAC_CFG, val); in mtk_star_enable()
938 regmap_clear_bits(priv->regs, MTK_STAR_REG_ARL_CFG, in mtk_star_enable()
943 regmap_clear_bits(priv->regs, MTK_STAR_REG_MAC_CFG, in mtk_star_enable()
954 ret = request_irq(ndev->irq, mtk_star_handle_irq, in mtk_star_enable()
955 IRQF_TRIGGER_FALLING, ndev->name, ndev); in mtk_star_enable()
959 napi_enable(&priv->napi); in mtk_star_enable()
965 priv->phydev = of_phy_connect(ndev, priv->phy_node, in mtk_star_enable()
966 mtk_star_adjust_link, 0, priv->phy_intf); in mtk_star_enable()
967 if (!priv->phydev) { in mtk_star_enable()
969 ret = -ENODEV; in mtk_star_enable()
974 phy_start(priv->phydev); in mtk_star_enable()
980 free_irq(ndev->irq, ndev); in mtk_star_enable()
992 napi_disable(&priv->napi); in mtk_star_disable()
996 phy_stop(priv->phydev); in mtk_star_disable()
997 phy_disconnect(priv->phydev); in mtk_star_disable()
998 free_irq(ndev->irq, ndev); in mtk_star_disable()
1019 return -EINVAL; in mtk_star_netdev_ioctl()
1021 return phy_mii_ioctl(ndev->phydev, req, cmd); in mtk_star_netdev_ioctl()
1028 struct mtk_star_ring *ring = &priv->tx_ring; in mtk_star_netdev_start_xmit()
1037 desc_data.len = skb->len; in mtk_star_netdev_start_xmit()
1039 spin_lock_bh(&priv->lock); in mtk_star_netdev_start_xmit()
1043 netdev_sent_queue(ndev, skb->len); in mtk_star_netdev_start_xmit()
1048 spin_unlock_bh(&priv->lock); in mtk_star_netdev_start_xmit()
1056 ndev->stats.tx_dropped++; in mtk_star_netdev_start_xmit()
1065 struct mtk_star_ring *ring = &priv->tx_ring; in mtk_star_tx_complete_one()
1074 ret = desc_data.skb->len; in mtk_star_tx_complete_one()
1082 struct mtk_star_ring *ring = &priv->tx_ring; in mtk_star_tx_complete_all()
1083 struct net_device *ndev = priv->ndev; in mtk_star_tx_complete_all()
1087 spin_lock(&priv->lock); in mtk_star_tx_complete_all()
1104 spin_unlock(&priv->lock); in mtk_star_tx_complete_all()
1114 memcpy(stats, &priv->stats, sizeof(*stats)); in mtk_star_netdev_get_stats64()
1124 if (ndev->flags & IFF_PROMISC) { in mtk_star_set_rx_mode()
1125 regmap_set_bits(priv->regs, MTK_STAR_REG_ARL_CFG, in mtk_star_set_rx_mode()
1128 ndev->flags & IFF_ALLMULTI) { in mtk_star_set_rx_mode()
1141 hash_addr = (hw_addr->addr[0] & 0x01) << 8; in mtk_star_set_rx_mode()
1142 hash_addr += hw_addr->addr[5]; in mtk_star_set_rx_mode()
1152 if (ret == -ETIMEDOUT) in mtk_star_set_rx_mode()
1155 /* Should be -EIO */ in mtk_star_set_rx_mode()
1173 strlcpy(info->driver, MTK_STAR_DRVNAME, sizeof(info->driver)); in mtk_star_get_drvinfo()
1186 struct mtk_star_ring *ring = &priv->rx_ring; in mtk_star_receive_packet()
1189 struct net_device *ndev = priv->ndev; in mtk_star_receive_packet()
1194 spin_lock(&priv->lock); in mtk_star_receive_packet()
1196 spin_unlock(&priv->lock); in mtk_star_receive_packet()
1198 return -1; in mtk_star_receive_packet()
1204 /* Error packet -> drop and reuse skb. */ in mtk_star_receive_packet()
1214 ndev->stats.rx_dropped++; in mtk_star_receive_packet()
1221 ndev->stats.rx_dropped++; in mtk_star_receive_packet()
1234 desc_data.skb->ip_summed = CHECKSUM_NONE; in mtk_star_receive_packet()
1235 desc_data.skb->protocol = eth_type_trans(desc_data.skb, ndev); in mtk_star_receive_packet()
1236 desc_data.skb->dev = ndev; in mtk_star_receive_packet()
1243 spin_lock(&priv->lock); in mtk_star_receive_packet()
1245 spin_unlock(&priv->lock); in mtk_star_receive_packet()
1274 /* Clean-up all TX descriptors. */ in mtk_star_poll()
1296 regmap_write(priv->regs, MTK_STAR_REG_PHY_CTRL0, in mtk_star_mdio_rwok_clear()
1304 return regmap_read_poll_timeout(priv->regs, MTK_STAR_REG_PHY_CTRL0, in mtk_star_mdio_rwok_wait()
1311 struct mtk_star_priv *priv = mii->priv; in mtk_star_mdio_read()
1316 return -EOPNOTSUPP; in mtk_star_mdio_read()
1324 regmap_write(priv->regs, MTK_STAR_REG_PHY_CTRL0, val); in mtk_star_mdio_read()
1330 regmap_read(priv->regs, MTK_STAR_REG_PHY_CTRL0, &data); in mtk_star_mdio_read()
1341 struct mtk_star_priv *priv = mii->priv; in mtk_star_mdio_write()
1345 return -EOPNOTSUPP; in mtk_star_mdio_write()
1357 regmap_write(priv->regs, MTK_STAR_REG_PHY_CTRL0, val); in mtk_star_mdio_write()
1369 of_node = dev->of_node; in mtk_star_mdio_init()
1373 return -ENODEV; in mtk_star_mdio_init()
1376 ret = -ENODEV; in mtk_star_mdio_init()
1380 priv->mii = devm_mdiobus_alloc(dev); in mtk_star_mdio_init()
1381 if (!priv->mii) { in mtk_star_mdio_init()
1382 ret = -ENOMEM; in mtk_star_mdio_init()
1386 snprintf(priv->mii->id, MII_BUS_ID_SIZE, "%s", dev_name(dev)); in mtk_star_mdio_init()
1387 priv->mii->name = "mtk-mac-mdio"; in mtk_star_mdio_init()
1388 priv->mii->parent = dev; in mtk_star_mdio_init()
1389 priv->mii->read = mtk_star_mdio_read; in mtk_star_mdio_init()
1390 priv->mii->write = mtk_star_mdio_write; in mtk_star_mdio_init()
1391 priv->mii->priv = priv; in mtk_star_mdio_init()
1393 ret = devm_of_mdiobus_register(dev, priv->mii, mdio_node); in mtk_star_mdio_init()
1411 clk_bulk_disable_unprepare(MTK_STAR_NCLKS, priv->clks); in mtk_star_suspend()
1425 ret = clk_bulk_prepare_enable(MTK_STAR_NCLKS, priv->clks); in mtk_star_resume()
1432 clk_bulk_disable_unprepare(MTK_STAR_NCLKS, priv->clks); in mtk_star_resume()
1442 clk_bulk_disable_unprepare(MTK_STAR_NCLKS, priv->clks); in mtk_star_clk_disable_unprepare()
1454 dev = &pdev->dev; in mtk_star_probe()
1455 of_node = dev->of_node; in mtk_star_probe()
1459 return -ENOMEM; in mtk_star_probe()
1462 priv->ndev = ndev; in mtk_star_probe()
1466 ndev->min_mtu = ETH_ZLEN; in mtk_star_probe()
1467 ndev->max_mtu = MTK_STAR_MAX_FRAME_SIZE; in mtk_star_probe()
1469 spin_lock_init(&priv->lock); in mtk_star_probe()
1479 priv->regs = devm_regmap_init_mmio(dev, base, in mtk_star_probe()
1481 if (IS_ERR(priv->regs)) in mtk_star_probe()
1482 return PTR_ERR(priv->regs); in mtk_star_probe()
1484 priv->pericfg = syscon_regmap_lookup_by_phandle(of_node, in mtk_star_probe()
1486 if (IS_ERR(priv->pericfg)) { in mtk_star_probe()
1488 return PTR_ERR(priv->pericfg); in mtk_star_probe()
1491 ndev->irq = platform_get_irq(pdev, 0); in mtk_star_probe()
1492 if (ndev->irq < 0) in mtk_star_probe()
1493 return ndev->irq; in mtk_star_probe()
1496 priv->clks[i].id = mtk_star_clk_names[i]; in mtk_star_probe()
1497 ret = devm_clk_bulk_get(dev, MTK_STAR_NCLKS, priv->clks); in mtk_star_probe()
1501 ret = clk_bulk_prepare_enable(MTK_STAR_NCLKS, priv->clks); in mtk_star_probe()
1510 ret = of_get_phy_mode(of_node, &priv->phy_intf); in mtk_star_probe()
1513 } else if (priv->phy_intf != PHY_INTERFACE_MODE_RMII) { in mtk_star_probe()
1515 phy_modes(priv->phy_intf)); in mtk_star_probe()
1516 return -EINVAL; in mtk_star_probe()
1519 priv->phy_node = of_parse_phandle(of_node, "phy-handle", 0); in mtk_star_probe()
1520 if (!priv->phy_node) { in mtk_star_probe()
1522 return -ENODEV; in mtk_star_probe()
1533 priv->ring_base = dmam_alloc_coherent(dev, MTK_STAR_DMA_SIZE, in mtk_star_probe()
1534 &priv->dma_addr, in mtk_star_probe()
1536 if (!priv->ring_base) in mtk_star_probe()
1537 return -ENOMEM; in mtk_star_probe()
1546 ret = eth_platform_get_mac_address(dev, ndev->dev_addr); in mtk_star_probe()
1547 if (ret || !is_valid_ether_addr(ndev->dev_addr)) in mtk_star_probe()
1550 ndev->netdev_ops = &mtk_star_netdev_ops; in mtk_star_probe()
1551 ndev->ethtool_ops = &mtk_star_ethtool_ops; in mtk_star_probe()
1553 netif_napi_add(ndev, &priv->napi, mtk_star_poll, MTK_STAR_NAPI_WEIGHT); in mtk_star_probe()
1559 { .compatible = "mediatek,mt8516-eth", },
1560 { .compatible = "mediatek,mt8518-eth", },
1561 { .compatible = "mediatek,mt8175-eth", },