]>
Commit | Line | Data |
---|---|---|
c3452b50 AM |
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> | |
f7ae49fc | 9 | #include <log.h> |
336d4615 | 10 | #include <malloc.h> |
c3452b50 AM |
11 | #include <miiphy.h> |
12 | #include <dm/device-internal.h> | |
336d4615 | 13 | #include <dm/device_compat.h> |
33aad0b0 | 14 | #include <dm/of_extra.h> |
c3452b50 | 15 | #include <dm/uclass-internal.h> |
336d4615 | 16 | #include <linux/compat.h> |
c3452b50 | 17 | |
2f624559 AM |
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 | ||
c3452b50 AM |
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 | { | |
6b3abc04 AM |
41 | const char *dt_name; |
42 | ||
43 | /* set a custom name for the MDIO device, if present in DT */ | |
f10643cf SG |
44 | if (dev_has_ofnode(dev)) { |
45 | dt_name = dev_read_string(dev, "device-name"); | |
6b3abc04 AM |
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 | ||
c3452b50 AM |
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; | |
398e7512 | 104 | strncpy(pdata->mii_bus->name, dev->name, MDIO_NAME_LEN - 1); |
c3452b50 AM |
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 | ||
a5d32c37 | 122 | struct phy_device *dm_mdio_phy_connect(struct udevice *mdiodev, int phyaddr, |
c3452b50 AM |
123 | struct udevice *ethdev, |
124 | phy_interface_t interface) | |
125 | { | |
a5d32c37 | 126 | struct mdio_perdev_priv *pdata = dev_get_uclass_priv(mdiodev); |
c3452b50 | 127 | |
a5d32c37 AM |
128 | if (device_probe(mdiodev)) |
129 | return NULL; | |
c3452b50 | 130 | |
a5d32c37 | 131 | return phy_connect(pdata->mii_bus, phyaddr, ethdev, interface); |
c3452b50 AM |
132 | } |
133 | ||
2f624559 AM |
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()}; | |
f27bc8af | 141 | ofnode phynode; |
2f624559 AM |
142 | int i; |
143 | ||
bdf31927 | 144 | if (CONFIG_IS_ENABLED(PHY_FIXED) && |
f27bc8af VO |
145 | ofnode_phy_is_fixed_link(dev_ofnode(ethdev), &phynode)) { |
146 | phy = phy_connect(NULL, 0, ethdev, interface); | |
147 | phandle.node = phynode; | |
bdf31927 RV |
148 | goto out; |
149 | } | |
150 | ||
2f624559 AM |
151 | 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)) { | |
0851bd1e | 157 | dev_dbg(ethdev, "can't find PHY node\n"); |
2f624559 AM |
158 | 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)) { | |
0851bd1e | 173 | dev_dbg(ethdev, "can't find MDIO bus for node %s\n", |
2f624559 AM |
174 | 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 | ||
bdf31927 | 180 | out: |
2f624559 AM |
181 | if (phy) |
182 | phy->node = phandle.node; | |
183 | ||
184 | return phy; | |
185 | } | |
186 | ||
187 | /* Connect to a PHY linked in eth DT node */ | |
188 | struct 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 | ||
f10643cf | 195 | if (!dev_has_ofnode(ethdev)) { |
2f624559 AM |
196 | 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++) { | |
f10643cf | 202 | if_str = dev_read_string(ethdev, phy_mode_str[i]); |
2f624559 AM |
203 | 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 | ||
c3452b50 AM |
223 | UCLASS_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, | |
41575d8e | 229 | .per_device_auto = sizeof(struct mdio_perdev_priv), |
c3452b50 | 230 | }; |