1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
22e192b24SSimon Glass /*
32e192b24SSimon Glass * (C) Copyright 2011 Freescale Semiconductor, Inc
42e192b24SSimon Glass * Andy Fleming
52e192b24SSimon Glass */
62e192b24SSimon Glass
72e192b24SSimon Glass /*
82e192b24SSimon Glass * MDIO Commands
92e192b24SSimon Glass */
102e192b24SSimon Glass
112e192b24SSimon Glass #include <common.h>
122e192b24SSimon Glass #include <command.h>
132e192b24SSimon Glass #include <miiphy.h>
142e192b24SSimon Glass #include <phy.h>
152e192b24SSimon Glass
162e192b24SSimon Glass static char last_op[2];
172e192b24SSimon Glass static uint last_data;
182e192b24SSimon Glass static uint last_addr_lo;
192e192b24SSimon Glass static uint last_addr_hi;
202e192b24SSimon Glass static uint last_devad_lo;
212e192b24SSimon Glass static uint last_devad_hi;
222e192b24SSimon Glass static uint last_reg_lo;
232e192b24SSimon Glass static uint last_reg_hi;
242e192b24SSimon Glass
extract_range(char * input,int * plo,int * phi)252e192b24SSimon Glass static int extract_range(char *input, int *plo, int *phi)
262e192b24SSimon Glass {
272e192b24SSimon Glass char *end;
2815a2acdfSMichal Simek *plo = simple_strtol(input, &end, 16);
292e192b24SSimon Glass if (end == input)
302e192b24SSimon Glass return -1;
312e192b24SSimon Glass
322e192b24SSimon Glass if ((*end == '-') && *(++end))
3315a2acdfSMichal Simek *phi = simple_strtol(end, NULL, 16);
342e192b24SSimon Glass else if (*end == '\0')
352e192b24SSimon Glass *phi = *plo;
362e192b24SSimon Glass else
372e192b24SSimon Glass return -1;
382e192b24SSimon Glass
392e192b24SSimon Glass return 0;
402e192b24SSimon Glass }
412e192b24SSimon Glass
mdio_write_ranges(struct phy_device * phydev,struct mii_dev * bus,int addrlo,int addrhi,int devadlo,int devadhi,int reglo,int reghi,unsigned short data,int extended)422e192b24SSimon Glass static int mdio_write_ranges(struct phy_device *phydev, struct mii_dev *bus,
432e192b24SSimon Glass int addrlo,
442e192b24SSimon Glass int addrhi, int devadlo, int devadhi,
452e192b24SSimon Glass int reglo, int reghi, unsigned short data,
462e192b24SSimon Glass int extended)
472e192b24SSimon Glass {
482e192b24SSimon Glass int addr, devad, reg;
492e192b24SSimon Glass int err = 0;
502e192b24SSimon Glass
512e192b24SSimon Glass for (addr = addrlo; addr <= addrhi; addr++) {
522e192b24SSimon Glass for (devad = devadlo; devad <= devadhi; devad++) {
532e192b24SSimon Glass for (reg = reglo; reg <= reghi; reg++) {
542e192b24SSimon Glass if (!extended)
552e192b24SSimon Glass err = bus->write(bus, addr, devad,
562e192b24SSimon Glass reg, data);
572e192b24SSimon Glass else
582e192b24SSimon Glass err = phydev->drv->writeext(phydev,
592e192b24SSimon Glass addr, devad, reg, data);
602e192b24SSimon Glass
612e192b24SSimon Glass if (err)
622e192b24SSimon Glass goto err_out;
632e192b24SSimon Glass }
642e192b24SSimon Glass }
652e192b24SSimon Glass }
662e192b24SSimon Glass
672e192b24SSimon Glass err_out:
682e192b24SSimon Glass return err;
692e192b24SSimon Glass }
702e192b24SSimon Glass
mdio_read_ranges(struct phy_device * phydev,struct mii_dev * bus,int addrlo,int addrhi,int devadlo,int devadhi,int reglo,int reghi,int extended)712e192b24SSimon Glass static int mdio_read_ranges(struct phy_device *phydev, struct mii_dev *bus,
722e192b24SSimon Glass int addrlo,
732e192b24SSimon Glass int addrhi, int devadlo, int devadhi,
742e192b24SSimon Glass int reglo, int reghi, int extended)
752e192b24SSimon Glass {
762e192b24SSimon Glass int addr, devad, reg;
772e192b24SSimon Glass
782e192b24SSimon Glass printf("Reading from bus %s\n", bus->name);
792e192b24SSimon Glass for (addr = addrlo; addr <= addrhi; addr++) {
8015a2acdfSMichal Simek printf("PHY at address %x:\n", addr);
812e192b24SSimon Glass
822e192b24SSimon Glass for (devad = devadlo; devad <= devadhi; devad++) {
832e192b24SSimon Glass for (reg = reglo; reg <= reghi; reg++) {
842e192b24SSimon Glass int val;
852e192b24SSimon Glass
862e192b24SSimon Glass if (!extended)
872e192b24SSimon Glass val = bus->read(bus, addr, devad, reg);
882e192b24SSimon Glass else
892e192b24SSimon Glass val = phydev->drv->readext(phydev, addr,
902e192b24SSimon Glass devad, reg);
912e192b24SSimon Glass
922e192b24SSimon Glass if (val < 0) {
932e192b24SSimon Glass printf("Error\n");
942e192b24SSimon Glass
952e192b24SSimon Glass return val;
962e192b24SSimon Glass }
972e192b24SSimon Glass
982e192b24SSimon Glass if (devad >= 0)
992e192b24SSimon Glass printf("%d.", devad);
1002e192b24SSimon Glass
1012e192b24SSimon Glass printf("%d - 0x%x\n", reg, val & 0xffff);
1022e192b24SSimon Glass }
1032e192b24SSimon Glass }
1042e192b24SSimon Glass }
1052e192b24SSimon Glass
1062e192b24SSimon Glass return 0;
1072e192b24SSimon Glass }
1082e192b24SSimon Glass
1092e192b24SSimon Glass /* The register will be in the form [a[-b].]x[-y] */
extract_reg_range(char * input,int * devadlo,int * devadhi,int * reglo,int * reghi)1102e192b24SSimon Glass static int extract_reg_range(char *input, int *devadlo, int *devadhi,
1112e192b24SSimon Glass int *reglo, int *reghi)
1122e192b24SSimon Glass {
1132e192b24SSimon Glass char *regstr;
1142e192b24SSimon Glass
1152e192b24SSimon Glass /* use strrchr to find the last string after a '.' */
1162e192b24SSimon Glass regstr = strrchr(input, '.');
1172e192b24SSimon Glass
1182e192b24SSimon Glass /* If it exists, extract the devad(s) */
1192e192b24SSimon Glass if (regstr) {
1202e192b24SSimon Glass char devadstr[32];
1212e192b24SSimon Glass
1222e192b24SSimon Glass strncpy(devadstr, input, regstr - input);
1232e192b24SSimon Glass devadstr[regstr - input] = '\0';
1242e192b24SSimon Glass
1252e192b24SSimon Glass if (extract_range(devadstr, devadlo, devadhi))
1262e192b24SSimon Glass return -1;
1272e192b24SSimon Glass
1282e192b24SSimon Glass regstr++;
1292e192b24SSimon Glass } else {
1302e192b24SSimon Glass /* Otherwise, we have no devad, and we just got regs */
1312e192b24SSimon Glass *devadlo = *devadhi = MDIO_DEVAD_NONE;
1322e192b24SSimon Glass
1332e192b24SSimon Glass regstr = input;
1342e192b24SSimon Glass }
1352e192b24SSimon Glass
1362e192b24SSimon Glass return extract_range(regstr, reglo, reghi);
1372e192b24SSimon Glass }
1382e192b24SSimon Glass
extract_phy_range(char * const argv[],int argc,struct mii_dev ** bus,struct phy_device ** phydev,int * addrlo,int * addrhi)1392e192b24SSimon Glass static int extract_phy_range(char *const argv[], int argc, struct mii_dev **bus,
1402e192b24SSimon Glass struct phy_device **phydev,
1412e192b24SSimon Glass int *addrlo, int *addrhi)
1422e192b24SSimon Glass {
1432e192b24SSimon Glass struct phy_device *dev = *phydev;
1442e192b24SSimon Glass
1452e192b24SSimon Glass if ((argc < 1) || (argc > 2))
1462e192b24SSimon Glass return -1;
1472e192b24SSimon Glass
1482e192b24SSimon Glass /* If there are two arguments, it's busname addr */
1492e192b24SSimon Glass if (argc == 2) {
1502e192b24SSimon Glass *bus = miiphy_get_dev_by_name(argv[0]);
1512e192b24SSimon Glass
1522e192b24SSimon Glass if (!*bus)
1532e192b24SSimon Glass return -1;
1542e192b24SSimon Glass
1552e192b24SSimon Glass return extract_range(argv[1], addrlo, addrhi);
1562e192b24SSimon Glass }
1572e192b24SSimon Glass
1582e192b24SSimon Glass /* It must be one argument, here */
1592e192b24SSimon Glass
1602e192b24SSimon Glass /*
1612e192b24SSimon Glass * This argument can be one of two things:
1622e192b24SSimon Glass * 1) Ethernet device name
1632e192b24SSimon Glass * 2) Just an address (use the previously-used bus)
1642e192b24SSimon Glass *
1652e192b24SSimon Glass * We check all buses for a PHY which is connected to an ethernet
1662e192b24SSimon Glass * device by the given name. If none are found, we call
1672e192b24SSimon Glass * extract_range() on the string, and see if it's an address range.
1682e192b24SSimon Glass */
1692e192b24SSimon Glass dev = mdio_phydev_for_ethname(argv[0]);
1702e192b24SSimon Glass
1712e192b24SSimon Glass if (dev) {
1722e192b24SSimon Glass *addrlo = *addrhi = dev->addr;
1732e192b24SSimon Glass *bus = dev->bus;
1742e192b24SSimon Glass
1752e192b24SSimon Glass return 0;
1762e192b24SSimon Glass }
1772e192b24SSimon Glass
1782e192b24SSimon Glass /* It's an address or nothing useful */
1792e192b24SSimon Glass return extract_range(argv[0], addrlo, addrhi);
1802e192b24SSimon Glass }
1812e192b24SSimon Glass
1822e192b24SSimon Glass /* ---------------------------------------------------------------- */
do_mdio(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])1832e192b24SSimon Glass static int do_mdio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1842e192b24SSimon Glass {
1852e192b24SSimon Glass char op[2];
1862e192b24SSimon Glass int addrlo, addrhi, reglo, reghi, devadlo, devadhi;
1872e192b24SSimon Glass unsigned short data;
1882e192b24SSimon Glass int pos = argc - 1;
1892e192b24SSimon Glass struct mii_dev *bus;
1902e192b24SSimon Glass struct phy_device *phydev = NULL;
1912e192b24SSimon Glass int extended = 0;
1922e192b24SSimon Glass
1932e192b24SSimon Glass if (argc < 2)
1942e192b24SSimon Glass return CMD_RET_USAGE;
1952e192b24SSimon Glass
1962e192b24SSimon Glass /*
1972e192b24SSimon Glass * We use the last specified parameters, unless new ones are
1982e192b24SSimon Glass * entered.
1992e192b24SSimon Glass */
2002e192b24SSimon Glass op[0] = argv[1][0];
2012e192b24SSimon Glass addrlo = last_addr_lo;
2022e192b24SSimon Glass addrhi = last_addr_hi;
2032e192b24SSimon Glass devadlo = last_devad_lo;
2042e192b24SSimon Glass devadhi = last_devad_hi;
2052e192b24SSimon Glass reglo = last_reg_lo;
2062e192b24SSimon Glass reghi = last_reg_hi;
2072e192b24SSimon Glass data = last_data;
2082e192b24SSimon Glass
2092e192b24SSimon Glass bus = mdio_get_current_dev();
2102e192b24SSimon Glass
2112e192b24SSimon Glass if (flag & CMD_FLAG_REPEAT)
2122e192b24SSimon Glass op[0] = last_op[0];
2132e192b24SSimon Glass
2142e192b24SSimon Glass if (strlen(argv[1]) > 1) {
2152e192b24SSimon Glass op[1] = argv[1][1];
2162e192b24SSimon Glass if (op[1] == 'x') {
2172e192b24SSimon Glass phydev = mdio_phydev_for_ethname(argv[2]);
2182e192b24SSimon Glass
2192e192b24SSimon Glass if (phydev) {
2202e192b24SSimon Glass addrlo = phydev->addr;
2212e192b24SSimon Glass addrhi = addrlo;
2222e192b24SSimon Glass bus = phydev->bus;
2232e192b24SSimon Glass extended = 1;
2242e192b24SSimon Glass } else {
2252e192b24SSimon Glass return -1;
2262e192b24SSimon Glass }
2272e192b24SSimon Glass
2282e192b24SSimon Glass if (!phydev->drv ||
2292e192b24SSimon Glass (!phydev->drv->writeext && (op[0] == 'w')) ||
2302e192b24SSimon Glass (!phydev->drv->readext && (op[0] == 'r'))) {
2312e192b24SSimon Glass puts("PHY does not have extended functions\n");
2322e192b24SSimon Glass return -1;
2332e192b24SSimon Glass }
2342e192b24SSimon Glass }
2352e192b24SSimon Glass }
2362e192b24SSimon Glass
2372e192b24SSimon Glass switch (op[0]) {
2382e192b24SSimon Glass case 'w':
2392e192b24SSimon Glass if (pos > 1)
2402e192b24SSimon Glass data = simple_strtoul(argv[pos--], NULL, 16);
2412e192b24SSimon Glass case 'r':
2422e192b24SSimon Glass if (pos > 1)
2432e192b24SSimon Glass if (extract_reg_range(argv[pos--], &devadlo, &devadhi,
2442e192b24SSimon Glass ®lo, ®hi))
2452e192b24SSimon Glass return -1;
2462e192b24SSimon Glass
2472e192b24SSimon Glass default:
2482e192b24SSimon Glass if (pos > 1)
249c5503898SMario Six if (extract_phy_range(&argv[2], pos - 1, &bus,
2502e192b24SSimon Glass &phydev, &addrlo, &addrhi))
2512e192b24SSimon Glass return -1;
2522e192b24SSimon Glass
2532e192b24SSimon Glass break;
2542e192b24SSimon Glass }
2552e192b24SSimon Glass
2562e192b24SSimon Glass if (op[0] == 'l') {
2572e192b24SSimon Glass mdio_list_devices();
2582e192b24SSimon Glass
2592e192b24SSimon Glass return 0;
2602e192b24SSimon Glass }
2612e192b24SSimon Glass
2622e192b24SSimon Glass /* Save the chosen bus */
2632e192b24SSimon Glass miiphy_set_current_dev(bus->name);
2642e192b24SSimon Glass
2652e192b24SSimon Glass switch (op[0]) {
2662e192b24SSimon Glass case 'w':
2672e192b24SSimon Glass mdio_write_ranges(phydev, bus, addrlo, addrhi, devadlo, devadhi,
2682e192b24SSimon Glass reglo, reghi, data, extended);
2692e192b24SSimon Glass break;
2702e192b24SSimon Glass
2712e192b24SSimon Glass case 'r':
2722e192b24SSimon Glass mdio_read_ranges(phydev, bus, addrlo, addrhi, devadlo, devadhi,
2732e192b24SSimon Glass reglo, reghi, extended);
2742e192b24SSimon Glass break;
2752e192b24SSimon Glass }
2762e192b24SSimon Glass
2772e192b24SSimon Glass /*
2782e192b24SSimon Glass * Save the parameters for repeats.
2792e192b24SSimon Glass */
2802e192b24SSimon Glass last_op[0] = op[0];
2812e192b24SSimon Glass last_addr_lo = addrlo;
2822e192b24SSimon Glass last_addr_hi = addrhi;
2832e192b24SSimon Glass last_devad_lo = devadlo;
2842e192b24SSimon Glass last_devad_hi = devadhi;
2852e192b24SSimon Glass last_reg_lo = reglo;
2862e192b24SSimon Glass last_reg_hi = reghi;
2872e192b24SSimon Glass last_data = data;
2882e192b24SSimon Glass
2892e192b24SSimon Glass return 0;
2902e192b24SSimon Glass }
2912e192b24SSimon Glass
2922e192b24SSimon Glass /***************************************************/
2932e192b24SSimon Glass
2942e192b24SSimon Glass U_BOOT_CMD(
2952e192b24SSimon Glass mdio, 6, 1, do_mdio,
2962e192b24SSimon Glass "MDIO utility commands",
2972e192b24SSimon Glass "list - List MDIO buses\n"
2982e192b24SSimon Glass "mdio read <phydev> [<devad>.]<reg> - "
2992e192b24SSimon Glass "read PHY's register at <devad>.<reg>\n"
3002e192b24SSimon Glass "mdio write <phydev> [<devad>.]<reg> <data> - "
3012e192b24SSimon Glass "write PHY's register at <devad>.<reg>\n"
3022e192b24SSimon Glass "mdio rx <phydev> [<devad>.]<reg> - "
3032e192b24SSimon Glass "read PHY's extended register at <devad>.<reg>\n"
3042e192b24SSimon Glass "mdio wx <phydev> [<devad>.]<reg> <data> - "
3052e192b24SSimon Glass "write PHY's extended register at <devad>.<reg>\n"
3062e192b24SSimon Glass "<phydev> may be:\n"
3072e192b24SSimon Glass " <busname> <addr>\n"
3082e192b24SSimon Glass " <addr>\n"
3092e192b24SSimon Glass " <eth name>\n"
3102e192b24SSimon Glass "<addr> <devad>, and <reg> may be ranges, e.g. 1-5.4-0x1f.\n"
3112e192b24SSimon Glass );
312