1 From 234d6f40fb4b771b396b45a9492aab463771bd0b Mon Sep 17 00:00:00 2001
2 From: Kristian Evensen <kristian.evensen@gmail.com>
3 Date: Tue, 6 Aug 2019 11:42:57 +0200
4 Subject: [PATCH] phy: Add ipq40xx mdio driver
7 drivers/net/phy/Kconfig | 7 +
8 drivers/net/phy/Makefile | 1 +
9 drivers/net/phy/mdio-ipq40xx.c | 247 +++++++++++++++++++++++++++++++++
10 3 files changed, 255 insertions(+)
11 create mode 100644 drivers/net/phy/mdio-ipq40xx.c
13 --- a/drivers/net/phy/Kconfig
14 +++ b/drivers/net/phy/Kconfig
15 @@ -519,6 +519,13 @@ config XILINX_GMII2RGMII
16 the Reduced Gigabit Media Independent Interface(RGMII) between
17 Ethernet physical media devices and the Gigabit Ethernet controller.
20 + tristate "Qualcomm Atheros ipq40xx MDIO interface"
21 + depends on HAS_IOMEM && OF
23 + This driver supports the MDIO interface found in Qualcomm
24 + Atheros ipq40xx Soc chip.
28 config MICREL_KS8995MA
29 --- a/drivers/net/phy/Makefile
30 +++ b/drivers/net/phy/Makefile
31 @@ -48,6 +48,7 @@ obj-$(CONFIG_MDIO_CAVIUM) += mdio-cavium
32 obj-$(CONFIG_MDIO_GPIO) += mdio-gpio.o
33 obj-$(CONFIG_MDIO_HISI_FEMAC) += mdio-hisi-femac.o
34 obj-$(CONFIG_MDIO_I2C) += mdio-i2c.o
35 +obj-$(CONFIG_MDIO_IPQ40XX) += mdio-ipq40xx.o
36 obj-$(CONFIG_MDIO_MOXART) += mdio-moxart.o
37 obj-$(CONFIG_MDIO_MSCC_MIIM) += mdio-mscc-miim.o
38 obj-$(CONFIG_MDIO_OCTEON) += mdio-octeon.o
40 +++ b/drivers/net/phy/mdio-ipq40xx.c
43 + * Copyright (c) 2015-2016, The Linux Foundation. All rights reserved.
45 + * Permission to use, copy, modify, and/or distribute this software for
46 + * any purpose with or without fee is hereby granted, provided that the
47 + * above copyright notice and this permission notice appear in all copies.
48 + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
49 + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
50 + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
51 + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
52 + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
53 + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
54 + * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
57 +#include <linux/delay.h>
58 +#include <linux/kernel.h>
59 +#include <linux/module.h>
60 +#include <linux/mutex.h>
61 +#include <linux/io.h>
62 +#include <linux/of_address.h>
63 +#include <linux/of_mdio.h>
64 +#include <linux/of_gpio.h>
65 +#include <linux/phy.h>
66 +#include <linux/platform_device.h>
67 +#include <linux/gpio.h>
69 +#define MDIO_CTRL_0_REG 0x40
70 +#define MDIO_CTRL_1_REG 0x44
71 +#define MDIO_CTRL_2_REG 0x48
72 +#define MDIO_CTRL_3_REG 0x4c
73 +#define MDIO_CTRL_4_REG 0x50
74 +#define MDIO_CTRL_4_ACCESS_BUSY BIT(16)
75 +#define MDIO_CTRL_4_ACCESS_START BIT(8)
76 +#define MDIO_CTRL_4_ACCESS_CODE_READ 0
77 +#define MDIO_CTRL_4_ACCESS_CODE_WRITE 1
78 +#define CTRL_0_REG_DEFAULT_VALUE 0x150FF
80 +#define IPQ40XX_MDIO_RETRY 1000
81 +#define IPQ40XX_MDIO_DELAY 10
83 +struct ipq40xx_mdio_data {
84 + struct mii_bus *mii_bus;
85 + void __iomem *membase;
89 +static int ipq40xx_mdio_wait_busy(struct ipq40xx_mdio_data *am)
93 + for (i = 0; i < IPQ40XX_MDIO_RETRY; i++) {
96 + busy = readl(am->membase + MDIO_CTRL_4_REG) &
97 + MDIO_CTRL_4_ACCESS_BUSY;
101 + /* BUSY might take to be cleard by 15~20 times of loop */
102 + udelay(IPQ40XX_MDIO_DELAY);
105 + dev_err(am->dev, "%s: MDIO operation timed out\n", am->mii_bus->name);
110 +static int ipq40xx_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
112 + struct ipq40xx_mdio_data *am = bus->priv;
114 + unsigned int cmd = 0;
116 + lockdep_assert_held(&bus->mdio_lock);
118 + if (ipq40xx_mdio_wait_busy(am))
121 + /* issue the phy address and reg */
122 + writel((mii_id << 8) | regnum, am->membase + MDIO_CTRL_1_REG);
124 + cmd = MDIO_CTRL_4_ACCESS_START|MDIO_CTRL_4_ACCESS_CODE_READ;
126 + /* issue read command */
127 + writel(cmd, am->membase + MDIO_CTRL_4_REG);
129 + /* Wait read complete */
130 + if (ipq40xx_mdio_wait_busy(am))
134 + value = readl(am->membase + MDIO_CTRL_3_REG);
139 +static int ipq40xx_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
142 + struct ipq40xx_mdio_data *am = bus->priv;
143 + unsigned int cmd = 0;
145 + lockdep_assert_held(&bus->mdio_lock);
147 + if (ipq40xx_mdio_wait_busy(am))
150 + /* issue the phy address and reg */
151 + writel((mii_id << 8) | regnum, am->membase + MDIO_CTRL_1_REG);
153 + /* issue write data */
154 + writel(value, am->membase + MDIO_CTRL_2_REG);
156 + cmd = MDIO_CTRL_4_ACCESS_START|MDIO_CTRL_4_ACCESS_CODE_WRITE;
157 + /* issue write command */
158 + writel(cmd, am->membase + MDIO_CTRL_4_REG);
160 + /* Wait write complete */
161 + if (ipq40xx_mdio_wait_busy(am))
167 +static int ipq40xx_phy_reset(struct platform_device *pdev)
169 + struct device_node *mdio_node;
170 + int phy_reset_gpio_number;
173 + mdio_node = of_find_node_by_name(NULL, "mdio");
175 + dev_err(&pdev->dev, "Could not find mdio node\n");
179 + ret = of_get_named_gpio(mdio_node, "phy-reset-gpio", 0);
181 + dev_err(&pdev->dev, "Could not find phy-reset-gpio\n");
185 + phy_reset_gpio_number = ret;
187 + ret = gpio_request(phy_reset_gpio_number, "phy-reset-gpio");
189 + dev_err(&pdev->dev, "Can't get phy-reset-gpio %d\n", ret);
193 + ret = gpio_direction_output(phy_reset_gpio_number, 0x0);
195 + dev_err(&pdev->dev,
196 + "Can't set direction for phy-reset-gpio %d\n", ret);
197 + goto phy_reset_out;
200 + usleep_range(1000, 10005);
202 + gpio_set_value(phy_reset_gpio_number, 0x01);
205 + gpio_free(phy_reset_gpio_number);
210 +static int ipq40xx_mdio_probe(struct platform_device *pdev)
212 + struct ipq40xx_mdio_data *am;
213 + struct resource *res;
216 + ret = ipq40xx_phy_reset(pdev);
218 + dev_err(&pdev->dev, "Could not find qca8075 reset gpio\n");
222 + am = devm_kzalloc(&pdev->dev, sizeof(*am), GFP_KERNEL);
226 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
228 + dev_err(&pdev->dev, "no iomem resource found\n");
232 + am->membase = devm_ioremap_resource(&pdev->dev, res);
233 + if (IS_ERR(am->membase)) {
234 + dev_err(&pdev->dev, "unable to ioremap registers\n");
235 + return PTR_ERR(am->membase);
238 + am->mii_bus = devm_mdiobus_alloc(&pdev->dev);
242 + writel(CTRL_0_REG_DEFAULT_VALUE, am->membase + MDIO_CTRL_0_REG);
244 + am->mii_bus->name = "ipq40xx_mdio";
245 + am->mii_bus->read = ipq40xx_mdio_read;
246 + am->mii_bus->write = ipq40xx_mdio_write;
247 + am->mii_bus->priv = am;
248 + am->mii_bus->parent = &pdev->dev;
249 + snprintf(am->mii_bus->id, MII_BUS_ID_SIZE, "%s", dev_name(&pdev->dev));
251 + am->dev = &pdev->dev;
252 + platform_set_drvdata(pdev, am);
254 + return of_mdiobus_register(am->mii_bus, pdev->dev.of_node);
257 +static int ipq40xx_mdio_remove(struct platform_device *pdev)
259 + struct ipq40xx_mdio_data *am = platform_get_drvdata(pdev);
261 + mdiobus_unregister(am->mii_bus);
266 +static const struct of_device_id ipq40xx_mdio_dt_ids[] = {
267 + { .compatible = "qcom,ipq4019-mdio" },
270 +MODULE_DEVICE_TABLE(of, ipq40xx_mdio_dt_ids);
272 +static struct platform_driver ipq40xx_mdio_driver = {
273 + .probe = ipq40xx_mdio_probe,
274 + .remove = ipq40xx_mdio_remove,
276 + .name = "ipq40xx-mdio",
277 + .of_match_table = ipq40xx_mdio_dt_ids,
281 +module_platform_driver(ipq40xx_mdio_driver);
283 +#define DRV_VERSION "1.0"
285 +MODULE_DESCRIPTION("IPQ40XX MDIO interface driver");
286 +MODULE_AUTHOR("Qualcomm Atheros");
287 +MODULE_VERSION(DRV_VERSION);
288 +MODULE_LICENSE("Dual BSD/GPL");