blob: 1b687765b8ca2263d927d27e054c748833a908d7 [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 */
19#define PHY_MODE_STR_CNT 2
20static const char *phy_mode_str[PHY_MODE_STR_CNT] = { "phy-mode",
21 "phy-connection-type" };
22/* DT node properties that reference a PHY node */
23#define PHY_HANDLE_STR_CNT 3
24const char *phy_handle_str[PHY_HANDLE_STR_CNT] = { "phy-handle",
25 "phy",
26 "phy-device" };
27
Alex Margineanc3452b52019-06-03 19:10:30 +030028void dm_mdio_probe_devices(void)
29{
30 struct udevice *it;
31 struct uclass *uc;
32
33 uclass_get(UCLASS_MDIO, &uc);
34 uclass_foreach_dev(it, uc) {
35 device_probe(it);
36 }
37}
38
39static int dm_mdio_post_bind(struct udevice *dev)
40{
Alex Marginean6b3abc02019-07-25 12:33:17 +030041 const char *dt_name;
42
43 /* set a custom name for the MDIO device, if present in DT */
Simon Glassf10643c2020-12-19 10:40:14 -070044 if (dev_has_ofnode(dev)) {
45 dt_name = dev_read_string(dev, "device-name");
Alex Marginean6b3abc02019-07-25 12:33:17 +030046 if (dt_name) {
47 debug("renaming dev %s to %s\n", dev->name, dt_name);
48 device_set_name(dev, dt_name);
49 }
50 }
51
Alex Margineanc3452b52019-06-03 19:10:30 +030052 /*
53 * MDIO command doesn't like spaces in names, don't allow them to keep
54 * it happy
55 */
56 if (strchr(dev->name, ' ')) {
57 debug("\nError: MDIO device name \"%s\" has a space!\n",
58 dev->name);
59 return -EINVAL;
60 }
61
62 return 0;
63}
64
65/*
66 * Following read/write/reset functions are registered with legacy MII code.
67 * These are called for PHY operations by upper layers and we further call the
68 * DM MDIO driver functions.
69 */
70static int mdio_read(struct mii_dev *mii_bus, int addr, int devad, int reg)
71{
72 struct udevice *dev = mii_bus->priv;
73
74 return mdio_get_ops(dev)->read(dev, addr, devad, reg);
75}
76
77static int mdio_write(struct mii_dev *mii_bus, int addr, int devad, int reg,
78 u16 val)
79{
80 struct udevice *dev = mii_bus->priv;
81
82 return mdio_get_ops(dev)->write(dev, addr, devad, reg, val);
83}
84
85static int mdio_reset(struct mii_dev *mii_bus)
86{
87 struct udevice *dev = mii_bus->priv;
88
89 if (mdio_get_ops(dev)->reset)
90 return mdio_get_ops(dev)->reset(dev);
91 else
92 return 0;
93}
94
95static int dm_mdio_post_probe(struct udevice *dev)
96{
97 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
98
99 pdata->mii_bus = mdio_alloc();
100 pdata->mii_bus->read = mdio_read;
101 pdata->mii_bus->write = mdio_write;
102 pdata->mii_bus->reset = mdio_reset;
103 pdata->mii_bus->priv = dev;
Joe Hershberger398e7512019-07-30 14:49:56 -0500104 strncpy(pdata->mii_bus->name, dev->name, MDIO_NAME_LEN - 1);
Alex Margineanc3452b52019-06-03 19:10:30 +0300105
106 return mdio_register(pdata->mii_bus);
107}
108
109static int dm_mdio_pre_remove(struct udevice *dev)
110{
111 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev);
112 struct mdio_ops *ops = mdio_get_ops(dev);
113
114 if (ops->reset)
115 ops->reset(dev);
116 mdio_unregister(pdata->mii_bus);
117 mdio_free(pdata->mii_bus);
118
119 return 0;
120}
121
Alex Margineana5d32c32019-11-25 17:15:11 +0200122struct phy_device *dm_mdio_phy_connect(struct udevice *mdiodev, int phyaddr,
Alex Margineanc3452b52019-06-03 19:10:30 +0300123 struct udevice *ethdev,
124 phy_interface_t interface)
125{
Alex Margineana5d32c32019-11-25 17:15:11 +0200126 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(mdiodev);
Alex Margineanc3452b52019-06-03 19:10:30 +0300127
Alex Margineana5d32c32019-11-25 17:15:11 +0200128 if (device_probe(mdiodev))
129 return NULL;
Alex Margineanc3452b52019-06-03 19:10:30 +0300130
Alex Margineana5d32c32019-11-25 17:15:11 +0200131 return phy_connect(pdata->mii_bus, phyaddr, ethdev, interface);
Alex Margineanc3452b52019-06-03 19:10:30 +0300132}
133
Alex Marginean2f624552019-11-25 17:15:12 +0200134static struct phy_device *dm_eth_connect_phy_handle(struct udevice *ethdev,
135 phy_interface_t interface)
136{
137 u32 phy_addr;
138 struct udevice *mdiodev;
139 struct phy_device *phy;
140 struct ofnode_phandle_args phandle = {.node = ofnode_null()};
Vladimir Olteanf27bc8a2021-03-14 20:14:48 +0800141 ofnode phynode;
Alex Marginean2f624552019-11-25 17:15:12 +0200142 int i;
143
Rasmus Villemoesbdf31922020-10-05 15:15:16 +0200144 if (CONFIG_IS_ENABLED(PHY_FIXED) &&
Vladimir Olteanf27bc8a2021-03-14 20:14:48 +0800145 ofnode_phy_is_fixed_link(dev_ofnode(ethdev), &phynode)) {
146 phy = phy_connect(NULL, 0, ethdev, interface);
147 phandle.node = phynode;
Rasmus Villemoesbdf31922020-10-05 15:15:16 +0200148 goto out;
149 }
150
Alex Marginean2f624552019-11-25 17:15:12 +0200151 for (i = 0; i < PHY_HANDLE_STR_CNT; i++)
152 if (!dev_read_phandle_with_args(ethdev, phy_handle_str[i], NULL,
153 0, 0, &phandle))
154 break;
155
156 if (!ofnode_valid(phandle.node)) {
Sean Anderson0851bd12020-09-15 10:44:53 -0400157 dev_dbg(ethdev, "can't find PHY node\n");
Alex Marginean2f624552019-11-25 17:15:12 +0200158 return NULL;
159 }
160
161 /*
162 * reading 'reg' directly should be fine. This is a PHY node, the
163 * address is always size 1 and requires no translation
164 */
165 if (ofnode_read_u32(phandle.node, "reg", &phy_addr)) {
166 dev_dbg(ethdev, "missing reg property in phy node\n");
167 return NULL;
168 }
169
170 if (uclass_get_device_by_ofnode(UCLASS_MDIO,
171 ofnode_get_parent(phandle.node),
172 &mdiodev)) {
Sean Anderson0851bd12020-09-15 10:44:53 -0400173 dev_dbg(ethdev, "can't find MDIO bus for node %s\n",
Alex Marginean2f624552019-11-25 17:15:12 +0200174 ofnode_get_name(ofnode_get_parent(phandle.node)));
175 return NULL;
176 }
177
178 phy = dm_mdio_phy_connect(mdiodev, phy_addr, ethdev, interface);
179
Rasmus Villemoesbdf31922020-10-05 15:15:16 +0200180out:
Alex Marginean2f624552019-11-25 17:15:12 +0200181 if (phy)
182 phy->node = phandle.node;
183
184 return phy;
185}
186
187/* Connect to a PHY linked in eth DT node */
188struct phy_device *dm_eth_phy_connect(struct udevice *ethdev)
189{
190 const char *if_str;
191 phy_interface_t interface;
192 struct phy_device *phy;
193 int i;
194
Simon Glassf10643c2020-12-19 10:40:14 -0700195 if (!dev_has_ofnode(ethdev)) {
Alex Marginean2f624552019-11-25 17:15:12 +0200196 debug("%s: supplied eth dev has no DT node!\n", ethdev->name);
197 return NULL;
198 }
199
200 interface = PHY_INTERFACE_MODE_NONE;
201 for (i = 0; i < PHY_MODE_STR_CNT; i++) {
Simon Glassf10643c2020-12-19 10:40:14 -0700202 if_str = dev_read_string(ethdev, phy_mode_str[i]);
Alex Marginean2f624552019-11-25 17:15:12 +0200203 if (if_str) {
204 interface = phy_get_interface_by_name(if_str);
205 break;
206 }
207 }
208 if (interface < 0)
209 interface = PHY_INTERFACE_MODE_NONE;
210 if (interface == PHY_INTERFACE_MODE_NONE)
211 dev_dbg(ethdev, "can't find interface mode, default to NONE\n");
212
213 phy = dm_eth_connect_phy_handle(ethdev, interface);
214
215 if (!phy)
216 return NULL;
217
218 phy->interface = interface;
219
220 return phy;
221}
222
Alex Margineanc3452b52019-06-03 19:10:30 +0300223UCLASS_DRIVER(mdio) = {
224 .id = UCLASS_MDIO,
225 .name = "mdio",
226 .post_bind = dm_mdio_post_bind,
227 .post_probe = dm_mdio_post_probe,
228 .pre_remove = dm_mdio_pre_remove,
Simon Glass41575d82020-12-03 16:55:17 -0700229 .per_device_auto = sizeof(struct mdio_perdev_priv),
Alex Margineanc3452b52019-06-03 19:10:30 +0300230};