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