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