2 * SuperH Ethernet device driver
4 * Copyright (C) 2006-2012 Nobuhiro Iwamatsu
5 * Copyright (C) 2008-2013 Renesas Solutions Corp.
6 * Copyright (C) 2013 Cogent Embedded, Inc.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20 * The full GNU General Public License is included in this distribution in
21 * the file called "COPYING".
24 #include <linux/init.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/spinlock.h>
28 #include <linux/interrupt.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/etherdevice.h>
31 #include <linux/delay.h>
32 #include <linux/platform_device.h>
33 #include <linux/mdio-bitbang.h>
34 #include <linux/netdevice.h>
35 #include <linux/phy.h>
36 #include <linux/cache.h>
38 #include <linux/pm_runtime.h>
39 #include <linux/slab.h>
40 #include <linux/ethtool.h>
41 #include <linux/if_vlan.h>
42 #include <linux/clk.h>
43 #include <linux/sh_eth.h>
47 #define SH_ETH_DEF_MSG_ENABLE \
53 static const u16 sh_eth_offset_gigabit[SH_ETH_MAX_REGISTER_OFFSET] = {
107 [TSU_CTRST] = 0x0004,
108 [TSU_FWEN0] = 0x0010,
109 [TSU_FWEN1] = 0x0014,
111 [TSU_BSYSL0] = 0x0020,
112 [TSU_BSYSL1] = 0x0024,
113 [TSU_PRISL0] = 0x0028,
114 [TSU_PRISL1] = 0x002c,
115 [TSU_FWSL0] = 0x0030,
116 [TSU_FWSL1] = 0x0034,
117 [TSU_FWSLC] = 0x0038,
118 [TSU_QTAG0] = 0x0040,
119 [TSU_QTAG1] = 0x0044,
121 [TSU_FWINMK] = 0x0054,
122 [TSU_ADQT0] = 0x0048,
123 [TSU_ADQT1] = 0x004c,
124 [TSU_VTAG0] = 0x0058,
125 [TSU_VTAG1] = 0x005c,
126 [TSU_ADSBSY] = 0x0060,
128 [TSU_POST1] = 0x0070,
129 [TSU_POST2] = 0x0074,
130 [TSU_POST3] = 0x0078,
131 [TSU_POST4] = 0x007c,
132 [TSU_ADRH0] = 0x0100,
133 [TSU_ADRL0] = 0x0104,
134 [TSU_ADRH31] = 0x01f8,
135 [TSU_ADRL31] = 0x01fc,
151 static const u16 sh_eth_offset_fast_rcar[SH_ETH_MAX_REGISTER_OFFSET] = {
196 static const u16 sh_eth_offset_fast_sh4[SH_ETH_MAX_REGISTER_OFFSET] = {
248 static const u16 sh_eth_offset_fast_sh3_sh2[SH_ETH_MAX_REGISTER_OFFSET] = {
274 [TSU_CTRST] = 0x0004,
275 [TSU_FWEN0] = 0x0010,
276 [TSU_FWEN1] = 0x0014,
278 [TSU_BSYSL0] = 0x0020,
279 [TSU_BSYSL1] = 0x0024,
280 [TSU_PRISL0] = 0x0028,
281 [TSU_PRISL1] = 0x002c,
282 [TSU_FWSL0] = 0x0030,
283 [TSU_FWSL1] = 0x0034,
284 [TSU_FWSLC] = 0x0038,
285 [TSU_QTAGM0] = 0x0040,
286 [TSU_QTAGM1] = 0x0044,
287 [TSU_ADQT0] = 0x0048,
288 [TSU_ADQT1] = 0x004c,
290 [TSU_FWINMK] = 0x0054,
291 [TSU_ADSBSY] = 0x0060,
293 [TSU_POST1] = 0x0070,
294 [TSU_POST2] = 0x0074,
295 [TSU_POST3] = 0x0078,
296 [TSU_POST4] = 0x007c,
311 [TSU_ADRH0] = 0x0100,
312 [TSU_ADRL0] = 0x0104,
313 [TSU_ADRL31] = 0x01fc,
316 static int sh_eth_is_gether(struct sh_eth_private *mdp)
318 if (mdp->reg_offset == sh_eth_offset_gigabit)
324 static void __maybe_unused sh_eth_select_mii(struct net_device *ndev)
327 struct sh_eth_private *mdp = netdev_priv(ndev);
329 switch (mdp->phy_interface) {
330 case PHY_INTERFACE_MODE_GMII:
333 case PHY_INTERFACE_MODE_MII:
336 case PHY_INTERFACE_MODE_RMII:
340 pr_warn("PHY interface mode was not setup. Set to MII.\n");
345 sh_eth_write(ndev, value, RMII_MII);
348 static void __maybe_unused sh_eth_set_duplex(struct net_device *ndev)
350 struct sh_eth_private *mdp = netdev_priv(ndev);
352 if (mdp->duplex) /* Full */
353 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
355 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
358 /* There is CPU dependent code */
359 #if defined(CONFIG_ARCH_R8A7778) || defined(CONFIG_ARCH_R8A7779)
360 static void sh_eth_set_rate(struct net_device *ndev)
362 struct sh_eth_private *mdp = netdev_priv(ndev);
364 switch (mdp->speed) {
365 case 10: /* 10BASE */
366 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_ELB, ECMR);
368 case 100:/* 100BASE */
369 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_ELB, ECMR);
377 static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
378 .set_duplex = sh_eth_set_duplex,
379 .set_rate = sh_eth_set_rate,
381 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
382 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
383 .eesipr_value = 0x01ff009f,
385 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
386 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE |
387 EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI,
388 .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE,
397 static void sh_eth_set_rate_sh7724(struct net_device *ndev)
399 struct sh_eth_private *mdp = netdev_priv(ndev);
401 switch (mdp->speed) {
402 case 10: /* 10BASE */
403 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_RTM, ECMR);
405 case 100:/* 100BASE */
406 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_RTM, ECMR);
414 static struct sh_eth_cpu_data sh7724_data = {
415 .set_duplex = sh_eth_set_duplex,
416 .set_rate = sh_eth_set_rate_sh7724,
418 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
419 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
420 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x01ff009f,
422 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
423 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE |
424 EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI,
425 .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE,
432 .rpadir_value = 0x00020000, /* NET_IP_ALIGN assumed to be 2 */
435 static void sh_eth_set_rate_sh7757(struct net_device *ndev)
437 struct sh_eth_private *mdp = netdev_priv(ndev);
439 switch (mdp->speed) {
440 case 10: /* 10BASE */
441 sh_eth_write(ndev, 0, RTRATE);
443 case 100:/* 100BASE */
444 sh_eth_write(ndev, 1, RTRATE);
452 static struct sh_eth_cpu_data sh7757_data = {
453 .set_duplex = sh_eth_set_duplex,
454 .set_rate = sh_eth_set_rate_sh7757,
456 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
457 .rmcr_value = 0x00000001,
459 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
460 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE |
461 EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI,
462 .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE,
464 .irq_flags = IRQF_SHARED,
471 .rpadir_value = 2 << 16,
474 #define SH_GIGA_ETH_BASE 0xfee00000
475 #define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8)
476 #define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0)
477 static void sh_eth_chip_reset_giga(struct net_device *ndev)
480 unsigned long mahr[2], malr[2];
482 /* save MAHR and MALR */
483 for (i = 0; i < 2; i++) {
484 malr[i] = ioread32((void *)GIGA_MALR(i));
485 mahr[i] = ioread32((void *)GIGA_MAHR(i));
489 iowrite32(ARSTR_ARSTR, (void *)(SH_GIGA_ETH_BASE + 0x1800));
492 /* restore MAHR and MALR */
493 for (i = 0; i < 2; i++) {
494 iowrite32(malr[i], (void *)GIGA_MALR(i));
495 iowrite32(mahr[i], (void *)GIGA_MAHR(i));
499 static void sh_eth_set_rate_giga(struct net_device *ndev)
501 struct sh_eth_private *mdp = netdev_priv(ndev);
503 switch (mdp->speed) {
504 case 10: /* 10BASE */
505 sh_eth_write(ndev, 0x00000000, GECMR);
507 case 100:/* 100BASE */
508 sh_eth_write(ndev, 0x00000010, GECMR);
510 case 1000: /* 1000BASE */
511 sh_eth_write(ndev, 0x00000020, GECMR);
518 /* SH7757(GETHERC) */
519 static struct sh_eth_cpu_data sh7757_data_giga = {
520 .chip_reset = sh_eth_chip_reset_giga,
521 .set_duplex = sh_eth_set_duplex,
522 .set_rate = sh_eth_set_rate_giga,
524 .ecsr_value = ECSR_ICD | ECSR_MPD,
525 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
526 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
528 .tx_check = EESR_TC1 | EESR_FTC,
529 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
530 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
532 .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
534 .fdr_value = 0x0000072f,
535 .rmcr_value = 0x00000001,
537 .irq_flags = IRQF_SHARED,
544 .rpadir_value = 2 << 16,
550 static void sh_eth_chip_reset(struct net_device *ndev)
552 struct sh_eth_private *mdp = netdev_priv(ndev);
555 sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
559 static void sh_eth_set_rate_gether(struct net_device *ndev)
561 struct sh_eth_private *mdp = netdev_priv(ndev);
563 switch (mdp->speed) {
564 case 10: /* 10BASE */
565 sh_eth_write(ndev, GECMR_10, GECMR);
567 case 100:/* 100BASE */
568 sh_eth_write(ndev, GECMR_100, GECMR);
570 case 1000: /* 1000BASE */
571 sh_eth_write(ndev, GECMR_1000, GECMR);
579 static struct sh_eth_cpu_data sh7734_data = {
580 .chip_reset = sh_eth_chip_reset,
581 .set_duplex = sh_eth_set_duplex,
582 .set_rate = sh_eth_set_rate_gether,
584 .ecsr_value = ECSR_ICD | ECSR_MPD,
585 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
586 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
588 .tx_check = EESR_TC1 | EESR_FTC,
589 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
590 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
592 .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
608 static struct sh_eth_cpu_data sh7763_data = {
609 .chip_reset = sh_eth_chip_reset,
610 .set_duplex = sh_eth_set_duplex,
611 .set_rate = sh_eth_set_rate_gether,
613 .ecsr_value = ECSR_ICD | ECSR_MPD,
614 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
615 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
617 .tx_check = EESR_TC1 | EESR_FTC,
618 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
619 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
621 .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
632 .irq_flags = IRQF_SHARED,
635 static void sh_eth_chip_reset_r8a7740(struct net_device *ndev)
637 struct sh_eth_private *mdp = netdev_priv(ndev);
640 sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
643 sh_eth_select_mii(ndev);
647 static struct sh_eth_cpu_data r8a7740_data = {
648 .chip_reset = sh_eth_chip_reset_r8a7740,
649 .set_duplex = sh_eth_set_duplex,
650 .set_rate = sh_eth_set_rate_gether,
652 .ecsr_value = ECSR_ICD | ECSR_MPD,
653 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
654 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
656 .tx_check = EESR_TC1 | EESR_FTC,
657 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
658 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
660 .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
674 static struct sh_eth_cpu_data sh7619_data = {
675 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
683 static struct sh_eth_cpu_data sh771x_data = {
684 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
688 static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data *cd)
691 cd->ecsr_value = DEFAULT_ECSR_INIT;
693 if (!cd->ecsipr_value)
694 cd->ecsipr_value = DEFAULT_ECSIPR_INIT;
696 if (!cd->fcftr_value)
697 cd->fcftr_value = DEFAULT_FIFO_F_D_RFF | \
698 DEFAULT_FIFO_F_D_RFD;
701 cd->fdr_value = DEFAULT_FDR_INIT;
704 cd->rmcr_value = DEFAULT_RMCR_VALUE;
707 cd->tx_check = DEFAULT_TX_CHECK;
709 if (!cd->eesr_err_check)
710 cd->eesr_err_check = DEFAULT_EESR_ERR_CHECK;
712 if (!cd->tx_error_check)
713 cd->tx_error_check = DEFAULT_TX_ERROR_CHECK;
716 static int sh_eth_check_reset(struct net_device *ndev)
722 if (!(sh_eth_read(ndev, EDMR) & 0x3))
728 pr_err("Device reset fail\n");
734 static int sh_eth_reset(struct net_device *ndev)
736 struct sh_eth_private *mdp = netdev_priv(ndev);
739 if (sh_eth_is_gether(mdp)) {
740 sh_eth_write(ndev, EDSR_ENALL, EDSR);
741 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_GETHER,
744 ret = sh_eth_check_reset(ndev);
749 sh_eth_write(ndev, 0x0, TDLAR);
750 sh_eth_write(ndev, 0x0, TDFAR);
751 sh_eth_write(ndev, 0x0, TDFXR);
752 sh_eth_write(ndev, 0x0, TDFFR);
753 sh_eth_write(ndev, 0x0, RDLAR);
754 sh_eth_write(ndev, 0x0, RDFAR);
755 sh_eth_write(ndev, 0x0, RDFXR);
756 sh_eth_write(ndev, 0x0, RDFFR);
758 /* Reset HW CRC register */
760 sh_eth_write(ndev, 0x0, CSMR);
762 /* Select MII mode */
763 if (mdp->cd->select_mii)
764 sh_eth_select_mii(ndev);
766 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_ETHER,
769 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) & ~EDMR_SRST_ETHER,
777 #if defined(CONFIG_CPU_SH4) || defined(CONFIG_ARCH_SHMOBILE)
778 static void sh_eth_set_receive_align(struct sk_buff *skb)
782 reserve = SH4_SKB_RX_ALIGN - ((u32)skb->data & (SH4_SKB_RX_ALIGN - 1));
784 skb_reserve(skb, reserve);
787 static void sh_eth_set_receive_align(struct sk_buff *skb)
789 skb_reserve(skb, SH2_SH3_SKB_RX_ALIGN);
794 /* CPU <-> EDMAC endian convert */
795 static inline __u32 cpu_to_edmac(struct sh_eth_private *mdp, u32 x)
797 switch (mdp->edmac_endian) {
798 case EDMAC_LITTLE_ENDIAN:
799 return cpu_to_le32(x);
800 case EDMAC_BIG_ENDIAN:
801 return cpu_to_be32(x);
806 static inline __u32 edmac_to_cpu(struct sh_eth_private *mdp, u32 x)
808 switch (mdp->edmac_endian) {
809 case EDMAC_LITTLE_ENDIAN:
810 return le32_to_cpu(x);
811 case EDMAC_BIG_ENDIAN:
812 return be32_to_cpu(x);
818 * Program the hardware MAC address from dev->dev_addr.
820 static void update_mac_address(struct net_device *ndev)
823 (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
824 (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
826 (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
830 * Get MAC address from SuperH MAC address register
832 * SuperH's Ethernet device doesn't have 'ROM' to MAC address.
833 * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g).
834 * When you want use this device, you must set MAC address in bootloader.
837 static void read_mac_address(struct net_device *ndev, unsigned char *mac)
839 if (mac[0] || mac[1] || mac[2] || mac[3] || mac[4] || mac[5]) {
840 memcpy(ndev->dev_addr, mac, 6);
842 ndev->dev_addr[0] = (sh_eth_read(ndev, MAHR) >> 24);
843 ndev->dev_addr[1] = (sh_eth_read(ndev, MAHR) >> 16) & 0xFF;
844 ndev->dev_addr[2] = (sh_eth_read(ndev, MAHR) >> 8) & 0xFF;
845 ndev->dev_addr[3] = (sh_eth_read(ndev, MAHR) & 0xFF);
846 ndev->dev_addr[4] = (sh_eth_read(ndev, MALR) >> 8) & 0xFF;
847 ndev->dev_addr[5] = (sh_eth_read(ndev, MALR) & 0xFF);
851 static unsigned long sh_eth_get_edtrr_trns(struct sh_eth_private *mdp)
853 if (sh_eth_is_gether(mdp))
854 return EDTRR_TRNS_GETHER;
856 return EDTRR_TRNS_ETHER;
860 void (*set_gate)(void *addr);
861 struct mdiobb_ctrl ctrl;
863 u32 mmd_msk;/* MMD */
870 static void bb_set(void *addr, u32 msk)
872 iowrite32(ioread32(addr) | msk, addr);
876 static void bb_clr(void *addr, u32 msk)
878 iowrite32((ioread32(addr) & ~msk), addr);
882 static int bb_read(void *addr, u32 msk)
884 return (ioread32(addr) & msk) != 0;
887 /* Data I/O pin control */
888 static void sh_mmd_ctrl(struct mdiobb_ctrl *ctrl, int bit)
890 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
892 if (bitbang->set_gate)
893 bitbang->set_gate(bitbang->addr);
896 bb_set(bitbang->addr, bitbang->mmd_msk);
898 bb_clr(bitbang->addr, bitbang->mmd_msk);
902 static void sh_set_mdio(struct mdiobb_ctrl *ctrl, int bit)
904 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
906 if (bitbang->set_gate)
907 bitbang->set_gate(bitbang->addr);
910 bb_set(bitbang->addr, bitbang->mdo_msk);
912 bb_clr(bitbang->addr, bitbang->mdo_msk);
916 static int sh_get_mdio(struct mdiobb_ctrl *ctrl)
918 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
920 if (bitbang->set_gate)
921 bitbang->set_gate(bitbang->addr);
923 return bb_read(bitbang->addr, bitbang->mdi_msk);
926 /* MDC pin control */
927 static void sh_mdc_ctrl(struct mdiobb_ctrl *ctrl, int bit)
929 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
931 if (bitbang->set_gate)
932 bitbang->set_gate(bitbang->addr);
935 bb_set(bitbang->addr, bitbang->mdc_msk);
937 bb_clr(bitbang->addr, bitbang->mdc_msk);
940 /* mdio bus control struct */
941 static struct mdiobb_ops bb_ops = {
942 .owner = THIS_MODULE,
943 .set_mdc = sh_mdc_ctrl,
944 .set_mdio_dir = sh_mmd_ctrl,
945 .set_mdio_data = sh_set_mdio,
946 .get_mdio_data = sh_get_mdio,
949 /* free skb and descriptor buffer */
950 static void sh_eth_ring_free(struct net_device *ndev)
952 struct sh_eth_private *mdp = netdev_priv(ndev);
955 /* Free Rx skb ringbuffer */
956 if (mdp->rx_skbuff) {
957 for (i = 0; i < mdp->num_rx_ring; i++) {
958 if (mdp->rx_skbuff[i])
959 dev_kfree_skb(mdp->rx_skbuff[i]);
962 kfree(mdp->rx_skbuff);
963 mdp->rx_skbuff = NULL;
965 /* Free Tx skb ringbuffer */
966 if (mdp->tx_skbuff) {
967 for (i = 0; i < mdp->num_tx_ring; i++) {
968 if (mdp->tx_skbuff[i])
969 dev_kfree_skb(mdp->tx_skbuff[i]);
972 kfree(mdp->tx_skbuff);
973 mdp->tx_skbuff = NULL;
976 /* format skb and descriptor buffer */
977 static void sh_eth_ring_format(struct net_device *ndev)
979 struct sh_eth_private *mdp = netdev_priv(ndev);
982 struct sh_eth_rxdesc *rxdesc = NULL;
983 struct sh_eth_txdesc *txdesc = NULL;
984 int rx_ringsize = sizeof(*rxdesc) * mdp->num_rx_ring;
985 int tx_ringsize = sizeof(*txdesc) * mdp->num_tx_ring;
987 mdp->cur_rx = mdp->cur_tx = 0;
988 mdp->dirty_rx = mdp->dirty_tx = 0;
990 memset(mdp->rx_ring, 0, rx_ringsize);
992 /* build Rx ring buffer */
993 for (i = 0; i < mdp->num_rx_ring; i++) {
995 mdp->rx_skbuff[i] = NULL;
996 skb = netdev_alloc_skb(ndev, mdp->rx_buf_sz);
997 mdp->rx_skbuff[i] = skb;
1000 dma_map_single(&ndev->dev, skb->data, mdp->rx_buf_sz,
1002 sh_eth_set_receive_align(skb);
1005 rxdesc = &mdp->rx_ring[i];
1006 rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4));
1007 rxdesc->status = cpu_to_edmac(mdp, RD_RACT | RD_RFP);
1009 /* The size of the buffer is 16 byte boundary. */
1010 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
1011 /* Rx descriptor address set */
1013 sh_eth_write(ndev, mdp->rx_desc_dma, RDLAR);
1014 if (sh_eth_is_gether(mdp))
1015 sh_eth_write(ndev, mdp->rx_desc_dma, RDFAR);
1019 mdp->dirty_rx = (u32) (i - mdp->num_rx_ring);
1021 /* Mark the last entry as wrapping the ring. */
1022 rxdesc->status |= cpu_to_edmac(mdp, RD_RDEL);
1024 memset(mdp->tx_ring, 0, tx_ringsize);
1026 /* build Tx ring buffer */
1027 for (i = 0; i < mdp->num_tx_ring; i++) {
1028 mdp->tx_skbuff[i] = NULL;
1029 txdesc = &mdp->tx_ring[i];
1030 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
1031 txdesc->buffer_length = 0;
1033 /* Tx descriptor address set */
1034 sh_eth_write(ndev, mdp->tx_desc_dma, TDLAR);
1035 if (sh_eth_is_gether(mdp))
1036 sh_eth_write(ndev, mdp->tx_desc_dma, TDFAR);
1040 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
1043 /* Get skb and descriptor buffer */
1044 static int sh_eth_ring_init(struct net_device *ndev)
1046 struct sh_eth_private *mdp = netdev_priv(ndev);
1047 int rx_ringsize, tx_ringsize, ret = 0;
1050 * +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
1051 * card needs room to do 8 byte alignment, +2 so we can reserve
1052 * the first 2 bytes, and +16 gets room for the status word from the
1055 mdp->rx_buf_sz = (ndev->mtu <= 1492 ? PKT_BUF_SZ :
1056 (((ndev->mtu + 26 + 7) & ~7) + 2 + 16));
1057 if (mdp->cd->rpadir)
1058 mdp->rx_buf_sz += NET_IP_ALIGN;
1060 /* Allocate RX and TX skb rings */
1061 mdp->rx_skbuff = kmalloc_array(mdp->num_rx_ring,
1062 sizeof(*mdp->rx_skbuff), GFP_KERNEL);
1063 if (!mdp->rx_skbuff) {
1068 mdp->tx_skbuff = kmalloc_array(mdp->num_tx_ring,
1069 sizeof(*mdp->tx_skbuff), GFP_KERNEL);
1070 if (!mdp->tx_skbuff) {
1075 /* Allocate all Rx descriptors. */
1076 rx_ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
1077 mdp->rx_ring = dma_alloc_coherent(NULL, rx_ringsize, &mdp->rx_desc_dma,
1079 if (!mdp->rx_ring) {
1081 goto desc_ring_free;
1086 /* Allocate all Tx descriptors. */
1087 tx_ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
1088 mdp->tx_ring = dma_alloc_coherent(NULL, tx_ringsize, &mdp->tx_desc_dma,
1090 if (!mdp->tx_ring) {
1092 goto desc_ring_free;
1097 /* free DMA buffer */
1098 dma_free_coherent(NULL, rx_ringsize, mdp->rx_ring, mdp->rx_desc_dma);
1101 /* Free Rx and Tx skb ring buffer */
1102 sh_eth_ring_free(ndev);
1103 mdp->tx_ring = NULL;
1104 mdp->rx_ring = NULL;
1109 static void sh_eth_free_dma_buffer(struct sh_eth_private *mdp)
1114 ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
1115 dma_free_coherent(NULL, ringsize, mdp->rx_ring,
1117 mdp->rx_ring = NULL;
1121 ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
1122 dma_free_coherent(NULL, ringsize, mdp->tx_ring,
1124 mdp->tx_ring = NULL;
1128 static int sh_eth_dev_init(struct net_device *ndev, bool start)
1131 struct sh_eth_private *mdp = netdev_priv(ndev);
1135 ret = sh_eth_reset(ndev);
1139 /* Descriptor format */
1140 sh_eth_ring_format(ndev);
1141 if (mdp->cd->rpadir)
1142 sh_eth_write(ndev, mdp->cd->rpadir_value, RPADIR);
1144 /* all sh_eth int mask */
1145 sh_eth_write(ndev, 0, EESIPR);
1147 #if defined(__LITTLE_ENDIAN)
1148 if (mdp->cd->hw_swap)
1149 sh_eth_write(ndev, EDMR_EL, EDMR);
1152 sh_eth_write(ndev, 0, EDMR);
1155 sh_eth_write(ndev, mdp->cd->fdr_value, FDR);
1156 sh_eth_write(ndev, 0, TFTR);
1158 /* Frame recv control */
1159 sh_eth_write(ndev, mdp->cd->rmcr_value, RMCR);
1161 sh_eth_write(ndev, DESC_I_RINT8 | DESC_I_RINT5 | DESC_I_TINT2, TRSCER);
1164 sh_eth_write(ndev, 0x800, BCULR); /* Burst sycle set */
1166 sh_eth_write(ndev, mdp->cd->fcftr_value, FCFTR);
1168 if (!mdp->cd->no_trimd)
1169 sh_eth_write(ndev, 0, TRIMD);
1171 /* Recv frame limit set register */
1172 sh_eth_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN,
1175 sh_eth_write(ndev, sh_eth_read(ndev, EESR), EESR);
1177 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
1179 /* PAUSE Prohibition */
1180 val = (sh_eth_read(ndev, ECMR) & ECMR_DM) |
1181 ECMR_ZPF | (mdp->duplex ? ECMR_DM : 0) | ECMR_TE | ECMR_RE;
1183 sh_eth_write(ndev, val, ECMR);
1185 if (mdp->cd->set_rate)
1186 mdp->cd->set_rate(ndev);
1188 /* E-MAC Status Register clear */
1189 sh_eth_write(ndev, mdp->cd->ecsr_value, ECSR);
1191 /* E-MAC Interrupt Enable register */
1193 sh_eth_write(ndev, mdp->cd->ecsipr_value, ECSIPR);
1195 /* Set MAC address */
1196 update_mac_address(ndev);
1200 sh_eth_write(ndev, APR_AP, APR);
1202 sh_eth_write(ndev, MPR_MP, MPR);
1203 if (mdp->cd->tpauser)
1204 sh_eth_write(ndev, TPAUSER_UNLIMITED, TPAUSER);
1207 /* Setting the Rx mode will start the Rx process. */
1208 sh_eth_write(ndev, EDRRR_R, EDRRR);
1210 netif_start_queue(ndev);
1217 /* free Tx skb function */
1218 static int sh_eth_txfree(struct net_device *ndev)
1220 struct sh_eth_private *mdp = netdev_priv(ndev);
1221 struct sh_eth_txdesc *txdesc;
1225 for (; mdp->cur_tx - mdp->dirty_tx > 0; mdp->dirty_tx++) {
1226 entry = mdp->dirty_tx % mdp->num_tx_ring;
1227 txdesc = &mdp->tx_ring[entry];
1228 if (txdesc->status & cpu_to_edmac(mdp, TD_TACT))
1230 /* Free the original skb. */
1231 if (mdp->tx_skbuff[entry]) {
1232 dma_unmap_single(&ndev->dev, txdesc->addr,
1233 txdesc->buffer_length, DMA_TO_DEVICE);
1234 dev_kfree_skb_irq(mdp->tx_skbuff[entry]);
1235 mdp->tx_skbuff[entry] = NULL;
1238 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
1239 if (entry >= mdp->num_tx_ring - 1)
1240 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
1242 ndev->stats.tx_packets++;
1243 ndev->stats.tx_bytes += txdesc->buffer_length;
1248 /* Packet receive function */
1249 static int sh_eth_rx(struct net_device *ndev, u32 intr_status)
1251 struct sh_eth_private *mdp = netdev_priv(ndev);
1252 struct sh_eth_rxdesc *rxdesc;
1254 int entry = mdp->cur_rx % mdp->num_rx_ring;
1255 int boguscnt = (mdp->dirty_rx + mdp->num_rx_ring) - mdp->cur_rx;
1256 struct sk_buff *skb;
1260 rxdesc = &mdp->rx_ring[entry];
1261 while (!(rxdesc->status & cpu_to_edmac(mdp, RD_RACT))) {
1262 desc_status = edmac_to_cpu(mdp, rxdesc->status);
1263 pkt_len = rxdesc->frame_length;
1265 #if defined(CONFIG_ARCH_R8A7740)
1272 if (!(desc_status & RDFEND))
1273 ndev->stats.rx_length_errors++;
1275 if (desc_status & (RD_RFS1 | RD_RFS2 | RD_RFS3 | RD_RFS4 |
1276 RD_RFS5 | RD_RFS6 | RD_RFS10)) {
1277 ndev->stats.rx_errors++;
1278 if (desc_status & RD_RFS1)
1279 ndev->stats.rx_crc_errors++;
1280 if (desc_status & RD_RFS2)
1281 ndev->stats.rx_frame_errors++;
1282 if (desc_status & RD_RFS3)
1283 ndev->stats.rx_length_errors++;
1284 if (desc_status & RD_RFS4)
1285 ndev->stats.rx_length_errors++;
1286 if (desc_status & RD_RFS6)
1287 ndev->stats.rx_missed_errors++;
1288 if (desc_status & RD_RFS10)
1289 ndev->stats.rx_over_errors++;
1291 if (!mdp->cd->hw_swap)
1293 phys_to_virt(ALIGN(rxdesc->addr, 4)),
1295 skb = mdp->rx_skbuff[entry];
1296 mdp->rx_skbuff[entry] = NULL;
1297 if (mdp->cd->rpadir)
1298 skb_reserve(skb, NET_IP_ALIGN);
1299 skb_put(skb, pkt_len);
1300 skb->protocol = eth_type_trans(skb, ndev);
1302 ndev->stats.rx_packets++;
1303 ndev->stats.rx_bytes += pkt_len;
1305 rxdesc->status |= cpu_to_edmac(mdp, RD_RACT);
1306 entry = (++mdp->cur_rx) % mdp->num_rx_ring;
1307 rxdesc = &mdp->rx_ring[entry];
1310 /* Refill the Rx ring buffers. */
1311 for (; mdp->cur_rx - mdp->dirty_rx > 0; mdp->dirty_rx++) {
1312 entry = mdp->dirty_rx % mdp->num_rx_ring;
1313 rxdesc = &mdp->rx_ring[entry];
1314 /* The size of the buffer is 16 byte boundary. */
1315 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
1317 if (mdp->rx_skbuff[entry] == NULL) {
1318 skb = netdev_alloc_skb(ndev, mdp->rx_buf_sz);
1319 mdp->rx_skbuff[entry] = skb;
1321 break; /* Better luck next round. */
1322 dma_map_single(&ndev->dev, skb->data, mdp->rx_buf_sz,
1324 sh_eth_set_receive_align(skb);
1326 skb_checksum_none_assert(skb);
1327 rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4));
1329 if (entry >= mdp->num_rx_ring - 1)
1331 cpu_to_edmac(mdp, RD_RACT | RD_RFP | RD_RDEL);
1334 cpu_to_edmac(mdp, RD_RACT | RD_RFP);
1337 /* Restart Rx engine if stopped. */
1338 /* If we don't need to check status, don't. -KDU */
1339 if (!(sh_eth_read(ndev, EDRRR) & EDRRR_R)) {
1340 /* fix the values for the next receiving if RDE is set */
1341 if (intr_status & EESR_RDE)
1342 mdp->cur_rx = mdp->dirty_rx =
1343 (sh_eth_read(ndev, RDFAR) -
1344 sh_eth_read(ndev, RDLAR)) >> 4;
1345 sh_eth_write(ndev, EDRRR_R, EDRRR);
1351 static void sh_eth_rcv_snd_disable(struct net_device *ndev)
1353 /* disable tx and rx */
1354 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) &
1355 ~(ECMR_RE | ECMR_TE), ECMR);
1358 static void sh_eth_rcv_snd_enable(struct net_device *ndev)
1360 /* enable tx and rx */
1361 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) |
1362 (ECMR_RE | ECMR_TE), ECMR);
1365 /* error control function */
1366 static void sh_eth_error(struct net_device *ndev, int intr_status)
1368 struct sh_eth_private *mdp = netdev_priv(ndev);
1373 if (intr_status & EESR_ECI) {
1374 felic_stat = sh_eth_read(ndev, ECSR);
1375 sh_eth_write(ndev, felic_stat, ECSR); /* clear int */
1376 if (felic_stat & ECSR_ICD)
1377 ndev->stats.tx_carrier_errors++;
1378 if (felic_stat & ECSR_LCHNG) {
1380 if (mdp->cd->no_psr || mdp->no_ether_link) {
1383 link_stat = (sh_eth_read(ndev, PSR));
1384 if (mdp->ether_link_active_low)
1385 link_stat = ~link_stat;
1387 if (!(link_stat & PHY_ST_LINK))
1388 sh_eth_rcv_snd_disable(ndev);
1391 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) &
1392 ~DMAC_M_ECI, EESIPR);
1394 sh_eth_write(ndev, sh_eth_read(ndev, ECSR),
1396 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) |
1397 DMAC_M_ECI, EESIPR);
1398 /* enable tx and rx */
1399 sh_eth_rcv_snd_enable(ndev);
1405 if (intr_status & EESR_TWB) {
1406 /* Write buck end. unused write back interrupt */
1407 if (intr_status & EESR_TABT) /* Transmit Abort int */
1408 ndev->stats.tx_aborted_errors++;
1409 if (netif_msg_tx_err(mdp))
1410 dev_err(&ndev->dev, "Transmit Abort\n");
1413 if (intr_status & EESR_RABT) {
1414 /* Receive Abort int */
1415 if (intr_status & EESR_RFRMER) {
1416 /* Receive Frame Overflow int */
1417 ndev->stats.rx_frame_errors++;
1418 if (netif_msg_rx_err(mdp))
1419 dev_err(&ndev->dev, "Receive Abort\n");
1423 if (intr_status & EESR_TDE) {
1424 /* Transmit Descriptor Empty int */
1425 ndev->stats.tx_fifo_errors++;
1426 if (netif_msg_tx_err(mdp))
1427 dev_err(&ndev->dev, "Transmit Descriptor Empty\n");
1430 if (intr_status & EESR_TFE) {
1431 /* FIFO under flow */
1432 ndev->stats.tx_fifo_errors++;
1433 if (netif_msg_tx_err(mdp))
1434 dev_err(&ndev->dev, "Transmit FIFO Under flow\n");
1437 if (intr_status & EESR_RDE) {
1438 /* Receive Descriptor Empty int */
1439 ndev->stats.rx_over_errors++;
1441 if (netif_msg_rx_err(mdp))
1442 dev_err(&ndev->dev, "Receive Descriptor Empty\n");
1445 if (intr_status & EESR_RFE) {
1446 /* Receive FIFO Overflow int */
1447 ndev->stats.rx_fifo_errors++;
1448 if (netif_msg_rx_err(mdp))
1449 dev_err(&ndev->dev, "Receive FIFO Overflow\n");
1452 if (!mdp->cd->no_ade && (intr_status & EESR_ADE)) {
1454 ndev->stats.tx_fifo_errors++;
1455 if (netif_msg_tx_err(mdp))
1456 dev_err(&ndev->dev, "Address Error\n");
1459 mask = EESR_TWB | EESR_TABT | EESR_ADE | EESR_TDE | EESR_TFE;
1460 if (mdp->cd->no_ade)
1462 if (intr_status & mask) {
1464 u32 edtrr = sh_eth_read(ndev, EDTRR);
1466 dev_err(&ndev->dev, "TX error. status=%8.8x cur_tx=%8.8x ",
1467 intr_status, mdp->cur_tx);
1468 dev_err(&ndev->dev, "dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
1469 mdp->dirty_tx, (u32) ndev->state, edtrr);
1470 /* dirty buffer free */
1471 sh_eth_txfree(ndev);
1474 if (edtrr ^ sh_eth_get_edtrr_trns(mdp)) {
1476 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
1479 netif_wake_queue(ndev);
1483 static irqreturn_t sh_eth_interrupt(int irq, void *netdev)
1485 struct net_device *ndev = netdev;
1486 struct sh_eth_private *mdp = netdev_priv(ndev);
1487 struct sh_eth_cpu_data *cd = mdp->cd;
1488 irqreturn_t ret = IRQ_NONE;
1489 unsigned long intr_status;
1491 spin_lock(&mdp->lock);
1493 /* Get interrupt status */
1494 intr_status = sh_eth_read(ndev, EESR);
1495 /* Mask it with the interrupt mask, forcing ECI interrupt to be always
1496 * enabled since it's the one that comes thru regardless of the mask,
1497 * and we need to fully handle it in sh_eth_error() in order to quench
1498 * it as it doesn't get cleared by just writing 1 to the ECI bit...
1500 intr_status &= sh_eth_read(ndev, EESIPR) | DMAC_M_ECI;
1501 /* Clear interrupt */
1502 if (intr_status & (EESR_FRC | EESR_RMAF | EESR_RRF |
1503 EESR_RTLF | EESR_RTSF | EESR_PRE | EESR_CERF |
1504 cd->tx_check | cd->eesr_err_check)) {
1505 sh_eth_write(ndev, intr_status, EESR);
1510 if (intr_status & (EESR_FRC | /* Frame recv*/
1511 EESR_RMAF | /* Multi cast address recv*/
1512 EESR_RRF | /* Bit frame recv */
1513 EESR_RTLF | /* Long frame recv*/
1514 EESR_RTSF | /* short frame recv */
1515 EESR_PRE | /* PHY-LSI recv error */
1516 EESR_CERF)){ /* recv frame CRC error */
1517 sh_eth_rx(ndev, intr_status);
1521 if (intr_status & cd->tx_check) {
1522 sh_eth_txfree(ndev);
1523 netif_wake_queue(ndev);
1526 if (intr_status & cd->eesr_err_check)
1527 sh_eth_error(ndev, intr_status);
1530 spin_unlock(&mdp->lock);
1535 /* PHY state control function */
1536 static void sh_eth_adjust_link(struct net_device *ndev)
1538 struct sh_eth_private *mdp = netdev_priv(ndev);
1539 struct phy_device *phydev = mdp->phydev;
1543 if (phydev->duplex != mdp->duplex) {
1545 mdp->duplex = phydev->duplex;
1546 if (mdp->cd->set_duplex)
1547 mdp->cd->set_duplex(ndev);
1550 if (phydev->speed != mdp->speed) {
1552 mdp->speed = phydev->speed;
1553 if (mdp->cd->set_rate)
1554 mdp->cd->set_rate(ndev);
1558 (sh_eth_read(ndev, ECMR) & ~ECMR_TXF), ECMR);
1560 mdp->link = phydev->link;
1561 if (mdp->cd->no_psr || mdp->no_ether_link)
1562 sh_eth_rcv_snd_enable(ndev);
1564 } else if (mdp->link) {
1569 if (mdp->cd->no_psr || mdp->no_ether_link)
1570 sh_eth_rcv_snd_disable(ndev);
1573 if (new_state && netif_msg_link(mdp))
1574 phy_print_status(phydev);
1577 /* PHY init function */
1578 static int sh_eth_phy_init(struct net_device *ndev)
1580 struct sh_eth_private *mdp = netdev_priv(ndev);
1581 char phy_id[MII_BUS_ID_SIZE + 3];
1582 struct phy_device *phydev = NULL;
1584 snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
1585 mdp->mii_bus->id , mdp->phy_id);
1591 /* Try connect to PHY */
1592 phydev = phy_connect(ndev, phy_id, sh_eth_adjust_link,
1593 mdp->phy_interface);
1594 if (IS_ERR(phydev)) {
1595 dev_err(&ndev->dev, "phy_connect failed\n");
1596 return PTR_ERR(phydev);
1599 dev_info(&ndev->dev, "attached phy %i to driver %s\n",
1600 phydev->addr, phydev->drv->name);
1602 mdp->phydev = phydev;
1607 /* PHY control start function */
1608 static int sh_eth_phy_start(struct net_device *ndev)
1610 struct sh_eth_private *mdp = netdev_priv(ndev);
1613 ret = sh_eth_phy_init(ndev);
1617 /* reset phy - this also wakes it from PDOWN */
1618 phy_write(mdp->phydev, MII_BMCR, BMCR_RESET);
1619 phy_start(mdp->phydev);
1624 static int sh_eth_get_settings(struct net_device *ndev,
1625 struct ethtool_cmd *ecmd)
1627 struct sh_eth_private *mdp = netdev_priv(ndev);
1628 unsigned long flags;
1631 spin_lock_irqsave(&mdp->lock, flags);
1632 ret = phy_ethtool_gset(mdp->phydev, ecmd);
1633 spin_unlock_irqrestore(&mdp->lock, flags);
1638 static int sh_eth_set_settings(struct net_device *ndev,
1639 struct ethtool_cmd *ecmd)
1641 struct sh_eth_private *mdp = netdev_priv(ndev);
1642 unsigned long flags;
1645 spin_lock_irqsave(&mdp->lock, flags);
1647 /* disable tx and rx */
1648 sh_eth_rcv_snd_disable(ndev);
1650 ret = phy_ethtool_sset(mdp->phydev, ecmd);
1654 if (ecmd->duplex == DUPLEX_FULL)
1659 if (mdp->cd->set_duplex)
1660 mdp->cd->set_duplex(ndev);
1665 /* enable tx and rx */
1666 sh_eth_rcv_snd_enable(ndev);
1668 spin_unlock_irqrestore(&mdp->lock, flags);
1673 static int sh_eth_nway_reset(struct net_device *ndev)
1675 struct sh_eth_private *mdp = netdev_priv(ndev);
1676 unsigned long flags;
1679 spin_lock_irqsave(&mdp->lock, flags);
1680 ret = phy_start_aneg(mdp->phydev);
1681 spin_unlock_irqrestore(&mdp->lock, flags);
1686 static u32 sh_eth_get_msglevel(struct net_device *ndev)
1688 struct sh_eth_private *mdp = netdev_priv(ndev);
1689 return mdp->msg_enable;
1692 static void sh_eth_set_msglevel(struct net_device *ndev, u32 value)
1694 struct sh_eth_private *mdp = netdev_priv(ndev);
1695 mdp->msg_enable = value;
1698 static const char sh_eth_gstrings_stats[][ETH_GSTRING_LEN] = {
1699 "rx_current", "tx_current",
1700 "rx_dirty", "tx_dirty",
1702 #define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats)
1704 static int sh_eth_get_sset_count(struct net_device *netdev, int sset)
1708 return SH_ETH_STATS_LEN;
1714 static void sh_eth_get_ethtool_stats(struct net_device *ndev,
1715 struct ethtool_stats *stats, u64 *data)
1717 struct sh_eth_private *mdp = netdev_priv(ndev);
1720 /* device-specific stats */
1721 data[i++] = mdp->cur_rx;
1722 data[i++] = mdp->cur_tx;
1723 data[i++] = mdp->dirty_rx;
1724 data[i++] = mdp->dirty_tx;
1727 static void sh_eth_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1729 switch (stringset) {
1731 memcpy(data, *sh_eth_gstrings_stats,
1732 sizeof(sh_eth_gstrings_stats));
1737 static void sh_eth_get_ringparam(struct net_device *ndev,
1738 struct ethtool_ringparam *ring)
1740 struct sh_eth_private *mdp = netdev_priv(ndev);
1742 ring->rx_max_pending = RX_RING_MAX;
1743 ring->tx_max_pending = TX_RING_MAX;
1744 ring->rx_pending = mdp->num_rx_ring;
1745 ring->tx_pending = mdp->num_tx_ring;
1748 static int sh_eth_set_ringparam(struct net_device *ndev,
1749 struct ethtool_ringparam *ring)
1751 struct sh_eth_private *mdp = netdev_priv(ndev);
1754 if (ring->tx_pending > TX_RING_MAX ||
1755 ring->rx_pending > RX_RING_MAX ||
1756 ring->tx_pending < TX_RING_MIN ||
1757 ring->rx_pending < RX_RING_MIN)
1759 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
1762 if (netif_running(ndev)) {
1763 netif_tx_disable(ndev);
1764 /* Disable interrupts by clearing the interrupt mask. */
1765 sh_eth_write(ndev, 0x0000, EESIPR);
1766 /* Stop the chip's Tx and Rx processes. */
1767 sh_eth_write(ndev, 0, EDTRR);
1768 sh_eth_write(ndev, 0, EDRRR);
1769 synchronize_irq(ndev->irq);
1772 /* Free all the skbuffs in the Rx queue. */
1773 sh_eth_ring_free(ndev);
1774 /* Free DMA buffer */
1775 sh_eth_free_dma_buffer(mdp);
1777 /* Set new parameters */
1778 mdp->num_rx_ring = ring->rx_pending;
1779 mdp->num_tx_ring = ring->tx_pending;
1781 ret = sh_eth_ring_init(ndev);
1783 dev_err(&ndev->dev, "%s: sh_eth_ring_init failed.\n", __func__);
1786 ret = sh_eth_dev_init(ndev, false);
1788 dev_err(&ndev->dev, "%s: sh_eth_dev_init failed.\n", __func__);
1792 if (netif_running(ndev)) {
1793 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
1794 /* Setting the Rx mode will start the Rx process. */
1795 sh_eth_write(ndev, EDRRR_R, EDRRR);
1796 netif_wake_queue(ndev);
1802 static const struct ethtool_ops sh_eth_ethtool_ops = {
1803 .get_settings = sh_eth_get_settings,
1804 .set_settings = sh_eth_set_settings,
1805 .nway_reset = sh_eth_nway_reset,
1806 .get_msglevel = sh_eth_get_msglevel,
1807 .set_msglevel = sh_eth_set_msglevel,
1808 .get_link = ethtool_op_get_link,
1809 .get_strings = sh_eth_get_strings,
1810 .get_ethtool_stats = sh_eth_get_ethtool_stats,
1811 .get_sset_count = sh_eth_get_sset_count,
1812 .get_ringparam = sh_eth_get_ringparam,
1813 .set_ringparam = sh_eth_set_ringparam,
1816 /* network device open function */
1817 static int sh_eth_open(struct net_device *ndev)
1820 struct sh_eth_private *mdp = netdev_priv(ndev);
1822 pm_runtime_get_sync(&mdp->pdev->dev);
1824 ret = request_irq(ndev->irq, sh_eth_interrupt,
1825 mdp->cd->irq_flags, ndev->name, ndev);
1827 dev_err(&ndev->dev, "Can not assign IRQ number\n");
1831 /* Descriptor set */
1832 ret = sh_eth_ring_init(ndev);
1837 ret = sh_eth_dev_init(ndev, true);
1841 /* PHY control start*/
1842 ret = sh_eth_phy_start(ndev);
1849 free_irq(ndev->irq, ndev);
1850 pm_runtime_put_sync(&mdp->pdev->dev);
1854 /* Timeout function */
1855 static void sh_eth_tx_timeout(struct net_device *ndev)
1857 struct sh_eth_private *mdp = netdev_priv(ndev);
1858 struct sh_eth_rxdesc *rxdesc;
1861 netif_stop_queue(ndev);
1863 if (netif_msg_timer(mdp))
1864 dev_err(&ndev->dev, "%s: transmit timed out, status %8.8x,"
1865 " resetting...\n", ndev->name, (int)sh_eth_read(ndev, EESR));
1867 /* tx_errors count up */
1868 ndev->stats.tx_errors++;
1870 /* Free all the skbuffs in the Rx queue. */
1871 for (i = 0; i < mdp->num_rx_ring; i++) {
1872 rxdesc = &mdp->rx_ring[i];
1874 rxdesc->addr = 0xBADF00D0;
1875 if (mdp->rx_skbuff[i])
1876 dev_kfree_skb(mdp->rx_skbuff[i]);
1877 mdp->rx_skbuff[i] = NULL;
1879 for (i = 0; i < mdp->num_tx_ring; i++) {
1880 if (mdp->tx_skbuff[i])
1881 dev_kfree_skb(mdp->tx_skbuff[i]);
1882 mdp->tx_skbuff[i] = NULL;
1886 sh_eth_dev_init(ndev, true);
1889 /* Packet transmit function */
1890 static int sh_eth_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1892 struct sh_eth_private *mdp = netdev_priv(ndev);
1893 struct sh_eth_txdesc *txdesc;
1895 unsigned long flags;
1897 spin_lock_irqsave(&mdp->lock, flags);
1898 if ((mdp->cur_tx - mdp->dirty_tx) >= (mdp->num_tx_ring - 4)) {
1899 if (!sh_eth_txfree(ndev)) {
1900 if (netif_msg_tx_queued(mdp))
1901 dev_warn(&ndev->dev, "TxFD exhausted.\n");
1902 netif_stop_queue(ndev);
1903 spin_unlock_irqrestore(&mdp->lock, flags);
1904 return NETDEV_TX_BUSY;
1907 spin_unlock_irqrestore(&mdp->lock, flags);
1909 entry = mdp->cur_tx % mdp->num_tx_ring;
1910 mdp->tx_skbuff[entry] = skb;
1911 txdesc = &mdp->tx_ring[entry];
1913 if (!mdp->cd->hw_swap)
1914 sh_eth_soft_swap(phys_to_virt(ALIGN(txdesc->addr, 4)),
1916 txdesc->addr = dma_map_single(&ndev->dev, skb->data, skb->len,
1918 if (skb->len < ETHERSMALL)
1919 txdesc->buffer_length = ETHERSMALL;
1921 txdesc->buffer_length = skb->len;
1923 if (entry >= mdp->num_tx_ring - 1)
1924 txdesc->status |= cpu_to_edmac(mdp, TD_TACT | TD_TDLE);
1926 txdesc->status |= cpu_to_edmac(mdp, TD_TACT);
1930 if (!(sh_eth_read(ndev, EDTRR) & sh_eth_get_edtrr_trns(mdp)))
1931 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
1933 return NETDEV_TX_OK;
1936 /* device close function */
1937 static int sh_eth_close(struct net_device *ndev)
1939 struct sh_eth_private *mdp = netdev_priv(ndev);
1941 netif_stop_queue(ndev);
1943 /* Disable interrupts by clearing the interrupt mask. */
1944 sh_eth_write(ndev, 0x0000, EESIPR);
1946 /* Stop the chip's Tx and Rx processes. */
1947 sh_eth_write(ndev, 0, EDTRR);
1948 sh_eth_write(ndev, 0, EDRRR);
1950 /* PHY Disconnect */
1952 phy_stop(mdp->phydev);
1953 phy_disconnect(mdp->phydev);
1956 free_irq(ndev->irq, ndev);
1958 /* Free all the skbuffs in the Rx queue. */
1959 sh_eth_ring_free(ndev);
1961 /* free DMA buffer */
1962 sh_eth_free_dma_buffer(mdp);
1964 pm_runtime_put_sync(&mdp->pdev->dev);
1969 static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev)
1971 struct sh_eth_private *mdp = netdev_priv(ndev);
1973 pm_runtime_get_sync(&mdp->pdev->dev);
1975 ndev->stats.tx_dropped += sh_eth_read(ndev, TROCR);
1976 sh_eth_write(ndev, 0, TROCR); /* (write clear) */
1977 ndev->stats.collisions += sh_eth_read(ndev, CDCR);
1978 sh_eth_write(ndev, 0, CDCR); /* (write clear) */
1979 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, LCCR);
1980 sh_eth_write(ndev, 0, LCCR); /* (write clear) */
1981 if (sh_eth_is_gether(mdp)) {
1982 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CERCR);
1983 sh_eth_write(ndev, 0, CERCR); /* (write clear) */
1984 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CEECR);
1985 sh_eth_write(ndev, 0, CEECR); /* (write clear) */
1987 ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CNDCR);
1988 sh_eth_write(ndev, 0, CNDCR); /* (write clear) */
1990 pm_runtime_put_sync(&mdp->pdev->dev);
1992 return &ndev->stats;
1995 /* ioctl to device function */
1996 static int sh_eth_do_ioctl(struct net_device *ndev, struct ifreq *rq,
1999 struct sh_eth_private *mdp = netdev_priv(ndev);
2000 struct phy_device *phydev = mdp->phydev;
2002 if (!netif_running(ndev))
2008 return phy_mii_ioctl(phydev, rq, cmd);
2011 /* For TSU_POSTn. Please refer to the manual about this (strange) bitfields */
2012 static void *sh_eth_tsu_get_post_reg_offset(struct sh_eth_private *mdp,
2015 return sh_eth_tsu_get_offset(mdp, TSU_POST1) + (entry / 8 * 4);
2018 static u32 sh_eth_tsu_get_post_mask(int entry)
2020 return 0x0f << (28 - ((entry % 8) * 4));
2023 static u32 sh_eth_tsu_get_post_bit(struct sh_eth_private *mdp, int entry)
2025 return (0x08 >> (mdp->port << 1)) << (28 - ((entry % 8) * 4));
2028 static void sh_eth_tsu_enable_cam_entry_post(struct net_device *ndev,
2031 struct sh_eth_private *mdp = netdev_priv(ndev);
2035 reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
2036 tmp = ioread32(reg_offset);
2037 iowrite32(tmp | sh_eth_tsu_get_post_bit(mdp, entry), reg_offset);
2040 static bool sh_eth_tsu_disable_cam_entry_post(struct net_device *ndev,
2043 struct sh_eth_private *mdp = netdev_priv(ndev);
2044 u32 post_mask, ref_mask, tmp;
2047 reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
2048 post_mask = sh_eth_tsu_get_post_mask(entry);
2049 ref_mask = sh_eth_tsu_get_post_bit(mdp, entry) & ~post_mask;
2051 tmp = ioread32(reg_offset);
2052 iowrite32(tmp & ~post_mask, reg_offset);
2054 /* If other port enables, the function returns "true" */
2055 return tmp & ref_mask;
2058 static int sh_eth_tsu_busy(struct net_device *ndev)
2060 int timeout = SH_ETH_TSU_TIMEOUT_MS * 100;
2061 struct sh_eth_private *mdp = netdev_priv(ndev);
2063 while ((sh_eth_tsu_read(mdp, TSU_ADSBSY) & TSU_ADSBSY_0)) {
2067 dev_err(&ndev->dev, "%s: timeout\n", __func__);
2075 static int sh_eth_tsu_write_entry(struct net_device *ndev, void *reg,
2080 val = addr[0] << 24 | addr[1] << 16 | addr[2] << 8 | addr[3];
2081 iowrite32(val, reg);
2082 if (sh_eth_tsu_busy(ndev) < 0)
2085 val = addr[4] << 8 | addr[5];
2086 iowrite32(val, reg + 4);
2087 if (sh_eth_tsu_busy(ndev) < 0)
2093 static void sh_eth_tsu_read_entry(void *reg, u8 *addr)
2097 val = ioread32(reg);
2098 addr[0] = (val >> 24) & 0xff;
2099 addr[1] = (val >> 16) & 0xff;
2100 addr[2] = (val >> 8) & 0xff;
2101 addr[3] = val & 0xff;
2102 val = ioread32(reg + 4);
2103 addr[4] = (val >> 8) & 0xff;
2104 addr[5] = val & 0xff;
2108 static int sh_eth_tsu_find_entry(struct net_device *ndev, const u8 *addr)
2110 struct sh_eth_private *mdp = netdev_priv(ndev);
2111 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2113 u8 c_addr[ETH_ALEN];
2115 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
2116 sh_eth_tsu_read_entry(reg_offset, c_addr);
2117 if (memcmp(addr, c_addr, ETH_ALEN) == 0)
2124 static int sh_eth_tsu_find_empty(struct net_device *ndev)
2129 memset(blank, 0, sizeof(blank));
2130 entry = sh_eth_tsu_find_entry(ndev, blank);
2131 return (entry < 0) ? -ENOMEM : entry;
2134 static int sh_eth_tsu_disable_cam_entry_table(struct net_device *ndev,
2137 struct sh_eth_private *mdp = netdev_priv(ndev);
2138 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2142 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) &
2143 ~(1 << (31 - entry)), TSU_TEN);
2145 memset(blank, 0, sizeof(blank));
2146 ret = sh_eth_tsu_write_entry(ndev, reg_offset + entry * 8, blank);
2152 static int sh_eth_tsu_add_entry(struct net_device *ndev, const u8 *addr)
2154 struct sh_eth_private *mdp = netdev_priv(ndev);
2155 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2161 i = sh_eth_tsu_find_entry(ndev, addr);
2163 /* No entry found, create one */
2164 i = sh_eth_tsu_find_empty(ndev);
2167 ret = sh_eth_tsu_write_entry(ndev, reg_offset + i * 8, addr);
2171 /* Enable the entry */
2172 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) |
2173 (1 << (31 - i)), TSU_TEN);
2176 /* Entry found or created, enable POST */
2177 sh_eth_tsu_enable_cam_entry_post(ndev, i);
2182 static int sh_eth_tsu_del_entry(struct net_device *ndev, const u8 *addr)
2184 struct sh_eth_private *mdp = netdev_priv(ndev);
2190 i = sh_eth_tsu_find_entry(ndev, addr);
2193 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2196 /* Disable the entry if both ports was disabled */
2197 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2205 static int sh_eth_tsu_purge_all(struct net_device *ndev)
2207 struct sh_eth_private *mdp = netdev_priv(ndev);
2210 if (unlikely(!mdp->cd->tsu))
2213 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++) {
2214 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2217 /* Disable the entry if both ports was disabled */
2218 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2226 static void sh_eth_tsu_purge_mcast(struct net_device *ndev)
2228 struct sh_eth_private *mdp = netdev_priv(ndev);
2230 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2233 if (unlikely(!mdp->cd->tsu))
2236 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
2237 sh_eth_tsu_read_entry(reg_offset, addr);
2238 if (is_multicast_ether_addr(addr))
2239 sh_eth_tsu_del_entry(ndev, addr);
2243 /* Multicast reception directions set */
2244 static void sh_eth_set_multicast_list(struct net_device *ndev)
2246 struct sh_eth_private *mdp = netdev_priv(ndev);
2249 unsigned long flags;
2251 spin_lock_irqsave(&mdp->lock, flags);
2253 * Initial condition is MCT = 1, PRM = 0.
2254 * Depending on ndev->flags, set PRM or clear MCT
2256 ecmr_bits = (sh_eth_read(ndev, ECMR) & ~ECMR_PRM) | ECMR_MCT;
2258 if (!(ndev->flags & IFF_MULTICAST)) {
2259 sh_eth_tsu_purge_mcast(ndev);
2262 if (ndev->flags & IFF_ALLMULTI) {
2263 sh_eth_tsu_purge_mcast(ndev);
2264 ecmr_bits &= ~ECMR_MCT;
2268 if (ndev->flags & IFF_PROMISC) {
2269 sh_eth_tsu_purge_all(ndev);
2270 ecmr_bits = (ecmr_bits & ~ECMR_MCT) | ECMR_PRM;
2271 } else if (mdp->cd->tsu) {
2272 struct netdev_hw_addr *ha;
2273 netdev_for_each_mc_addr(ha, ndev) {
2274 if (mcast_all && is_multicast_ether_addr(ha->addr))
2277 if (sh_eth_tsu_add_entry(ndev, ha->addr) < 0) {
2279 sh_eth_tsu_purge_mcast(ndev);
2280 ecmr_bits &= ~ECMR_MCT;
2286 /* Normal, unicast/broadcast-only mode. */
2287 ecmr_bits = (ecmr_bits & ~ECMR_PRM) | ECMR_MCT;
2290 /* update the ethernet mode */
2291 sh_eth_write(ndev, ecmr_bits, ECMR);
2293 spin_unlock_irqrestore(&mdp->lock, flags);
2296 static int sh_eth_get_vtag_index(struct sh_eth_private *mdp)
2304 static int sh_eth_vlan_rx_add_vid(struct net_device *ndev,
2305 __be16 proto, u16 vid)
2307 struct sh_eth_private *mdp = netdev_priv(ndev);
2308 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2310 if (unlikely(!mdp->cd->tsu))
2313 /* No filtering if vid = 0 */
2317 mdp->vlan_num_ids++;
2320 * The controller has one VLAN tag HW filter. So, if the filter is
2321 * already enabled, the driver disables it and the filte
2323 if (mdp->vlan_num_ids > 1) {
2324 /* disable VLAN filter */
2325 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2329 sh_eth_tsu_write(mdp, TSU_VTAG_ENABLE | (vid & TSU_VTAG_VID_MASK),
2335 static int sh_eth_vlan_rx_kill_vid(struct net_device *ndev,
2336 __be16 proto, u16 vid)
2338 struct sh_eth_private *mdp = netdev_priv(ndev);
2339 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2341 if (unlikely(!mdp->cd->tsu))
2344 /* No filtering if vid = 0 */
2348 mdp->vlan_num_ids--;
2349 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2354 /* SuperH's TSU register init function */
2355 static void sh_eth_tsu_init(struct sh_eth_private *mdp)
2357 sh_eth_tsu_write(mdp, 0, TSU_FWEN0); /* Disable forward(0->1) */
2358 sh_eth_tsu_write(mdp, 0, TSU_FWEN1); /* Disable forward(1->0) */
2359 sh_eth_tsu_write(mdp, 0, TSU_FCM); /* forward fifo 3k-3k */
2360 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL0);
2361 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL1);
2362 sh_eth_tsu_write(mdp, 0, TSU_PRISL0);
2363 sh_eth_tsu_write(mdp, 0, TSU_PRISL1);
2364 sh_eth_tsu_write(mdp, 0, TSU_FWSL0);
2365 sh_eth_tsu_write(mdp, 0, TSU_FWSL1);
2366 sh_eth_tsu_write(mdp, TSU_FWSLC_POSTENU | TSU_FWSLC_POSTENL, TSU_FWSLC);
2367 if (sh_eth_is_gether(mdp)) {
2368 sh_eth_tsu_write(mdp, 0, TSU_QTAG0); /* Disable QTAG(0->1) */
2369 sh_eth_tsu_write(mdp, 0, TSU_QTAG1); /* Disable QTAG(1->0) */
2371 sh_eth_tsu_write(mdp, 0, TSU_QTAGM0); /* Disable QTAG(0->1) */
2372 sh_eth_tsu_write(mdp, 0, TSU_QTAGM1); /* Disable QTAG(1->0) */
2374 sh_eth_tsu_write(mdp, 0, TSU_FWSR); /* all interrupt status clear */
2375 sh_eth_tsu_write(mdp, 0, TSU_FWINMK); /* Disable all interrupt */
2376 sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
2377 sh_eth_tsu_write(mdp, 0, TSU_POST1); /* Disable CAM entry [ 0- 7] */
2378 sh_eth_tsu_write(mdp, 0, TSU_POST2); /* Disable CAM entry [ 8-15] */
2379 sh_eth_tsu_write(mdp, 0, TSU_POST3); /* Disable CAM entry [16-23] */
2380 sh_eth_tsu_write(mdp, 0, TSU_POST4); /* Disable CAM entry [24-31] */
2383 /* MDIO bus release function */
2384 static int sh_mdio_release(struct net_device *ndev)
2386 struct mii_bus *bus = dev_get_drvdata(&ndev->dev);
2388 /* unregister mdio bus */
2389 mdiobus_unregister(bus);
2391 /* remove mdio bus info from net_device */
2392 dev_set_drvdata(&ndev->dev, NULL);
2394 /* free bitbang info */
2395 free_mdio_bitbang(bus);
2400 /* MDIO bus init function */
2401 static int sh_mdio_init(struct net_device *ndev, int id,
2402 struct sh_eth_plat_data *pd)
2405 struct bb_info *bitbang;
2406 struct sh_eth_private *mdp = netdev_priv(ndev);
2408 /* create bit control struct for PHY */
2409 bitbang = devm_kzalloc(&ndev->dev, sizeof(struct bb_info),
2417 bitbang->addr = mdp->addr + mdp->reg_offset[PIR];
2418 bitbang->set_gate = pd->set_mdio_gate;
2419 bitbang->mdi_msk = PIR_MDI;
2420 bitbang->mdo_msk = PIR_MDO;
2421 bitbang->mmd_msk = PIR_MMD;
2422 bitbang->mdc_msk = PIR_MDC;
2423 bitbang->ctrl.ops = &bb_ops;
2425 /* MII controller setting */
2426 mdp->mii_bus = alloc_mdio_bitbang(&bitbang->ctrl);
2427 if (!mdp->mii_bus) {
2432 /* Hook up MII support for ethtool */
2433 mdp->mii_bus->name = "sh_mii";
2434 mdp->mii_bus->parent = &ndev->dev;
2435 snprintf(mdp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
2436 mdp->pdev->name, id);
2439 mdp->mii_bus->irq = devm_kzalloc(&ndev->dev,
2440 sizeof(int) * PHY_MAX_ADDR,
2442 if (!mdp->mii_bus->irq) {
2447 for (i = 0; i < PHY_MAX_ADDR; i++)
2448 mdp->mii_bus->irq[i] = PHY_POLL;
2450 /* register mdio bus */
2451 ret = mdiobus_register(mdp->mii_bus);
2455 dev_set_drvdata(&ndev->dev, mdp->mii_bus);
2460 free_mdio_bitbang(mdp->mii_bus);
2466 static const u16 *sh_eth_get_register_offset(int register_type)
2468 const u16 *reg_offset = NULL;
2470 switch (register_type) {
2471 case SH_ETH_REG_GIGABIT:
2472 reg_offset = sh_eth_offset_gigabit;
2474 case SH_ETH_REG_FAST_RCAR:
2475 reg_offset = sh_eth_offset_fast_rcar;
2477 case SH_ETH_REG_FAST_SH4:
2478 reg_offset = sh_eth_offset_fast_sh4;
2480 case SH_ETH_REG_FAST_SH3_SH2:
2481 reg_offset = sh_eth_offset_fast_sh3_sh2;
2484 pr_err("Unknown register type (%d)\n", register_type);
2491 static struct net_device_ops sh_eth_netdev_ops = {
2492 .ndo_open = sh_eth_open,
2493 .ndo_stop = sh_eth_close,
2494 .ndo_start_xmit = sh_eth_start_xmit,
2495 .ndo_get_stats = sh_eth_get_stats,
2496 .ndo_tx_timeout = sh_eth_tx_timeout,
2497 .ndo_do_ioctl = sh_eth_do_ioctl,
2498 .ndo_validate_addr = eth_validate_addr,
2499 .ndo_set_mac_address = eth_mac_addr,
2500 .ndo_change_mtu = eth_change_mtu,
2503 static int sh_eth_drv_probe(struct platform_device *pdev)
2506 struct resource *res;
2507 struct net_device *ndev = NULL;
2508 struct sh_eth_private *mdp = NULL;
2509 struct sh_eth_plat_data *pd = pdev->dev.platform_data;
2510 const struct platform_device_id *id = platform_get_device_id(pdev);
2513 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2514 if (unlikely(res == NULL)) {
2515 dev_err(&pdev->dev, "invalid resource\n");
2520 ndev = alloc_etherdev(sizeof(struct sh_eth_private));
2526 /* The sh Ether-specific entries in the device structure. */
2527 ndev->base_addr = res->start;
2533 ret = platform_get_irq(pdev, 0);
2540 SET_NETDEV_DEV(ndev, &pdev->dev);
2542 /* Fill in the fields of the device structure with ethernet values. */
2545 mdp = netdev_priv(ndev);
2546 mdp->num_tx_ring = TX_RING_SIZE;
2547 mdp->num_rx_ring = RX_RING_SIZE;
2548 mdp->addr = devm_ioremap_resource(&pdev->dev, res);
2549 if (IS_ERR(mdp->addr)) {
2550 ret = PTR_ERR(mdp->addr);
2554 spin_lock_init(&mdp->lock);
2556 pm_runtime_enable(&pdev->dev);
2557 pm_runtime_resume(&pdev->dev);
2560 mdp->phy_id = pd->phy;
2561 mdp->phy_interface = pd->phy_interface;
2563 mdp->edmac_endian = pd->edmac_endian;
2564 mdp->no_ether_link = pd->no_ether_link;
2565 mdp->ether_link_active_low = pd->ether_link_active_low;
2566 mdp->reg_offset = sh_eth_get_register_offset(pd->register_type);
2569 mdp->cd = &sh_eth_my_cpu_data;
2570 if (id->driver_data)
2571 mdp->cd = (struct sh_eth_cpu_data *)id->driver_data;
2572 sh_eth_set_default_cpu_data(mdp->cd);
2576 sh_eth_netdev_ops.ndo_set_rx_mode = sh_eth_set_multicast_list;
2577 sh_eth_netdev_ops.ndo_vlan_rx_add_vid = sh_eth_vlan_rx_add_vid;
2578 sh_eth_netdev_ops.ndo_vlan_rx_kill_vid =
2579 sh_eth_vlan_rx_kill_vid;
2582 ndev->netdev_ops = &sh_eth_netdev_ops;
2583 SET_ETHTOOL_OPS(ndev, &sh_eth_ethtool_ops);
2584 ndev->watchdog_timeo = TX_TIMEOUT;
2586 /* debug message level */
2587 mdp->msg_enable = SH_ETH_DEF_MSG_ENABLE;
2589 /* read and set MAC address */
2590 read_mac_address(ndev, pd->mac_addr);
2591 if (!is_valid_ether_addr(ndev->dev_addr)) {
2592 dev_warn(&pdev->dev,
2593 "no valid MAC address supplied, using a random one.\n");
2594 eth_hw_addr_random(ndev);
2597 /* ioremap the TSU registers */
2599 struct resource *rtsu;
2600 rtsu = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2601 mdp->tsu_addr = devm_ioremap_resource(&pdev->dev, rtsu);
2602 if (IS_ERR(mdp->tsu_addr)) {
2603 ret = PTR_ERR(mdp->tsu_addr);
2606 mdp->port = devno % 2;
2607 ndev->features = NETIF_F_HW_VLAN_CTAG_FILTER;
2610 /* initialize first or needed device */
2611 if (!devno || pd->needs_init) {
2612 if (mdp->cd->chip_reset)
2613 mdp->cd->chip_reset(ndev);
2616 /* TSU init (Init only)*/
2617 sh_eth_tsu_init(mdp);
2621 /* network device register */
2622 ret = register_netdev(ndev);
2627 ret = sh_mdio_init(ndev, pdev->id, pd);
2629 goto out_unregister;
2631 /* print device information */
2632 pr_info("Base address at 0x%x, %pM, IRQ %d.\n",
2633 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
2635 platform_set_drvdata(pdev, ndev);
2640 unregister_netdev(ndev);
2651 static int sh_eth_drv_remove(struct platform_device *pdev)
2653 struct net_device *ndev = platform_get_drvdata(pdev);
2655 sh_mdio_release(ndev);
2656 unregister_netdev(ndev);
2657 pm_runtime_disable(&pdev->dev);
2664 static int sh_eth_runtime_nop(struct device *dev)
2667 * Runtime PM callback shared between ->runtime_suspend()
2668 * and ->runtime_resume(). Simply returns success.
2670 * This driver re-initializes all registers after
2671 * pm_runtime_get_sync() anyway so there is no need
2672 * to save and restore registers here.
2677 static const struct dev_pm_ops sh_eth_dev_pm_ops = {
2678 .runtime_suspend = sh_eth_runtime_nop,
2679 .runtime_resume = sh_eth_runtime_nop,
2681 #define SH_ETH_PM_OPS (&sh_eth_dev_pm_ops)
2683 #define SH_ETH_PM_OPS NULL
2686 static struct platform_device_id sh_eth_id_table[] = {
2687 { "sh7619-ether", (kernel_ulong_t)&sh7619_data },
2688 { "sh771x-ether", (kernel_ulong_t)&sh771x_data },
2689 { "sh7724-ether", (kernel_ulong_t)&sh7724_data },
2690 { "sh7734-gether", (kernel_ulong_t)&sh7734_data },
2691 { "sh7757-ether", (kernel_ulong_t)&sh7757_data },
2692 { "sh7757-gether", (kernel_ulong_t)&sh7757_data_giga },
2693 { "sh7763-gether", (kernel_ulong_t)&sh7763_data },
2694 { "r8a7740-gether", (kernel_ulong_t)&r8a7740_data },
2698 MODULE_DEVICE_TABLE(platform, sh_eth_id_table);
2700 static struct platform_driver sh_eth_driver = {
2701 .probe = sh_eth_drv_probe,
2702 .remove = sh_eth_drv_remove,
2703 .id_table = sh_eth_id_table,
2706 .pm = SH_ETH_PM_OPS,
2710 module_platform_driver(sh_eth_driver);
2712 MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
2713 MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
2714 MODULE_LICENSE("GPL v2");