dm: mdio: Use ofnode_phy_is_fixed_link() API
[platform/kernel/u-boot.git] / net / mdio-uclass.c
1 // 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>
9 #include <log.h>
10 #include <malloc.h>
11 #include <miiphy.h>
12 #include <dm/device-internal.h>
13 #include <dm/device_compat.h>
14 #include <dm/of_extra.h>
15 #include <dm/uclass-internal.h>
16 #include <linux/compat.h>
17
18 /* DT node properties for MAC-PHY interface */
19 #define PHY_MODE_STR_CNT        2
20 static 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
24 const char *phy_handle_str[PHY_HANDLE_STR_CNT] = { "phy-handle",
25                                                    "phy",
26                                                    "phy-device" };
27
28 void 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
39 static int dm_mdio_post_bind(struct udevice *dev)
40 {
41         const char *dt_name;
42
43         /* set a custom name for the MDIO device, if present in DT */
44         if (dev_has_ofnode(dev)) {
45                 dt_name = dev_read_string(dev, "device-name");
46                 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
52         /*
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  */
70 static 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
77 static 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
85 static 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
95 static 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;
104         strncpy(pdata->mii_bus->name, dev->name, MDIO_NAME_LEN - 1);
105
106         return mdio_register(pdata->mii_bus);
107 }
108
109 static 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
122 struct phy_device *dm_mdio_phy_connect(struct udevice *mdiodev, int phyaddr,
123                                        struct udevice *ethdev,
124                                        phy_interface_t interface)
125 {
126         struct mdio_perdev_priv *pdata = dev_get_uclass_priv(mdiodev);
127
128         if (device_probe(mdiodev))
129                 return NULL;
130
131         return phy_connect(pdata->mii_bus, phyaddr, ethdev, interface);
132 }
133
134 static 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()};
141         int i;
142
143         if (CONFIG_IS_ENABLED(PHY_FIXED) &&
144             ofnode_phy_is_fixed_link(dev_ofnode(ethdev), NULL)) {
145                 phy = phy_connect(NULL, -1, ethdev, interface);
146                 goto out;
147         }
148
149         for (i = 0; i < PHY_HANDLE_STR_CNT; i++)
150                 if (!dev_read_phandle_with_args(ethdev, phy_handle_str[i], NULL,
151                                                 0, 0, &phandle))
152                         break;
153
154         if (!ofnode_valid(phandle.node)) {
155                 dev_dbg(ethdev, "can't find PHY node\n");
156                 return NULL;
157         }
158
159         /*
160          * reading 'reg' directly should be fine.  This is a PHY node, the
161          * address is always size 1 and requires no translation
162          */
163         if (ofnode_read_u32(phandle.node, "reg", &phy_addr)) {
164                 dev_dbg(ethdev, "missing reg property in phy node\n");
165                 return NULL;
166         }
167
168         if (uclass_get_device_by_ofnode(UCLASS_MDIO,
169                                         ofnode_get_parent(phandle.node),
170                                         &mdiodev)) {
171                 dev_dbg(ethdev, "can't find MDIO bus for node %s\n",
172                         ofnode_get_name(ofnode_get_parent(phandle.node)));
173                 return NULL;
174         }
175
176         phy = dm_mdio_phy_connect(mdiodev, phy_addr, ethdev, interface);
177
178 out:
179         if (phy)
180                 phy->node = phandle.node;
181
182         return phy;
183 }
184
185 /* Connect to a PHY linked in eth DT node */
186 struct phy_device *dm_eth_phy_connect(struct udevice *ethdev)
187 {
188         const char *if_str;
189         phy_interface_t interface;
190         struct phy_device *phy;
191         int i;
192
193         if (!dev_has_ofnode(ethdev)) {
194                 debug("%s: supplied eth dev has no DT node!\n", ethdev->name);
195                 return NULL;
196         }
197
198         interface = PHY_INTERFACE_MODE_NONE;
199         for (i = 0; i < PHY_MODE_STR_CNT; i++) {
200                 if_str = dev_read_string(ethdev, phy_mode_str[i]);
201                 if (if_str) {
202                         interface = phy_get_interface_by_name(if_str);
203                         break;
204                 }
205         }
206         if (interface < 0)
207                 interface = PHY_INTERFACE_MODE_NONE;
208         if (interface == PHY_INTERFACE_MODE_NONE)
209                 dev_dbg(ethdev, "can't find interface mode, default to NONE\n");
210
211         phy = dm_eth_connect_phy_handle(ethdev, interface);
212
213         if (!phy)
214                 return NULL;
215
216         phy->interface = interface;
217
218         return phy;
219 }
220
221 UCLASS_DRIVER(mdio) = {
222         .id = UCLASS_MDIO,
223         .name = "mdio",
224         .post_bind  = dm_mdio_post_bind,
225         .post_probe = dm_mdio_post_probe,
226         .pre_remove = dm_mdio_pre_remove,
227         .per_device_auto        = sizeof(struct mdio_perdev_priv),
228 };