xref: /openbmc/linux/drivers/net/mdio/mdio-aspeed.c (revision 2427f03f)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Copyright (C) 2019 IBM Corp. */
3 
4 #include <linux/bitfield.h>
5 #include <linux/delay.h>
6 #include <linux/iopoll.h>
7 #include <linux/mdio.h>
8 #include <linux/module.h>
9 #include <linux/of.h>
10 #include <linux/of_mdio.h>
11 #include <linux/phy.h>
12 #include <linux/platform_device.h>
13 
14 #define DRV_NAME "mdio-aspeed"
15 
16 #define ASPEED_MDIO_CTRL		0x0
17 #define   ASPEED_MDIO_CTRL_FIRE		BIT(31)
18 #define   ASPEED_MDIO_CTRL_ST		BIT(28)
19 #define     ASPEED_MDIO_CTRL_ST_C45	0
20 #define     ASPEED_MDIO_CTRL_ST_C22	1
21 #define   ASPEED_MDIO_CTRL_OP		GENMASK(27, 26)
22 #define     MDIO_C22_OP_WRITE		0b01
23 #define     MDIO_C22_OP_READ		0b10
24 #define   ASPEED_MDIO_CTRL_PHYAD	GENMASK(25, 21)
25 #define   ASPEED_MDIO_CTRL_REGAD	GENMASK(20, 16)
26 #define   ASPEED_MDIO_CTRL_MIIWDATA	GENMASK(15, 0)
27 
28 #define ASPEED_MDIO_DATA		0x4
29 #define   ASPEED_MDIO_DATA_MDC_THRES	GENMASK(31, 24)
30 #define   ASPEED_MDIO_DATA_MDIO_EDGE	BIT(23)
31 #define   ASPEED_MDIO_DATA_MDIO_LATCH	GENMASK(22, 20)
32 #define   ASPEED_MDIO_DATA_IDLE		BIT(16)
33 #define   ASPEED_MDIO_DATA_MIIRDATA	GENMASK(15, 0)
34 
35 #define ASPEED_MDIO_INTERVAL_US		100
36 #define ASPEED_MDIO_TIMEOUT_US		(ASPEED_MDIO_INTERVAL_US * 10)
37 
38 struct aspeed_mdio {
39 	void __iomem *base;
40 };
41 
42 static int aspeed_mdio_read(struct mii_bus *bus, int addr, int regnum)
43 {
44 	struct aspeed_mdio *ctx = bus->priv;
45 	u32 ctrl;
46 	u32 data;
47 	int rc;
48 
49 	dev_dbg(&bus->dev, "%s: addr: %d, regnum: %d\n", __func__, addr,
50 		regnum);
51 
52 	/* Just clause 22 for the moment */
53 	if (regnum & MII_ADDR_C45)
54 		return -EOPNOTSUPP;
55 
56 	ctrl = ASPEED_MDIO_CTRL_FIRE
57 		| FIELD_PREP(ASPEED_MDIO_CTRL_ST, ASPEED_MDIO_CTRL_ST_C22)
58 		| FIELD_PREP(ASPEED_MDIO_CTRL_OP, MDIO_C22_OP_READ)
59 		| FIELD_PREP(ASPEED_MDIO_CTRL_PHYAD, addr)
60 		| FIELD_PREP(ASPEED_MDIO_CTRL_REGAD, regnum);
61 
62 	iowrite32(ctrl, ctx->base + ASPEED_MDIO_CTRL);
63 
64 	rc = readl_poll_timeout(ctx->base + ASPEED_MDIO_CTRL, ctrl,
65 				!(ctrl & ASPEED_MDIO_CTRL_FIRE),
66 				ASPEED_MDIO_INTERVAL_US,
67 				ASPEED_MDIO_TIMEOUT_US);
68 	if (rc < 0)
69 		return rc;
70 
71 	rc = readl_poll_timeout(ctx->base + ASPEED_MDIO_DATA, data,
72 				data & ASPEED_MDIO_DATA_IDLE,
73 				ASPEED_MDIO_INTERVAL_US,
74 				ASPEED_MDIO_TIMEOUT_US);
75 	if (rc < 0)
76 		return rc;
77 
78 	return FIELD_GET(ASPEED_MDIO_DATA_MIIRDATA, data);
79 }
80 
81 static int aspeed_mdio_write(struct mii_bus *bus, int addr, int regnum, u16 val)
82 {
83 	struct aspeed_mdio *ctx = bus->priv;
84 	u32 ctrl;
85 
86 	dev_dbg(&bus->dev, "%s: addr: %d, regnum: %d, val: 0x%x\n",
87 		__func__, addr, regnum, val);
88 
89 	/* Just clause 22 for the moment */
90 	if (regnum & MII_ADDR_C45)
91 		return -EOPNOTSUPP;
92 
93 	ctrl = ASPEED_MDIO_CTRL_FIRE
94 		| FIELD_PREP(ASPEED_MDIO_CTRL_ST, ASPEED_MDIO_CTRL_ST_C22)
95 		| FIELD_PREP(ASPEED_MDIO_CTRL_OP, MDIO_C22_OP_WRITE)
96 		| FIELD_PREP(ASPEED_MDIO_CTRL_PHYAD, addr)
97 		| FIELD_PREP(ASPEED_MDIO_CTRL_REGAD, regnum)
98 		| FIELD_PREP(ASPEED_MDIO_CTRL_MIIWDATA, val);
99 
100 	iowrite32(ctrl, ctx->base + ASPEED_MDIO_CTRL);
101 
102 	return readl_poll_timeout(ctx->base + ASPEED_MDIO_CTRL, ctrl,
103 				  !(ctrl & ASPEED_MDIO_CTRL_FIRE),
104 				  ASPEED_MDIO_INTERVAL_US,
105 				  ASPEED_MDIO_TIMEOUT_US);
106 }
107 
108 static int aspeed_mdio_probe(struct platform_device *pdev)
109 {
110 	struct aspeed_mdio *ctx;
111 	struct mii_bus *bus;
112 	int rc;
113 
114 	bus = devm_mdiobus_alloc_size(&pdev->dev, sizeof(*ctx));
115 	if (!bus)
116 		return -ENOMEM;
117 
118 	ctx = bus->priv;
119 	ctx->base = devm_platform_ioremap_resource(pdev, 0);
120 	if (IS_ERR(ctx->base))
121 		return PTR_ERR(ctx->base);
122 
123 	bus->name = DRV_NAME;
124 	snprintf(bus->id, MII_BUS_ID_SIZE, "%s%d", pdev->name, pdev->id);
125 	bus->parent = &pdev->dev;
126 	bus->read = aspeed_mdio_read;
127 	bus->write = aspeed_mdio_write;
128 
129 	rc = of_mdiobus_register(bus, pdev->dev.of_node);
130 	if (rc) {
131 		dev_err(&pdev->dev, "Cannot register MDIO bus!\n");
132 		return rc;
133 	}
134 
135 	platform_set_drvdata(pdev, bus);
136 
137 	return 0;
138 }
139 
140 static int aspeed_mdio_remove(struct platform_device *pdev)
141 {
142 	mdiobus_unregister(platform_get_drvdata(pdev));
143 
144 	return 0;
145 }
146 
147 static const struct of_device_id aspeed_mdio_of_match[] = {
148 	{ .compatible = "aspeed,ast2600-mdio", },
149 	{ },
150 };
151 
152 static struct platform_driver aspeed_mdio_driver = {
153 	.driver = {
154 		.name = DRV_NAME,
155 		.of_match_table = aspeed_mdio_of_match,
156 	},
157 	.probe = aspeed_mdio_probe,
158 	.remove = aspeed_mdio_remove,
159 };
160 
161 module_platform_driver(aspeed_mdio_driver);
162 
163 MODULE_AUTHOR("Andrew Jeffery <andrew@aj.id.au>");
164 MODULE_LICENSE("GPL");
165