lkdtm: Add Control Flow Integrity test
[linux/fpc-iii.git] / drivers / net / phy / mdio-bcm-iproc.c
blob7d0f388d8db89bfb95f395558b484d948890bdd5
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2015 Broadcom Corporation
4 */
6 #include <linux/delay.h>
7 #include <linux/io.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/of.h>
11 #include <linux/of_platform.h>
12 #include <linux/of_mdio.h>
13 #include <linux/phy.h>
14 #include <linux/platform_device.h>
15 #include <linux/sched.h>
17 #define IPROC_GPHY_MDCDIV 0x1a
19 #define MII_CTRL_OFFSET 0x000
21 #define MII_CTRL_DIV_SHIFT 0
22 #define MII_CTRL_PRE_SHIFT 7
23 #define MII_CTRL_BUSY_SHIFT 8
25 #define MII_DATA_OFFSET 0x004
26 #define MII_DATA_MASK 0xffff
27 #define MII_DATA_TA_SHIFT 16
28 #define MII_DATA_TA_VAL 2
29 #define MII_DATA_RA_SHIFT 18
30 #define MII_DATA_PA_SHIFT 23
31 #define MII_DATA_OP_SHIFT 28
32 #define MII_DATA_OP_WRITE 1
33 #define MII_DATA_OP_READ 2
34 #define MII_DATA_SB_SHIFT 30
36 struct iproc_mdio_priv {
37 struct mii_bus *mii_bus;
38 void __iomem *base;
41 static inline int iproc_mdio_wait_for_idle(void __iomem *base)
43 u32 val;
44 unsigned int timeout = 1000; /* loop for 1s */
46 do {
47 val = readl(base + MII_CTRL_OFFSET);
48 if ((val & BIT(MII_CTRL_BUSY_SHIFT)) == 0)
49 return 0;
51 usleep_range(1000, 2000);
52 } while (timeout--);
54 return -ETIMEDOUT;
57 static inline void iproc_mdio_config_clk(void __iomem *base)
59 u32 val;
61 val = (IPROC_GPHY_MDCDIV << MII_CTRL_DIV_SHIFT) |
62 BIT(MII_CTRL_PRE_SHIFT);
63 writel(val, base + MII_CTRL_OFFSET);
66 static int iproc_mdio_read(struct mii_bus *bus, int phy_id, int reg)
68 struct iproc_mdio_priv *priv = bus->priv;
69 u32 cmd;
70 int rc;
72 rc = iproc_mdio_wait_for_idle(priv->base);
73 if (rc)
74 return rc;
76 /* Prepare the read operation */
77 cmd = (MII_DATA_TA_VAL << MII_DATA_TA_SHIFT) |
78 (reg << MII_DATA_RA_SHIFT) |
79 (phy_id << MII_DATA_PA_SHIFT) |
80 BIT(MII_DATA_SB_SHIFT) |
81 (MII_DATA_OP_READ << MII_DATA_OP_SHIFT);
83 writel(cmd, priv->base + MII_DATA_OFFSET);
85 rc = iproc_mdio_wait_for_idle(priv->base);
86 if (rc)
87 return rc;
89 cmd = readl(priv->base + MII_DATA_OFFSET) & MII_DATA_MASK;
91 return cmd;
94 static int iproc_mdio_write(struct mii_bus *bus, int phy_id,
95 int reg, u16 val)
97 struct iproc_mdio_priv *priv = bus->priv;
98 u32 cmd;
99 int rc;
101 rc = iproc_mdio_wait_for_idle(priv->base);
102 if (rc)
103 return rc;
105 /* Prepare the write operation */
106 cmd = (MII_DATA_TA_VAL << MII_DATA_TA_SHIFT) |
107 (reg << MII_DATA_RA_SHIFT) |
108 (phy_id << MII_DATA_PA_SHIFT) |
109 BIT(MII_DATA_SB_SHIFT) |
110 (MII_DATA_OP_WRITE << MII_DATA_OP_SHIFT) |
111 ((u32)(val) & MII_DATA_MASK);
113 writel(cmd, priv->base + MII_DATA_OFFSET);
115 rc = iproc_mdio_wait_for_idle(priv->base);
116 if (rc)
117 return rc;
119 return 0;
122 static int iproc_mdio_probe(struct platform_device *pdev)
124 struct iproc_mdio_priv *priv;
125 struct mii_bus *bus;
126 struct resource *res;
127 int rc;
129 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
130 if (!priv)
131 return -ENOMEM;
133 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
134 priv->base = devm_ioremap_resource(&pdev->dev, res);
135 if (IS_ERR(priv->base)) {
136 dev_err(&pdev->dev, "failed to ioremap register\n");
137 return PTR_ERR(priv->base);
140 priv->mii_bus = mdiobus_alloc();
141 if (!priv->mii_bus) {
142 dev_err(&pdev->dev, "MDIO bus alloc failed\n");
143 return -ENOMEM;
146 bus = priv->mii_bus;
147 bus->priv = priv;
148 bus->name = "iProc MDIO bus";
149 snprintf(bus->id, MII_BUS_ID_SIZE, "%s-%d", pdev->name, pdev->id);
150 bus->parent = &pdev->dev;
151 bus->read = iproc_mdio_read;
152 bus->write = iproc_mdio_write;
154 iproc_mdio_config_clk(priv->base);
156 rc = of_mdiobus_register(bus, pdev->dev.of_node);
157 if (rc) {
158 dev_err(&pdev->dev, "MDIO bus registration failed\n");
159 goto err_iproc_mdio;
162 platform_set_drvdata(pdev, priv);
164 dev_info(&pdev->dev, "Broadcom iProc MDIO bus at 0x%p\n", priv->base);
166 return 0;
168 err_iproc_mdio:
169 mdiobus_free(bus);
170 return rc;
173 static int iproc_mdio_remove(struct platform_device *pdev)
175 struct iproc_mdio_priv *priv = platform_get_drvdata(pdev);
177 mdiobus_unregister(priv->mii_bus);
178 mdiobus_free(priv->mii_bus);
180 return 0;
183 static const struct of_device_id iproc_mdio_of_match[] = {
184 { .compatible = "brcm,iproc-mdio", },
185 { /* sentinel */ },
187 MODULE_DEVICE_TABLE(of, iproc_mdio_of_match);
189 static struct platform_driver iproc_mdio_driver = {
190 .driver = {
191 .name = "iproc-mdio",
192 .of_match_table = iproc_mdio_of_match,
194 .probe = iproc_mdio_probe,
195 .remove = iproc_mdio_remove,
198 module_platform_driver(iproc_mdio_driver);
200 MODULE_AUTHOR("Broadcom Corporation");
201 MODULE_DESCRIPTION("Broadcom iProc MDIO bus controller");
202 MODULE_LICENSE("GPL v2");
203 MODULE_ALIAS("platform:iproc-mdio");