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 phy_device *phydev, struct mii_dev *bus,
44 int addrhi, int devadlo, int devadhi,
45 int reglo, int reghi, unsigned short data,
51 for (addr = addrlo; addr <= addrhi; addr++) {
52 for (devad = devadlo; devad <= devadhi; devad++) {
53 for (reg = reglo; reg <= reghi; reg++) {
55 err = bus->write(bus, addr, devad,
58 err = phydev->drv->writeext(phydev,
59 addr, devad, reg, data);
71 static int mdio_read_ranges(struct phy_device *phydev, struct mii_dev *bus,
73 int addrhi, int devadlo, int devadhi,
74 int reglo, int reghi, int extended)
78 printf("Reading from bus %s\n", bus->name);
79 for (addr = addrlo; addr <= addrhi; addr++) {
80 printf("PHY at address %x:\n", addr);
82 for (devad = devadlo; devad <= devadhi; devad++) {
83 for (reg = reglo; reg <= reghi; reg++) {
87 val = bus->read(bus, addr, devad, reg);
89 val = phydev->drv->readext(phydev, addr,
101 printf("%d - 0x%x\n", reg, val & 0xffff);
109 /* The register will be in the form [a[-b].]x[-y] */
110 static int extract_reg_range(char *input, int *devadlo, int *devadhi,
111 int *reglo, int *reghi)
115 /* use strrchr to find the last string after a '.' */
116 regstr = strrchr(input, '.');
118 /* If it exists, extract the devad(s) */
122 strncpy(devadstr, input, regstr - input);
123 devadstr[regstr - input] = '\0';
125 if (extract_range(devadstr, devadlo, devadhi))
130 /* Otherwise, we have no devad, and we just got regs */
131 *devadlo = *devadhi = MDIO_DEVAD_NONE;
136 return extract_range(regstr, reglo, reghi);
139 static int extract_phy_range(char *const argv[], int argc, struct mii_dev **bus,
140 struct phy_device **phydev,
141 int *addrlo, int *addrhi)
143 struct phy_device *dev = *phydev;
145 if ((argc < 1) || (argc > 2))
148 /* If there are two arguments, it's busname addr */
150 *bus = miiphy_get_dev_by_name(argv[0]);
155 return extract_range(argv[1], addrlo, addrhi);
158 /* It must be one argument, here */
161 * This argument can be one of two things:
162 * 1) Ethernet device name
163 * 2) Just an address (use the previously-used bus)
165 * We check all buses for a PHY which is connected to an ethernet
166 * device by the given name. If none are found, we call
167 * extract_range() on the string, and see if it's an address range.
169 dev = mdio_phydev_for_ethname(argv[0]);
172 *addrlo = *addrhi = dev->addr;
178 /* It's an address or nothing useful */
179 return extract_range(argv[0], addrlo, addrhi);
182 /* ---------------------------------------------------------------- */
183 static int do_mdio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
186 int addrlo, addrhi, reglo, reghi, devadlo, devadhi;
190 struct phy_device *phydev = NULL;
194 return CMD_RET_USAGE;
197 * We use the last specified parameters, unless new ones are
201 addrlo = last_addr_lo;
202 addrhi = last_addr_hi;
203 devadlo = last_devad_lo;
204 devadhi = last_devad_hi;
209 bus = mdio_get_current_dev();
211 if (flag & CMD_FLAG_REPEAT)
214 if (strlen(argv[1]) > 1) {
217 phydev = mdio_phydev_for_ethname(argv[2]);
220 addrlo = phydev->addr;
229 (!phydev->drv->writeext && (op[0] == 'w')) ||
230 (!phydev->drv->readext && (op[0] == 'r'))) {
231 puts("PHY does not have extended functions\n");
240 data = simple_strtoul(argv[pos--], NULL, 16);
243 if (extract_reg_range(argv[pos--], &devadlo, &devadhi,
249 if (extract_phy_range(&argv[2], pos - 1, &bus,
250 &phydev, &addrlo, &addrhi))
262 /* Save the chosen bus */
263 miiphy_set_current_dev(bus->name);
267 mdio_write_ranges(phydev, bus, addrlo, addrhi, devadlo, devadhi,
268 reglo, reghi, data, extended);
272 mdio_read_ranges(phydev, bus, addrlo, addrhi, devadlo, devadhi,
273 reglo, reghi, extended);
278 * Save the parameters for repeats.
281 last_addr_lo = addrlo;
282 last_addr_hi = addrhi;
283 last_devad_lo = devadlo;
284 last_devad_hi = devadhi;
292 /***************************************************/
296 "MDIO utility commands",
297 "list - List MDIO buses\n"
298 "mdio read <phydev> [<devad>.]<reg> - "
299 "read PHY's register at <devad>.<reg>\n"
300 "mdio write <phydev> [<devad>.]<reg> <data> - "
301 "write PHY's register at <devad>.<reg>\n"
302 "mdio rx <phydev> [<devad>.]<reg> - "
303 "read PHY's extended register at <devad>.<reg>\n"
304 "mdio wx <phydev> [<devad>.]<reg> <data> - "
305 "write PHY's extended register at <devad>.<reg>\n"
307 " <busname> <addr>\n"
310 "<addr> <devad>, and <reg> may be ranges, e.g. 1-5.4-0x1f.\n"