blob: 5e219f699d8dfbf9d3cb068b583c4df56ac2cf87 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Andy Fleminga1ca3682011-03-21 18:03:22 -05002/*
3 * (C) Copyright 2011 Freescale Semiconductor, Inc
4 * Andy Fleming
Andy Fleminga1ca3682011-03-21 18:03:22 -05005 */
6
7/*
8 * MDIO Commands
9 */
10
11#include <common.h>
12#include <command.h>
13#include <miiphy.h>
14#include <phy.h>
15
Andy Fleminga1ca3682011-03-21 18:03:22 -050016static char last_op[2];
17static uint last_data;
18static uint last_addr_lo;
19static uint last_addr_hi;
20static uint last_devad_lo;
21static uint last_devad_hi;
22static uint last_reg_lo;
23static uint last_reg_hi;
24
25static int extract_range(char *input, int *plo, int *phi)
26{
27 char *end;
Michal Simekfca1e842016-11-16 08:41:01 +010028 *plo = simple_strtol(input, &end, 16);
Andy Fleminga1ca3682011-03-21 18:03:22 -050029 if (end == input)
30 return -1;
31
32 if ((*end == '-') && *(++end))
Michal Simekfca1e842016-11-16 08:41:01 +010033 *phi = simple_strtol(end, NULL, 16);
Andy Fleminga1ca3682011-03-21 18:03:22 -050034 else if (*end == '\0')
35 *phi = *plo;
36 else
37 return -1;
38
39 return 0;
40}
41
Carlo Caione1d70a3f2019-02-08 17:25:08 +000042static int mdio_write_ranges(struct mii_dev *bus,
Stefano Babic6894ea72013-09-02 15:42:32 +020043 int addrlo,
Kim Phillipsdc00a682012-10-29 13:34:31 +000044 int addrhi, int devadlo, int devadhi,
Stefano Babic6894ea72013-09-02 15:42:32 +020045 int reglo, int reghi, unsigned short data,
46 int extended)
Andy Fleminga1ca3682011-03-21 18:03:22 -050047{
Carlo Caione1d70a3f2019-02-08 17:25:08 +000048 struct phy_device *phydev;
Andy Fleminga1ca3682011-03-21 18:03:22 -050049 int addr, devad, reg;
50 int err = 0;
51
52 for (addr = addrlo; addr <= addrhi; addr++) {
Carlo Caione1d70a3f2019-02-08 17:25:08 +000053 phydev = bus->phymap[addr];
54
Andy Fleminga1ca3682011-03-21 18:03:22 -050055 for (devad = devadlo; devad <= devadhi; devad++) {
56 for (reg = reglo; reg <= reghi; reg++) {
Vladimir Oltean6572c102019-05-30 03:08:32 +030057 if (!phydev)
58 err = bus->write(bus, addr, devad,
59 reg, data);
60 else if (!extended)
Carlo Caione1d70a3f2019-02-08 17:25:08 +000061 err = phy_write_mmd(phydev, devad,
62 reg, data);
Stefano Babic6894ea72013-09-02 15:42:32 +020063 else
64 err = phydev->drv->writeext(phydev,
65 addr, devad, reg, data);
Andy Fleminga1ca3682011-03-21 18:03:22 -050066
67 if (err)
68 goto err_out;
69 }
70 }
71 }
72
73err_out:
74 return err;
75}
76
Carlo Caione1d70a3f2019-02-08 17:25:08 +000077static int mdio_read_ranges(struct mii_dev *bus,
Stefano Babic6894ea72013-09-02 15:42:32 +020078 int addrlo,
Kim Phillipsdc00a682012-10-29 13:34:31 +000079 int addrhi, int devadlo, int devadhi,
Stefano Babic6894ea72013-09-02 15:42:32 +020080 int reglo, int reghi, int extended)
Andy Fleminga1ca3682011-03-21 18:03:22 -050081{
82 int addr, devad, reg;
Carlo Caione1d70a3f2019-02-08 17:25:08 +000083 struct phy_device *phydev;
Andy Fleminga1ca3682011-03-21 18:03:22 -050084
85 printf("Reading from bus %s\n", bus->name);
86 for (addr = addrlo; addr <= addrhi; addr++) {
Carlo Caione1d70a3f2019-02-08 17:25:08 +000087 phydev = bus->phymap[addr];
Michal Simekfca1e842016-11-16 08:41:01 +010088 printf("PHY at address %x:\n", addr);
Andy Fleminga1ca3682011-03-21 18:03:22 -050089
90 for (devad = devadlo; devad <= devadhi; devad++) {
91 for (reg = reglo; reg <= reghi; reg++) {
Anatolij Gustschin9531b472011-04-30 02:39:29 +000092 int val;
Andy Fleminga1ca3682011-03-21 18:03:22 -050093
Vladimir Oltean6572c102019-05-30 03:08:32 +030094 if (!phydev)
95 val = bus->read(bus, addr, devad, reg);
96 else if (!extended)
Carlo Caione1d70a3f2019-02-08 17:25:08 +000097 val = phy_read_mmd(phydev, devad, reg);
Stefano Babic6894ea72013-09-02 15:42:32 +020098 else
99 val = phydev->drv->readext(phydev, addr,
100 devad, reg);
101
Andy Fleminga1ca3682011-03-21 18:03:22 -0500102 if (val < 0) {
103 printf("Error\n");
104
105 return val;
106 }
107
108 if (devad >= 0)
109 printf("%d.", devad);
110
111 printf("%d - 0x%x\n", reg, val & 0xffff);
112 }
113 }
114 }
115
116 return 0;
117}
118
119/* The register will be in the form [a[-b].]x[-y] */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000120static int extract_reg_range(char *input, int *devadlo, int *devadhi,
121 int *reglo, int *reghi)
Andy Fleminga1ca3682011-03-21 18:03:22 -0500122{
123 char *regstr;
124
125 /* use strrchr to find the last string after a '.' */
126 regstr = strrchr(input, '.');
127
128 /* If it exists, extract the devad(s) */
129 if (regstr) {
130 char devadstr[32];
131
132 strncpy(devadstr, input, regstr - input);
133 devadstr[regstr - input] = '\0';
134
135 if (extract_range(devadstr, devadlo, devadhi))
136 return -1;
137
138 regstr++;
139 } else {
140 /* Otherwise, we have no devad, and we just got regs */
141 *devadlo = *devadhi = MDIO_DEVAD_NONE;
142
143 regstr = input;
144 }
145
146 return extract_range(regstr, reglo, reghi);
147}
148
Kim Phillipsdc00a682012-10-29 13:34:31 +0000149static int extract_phy_range(char *const argv[], int argc, struct mii_dev **bus,
Stefano Babic6894ea72013-09-02 15:42:32 +0200150 struct phy_device **phydev,
Kim Phillipsdc00a682012-10-29 13:34:31 +0000151 int *addrlo, int *addrhi)
Andy Fleminga1ca3682011-03-21 18:03:22 -0500152{
Stefano Babic6894ea72013-09-02 15:42:32 +0200153 struct phy_device *dev = *phydev;
Andy Fleminga1ca3682011-03-21 18:03:22 -0500154
155 if ((argc < 1) || (argc > 2))
156 return -1;
157
158 /* If there are two arguments, it's busname addr */
159 if (argc == 2) {
160 *bus = miiphy_get_dev_by_name(argv[0]);
161
162 if (!*bus)
163 return -1;
164
165 return extract_range(argv[1], addrlo, addrhi);
166 }
167
168 /* It must be one argument, here */
169
170 /*
171 * This argument can be one of two things:
172 * 1) Ethernet device name
173 * 2) Just an address (use the previously-used bus)
174 *
175 * We check all buses for a PHY which is connected to an ethernet
176 * device by the given name. If none are found, we call
177 * extract_range() on the string, and see if it's an address range.
178 */
Stefano Babic6894ea72013-09-02 15:42:32 +0200179 dev = mdio_phydev_for_ethname(argv[0]);
Andy Fleminga1ca3682011-03-21 18:03:22 -0500180
Stefano Babic6894ea72013-09-02 15:42:32 +0200181 if (dev) {
182 *addrlo = *addrhi = dev->addr;
183 *bus = dev->bus;
Andy Fleminga1ca3682011-03-21 18:03:22 -0500184
185 return 0;
186 }
187
188 /* It's an address or nothing useful */
189 return extract_range(argv[0], addrlo, addrhi);
190}
191
192/* ---------------------------------------------------------------- */
193static int do_mdio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
194{
195 char op[2];
196 int addrlo, addrhi, reglo, reghi, devadlo, devadhi;
197 unsigned short data;
198 int pos = argc - 1;
199 struct mii_dev *bus;
Stefano Babic6894ea72013-09-02 15:42:32 +0200200 struct phy_device *phydev = NULL;
201 int extended = 0;
Andy Fleminga1ca3682011-03-21 18:03:22 -0500202
203 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000204 return CMD_RET_USAGE;
Andy Fleminga1ca3682011-03-21 18:03:22 -0500205
206 /*
207 * We use the last specified parameters, unless new ones are
208 * entered.
209 */
210 op[0] = argv[1][0];
211 addrlo = last_addr_lo;
212 addrhi = last_addr_hi;
213 devadlo = last_devad_lo;
214 devadhi = last_devad_hi;
215 reglo = last_reg_lo;
216 reghi = last_reg_hi;
217 data = last_data;
218
219 bus = mdio_get_current_dev();
220
221 if (flag & CMD_FLAG_REPEAT)
222 op[0] = last_op[0];
223
Stefano Babic6894ea72013-09-02 15:42:32 +0200224 if (strlen(argv[1]) > 1) {
225 op[1] = argv[1][1];
226 if (op[1] == 'x') {
227 phydev = mdio_phydev_for_ethname(argv[2]);
228
229 if (phydev) {
230 addrlo = phydev->addr;
231 addrhi = addrlo;
232 bus = phydev->bus;
233 extended = 1;
234 } else {
Carlo Caione1d70a3f2019-02-08 17:25:08 +0000235 return CMD_RET_FAILURE;
Stefano Babic6894ea72013-09-02 15:42:32 +0200236 }
237
238 if (!phydev->drv ||
239 (!phydev->drv->writeext && (op[0] == 'w')) ||
240 (!phydev->drv->readext && (op[0] == 'r'))) {
241 puts("PHY does not have extended functions\n");
Carlo Caione1d70a3f2019-02-08 17:25:08 +0000242 return CMD_RET_FAILURE;
Stefano Babic6894ea72013-09-02 15:42:32 +0200243 }
244 }
245 }
246
Andy Fleminga1ca3682011-03-21 18:03:22 -0500247 switch (op[0]) {
248 case 'w':
249 if (pos > 1)
250 data = simple_strtoul(argv[pos--], NULL, 16);
251 case 'r':
252 if (pos > 1)
253 if (extract_reg_range(argv[pos--], &devadlo, &devadhi,
Mario Sixd6808cf2018-01-15 11:08:26 +0100254 &reglo, &reghi))
Carlo Caione1d70a3f2019-02-08 17:25:08 +0000255 return CMD_RET_FAILURE;
Andy Fleminga1ca3682011-03-21 18:03:22 -0500256
257 default:
258 if (pos > 1)
Mario Sixd6808cf2018-01-15 11:08:26 +0100259 if (extract_phy_range(&argv[2], pos - 1, &bus,
260 &phydev, &addrlo, &addrhi))
Carlo Caione1d70a3f2019-02-08 17:25:08 +0000261 return CMD_RET_FAILURE;
Andy Fleminga1ca3682011-03-21 18:03:22 -0500262
263 break;
264 }
265
266 if (op[0] == 'l') {
267 mdio_list_devices();
268
269 return 0;
270 }
271
272 /* Save the chosen bus */
273 miiphy_set_current_dev(bus->name);
274
275 switch (op[0]) {
276 case 'w':
Carlo Caione1d70a3f2019-02-08 17:25:08 +0000277 mdio_write_ranges(bus, addrlo, addrhi, devadlo, devadhi,
Stefano Babic6894ea72013-09-02 15:42:32 +0200278 reglo, reghi, data, extended);
Andy Fleminga1ca3682011-03-21 18:03:22 -0500279 break;
280
281 case 'r':
Carlo Caione1d70a3f2019-02-08 17:25:08 +0000282 mdio_read_ranges(bus, addrlo, addrhi, devadlo, devadhi,
Stefano Babic6894ea72013-09-02 15:42:32 +0200283 reglo, reghi, extended);
Andy Fleminga1ca3682011-03-21 18:03:22 -0500284 break;
285 }
286
287 /*
288 * Save the parameters for repeats.
289 */
290 last_op[0] = op[0];
291 last_addr_lo = addrlo;
292 last_addr_hi = addrhi;
293 last_devad_lo = devadlo;
294 last_devad_hi = devadhi;
295 last_reg_lo = reglo;
296 last_reg_hi = reghi;
297 last_data = data;
298
299 return 0;
300}
301
302/***************************************************/
303
304U_BOOT_CMD(
305 mdio, 6, 1, do_mdio,
306 "MDIO utility commands",
307 "list - List MDIO buses\n"
308 "mdio read <phydev> [<devad>.]<reg> - "
309 "read PHY's register at <devad>.<reg>\n"
310 "mdio write <phydev> [<devad>.]<reg> <data> - "
311 "write PHY's register at <devad>.<reg>\n"
Stefano Babic6894ea72013-09-02 15:42:32 +0200312 "mdio rx <phydev> [<devad>.]<reg> - "
313 "read PHY's extended register at <devad>.<reg>\n"
314 "mdio wx <phydev> [<devad>.]<reg> <data> - "
315 "write PHY's extended register at <devad>.<reg>\n"
Andy Fleminga1ca3682011-03-21 18:03:22 -0500316 "<phydev> may be:\n"
317 " <busname> <addr>\n"
318 " <addr>\n"
319 " <eth name>\n"
320 "<addr> <devad>, and <reg> may be ranges, e.g. 1-5.4-0x1f.\n"
321);