blob: 5735afe49e1025c5747cb475d37422bfc1b4dc73 [file] [log] [blame]
Alex Marginean1a5b0982019-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 Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070010#include <malloc.h>
Alex Marginean1a5b0982019-06-03 19:10:30 +030011#include <miiphy.h>
12#include <dm/device-internal.h>
Simon Glass9bc15642020-02-03 07:36:16 -070013#include <dm/device_compat.h>
Bin Menge0518f02021-03-14 20:14:47 +080014#include <dm/of_extra.h>
Alex Marginean1a5b0982019-06-03 19:10:30 +030015#include <dm/uclass-internal.h>
Simon Glass9bc15642020-02-03 07:36:16 -070016#include <linux/compat.h>
Alex Marginean1a5b0982019-06-03 19:10:30 +030017
Alex Margineancf640552019-11-25 17:15:12 +020018/* DT node properties for MAC-PHY interface */
19#define PHY_MODE_STR_CNT 2
Marek BehĂșn2a106d82022-04-07 00:32:55 +020020static const char * const phy_mode_str[PHY_MODE_STR_CNT] = {
21 "phy-mode", "phy-connection-type"
22};
Alex Margineancf640552019-11-25 17:15:12 +020023/* DT node properties that reference a PHY node */
24#define PHY_HANDLE_STR_CNT 3
Marek BehĂșn2a106d82022-04-07 00:32:55 +020025static const char * const phy_handle_str[PHY_HANDLE_STR_CNT] = {
26 "phy-handle", "phy", "phy-device"
27};
Alex Margineancf640552019-11-25 17:15:12 +020028
Alex Marginean1a5b0982019-06-03 19:10:30 +030029void dm_mdio_probe_devices(void)
30{
31 struct udevice *it;
32 struct uclass *uc;
33
34 uclass_get(UCLASS_MDIO, &uc);
35 uclass_foreach_dev(it, uc) {
36 device_probe(it);
37 }
38}
39
40static int dm_mdio_post_bind(struct udevice *dev)
41{
Alex Marginean2e859ad2019-07-25 12:33:17 +030042 const char *dt_name;
43
44 /* set a custom name for the MDIO device, if present in DT */
Simon Glassa7ece582020-12-19 10:40:14 -070045 if (dev_has_ofnode(dev)) {
46 dt_name = dev_read_string(dev, "device-name");
Alex Marginean2e859ad2019-07-25 12:33:17 +030047 if (dt_name) {
48 debug("renaming dev %s to %s\n", dev->name, dt_name);
49 device_set_name(dev, dt_name);
50 }
51 }
52
Alex Marginean1a5b0982019-06-03 19:10:30 +030053 /*
54 * MDIO command doesn't like spaces in names, don't allow them to keep
55 * it happy
56 */
57 if (strchr(dev->name, ' ')) {
58 debug("\nError: MDIO device name \"%s\" has a space!\n",
59 dev->name);
60 return -EINVAL;
61 }
62
63 return 0;
64}
65
66/*
67 * Following read/write/reset functions are registered with legacy MII code.
68 * These are called for PHY operations by upper layers and we further call the
69 * DM MDIO driver functions.
70 */
71static int mdio_read(struct mii_dev *mii_bus, int addr, int devad, int reg)
72{
73 struct udevice *dev = mii_bus->priv;
74
75 return mdio_get_ops(dev)->read(dev, addr, devad, reg);
76}
77
78static int mdio_write(struct mii_dev *mii_bus, int addr, int devad, int reg,
79 u16 val)
80{
81 struct udevice *dev = mii_bus->priv;
82
83 return mdio_get_ops(dev)->write(dev, addr, devad, reg, val);
84}
85
86static int mdio_reset(struct mii_dev *mii_bus)
87{
88 struct udevice *dev = mii_bus->priv;
89
90 if (mdio_get_ops(dev)->reset)
91 return mdio_get_ops(dev)->reset(dev);
92 else
93 return 0;
94}
95
96static int dm_mdio_post_probe(struct udevice *dev)
97{
98 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
99
100 pdata->mii_bus = mdio_alloc();
101 pdata->mii_bus->read = mdio_read;
102 pdata->mii_bus->write = mdio_write;
103 pdata->mii_bus->reset = mdio_reset;
104 pdata->mii_bus->priv = dev;
Vladimir Oltean54045442021-09-27 14:22:00 +0300105 strlcpy(pdata->mii_bus->name, dev->name, MDIO_NAME_LEN);
Alex Marginean1a5b0982019-06-03 19:10:30 +0300106
107 return mdio_register(pdata->mii_bus);
108}
109
110static int dm_mdio_pre_remove(struct udevice *dev)
111{
112 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
113 struct mdio_ops *ops = mdio_get_ops(dev);
114
115 if (ops->reset)
116 ops->reset(dev);
117 mdio_unregister(pdata->mii_bus);
118 mdio_free(pdata->mii_bus);
119
120 return 0;
121}
122
Alex Marginean6b3089e2019-11-25 17:15:11 +0200123struct phy_device *dm_mdio_phy_connect(struct udevice *mdiodev, int phyaddr,
Alex Marginean1a5b0982019-06-03 19:10:30 +0300124 struct udevice *ethdev,
125 phy_interface_t interface)
126{
Alex Marginean6b3089e2019-11-25 17:15:11 +0200127 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(mdiodev);
Alex Marginean1a5b0982019-06-03 19:10:30 +0300128
Alex Marginean6b3089e2019-11-25 17:15:11 +0200129 if (device_probe(mdiodev))
130 return NULL;
Alex Marginean1a5b0982019-06-03 19:10:30 +0300131
Alex Marginean6b3089e2019-11-25 17:15:11 +0200132 return phy_connect(pdata->mii_bus, phyaddr, ethdev, interface);
Alex Marginean1a5b0982019-06-03 19:10:30 +0300133}
134
Alex Margineancf640552019-11-25 17:15:12 +0200135static struct phy_device *dm_eth_connect_phy_handle(struct udevice *ethdev,
136 phy_interface_t interface)
137{
138 u32 phy_addr;
139 struct udevice *mdiodev;
140 struct phy_device *phy;
141 struct ofnode_phandle_args phandle = {.node = ofnode_null()};
Vladimir Oltean6ca194a2021-03-14 20:14:48 +0800142 ofnode phynode;
Alex Margineancf640552019-11-25 17:15:12 +0200143 int i;
144
Rasmus Villemoese5051642020-10-05 15:15:16 +0200145 if (CONFIG_IS_ENABLED(PHY_FIXED) &&
Vladimir Oltean6ca194a2021-03-14 20:14:48 +0800146 ofnode_phy_is_fixed_link(dev_ofnode(ethdev), &phynode)) {
147 phy = phy_connect(NULL, 0, ethdev, interface);
148 phandle.node = phynode;
Rasmus Villemoese5051642020-10-05 15:15:16 +0200149 goto out;
150 }
151
Alex Margineancf640552019-11-25 17:15:12 +0200152 for (i = 0; i < PHY_HANDLE_STR_CNT; i++)
153 if (!dev_read_phandle_with_args(ethdev, phy_handle_str[i], NULL,
154 0, 0, &phandle))
155 break;
156
157 if (!ofnode_valid(phandle.node)) {
Sean Anderson836cb5e2020-09-15 10:44:53 -0400158 dev_dbg(ethdev, "can't find PHY node\n");
Alex Margineancf640552019-11-25 17:15:12 +0200159 return NULL;
160 }
161
162 /*
163 * reading 'reg' directly should be fine. This is a PHY node, the
164 * address is always size 1 and requires no translation
165 */
166 if (ofnode_read_u32(phandle.node, "reg", &phy_addr)) {
167 dev_dbg(ethdev, "missing reg property in phy node\n");
168 return NULL;
169 }
170
171 if (uclass_get_device_by_ofnode(UCLASS_MDIO,
172 ofnode_get_parent(phandle.node),
173 &mdiodev)) {
Sean Anderson836cb5e2020-09-15 10:44:53 -0400174 dev_dbg(ethdev, "can't find MDIO bus for node %s\n",
Alex Margineancf640552019-11-25 17:15:12 +0200175 ofnode_get_name(ofnode_get_parent(phandle.node)));
176 return NULL;
177 }
178
179 phy = dm_mdio_phy_connect(mdiodev, phy_addr, ethdev, interface);
180
Rasmus Villemoese5051642020-10-05 15:15:16 +0200181out:
Alex Margineancf640552019-11-25 17:15:12 +0200182 if (phy)
183 phy->node = phandle.node;
184
185 return phy;
186}
187
188/* Connect to a PHY linked in eth DT node */
189struct phy_device *dm_eth_phy_connect(struct udevice *ethdev)
190{
191 const char *if_str;
192 phy_interface_t interface;
193 struct phy_device *phy;
194 int i;
195
Simon Glassa7ece582020-12-19 10:40:14 -0700196 if (!dev_has_ofnode(ethdev)) {
Alex Margineancf640552019-11-25 17:15:12 +0200197 debug("%s: supplied eth dev has no DT node!\n", ethdev->name);
198 return NULL;
199 }
200
201 interface = PHY_INTERFACE_MODE_NONE;
202 for (i = 0; i < PHY_MODE_STR_CNT; i++) {
Simon Glassa7ece582020-12-19 10:40:14 -0700203 if_str = dev_read_string(ethdev, phy_mode_str[i]);
Alex Margineancf640552019-11-25 17:15:12 +0200204 if (if_str) {
205 interface = phy_get_interface_by_name(if_str);
206 break;
207 }
208 }
209 if (interface < 0)
210 interface = PHY_INTERFACE_MODE_NONE;
211 if (interface == PHY_INTERFACE_MODE_NONE)
212 dev_dbg(ethdev, "can't find interface mode, default to NONE\n");
213
214 phy = dm_eth_connect_phy_handle(ethdev, interface);
215
216 if (!phy)
217 return NULL;
218
219 phy->interface = interface;
220
221 return phy;
222}
223
Alex Marginean1a5b0982019-06-03 19:10:30 +0300224UCLASS_DRIVER(mdio) = {
225 .id = UCLASS_MDIO,
226 .name = "mdio",
227 .post_bind = dm_mdio_post_bind,
228 .post_probe = dm_mdio_post_probe,
229 .pre_remove = dm_mdio_pre_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700230 .per_device_auto = sizeof(struct mdio_perdev_priv),
Alex Marginean1a5b0982019-06-03 19:10:30 +0300231};