common: Drop linux/delay.h from common header
[oweals/u-boot.git] / drivers / net / phy / b53.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2017
4  * Broadcom
5  * Florian Fainelli <f.fainelli@gmail.com>
6  */
7
8 /*
9  * PHY driver for Broadcom BCM53xx (roboswitch) Ethernet switches.
10  *
11  * This driver configures the b53 for basic use as a PHY. The switch supports
12  * vendor tags and VLAN configuration that can affect the switching decisions.
13  * This driver uses a simple configuration in which all ports are only allowed
14  * to send frames to the CPU port and receive frames from the CPU port this
15  * providing port isolation (no cross talk).
16  *
17  * The configuration determines which PHY ports to activate using the
18  * CONFIG_B53_PHY_PORTS bitmask. Set bit N will active port N and so on.
19  *
20  * This driver was written primarily for the Lamobo R1 platform using a BCM53152
21  * switch but the BCM53xx being largely register compatible, extending it to
22  * cover other switches would be trivial.
23  */
24
25 #include <common.h>
26 #include <command.h>
27 #include <linux/delay.h>
28
29 #include <errno.h>
30 #include <malloc.h>
31 #include <miiphy.h>
32 #include <netdev.h>
33
34 /* Pseudo-PHY address (non configurable) to access internal registers */
35 #define BRCM_PSEUDO_PHY_ADDR            30
36
37 /* Maximum number of ports possible */
38 #define B53_N_PORTS                     9
39
40 #define B53_CTRL_PAGE                   0x00 /* Control */
41 #define B53_MGMT_PAGE                   0x02 /* Management Mode */
42 /* Port VLAN Page */
43 #define B53_PVLAN_PAGE                  0x31
44
45 /* Control Page registers */
46 #define B53_PORT_CTRL(i)                (0x00 + (i))
47 #define   PORT_CTRL_RX_DISABLE          BIT(0)
48 #define   PORT_CTRL_TX_DISABLE          BIT(1)
49 #define   PORT_CTRL_RX_BCST_EN          BIT(2) /* Broadcast RX (P8 only) */
50 #define   PORT_CTRL_RX_MCST_EN          BIT(3) /* Multicast RX (P8 only) */
51 #define   PORT_CTRL_RX_UCST_EN          BIT(4) /* Unicast RX (P8 only) */
52
53 /* Switch Mode Control Register (8 bit) */
54 #define B53_SWITCH_MODE                 0x0b
55 #define   SM_SW_FWD_MODE                BIT(0)  /* 1 = Managed Mode */
56 #define   SM_SW_FWD_EN                  BIT(1)  /* Forwarding Enable */
57
58 /* IMP Port state override register (8 bit) */
59 #define B53_PORT_OVERRIDE_CTRL          0x0e
60 #define   PORT_OVERRIDE_LINK            BIT(0)
61 #define   PORT_OVERRIDE_FULL_DUPLEX     BIT(1) /* 0 = Half Duplex */
62 #define   PORT_OVERRIDE_SPEED_S         2
63 #define   PORT_OVERRIDE_SPEED_10M       (0 << PORT_OVERRIDE_SPEED_S)
64 #define   PORT_OVERRIDE_SPEED_100M      (1 << PORT_OVERRIDE_SPEED_S)
65 #define   PORT_OVERRIDE_SPEED_1000M     (2 << PORT_OVERRIDE_SPEED_S)
66 /* BCM5325 only */
67 #define   PORT_OVERRIDE_RV_MII_25       BIT(4)
68 #define   PORT_OVERRIDE_RX_FLOW         BIT(4)
69 #define   PORT_OVERRIDE_TX_FLOW         BIT(5)
70 /* BCM5301X only, requires setting 1000M */
71 #define   PORT_OVERRIDE_SPEED_2000M     BIT(6)
72 #define   PORT_OVERRIDE_EN              BIT(7) /* Use the register contents */
73
74 #define B53_RGMII_CTRL_IMP              0x60
75 #define   RGMII_CTRL_ENABLE_GMII        BIT(7)
76 #define   RGMII_CTRL_TIMING_SEL         BIT(2)
77 #define   RGMII_CTRL_DLL_RXC            BIT(1)
78 #define   RGMII_CTRL_DLL_TXC            BIT(0)
79
80 /* Switch control (8 bit) */
81 #define B53_SWITCH_CTRL                 0x22
82 #define  B53_MII_DUMB_FWDG_EN           BIT(6)
83
84 /* Software reset register (8 bit) */
85 #define B53_SOFTRESET                   0x79
86 #define   SW_RST                        BIT(7)
87 #define   EN_CH_RST                     BIT(6)
88 #define   EN_SW_RST                     BIT(4)
89
90 /* Fast Aging Control register (8 bit) */
91 #define B53_FAST_AGE_CTRL               0x88
92 #define   FAST_AGE_STATIC               BIT(0)
93 #define   FAST_AGE_DYNAMIC              BIT(1)
94 #define   FAST_AGE_PORT                 BIT(2)
95 #define   FAST_AGE_VLAN                 BIT(3)
96 #define   FAST_AGE_STP                  BIT(4)
97 #define   FAST_AGE_MC                   BIT(5)
98 #define   FAST_AGE_DONE                 BIT(7)
99
100 /* Port VLAN mask (16 bit) IMP port is always 8, also on 5325 & co */
101 #define B53_PVLAN_PORT_MASK(i)          ((i) * 2)
102
103 /* MII registers */
104 #define REG_MII_PAGE    0x10    /* MII Page register */
105 #define REG_MII_ADDR    0x11    /* MII Address register */
106 #define REG_MII_DATA0   0x18    /* MII Data register 0 */
107 #define REG_MII_DATA1   0x19    /* MII Data register 1 */
108 #define REG_MII_DATA2   0x1a    /* MII Data register 2 */
109 #define REG_MII_DATA3   0x1b    /* MII Data register 3 */
110
111 #define REG_MII_PAGE_ENABLE     BIT(0)
112 #define REG_MII_ADDR_WRITE      BIT(0)
113 #define REG_MII_ADDR_READ       BIT(1)
114
115 struct b53_device {
116         struct mii_dev  *bus;
117         unsigned int cpu_port;
118 };
119
120 static int b53_mdio_op(struct mii_dev *bus, u8 page, u8 reg, u16 op)
121 {
122         int ret;
123         int i;
124         u16 v;
125
126         /* set page number */
127         v = (page << 8) | REG_MII_PAGE_ENABLE;
128         ret = bus->write(bus, BRCM_PSEUDO_PHY_ADDR, MDIO_DEVAD_NONE,
129                          REG_MII_PAGE, v);
130         if (ret)
131                 return ret;
132
133         /* set register address */
134         v = (reg << 8) | op;
135         ret = bus->write(bus, BRCM_PSEUDO_PHY_ADDR, MDIO_DEVAD_NONE,
136                          REG_MII_ADDR, v);
137         if (ret)
138                 return ret;
139
140         /* check if operation completed */
141         for (i = 0; i < 5; ++i) {
142                 v = bus->read(bus, BRCM_PSEUDO_PHY_ADDR, MDIO_DEVAD_NONE,
143                               REG_MII_ADDR);
144                 if (!(v & (REG_MII_ADDR_WRITE | REG_MII_ADDR_READ)))
145                         break;
146
147                 udelay(100);
148         }
149
150         if (i == 5)
151                 return -EIO;
152
153         return 0;
154 }
155
156 static int b53_mdio_read8(struct mii_dev *bus, u8 page, u8 reg, u8 *val)
157 {
158         int ret;
159
160         ret = b53_mdio_op(bus, page, reg, REG_MII_ADDR_READ);
161         if (ret)
162                 return ret;
163
164         *val = bus->read(bus, BRCM_PSEUDO_PHY_ADDR, MDIO_DEVAD_NONE,
165                          REG_MII_DATA0) & 0xff;
166
167         return 0;
168 }
169
170 static int b53_mdio_read16(struct mii_dev *bus, u8 page, u8 reg, u16 *val)
171 {
172         int ret;
173
174         ret = b53_mdio_op(bus, page, reg, REG_MII_ADDR_READ);
175         if (ret)
176                 return ret;
177
178         *val = bus->read(bus, BRCM_PSEUDO_PHY_ADDR, MDIO_DEVAD_NONE,
179                          REG_MII_DATA0);
180
181         return 0;
182 }
183
184 static int b53_mdio_read32(struct mii_dev *bus, u8 page, u8 reg, u32 *val)
185 {
186         int ret;
187
188         ret = b53_mdio_op(bus, page, reg, REG_MII_ADDR_READ);
189         if (ret)
190                 return ret;
191
192         *val = bus->read(bus, BRCM_PSEUDO_PHY_ADDR, MDIO_DEVAD_NONE,
193                          REG_MII_DATA0);
194         *val |= bus->read(bus, BRCM_PSEUDO_PHY_ADDR, MDIO_DEVAD_NONE,
195                           REG_MII_DATA1) << 16;
196
197         return 0;
198 }
199
200 static int b53_mdio_read48(struct mii_dev *bus, u8 page, u8 reg, u64 *val)
201 {
202         u64 temp = 0;
203         int i;
204         int ret;
205
206         ret = b53_mdio_op(bus, page, reg, REG_MII_ADDR_READ);
207         if (ret)
208                 return ret;
209
210         for (i = 2; i >= 0; i--) {
211                 temp <<= 16;
212                 temp |= bus->read(bus, BRCM_PSEUDO_PHY_ADDR, MDIO_DEVAD_NONE,
213                                   REG_MII_DATA0 + i);
214         }
215
216         *val = temp;
217
218         return 0;
219 }
220
221 static int b53_mdio_read64(struct mii_dev *bus, u8 page, u8 reg, u64 *val)
222 {
223         u64 temp = 0;
224         int i;
225         int ret;
226
227         ret = b53_mdio_op(bus, page, reg, REG_MII_ADDR_READ);
228         if (ret)
229                 return ret;
230
231         for (i = 3; i >= 0; i--) {
232                 temp <<= 16;
233                 temp |= bus->read(bus, BRCM_PSEUDO_PHY_ADDR, MDIO_DEVAD_NONE,
234                                   REG_MII_DATA0 + i);
235         }
236
237         *val = temp;
238
239         return 0;
240 }
241
242 static int b53_mdio_write8(struct mii_dev *bus, u8 page, u8 reg, u8 value)
243 {
244         int ret;
245
246         ret = bus->write(bus, BRCM_PSEUDO_PHY_ADDR, MDIO_DEVAD_NONE,
247                          REG_MII_DATA0, value);
248         if (ret)
249                 return ret;
250
251         return b53_mdio_op(bus, page, reg, REG_MII_ADDR_WRITE);
252 }
253
254 static int b53_mdio_write16(struct mii_dev *bus, u8 page, u8 reg,
255                             u16 value)
256 {
257         int ret;
258
259         ret = bus->write(bus, BRCM_PSEUDO_PHY_ADDR, MDIO_DEVAD_NONE,
260                          REG_MII_DATA0, value);
261         if (ret)
262                 return ret;
263
264         return b53_mdio_op(bus, page, reg, REG_MII_ADDR_WRITE);
265 }
266
267 static int b53_mdio_write32(struct mii_dev *bus, u8 page, u8 reg,
268                             u32 value)
269 {
270         unsigned int i;
271         u32 temp = value;
272
273         for (i = 0; i < 2; i++) {
274                 int ret = bus->write(bus, BRCM_PSEUDO_PHY_ADDR,
275                                      MDIO_DEVAD_NONE,
276                                      REG_MII_DATA0 + i, temp & 0xffff);
277                 if (ret)
278                         return ret;
279                 temp >>= 16;
280         }
281
282         return b53_mdio_op(bus, page, reg, REG_MII_ADDR_WRITE);
283 }
284
285 static int b53_mdio_write48(struct mii_dev *bus, u8 page, u8 reg,
286                             u64 value)
287 {
288         unsigned int i;
289         u64 temp = value;
290
291         for (i = 0; i < 3; i++) {
292                 int ret = bus->write(bus, BRCM_PSEUDO_PHY_ADDR,
293                                      MDIO_DEVAD_NONE,
294                                      REG_MII_DATA0 + i, temp & 0xffff);
295                 if (ret)
296                         return ret;
297                 temp >>= 16;
298         }
299
300         return b53_mdio_op(bus, page, reg, REG_MII_ADDR_WRITE);
301 }
302
303 static int b53_mdio_write64(struct mii_dev *bus, u8 page, u8 reg,
304                             u64 value)
305 {
306         unsigned int i;
307         u64 temp = value;
308
309         for (i = 0; i < 4; i++) {
310                 int ret = bus->write(bus, BRCM_PSEUDO_PHY_ADDR,
311                                      MDIO_DEVAD_NONE,
312                                      REG_MII_DATA0 + i, temp & 0xffff);
313                 if (ret)
314                         return ret;
315                 temp >>= 16;
316         }
317
318         return b53_mdio_op(bus, page, reg, REG_MII_ADDR_WRITE);
319 }
320
321 static inline int b53_read8(struct b53_device *dev, u8 page,
322                             u8 reg, u8 *value)
323 {
324         return b53_mdio_read8(dev->bus, page, reg, value);
325 }
326
327 static inline int b53_read16(struct b53_device *dev, u8 page,
328                              u8 reg, u16 *value)
329 {
330         return b53_mdio_read16(dev->bus, page, reg, value);
331 }
332
333 static inline int b53_read32(struct b53_device *dev, u8 page,
334                              u8 reg, u32 *value)
335 {
336         return b53_mdio_read32(dev->bus, page, reg, value);
337 }
338
339 static inline int b53_read48(struct b53_device *dev, u8 page,
340                              u8 reg, u64 *value)
341 {
342         return b53_mdio_read48(dev->bus, page, reg, value);
343 }
344
345 static inline int b53_read64(struct b53_device *dev, u8 page,
346                              u8 reg, u64 *value)
347 {
348         return b53_mdio_read64(dev->bus, page, reg, value);
349 }
350
351 static inline int b53_write8(struct b53_device *dev, u8 page,
352                              u8 reg, u8 value)
353 {
354         return b53_mdio_write8(dev->bus, page, reg, value);
355 }
356
357 static inline int b53_write16(struct b53_device *dev, u8 page,
358                               u8 reg, u16 value)
359 {
360         return b53_mdio_write16(dev->bus, page, reg, value);
361 }
362
363 static inline int b53_write32(struct b53_device *dev, u8 page,
364                               u8 reg, u32 value)
365 {
366         return b53_mdio_write32(dev->bus, page, reg, value);
367 }
368
369 static inline int b53_write48(struct b53_device *dev, u8 page,
370                               u8 reg, u64 value)
371 {
372         return b53_mdio_write48(dev->bus, page, reg, value);
373 }
374
375 static inline int b53_write64(struct b53_device *dev, u8 page,
376                               u8 reg, u64 value)
377 {
378         return b53_mdio_write64(dev->bus, page, reg, value);
379 }
380
381 static int b53_flush_arl(struct b53_device *dev, u8 mask)
382 {
383         unsigned int i;
384
385         b53_write8(dev, B53_CTRL_PAGE, B53_FAST_AGE_CTRL,
386                    FAST_AGE_DONE | FAST_AGE_DYNAMIC | mask);
387
388         for (i = 0; i < 10; i++) {
389                 u8 fast_age_ctrl;
390
391                 b53_read8(dev, B53_CTRL_PAGE, B53_FAST_AGE_CTRL,
392                           &fast_age_ctrl);
393
394                 if (!(fast_age_ctrl & FAST_AGE_DONE))
395                         goto out;
396
397                 mdelay(1);
398         }
399
400         return -ETIMEDOUT;
401 out:
402         /* Only age dynamic entries (default behavior) */
403         b53_write8(dev, B53_CTRL_PAGE, B53_FAST_AGE_CTRL, FAST_AGE_DYNAMIC);
404         return 0;
405 }
406
407 static int b53_switch_reset(struct phy_device *phydev)
408 {
409         struct b53_device *dev = phydev->priv;
410         unsigned int timeout = 1000;
411         u8 mgmt;
412         u8 reg;
413
414         b53_read8(dev, B53_CTRL_PAGE, B53_SOFTRESET, &reg);
415         reg |= SW_RST | EN_SW_RST | EN_CH_RST;
416         b53_write8(dev, B53_CTRL_PAGE, B53_SOFTRESET, reg);
417
418         do {
419                 b53_read8(dev, B53_CTRL_PAGE, B53_SOFTRESET, &reg);
420                 if (!(reg & SW_RST))
421                         break;
422
423                 mdelay(1);
424         } while (timeout-- > 0);
425
426         if (timeout == 0)
427                 return -ETIMEDOUT;
428
429         b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
430
431         if (!(mgmt & SM_SW_FWD_EN)) {
432                 mgmt &= ~SM_SW_FWD_MODE;
433                 mgmt |= SM_SW_FWD_EN;
434
435                 b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, mgmt);
436                 b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
437
438                 if (!(mgmt & SM_SW_FWD_EN)) {
439                         printf("Failed to enable switch!\n");
440                         return -EINVAL;
441                 }
442         }
443
444         /* Include IMP port in dumb forwarding mode when no tagging protocol
445          * is configured
446          */
447         b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_CTRL, &mgmt);
448         mgmt |= B53_MII_DUMB_FWDG_EN;
449         b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_CTRL, mgmt);
450
451         return b53_flush_arl(dev, FAST_AGE_STATIC);
452 }
453
454 static void b53_enable_cpu_port(struct phy_device *phydev)
455 {
456         struct b53_device *dev = phydev->priv;
457         u8 port_ctrl;
458
459         port_ctrl = PORT_CTRL_RX_BCST_EN |
460                     PORT_CTRL_RX_MCST_EN |
461                     PORT_CTRL_RX_UCST_EN;
462         b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(dev->cpu_port), port_ctrl);
463
464         port_ctrl = PORT_OVERRIDE_EN | PORT_OVERRIDE_LINK |
465                     PORT_OVERRIDE_FULL_DUPLEX | PORT_OVERRIDE_SPEED_1000M;
466         b53_write8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL, port_ctrl);
467
468         b53_read8(dev, B53_CTRL_PAGE, B53_RGMII_CTRL_IMP, &port_ctrl);
469 }
470
471 static void b53_imp_vlan_setup(struct b53_device *dev, int cpu_port)
472 {
473         unsigned int port;
474         u16 pvlan;
475
476         /* Enable the IMP port to be in the same VLAN as the other ports
477          * on a per-port basis such that we only have Port i and IMP in
478          * the same VLAN.
479          */
480         for (port = 0; port < B53_N_PORTS; port++) {
481                 if (!((1 << port) & CONFIG_B53_PHY_PORTS))
482                         continue;
483
484                 b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port),
485                            &pvlan);
486                 pvlan |= BIT(cpu_port);
487                 b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port),
488                             pvlan);
489         }
490 }
491
492 static int b53_port_enable(struct phy_device *phydev, unsigned int port)
493 {
494         struct b53_device *dev = phydev->priv;
495         unsigned int cpu_port = dev->cpu_port;
496         u16 pvlan;
497
498         /* Clear the Rx and Tx disable bits and set to no spanning tree */
499         b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), 0);
500
501         /* Set this port, and only this one to be in the default VLAN */
502         b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), &pvlan);
503         pvlan &= ~0x1ff;
504         pvlan |= BIT(port);
505         b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), pvlan);
506
507         b53_imp_vlan_setup(dev, cpu_port);
508
509         return 0;
510 }
511
512 static int b53_switch_init(struct phy_device *phydev)
513 {
514         static int init;
515         int ret;
516
517         if (init)
518                 return 0;
519
520         ret = b53_switch_reset(phydev);
521         if (ret < 0)
522                 return ret;
523
524         b53_enable_cpu_port(phydev);
525
526         init = 1;
527
528         return 0;
529 }
530
531 static int b53_probe(struct phy_device *phydev)
532 {
533         struct b53_device *dev;
534         int ret;
535
536         dev = malloc(sizeof(*dev));
537         if (!dev)
538                 return -ENOMEM;
539
540         memset(dev, 0, sizeof(*dev));
541
542         phydev->priv = dev;
543         dev->bus = phydev->bus;
544         dev->cpu_port = CONFIG_B53_CPU_PORT;
545
546         ret = b53_switch_reset(phydev);
547         if (ret < 0)
548                 return ret;
549
550         return 0;
551 }
552
553 static int b53_phy_config(struct phy_device *phydev)
554 {
555         unsigned int port;
556         int res;
557
558         res = b53_switch_init(phydev);
559         if (res < 0)
560                 return res;
561
562         for (port = 0; port < B53_N_PORTS; port++) {
563                 if (!((1 << port) & CONFIG_B53_PHY_PORTS))
564                         continue;
565
566                 res = b53_port_enable(phydev, port);
567                 if (res < 0) {
568                         printf("Error enabling port %i\n", port);
569                         continue;
570                 }
571
572                 res = genphy_config_aneg(phydev);
573                 if (res < 0) {
574                         printf("Error setting PHY %i autoneg\n", port);
575                         continue;
576                 }
577
578                 res = 0;
579         }
580
581         return res;
582 }
583
584 static int b53_phy_startup(struct phy_device *phydev)
585 {
586         unsigned int port;
587         int res;
588
589         for (port = 0; port < B53_N_PORTS; port++) {
590                 if (!((1 << port) & CONFIG_B53_PHY_PORTS))
591                         continue;
592
593                 phydev->addr = port;
594
595                 res = genphy_startup(phydev);
596                 if (res < 0)
597                         continue;
598                 else
599                         break;
600         }
601
602         /* Since we are connected directly to the switch, hardcode the link
603          * parameters to match those of the CPU port configured in
604          * b53_enable_cpu_port, we cannot be dependent on the user-facing port
605          * settings (e.g: 100Mbits/sec would not work here)
606          */
607         phydev->speed = 1000;
608         phydev->duplex = 1;
609         phydev->link = 1;
610
611         return 0;
612 }
613
614 static struct phy_driver b53_driver = {
615         .name = "Broadcom BCM53125",
616         .uid = 0x03625c00,
617         .mask = 0xfffffc00,
618         .features = PHY_GBIT_FEATURES,
619         .probe = b53_probe,
620         .config = b53_phy_config,
621         .startup = b53_phy_startup,
622         .shutdown = &genphy_shutdown,
623 };
624
625 int phy_b53_init(void)
626 {
627         phy_register(&b53_driver);
628
629         return 0;
630 }
631
632 int do_b53_reg_read(const char *name, int argc, char *const argv[])
633 {
634         u8 page, offset, width;
635         struct mii_dev *bus;
636         int ret = -EINVAL;
637         u64 value64 = 0;
638         u32 value32 = 0;
639         u16 value16 = 0;
640         u8 value8 = 0;
641
642         bus = miiphy_get_dev_by_name(name);
643         if (!bus) {
644                 printf("unable to find MDIO bus: %s\n", name);
645                 return ret;
646         }
647
648         page = simple_strtoul(argv[1], NULL, 16);
649         offset = simple_strtoul(argv[2], NULL, 16);
650         width = simple_strtoul(argv[3], NULL, 10);
651
652         switch (width) {
653         case 8:
654                 ret = b53_mdio_read8(bus, page, offset, &value8);
655                 printf("page=0x%02x, offset=0x%02x, value=0x%02x\n",
656                        page, offset, value8);
657                 break;
658         case 16:
659                 ret = b53_mdio_read16(bus, page, offset, &value16);
660                 printf("page=0x%02x, offset=0x%02x, value=0x%04x\n",
661                        page, offset, value16);
662                 break;
663         case 32:
664                 ret = b53_mdio_read32(bus, page, offset, &value32);
665                 printf("page=0x%02x, offset=0x%02x, value=0x%08x\n",
666                        page, offset, value32);
667                 break;
668         case 48:
669                 ret = b53_mdio_read48(bus, page, offset, &value64);
670                 printf("page=0x%02x, offset=0x%02x, value=0x%012llx\n",
671                        page, offset, value64);
672                 break;
673         case 64:
674                 ret = b53_mdio_read48(bus, page, offset, &value64);
675                 printf("page=0x%02x, offset=0x%02x, value=0x%016llx\n",
676                        page, offset, value64);
677                 break;
678         default:
679                 printf("Unsupported width: %d\n", width);
680                 break;
681         }
682
683         return ret;
684 }
685
686 int do_b53_reg_write(const char *name, int argc, char *const argv[])
687 {
688         u8 page, offset, width;
689         struct mii_dev *bus;
690         int ret = -EINVAL;
691         u64 value64 = 0;
692         u32 value = 0;
693
694         bus = miiphy_get_dev_by_name(name);
695         if (!bus) {
696                 printf("unable to find MDIO bus: %s\n", name);
697                 return ret;
698         }
699
700         page = simple_strtoul(argv[1], NULL, 16);
701         offset = simple_strtoul(argv[2], NULL, 16);
702         width = simple_strtoul(argv[3], NULL, 10);
703         if (width == 48 || width == 64)
704                 value64 = simple_strtoull(argv[4], NULL, 16);
705         else
706                 value = simple_strtoul(argv[4], NULL, 16);
707
708         switch (width) {
709         case 8:
710                 ret = b53_mdio_write8(bus, page, offset, value & 0xff);
711                 break;
712         case 16:
713                 ret = b53_mdio_write16(bus, page, offset, value);
714                 break;
715         case 32:
716                 ret = b53_mdio_write32(bus, page, offset, value);
717                 break;
718         case 48:
719                 ret = b53_mdio_write48(bus, page, offset, value64);
720                 break;
721         case 64:
722                 ret = b53_mdio_write64(bus, page, offset, value64);
723                 break;
724         default:
725                 printf("Unsupported width: %d\n", width);
726                 break;
727         }
728
729         return ret;
730 }
731
732 int do_b53_reg(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
733 {
734         const char *cmd, *mdioname;
735         int ret = 0;
736
737         if (argc < 2)
738                 return cmd_usage(cmdtp);
739
740         cmd = argv[1];
741         --argc;
742         ++argv;
743
744         if (!strcmp(cmd, "write")) {
745                 if (argc < 4)
746                         return cmd_usage(cmdtp);
747                 mdioname = argv[1];
748                 --argc;
749                 ++argv;
750                 ret = do_b53_reg_write(mdioname, argc, argv);
751         } else if (!strcmp(cmd, "read")) {
752                 if (argc < 5)
753                         return cmd_usage(cmdtp);
754                 mdioname = argv[1];
755                 --argc;
756                 ++argv;
757                 ret = do_b53_reg_read(mdioname, argc, argv);
758         } else {
759                 return cmd_usage(cmdtp);
760         }
761
762         return ret;
763 }
764
765 U_BOOT_CMD(b53_reg, 7, 1, do_b53_reg,
766            "Broadcom B53 switch register access",
767            "write mdioname page (hex) offset (hex) width (dec) value (hex)\n"
768            "read mdioname page (hex) offset (hex) width (dec)\n"
769           );