2 * (C) Copyright 2011 Freescale Semiconductor, Inc
5 * SPDX-License-Identifier: GPL-2.0+
18 static char last_op[2];
19 static uint last_data;
20 static uint last_addr_lo;
21 static uint last_addr_hi;
22 static uint last_devad_lo;
23 static uint last_devad_hi;
24 static uint last_reg_lo;
25 static uint last_reg_hi;
27 static int extract_range(char *input, int *plo, int *phi)
30 *plo = simple_strtol(input, &end, 0);
34 if ((*end == '-') && *(++end))
35 *phi = simple_strtol(end, NULL, 0);
36 else if (*end == '\0')
44 static int mdio_write_ranges(struct phy_device *phydev, struct mii_dev *bus,
46 int addrhi, int devadlo, int devadhi,
47 int reglo, int reghi, unsigned short data,
53 for (addr = addrlo; addr <= addrhi; addr++) {
54 for (devad = devadlo; devad <= devadhi; devad++) {
55 for (reg = reglo; reg <= reghi; reg++) {
57 err = bus->write(bus, addr, devad,
60 err = phydev->drv->writeext(phydev,
61 addr, devad, reg, data);
73 static int mdio_read_ranges(struct phy_device *phydev, struct mii_dev *bus,
75 int addrhi, int devadlo, int devadhi,
76 int reglo, int reghi, int extended)
80 printf("Reading from bus %s\n", bus->name);
81 for (addr = addrlo; addr <= addrhi; addr++) {
82 printf("PHY at address %d:\n", addr);
84 for (devad = devadlo; devad <= devadhi; devad++) {
85 for (reg = reglo; reg <= reghi; reg++) {
89 val = bus->read(bus, addr, devad, reg);
91 val = phydev->drv->readext(phydev, addr,
101 printf("%d.", devad);
103 printf("%d - 0x%x\n", reg, val & 0xffff);
111 /* The register will be in the form [a[-b].]x[-y] */
112 static int extract_reg_range(char *input, int *devadlo, int *devadhi,
113 int *reglo, int *reghi)
117 /* use strrchr to find the last string after a '.' */
118 regstr = strrchr(input, '.');
120 /* If it exists, extract the devad(s) */
124 strncpy(devadstr, input, regstr - input);
125 devadstr[regstr - input] = '\0';
127 if (extract_range(devadstr, devadlo, devadhi))
132 /* Otherwise, we have no devad, and we just got regs */
133 *devadlo = *devadhi = MDIO_DEVAD_NONE;
138 return extract_range(regstr, reglo, reghi);
141 static int extract_phy_range(char *const argv[], int argc, struct mii_dev **bus,
142 struct phy_device **phydev,
143 int *addrlo, int *addrhi)
145 struct phy_device *dev = *phydev;
147 if ((argc < 1) || (argc > 2))
150 /* If there are two arguments, it's busname addr */
152 *bus = miiphy_get_dev_by_name(argv[0]);
157 return extract_range(argv[1], addrlo, addrhi);
160 /* It must be one argument, here */
163 * This argument can be one of two things:
164 * 1) Ethernet device name
165 * 2) Just an address (use the previously-used bus)
167 * We check all buses for a PHY which is connected to an ethernet
168 * device by the given name. If none are found, we call
169 * extract_range() on the string, and see if it's an address range.
171 dev = mdio_phydev_for_ethname(argv[0]);
174 *addrlo = *addrhi = dev->addr;
180 /* It's an address or nothing useful */
181 return extract_range(argv[0], addrlo, addrhi);
184 /* ---------------------------------------------------------------- */
185 static int do_mdio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
188 int addrlo, addrhi, reglo, reghi, devadlo, devadhi;
192 struct phy_device *phydev = NULL;
196 return CMD_RET_USAGE;
199 * We use the last specified parameters, unless new ones are
203 addrlo = last_addr_lo;
204 addrhi = last_addr_hi;
205 devadlo = last_devad_lo;
206 devadhi = last_devad_hi;
211 bus = mdio_get_current_dev();
213 if (flag & CMD_FLAG_REPEAT)
216 if (strlen(argv[1]) > 1) {
219 phydev = mdio_phydev_for_ethname(argv[2]);
222 addrlo = phydev->addr;
231 (!phydev->drv->writeext && (op[0] == 'w')) ||
232 (!phydev->drv->readext && (op[0] == 'r'))) {
233 puts("PHY does not have extended functions\n");
242 data = simple_strtoul(argv[pos--], NULL, 16);
245 if (extract_reg_range(argv[pos--], &devadlo, &devadhi,
251 if (extract_phy_range(&(argv[2]), pos - 1, &bus,
252 &phydev, &addrlo, &addrhi))
264 /* Save the chosen bus */
265 miiphy_set_current_dev(bus->name);
269 mdio_write_ranges(phydev, bus, addrlo, addrhi, devadlo, devadhi,
270 reglo, reghi, data, extended);
274 mdio_read_ranges(phydev, bus, addrlo, addrhi, devadlo, devadhi,
275 reglo, reghi, extended);
280 * Save the parameters for repeats.
283 last_addr_lo = addrlo;
284 last_addr_hi = addrhi;
285 last_devad_lo = devadlo;
286 last_devad_hi = devadhi;
294 /***************************************************/
298 "MDIO utility commands",
299 "list - List MDIO buses\n"
300 "mdio read <phydev> [<devad>.]<reg> - "
301 "read PHY's register at <devad>.<reg>\n"
302 "mdio write <phydev> [<devad>.]<reg> <data> - "
303 "write PHY's register at <devad>.<reg>\n"
304 "mdio rx <phydev> [<devad>.]<reg> - "
305 "read PHY's extended register at <devad>.<reg>\n"
306 "mdio wx <phydev> [<devad>.]<reg> <data> - "
307 "write PHY's extended register at <devad>.<reg>\n"
309 " <busname> <addr>\n"
312 "<addr> <devad>, and <reg> may be ranges, e.g. 1-5.4-0x1f.\n"