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