Lines Matching +full:sun8i +full:- +full:r40 +full:- +full:ccu
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * dwmac-sun8i.c - Allwinner sun8i DWMAC specific glue layer
11 #include <linux/mdio-mux.h>
26 /* General notes on dwmac-sun8i:
31 /* struct emac_variant - Describe dwmac-sun8i hardware variant
59 /* struct sunxi_priv_data - hold all sunxi private data
67 * @mux_handle: Internal pointer used by mdio-mux lib
87 /* EMAC clock register @ 0x164 in the CCU address range */
145 * co-packaged AC200 chip instead.
265 /* sun8i_dwmac_dma_reset() - reset the EMAC
266 * Called from stmmac via stmmac_dma_ops->reset
280 /* sun8i_dwmac_dma_init() - initialize the EMAC
281 * Called from stmmac via stmmac_dma_ops->init
306 /* sun8i_dwmac_dump_regs() - Dump EMAC address space
307 * Called from stmmac_dma_ops->dump_regs
321 /* sun8i_dwmac_dump_mac_regs() - Dump EMAC address space
322 * Called from stmmac_ops->dump_regs
329 void __iomem *ioaddr = hw->pcsr; in sun8i_dwmac_dump_mac_regs()
422 x->tx_normal_irq_n++; in sun8i_dwmac_dma_interrupt()
426 x->tx_process_stopped_irq++; in sun8i_dwmac_dma_interrupt()
429 x->tx_process_stopped_irq++; in sun8i_dwmac_dma_interrupt()
436 x->tx_undeflow_irq++; in sun8i_dwmac_dma_interrupt()
440 x->tx_early_irq++; in sun8i_dwmac_dma_interrupt()
444 x->rx_normal_irq_n++; in sun8i_dwmac_dma_interrupt()
448 x->rx_buf_unav_irq++; in sun8i_dwmac_dma_interrupt()
451 x->rx_process_stopped_irq++; in sun8i_dwmac_dma_interrupt()
458 x->rx_overflow_irq++; in sun8i_dwmac_dma_interrupt()
462 x->rx_early_irq++; in sun8i_dwmac_dma_interrupt()
465 x->irq_rgmii_n++; in sun8i_dwmac_dma_interrupt()
506 * especially when transmit store-and-forward is used." in sun8i_dwmac_dma_operation_mode_tx()
547 if (gmac->regulator) { in sun8i_dwmac_init()
548 ret = regulator_enable(gmac->regulator); in sun8i_dwmac_init()
550 dev_err(&pdev->dev, "Fail to enable regulator\n"); in sun8i_dwmac_init()
555 ret = clk_prepare_enable(gmac->tx_clk); in sun8i_dwmac_init()
557 if (gmac->regulator) in sun8i_dwmac_init()
558 regulator_disable(gmac->regulator); in sun8i_dwmac_init()
559 dev_err(&pdev->dev, "Could not enable AHB clock\n"); in sun8i_dwmac_init()
569 void __iomem *ioaddr = hw->pcsr; in sun8i_dwmac_core_init()
601 void __iomem *ioaddr = hw->pcsr; in sun8i_dwmac_set_umac_addr()
622 void __iomem *ioaddr = hw->pcsr; in sun8i_dwmac_get_umac_addr()
631 void __iomem *ioaddr = hw->pcsr; in sun8i_dwmac_rx_ipc_enable()
644 void __iomem *ioaddr = hw->pcsr; in sun8i_dwmac_set_filter()
652 if (dev->flags & IFF_PROMISC) { in sun8i_dwmac_set_filter()
654 } else if (dev->flags & IFF_ALLMULTI) { in sun8i_dwmac_set_filter()
656 } else if (macaddrs <= hw->unicast_filter_entries) { in sun8i_dwmac_set_filter()
659 sun8i_dwmac_set_umac_addr(hw, ha->addr, i); in sun8i_dwmac_set_filter()
665 sun8i_dwmac_set_umac_addr(hw, ha->addr, i); in sun8i_dwmac_set_filter()
676 while (i < hw->unicast_filter_entries) in sun8i_dwmac_set_filter()
686 void __iomem *ioaddr = hw->pcsr; in sun8i_dwmac_flow_ctrl()
709 v = readl(priv->ioaddr + EMAC_BASIC_CTL1); in sun8i_dwmac_reset()
710 writel(v | 0x01, priv->ioaddr + EMAC_BASIC_CTL1); in sun8i_dwmac_reset()
715 err = readl_poll_timeout(priv->ioaddr + EMAC_BASIC_CTL1, v, in sun8i_dwmac_reset()
719 dev_err(priv->device, "EMAC reset timeout\n"); in sun8i_dwmac_reset()
720 return -EFAULT; in sun8i_dwmac_reset()
725 /* Search in mdio-mux node for internal PHY node and get its clk/reset */
728 struct sunxi_priv_data *gmac = priv->plat->bsp_priv; in get_ephy_nodes()
733 mdio_mux = of_get_child_by_name(priv->device->of_node, "mdio-mux"); in get_ephy_nodes()
735 dev_err(priv->device, "Cannot get mdio-mux node\n"); in get_ephy_nodes()
736 return -ENODEV; in get_ephy_nodes()
740 "allwinner,sun8i-h3-mdio-internal"); in get_ephy_nodes()
743 dev_err(priv->device, "Cannot get internal_mdio node\n"); in get_ephy_nodes()
744 return -ENODEV; in get_ephy_nodes()
749 gmac->ephy_clk = of_clk_get(iphynode, 0); in get_ephy_nodes()
750 if (IS_ERR(gmac->ephy_clk)) in get_ephy_nodes()
752 gmac->rst_ephy = of_reset_control_get_exclusive(iphynode, NULL); in get_ephy_nodes()
753 if (IS_ERR(gmac->rst_ephy)) { in get_ephy_nodes()
754 ret = PTR_ERR(gmac->rst_ephy); in get_ephy_nodes()
755 if (ret == -EPROBE_DEFER) { in get_ephy_nodes()
762 dev_info(priv->device, "Found internal PHY node\n"); in get_ephy_nodes()
769 return -ENODEV; in get_ephy_nodes()
774 struct sunxi_priv_data *gmac = priv->plat->bsp_priv; in sun8i_dwmac_power_internal_phy()
777 if (gmac->internal_phy_powered) { in sun8i_dwmac_power_internal_phy()
778 dev_warn(priv->device, "Internal PHY already powered\n"); in sun8i_dwmac_power_internal_phy()
782 dev_info(priv->device, "Powering internal PHY\n"); in sun8i_dwmac_power_internal_phy()
783 ret = clk_prepare_enable(gmac->ephy_clk); in sun8i_dwmac_power_internal_phy()
785 dev_err(priv->device, "Cannot enable internal PHY\n"); in sun8i_dwmac_power_internal_phy()
789 /* Make sure the EPHY is properly reseted, as U-Boot may leave in sun8i_dwmac_power_internal_phy()
792 reset_control_assert(gmac->rst_ephy); in sun8i_dwmac_power_internal_phy()
794 ret = reset_control_deassert(gmac->rst_ephy); in sun8i_dwmac_power_internal_phy()
796 dev_err(priv->device, "Cannot deassert internal phy\n"); in sun8i_dwmac_power_internal_phy()
797 clk_disable_unprepare(gmac->ephy_clk); in sun8i_dwmac_power_internal_phy()
801 gmac->internal_phy_powered = true; in sun8i_dwmac_power_internal_phy()
808 if (!gmac->internal_phy_powered) in sun8i_dwmac_unpower_internal_phy()
811 clk_disable_unprepare(gmac->ephy_clk); in sun8i_dwmac_unpower_internal_phy()
812 reset_control_assert(gmac->rst_ephy); in sun8i_dwmac_unpower_internal_phy()
813 gmac->internal_phy_powered = false; in sun8i_dwmac_unpower_internal_phy()
818 * This function is called by the mdio-mux layer when it thinks the mdio bus
823 * The first time this function is called, current_child == -1.
831 struct sunxi_priv_data *gmac = priv->plat->bsp_priv; in mdio_mux_syscon_switch_fn()
837 regmap_field_read(gmac->regmap_field, ®); in mdio_mux_syscon_switch_fn()
840 dev_info(priv->device, "Switch mux to internal PHY"); in mdio_mux_syscon_switch_fn()
846 dev_info(priv->device, "Switch mux to external PHY"); in mdio_mux_syscon_switch_fn()
851 dev_err(priv->device, "Invalid child ID %x\n", in mdio_mux_syscon_switch_fn()
853 return -EINVAL; in mdio_mux_syscon_switch_fn()
855 regmap_field_write(gmac->regmap_field, val); in mdio_mux_syscon_switch_fn()
875 struct sunxi_priv_data *gmac = priv->plat->bsp_priv; in sun8i_dwmac_register_mdio_mux()
877 mdio_mux = of_get_child_by_name(priv->device->of_node, "mdio-mux"); in sun8i_dwmac_register_mdio_mux()
879 return -ENODEV; in sun8i_dwmac_register_mdio_mux()
881 ret = mdio_mux_init(priv->device, mdio_mux, mdio_mux_syscon_switch_fn, in sun8i_dwmac_register_mdio_mux()
882 &gmac->mux_handle, priv, priv->mii); in sun8i_dwmac_register_mdio_mux()
888 struct sunxi_priv_data *gmac = priv->plat->bsp_priv; in sun8i_dwmac_set_syscon()
889 struct device_node *node = priv->device->of_node; in sun8i_dwmac_set_syscon()
893 ret = regmap_field_read(gmac->regmap_field, &val); in sun8i_dwmac_set_syscon()
895 dev_err(priv->device, "Fail to read from regmap field.\n"); in sun8i_dwmac_set_syscon()
899 reg = gmac->variant->default_syscon_value; in sun8i_dwmac_set_syscon()
901 dev_warn(priv->device, in sun8i_dwmac_set_syscon()
905 if (gmac->variant->soc_has_internal_phy) { in sun8i_dwmac_set_syscon()
906 if (of_property_read_bool(node, "allwinner,leds-active-low")) in sun8i_dwmac_set_syscon()
914 ret = of_mdio_parse_addr(priv->device, priv->plat->phy_node); in sun8i_dwmac_set_syscon()
916 dev_err(priv->device, "Could not parse MDIO addr\n"); in sun8i_dwmac_set_syscon()
930 if (!of_property_read_u32(node, "allwinner,tx-delay-ps", &val)) { in sun8i_dwmac_set_syscon()
932 dev_err(priv->device, "tx-delay must be a multiple of 100\n"); in sun8i_dwmac_set_syscon()
933 return -EINVAL; in sun8i_dwmac_set_syscon()
936 dev_dbg(priv->device, "set tx-delay to %x\n", val); in sun8i_dwmac_set_syscon()
937 if (val <= gmac->variant->tx_delay_max) { in sun8i_dwmac_set_syscon()
938 reg &= ~(gmac->variant->tx_delay_max << in sun8i_dwmac_set_syscon()
942 dev_err(priv->device, "Invalid TX clock delay: %d\n", in sun8i_dwmac_set_syscon()
944 return -EINVAL; in sun8i_dwmac_set_syscon()
948 if (!of_property_read_u32(node, "allwinner,rx-delay-ps", &val)) { in sun8i_dwmac_set_syscon()
950 dev_err(priv->device, "rx-delay must be a multiple of 100\n"); in sun8i_dwmac_set_syscon()
951 return -EINVAL; in sun8i_dwmac_set_syscon()
954 dev_dbg(priv->device, "set rx-delay to %x\n", val); in sun8i_dwmac_set_syscon()
955 if (val <= gmac->variant->rx_delay_max) { in sun8i_dwmac_set_syscon()
956 reg &= ~(gmac->variant->rx_delay_max << in sun8i_dwmac_set_syscon()
960 dev_err(priv->device, "Invalid RX clock delay: %d\n", in sun8i_dwmac_set_syscon()
962 return -EINVAL; in sun8i_dwmac_set_syscon()
968 if (gmac->variant->support_rmii) in sun8i_dwmac_set_syscon()
971 switch (priv->plat->interface) { in sun8i_dwmac_set_syscon()
985 dev_err(priv->device, "Unsupported interface mode: %s", in sun8i_dwmac_set_syscon()
986 phy_modes(priv->plat->interface)); in sun8i_dwmac_set_syscon()
987 return -EINVAL; in sun8i_dwmac_set_syscon()
990 regmap_field_write(gmac->regmap_field, reg); in sun8i_dwmac_set_syscon()
997 u32 reg = gmac->variant->default_syscon_value; in sun8i_dwmac_unset_syscon()
999 regmap_field_write(gmac->regmap_field, reg); in sun8i_dwmac_unset_syscon()
1006 if (gmac->variant->soc_has_internal_phy) { in sun8i_dwmac_exit()
1008 if (gmac->mux_handle) in sun8i_dwmac_exit()
1009 mdio_mux_uninit(gmac->mux_handle); in sun8i_dwmac_exit()
1010 if (gmac->internal_phy_powered) in sun8i_dwmac_exit()
1016 reset_control_put(gmac->rst_ephy); in sun8i_dwmac_exit()
1018 clk_disable_unprepare(gmac->tx_clk); in sun8i_dwmac_exit()
1020 if (gmac->regulator) in sun8i_dwmac_exit()
1021 regulator_disable(gmac->regulator); in sun8i_dwmac_exit()
1054 mac = devm_kzalloc(priv->device, sizeof(*mac), GFP_KERNEL); in sun8i_dwmac_setup()
1062 mac->pcsr = priv->ioaddr; in sun8i_dwmac_setup()
1063 mac->mac = &sun8i_dwmac_ops; in sun8i_dwmac_setup()
1064 mac->dma = &sun8i_dwmac_dma_ops; in sun8i_dwmac_setup()
1066 priv->dev->priv_flags |= IFF_UNICAST_FLT; in sun8i_dwmac_setup()
1068 /* The loopback bit seems to be re-set when link change in sun8i_dwmac_setup()
1072 mac->link.speed_mask = GENMASK(3, 2) | EMAC_LOOPBACK; in sun8i_dwmac_setup()
1073 mac->link.speed10 = EMAC_SPEED_10; in sun8i_dwmac_setup()
1074 mac->link.speed100 = EMAC_SPEED_100; in sun8i_dwmac_setup()
1075 mac->link.speed1000 = EMAC_SPEED_1000; in sun8i_dwmac_setup()
1076 mac->link.duplex = EMAC_DUPLEX_FULL; in sun8i_dwmac_setup()
1077 mac->mii.addr = EMAC_MDIO_CMD; in sun8i_dwmac_setup()
1078 mac->mii.data = EMAC_MDIO_DATA; in sun8i_dwmac_setup()
1079 mac->mii.reg_shift = 4; in sun8i_dwmac_setup()
1080 mac->mii.reg_mask = GENMASK(8, 4); in sun8i_dwmac_setup()
1081 mac->mii.addr_shift = 12; in sun8i_dwmac_setup()
1082 mac->mii.addr_mask = GENMASK(16, 12); in sun8i_dwmac_setup()
1083 mac->mii.clk_csr_shift = 20; in sun8i_dwmac_setup()
1084 mac->mii.clk_csr_mask = GENMASK(22, 20); in sun8i_dwmac_setup()
1085 mac->unicast_filter_entries = 8; in sun8i_dwmac_setup()
1088 priv->synopsys_id = 0; in sun8i_dwmac_setup()
1101 return ERR_PTR(-ENODEV); in sun8i_dwmac_get_syscon_from_dev()
1106 regmap = ERR_PTR(-EPROBE_DEFER); in sun8i_dwmac_get_syscon_from_dev()
1111 regmap = dev_get_regmap(&syscon_pdev->dev, NULL); in sun8i_dwmac_get_syscon_from_dev()
1113 regmap = ERR_PTR(-EINVAL); in sun8i_dwmac_get_syscon_from_dev()
1126 struct device *dev = &pdev->dev; in sun8i_dwmac_probe()
1143 return -ENOMEM; in sun8i_dwmac_probe()
1145 gmac->variant = of_device_get_match_data(&pdev->dev); in sun8i_dwmac_probe()
1146 if (!gmac->variant) { in sun8i_dwmac_probe()
1147 dev_err(&pdev->dev, "Missing dwmac-sun8i variant\n"); in sun8i_dwmac_probe()
1148 return -EINVAL; in sun8i_dwmac_probe()
1151 gmac->tx_clk = devm_clk_get(dev, "stmmaceth"); in sun8i_dwmac_probe()
1152 if (IS_ERR(gmac->tx_clk)) { in sun8i_dwmac_probe()
1154 return PTR_ERR(gmac->tx_clk); in sun8i_dwmac_probe()
1158 gmac->regulator = devm_regulator_get_optional(dev, "phy"); in sun8i_dwmac_probe()
1159 if (IS_ERR(gmac->regulator)) { in sun8i_dwmac_probe()
1160 if (PTR_ERR(gmac->regulator) == -EPROBE_DEFER) in sun8i_dwmac_probe()
1161 return -EPROBE_DEFER; in sun8i_dwmac_probe()
1163 gmac->regulator = NULL; in sun8i_dwmac_probe()
1167 * CCU address range (on the R40), or the system control address in sun8i_dwmac_probe()
1168 * range (on most other sun8i and later SoCs). in sun8i_dwmac_probe()
1183 regmap = sun8i_dwmac_get_syscon_from_dev(pdev->dev.of_node); in sun8i_dwmac_probe()
1185 regmap = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, in sun8i_dwmac_probe()
1189 dev_err(&pdev->dev, "Unable to map syscon: %d\n", ret); in sun8i_dwmac_probe()
1193 gmac->regmap_field = devm_regmap_field_alloc(dev, regmap, in sun8i_dwmac_probe()
1194 *gmac->variant->syscon_field); in sun8i_dwmac_probe()
1195 if (IS_ERR(gmac->regmap_field)) { in sun8i_dwmac_probe()
1196 ret = PTR_ERR(gmac->regmap_field); in sun8i_dwmac_probe()
1201 ret = of_get_phy_mode(dev->of_node, &interface); in sun8i_dwmac_probe()
1203 return -EINVAL; in sun8i_dwmac_probe()
1204 plat_dat->interface = interface; in sun8i_dwmac_probe()
1209 plat_dat->rx_coe = STMMAC_RX_COE_TYPE2; in sun8i_dwmac_probe()
1210 plat_dat->tx_coe = 1; in sun8i_dwmac_probe()
1211 plat_dat->has_sun8i = true; in sun8i_dwmac_probe()
1212 plat_dat->bsp_priv = gmac; in sun8i_dwmac_probe()
1213 plat_dat->init = sun8i_dwmac_init; in sun8i_dwmac_probe()
1214 plat_dat->exit = sun8i_dwmac_exit; in sun8i_dwmac_probe()
1215 plat_dat->setup = sun8i_dwmac_setup; in sun8i_dwmac_probe()
1217 ret = sun8i_dwmac_init(pdev, plat_dat->bsp_priv); in sun8i_dwmac_probe()
1221 ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res); in sun8i_dwmac_probe()
1225 ndev = dev_get_drvdata(&pdev->dev); in sun8i_dwmac_probe()
1230 if (gmac->variant->soc_has_internal_phy) { in sun8i_dwmac_probe()
1236 dev_err(&pdev->dev, "Failed to register mux\n"); in sun8i_dwmac_probe()
1254 { .compatible = "allwinner,sun8i-h3-emac",
1256 { .compatible = "allwinner,sun8i-v3s-emac",
1258 { .compatible = "allwinner,sun8i-a83t-emac",
1260 { .compatible = "allwinner,sun8i-r40-gmac",
1262 { .compatible = "allwinner,sun50i-a64-emac",
1264 { .compatible = "allwinner,sun50i-h6-emac",
1274 .name = "dwmac-sun8i",
1282 MODULE_DESCRIPTION("Allwinner sun8i DWMAC specific glue layer");