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 = bus->write(bus, addr, devad,
61 err = phy_write_mmd(phydev, devad,
64 err = phydev->drv->writeext(phydev,
65 addr, devad, reg, data);
77 static int mdio_read_ranges(struct mii_dev *bus,
79 int addrhi, int devadlo, int devadhi,
80 int reglo, int reghi, int extended)
83 struct phy_device *phydev;
85 printf("Reading from bus %s\n", bus->name);
86 for (addr = addrlo; addr <= addrhi; addr++) {
87 phydev = bus->phymap[addr];
88 printf("PHY at address %x:\n", addr);
90 for (devad = devadlo; devad <= devadhi; devad++) {
91 for (reg = reglo; reg <= reghi; reg++) {
95 val = bus->read(bus, addr, devad, reg);
97 val = phy_read_mmd(phydev, devad, reg);
99 val = phydev->drv->readext(phydev, addr,
109 printf("%d.", devad);
111 printf("%d - 0x%x\n", reg, val & 0xffff);
119 /* The register will be in the form [a[-b].]x[-y] */
120 static int extract_reg_range(char *input, int *devadlo, int *devadhi,
121 int *reglo, int *reghi)
125 /* use strrchr to find the last string after a '.' */
126 regstr = strrchr(input, '.');
128 /* If it exists, extract the devad(s) */
132 strncpy(devadstr, input, regstr - input);
133 devadstr[regstr - input] = '\0';
135 if (extract_range(devadstr, devadlo, devadhi))
140 /* Otherwise, we have no devad, and we just got regs */
141 *devadlo = *devadhi = MDIO_DEVAD_NONE;
146 return extract_range(regstr, reglo, reghi);
149 static int extract_phy_range(char *const argv[], int argc, struct mii_dev **bus,
150 struct phy_device **phydev,
151 int *addrlo, int *addrhi)
153 struct phy_device *dev = *phydev;
155 if ((argc < 1) || (argc > 2))
158 /* If there are two arguments, it's busname addr */
160 *bus = miiphy_get_dev_by_name(argv[0]);
165 return extract_range(argv[1], addrlo, addrhi);
168 /* It must be one argument, here */
171 * This argument can be one of two things:
172 * 1) Ethernet device name
173 * 2) Just an address (use the previously-used bus)
175 * We check all buses for a PHY which is connected to an ethernet
176 * device by the given name. If none are found, we call
177 * extract_range() on the string, and see if it's an address range.
179 dev = mdio_phydev_for_ethname(argv[0]);
182 *addrlo = *addrhi = dev->addr;
188 /* It's an address or nothing useful */
189 return extract_range(argv[0], addrlo, addrhi);
192 /* ---------------------------------------------------------------- */
193 static int do_mdio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
196 int addrlo, addrhi, reglo, reghi, devadlo, devadhi;
200 struct phy_device *phydev = NULL;
204 return CMD_RET_USAGE;
206 #ifdef CONFIG_DM_MDIO
207 /* probe DM MII device before any operation so they are all accesible */
208 dm_mdio_probe_devices();
212 * We use the last specified parameters, unless new ones are
216 addrlo = last_addr_lo;
217 addrhi = last_addr_hi;
218 devadlo = last_devad_lo;
219 devadhi = last_devad_hi;
224 bus = mdio_get_current_dev();
226 if (flag & CMD_FLAG_REPEAT)
229 if (strlen(argv[1]) > 1) {
232 phydev = mdio_phydev_for_ethname(argv[2]);
235 addrlo = phydev->addr;
240 return CMD_RET_FAILURE;
244 (!phydev->drv->writeext && (op[0] == 'w')) ||
245 (!phydev->drv->readext && (op[0] == 'r'))) {
246 puts("PHY does not have extended functions\n");
247 return CMD_RET_FAILURE;
255 data = simple_strtoul(argv[pos--], NULL, 16);
258 if (extract_reg_range(argv[pos--], &devadlo, &devadhi,
260 return CMD_RET_FAILURE;
264 if (extract_phy_range(&argv[2], pos - 1, &bus,
265 &phydev, &addrlo, &addrhi))
266 return CMD_RET_FAILURE;
272 puts("No MDIO bus found\n");
273 return CMD_RET_FAILURE;
282 /* Save the chosen bus */
283 miiphy_set_current_dev(bus->name);
287 mdio_write_ranges(bus, addrlo, addrhi, devadlo, devadhi,
288 reglo, reghi, data, extended);
292 mdio_read_ranges(bus, addrlo, addrhi, devadlo, devadhi,
293 reglo, reghi, extended);
298 * Save the parameters for repeats.
301 last_addr_lo = addrlo;
302 last_addr_hi = addrhi;
303 last_devad_lo = devadlo;
304 last_devad_hi = devadhi;
312 /***************************************************/
316 "MDIO utility commands",
317 "list - List MDIO buses\n"
318 "mdio read <phydev> [<devad>.]<reg> - "
319 "read PHY's register at <devad>.<reg>\n"
320 "mdio write <phydev> [<devad>.]<reg> <data> - "
321 "write PHY's register at <devad>.<reg>\n"
322 "mdio rx <phydev> [<devad>.]<reg> - "
323 "read PHY's extended register at <devad>.<reg>\n"
324 "mdio wx <phydev> [<devad>.]<reg> <data> - "
325 "write PHY's extended register at <devad>.<reg>\n"
327 " <busname> <addr>\n"
330 "<addr> <devad>, and <reg> may be ranges, e.g. 1-5.4-0x1f.\n"