blob: efe8c9ef09540d91b628cefb5bec7316e55a8372 [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++) {
Stefano Babic6894ea72013-09-02 15:42:32 +020057 if (!extended)
Carlo Caione1d70a3f2019-02-08 17:25:08 +000058 err = phy_write_mmd(phydev, devad,
59 reg, data);
Stefano Babic6894ea72013-09-02 15:42:32 +020060 else
61 err = phydev->drv->writeext(phydev,
62 addr, devad, reg, data);
Andy Fleminga1ca3682011-03-21 18:03:22 -050063
64 if (err)
65 goto err_out;
66 }
67 }
68 }
69
70err_out:
71 return err;
72}
73
Carlo Caione1d70a3f2019-02-08 17:25:08 +000074static int mdio_read_ranges(struct mii_dev *bus,
Stefano Babic6894ea72013-09-02 15:42:32 +020075 int addrlo,
Kim Phillipsdc00a682012-10-29 13:34:31 +000076 int addrhi, int devadlo, int devadhi,
Stefano Babic6894ea72013-09-02 15:42:32 +020077 int reglo, int reghi, int extended)
Andy Fleminga1ca3682011-03-21 18:03:22 -050078{
79 int addr, devad, reg;
Carlo Caione1d70a3f2019-02-08 17:25:08 +000080 struct phy_device *phydev;
Andy Fleminga1ca3682011-03-21 18:03:22 -050081
82 printf("Reading from bus %s\n", bus->name);
83 for (addr = addrlo; addr <= addrhi; addr++) {
Carlo Caione1d70a3f2019-02-08 17:25:08 +000084 phydev = bus->phymap[addr];
Michal Simekfca1e842016-11-16 08:41:01 +010085 printf("PHY at address %x:\n", addr);
Andy Fleminga1ca3682011-03-21 18:03:22 -050086
87 for (devad = devadlo; devad <= devadhi; devad++) {
88 for (reg = reglo; reg <= reghi; reg++) {
Anatolij Gustschin9531b472011-04-30 02:39:29 +000089 int val;
Andy Fleminga1ca3682011-03-21 18:03:22 -050090
Stefano Babic6894ea72013-09-02 15:42:32 +020091 if (!extended)
Carlo Caione1d70a3f2019-02-08 17:25:08 +000092 val = phy_read_mmd(phydev, devad, reg);
Stefano Babic6894ea72013-09-02 15:42:32 +020093 else
94 val = phydev->drv->readext(phydev, addr,
95 devad, reg);
96
Andy Fleminga1ca3682011-03-21 18:03:22 -050097 if (val < 0) {
98 printf("Error\n");
99
100 return val;
101 }
102
103 if (devad >= 0)
104 printf("%d.", devad);
105
106 printf("%d - 0x%x\n", reg, val & 0xffff);
107 }
108 }
109 }
110
111 return 0;
112}
113
114/* The register will be in the form [a[-b].]x[-y] */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000115static int extract_reg_range(char *input, int *devadlo, int *devadhi,
116 int *reglo, int *reghi)
Andy Fleminga1ca3682011-03-21 18:03:22 -0500117{
118 char *regstr;
119
120 /* use strrchr to find the last string after a '.' */
121 regstr = strrchr(input, '.');
122
123 /* If it exists, extract the devad(s) */
124 if (regstr) {
125 char devadstr[32];
126
127 strncpy(devadstr, input, regstr - input);
128 devadstr[regstr - input] = '\0';
129
130 if (extract_range(devadstr, devadlo, devadhi))
131 return -1;
132
133 regstr++;
134 } else {
135 /* Otherwise, we have no devad, and we just got regs */
136 *devadlo = *devadhi = MDIO_DEVAD_NONE;
137
138 regstr = input;
139 }
140
141 return extract_range(regstr, reglo, reghi);
142}
143
Kim Phillipsdc00a682012-10-29 13:34:31 +0000144static int extract_phy_range(char *const argv[], int argc, struct mii_dev **bus,
Stefano Babic6894ea72013-09-02 15:42:32 +0200145 struct phy_device **phydev,
Kim Phillipsdc00a682012-10-29 13:34:31 +0000146 int *addrlo, int *addrhi)
Andy Fleminga1ca3682011-03-21 18:03:22 -0500147{
Stefano Babic6894ea72013-09-02 15:42:32 +0200148 struct phy_device *dev = *phydev;
Andy Fleminga1ca3682011-03-21 18:03:22 -0500149
150 if ((argc < 1) || (argc > 2))
151 return -1;
152
153 /* If there are two arguments, it's busname addr */
154 if (argc == 2) {
155 *bus = miiphy_get_dev_by_name(argv[0]);
156
157 if (!*bus)
158 return -1;
159
160 return extract_range(argv[1], addrlo, addrhi);
161 }
162
163 /* It must be one argument, here */
164
165 /*
166 * This argument can be one of two things:
167 * 1) Ethernet device name
168 * 2) Just an address (use the previously-used bus)
169 *
170 * We check all buses for a PHY which is connected to an ethernet
171 * device by the given name. If none are found, we call
172 * extract_range() on the string, and see if it's an address range.
173 */
Stefano Babic6894ea72013-09-02 15:42:32 +0200174 dev = mdio_phydev_for_ethname(argv[0]);
Andy Fleminga1ca3682011-03-21 18:03:22 -0500175
Stefano Babic6894ea72013-09-02 15:42:32 +0200176 if (dev) {
177 *addrlo = *addrhi = dev->addr;
178 *bus = dev->bus;
Andy Fleminga1ca3682011-03-21 18:03:22 -0500179
180 return 0;
181 }
182
183 /* It's an address or nothing useful */
184 return extract_range(argv[0], addrlo, addrhi);
185}
186
187/* ---------------------------------------------------------------- */
188static int do_mdio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
189{
190 char op[2];
191 int addrlo, addrhi, reglo, reghi, devadlo, devadhi;
192 unsigned short data;
193 int pos = argc - 1;
194 struct mii_dev *bus;
Stefano Babic6894ea72013-09-02 15:42:32 +0200195 struct phy_device *phydev = NULL;
196 int extended = 0;
Andy Fleminga1ca3682011-03-21 18:03:22 -0500197
198 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000199 return CMD_RET_USAGE;
Andy Fleminga1ca3682011-03-21 18:03:22 -0500200
201 /*
202 * We use the last specified parameters, unless new ones are
203 * entered.
204 */
205 op[0] = argv[1][0];
206 addrlo = last_addr_lo;
207 addrhi = last_addr_hi;
208 devadlo = last_devad_lo;
209 devadhi = last_devad_hi;
210 reglo = last_reg_lo;
211 reghi = last_reg_hi;
212 data = last_data;
213
214 bus = mdio_get_current_dev();
215
216 if (flag & CMD_FLAG_REPEAT)
217 op[0] = last_op[0];
218
Stefano Babic6894ea72013-09-02 15:42:32 +0200219 if (strlen(argv[1]) > 1) {
220 op[1] = argv[1][1];
221 if (op[1] == 'x') {
222 phydev = mdio_phydev_for_ethname(argv[2]);
223
224 if (phydev) {
225 addrlo = phydev->addr;
226 addrhi = addrlo;
227 bus = phydev->bus;
228 extended = 1;
229 } else {
Carlo Caione1d70a3f2019-02-08 17:25:08 +0000230 return CMD_RET_FAILURE;
Stefano Babic6894ea72013-09-02 15:42:32 +0200231 }
232
233 if (!phydev->drv ||
234 (!phydev->drv->writeext && (op[0] == 'w')) ||
235 (!phydev->drv->readext && (op[0] == 'r'))) {
236 puts("PHY does not have extended functions\n");
Carlo Caione1d70a3f2019-02-08 17:25:08 +0000237 return CMD_RET_FAILURE;
Stefano Babic6894ea72013-09-02 15:42:32 +0200238 }
239 }
240 }
241
Andy Fleminga1ca3682011-03-21 18:03:22 -0500242 switch (op[0]) {
243 case 'w':
244 if (pos > 1)
245 data = simple_strtoul(argv[pos--], NULL, 16);
246 case 'r':
247 if (pos > 1)
248 if (extract_reg_range(argv[pos--], &devadlo, &devadhi,
Mario Sixd6808cf2018-01-15 11:08:26 +0100249 &reglo, &reghi))
Carlo Caione1d70a3f2019-02-08 17:25:08 +0000250 return CMD_RET_FAILURE;
Andy Fleminga1ca3682011-03-21 18:03:22 -0500251
252 default:
253 if (pos > 1)
Mario Sixd6808cf2018-01-15 11:08:26 +0100254 if (extract_phy_range(&argv[2], pos - 1, &bus,
255 &phydev, &addrlo, &addrhi))
Carlo Caione1d70a3f2019-02-08 17:25:08 +0000256 return CMD_RET_FAILURE;
Andy Fleminga1ca3682011-03-21 18:03:22 -0500257
258 break;
259 }
260
261 if (op[0] == 'l') {
262 mdio_list_devices();
263
264 return 0;
265 }
266
267 /* Save the chosen bus */
268 miiphy_set_current_dev(bus->name);
269
270 switch (op[0]) {
271 case 'w':
Carlo Caione1d70a3f2019-02-08 17:25:08 +0000272 mdio_write_ranges(bus, addrlo, addrhi, devadlo, devadhi,
Stefano Babic6894ea72013-09-02 15:42:32 +0200273 reglo, reghi, data, extended);
Andy Fleminga1ca3682011-03-21 18:03:22 -0500274 break;
275
276 case 'r':
Carlo Caione1d70a3f2019-02-08 17:25:08 +0000277 mdio_read_ranges(bus, addrlo, addrhi, devadlo, devadhi,
Stefano Babic6894ea72013-09-02 15:42:32 +0200278 reglo, reghi, extended);
Andy Fleminga1ca3682011-03-21 18:03:22 -0500279 break;
280 }
281
282 /*
283 * Save the parameters for repeats.
284 */
285 last_op[0] = op[0];
286 last_addr_lo = addrlo;
287 last_addr_hi = addrhi;
288 last_devad_lo = devadlo;
289 last_devad_hi = devadhi;
290 last_reg_lo = reglo;
291 last_reg_hi = reghi;
292 last_data = data;
293
294 return 0;
295}
296
297/***************************************************/
298
299U_BOOT_CMD(
300 mdio, 6, 1, do_mdio,
301 "MDIO utility commands",
302 "list - List MDIO buses\n"
303 "mdio read <phydev> [<devad>.]<reg> - "
304 "read PHY's register at <devad>.<reg>\n"
305 "mdio write <phydev> [<devad>.]<reg> <data> - "
306 "write PHY's register at <devad>.<reg>\n"
Stefano Babic6894ea72013-09-02 15:42:32 +0200307 "mdio rx <phydev> [<devad>.]<reg> - "
308 "read PHY's extended register at <devad>.<reg>\n"
309 "mdio wx <phydev> [<devad>.]<reg> <data> - "
310 "write PHY's extended register at <devad>.<reg>\n"
Andy Fleminga1ca3682011-03-21 18:03:22 -0500311 "<phydev> may be:\n"
312 " <busname> <addr>\n"
313 " <addr>\n"
314 " <eth name>\n"
315 "<addr> <devad>, and <reg> may be ranges, e.g. 1-5.4-0x1f.\n"
316);