27 #include <linux/module.h>
28 #include <linux/kernel.h>
31 #include <linux/sched.h>
32 #include <linux/slab.h>
47 #define MDIO_TIMEOUT 100
49 #define PHY_REG_MASK 0x1f
50 #define PHY_ID_MASK 0x1f
52 #define DEF_OUT_FREQ 2200000
57 #define CONTROL_IDLE BIT(31)
58 #define CONTROL_ENABLE BIT(30)
59 #define CONTROL_MAX_DIV (0xffff)
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)
101 u32 mdio_in,
div, mdio_out_khz, access_time;
104 div = (mdio_in / data->
pdata.bus_freq) - 1;
117 mdio_out_khz = mdio_in / (1000 * (div + 1));
118 access_time = (88 * 1000) / mdio_out_khz;
131 static int davinci_mdio_reset(
struct mii_bus *
bus)
136 __davinci_mdio_reset(data);
143 dev_info(data->
dev,
"davinci mdio revision %d.%d\n",
144 (ver >> 8) & 0xff, ver & 0xff);
150 dev_info(data->
dev,
"detected phy mask %x\n", ~phy_mask);
151 phy_mask = ~phy_mask;
154 dev_warn(data->
dev,
"no live phy, scanning all\n");
183 dev_warn(data->
dev,
"resetting idled controller\n");
184 __davinci_mdio_reset(data);
189 if ((reg & USERACCESS_GO) == 0)
192 dev_err(data->
dev,
"timed out waiting for user access\n");
206 dev_err(data->
dev,
"timed out waiting for idle\n");
219 spin_lock(&data->
lock);
222 spin_unlock(&data->
lock);
230 ret = wait_for_user_access(data);
238 ret = wait_for_user_access(data);
249 spin_unlock(&data->
lock);
254 static int davinci_mdio_write(
struct mii_bus *bus,
int phy_id,
255 int phy_reg,
u16 phy_data)
264 spin_lock(&data->
lock);
267 spin_unlock(&data->
lock);
275 ret = wait_for_user_access(data);
283 ret = wait_for_user_access(data);
289 spin_unlock(&data->
lock);
303 if (of_property_read_u32(node,
"bus_freq", &prop)) {
304 pr_err(
"Missing bus_freq property in the DT.\n");
324 dev_err(dev,
"failed to alloc device data\n");
328 data->
bus = mdiobus_alloc();
330 dev_err(dev,
"failed to alloc mii bus\n");
336 if (davinci_mdio_probe_dt(&data->
pdata, pdev))
345 data->
bus->name = dev_name(dev);
346 data->
bus->read = davinci_mdio_read,
347 data->
bus->write = davinci_mdio_write,
348 data->
bus->reset = davinci_mdio_reset,
353 pm_runtime_get_sync(&pdev->
dev);
355 if (IS_ERR(data->
clk)) {
356 dev_err(dev,
"failed to get device clock\n");
357 ret = PTR_ERR(data->
clk);
368 dev_err(dev,
"could not find register map resource\n");
376 dev_err(dev,
"could not allocate register map resource\n");
383 dev_err(dev,
"could not map mdio registers\n");
395 phy = data->
bus->phy_map[
addr];
397 dev_info(dev,
"phy[%d]: device %s, driver %s\n",
398 phy->
addr, dev_name(&phy->
dev),
399 phy->
drv ? phy->
drv->name :
"unknown");
411 pm_runtime_put_sync(&pdev->
dev);
412 pm_runtime_disable(&pdev->
dev);
431 pm_runtime_put_sync(&pdev->
dev);
432 pm_runtime_disable(&pdev->
dev);
441 static int davinci_mdio_suspend(
struct device *dev)
446 spin_lock(&data->
lock);
454 pm_runtime_put_sync(data->
dev);
457 spin_unlock(&data->
lock);
462 static int davinci_mdio_resume(
struct device *dev)
467 spin_lock(&data->
lock);
468 pm_runtime_put_sync(data->
dev);
476 spin_unlock(&data->
lock);
481 static const struct dev_pm_ops davinci_mdio_pm_ops = {
482 .suspend = davinci_mdio_suspend,
483 .resume = davinci_mdio_resume,
486 static const struct of_device_id davinci_mdio_of_mtable[] = {
487 { .compatible =
"ti,davinci_mdio", },
493 .name =
"davinci_mdio",
495 .pm = &davinci_mdio_pm_ops,
498 .probe = davinci_mdio_probe,
502 static int __init davinci_mdio_init(
void)
508 static void __exit davinci_mdio_exit(
void)