blob: e6146b439b455067a42e5eb25a21f6d089b4307e [file] [log] [blame]
Pramod Kumar98bc8652016-06-10 11:03:49 +05301/*
2 * Copyright 2016 Broadcom
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2, as
6 * published by the Free Software Foundation (the "GPL").
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License version 2 (GPLv2) for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * version 2 (GPLv2) along with this source code.
15 */
16
17#include <linux/platform_device.h>
18#include <linux/device.h>
19#include <linux/of_mdio.h>
20#include <linux/module.h>
21#include <linux/phy.h>
22#include <linux/mdio-mux.h>
23#include <linux/delay.h>
24
Arun Parameswaran5634cb22018-08-01 17:53:49 -070025#define MDIO_SCAN_CTRL_OFFSET 0x008
26#define MDIO_SCAN_CTRL_OVRIDE_EXT_MSTR 28
27
Arun Parameswaran77fefa92018-08-01 17:53:47 -070028#define MDIO_PARAM_OFFSET 0x23c
Pramod Kumar98bc8652016-06-10 11:03:49 +053029#define MDIO_PARAM_MIIM_CYCLE 29
30#define MDIO_PARAM_INTERNAL_SEL 25
31#define MDIO_PARAM_BUS_ID 22
32#define MDIO_PARAM_C45_SEL 21
33#define MDIO_PARAM_PHY_ID 16
34#define MDIO_PARAM_PHY_DATA 0
35
Arun Parameswaran77fefa92018-08-01 17:53:47 -070036#define MDIO_READ_OFFSET 0x240
Pramod Kumar98bc8652016-06-10 11:03:49 +053037#define MDIO_READ_DATA_MASK 0xffff
Arun Parameswaran77fefa92018-08-01 17:53:47 -070038#define MDIO_ADDR_OFFSET 0x244
Pramod Kumar98bc8652016-06-10 11:03:49 +053039
Arun Parameswaran77fefa92018-08-01 17:53:47 -070040#define MDIO_CTRL_OFFSET 0x248
Pramod Kumar98bc8652016-06-10 11:03:49 +053041#define MDIO_CTRL_WRITE_OP 0x1
42#define MDIO_CTRL_READ_OP 0x2
43
Arun Parameswaran77fefa92018-08-01 17:53:47 -070044#define MDIO_STAT_OFFSET 0x24c
Pramod Kumar98bc8652016-06-10 11:03:49 +053045#define MDIO_STAT_DONE 1
46
47#define BUS_MAX_ADDR 32
48#define EXT_BUS_START_ADDR 16
49
Arun Parameswaran77fefa92018-08-01 17:53:47 -070050#define MDIO_REG_ADDR_SPACE_SIZE 0x250
51
Pramod Kumar98bc8652016-06-10 11:03:49 +053052struct iproc_mdiomux_desc {
53 void *mux_handle;
54 void __iomem *base;
55 struct device *dev;
56 struct mii_bus *mii_bus;
57};
58
Arun Parameswaran5634cb22018-08-01 17:53:49 -070059static void mdio_mux_iproc_config(struct iproc_mdiomux_desc *md)
60{
61 u32 val;
62
63 /* Disable external mdio master access */
64 val = readl(md->base + MDIO_SCAN_CTRL_OFFSET);
65 val |= BIT(MDIO_SCAN_CTRL_OVRIDE_EXT_MSTR);
66 writel(val, md->base + MDIO_SCAN_CTRL_OFFSET);
67}
68
Pramod Kumar98bc8652016-06-10 11:03:49 +053069static int iproc_mdio_wait_for_idle(void __iomem *base, bool result)
70{
71 unsigned int timeout = 1000; /* loop for 1s */
72 u32 val;
73
74 do {
75 val = readl(base + MDIO_STAT_OFFSET);
76 if ((val & MDIO_STAT_DONE) == result)
77 return 0;
78
79 usleep_range(1000, 2000);
80 } while (timeout--);
81
82 return -ETIMEDOUT;
83}
84
85/* start_miim_ops- Program and start MDIO transaction over mdio bus.
86 * @base: Base address
87 * @phyid: phyid of the selected bus.
88 * @reg: register offset to be read/written.
89 * @val :0 if read op else value to be written in @reg;
90 * @op: Operation that need to be carried out.
91 * MDIO_CTRL_READ_OP: Read transaction.
92 * MDIO_CTRL_WRITE_OP: Write transaction.
93 *
94 * Return value: Successful Read operation returns read reg values and write
95 * operation returns 0. Failure operation returns negative error code.
96 */
97static int start_miim_ops(void __iomem *base,
98 u16 phyid, u32 reg, u16 val, u32 op)
99{
100 u32 param;
101 int ret;
102
103 writel(0, base + MDIO_CTRL_OFFSET);
104 ret = iproc_mdio_wait_for_idle(base, 0);
105 if (ret)
106 goto err;
107
108 param = readl(base + MDIO_PARAM_OFFSET);
109 param |= phyid << MDIO_PARAM_PHY_ID;
110 param |= val << MDIO_PARAM_PHY_DATA;
111 if (reg & MII_ADDR_C45)
112 param |= BIT(MDIO_PARAM_C45_SEL);
113
114 writel(param, base + MDIO_PARAM_OFFSET);
115
116 writel(reg, base + MDIO_ADDR_OFFSET);
117
118 writel(op, base + MDIO_CTRL_OFFSET);
119
120 ret = iproc_mdio_wait_for_idle(base, 1);
121 if (ret)
122 goto err;
123
124 if (op == MDIO_CTRL_READ_OP)
125 ret = readl(base + MDIO_READ_OFFSET) & MDIO_READ_DATA_MASK;
126err:
127 return ret;
128}
129
130static int iproc_mdiomux_read(struct mii_bus *bus, int phyid, int reg)
131{
132 struct iproc_mdiomux_desc *md = bus->priv;
133 int ret;
134
135 ret = start_miim_ops(md->base, phyid, reg, 0, MDIO_CTRL_READ_OP);
136 if (ret < 0)
137 dev_err(&bus->dev, "mdiomux read operation failed!!!");
138
139 return ret;
140}
141
142static int iproc_mdiomux_write(struct mii_bus *bus,
143 int phyid, int reg, u16 val)
144{
145 struct iproc_mdiomux_desc *md = bus->priv;
146 int ret;
147
148 /* Write val at reg offset */
149 ret = start_miim_ops(md->base, phyid, reg, val, MDIO_CTRL_WRITE_OP);
150 if (ret < 0)
151 dev_err(&bus->dev, "mdiomux write operation failed!!!");
152
153 return ret;
154}
155
156static int mdio_mux_iproc_switch_fn(int current_child, int desired_child,
157 void *data)
158{
159 struct iproc_mdiomux_desc *md = data;
160 u32 param, bus_id;
161 bool bus_dir;
162
163 /* select bus and its properties */
164 bus_dir = (desired_child < EXT_BUS_START_ADDR);
165 bus_id = bus_dir ? desired_child : (desired_child - EXT_BUS_START_ADDR);
166
167 param = (bus_dir ? 1 : 0) << MDIO_PARAM_INTERNAL_SEL;
168 param |= (bus_id << MDIO_PARAM_BUS_ID);
169
170 writel(param, md->base + MDIO_PARAM_OFFSET);
171 return 0;
172}
173
174static int mdio_mux_iproc_probe(struct platform_device *pdev)
175{
176 struct iproc_mdiomux_desc *md;
177 struct mii_bus *bus;
178 struct resource *res;
179 int rc;
180
181 md = devm_kzalloc(&pdev->dev, sizeof(*md), GFP_KERNEL);
182 if (!md)
183 return -ENOMEM;
184 md->dev = &pdev->dev;
185
186 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Arun Parameswaran77fefa92018-08-01 17:53:47 -0700187 if (res->start & 0xfff) {
188 /* For backward compatibility in case the
189 * base address is specified with an offset.
190 */
191 dev_info(&pdev->dev, "fix base address in dt-blob\n");
192 res->start &= ~0xfff;
193 res->end = res->start + MDIO_REG_ADDR_SPACE_SIZE - 1;
194 }
Pramod Kumar98bc8652016-06-10 11:03:49 +0530195 md->base = devm_ioremap_resource(&pdev->dev, res);
196 if (IS_ERR(md->base)) {
197 dev_err(&pdev->dev, "failed to ioremap register\n");
198 return PTR_ERR(md->base);
199 }
200
Arun Parameswaran0fe2cd52018-08-01 17:53:50 -0700201 md->mii_bus = devm_mdiobus_alloc(&pdev->dev);
Pramod Kumar98bc8652016-06-10 11:03:49 +0530202 if (!md->mii_bus) {
203 dev_err(&pdev->dev, "mdiomux bus alloc failed\n");
204 return -ENOMEM;
205 }
206
207 bus = md->mii_bus;
208 bus->priv = md;
209 bus->name = "iProc MDIO mux bus";
210 snprintf(bus->id, MII_BUS_ID_SIZE, "%s-%d", pdev->name, pdev->id);
211 bus->parent = &pdev->dev;
212 bus->read = iproc_mdiomux_read;
213 bus->write = iproc_mdiomux_write;
214
215 bus->phy_mask = ~0;
216 bus->dev.of_node = pdev->dev.of_node;
217 rc = mdiobus_register(bus);
218 if (rc) {
219 dev_err(&pdev->dev, "mdiomux registration failed\n");
Arun Parameswaran0fe2cd52018-08-01 17:53:50 -0700220 return rc;
Pramod Kumar98bc8652016-06-10 11:03:49 +0530221 }
222
223 platform_set_drvdata(pdev, md);
224
Corentin Labbe5482a972017-09-04 18:30:14 +0200225 rc = mdio_mux_init(md->dev, md->dev->of_node, mdio_mux_iproc_switch_fn,
Pramod Kumar98bc8652016-06-10 11:03:49 +0530226 &md->mux_handle, md, md->mii_bus);
227 if (rc) {
228 dev_info(md->dev, "mdiomux initialization failed\n");
Jon Mason922c60e2017-05-08 17:48:35 -0400229 goto out_register;
Pramod Kumar98bc8652016-06-10 11:03:49 +0530230 }
231
Arun Parameswaran5634cb22018-08-01 17:53:49 -0700232 mdio_mux_iproc_config(md);
233
Pramod Kumar98bc8652016-06-10 11:03:49 +0530234 dev_info(md->dev, "iProc mdiomux registered\n");
235 return 0;
Jon Mason922c60e2017-05-08 17:48:35 -0400236
237out_register:
238 mdiobus_unregister(bus);
Pramod Kumar98bc8652016-06-10 11:03:49 +0530239 return rc;
240}
241
242static int mdio_mux_iproc_remove(struct platform_device *pdev)
243{
Anton Vasilyevb0753402018-07-27 18:57:47 +0300244 struct iproc_mdiomux_desc *md = platform_get_drvdata(pdev);
Pramod Kumar98bc8652016-06-10 11:03:49 +0530245
246 mdio_mux_uninit(md->mux_handle);
247 mdiobus_unregister(md->mii_bus);
Pramod Kumar98bc8652016-06-10 11:03:49 +0530248
249 return 0;
250}
251
252static const struct of_device_id mdio_mux_iproc_match[] = {
253 {
254 .compatible = "brcm,mdio-mux-iproc",
255 },
256 {},
257};
258MODULE_DEVICE_TABLE(of, mdio_mux_iproc_match);
259
260static struct platform_driver mdiomux_iproc_driver = {
261 .driver = {
262 .name = "mdio-mux-iproc",
263 .of_match_table = mdio_mux_iproc_match,
264 },
265 .probe = mdio_mux_iproc_probe,
266 .remove = mdio_mux_iproc_remove,
267};
268
269module_platform_driver(mdiomux_iproc_driver);
270
271MODULE_DESCRIPTION("iProc MDIO Mux Bus Driver");
272MODULE_AUTHOR("Pramod Kumar <pramod.kumar@broadcom.com>");
273MODULE_LICENSE("GPL v2");