blob: 7a5f1d6dcc83c7f9569a4e0912857014bafbb9e3 [file] [log] [blame]
Alex Margineanc3452b52019-06-03 19:10:30 +03001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2019
4 * Alex Marginean, NXP
5 */
6
7#include <common.h>
8#include <dm.h>
9#include <miiphy.h>
10#include <dm/device-internal.h>
11#include <dm/uclass-internal.h>
12
13void dm_mdio_probe_devices(void)
14{
15 struct udevice *it;
16 struct uclass *uc;
17
18 uclass_get(UCLASS_MDIO, &uc);
19 uclass_foreach_dev(it, uc) {
20 device_probe(it);
21 }
22}
23
24static int dm_mdio_post_bind(struct udevice *dev)
25{
Alex Marginean6b3abc02019-07-25 12:33:17 +030026 const char *dt_name;
27
28 /* set a custom name for the MDIO device, if present in DT */
29 if (ofnode_valid(dev->node)) {
30 dt_name = ofnode_read_string(dev->node, "device-name");
31 if (dt_name) {
32 debug("renaming dev %s to %s\n", dev->name, dt_name);
33 device_set_name(dev, dt_name);
34 }
35 }
36
Alex Margineanc3452b52019-06-03 19:10:30 +030037 /*
38 * MDIO command doesn't like spaces in names, don't allow them to keep
39 * it happy
40 */
41 if (strchr(dev->name, ' ')) {
42 debug("\nError: MDIO device name \"%s\" has a space!\n",
43 dev->name);
44 return -EINVAL;
45 }
46
47 return 0;
48}
49
50/*
51 * Following read/write/reset functions are registered with legacy MII code.
52 * These are called for PHY operations by upper layers and we further call the
53 * DM MDIO driver functions.
54 */
55static int mdio_read(struct mii_dev *mii_bus, int addr, int devad, int reg)
56{
57 struct udevice *dev = mii_bus->priv;
58
59 return mdio_get_ops(dev)->read(dev, addr, devad, reg);
60}
61
62static int mdio_write(struct mii_dev *mii_bus, int addr, int devad, int reg,
63 u16 val)
64{
65 struct udevice *dev = mii_bus->priv;
66
67 return mdio_get_ops(dev)->write(dev, addr, devad, reg, val);
68}
69
70static int mdio_reset(struct mii_dev *mii_bus)
71{
72 struct udevice *dev = mii_bus->priv;
73
74 if (mdio_get_ops(dev)->reset)
75 return mdio_get_ops(dev)->reset(dev);
76 else
77 return 0;
78}
79
80static int dm_mdio_post_probe(struct udevice *dev)
81{
82 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
83
84 pdata->mii_bus = mdio_alloc();
85 pdata->mii_bus->read = mdio_read;
86 pdata->mii_bus->write = mdio_write;
87 pdata->mii_bus->reset = mdio_reset;
88 pdata->mii_bus->priv = dev;
Joe Hershberger398e7512019-07-30 14:49:56 -050089 strncpy(pdata->mii_bus->name, dev->name, MDIO_NAME_LEN - 1);
Alex Margineanc3452b52019-06-03 19:10:30 +030090
91 return mdio_register(pdata->mii_bus);
92}
93
94static int dm_mdio_pre_remove(struct udevice *dev)
95{
96 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
97 struct mdio_ops *ops = mdio_get_ops(dev);
98
99 if (ops->reset)
100 ops->reset(dev);
101 mdio_unregister(pdata->mii_bus);
102 mdio_free(pdata->mii_bus);
103
104 return 0;
105}
106
Alex Margineana5d32c32019-11-25 17:15:11 +0200107struct phy_device *dm_mdio_phy_connect(struct udevice *mdiodev, int phyaddr,
Alex Margineanc3452b52019-06-03 19:10:30 +0300108 struct udevice *ethdev,
109 phy_interface_t interface)
110{
Alex Margineana5d32c32019-11-25 17:15:11 +0200111 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(mdiodev);
Alex Margineanc3452b52019-06-03 19:10:30 +0300112
Alex Margineana5d32c32019-11-25 17:15:11 +0200113 if (device_probe(mdiodev))
114 return NULL;
Alex Margineanc3452b52019-06-03 19:10:30 +0300115
Alex Margineana5d32c32019-11-25 17:15:11 +0200116 return phy_connect(pdata->mii_bus, phyaddr, ethdev, interface);
Alex Margineanc3452b52019-06-03 19:10:30 +0300117}
118
119UCLASS_DRIVER(mdio) = {
120 .id = UCLASS_MDIO,
121 .name = "mdio",
122 .post_bind = dm_mdio_post_bind,
123 .post_probe = dm_mdio_post_probe,
124 .pre_remove = dm_mdio_pre_remove,
125 .per_device_auto_alloc_size = sizeof(struct mdio_perdev_priv),
126};