Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next
[cascardo/linux.git] / drivers / net / ethernet / ti / davinci_mdio.c
1 /*
2  * DaVinci MDIO Module driver
3  *
4  * Copyright (C) 2010 Texas Instruments.
5  *
6  * Shamelessly ripped out of davinci_emac.c, original copyrights follow:
7  *
8  * Copyright (C) 2009 Texas Instruments.
9  *
10  * ---------------------------------------------------------------------------
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  * ---------------------------------------------------------------------------
26  */
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/platform_device.h>
30 #include <linux/delay.h>
31 #include <linux/sched.h>
32 #include <linux/slab.h>
33 #include <linux/phy.h>
34 #include <linux/clk.h>
35 #include <linux/err.h>
36 #include <linux/io.h>
37 #include <linux/pm_runtime.h>
38 #include <linux/davinci_emac.h>
39 #include <linux/of.h>
40 #include <linux/of_device.h>
41
42 /*
43  * This timeout definition is a worst-case ultra defensive measure against
44  * unexpected controller lock ups.  Ideally, we should never ever hit this
45  * scenario in practice.
46  */
47 #define MDIO_TIMEOUT            100 /* msecs */
48
49 #define PHY_REG_MASK            0x1f
50 #define PHY_ID_MASK             0x1f
51
52 #define DEF_OUT_FREQ            2200000         /* 2.2 MHz */
53
54 struct davinci_mdio_regs {
55         u32     version;
56         u32     control;
57 #define CONTROL_IDLE            BIT(31)
58 #define CONTROL_ENABLE          BIT(30)
59 #define CONTROL_MAX_DIV         (0xffff)
60
61         u32     alive;
62         u32     link;
63         u32     linkintraw;
64         u32     linkintmasked;
65         u32     __reserved_0[2];
66         u32     userintraw;
67         u32     userintmasked;
68         u32     userintmaskset;
69         u32     userintmaskclr;
70         u32     __reserved_1[20];
71
72         struct {
73                 u32     access;
74 #define USERACCESS_GO           BIT(31)
75 #define USERACCESS_WRITE        BIT(30)
76 #define USERACCESS_ACK          BIT(29)
77 #define USERACCESS_READ         (0)
78 #define USERACCESS_DATA         (0xffff)
79
80                 u32     physel;
81         }       user[0];
82 };
83
84 struct mdio_platform_data default_pdata = {
85         .bus_freq = DEF_OUT_FREQ,
86 };
87
88 struct davinci_mdio_data {
89         struct mdio_platform_data pdata;
90         struct davinci_mdio_regs __iomem *regs;
91         spinlock_t      lock;
92         struct clk      *clk;
93         struct device   *dev;
94         struct mii_bus  *bus;
95         bool            suspended;
96         unsigned long   access_time; /* jiffies */
97 };
98
99 static void __davinci_mdio_reset(struct davinci_mdio_data *data)
100 {
101         u32 mdio_in, div, mdio_out_khz, access_time;
102
103         mdio_in = clk_get_rate(data->clk);
104         div = (mdio_in / data->pdata.bus_freq) - 1;
105         if (div > CONTROL_MAX_DIV)
106                 div = CONTROL_MAX_DIV;
107
108         /* set enable and clock divider */
109         __raw_writel(div | CONTROL_ENABLE, &data->regs->control);
110
111         /*
112          * One mdio transaction consists of:
113          *      32 bits of preamble
114          *      32 bits of transferred data
115          *      24 bits of bus yield (not needed unless shared?)
116          */
117         mdio_out_khz = mdio_in / (1000 * (div + 1));
118         access_time  = (88 * 1000) / mdio_out_khz;
119
120         /*
121          * In the worst case, we could be kicking off a user-access immediately
122          * after the mdio bus scan state-machine triggered its own read.  If
123          * so, our request could get deferred by one access cycle.  We
124          * defensively allow for 4 access cycles.
125          */
126         data->access_time = usecs_to_jiffies(access_time * 4);
127         if (!data->access_time)
128                 data->access_time = 1;
129 }
130
131 static int davinci_mdio_reset(struct mii_bus *bus)
132 {
133         struct davinci_mdio_data *data = bus->priv;
134         u32 phy_mask, ver;
135
136         __davinci_mdio_reset(data);
137
138         /* wait for scan logic to settle */
139         msleep(PHY_MAX_ADDR * data->access_time);
140
141         /* dump hardware version info */
142         ver = __raw_readl(&data->regs->version);
143         dev_info(data->dev, "davinci mdio revision %d.%d\n",
144                  (ver >> 8) & 0xff, ver & 0xff);
145
146         /* get phy mask from the alive register */
147         phy_mask = __raw_readl(&data->regs->alive);
148         if (phy_mask) {
149                 /* restrict mdio bus to live phys only */
150                 dev_info(data->dev, "detected phy mask %x\n", ~phy_mask);
151                 phy_mask = ~phy_mask;
152         } else {
153                 /* desperately scan all phys */
154                 dev_warn(data->dev, "no live phy, scanning all\n");
155                 phy_mask = 0;
156         }
157         data->bus->phy_mask = phy_mask;
158
159         return 0;
160 }
161
162 /* wait until hardware is ready for another user access */
163 static inline int wait_for_user_access(struct davinci_mdio_data *data)
164 {
165         struct davinci_mdio_regs __iomem *regs = data->regs;
166         unsigned long timeout = jiffies + msecs_to_jiffies(MDIO_TIMEOUT);
167         u32 reg;
168
169         while (time_after(timeout, jiffies)) {
170                 reg = __raw_readl(&regs->user[0].access);
171                 if ((reg & USERACCESS_GO) == 0)
172                         return 0;
173
174                 reg = __raw_readl(&regs->control);
175                 if ((reg & CONTROL_IDLE) == 0)
176                         continue;
177
178                 /*
179                  * An emac soft_reset may have clobbered the mdio controller's
180                  * state machine.  We need to reset and retry the current
181                  * operation
182                  */
183                 dev_warn(data->dev, "resetting idled controller\n");
184                 __davinci_mdio_reset(data);
185                 return -EAGAIN;
186         }
187
188         reg = __raw_readl(&regs->user[0].access);
189         if ((reg & USERACCESS_GO) == 0)
190                 return 0;
191
192         dev_err(data->dev, "timed out waiting for user access\n");
193         return -ETIMEDOUT;
194 }
195
196 /* wait until hardware state machine is idle */
197 static inline int wait_for_idle(struct davinci_mdio_data *data)
198 {
199         struct davinci_mdio_regs __iomem *regs = data->regs;
200         unsigned long timeout = jiffies + msecs_to_jiffies(MDIO_TIMEOUT);
201
202         while (time_after(timeout, jiffies)) {
203                 if (__raw_readl(&regs->control) & CONTROL_IDLE)
204                         return 0;
205         }
206         dev_err(data->dev, "timed out waiting for idle\n");
207         return -ETIMEDOUT;
208 }
209
210 static int davinci_mdio_read(struct mii_bus *bus, int phy_id, int phy_reg)
211 {
212         struct davinci_mdio_data *data = bus->priv;
213         u32 reg;
214         int ret;
215
216         if (phy_reg & ~PHY_REG_MASK || phy_id & ~PHY_ID_MASK)
217                 return -EINVAL;
218
219         spin_lock(&data->lock);
220
221         if (data->suspended) {
222                 spin_unlock(&data->lock);
223                 return -ENODEV;
224         }
225
226         reg = (USERACCESS_GO | USERACCESS_READ | (phy_reg << 21) |
227                (phy_id << 16));
228
229         while (1) {
230                 ret = wait_for_user_access(data);
231                 if (ret == -EAGAIN)
232                         continue;
233                 if (ret < 0)
234                         break;
235
236                 __raw_writel(reg, &data->regs->user[0].access);
237
238                 ret = wait_for_user_access(data);
239                 if (ret == -EAGAIN)
240                         continue;
241                 if (ret < 0)
242                         break;
243
244                 reg = __raw_readl(&data->regs->user[0].access);
245                 ret = (reg & USERACCESS_ACK) ? (reg & USERACCESS_DATA) : -EIO;
246                 break;
247         }
248
249         spin_unlock(&data->lock);
250
251         return ret;
252 }
253
254 static int davinci_mdio_write(struct mii_bus *bus, int phy_id,
255                               int phy_reg, u16 phy_data)
256 {
257         struct davinci_mdio_data *data = bus->priv;
258         u32 reg;
259         int ret;
260
261         if (phy_reg & ~PHY_REG_MASK || phy_id & ~PHY_ID_MASK)
262                 return -EINVAL;
263
264         spin_lock(&data->lock);
265
266         if (data->suspended) {
267                 spin_unlock(&data->lock);
268                 return -ENODEV;
269         }
270
271         reg = (USERACCESS_GO | USERACCESS_WRITE | (phy_reg << 21) |
272                    (phy_id << 16) | (phy_data & USERACCESS_DATA));
273
274         while (1) {
275                 ret = wait_for_user_access(data);
276                 if (ret == -EAGAIN)
277                         continue;
278                 if (ret < 0)
279                         break;
280
281                 __raw_writel(reg, &data->regs->user[0].access);
282
283                 ret = wait_for_user_access(data);
284                 if (ret == -EAGAIN)
285                         continue;
286                 break;
287         }
288
289         spin_unlock(&data->lock);
290
291         return 0;
292 }
293
294 #if IS_ENABLED(CONFIG_OF)
295 static int davinci_mdio_probe_dt(struct mdio_platform_data *data,
296                          struct platform_device *pdev)
297 {
298         struct device_node *node = pdev->dev.of_node;
299         u32 prop;
300
301         if (!node)
302                 return -EINVAL;
303
304         if (of_property_read_u32(node, "bus_freq", &prop)) {
305                 pr_err("Missing bus_freq property in the DT.\n");
306                 return -EINVAL;
307         }
308         data->bus_freq = prop;
309
310         return 0;
311 }
312 #endif
313
314 static int davinci_mdio_probe(struct platform_device *pdev)
315 {
316         struct mdio_platform_data *pdata = pdev->dev.platform_data;
317         struct device *dev = &pdev->dev;
318         struct davinci_mdio_data *data;
319         struct resource *res;
320         struct phy_device *phy;
321         int ret, addr;
322
323         data = kzalloc(sizeof(*data), GFP_KERNEL);
324         if (!data)
325                 return -ENOMEM;
326
327         data->bus = mdiobus_alloc();
328         if (!data->bus) {
329                 dev_err(dev, "failed to alloc mii bus\n");
330                 ret = -ENOMEM;
331                 goto bail_out;
332         }
333
334         if (dev->of_node) {
335                 if (davinci_mdio_probe_dt(&data->pdata, pdev))
336                         data->pdata = default_pdata;
337                 snprintf(data->bus->id, MII_BUS_ID_SIZE, "%s", pdev->name);
338         } else {
339                 data->pdata = pdata ? (*pdata) : default_pdata;
340                 snprintf(data->bus->id, MII_BUS_ID_SIZE, "%s-%x",
341                          pdev->name, pdev->id);
342         }
343
344         data->bus->name         = dev_name(dev);
345         data->bus->read         = davinci_mdio_read,
346         data->bus->write        = davinci_mdio_write,
347         data->bus->reset        = davinci_mdio_reset,
348         data->bus->parent       = dev;
349         data->bus->priv         = data;
350
351         pm_runtime_enable(&pdev->dev);
352         pm_runtime_get_sync(&pdev->dev);
353         data->clk = clk_get(&pdev->dev, "fck");
354         if (IS_ERR(data->clk)) {
355                 dev_err(dev, "failed to get device clock\n");
356                 ret = PTR_ERR(data->clk);
357                 data->clk = NULL;
358                 goto bail_out;
359         }
360
361         dev_set_drvdata(dev, data);
362         data->dev = dev;
363         spin_lock_init(&data->lock);
364
365         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
366         if (!res) {
367                 dev_err(dev, "could not find register map resource\n");
368                 ret = -ENOENT;
369                 goto bail_out;
370         }
371
372         res = devm_request_mem_region(dev, res->start, resource_size(res),
373                                             dev_name(dev));
374         if (!res) {
375                 dev_err(dev, "could not allocate register map resource\n");
376                 ret = -ENXIO;
377                 goto bail_out;
378         }
379
380         data->regs = devm_ioremap_nocache(dev, res->start, resource_size(res));
381         if (!data->regs) {
382                 dev_err(dev, "could not map mdio registers\n");
383                 ret = -ENOMEM;
384                 goto bail_out;
385         }
386
387         /* register the mii bus */
388         ret = mdiobus_register(data->bus);
389         if (ret)
390                 goto bail_out;
391
392         /* scan and dump the bus */
393         for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
394                 phy = data->bus->phy_map[addr];
395                 if (phy) {
396                         dev_info(dev, "phy[%d]: device %s, driver %s\n",
397                                  phy->addr, dev_name(&phy->dev),
398                                  phy->drv ? phy->drv->name : "unknown");
399                 }
400         }
401
402         return 0;
403
404 bail_out:
405         if (data->bus)
406                 mdiobus_free(data->bus);
407
408         if (data->clk)
409                 clk_put(data->clk);
410         pm_runtime_put_sync(&pdev->dev);
411         pm_runtime_disable(&pdev->dev);
412
413         kfree(data);
414
415         return ret;
416 }
417
418 static int davinci_mdio_remove(struct platform_device *pdev)
419 {
420         struct device *dev = &pdev->dev;
421         struct davinci_mdio_data *data = dev_get_drvdata(dev);
422
423         if (data->bus) {
424                 mdiobus_unregister(data->bus);
425                 mdiobus_free(data->bus);
426         }
427
428         if (data->clk)
429                 clk_put(data->clk);
430         pm_runtime_put_sync(&pdev->dev);
431         pm_runtime_disable(&pdev->dev);
432
433         dev_set_drvdata(dev, NULL);
434
435         kfree(data);
436
437         return 0;
438 }
439
440 static int davinci_mdio_suspend(struct device *dev)
441 {
442         struct davinci_mdio_data *data = dev_get_drvdata(dev);
443         u32 ctrl;
444
445         spin_lock(&data->lock);
446
447         /* shutdown the scan state machine */
448         ctrl = __raw_readl(&data->regs->control);
449         ctrl &= ~CONTROL_ENABLE;
450         __raw_writel(ctrl, &data->regs->control);
451         wait_for_idle(data);
452
453         data->suspended = true;
454         spin_unlock(&data->lock);
455         pm_runtime_put_sync(data->dev);
456
457         return 0;
458 }
459
460 static int davinci_mdio_resume(struct device *dev)
461 {
462         struct davinci_mdio_data *data = dev_get_drvdata(dev);
463
464         pm_runtime_get_sync(data->dev);
465
466         spin_lock(&data->lock);
467         /* restart the scan state machine */
468         __davinci_mdio_reset(data);
469
470         data->suspended = false;
471         spin_unlock(&data->lock);
472
473         return 0;
474 }
475
476 static const struct dev_pm_ops davinci_mdio_pm_ops = {
477         .suspend_late   = davinci_mdio_suspend,
478         .resume_early   = davinci_mdio_resume,
479 };
480
481 #if IS_ENABLED(CONFIG_OF)
482 static const struct of_device_id davinci_mdio_of_mtable[] = {
483         { .compatible = "ti,davinci_mdio", },
484         { /* sentinel */ },
485 };
486 MODULE_DEVICE_TABLE(of, davinci_mdio_of_mtable);
487 #endif
488
489 static struct platform_driver davinci_mdio_driver = {
490         .driver = {
491                 .name    = "davinci_mdio",
492                 .owner   = THIS_MODULE,
493                 .pm      = &davinci_mdio_pm_ops,
494                 .of_match_table = of_match_ptr(davinci_mdio_of_mtable),
495         },
496         .probe = davinci_mdio_probe,
497         .remove = davinci_mdio_remove,
498 };
499
500 static int __init davinci_mdio_init(void)
501 {
502         return platform_driver_register(&davinci_mdio_driver);
503 }
504 device_initcall(davinci_mdio_init);
505
506 static void __exit davinci_mdio_exit(void)
507 {
508         platform_driver_unregister(&davinci_mdio_driver);
509 }
510 module_exit(davinci_mdio_exit);
511
512 MODULE_LICENSE("GPL");
513 MODULE_DESCRIPTION("DaVinci MDIO driver");