1 // SPDX-License-Identifier: GPL-2.0+
3 * sni_ave.c - Socionext UniPhier AVE ethernet driver
4 * Copyright 2016-2018 Socionext inc.
10 #include <fdt_support.h>
18 #include <asm/cache.h>
19 #include <asm/global_data.h>
20 #include <dm/device_compat.h>
21 #include <linux/bitops.h>
22 #include <linux/delay.h>
23 #include <linux/err.h>
25 #include <linux/iopoll.h>
26 #include <linux/printk.h>
28 #define AVE_GRST_DELAY_MSEC 40
29 #define AVE_MIN_XMITSIZE 60
30 #define AVE_SEND_TIMEOUT_COUNT 1000
31 #define AVE_MDIO_TIMEOUT_USEC 10000
32 #define AVE_HALT_TIMEOUT_USEC 10000
34 /* General Register Group */
35 #define AVE_IDR 0x000 /* ID */
36 #define AVE_VR 0x004 /* Version */
37 #define AVE_GRR 0x008 /* Global Reset */
38 #define AVE_CFGR 0x00c /* Configuration */
40 /* Interrupt Register Group */
41 #define AVE_GIMR 0x100 /* Global Interrupt Mask */
42 #define AVE_GISR 0x104 /* Global Interrupt Status */
44 /* MAC Register Group */
45 #define AVE_TXCR 0x200 /* TX Setup */
46 #define AVE_RXCR 0x204 /* RX Setup */
47 #define AVE_RXMAC1R 0x208 /* MAC address (lower) */
48 #define AVE_RXMAC2R 0x20c /* MAC address (upper) */
49 #define AVE_MDIOCTR 0x214 /* MDIO Control */
50 #define AVE_MDIOAR 0x218 /* MDIO Address */
51 #define AVE_MDIOWDR 0x21c /* MDIO Data */
52 #define AVE_MDIOSR 0x220 /* MDIO Status */
53 #define AVE_MDIORDR 0x224 /* MDIO Rd Data */
55 /* Descriptor Control Register Group */
56 #define AVE_DESCC 0x300 /* Descriptor Control */
57 #define AVE_TXDC 0x304 /* TX Descriptor Configuration */
58 #define AVE_RXDC 0x308 /* RX Descriptor Ring0 Configuration */
59 #define AVE_IIRQC 0x34c /* Interval IRQ Control */
61 /* 64bit descriptor memory */
62 #define AVE_DESC_SIZE_64 12 /* Descriptor Size */
63 #define AVE_TXDM_64 0x1000 /* Tx Descriptor Memory */
64 #define AVE_RXDM_64 0x1c00 /* Rx Descriptor Memory */
66 /* 32bit descriptor memory */
67 #define AVE_DESC_SIZE_32 8 /* Descriptor Size */
68 #define AVE_TXDM_32 0x1000 /* Tx Descriptor Memory */
69 #define AVE_RXDM_32 0x1800 /* Rx Descriptor Memory */
71 /* RMII Bridge Register Group */
72 #define AVE_RSTCTRL 0x8028 /* Reset control */
73 #define AVE_RSTCTRL_RMIIRST BIT(16)
74 #define AVE_LINKSEL 0x8034 /* Link speed setting */
75 #define AVE_LINKSEL_100M BIT(0)
78 #define AVE_GRR_PHYRST BIT(4) /* Reset external PHY */
79 #define AVE_GRR_GRST BIT(0) /* Reset all MAC */
82 #define AVE_CFGR_MII BIT(27) /* Func mode (1:MII/RMII, 0:RGMII) */
84 /* AVE_GISR (common with GIMR) */
85 #define AVE_GIMR_CLR 0
86 #define AVE_GISR_CLR GENMASK(31, 0)
89 #define AVE_TXCR_FLOCTR BIT(18) /* Flow control */
90 #define AVE_TXCR_TXSPD_1G BIT(17)
91 #define AVE_TXCR_TXSPD_100 BIT(16)
94 #define AVE_RXCR_RXEN BIT(30) /* Rx enable */
95 #define AVE_RXCR_FDUPEN BIT(22) /* Interface mode */
96 #define AVE_RXCR_FLOCTR BIT(21) /* Flow control */
99 #define AVE_MDIOCTR_RREQ BIT(3) /* Read request */
100 #define AVE_MDIOCTR_WREQ BIT(2) /* Write request */
103 #define AVE_MDIOSR_STS BIT(0) /* access status */
106 #define AVE_DESCC_RXDSTPSTS BIT(20)
107 #define AVE_DESCC_RD0 BIT(8) /* Enable Rx descriptor Ring0 */
108 #define AVE_DESCC_RXDSTP BIT(4) /* Pause Rx descriptor */
109 #define AVE_DESCC_TD BIT(0) /* Enable Tx descriptor */
112 #define AVE_DESC_SIZE(priv, num) \
113 ((num) * ((priv)->data->is_desc_64bit ? AVE_DESC_SIZE_64 : \
116 /* Command status for descriptor */
117 #define AVE_STS_OWN BIT(31) /* Descriptor ownership */
118 #define AVE_STS_OK BIT(27) /* Normal transmit */
119 #define AVE_STS_1ST BIT(26) /* Head of buffer chain */
120 #define AVE_STS_LAST BIT(25) /* Tail of buffer chain */
121 #define AVE_STS_PKTLEN_TX_MASK GENMASK(15, 0)
122 #define AVE_STS_PKTLEN_RX_MASK GENMASK(10, 0)
124 #define AVE_DESC_OFS_CMDSTS 0
125 #define AVE_DESC_OFS_ADDRL 4
126 #define AVE_DESC_OFS_ADDRU 8
128 /* Parameter for ethernet frame */
129 #define AVE_RXCR_MTU 1518
132 #define SG_ETPINMODE 0x540
133 #define SG_ETPINMODE_EXTPHY BIT(1) /* for LD11 */
134 #define SG_ETPINMODE_RMII(ins) BIT(ins)
136 #define AVE_MAX_CLKS 4
137 #define AVE_MAX_RSTS 2
147 struct clk clk[AVE_MAX_CLKS];
149 struct reset_ctl rst[AVE_MAX_RSTS];
150 struct regmap *regmap;
151 unsigned int regmap_arg;
154 struct phy_device *phydev;
163 u8 tx_adj_packetbuf[PKTSIZE_ALIGN + PKTALIGN];
166 const struct ave_soc_data *data;
169 struct ave_soc_data {
171 const char *clock_names[AVE_MAX_CLKS];
172 const char *reset_names[AVE_MAX_RSTS];
173 int (*get_pinmode)(struct ave_private *priv);
176 static u32 ave_desc_read(struct ave_private *priv, enum desc_id id, int entry,
182 if (priv->data->is_desc_64bit) {
183 desc_size = AVE_DESC_SIZE_64;
184 addr = (id == AVE_DESCID_TX) ? AVE_TXDM_64 : AVE_RXDM_64;
186 desc_size = AVE_DESC_SIZE_32;
187 addr = (id == AVE_DESCID_TX) ? AVE_TXDM_32 : AVE_RXDM_32;
190 addr += entry * desc_size + offset;
192 return readl(priv->iobase + addr);
195 static u32 ave_desc_read_cmdsts(struct ave_private *priv, enum desc_id id,
198 return ave_desc_read(priv, id, entry, AVE_DESC_OFS_CMDSTS);
201 static void ave_desc_write(struct ave_private *priv, enum desc_id id,
202 int entry, int offset, u32 val)
207 if (priv->data->is_desc_64bit) {
208 desc_size = AVE_DESC_SIZE_64;
209 addr = (id == AVE_DESCID_TX) ? AVE_TXDM_64 : AVE_RXDM_64;
211 desc_size = AVE_DESC_SIZE_32;
212 addr = (id == AVE_DESCID_TX) ? AVE_TXDM_32 : AVE_RXDM_32;
215 addr += entry * desc_size + offset;
216 writel(val, priv->iobase + addr);
219 static void ave_desc_write_cmdsts(struct ave_private *priv, enum desc_id id,
222 ave_desc_write(priv, id, entry, AVE_DESC_OFS_CMDSTS, val);
225 static void ave_desc_write_addr(struct ave_private *priv, enum desc_id id,
226 int entry, uintptr_t paddr)
228 ave_desc_write(priv, id, entry,
229 AVE_DESC_OFS_ADDRL, lower_32_bits(paddr));
230 if (priv->data->is_desc_64bit)
231 ave_desc_write(priv, id, entry,
232 AVE_DESC_OFS_ADDRU, upper_32_bits(paddr));
235 static void ave_cache_invalidate(uintptr_t vaddr, int len)
237 invalidate_dcache_range(rounddown(vaddr, ARCH_DMA_MINALIGN),
238 roundup(vaddr + len, ARCH_DMA_MINALIGN));
241 static void ave_cache_flush(uintptr_t vaddr, int len)
243 flush_dcache_range(rounddown(vaddr, ARCH_DMA_MINALIGN),
244 roundup(vaddr + len, ARCH_DMA_MINALIGN));
247 static int ave_mdiobus_read(struct mii_dev *bus,
248 int phyid, int devad, int regnum)
250 struct ave_private *priv = bus->priv;
255 writel((phyid << 8) | regnum, priv->iobase + AVE_MDIOAR);
258 mdioctl = readl(priv->iobase + AVE_MDIOCTR);
259 writel(mdioctl | AVE_MDIOCTR_RREQ, priv->iobase + AVE_MDIOCTR);
261 ret = readl_poll_timeout(priv->iobase + AVE_MDIOSR, mdiosr,
262 !(mdiosr & AVE_MDIOSR_STS),
263 AVE_MDIO_TIMEOUT_USEC);
265 pr_err("%s: failed to read from mdio (phy:%d reg:%x)\n",
266 priv->phydev->dev->name, phyid, regnum);
270 return readl(priv->iobase + AVE_MDIORDR) & GENMASK(15, 0);
273 static int ave_mdiobus_write(struct mii_dev *bus,
274 int phyid, int devad, int regnum, u16 val)
276 struct ave_private *priv = bus->priv;
281 writel((phyid << 8) | regnum, priv->iobase + AVE_MDIOAR);
284 writel(val, priv->iobase + AVE_MDIOWDR);
287 mdioctl = readl(priv->iobase + AVE_MDIOCTR);
288 writel((mdioctl | AVE_MDIOCTR_WREQ) & ~AVE_MDIOCTR_RREQ,
289 priv->iobase + AVE_MDIOCTR);
291 ret = readl_poll_timeout(priv->iobase + AVE_MDIOSR, mdiosr,
292 !(mdiosr & AVE_MDIOSR_STS),
293 AVE_MDIO_TIMEOUT_USEC);
295 pr_err("%s: failed to write to mdio (phy:%d reg:%x)\n",
296 priv->phydev->dev->name, phyid, regnum);
301 static int ave_adjust_link(struct ave_private *priv)
303 struct phy_device *phydev = priv->phydev;
304 struct eth_pdata *pdata = dev_get_plat(phydev->dev);
305 u32 val, txcr, rxcr, rxcr_org;
306 u16 rmt_adv = 0, lcl_adv = 0;
309 /* set RGMII speed */
310 val = readl(priv->iobase + AVE_TXCR);
311 val &= ~(AVE_TXCR_TXSPD_100 | AVE_TXCR_TXSPD_1G);
313 if (phy_interface_is_rgmii(phydev) && phydev->speed == SPEED_1000)
314 val |= AVE_TXCR_TXSPD_1G;
315 else if (phydev->speed == SPEED_100)
316 val |= AVE_TXCR_TXSPD_100;
318 writel(val, priv->iobase + AVE_TXCR);
320 /* set RMII speed (100M/10M only) */
321 if (!phy_interface_is_rgmii(phydev)) {
322 val = readl(priv->iobase + AVE_LINKSEL);
323 if (phydev->speed == SPEED_10)
324 val &= ~AVE_LINKSEL_100M;
326 val |= AVE_LINKSEL_100M;
327 writel(val, priv->iobase + AVE_LINKSEL);
330 /* check current RXCR/TXCR */
331 rxcr = readl(priv->iobase + AVE_RXCR);
332 txcr = readl(priv->iobase + AVE_TXCR);
335 if (phydev->duplex) {
336 rxcr |= AVE_RXCR_FDUPEN;
339 rmt_adv |= LPA_PAUSE_CAP;
340 if (phydev->asym_pause)
341 rmt_adv |= LPA_PAUSE_ASYM;
342 if (phydev->advertising & ADVERTISED_Pause)
343 lcl_adv |= ADVERTISE_PAUSE_CAP;
344 if (phydev->advertising & ADVERTISED_Asym_Pause)
345 lcl_adv |= ADVERTISE_PAUSE_ASYM;
347 cap = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv);
348 if (cap & FLOW_CTRL_TX)
349 txcr |= AVE_TXCR_FLOCTR;
351 txcr &= ~AVE_TXCR_FLOCTR;
352 if (cap & FLOW_CTRL_RX)
353 rxcr |= AVE_RXCR_FLOCTR;
355 rxcr &= ~AVE_RXCR_FLOCTR;
357 rxcr &= ~AVE_RXCR_FDUPEN;
358 rxcr &= ~AVE_RXCR_FLOCTR;
359 txcr &= ~AVE_TXCR_FLOCTR;
362 if (rxcr_org != rxcr) {
364 writel(rxcr & ~AVE_RXCR_RXEN, priv->iobase + AVE_RXCR);
365 /* change and enable TX/Rx mac */
366 writel(txcr, priv->iobase + AVE_TXCR);
367 writel(rxcr, priv->iobase + AVE_RXCR);
370 pr_notice("%s: phy:%s speed:%d mac:%pM\n",
371 phydev->dev->name, phydev->drv->name, phydev->speed,
377 static int ave_mdiobus_init(struct ave_private *priv, const char *name)
379 struct mii_dev *bus = mdio_alloc();
384 bus->read = ave_mdiobus_read;
385 bus->write = ave_mdiobus_write;
386 snprintf(bus->name, sizeof(bus->name), "%s", name);
389 return mdio_register(bus);
392 static int ave_phy_init(struct ave_private *priv, void *dev)
394 struct phy_device *phydev;
397 phydev = phy_connect(priv->bus, -1, dev, priv->phy_mode);
401 phydev->supported &= PHY_GBIT_FEATURES;
402 if (priv->max_speed) {
403 ret = phy_set_supported(phydev, priv->max_speed);
407 phydev->advertising = phydev->supported;
409 priv->phydev = phydev;
415 static void ave_stop(struct udevice *dev)
417 struct ave_private *priv = dev_get_priv(dev);
421 val = readl(priv->iobase + AVE_GRR);
425 val = readl(priv->iobase + AVE_RXCR);
426 val &= ~AVE_RXCR_RXEN;
427 writel(val, priv->iobase + AVE_RXCR);
429 writel(0, priv->iobase + AVE_DESCC);
430 ret = readl_poll_timeout(priv->iobase + AVE_DESCC, val, !val,
431 AVE_HALT_TIMEOUT_USEC);
433 pr_warn("%s: halt timeout\n", priv->phydev->dev->name);
435 writel(AVE_GRR_GRST, priv->iobase + AVE_GRR);
437 phy_shutdown(priv->phydev);
440 static void ave_reset(struct ave_private *priv)
444 /* reset RMII register */
445 val = readl(priv->iobase + AVE_RSTCTRL);
446 val &= ~AVE_RSTCTRL_RMIIRST;
447 writel(val, priv->iobase + AVE_RSTCTRL);
450 writel(AVE_GRR_GRST | AVE_GRR_PHYRST, priv->iobase + AVE_GRR);
451 mdelay(AVE_GRST_DELAY_MSEC);
453 /* 1st, negate PHY reset only */
454 writel(AVE_GRR_GRST, priv->iobase + AVE_GRR);
455 mdelay(AVE_GRST_DELAY_MSEC);
458 writel(0, priv->iobase + AVE_GRR);
459 mdelay(AVE_GRST_DELAY_MSEC);
461 /* negate RMII register */
462 val = readl(priv->iobase + AVE_RSTCTRL);
463 val |= AVE_RSTCTRL_RMIIRST;
464 writel(val, priv->iobase + AVE_RSTCTRL);
467 static int ave_start(struct udevice *dev)
469 struct ave_private *priv = dev_get_priv(dev);
477 priv->rx_off = 2; /* RX data has 2byte offsets */
480 (void *)roundup((uintptr_t)&priv->tx_adj_packetbuf[0],
482 priv->rx_siz = (PKTSIZE_ALIGN - priv->rx_off);
485 if (priv->phy_mode != PHY_INTERFACE_MODE_RGMII &&
486 priv->phy_mode != PHY_INTERFACE_MODE_RGMII_ID &&
487 priv->phy_mode != PHY_INTERFACE_MODE_RGMII_RXID &&
488 priv->phy_mode != PHY_INTERFACE_MODE_RGMII_TXID)
490 writel(val, priv->iobase + AVE_CFGR);
492 /* use one descriptor for Tx */
493 writel(AVE_DESC_SIZE(priv, 1) << 16, priv->iobase + AVE_TXDC);
494 ave_desc_write_cmdsts(priv, AVE_DESCID_TX, 0, 0);
495 ave_desc_write_addr(priv, AVE_DESCID_TX, 0, 0);
497 /* use PKTBUFSRX descriptors for Rx */
498 writel(AVE_DESC_SIZE(priv, PKTBUFSRX) << 16, priv->iobase + AVE_RXDC);
499 for (i = 0; i < PKTBUFSRX; i++) {
500 paddr = (uintptr_t)net_rx_packets[i];
501 ave_cache_flush(paddr, priv->rx_siz + priv->rx_off);
502 ave_desc_write_addr(priv, AVE_DESCID_RX, i, paddr);
503 ave_desc_write_cmdsts(priv, AVE_DESCID_RX, i, priv->rx_siz);
506 writel(AVE_GISR_CLR, priv->iobase + AVE_GISR);
507 writel(AVE_GIMR_CLR, priv->iobase + AVE_GIMR);
509 writel(AVE_RXCR_RXEN | AVE_RXCR_FDUPEN | AVE_RXCR_FLOCTR | AVE_RXCR_MTU,
510 priv->iobase + AVE_RXCR);
511 writel(AVE_DESCC_RD0 | AVE_DESCC_TD, priv->iobase + AVE_DESCC);
513 phy_startup(priv->phydev);
514 ave_adjust_link(priv);
519 static int ave_write_hwaddr(struct udevice *dev)
521 struct ave_private *priv = dev_get_priv(dev);
522 struct eth_pdata *pdata = dev_get_plat(dev);
523 u8 *mac = pdata->enetaddr;
525 writel(mac[0] | mac[1] << 8 | mac[2] << 16 | mac[3] << 24,
526 priv->iobase + AVE_RXMAC1R);
527 writel(mac[4] | mac[5] << 8, priv->iobase + AVE_RXMAC2R);
532 static int ave_send(struct udevice *dev, void *packet, int length)
534 struct ave_private *priv = dev_get_priv(dev);
539 /* adjust alignment for descriptor */
540 if ((uintptr_t)ptr & 0x3) {
541 memcpy(priv->tx_adj_buf, (const void *)ptr, length);
542 ptr = priv->tx_adj_buf;
545 /* padding for minimum length */
546 if (length < AVE_MIN_XMITSIZE) {
547 memset(ptr + length, 0, AVE_MIN_XMITSIZE - length);
548 length = AVE_MIN_XMITSIZE;
551 /* check ownership and wait for previous xmit done */
552 count = AVE_SEND_TIMEOUT_COUNT;
554 val = ave_desc_read_cmdsts(priv, AVE_DESCID_TX, 0);
555 } while ((val & AVE_STS_OWN) && --count);
559 ave_cache_flush((uintptr_t)ptr, length);
560 ave_desc_write_addr(priv, AVE_DESCID_TX, 0, (uintptr_t)ptr);
562 val = AVE_STS_OWN | AVE_STS_1ST | AVE_STS_LAST |
563 (length & AVE_STS_PKTLEN_TX_MASK);
564 ave_desc_write_cmdsts(priv, AVE_DESCID_TX, 0, val);
567 count = AVE_SEND_TIMEOUT_COUNT;
569 val = ave_desc_read_cmdsts(priv, AVE_DESCID_TX, 0);
570 } while ((val & AVE_STS_OWN) && --count);
574 if (!(val & AVE_STS_OK))
575 pr_warn("%s: bad send packet status:%08x\n",
576 priv->phydev->dev->name, le32_to_cpu(val));
581 static int ave_recv(struct udevice *dev, int flags, uchar **packetp)
583 struct ave_private *priv = dev_get_priv(dev);
589 cmdsts = ave_desc_read_cmdsts(priv, AVE_DESCID_RX,
591 if (!(cmdsts & AVE_STS_OWN))
592 /* hardware ownership, no received packets */
595 ptr = net_rx_packets[priv->rx_pos] + priv->rx_off;
596 if (cmdsts & AVE_STS_OK)
599 pr_warn("%s: bad packet[%d] status:%08x ptr:%p\n",
600 priv->phydev->dev->name, priv->rx_pos,
601 le32_to_cpu(cmdsts), ptr);
604 length = cmdsts & AVE_STS_PKTLEN_RX_MASK;
606 /* invalidate after DMA is done */
607 ave_cache_invalidate((uintptr_t)ptr, length);
613 static int ave_free_packet(struct udevice *dev, uchar *packet, int length)
615 struct ave_private *priv = dev_get_priv(dev);
617 ave_cache_flush((uintptr_t)net_rx_packets[priv->rx_pos],
618 priv->rx_siz + priv->rx_off);
620 ave_desc_write_cmdsts(priv, AVE_DESCID_RX,
621 priv->rx_pos, priv->rx_siz);
623 if (++priv->rx_pos >= PKTBUFSRX)
629 static int ave_pro4_get_pinmode(struct ave_private *priv)
631 u32 reg, mask, val = 0;
633 if (priv->regmap_arg > 0)
636 mask = SG_ETPINMODE_RMII(0);
638 switch (priv->phy_mode) {
639 case PHY_INTERFACE_MODE_RMII:
640 val = SG_ETPINMODE_RMII(0);
642 case PHY_INTERFACE_MODE_MII:
643 case PHY_INTERFACE_MODE_RGMII:
644 case PHY_INTERFACE_MODE_RGMII_ID:
645 case PHY_INTERFACE_MODE_RGMII_RXID:
646 case PHY_INTERFACE_MODE_RGMII_TXID:
652 regmap_read(priv->regmap, SG_ETPINMODE, ®);
655 regmap_write(priv->regmap, SG_ETPINMODE, reg);
660 static int ave_ld11_get_pinmode(struct ave_private *priv)
662 u32 reg, mask, val = 0;
664 if (priv->regmap_arg > 0)
667 mask = SG_ETPINMODE_EXTPHY | SG_ETPINMODE_RMII(0);
669 switch (priv->phy_mode) {
670 case PHY_INTERFACE_MODE_INTERNAL:
672 case PHY_INTERFACE_MODE_RMII:
673 val = SG_ETPINMODE_EXTPHY | SG_ETPINMODE_RMII(0);
679 regmap_read(priv->regmap, SG_ETPINMODE, ®);
682 regmap_write(priv->regmap, SG_ETPINMODE, reg);
687 static int ave_ld20_get_pinmode(struct ave_private *priv)
689 u32 reg, mask, val = 0;
691 if (priv->regmap_arg > 0)
694 mask = SG_ETPINMODE_RMII(0);
696 switch (priv->phy_mode) {
697 case PHY_INTERFACE_MODE_RMII:
698 val = SG_ETPINMODE_RMII(0);
700 case PHY_INTERFACE_MODE_RGMII:
701 case PHY_INTERFACE_MODE_RGMII_ID:
702 case PHY_INTERFACE_MODE_RGMII_RXID:
703 case PHY_INTERFACE_MODE_RGMII_TXID:
709 regmap_read(priv->regmap, SG_ETPINMODE, ®);
712 regmap_write(priv->regmap, SG_ETPINMODE, reg);
717 static int ave_pxs3_get_pinmode(struct ave_private *priv)
719 u32 reg, mask, val = 0;
721 if (priv->regmap_arg > 1)
724 mask = SG_ETPINMODE_RMII(priv->regmap_arg);
726 switch (priv->phy_mode) {
727 case PHY_INTERFACE_MODE_RMII:
728 val = SG_ETPINMODE_RMII(priv->regmap_arg);
730 case PHY_INTERFACE_MODE_RGMII:
731 case PHY_INTERFACE_MODE_RGMII_ID:
732 case PHY_INTERFACE_MODE_RGMII_RXID:
733 case PHY_INTERFACE_MODE_RGMII_TXID:
739 regmap_read(priv->regmap, SG_ETPINMODE, ®);
742 regmap_write(priv->regmap, SG_ETPINMODE, reg);
747 static int ave_of_to_plat(struct udevice *dev)
749 struct eth_pdata *pdata = dev_get_plat(dev);
750 struct ave_private *priv = dev_get_priv(dev);
751 struct ofnode_phandle_args args;
756 priv->data = (const struct ave_soc_data *)dev_get_driver_data(dev);
760 pdata->iobase = dev_read_addr(dev);
762 pdata->phy_interface = dev_read_phy_mode(dev);
763 if (pdata->phy_interface == PHY_INTERFACE_MODE_NA)
766 pdata->max_speed = 0;
767 valp = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "max-speed",
770 pdata->max_speed = fdt32_to_cpu(*valp);
772 for (nc = 0; nc < AVE_MAX_CLKS; nc++) {
773 name = priv->data->clock_names[nc];
776 ret = clk_get_by_name(dev, name, &priv->clk[nc]);
778 dev_err(dev, "Failed to get clocks property: %d\n",
785 for (nr = 0; nr < AVE_MAX_RSTS; nr++) {
786 name = priv->data->reset_names[nr];
789 ret = reset_get_by_name(dev, name, &priv->rst[nr]);
791 dev_err(dev, "Failed to get resets property: %d\n",
798 ret = dev_read_phandle_with_args(dev, "socionext,syscon-phy-mode",
801 dev_err(dev, "Failed to get syscon-phy-mode property: %d\n",
806 priv->regmap = syscon_node_to_regmap(args.node);
807 if (IS_ERR(priv->regmap)) {
808 ret = PTR_ERR(priv->regmap);
809 dev_err(dev, "can't get syscon: %d\n", ret);
813 if (args.args_count != 1) {
815 dev_err(dev, "Invalid argument of syscon-phy-mode\n");
819 priv->regmap_arg = args.args[0];
825 reset_free(&priv->rst[nr]);
830 static int ave_probe(struct udevice *dev)
832 struct eth_pdata *pdata = dev_get_plat(dev);
833 struct ave_private *priv = dev_get_priv(dev);
836 priv->data = (const struct ave_soc_data *)dev_get_driver_data(dev);
840 priv->iobase = pdata->iobase;
841 priv->phy_mode = pdata->phy_interface;
842 priv->max_speed = pdata->max_speed;
844 ret = priv->data->get_pinmode(priv);
846 dev_err(dev, "Invalid phy-mode\n");
850 for (nc = 0; nc < priv->nclks; nc++) {
851 ret = clk_enable(&priv->clk[nc]);
853 dev_err(dev, "Failed to enable clk: %d\n", ret);
854 goto out_clk_release;
858 for (nr = 0; nr < priv->nrsts; nr++) {
859 ret = reset_deassert(&priv->rst[nr]);
861 dev_err(dev, "Failed to deassert reset: %d\n", ret);
862 goto out_reset_release;
868 ret = ave_mdiobus_init(priv, dev->name);
870 dev_err(dev, "Failed to initialize mdiobus: %d\n", ret);
871 goto out_reset_release;
874 priv->bus = miiphy_get_dev_by_name(dev->name);
876 ret = ave_phy_init(priv, dev);
878 dev_err(dev, "Failed to initialize phy: %d\n", ret);
879 goto out_mdiobus_release;
885 mdio_unregister(priv->bus);
886 mdio_free(priv->bus);
888 reset_release_all(priv->rst, nr);
890 clk_release_all(priv->clk, nc);
895 static int ave_remove(struct udevice *dev)
897 struct ave_private *priv = dev_get_priv(dev);
900 mdio_unregister(priv->bus);
901 mdio_free(priv->bus);
902 reset_release_all(priv->rst, priv->nrsts);
903 clk_release_all(priv->clk, priv->nclks);
908 static const struct eth_ops ave_ops = {
913 .free_pkt = ave_free_packet,
914 .write_hwaddr = ave_write_hwaddr,
917 static const struct ave_soc_data ave_pro4_data = {
918 .is_desc_64bit = false,
920 "gio", "ether", "ether-gb", "ether-phy",
925 .get_pinmode = ave_pro4_get_pinmode,
928 static const struct ave_soc_data ave_pxs2_data = {
929 .is_desc_64bit = false,
936 .get_pinmode = ave_pro4_get_pinmode,
939 static const struct ave_soc_data ave_ld11_data = {
940 .is_desc_64bit = false,
947 .get_pinmode = ave_ld11_get_pinmode,
950 static const struct ave_soc_data ave_ld20_data = {
951 .is_desc_64bit = true,
958 .get_pinmode = ave_ld20_get_pinmode,
961 static const struct ave_soc_data ave_pxs3_data = {
962 .is_desc_64bit = false,
969 .get_pinmode = ave_pxs3_get_pinmode,
972 static const struct udevice_id ave_ids[] = {
974 .compatible = "socionext,uniphier-pro4-ave4",
975 .data = (ulong)&ave_pro4_data,
978 .compatible = "socionext,uniphier-pxs2-ave4",
979 .data = (ulong)&ave_pxs2_data,
982 .compatible = "socionext,uniphier-ld11-ave4",
983 .data = (ulong)&ave_ld11_data,
986 .compatible = "socionext,uniphier-ld20-ave4",
987 .data = (ulong)&ave_ld20_data,
990 .compatible = "socionext,uniphier-pxs3-ave4",
991 .data = (ulong)&ave_pxs3_data,
996 U_BOOT_DRIVER(ave) = {
1001 .remove = ave_remove,
1002 .of_to_plat = ave_of_to_plat,
1004 .priv_auto = sizeof(struct ave_private),
1005 .plat_auto = sizeof(struct eth_pdata),