blob: 5da984ca3f1555ba9ccdf691742f25ab73a1d4e0 [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>
Simon Glassf7ae49f2020-05-10 11:40:05 -06009#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070010#include <malloc.h>
Alex Margineanc3452b52019-06-03 19:10:30 +030011#include <miiphy.h>
12#include <dm/device-internal.h>
Simon Glass336d4612020-02-03 07:36:16 -070013#include <dm/device_compat.h>
Alex Margineanc3452b52019-06-03 19:10:30 +030014#include <dm/uclass-internal.h>
Simon Glass336d4612020-02-03 07:36:16 -070015#include <linux/compat.h>
Alex Margineanc3452b52019-06-03 19:10:30 +030016
Alex Marginean2f624552019-11-25 17:15:12 +020017/* DT node properties for MAC-PHY interface */
18#define PHY_MODE_STR_CNT 2
19static const char *phy_mode_str[PHY_MODE_STR_CNT] = { "phy-mode",
20 "phy-connection-type" };
21/* DT node properties that reference a PHY node */
22#define PHY_HANDLE_STR_CNT 3
23const char *phy_handle_str[PHY_HANDLE_STR_CNT] = { "phy-handle",
24 "phy",
25 "phy-device" };
26
Alex Margineanc3452b52019-06-03 19:10:30 +030027void dm_mdio_probe_devices(void)
28{
29 struct udevice *it;
30 struct uclass *uc;
31
32 uclass_get(UCLASS_MDIO, &uc);
33 uclass_foreach_dev(it, uc) {
34 device_probe(it);
35 }
36}
37
38static int dm_mdio_post_bind(struct udevice *dev)
39{
Alex Marginean6b3abc02019-07-25 12:33:17 +030040 const char *dt_name;
41
42 /* set a custom name for the MDIO device, if present in DT */
Simon Glassf10643c2020-12-19 10:40:14 -070043 if (dev_has_ofnode(dev)) {
44 dt_name = dev_read_string(dev, "device-name");
Alex Marginean6b3abc02019-07-25 12:33:17 +030045 if (dt_name) {
46 debug("renaming dev %s to %s\n", dev->name, dt_name);
47 device_set_name(dev, dt_name);
48 }
49 }
50
Alex Margineanc3452b52019-06-03 19:10:30 +030051 /*
52 * MDIO command doesn't like spaces in names, don't allow them to keep
53 * it happy
54 */
55 if (strchr(dev->name, ' ')) {
56 debug("\nError: MDIO device name \"%s\" has a space!\n",
57 dev->name);
58 return -EINVAL;
59 }
60
61 return 0;
62}
63
64/*
65 * Following read/write/reset functions are registered with legacy MII code.
66 * These are called for PHY operations by upper layers and we further call the
67 * DM MDIO driver functions.
68 */
69static int mdio_read(struct mii_dev *mii_bus, int addr, int devad, int reg)
70{
71 struct udevice *dev = mii_bus->priv;
72
73 return mdio_get_ops(dev)->read(dev, addr, devad, reg);
74}
75
76static int mdio_write(struct mii_dev *mii_bus, int addr, int devad, int reg,
77 u16 val)
78{
79 struct udevice *dev = mii_bus->priv;
80
81 return mdio_get_ops(dev)->write(dev, addr, devad, reg, val);
82}
83
84static int mdio_reset(struct mii_dev *mii_bus)
85{
86 struct udevice *dev = mii_bus->priv;
87
88 if (mdio_get_ops(dev)->reset)
89 return mdio_get_ops(dev)->reset(dev);
90 else
91 return 0;
92}
93
94static int dm_mdio_post_probe(struct udevice *dev)
95{
96 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
97
98 pdata->mii_bus = mdio_alloc();
99 pdata->mii_bus->read = mdio_read;
100 pdata->mii_bus->write = mdio_write;
101 pdata->mii_bus->reset = mdio_reset;
102 pdata->mii_bus->priv = dev;
Joe Hershberger398e7512019-07-30 14:49:56 -0500103 strncpy(pdata->mii_bus->name, dev->name, MDIO_NAME_LEN - 1);
Alex Margineanc3452b52019-06-03 19:10:30 +0300104
105 return mdio_register(pdata->mii_bus);
106}
107
108static int dm_mdio_pre_remove(struct udevice *dev)
109{
110 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
111 struct mdio_ops *ops = mdio_get_ops(dev);
112
113 if (ops->reset)
114 ops->reset(dev);
115 mdio_unregister(pdata->mii_bus);
116 mdio_free(pdata->mii_bus);
117
118 return 0;
119}
120
Alex Margineana5d32c32019-11-25 17:15:11 +0200121struct phy_device *dm_mdio_phy_connect(struct udevice *mdiodev, int phyaddr,
Alex Margineanc3452b52019-06-03 19:10:30 +0300122 struct udevice *ethdev,
123 phy_interface_t interface)
124{
Alex Margineana5d32c32019-11-25 17:15:11 +0200125 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(mdiodev);
Alex Margineanc3452b52019-06-03 19:10:30 +0300126
Alex Margineana5d32c32019-11-25 17:15:11 +0200127 if (device_probe(mdiodev))
128 return NULL;
Alex Margineanc3452b52019-06-03 19:10:30 +0300129
Alex Margineana5d32c32019-11-25 17:15:11 +0200130 return phy_connect(pdata->mii_bus, phyaddr, ethdev, interface);
Alex Margineanc3452b52019-06-03 19:10:30 +0300131}
132
Alex Marginean2f624552019-11-25 17:15:12 +0200133static struct phy_device *dm_eth_connect_phy_handle(struct udevice *ethdev,
134 phy_interface_t interface)
135{
136 u32 phy_addr;
137 struct udevice *mdiodev;
138 struct phy_device *phy;
139 struct ofnode_phandle_args phandle = {.node = ofnode_null()};
140 int i;
141
Rasmus Villemoesbdf31922020-10-05 15:15:16 +0200142 if (CONFIG_IS_ENABLED(PHY_FIXED) &&
143 ofnode_valid(dev_read_subnode(ethdev, "fixed-link"))) {
144 phy = phy_connect(NULL, -1, ethdev, interface);
145 goto out;
146 }
147
Alex Marginean2f624552019-11-25 17:15:12 +0200148 for (i = 0; i < PHY_HANDLE_STR_CNT; i++)
149 if (!dev_read_phandle_with_args(ethdev, phy_handle_str[i], NULL,
150 0, 0, &phandle))
151 break;
152
153 if (!ofnode_valid(phandle.node)) {
Sean Anderson0851bd12020-09-15 10:44:53 -0400154 dev_dbg(ethdev, "can't find PHY node\n");
Alex Marginean2f624552019-11-25 17:15:12 +0200155 return NULL;
156 }
157
158 /*
159 * reading 'reg' directly should be fine. This is a PHY node, the
160 * address is always size 1 and requires no translation
161 */
162 if (ofnode_read_u32(phandle.node, "reg", &phy_addr)) {
163 dev_dbg(ethdev, "missing reg property in phy node\n");
164 return NULL;
165 }
166
167 if (uclass_get_device_by_ofnode(UCLASS_MDIO,
168 ofnode_get_parent(phandle.node),
169 &mdiodev)) {
Sean Anderson0851bd12020-09-15 10:44:53 -0400170 dev_dbg(ethdev, "can't find MDIO bus for node %s\n",
Alex Marginean2f624552019-11-25 17:15:12 +0200171 ofnode_get_name(ofnode_get_parent(phandle.node)));
172 return NULL;
173 }
174
175 phy = dm_mdio_phy_connect(mdiodev, phy_addr, ethdev, interface);
176
Rasmus Villemoesbdf31922020-10-05 15:15:16 +0200177out:
Alex Marginean2f624552019-11-25 17:15:12 +0200178 if (phy)
179 phy->node = phandle.node;
180
181 return phy;
182}
183
184/* Connect to a PHY linked in eth DT node */
185struct phy_device *dm_eth_phy_connect(struct udevice *ethdev)
186{
187 const char *if_str;
188 phy_interface_t interface;
189 struct phy_device *phy;
190 int i;
191
Simon Glassf10643c2020-12-19 10:40:14 -0700192 if (!dev_has_ofnode(ethdev)) {
Alex Marginean2f624552019-11-25 17:15:12 +0200193 debug("%s: supplied eth dev has no DT node!\n", ethdev->name);
194 return NULL;
195 }
196
197 interface = PHY_INTERFACE_MODE_NONE;
198 for (i = 0; i < PHY_MODE_STR_CNT; i++) {
Simon Glassf10643c2020-12-19 10:40:14 -0700199 if_str = dev_read_string(ethdev, phy_mode_str[i]);
Alex Marginean2f624552019-11-25 17:15:12 +0200200 if (if_str) {
201 interface = phy_get_interface_by_name(if_str);
202 break;
203 }
204 }
205 if (interface < 0)
206 interface = PHY_INTERFACE_MODE_NONE;
207 if (interface == PHY_INTERFACE_MODE_NONE)
208 dev_dbg(ethdev, "can't find interface mode, default to NONE\n");
209
210 phy = dm_eth_connect_phy_handle(ethdev, interface);
211
212 if (!phy)
213 return NULL;
214
215 phy->interface = interface;
216
217 return phy;
218}
219
Alex Margineanc3452b52019-06-03 19:10:30 +0300220UCLASS_DRIVER(mdio) = {
221 .id = UCLASS_MDIO,
222 .name = "mdio",
223 .post_bind = dm_mdio_post_bind,
224 .post_probe = dm_mdio_post_probe,
225 .pre_remove = dm_mdio_pre_remove,
Simon Glass41575d82020-12-03 16:55:17 -0700226 .per_device_auto = sizeof(struct mdio_perdev_priv),
Alex Margineanc3452b52019-06-03 19:10:30 +0300227};