blob: f75e4df626bd70e99522527b3b5208b2f81623c5 [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
Alex Marginean2f624552019-11-25 17:15:12 +020013/* DT node properties for MAC-PHY interface */
14#define PHY_MODE_STR_CNT 2
15static const char *phy_mode_str[PHY_MODE_STR_CNT] = { "phy-mode",
16 "phy-connection-type" };
17/* DT node properties that reference a PHY node */
18#define PHY_HANDLE_STR_CNT 3
19const char *phy_handle_str[PHY_HANDLE_STR_CNT] = { "phy-handle",
20 "phy",
21 "phy-device" };
22
Alex Margineanc3452b52019-06-03 19:10:30 +030023void dm_mdio_probe_devices(void)
24{
25 struct udevice *it;
26 struct uclass *uc;
27
28 uclass_get(UCLASS_MDIO, &uc);
29 uclass_foreach_dev(it, uc) {
30 device_probe(it);
31 }
32}
33
34static int dm_mdio_post_bind(struct udevice *dev)
35{
Alex Marginean6b3abc02019-07-25 12:33:17 +030036 const char *dt_name;
37
38 /* set a custom name for the MDIO device, if present in DT */
39 if (ofnode_valid(dev->node)) {
40 dt_name = ofnode_read_string(dev->node, "device-name");
41 if (dt_name) {
42 debug("renaming dev %s to %s\n", dev->name, dt_name);
43 device_set_name(dev, dt_name);
44 }
45 }
46
Alex Margineanc3452b52019-06-03 19:10:30 +030047 /*
48 * MDIO command doesn't like spaces in names, don't allow them to keep
49 * it happy
50 */
51 if (strchr(dev->name, ' ')) {
52 debug("\nError: MDIO device name \"%s\" has a space!\n",
53 dev->name);
54 return -EINVAL;
55 }
56
57 return 0;
58}
59
60/*
61 * Following read/write/reset functions are registered with legacy MII code.
62 * These are called for PHY operations by upper layers and we further call the
63 * DM MDIO driver functions.
64 */
65static int mdio_read(struct mii_dev *mii_bus, int addr, int devad, int reg)
66{
67 struct udevice *dev = mii_bus->priv;
68
69 return mdio_get_ops(dev)->read(dev, addr, devad, reg);
70}
71
72static int mdio_write(struct mii_dev *mii_bus, int addr, int devad, int reg,
73 u16 val)
74{
75 struct udevice *dev = mii_bus->priv;
76
77 return mdio_get_ops(dev)->write(dev, addr, devad, reg, val);
78}
79
80static int mdio_reset(struct mii_dev *mii_bus)
81{
82 struct udevice *dev = mii_bus->priv;
83
84 if (mdio_get_ops(dev)->reset)
85 return mdio_get_ops(dev)->reset(dev);
86 else
87 return 0;
88}
89
90static int dm_mdio_post_probe(struct udevice *dev)
91{
92 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
93
94 pdata->mii_bus = mdio_alloc();
95 pdata->mii_bus->read = mdio_read;
96 pdata->mii_bus->write = mdio_write;
97 pdata->mii_bus->reset = mdio_reset;
98 pdata->mii_bus->priv = dev;
Joe Hershberger398e7512019-07-30 14:49:56 -050099 strncpy(pdata->mii_bus->name, dev->name, MDIO_NAME_LEN - 1);
Alex Margineanc3452b52019-06-03 19:10:30 +0300100
101 return mdio_register(pdata->mii_bus);
102}
103
104static int dm_mdio_pre_remove(struct udevice *dev)
105{
106 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
107 struct mdio_ops *ops = mdio_get_ops(dev);
108
109 if (ops->reset)
110 ops->reset(dev);
111 mdio_unregister(pdata->mii_bus);
112 mdio_free(pdata->mii_bus);
113
114 return 0;
115}
116
Alex Margineana5d32c32019-11-25 17:15:11 +0200117struct phy_device *dm_mdio_phy_connect(struct udevice *mdiodev, int phyaddr,
Alex Margineanc3452b52019-06-03 19:10:30 +0300118 struct udevice *ethdev,
119 phy_interface_t interface)
120{
Alex Margineana5d32c32019-11-25 17:15:11 +0200121 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(mdiodev);
Alex Margineanc3452b52019-06-03 19:10:30 +0300122
Alex Margineana5d32c32019-11-25 17:15:11 +0200123 if (device_probe(mdiodev))
124 return NULL;
Alex Margineanc3452b52019-06-03 19:10:30 +0300125
Alex Margineana5d32c32019-11-25 17:15:11 +0200126 return phy_connect(pdata->mii_bus, phyaddr, ethdev, interface);
Alex Margineanc3452b52019-06-03 19:10:30 +0300127}
128
Alex Marginean2f624552019-11-25 17:15:12 +0200129static struct phy_device *dm_eth_connect_phy_handle(struct udevice *ethdev,
130 phy_interface_t interface)
131{
132 u32 phy_addr;
133 struct udevice *mdiodev;
134 struct phy_device *phy;
135 struct ofnode_phandle_args phandle = {.node = ofnode_null()};
136 int i;
137
138 for (i = 0; i < PHY_HANDLE_STR_CNT; i++)
139 if (!dev_read_phandle_with_args(ethdev, phy_handle_str[i], NULL,
140 0, 0, &phandle))
141 break;
142
143 if (!ofnode_valid(phandle.node)) {
144 dev_dbg(dev, "can't find PHY node\n");
145 return NULL;
146 }
147
148 /*
149 * reading 'reg' directly should be fine. This is a PHY node, the
150 * address is always size 1 and requires no translation
151 */
152 if (ofnode_read_u32(phandle.node, "reg", &phy_addr)) {
153 dev_dbg(ethdev, "missing reg property in phy node\n");
154 return NULL;
155 }
156
157 if (uclass_get_device_by_ofnode(UCLASS_MDIO,
158 ofnode_get_parent(phandle.node),
159 &mdiodev)) {
160 dev_dbg(dev, "can't find MDIO bus for node %s\n",
161 ofnode_get_name(ofnode_get_parent(phandle.node)));
162 return NULL;
163 }
164
165 phy = dm_mdio_phy_connect(mdiodev, phy_addr, ethdev, interface);
166
167 if (phy)
168 phy->node = phandle.node;
169
170 return phy;
171}
172
173/* Connect to a PHY linked in eth DT node */
174struct phy_device *dm_eth_phy_connect(struct udevice *ethdev)
175{
176 const char *if_str;
177 phy_interface_t interface;
178 struct phy_device *phy;
179 int i;
180
181 if (!ofnode_valid(ethdev->node)) {
182 debug("%s: supplied eth dev has no DT node!\n", ethdev->name);
183 return NULL;
184 }
185
186 interface = PHY_INTERFACE_MODE_NONE;
187 for (i = 0; i < PHY_MODE_STR_CNT; i++) {
188 if_str = ofnode_read_string(ethdev->node, phy_mode_str[i]);
189 if (if_str) {
190 interface = phy_get_interface_by_name(if_str);
191 break;
192 }
193 }
194 if (interface < 0)
195 interface = PHY_INTERFACE_MODE_NONE;
196 if (interface == PHY_INTERFACE_MODE_NONE)
197 dev_dbg(ethdev, "can't find interface mode, default to NONE\n");
198
199 phy = dm_eth_connect_phy_handle(ethdev, interface);
200
201 if (!phy)
202 return NULL;
203
204 phy->interface = interface;
205
206 return phy;
207}
208
Alex Margineanc3452b52019-06-03 19:10:30 +0300209UCLASS_DRIVER(mdio) = {
210 .id = UCLASS_MDIO,
211 .name = "mdio",
212 .post_bind = dm_mdio_post_bind,
213 .post_probe = dm_mdio_post_probe,
214 .pre_remove = dm_mdio_pre_remove,
215 .per_device_auto_alloc_size = sizeof(struct mdio_perdev_priv),
216};