2 * (C) Copyright 2011 Freescale Semiconductor, Inc
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
34 static char last_op[2];
35 static uint last_data;
36 static uint last_addr_lo;
37 static uint last_addr_hi;
38 static uint last_devad_lo;
39 static uint last_devad_hi;
40 static uint last_reg_lo;
41 static uint last_reg_hi;
43 static int extract_range(char *input, int *plo, int *phi)
46 *plo = simple_strtol(input, &end, 0);
50 if ((*end == '-') && *(++end))
51 *phi = simple_strtol(end, NULL, 0);
52 else if (*end == '\0')
60 static int mdio_write_ranges(struct mii_dev *bus, int addrlo,
61 int addrhi, int devadlo, int devadhi,
62 int reglo, int reghi, unsigned short data)
67 for (addr = addrlo; addr <= addrhi; addr++) {
68 for (devad = devadlo; devad <= devadhi; devad++) {
69 for (reg = reglo; reg <= reghi; reg++) {
70 err = bus->write(bus, addr, devad, reg, data);
82 static int mdio_read_ranges(struct mii_dev *bus, int addrlo,
83 int addrhi, int devadlo, int devadhi,
88 printf("Reading from bus %s\n", bus->name);
89 for (addr = addrlo; addr <= addrhi; addr++) {
90 printf("PHY at address %d:\n", addr);
92 for (devad = devadlo; devad <= devadhi; devad++) {
93 for (reg = reglo; reg <= reghi; reg++) {
96 val = bus->read(bus, addr, devad, reg);
104 printf("%d.", devad);
106 printf("%d - 0x%x\n", reg, val & 0xffff);
114 /* The register will be in the form [a[-b].]x[-y] */
115 static int extract_reg_range(char *input, int *devadlo, int *devadhi,
116 int *reglo, int *reghi)
120 /* use strrchr to find the last string after a '.' */
121 regstr = strrchr(input, '.');
123 /* If it exists, extract the devad(s) */
127 strncpy(devadstr, input, regstr - input);
128 devadstr[regstr - input] = '\0';
130 if (extract_range(devadstr, devadlo, devadhi))
135 /* Otherwise, we have no devad, and we just got regs */
136 *devadlo = *devadhi = MDIO_DEVAD_NONE;
141 return extract_range(regstr, reglo, reghi);
144 static int extract_phy_range(char *const argv[], int argc, struct mii_dev **bus,
145 int *addrlo, int *addrhi)
147 struct phy_device *phydev;
149 if ((argc < 1) || (argc > 2))
152 /* If there are two arguments, it's busname addr */
154 *bus = miiphy_get_dev_by_name(argv[0]);
159 return extract_range(argv[1], addrlo, addrhi);
162 /* It must be one argument, here */
165 * This argument can be one of two things:
166 * 1) Ethernet device name
167 * 2) Just an address (use the previously-used bus)
169 * We check all buses for a PHY which is connected to an ethernet
170 * device by the given name. If none are found, we call
171 * extract_range() on the string, and see if it's an address range.
173 phydev = mdio_phydev_for_ethname(argv[0]);
176 *addrlo = *addrhi = phydev->addr;
182 /* It's an address or nothing useful */
183 return extract_range(argv[0], addrlo, addrhi);
186 /* ---------------------------------------------------------------- */
187 static int do_mdio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
190 int addrlo, addrhi, reglo, reghi, devadlo, devadhi;
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)
219 data = simple_strtoul(argv[pos--], NULL, 16);
222 if (extract_reg_range(argv[pos--], &devadlo, &devadhi,
228 if (extract_phy_range(&(argv[2]), pos - 1, &bus,
241 /* Save the chosen bus */
242 miiphy_set_current_dev(bus->name);
246 mdio_write_ranges(bus, addrlo, addrhi, devadlo, devadhi,
251 mdio_read_ranges(bus, addrlo, addrhi, devadlo, devadhi,
257 * Save the parameters for repeats.
260 last_addr_lo = addrlo;
261 last_addr_hi = addrhi;
262 last_devad_lo = devadlo;
263 last_devad_hi = devadhi;
271 /***************************************************/
275 "MDIO utility commands",
276 "list - List MDIO buses\n"
277 "mdio read <phydev> [<devad>.]<reg> - "
278 "read PHY's register at <devad>.<reg>\n"
279 "mdio write <phydev> [<devad>.]<reg> <data> - "
280 "write PHY's register at <devad>.<reg>\n"
282 " <busname> <addr>\n"
285 "<addr> <devad>, and <reg> may be ranges, e.g. 1-5.4-0x1f.\n"