blob: 233b70171b5af03d814feb3fecd17f62f4aa5dee [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
Alex Marginean2f624552019-11-25 17:15:12 +020018/* DT node properties for MAC-PHY interface */
Marek Behúna7a96ef2022-04-07 00:32:56 +020019static const char * const phy_mode_str[] = {
Marek Behún6fb44822022-04-07 00:32:55 +020020 "phy-mode", "phy-connection-type"
21};
Marek Behúna7a96ef2022-04-07 00:32:56 +020022
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 */
Simon Glassf10643c2020-12-19 10:40:14 -070039 if (dev_has_ofnode(dev)) {
40 dt_name = dev_read_string(dev, "device-name");
Alex Marginean6b3abc02019-07-25 12:33:17 +030041 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;
Vladimir Olteanbf35c312021-09-27 14:22:00 +030099 strlcpy(pdata->mii_bus->name, dev->name, MDIO_NAME_LEN);
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;
Vladimir Olteanf27bc8a2021-03-14 20:14:48 +0800135 ofnode phynode;
Alex Marginean2f624552019-11-25 17:15:12 +0200136
Rasmus Villemoesbdf31922020-10-05 15:15:16 +0200137 if (CONFIG_IS_ENABLED(PHY_FIXED) &&
Vladimir Olteanf27bc8a2021-03-14 20:14:48 +0800138 ofnode_phy_is_fixed_link(dev_ofnode(ethdev), &phynode)) {
139 phy = phy_connect(NULL, 0, ethdev, interface);
Rasmus Villemoesbdf31922020-10-05 15:15:16 +0200140 goto out;
141 }
142
Marek Behúnf3dd2132022-04-07 00:32:57 +0200143 phynode = dev_get_phy_node(ethdev);
144 if (!ofnode_valid(phynode)) {
Sean Anderson0851bd12020-09-15 10:44:53 -0400145 dev_dbg(ethdev, "can't find PHY node\n");
Alex Marginean2f624552019-11-25 17:15:12 +0200146 return NULL;
147 }
148
149 /*
150 * reading 'reg' directly should be fine. This is a PHY node, the
151 * address is always size 1 and requires no translation
152 */
Marek Behúnf3dd2132022-04-07 00:32:57 +0200153 if (ofnode_read_u32(phynode, "reg", &phy_addr)) {
Alex Marginean2f624552019-11-25 17:15:12 +0200154 dev_dbg(ethdev, "missing reg property in phy node\n");
155 return NULL;
156 }
157
158 if (uclass_get_device_by_ofnode(UCLASS_MDIO,
Marek Behúnf3dd2132022-04-07 00:32:57 +0200159 ofnode_get_parent(phynode),
Alex Marginean2f624552019-11-25 17:15:12 +0200160 &mdiodev)) {
Sean Anderson0851bd12020-09-15 10:44:53 -0400161 dev_dbg(ethdev, "can't find MDIO bus for node %s\n",
Marek Behúnf3dd2132022-04-07 00:32:57 +0200162 ofnode_get_name(ofnode_get_parent(phynode)));
Alex Marginean2f624552019-11-25 17:15:12 +0200163 return NULL;
164 }
165
166 phy = dm_mdio_phy_connect(mdiodev, phy_addr, ethdev, interface);
167
Rasmus Villemoesbdf31922020-10-05 15:15:16 +0200168out:
Alex Marginean2f624552019-11-25 17:15:12 +0200169 if (phy)
Marek Behúnf3dd2132022-04-07 00:32:57 +0200170 phy->node = phynode;
Alex Marginean2f624552019-11-25 17:15:12 +0200171
172 return phy;
173}
174
175/* Connect to a PHY linked in eth DT node */
176struct phy_device *dm_eth_phy_connect(struct udevice *ethdev)
177{
178 const char *if_str;
179 phy_interface_t interface;
180 struct phy_device *phy;
181 int i;
182
Simon Glassf10643c2020-12-19 10:40:14 -0700183 if (!dev_has_ofnode(ethdev)) {
Alex Marginean2f624552019-11-25 17:15:12 +0200184 debug("%s: supplied eth dev has no DT node!\n", ethdev->name);
185 return NULL;
186 }
187
188 interface = PHY_INTERFACE_MODE_NONE;
Marek Behúna7a96ef2022-04-07 00:32:56 +0200189 for (i = 0; i < ARRAY_SIZE(phy_mode_str); i++) {
Simon Glassf10643c2020-12-19 10:40:14 -0700190 if_str = dev_read_string(ethdev, phy_mode_str[i]);
Alex Marginean2f624552019-11-25 17:15:12 +0200191 if (if_str) {
192 interface = phy_get_interface_by_name(if_str);
193 break;
194 }
195 }
196 if (interface < 0)
197 interface = PHY_INTERFACE_MODE_NONE;
198 if (interface == PHY_INTERFACE_MODE_NONE)
199 dev_dbg(ethdev, "can't find interface mode, default to NONE\n");
200
201 phy = dm_eth_connect_phy_handle(ethdev, interface);
202
203 if (!phy)
204 return NULL;
205
206 phy->interface = interface;
207
208 return phy;
209}
210
Alex Margineanc3452b52019-06-03 19:10:30 +0300211UCLASS_DRIVER(mdio) = {
212 .id = UCLASS_MDIO,
213 .name = "mdio",
214 .post_bind = dm_mdio_post_bind,
215 .post_probe = dm_mdio_post_probe,
216 .pre_remove = dm_mdio_pre_remove,
Simon Glass41575d82020-12-03 16:55:17 -0700217 .per_device_auto = sizeof(struct mdio_perdev_priv),
Alex Margineanc3452b52019-06-03 19:10:30 +0300218};