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 mii_dev *bus, int addrlo,
45 int addrhi, int devadlo, int devadhi,
46 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++) {
54 err = bus->write(bus, addr, devad, reg, data);
66 static int mdio_read_ranges(struct mii_dev *bus, int addrlo,
67 int addrhi, int devadlo, int devadhi,
72 printf("Reading from bus %s\n", bus->name);
73 for (addr = addrlo; addr <= addrhi; addr++) {
74 printf("PHY at address %d:\n", addr);
76 for (devad = devadlo; devad <= devadhi; devad++) {
77 for (reg = reglo; reg <= reghi; reg++) {
80 val = bus->read(bus, addr, devad, reg);
90 printf("%d - 0x%x\n", reg, val & 0xffff);
98 /* The register will be in the form [a[-b].]x[-y] */
99 static int extract_reg_range(char *input, int *devadlo, int *devadhi,
100 int *reglo, int *reghi)
104 /* use strrchr to find the last string after a '.' */
105 regstr = strrchr(input, '.');
107 /* If it exists, extract the devad(s) */
111 strncpy(devadstr, input, regstr - input);
112 devadstr[regstr - input] = '\0';
114 if (extract_range(devadstr, devadlo, devadhi))
119 /* Otherwise, we have no devad, and we just got regs */
120 *devadlo = *devadhi = MDIO_DEVAD_NONE;
125 return extract_range(regstr, reglo, reghi);
128 static int extract_phy_range(char *const argv[], int argc, struct mii_dev **bus,
129 int *addrlo, int *addrhi)
131 struct phy_device *phydev;
133 if ((argc < 1) || (argc > 2))
136 /* If there are two arguments, it's busname addr */
138 *bus = miiphy_get_dev_by_name(argv[0]);
143 return extract_range(argv[1], addrlo, addrhi);
146 /* It must be one argument, here */
149 * This argument can be one of two things:
150 * 1) Ethernet device name
151 * 2) Just an address (use the previously-used bus)
153 * We check all buses for a PHY which is connected to an ethernet
154 * device by the given name. If none are found, we call
155 * extract_range() on the string, and see if it's an address range.
157 phydev = mdio_phydev_for_ethname(argv[0]);
160 *addrlo = *addrhi = phydev->addr;
166 /* It's an address or nothing useful */
167 return extract_range(argv[0], addrlo, addrhi);
170 /* ---------------------------------------------------------------- */
171 static int do_mdio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
174 int addrlo, addrhi, reglo, reghi, devadlo, devadhi;
180 return CMD_RET_USAGE;
183 * We use the last specified parameters, unless new ones are
187 addrlo = last_addr_lo;
188 addrhi = last_addr_hi;
189 devadlo = last_devad_lo;
190 devadhi = last_devad_hi;
195 bus = mdio_get_current_dev();
197 if (flag & CMD_FLAG_REPEAT)
203 data = simple_strtoul(argv[pos--], NULL, 16);
206 if (extract_reg_range(argv[pos--], &devadlo, &devadhi,
212 if (extract_phy_range(&(argv[2]), pos - 1, &bus,
225 /* Save the chosen bus */
226 miiphy_set_current_dev(bus->name);
230 mdio_write_ranges(bus, addrlo, addrhi, devadlo, devadhi,
235 mdio_read_ranges(bus, addrlo, addrhi, devadlo, devadhi,
241 * Save the parameters for repeats.
244 last_addr_lo = addrlo;
245 last_addr_hi = addrhi;
246 last_devad_lo = devadlo;
247 last_devad_hi = devadhi;
255 /***************************************************/
259 "MDIO utility commands",
260 "list - List MDIO buses\n"
261 "mdio read <phydev> [<devad>.]<reg> - "
262 "read PHY's register at <devad>.<reg>\n"
263 "mdio write <phydev> [<devad>.]<reg> <data> - "
264 "write PHY's register at <devad>.<reg>\n"
266 " <busname> <addr>\n"
269 "<addr> <devad>, and <reg> may be ranges, e.g. 1-5.4-0x1f.\n"