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
19 #include <linux/errno.h>
25 #include <linux/mii.h>
31 #ifndef CONFIG_SH_ETHER_USE_PORT
32 # error "Please define CONFIG_SH_ETHER_USE_PORT"
34 #ifndef CONFIG_SH_ETHER_PHY_ADDR
35 # error "Please define CONFIG_SH_ETHER_PHY_ADDR"
38 #if defined(CONFIG_SH_ETHER_CACHE_WRITEBACK) && \
39 !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
40 #define flush_cache_wback(addr, len) \
41 flush_dcache_range((unsigned long)addr, \
42 (unsigned long)(addr + ALIGN(len, CONFIG_SH_ETHER_ALIGNE_SIZE)))
44 #define flush_cache_wback(...)
47 #if defined(CONFIG_SH_ETHER_CACHE_INVALIDATE) && defined(CONFIG_ARM)
48 #define invalidate_cache(addr, len) \
50 unsigned long line_size = CONFIG_SH_ETHER_ALIGNE_SIZE; \
51 unsigned long start, end; \
53 start = (unsigned long)addr; \
55 start &= ~(line_size - 1); \
56 end = ((end + line_size - 1) & ~(line_size - 1)); \
58 invalidate_dcache_range(start, end); \
61 #define invalidate_cache(...)
64 #define TIMEOUT_CNT 1000
66 static int sh_eth_send_common(struct sh_eth_dev *eth, void *packet, int len)
69 struct sh_eth_info *port_info = ð->port_info[eth->port];
71 if (!packet || len > 0xffff) {
72 printf(SHETHER_NAME ": %s: Invalid argument\n", __func__);
77 /* packet must be a 4 byte boundary */
78 if ((uintptr_t)packet & 3) {
79 printf(SHETHER_NAME ": %s: packet not 4 byte aligned\n"
85 /* Update tx descriptor */
86 flush_cache_wback(packet, len);
87 port_info->tx_desc_cur->td2 = ADDR_TO_PHY(packet);
88 port_info->tx_desc_cur->td1 = len << 16;
89 /* Must preserve the end of descriptor list indication */
90 if (port_info->tx_desc_cur->td0 & TD_TDLE)
91 port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP | TD_TDLE;
93 port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP;
95 flush_cache_wback(port_info->tx_desc_cur, sizeof(struct tx_desc_s));
97 /* Restart the transmitter if disabled */
98 if (!(sh_eth_read(port_info, EDTRR) & EDTRR_TRNS))
99 sh_eth_write(port_info, EDTRR_TRNS, EDTRR);
101 /* Wait until packet is transmitted */
102 timeout = TIMEOUT_CNT;
104 invalidate_cache(port_info->tx_desc_cur,
105 sizeof(struct tx_desc_s));
107 } while (port_info->tx_desc_cur->td0 & TD_TACT && timeout--);
110 printf(SHETHER_NAME ": transmit timeout\n");
115 port_info->tx_desc_cur++;
116 if (port_info->tx_desc_cur >= port_info->tx_desc_base + NUM_TX_DESC)
117 port_info->tx_desc_cur = port_info->tx_desc_base;
123 static int sh_eth_recv_start(struct sh_eth_dev *eth)
125 struct sh_eth_info *port_info = ð->port_info[eth->port];
127 /* Check if the rx descriptor is ready */
128 invalidate_cache(port_info->rx_desc_cur, sizeof(struct rx_desc_s));
129 if (port_info->rx_desc_cur->rd0 & RD_RACT)
132 /* Check for errors */
133 if (port_info->rx_desc_cur->rd0 & RD_RFE)
136 return port_info->rx_desc_cur->rd1 & 0xffff;
139 static void sh_eth_recv_finish(struct sh_eth_dev *eth)
141 struct sh_eth_info *port_info = ð->port_info[eth->port];
143 /* Make current descriptor available again */
144 if (port_info->rx_desc_cur->rd0 & RD_RDLE)
145 port_info->rx_desc_cur->rd0 = RD_RACT | RD_RDLE;
147 port_info->rx_desc_cur->rd0 = RD_RACT;
149 flush_cache_wback(port_info->rx_desc_cur,
150 sizeof(struct rx_desc_s));
152 /* Point to the next descriptor */
153 port_info->rx_desc_cur++;
154 if (port_info->rx_desc_cur >=
155 port_info->rx_desc_base + NUM_RX_DESC)
156 port_info->rx_desc_cur = port_info->rx_desc_base;
159 static int sh_eth_reset(struct sh_eth_dev *eth)
161 struct sh_eth_info *port_info = ð->port_info[eth->port];
162 #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
165 /* Start e-dmac transmitter and receiver */
166 sh_eth_write(port_info, EDSR_ENALL, EDSR);
168 /* Perform a software reset and wait for it to complete */
169 sh_eth_write(port_info, EDMR_SRST, EDMR);
170 for (i = 0; i < TIMEOUT_CNT; i++) {
171 if (!(sh_eth_read(port_info, EDMR) & EDMR_SRST))
176 if (i == TIMEOUT_CNT) {
177 printf(SHETHER_NAME ": Software reset timeout\n");
183 sh_eth_write(port_info, sh_eth_read(port_info, EDMR) | EDMR_SRST, EDMR);
185 sh_eth_write(port_info,
186 sh_eth_read(port_info, EDMR) & ~EDMR_SRST, EDMR);
192 static int sh_eth_tx_desc_init(struct sh_eth_dev *eth)
195 u32 alloc_desc_size = NUM_TX_DESC * sizeof(struct tx_desc_s);
196 struct sh_eth_info *port_info = ð->port_info[eth->port];
197 struct tx_desc_s *cur_tx_desc;
200 * Allocate rx descriptors. They must be aligned to size of struct
203 port_info->tx_desc_alloc =
204 memalign(sizeof(struct tx_desc_s), alloc_desc_size);
205 if (!port_info->tx_desc_alloc) {
206 printf(SHETHER_NAME ": memalign failed\n");
211 flush_cache_wback(port_info->tx_desc_alloc, alloc_desc_size);
213 /* Make sure we use a P2 address (non-cacheable) */
214 port_info->tx_desc_base =
215 (struct tx_desc_s *)ADDR_TO_P2((uintptr_t)port_info->tx_desc_alloc);
216 port_info->tx_desc_cur = port_info->tx_desc_base;
218 /* Initialize all descriptors */
219 for (cur_tx_desc = port_info->tx_desc_base, i = 0; i < NUM_TX_DESC;
220 cur_tx_desc++, i++) {
221 cur_tx_desc->td0 = 0x00;
222 cur_tx_desc->td1 = 0x00;
223 cur_tx_desc->td2 = 0x00;
226 /* Mark the end of the descriptors */
228 cur_tx_desc->td0 |= TD_TDLE;
231 * Point the controller to the tx descriptor list. Must use physical
234 sh_eth_write(port_info, ADDR_TO_PHY(port_info->tx_desc_base), TDLAR);
235 #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
236 sh_eth_write(port_info, ADDR_TO_PHY(port_info->tx_desc_base), TDFAR);
237 sh_eth_write(port_info, ADDR_TO_PHY(cur_tx_desc), TDFXR);
238 sh_eth_write(port_info, 0x01, TDFFR);/* Last discriptor bit */
245 static int sh_eth_rx_desc_init(struct sh_eth_dev *eth)
248 u32 alloc_desc_size = NUM_RX_DESC * sizeof(struct rx_desc_s);
249 struct sh_eth_info *port_info = ð->port_info[eth->port];
250 struct rx_desc_s *cur_rx_desc;
254 * Allocate rx descriptors. They must be aligned to size of struct
257 port_info->rx_desc_alloc =
258 memalign(sizeof(struct rx_desc_s), alloc_desc_size);
259 if (!port_info->rx_desc_alloc) {
260 printf(SHETHER_NAME ": memalign failed\n");
265 flush_cache_wback(port_info->rx_desc_alloc, alloc_desc_size);
267 /* Make sure we use a P2 address (non-cacheable) */
268 port_info->rx_desc_base =
269 (struct rx_desc_s *)ADDR_TO_P2((uintptr_t)port_info->rx_desc_alloc);
271 port_info->rx_desc_cur = port_info->rx_desc_base;
274 * Allocate rx data buffers. They must be RX_BUF_ALIGNE_SIZE bytes
275 * aligned and in P2 area.
277 port_info->rx_buf_alloc =
278 memalign(RX_BUF_ALIGNE_SIZE, NUM_RX_DESC * MAX_BUF_SIZE);
279 if (!port_info->rx_buf_alloc) {
280 printf(SHETHER_NAME ": alloc failed\n");
285 port_info->rx_buf_base = (u8 *)ADDR_TO_P2((uintptr_t)port_info->rx_buf_alloc);
287 /* Initialize all descriptors */
288 for (cur_rx_desc = port_info->rx_desc_base,
289 rx_buf = port_info->rx_buf_base, i = 0;
290 i < NUM_RX_DESC; cur_rx_desc++, rx_buf += MAX_BUF_SIZE, i++) {
291 cur_rx_desc->rd0 = RD_RACT;
292 cur_rx_desc->rd1 = MAX_BUF_SIZE << 16;
293 cur_rx_desc->rd2 = (u32)ADDR_TO_PHY(rx_buf);
296 /* Mark the end of the descriptors */
298 cur_rx_desc->rd0 |= RD_RDLE;
300 /* Point the controller to the rx descriptor list */
301 sh_eth_write(port_info, ADDR_TO_PHY(port_info->rx_desc_base), RDLAR);
302 #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
303 sh_eth_write(port_info, ADDR_TO_PHY(port_info->rx_desc_base), RDFAR);
304 sh_eth_write(port_info, ADDR_TO_PHY(cur_rx_desc), RDFXR);
305 sh_eth_write(port_info, RDFFR_RDLF, RDFFR);
311 free(port_info->rx_desc_alloc);
312 port_info->rx_desc_alloc = NULL;
318 static void sh_eth_tx_desc_free(struct sh_eth_dev *eth)
320 struct sh_eth_info *port_info = ð->port_info[eth->port];
322 if (port_info->tx_desc_alloc) {
323 free(port_info->tx_desc_alloc);
324 port_info->tx_desc_alloc = NULL;
328 static void sh_eth_rx_desc_free(struct sh_eth_dev *eth)
330 struct sh_eth_info *port_info = ð->port_info[eth->port];
332 if (port_info->rx_desc_alloc) {
333 free(port_info->rx_desc_alloc);
334 port_info->rx_desc_alloc = NULL;
337 if (port_info->rx_buf_alloc) {
338 free(port_info->rx_buf_alloc);
339 port_info->rx_buf_alloc = NULL;
343 static int sh_eth_desc_init(struct sh_eth_dev *eth)
347 ret = sh_eth_tx_desc_init(eth);
351 ret = sh_eth_rx_desc_init(eth);
357 sh_eth_tx_desc_free(eth);
363 static void sh_eth_write_hwaddr(struct sh_eth_info *port_info,
368 val = (mac[0] << 24) | (mac[1] << 16) | (mac[2] << 8) | mac[3];
369 sh_eth_write(port_info, val, MAHR);
371 val = (mac[4] << 8) | mac[5];
372 sh_eth_write(port_info, val, MALR);
375 static void sh_eth_mac_regs_config(struct sh_eth_dev *eth, unsigned char *mac)
377 struct sh_eth_info *port_info = ð->port_info[eth->port];
380 /* Configure e-dmac registers */
381 edmr = sh_eth_read(port_info, EDMR);
382 edmr &= ~EMDR_DESC_R;
383 edmr |= EMDR_DESC | EDMR_EL;
384 #if defined(CONFIG_R8A77980)
387 sh_eth_write(port_info, edmr, EDMR);
389 sh_eth_write(port_info, 0, EESIPR);
390 sh_eth_write(port_info, 0, TRSCER);
391 sh_eth_write(port_info, 0, TFTR);
392 sh_eth_write(port_info, (FIFO_SIZE_T | FIFO_SIZE_R), FDR);
393 sh_eth_write(port_info, RMCR_RST, RMCR);
394 #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
395 sh_eth_write(port_info, 0, RPADIR);
397 sh_eth_write(port_info, (FIFO_F_D_RFF | FIFO_F_D_RFD), FCFTR);
399 /* Configure e-mac registers */
400 sh_eth_write(port_info, 0, ECSIPR);
402 /* Set Mac address */
403 sh_eth_write_hwaddr(port_info, mac);
405 sh_eth_write(port_info, RFLR_RFL_MIN, RFLR);
406 #if defined(SH_ETH_TYPE_GETHER)
407 sh_eth_write(port_info, 0, PIPR);
409 #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ)
410 sh_eth_write(port_info, APR_AP, APR);
411 sh_eth_write(port_info, MPR_MP, MPR);
412 sh_eth_write(port_info, TPAUSER_TPAUSE, TPAUSER);
415 #if defined(CONFIG_CPU_SH7734) || defined(CONFIG_R8A7740)
416 sh_eth_write(port_info, CONFIG_SH_ETHER_SH7734_MII, RMII_MII);
417 #elif defined(CONFIG_RCAR_GEN2) || defined(CONFIG_R8A77980)
418 sh_eth_write(port_info, sh_eth_read(port_info, RMIIMR) | 0x1, RMIIMR);
422 static int sh_eth_phy_regs_config(struct sh_eth_dev *eth)
424 struct sh_eth_info *port_info = ð->port_info[eth->port];
425 struct phy_device *phy = port_info->phydev;
429 /* Set the transfer speed */
430 if (phy->speed == 100) {
431 printf(SHETHER_NAME ": 100Base/");
432 #if defined(SH_ETH_TYPE_GETHER)
433 sh_eth_write(port_info, GECMR_100B, GECMR);
434 #elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
435 sh_eth_write(port_info, 1, RTRATE);
436 #elif defined(CONFIG_RCAR_GEN2) || defined(CONFIG_R8A77980)
439 } else if (phy->speed == 10) {
440 printf(SHETHER_NAME ": 10Base/");
441 #if defined(SH_ETH_TYPE_GETHER)
442 sh_eth_write(port_info, GECMR_10B, GECMR);
443 #elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752)
444 sh_eth_write(port_info, 0, RTRATE);
447 #if defined(SH_ETH_TYPE_GETHER)
448 else if (phy->speed == 1000) {
449 printf(SHETHER_NAME ": 1000Base/");
450 sh_eth_write(port_info, GECMR_1000B, GECMR);
454 /* Check if full duplex mode is supported by the phy */
457 sh_eth_write(port_info,
458 val | (ECMR_CHG_DM | ECMR_RE | ECMR_TE | ECMR_DM),
462 sh_eth_write(port_info,
463 val | (ECMR_CHG_DM | ECMR_RE | ECMR_TE),
470 static void sh_eth_start(struct sh_eth_dev *eth)
472 struct sh_eth_info *port_info = ð->port_info[eth->port];
475 * Enable the e-dmac receiver only. The transmitter will be enabled when
476 * we have something to transmit
478 sh_eth_write(port_info, EDRRR_R, EDRRR);
481 static void sh_eth_stop(struct sh_eth_dev *eth)
483 struct sh_eth_info *port_info = ð->port_info[eth->port];
485 sh_eth_write(port_info, ~EDRRR_R, EDRRR);
488 static int sh_eth_init_common(struct sh_eth_dev *eth, unsigned char *mac)
492 ret = sh_eth_reset(eth);
496 ret = sh_eth_desc_init(eth);
500 sh_eth_mac_regs_config(eth, mac);
505 static int sh_eth_start_common(struct sh_eth_dev *eth)
507 struct sh_eth_info *port_info = ð->port_info[eth->port];
510 ret = phy_startup(port_info->phydev);
512 printf(SHETHER_NAME ": phy startup failure\n");
516 ret = sh_eth_phy_regs_config(eth);
525 #ifndef CONFIG_DM_ETH
526 static int sh_eth_phy_config_legacy(struct sh_eth_dev *eth)
529 struct sh_eth_info *port_info = ð->port_info[eth->port];
530 struct eth_device *dev = port_info->dev;
531 struct phy_device *phydev;
533 phydev = phy_connect(
534 miiphy_get_dev_by_name(dev->name),
535 port_info->phy_addr, dev, CONFIG_SH_ETHER_PHY_MODE);
536 port_info->phydev = phydev;
542 static int sh_eth_send_legacy(struct eth_device *dev, void *packet, int len)
544 struct sh_eth_dev *eth = dev->priv;
546 return sh_eth_send_common(eth, packet, len);
549 static int sh_eth_recv_common(struct sh_eth_dev *eth)
552 struct sh_eth_info *port_info = ð->port_info[eth->port];
553 uchar *packet = (uchar *)ADDR_TO_P2(port_info->rx_desc_cur->rd2);
555 len = sh_eth_recv_start(eth);
557 invalidate_cache(packet, len);
558 net_process_received_packet(packet, len);
559 sh_eth_recv_finish(eth);
563 /* Restart the receiver if disabled */
564 if (!(sh_eth_read(port_info, EDRRR) & EDRRR_R))
565 sh_eth_write(port_info, EDRRR_R, EDRRR);
570 static int sh_eth_recv_legacy(struct eth_device *dev)
572 struct sh_eth_dev *eth = dev->priv;
574 return sh_eth_recv_common(eth);
577 static int sh_eth_init_legacy(struct eth_device *dev, bd_t *bd)
579 struct sh_eth_dev *eth = dev->priv;
582 ret = sh_eth_init_common(eth, dev->enetaddr);
586 ret = sh_eth_phy_config_legacy(eth);
588 printf(SHETHER_NAME ": phy config timeout\n");
592 ret = sh_eth_start_common(eth);
599 sh_eth_tx_desc_free(eth);
600 sh_eth_rx_desc_free(eth);
604 void sh_eth_halt_legacy(struct eth_device *dev)
606 struct sh_eth_dev *eth = dev->priv;
611 int sh_eth_initialize(bd_t *bd)
614 struct sh_eth_dev *eth = NULL;
615 struct eth_device *dev = NULL;
616 struct mii_dev *mdiodev;
618 eth = (struct sh_eth_dev *)malloc(sizeof(struct sh_eth_dev));
620 printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
625 dev = (struct eth_device *)malloc(sizeof(struct eth_device));
627 printf(SHETHER_NAME ": %s: malloc failed\n", __func__);
631 memset(dev, 0, sizeof(struct eth_device));
632 memset(eth, 0, sizeof(struct sh_eth_dev));
634 eth->port = CONFIG_SH_ETHER_USE_PORT;
635 eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
636 eth->port_info[eth->port].iobase =
637 (void __iomem *)(BASE_IO_ADDR + 0x800 * eth->port);
639 dev->priv = (void *)eth;
641 dev->init = sh_eth_init_legacy;
642 dev->halt = sh_eth_halt_legacy;
643 dev->send = sh_eth_send_legacy;
644 dev->recv = sh_eth_recv_legacy;
645 eth->port_info[eth->port].dev = dev;
647 strcpy(dev->name, SHETHER_NAME);
649 /* Register Device to EtherNet subsystem */
652 bb_miiphy_buses[0].priv = eth;
653 mdiodev = mdio_alloc();
656 strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
657 mdiodev->read = bb_miiphy_read;
658 mdiodev->write = bb_miiphy_write;
660 ret = mdio_register(mdiodev);
664 if (!eth_env_get_enetaddr("ethaddr", dev->enetaddr))
665 puts("Please set MAC address\n");
676 printf(SHETHER_NAME ": Failed\n");
680 #else /* CONFIG_DM_ETH */
682 struct sh_ether_priv {
683 struct sh_eth_dev shdev;
688 struct gpio_desc reset_gpio;
691 static int sh_ether_send(struct udevice *dev, void *packet, int len)
693 struct sh_ether_priv *priv = dev_get_priv(dev);
694 struct sh_eth_dev *eth = &priv->shdev;
696 return sh_eth_send_common(eth, packet, len);
699 static int sh_ether_recv(struct udevice *dev, int flags, uchar **packetp)
701 struct sh_ether_priv *priv = dev_get_priv(dev);
702 struct sh_eth_dev *eth = &priv->shdev;
703 struct sh_eth_info *port_info = ð->port_info[eth->port];
704 uchar *packet = (uchar *)ADDR_TO_P2((uintptr_t)port_info->rx_desc_cur->rd2);
707 len = sh_eth_recv_start(eth);
709 invalidate_cache(packet, len);
716 /* Restart the receiver if disabled */
717 if (!(sh_eth_read(port_info, EDRRR) & EDRRR_R))
718 sh_eth_write(port_info, EDRRR_R, EDRRR);
724 static int sh_ether_free_pkt(struct udevice *dev, uchar *packet, int length)
726 struct sh_ether_priv *priv = dev_get_priv(dev);
727 struct sh_eth_dev *eth = &priv->shdev;
728 struct sh_eth_info *port_info = ð->port_info[eth->port];
730 sh_eth_recv_finish(eth);
732 /* Restart the receiver if disabled */
733 if (!(sh_eth_read(port_info, EDRRR) & EDRRR_R))
734 sh_eth_write(port_info, EDRRR_R, EDRRR);
739 static int sh_ether_write_hwaddr(struct udevice *dev)
741 struct sh_ether_priv *priv = dev_get_priv(dev);
742 struct sh_eth_dev *eth = &priv->shdev;
743 struct sh_eth_info *port_info = ð->port_info[eth->port];
744 struct eth_pdata *pdata = dev_get_platdata(dev);
746 sh_eth_write_hwaddr(port_info, pdata->enetaddr);
751 static int sh_eth_phy_config(struct udevice *dev)
753 struct sh_ether_priv *priv = dev_get_priv(dev);
754 struct eth_pdata *pdata = dev_get_platdata(dev);
755 struct sh_eth_dev *eth = &priv->shdev;
757 struct sh_eth_info *port_info = ð->port_info[eth->port];
758 struct phy_device *phydev;
759 int mask = 0xffffffff;
761 phydev = phy_find_by_mask(priv->bus, mask, pdata->phy_interface);
765 phy_connect_dev(phydev, dev);
767 port_info->phydev = phydev;
773 static int sh_ether_start(struct udevice *dev)
775 struct sh_ether_priv *priv = dev_get_priv(dev);
776 struct eth_pdata *pdata = dev_get_platdata(dev);
777 struct sh_eth_dev *eth = &priv->shdev;
780 ret = sh_eth_init_common(eth, pdata->enetaddr);
784 ret = sh_eth_start_common(eth);
791 sh_eth_tx_desc_free(eth);
792 sh_eth_rx_desc_free(eth);
796 static void sh_ether_stop(struct udevice *dev)
798 struct sh_ether_priv *priv = dev_get_priv(dev);
799 struct sh_eth_dev *eth = &priv->shdev;
800 struct sh_eth_info *port_info = ð->port_info[eth->port];
802 phy_shutdown(port_info->phydev);
803 sh_eth_stop(&priv->shdev);
806 static int sh_ether_probe(struct udevice *udev)
808 struct eth_pdata *pdata = dev_get_platdata(udev);
809 struct sh_ether_priv *priv = dev_get_priv(udev);
810 struct sh_eth_dev *eth = &priv->shdev;
811 struct ofnode_phandle_args phandle_args;
812 struct mii_dev *mdiodev;
815 priv->iobase = pdata->iobase;
817 #if CONFIG_IS_ENABLED(CLK)
818 ret = clk_get_by_index(udev, 0, &priv->clk);
823 ret = dev_read_phandle_with_args(udev, "phy-handle", NULL, 0, 0, &phandle_args);
825 gpio_request_by_name_nodev(phandle_args.node, "reset-gpios", 0,
826 &priv->reset_gpio, GPIOD_IS_OUT);
829 if (!dm_gpio_is_valid(&priv->reset_gpio)) {
830 gpio_request_by_name(udev, "reset-gpios", 0, &priv->reset_gpio,
834 mdiodev = mdio_alloc();
840 mdiodev->read = bb_miiphy_read;
841 mdiodev->write = bb_miiphy_write;
842 bb_miiphy_buses[0].priv = eth;
843 snprintf(mdiodev->name, sizeof(mdiodev->name), udev->name);
845 ret = mdio_register(mdiodev);
847 goto err_mdio_register;
849 priv->bus = miiphy_get_dev_by_name(udev->name);
851 eth->port = CONFIG_SH_ETHER_USE_PORT;
852 eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
853 eth->port_info[eth->port].iobase =
854 (void __iomem *)(uintptr_t)(BASE_IO_ADDR + 0x800 * eth->port);
856 #if CONFIG_IS_ENABLED(CLK)
857 ret = clk_enable(&priv->clk);
859 goto err_mdio_register;
862 ret = sh_eth_phy_config(udev);
864 printf(SHETHER_NAME ": phy config timeout\n");
871 #if CONFIG_IS_ENABLED(CLK)
872 clk_disable(&priv->clk);
879 static int sh_ether_remove(struct udevice *udev)
881 struct sh_ether_priv *priv = dev_get_priv(udev);
882 struct sh_eth_dev *eth = &priv->shdev;
883 struct sh_eth_info *port_info = ð->port_info[eth->port];
885 #if CONFIG_IS_ENABLED(CLK)
886 clk_disable(&priv->clk);
888 free(port_info->phydev);
889 mdio_unregister(priv->bus);
890 mdio_free(priv->bus);
892 if (dm_gpio_is_valid(&priv->reset_gpio))
893 dm_gpio_free(udev, &priv->reset_gpio);
898 static const struct eth_ops sh_ether_ops = {
899 .start = sh_ether_start,
900 .send = sh_ether_send,
901 .recv = sh_ether_recv,
902 .free_pkt = sh_ether_free_pkt,
903 .stop = sh_ether_stop,
904 .write_hwaddr = sh_ether_write_hwaddr,
907 int sh_ether_ofdata_to_platdata(struct udevice *dev)
909 struct eth_pdata *pdata = dev_get_platdata(dev);
910 const char *phy_mode;
914 pdata->iobase = devfdt_get_addr(dev);
915 pdata->phy_interface = -1;
916 phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
919 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
920 if (pdata->phy_interface == -1) {
921 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
925 pdata->max_speed = 1000;
926 cell = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "max-speed", NULL);
928 pdata->max_speed = fdt32_to_cpu(*cell);
930 sprintf(bb_miiphy_buses[0].name, dev->name);
935 static const struct udevice_id sh_ether_ids[] = {
936 { .compatible = "renesas,ether-r7s72100" },
937 { .compatible = "renesas,ether-r8a7790" },
938 { .compatible = "renesas,ether-r8a7791" },
939 { .compatible = "renesas,ether-r8a7793" },
940 { .compatible = "renesas,ether-r8a7794" },
941 { .compatible = "renesas,gether-r8a77980" },
945 U_BOOT_DRIVER(eth_sh_ether) = {
948 .of_match = sh_ether_ids,
949 .ofdata_to_platdata = sh_ether_ofdata_to_platdata,
950 .probe = sh_ether_probe,
951 .remove = sh_ether_remove,
952 .ops = &sh_ether_ops,
953 .priv_auto_alloc_size = sizeof(struct sh_ether_priv),
954 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
955 .flags = DM_FLAG_ALLOC_PRIV_DMA,
959 /******* for bb_miiphy *******/
960 static int sh_eth_bb_init(struct bb_miiphy_bus *bus)
965 static int sh_eth_bb_mdio_active(struct bb_miiphy_bus *bus)
967 struct sh_eth_dev *eth = bus->priv;
968 struct sh_eth_info *port_info = ð->port_info[eth->port];
970 sh_eth_write(port_info, sh_eth_read(port_info, PIR) | PIR_MMD, PIR);
975 static int sh_eth_bb_mdio_tristate(struct bb_miiphy_bus *bus)
977 struct sh_eth_dev *eth = bus->priv;
978 struct sh_eth_info *port_info = ð->port_info[eth->port];
980 sh_eth_write(port_info, sh_eth_read(port_info, PIR) & ~PIR_MMD, PIR);
985 static int sh_eth_bb_set_mdio(struct bb_miiphy_bus *bus, int v)
987 struct sh_eth_dev *eth = bus->priv;
988 struct sh_eth_info *port_info = ð->port_info[eth->port];
991 sh_eth_write(port_info,
992 sh_eth_read(port_info, PIR) | PIR_MDO, PIR);
994 sh_eth_write(port_info,
995 sh_eth_read(port_info, PIR) & ~PIR_MDO, PIR);
1000 static int sh_eth_bb_get_mdio(struct bb_miiphy_bus *bus, int *v)
1002 struct sh_eth_dev *eth = bus->priv;
1003 struct sh_eth_info *port_info = ð->port_info[eth->port];
1005 *v = (sh_eth_read(port_info, PIR) & PIR_MDI) >> 3;
1010 static int sh_eth_bb_set_mdc(struct bb_miiphy_bus *bus, int v)
1012 struct sh_eth_dev *eth = bus->priv;
1013 struct sh_eth_info *port_info = ð->port_info[eth->port];
1016 sh_eth_write(port_info,
1017 sh_eth_read(port_info, PIR) | PIR_MDC, PIR);
1019 sh_eth_write(port_info,
1020 sh_eth_read(port_info, PIR) & ~PIR_MDC, PIR);
1025 static int sh_eth_bb_delay(struct bb_miiphy_bus *bus)
1032 struct bb_miiphy_bus bb_miiphy_buses[] = {
1035 .init = sh_eth_bb_init,
1036 .mdio_active = sh_eth_bb_mdio_active,
1037 .mdio_tristate = sh_eth_bb_mdio_tristate,
1038 .set_mdio = sh_eth_bb_set_mdio,
1039 .get_mdio = sh_eth_bb_get_mdio,
1040 .set_mdc = sh_eth_bb_set_mdc,
1041 .delay = sh_eth_bb_delay,
1045 int bb_miiphy_buses_num = ARRAY_SIZE(bb_miiphy_buses);