blob: b5e8e465123743a32ea3e32255cdd0c9c4fb7538 [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>
Alex Marginean1a5b0982019-06-03 19:10:30 +030014#include <dm/uclass-internal.h>
Simon Glass9bc15642020-02-03 07:36:16 -070015#include <linux/compat.h>
Alex Marginean1a5b0982019-06-03 19:10:30 +030016
Alex Margineancf640552019-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 Marginean1a5b0982019-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 Marginean2e859ad2019-07-25 12:33:17 +030040 const char *dt_name;
41
42 /* set a custom name for the MDIO device, if present in DT */
43 if (ofnode_valid(dev->node)) {
44 dt_name = ofnode_read_string(dev->node, "device-name");
45 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 Marginean1a5b0982019-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 Hershbergerefb94292019-07-30 14:49:56 -0500103 strncpy(pdata->mii_bus->name, dev->name, MDIO_NAME_LEN - 1);
Alex Marginean1a5b0982019-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 Marginean6b3089e2019-11-25 17:15:11 +0200121struct phy_device *dm_mdio_phy_connect(struct udevice *mdiodev, int phyaddr,
Alex Marginean1a5b0982019-06-03 19:10:30 +0300122 struct udevice *ethdev,
123 phy_interface_t interface)
124{
Alex Marginean6b3089e2019-11-25 17:15:11 +0200125 struct mdio_perdev_priv *pdata = dev_get_uclass_priv(mdiodev);
Alex Marginean1a5b0982019-06-03 19:10:30 +0300126
Alex Marginean6b3089e2019-11-25 17:15:11 +0200127 if (device_probe(mdiodev))
128 return NULL;
Alex Marginean1a5b0982019-06-03 19:10:30 +0300129
Alex Marginean6b3089e2019-11-25 17:15:11 +0200130 return phy_connect(pdata->mii_bus, phyaddr, ethdev, interface);
Alex Marginean1a5b0982019-06-03 19:10:30 +0300131}
132
Alex Margineancf640552019-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
142 for (i = 0; i < PHY_HANDLE_STR_CNT; i++)
143 if (!dev_read_phandle_with_args(ethdev, phy_handle_str[i], NULL,
144 0, 0, &phandle))
145 break;
146
147 if (!ofnode_valid(phandle.node)) {
Sean Anderson836cb5e2020-09-15 10:44:53 -0400148 dev_dbg(ethdev, "can't find PHY node\n");
Alex Margineancf640552019-11-25 17:15:12 +0200149 return NULL;
150 }
151
152 /*
153 * reading 'reg' directly should be fine. This is a PHY node, the
154 * address is always size 1 and requires no translation
155 */
156 if (ofnode_read_u32(phandle.node, "reg", &phy_addr)) {
157 dev_dbg(ethdev, "missing reg property in phy node\n");
158 return NULL;
159 }
160
161 if (uclass_get_device_by_ofnode(UCLASS_MDIO,
162 ofnode_get_parent(phandle.node),
163 &mdiodev)) {
Sean Anderson836cb5e2020-09-15 10:44:53 -0400164 dev_dbg(ethdev, "can't find MDIO bus for node %s\n",
Alex Margineancf640552019-11-25 17:15:12 +0200165 ofnode_get_name(ofnode_get_parent(phandle.node)));
166 return NULL;
167 }
168
169 phy = dm_mdio_phy_connect(mdiodev, phy_addr, ethdev, interface);
170
171 if (phy)
172 phy->node = phandle.node;
173
174 return phy;
175}
176
177/* Connect to a PHY linked in eth DT node */
178struct phy_device *dm_eth_phy_connect(struct udevice *ethdev)
179{
180 const char *if_str;
181 phy_interface_t interface;
182 struct phy_device *phy;
183 int i;
184
185 if (!ofnode_valid(ethdev->node)) {
186 debug("%s: supplied eth dev has no DT node!\n", ethdev->name);
187 return NULL;
188 }
189
190 interface = PHY_INTERFACE_MODE_NONE;
191 for (i = 0; i < PHY_MODE_STR_CNT; i++) {
192 if_str = ofnode_read_string(ethdev->node, phy_mode_str[i]);
193 if (if_str) {
194 interface = phy_get_interface_by_name(if_str);
195 break;
196 }
197 }
198 if (interface < 0)
199 interface = PHY_INTERFACE_MODE_NONE;
200 if (interface == PHY_INTERFACE_MODE_NONE)
201 dev_dbg(ethdev, "can't find interface mode, default to NONE\n");
202
203 phy = dm_eth_connect_phy_handle(ethdev, interface);
204
205 if (!phy)
206 return NULL;
207
208 phy->interface = interface;
209
210 return phy;
211}
212
Alex Marginean1a5b0982019-06-03 19:10:30 +0300213UCLASS_DRIVER(mdio) = {
214 .id = UCLASS_MDIO,
215 .name = "mdio",
216 .post_bind = dm_mdio_post_bind,
217 .post_probe = dm_mdio_post_probe,
218 .pre_remove = dm_mdio_pre_remove,
219 .per_device_auto_alloc_size = sizeof(struct mdio_perdev_priv),
220};