1 // SPDX-License-Identifier: GPL-2.0+
3 * sh_eth.c - Driver for Renesas ethernet controller.
5 * Copyright (C) 2008, 2011 Renesas Solutions Corp.
6 * Copyright (c) 2008, 2011, 2014 2014 Nobuhiro Iwamatsu
8 * Copyright (C) 2013, 2014 Renesas Electronics Corporation
20 #include <asm/cache.h>
21 #include <linux/delay.h>
22 #include <linux/errno.h>
23 #include <asm/global_data.h>
29 #include <linux/mii.h>
35 #ifndef CONFIG_SH_ETHER_USE_PORT
36 # error "Please define CONFIG_SH_ETHER_USE_PORT"
38 #ifndef CONFIG_SH_ETHER_PHY_ADDR
39 # error "Please define CONFIG_SH_ETHER_PHY_ADDR"
42 #if defined(CONFIG_SH_ETHER_CACHE_WRITEBACK) && \
43 !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
44 #define flush_cache_wback(addr, len) \
45 flush_dcache_range((unsigned long)addr, \
46 (unsigned long)(addr + ALIGN(len, CONFIG_SH_ETHER_ALIGNE_SIZE)))
48 #define flush_cache_wback(...)
51 #if defined(CONFIG_SH_ETHER_CACHE_INVALIDATE) && defined(CONFIG_ARM)
52 #define invalidate_cache(addr, len) \
54 unsigned long line_size = CONFIG_SH_ETHER_ALIGNE_SIZE; \
55 unsigned long start, end; \
57 start = (unsigned long)addr; \
59 start &= ~(line_size - 1); \
60 end = ((end + line_size - 1) & ~(line_size - 1)); \
62 invalidate_dcache_range(start, end); \
65 #define invalidate_cache(...)
68 #define TIMEOUT_CNT 1000
70 static int sh_eth_send_common(struct sh_eth_dev *eth, void *packet, int len)
73 struct sh_eth_info *port_info = ð->port_info[eth->port];
75 if (!packet || len > 0xffff) {
76 printf(SHETHER_NAME ": %s: Invalid argument\n", __func__);
81 /* packet must be a 4 byte boundary */
82 if ((uintptr_t)packet & 3) {
83 printf(SHETHER_NAME ": %s: packet not 4 byte aligned\n"
89 /* Update tx descriptor */
90 flush_cache_wback(packet, len);
91 port_info->tx_desc_cur->td2 = ADDR_TO_PHY(packet);
92 port_info->tx_desc_cur->td1 = len << 16;
93 /* Must preserve the end of descriptor list indication */
94 if (port_info->tx_desc_cur->td0 & TD_TDLE)
95 port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP | TD_TDLE;
97 port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP;
99 flush_cache_wback(port_info->tx_desc_cur, sizeof(struct tx_desc_s));
101 /* Restart the transmitter if disabled */
102 if (!(sh_eth_read(port_info, EDTRR) & EDTRR_TRNS))
103 sh_eth_write(port_info, EDTRR_TRNS, EDTRR);
105 /* Wait until packet is transmitted */
106 timeout = TIMEOUT_CNT;
108 invalidate_cache(port_info->tx_desc_cur,
109 sizeof(struct tx_desc_s));
111 } while (port_info->tx_desc_cur->td0 & TD_TACT && timeout--);
114 printf(SHETHER_NAME ": transmit timeout\n");
119 port_info->tx_desc_cur++;
120 if (port_info->tx_desc_cur >= port_info->tx_desc_base + NUM_TX_DESC)
121 port_info->tx_desc_cur = port_info->tx_desc_base;
127 static int sh_eth_recv_start(struct sh_eth_dev *eth)
129 struct sh_eth_info *port_info = ð->port_info[eth->port];
131 /* Check if the rx descriptor is ready */
132 invalidate_cache(port_info->rx_desc_cur, sizeof(struct rx_desc_s));
133 if (port_info->rx_desc_cur->rd0 & RD_RACT)
136 /* Check for errors */
137 if (port_info->rx_desc_cur->rd0 & RD_RFE)
140 return port_info->rx_desc_cur->rd1 & 0xffff;
143 static void sh_eth_recv_finish(struct sh_eth_dev *eth)
145 struct sh_eth_info *port_info = ð->port_info[eth->port];
147 /* Make current descriptor available again */
148 if (port_info->rx_desc_cur->rd0 & RD_RDLE)
149 port_info->rx_desc_cur->rd0 = RD_RACT | RD_RDLE;
151 port_info->rx_desc_cur->rd0 = RD_RACT;
153 flush_cache_wback(port_info->rx_desc_cur,
154 sizeof(struct rx_desc_s));
156 /* Point to the next descriptor */
157 port_info->rx_desc_cur++;
158 if (port_info->rx_desc_cur >=
159 port_info->rx_desc_base + NUM_RX_DESC)
160 port_info->rx_desc_cur = port_info->rx_desc_base;
163 static int sh_eth_reset(struct sh_eth_dev *eth)
165 struct sh_eth_info *port_info = ð->port_info[eth->port];
166 #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
169 /* Start e-dmac transmitter and receiver */
170 sh_eth_write(port_info, EDSR_ENALL, EDSR);
172 /* Perform a software reset and wait for it to complete */
173 sh_eth_write(port_info, EDMR_SRST, EDMR);
174 for (i = 0; i < TIMEOUT_CNT; i++) {
175 if (!(sh_eth_read(port_info, EDMR) & EDMR_SRST))
180 if (i == TIMEOUT_CNT) {
181 printf(SHETHER_NAME ": Software reset timeout\n");
187 sh_eth_write(port_info, sh_eth_read(port_info, EDMR) | EDMR_SRST, EDMR);
189 sh_eth_write(port_info,
190 sh_eth_read(port_info, EDMR) & ~EDMR_SRST, EDMR);
196 static int sh_eth_tx_desc_init(struct sh_eth_dev *eth)
199 u32 alloc_desc_size = NUM_TX_DESC * sizeof(struct tx_desc_s);
200 struct sh_eth_info *port_info = ð->port_info[eth->port];
201 struct tx_desc_s *cur_tx_desc;
204 * Allocate rx descriptors. They must be aligned to size of struct
207 port_info->tx_desc_alloc =
208 memalign(sizeof(struct tx_desc_s), alloc_desc_size);
209 if (!port_info->tx_desc_alloc) {
210 printf(SHETHER_NAME ": memalign failed\n");
215 flush_cache_wback(port_info->tx_desc_alloc, alloc_desc_size);
217 /* Make sure we use a P2 address (non-cacheable) */
218 port_info->tx_desc_base =
219 (struct tx_desc_s *)ADDR_TO_P2((uintptr_t)port_info->tx_desc_alloc);
220 port_info->tx_desc_cur = port_info->tx_desc_base;
222 /* Initialize all descriptors */
223 for (cur_tx_desc = port_info->tx_desc_base, i = 0; i < NUM_TX_DESC;
224 cur_tx_desc++, i++) {
225 cur_tx_desc->td0 = 0x00;
226 cur_tx_desc->td1 = 0x00;
227 cur_tx_desc->td2 = 0x00;
230 /* Mark the end of the descriptors */
232 cur_tx_desc->td0 |= TD_TDLE;
235 * Point the controller to the tx descriptor list. Must use physical
238 sh_eth_write(port_info, ADDR_TO_PHY(port_info->tx_desc_base), TDLAR);
239 #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
240 sh_eth_write(port_info, ADDR_TO_PHY(port_info->tx_desc_base), TDFAR);
241 sh_eth_write(port_info, ADDR_TO_PHY(cur_tx_desc), TDFXR);
242 sh_eth_write(port_info, 0x01, TDFFR);/* Last discriptor bit */
249 static int sh_eth_rx_desc_init(struct sh_eth_dev *eth)
252 u32 alloc_desc_size = NUM_RX_DESC * sizeof(struct rx_desc_s);
253 struct sh_eth_info *port_info = ð->port_info[eth->port];
254 struct rx_desc_s *cur_rx_desc;
258 * Allocate rx descriptors. They must be aligned to size of struct
261 port_info->rx_desc_alloc =
262 memalign(sizeof(struct rx_desc_s), alloc_desc_size);
263 if (!port_info->rx_desc_alloc) {
264 printf(SHETHER_NAME ": memalign failed\n");
269 flush_cache_wback(port_info->rx_desc_alloc, alloc_desc_size);
271 /* Make sure we use a P2 address (non-cacheable) */
272 port_info->rx_desc_base =
273 (struct rx_desc_s *)ADDR_TO_P2((uintptr_t)port_info->rx_desc_alloc);
275 port_info->rx_desc_cur = port_info->rx_desc_base;
278 * Allocate rx data buffers. They must be RX_BUF_ALIGNE_SIZE bytes
279 * aligned and in P2 area.
281 port_info->rx_buf_alloc =
282 memalign(RX_BUF_ALIGNE_SIZE, NUM_RX_DESC * MAX_BUF_SIZE);
283 if (!port_info->rx_buf_alloc) {
284 printf(SHETHER_NAME ": alloc failed\n");
289 port_info->rx_buf_base = (u8 *)ADDR_TO_P2((uintptr_t)port_info->rx_buf_alloc);
291 /* Initialize all descriptors */
292 for (cur_rx_desc = port_info->rx_desc_base,
293 rx_buf = port_info->rx_buf_base, i = 0;
294 i < NUM_RX_DESC; cur_rx_desc++, rx_buf += MAX_BUF_SIZE, i++) {
295 cur_rx_desc->rd0 = RD_RACT;
296 cur_rx_desc->rd1 = MAX_BUF_SIZE << 16;
297 cur_rx_desc->rd2 = (u32)ADDR_TO_PHY(rx_buf);
300 /* Mark the end of the descriptors */
302 cur_rx_desc->rd0 |= RD_RDLE;
304 /* Point the controller to the rx descriptor list */
305 sh_eth_write(port_info, ADDR_TO_PHY(port_info->rx_desc_base), RDLAR);
306 #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
307 sh_eth_write(port_info, ADDR_TO_PHY(port_info->rx_desc_base), RDFAR);
308 sh_eth_write(port_info, ADDR_TO_PHY(cur_rx_desc), RDFXR);
309 sh_eth_write(port_info, RDFFR_RDLF, RDFFR);
315 free(port_info->rx_desc_alloc);
316 port_info->rx_desc_alloc = NULL;
322 static void sh_eth_tx_desc_free(struct sh_eth_dev *eth)
324 struct sh_eth_info *port_info = ð->port_info[eth->port];
326 if (port_info->tx_desc_alloc) {
327 free(port_info->tx_desc_alloc);
328 port_info->tx_desc_alloc = NULL;
332 static void sh_eth_rx_desc_free(struct sh_eth_dev *eth)
334 struct sh_eth_info *port_info = ð->port_info[eth->port];
336 if (port_info->rx_desc_alloc) {
337 free(port_info->rx_desc_alloc);
338 port_info->rx_desc_alloc = NULL;
341 if (port_info->rx_buf_alloc) {
342 free(port_info->rx_buf_alloc);
343 port_info->rx_buf_alloc = NULL;
347 static int sh_eth_desc_init(struct sh_eth_dev *eth)
351 ret = sh_eth_tx_desc_init(eth);
355 ret = sh_eth_rx_desc_init(eth);
361 sh_eth_tx_desc_free(eth);
367 static void sh_eth_write_hwaddr(struct sh_eth_info *port_info,
372 val = (mac[0] << 24) | (mac[1] << 16) | (mac[2] << 8) | mac[3];
373 sh_eth_write(port_info, val, MAHR);
375 val = (mac[4] << 8) | mac[5];
376 sh_eth_write(port_info, val, MALR);
379 static void sh_eth_mac_regs_config(struct sh_eth_dev *eth, unsigned char *mac)
381 struct sh_eth_info *port_info = ð->port_info[eth->port];
384 /* Configure e-dmac registers */
385 edmr = sh_eth_read(port_info, EDMR);
386 edmr &= ~EMDR_DESC_R;
387 edmr |= EMDR_DESC | EDMR_EL;
388 #if defined(CONFIG_R8A77980)
391 sh_eth_write(port_info, edmr, EDMR);
393 sh_eth_write(port_info, 0, EESIPR);
394 sh_eth_write(port_info, 0, TRSCER);
395 sh_eth_write(port_info, 0, TFTR);
396 sh_eth_write(port_info, (FIFO_SIZE_T | FIFO_SIZE_R), FDR);
397 sh_eth_write(port_info, RMCR_RST, RMCR);
398 #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
399 sh_eth_write(port_info, 0, RPADIR);
401 sh_eth_write(port_info, (FIFO_F_D_RFF | FIFO_F_D_RFD), FCFTR);
403 /* Configure e-mac registers */
404 sh_eth_write(port_info, 0, ECSIPR);
406 /* Set Mac address */
407 sh_eth_write_hwaddr(port_info, mac);
409 sh_eth_write(port_info, RFLR_RFL_MIN, RFLR);
410 #if defined(SH_ETH_TYPE_GETHER)
411 sh_eth_write(port_info, 0, PIPR);
413 #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
414 sh_eth_write(port_info, APR_AP, APR);
415 sh_eth_write(port_info, MPR_MP, MPR);
416 sh_eth_write(port_info, TPAUSER_TPAUSE, TPAUSER);
419 #if defined(CONFIG_CPU_SH7734) || defined(CONFIG_R8A7740)
420 sh_eth_write(port_info, CONFIG_SH_ETHER_SH7734_MII, RMII_MII);
421 #elif defined(CONFIG_RCAR_GEN2) || defined(CONFIG_R8A77980)
422 sh_eth_write(port_info, sh_eth_read(port_info, RMIIMR) | 0x1, RMIIMR);
426 static int sh_eth_phy_regs_config(struct sh_eth_dev *eth)
428 struct sh_eth_info *port_info = ð->port_info[eth->port];
429 struct phy_device *phy = port_info->phydev;
433 /* Set the transfer speed */
434 if (phy->speed == 100) {
435 printf(SHETHER_NAME ": 100Base/");
436 #if defined(SH_ETH_TYPE_GETHER)
437 sh_eth_write(port_info, GECMR_100B, GECMR);
438 #elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
439 sh_eth_write(port_info, 1, RTRATE);
440 #elif defined(CONFIG_RCAR_GEN2) || defined(CONFIG_R8A77980)
443 } else if (phy->speed == 10) {
444 printf(SHETHER_NAME ": 10Base/");
445 #if defined(SH_ETH_TYPE_GETHER)
446 sh_eth_write(port_info, GECMR_10B, GECMR);
447 #elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
448 sh_eth_write(port_info, 0, RTRATE);
451 #if defined(SH_ETH_TYPE_GETHER)
452 else if (phy->speed == 1000) {
453 printf(SHETHER_NAME ": 1000Base/");
454 sh_eth_write(port_info, GECMR_1000B, GECMR);
458 /* Check if full duplex mode is supported by the phy */
461 sh_eth_write(port_info,
462 val | (ECMR_CHG_DM | ECMR_RE | ECMR_TE | ECMR_DM),
466 sh_eth_write(port_info,
467 val | (ECMR_CHG_DM | ECMR_RE | ECMR_TE),
474 static void sh_eth_start(struct sh_eth_dev *eth)
476 struct sh_eth_info *port_info = ð->port_info[eth->port];
479 * Enable the e-dmac receiver only. The transmitter will be enabled when
480 * we have something to transmit
482 sh_eth_write(port_info, EDRRR_R, EDRRR);
485 static void sh_eth_stop(struct sh_eth_dev *eth)
487 struct sh_eth_info *port_info = ð->port_info[eth->port];
489 sh_eth_write(port_info, ~EDRRR_R, EDRRR);
492 static int sh_eth_init_common(struct sh_eth_dev *eth, unsigned char *mac)
496 ret = sh_eth_reset(eth);
500 ret = sh_eth_desc_init(eth);
504 sh_eth_mac_regs_config(eth, mac);
509 static int sh_eth_start_common(struct sh_eth_dev *eth)
511 struct sh_eth_info *port_info = ð->port_info[eth->port];
514 ret = phy_startup(port_info->phydev);
516 printf(SHETHER_NAME ": phy startup failure\n");
520 ret = sh_eth_phy_regs_config(eth);
529 #ifndef CONFIG_DM_ETH
530 static int sh_eth_phy_config_legacy(struct sh_eth_dev *eth)
533 struct sh_eth_info *port_info = ð->port_info[eth->port];
534 struct eth_device *dev = port_info->dev;
535 struct phy_device *phydev;
537 phydev = phy_connect(
538 miiphy_get_dev_by_name(dev->name),
539 port_info->phy_addr, dev, CONFIG_SH_ETHER_PHY_MODE);
540 port_info->phydev = phydev;
546 static int sh_eth_send_legacy(struct eth_device *dev, void *packet, int len)
548 struct sh_eth_dev *eth = dev->priv;
550 return sh_eth_send_common(eth, packet, len);
553 static int sh_eth_recv_common(struct sh_eth_dev *eth)
556 struct sh_eth_info *port_info = ð->port_info[eth->port];
557 uchar *packet = (uchar *)ADDR_TO_P2(port_info->rx_desc_cur->rd2);
559 len = sh_eth_recv_start(eth);
561 invalidate_cache(packet, len);
562 net_process_received_packet(packet, len);
563 sh_eth_recv_finish(eth);
567 /* Restart the receiver if disabled */
568 if (!(sh_eth_read(port_info, EDRRR) & EDRRR_R))
569 sh_eth_write(port_info, EDRRR_R, EDRRR);
574 static int sh_eth_recv_legacy(struct eth_device *dev)
576 struct sh_eth_dev *eth = dev->priv;
578 return sh_eth_recv_common(eth);
581 static int sh_eth_init_legacy(struct eth_device *dev, struct bd_info *bd)
583 struct sh_eth_dev *eth = dev->priv;
586 ret = sh_eth_init_common(eth, dev->enetaddr);
590 ret = sh_eth_phy_config_legacy(eth);
592 printf(SHETHER_NAME ": phy config timeout\n");
596 ret = sh_eth_start_common(eth);
603 sh_eth_tx_desc_free(eth);
604 sh_eth_rx_desc_free(eth);
608 void sh_eth_halt_legacy(struct eth_device *dev)
610 struct sh_eth_dev *eth = dev->priv;
615 int sh_eth_initialize(struct bd_info *bd)
618 struct sh_eth_dev *eth = NULL;
619 struct eth_device *dev = NULL;
620 struct mii_dev *mdiodev;
622 eth = (struct sh_eth_dev *)malloc(sizeof(struct sh_eth_dev));
624 printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
629 dev = (struct eth_device *)malloc(sizeof(struct eth_device));
631 printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
635 memset(dev, 0, sizeof(struct eth_device));
636 memset(eth, 0, sizeof(struct sh_eth_dev));
638 eth->port = CONFIG_SH_ETHER_USE_PORT;
639 eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
640 eth->port_info[eth->port].iobase =
641 (void __iomem *)(BASE_IO_ADDR + 0x800 * eth->port);
643 dev->priv = (void *)eth;
645 dev->init = sh_eth_init_legacy;
646 dev->halt = sh_eth_halt_legacy;
647 dev->send = sh_eth_send_legacy;
648 dev->recv = sh_eth_recv_legacy;
649 eth->port_info[eth->port].dev = dev;
651 strcpy(dev->name, SHETHER_NAME);
653 /* Register Device to EtherNet subsystem */
656 bb_miiphy_buses[0].priv = eth;
657 mdiodev = mdio_alloc();
660 strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
661 mdiodev->read = bb_miiphy_read;
662 mdiodev->write = bb_miiphy_write;
664 ret = mdio_register(mdiodev);
668 if (!eth_env_get_enetaddr("ethaddr", dev->enetaddr))
669 puts("Please set MAC address\n");
680 printf(SHETHER_NAME ": Failed\n");
684 #else /* CONFIG_DM_ETH */
686 struct sh_ether_priv {
687 struct sh_eth_dev shdev;
692 struct gpio_desc reset_gpio;
695 static int sh_ether_send(struct udevice *dev, void *packet, int len)
697 struct sh_ether_priv *priv = dev_get_priv(dev);
698 struct sh_eth_dev *eth = &priv->shdev;
700 return sh_eth_send_common(eth, packet, len);
703 static int sh_ether_recv(struct udevice *dev, int flags, uchar **packetp)
705 struct sh_ether_priv *priv = dev_get_priv(dev);
706 struct sh_eth_dev *eth = &priv->shdev;
707 struct sh_eth_info *port_info = ð->port_info[eth->port];
708 uchar *packet = (uchar *)ADDR_TO_P2((uintptr_t)port_info->rx_desc_cur->rd2);
711 len = sh_eth_recv_start(eth);
713 invalidate_cache(packet, len);
720 /* Restart the receiver if disabled */
721 if (!(sh_eth_read(port_info, EDRRR) & EDRRR_R))
722 sh_eth_write(port_info, EDRRR_R, EDRRR);
728 static int sh_ether_free_pkt(struct udevice *dev, uchar *packet, int length)
730 struct sh_ether_priv *priv = dev_get_priv(dev);
731 struct sh_eth_dev *eth = &priv->shdev;
732 struct sh_eth_info *port_info = ð->port_info[eth->port];
734 sh_eth_recv_finish(eth);
736 /* Restart the receiver if disabled */
737 if (!(sh_eth_read(port_info, EDRRR) & EDRRR_R))
738 sh_eth_write(port_info, EDRRR_R, EDRRR);
743 static int sh_ether_write_hwaddr(struct udevice *dev)
745 struct sh_ether_priv *priv = dev_get_priv(dev);
746 struct sh_eth_dev *eth = &priv->shdev;
747 struct sh_eth_info *port_info = ð->port_info[eth->port];
748 struct eth_pdata *pdata = dev_get_plat(dev);
750 sh_eth_write_hwaddr(port_info, pdata->enetaddr);
755 static int sh_eth_phy_config(struct udevice *dev)
757 struct sh_ether_priv *priv = dev_get_priv(dev);
758 struct eth_pdata *pdata = dev_get_plat(dev);
759 struct sh_eth_dev *eth = &priv->shdev;
761 struct sh_eth_info *port_info = ð->port_info[eth->port];
762 struct phy_device *phydev;
763 int mask = 0xffffffff;
765 phydev = phy_find_by_mask(priv->bus, mask, pdata->phy_interface);
769 phy_connect_dev(phydev, dev);
771 port_info->phydev = phydev;
777 static int sh_ether_start(struct udevice *dev)
779 struct sh_ether_priv *priv = dev_get_priv(dev);
780 struct eth_pdata *pdata = dev_get_plat(dev);
781 struct sh_eth_dev *eth = &priv->shdev;
784 ret = sh_eth_init_common(eth, pdata->enetaddr);
788 ret = sh_eth_start_common(eth);
795 sh_eth_tx_desc_free(eth);
796 sh_eth_rx_desc_free(eth);
800 static void sh_ether_stop(struct udevice *dev)
802 struct sh_ether_priv *priv = dev_get_priv(dev);
803 struct sh_eth_dev *eth = &priv->shdev;
804 struct sh_eth_info *port_info = ð->port_info[eth->port];
806 phy_shutdown(port_info->phydev);
807 sh_eth_stop(&priv->shdev);
810 static int sh_ether_probe(struct udevice *udev)
812 struct eth_pdata *pdata = dev_get_plat(udev);
813 struct sh_ether_priv *priv = dev_get_priv(udev);
814 struct sh_eth_dev *eth = &priv->shdev;
815 struct ofnode_phandle_args phandle_args;
816 struct mii_dev *mdiodev;
819 priv->iobase = pdata->iobase;
821 #if CONFIG_IS_ENABLED(CLK)
822 ret = clk_get_by_index(udev, 0, &priv->clk);
827 ret = dev_read_phandle_with_args(udev, "phy-handle", NULL, 0, 0, &phandle_args);
829 gpio_request_by_name_nodev(phandle_args.node, "reset-gpios", 0,
830 &priv->reset_gpio, GPIOD_IS_OUT);
833 if (!dm_gpio_is_valid(&priv->reset_gpio)) {
834 gpio_request_by_name(udev, "reset-gpios", 0, &priv->reset_gpio,
838 mdiodev = mdio_alloc();
844 mdiodev->read = bb_miiphy_read;
845 mdiodev->write = bb_miiphy_write;
846 bb_miiphy_buses[0].priv = eth;
847 snprintf(mdiodev->name, sizeof(mdiodev->name), udev->name);
849 ret = mdio_register(mdiodev);
851 goto err_mdio_register;
853 priv->bus = miiphy_get_dev_by_name(udev->name);
855 eth->port = CONFIG_SH_ETHER_USE_PORT;
856 eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
857 eth->port_info[eth->port].iobase =
858 (void __iomem *)(uintptr_t)(BASE_IO_ADDR + 0x800 * eth->port);
860 #if CONFIG_IS_ENABLED(CLK)
861 ret = clk_enable(&priv->clk);
863 goto err_mdio_register;
866 ret = sh_eth_init_common(eth, pdata->enetaddr);
870 ret = sh_eth_phy_config(udev);
872 printf(SHETHER_NAME ": phy config timeout\n");
879 #if CONFIG_IS_ENABLED(CLK)
880 clk_disable(&priv->clk);
887 static int sh_ether_remove(struct udevice *udev)
889 struct sh_ether_priv *priv = dev_get_priv(udev);
890 struct sh_eth_dev *eth = &priv->shdev;
891 struct sh_eth_info *port_info = ð->port_info[eth->port];
893 #if CONFIG_IS_ENABLED(CLK)
894 clk_disable(&priv->clk);
896 free(port_info->phydev);
897 mdio_unregister(priv->bus);
898 mdio_free(priv->bus);
900 if (dm_gpio_is_valid(&priv->reset_gpio))
901 dm_gpio_free(udev, &priv->reset_gpio);
906 static const struct eth_ops sh_ether_ops = {
907 .start = sh_ether_start,
908 .send = sh_ether_send,
909 .recv = sh_ether_recv,
910 .free_pkt = sh_ether_free_pkt,
911 .stop = sh_ether_stop,
912 .write_hwaddr = sh_ether_write_hwaddr,
915 int sh_ether_of_to_plat(struct udevice *dev)
917 struct eth_pdata *pdata = dev_get_plat(dev);
918 const char *phy_mode;
922 pdata->iobase = dev_read_addr(dev);
923 pdata->phy_interface = -1;
924 phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
927 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
928 if (pdata->phy_interface == -1) {
929 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
933 pdata->max_speed = 1000;
934 cell = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "max-speed", NULL);
936 pdata->max_speed = fdt32_to_cpu(*cell);
938 sprintf(bb_miiphy_buses[0].name, dev->name);
943 static const struct udevice_id sh_ether_ids[] = {
944 { .compatible = "renesas,ether-r7s72100" },
945 { .compatible = "renesas,ether-r8a7790" },
946 { .compatible = "renesas,ether-r8a7791" },
947 { .compatible = "renesas,ether-r8a7793" },
948 { .compatible = "renesas,ether-r8a7794" },
949 { .compatible = "renesas,gether-r8a77980" },
953 U_BOOT_DRIVER(eth_sh_ether) = {
956 .of_match = sh_ether_ids,
957 .of_to_plat = sh_ether_of_to_plat,
958 .probe = sh_ether_probe,
959 .remove = sh_ether_remove,
960 .ops = &sh_ether_ops,
961 .priv_auto = sizeof(struct sh_ether_priv),
962 .plat_auto = sizeof(struct eth_pdata),
963 .flags = DM_FLAG_ALLOC_PRIV_DMA,
967 /******* for bb_miiphy *******/
968 static int sh_eth_bb_init(struct bb_miiphy_bus *bus)
973 static int sh_eth_bb_mdio_active(struct bb_miiphy_bus *bus)
975 struct sh_eth_dev *eth = bus->priv;
976 struct sh_eth_info *port_info = ð->port_info[eth->port];
978 sh_eth_write(port_info, sh_eth_read(port_info, PIR) | PIR_MMD, PIR);
983 static int sh_eth_bb_mdio_tristate(struct bb_miiphy_bus *bus)
985 struct sh_eth_dev *eth = bus->priv;
986 struct sh_eth_info *port_info = ð->port_info[eth->port];
988 sh_eth_write(port_info, sh_eth_read(port_info, PIR) & ~PIR_MMD, PIR);
993 static int sh_eth_bb_set_mdio(struct bb_miiphy_bus *bus, int v)
995 struct sh_eth_dev *eth = bus->priv;
996 struct sh_eth_info *port_info = ð->port_info[eth->port];
999 sh_eth_write(port_info,
1000 sh_eth_read(port_info, PIR) | PIR_MDO, PIR);
1002 sh_eth_write(port_info,
1003 sh_eth_read(port_info, PIR) & ~PIR_MDO, PIR);
1008 static int sh_eth_bb_get_mdio(struct bb_miiphy_bus *bus, int *v)
1010 struct sh_eth_dev *eth = bus->priv;
1011 struct sh_eth_info *port_info = ð->port_info[eth->port];
1013 *v = (sh_eth_read(port_info, PIR) & PIR_MDI) >> 3;
1018 static int sh_eth_bb_set_mdc(struct bb_miiphy_bus *bus, int v)
1020 struct sh_eth_dev *eth = bus->priv;
1021 struct sh_eth_info *port_info = ð->port_info[eth->port];
1024 sh_eth_write(port_info,
1025 sh_eth_read(port_info, PIR) | PIR_MDC, PIR);
1027 sh_eth_write(port_info,
1028 sh_eth_read(port_info, PIR) & ~PIR_MDC, PIR);
1033 static int sh_eth_bb_delay(struct bb_miiphy_bus *bus)
1040 struct bb_miiphy_bus bb_miiphy_buses[] = {
1043 .init = sh_eth_bb_init,
1044 .mdio_active = sh_eth_bb_mdio_active,
1045 .mdio_tristate = sh_eth_bb_mdio_tristate,
1046 .set_mdio = sh_eth_bb_set_mdio,
1047 .get_mdio = sh_eth_bb_get_mdio,
1048 .set_mdc = sh_eth_bb_set_mdc,
1049 .delay = sh_eth_bb_delay,
1053 int bb_miiphy_buses_num = ARRAY_SIZE(bb_miiphy_buses);