PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / net / phy / mdio-sun4i.c
blob9367acc84fbb2e54f3d058864d820bf8974d21c7
1 /*
2 * Allwinner EMAC MDIO interface driver
4 * Copyright 2012-2013 Stefan Roese <sr@denx.de>
5 * Copyright 2013 Maxime Ripard <maxime.ripard@free-electrons.com>
7 * Based on the Linux driver provided by Allwinner:
8 * Copyright (C) 1997 Sten Wang
10 * This file is licensed under the terms of the GNU General Public
11 * License version 2. This program is licensed "as is" without any
12 * warranty of any kind, whether express or implied.
15 #include <linux/delay.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/mutex.h>
19 #include <linux/of_address.h>
20 #include <linux/of_mdio.h>
21 #include <linux/phy.h>
22 #include <linux/platform_device.h>
23 #include <linux/regulator/consumer.h>
25 #define EMAC_MAC_MCMD_REG (0x00)
26 #define EMAC_MAC_MADR_REG (0x04)
27 #define EMAC_MAC_MWTD_REG (0x08)
28 #define EMAC_MAC_MRDD_REG (0x0c)
29 #define EMAC_MAC_MIND_REG (0x10)
30 #define EMAC_MAC_SSRR_REG (0x14)
32 #define MDIO_TIMEOUT (msecs_to_jiffies(100))
34 struct sun4i_mdio_data {
35 void __iomem *membase;
36 struct regulator *regulator;
39 static int sun4i_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
41 struct sun4i_mdio_data *data = bus->priv;
42 unsigned long timeout_jiffies;
43 int value;
45 /* issue the phy address and reg */
46 writel((mii_id << 8) | regnum, data->membase + EMAC_MAC_MADR_REG);
47 /* pull up the phy io line */
48 writel(0x1, data->membase + EMAC_MAC_MCMD_REG);
50 /* Wait read complete */
51 timeout_jiffies = jiffies + MDIO_TIMEOUT;
52 while (readl(data->membase + EMAC_MAC_MIND_REG) & 0x1) {
53 if (time_is_before_jiffies(timeout_jiffies))
54 return -ETIMEDOUT;
55 msleep(1);
58 /* push down the phy io line */
59 writel(0x0, data->membase + EMAC_MAC_MCMD_REG);
60 /* and read data */
61 value = readl(data->membase + EMAC_MAC_MRDD_REG);
63 return value;
66 static int sun4i_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
67 u16 value)
69 struct sun4i_mdio_data *data = bus->priv;
70 unsigned long timeout_jiffies;
72 /* issue the phy address and reg */
73 writel((mii_id << 8) | regnum, data->membase + EMAC_MAC_MADR_REG);
74 /* pull up the phy io line */
75 writel(0x1, data->membase + EMAC_MAC_MCMD_REG);
77 /* Wait read complete */
78 timeout_jiffies = jiffies + MDIO_TIMEOUT;
79 while (readl(data->membase + EMAC_MAC_MIND_REG) & 0x1) {
80 if (time_is_before_jiffies(timeout_jiffies))
81 return -ETIMEDOUT;
82 msleep(1);
85 /* push down the phy io line */
86 writel(0x0, data->membase + EMAC_MAC_MCMD_REG);
87 /* and write data */
88 writel(value, data->membase + EMAC_MAC_MWTD_REG);
90 return 0;
93 static int sun4i_mdio_reset(struct mii_bus *bus)
95 return 0;
98 static int sun4i_mdio_probe(struct platform_device *pdev)
100 struct device_node *np = pdev->dev.of_node;
101 struct mii_bus *bus;
102 struct sun4i_mdio_data *data;
103 struct resource *res;
104 int ret, i;
106 bus = mdiobus_alloc_size(sizeof(*data));
107 if (!bus)
108 return -ENOMEM;
110 bus->name = "sun4i_mii_bus";
111 bus->read = &sun4i_mdio_read;
112 bus->write = &sun4i_mdio_write;
113 bus->reset = &sun4i_mdio_reset;
114 snprintf(bus->id, MII_BUS_ID_SIZE, "%s-mii", dev_name(&pdev->dev));
115 bus->parent = &pdev->dev;
117 bus->irq = devm_kzalloc(&pdev->dev, sizeof(int) * PHY_MAX_ADDR,
118 GFP_KERNEL);
119 if (!bus->irq) {
120 ret = -ENOMEM;
121 goto err_out_free_mdiobus;
124 for (i = 0; i < PHY_MAX_ADDR; i++)
125 bus->irq[i] = PHY_POLL;
127 data = bus->priv;
128 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
129 data->membase = devm_ioremap_resource(&pdev->dev, res);
130 if (IS_ERR(data->membase)) {
131 ret = PTR_ERR(data->membase);
132 goto err_out_free_mdiobus;
135 data->regulator = devm_regulator_get(&pdev->dev, "phy");
136 if (IS_ERR(data->regulator)) {
137 if (PTR_ERR(data->regulator) == -EPROBE_DEFER)
138 return -EPROBE_DEFER;
140 dev_info(&pdev->dev, "no regulator found\n");
141 } else {
142 ret = regulator_enable(data->regulator);
143 if (ret)
144 goto err_out_free_mdiobus;
147 ret = of_mdiobus_register(bus, np);
148 if (ret < 0)
149 goto err_out_disable_regulator;
151 platform_set_drvdata(pdev, bus);
153 return 0;
155 err_out_disable_regulator:
156 regulator_disable(data->regulator);
157 err_out_free_mdiobus:
158 mdiobus_free(bus);
159 return ret;
162 static int sun4i_mdio_remove(struct platform_device *pdev)
164 struct mii_bus *bus = platform_get_drvdata(pdev);
166 mdiobus_unregister(bus);
167 mdiobus_free(bus);
169 return 0;
172 static const struct of_device_id sun4i_mdio_dt_ids[] = {
173 { .compatible = "allwinner,sun4i-a10-mdio" },
175 /* Deprecated */
176 { .compatible = "allwinner,sun4i-mdio" },
179 MODULE_DEVICE_TABLE(of, sun4i_mdio_dt_ids);
181 static struct platform_driver sun4i_mdio_driver = {
182 .probe = sun4i_mdio_probe,
183 .remove = sun4i_mdio_remove,
184 .driver = {
185 .name = "sun4i-mdio",
186 .of_match_table = sun4i_mdio_dt_ids,
190 module_platform_driver(sun4i_mdio_driver);
192 MODULE_DESCRIPTION("Allwinner EMAC MDIO interface driver");
193 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
194 MODULE_LICENSE("GPL");