1 /* SuperH Ethernet device driver
3 * Copyright (C) 2014 Renesas Electronics Corporation
4 * Copyright (C) 2006-2012 Nobuhiro Iwamatsu
5 * Copyright (C) 2008-2014 Renesas Solutions Corp.
6 * Copyright (C) 2013-2016 Cogent Embedded, Inc.
7 * Copyright (C) 2014 Codethink Limited
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms and conditions of the GNU General Public License,
11 * version 2, as published by the Free Software Foundation.
13 * This program is distributed in the hope it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/spinlock.h>
25 #include <linux/interrupt.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/etherdevice.h>
28 #include <linux/delay.h>
29 #include <linux/platform_device.h>
30 #include <linux/mdio-bitbang.h>
31 #include <linux/netdevice.h>
33 #include <linux/of_device.h>
34 #include <linux/of_irq.h>
35 #include <linux/of_net.h>
36 #include <linux/phy.h>
37 #include <linux/cache.h>
39 #include <linux/pm_runtime.h>
40 #include <linux/slab.h>
41 #include <linux/ethtool.h>
42 #include <linux/if_vlan.h>
43 #include <linux/clk.h>
44 #include <linux/sh_eth.h>
45 #include <linux/of_mdio.h>
49 #define SH_ETH_DEF_MSG_ENABLE \
55 #define SH_ETH_OFFSET_INVALID ((u16)~0)
57 #define SH_ETH_OFFSET_DEFAULTS \
58 [0 ... SH_ETH_MAX_REGISTER_OFFSET - 1] = SH_ETH_OFFSET_INVALID
60 static const u16 sh_eth_offset_gigabit
[SH_ETH_MAX_REGISTER_OFFSET
] = {
61 SH_ETH_OFFSET_DEFAULTS
,
116 [TSU_CTRST
] = 0x0004,
117 [TSU_FWEN0
] = 0x0010,
118 [TSU_FWEN1
] = 0x0014,
120 [TSU_BSYSL0
] = 0x0020,
121 [TSU_BSYSL1
] = 0x0024,
122 [TSU_PRISL0
] = 0x0028,
123 [TSU_PRISL1
] = 0x002c,
124 [TSU_FWSL0
] = 0x0030,
125 [TSU_FWSL1
] = 0x0034,
126 [TSU_FWSLC
] = 0x0038,
127 [TSU_QTAG0
] = 0x0040,
128 [TSU_QTAG1
] = 0x0044,
130 [TSU_FWINMK
] = 0x0054,
131 [TSU_ADQT0
] = 0x0048,
132 [TSU_ADQT1
] = 0x004c,
133 [TSU_VTAG0
] = 0x0058,
134 [TSU_VTAG1
] = 0x005c,
135 [TSU_ADSBSY
] = 0x0060,
137 [TSU_POST1
] = 0x0070,
138 [TSU_POST2
] = 0x0074,
139 [TSU_POST3
] = 0x0078,
140 [TSU_POST4
] = 0x007c,
141 [TSU_ADRH0
] = 0x0100,
157 static const u16 sh_eth_offset_fast_rz
[SH_ETH_MAX_REGISTER_OFFSET
] = {
158 SH_ETH_OFFSET_DEFAULTS
,
203 [TSU_CTRST
] = 0x0004,
204 [TSU_FWSLC
] = 0x0038,
205 [TSU_VTAG0
] = 0x0058,
206 [TSU_ADSBSY
] = 0x0060,
208 [TSU_POST1
] = 0x0070,
209 [TSU_POST2
] = 0x0074,
210 [TSU_POST3
] = 0x0078,
211 [TSU_POST4
] = 0x007c,
212 [TSU_ADRH0
] = 0x0100,
220 static const u16 sh_eth_offset_fast_rcar
[SH_ETH_MAX_REGISTER_OFFSET
] = {
221 SH_ETH_OFFSET_DEFAULTS
,
268 static const u16 sh_eth_offset_fast_sh4
[SH_ETH_MAX_REGISTER_OFFSET
] = {
269 SH_ETH_OFFSET_DEFAULTS
,
322 static const u16 sh_eth_offset_fast_sh3_sh2
[SH_ETH_MAX_REGISTER_OFFSET
] = {
323 SH_ETH_OFFSET_DEFAULTS
,
371 [TSU_CTRST
] = 0x0004,
372 [TSU_FWEN0
] = 0x0010,
373 [TSU_FWEN1
] = 0x0014,
375 [TSU_BSYSL0
] = 0x0020,
376 [TSU_BSYSL1
] = 0x0024,
377 [TSU_PRISL0
] = 0x0028,
378 [TSU_PRISL1
] = 0x002c,
379 [TSU_FWSL0
] = 0x0030,
380 [TSU_FWSL1
] = 0x0034,
381 [TSU_FWSLC
] = 0x0038,
382 [TSU_QTAGM0
] = 0x0040,
383 [TSU_QTAGM1
] = 0x0044,
384 [TSU_ADQT0
] = 0x0048,
385 [TSU_ADQT1
] = 0x004c,
387 [TSU_FWINMK
] = 0x0054,
388 [TSU_ADSBSY
] = 0x0060,
390 [TSU_POST1
] = 0x0070,
391 [TSU_POST2
] = 0x0074,
392 [TSU_POST3
] = 0x0078,
393 [TSU_POST4
] = 0x007c,
408 [TSU_ADRH0
] = 0x0100,
411 static void sh_eth_rcv_snd_disable(struct net_device
*ndev
);
412 static struct net_device_stats
*sh_eth_get_stats(struct net_device
*ndev
);
414 static void sh_eth_write(struct net_device
*ndev
, u32 data
, int enum_index
)
416 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
417 u16 offset
= mdp
->reg_offset
[enum_index
];
419 if (WARN_ON(offset
== SH_ETH_OFFSET_INVALID
))
422 iowrite32(data
, mdp
->addr
+ offset
);
425 static u32
sh_eth_read(struct net_device
*ndev
, int enum_index
)
427 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
428 u16 offset
= mdp
->reg_offset
[enum_index
];
430 if (WARN_ON(offset
== SH_ETH_OFFSET_INVALID
))
433 return ioread32(mdp
->addr
+ offset
);
436 static void sh_eth_modify(struct net_device
*ndev
, int enum_index
, u32 clear
,
439 sh_eth_write(ndev
, (sh_eth_read(ndev
, enum_index
) & ~clear
) | set
,
443 static bool sh_eth_is_gether(struct sh_eth_private
*mdp
)
445 return mdp
->reg_offset
== sh_eth_offset_gigabit
;
448 static bool sh_eth_is_rz_fast_ether(struct sh_eth_private
*mdp
)
450 return mdp
->reg_offset
== sh_eth_offset_fast_rz
;
453 static void sh_eth_select_mii(struct net_device
*ndev
)
455 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
458 switch (mdp
->phy_interface
) {
459 case PHY_INTERFACE_MODE_GMII
:
462 case PHY_INTERFACE_MODE_MII
:
465 case PHY_INTERFACE_MODE_RMII
:
470 "PHY interface mode was not setup. Set to MII.\n");
475 sh_eth_write(ndev
, value
, RMII_MII
);
478 static void sh_eth_set_duplex(struct net_device
*ndev
)
480 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
482 sh_eth_modify(ndev
, ECMR
, ECMR_DM
, mdp
->duplex
? ECMR_DM
: 0);
485 static void sh_eth_chip_reset(struct net_device
*ndev
)
487 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
490 sh_eth_tsu_write(mdp
, ARSTR_ARST
, ARSTR
);
494 static void sh_eth_set_rate_gether(struct net_device
*ndev
)
496 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
498 switch (mdp
->speed
) {
499 case 10: /* 10BASE */
500 sh_eth_write(ndev
, GECMR_10
, GECMR
);
502 case 100:/* 100BASE */
503 sh_eth_write(ndev
, GECMR_100
, GECMR
);
505 case 1000: /* 1000BASE */
506 sh_eth_write(ndev
, GECMR_1000
, GECMR
);
513 static struct sh_eth_cpu_data r7s72100_data
= {
514 .chip_reset
= sh_eth_chip_reset
,
515 .set_duplex
= sh_eth_set_duplex
,
517 .register_type
= SH_ETH_REG_FAST_RZ
,
519 .ecsr_value
= ECSR_ICD
,
520 .ecsipr_value
= ECSIPR_ICDIP
,
521 .eesipr_value
= 0xe77f009f,
523 .tx_check
= EESR_TC1
| EESR_FTC
,
524 .eesr_err_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_RABT
|
525 EESR_RFE
| EESR_RDE
| EESR_RFRMER
| EESR_TFE
|
527 .fdr_value
= 0x0000070f,
535 .rpadir_value
= 2 << 16,
543 static void sh_eth_chip_reset_r8a7740(struct net_device
*ndev
)
545 sh_eth_chip_reset(ndev
);
547 sh_eth_select_mii(ndev
);
551 static struct sh_eth_cpu_data r8a7740_data
= {
552 .chip_reset
= sh_eth_chip_reset_r8a7740
,
553 .set_duplex
= sh_eth_set_duplex
,
554 .set_rate
= sh_eth_set_rate_gether
,
556 .register_type
= SH_ETH_REG_GIGABIT
,
558 .ecsr_value
= ECSR_ICD
| ECSR_MPD
,
559 .ecsipr_value
= ECSIPR_LCHNGIP
| ECSIPR_ICDIP
| ECSIPR_MPDIP
,
560 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
562 .tx_check
= EESR_TC1
| EESR_FTC
,
563 .eesr_err_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_RABT
|
564 EESR_RFE
| EESR_RDE
| EESR_RFRMER
| EESR_TFE
|
566 .fdr_value
= 0x0000070f,
574 .rpadir_value
= 2 << 16,
582 /* There is CPU dependent code */
583 static void sh_eth_set_rate_r8a777x(struct net_device
*ndev
)
585 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
587 switch (mdp
->speed
) {
588 case 10: /* 10BASE */
589 sh_eth_modify(ndev
, ECMR
, ECMR_ELB
, 0);
591 case 100:/* 100BASE */
592 sh_eth_modify(ndev
, ECMR
, ECMR_ELB
, ECMR_ELB
);
598 static struct sh_eth_cpu_data r8a777x_data
= {
599 .set_duplex
= sh_eth_set_duplex
,
600 .set_rate
= sh_eth_set_rate_r8a777x
,
602 .register_type
= SH_ETH_REG_FAST_RCAR
,
604 .ecsr_value
= ECSR_PSRTO
| ECSR_LCHNG
| ECSR_ICD
,
605 .ecsipr_value
= ECSIPR_PSRTOIP
| ECSIPR_LCHNGIP
| ECSIPR_ICDIP
,
606 .eesipr_value
= 0x01ff009f,
608 .tx_check
= EESR_FTC
| EESR_CND
| EESR_DLC
| EESR_CD
| EESR_RTO
,
609 .eesr_err_check
= EESR_TWB
| EESR_TABT
| EESR_RABT
| EESR_RFE
|
610 EESR_RDE
| EESR_RFRMER
| EESR_TFE
| EESR_TDE
|
612 .fdr_value
= 0x00000f0f,
621 static struct sh_eth_cpu_data r8a779x_data
= {
622 .set_duplex
= sh_eth_set_duplex
,
623 .set_rate
= sh_eth_set_rate_r8a777x
,
625 .register_type
= SH_ETH_REG_FAST_RCAR
,
627 .ecsr_value
= ECSR_PSRTO
| ECSR_LCHNG
| ECSR_ICD
,
628 .ecsipr_value
= ECSIPR_PSRTOIP
| ECSIPR_LCHNGIP
| ECSIPR_ICDIP
,
629 .eesipr_value
= 0x01ff009f,
631 .tx_check
= EESR_FTC
| EESR_CND
| EESR_DLC
| EESR_CD
| EESR_RTO
,
632 .eesr_err_check
= EESR_TWB
| EESR_TABT
| EESR_RABT
| EESR_RFE
|
633 EESR_RDE
| EESR_RFRMER
| EESR_TFE
| EESR_TDE
|
635 .fdr_value
= 0x00000f0f,
637 .trscer_err_mask
= DESC_I_RINT8
,
645 #endif /* CONFIG_OF */
647 static void sh_eth_set_rate_sh7724(struct net_device
*ndev
)
649 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
651 switch (mdp
->speed
) {
652 case 10: /* 10BASE */
653 sh_eth_modify(ndev
, ECMR
, ECMR_RTM
, 0);
655 case 100:/* 100BASE */
656 sh_eth_modify(ndev
, ECMR
, ECMR_RTM
, ECMR_RTM
);
662 static struct sh_eth_cpu_data sh7724_data
= {
663 .set_duplex
= sh_eth_set_duplex
,
664 .set_rate
= sh_eth_set_rate_sh7724
,
666 .register_type
= SH_ETH_REG_FAST_SH4
,
668 .ecsr_value
= ECSR_PSRTO
| ECSR_LCHNG
| ECSR_ICD
,
669 .ecsipr_value
= ECSIPR_PSRTOIP
| ECSIPR_LCHNGIP
| ECSIPR_ICDIP
,
670 .eesipr_value
= 0x01ff009f,
672 .tx_check
= EESR_FTC
| EESR_CND
| EESR_DLC
| EESR_CD
| EESR_RTO
,
673 .eesr_err_check
= EESR_TWB
| EESR_TABT
| EESR_RABT
| EESR_RFE
|
674 EESR_RDE
| EESR_RFRMER
| EESR_TFE
| EESR_TDE
|
682 .rpadir_value
= 0x00020000, /* NET_IP_ALIGN assumed to be 2 */
685 static void sh_eth_set_rate_sh7757(struct net_device
*ndev
)
687 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
689 switch (mdp
->speed
) {
690 case 10: /* 10BASE */
691 sh_eth_write(ndev
, 0, RTRATE
);
693 case 100:/* 100BASE */
694 sh_eth_write(ndev
, 1, RTRATE
);
700 static struct sh_eth_cpu_data sh7757_data
= {
701 .set_duplex
= sh_eth_set_duplex
,
702 .set_rate
= sh_eth_set_rate_sh7757
,
704 .register_type
= SH_ETH_REG_FAST_SH4
,
706 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
708 .tx_check
= EESR_FTC
| EESR_CND
| EESR_DLC
| EESR_CD
| EESR_RTO
,
709 .eesr_err_check
= EESR_TWB
| EESR_TABT
| EESR_RABT
| EESR_RFE
|
710 EESR_RDE
| EESR_RFRMER
| EESR_TFE
| EESR_TDE
|
713 .irq_flags
= IRQF_SHARED
,
720 .rpadir_value
= 2 << 16,
724 #define SH_GIGA_ETH_BASE 0xfee00000UL
725 #define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8)
726 #define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0)
727 static void sh_eth_chip_reset_giga(struct net_device
*ndev
)
729 u32 mahr
[2], malr
[2];
732 /* save MAHR and MALR */
733 for (i
= 0; i
< 2; i
++) {
734 malr
[i
] = ioread32((void *)GIGA_MALR(i
));
735 mahr
[i
] = ioread32((void *)GIGA_MAHR(i
));
738 sh_eth_chip_reset(ndev
);
740 /* restore MAHR and MALR */
741 for (i
= 0; i
< 2; i
++) {
742 iowrite32(malr
[i
], (void *)GIGA_MALR(i
));
743 iowrite32(mahr
[i
], (void *)GIGA_MAHR(i
));
747 static void sh_eth_set_rate_giga(struct net_device
*ndev
)
749 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
751 switch (mdp
->speed
) {
752 case 10: /* 10BASE */
753 sh_eth_write(ndev
, 0x00000000, GECMR
);
755 case 100:/* 100BASE */
756 sh_eth_write(ndev
, 0x00000010, GECMR
);
758 case 1000: /* 1000BASE */
759 sh_eth_write(ndev
, 0x00000020, GECMR
);
764 /* SH7757(GETHERC) */
765 static struct sh_eth_cpu_data sh7757_data_giga
= {
766 .chip_reset
= sh_eth_chip_reset_giga
,
767 .set_duplex
= sh_eth_set_duplex
,
768 .set_rate
= sh_eth_set_rate_giga
,
770 .register_type
= SH_ETH_REG_GIGABIT
,
772 .ecsr_value
= ECSR_ICD
| ECSR_MPD
,
773 .ecsipr_value
= ECSIPR_LCHNGIP
| ECSIPR_ICDIP
| ECSIPR_MPDIP
,
774 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
776 .tx_check
= EESR_TC1
| EESR_FTC
,
777 .eesr_err_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_RABT
|
778 EESR_RFE
| EESR_RDE
| EESR_RFRMER
| EESR_TFE
|
780 .fdr_value
= 0x0000072f,
782 .irq_flags
= IRQF_SHARED
,
789 .rpadir_value
= 2 << 16,
796 static struct sh_eth_cpu_data sh7734_data
= {
797 .chip_reset
= sh_eth_chip_reset
,
798 .set_duplex
= sh_eth_set_duplex
,
799 .set_rate
= sh_eth_set_rate_gether
,
801 .register_type
= SH_ETH_REG_GIGABIT
,
803 .ecsr_value
= ECSR_ICD
| ECSR_MPD
,
804 .ecsipr_value
= ECSIPR_LCHNGIP
| ECSIPR_ICDIP
| ECSIPR_MPDIP
,
805 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
807 .tx_check
= EESR_TC1
| EESR_FTC
,
808 .eesr_err_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_RABT
|
809 EESR_RFE
| EESR_RDE
| EESR_RFRMER
| EESR_TFE
|
825 static struct sh_eth_cpu_data sh7763_data
= {
826 .chip_reset
= sh_eth_chip_reset
,
827 .set_duplex
= sh_eth_set_duplex
,
828 .set_rate
= sh_eth_set_rate_gether
,
830 .register_type
= SH_ETH_REG_GIGABIT
,
832 .ecsr_value
= ECSR_ICD
| ECSR_MPD
,
833 .ecsipr_value
= ECSIPR_LCHNGIP
| ECSIPR_ICDIP
| ECSIPR_MPDIP
,
834 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
836 .tx_check
= EESR_TC1
| EESR_FTC
,
837 .eesr_err_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_RABT
|
838 EESR_RDE
| EESR_RFRMER
| EESR_TFE
| EESR_TDE
|
849 .irq_flags
= IRQF_SHARED
,
852 static struct sh_eth_cpu_data sh7619_data
= {
853 .register_type
= SH_ETH_REG_FAST_SH3_SH2
,
855 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
863 static struct sh_eth_cpu_data sh771x_data
= {
864 .register_type
= SH_ETH_REG_FAST_SH3_SH2
,
866 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
870 static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data
*cd
)
873 cd
->ecsr_value
= DEFAULT_ECSR_INIT
;
875 if (!cd
->ecsipr_value
)
876 cd
->ecsipr_value
= DEFAULT_ECSIPR_INIT
;
878 if (!cd
->fcftr_value
)
879 cd
->fcftr_value
= DEFAULT_FIFO_F_D_RFF
|
880 DEFAULT_FIFO_F_D_RFD
;
883 cd
->fdr_value
= DEFAULT_FDR_INIT
;
886 cd
->tx_check
= DEFAULT_TX_CHECK
;
888 if (!cd
->eesr_err_check
)
889 cd
->eesr_err_check
= DEFAULT_EESR_ERR_CHECK
;
891 if (!cd
->trscer_err_mask
)
892 cd
->trscer_err_mask
= DEFAULT_TRSCER_ERR_MASK
;
895 static int sh_eth_check_reset(struct net_device
*ndev
)
901 if (!(sh_eth_read(ndev
, EDMR
) & EDMR_SRST_GETHER
))
907 netdev_err(ndev
, "Device reset failed\n");
913 static int sh_eth_reset(struct net_device
*ndev
)
915 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
918 if (sh_eth_is_gether(mdp
) || sh_eth_is_rz_fast_ether(mdp
)) {
919 sh_eth_write(ndev
, EDSR_ENALL
, EDSR
);
920 sh_eth_modify(ndev
, EDMR
, EDMR_SRST_GETHER
, EDMR_SRST_GETHER
);
922 ret
= sh_eth_check_reset(ndev
);
927 sh_eth_write(ndev
, 0x0, TDLAR
);
928 sh_eth_write(ndev
, 0x0, TDFAR
);
929 sh_eth_write(ndev
, 0x0, TDFXR
);
930 sh_eth_write(ndev
, 0x0, TDFFR
);
931 sh_eth_write(ndev
, 0x0, RDLAR
);
932 sh_eth_write(ndev
, 0x0, RDFAR
);
933 sh_eth_write(ndev
, 0x0, RDFXR
);
934 sh_eth_write(ndev
, 0x0, RDFFR
);
936 /* Reset HW CRC register */
938 sh_eth_write(ndev
, 0x0, CSMR
);
940 /* Select MII mode */
941 if (mdp
->cd
->select_mii
)
942 sh_eth_select_mii(ndev
);
944 sh_eth_modify(ndev
, EDMR
, EDMR_SRST_ETHER
, EDMR_SRST_ETHER
);
946 sh_eth_modify(ndev
, EDMR
, EDMR_SRST_ETHER
, 0);
952 static void sh_eth_set_receive_align(struct sk_buff
*skb
)
954 uintptr_t reserve
= (uintptr_t)skb
->data
& (SH_ETH_RX_ALIGN
- 1);
957 skb_reserve(skb
, SH_ETH_RX_ALIGN
- reserve
);
960 /* Program the hardware MAC address from dev->dev_addr. */
961 static void update_mac_address(struct net_device
*ndev
)
964 (ndev
->dev_addr
[0] << 24) | (ndev
->dev_addr
[1] << 16) |
965 (ndev
->dev_addr
[2] << 8) | (ndev
->dev_addr
[3]), MAHR
);
967 (ndev
->dev_addr
[4] << 8) | (ndev
->dev_addr
[5]), MALR
);
970 /* Get MAC address from SuperH MAC address register
972 * SuperH's Ethernet device doesn't have 'ROM' to MAC address.
973 * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g).
974 * When you want use this device, you must set MAC address in bootloader.
977 static void read_mac_address(struct net_device
*ndev
, unsigned char *mac
)
979 if (mac
[0] || mac
[1] || mac
[2] || mac
[3] || mac
[4] || mac
[5]) {
980 memcpy(ndev
->dev_addr
, mac
, ETH_ALEN
);
982 u32 mahr
= sh_eth_read(ndev
, MAHR
);
983 u32 malr
= sh_eth_read(ndev
, MALR
);
985 ndev
->dev_addr
[0] = (mahr
>> 24) & 0xFF;
986 ndev
->dev_addr
[1] = (mahr
>> 16) & 0xFF;
987 ndev
->dev_addr
[2] = (mahr
>> 8) & 0xFF;
988 ndev
->dev_addr
[3] = (mahr
>> 0) & 0xFF;
989 ndev
->dev_addr
[4] = (malr
>> 8) & 0xFF;
990 ndev
->dev_addr
[5] = (malr
>> 0) & 0xFF;
994 static u32
sh_eth_get_edtrr_trns(struct sh_eth_private
*mdp
)
996 if (sh_eth_is_gether(mdp
) || sh_eth_is_rz_fast_ether(mdp
))
997 return EDTRR_TRNS_GETHER
;
999 return EDTRR_TRNS_ETHER
;
1003 void (*set_gate
)(void *addr
);
1004 struct mdiobb_ctrl ctrl
;
1008 static void sh_mdio_ctrl(struct mdiobb_ctrl
*ctrl
, u32 mask
, int set
)
1010 struct bb_info
*bitbang
= container_of(ctrl
, struct bb_info
, ctrl
);
1013 if (bitbang
->set_gate
)
1014 bitbang
->set_gate(bitbang
->addr
);
1016 pir
= ioread32(bitbang
->addr
);
1021 iowrite32(pir
, bitbang
->addr
);
1024 /* Data I/O pin control */
1025 static void sh_mmd_ctrl(struct mdiobb_ctrl
*ctrl
, int bit
)
1027 sh_mdio_ctrl(ctrl
, PIR_MMD
, bit
);
1031 static void sh_set_mdio(struct mdiobb_ctrl
*ctrl
, int bit
)
1033 sh_mdio_ctrl(ctrl
, PIR_MDO
, bit
);
1037 static int sh_get_mdio(struct mdiobb_ctrl
*ctrl
)
1039 struct bb_info
*bitbang
= container_of(ctrl
, struct bb_info
, ctrl
);
1041 if (bitbang
->set_gate
)
1042 bitbang
->set_gate(bitbang
->addr
);
1044 return (ioread32(bitbang
->addr
) & PIR_MDI
) != 0;
1047 /* MDC pin control */
1048 static void sh_mdc_ctrl(struct mdiobb_ctrl
*ctrl
, int bit
)
1050 sh_mdio_ctrl(ctrl
, PIR_MDC
, bit
);
1053 /* mdio bus control struct */
1054 static struct mdiobb_ops bb_ops
= {
1055 .owner
= THIS_MODULE
,
1056 .set_mdc
= sh_mdc_ctrl
,
1057 .set_mdio_dir
= sh_mmd_ctrl
,
1058 .set_mdio_data
= sh_set_mdio
,
1059 .get_mdio_data
= sh_get_mdio
,
1062 /* free Tx skb function */
1063 static int sh_eth_tx_free(struct net_device
*ndev
, bool sent_only
)
1065 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1066 struct sh_eth_txdesc
*txdesc
;
1071 for (; mdp
->cur_tx
- mdp
->dirty_tx
> 0; mdp
->dirty_tx
++) {
1072 entry
= mdp
->dirty_tx
% mdp
->num_tx_ring
;
1073 txdesc
= &mdp
->tx_ring
[entry
];
1074 sent
= !(txdesc
->status
& cpu_to_le32(TD_TACT
));
1075 if (sent_only
&& !sent
)
1077 /* TACT bit must be checked before all the following reads */
1079 netif_info(mdp
, tx_done
, ndev
,
1080 "tx entry %d status 0x%08x\n",
1081 entry
, le32_to_cpu(txdesc
->status
));
1082 /* Free the original skb. */
1083 if (mdp
->tx_skbuff
[entry
]) {
1084 dma_unmap_single(&ndev
->dev
, le32_to_cpu(txdesc
->addr
),
1085 le32_to_cpu(txdesc
->len
) >> 16,
1087 dev_kfree_skb_irq(mdp
->tx_skbuff
[entry
]);
1088 mdp
->tx_skbuff
[entry
] = NULL
;
1091 txdesc
->status
= cpu_to_le32(TD_TFP
);
1092 if (entry
>= mdp
->num_tx_ring
- 1)
1093 txdesc
->status
|= cpu_to_le32(TD_TDLE
);
1096 ndev
->stats
.tx_packets
++;
1097 ndev
->stats
.tx_bytes
+= le32_to_cpu(txdesc
->len
) >> 16;
1103 /* free skb and descriptor buffer */
1104 static void sh_eth_ring_free(struct net_device
*ndev
)
1106 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1110 for (i
= 0; i
< mdp
->num_rx_ring
; i
++) {
1111 if (mdp
->rx_skbuff
[i
]) {
1112 struct sh_eth_rxdesc
*rxdesc
= &mdp
->rx_ring
[i
];
1114 dma_unmap_single(&ndev
->dev
,
1115 le32_to_cpu(rxdesc
->addr
),
1116 ALIGN(mdp
->rx_buf_sz
, 32),
1120 ringsize
= sizeof(struct sh_eth_rxdesc
) * mdp
->num_rx_ring
;
1121 dma_free_coherent(NULL
, ringsize
, mdp
->rx_ring
,
1123 mdp
->rx_ring
= NULL
;
1126 /* Free Rx skb ringbuffer */
1127 if (mdp
->rx_skbuff
) {
1128 for (i
= 0; i
< mdp
->num_rx_ring
; i
++)
1129 dev_kfree_skb(mdp
->rx_skbuff
[i
]);
1131 kfree(mdp
->rx_skbuff
);
1132 mdp
->rx_skbuff
= NULL
;
1135 sh_eth_tx_free(ndev
, false);
1137 ringsize
= sizeof(struct sh_eth_txdesc
) * mdp
->num_tx_ring
;
1138 dma_free_coherent(NULL
, ringsize
, mdp
->tx_ring
,
1140 mdp
->tx_ring
= NULL
;
1143 /* Free Tx skb ringbuffer */
1144 kfree(mdp
->tx_skbuff
);
1145 mdp
->tx_skbuff
= NULL
;
1148 /* format skb and descriptor buffer */
1149 static void sh_eth_ring_format(struct net_device
*ndev
)
1151 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1153 struct sk_buff
*skb
;
1154 struct sh_eth_rxdesc
*rxdesc
= NULL
;
1155 struct sh_eth_txdesc
*txdesc
= NULL
;
1156 int rx_ringsize
= sizeof(*rxdesc
) * mdp
->num_rx_ring
;
1157 int tx_ringsize
= sizeof(*txdesc
) * mdp
->num_tx_ring
;
1158 int skbuff_size
= mdp
->rx_buf_sz
+ SH_ETH_RX_ALIGN
+ 32 - 1;
1159 dma_addr_t dma_addr
;
1167 memset(mdp
->rx_ring
, 0, rx_ringsize
);
1169 /* build Rx ring buffer */
1170 for (i
= 0; i
< mdp
->num_rx_ring
; i
++) {
1172 mdp
->rx_skbuff
[i
] = NULL
;
1173 skb
= netdev_alloc_skb(ndev
, skbuff_size
);
1176 sh_eth_set_receive_align(skb
);
1178 /* The size of the buffer is a multiple of 32 bytes. */
1179 buf_len
= ALIGN(mdp
->rx_buf_sz
, 32);
1180 dma_addr
= dma_map_single(&ndev
->dev
, skb
->data
, buf_len
,
1182 if (dma_mapping_error(&ndev
->dev
, dma_addr
)) {
1186 mdp
->rx_skbuff
[i
] = skb
;
1189 rxdesc
= &mdp
->rx_ring
[i
];
1190 rxdesc
->len
= cpu_to_le32(buf_len
<< 16);
1191 rxdesc
->addr
= cpu_to_le32(dma_addr
);
1192 rxdesc
->status
= cpu_to_le32(RD_RACT
| RD_RFP
);
1194 /* Rx descriptor address set */
1196 sh_eth_write(ndev
, mdp
->rx_desc_dma
, RDLAR
);
1197 if (sh_eth_is_gether(mdp
) ||
1198 sh_eth_is_rz_fast_ether(mdp
))
1199 sh_eth_write(ndev
, mdp
->rx_desc_dma
, RDFAR
);
1203 mdp
->dirty_rx
= (u32
) (i
- mdp
->num_rx_ring
);
1205 /* Mark the last entry as wrapping the ring. */
1207 rxdesc
->status
|= cpu_to_le32(RD_RDLE
);
1209 memset(mdp
->tx_ring
, 0, tx_ringsize
);
1211 /* build Tx ring buffer */
1212 for (i
= 0; i
< mdp
->num_tx_ring
; i
++) {
1213 mdp
->tx_skbuff
[i
] = NULL
;
1214 txdesc
= &mdp
->tx_ring
[i
];
1215 txdesc
->status
= cpu_to_le32(TD_TFP
);
1216 txdesc
->len
= cpu_to_le32(0);
1218 /* Tx descriptor address set */
1219 sh_eth_write(ndev
, mdp
->tx_desc_dma
, TDLAR
);
1220 if (sh_eth_is_gether(mdp
) ||
1221 sh_eth_is_rz_fast_ether(mdp
))
1222 sh_eth_write(ndev
, mdp
->tx_desc_dma
, TDFAR
);
1226 txdesc
->status
|= cpu_to_le32(TD_TDLE
);
1229 /* Get skb and descriptor buffer */
1230 static int sh_eth_ring_init(struct net_device
*ndev
)
1232 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1233 int rx_ringsize
, tx_ringsize
;
1235 /* +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
1236 * card needs room to do 8 byte alignment, +2 so we can reserve
1237 * the first 2 bytes, and +16 gets room for the status word from the
1240 mdp
->rx_buf_sz
= (ndev
->mtu
<= 1492 ? PKT_BUF_SZ
:
1241 (((ndev
->mtu
+ 26 + 7) & ~7) + 2 + 16));
1242 if (mdp
->cd
->rpadir
)
1243 mdp
->rx_buf_sz
+= NET_IP_ALIGN
;
1245 /* Allocate RX and TX skb rings */
1246 mdp
->rx_skbuff
= kcalloc(mdp
->num_rx_ring
, sizeof(*mdp
->rx_skbuff
),
1248 if (!mdp
->rx_skbuff
)
1251 mdp
->tx_skbuff
= kcalloc(mdp
->num_tx_ring
, sizeof(*mdp
->tx_skbuff
),
1253 if (!mdp
->tx_skbuff
)
1256 /* Allocate all Rx descriptors. */
1257 rx_ringsize
= sizeof(struct sh_eth_rxdesc
) * mdp
->num_rx_ring
;
1258 mdp
->rx_ring
= dma_alloc_coherent(NULL
, rx_ringsize
, &mdp
->rx_desc_dma
,
1265 /* Allocate all Tx descriptors. */
1266 tx_ringsize
= sizeof(struct sh_eth_txdesc
) * mdp
->num_tx_ring
;
1267 mdp
->tx_ring
= dma_alloc_coherent(NULL
, tx_ringsize
, &mdp
->tx_desc_dma
,
1274 /* Free Rx and Tx skb ring buffer and DMA buffer */
1275 sh_eth_ring_free(ndev
);
1280 static int sh_eth_dev_init(struct net_device
*ndev
)
1282 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1286 ret
= sh_eth_reset(ndev
);
1290 if (mdp
->cd
->rmiimode
)
1291 sh_eth_write(ndev
, 0x1, RMIIMODE
);
1293 /* Descriptor format */
1294 sh_eth_ring_format(ndev
);
1295 if (mdp
->cd
->rpadir
)
1296 sh_eth_write(ndev
, mdp
->cd
->rpadir_value
, RPADIR
);
1298 /* all sh_eth int mask */
1299 sh_eth_write(ndev
, 0, EESIPR
);
1301 #if defined(__LITTLE_ENDIAN)
1302 if (mdp
->cd
->hw_swap
)
1303 sh_eth_write(ndev
, EDMR_EL
, EDMR
);
1306 sh_eth_write(ndev
, 0, EDMR
);
1309 sh_eth_write(ndev
, mdp
->cd
->fdr_value
, FDR
);
1310 sh_eth_write(ndev
, 0, TFTR
);
1312 /* Frame recv control (enable multiple-packets per rx irq) */
1313 sh_eth_write(ndev
, RMCR_RNC
, RMCR
);
1315 sh_eth_write(ndev
, mdp
->cd
->trscer_err_mask
, TRSCER
);
1318 sh_eth_write(ndev
, 0x800, BCULR
); /* Burst sycle set */
1320 sh_eth_write(ndev
, mdp
->cd
->fcftr_value
, FCFTR
);
1322 if (!mdp
->cd
->no_trimd
)
1323 sh_eth_write(ndev
, 0, TRIMD
);
1325 /* Recv frame limit set register */
1326 sh_eth_write(ndev
, ndev
->mtu
+ ETH_HLEN
+ VLAN_HLEN
+ ETH_FCS_LEN
,
1329 sh_eth_modify(ndev
, EESR
, 0, 0);
1330 mdp
->irq_enabled
= true;
1331 sh_eth_write(ndev
, mdp
->cd
->eesipr_value
, EESIPR
);
1333 /* PAUSE Prohibition */
1334 sh_eth_write(ndev
, ECMR_ZPF
| (mdp
->duplex
? ECMR_DM
: 0) |
1335 ECMR_TE
| ECMR_RE
, ECMR
);
1337 if (mdp
->cd
->set_rate
)
1338 mdp
->cd
->set_rate(ndev
);
1340 /* E-MAC Status Register clear */
1341 sh_eth_write(ndev
, mdp
->cd
->ecsr_value
, ECSR
);
1343 /* E-MAC Interrupt Enable register */
1344 sh_eth_write(ndev
, mdp
->cd
->ecsipr_value
, ECSIPR
);
1346 /* Set MAC address */
1347 update_mac_address(ndev
);
1351 sh_eth_write(ndev
, APR_AP
, APR
);
1353 sh_eth_write(ndev
, MPR_MP
, MPR
);
1354 if (mdp
->cd
->tpauser
)
1355 sh_eth_write(ndev
, TPAUSER_UNLIMITED
, TPAUSER
);
1357 /* Setting the Rx mode will start the Rx process. */
1358 sh_eth_write(ndev
, EDRRR_R
, EDRRR
);
1363 static void sh_eth_dev_exit(struct net_device
*ndev
)
1365 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1368 /* Deactivate all TX descriptors, so DMA should stop at next
1369 * packet boundary if it's currently running
1371 for (i
= 0; i
< mdp
->num_tx_ring
; i
++)
1372 mdp
->tx_ring
[i
].status
&= ~cpu_to_le32(TD_TACT
);
1374 /* Disable TX FIFO egress to MAC */
1375 sh_eth_rcv_snd_disable(ndev
);
1377 /* Stop RX DMA at next packet boundary */
1378 sh_eth_write(ndev
, 0, EDRRR
);
1380 /* Aside from TX DMA, we can't tell when the hardware is
1381 * really stopped, so we need to reset to make sure.
1382 * Before doing that, wait for long enough to *probably*
1383 * finish transmitting the last packet and poll stats.
1385 msleep(2); /* max frame time at 10 Mbps < 1250 us */
1386 sh_eth_get_stats(ndev
);
1389 /* Set MAC address again */
1390 update_mac_address(ndev
);
1393 /* Packet receive function */
1394 static int sh_eth_rx(struct net_device
*ndev
, u32 intr_status
, int *quota
)
1396 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1397 struct sh_eth_rxdesc
*rxdesc
;
1399 int entry
= mdp
->cur_rx
% mdp
->num_rx_ring
;
1400 int boguscnt
= (mdp
->dirty_rx
+ mdp
->num_rx_ring
) - mdp
->cur_rx
;
1402 struct sk_buff
*skb
;
1404 int skbuff_size
= mdp
->rx_buf_sz
+ SH_ETH_RX_ALIGN
+ 32 - 1;
1405 dma_addr_t dma_addr
;
1409 boguscnt
= min(boguscnt
, *quota
);
1411 rxdesc
= &mdp
->rx_ring
[entry
];
1412 while (!(rxdesc
->status
& cpu_to_le32(RD_RACT
))) {
1413 /* RACT bit must be checked before all the following reads */
1415 desc_status
= le32_to_cpu(rxdesc
->status
);
1416 pkt_len
= le32_to_cpu(rxdesc
->len
) & RD_RFL
;
1421 netif_info(mdp
, rx_status
, ndev
,
1422 "rx entry %d status 0x%08x len %d\n",
1423 entry
, desc_status
, pkt_len
);
1425 if (!(desc_status
& RDFEND
))
1426 ndev
->stats
.rx_length_errors
++;
1428 /* In case of almost all GETHER/ETHERs, the Receive Frame State
1429 * (RFS) bits in the Receive Descriptor 0 are from bit 9 to
1430 * bit 0. However, in case of the R8A7740 and R7S72100
1431 * the RFS bits are from bit 25 to bit 16. So, the
1432 * driver needs right shifting by 16.
1434 if (mdp
->cd
->shift_rd0
)
1437 skb
= mdp
->rx_skbuff
[entry
];
1438 if (desc_status
& (RD_RFS1
| RD_RFS2
| RD_RFS3
| RD_RFS4
|
1439 RD_RFS5
| RD_RFS6
| RD_RFS10
)) {
1440 ndev
->stats
.rx_errors
++;
1441 if (desc_status
& RD_RFS1
)
1442 ndev
->stats
.rx_crc_errors
++;
1443 if (desc_status
& RD_RFS2
)
1444 ndev
->stats
.rx_frame_errors
++;
1445 if (desc_status
& RD_RFS3
)
1446 ndev
->stats
.rx_length_errors
++;
1447 if (desc_status
& RD_RFS4
)
1448 ndev
->stats
.rx_length_errors
++;
1449 if (desc_status
& RD_RFS6
)
1450 ndev
->stats
.rx_missed_errors
++;
1451 if (desc_status
& RD_RFS10
)
1452 ndev
->stats
.rx_over_errors
++;
1454 dma_addr
= le32_to_cpu(rxdesc
->addr
);
1455 if (!mdp
->cd
->hw_swap
)
1457 phys_to_virt(ALIGN(dma_addr
, 4)),
1459 mdp
->rx_skbuff
[entry
] = NULL
;
1460 if (mdp
->cd
->rpadir
)
1461 skb_reserve(skb
, NET_IP_ALIGN
);
1462 dma_unmap_single(&ndev
->dev
, dma_addr
,
1463 ALIGN(mdp
->rx_buf_sz
, 32),
1465 skb_put(skb
, pkt_len
);
1466 skb
->protocol
= eth_type_trans(skb
, ndev
);
1467 netif_receive_skb(skb
);
1468 ndev
->stats
.rx_packets
++;
1469 ndev
->stats
.rx_bytes
+= pkt_len
;
1470 if (desc_status
& RD_RFS8
)
1471 ndev
->stats
.multicast
++;
1473 entry
= (++mdp
->cur_rx
) % mdp
->num_rx_ring
;
1474 rxdesc
= &mdp
->rx_ring
[entry
];
1477 /* Refill the Rx ring buffers. */
1478 for (; mdp
->cur_rx
- mdp
->dirty_rx
> 0; mdp
->dirty_rx
++) {
1479 entry
= mdp
->dirty_rx
% mdp
->num_rx_ring
;
1480 rxdesc
= &mdp
->rx_ring
[entry
];
1481 /* The size of the buffer is 32 byte boundary. */
1482 buf_len
= ALIGN(mdp
->rx_buf_sz
, 32);
1483 rxdesc
->len
= cpu_to_le32(buf_len
<< 16);
1485 if (mdp
->rx_skbuff
[entry
] == NULL
) {
1486 skb
= netdev_alloc_skb(ndev
, skbuff_size
);
1488 break; /* Better luck next round. */
1489 sh_eth_set_receive_align(skb
);
1490 dma_addr
= dma_map_single(&ndev
->dev
, skb
->data
,
1491 buf_len
, DMA_FROM_DEVICE
);
1492 if (dma_mapping_error(&ndev
->dev
, dma_addr
)) {
1496 mdp
->rx_skbuff
[entry
] = skb
;
1498 skb_checksum_none_assert(skb
);
1499 rxdesc
->addr
= cpu_to_le32(dma_addr
);
1501 dma_wmb(); /* RACT bit must be set after all the above writes */
1502 if (entry
>= mdp
->num_rx_ring
- 1)
1504 cpu_to_le32(RD_RACT
| RD_RFP
| RD_RDLE
);
1506 rxdesc
->status
|= cpu_to_le32(RD_RACT
| RD_RFP
);
1509 /* Restart Rx engine if stopped. */
1510 /* If we don't need to check status, don't. -KDU */
1511 if (!(sh_eth_read(ndev
, EDRRR
) & EDRRR_R
)) {
1512 /* fix the values for the next receiving if RDE is set */
1513 if (intr_status
& EESR_RDE
&&
1514 mdp
->reg_offset
[RDFAR
] != SH_ETH_OFFSET_INVALID
) {
1515 u32 count
= (sh_eth_read(ndev
, RDFAR
) -
1516 sh_eth_read(ndev
, RDLAR
)) >> 4;
1518 mdp
->cur_rx
= count
;
1519 mdp
->dirty_rx
= count
;
1521 sh_eth_write(ndev
, EDRRR_R
, EDRRR
);
1524 *quota
-= limit
- boguscnt
- 1;
1529 static void sh_eth_rcv_snd_disable(struct net_device
*ndev
)
1531 /* disable tx and rx */
1532 sh_eth_modify(ndev
, ECMR
, ECMR_RE
| ECMR_TE
, 0);
1535 static void sh_eth_rcv_snd_enable(struct net_device
*ndev
)
1537 /* enable tx and rx */
1538 sh_eth_modify(ndev
, ECMR
, ECMR_RE
| ECMR_TE
, ECMR_RE
| ECMR_TE
);
1541 /* error control function */
1542 static void sh_eth_error(struct net_device
*ndev
, u32 intr_status
)
1544 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1549 if (intr_status
& EESR_ECI
) {
1550 felic_stat
= sh_eth_read(ndev
, ECSR
);
1551 sh_eth_write(ndev
, felic_stat
, ECSR
); /* clear int */
1552 if (felic_stat
& ECSR_ICD
)
1553 ndev
->stats
.tx_carrier_errors
++;
1554 if (felic_stat
& ECSR_LCHNG
) {
1556 if (mdp
->cd
->no_psr
|| mdp
->no_ether_link
) {
1559 link_stat
= (sh_eth_read(ndev
, PSR
));
1560 if (mdp
->ether_link_active_low
)
1561 link_stat
= ~link_stat
;
1563 if (!(link_stat
& PHY_ST_LINK
)) {
1564 sh_eth_rcv_snd_disable(ndev
);
1567 sh_eth_modify(ndev
, EESIPR
, DMAC_M_ECI
, 0);
1569 sh_eth_modify(ndev
, ECSR
, 0, 0);
1570 sh_eth_modify(ndev
, EESIPR
, DMAC_M_ECI
,
1572 /* enable tx and rx */
1573 sh_eth_rcv_snd_enable(ndev
);
1579 if (intr_status
& EESR_TWB
) {
1580 /* Unused write back interrupt */
1581 if (intr_status
& EESR_TABT
) { /* Transmit Abort int */
1582 ndev
->stats
.tx_aborted_errors
++;
1583 netif_err(mdp
, tx_err
, ndev
, "Transmit Abort\n");
1587 if (intr_status
& EESR_RABT
) {
1588 /* Receive Abort int */
1589 if (intr_status
& EESR_RFRMER
) {
1590 /* Receive Frame Overflow int */
1591 ndev
->stats
.rx_frame_errors
++;
1595 if (intr_status
& EESR_TDE
) {
1596 /* Transmit Descriptor Empty int */
1597 ndev
->stats
.tx_fifo_errors
++;
1598 netif_err(mdp
, tx_err
, ndev
, "Transmit Descriptor Empty\n");
1601 if (intr_status
& EESR_TFE
) {
1602 /* FIFO under flow */
1603 ndev
->stats
.tx_fifo_errors
++;
1604 netif_err(mdp
, tx_err
, ndev
, "Transmit FIFO Under flow\n");
1607 if (intr_status
& EESR_RDE
) {
1608 /* Receive Descriptor Empty int */
1609 ndev
->stats
.rx_over_errors
++;
1612 if (intr_status
& EESR_RFE
) {
1613 /* Receive FIFO Overflow int */
1614 ndev
->stats
.rx_fifo_errors
++;
1617 if (!mdp
->cd
->no_ade
&& (intr_status
& EESR_ADE
)) {
1619 ndev
->stats
.tx_fifo_errors
++;
1620 netif_err(mdp
, tx_err
, ndev
, "Address Error\n");
1623 mask
= EESR_TWB
| EESR_TABT
| EESR_ADE
| EESR_TDE
| EESR_TFE
;
1624 if (mdp
->cd
->no_ade
)
1626 if (intr_status
& mask
) {
1628 u32 edtrr
= sh_eth_read(ndev
, EDTRR
);
1631 netdev_err(ndev
, "TX error. status=%8.8x cur_tx=%8.8x dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
1632 intr_status
, mdp
->cur_tx
, mdp
->dirty_tx
,
1633 (u32
)ndev
->state
, edtrr
);
1634 /* dirty buffer free */
1635 sh_eth_tx_free(ndev
, true);
1638 if (edtrr
^ sh_eth_get_edtrr_trns(mdp
)) {
1640 sh_eth_write(ndev
, sh_eth_get_edtrr_trns(mdp
), EDTRR
);
1643 netif_wake_queue(ndev
);
1647 static irqreturn_t
sh_eth_interrupt(int irq
, void *netdev
)
1649 struct net_device
*ndev
= netdev
;
1650 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1651 struct sh_eth_cpu_data
*cd
= mdp
->cd
;
1652 irqreturn_t ret
= IRQ_NONE
;
1653 u32 intr_status
, intr_enable
;
1655 spin_lock(&mdp
->lock
);
1657 /* Get interrupt status */
1658 intr_status
= sh_eth_read(ndev
, EESR
);
1659 /* Mask it with the interrupt mask, forcing ECI interrupt to be always
1660 * enabled since it's the one that comes thru regardless of the mask,
1661 * and we need to fully handle it in sh_eth_error() in order to quench
1662 * it as it doesn't get cleared by just writing 1 to the ECI bit...
1664 intr_enable
= sh_eth_read(ndev
, EESIPR
);
1665 intr_status
&= intr_enable
| DMAC_M_ECI
;
1666 if (intr_status
& (EESR_RX_CHECK
| cd
->tx_check
| cd
->eesr_err_check
))
1671 if (!likely(mdp
->irq_enabled
)) {
1672 sh_eth_write(ndev
, 0, EESIPR
);
1676 if (intr_status
& EESR_RX_CHECK
) {
1677 if (napi_schedule_prep(&mdp
->napi
)) {
1678 /* Mask Rx interrupts */
1679 sh_eth_write(ndev
, intr_enable
& ~EESR_RX_CHECK
,
1681 __napi_schedule(&mdp
->napi
);
1684 "ignoring interrupt, status 0x%08x, mask 0x%08x.\n",
1685 intr_status
, intr_enable
);
1690 if (intr_status
& cd
->tx_check
) {
1691 /* Clear Tx interrupts */
1692 sh_eth_write(ndev
, intr_status
& cd
->tx_check
, EESR
);
1694 sh_eth_tx_free(ndev
, true);
1695 netif_wake_queue(ndev
);
1698 if (intr_status
& cd
->eesr_err_check
) {
1699 /* Clear error interrupts */
1700 sh_eth_write(ndev
, intr_status
& cd
->eesr_err_check
, EESR
);
1702 sh_eth_error(ndev
, intr_status
);
1706 spin_unlock(&mdp
->lock
);
1711 static int sh_eth_poll(struct napi_struct
*napi
, int budget
)
1713 struct sh_eth_private
*mdp
= container_of(napi
, struct sh_eth_private
,
1715 struct net_device
*ndev
= napi
->dev
;
1720 intr_status
= sh_eth_read(ndev
, EESR
);
1721 if (!(intr_status
& EESR_RX_CHECK
))
1723 /* Clear Rx interrupts */
1724 sh_eth_write(ndev
, intr_status
& EESR_RX_CHECK
, EESR
);
1726 if (sh_eth_rx(ndev
, intr_status
, "a
))
1730 napi_complete(napi
);
1732 /* Reenable Rx interrupts */
1733 if (mdp
->irq_enabled
)
1734 sh_eth_write(ndev
, mdp
->cd
->eesipr_value
, EESIPR
);
1736 return budget
- quota
;
1739 /* PHY state control function */
1740 static void sh_eth_adjust_link(struct net_device
*ndev
)
1742 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1743 struct phy_device
*phydev
= ndev
->phydev
;
1747 if (phydev
->duplex
!= mdp
->duplex
) {
1749 mdp
->duplex
= phydev
->duplex
;
1750 if (mdp
->cd
->set_duplex
)
1751 mdp
->cd
->set_duplex(ndev
);
1754 if (phydev
->speed
!= mdp
->speed
) {
1756 mdp
->speed
= phydev
->speed
;
1757 if (mdp
->cd
->set_rate
)
1758 mdp
->cd
->set_rate(ndev
);
1761 sh_eth_modify(ndev
, ECMR
, ECMR_TXF
, 0);
1763 mdp
->link
= phydev
->link
;
1764 if (mdp
->cd
->no_psr
|| mdp
->no_ether_link
)
1765 sh_eth_rcv_snd_enable(ndev
);
1767 } else if (mdp
->link
) {
1772 if (mdp
->cd
->no_psr
|| mdp
->no_ether_link
)
1773 sh_eth_rcv_snd_disable(ndev
);
1776 if (new_state
&& netif_msg_link(mdp
))
1777 phy_print_status(phydev
);
1780 /* PHY init function */
1781 static int sh_eth_phy_init(struct net_device
*ndev
)
1783 struct device_node
*np
= ndev
->dev
.parent
->of_node
;
1784 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1785 struct phy_device
*phydev
;
1791 /* Try connect to PHY */
1793 struct device_node
*pn
;
1795 pn
= of_parse_phandle(np
, "phy-handle", 0);
1796 phydev
= of_phy_connect(ndev
, pn
,
1797 sh_eth_adjust_link
, 0,
1798 mdp
->phy_interface
);
1802 phydev
= ERR_PTR(-ENOENT
);
1804 char phy_id
[MII_BUS_ID_SIZE
+ 3];
1806 snprintf(phy_id
, sizeof(phy_id
), PHY_ID_FMT
,
1807 mdp
->mii_bus
->id
, mdp
->phy_id
);
1809 phydev
= phy_connect(ndev
, phy_id
, sh_eth_adjust_link
,
1810 mdp
->phy_interface
);
1813 if (IS_ERR(phydev
)) {
1814 netdev_err(ndev
, "failed to connect PHY\n");
1815 return PTR_ERR(phydev
);
1818 phy_attached_info(phydev
);
1823 /* PHY control start function */
1824 static int sh_eth_phy_start(struct net_device
*ndev
)
1828 ret
= sh_eth_phy_init(ndev
);
1832 phy_start(ndev
->phydev
);
1837 static int sh_eth_get_link_ksettings(struct net_device
*ndev
,
1838 struct ethtool_link_ksettings
*cmd
)
1840 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1841 unsigned long flags
;
1847 spin_lock_irqsave(&mdp
->lock
, flags
);
1848 ret
= phy_ethtool_ksettings_get(ndev
->phydev
, cmd
);
1849 spin_unlock_irqrestore(&mdp
->lock
, flags
);
1854 static int sh_eth_set_link_ksettings(struct net_device
*ndev
,
1855 const struct ethtool_link_ksettings
*cmd
)
1857 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1858 unsigned long flags
;
1864 spin_lock_irqsave(&mdp
->lock
, flags
);
1866 /* disable tx and rx */
1867 sh_eth_rcv_snd_disable(ndev
);
1869 ret
= phy_ethtool_ksettings_set(ndev
->phydev
, cmd
);
1873 if (cmd
->base
.duplex
== DUPLEX_FULL
)
1878 if (mdp
->cd
->set_duplex
)
1879 mdp
->cd
->set_duplex(ndev
);
1884 /* enable tx and rx */
1885 sh_eth_rcv_snd_enable(ndev
);
1887 spin_unlock_irqrestore(&mdp
->lock
, flags
);
1892 /* If it is ever necessary to increase SH_ETH_REG_DUMP_MAX_REGS, the
1893 * version must be bumped as well. Just adding registers up to that
1894 * limit is fine, as long as the existing register indices don't
1897 #define SH_ETH_REG_DUMP_VERSION 1
1898 #define SH_ETH_REG_DUMP_MAX_REGS 256
1900 static size_t __sh_eth_get_regs(struct net_device
*ndev
, u32
*buf
)
1902 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1903 struct sh_eth_cpu_data
*cd
= mdp
->cd
;
1907 BUILD_BUG_ON(SH_ETH_MAX_REGISTER_OFFSET
> SH_ETH_REG_DUMP_MAX_REGS
);
1909 /* Dump starts with a bitmap that tells ethtool which
1910 * registers are defined for this chip.
1912 len
= DIV_ROUND_UP(SH_ETH_REG_DUMP_MAX_REGS
, 32);
1920 /* Add a register to the dump, if it has a defined offset.
1921 * This automatically skips most undefined registers, but for
1922 * some it is also necessary to check a capability flag in
1923 * struct sh_eth_cpu_data.
1925 #define mark_reg_valid(reg) valid_map[reg / 32] |= 1U << (reg % 32)
1926 #define add_reg_from(reg, read_expr) do { \
1927 if (mdp->reg_offset[reg] != SH_ETH_OFFSET_INVALID) { \
1929 mark_reg_valid(reg); \
1930 *buf++ = read_expr; \
1935 #define add_reg(reg) add_reg_from(reg, sh_eth_read(ndev, reg))
1936 #define add_tsu_reg(reg) add_reg_from(reg, sh_eth_tsu_read(mdp, reg))
2008 add_tsu_reg(TSU_CTRST
);
2009 add_tsu_reg(TSU_FWEN0
);
2010 add_tsu_reg(TSU_FWEN1
);
2011 add_tsu_reg(TSU_FCM
);
2012 add_tsu_reg(TSU_BSYSL0
);
2013 add_tsu_reg(TSU_BSYSL1
);
2014 add_tsu_reg(TSU_PRISL0
);
2015 add_tsu_reg(TSU_PRISL1
);
2016 add_tsu_reg(TSU_FWSL0
);
2017 add_tsu_reg(TSU_FWSL1
);
2018 add_tsu_reg(TSU_FWSLC
);
2019 add_tsu_reg(TSU_QTAG0
);
2020 add_tsu_reg(TSU_QTAG1
);
2021 add_tsu_reg(TSU_QTAGM0
);
2022 add_tsu_reg(TSU_QTAGM1
);
2023 add_tsu_reg(TSU_FWSR
);
2024 add_tsu_reg(TSU_FWINMK
);
2025 add_tsu_reg(TSU_ADQT0
);
2026 add_tsu_reg(TSU_ADQT1
);
2027 add_tsu_reg(TSU_VTAG0
);
2028 add_tsu_reg(TSU_VTAG1
);
2029 add_tsu_reg(TSU_ADSBSY
);
2030 add_tsu_reg(TSU_TEN
);
2031 add_tsu_reg(TSU_POST1
);
2032 add_tsu_reg(TSU_POST2
);
2033 add_tsu_reg(TSU_POST3
);
2034 add_tsu_reg(TSU_POST4
);
2035 if (mdp
->reg_offset
[TSU_ADRH0
] != SH_ETH_OFFSET_INVALID
) {
2036 /* This is the start of a table, not just a single
2042 mark_reg_valid(TSU_ADRH0
);
2043 for (i
= 0; i
< SH_ETH_TSU_CAM_ENTRIES
* 2; i
++)
2046 mdp
->reg_offset
[TSU_ADRH0
] +
2049 len
+= SH_ETH_TSU_CAM_ENTRIES
* 2;
2053 #undef mark_reg_valid
2061 static int sh_eth_get_regs_len(struct net_device
*ndev
)
2063 return __sh_eth_get_regs(ndev
, NULL
);
2066 static void sh_eth_get_regs(struct net_device
*ndev
, struct ethtool_regs
*regs
,
2069 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2071 regs
->version
= SH_ETH_REG_DUMP_VERSION
;
2073 pm_runtime_get_sync(&mdp
->pdev
->dev
);
2074 __sh_eth_get_regs(ndev
, buf
);
2075 pm_runtime_put_sync(&mdp
->pdev
->dev
);
2078 static int sh_eth_nway_reset(struct net_device
*ndev
)
2080 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2081 unsigned long flags
;
2087 spin_lock_irqsave(&mdp
->lock
, flags
);
2088 ret
= phy_start_aneg(ndev
->phydev
);
2089 spin_unlock_irqrestore(&mdp
->lock
, flags
);
2094 static u32
sh_eth_get_msglevel(struct net_device
*ndev
)
2096 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2097 return mdp
->msg_enable
;
2100 static void sh_eth_set_msglevel(struct net_device
*ndev
, u32 value
)
2102 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2103 mdp
->msg_enable
= value
;
2106 static const char sh_eth_gstrings_stats
[][ETH_GSTRING_LEN
] = {
2107 "rx_current", "tx_current",
2108 "rx_dirty", "tx_dirty",
2110 #define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats)
2112 static int sh_eth_get_sset_count(struct net_device
*netdev
, int sset
)
2116 return SH_ETH_STATS_LEN
;
2122 static void sh_eth_get_ethtool_stats(struct net_device
*ndev
,
2123 struct ethtool_stats
*stats
, u64
*data
)
2125 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2128 /* device-specific stats */
2129 data
[i
++] = mdp
->cur_rx
;
2130 data
[i
++] = mdp
->cur_tx
;
2131 data
[i
++] = mdp
->dirty_rx
;
2132 data
[i
++] = mdp
->dirty_tx
;
2135 static void sh_eth_get_strings(struct net_device
*ndev
, u32 stringset
, u8
*data
)
2137 switch (stringset
) {
2139 memcpy(data
, *sh_eth_gstrings_stats
,
2140 sizeof(sh_eth_gstrings_stats
));
2145 static void sh_eth_get_ringparam(struct net_device
*ndev
,
2146 struct ethtool_ringparam
*ring
)
2148 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2150 ring
->rx_max_pending
= RX_RING_MAX
;
2151 ring
->tx_max_pending
= TX_RING_MAX
;
2152 ring
->rx_pending
= mdp
->num_rx_ring
;
2153 ring
->tx_pending
= mdp
->num_tx_ring
;
2156 static int sh_eth_set_ringparam(struct net_device
*ndev
,
2157 struct ethtool_ringparam
*ring
)
2159 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2162 if (ring
->tx_pending
> TX_RING_MAX
||
2163 ring
->rx_pending
> RX_RING_MAX
||
2164 ring
->tx_pending
< TX_RING_MIN
||
2165 ring
->rx_pending
< RX_RING_MIN
)
2167 if (ring
->rx_mini_pending
|| ring
->rx_jumbo_pending
)
2170 if (netif_running(ndev
)) {
2171 netif_device_detach(ndev
);
2172 netif_tx_disable(ndev
);
2174 /* Serialise with the interrupt handler and NAPI, then
2175 * disable interrupts. We have to clear the
2176 * irq_enabled flag first to ensure that interrupts
2177 * won't be re-enabled.
2179 mdp
->irq_enabled
= false;
2180 synchronize_irq(ndev
->irq
);
2181 napi_synchronize(&mdp
->napi
);
2182 sh_eth_write(ndev
, 0x0000, EESIPR
);
2184 sh_eth_dev_exit(ndev
);
2186 /* Free all the skbuffs in the Rx queue and the DMA buffers. */
2187 sh_eth_ring_free(ndev
);
2190 /* Set new parameters */
2191 mdp
->num_rx_ring
= ring
->rx_pending
;
2192 mdp
->num_tx_ring
= ring
->tx_pending
;
2194 if (netif_running(ndev
)) {
2195 ret
= sh_eth_ring_init(ndev
);
2197 netdev_err(ndev
, "%s: sh_eth_ring_init failed.\n",
2201 ret
= sh_eth_dev_init(ndev
);
2203 netdev_err(ndev
, "%s: sh_eth_dev_init failed.\n",
2208 netif_device_attach(ndev
);
2214 static const struct ethtool_ops sh_eth_ethtool_ops
= {
2215 .get_regs_len
= sh_eth_get_regs_len
,
2216 .get_regs
= sh_eth_get_regs
,
2217 .nway_reset
= sh_eth_nway_reset
,
2218 .get_msglevel
= sh_eth_get_msglevel
,
2219 .set_msglevel
= sh_eth_set_msglevel
,
2220 .get_link
= ethtool_op_get_link
,
2221 .get_strings
= sh_eth_get_strings
,
2222 .get_ethtool_stats
= sh_eth_get_ethtool_stats
,
2223 .get_sset_count
= sh_eth_get_sset_count
,
2224 .get_ringparam
= sh_eth_get_ringparam
,
2225 .set_ringparam
= sh_eth_set_ringparam
,
2226 .get_link_ksettings
= sh_eth_get_link_ksettings
,
2227 .set_link_ksettings
= sh_eth_set_link_ksettings
,
2230 /* network device open function */
2231 static int sh_eth_open(struct net_device
*ndev
)
2233 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2236 pm_runtime_get_sync(&mdp
->pdev
->dev
);
2238 napi_enable(&mdp
->napi
);
2240 ret
= request_irq(ndev
->irq
, sh_eth_interrupt
,
2241 mdp
->cd
->irq_flags
, ndev
->name
, ndev
);
2243 netdev_err(ndev
, "Can not assign IRQ number\n");
2247 /* Descriptor set */
2248 ret
= sh_eth_ring_init(ndev
);
2253 ret
= sh_eth_dev_init(ndev
);
2257 /* PHY control start*/
2258 ret
= sh_eth_phy_start(ndev
);
2262 netif_start_queue(ndev
);
2269 free_irq(ndev
->irq
, ndev
);
2271 napi_disable(&mdp
->napi
);
2272 pm_runtime_put_sync(&mdp
->pdev
->dev
);
2276 /* Timeout function */
2277 static void sh_eth_tx_timeout(struct net_device
*ndev
)
2279 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2280 struct sh_eth_rxdesc
*rxdesc
;
2283 netif_stop_queue(ndev
);
2285 netif_err(mdp
, timer
, ndev
,
2286 "transmit timed out, status %8.8x, resetting...\n",
2287 sh_eth_read(ndev
, EESR
));
2289 /* tx_errors count up */
2290 ndev
->stats
.tx_errors
++;
2292 /* Free all the skbuffs in the Rx queue. */
2293 for (i
= 0; i
< mdp
->num_rx_ring
; i
++) {
2294 rxdesc
= &mdp
->rx_ring
[i
];
2295 rxdesc
->status
= cpu_to_le32(0);
2296 rxdesc
->addr
= cpu_to_le32(0xBADF00D0);
2297 dev_kfree_skb(mdp
->rx_skbuff
[i
]);
2298 mdp
->rx_skbuff
[i
] = NULL
;
2300 for (i
= 0; i
< mdp
->num_tx_ring
; i
++) {
2301 dev_kfree_skb(mdp
->tx_skbuff
[i
]);
2302 mdp
->tx_skbuff
[i
] = NULL
;
2306 sh_eth_dev_init(ndev
);
2308 netif_start_queue(ndev
);
2311 /* Packet transmit function */
2312 static int sh_eth_start_xmit(struct sk_buff
*skb
, struct net_device
*ndev
)
2314 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2315 struct sh_eth_txdesc
*txdesc
;
2316 dma_addr_t dma_addr
;
2318 unsigned long flags
;
2320 spin_lock_irqsave(&mdp
->lock
, flags
);
2321 if ((mdp
->cur_tx
- mdp
->dirty_tx
) >= (mdp
->num_tx_ring
- 4)) {
2322 if (!sh_eth_tx_free(ndev
, true)) {
2323 netif_warn(mdp
, tx_queued
, ndev
, "TxFD exhausted.\n");
2324 netif_stop_queue(ndev
);
2325 spin_unlock_irqrestore(&mdp
->lock
, flags
);
2326 return NETDEV_TX_BUSY
;
2329 spin_unlock_irqrestore(&mdp
->lock
, flags
);
2331 if (skb_put_padto(skb
, ETH_ZLEN
))
2332 return NETDEV_TX_OK
;
2334 entry
= mdp
->cur_tx
% mdp
->num_tx_ring
;
2335 mdp
->tx_skbuff
[entry
] = skb
;
2336 txdesc
= &mdp
->tx_ring
[entry
];
2338 if (!mdp
->cd
->hw_swap
)
2339 sh_eth_soft_swap(PTR_ALIGN(skb
->data
, 4), skb
->len
+ 2);
2340 dma_addr
= dma_map_single(&ndev
->dev
, skb
->data
, skb
->len
,
2342 if (dma_mapping_error(&ndev
->dev
, dma_addr
)) {
2344 return NETDEV_TX_OK
;
2346 txdesc
->addr
= cpu_to_le32(dma_addr
);
2347 txdesc
->len
= cpu_to_le32(skb
->len
<< 16);
2349 dma_wmb(); /* TACT bit must be set after all the above writes */
2350 if (entry
>= mdp
->num_tx_ring
- 1)
2351 txdesc
->status
|= cpu_to_le32(TD_TACT
| TD_TDLE
);
2353 txdesc
->status
|= cpu_to_le32(TD_TACT
);
2357 if (!(sh_eth_read(ndev
, EDTRR
) & sh_eth_get_edtrr_trns(mdp
)))
2358 sh_eth_write(ndev
, sh_eth_get_edtrr_trns(mdp
), EDTRR
);
2360 return NETDEV_TX_OK
;
2363 /* The statistics registers have write-clear behaviour, which means we
2364 * will lose any increment between the read and write. We mitigate
2365 * this by only clearing when we read a non-zero value, so we will
2366 * never falsely report a total of zero.
2369 sh_eth_update_stat(struct net_device
*ndev
, unsigned long *stat
, int reg
)
2371 u32 delta
= sh_eth_read(ndev
, reg
);
2375 sh_eth_write(ndev
, 0, reg
);
2379 static struct net_device_stats
*sh_eth_get_stats(struct net_device
*ndev
)
2381 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2383 if (sh_eth_is_rz_fast_ether(mdp
))
2384 return &ndev
->stats
;
2386 if (!mdp
->is_opened
)
2387 return &ndev
->stats
;
2389 sh_eth_update_stat(ndev
, &ndev
->stats
.tx_dropped
, TROCR
);
2390 sh_eth_update_stat(ndev
, &ndev
->stats
.collisions
, CDCR
);
2391 sh_eth_update_stat(ndev
, &ndev
->stats
.tx_carrier_errors
, LCCR
);
2393 if (sh_eth_is_gether(mdp
)) {
2394 sh_eth_update_stat(ndev
, &ndev
->stats
.tx_carrier_errors
,
2396 sh_eth_update_stat(ndev
, &ndev
->stats
.tx_carrier_errors
,
2399 sh_eth_update_stat(ndev
, &ndev
->stats
.tx_carrier_errors
,
2403 return &ndev
->stats
;
2406 /* device close function */
2407 static int sh_eth_close(struct net_device
*ndev
)
2409 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2411 netif_stop_queue(ndev
);
2413 /* Serialise with the interrupt handler and NAPI, then disable
2414 * interrupts. We have to clear the irq_enabled flag first to
2415 * ensure that interrupts won't be re-enabled.
2417 mdp
->irq_enabled
= false;
2418 synchronize_irq(ndev
->irq
);
2419 napi_disable(&mdp
->napi
);
2420 sh_eth_write(ndev
, 0x0000, EESIPR
);
2422 sh_eth_dev_exit(ndev
);
2424 /* PHY Disconnect */
2426 phy_stop(ndev
->phydev
);
2427 phy_disconnect(ndev
->phydev
);
2430 free_irq(ndev
->irq
, ndev
);
2432 /* Free all the skbuffs in the Rx queue and the DMA buffer. */
2433 sh_eth_ring_free(ndev
);
2435 pm_runtime_put_sync(&mdp
->pdev
->dev
);
2442 /* ioctl to device function */
2443 static int sh_eth_do_ioctl(struct net_device
*ndev
, struct ifreq
*rq
, int cmd
)
2445 struct phy_device
*phydev
= ndev
->phydev
;
2447 if (!netif_running(ndev
))
2453 return phy_mii_ioctl(phydev
, rq
, cmd
);
2456 /* For TSU_POSTn. Please refer to the manual about this (strange) bitfields */
2457 static void *sh_eth_tsu_get_post_reg_offset(struct sh_eth_private
*mdp
,
2460 return sh_eth_tsu_get_offset(mdp
, TSU_POST1
) + (entry
/ 8 * 4);
2463 static u32
sh_eth_tsu_get_post_mask(int entry
)
2465 return 0x0f << (28 - ((entry
% 8) * 4));
2468 static u32
sh_eth_tsu_get_post_bit(struct sh_eth_private
*mdp
, int entry
)
2470 return (0x08 >> (mdp
->port
<< 1)) << (28 - ((entry
% 8) * 4));
2473 static void sh_eth_tsu_enable_cam_entry_post(struct net_device
*ndev
,
2476 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2480 reg_offset
= sh_eth_tsu_get_post_reg_offset(mdp
, entry
);
2481 tmp
= ioread32(reg_offset
);
2482 iowrite32(tmp
| sh_eth_tsu_get_post_bit(mdp
, entry
), reg_offset
);
2485 static bool sh_eth_tsu_disable_cam_entry_post(struct net_device
*ndev
,
2488 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2489 u32 post_mask
, ref_mask
, tmp
;
2492 reg_offset
= sh_eth_tsu_get_post_reg_offset(mdp
, entry
);
2493 post_mask
= sh_eth_tsu_get_post_mask(entry
);
2494 ref_mask
= sh_eth_tsu_get_post_bit(mdp
, entry
) & ~post_mask
;
2496 tmp
= ioread32(reg_offset
);
2497 iowrite32(tmp
& ~post_mask
, reg_offset
);
2499 /* If other port enables, the function returns "true" */
2500 return tmp
& ref_mask
;
2503 static int sh_eth_tsu_busy(struct net_device
*ndev
)
2505 int timeout
= SH_ETH_TSU_TIMEOUT_MS
* 100;
2506 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2508 while ((sh_eth_tsu_read(mdp
, TSU_ADSBSY
) & TSU_ADSBSY_0
)) {
2512 netdev_err(ndev
, "%s: timeout\n", __func__
);
2520 static int sh_eth_tsu_write_entry(struct net_device
*ndev
, void *reg
,
2525 val
= addr
[0] << 24 | addr
[1] << 16 | addr
[2] << 8 | addr
[3];
2526 iowrite32(val
, reg
);
2527 if (sh_eth_tsu_busy(ndev
) < 0)
2530 val
= addr
[4] << 8 | addr
[5];
2531 iowrite32(val
, reg
+ 4);
2532 if (sh_eth_tsu_busy(ndev
) < 0)
2538 static void sh_eth_tsu_read_entry(void *reg
, u8
*addr
)
2542 val
= ioread32(reg
);
2543 addr
[0] = (val
>> 24) & 0xff;
2544 addr
[1] = (val
>> 16) & 0xff;
2545 addr
[2] = (val
>> 8) & 0xff;
2546 addr
[3] = val
& 0xff;
2547 val
= ioread32(reg
+ 4);
2548 addr
[4] = (val
>> 8) & 0xff;
2549 addr
[5] = val
& 0xff;
2553 static int sh_eth_tsu_find_entry(struct net_device
*ndev
, const u8
*addr
)
2555 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2556 void *reg_offset
= sh_eth_tsu_get_offset(mdp
, TSU_ADRH0
);
2558 u8 c_addr
[ETH_ALEN
];
2560 for (i
= 0; i
< SH_ETH_TSU_CAM_ENTRIES
; i
++, reg_offset
+= 8) {
2561 sh_eth_tsu_read_entry(reg_offset
, c_addr
);
2562 if (ether_addr_equal(addr
, c_addr
))
2569 static int sh_eth_tsu_find_empty(struct net_device
*ndev
)
2574 memset(blank
, 0, sizeof(blank
));
2575 entry
= sh_eth_tsu_find_entry(ndev
, blank
);
2576 return (entry
< 0) ? -ENOMEM
: entry
;
2579 static int sh_eth_tsu_disable_cam_entry_table(struct net_device
*ndev
,
2582 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2583 void *reg_offset
= sh_eth_tsu_get_offset(mdp
, TSU_ADRH0
);
2587 sh_eth_tsu_write(mdp
, sh_eth_tsu_read(mdp
, TSU_TEN
) &
2588 ~(1 << (31 - entry
)), TSU_TEN
);
2590 memset(blank
, 0, sizeof(blank
));
2591 ret
= sh_eth_tsu_write_entry(ndev
, reg_offset
+ entry
* 8, blank
);
2597 static int sh_eth_tsu_add_entry(struct net_device
*ndev
, const u8
*addr
)
2599 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2600 void *reg_offset
= sh_eth_tsu_get_offset(mdp
, TSU_ADRH0
);
2606 i
= sh_eth_tsu_find_entry(ndev
, addr
);
2608 /* No entry found, create one */
2609 i
= sh_eth_tsu_find_empty(ndev
);
2612 ret
= sh_eth_tsu_write_entry(ndev
, reg_offset
+ i
* 8, addr
);
2616 /* Enable the entry */
2617 sh_eth_tsu_write(mdp
, sh_eth_tsu_read(mdp
, TSU_TEN
) |
2618 (1 << (31 - i
)), TSU_TEN
);
2621 /* Entry found or created, enable POST */
2622 sh_eth_tsu_enable_cam_entry_post(ndev
, i
);
2627 static int sh_eth_tsu_del_entry(struct net_device
*ndev
, const u8
*addr
)
2629 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2635 i
= sh_eth_tsu_find_entry(ndev
, addr
);
2638 if (sh_eth_tsu_disable_cam_entry_post(ndev
, i
))
2641 /* Disable the entry if both ports was disabled */
2642 ret
= sh_eth_tsu_disable_cam_entry_table(ndev
, i
);
2650 static int sh_eth_tsu_purge_all(struct net_device
*ndev
)
2652 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2658 for (i
= 0; i
< SH_ETH_TSU_CAM_ENTRIES
; i
++) {
2659 if (sh_eth_tsu_disable_cam_entry_post(ndev
, i
))
2662 /* Disable the entry if both ports was disabled */
2663 ret
= sh_eth_tsu_disable_cam_entry_table(ndev
, i
);
2671 static void sh_eth_tsu_purge_mcast(struct net_device
*ndev
)
2673 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2675 void *reg_offset
= sh_eth_tsu_get_offset(mdp
, TSU_ADRH0
);
2681 for (i
= 0; i
< SH_ETH_TSU_CAM_ENTRIES
; i
++, reg_offset
+= 8) {
2682 sh_eth_tsu_read_entry(reg_offset
, addr
);
2683 if (is_multicast_ether_addr(addr
))
2684 sh_eth_tsu_del_entry(ndev
, addr
);
2688 /* Update promiscuous flag and multicast filter */
2689 static void sh_eth_set_rx_mode(struct net_device
*ndev
)
2691 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2694 unsigned long flags
;
2696 spin_lock_irqsave(&mdp
->lock
, flags
);
2697 /* Initial condition is MCT = 1, PRM = 0.
2698 * Depending on ndev->flags, set PRM or clear MCT
2700 ecmr_bits
= sh_eth_read(ndev
, ECMR
) & ~ECMR_PRM
;
2702 ecmr_bits
|= ECMR_MCT
;
2704 if (!(ndev
->flags
& IFF_MULTICAST
)) {
2705 sh_eth_tsu_purge_mcast(ndev
);
2708 if (ndev
->flags
& IFF_ALLMULTI
) {
2709 sh_eth_tsu_purge_mcast(ndev
);
2710 ecmr_bits
&= ~ECMR_MCT
;
2714 if (ndev
->flags
& IFF_PROMISC
) {
2715 sh_eth_tsu_purge_all(ndev
);
2716 ecmr_bits
= (ecmr_bits
& ~ECMR_MCT
) | ECMR_PRM
;
2717 } else if (mdp
->cd
->tsu
) {
2718 struct netdev_hw_addr
*ha
;
2719 netdev_for_each_mc_addr(ha
, ndev
) {
2720 if (mcast_all
&& is_multicast_ether_addr(ha
->addr
))
2723 if (sh_eth_tsu_add_entry(ndev
, ha
->addr
) < 0) {
2725 sh_eth_tsu_purge_mcast(ndev
);
2726 ecmr_bits
&= ~ECMR_MCT
;
2733 /* update the ethernet mode */
2734 sh_eth_write(ndev
, ecmr_bits
, ECMR
);
2736 spin_unlock_irqrestore(&mdp
->lock
, flags
);
2739 static int sh_eth_get_vtag_index(struct sh_eth_private
*mdp
)
2747 static int sh_eth_vlan_rx_add_vid(struct net_device
*ndev
,
2748 __be16 proto
, u16 vid
)
2750 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2751 int vtag_reg_index
= sh_eth_get_vtag_index(mdp
);
2753 if (unlikely(!mdp
->cd
->tsu
))
2756 /* No filtering if vid = 0 */
2760 mdp
->vlan_num_ids
++;
2762 /* The controller has one VLAN tag HW filter. So, if the filter is
2763 * already enabled, the driver disables it and the filte
2765 if (mdp
->vlan_num_ids
> 1) {
2766 /* disable VLAN filter */
2767 sh_eth_tsu_write(mdp
, 0, vtag_reg_index
);
2771 sh_eth_tsu_write(mdp
, TSU_VTAG_ENABLE
| (vid
& TSU_VTAG_VID_MASK
),
2777 static int sh_eth_vlan_rx_kill_vid(struct net_device
*ndev
,
2778 __be16 proto
, u16 vid
)
2780 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2781 int vtag_reg_index
= sh_eth_get_vtag_index(mdp
);
2783 if (unlikely(!mdp
->cd
->tsu
))
2786 /* No filtering if vid = 0 */
2790 mdp
->vlan_num_ids
--;
2791 sh_eth_tsu_write(mdp
, 0, vtag_reg_index
);
2796 /* SuperH's TSU register init function */
2797 static void sh_eth_tsu_init(struct sh_eth_private
*mdp
)
2799 if (sh_eth_is_rz_fast_ether(mdp
)) {
2800 sh_eth_tsu_write(mdp
, 0, TSU_TEN
); /* Disable all CAM entry */
2801 sh_eth_tsu_write(mdp
, TSU_FWSLC_POSTENU
| TSU_FWSLC_POSTENL
,
2802 TSU_FWSLC
); /* Enable POST registers */
2806 sh_eth_tsu_write(mdp
, 0, TSU_FWEN0
); /* Disable forward(0->1) */
2807 sh_eth_tsu_write(mdp
, 0, TSU_FWEN1
); /* Disable forward(1->0) */
2808 sh_eth_tsu_write(mdp
, 0, TSU_FCM
); /* forward fifo 3k-3k */
2809 sh_eth_tsu_write(mdp
, 0xc, TSU_BSYSL0
);
2810 sh_eth_tsu_write(mdp
, 0xc, TSU_BSYSL1
);
2811 sh_eth_tsu_write(mdp
, 0, TSU_PRISL0
);
2812 sh_eth_tsu_write(mdp
, 0, TSU_PRISL1
);
2813 sh_eth_tsu_write(mdp
, 0, TSU_FWSL0
);
2814 sh_eth_tsu_write(mdp
, 0, TSU_FWSL1
);
2815 sh_eth_tsu_write(mdp
, TSU_FWSLC_POSTENU
| TSU_FWSLC_POSTENL
, TSU_FWSLC
);
2816 if (sh_eth_is_gether(mdp
)) {
2817 sh_eth_tsu_write(mdp
, 0, TSU_QTAG0
); /* Disable QTAG(0->1) */
2818 sh_eth_tsu_write(mdp
, 0, TSU_QTAG1
); /* Disable QTAG(1->0) */
2820 sh_eth_tsu_write(mdp
, 0, TSU_QTAGM0
); /* Disable QTAG(0->1) */
2821 sh_eth_tsu_write(mdp
, 0, TSU_QTAGM1
); /* Disable QTAG(1->0) */
2823 sh_eth_tsu_write(mdp
, 0, TSU_FWSR
); /* all interrupt status clear */
2824 sh_eth_tsu_write(mdp
, 0, TSU_FWINMK
); /* Disable all interrupt */
2825 sh_eth_tsu_write(mdp
, 0, TSU_TEN
); /* Disable all CAM entry */
2826 sh_eth_tsu_write(mdp
, 0, TSU_POST1
); /* Disable CAM entry [ 0- 7] */
2827 sh_eth_tsu_write(mdp
, 0, TSU_POST2
); /* Disable CAM entry [ 8-15] */
2828 sh_eth_tsu_write(mdp
, 0, TSU_POST3
); /* Disable CAM entry [16-23] */
2829 sh_eth_tsu_write(mdp
, 0, TSU_POST4
); /* Disable CAM entry [24-31] */
2832 /* MDIO bus release function */
2833 static int sh_mdio_release(struct sh_eth_private
*mdp
)
2835 /* unregister mdio bus */
2836 mdiobus_unregister(mdp
->mii_bus
);
2838 /* free bitbang info */
2839 free_mdio_bitbang(mdp
->mii_bus
);
2844 /* MDIO bus init function */
2845 static int sh_mdio_init(struct sh_eth_private
*mdp
,
2846 struct sh_eth_plat_data
*pd
)
2849 struct bb_info
*bitbang
;
2850 struct platform_device
*pdev
= mdp
->pdev
;
2851 struct device
*dev
= &mdp
->pdev
->dev
;
2853 /* create bit control struct for PHY */
2854 bitbang
= devm_kzalloc(dev
, sizeof(struct bb_info
), GFP_KERNEL
);
2859 bitbang
->addr
= mdp
->addr
+ mdp
->reg_offset
[PIR
];
2860 bitbang
->set_gate
= pd
->set_mdio_gate
;
2861 bitbang
->ctrl
.ops
= &bb_ops
;
2863 /* MII controller setting */
2864 mdp
->mii_bus
= alloc_mdio_bitbang(&bitbang
->ctrl
);
2868 /* Hook up MII support for ethtool */
2869 mdp
->mii_bus
->name
= "sh_mii";
2870 mdp
->mii_bus
->parent
= dev
;
2871 snprintf(mdp
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%s-%x",
2872 pdev
->name
, pdev
->id
);
2874 /* register MDIO bus */
2876 ret
= of_mdiobus_register(mdp
->mii_bus
, dev
->of_node
);
2878 if (pd
->phy_irq
> 0)
2879 mdp
->mii_bus
->irq
[pd
->phy
] = pd
->phy_irq
;
2881 ret
= mdiobus_register(mdp
->mii_bus
);
2890 free_mdio_bitbang(mdp
->mii_bus
);
2894 static const u16
*sh_eth_get_register_offset(int register_type
)
2896 const u16
*reg_offset
= NULL
;
2898 switch (register_type
) {
2899 case SH_ETH_REG_GIGABIT
:
2900 reg_offset
= sh_eth_offset_gigabit
;
2902 case SH_ETH_REG_FAST_RZ
:
2903 reg_offset
= sh_eth_offset_fast_rz
;
2905 case SH_ETH_REG_FAST_RCAR
:
2906 reg_offset
= sh_eth_offset_fast_rcar
;
2908 case SH_ETH_REG_FAST_SH4
:
2909 reg_offset
= sh_eth_offset_fast_sh4
;
2911 case SH_ETH_REG_FAST_SH3_SH2
:
2912 reg_offset
= sh_eth_offset_fast_sh3_sh2
;
2919 static const struct net_device_ops sh_eth_netdev_ops
= {
2920 .ndo_open
= sh_eth_open
,
2921 .ndo_stop
= sh_eth_close
,
2922 .ndo_start_xmit
= sh_eth_start_xmit
,
2923 .ndo_get_stats
= sh_eth_get_stats
,
2924 .ndo_set_rx_mode
= sh_eth_set_rx_mode
,
2925 .ndo_tx_timeout
= sh_eth_tx_timeout
,
2926 .ndo_do_ioctl
= sh_eth_do_ioctl
,
2927 .ndo_validate_addr
= eth_validate_addr
,
2928 .ndo_set_mac_address
= eth_mac_addr
,
2929 .ndo_change_mtu
= eth_change_mtu
,
2932 static const struct net_device_ops sh_eth_netdev_ops_tsu
= {
2933 .ndo_open
= sh_eth_open
,
2934 .ndo_stop
= sh_eth_close
,
2935 .ndo_start_xmit
= sh_eth_start_xmit
,
2936 .ndo_get_stats
= sh_eth_get_stats
,
2937 .ndo_set_rx_mode
= sh_eth_set_rx_mode
,
2938 .ndo_vlan_rx_add_vid
= sh_eth_vlan_rx_add_vid
,
2939 .ndo_vlan_rx_kill_vid
= sh_eth_vlan_rx_kill_vid
,
2940 .ndo_tx_timeout
= sh_eth_tx_timeout
,
2941 .ndo_do_ioctl
= sh_eth_do_ioctl
,
2942 .ndo_validate_addr
= eth_validate_addr
,
2943 .ndo_set_mac_address
= eth_mac_addr
,
2944 .ndo_change_mtu
= eth_change_mtu
,
2948 static struct sh_eth_plat_data
*sh_eth_parse_dt(struct device
*dev
)
2950 struct device_node
*np
= dev
->of_node
;
2951 struct sh_eth_plat_data
*pdata
;
2952 const char *mac_addr
;
2954 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
2958 pdata
->phy_interface
= of_get_phy_mode(np
);
2960 mac_addr
= of_get_mac_address(np
);
2962 memcpy(pdata
->mac_addr
, mac_addr
, ETH_ALEN
);
2964 pdata
->no_ether_link
=
2965 of_property_read_bool(np
, "renesas,no-ether-link");
2966 pdata
->ether_link_active_low
=
2967 of_property_read_bool(np
, "renesas,ether-link-active-low");
2972 static const struct of_device_id sh_eth_match_table
[] = {
2973 { .compatible
= "renesas,gether-r8a7740", .data
= &r8a7740_data
},
2974 { .compatible
= "renesas,ether-r8a7743", .data
= &r8a779x_data
},
2975 { .compatible
= "renesas,ether-r8a7745", .data
= &r8a779x_data
},
2976 { .compatible
= "renesas,ether-r8a7778", .data
= &r8a777x_data
},
2977 { .compatible
= "renesas,ether-r8a7779", .data
= &r8a777x_data
},
2978 { .compatible
= "renesas,ether-r8a7790", .data
= &r8a779x_data
},
2979 { .compatible
= "renesas,ether-r8a7791", .data
= &r8a779x_data
},
2980 { .compatible
= "renesas,ether-r8a7793", .data
= &r8a779x_data
},
2981 { .compatible
= "renesas,ether-r8a7794", .data
= &r8a779x_data
},
2982 { .compatible
= "renesas,ether-r7s72100", .data
= &r7s72100_data
},
2985 MODULE_DEVICE_TABLE(of
, sh_eth_match_table
);
2987 static inline struct sh_eth_plat_data
*sh_eth_parse_dt(struct device
*dev
)
2993 static int sh_eth_drv_probe(struct platform_device
*pdev
)
2995 struct resource
*res
;
2996 struct sh_eth_plat_data
*pd
= dev_get_platdata(&pdev
->dev
);
2997 const struct platform_device_id
*id
= platform_get_device_id(pdev
);
2998 struct sh_eth_private
*mdp
;
2999 struct net_device
*ndev
;
3003 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
3005 ndev
= alloc_etherdev(sizeof(struct sh_eth_private
));
3009 pm_runtime_enable(&pdev
->dev
);
3010 pm_runtime_get_sync(&pdev
->dev
);
3016 ret
= platform_get_irq(pdev
, 0);
3021 SET_NETDEV_DEV(ndev
, &pdev
->dev
);
3023 mdp
= netdev_priv(ndev
);
3024 mdp
->num_tx_ring
= TX_RING_SIZE
;
3025 mdp
->num_rx_ring
= RX_RING_SIZE
;
3026 mdp
->addr
= devm_ioremap_resource(&pdev
->dev
, res
);
3027 if (IS_ERR(mdp
->addr
)) {
3028 ret
= PTR_ERR(mdp
->addr
);
3032 ndev
->base_addr
= res
->start
;
3034 spin_lock_init(&mdp
->lock
);
3037 if (pdev
->dev
.of_node
)
3038 pd
= sh_eth_parse_dt(&pdev
->dev
);
3040 dev_err(&pdev
->dev
, "no platform data\n");
3046 mdp
->phy_id
= pd
->phy
;
3047 mdp
->phy_interface
= pd
->phy_interface
;
3048 mdp
->no_ether_link
= pd
->no_ether_link
;
3049 mdp
->ether_link_active_low
= pd
->ether_link_active_low
;
3053 mdp
->cd
= (struct sh_eth_cpu_data
*)id
->driver_data
;
3055 mdp
->cd
= (struct sh_eth_cpu_data
*)of_device_get_match_data(&pdev
->dev
);
3057 mdp
->reg_offset
= sh_eth_get_register_offset(mdp
->cd
->register_type
);
3058 if (!mdp
->reg_offset
) {
3059 dev_err(&pdev
->dev
, "Unknown register type (%d)\n",
3060 mdp
->cd
->register_type
);
3064 sh_eth_set_default_cpu_data(mdp
->cd
);
3068 ndev
->netdev_ops
= &sh_eth_netdev_ops_tsu
;
3070 ndev
->netdev_ops
= &sh_eth_netdev_ops
;
3071 ndev
->ethtool_ops
= &sh_eth_ethtool_ops
;
3072 ndev
->watchdog_timeo
= TX_TIMEOUT
;
3074 /* debug message level */
3075 mdp
->msg_enable
= SH_ETH_DEF_MSG_ENABLE
;
3077 /* read and set MAC address */
3078 read_mac_address(ndev
, pd
->mac_addr
);
3079 if (!is_valid_ether_addr(ndev
->dev_addr
)) {
3080 dev_warn(&pdev
->dev
,
3081 "no valid MAC address supplied, using a random one.\n");
3082 eth_hw_addr_random(ndev
);
3085 /* ioremap the TSU registers */
3087 struct resource
*rtsu
;
3088 rtsu
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
3089 mdp
->tsu_addr
= devm_ioremap_resource(&pdev
->dev
, rtsu
);
3090 if (IS_ERR(mdp
->tsu_addr
)) {
3091 ret
= PTR_ERR(mdp
->tsu_addr
);
3094 mdp
->port
= devno
% 2;
3095 ndev
->features
= NETIF_F_HW_VLAN_CTAG_FILTER
;
3098 /* initialize first or needed device */
3099 if (!devno
|| pd
->needs_init
) {
3100 if (mdp
->cd
->chip_reset
)
3101 mdp
->cd
->chip_reset(ndev
);
3104 /* TSU init (Init only)*/
3105 sh_eth_tsu_init(mdp
);
3109 if (mdp
->cd
->rmiimode
)
3110 sh_eth_write(ndev
, 0x1, RMIIMODE
);
3113 ret
= sh_mdio_init(mdp
, pd
);
3115 dev_err(&ndev
->dev
, "failed to initialise MDIO\n");
3119 netif_napi_add(ndev
, &mdp
->napi
, sh_eth_poll
, 64);
3121 /* network device register */
3122 ret
= register_netdev(ndev
);
3126 /* print device information */
3127 netdev_info(ndev
, "Base address at 0x%x, %pM, IRQ %d.\n",
3128 (u32
)ndev
->base_addr
, ndev
->dev_addr
, ndev
->irq
);
3130 pm_runtime_put(&pdev
->dev
);
3131 platform_set_drvdata(pdev
, ndev
);
3136 netif_napi_del(&mdp
->napi
);
3137 sh_mdio_release(mdp
);
3144 pm_runtime_put(&pdev
->dev
);
3145 pm_runtime_disable(&pdev
->dev
);
3149 static int sh_eth_drv_remove(struct platform_device
*pdev
)
3151 struct net_device
*ndev
= platform_get_drvdata(pdev
);
3152 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
3154 unregister_netdev(ndev
);
3155 netif_napi_del(&mdp
->napi
);
3156 sh_mdio_release(mdp
);
3157 pm_runtime_disable(&pdev
->dev
);
3164 #ifdef CONFIG_PM_SLEEP
3165 static int sh_eth_suspend(struct device
*dev
)
3167 struct net_device
*ndev
= dev_get_drvdata(dev
);
3170 if (netif_running(ndev
)) {
3171 netif_device_detach(ndev
);
3172 ret
= sh_eth_close(ndev
);
3178 static int sh_eth_resume(struct device
*dev
)
3180 struct net_device
*ndev
= dev_get_drvdata(dev
);
3183 if (netif_running(ndev
)) {
3184 ret
= sh_eth_open(ndev
);
3187 netif_device_attach(ndev
);
3194 static int sh_eth_runtime_nop(struct device
*dev
)
3196 /* Runtime PM callback shared between ->runtime_suspend()
3197 * and ->runtime_resume(). Simply returns success.
3199 * This driver re-initializes all registers after
3200 * pm_runtime_get_sync() anyway so there is no need
3201 * to save and restore registers here.
3206 static const struct dev_pm_ops sh_eth_dev_pm_ops
= {
3207 SET_SYSTEM_SLEEP_PM_OPS(sh_eth_suspend
, sh_eth_resume
)
3208 SET_RUNTIME_PM_OPS(sh_eth_runtime_nop
, sh_eth_runtime_nop
, NULL
)
3210 #define SH_ETH_PM_OPS (&sh_eth_dev_pm_ops)
3212 #define SH_ETH_PM_OPS NULL
3215 static struct platform_device_id sh_eth_id_table
[] = {
3216 { "sh7619-ether", (kernel_ulong_t
)&sh7619_data
},
3217 { "sh771x-ether", (kernel_ulong_t
)&sh771x_data
},
3218 { "sh7724-ether", (kernel_ulong_t
)&sh7724_data
},
3219 { "sh7734-gether", (kernel_ulong_t
)&sh7734_data
},
3220 { "sh7757-ether", (kernel_ulong_t
)&sh7757_data
},
3221 { "sh7757-gether", (kernel_ulong_t
)&sh7757_data_giga
},
3222 { "sh7763-gether", (kernel_ulong_t
)&sh7763_data
},
3225 MODULE_DEVICE_TABLE(platform
, sh_eth_id_table
);
3227 static struct platform_driver sh_eth_driver
= {
3228 .probe
= sh_eth_drv_probe
,
3229 .remove
= sh_eth_drv_remove
,
3230 .id_table
= sh_eth_id_table
,
3233 .pm
= SH_ETH_PM_OPS
,
3234 .of_match_table
= of_match_ptr(sh_eth_match_table
),
3238 module_platform_driver(sh_eth_driver
);
3240 MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
3241 MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
3242 MODULE_LICENSE("GPL v2");