Skip to content

Commit 2bec758

Browse files
arvinfnashif
authored andcommitted
drivers: mdio: add shell
MDIO shell support. Signed-off-by: Arvin Farahmand <[email protected]>
1 parent c4cb45e commit 2bec758

File tree

3 files changed

+161
-1
lines changed

3 files changed

+161
-1
lines changed

drivers/mdio/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
zephyr_library()
44

5-
zephyr_library_sources_ifdef(CONFIG_MDIO_ATMEL_SAM mdio_sam.c)
5+
zephyr_library_sources_ifdef(CONFIG_MDIO_SHELL mdio_shell.c)
6+
zephyr_library_sources_ifdef(CONFIG_MDIO_ATMEL_SAM mdio_sam.c)

drivers/mdio/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ menuconfig MDIO
1313

1414
if MDIO
1515

16+
config MDIO_SHELL
17+
bool "Enable MDIO Shell"
18+
default y
19+
depends on SHELL
20+
help
21+
Enable MDIO Shell.
22+
23+
The MDIO shell currently supports scanning and device
24+
read/write.
25+
1626
# Include these first so that any properties (e.g. defaults) below can be
1727
# overridden (by defining symbols in multiple locations)
1828
source "drivers/mdio/Kconfig.sam"

drivers/mdio/mdio_shell.c

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/*
2+
* Copyright (c) 2021 IP-Logix Inc.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <shell/shell.h>
8+
#include <stdlib.h>
9+
#include <drivers/mdio.h>
10+
#include <string.h>
11+
#include <sys/util.h>
12+
13+
#include <logging/log.h>
14+
LOG_MODULE_REGISTER(mdio_shell, CONFIG_LOG_DEFAULT_LEVEL);
15+
16+
#define MDIO_DEVICE "MDIO"
17+
18+
/*
19+
* Scan the entire 5-bit address space of the MDIO bus
20+
*
21+
* scan [<dev_addr>]
22+
*/
23+
static int cmd_mdio_scan(const struct shell *sh, size_t argc, char **argv)
24+
{
25+
const struct device *dev;
26+
int cnt;
27+
uint16_t data;
28+
uint16_t dev_addr;
29+
30+
dev = device_get_binding(MDIO_DEVICE);
31+
if (!dev) {
32+
shell_error(sh, "MDIO: Device driver %s not found.",
33+
MDIO_DEVICE);
34+
35+
return -ENODEV;
36+
}
37+
38+
if (argc >= 2) {
39+
dev_addr = strtol(argv[1], NULL, 16);
40+
} else {
41+
dev_addr = 0;
42+
}
43+
44+
shell_print(sh,
45+
"Scanning bus for devices. Reading register 0x%x",
46+
dev_addr);
47+
cnt = 0;
48+
49+
mdio_bus_enable(dev);
50+
51+
for (int i = 0; i < 32; i++) {
52+
data = 0;
53+
if (mdio_read(dev, i, dev_addr, &data) >= 0 &&
54+
data != UINT16_MAX) {
55+
cnt++;
56+
shell_print(sh, "Found MDIO device @ 0x%x", i);
57+
}
58+
}
59+
60+
mdio_bus_disable(dev);
61+
62+
shell_print(sh, "%u devices found on %s", cnt, MDIO_DEVICE);
63+
64+
return 0;
65+
}
66+
67+
/* mdio write <port_addr> <dev_addr> <data> */
68+
static int cmd_mdio_write(const struct shell *sh, size_t argc, char **argv)
69+
{
70+
const struct device *dev;
71+
uint16_t data;
72+
uint16_t dev_addr;
73+
uint16_t port_addr;
74+
75+
dev = device_get_binding(MDIO_DEVICE);
76+
if (!dev) {
77+
shell_error(sh, "MDIO: Device driver %s not found.",
78+
MDIO_DEVICE);
79+
80+
return -ENODEV;
81+
}
82+
83+
port_addr = strtol(argv[1], NULL, 16);
84+
dev_addr = strtol(argv[2], NULL, 16);
85+
data = strtol(argv[3], NULL, 16);
86+
87+
mdio_bus_enable(dev);
88+
89+
if (mdio_write(dev, port_addr, dev_addr, data) < 0) {
90+
shell_error(sh, "Failed to write to device: %s", MDIO_DEVICE);
91+
mdio_bus_disable(dev);
92+
93+
return -EIO;
94+
}
95+
96+
mdio_bus_disable(dev);
97+
98+
return 0;
99+
}
100+
101+
/* mdio read <port_addr> <dev_addr> */
102+
static int cmd_mdio_read(const struct shell *sh, size_t argc, char **argv)
103+
{
104+
const struct device *dev;
105+
uint16_t data;
106+
uint16_t dev_addr;
107+
uint16_t port_addr;
108+
109+
dev = device_get_binding(MDIO_DEVICE);
110+
if (!dev) {
111+
shell_error(sh, "MDIO: Device driver %s not found.",
112+
MDIO_DEVICE);
113+
114+
return -ENODEV;
115+
}
116+
117+
port_addr = strtol(argv[1], NULL, 16);
118+
dev_addr = strtol(argv[2], NULL, 16);
119+
120+
mdio_bus_enable(dev);
121+
122+
if (mdio_read(dev, port_addr, dev_addr, &data) < 0) {
123+
shell_error(sh, "Failed to read from device: %s", MDIO_DEVICE);
124+
mdio_bus_disable(dev);
125+
126+
return -EIO;
127+
}
128+
129+
mdio_bus_disable(dev);
130+
131+
shell_print(sh, "%x[%x]: 0x%x", port_addr, dev_addr, data);
132+
133+
return 0;
134+
}
135+
136+
SHELL_STATIC_SUBCMD_SET_CREATE(sub_mdio_cmds,
137+
SHELL_CMD_ARG(scan, NULL,
138+
"Scan MDIO bus for devices: scan [<dev_addr>]",
139+
cmd_mdio_scan, 0, 1),
140+
SHELL_CMD_ARG(read, NULL,
141+
"Read from MDIO device: read <phy_addr> <dev_addr>",
142+
cmd_mdio_read, 3, 0),
143+
SHELL_CMD_ARG(write, NULL,
144+
"Write to MDIO device: write <phy_addr> <dev_addr> <value>",
145+
cmd_mdio_write, 4, 0),
146+
SHELL_SUBCMD_SET_END /* Array terminated. */
147+
);
148+
149+
SHELL_CMD_REGISTER(mdio, &sub_mdio_cmds, "MDIO commands", NULL);

0 commit comments

Comments
 (0)