18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * MDIO bus driver for the Xilinx Axi Ethernet device
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2009 Secret Lab Technologies, Ltd.
68c2ecf20Sopenharmony_ci * Copyright (c) 2010 - 2011 Michal Simek <monstr@monstr.eu>
78c2ecf20Sopenharmony_ci * Copyright (c) 2010 - 2011 PetaLogix
88c2ecf20Sopenharmony_ci * Copyright (c) 2019 SED Systems, a division of Calian Ltd.
98c2ecf20Sopenharmony_ci * Copyright (c) 2010 - 2012 Xilinx, Inc. All rights reserved.
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/clk.h>
138c2ecf20Sopenharmony_ci#include <linux/of_address.h>
148c2ecf20Sopenharmony_ci#include <linux/of_mdio.h>
158c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
168c2ecf20Sopenharmony_ci#include <linux/iopoll.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include "xilinx_axienet.h"
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#define MAX_MDIO_FREQ		2500000 /* 2.5 MHz */
218c2ecf20Sopenharmony_ci#define DEFAULT_HOST_CLOCK	150000000 /* 150 MHz */
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci/* Wait till MDIO interface is ready to accept a new transaction.*/
248c2ecf20Sopenharmony_cistatic int axienet_mdio_wait_until_ready(struct axienet_local *lp)
258c2ecf20Sopenharmony_ci{
268c2ecf20Sopenharmony_ci	u32 val;
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	return readx_poll_timeout(axinet_ior_read_mcr, lp,
298c2ecf20Sopenharmony_ci				  val, val & XAE_MDIO_MCR_READY_MASK,
308c2ecf20Sopenharmony_ci				  1, 20000);
318c2ecf20Sopenharmony_ci}
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/**
348c2ecf20Sopenharmony_ci * axienet_mdio_read - MDIO interface read function
358c2ecf20Sopenharmony_ci * @bus:	Pointer to mii bus structure
368c2ecf20Sopenharmony_ci * @phy_id:	Address of the PHY device
378c2ecf20Sopenharmony_ci * @reg:	PHY register to read
388c2ecf20Sopenharmony_ci *
398c2ecf20Sopenharmony_ci * Return:	The register contents on success, -ETIMEDOUT on a timeout
408c2ecf20Sopenharmony_ci *
418c2ecf20Sopenharmony_ci * Reads the contents of the requested register from the requested PHY
428c2ecf20Sopenharmony_ci * address by first writing the details into MCR register. After a while
438c2ecf20Sopenharmony_ci * the register MRD is read to obtain the PHY register content.
448c2ecf20Sopenharmony_ci */
458c2ecf20Sopenharmony_cistatic int axienet_mdio_read(struct mii_bus *bus, int phy_id, int reg)
468c2ecf20Sopenharmony_ci{
478c2ecf20Sopenharmony_ci	u32 rc;
488c2ecf20Sopenharmony_ci	int ret;
498c2ecf20Sopenharmony_ci	struct axienet_local *lp = bus->priv;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	ret = axienet_mdio_wait_until_ready(lp);
528c2ecf20Sopenharmony_ci	if (ret < 0)
538c2ecf20Sopenharmony_ci		return ret;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	axienet_iow(lp, XAE_MDIO_MCR_OFFSET,
568c2ecf20Sopenharmony_ci		    (((phy_id << XAE_MDIO_MCR_PHYAD_SHIFT) &
578c2ecf20Sopenharmony_ci		      XAE_MDIO_MCR_PHYAD_MASK) |
588c2ecf20Sopenharmony_ci		     ((reg << XAE_MDIO_MCR_REGAD_SHIFT) &
598c2ecf20Sopenharmony_ci		      XAE_MDIO_MCR_REGAD_MASK) |
608c2ecf20Sopenharmony_ci		     XAE_MDIO_MCR_INITIATE_MASK |
618c2ecf20Sopenharmony_ci		     XAE_MDIO_MCR_OP_READ_MASK));
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	ret = axienet_mdio_wait_until_ready(lp);
648c2ecf20Sopenharmony_ci	if (ret < 0)
658c2ecf20Sopenharmony_ci		return ret;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	rc = axienet_ior(lp, XAE_MDIO_MRD_OFFSET) & 0x0000FFFF;
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	dev_dbg(lp->dev, "axienet_mdio_read(phy_id=%i, reg=%x) == %x\n",
708c2ecf20Sopenharmony_ci		phy_id, reg, rc);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	return rc;
738c2ecf20Sopenharmony_ci}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci/**
768c2ecf20Sopenharmony_ci * axienet_mdio_write - MDIO interface write function
778c2ecf20Sopenharmony_ci * @bus:	Pointer to mii bus structure
788c2ecf20Sopenharmony_ci * @phy_id:	Address of the PHY device
798c2ecf20Sopenharmony_ci * @reg:	PHY register to write to
808c2ecf20Sopenharmony_ci * @val:	Value to be written into the register
818c2ecf20Sopenharmony_ci *
828c2ecf20Sopenharmony_ci * Return:	0 on success, -ETIMEDOUT on a timeout
838c2ecf20Sopenharmony_ci *
848c2ecf20Sopenharmony_ci * Writes the value to the requested register by first writing the value
858c2ecf20Sopenharmony_ci * into MWD register. The the MCR register is then appropriately setup
868c2ecf20Sopenharmony_ci * to finish the write operation.
878c2ecf20Sopenharmony_ci */
888c2ecf20Sopenharmony_cistatic int axienet_mdio_write(struct mii_bus *bus, int phy_id, int reg,
898c2ecf20Sopenharmony_ci			      u16 val)
908c2ecf20Sopenharmony_ci{
918c2ecf20Sopenharmony_ci	int ret;
928c2ecf20Sopenharmony_ci	struct axienet_local *lp = bus->priv;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	dev_dbg(lp->dev, "axienet_mdio_write(phy_id=%i, reg=%x, val=%x)\n",
958c2ecf20Sopenharmony_ci		phy_id, reg, val);
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	ret = axienet_mdio_wait_until_ready(lp);
988c2ecf20Sopenharmony_ci	if (ret < 0)
998c2ecf20Sopenharmony_ci		return ret;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	axienet_iow(lp, XAE_MDIO_MWD_OFFSET, (u32) val);
1028c2ecf20Sopenharmony_ci	axienet_iow(lp, XAE_MDIO_MCR_OFFSET,
1038c2ecf20Sopenharmony_ci		    (((phy_id << XAE_MDIO_MCR_PHYAD_SHIFT) &
1048c2ecf20Sopenharmony_ci		      XAE_MDIO_MCR_PHYAD_MASK) |
1058c2ecf20Sopenharmony_ci		     ((reg << XAE_MDIO_MCR_REGAD_SHIFT) &
1068c2ecf20Sopenharmony_ci		      XAE_MDIO_MCR_REGAD_MASK) |
1078c2ecf20Sopenharmony_ci		     XAE_MDIO_MCR_INITIATE_MASK |
1088c2ecf20Sopenharmony_ci		     XAE_MDIO_MCR_OP_WRITE_MASK));
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	ret = axienet_mdio_wait_until_ready(lp);
1118c2ecf20Sopenharmony_ci	if (ret < 0)
1128c2ecf20Sopenharmony_ci		return ret;
1138c2ecf20Sopenharmony_ci	return 0;
1148c2ecf20Sopenharmony_ci}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci/**
1178c2ecf20Sopenharmony_ci * axienet_mdio_enable - MDIO hardware setup function
1188c2ecf20Sopenharmony_ci * @lp:		Pointer to axienet local data structure.
1198c2ecf20Sopenharmony_ci *
1208c2ecf20Sopenharmony_ci * Return:	0 on success, -ETIMEDOUT on a timeout.
1218c2ecf20Sopenharmony_ci *
1228c2ecf20Sopenharmony_ci * Sets up the MDIO interface by initializing the MDIO clock and enabling the
1238c2ecf20Sopenharmony_ci * MDIO interface in hardware.
1248c2ecf20Sopenharmony_ci **/
1258c2ecf20Sopenharmony_ciint axienet_mdio_enable(struct axienet_local *lp)
1268c2ecf20Sopenharmony_ci{
1278c2ecf20Sopenharmony_ci	u32 clk_div, host_clock;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	if (lp->clk) {
1308c2ecf20Sopenharmony_ci		host_clock = clk_get_rate(lp->clk);
1318c2ecf20Sopenharmony_ci	} else {
1328c2ecf20Sopenharmony_ci		struct device_node *np1;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci		/* Legacy fallback: detect CPU clock frequency and use as AXI
1358c2ecf20Sopenharmony_ci		 * bus clock frequency. This only works on certain platforms.
1368c2ecf20Sopenharmony_ci		 */
1378c2ecf20Sopenharmony_ci		np1 = of_find_node_by_name(NULL, "cpu");
1388c2ecf20Sopenharmony_ci		if (!np1) {
1398c2ecf20Sopenharmony_ci			netdev_warn(lp->ndev, "Could not find CPU device node.\n");
1408c2ecf20Sopenharmony_ci			host_clock = DEFAULT_HOST_CLOCK;
1418c2ecf20Sopenharmony_ci		} else {
1428c2ecf20Sopenharmony_ci			int ret = of_property_read_u32(np1, "clock-frequency",
1438c2ecf20Sopenharmony_ci						       &host_clock);
1448c2ecf20Sopenharmony_ci			if (ret) {
1458c2ecf20Sopenharmony_ci				netdev_warn(lp->ndev, "CPU clock-frequency property not found.\n");
1468c2ecf20Sopenharmony_ci				host_clock = DEFAULT_HOST_CLOCK;
1478c2ecf20Sopenharmony_ci			}
1488c2ecf20Sopenharmony_ci			of_node_put(np1);
1498c2ecf20Sopenharmony_ci		}
1508c2ecf20Sopenharmony_ci		netdev_info(lp->ndev, "Setting assumed host clock to %u\n",
1518c2ecf20Sopenharmony_ci			    host_clock);
1528c2ecf20Sopenharmony_ci	}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	/* clk_div can be calculated by deriving it from the equation:
1558c2ecf20Sopenharmony_ci	 * fMDIO = fHOST / ((1 + clk_div) * 2)
1568c2ecf20Sopenharmony_ci	 *
1578c2ecf20Sopenharmony_ci	 * Where fMDIO <= 2500000, so we get:
1588c2ecf20Sopenharmony_ci	 * fHOST / ((1 + clk_div) * 2) <= 2500000
1598c2ecf20Sopenharmony_ci	 *
1608c2ecf20Sopenharmony_ci	 * Then we get:
1618c2ecf20Sopenharmony_ci	 * 1 / ((1 + clk_div) * 2) <= (2500000 / fHOST)
1628c2ecf20Sopenharmony_ci	 *
1638c2ecf20Sopenharmony_ci	 * Then we get:
1648c2ecf20Sopenharmony_ci	 * 1 / (1 + clk_div) <= ((2500000 * 2) / fHOST)
1658c2ecf20Sopenharmony_ci	 *
1668c2ecf20Sopenharmony_ci	 * Then we get:
1678c2ecf20Sopenharmony_ci	 * 1 / (1 + clk_div) <= (5000000 / fHOST)
1688c2ecf20Sopenharmony_ci	 *
1698c2ecf20Sopenharmony_ci	 * So:
1708c2ecf20Sopenharmony_ci	 * (1 + clk_div) >= (fHOST / 5000000)
1718c2ecf20Sopenharmony_ci	 *
1728c2ecf20Sopenharmony_ci	 * And finally:
1738c2ecf20Sopenharmony_ci	 * clk_div >= (fHOST / 5000000) - 1
1748c2ecf20Sopenharmony_ci	 *
1758c2ecf20Sopenharmony_ci	 * fHOST can be read from the flattened device tree as property
1768c2ecf20Sopenharmony_ci	 * "clock-frequency" from the CPU
1778c2ecf20Sopenharmony_ci	 */
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	clk_div = (host_clock / (MAX_MDIO_FREQ * 2)) - 1;
1808c2ecf20Sopenharmony_ci	/* If there is any remainder from the division of
1818c2ecf20Sopenharmony_ci	 * fHOST / (MAX_MDIO_FREQ * 2), then we need to add
1828c2ecf20Sopenharmony_ci	 * 1 to the clock divisor or we will surely be above 2.5 MHz
1838c2ecf20Sopenharmony_ci	 */
1848c2ecf20Sopenharmony_ci	if (host_clock % (MAX_MDIO_FREQ * 2))
1858c2ecf20Sopenharmony_ci		clk_div++;
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	netdev_dbg(lp->ndev,
1888c2ecf20Sopenharmony_ci		   "Setting MDIO clock divisor to %u/%u Hz host clock.\n",
1898c2ecf20Sopenharmony_ci		   clk_div, host_clock);
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	axienet_iow(lp, XAE_MDIO_MC_OFFSET, clk_div | XAE_MDIO_MC_MDIOEN_MASK);
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	return axienet_mdio_wait_until_ready(lp);
1948c2ecf20Sopenharmony_ci}
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci/**
1978c2ecf20Sopenharmony_ci * axienet_mdio_disable - MDIO hardware disable function
1988c2ecf20Sopenharmony_ci * @lp:		Pointer to axienet local data structure.
1998c2ecf20Sopenharmony_ci *
2008c2ecf20Sopenharmony_ci * Disable the MDIO interface in hardware.
2018c2ecf20Sopenharmony_ci **/
2028c2ecf20Sopenharmony_civoid axienet_mdio_disable(struct axienet_local *lp)
2038c2ecf20Sopenharmony_ci{
2048c2ecf20Sopenharmony_ci	axienet_iow(lp, XAE_MDIO_MC_OFFSET, 0);
2058c2ecf20Sopenharmony_ci}
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci/**
2088c2ecf20Sopenharmony_ci * axienet_mdio_setup - MDIO setup function
2098c2ecf20Sopenharmony_ci * @lp:		Pointer to axienet local data structure.
2108c2ecf20Sopenharmony_ci *
2118c2ecf20Sopenharmony_ci * Return:	0 on success, -ETIMEDOUT on a timeout, -ENOMEM when
2128c2ecf20Sopenharmony_ci *		mdiobus_alloc (to allocate memory for mii bus structure) fails.
2138c2ecf20Sopenharmony_ci *
2148c2ecf20Sopenharmony_ci * Sets up the MDIO interface by initializing the MDIO clock and enabling the
2158c2ecf20Sopenharmony_ci * MDIO interface in hardware. Register the MDIO interface.
2168c2ecf20Sopenharmony_ci **/
2178c2ecf20Sopenharmony_ciint axienet_mdio_setup(struct axienet_local *lp)
2188c2ecf20Sopenharmony_ci{
2198c2ecf20Sopenharmony_ci	struct device_node *mdio_node;
2208c2ecf20Sopenharmony_ci	struct mii_bus *bus;
2218c2ecf20Sopenharmony_ci	int ret;
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	ret = axienet_mdio_enable(lp);
2248c2ecf20Sopenharmony_ci	if (ret < 0)
2258c2ecf20Sopenharmony_ci		return ret;
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	bus = mdiobus_alloc();
2288c2ecf20Sopenharmony_ci	if (!bus)
2298c2ecf20Sopenharmony_ci		return -ENOMEM;
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	snprintf(bus->id, MII_BUS_ID_SIZE, "axienet-%.8llx",
2328c2ecf20Sopenharmony_ci		 (unsigned long long)lp->regs_start);
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	bus->priv = lp;
2358c2ecf20Sopenharmony_ci	bus->name = "Xilinx Axi Ethernet MDIO";
2368c2ecf20Sopenharmony_ci	bus->read = axienet_mdio_read;
2378c2ecf20Sopenharmony_ci	bus->write = axienet_mdio_write;
2388c2ecf20Sopenharmony_ci	bus->parent = lp->dev;
2398c2ecf20Sopenharmony_ci	lp->mii_bus = bus;
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	mdio_node = of_get_child_by_name(lp->dev->of_node, "mdio");
2428c2ecf20Sopenharmony_ci	ret = of_mdiobus_register(bus, mdio_node);
2438c2ecf20Sopenharmony_ci	of_node_put(mdio_node);
2448c2ecf20Sopenharmony_ci	if (ret) {
2458c2ecf20Sopenharmony_ci		mdiobus_free(bus);
2468c2ecf20Sopenharmony_ci		lp->mii_bus = NULL;
2478c2ecf20Sopenharmony_ci		return ret;
2488c2ecf20Sopenharmony_ci	}
2498c2ecf20Sopenharmony_ci	return 0;
2508c2ecf20Sopenharmony_ci}
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci/**
2538c2ecf20Sopenharmony_ci * axienet_mdio_teardown - MDIO remove function
2548c2ecf20Sopenharmony_ci * @lp:		Pointer to axienet local data structure.
2558c2ecf20Sopenharmony_ci *
2568c2ecf20Sopenharmony_ci * Unregisters the MDIO and frees any associate memory for mii bus.
2578c2ecf20Sopenharmony_ci */
2588c2ecf20Sopenharmony_civoid axienet_mdio_teardown(struct axienet_local *lp)
2598c2ecf20Sopenharmony_ci{
2608c2ecf20Sopenharmony_ci	mdiobus_unregister(lp->mii_bus);
2618c2ecf20Sopenharmony_ci	mdiobus_free(lp->mii_bus);
2628c2ecf20Sopenharmony_ci	lp->mii_bus = NULL;
2638c2ecf20Sopenharmony_ci}
264