1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2011 Freescale Semiconductor, Inc
16 static char last_op[2];
17 static uint last_data;
18 static uint last_addr_lo;
19 static uint last_addr_hi;
20 static uint last_devad_lo;
21 static uint last_devad_hi;
22 static uint last_reg_lo;
23 static uint last_reg_hi;
25 static int extract_range(char *input, int *plo, int *phi)
28 *plo = simple_strtol(input, &end, 16);
32 if ((*end == '-') && *(++end))
33 *phi = simple_strtol(end, NULL, 16);
34 else if (*end == '\0')
42 static int mdio_write_ranges(struct mii_dev *bus,
44 int addrhi, int devadlo, int devadhi,
45 int reglo, int reghi, unsigned short data,
48 struct phy_device *phydev;
52 for (addr = addrlo; addr <= addrhi; addr++) {
53 phydev = bus->phymap[addr];
55 for (devad = devadlo; devad <= devadhi; devad++) {
56 for (reg = reglo; reg <= reghi; reg++) {
58 err = phy_write_mmd(phydev, devad,
61 err = phydev->drv->writeext(phydev,
62 addr, devad, reg, data);
74 static int mdio_read_ranges(struct mii_dev *bus,
76 int addrhi, int devadlo, int devadhi,
77 int reglo, int reghi, int extended)
80 struct phy_device *phydev;
82 printf("Reading from bus %s\n", bus->name);
83 for (addr = addrlo; addr <= addrhi; addr++) {
84 phydev = bus->phymap[addr];
85 printf("PHY at address %x:\n", addr);
87 for (devad = devadlo; devad <= devadhi; devad++) {
88 for (reg = reglo; reg <= reghi; reg++) {
92 val = phy_read_mmd(phydev, devad, reg);
94 val = phydev->drv->readext(phydev, addr,
104 printf("%d.", devad);
106 printf("%d - 0x%x\n", reg, val & 0xffff);
114 /* The register will be in the form [a[-b].]x[-y] */
115 static int extract_reg_range(char *input, int *devadlo, int *devadhi,
116 int *reglo, int *reghi)
120 /* use strrchr to find the last string after a '.' */
121 regstr = strrchr(input, '.');
123 /* If it exists, extract the devad(s) */
127 strncpy(devadstr, input, regstr - input);
128 devadstr[regstr - input] = '\0';
130 if (extract_range(devadstr, devadlo, devadhi))
135 /* Otherwise, we have no devad, and we just got regs */
136 *devadlo = *devadhi = MDIO_DEVAD_NONE;
141 return extract_range(regstr, reglo, reghi);
144 static int extract_phy_range(char *const argv[], int argc, struct mii_dev **bus,
145 struct phy_device **phydev,
146 int *addrlo, int *addrhi)
148 struct phy_device *dev = *phydev;
150 if ((argc < 1) || (argc > 2))
153 /* If there are two arguments, it's busname addr */
155 *bus = miiphy_get_dev_by_name(argv[0]);
160 return extract_range(argv[1], addrlo, addrhi);
163 /* It must be one argument, here */
166 * This argument can be one of two things:
167 * 1) Ethernet device name
168 * 2) Just an address (use the previously-used bus)
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.
174 dev = mdio_phydev_for_ethname(argv[0]);
177 *addrlo = *addrhi = dev->addr;
183 /* It's an address or nothing useful */
184 return extract_range(argv[0], addrlo, addrhi);
187 /* ---------------------------------------------------------------- */
188 static int do_mdio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
191 int addrlo, addrhi, reglo, reghi, devadlo, devadhi;
195 struct phy_device *phydev = NULL;
199 return CMD_RET_USAGE;
202 * We use the last specified parameters, unless new ones are
206 addrlo = last_addr_lo;
207 addrhi = last_addr_hi;
208 devadlo = last_devad_lo;
209 devadhi = last_devad_hi;
214 bus = mdio_get_current_dev();
216 if (flag & CMD_FLAG_REPEAT)
219 if (strlen(argv[1]) > 1) {
222 phydev = mdio_phydev_for_ethname(argv[2]);
225 addrlo = phydev->addr;
230 return CMD_RET_FAILURE;
234 (!phydev->drv->writeext && (op[0] == 'w')) ||
235 (!phydev->drv->readext && (op[0] == 'r'))) {
236 puts("PHY does not have extended functions\n");
237 return CMD_RET_FAILURE;
245 data = simple_strtoul(argv[pos--], NULL, 16);
248 if (extract_reg_range(argv[pos--], &devadlo, &devadhi,
250 return CMD_RET_FAILURE;
254 if (extract_phy_range(&argv[2], pos - 1, &bus,
255 &phydev, &addrlo, &addrhi))
256 return CMD_RET_FAILURE;
267 /* Save the chosen bus */
268 miiphy_set_current_dev(bus->name);
272 mdio_write_ranges(bus, addrlo, addrhi, devadlo, devadhi,
273 reglo, reghi, data, extended);
277 mdio_read_ranges(bus, addrlo, addrhi, devadlo, devadhi,
278 reglo, reghi, extended);
283 * Save the parameters for repeats.
286 last_addr_lo = addrlo;
287 last_addr_hi = addrhi;
288 last_devad_lo = devadlo;
289 last_devad_hi = devadhi;
297 /***************************************************/
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"
307 "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"
312 " <busname> <addr>\n"
315 "<addr> <devad>, and <reg> may be ranges, e.g. 1-5.4-0x1f.\n"