/* General notes on dwmac-sun8i: * Locking: no locking is necessary in this file because all necessary locking * is done in the "stmmac files"
*/
/* struct emac_variant - Describe dwmac-sun8i hardware variant * @default_syscon_value: The default value of the EMAC register in syscon * This value is used for disabling properly EMAC * and used as a good starting value in case of the * boot process(uboot) leave some stuff. * @syscon_field reg_field for the syscon's gmac register * @soc_has_internal_phy: Does the MAC embed an internal PHY * @support_mii: Does the MAC handle MII * @support_rmii: Does the MAC handle RMII * @support_rgmii: Does the MAC handle RGMII * * @rx_delay_max: Maximum raw value for RX delay chain * @tx_delay_max: Maximum raw value for TX delay chain * These two also indicate the bitmask for * the RX and TX delay chain registers. A * value of zero indicates this is not supported.
*/ struct emac_variant {
u32 default_syscon_value; conststruct reg_field *syscon_field; bool soc_has_internal_phy; bool support_mii; bool support_rmii; bool support_rgmii;
u8 rx_delay_max;
u8 tx_delay_max;
};
/* struct sunxi_priv_data - hold all sunxi private data * @ephy_clk: reference to the optional EPHY clock for the internal PHY * @regulator: reference to the optional regulator * @rst_ephy: reference to the optional EPHY reset for the internal PHY * @variant: reference to the current board variant * @regmap: regmap for using the syscon * @internal_phy_powered: Does the internal PHY is enabled * @use_internal_phy: Is the internal PHY selected for use * @mux_handle: Internal pointer used by mdio-mux lib
*/ struct sunxi_priv_data { struct clk *ephy_clk; struct regulator *regulator; struct reset_control *rst_ephy; conststruct emac_variant *variant; struct regmap_field *regmap_field; bool internal_phy_powered; bool use_internal_phy; void *mux_handle;
};
/* EMAC clock register @ 0x30 in the "system control" address range */ staticconststruct reg_field sun8i_syscon_reg_field = {
.reg = 0x30,
.lsb = 0,
.msb = 31,
};
/* EMAC clock register @ 0x164 in the CCU address range */ staticconststruct reg_field sun8i_ccu_reg_field = {
.reg = 0x164,
.lsb = 0,
.msb = 31,
};
/* sun8i_dwmac_dump_regs() - Dump EMAC address space * Called from stmmac_dma_ops->dump_regs * Used for ethtool
*/ staticvoid sun8i_dwmac_dump_regs(struct stmmac_priv *priv, void __iomem *ioaddr, u32 *reg_space)
{ int i;
for (i = 0; i < 0xC8; i += 4) { if (i == 0x32 || i == 0x3C) continue;
reg_space[i / 4] = readl(ioaddr + i);
}
}
/* sun8i_dwmac_dump_mac_regs() - Dump EMAC address space * Called from stmmac_ops->dump_regs * Used for ethtool
*/ staticvoid sun8i_dwmac_dump_mac_regs(struct mac_device_info *hw,
u32 *reg_space)
{ int i; void __iomem *ioaddr = hw->pcsr;
for (i = 0; i < 0xC8; i += 4) { if (i == 0x32 || i == 0x3C) continue;
reg_space[i / 4] = readl(ioaddr + i);
}
}
if (dir == DMA_DIR_RX)
v &= EMAC_INT_MSK_RX; elseif (dir == DMA_DIR_TX)
v &= EMAC_INT_MSK_TX;
if (v & EMAC_TX_INT) {
ret |= handle_tx;
u64_stats_update_begin(&stats->syncp);
u64_stats_inc(&stats->tx_normal_irq_n[chan]);
u64_stats_update_end(&stats->syncp);
}
if (v & EMAC_TX_DMA_STOP_INT)
x->tx_process_stopped_irq++;
if (v & EMAC_TX_BUF_UA_INT)
x->tx_process_stopped_irq++;
if (v & EMAC_TX_TIMEOUT_INT)
ret |= tx_hard_error;
if (v & EMAC_TX_UNDERFLOW_INT) {
ret |= tx_hard_error;
x->tx_undeflow_irq++;
}
if (v & EMAC_TX_EARLY_INT)
x->tx_early_irq++;
if (v & EMAC_RX_INT) {
ret |= handle_rx;
u64_stats_update_begin(&stats->syncp);
u64_stats_inc(&stats->rx_normal_irq_n[chan]);
u64_stats_update_end(&stats->syncp);
}
if (v & EMAC_RX_BUF_UA_INT)
x->rx_buf_unav_irq++;
if (v & EMAC_RX_DMA_STOP_INT)
x->rx_process_stopped_irq++;
if (v & EMAC_RX_TIMEOUT_INT)
ret |= tx_hard_error;
if (v & EMAC_RX_OVERFLOW_INT) {
ret |= tx_hard_error;
x->rx_overflow_irq++;
}
if (v & EMAC_RX_EARLY_INT)
x->rx_early_irq++;
if (v & EMAC_RGMII_STA_INT)
x->irq_rgmii_n++;
writel(v, ioaddr + EMAC_INT_STA);
return ret;
}
staticvoid sun8i_dwmac_dma_operation_mode_rx(struct stmmac_priv *priv, void __iomem *ioaddr, int mode,
u32 channel, int fifosz, u8 qmode)
{
u32 v;
v = readl(ioaddr + EMAC_RX_CTL1); if (mode == SF_DMA_MODE) {
v |= EMAC_RX_MD;
} else {
v &= ~EMAC_RX_MD;
v &= ~EMAC_RX_TH_MASK; if (mode < 32)
v |= EMAC_RX_TH_32; elseif (mode < 64)
v |= EMAC_RX_TH_64; elseif (mode < 96)
v |= EMAC_RX_TH_96; elseif (mode < 128)
v |= EMAC_RX_TH_128;
}
writel(v, ioaddr + EMAC_RX_CTL1);
}
staticvoid sun8i_dwmac_dma_operation_mode_tx(struct stmmac_priv *priv, void __iomem *ioaddr, int mode,
u32 channel, int fifosz, u8 qmode)
{
u32 v;
v = readl(ioaddr + EMAC_TX_CTL1); if (mode == SF_DMA_MODE) {
v |= EMAC_TX_MD; /* Undocumented bit (called TX_NEXT_FRM in BSP), the original * comment is * "Operating on second frame increase the performance * especially when transmit store-and-forward is used."
*/
v |= EMAC_TX_NEXT_FRM;
} else {
v &= ~EMAC_TX_MD;
v &= ~EMAC_TX_TH_MASK; if (mode < 64)
v |= EMAC_TX_TH_64; elseif (mode < 128)
v |= EMAC_TX_TH_128; elseif (mode < 192)
v |= EMAC_TX_TH_192; elseif (mode < 256)
v |= EMAC_TX_TH_256;
}
writel(v, ioaddr + EMAC_TX_CTL1);
}
t = readl(ioaddr + EMAC_TX_CTL0);
r = readl(ioaddr + EMAC_RX_CTL0); if (enable) {
t |= EMAC_TX_TRANSMITTER_EN;
r |= EMAC_RX_RECEIVER_EN;
} else {
t &= ~EMAC_TX_TRANSMITTER_EN;
r &= ~EMAC_RX_RECEIVER_EN;
}
writel(t, ioaddr + EMAC_TX_CTL0);
writel(r, ioaddr + EMAC_RX_CTL0);
}
/* Set MAC address at slot reg_n * All slot > 0 need to be enabled with MAC_ADDR_TYPE_DST * If addr is NULL, clear the slot
*/ staticvoid sun8i_dwmac_set_umac_addr(struct mac_device_info *hw, constunsignedchar *addr, unsignedint reg_n)
{ void __iomem *ioaddr = hw->pcsr;
u32 v;
if (!addr) {
writel(0, ioaddr + EMAC_MACADDR_HI(reg_n)); return;
}
stmmac_set_mac_addr(ioaddr, addr, EMAC_MACADDR_HI(reg_n),
EMAC_MACADDR_LO(reg_n)); if (reg_n > 0) {
v = readl(ioaddr + EMAC_MACADDR_HI(reg_n));
v |= MAC_ADDR_TYPE_DST;
writel(v, ioaddr + EMAC_MACADDR_HI(reg_n));
}
}
/* caution this function must return non 0 to work */ staticint sun8i_dwmac_rx_ipc_enable(struct mac_device_info *hw)
{ void __iomem *ioaddr = hw->pcsr;
u32 v;
v = readl(ioaddr + EMAC_RX_CTL0);
v |= EMAC_RX_DO_CRC;
writel(v, ioaddr + EMAC_RX_CTL0);
return 1;
}
staticvoid sun8i_dwmac_set_filter(struct mac_device_info *hw, struct net_device *dev)
{ void __iomem *ioaddr = hw->pcsr;
u32 v; int i = 1; struct netdev_hw_addr *ha; int macaddrs = netdev_uc_count(dev) + netdev_mc_count(dev) + 1;
v = EMAC_FRM_FLT_CTL;
if (dev->flags & IFF_PROMISC) {
v = EMAC_FRM_FLT_RXALL;
} elseif (dev->flags & IFF_ALLMULTI) {
v |= EMAC_FRM_FLT_MULTICAST;
} elseif (macaddrs <= hw->unicast_filter_entries) { if (!netdev_mc_empty(dev)) {
netdev_for_each_mc_addr(ha, dev) {
sun8i_dwmac_set_umac_addr(hw, ha->addr, i);
i++;
}
} if (!netdev_uc_empty(dev)) {
netdev_for_each_uc_addr(ha, dev) {
sun8i_dwmac_set_umac_addr(hw, ha->addr, i);
i++;
}
}
} else { if (!(readl(ioaddr + EMAC_RX_FRM_FLT) & EMAC_FRM_FLT_RXALL))
netdev_info(dev, "Too many address, switching to promiscuous\n");
v = EMAC_FRM_FLT_RXALL;
}
/* Disable unused address filter slots */ while (i < hw->unicast_filter_entries)
sun8i_dwmac_set_umac_addr(hw, NULL, i++);
/* The timeout was previoulsy set to 10ms, but some board (OrangePI0) * need more if no cable plugged. 100ms seems OK
*/
err = readl_poll_timeout(priv->ioaddr + EMAC_BASIC_CTL1, v,
!(v & 0x01), 100, 100000);
dev_info(priv->device, "Powering internal PHY\n");
ret = clk_prepare_enable(gmac->ephy_clk); if (ret) {
dev_err(priv->device, "Cannot enable internal PHY\n"); return ret;
}
/* Make sure the EPHY is properly reseted, as U-Boot may leave * it at deasserted state, and thus it may fail to reset EMAC. * * This assumes the driver has exclusive access to the EPHY reset.
*/
ret = reset_control_reset(gmac->rst_ephy); if (ret) {
dev_err(priv->device, "Cannot reset internal PHY\n");
clk_disable_unprepare(gmac->ephy_clk); return ret;
}
gmac->internal_phy_powered = true;
return 0;
}
staticvoid sun8i_dwmac_unpower_internal_phy(struct sunxi_priv_data *gmac)
{ if (!gmac->internal_phy_powered) return;
/* MDIO multiplexing switch function * This function is called by the mdio-mux layer when it thinks the mdio bus * multiplexer needs to switch. * 'current_child' is the current value of the mux register * 'desired_child' is the value of the 'reg' property of the target child MDIO * node. * The first time this function is called, current_child == -1. * If current_child == desired_child, then the mux is already set to the * correct bus.
*/ staticint mdio_mux_syscon_switch_fn(int current_child, int desired_child, void *data)
{ struct stmmac_priv *priv = data; struct sunxi_priv_data *gmac = priv->plat->bsp_priv;
u32 reg, val; int ret = 0;
if (current_child ^ desired_child) {
regmap_field_read(gmac->regmap_field, ®); switch (desired_child) { case DWMAC_SUN8I_MDIO_MUX_INTERNAL_ID:
dev_info(priv->device, "Switch mux to internal PHY");
val = (reg & ~H3_EPHY_MUX_MASK) | H3_EPHY_SELECT;
gmac->use_internal_phy = true; break; case DWMAC_SUN8I_MDIO_MUX_EXTERNAL_ID:
dev_info(priv->device, "Switch mux to external PHY");
val = (reg & ~H3_EPHY_MUX_MASK) | H3_EPHY_SHUTDOWN;
gmac->use_internal_phy = false; break; default:
dev_err(priv->device, "Invalid child ID %x\n",
desired_child); return -EINVAL;
}
regmap_field_write(gmac->regmap_field, val); if (gmac->use_internal_phy) {
ret = sun8i_dwmac_power_internal_phy(priv); if (ret) return ret;
} else {
sun8i_dwmac_unpower_internal_phy(gmac);
} /* After changing syscon value, the MAC need reset or it will * use the last value (and so the last PHY set).
*/
ret = sun8i_dwmac_reset(priv);
} return ret;
}
ret = regmap_field_read(gmac->regmap_field, &val); if (ret) {
dev_err(dev, "Fail to read from regmap field.\n"); return ret;
}
reg = gmac->variant->default_syscon_value; if (reg != val)
dev_warn(dev, "Current syscon value is not the default %x (expect %x)\n",
val, reg);
if (gmac->variant->soc_has_internal_phy) { if (of_property_read_bool(node, "allwinner,leds-active-low"))
reg |= H3_EPHY_LED_POL; else
reg &= ~H3_EPHY_LED_POL;
/* Force EPHY xtal frequency to 24MHz. */
reg |= H3_EPHY_CLK_SEL;
ret = of_mdio_parse_addr(dev, plat->phy_node); if (ret < 0) {
dev_err(dev, "Could not parse MDIO addr\n"); return ret;
} /* of_mdio_parse_addr returns a valid (0 ~ 31) PHY * address. No need to mask it again.
*/
reg |= ret << H3_EPHY_ADDR_SHIFT;
} else { /* For SoCs without internal PHY the PHY selection bit should be * set to 0 (external PHY).
*/
reg &= ~H3_EPHY_SELECT;
}
if (!of_property_read_u32(node, "allwinner,tx-delay-ps", &val)) { if (val % 100) {
dev_err(dev, "tx-delay must be a multiple of 100\n"); return -EINVAL;
}
val /= 100;
dev_dbg(dev, "set tx-delay to %x\n", val); if (val <= gmac->variant->tx_delay_max) {
reg &= ~(gmac->variant->tx_delay_max <<
SYSCON_ETXDC_SHIFT);
reg |= (val << SYSCON_ETXDC_SHIFT);
} else {
dev_err(dev, "Invalid TX clock delay: %d\n",
val); return -EINVAL;
}
}
if (!of_property_read_u32(node, "allwinner,rx-delay-ps", &val)) { if (val % 100) {
dev_err(dev, "rx-delay must be a multiple of 100\n"); return -EINVAL;
}
val /= 100;
dev_dbg(dev, "set rx-delay to %x\n", val); if (val <= gmac->variant->rx_delay_max) {
reg &= ~(gmac->variant->rx_delay_max <<
SYSCON_ERXDC_SHIFT);
reg |= (val << SYSCON_ERXDC_SHIFT);
} else {
dev_err(dev, "Invalid RX clock delay: %d\n",
val); return -EINVAL;
}
}
syscon_node = of_parse_phandle(node, "syscon", 0); if (!syscon_node) return ERR_PTR(-ENODEV);
syscon_pdev = of_find_device_by_node(syscon_node); if (!syscon_pdev) { /* platform device might not be probed yet */
regmap = ERR_PTR(-EPROBE_DEFER); goto out_put_node;
}
/* If no regmap is found then the other device driver is at fault */
regmap = dev_get_regmap(&syscon_pdev->dev, NULL); if (!regmap)
regmap = ERR_PTR(-EINVAL);
/* Optional regulator for PHY */
gmac->regulator = devm_regulator_get_optional(dev, "phy"); if (IS_ERR(gmac->regulator)) { if (PTR_ERR(gmac->regulator) == -EPROBE_DEFER) return -EPROBE_DEFER;
dev_info(dev, "No regulator found\n");
gmac->regulator = NULL;
}
/* The "GMAC clock control" register might be located in the * CCU address range (on the R40), or the system control address * range (on most other sun8i and later SoCs). * * The former controls most if not all clocks in the SoC. The * latter has an SoC identification register, and on some SoCs, * controls to map device specific SRAM to either the intended * peripheral, or the CPU address space. * * In either case, there should be a coordinated and restricted * method of accessing the register needed here. This is done by * having the device export a custom regmap, instead of a generic * syscon, which grants all access to all registers. * * To support old device trees, we fall back to using the syscon * interface if possible.
*/
regmap = sun8i_dwmac_get_syscon_from_dev(pdev->dev.of_node); if (IS_ERR(regmap))
regmap = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "syscon"); if (IS_ERR(regmap)) {
ret = PTR_ERR(regmap);
dev_err(&pdev->dev, "Unable to map syscon: %d\n", ret); return ret;
}
gmac->regmap_field = devm_regmap_field_alloc(dev, regmap,
*gmac->variant->syscon_field); if (IS_ERR(gmac->regmap_field)) {
ret = PTR_ERR(gmac->regmap_field);
dev_err(dev, "Unable to map syscon register: %d\n", ret); return ret;
}
plat_dat = devm_stmmac_probe_config_dt(pdev, stmmac_res.mac); if (IS_ERR(plat_dat)) return PTR_ERR(plat_dat);
/* platform data specifying hardware features and callbacks. * hardware features were copied from Allwinner drivers.
*/
plat_dat->rx_coe = STMMAC_RX_COE_TYPE2;
plat_dat->tx_coe = 1;
plat_dat->flags |= STMMAC_FLAG_HAS_SUN8I;
plat_dat->bsp_priv = gmac;
plat_dat->init = sun8i_dwmac_init;
plat_dat->exit = sun8i_dwmac_exit;
plat_dat->setup = sun8i_dwmac_setup;
plat_dat->tx_fifo_size = 4096;
plat_dat->rx_fifo_size = 16384;
ret = sun8i_dwmac_set_syscon(&pdev->dev, plat_dat); if (ret) return ret;
ret = stmmac_pltfr_probe(pdev, plat_dat, &stmmac_res); if (ret) goto dwmac_syscon;
/* the MAC is runtime suspended after stmmac_dvr_probe(), so we * need to ensure the MAC resume back before other operations such * as reset.
*/
pm_runtime_get_sync(&pdev->dev);
/* The mux must be registered after parent MDIO * so after stmmac_dvr_probe()
*/ if (gmac->variant->soc_has_internal_phy) {
ret = get_ephy_nodes(priv); if (ret) goto dwmac_remove;
ret = sun8i_dwmac_register_mdio_mux(priv); if (ret) {
dev_err(&pdev->dev, "Failed to register mux\n"); goto dwmac_mux;
}
} else {
ret = sun8i_dwmac_reset(priv); if (ret) goto dwmac_remove;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.