cmd: ubifs: Remove select for non-existent option
[platform/kernel/u-boot.git] / cmd / mdio.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2011 Freescale Semiconductor, Inc
4  * Andy Fleming
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
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;
28         *plo = simple_strtol(input, &end, 16);
29         if (end == input)
30                 return -1;
31
32         if ((*end == '-') && *(++end))
33                 *phi = simple_strtol(end, NULL, 16);
34         else if (*end == '\0')
35                 *phi = *plo;
36         else
37                 return -1;
38
39         return 0;
40 }
41
42 static int mdio_write_ranges(struct mii_dev *bus,
43                              int addrlo,
44                              int addrhi, int devadlo, int devadhi,
45                              int reglo, int reghi, unsigned short data,
46                              int extended)
47 {
48         struct phy_device *phydev;
49         int addr, devad, reg;
50         int err = 0;
51
52         for (addr = addrlo; addr <= addrhi; addr++) {
53                 phydev = bus->phymap[addr];
54
55                 for (devad = devadlo; devad <= devadhi; devad++) {
56                         for (reg = reglo; reg <= reghi; reg++) {
57                                 if (!extended)
58                                         err = phy_write_mmd(phydev, devad,
59                                                             reg, data);
60                                 else
61                                         err = phydev->drv->writeext(phydev,
62                                                         addr, devad, reg, data);
63
64                                 if (err)
65                                         goto err_out;
66                         }
67                 }
68         }
69
70 err_out:
71         return err;
72 }
73
74 static int mdio_read_ranges(struct mii_dev *bus,
75                             int addrlo,
76                             int addrhi, int devadlo, int devadhi,
77                             int reglo, int reghi, int extended)
78 {
79         int addr, devad, reg;
80         struct phy_device *phydev;
81
82         printf("Reading from bus %s\n", bus->name);
83         for (addr = addrlo; addr <= addrhi; addr++) {
84                 phydev = bus->phymap[addr];
85                 printf("PHY at address %x:\n", addr);
86
87                 for (devad = devadlo; devad <= devadhi; devad++) {
88                         for (reg = reglo; reg <= reghi; reg++) {
89                                 int val;
90
91                                 if (!extended)
92                                         val = phy_read_mmd(phydev, devad, reg);
93                                 else
94                                         val = phydev->drv->readext(phydev, addr,
95                                                 devad, reg);
96
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] */
115 static int extract_reg_range(char *input, int *devadlo, int *devadhi,
116                              int *reglo, int *reghi)
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
144 static int extract_phy_range(char *const argv[], int argc, struct mii_dev **bus,
145                              struct phy_device **phydev,
146                              int *addrlo, int *addrhi)
147 {
148         struct phy_device *dev = *phydev;
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          */
174         dev = mdio_phydev_for_ethname(argv[0]);
175
176         if (dev) {
177                 *addrlo = *addrhi = dev->addr;
178                 *bus = dev->bus;
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;
195         struct phy_device *phydev = NULL;
196         int extended = 0;
197
198         if (argc < 2)
199                 return CMD_RET_USAGE;
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
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 {
230                                 return CMD_RET_FAILURE;
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");
237                                 return CMD_RET_FAILURE;
238                         }
239                 }
240         }
241
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,
249                                               &reglo, &reghi))
250                                 return CMD_RET_FAILURE;
251
252         default:
253                 if (pos > 1)
254                         if (extract_phy_range(&argv[2], pos - 1, &bus,
255                                               &phydev, &addrlo, &addrhi))
256                                 return CMD_RET_FAILURE;
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':
272                 mdio_write_ranges(bus, addrlo, addrhi, devadlo, devadhi,
273                                   reglo, reghi, data, extended);
274                 break;
275
276         case 'r':
277                 mdio_read_ranges(bus, addrlo, addrhi, devadlo, devadhi,
278                                  reglo, reghi, extended);
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"
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"
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 );