1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2011 Freescale Semiconductor, Inc
17 static char last_op[2];
18 static uint last_data;
19 static uint last_addr_lo;
20 static uint last_addr_hi;
21 static uint last_devad_lo;
22 static uint last_devad_hi;
23 static uint last_reg_lo;
24 static uint last_reg_hi;
26 static int extract_range(char *input, int *plo, int *phi)
29 *plo = simple_strtol(input, &end, 16);
33 if ((*end == '-') && *(++end))
34 *phi = simple_strtol(end, NULL, 16);
35 else if (*end == '\0')
43 static int mdio_write_ranges(struct mii_dev *bus,
45 int addrhi, int devadlo, int devadhi,
46 int reglo, int reghi, unsigned short data,
49 struct phy_device *phydev;
53 for (addr = addrlo; addr <= addrhi; addr++) {
54 phydev = bus->phymap[addr];
56 for (devad = devadlo; devad <= devadhi; devad++) {
57 for (reg = reglo; reg <= reghi; reg++) {
59 err = bus->write(bus, addr, devad,
62 err = phy_write_mmd(phydev, devad,
65 err = phydev->drv->writeext(phydev,
66 addr, devad, reg, data);
78 static int mdio_read_ranges(struct mii_dev *bus,
80 int addrhi, int devadlo, int devadhi,
81 int reglo, int reghi, int extended)
84 struct phy_device *phydev;
86 printf("Reading from bus %s\n", bus->name);
87 for (addr = addrlo; addr <= addrhi; addr++) {
88 phydev = bus->phymap[addr];
89 printf("PHY at address %x:\n", addr);
91 for (devad = devadlo; devad <= devadhi; devad++) {
92 for (reg = reglo; reg <= reghi; reg++) {
96 val = bus->read(bus, addr, devad, reg);
98 val = phy_read_mmd(phydev, devad, reg);
100 val = phydev->drv->readext(phydev, addr,
110 printf("%d.", devad);
112 printf("%d - 0x%x\n", reg, val & 0xffff);
120 /* The register will be in the form [a[-b].]x[-y] */
121 static int extract_reg_range(char *input, int *devadlo, int *devadhi,
122 int *reglo, int *reghi)
126 /* use strrchr to find the last string after a '.' */
127 regstr = strrchr(input, '.');
129 /* If it exists, extract the devad(s) */
133 strncpy(devadstr, input, regstr - input);
134 devadstr[regstr - input] = '\0';
136 if (extract_range(devadstr, devadlo, devadhi))
141 /* Otherwise, we have no devad, and we just got regs */
142 *devadlo = *devadhi = MDIO_DEVAD_NONE;
147 return extract_range(regstr, reglo, reghi);
150 static int extract_phy_range(char *const argv[], int argc, struct mii_dev **bus,
151 struct phy_device **phydev,
152 int *addrlo, int *addrhi)
154 struct phy_device *dev = *phydev;
156 if ((argc < 1) || (argc > 2))
159 /* If there are two arguments, it's busname addr */
161 *bus = miiphy_get_dev_by_name(argv[0]);
166 return extract_range(argv[1], addrlo, addrhi);
169 /* It must be one argument, here */
172 * This argument can be one of two things:
173 * 1) Ethernet device name
174 * 2) Just an address (use the previously-used bus)
176 * We check all buses for a PHY which is connected to an ethernet
177 * device by the given name. If none are found, we call
178 * extract_range() on the string, and see if it's an address range.
180 dev = mdio_phydev_for_ethname(argv[0]);
183 *addrlo = *addrhi = dev->addr;
189 /* It's an address or nothing useful */
190 return extract_range(argv[0], addrlo, addrhi);
193 /* ---------------------------------------------------------------- */
194 static int do_mdio(struct cmd_tbl *cmdtp, int flag, int argc,
198 int addrlo, addrhi, reglo, reghi, devadlo, devadhi;
202 struct phy_device *phydev = NULL;
206 return CMD_RET_USAGE;
208 #ifdef CONFIG_DM_MDIO
209 /* probe DM MII device before any operation so they are all accesible */
210 dm_mdio_probe_devices();
214 * We use the last specified parameters, unless new ones are
218 addrlo = last_addr_lo;
219 addrhi = last_addr_hi;
220 devadlo = last_devad_lo;
221 devadhi = last_devad_hi;
226 bus = mdio_get_current_dev();
228 if (flag & CMD_FLAG_REPEAT)
231 if (strlen(argv[1]) > 1) {
234 phydev = mdio_phydev_for_ethname(argv[2]);
237 addrlo = phydev->addr;
242 return CMD_RET_FAILURE;
246 (!phydev->drv->writeext && (op[0] == 'w')) ||
247 (!phydev->drv->readext && (op[0] == 'r'))) {
248 puts("PHY does not have extended functions\n");
249 return CMD_RET_FAILURE;
257 data = simple_strtoul(argv[pos--], NULL, 16);
258 /* Intentional fall-through - Get reg for read and write */
261 if (extract_reg_range(argv[pos--], &devadlo, &devadhi,
263 return CMD_RET_FAILURE;
264 /* Intentional fall-through - Get phy for all commands */
267 if (extract_phy_range(&argv[2], pos - 1, &bus,
268 &phydev, &addrlo, &addrhi))
269 return CMD_RET_FAILURE;
275 puts("No MDIO bus found\n");
276 return CMD_RET_FAILURE;
285 /* Save the chosen bus */
286 miiphy_set_current_dev(bus->name);
290 mdio_write_ranges(bus, addrlo, addrhi, devadlo, devadhi,
291 reglo, reghi, data, extended);
295 mdio_read_ranges(bus, addrlo, addrhi, devadlo, devadhi,
296 reglo, reghi, extended);
301 * Save the parameters for repeats.
304 last_addr_lo = addrlo;
305 last_addr_hi = addrhi;
306 last_devad_lo = devadlo;
307 last_devad_hi = devadhi;
315 /***************************************************/
319 "MDIO utility commands",
320 "list - List MDIO buses\n"
321 "mdio read <phydev> [<devad>.]<reg> - "
322 "read PHY's register at <devad>.<reg>\n"
323 "mdio write <phydev> [<devad>.]<reg> <data> - "
324 "write PHY's register at <devad>.<reg>\n"
325 "mdio rx <phydev> [<devad>.]<reg> - "
326 "read PHY's extended register at <devad>.<reg>\n"
327 "mdio wx <phydev> [<devad>.]<reg> <data> - "
328 "write PHY's extended register at <devad>.<reg>\n"
330 " <busname> <addr>\n"
333 "<addr> <devad>, and <reg> may be ranges, e.g. 1-5.4-0x1f.\n"