blob: 4511bcc73b3671a015ce97d1cda64809fe56ad24 [file] [log] [blame]
Andrew Lunn5f857572019-01-21 19:10:19 +01001// SPDX-License-Identifier: GPL-2.0
Maxime Ripard4bdcb1d2013-05-30 03:49:21 +00002/*
3 * Allwinner EMAC MDIO interface driver
4 *
5 * Copyright 2012-2013 Stefan Roese <sr@denx.de>
6 * Copyright 2013 Maxime Ripard <maxime.ripard@free-electrons.com>
7 *
8 * Based on the Linux driver provided by Allwinner:
9 * Copyright (C) 1997 Sten Wang
Maxime Ripard4bdcb1d2013-05-30 03:49:21 +000010 */
11
12#include <linux/delay.h>
Maxime Ripard4bdcb1d2013-05-30 03:49:21 +000013#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/mutex.h>
16#include <linux/of_address.h>
17#include <linux/of_mdio.h>
18#include <linux/phy.h>
19#include <linux/platform_device.h>
20#include <linux/regulator/consumer.h>
21
22#define EMAC_MAC_MCMD_REG (0x00)
23#define EMAC_MAC_MADR_REG (0x04)
24#define EMAC_MAC_MWTD_REG (0x08)
25#define EMAC_MAC_MRDD_REG (0x0c)
26#define EMAC_MAC_MIND_REG (0x10)
27#define EMAC_MAC_SSRR_REG (0x14)
28
29#define MDIO_TIMEOUT (msecs_to_jiffies(100))
30
31struct sun4i_mdio_data {
32 void __iomem *membase;
33 struct regulator *regulator;
34};
35
36static int sun4i_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
37{
38 struct sun4i_mdio_data *data = bus->priv;
Emilio López2bf420a2013-07-22 22:09:18 -030039 unsigned long timeout_jiffies;
Maxime Ripard4bdcb1d2013-05-30 03:49:21 +000040 int value;
41
42 /* issue the phy address and reg */
43 writel((mii_id << 8) | regnum, data->membase + EMAC_MAC_MADR_REG);
44 /* pull up the phy io line */
45 writel(0x1, data->membase + EMAC_MAC_MCMD_REG);
46
47 /* Wait read complete */
Emilio López2bf420a2013-07-22 22:09:18 -030048 timeout_jiffies = jiffies + MDIO_TIMEOUT;
Maxime Ripard4bdcb1d2013-05-30 03:49:21 +000049 while (readl(data->membase + EMAC_MAC_MIND_REG) & 0x1) {
Emilio López2bf420a2013-07-22 22:09:18 -030050 if (time_is_before_jiffies(timeout_jiffies))
Maxime Ripard4bdcb1d2013-05-30 03:49:21 +000051 return -ETIMEDOUT;
52 msleep(1);
53 }
54
55 /* push down the phy io line */
56 writel(0x0, data->membase + EMAC_MAC_MCMD_REG);
57 /* and read data */
58 value = readl(data->membase + EMAC_MAC_MRDD_REG);
59
60 return value;
61}
62
63static int sun4i_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
64 u16 value)
65{
66 struct sun4i_mdio_data *data = bus->priv;
Emilio López2bf420a2013-07-22 22:09:18 -030067 unsigned long timeout_jiffies;
Maxime Ripard4bdcb1d2013-05-30 03:49:21 +000068
69 /* issue the phy address and reg */
70 writel((mii_id << 8) | regnum, data->membase + EMAC_MAC_MADR_REG);
71 /* pull up the phy io line */
72 writel(0x1, data->membase + EMAC_MAC_MCMD_REG);
73
74 /* Wait read complete */
Emilio López2bf420a2013-07-22 22:09:18 -030075 timeout_jiffies = jiffies + MDIO_TIMEOUT;
Maxime Ripard4bdcb1d2013-05-30 03:49:21 +000076 while (readl(data->membase + EMAC_MAC_MIND_REG) & 0x1) {
Emilio López2bf420a2013-07-22 22:09:18 -030077 if (time_is_before_jiffies(timeout_jiffies))
Maxime Ripard4bdcb1d2013-05-30 03:49:21 +000078 return -ETIMEDOUT;
79 msleep(1);
80 }
81
82 /* push down the phy io line */
83 writel(0x0, data->membase + EMAC_MAC_MCMD_REG);
84 /* and write data */
85 writel(value, data->membase + EMAC_MAC_MWTD_REG);
86
87 return 0;
88}
89
Maxime Ripard4bdcb1d2013-05-30 03:49:21 +000090static int sun4i_mdio_probe(struct platform_device *pdev)
91{
92 struct device_node *np = pdev->dev.of_node;
93 struct mii_bus *bus;
94 struct sun4i_mdio_data *data;
Andrew Lunne7f4dc32016-01-06 20:11:15 +010095 int ret;
Maxime Ripard4bdcb1d2013-05-30 03:49:21 +000096
97 bus = mdiobus_alloc_size(sizeof(*data));
98 if (!bus)
99 return -ENOMEM;
100
101 bus->name = "sun4i_mii_bus";
102 bus->read = &sun4i_mdio_read;
103 bus->write = &sun4i_mdio_write;
Maxime Ripard4bdcb1d2013-05-30 03:49:21 +0000104 snprintf(bus->id, MII_BUS_ID_SIZE, "%s-mii", dev_name(&pdev->dev));
105 bus->parent = &pdev->dev;
106
Maxime Ripard4bdcb1d2013-05-30 03:49:21 +0000107 data = bus->priv;
YueHaibing38947932019-08-27 21:50:32 +0800108 data->membase = devm_platform_ioremap_resource(pdev, 0);
Jisheng Zhang03536cc2013-08-26 21:11:57 +0800109 if (IS_ERR(data->membase)) {
110 ret = PTR_ERR(data->membase);
111 goto err_out_free_mdiobus;
Maxime Ripard4bdcb1d2013-05-30 03:49:21 +0000112 }
113
114 data->regulator = devm_regulator_get(&pdev->dev, "phy");
115 if (IS_ERR(data->regulator)) {
Christophe JAILLET56c02902018-01-06 09:00:09 +0100116 if (PTR_ERR(data->regulator) == -EPROBE_DEFER) {
117 ret = -EPROBE_DEFER;
118 goto err_out_free_mdiobus;
119 }
Maxime Ripard4bdcb1d2013-05-30 03:49:21 +0000120
121 dev_info(&pdev->dev, "no regulator found\n");
Dan Carpenter227f33b2016-03-21 12:02:31 +0300122 data->regulator = NULL;
Maxime Ripard4bdcb1d2013-05-30 03:49:21 +0000123 } else {
124 ret = regulator_enable(data->regulator);
125 if (ret)
Jisheng Zhang03536cc2013-08-26 21:11:57 +0800126 goto err_out_free_mdiobus;
Maxime Ripard4bdcb1d2013-05-30 03:49:21 +0000127 }
128
129 ret = of_mdiobus_register(bus, np);
130 if (ret < 0)
131 goto err_out_disable_regulator;
132
133 platform_set_drvdata(pdev, bus);
134
135 return 0;
136
137err_out_disable_regulator:
Dan Carpenter227f33b2016-03-21 12:02:31 +0300138 if (data->regulator)
139 regulator_disable(data->regulator);
Maxime Ripard4bdcb1d2013-05-30 03:49:21 +0000140err_out_free_mdiobus:
141 mdiobus_free(bus);
142 return ret;
143}
144
Uwe Kleine-Königefd8d902023-09-18 21:51:01 +0200145static void sun4i_mdio_remove(struct platform_device *pdev)
Maxime Ripard4bdcb1d2013-05-30 03:49:21 +0000146{
147 struct mii_bus *bus = platform_get_drvdata(pdev);
Chuhong Yuan39c68b32019-11-18 19:41:15 +0800148 struct sun4i_mdio_data *data = bus->priv;
Maxime Ripard4bdcb1d2013-05-30 03:49:21 +0000149
150 mdiobus_unregister(bus);
Chuhong Yuan39c68b32019-11-18 19:41:15 +0800151 if (data->regulator)
152 regulator_disable(data->regulator);
Maxime Ripard4bdcb1d2013-05-30 03:49:21 +0000153 mdiobus_free(bus);
Maxime Ripard4bdcb1d2013-05-30 03:49:21 +0000154}
155
156static const struct of_device_id sun4i_mdio_dt_ids[] = {
Maxime Ripardefe20572014-02-02 14:49:12 +0100157 { .compatible = "allwinner,sun4i-a10-mdio" },
158
159 /* Deprecated */
Maxime Ripard4bdcb1d2013-05-30 03:49:21 +0000160 { .compatible = "allwinner,sun4i-mdio" },
161 { }
162};
163MODULE_DEVICE_TABLE(of, sun4i_mdio_dt_ids);
164
165static struct platform_driver sun4i_mdio_driver = {
166 .probe = sun4i_mdio_probe,
Uwe Kleine-Königefd8d902023-09-18 21:51:01 +0200167 .remove_new = sun4i_mdio_remove,
Maxime Ripard4bdcb1d2013-05-30 03:49:21 +0000168 .driver = {
169 .name = "sun4i-mdio",
170 .of_match_table = sun4i_mdio_dt_ids,
171 },
172};
173
174module_platform_driver(sun4i_mdio_driver);
175
176MODULE_DESCRIPTION("Allwinner EMAC MDIO interface driver");
177MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
Andrew Lunn5f857572019-01-21 19:10:19 +0100178MODULE_LICENSE("GPL v2");