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