1 /* Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
13 /* Qualcomm Technologies, Inc. EMAC PHY Controller driver.
16 #include <linux/of_mdio.h>
17 #include <linux/phy.h>
18 #include <linux/iopoll.h>
19 #include <linux/acpi.h>
22 /* EMAC base register offsets */
23 #define EMAC_MDIO_CTRL 0x001414
24 #define EMAC_PHY_STS 0x001418
25 #define EMAC_MDIO_EX_CTRL 0x001440
28 #define MDIO_MODE BIT(30)
29 #define MDIO_PR BIT(29)
30 #define MDIO_AP_EN BIT(28)
31 #define MDIO_BUSY BIT(27)
32 #define MDIO_CLK_SEL_BMSK 0x7000000
33 #define MDIO_CLK_SEL_SHFT 24
34 #define MDIO_START BIT(23)
35 #define SUP_PREAMBLE BIT(22)
36 #define MDIO_RD_NWR BIT(21)
37 #define MDIO_REG_ADDR_BMSK 0x1f0000
38 #define MDIO_REG_ADDR_SHFT 16
39 #define MDIO_DATA_BMSK 0xffff
40 #define MDIO_DATA_SHFT 0
43 #define PHY_ADDR_BMSK 0x1f0000
44 #define PHY_ADDR_SHFT 16
46 #define MDIO_CLK_25_4 0
47 #define MDIO_CLK_25_28 7
49 #define MDIO_WAIT_TIMES 1000
51 static int emac_mdio_read(struct mii_bus *bus, int addr, int regnum)
53 struct emac_adapter *adpt = bus->priv;
56 emac_reg_update32(adpt->base + EMAC_PHY_STS, PHY_ADDR_BMSK,
57 (addr << PHY_ADDR_SHFT));
60 ((MDIO_CLK_25_4 << MDIO_CLK_SEL_SHFT) & MDIO_CLK_SEL_BMSK) |
61 ((regnum << MDIO_REG_ADDR_SHFT) & MDIO_REG_ADDR_BMSK) |
62 MDIO_START | MDIO_RD_NWR;
64 writel(reg, adpt->base + EMAC_MDIO_CTRL);
66 if (readl_poll_timeout(adpt->base + EMAC_MDIO_CTRL, reg,
67 !(reg & (MDIO_START | MDIO_BUSY)),
68 100, MDIO_WAIT_TIMES * 100))
71 return (reg >> MDIO_DATA_SHFT) & MDIO_DATA_BMSK;
74 static int emac_mdio_write(struct mii_bus *bus, int addr, int regnum, u16 val)
76 struct emac_adapter *adpt = bus->priv;
79 emac_reg_update32(adpt->base + EMAC_PHY_STS, PHY_ADDR_BMSK,
80 (addr << PHY_ADDR_SHFT));
83 ((MDIO_CLK_25_4 << MDIO_CLK_SEL_SHFT) & MDIO_CLK_SEL_BMSK) |
84 ((regnum << MDIO_REG_ADDR_SHFT) & MDIO_REG_ADDR_BMSK) |
85 ((val << MDIO_DATA_SHFT) & MDIO_DATA_BMSK) |
88 writel(reg, adpt->base + EMAC_MDIO_CTRL);
90 if (readl_poll_timeout(adpt->base + EMAC_MDIO_CTRL, reg,
91 !(reg & (MDIO_START | MDIO_BUSY)), 100,
92 MDIO_WAIT_TIMES * 100))
98 /* Configure the MDIO bus and connect the external PHY */
99 int emac_phy_config(struct platform_device *pdev, struct emac_adapter *adpt)
101 struct device_node *np = pdev->dev.of_node;
102 struct mii_bus *mii_bus;
105 /* Create the mii_bus object for talking to the MDIO bus */
106 adpt->mii_bus = mii_bus = devm_mdiobus_alloc(&pdev->dev);
110 mii_bus->name = "emac-mdio";
111 snprintf(mii_bus->id, MII_BUS_ID_SIZE, "%s", pdev->name);
112 mii_bus->read = emac_mdio_read;
113 mii_bus->write = emac_mdio_write;
114 mii_bus->parent = &pdev->dev;
115 mii_bus->priv = adpt;
117 if (has_acpi_companion(&pdev->dev)) {
120 ret = mdiobus_register(mii_bus);
122 dev_err(&pdev->dev, "could not register mdio bus\n");
125 ret = device_property_read_u32(&pdev->dev, "phy-channel",
128 /* If we can't read a valid phy address, then assume
129 * that there is only one phy on this mdio bus.
131 adpt->phydev = phy_find_first(mii_bus);
133 adpt->phydev = mdiobus_get_phy(mii_bus, phy_addr);
135 /* of_phy_find_device() claims a reference to the phydev,
136 * so we do that here manually as well. When the driver
137 * later unloads, it can unilaterally drop the reference
138 * without worrying about ACPI vs DT.
141 get_device(&adpt->phydev->mdio.dev);
143 struct device_node *phy_np;
145 ret = of_mdiobus_register(mii_bus, np);
147 dev_err(&pdev->dev, "could not register mdio bus\n");
151 phy_np = of_parse_phandle(np, "phy-handle", 0);
152 adpt->phydev = of_phy_find_device(phy_np);
157 dev_err(&pdev->dev, "could not find external phy\n");
158 mdiobus_unregister(mii_bus);