blob: fb13d050752a7f2abeb615101e76046257477fe5 [file] [log] [blame]
Andy Fleming995daa02011-03-21 18:03:22 -05001/*
2 * (C) Copyright 2011 Freescale Semiconductor, Inc
3 * Andy Fleming
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Andy Fleming995daa02011-03-21 18:03:22 -05006 */
7
8/*
9 * MDIO Commands
10 */
11
12#include <common.h>
13#include <command.h>
14#include <miiphy.h>
15#include <phy.h>
16
17
18static char last_op[2];
19static uint last_data;
20static uint last_addr_lo;
21static uint last_addr_hi;
22static uint last_devad_lo;
23static uint last_devad_hi;
24static uint last_reg_lo;
25static uint last_reg_hi;
26
27static int extract_range(char *input, int *plo, int *phi)
28{
29 char *end;
30 *plo = simple_strtol(input, &end, 0);
31 if (end == input)
32 return -1;
33
34 if ((*end == '-') && *(++end))
35 *phi = simple_strtol(end, NULL, 0);
36 else if (*end == '\0')
37 *phi = *plo;
38 else
39 return -1;
40
41 return 0;
42}
43
Stefano Babic26b807c2013-09-02 15:42:32 +020044static int mdio_write_ranges(struct phy_device *phydev, struct mii_dev *bus,
45 int addrlo,
Kim Phillips088f1b12012-10-29 13:34:31 +000046 int addrhi, int devadlo, int devadhi,
Stefano Babic26b807c2013-09-02 15:42:32 +020047 int reglo, int reghi, unsigned short data,
48 int extended)
Andy Fleming995daa02011-03-21 18:03:22 -050049{
50 int addr, devad, reg;
51 int err = 0;
52
53 for (addr = addrlo; addr <= addrhi; addr++) {
54 for (devad = devadlo; devad <= devadhi; devad++) {
55 for (reg = reglo; reg <= reghi; reg++) {
Stefano Babic26b807c2013-09-02 15:42:32 +020056 if (!extended)
57 err = bus->write(bus, addr, devad,
58 reg, data);
59 else
60 err = phydev->drv->writeext(phydev,
61 addr, devad, reg, data);
Andy Fleming995daa02011-03-21 18:03:22 -050062
63 if (err)
64 goto err_out;
65 }
66 }
67 }
68
69err_out:
70 return err;
71}
72
Stefano Babic26b807c2013-09-02 15:42:32 +020073static int mdio_read_ranges(struct phy_device *phydev, struct mii_dev *bus,
74 int addrlo,
Kim Phillips088f1b12012-10-29 13:34:31 +000075 int addrhi, int devadlo, int devadhi,
Stefano Babic26b807c2013-09-02 15:42:32 +020076 int reglo, int reghi, int extended)
Andy Fleming995daa02011-03-21 18:03:22 -050077{
78 int addr, devad, reg;
79
80 printf("Reading from bus %s\n", bus->name);
81 for (addr = addrlo; addr <= addrhi; addr++) {
82 printf("PHY at address %d:\n", addr);
83
84 for (devad = devadlo; devad <= devadhi; devad++) {
85 for (reg = reglo; reg <= reghi; reg++) {
Anatolij Gustschina621b162011-04-30 02:39:29 +000086 int val;
Andy Fleming995daa02011-03-21 18:03:22 -050087
Stefano Babic26b807c2013-09-02 15:42:32 +020088 if (!extended)
89 val = bus->read(bus, addr, devad, reg);
90 else
91 val = phydev->drv->readext(phydev, addr,
92 devad, reg);
93
Andy Fleming995daa02011-03-21 18:03:22 -050094 if (val < 0) {
95 printf("Error\n");
96
97 return val;
98 }
99
100 if (devad >= 0)
101 printf("%d.", devad);
102
103 printf("%d - 0x%x\n", reg, val & 0xffff);
104 }
105 }
106 }
107
108 return 0;
109}
110
111/* The register will be in the form [a[-b].]x[-y] */
Kim Phillips088f1b12012-10-29 13:34:31 +0000112static int extract_reg_range(char *input, int *devadlo, int *devadhi,
113 int *reglo, int *reghi)
Andy Fleming995daa02011-03-21 18:03:22 -0500114{
115 char *regstr;
116
117 /* use strrchr to find the last string after a '.' */
118 regstr = strrchr(input, '.');
119
120 /* If it exists, extract the devad(s) */
121 if (regstr) {
122 char devadstr[32];
123
124 strncpy(devadstr, input, regstr - input);
125 devadstr[regstr - input] = '\0';
126
127 if (extract_range(devadstr, devadlo, devadhi))
128 return -1;
129
130 regstr++;
131 } else {
132 /* Otherwise, we have no devad, and we just got regs */
133 *devadlo = *devadhi = MDIO_DEVAD_NONE;
134
135 regstr = input;
136 }
137
138 return extract_range(regstr, reglo, reghi);
139}
140
Kim Phillips088f1b12012-10-29 13:34:31 +0000141static int extract_phy_range(char *const argv[], int argc, struct mii_dev **bus,
Stefano Babic26b807c2013-09-02 15:42:32 +0200142 struct phy_device **phydev,
Kim Phillips088f1b12012-10-29 13:34:31 +0000143 int *addrlo, int *addrhi)
Andy Fleming995daa02011-03-21 18:03:22 -0500144{
Stefano Babic26b807c2013-09-02 15:42:32 +0200145 struct phy_device *dev = *phydev;
Andy Fleming995daa02011-03-21 18:03:22 -0500146
147 if ((argc < 1) || (argc > 2))
148 return -1;
149
150 /* If there are two arguments, it's busname addr */
151 if (argc == 2) {
152 *bus = miiphy_get_dev_by_name(argv[0]);
153
154 if (!*bus)
155 return -1;
156
157 return extract_range(argv[1], addrlo, addrhi);
158 }
159
160 /* It must be one argument, here */
161
162 /*
163 * This argument can be one of two things:
164 * 1) Ethernet device name
165 * 2) Just an address (use the previously-used bus)
166 *
167 * We check all buses for a PHY which is connected to an ethernet
168 * device by the given name. If none are found, we call
169 * extract_range() on the string, and see if it's an address range.
170 */
Stefano Babic26b807c2013-09-02 15:42:32 +0200171 dev = mdio_phydev_for_ethname(argv[0]);
Andy Fleming995daa02011-03-21 18:03:22 -0500172
Stefano Babic26b807c2013-09-02 15:42:32 +0200173 if (dev) {
174 *addrlo = *addrhi = dev->addr;
175 *bus = dev->bus;
Andy Fleming995daa02011-03-21 18:03:22 -0500176
177 return 0;
178 }
179
180 /* It's an address or nothing useful */
181 return extract_range(argv[0], addrlo, addrhi);
182}
183
184/* ---------------------------------------------------------------- */
185static int do_mdio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
186{
187 char op[2];
188 int addrlo, addrhi, reglo, reghi, devadlo, devadhi;
189 unsigned short data;
190 int pos = argc - 1;
191 struct mii_dev *bus;
Stefano Babic26b807c2013-09-02 15:42:32 +0200192 struct phy_device *phydev = NULL;
193 int extended = 0;
Andy Fleming995daa02011-03-21 18:03:22 -0500194
195 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000196 return CMD_RET_USAGE;
Andy Fleming995daa02011-03-21 18:03:22 -0500197
198 /*
199 * We use the last specified parameters, unless new ones are
200 * entered.
201 */
202 op[0] = argv[1][0];
203 addrlo = last_addr_lo;
204 addrhi = last_addr_hi;
205 devadlo = last_devad_lo;
206 devadhi = last_devad_hi;
207 reglo = last_reg_lo;
208 reghi = last_reg_hi;
209 data = last_data;
210
211 bus = mdio_get_current_dev();
212
213 if (flag & CMD_FLAG_REPEAT)
214 op[0] = last_op[0];
215
Stefano Babic26b807c2013-09-02 15:42:32 +0200216 if (strlen(argv[1]) > 1) {
217 op[1] = argv[1][1];
218 if (op[1] == 'x') {
219 phydev = mdio_phydev_for_ethname(argv[2]);
220
221 if (phydev) {
222 addrlo = phydev->addr;
223 addrhi = addrlo;
224 bus = phydev->bus;
225 extended = 1;
226 } else {
227 return -1;
228 }
229
230 if (!phydev->drv ||
231 (!phydev->drv->writeext && (op[0] == 'w')) ||
232 (!phydev->drv->readext && (op[0] == 'r'))) {
233 puts("PHY does not have extended functions\n");
234 return -1;
235 }
236 }
237 }
238
Andy Fleming995daa02011-03-21 18:03:22 -0500239 switch (op[0]) {
240 case 'w':
241 if (pos > 1)
242 data = simple_strtoul(argv[pos--], NULL, 16);
243 case 'r':
244 if (pos > 1)
245 if (extract_reg_range(argv[pos--], &devadlo, &devadhi,
246 &reglo, &reghi))
247 return -1;
248
249 default:
250 if (pos > 1)
251 if (extract_phy_range(&(argv[2]), pos - 1, &bus,
Stefano Babic26b807c2013-09-02 15:42:32 +0200252 &phydev, &addrlo, &addrhi))
Andy Fleming995daa02011-03-21 18:03:22 -0500253 return -1;
254
255 break;
256 }
257
258 if (op[0] == 'l') {
259 mdio_list_devices();
260
261 return 0;
262 }
263
264 /* Save the chosen bus */
265 miiphy_set_current_dev(bus->name);
266
267 switch (op[0]) {
268 case 'w':
Stefano Babic26b807c2013-09-02 15:42:32 +0200269 mdio_write_ranges(phydev, bus, addrlo, addrhi, devadlo, devadhi,
270 reglo, reghi, data, extended);
Andy Fleming995daa02011-03-21 18:03:22 -0500271 break;
272
273 case 'r':
Stefano Babic26b807c2013-09-02 15:42:32 +0200274 mdio_read_ranges(phydev, bus, addrlo, addrhi, devadlo, devadhi,
275 reglo, reghi, extended);
Andy Fleming995daa02011-03-21 18:03:22 -0500276 break;
277 }
278
279 /*
280 * Save the parameters for repeats.
281 */
282 last_op[0] = op[0];
283 last_addr_lo = addrlo;
284 last_addr_hi = addrhi;
285 last_devad_lo = devadlo;
286 last_devad_hi = devadhi;
287 last_reg_lo = reglo;
288 last_reg_hi = reghi;
289 last_data = data;
290
291 return 0;
292}
293
294/***************************************************/
295
296U_BOOT_CMD(
297 mdio, 6, 1, do_mdio,
298 "MDIO utility commands",
299 "list - List MDIO buses\n"
300 "mdio read <phydev> [<devad>.]<reg> - "
301 "read PHY's register at <devad>.<reg>\n"
302 "mdio write <phydev> [<devad>.]<reg> <data> - "
303 "write PHY's register at <devad>.<reg>\n"
Stefano Babic26b807c2013-09-02 15:42:32 +0200304 "mdio rx <phydev> [<devad>.]<reg> - "
305 "read PHY's extended register at <devad>.<reg>\n"
306 "mdio wx <phydev> [<devad>.]<reg> <data> - "
307 "write PHY's extended register at <devad>.<reg>\n"
Andy Fleming995daa02011-03-21 18:03:22 -0500308 "<phydev> may be:\n"
309 " <busname> <addr>\n"
310 " <addr>\n"
311 " <eth name>\n"
312 "<addr> <devad>, and <reg> may be ranges, e.g. 1-5.4-0x1f.\n"
313);