Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Andy Fleming | 995daa0 | 2011-03-21 18:03:22 -0500 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2011 Freescale Semiconductor, Inc |
| 4 | * Andy Fleming |
Andy Fleming | 995daa0 | 2011-03-21 18:03:22 -0500 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * MDIO Commands |
| 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <command.h> |
| 13 | #include <miiphy.h> |
| 14 | #include <phy.h> |
| 15 | |
Andy Fleming | 995daa0 | 2011-03-21 18:03:22 -0500 | [diff] [blame] | 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; |
| 24 | |
| 25 | static int extract_range(char *input, int *plo, int *phi) |
| 26 | { |
| 27 | char *end; |
Michal Simek | 15a2acd | 2016-11-16 08:41:01 +0100 | [diff] [blame] | 28 | *plo = simple_strtol(input, &end, 16); |
Andy Fleming | 995daa0 | 2011-03-21 18:03:22 -0500 | [diff] [blame] | 29 | if (end == input) |
| 30 | return -1; |
| 31 | |
| 32 | if ((*end == '-') && *(++end)) |
Michal Simek | 15a2acd | 2016-11-16 08:41:01 +0100 | [diff] [blame] | 33 | *phi = simple_strtol(end, NULL, 16); |
Andy Fleming | 995daa0 | 2011-03-21 18:03:22 -0500 | [diff] [blame] | 34 | else if (*end == '\0') |
| 35 | *phi = *plo; |
| 36 | else |
| 37 | return -1; |
| 38 | |
| 39 | return 0; |
| 40 | } |
| 41 | |
Carlo Caione | e55047e | 2019-02-08 17:25:08 +0000 | [diff] [blame] | 42 | static int mdio_write_ranges(struct mii_dev *bus, |
Stefano Babic | 26b807c | 2013-09-02 15:42:32 +0200 | [diff] [blame] | 43 | int addrlo, |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 44 | int addrhi, int devadlo, int devadhi, |
Stefano Babic | 26b807c | 2013-09-02 15:42:32 +0200 | [diff] [blame] | 45 | int reglo, int reghi, unsigned short data, |
| 46 | int extended) |
Andy Fleming | 995daa0 | 2011-03-21 18:03:22 -0500 | [diff] [blame] | 47 | { |
Carlo Caione | e55047e | 2019-02-08 17:25:08 +0000 | [diff] [blame] | 48 | struct phy_device *phydev; |
Andy Fleming | 995daa0 | 2011-03-21 18:03:22 -0500 | [diff] [blame] | 49 | int addr, devad, reg; |
| 50 | int err = 0; |
| 51 | |
| 52 | for (addr = addrlo; addr <= addrhi; addr++) { |
Carlo Caione | e55047e | 2019-02-08 17:25:08 +0000 | [diff] [blame] | 53 | phydev = bus->phymap[addr]; |
| 54 | |
Andy Fleming | 995daa0 | 2011-03-21 18:03:22 -0500 | [diff] [blame] | 55 | for (devad = devadlo; devad <= devadhi; devad++) { |
| 56 | for (reg = reglo; reg <= reghi; reg++) { |
Vladimir Oltean | b4c20f2 | 2019-05-30 03:08:32 +0300 | [diff] [blame] | 57 | if (!phydev) |
| 58 | err = bus->write(bus, addr, devad, |
| 59 | reg, data); |
| 60 | else if (!extended) |
Carlo Caione | e55047e | 2019-02-08 17:25:08 +0000 | [diff] [blame] | 61 | err = phy_write_mmd(phydev, devad, |
| 62 | reg, data); |
Stefano Babic | 26b807c | 2013-09-02 15:42:32 +0200 | [diff] [blame] | 63 | else |
| 64 | err = phydev->drv->writeext(phydev, |
| 65 | addr, devad, reg, data); |
Andy Fleming | 995daa0 | 2011-03-21 18:03:22 -0500 | [diff] [blame] | 66 | |
| 67 | if (err) |
| 68 | goto err_out; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | err_out: |
| 74 | return err; |
| 75 | } |
| 76 | |
Carlo Caione | e55047e | 2019-02-08 17:25:08 +0000 | [diff] [blame] | 77 | static int mdio_read_ranges(struct mii_dev *bus, |
Stefano Babic | 26b807c | 2013-09-02 15:42:32 +0200 | [diff] [blame] | 78 | int addrlo, |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 79 | int addrhi, int devadlo, int devadhi, |
Stefano Babic | 26b807c | 2013-09-02 15:42:32 +0200 | [diff] [blame] | 80 | int reglo, int reghi, int extended) |
Andy Fleming | 995daa0 | 2011-03-21 18:03:22 -0500 | [diff] [blame] | 81 | { |
| 82 | int addr, devad, reg; |
Carlo Caione | e55047e | 2019-02-08 17:25:08 +0000 | [diff] [blame] | 83 | struct phy_device *phydev; |
Andy Fleming | 995daa0 | 2011-03-21 18:03:22 -0500 | [diff] [blame] | 84 | |
| 85 | printf("Reading from bus %s\n", bus->name); |
| 86 | for (addr = addrlo; addr <= addrhi; addr++) { |
Carlo Caione | e55047e | 2019-02-08 17:25:08 +0000 | [diff] [blame] | 87 | phydev = bus->phymap[addr]; |
Michal Simek | 15a2acd | 2016-11-16 08:41:01 +0100 | [diff] [blame] | 88 | printf("PHY at address %x:\n", addr); |
Andy Fleming | 995daa0 | 2011-03-21 18:03:22 -0500 | [diff] [blame] | 89 | |
| 90 | for (devad = devadlo; devad <= devadhi; devad++) { |
| 91 | for (reg = reglo; reg <= reghi; reg++) { |
Anatolij Gustschin | a621b16 | 2011-04-30 02:39:29 +0000 | [diff] [blame] | 92 | int val; |
Andy Fleming | 995daa0 | 2011-03-21 18:03:22 -0500 | [diff] [blame] | 93 | |
Vladimir Oltean | b4c20f2 | 2019-05-30 03:08:32 +0300 | [diff] [blame] | 94 | if (!phydev) |
| 95 | val = bus->read(bus, addr, devad, reg); |
| 96 | else if (!extended) |
Carlo Caione | e55047e | 2019-02-08 17:25:08 +0000 | [diff] [blame] | 97 | val = phy_read_mmd(phydev, devad, reg); |
Stefano Babic | 26b807c | 2013-09-02 15:42:32 +0200 | [diff] [blame] | 98 | else |
| 99 | val = phydev->drv->readext(phydev, addr, |
| 100 | devad, reg); |
| 101 | |
Andy Fleming | 995daa0 | 2011-03-21 18:03:22 -0500 | [diff] [blame] | 102 | if (val < 0) { |
| 103 | printf("Error\n"); |
| 104 | |
| 105 | return val; |
| 106 | } |
| 107 | |
| 108 | if (devad >= 0) |
| 109 | printf("%d.", devad); |
| 110 | |
| 111 | printf("%d - 0x%x\n", reg, val & 0xffff); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | /* The register will be in the form [a[-b].]x[-y] */ |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 120 | static int extract_reg_range(char *input, int *devadlo, int *devadhi, |
| 121 | int *reglo, int *reghi) |
Andy Fleming | 995daa0 | 2011-03-21 18:03:22 -0500 | [diff] [blame] | 122 | { |
| 123 | char *regstr; |
| 124 | |
| 125 | /* use strrchr to find the last string after a '.' */ |
| 126 | regstr = strrchr(input, '.'); |
| 127 | |
| 128 | /* If it exists, extract the devad(s) */ |
| 129 | if (regstr) { |
| 130 | char devadstr[32]; |
| 131 | |
| 132 | strncpy(devadstr, input, regstr - input); |
| 133 | devadstr[regstr - input] = '\0'; |
| 134 | |
| 135 | if (extract_range(devadstr, devadlo, devadhi)) |
| 136 | return -1; |
| 137 | |
| 138 | regstr++; |
| 139 | } else { |
| 140 | /* Otherwise, we have no devad, and we just got regs */ |
| 141 | *devadlo = *devadhi = MDIO_DEVAD_NONE; |
| 142 | |
| 143 | regstr = input; |
| 144 | } |
| 145 | |
| 146 | return extract_range(regstr, reglo, reghi); |
| 147 | } |
| 148 | |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 149 | static int extract_phy_range(char *const argv[], int argc, struct mii_dev **bus, |
Stefano Babic | 26b807c | 2013-09-02 15:42:32 +0200 | [diff] [blame] | 150 | struct phy_device **phydev, |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 151 | int *addrlo, int *addrhi) |
Andy Fleming | 995daa0 | 2011-03-21 18:03:22 -0500 | [diff] [blame] | 152 | { |
Stefano Babic | 26b807c | 2013-09-02 15:42:32 +0200 | [diff] [blame] | 153 | struct phy_device *dev = *phydev; |
Andy Fleming | 995daa0 | 2011-03-21 18:03:22 -0500 | [diff] [blame] | 154 | |
| 155 | if ((argc < 1) || (argc > 2)) |
| 156 | return -1; |
| 157 | |
| 158 | /* If there are two arguments, it's busname addr */ |
| 159 | if (argc == 2) { |
| 160 | *bus = miiphy_get_dev_by_name(argv[0]); |
| 161 | |
| 162 | if (!*bus) |
| 163 | return -1; |
| 164 | |
| 165 | return extract_range(argv[1], addrlo, addrhi); |
| 166 | } |
| 167 | |
| 168 | /* It must be one argument, here */ |
| 169 | |
| 170 | /* |
| 171 | * This argument can be one of two things: |
| 172 | * 1) Ethernet device name |
| 173 | * 2) Just an address (use the previously-used bus) |
| 174 | * |
| 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. |
| 178 | */ |
Stefano Babic | 26b807c | 2013-09-02 15:42:32 +0200 | [diff] [blame] | 179 | dev = mdio_phydev_for_ethname(argv[0]); |
Andy Fleming | 995daa0 | 2011-03-21 18:03:22 -0500 | [diff] [blame] | 180 | |
Stefano Babic | 26b807c | 2013-09-02 15:42:32 +0200 | [diff] [blame] | 181 | if (dev) { |
| 182 | *addrlo = *addrhi = dev->addr; |
| 183 | *bus = dev->bus; |
Andy Fleming | 995daa0 | 2011-03-21 18:03:22 -0500 | [diff] [blame] | 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | /* It's an address or nothing useful */ |
| 189 | return extract_range(argv[0], addrlo, addrhi); |
| 190 | } |
| 191 | |
| 192 | /* ---------------------------------------------------------------- */ |
| 193 | static int do_mdio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 194 | { |
| 195 | char op[2]; |
| 196 | int addrlo, addrhi, reglo, reghi, devadlo, devadhi; |
| 197 | unsigned short data; |
| 198 | int pos = argc - 1; |
| 199 | struct mii_dev *bus; |
Stefano Babic | 26b807c | 2013-09-02 15:42:32 +0200 | [diff] [blame] | 200 | struct phy_device *phydev = NULL; |
| 201 | int extended = 0; |
Andy Fleming | 995daa0 | 2011-03-21 18:03:22 -0500 | [diff] [blame] | 202 | |
| 203 | if (argc < 2) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 204 | return CMD_RET_USAGE; |
Andy Fleming | 995daa0 | 2011-03-21 18:03:22 -0500 | [diff] [blame] | 205 | |
Alex Marginean | c3452b5 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 206 | #ifdef CONFIG_DM_MDIO |
| 207 | /* probe DM MII device before any operation so they are all accesible */ |
| 208 | dm_mdio_probe_devices(); |
| 209 | #endif |
| 210 | |
Andy Fleming | 995daa0 | 2011-03-21 18:03:22 -0500 | [diff] [blame] | 211 | /* |
| 212 | * We use the last specified parameters, unless new ones are |
| 213 | * entered. |
| 214 | */ |
| 215 | op[0] = argv[1][0]; |
| 216 | addrlo = last_addr_lo; |
| 217 | addrhi = last_addr_hi; |
| 218 | devadlo = last_devad_lo; |
| 219 | devadhi = last_devad_hi; |
| 220 | reglo = last_reg_lo; |
| 221 | reghi = last_reg_hi; |
| 222 | data = last_data; |
| 223 | |
| 224 | bus = mdio_get_current_dev(); |
| 225 | |
| 226 | if (flag & CMD_FLAG_REPEAT) |
| 227 | op[0] = last_op[0]; |
| 228 | |
Stefano Babic | 26b807c | 2013-09-02 15:42:32 +0200 | [diff] [blame] | 229 | if (strlen(argv[1]) > 1) { |
| 230 | op[1] = argv[1][1]; |
| 231 | if (op[1] == 'x') { |
| 232 | phydev = mdio_phydev_for_ethname(argv[2]); |
| 233 | |
| 234 | if (phydev) { |
| 235 | addrlo = phydev->addr; |
| 236 | addrhi = addrlo; |
| 237 | bus = phydev->bus; |
| 238 | extended = 1; |
| 239 | } else { |
Carlo Caione | e55047e | 2019-02-08 17:25:08 +0000 | [diff] [blame] | 240 | return CMD_RET_FAILURE; |
Stefano Babic | 26b807c | 2013-09-02 15:42:32 +0200 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | if (!phydev->drv || |
| 244 | (!phydev->drv->writeext && (op[0] == 'w')) || |
| 245 | (!phydev->drv->readext && (op[0] == 'r'))) { |
| 246 | puts("PHY does not have extended functions\n"); |
Carlo Caione | e55047e | 2019-02-08 17:25:08 +0000 | [diff] [blame] | 247 | return CMD_RET_FAILURE; |
Stefano Babic | 26b807c | 2013-09-02 15:42:32 +0200 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
Andy Fleming | 995daa0 | 2011-03-21 18:03:22 -0500 | [diff] [blame] | 252 | switch (op[0]) { |
| 253 | case 'w': |
| 254 | if (pos > 1) |
| 255 | data = simple_strtoul(argv[pos--], NULL, 16); |
| 256 | case 'r': |
| 257 | if (pos > 1) |
| 258 | if (extract_reg_range(argv[pos--], &devadlo, &devadhi, |
Mario Six | c550389 | 2018-01-15 11:08:26 +0100 | [diff] [blame] | 259 | ®lo, ®hi)) |
Carlo Caione | e55047e | 2019-02-08 17:25:08 +0000 | [diff] [blame] | 260 | return CMD_RET_FAILURE; |
Andy Fleming | 995daa0 | 2011-03-21 18:03:22 -0500 | [diff] [blame] | 261 | |
| 262 | default: |
| 263 | if (pos > 1) |
Mario Six | c550389 | 2018-01-15 11:08:26 +0100 | [diff] [blame] | 264 | if (extract_phy_range(&argv[2], pos - 1, &bus, |
| 265 | &phydev, &addrlo, &addrhi)) |
Carlo Caione | e55047e | 2019-02-08 17:25:08 +0000 | [diff] [blame] | 266 | return CMD_RET_FAILURE; |
Andy Fleming | 995daa0 | 2011-03-21 18:03:22 -0500 | [diff] [blame] | 267 | |
| 268 | break; |
| 269 | } |
| 270 | |
Simon Goldschmidt | cb58d18 | 2019-07-12 20:58:18 +0200 | [diff] [blame] | 271 | if (!bus) { |
| 272 | puts("No MDIO bus found\n"); |
| 273 | return CMD_RET_FAILURE; |
| 274 | } |
| 275 | |
Andy Fleming | 995daa0 | 2011-03-21 18:03:22 -0500 | [diff] [blame] | 276 | if (op[0] == 'l') { |
| 277 | mdio_list_devices(); |
| 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | /* Save the chosen bus */ |
| 283 | miiphy_set_current_dev(bus->name); |
| 284 | |
| 285 | switch (op[0]) { |
| 286 | case 'w': |
Carlo Caione | e55047e | 2019-02-08 17:25:08 +0000 | [diff] [blame] | 287 | mdio_write_ranges(bus, addrlo, addrhi, devadlo, devadhi, |
Stefano Babic | 26b807c | 2013-09-02 15:42:32 +0200 | [diff] [blame] | 288 | reglo, reghi, data, extended); |
Andy Fleming | 995daa0 | 2011-03-21 18:03:22 -0500 | [diff] [blame] | 289 | break; |
| 290 | |
| 291 | case 'r': |
Carlo Caione | e55047e | 2019-02-08 17:25:08 +0000 | [diff] [blame] | 292 | mdio_read_ranges(bus, addrlo, addrhi, devadlo, devadhi, |
Stefano Babic | 26b807c | 2013-09-02 15:42:32 +0200 | [diff] [blame] | 293 | reglo, reghi, extended); |
Andy Fleming | 995daa0 | 2011-03-21 18:03:22 -0500 | [diff] [blame] | 294 | break; |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | * Save the parameters for repeats. |
| 299 | */ |
| 300 | last_op[0] = op[0]; |
| 301 | last_addr_lo = addrlo; |
| 302 | last_addr_hi = addrhi; |
| 303 | last_devad_lo = devadlo; |
| 304 | last_devad_hi = devadhi; |
| 305 | last_reg_lo = reglo; |
| 306 | last_reg_hi = reghi; |
| 307 | last_data = data; |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | /***************************************************/ |
| 313 | |
| 314 | U_BOOT_CMD( |
| 315 | mdio, 6, 1, do_mdio, |
| 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" |
Stefano Babic | 26b807c | 2013-09-02 15:42:32 +0200 | [diff] [blame] | 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" |
Andy Fleming | 995daa0 | 2011-03-21 18:03:22 -0500 | [diff] [blame] | 326 | "<phydev> may be:\n" |
| 327 | " <busname> <addr>\n" |
| 328 | " <addr>\n" |
| 329 | " <eth name>\n" |
| 330 | "<addr> <devad>, and <reg> may be ranges, e.g. 1-5.4-0x1f.\n" |
| 331 | ); |