1 /*
2 * Copyright (c) 2021 IP-Logix Inc.
3 * Copyright 2023 NXP
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include <zephyr/shell/shell.h>
9 #include <stdlib.h>
10 #include <zephyr/drivers/mdio.h>
11 #include <string.h>
12 #include <zephyr/sys/util.h>
13 #include <zephyr/devicetree.h>
14
15 #include <zephyr/logging/log.h>
16 LOG_MODULE_REGISTER(mdio_shell, CONFIG_LOG_DEFAULT_LEVEL);
17
18 #if DT_HAS_COMPAT_STATUS_OKAY(atmel_sam_mdio)
19 #define DT_DRV_COMPAT atmel_sam_mdio
20 #elif DT_HAS_COMPAT_STATUS_OKAY(espressif_esp32_mdio)
21 #define DT_DRV_COMPAT espressif_esp32_mdio
22 #elif DT_HAS_COMPAT_STATUS_OKAY(nxp_s32_netc_emdio)
23 #define DT_DRV_COMPAT nxp_s32_netc_emdio
24 #elif DT_HAS_COMPAT_STATUS_OKAY(adi_adin2111_mdio)
25 #define DT_DRV_COMPAT adi_adin2111_mdio
26 #elif DT_HAS_COMPAT_STATUS_OKAY(smsc_lan91c111_mdio)
27 #define DT_DRV_COMPAT smsc_lan91c111_mdio
28 #elif DT_HAS_COMPAT_STATUS_OKAY(zephyr_mdio_gpio)
29 #define DT_DRV_COMPAT zephyr_mdio_gpio
30 #else
31 #error "No known devicetree compatible match for MDIO shell"
32 #endif
33
34 #define MDIO_NODE_ID DT_COMPAT_GET_ANY_STATUS_OKAY(DT_DRV_COMPAT)
35
36 /*
37 * Scan the entire 5-bit address space of the MDIO bus
38 *
39 * scan [<reg_addr>]
40 */
cmd_mdio_scan(const struct shell * sh,size_t argc,char ** argv)41 static int cmd_mdio_scan(const struct shell *sh, size_t argc, char **argv)
42 {
43 const struct device *dev;
44 int cnt;
45 uint16_t data;
46 uint16_t reg_addr;
47
48 dev = DEVICE_DT_GET(MDIO_NODE_ID);
49 if (!device_is_ready(dev)) {
50 shell_error(sh, "MDIO: Device driver %s is not ready.", dev->name);
51
52 return -ENODEV;
53 }
54
55 if (argc >= 2) {
56 reg_addr = strtol(argv[1], NULL, 16);
57 } else {
58 reg_addr = 0;
59 }
60
61 shell_print(sh,
62 "Scanning bus for devices. Reading register 0x%x",
63 reg_addr);
64 cnt = 0;
65
66 mdio_bus_enable(dev);
67
68 for (int i = 0; i < 32; i++) {
69 data = 0;
70 if (mdio_read(dev, i, reg_addr, &data) >= 0 &&
71 data != UINT16_MAX) {
72 cnt++;
73 shell_print(sh, "Found MDIO device @ 0x%x", i);
74 }
75 }
76
77 mdio_bus_disable(dev);
78
79 shell_print(sh, "%u devices found on %s", cnt, dev->name);
80
81 return 0;
82 }
83
84 /* mdio write <port_addr> <reg_addr> <data> */
cmd_mdio_write(const struct shell * sh,size_t argc,char ** argv)85 static int cmd_mdio_write(const struct shell *sh, size_t argc, char **argv)
86 {
87 const struct device *dev;
88 uint16_t data;
89 uint16_t reg_addr;
90 uint16_t port_addr;
91
92 dev = DEVICE_DT_GET(MDIO_NODE_ID);
93 if (!device_is_ready(dev)) {
94 shell_error(sh, "MDIO: Device driver %s is not ready.", dev->name);
95
96 return -ENODEV;
97 }
98
99 port_addr = strtol(argv[1], NULL, 16);
100 reg_addr = strtol(argv[2], NULL, 16);
101 data = strtol(argv[3], NULL, 16);
102
103 mdio_bus_enable(dev);
104
105 if (mdio_write(dev, port_addr, reg_addr, data) < 0) {
106 shell_error(sh, "Failed to write to device: %s", dev->name);
107 mdio_bus_disable(dev);
108
109 return -EIO;
110 }
111
112 mdio_bus_disable(dev);
113
114 return 0;
115 }
116
117 /* mdio read <port_addr> <reg_addr> */
cmd_mdio_read(const struct shell * sh,size_t argc,char ** argv)118 static int cmd_mdio_read(const struct shell *sh, size_t argc, char **argv)
119 {
120 const struct device *dev;
121 uint16_t data;
122 uint16_t reg_addr;
123 uint16_t port_addr;
124
125 dev = DEVICE_DT_GET(MDIO_NODE_ID);
126 if (!device_is_ready(dev)) {
127 shell_error(sh, "MDIO: Device driver %s is not ready.", dev->name);
128
129 return -ENODEV;
130 }
131
132 port_addr = strtol(argv[1], NULL, 16);
133 reg_addr = strtol(argv[2], NULL, 16);
134
135 mdio_bus_enable(dev);
136
137 if (mdio_read(dev, port_addr, reg_addr, &data) < 0) {
138 shell_error(sh, "Failed to read from device: %s", dev->name);
139 mdio_bus_disable(dev);
140
141 return -EIO;
142 }
143
144 mdio_bus_disable(dev);
145
146 shell_print(sh, "%x[%x]: 0x%x", port_addr, reg_addr, data);
147
148 return 0;
149 }
150
151 /* mdio write_c45 <port_addr> <dev_addr> <reg_addr> <value> */
cmd_mdio_write_45(const struct shell * sh,size_t argc,char ** argv)152 static int cmd_mdio_write_45(const struct shell *sh, size_t argc, char **argv)
153 {
154 const struct device *dev;
155 uint16_t data;
156 uint16_t reg_addr;
157 uint8_t dev_addr;
158 uint8_t port_addr;
159
160 dev = DEVICE_DT_GET(MDIO_NODE_ID);
161 if (!device_is_ready(dev)) {
162 shell_error(sh, "MDIO: Device driver %s is not ready.", dev->name);
163
164 return -ENODEV;
165 }
166
167 port_addr = strtol(argv[1], NULL, 16);
168 dev_addr = strtol(argv[2], NULL, 16);
169 reg_addr = strtol(argv[3], NULL, 16);
170 data = strtol(argv[4], NULL, 16);
171
172 mdio_bus_enable(dev);
173
174 if (mdio_write_c45(dev, port_addr, dev_addr, reg_addr, data) < 0) {
175 shell_error(sh, "Failed to write to device: %s", dev->name);
176 mdio_bus_disable(dev);
177
178 return -EIO;
179 }
180
181 mdio_bus_disable(dev);
182
183 return 0;
184 }
185
186 /* mdio read_c45 <port_addr> <dev_addr> <reg_addr> */
cmd_mdio_read_c45(const struct shell * sh,size_t argc,char ** argv)187 static int cmd_mdio_read_c45(const struct shell *sh, size_t argc, char **argv)
188 {
189 const struct device *dev;
190 uint16_t data;
191 uint16_t reg_addr;
192 uint8_t dev_addr;
193 uint8_t port_addr;
194
195 dev = DEVICE_DT_GET(MDIO_NODE_ID);
196 if (!device_is_ready(dev)) {
197 shell_error(sh, "MDIO: Device driver %s is not ready.", dev->name);
198
199 return -ENODEV;
200 }
201
202 port_addr = strtol(argv[1], NULL, 16);
203 dev_addr = strtol(argv[2], NULL, 16);
204 reg_addr = strtol(argv[3], NULL, 16);
205
206 mdio_bus_enable(dev);
207
208 if (mdio_read_c45(dev, port_addr, dev_addr, reg_addr, &data) < 0) {
209 shell_error(sh, "Failed to read from device: %s", dev->name);
210 mdio_bus_disable(dev);
211
212 return -EIO;
213 }
214
215 mdio_bus_disable(dev);
216
217 shell_print(sh, "%x[%x:%x]: 0x%x", port_addr, dev_addr, reg_addr, data);
218
219 return 0;
220 }
221
222 SHELL_STATIC_SUBCMD_SET_CREATE(sub_mdio_cmds,
223 SHELL_CMD_ARG(scan, NULL,
224 "Scan MDIO bus for devices: scan [<reg_addr>]",
225 cmd_mdio_scan, 0, 1),
226 SHELL_CMD_ARG(read, NULL,
227 "Read from MDIO device: read <phy_addr> <reg_addr>",
228 cmd_mdio_read, 3, 0),
229 SHELL_CMD_ARG(write, NULL,
230 "Write to MDIO device: write <phy_addr> <reg_addr> <value>",
231 cmd_mdio_write, 4, 0),
232 SHELL_CMD_ARG(read_c45, NULL,
233 "Read from MDIO Clause 45 device: "
234 "read_c45 <port_addr> <dev_addr> <reg_addr>",
235 cmd_mdio_read_c45, 4, 0),
236 SHELL_CMD_ARG(write_c45, NULL,
237 "Write to MDIO Clause 45 device: "
238 "write_c45 <port_addr> <dev_addr> <reg_addr> <value>",
239 cmd_mdio_write_45, 5, 0),
240 SHELL_SUBCMD_SET_END /* Array terminated. */
241 );
242
243 SHELL_CMD_REGISTER(mdio, &sub_mdio_cmds, "MDIO commands", NULL);
244