blob: d80037d0ac71d5f6334acdb365f3cdad9f034ace [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>
Bin Meng33aad0b2021-03-14 20:14:47 +080014#include <dm/of_extra.h>
Alex Margineanc3452b52019-06-03 19:10:30 +030015#include <dm/uclass-internal.h>
Simon Glass336d4612020-02-03 07:36:16 -070016#include <linux/compat.h>
Alex Margineanc3452b52019-06-03 19:10:30 +030017
18void dm_mdio_probe_devices(void)
19{
20 struct udevice *it;
21 struct uclass *uc;
22
23 uclass_get(UCLASS_MDIO, &uc);
24 uclass_foreach_dev(it, uc) {
25 device_probe(it);
26 }
27}
28
29static int dm_mdio_post_bind(struct udevice *dev)
30{
Alex Marginean6b3abc02019-07-25 12:33:17 +030031 const char *dt_name;
32
33 /* set a custom name for the MDIO device, if present in DT */
Simon Glassf10643c2020-12-19 10:40:14 -070034 if (dev_has_ofnode(dev)) {
35 dt_name = dev_read_string(dev, "device-name");
Alex Marginean6b3abc02019-07-25 12:33:17 +030036 if (dt_name) {
37 debug("renaming dev %s to %s\n", dev->name, dt_name);
38 device_set_name(dev, dt_name);
39 }
40 }
41
Alex Margineanc3452b52019-06-03 19:10:30 +030042 /*
43 * MDIO command doesn't like spaces in names, don't allow them to keep
44 * it happy
45 */
46 if (strchr(dev->name, ' ')) {
47 debug("\nError: MDIO device name \"%s\" has a space!\n",
48 dev->name);
49 return -EINVAL;
50 }
51
Tim Harveyf54002d2022-11-30 09:42:44 -080052#if CONFIG_IS_ENABLED(OF_REAL)
53 return dm_scan_fdt_dev(dev);
54#else
Alex Margineanc3452b52019-06-03 19:10:30 +030055 return 0;
Tim Harveyf54002d2022-11-30 09:42:44 -080056#endif
Alex Margineanc3452b52019-06-03 19:10:30 +030057}
58
Marek Behún351bfa62022-04-07 00:32:58 +020059int dm_mdio_read(struct udevice *mdio_dev, int addr, int devad, int reg)
60{
61 struct mdio_ops *ops = mdio_get_ops(mdio_dev);
62
63 if (!ops->read)
64 return -ENOSYS;
65
66 return ops->read(mdio_dev, addr, devad, reg);
67}
68
69int dm_mdio_write(struct udevice *mdio_dev, int addr, int devad, int reg,
70 u16 val)
71{
72 struct mdio_ops *ops = mdio_get_ops(mdio_dev);
73
74 if (!ops->write)
75 return -ENOSYS;
76
77 return ops->write(mdio_dev, addr, devad, reg, val);
78}
79
80int dm_mdio_reset(struct udevice *mdio_dev)
81{
82 struct mdio_ops *ops = mdio_get_ops(mdio_dev);
83
84 if (!ops->reset)
85 return 0;
86
87 return ops->reset(mdio_dev);
88}
89
Alex Margineanc3452b52019-06-03 19:10:30 +030090/*
91 * Following read/write/reset functions are registered with legacy MII code.
92 * These are called for PHY operations by upper layers and we further call the
93 * DM MDIO driver functions.
94 */
95static int mdio_read(struct mii_dev *mii_bus, int addr, int devad, int reg)
96{
Marek Behún1776a242022-04-07 00:32:59 +020097 return dm_mdio_read(mii_bus->priv, addr, devad, reg);
Alex Margineanc3452b52019-06-03 19:10:30 +030098}
99
100static int mdio_write(struct mii_dev *mii_bus, int addr, int devad, int reg,
101 u16 val)
102{
Marek Behún1776a242022-04-07 00:32:59 +0200103 return dm_mdio_write(mii_bus->priv, addr, devad, reg, val);
Alex Margineanc3452b52019-06-03 19:10:30 +0300104}
105
106static int mdio_reset(struct mii_dev *mii_bus)
107{
Marek Behún1776a242022-04-07 00:32:59 +0200108 return dm_mdio_reset(mii_bus->priv);
Alex Margineanc3452b52019-06-03 19:10:30 +0300109}
110
111static int dm_mdio_post_probe(struct udevice *dev)
112{
113 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
114
115 pdata->mii_bus = mdio_alloc();
116 pdata->mii_bus->read = mdio_read;
117 pdata->mii_bus->write = mdio_write;
118 pdata->mii_bus->reset = mdio_reset;
119 pdata->mii_bus->priv = dev;
Vladimir Olteanbf35c312021-09-27 14:22:00 +0300120 strlcpy(pdata->mii_bus->name, dev->name, MDIO_NAME_LEN);
Alex Margineanc3452b52019-06-03 19:10:30 +0300121
122 return mdio_register(pdata->mii_bus);
123}
124
125static int dm_mdio_pre_remove(struct udevice *dev)
126{
127 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
Alex Margineanc3452b52019-06-03 19:10:30 +0300128
Marek Behún1776a242022-04-07 00:32:59 +0200129 dm_mdio_reset(dev);
Alex Margineanc3452b52019-06-03 19:10:30 +0300130 mdio_unregister(pdata->mii_bus);
131 mdio_free(pdata->mii_bus);
132
133 return 0;
134}
135
Marek Behún00b1bad2022-04-27 12:41:49 +0200136struct phy_device *dm_phy_find_by_ofnode(ofnode phynode)
137{
138 struct mdio_perdev_priv *pdata;
139 struct udevice *mdiodev;
140 u32 phy_addr;
141
142 if (ofnode_read_u32(phynode, "reg", &phy_addr))
143 return NULL;
144
145 if (uclass_get_device_by_ofnode(UCLASS_MDIO,
146 ofnode_get_parent(phynode),
147 &mdiodev))
148 return NULL;
149
150 if (device_probe(mdiodev))
151 return NULL;
152
153 pdata = dev_get_uclass_priv(mdiodev);
154
155 return phy_find_by_mask(pdata->mii_bus, BIT(phy_addr));
156}
157
Alex Margineana5d32c32019-11-25 17:15:11 +0200158struct phy_device *dm_mdio_phy_connect(struct udevice *mdiodev, int phyaddr,
Alex Margineanc3452b52019-06-03 19:10:30 +0300159 struct udevice *ethdev,
160 phy_interface_t interface)
161{
Alex Margineana5d32c32019-11-25 17:15:11 +0200162 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(mdiodev);
Alex Margineanc3452b52019-06-03 19:10:30 +0300163
Alex Margineana5d32c32019-11-25 17:15:11 +0200164 if (device_probe(mdiodev))
165 return NULL;
Alex Margineanc3452b52019-06-03 19:10:30 +0300166
Alex Margineana5d32c32019-11-25 17:15:11 +0200167 return phy_connect(pdata->mii_bus, phyaddr, ethdev, interface);
Alex Margineanc3452b52019-06-03 19:10:30 +0300168}
169
Alex Marginean2f624552019-11-25 17:15:12 +0200170static struct phy_device *dm_eth_connect_phy_handle(struct udevice *ethdev,
171 phy_interface_t interface)
172{
173 u32 phy_addr;
174 struct udevice *mdiodev;
175 struct phy_device *phy;
Vladimir Olteanf27bc8a2021-03-14 20:14:48 +0800176 ofnode phynode;
Alex Marginean2f624552019-11-25 17:15:12 +0200177
Rasmus Villemoesbdf31922020-10-05 15:15:16 +0200178 if (CONFIG_IS_ENABLED(PHY_FIXED) &&
Vladimir Olteanf27bc8a2021-03-14 20:14:48 +0800179 ofnode_phy_is_fixed_link(dev_ofnode(ethdev), &phynode)) {
180 phy = phy_connect(NULL, 0, ethdev, interface);
Rasmus Villemoesbdf31922020-10-05 15:15:16 +0200181 goto out;
182 }
183
Marek Behúnf3dd2132022-04-07 00:32:57 +0200184 phynode = dev_get_phy_node(ethdev);
185 if (!ofnode_valid(phynode)) {
Sean Anderson0851bd12020-09-15 10:44:53 -0400186 dev_dbg(ethdev, "can't find PHY node\n");
Alex Marginean2f624552019-11-25 17:15:12 +0200187 return NULL;
188 }
189
190 /*
191 * reading 'reg' directly should be fine. This is a PHY node, the
192 * address is always size 1 and requires no translation
193 */
Marek Behúnf3dd2132022-04-07 00:32:57 +0200194 if (ofnode_read_u32(phynode, "reg", &phy_addr)) {
Alex Marginean2f624552019-11-25 17:15:12 +0200195 dev_dbg(ethdev, "missing reg property in phy node\n");
196 return NULL;
197 }
198
199 if (uclass_get_device_by_ofnode(UCLASS_MDIO,
Marek Behúnf3dd2132022-04-07 00:32:57 +0200200 ofnode_get_parent(phynode),
Alex Marginean2f624552019-11-25 17:15:12 +0200201 &mdiodev)) {
Sean Anderson0851bd12020-09-15 10:44:53 -0400202 dev_dbg(ethdev, "can't find MDIO bus for node %s\n",
Marek Behúnf3dd2132022-04-07 00:32:57 +0200203 ofnode_get_name(ofnode_get_parent(phynode)));
Alex Marginean2f624552019-11-25 17:15:12 +0200204 return NULL;
205 }
206
207 phy = dm_mdio_phy_connect(mdiodev, phy_addr, ethdev, interface);
208
Rasmus Villemoesbdf31922020-10-05 15:15:16 +0200209out:
Alex Marginean2f624552019-11-25 17:15:12 +0200210 if (phy)
Marek Behúnf3dd2132022-04-07 00:32:57 +0200211 phy->node = phynode;
Alex Marginean2f624552019-11-25 17:15:12 +0200212
213 return phy;
214}
215
216/* Connect to a PHY linked in eth DT node */
217struct phy_device *dm_eth_phy_connect(struct udevice *ethdev)
218{
Alex Marginean2f624552019-11-25 17:15:12 +0200219 phy_interface_t interface;
220 struct phy_device *phy;
Alex Marginean2f624552019-11-25 17:15:12 +0200221
Simon Glassf10643c2020-12-19 10:40:14 -0700222 if (!dev_has_ofnode(ethdev)) {
Alex Marginean2f624552019-11-25 17:15:12 +0200223 debug("%s: supplied eth dev has no DT node!\n", ethdev->name);
224 return NULL;
225 }
226
Marek Behún123ca112022-04-07 00:33:01 +0200227 interface = dev_read_phy_mode(ethdev);
Marek Behúnffb0f6f2022-04-07 00:33:03 +0200228 if (interface == PHY_INTERFACE_MODE_NA)
229 dev_dbg(ethdev, "can't find interface mode, default to NA\n");
Alex Marginean2f624552019-11-25 17:15:12 +0200230
231 phy = dm_eth_connect_phy_handle(ethdev, interface);
232
233 if (!phy)
234 return NULL;
235
236 phy->interface = interface;
237
238 return phy;
239}
240
Alex Margineanc3452b52019-06-03 19:10:30 +0300241UCLASS_DRIVER(mdio) = {
242 .id = UCLASS_MDIO,
243 .name = "mdio",
244 .post_bind = dm_mdio_post_bind,
245 .post_probe = dm_mdio_post_probe,
246 .pre_remove = dm_mdio_pre_remove,
Simon Glass41575d82020-12-03 16:55:17 -0700247 .per_device_auto = sizeof(struct mdio_perdev_priv),
Alex Margineanc3452b52019-06-03 19:10:30 +0300248};