162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * MDIO bus driver for the Xilinx Axi Ethernet device 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (c) 2009 Secret Lab Technologies, Ltd. 662306a36Sopenharmony_ci * Copyright (c) 2010 - 2011 Michal Simek <monstr@monstr.eu> 762306a36Sopenharmony_ci * Copyright (c) 2010 - 2011 PetaLogix 862306a36Sopenharmony_ci * Copyright (c) 2019 SED Systems, a division of Calian Ltd. 962306a36Sopenharmony_ci * Copyright (c) 2010 - 2012 Xilinx, Inc. All rights reserved. 1062306a36Sopenharmony_ci */ 1162306a36Sopenharmony_ci 1262306a36Sopenharmony_ci#include <linux/clk.h> 1362306a36Sopenharmony_ci#include <linux/of_address.h> 1462306a36Sopenharmony_ci#include <linux/of_mdio.h> 1562306a36Sopenharmony_ci#include <linux/jiffies.h> 1662306a36Sopenharmony_ci#include <linux/iopoll.h> 1762306a36Sopenharmony_ci 1862306a36Sopenharmony_ci#include "xilinx_axienet.h" 1962306a36Sopenharmony_ci 2062306a36Sopenharmony_ci#define DEFAULT_MDIO_FREQ 2500000 /* 2.5 MHz */ 2162306a36Sopenharmony_ci#define DEFAULT_HOST_CLOCK 150000000 /* 150 MHz */ 2262306a36Sopenharmony_ci 2362306a36Sopenharmony_ci/* Wait till MDIO interface is ready to accept a new transaction.*/ 2462306a36Sopenharmony_cistatic int axienet_mdio_wait_until_ready(struct axienet_local *lp) 2562306a36Sopenharmony_ci{ 2662306a36Sopenharmony_ci u32 val; 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_ci return readx_poll_timeout(axinet_ior_read_mcr, lp, 2962306a36Sopenharmony_ci val, val & XAE_MDIO_MCR_READY_MASK, 3062306a36Sopenharmony_ci 1, 20000); 3162306a36Sopenharmony_ci} 3262306a36Sopenharmony_ci 3362306a36Sopenharmony_ci/* Enable the MDIO MDC. Called prior to a read/write operation */ 3462306a36Sopenharmony_cistatic void axienet_mdio_mdc_enable(struct axienet_local *lp) 3562306a36Sopenharmony_ci{ 3662306a36Sopenharmony_ci axienet_iow(lp, XAE_MDIO_MC_OFFSET, 3762306a36Sopenharmony_ci ((u32)lp->mii_clk_div | XAE_MDIO_MC_MDIOEN_MASK)); 3862306a36Sopenharmony_ci} 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_ci/* Disable the MDIO MDC. Called after a read/write operation*/ 4162306a36Sopenharmony_cistatic void axienet_mdio_mdc_disable(struct axienet_local *lp) 4262306a36Sopenharmony_ci{ 4362306a36Sopenharmony_ci u32 mc_reg; 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_ci mc_reg = axienet_ior(lp, XAE_MDIO_MC_OFFSET); 4662306a36Sopenharmony_ci axienet_iow(lp, XAE_MDIO_MC_OFFSET, 4762306a36Sopenharmony_ci (mc_reg & ~XAE_MDIO_MC_MDIOEN_MASK)); 4862306a36Sopenharmony_ci} 4962306a36Sopenharmony_ci 5062306a36Sopenharmony_ci/** 5162306a36Sopenharmony_ci * axienet_mdio_read - MDIO interface read function 5262306a36Sopenharmony_ci * @bus: Pointer to mii bus structure 5362306a36Sopenharmony_ci * @phy_id: Address of the PHY device 5462306a36Sopenharmony_ci * @reg: PHY register to read 5562306a36Sopenharmony_ci * 5662306a36Sopenharmony_ci * Return: The register contents on success, -ETIMEDOUT on a timeout 5762306a36Sopenharmony_ci * 5862306a36Sopenharmony_ci * Reads the contents of the requested register from the requested PHY 5962306a36Sopenharmony_ci * address by first writing the details into MCR register. After a while 6062306a36Sopenharmony_ci * the register MRD is read to obtain the PHY register content. 6162306a36Sopenharmony_ci */ 6262306a36Sopenharmony_cistatic int axienet_mdio_read(struct mii_bus *bus, int phy_id, int reg) 6362306a36Sopenharmony_ci{ 6462306a36Sopenharmony_ci u32 rc; 6562306a36Sopenharmony_ci int ret; 6662306a36Sopenharmony_ci struct axienet_local *lp = bus->priv; 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ci axienet_mdio_mdc_enable(lp); 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_ci ret = axienet_mdio_wait_until_ready(lp); 7162306a36Sopenharmony_ci if (ret < 0) { 7262306a36Sopenharmony_ci axienet_mdio_mdc_disable(lp); 7362306a36Sopenharmony_ci return ret; 7462306a36Sopenharmony_ci } 7562306a36Sopenharmony_ci 7662306a36Sopenharmony_ci axienet_iow(lp, XAE_MDIO_MCR_OFFSET, 7762306a36Sopenharmony_ci (((phy_id << XAE_MDIO_MCR_PHYAD_SHIFT) & 7862306a36Sopenharmony_ci XAE_MDIO_MCR_PHYAD_MASK) | 7962306a36Sopenharmony_ci ((reg << XAE_MDIO_MCR_REGAD_SHIFT) & 8062306a36Sopenharmony_ci XAE_MDIO_MCR_REGAD_MASK) | 8162306a36Sopenharmony_ci XAE_MDIO_MCR_INITIATE_MASK | 8262306a36Sopenharmony_ci XAE_MDIO_MCR_OP_READ_MASK)); 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ci ret = axienet_mdio_wait_until_ready(lp); 8562306a36Sopenharmony_ci if (ret < 0) { 8662306a36Sopenharmony_ci axienet_mdio_mdc_disable(lp); 8762306a36Sopenharmony_ci return ret; 8862306a36Sopenharmony_ci } 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ci rc = axienet_ior(lp, XAE_MDIO_MRD_OFFSET) & 0x0000FFFF; 9162306a36Sopenharmony_ci 9262306a36Sopenharmony_ci dev_dbg(lp->dev, "axienet_mdio_read(phy_id=%i, reg=%x) == %x\n", 9362306a36Sopenharmony_ci phy_id, reg, rc); 9462306a36Sopenharmony_ci 9562306a36Sopenharmony_ci axienet_mdio_mdc_disable(lp); 9662306a36Sopenharmony_ci return rc; 9762306a36Sopenharmony_ci} 9862306a36Sopenharmony_ci 9962306a36Sopenharmony_ci/** 10062306a36Sopenharmony_ci * axienet_mdio_write - MDIO interface write function 10162306a36Sopenharmony_ci * @bus: Pointer to mii bus structure 10262306a36Sopenharmony_ci * @phy_id: Address of the PHY device 10362306a36Sopenharmony_ci * @reg: PHY register to write to 10462306a36Sopenharmony_ci * @val: Value to be written into the register 10562306a36Sopenharmony_ci * 10662306a36Sopenharmony_ci * Return: 0 on success, -ETIMEDOUT on a timeout 10762306a36Sopenharmony_ci * 10862306a36Sopenharmony_ci * Writes the value to the requested register by first writing the value 10962306a36Sopenharmony_ci * into MWD register. The MCR register is then appropriately setup 11062306a36Sopenharmony_ci * to finish the write operation. 11162306a36Sopenharmony_ci */ 11262306a36Sopenharmony_cistatic int axienet_mdio_write(struct mii_bus *bus, int phy_id, int reg, 11362306a36Sopenharmony_ci u16 val) 11462306a36Sopenharmony_ci{ 11562306a36Sopenharmony_ci int ret; 11662306a36Sopenharmony_ci struct axienet_local *lp = bus->priv; 11762306a36Sopenharmony_ci 11862306a36Sopenharmony_ci dev_dbg(lp->dev, "axienet_mdio_write(phy_id=%i, reg=%x, val=%x)\n", 11962306a36Sopenharmony_ci phy_id, reg, val); 12062306a36Sopenharmony_ci 12162306a36Sopenharmony_ci axienet_mdio_mdc_enable(lp); 12262306a36Sopenharmony_ci 12362306a36Sopenharmony_ci ret = axienet_mdio_wait_until_ready(lp); 12462306a36Sopenharmony_ci if (ret < 0) { 12562306a36Sopenharmony_ci axienet_mdio_mdc_disable(lp); 12662306a36Sopenharmony_ci return ret; 12762306a36Sopenharmony_ci } 12862306a36Sopenharmony_ci 12962306a36Sopenharmony_ci axienet_iow(lp, XAE_MDIO_MWD_OFFSET, (u32)val); 13062306a36Sopenharmony_ci axienet_iow(lp, XAE_MDIO_MCR_OFFSET, 13162306a36Sopenharmony_ci (((phy_id << XAE_MDIO_MCR_PHYAD_SHIFT) & 13262306a36Sopenharmony_ci XAE_MDIO_MCR_PHYAD_MASK) | 13362306a36Sopenharmony_ci ((reg << XAE_MDIO_MCR_REGAD_SHIFT) & 13462306a36Sopenharmony_ci XAE_MDIO_MCR_REGAD_MASK) | 13562306a36Sopenharmony_ci XAE_MDIO_MCR_INITIATE_MASK | 13662306a36Sopenharmony_ci XAE_MDIO_MCR_OP_WRITE_MASK)); 13762306a36Sopenharmony_ci 13862306a36Sopenharmony_ci ret = axienet_mdio_wait_until_ready(lp); 13962306a36Sopenharmony_ci if (ret < 0) { 14062306a36Sopenharmony_ci axienet_mdio_mdc_disable(lp); 14162306a36Sopenharmony_ci return ret; 14262306a36Sopenharmony_ci } 14362306a36Sopenharmony_ci axienet_mdio_mdc_disable(lp); 14462306a36Sopenharmony_ci return 0; 14562306a36Sopenharmony_ci} 14662306a36Sopenharmony_ci 14762306a36Sopenharmony_ci/** 14862306a36Sopenharmony_ci * axienet_mdio_enable - MDIO hardware setup function 14962306a36Sopenharmony_ci * @lp: Pointer to axienet local data structure. 15062306a36Sopenharmony_ci * @np: Pointer to mdio device tree node. 15162306a36Sopenharmony_ci * 15262306a36Sopenharmony_ci * Return: 0 on success, -ETIMEDOUT on a timeout, -EOVERFLOW on a clock 15362306a36Sopenharmony_ci * divisor overflow. 15462306a36Sopenharmony_ci * 15562306a36Sopenharmony_ci * Sets up the MDIO interface by initializing the MDIO clock and enabling the 15662306a36Sopenharmony_ci * MDIO interface in hardware. 15762306a36Sopenharmony_ci **/ 15862306a36Sopenharmony_cistatic int axienet_mdio_enable(struct axienet_local *lp, struct device_node *np) 15962306a36Sopenharmony_ci{ 16062306a36Sopenharmony_ci u32 mdio_freq = DEFAULT_MDIO_FREQ; 16162306a36Sopenharmony_ci u32 host_clock; 16262306a36Sopenharmony_ci u32 clk_div; 16362306a36Sopenharmony_ci int ret; 16462306a36Sopenharmony_ci 16562306a36Sopenharmony_ci lp->mii_clk_div = 0; 16662306a36Sopenharmony_ci 16762306a36Sopenharmony_ci if (lp->axi_clk) { 16862306a36Sopenharmony_ci host_clock = clk_get_rate(lp->axi_clk); 16962306a36Sopenharmony_ci } else { 17062306a36Sopenharmony_ci struct device_node *np1; 17162306a36Sopenharmony_ci 17262306a36Sopenharmony_ci /* Legacy fallback: detect CPU clock frequency and use as AXI 17362306a36Sopenharmony_ci * bus clock frequency. This only works on certain platforms. 17462306a36Sopenharmony_ci */ 17562306a36Sopenharmony_ci np1 = of_find_node_by_name(NULL, "cpu"); 17662306a36Sopenharmony_ci if (!np1) { 17762306a36Sopenharmony_ci netdev_warn(lp->ndev, "Could not find CPU device node.\n"); 17862306a36Sopenharmony_ci host_clock = DEFAULT_HOST_CLOCK; 17962306a36Sopenharmony_ci } else { 18062306a36Sopenharmony_ci int ret = of_property_read_u32(np1, "clock-frequency", 18162306a36Sopenharmony_ci &host_clock); 18262306a36Sopenharmony_ci if (ret) { 18362306a36Sopenharmony_ci netdev_warn(lp->ndev, "CPU clock-frequency property not found.\n"); 18462306a36Sopenharmony_ci host_clock = DEFAULT_HOST_CLOCK; 18562306a36Sopenharmony_ci } 18662306a36Sopenharmony_ci of_node_put(np1); 18762306a36Sopenharmony_ci } 18862306a36Sopenharmony_ci netdev_info(lp->ndev, "Setting assumed host clock to %u\n", 18962306a36Sopenharmony_ci host_clock); 19062306a36Sopenharmony_ci } 19162306a36Sopenharmony_ci 19262306a36Sopenharmony_ci if (np) 19362306a36Sopenharmony_ci of_property_read_u32(np, "clock-frequency", &mdio_freq); 19462306a36Sopenharmony_ci if (mdio_freq != DEFAULT_MDIO_FREQ) 19562306a36Sopenharmony_ci netdev_info(lp->ndev, "Setting non-standard mdio bus frequency to %u Hz\n", 19662306a36Sopenharmony_ci mdio_freq); 19762306a36Sopenharmony_ci 19862306a36Sopenharmony_ci /* clk_div can be calculated by deriving it from the equation: 19962306a36Sopenharmony_ci * fMDIO = fHOST / ((1 + clk_div) * 2) 20062306a36Sopenharmony_ci * 20162306a36Sopenharmony_ci * Where fMDIO <= 2500000, so we get: 20262306a36Sopenharmony_ci * fHOST / ((1 + clk_div) * 2) <= 2500000 20362306a36Sopenharmony_ci * 20462306a36Sopenharmony_ci * Then we get: 20562306a36Sopenharmony_ci * 1 / ((1 + clk_div) * 2) <= (2500000 / fHOST) 20662306a36Sopenharmony_ci * 20762306a36Sopenharmony_ci * Then we get: 20862306a36Sopenharmony_ci * 1 / (1 + clk_div) <= ((2500000 * 2) / fHOST) 20962306a36Sopenharmony_ci * 21062306a36Sopenharmony_ci * Then we get: 21162306a36Sopenharmony_ci * 1 / (1 + clk_div) <= (5000000 / fHOST) 21262306a36Sopenharmony_ci * 21362306a36Sopenharmony_ci * So: 21462306a36Sopenharmony_ci * (1 + clk_div) >= (fHOST / 5000000) 21562306a36Sopenharmony_ci * 21662306a36Sopenharmony_ci * And finally: 21762306a36Sopenharmony_ci * clk_div >= (fHOST / 5000000) - 1 21862306a36Sopenharmony_ci * 21962306a36Sopenharmony_ci * fHOST can be read from the flattened device tree as property 22062306a36Sopenharmony_ci * "clock-frequency" from the CPU 22162306a36Sopenharmony_ci */ 22262306a36Sopenharmony_ci 22362306a36Sopenharmony_ci clk_div = (host_clock / (mdio_freq * 2)) - 1; 22462306a36Sopenharmony_ci /* If there is any remainder from the division of 22562306a36Sopenharmony_ci * fHOST / (mdio_freq * 2), then we need to add 22662306a36Sopenharmony_ci * 1 to the clock divisor or we will surely be 22762306a36Sopenharmony_ci * above the requested frequency 22862306a36Sopenharmony_ci */ 22962306a36Sopenharmony_ci if (host_clock % (mdio_freq * 2)) 23062306a36Sopenharmony_ci clk_div++; 23162306a36Sopenharmony_ci 23262306a36Sopenharmony_ci /* Check for overflow of mii_clk_div */ 23362306a36Sopenharmony_ci if (clk_div & ~XAE_MDIO_MC_CLOCK_DIVIDE_MAX) { 23462306a36Sopenharmony_ci netdev_warn(lp->ndev, "MDIO clock divisor overflow\n"); 23562306a36Sopenharmony_ci return -EOVERFLOW; 23662306a36Sopenharmony_ci } 23762306a36Sopenharmony_ci lp->mii_clk_div = (u8)clk_div; 23862306a36Sopenharmony_ci 23962306a36Sopenharmony_ci netdev_dbg(lp->ndev, 24062306a36Sopenharmony_ci "Setting MDIO clock divisor to %u/%u Hz host clock.\n", 24162306a36Sopenharmony_ci lp->mii_clk_div, host_clock); 24262306a36Sopenharmony_ci 24362306a36Sopenharmony_ci axienet_mdio_mdc_enable(lp); 24462306a36Sopenharmony_ci 24562306a36Sopenharmony_ci ret = axienet_mdio_wait_until_ready(lp); 24662306a36Sopenharmony_ci if (ret) 24762306a36Sopenharmony_ci axienet_mdio_mdc_disable(lp); 24862306a36Sopenharmony_ci 24962306a36Sopenharmony_ci return ret; 25062306a36Sopenharmony_ci} 25162306a36Sopenharmony_ci 25262306a36Sopenharmony_ci/** 25362306a36Sopenharmony_ci * axienet_mdio_setup - MDIO setup function 25462306a36Sopenharmony_ci * @lp: Pointer to axienet local data structure. 25562306a36Sopenharmony_ci * 25662306a36Sopenharmony_ci * Return: 0 on success, -ETIMEDOUT on a timeout, -EOVERFLOW on a clock 25762306a36Sopenharmony_ci * divisor overflow, -ENOMEM when mdiobus_alloc (to allocate 25862306a36Sopenharmony_ci * memory for mii bus structure) fails. 25962306a36Sopenharmony_ci * 26062306a36Sopenharmony_ci * Sets up the MDIO interface by initializing the MDIO clock. 26162306a36Sopenharmony_ci * Register the MDIO interface. 26262306a36Sopenharmony_ci **/ 26362306a36Sopenharmony_ciint axienet_mdio_setup(struct axienet_local *lp) 26462306a36Sopenharmony_ci{ 26562306a36Sopenharmony_ci struct device_node *mdio_node; 26662306a36Sopenharmony_ci struct mii_bus *bus; 26762306a36Sopenharmony_ci int ret; 26862306a36Sopenharmony_ci 26962306a36Sopenharmony_ci bus = mdiobus_alloc(); 27062306a36Sopenharmony_ci if (!bus) 27162306a36Sopenharmony_ci return -ENOMEM; 27262306a36Sopenharmony_ci 27362306a36Sopenharmony_ci snprintf(bus->id, MII_BUS_ID_SIZE, "axienet-%.8llx", 27462306a36Sopenharmony_ci (unsigned long long)lp->regs_start); 27562306a36Sopenharmony_ci 27662306a36Sopenharmony_ci bus->priv = lp; 27762306a36Sopenharmony_ci bus->name = "Xilinx Axi Ethernet MDIO"; 27862306a36Sopenharmony_ci bus->read = axienet_mdio_read; 27962306a36Sopenharmony_ci bus->write = axienet_mdio_write; 28062306a36Sopenharmony_ci bus->parent = lp->dev; 28162306a36Sopenharmony_ci lp->mii_bus = bus; 28262306a36Sopenharmony_ci 28362306a36Sopenharmony_ci mdio_node = of_get_child_by_name(lp->dev->of_node, "mdio"); 28462306a36Sopenharmony_ci ret = axienet_mdio_enable(lp, mdio_node); 28562306a36Sopenharmony_ci if (ret < 0) 28662306a36Sopenharmony_ci goto unregister; 28762306a36Sopenharmony_ci ret = of_mdiobus_register(bus, mdio_node); 28862306a36Sopenharmony_ci if (ret) 28962306a36Sopenharmony_ci goto unregister_mdio_enabled; 29062306a36Sopenharmony_ci of_node_put(mdio_node); 29162306a36Sopenharmony_ci axienet_mdio_mdc_disable(lp); 29262306a36Sopenharmony_ci return 0; 29362306a36Sopenharmony_ci 29462306a36Sopenharmony_ciunregister_mdio_enabled: 29562306a36Sopenharmony_ci axienet_mdio_mdc_disable(lp); 29662306a36Sopenharmony_ciunregister: 29762306a36Sopenharmony_ci of_node_put(mdio_node); 29862306a36Sopenharmony_ci mdiobus_free(bus); 29962306a36Sopenharmony_ci lp->mii_bus = NULL; 30062306a36Sopenharmony_ci return ret; 30162306a36Sopenharmony_ci} 30262306a36Sopenharmony_ci 30362306a36Sopenharmony_ci/** 30462306a36Sopenharmony_ci * axienet_mdio_teardown - MDIO remove function 30562306a36Sopenharmony_ci * @lp: Pointer to axienet local data structure. 30662306a36Sopenharmony_ci * 30762306a36Sopenharmony_ci * Unregisters the MDIO and frees any associate memory for mii bus. 30862306a36Sopenharmony_ci */ 30962306a36Sopenharmony_civoid axienet_mdio_teardown(struct axienet_local *lp) 31062306a36Sopenharmony_ci{ 31162306a36Sopenharmony_ci mdiobus_unregister(lp->mii_bus); 31262306a36Sopenharmony_ci mdiobus_free(lp->mii_bus); 31362306a36Sopenharmony_ci lp->mii_bus = NULL; 31462306a36Sopenharmony_ci} 315