1 // SPDX-License-Identifier: GPL-2.0
2 /* SuperH Ethernet device driver
4 * Copyright (C) 2014 Renesas Electronics Corporation
5 * Copyright (C) 2006-2012 Nobuhiro Iwamatsu
6 * Copyright (C) 2008-2014 Renesas Solutions Corp.
7 * Copyright (C) 2013-2017 Cogent Embedded, Inc.
8 * Copyright (C) 2014 Codethink Limited
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/spinlock.h>
14 #include <linux/interrupt.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/etherdevice.h>
17 #include <linux/delay.h>
18 #include <linux/platform_device.h>
19 #include <linux/mdio-bitbang.h>
20 #include <linux/netdevice.h>
22 #include <linux/of_device.h>
23 #include <linux/of_irq.h>
24 #include <linux/of_net.h>
25 #include <linux/phy.h>
26 #include <linux/cache.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/slab.h>
30 #include <linux/ethtool.h>
31 #include <linux/if_vlan.h>
32 #include <linux/sh_eth.h>
33 #include <linux/of_mdio.h>
37 #define SH_ETH_DEF_MSG_ENABLE \
43 #define SH_ETH_OFFSET_INVALID ((u16)~0)
45 #define SH_ETH_OFFSET_DEFAULTS \
46 [0 ... SH_ETH_MAX_REGISTER_OFFSET - 1] = SH_ETH_OFFSET_INVALID
48 static const u16 sh_eth_offset_gigabit
[SH_ETH_MAX_REGISTER_OFFSET
] = {
49 SH_ETH_OFFSET_DEFAULTS
,
104 [TSU_CTRST
] = 0x0004,
105 [TSU_FWEN0
] = 0x0010,
106 [TSU_FWEN1
] = 0x0014,
108 [TSU_BSYSL0
] = 0x0020,
109 [TSU_BSYSL1
] = 0x0024,
110 [TSU_PRISL0
] = 0x0028,
111 [TSU_PRISL1
] = 0x002c,
112 [TSU_FWSL0
] = 0x0030,
113 [TSU_FWSL1
] = 0x0034,
114 [TSU_FWSLC
] = 0x0038,
115 [TSU_QTAGM0
] = 0x0040,
116 [TSU_QTAGM1
] = 0x0044,
118 [TSU_FWINMK
] = 0x0054,
119 [TSU_ADQT0
] = 0x0048,
120 [TSU_ADQT1
] = 0x004c,
121 [TSU_VTAG0
] = 0x0058,
122 [TSU_VTAG1
] = 0x005c,
123 [TSU_ADSBSY
] = 0x0060,
125 [TSU_POST1
] = 0x0070,
126 [TSU_POST2
] = 0x0074,
127 [TSU_POST3
] = 0x0078,
128 [TSU_POST4
] = 0x007c,
129 [TSU_ADRH0
] = 0x0100,
145 static const u16 sh_eth_offset_fast_rz
[SH_ETH_MAX_REGISTER_OFFSET
] = {
146 SH_ETH_OFFSET_DEFAULTS
,
191 [TSU_CTRST
] = 0x0004,
192 [TSU_FWSLC
] = 0x0038,
193 [TSU_VTAG0
] = 0x0058,
194 [TSU_ADSBSY
] = 0x0060,
196 [TSU_POST1
] = 0x0070,
197 [TSU_POST2
] = 0x0074,
198 [TSU_POST3
] = 0x0078,
199 [TSU_POST4
] = 0x007c,
200 [TSU_ADRH0
] = 0x0100,
208 static const u16 sh_eth_offset_fast_rcar
[SH_ETH_MAX_REGISTER_OFFSET
] = {
209 SH_ETH_OFFSET_DEFAULTS
,
256 static const u16 sh_eth_offset_fast_sh4
[SH_ETH_MAX_REGISTER_OFFSET
] = {
257 SH_ETH_OFFSET_DEFAULTS
,
310 static const u16 sh_eth_offset_fast_sh3_sh2
[SH_ETH_MAX_REGISTER_OFFSET
] = {
311 SH_ETH_OFFSET_DEFAULTS
,
359 [TSU_CTRST
] = 0x0004,
360 [TSU_FWEN0
] = 0x0010,
361 [TSU_FWEN1
] = 0x0014,
363 [TSU_BSYSL0
] = 0x0020,
364 [TSU_BSYSL1
] = 0x0024,
365 [TSU_PRISL0
] = 0x0028,
366 [TSU_PRISL1
] = 0x002c,
367 [TSU_FWSL0
] = 0x0030,
368 [TSU_FWSL1
] = 0x0034,
369 [TSU_FWSLC
] = 0x0038,
370 [TSU_QTAGM0
] = 0x0040,
371 [TSU_QTAGM1
] = 0x0044,
372 [TSU_ADQT0
] = 0x0048,
373 [TSU_ADQT1
] = 0x004c,
375 [TSU_FWINMK
] = 0x0054,
376 [TSU_ADSBSY
] = 0x0060,
378 [TSU_POST1
] = 0x0070,
379 [TSU_POST2
] = 0x0074,
380 [TSU_POST3
] = 0x0078,
381 [TSU_POST4
] = 0x007c,
396 [TSU_ADRH0
] = 0x0100,
399 static void sh_eth_rcv_snd_disable(struct net_device
*ndev
);
400 static struct net_device_stats
*sh_eth_get_stats(struct net_device
*ndev
);
402 static void sh_eth_write(struct net_device
*ndev
, u32 data
, int enum_index
)
404 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
405 u16 offset
= mdp
->reg_offset
[enum_index
];
407 if (WARN_ON(offset
== SH_ETH_OFFSET_INVALID
))
410 iowrite32(data
, mdp
->addr
+ offset
);
413 static u32
sh_eth_read(struct net_device
*ndev
, int enum_index
)
415 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
416 u16 offset
= mdp
->reg_offset
[enum_index
];
418 if (WARN_ON(offset
== SH_ETH_OFFSET_INVALID
))
421 return ioread32(mdp
->addr
+ offset
);
424 static void sh_eth_modify(struct net_device
*ndev
, int enum_index
, u32 clear
,
427 sh_eth_write(ndev
, (sh_eth_read(ndev
, enum_index
) & ~clear
) | set
,
431 static u16
sh_eth_tsu_get_offset(struct sh_eth_private
*mdp
, int enum_index
)
433 return mdp
->reg_offset
[enum_index
];
436 static void sh_eth_tsu_write(struct sh_eth_private
*mdp
, u32 data
,
439 u16 offset
= sh_eth_tsu_get_offset(mdp
, enum_index
);
441 if (WARN_ON(offset
== SH_ETH_OFFSET_INVALID
))
444 iowrite32(data
, mdp
->tsu_addr
+ offset
);
447 static u32
sh_eth_tsu_read(struct sh_eth_private
*mdp
, int enum_index
)
449 u16 offset
= sh_eth_tsu_get_offset(mdp
, enum_index
);
451 if (WARN_ON(offset
== SH_ETH_OFFSET_INVALID
))
454 return ioread32(mdp
->tsu_addr
+ offset
);
457 static void sh_eth_soft_swap(char *src
, int len
)
459 #ifdef __LITTLE_ENDIAN
461 u32
*maxp
= p
+ DIV_ROUND_UP(len
, sizeof(u32
));
463 for (; p
< maxp
; p
++)
468 static void sh_eth_select_mii(struct net_device
*ndev
)
470 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
473 switch (mdp
->phy_interface
) {
474 case PHY_INTERFACE_MODE_RGMII
... PHY_INTERFACE_MODE_RGMII_TXID
:
477 case PHY_INTERFACE_MODE_GMII
:
480 case PHY_INTERFACE_MODE_MII
:
483 case PHY_INTERFACE_MODE_RMII
:
488 "PHY interface mode was not setup. Set to MII.\n");
493 sh_eth_write(ndev
, value
, RMII_MII
);
496 static void sh_eth_set_duplex(struct net_device
*ndev
)
498 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
500 sh_eth_modify(ndev
, ECMR
, ECMR_DM
, mdp
->duplex
? ECMR_DM
: 0);
503 static void sh_eth_chip_reset(struct net_device
*ndev
)
505 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
508 sh_eth_tsu_write(mdp
, ARSTR_ARST
, ARSTR
);
512 static int sh_eth_soft_reset(struct net_device
*ndev
)
514 sh_eth_modify(ndev
, EDMR
, EDMR_SRST_ETHER
, EDMR_SRST_ETHER
);
516 sh_eth_modify(ndev
, EDMR
, EDMR_SRST_ETHER
, 0);
521 static int sh_eth_check_soft_reset(struct net_device
*ndev
)
525 for (cnt
= 100; cnt
> 0; cnt
--) {
526 if (!(sh_eth_read(ndev
, EDMR
) & EDMR_SRST_GETHER
))
531 netdev_err(ndev
, "Device reset failed\n");
535 static int sh_eth_soft_reset_gether(struct net_device
*ndev
)
537 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
540 sh_eth_write(ndev
, EDSR_ENALL
, EDSR
);
541 sh_eth_modify(ndev
, EDMR
, EDMR_SRST_GETHER
, EDMR_SRST_GETHER
);
543 ret
= sh_eth_check_soft_reset(ndev
);
548 sh_eth_write(ndev
, 0, TDLAR
);
549 sh_eth_write(ndev
, 0, TDFAR
);
550 sh_eth_write(ndev
, 0, TDFXR
);
551 sh_eth_write(ndev
, 0, TDFFR
);
552 sh_eth_write(ndev
, 0, RDLAR
);
553 sh_eth_write(ndev
, 0, RDFAR
);
554 sh_eth_write(ndev
, 0, RDFXR
);
555 sh_eth_write(ndev
, 0, RDFFR
);
557 /* Reset HW CRC register */
559 sh_eth_write(ndev
, 0, CSMR
);
561 /* Select MII mode */
562 if (mdp
->cd
->select_mii
)
563 sh_eth_select_mii(ndev
);
568 static void sh_eth_set_rate_gether(struct net_device
*ndev
)
570 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
572 switch (mdp
->speed
) {
573 case 10: /* 10BASE */
574 sh_eth_write(ndev
, GECMR_10
, GECMR
);
576 case 100:/* 100BASE */
577 sh_eth_write(ndev
, GECMR_100
, GECMR
);
579 case 1000: /* 1000BASE */
580 sh_eth_write(ndev
, GECMR_1000
, GECMR
);
587 static struct sh_eth_cpu_data r7s72100_data
= {
588 .soft_reset
= sh_eth_soft_reset_gether
,
590 .chip_reset
= sh_eth_chip_reset
,
591 .set_duplex
= sh_eth_set_duplex
,
593 .register_type
= SH_ETH_REG_FAST_RZ
,
595 .edtrr_trns
= EDTRR_TRNS_GETHER
,
596 .ecsr_value
= ECSR_ICD
,
597 .ecsipr_value
= ECSIPR_ICDIP
,
598 .eesipr_value
= EESIPR_TWB1IP
| EESIPR_TWBIP
| EESIPR_TC1IP
|
599 EESIPR_TABTIP
| EESIPR_RABTIP
| EESIPR_RFCOFIP
|
601 EESIPR_FTCIP
| EESIPR_TDEIP
| EESIPR_TFUFIP
|
602 EESIPR_FRIP
| EESIPR_RDEIP
| EESIPR_RFOFIP
|
603 EESIPR_RMAFIP
| EESIPR_RRFIP
|
604 EESIPR_RTLFIP
| EESIPR_RTSFIP
|
605 EESIPR_PREIP
| EESIPR_CERFIP
,
607 .tx_check
= EESR_TC1
| EESR_FTC
,
608 .eesr_err_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_RABT
|
609 EESR_RFE
| EESR_RDE
| EESR_RFRMER
| EESR_TFE
|
611 .fdr_value
= 0x0000070f,
628 static void sh_eth_chip_reset_r8a7740(struct net_device
*ndev
)
630 sh_eth_chip_reset(ndev
);
632 sh_eth_select_mii(ndev
);
636 static struct sh_eth_cpu_data r8a7740_data
= {
637 .soft_reset
= sh_eth_soft_reset_gether
,
639 .chip_reset
= sh_eth_chip_reset_r8a7740
,
640 .set_duplex
= sh_eth_set_duplex
,
641 .set_rate
= sh_eth_set_rate_gether
,
643 .register_type
= SH_ETH_REG_GIGABIT
,
645 .edtrr_trns
= EDTRR_TRNS_GETHER
,
646 .ecsr_value
= ECSR_ICD
| ECSR_MPD
,
647 .ecsipr_value
= ECSIPR_LCHNGIP
| ECSIPR_ICDIP
| ECSIPR_MPDIP
,
648 .eesipr_value
= EESIPR_RFCOFIP
| EESIPR_ECIIP
|
649 EESIPR_FTCIP
| EESIPR_TDEIP
| EESIPR_TFUFIP
|
650 EESIPR_FRIP
| EESIPR_RDEIP
| EESIPR_RFOFIP
|
651 0x0000f000 | EESIPR_CNDIP
| EESIPR_DLCIP
|
652 EESIPR_CDIP
| EESIPR_TROIP
| EESIPR_RMAFIP
|
653 EESIPR_CEEFIP
| EESIPR_CELFIP
|
654 EESIPR_RRFIP
| EESIPR_RTLFIP
| EESIPR_RTSFIP
|
655 EESIPR_PREIP
| EESIPR_CERFIP
,
657 .tx_check
= EESR_TC1
| EESR_FTC
,
658 .eesr_err_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_RABT
|
659 EESR_RFE
| EESR_RDE
| EESR_RFRMER
| EESR_TFE
|
661 .fdr_value
= 0x0000070f,
680 /* There is CPU dependent code */
681 static void sh_eth_set_rate_rcar(struct net_device
*ndev
)
683 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
685 switch (mdp
->speed
) {
686 case 10: /* 10BASE */
687 sh_eth_modify(ndev
, ECMR
, ECMR_ELB
, 0);
689 case 100:/* 100BASE */
690 sh_eth_modify(ndev
, ECMR
, ECMR_ELB
, ECMR_ELB
);
696 static struct sh_eth_cpu_data rcar_gen1_data
= {
697 .soft_reset
= sh_eth_soft_reset
,
699 .set_duplex
= sh_eth_set_duplex
,
700 .set_rate
= sh_eth_set_rate_rcar
,
702 .register_type
= SH_ETH_REG_FAST_RCAR
,
704 .edtrr_trns
= EDTRR_TRNS_ETHER
,
705 .ecsr_value
= ECSR_PSRTO
| ECSR_LCHNG
| ECSR_ICD
,
706 .ecsipr_value
= ECSIPR_PSRTOIP
| ECSIPR_LCHNGIP
| ECSIPR_ICDIP
,
707 .eesipr_value
= EESIPR_RFCOFIP
| EESIPR_ADEIP
| EESIPR_ECIIP
|
708 EESIPR_FTCIP
| EESIPR_TDEIP
| EESIPR_TFUFIP
|
709 EESIPR_FRIP
| EESIPR_RDEIP
| EESIPR_RFOFIP
|
710 EESIPR_RMAFIP
| EESIPR_RRFIP
|
711 EESIPR_RTLFIP
| EESIPR_RTSFIP
|
712 EESIPR_PREIP
| EESIPR_CERFIP
,
714 .tx_check
= EESR_FTC
| EESR_CND
| EESR_DLC
| EESR_CD
| EESR_TRO
,
715 .eesr_err_check
= EESR_TWB
| EESR_TABT
| EESR_RABT
| EESR_RFE
|
716 EESR_RDE
| EESR_RFRMER
| EESR_TFE
| EESR_TDE
,
717 .fdr_value
= 0x00000f0f,
726 /* R-Car Gen2 and RZ/G1 */
727 static struct sh_eth_cpu_data rcar_gen2_data
= {
728 .soft_reset
= sh_eth_soft_reset
,
730 .set_duplex
= sh_eth_set_duplex
,
731 .set_rate
= sh_eth_set_rate_rcar
,
733 .register_type
= SH_ETH_REG_FAST_RCAR
,
735 .edtrr_trns
= EDTRR_TRNS_ETHER
,
736 .ecsr_value
= ECSR_PSRTO
| ECSR_LCHNG
| ECSR_ICD
| ECSR_MPD
,
737 .ecsipr_value
= ECSIPR_PSRTOIP
| ECSIPR_LCHNGIP
| ECSIPR_ICDIP
|
739 .eesipr_value
= EESIPR_RFCOFIP
| EESIPR_ADEIP
| EESIPR_ECIIP
|
740 EESIPR_FTCIP
| EESIPR_TDEIP
| EESIPR_TFUFIP
|
741 EESIPR_FRIP
| EESIPR_RDEIP
| EESIPR_RFOFIP
|
742 EESIPR_RMAFIP
| EESIPR_RRFIP
|
743 EESIPR_RTLFIP
| EESIPR_RTSFIP
|
744 EESIPR_PREIP
| EESIPR_CERFIP
,
746 .tx_check
= EESR_FTC
| EESR_CND
| EESR_DLC
| EESR_CD
| EESR_TRO
,
747 .eesr_err_check
= EESR_TWB
| EESR_TABT
| EESR_RABT
| EESR_RFE
|
748 EESR_RDE
| EESR_RFRMER
| EESR_TFE
| EESR_TDE
,
749 .fdr_value
= 0x00000f0f,
751 .trscer_err_mask
= DESC_I_RINT8
,
763 static struct sh_eth_cpu_data r8a77980_data
= {
764 .soft_reset
= sh_eth_soft_reset_gether
,
766 .set_duplex
= sh_eth_set_duplex
,
767 .set_rate
= sh_eth_set_rate_gether
,
769 .register_type
= SH_ETH_REG_GIGABIT
,
771 .edtrr_trns
= EDTRR_TRNS_GETHER
,
772 .ecsr_value
= ECSR_PSRTO
| ECSR_LCHNG
| ECSR_ICD
| ECSR_MPD
,
773 .ecsipr_value
= ECSIPR_PSRTOIP
| ECSIPR_LCHNGIP
| ECSIPR_ICDIP
|
775 .eesipr_value
= EESIPR_RFCOFIP
| EESIPR_ECIIP
|
776 EESIPR_FTCIP
| EESIPR_TDEIP
| EESIPR_TFUFIP
|
777 EESIPR_FRIP
| EESIPR_RDEIP
| EESIPR_RFOFIP
|
778 EESIPR_RMAFIP
| EESIPR_RRFIP
|
779 EESIPR_RTLFIP
| EESIPR_RTSFIP
|
780 EESIPR_PREIP
| EESIPR_CERFIP
,
782 .tx_check
= EESR_FTC
| EESR_CD
| EESR_TRO
,
783 .eesr_err_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_RABT
|
784 EESR_RFE
| EESR_RDE
| EESR_RFRMER
|
785 EESR_TFE
| EESR_TDE
| EESR_ECI
,
786 .fdr_value
= 0x0000070f,
806 static struct sh_eth_cpu_data r7s9210_data
= {
807 .soft_reset
= sh_eth_soft_reset
,
809 .set_duplex
= sh_eth_set_duplex
,
810 .set_rate
= sh_eth_set_rate_rcar
,
812 .register_type
= SH_ETH_REG_FAST_SH4
,
814 .edtrr_trns
= EDTRR_TRNS_ETHER
,
815 .ecsr_value
= ECSR_ICD
,
816 .ecsipr_value
= ECSIPR_ICDIP
,
817 .eesipr_value
= EESIPR_TWBIP
| EESIPR_TABTIP
| EESIPR_RABTIP
|
818 EESIPR_RFCOFIP
| EESIPR_ECIIP
| EESIPR_FTCIP
|
819 EESIPR_TDEIP
| EESIPR_TFUFIP
| EESIPR_FRIP
|
820 EESIPR_RDEIP
| EESIPR_RFOFIP
| EESIPR_CNDIP
|
821 EESIPR_DLCIP
| EESIPR_CDIP
| EESIPR_TROIP
|
822 EESIPR_RMAFIP
| EESIPR_RRFIP
| EESIPR_RTLFIP
|
823 EESIPR_RTSFIP
| EESIPR_PREIP
| EESIPR_CERFIP
,
825 .tx_check
= EESR_FTC
| EESR_CND
| EESR_DLC
| EESR_CD
| EESR_TRO
,
826 .eesr_err_check
= EESR_TWB
| EESR_TABT
| EESR_RABT
| EESR_RFE
|
827 EESR_RDE
| EESR_RFRMER
| EESR_TFE
| EESR_TDE
,
829 .fdr_value
= 0x0000070f,
839 #endif /* CONFIG_OF */
841 static void sh_eth_set_rate_sh7724(struct net_device
*ndev
)
843 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
845 switch (mdp
->speed
) {
846 case 10: /* 10BASE */
847 sh_eth_modify(ndev
, ECMR
, ECMR_RTM
, 0);
849 case 100:/* 100BASE */
850 sh_eth_modify(ndev
, ECMR
, ECMR_RTM
, ECMR_RTM
);
856 static struct sh_eth_cpu_data sh7724_data
= {
857 .soft_reset
= sh_eth_soft_reset
,
859 .set_duplex
= sh_eth_set_duplex
,
860 .set_rate
= sh_eth_set_rate_sh7724
,
862 .register_type
= SH_ETH_REG_FAST_SH4
,
864 .edtrr_trns
= EDTRR_TRNS_ETHER
,
865 .ecsr_value
= ECSR_PSRTO
| ECSR_LCHNG
| ECSR_ICD
,
866 .ecsipr_value
= ECSIPR_PSRTOIP
| ECSIPR_LCHNGIP
| ECSIPR_ICDIP
,
867 .eesipr_value
= EESIPR_RFCOFIP
| EESIPR_ADEIP
| EESIPR_ECIIP
|
868 EESIPR_FTCIP
| EESIPR_TDEIP
| EESIPR_TFUFIP
|
869 EESIPR_FRIP
| EESIPR_RDEIP
| EESIPR_RFOFIP
|
870 EESIPR_RMAFIP
| EESIPR_RRFIP
|
871 EESIPR_RTLFIP
| EESIPR_RTSFIP
|
872 EESIPR_PREIP
| EESIPR_CERFIP
,
874 .tx_check
= EESR_FTC
| EESR_CND
| EESR_DLC
| EESR_CD
| EESR_TRO
,
875 .eesr_err_check
= EESR_TWB
| EESR_TABT
| EESR_RABT
| EESR_RFE
|
876 EESR_RDE
| EESR_RFRMER
| EESR_TFE
| EESR_TDE
,
885 static void sh_eth_set_rate_sh7757(struct net_device
*ndev
)
887 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
889 switch (mdp
->speed
) {
890 case 10: /* 10BASE */
891 sh_eth_write(ndev
, 0, RTRATE
);
893 case 100:/* 100BASE */
894 sh_eth_write(ndev
, 1, RTRATE
);
900 static struct sh_eth_cpu_data sh7757_data
= {
901 .soft_reset
= sh_eth_soft_reset
,
903 .set_duplex
= sh_eth_set_duplex
,
904 .set_rate
= sh_eth_set_rate_sh7757
,
906 .register_type
= SH_ETH_REG_FAST_SH4
,
908 .edtrr_trns
= EDTRR_TRNS_ETHER
,
909 .eesipr_value
= EESIPR_RFCOFIP
| EESIPR_ECIIP
|
910 EESIPR_FTCIP
| EESIPR_TDEIP
| EESIPR_TFUFIP
|
911 EESIPR_FRIP
| EESIPR_RDEIP
| EESIPR_RFOFIP
|
912 0x0000f000 | EESIPR_CNDIP
| EESIPR_DLCIP
|
913 EESIPR_CDIP
| EESIPR_TROIP
| EESIPR_RMAFIP
|
914 EESIPR_CEEFIP
| EESIPR_CELFIP
|
915 EESIPR_RRFIP
| EESIPR_RTLFIP
| EESIPR_RTSFIP
|
916 EESIPR_PREIP
| EESIPR_CERFIP
,
918 .tx_check
= EESR_FTC
| EESR_CND
| EESR_DLC
| EESR_CD
| EESR_TRO
,
919 .eesr_err_check
= EESR_TWB
| EESR_TABT
| EESR_RABT
| EESR_RFE
|
920 EESR_RDE
| EESR_RFRMER
| EESR_TFE
| EESR_TDE
,
922 .irq_flags
= IRQF_SHARED
,
933 #define SH_GIGA_ETH_BASE 0xfee00000UL
934 #define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8)
935 #define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0)
936 static void sh_eth_chip_reset_giga(struct net_device
*ndev
)
938 u32 mahr
[2], malr
[2];
941 /* save MAHR and MALR */
942 for (i
= 0; i
< 2; i
++) {
943 malr
[i
] = ioread32((void *)GIGA_MALR(i
));
944 mahr
[i
] = ioread32((void *)GIGA_MAHR(i
));
947 sh_eth_chip_reset(ndev
);
949 /* restore MAHR and MALR */
950 for (i
= 0; i
< 2; i
++) {
951 iowrite32(malr
[i
], (void *)GIGA_MALR(i
));
952 iowrite32(mahr
[i
], (void *)GIGA_MAHR(i
));
956 static void sh_eth_set_rate_giga(struct net_device
*ndev
)
958 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
960 switch (mdp
->speed
) {
961 case 10: /* 10BASE */
962 sh_eth_write(ndev
, 0x00000000, GECMR
);
964 case 100:/* 100BASE */
965 sh_eth_write(ndev
, 0x00000010, GECMR
);
967 case 1000: /* 1000BASE */
968 sh_eth_write(ndev
, 0x00000020, GECMR
);
973 /* SH7757(GETHERC) */
974 static struct sh_eth_cpu_data sh7757_data_giga
= {
975 .soft_reset
= sh_eth_soft_reset_gether
,
977 .chip_reset
= sh_eth_chip_reset_giga
,
978 .set_duplex
= sh_eth_set_duplex
,
979 .set_rate
= sh_eth_set_rate_giga
,
981 .register_type
= SH_ETH_REG_GIGABIT
,
983 .edtrr_trns
= EDTRR_TRNS_GETHER
,
984 .ecsr_value
= ECSR_ICD
| ECSR_MPD
,
985 .ecsipr_value
= ECSIPR_LCHNGIP
| ECSIPR_ICDIP
| ECSIPR_MPDIP
,
986 .eesipr_value
= EESIPR_RFCOFIP
| EESIPR_ECIIP
|
987 EESIPR_FTCIP
| EESIPR_TDEIP
| EESIPR_TFUFIP
|
988 EESIPR_FRIP
| EESIPR_RDEIP
| EESIPR_RFOFIP
|
989 0x0000f000 | EESIPR_CNDIP
| EESIPR_DLCIP
|
990 EESIPR_CDIP
| EESIPR_TROIP
| EESIPR_RMAFIP
|
991 EESIPR_CEEFIP
| EESIPR_CELFIP
|
992 EESIPR_RRFIP
| EESIPR_RTLFIP
| EESIPR_RTSFIP
|
993 EESIPR_PREIP
| EESIPR_CERFIP
,
995 .tx_check
= EESR_TC1
| EESR_FTC
,
996 .eesr_err_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_RABT
|
997 EESR_RFE
| EESR_RDE
| EESR_RFRMER
| EESR_TFE
|
999 .fdr_value
= 0x0000072f,
1001 .irq_flags
= IRQF_SHARED
,
1017 static struct sh_eth_cpu_data sh7734_data
= {
1018 .soft_reset
= sh_eth_soft_reset_gether
,
1020 .chip_reset
= sh_eth_chip_reset
,
1021 .set_duplex
= sh_eth_set_duplex
,
1022 .set_rate
= sh_eth_set_rate_gether
,
1024 .register_type
= SH_ETH_REG_GIGABIT
,
1026 .edtrr_trns
= EDTRR_TRNS_GETHER
,
1027 .ecsr_value
= ECSR_ICD
| ECSR_MPD
,
1028 .ecsipr_value
= ECSIPR_LCHNGIP
| ECSIPR_ICDIP
| ECSIPR_MPDIP
,
1029 .eesipr_value
= EESIPR_RFCOFIP
| EESIPR_ECIIP
|
1030 EESIPR_FTCIP
| EESIPR_TDEIP
| EESIPR_TFUFIP
|
1031 EESIPR_FRIP
| EESIPR_RDEIP
| EESIPR_RFOFIP
|
1032 EESIPR_DLCIP
| EESIPR_CDIP
| EESIPR_TROIP
|
1033 EESIPR_RMAFIP
| EESIPR_CEEFIP
| EESIPR_CELFIP
|
1034 EESIPR_RRFIP
| EESIPR_RTLFIP
| EESIPR_RTSFIP
|
1035 EESIPR_PREIP
| EESIPR_CERFIP
,
1037 .tx_check
= EESR_TC1
| EESR_FTC
,
1038 .eesr_err_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_RABT
|
1039 EESR_RFE
| EESR_RDE
| EESR_RFRMER
| EESR_TFE
|
1059 static struct sh_eth_cpu_data sh7763_data
= {
1060 .soft_reset
= sh_eth_soft_reset_gether
,
1062 .chip_reset
= sh_eth_chip_reset
,
1063 .set_duplex
= sh_eth_set_duplex
,
1064 .set_rate
= sh_eth_set_rate_gether
,
1066 .register_type
= SH_ETH_REG_GIGABIT
,
1068 .edtrr_trns
= EDTRR_TRNS_GETHER
,
1069 .ecsr_value
= ECSR_ICD
| ECSR_MPD
,
1070 .ecsipr_value
= ECSIPR_LCHNGIP
| ECSIPR_ICDIP
| ECSIPR_MPDIP
,
1071 .eesipr_value
= EESIPR_RFCOFIP
| EESIPR_ECIIP
|
1072 EESIPR_FTCIP
| EESIPR_TDEIP
| EESIPR_TFUFIP
|
1073 EESIPR_FRIP
| EESIPR_RDEIP
| EESIPR_RFOFIP
|
1074 EESIPR_DLCIP
| EESIPR_CDIP
| EESIPR_TROIP
|
1075 EESIPR_RMAFIP
| EESIPR_CEEFIP
| EESIPR_CELFIP
|
1076 EESIPR_RRFIP
| EESIPR_RTLFIP
| EESIPR_RTSFIP
|
1077 EESIPR_PREIP
| EESIPR_CERFIP
,
1079 .tx_check
= EESR_TC1
| EESR_FTC
,
1080 .eesr_err_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_RABT
|
1081 EESR_RDE
| EESR_RFRMER
| EESR_TFE
| EESR_TDE
,
1092 .irq_flags
= IRQF_SHARED
,
1099 static struct sh_eth_cpu_data sh7619_data
= {
1100 .soft_reset
= sh_eth_soft_reset
,
1102 .register_type
= SH_ETH_REG_FAST_SH3_SH2
,
1104 .edtrr_trns
= EDTRR_TRNS_ETHER
,
1105 .eesipr_value
= EESIPR_RFCOFIP
| EESIPR_ECIIP
|
1106 EESIPR_FTCIP
| EESIPR_TDEIP
| EESIPR_TFUFIP
|
1107 EESIPR_FRIP
| EESIPR_RDEIP
| EESIPR_RFOFIP
|
1108 0x0000f000 | EESIPR_CNDIP
| EESIPR_DLCIP
|
1109 EESIPR_CDIP
| EESIPR_TROIP
| EESIPR_RMAFIP
|
1110 EESIPR_CEEFIP
| EESIPR_CELFIP
|
1111 EESIPR_RRFIP
| EESIPR_RTLFIP
| EESIPR_RTSFIP
|
1112 EESIPR_PREIP
| EESIPR_CERFIP
,
1120 static struct sh_eth_cpu_data sh771x_data
= {
1121 .soft_reset
= sh_eth_soft_reset
,
1123 .register_type
= SH_ETH_REG_FAST_SH3_SH2
,
1125 .edtrr_trns
= EDTRR_TRNS_ETHER
,
1126 .eesipr_value
= EESIPR_RFCOFIP
| EESIPR_ECIIP
|
1127 EESIPR_FTCIP
| EESIPR_TDEIP
| EESIPR_TFUFIP
|
1128 EESIPR_FRIP
| EESIPR_RDEIP
| EESIPR_RFOFIP
|
1129 0x0000f000 | EESIPR_CNDIP
| EESIPR_DLCIP
|
1130 EESIPR_CDIP
| EESIPR_TROIP
| EESIPR_RMAFIP
|
1131 EESIPR_CEEFIP
| EESIPR_CELFIP
|
1132 EESIPR_RRFIP
| EESIPR_RTLFIP
| EESIPR_RTSFIP
|
1133 EESIPR_PREIP
| EESIPR_CERFIP
,
1138 static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data
*cd
)
1140 if (!cd
->ecsr_value
)
1141 cd
->ecsr_value
= DEFAULT_ECSR_INIT
;
1143 if (!cd
->ecsipr_value
)
1144 cd
->ecsipr_value
= DEFAULT_ECSIPR_INIT
;
1146 if (!cd
->fcftr_value
)
1147 cd
->fcftr_value
= DEFAULT_FIFO_F_D_RFF
|
1148 DEFAULT_FIFO_F_D_RFD
;
1151 cd
->fdr_value
= DEFAULT_FDR_INIT
;
1154 cd
->tx_check
= DEFAULT_TX_CHECK
;
1156 if (!cd
->eesr_err_check
)
1157 cd
->eesr_err_check
= DEFAULT_EESR_ERR_CHECK
;
1159 if (!cd
->trscer_err_mask
)
1160 cd
->trscer_err_mask
= DEFAULT_TRSCER_ERR_MASK
;
1163 static void sh_eth_set_receive_align(struct sk_buff
*skb
)
1165 uintptr_t reserve
= (uintptr_t)skb
->data
& (SH_ETH_RX_ALIGN
- 1);
1168 skb_reserve(skb
, SH_ETH_RX_ALIGN
- reserve
);
1171 /* Program the hardware MAC address from dev->dev_addr. */
1172 static void update_mac_address(struct net_device
*ndev
)
1175 (ndev
->dev_addr
[0] << 24) | (ndev
->dev_addr
[1] << 16) |
1176 (ndev
->dev_addr
[2] << 8) | (ndev
->dev_addr
[3]), MAHR
);
1178 (ndev
->dev_addr
[4] << 8) | (ndev
->dev_addr
[5]), MALR
);
1181 /* Get MAC address from SuperH MAC address register
1183 * SuperH's Ethernet device doesn't have 'ROM' to MAC address.
1184 * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g).
1185 * When you want use this device, you must set MAC address in bootloader.
1188 static void read_mac_address(struct net_device
*ndev
, unsigned char *mac
)
1190 if (mac
[0] || mac
[1] || mac
[2] || mac
[3] || mac
[4] || mac
[5]) {
1191 memcpy(ndev
->dev_addr
, mac
, ETH_ALEN
);
1193 u32 mahr
= sh_eth_read(ndev
, MAHR
);
1194 u32 malr
= sh_eth_read(ndev
, MALR
);
1196 ndev
->dev_addr
[0] = (mahr
>> 24) & 0xFF;
1197 ndev
->dev_addr
[1] = (mahr
>> 16) & 0xFF;
1198 ndev
->dev_addr
[2] = (mahr
>> 8) & 0xFF;
1199 ndev
->dev_addr
[3] = (mahr
>> 0) & 0xFF;
1200 ndev
->dev_addr
[4] = (malr
>> 8) & 0xFF;
1201 ndev
->dev_addr
[5] = (malr
>> 0) & 0xFF;
1206 void (*set_gate
)(void *addr
);
1207 struct mdiobb_ctrl ctrl
;
1211 static void sh_mdio_ctrl(struct mdiobb_ctrl
*ctrl
, u32 mask
, int set
)
1213 struct bb_info
*bitbang
= container_of(ctrl
, struct bb_info
, ctrl
);
1216 if (bitbang
->set_gate
)
1217 bitbang
->set_gate(bitbang
->addr
);
1219 pir
= ioread32(bitbang
->addr
);
1224 iowrite32(pir
, bitbang
->addr
);
1227 /* Data I/O pin control */
1228 static void sh_mmd_ctrl(struct mdiobb_ctrl
*ctrl
, int bit
)
1230 sh_mdio_ctrl(ctrl
, PIR_MMD
, bit
);
1234 static void sh_set_mdio(struct mdiobb_ctrl
*ctrl
, int bit
)
1236 sh_mdio_ctrl(ctrl
, PIR_MDO
, bit
);
1240 static int sh_get_mdio(struct mdiobb_ctrl
*ctrl
)
1242 struct bb_info
*bitbang
= container_of(ctrl
, struct bb_info
, ctrl
);
1244 if (bitbang
->set_gate
)
1245 bitbang
->set_gate(bitbang
->addr
);
1247 return (ioread32(bitbang
->addr
) & PIR_MDI
) != 0;
1250 /* MDC pin control */
1251 static void sh_mdc_ctrl(struct mdiobb_ctrl
*ctrl
, int bit
)
1253 sh_mdio_ctrl(ctrl
, PIR_MDC
, bit
);
1256 /* mdio bus control struct */
1257 static struct mdiobb_ops bb_ops
= {
1258 .owner
= THIS_MODULE
,
1259 .set_mdc
= sh_mdc_ctrl
,
1260 .set_mdio_dir
= sh_mmd_ctrl
,
1261 .set_mdio_data
= sh_set_mdio
,
1262 .get_mdio_data
= sh_get_mdio
,
1265 /* free Tx skb function */
1266 static int sh_eth_tx_free(struct net_device
*ndev
, bool sent_only
)
1268 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1269 struct sh_eth_txdesc
*txdesc
;
1274 for (; mdp
->cur_tx
- mdp
->dirty_tx
> 0; mdp
->dirty_tx
++) {
1275 entry
= mdp
->dirty_tx
% mdp
->num_tx_ring
;
1276 txdesc
= &mdp
->tx_ring
[entry
];
1277 sent
= !(txdesc
->status
& cpu_to_le32(TD_TACT
));
1278 if (sent_only
&& !sent
)
1280 /* TACT bit must be checked before all the following reads */
1282 netif_info(mdp
, tx_done
, ndev
,
1283 "tx entry %d status 0x%08x\n",
1284 entry
, le32_to_cpu(txdesc
->status
));
1285 /* Free the original skb. */
1286 if (mdp
->tx_skbuff
[entry
]) {
1287 dma_unmap_single(&mdp
->pdev
->dev
,
1288 le32_to_cpu(txdesc
->addr
),
1289 le32_to_cpu(txdesc
->len
) >> 16,
1291 dev_kfree_skb_irq(mdp
->tx_skbuff
[entry
]);
1292 mdp
->tx_skbuff
[entry
] = NULL
;
1295 txdesc
->status
= cpu_to_le32(TD_TFP
);
1296 if (entry
>= mdp
->num_tx_ring
- 1)
1297 txdesc
->status
|= cpu_to_le32(TD_TDLE
);
1300 ndev
->stats
.tx_packets
++;
1301 ndev
->stats
.tx_bytes
+= le32_to_cpu(txdesc
->len
) >> 16;
1307 /* free skb and descriptor buffer */
1308 static void sh_eth_ring_free(struct net_device
*ndev
)
1310 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1314 for (i
= 0; i
< mdp
->num_rx_ring
; i
++) {
1315 if (mdp
->rx_skbuff
[i
]) {
1316 struct sh_eth_rxdesc
*rxdesc
= &mdp
->rx_ring
[i
];
1318 dma_unmap_single(&mdp
->pdev
->dev
,
1319 le32_to_cpu(rxdesc
->addr
),
1320 ALIGN(mdp
->rx_buf_sz
, 32),
1324 ringsize
= sizeof(struct sh_eth_rxdesc
) * mdp
->num_rx_ring
;
1325 dma_free_coherent(&mdp
->pdev
->dev
, ringsize
, mdp
->rx_ring
,
1327 mdp
->rx_ring
= NULL
;
1330 /* Free Rx skb ringbuffer */
1331 if (mdp
->rx_skbuff
) {
1332 for (i
= 0; i
< mdp
->num_rx_ring
; i
++)
1333 dev_kfree_skb(mdp
->rx_skbuff
[i
]);
1335 kfree(mdp
->rx_skbuff
);
1336 mdp
->rx_skbuff
= NULL
;
1339 sh_eth_tx_free(ndev
, false);
1341 ringsize
= sizeof(struct sh_eth_txdesc
) * mdp
->num_tx_ring
;
1342 dma_free_coherent(&mdp
->pdev
->dev
, ringsize
, mdp
->tx_ring
,
1344 mdp
->tx_ring
= NULL
;
1347 /* Free Tx skb ringbuffer */
1348 kfree(mdp
->tx_skbuff
);
1349 mdp
->tx_skbuff
= NULL
;
1352 /* format skb and descriptor buffer */
1353 static void sh_eth_ring_format(struct net_device
*ndev
)
1355 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1357 struct sk_buff
*skb
;
1358 struct sh_eth_rxdesc
*rxdesc
= NULL
;
1359 struct sh_eth_txdesc
*txdesc
= NULL
;
1360 int rx_ringsize
= sizeof(*rxdesc
) * mdp
->num_rx_ring
;
1361 int tx_ringsize
= sizeof(*txdesc
) * mdp
->num_tx_ring
;
1362 int skbuff_size
= mdp
->rx_buf_sz
+ SH_ETH_RX_ALIGN
+ 32 - 1;
1363 dma_addr_t dma_addr
;
1371 memset(mdp
->rx_ring
, 0, rx_ringsize
);
1373 /* build Rx ring buffer */
1374 for (i
= 0; i
< mdp
->num_rx_ring
; i
++) {
1376 mdp
->rx_skbuff
[i
] = NULL
;
1377 skb
= netdev_alloc_skb(ndev
, skbuff_size
);
1380 sh_eth_set_receive_align(skb
);
1382 /* The size of the buffer is a multiple of 32 bytes. */
1383 buf_len
= ALIGN(mdp
->rx_buf_sz
, 32);
1384 dma_addr
= dma_map_single(&mdp
->pdev
->dev
, skb
->data
, buf_len
,
1386 if (dma_mapping_error(&mdp
->pdev
->dev
, dma_addr
)) {
1390 mdp
->rx_skbuff
[i
] = skb
;
1393 rxdesc
= &mdp
->rx_ring
[i
];
1394 rxdesc
->len
= cpu_to_le32(buf_len
<< 16);
1395 rxdesc
->addr
= cpu_to_le32(dma_addr
);
1396 rxdesc
->status
= cpu_to_le32(RD_RACT
| RD_RFP
);
1398 /* Rx descriptor address set */
1400 sh_eth_write(ndev
, mdp
->rx_desc_dma
, RDLAR
);
1401 if (mdp
->cd
->xdfar_rw
)
1402 sh_eth_write(ndev
, mdp
->rx_desc_dma
, RDFAR
);
1406 mdp
->dirty_rx
= (u32
) (i
- mdp
->num_rx_ring
);
1408 /* Mark the last entry as wrapping the ring. */
1410 rxdesc
->status
|= cpu_to_le32(RD_RDLE
);
1412 memset(mdp
->tx_ring
, 0, tx_ringsize
);
1414 /* build Tx ring buffer */
1415 for (i
= 0; i
< mdp
->num_tx_ring
; i
++) {
1416 mdp
->tx_skbuff
[i
] = NULL
;
1417 txdesc
= &mdp
->tx_ring
[i
];
1418 txdesc
->status
= cpu_to_le32(TD_TFP
);
1419 txdesc
->len
= cpu_to_le32(0);
1421 /* Tx descriptor address set */
1422 sh_eth_write(ndev
, mdp
->tx_desc_dma
, TDLAR
);
1423 if (mdp
->cd
->xdfar_rw
)
1424 sh_eth_write(ndev
, mdp
->tx_desc_dma
, TDFAR
);
1428 txdesc
->status
|= cpu_to_le32(TD_TDLE
);
1431 /* Get skb and descriptor buffer */
1432 static int sh_eth_ring_init(struct net_device
*ndev
)
1434 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1435 int rx_ringsize
, tx_ringsize
;
1437 /* +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
1438 * card needs room to do 8 byte alignment, +2 so we can reserve
1439 * the first 2 bytes, and +16 gets room for the status word from the
1442 mdp
->rx_buf_sz
= (ndev
->mtu
<= 1492 ? PKT_BUF_SZ
:
1443 (((ndev
->mtu
+ 26 + 7) & ~7) + 2 + 16));
1444 if (mdp
->cd
->rpadir
)
1445 mdp
->rx_buf_sz
+= NET_IP_ALIGN
;
1447 /* Allocate RX and TX skb rings */
1448 mdp
->rx_skbuff
= kcalloc(mdp
->num_rx_ring
, sizeof(*mdp
->rx_skbuff
),
1450 if (!mdp
->rx_skbuff
)
1453 mdp
->tx_skbuff
= kcalloc(mdp
->num_tx_ring
, sizeof(*mdp
->tx_skbuff
),
1455 if (!mdp
->tx_skbuff
)
1458 /* Allocate all Rx descriptors. */
1459 rx_ringsize
= sizeof(struct sh_eth_rxdesc
) * mdp
->num_rx_ring
;
1460 mdp
->rx_ring
= dma_alloc_coherent(&mdp
->pdev
->dev
, rx_ringsize
,
1461 &mdp
->rx_desc_dma
, GFP_KERNEL
);
1467 /* Allocate all Tx descriptors. */
1468 tx_ringsize
= sizeof(struct sh_eth_txdesc
) * mdp
->num_tx_ring
;
1469 mdp
->tx_ring
= dma_alloc_coherent(&mdp
->pdev
->dev
, tx_ringsize
,
1470 &mdp
->tx_desc_dma
, GFP_KERNEL
);
1476 /* Free Rx and Tx skb ring buffer and DMA buffer */
1477 sh_eth_ring_free(ndev
);
1482 static int sh_eth_dev_init(struct net_device
*ndev
)
1484 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1488 ret
= mdp
->cd
->soft_reset(ndev
);
1492 if (mdp
->cd
->rmiimode
)
1493 sh_eth_write(ndev
, 0x1, RMIIMODE
);
1495 /* Descriptor format */
1496 sh_eth_ring_format(ndev
);
1497 if (mdp
->cd
->rpadir
)
1498 sh_eth_write(ndev
, NET_IP_ALIGN
<< 16, RPADIR
);
1500 /* all sh_eth int mask */
1501 sh_eth_write(ndev
, 0, EESIPR
);
1503 #if defined(__LITTLE_ENDIAN)
1504 if (mdp
->cd
->hw_swap
)
1505 sh_eth_write(ndev
, EDMR_EL
, EDMR
);
1508 sh_eth_write(ndev
, 0, EDMR
);
1511 sh_eth_write(ndev
, mdp
->cd
->fdr_value
, FDR
);
1512 sh_eth_write(ndev
, 0, TFTR
);
1514 /* Frame recv control (enable multiple-packets per rx irq) */
1515 sh_eth_write(ndev
, RMCR_RNC
, RMCR
);
1517 sh_eth_write(ndev
, mdp
->cd
->trscer_err_mask
, TRSCER
);
1519 /* DMA transfer burst mode */
1521 sh_eth_modify(ndev
, EDMR
, EDMR_NBST
, EDMR_NBST
);
1523 /* Burst cycle count upper-limit */
1525 sh_eth_write(ndev
, 0x800, BCULR
);
1527 sh_eth_write(ndev
, mdp
->cd
->fcftr_value
, FCFTR
);
1529 if (!mdp
->cd
->no_trimd
)
1530 sh_eth_write(ndev
, 0, TRIMD
);
1532 /* Recv frame limit set register */
1533 sh_eth_write(ndev
, ndev
->mtu
+ ETH_HLEN
+ VLAN_HLEN
+ ETH_FCS_LEN
,
1536 sh_eth_modify(ndev
, EESR
, 0, 0);
1537 mdp
->irq_enabled
= true;
1538 sh_eth_write(ndev
, mdp
->cd
->eesipr_value
, EESIPR
);
1540 /* EMAC Mode: PAUSE prohibition; Duplex; RX Checksum; TX; RX */
1541 sh_eth_write(ndev
, ECMR_ZPF
| (mdp
->duplex
? ECMR_DM
: 0) |
1542 (ndev
->features
& NETIF_F_RXCSUM
? ECMR_RCSC
: 0) |
1543 ECMR_TE
| ECMR_RE
, ECMR
);
1545 if (mdp
->cd
->set_rate
)
1546 mdp
->cd
->set_rate(ndev
);
1548 /* E-MAC Status Register clear */
1549 sh_eth_write(ndev
, mdp
->cd
->ecsr_value
, ECSR
);
1551 /* E-MAC Interrupt Enable register */
1552 sh_eth_write(ndev
, mdp
->cd
->ecsipr_value
, ECSIPR
);
1554 /* Set MAC address */
1555 update_mac_address(ndev
);
1559 sh_eth_write(ndev
, 1, APR
);
1561 sh_eth_write(ndev
, 1, MPR
);
1562 if (mdp
->cd
->tpauser
)
1563 sh_eth_write(ndev
, TPAUSER_UNLIMITED
, TPAUSER
);
1565 /* Setting the Rx mode will start the Rx process. */
1566 sh_eth_write(ndev
, EDRRR_R
, EDRRR
);
1571 static void sh_eth_dev_exit(struct net_device
*ndev
)
1573 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1576 /* Deactivate all TX descriptors, so DMA should stop at next
1577 * packet boundary if it's currently running
1579 for (i
= 0; i
< mdp
->num_tx_ring
; i
++)
1580 mdp
->tx_ring
[i
].status
&= ~cpu_to_le32(TD_TACT
);
1582 /* Disable TX FIFO egress to MAC */
1583 sh_eth_rcv_snd_disable(ndev
);
1585 /* Stop RX DMA at next packet boundary */
1586 sh_eth_write(ndev
, 0, EDRRR
);
1588 /* Aside from TX DMA, we can't tell when the hardware is
1589 * really stopped, so we need to reset to make sure.
1590 * Before doing that, wait for long enough to *probably*
1591 * finish transmitting the last packet and poll stats.
1593 msleep(2); /* max frame time at 10 Mbps < 1250 us */
1594 sh_eth_get_stats(ndev
);
1595 mdp
->cd
->soft_reset(ndev
);
1597 /* Set the RMII mode again if required */
1598 if (mdp
->cd
->rmiimode
)
1599 sh_eth_write(ndev
, 0x1, RMIIMODE
);
1601 /* Set MAC address again */
1602 update_mac_address(ndev
);
1605 static void sh_eth_rx_csum(struct sk_buff
*skb
)
1609 /* The hardware checksum is 2 bytes appended to packet data */
1610 if (unlikely(skb
->len
< sizeof(__sum16
)))
1612 hw_csum
= skb_tail_pointer(skb
) - sizeof(__sum16
);
1613 skb
->csum
= csum_unfold((__force __sum16
)get_unaligned_le16(hw_csum
));
1614 skb
->ip_summed
= CHECKSUM_COMPLETE
;
1615 skb_trim(skb
, skb
->len
- sizeof(__sum16
));
1618 /* Packet receive function */
1619 static int sh_eth_rx(struct net_device
*ndev
, u32 intr_status
, int *quota
)
1621 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1622 struct sh_eth_rxdesc
*rxdesc
;
1624 int entry
= mdp
->cur_rx
% mdp
->num_rx_ring
;
1625 int boguscnt
= (mdp
->dirty_rx
+ mdp
->num_rx_ring
) - mdp
->cur_rx
;
1627 struct sk_buff
*skb
;
1629 int skbuff_size
= mdp
->rx_buf_sz
+ SH_ETH_RX_ALIGN
+ 32 - 1;
1630 dma_addr_t dma_addr
;
1634 boguscnt
= min(boguscnt
, *quota
);
1636 rxdesc
= &mdp
->rx_ring
[entry
];
1637 while (!(rxdesc
->status
& cpu_to_le32(RD_RACT
))) {
1638 /* RACT bit must be checked before all the following reads */
1640 desc_status
= le32_to_cpu(rxdesc
->status
);
1641 pkt_len
= le32_to_cpu(rxdesc
->len
) & RD_RFL
;
1646 netif_info(mdp
, rx_status
, ndev
,
1647 "rx entry %d status 0x%08x len %d\n",
1648 entry
, desc_status
, pkt_len
);
1650 if (!(desc_status
& RDFEND
))
1651 ndev
->stats
.rx_length_errors
++;
1653 /* In case of almost all GETHER/ETHERs, the Receive Frame State
1654 * (RFS) bits in the Receive Descriptor 0 are from bit 9 to
1655 * bit 0. However, in case of the R8A7740 and R7S72100
1656 * the RFS bits are from bit 25 to bit 16. So, the
1657 * driver needs right shifting by 16.
1662 skb
= mdp
->rx_skbuff
[entry
];
1663 if (desc_status
& (RD_RFS1
| RD_RFS2
| RD_RFS3
| RD_RFS4
|
1664 RD_RFS5
| RD_RFS6
| RD_RFS10
)) {
1665 ndev
->stats
.rx_errors
++;
1666 if (desc_status
& RD_RFS1
)
1667 ndev
->stats
.rx_crc_errors
++;
1668 if (desc_status
& RD_RFS2
)
1669 ndev
->stats
.rx_frame_errors
++;
1670 if (desc_status
& RD_RFS3
)
1671 ndev
->stats
.rx_length_errors
++;
1672 if (desc_status
& RD_RFS4
)
1673 ndev
->stats
.rx_length_errors
++;
1674 if (desc_status
& RD_RFS6
)
1675 ndev
->stats
.rx_missed_errors
++;
1676 if (desc_status
& RD_RFS10
)
1677 ndev
->stats
.rx_over_errors
++;
1679 dma_addr
= le32_to_cpu(rxdesc
->addr
);
1680 if (!mdp
->cd
->hw_swap
)
1682 phys_to_virt(ALIGN(dma_addr
, 4)),
1684 mdp
->rx_skbuff
[entry
] = NULL
;
1685 if (mdp
->cd
->rpadir
)
1686 skb_reserve(skb
, NET_IP_ALIGN
);
1687 dma_unmap_single(&mdp
->pdev
->dev
, dma_addr
,
1688 ALIGN(mdp
->rx_buf_sz
, 32),
1690 skb_put(skb
, pkt_len
);
1691 skb
->protocol
= eth_type_trans(skb
, ndev
);
1692 if (ndev
->features
& NETIF_F_RXCSUM
)
1693 sh_eth_rx_csum(skb
);
1694 netif_receive_skb(skb
);
1695 ndev
->stats
.rx_packets
++;
1696 ndev
->stats
.rx_bytes
+= pkt_len
;
1697 if (desc_status
& RD_RFS8
)
1698 ndev
->stats
.multicast
++;
1700 entry
= (++mdp
->cur_rx
) % mdp
->num_rx_ring
;
1701 rxdesc
= &mdp
->rx_ring
[entry
];
1704 /* Refill the Rx ring buffers. */
1705 for (; mdp
->cur_rx
- mdp
->dirty_rx
> 0; mdp
->dirty_rx
++) {
1706 entry
= mdp
->dirty_rx
% mdp
->num_rx_ring
;
1707 rxdesc
= &mdp
->rx_ring
[entry
];
1708 /* The size of the buffer is 32 byte boundary. */
1709 buf_len
= ALIGN(mdp
->rx_buf_sz
, 32);
1710 rxdesc
->len
= cpu_to_le32(buf_len
<< 16);
1712 if (mdp
->rx_skbuff
[entry
] == NULL
) {
1713 skb
= netdev_alloc_skb(ndev
, skbuff_size
);
1715 break; /* Better luck next round. */
1716 sh_eth_set_receive_align(skb
);
1717 dma_addr
= dma_map_single(&mdp
->pdev
->dev
, skb
->data
,
1718 buf_len
, DMA_FROM_DEVICE
);
1719 if (dma_mapping_error(&mdp
->pdev
->dev
, dma_addr
)) {
1723 mdp
->rx_skbuff
[entry
] = skb
;
1725 skb_checksum_none_assert(skb
);
1726 rxdesc
->addr
= cpu_to_le32(dma_addr
);
1728 dma_wmb(); /* RACT bit must be set after all the above writes */
1729 if (entry
>= mdp
->num_rx_ring
- 1)
1731 cpu_to_le32(RD_RACT
| RD_RFP
| RD_RDLE
);
1733 rxdesc
->status
|= cpu_to_le32(RD_RACT
| RD_RFP
);
1736 /* Restart Rx engine if stopped. */
1737 /* If we don't need to check status, don't. -KDU */
1738 if (!(sh_eth_read(ndev
, EDRRR
) & EDRRR_R
)) {
1739 /* fix the values for the next receiving if RDE is set */
1740 if (intr_status
& EESR_RDE
&& !mdp
->cd
->no_xdfar
) {
1741 u32 count
= (sh_eth_read(ndev
, RDFAR
) -
1742 sh_eth_read(ndev
, RDLAR
)) >> 4;
1744 mdp
->cur_rx
= count
;
1745 mdp
->dirty_rx
= count
;
1747 sh_eth_write(ndev
, EDRRR_R
, EDRRR
);
1750 *quota
-= limit
- boguscnt
- 1;
1755 static void sh_eth_rcv_snd_disable(struct net_device
*ndev
)
1757 /* disable tx and rx */
1758 sh_eth_modify(ndev
, ECMR
, ECMR_RE
| ECMR_TE
, 0);
1761 static void sh_eth_rcv_snd_enable(struct net_device
*ndev
)
1763 /* enable tx and rx */
1764 sh_eth_modify(ndev
, ECMR
, ECMR_RE
| ECMR_TE
, ECMR_RE
| ECMR_TE
);
1767 /* E-MAC interrupt handler */
1768 static void sh_eth_emac_interrupt(struct net_device
*ndev
)
1770 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1774 felic_stat
= sh_eth_read(ndev
, ECSR
) & sh_eth_read(ndev
, ECSIPR
);
1775 sh_eth_write(ndev
, felic_stat
, ECSR
); /* clear int */
1776 if (felic_stat
& ECSR_ICD
)
1777 ndev
->stats
.tx_carrier_errors
++;
1778 if (felic_stat
& ECSR_MPD
)
1779 pm_wakeup_event(&mdp
->pdev
->dev
, 0);
1780 if (felic_stat
& ECSR_LCHNG
) {
1782 if (mdp
->cd
->no_psr
|| mdp
->no_ether_link
)
1784 link_stat
= sh_eth_read(ndev
, PSR
);
1785 if (mdp
->ether_link_active_low
)
1786 link_stat
= ~link_stat
;
1787 if (!(link_stat
& PHY_ST_LINK
)) {
1788 sh_eth_rcv_snd_disable(ndev
);
1791 sh_eth_modify(ndev
, EESIPR
, EESIPR_ECIIP
, 0);
1793 sh_eth_modify(ndev
, ECSR
, 0, 0);
1794 sh_eth_modify(ndev
, EESIPR
, EESIPR_ECIIP
, EESIPR_ECIIP
);
1795 /* enable tx and rx */
1796 sh_eth_rcv_snd_enable(ndev
);
1801 /* error control function */
1802 static void sh_eth_error(struct net_device
*ndev
, u32 intr_status
)
1804 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1807 if (intr_status
& EESR_TWB
) {
1808 /* Unused write back interrupt */
1809 if (intr_status
& EESR_TABT
) { /* Transmit Abort int */
1810 ndev
->stats
.tx_aborted_errors
++;
1811 netif_err(mdp
, tx_err
, ndev
, "Transmit Abort\n");
1815 if (intr_status
& EESR_RABT
) {
1816 /* Receive Abort int */
1817 if (intr_status
& EESR_RFRMER
) {
1818 /* Receive Frame Overflow int */
1819 ndev
->stats
.rx_frame_errors
++;
1823 if (intr_status
& EESR_TDE
) {
1824 /* Transmit Descriptor Empty int */
1825 ndev
->stats
.tx_fifo_errors
++;
1826 netif_err(mdp
, tx_err
, ndev
, "Transmit Descriptor Empty\n");
1829 if (intr_status
& EESR_TFE
) {
1830 /* FIFO under flow */
1831 ndev
->stats
.tx_fifo_errors
++;
1832 netif_err(mdp
, tx_err
, ndev
, "Transmit FIFO Under flow\n");
1835 if (intr_status
& EESR_RDE
) {
1836 /* Receive Descriptor Empty int */
1837 ndev
->stats
.rx_over_errors
++;
1840 if (intr_status
& EESR_RFE
) {
1841 /* Receive FIFO Overflow int */
1842 ndev
->stats
.rx_fifo_errors
++;
1845 if (!mdp
->cd
->no_ade
&& (intr_status
& EESR_ADE
)) {
1847 ndev
->stats
.tx_fifo_errors
++;
1848 netif_err(mdp
, tx_err
, ndev
, "Address Error\n");
1851 mask
= EESR_TWB
| EESR_TABT
| EESR_ADE
| EESR_TDE
| EESR_TFE
;
1852 if (mdp
->cd
->no_ade
)
1854 if (intr_status
& mask
) {
1856 u32 edtrr
= sh_eth_read(ndev
, EDTRR
);
1859 netdev_err(ndev
, "TX error. status=%8.8x cur_tx=%8.8x dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
1860 intr_status
, mdp
->cur_tx
, mdp
->dirty_tx
,
1861 (u32
)ndev
->state
, edtrr
);
1862 /* dirty buffer free */
1863 sh_eth_tx_free(ndev
, true);
1866 if (edtrr
^ mdp
->cd
->edtrr_trns
) {
1868 sh_eth_write(ndev
, mdp
->cd
->edtrr_trns
, EDTRR
);
1871 netif_wake_queue(ndev
);
1875 static irqreturn_t
sh_eth_interrupt(int irq
, void *netdev
)
1877 struct net_device
*ndev
= netdev
;
1878 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1879 struct sh_eth_cpu_data
*cd
= mdp
->cd
;
1880 irqreturn_t ret
= IRQ_NONE
;
1881 u32 intr_status
, intr_enable
;
1883 spin_lock(&mdp
->lock
);
1885 /* Get interrupt status */
1886 intr_status
= sh_eth_read(ndev
, EESR
);
1887 /* Mask it with the interrupt mask, forcing ECI interrupt to be always
1888 * enabled since it's the one that comes thru regardless of the mask,
1889 * and we need to fully handle it in sh_eth_emac_interrupt() in order
1890 * to quench it as it doesn't get cleared by just writing 1 to the ECI
1893 intr_enable
= sh_eth_read(ndev
, EESIPR
);
1894 intr_status
&= intr_enable
| EESIPR_ECIIP
;
1895 if (intr_status
& (EESR_RX_CHECK
| cd
->tx_check
| EESR_ECI
|
1896 cd
->eesr_err_check
))
1901 if (unlikely(!mdp
->irq_enabled
)) {
1902 sh_eth_write(ndev
, 0, EESIPR
);
1906 if (intr_status
& EESR_RX_CHECK
) {
1907 if (napi_schedule_prep(&mdp
->napi
)) {
1908 /* Mask Rx interrupts */
1909 sh_eth_write(ndev
, intr_enable
& ~EESR_RX_CHECK
,
1911 __napi_schedule(&mdp
->napi
);
1914 "ignoring interrupt, status 0x%08x, mask 0x%08x.\n",
1915 intr_status
, intr_enable
);
1920 if (intr_status
& cd
->tx_check
) {
1921 /* Clear Tx interrupts */
1922 sh_eth_write(ndev
, intr_status
& cd
->tx_check
, EESR
);
1924 sh_eth_tx_free(ndev
, true);
1925 netif_wake_queue(ndev
);
1928 /* E-MAC interrupt */
1929 if (intr_status
& EESR_ECI
)
1930 sh_eth_emac_interrupt(ndev
);
1932 if (intr_status
& cd
->eesr_err_check
) {
1933 /* Clear error interrupts */
1934 sh_eth_write(ndev
, intr_status
& cd
->eesr_err_check
, EESR
);
1936 sh_eth_error(ndev
, intr_status
);
1940 spin_unlock(&mdp
->lock
);
1945 static int sh_eth_poll(struct napi_struct
*napi
, int budget
)
1947 struct sh_eth_private
*mdp
= container_of(napi
, struct sh_eth_private
,
1949 struct net_device
*ndev
= napi
->dev
;
1954 intr_status
= sh_eth_read(ndev
, EESR
);
1955 if (!(intr_status
& EESR_RX_CHECK
))
1957 /* Clear Rx interrupts */
1958 sh_eth_write(ndev
, intr_status
& EESR_RX_CHECK
, EESR
);
1960 if (sh_eth_rx(ndev
, intr_status
, "a
))
1964 napi_complete(napi
);
1966 /* Reenable Rx interrupts */
1967 if (mdp
->irq_enabled
)
1968 sh_eth_write(ndev
, mdp
->cd
->eesipr_value
, EESIPR
);
1970 return budget
- quota
;
1973 /* PHY state control function */
1974 static void sh_eth_adjust_link(struct net_device
*ndev
)
1976 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1977 struct phy_device
*phydev
= ndev
->phydev
;
1978 unsigned long flags
;
1981 spin_lock_irqsave(&mdp
->lock
, flags
);
1983 /* Disable TX and RX right over here, if E-MAC change is ignored */
1984 if (mdp
->cd
->no_psr
|| mdp
->no_ether_link
)
1985 sh_eth_rcv_snd_disable(ndev
);
1988 if (phydev
->duplex
!= mdp
->duplex
) {
1990 mdp
->duplex
= phydev
->duplex
;
1991 if (mdp
->cd
->set_duplex
)
1992 mdp
->cd
->set_duplex(ndev
);
1995 if (phydev
->speed
!= mdp
->speed
) {
1997 mdp
->speed
= phydev
->speed
;
1998 if (mdp
->cd
->set_rate
)
1999 mdp
->cd
->set_rate(ndev
);
2002 sh_eth_modify(ndev
, ECMR
, ECMR_TXF
, 0);
2004 mdp
->link
= phydev
->link
;
2006 } else if (mdp
->link
) {
2013 /* Enable TX and RX right over here, if E-MAC change is ignored */
2014 if ((mdp
->cd
->no_psr
|| mdp
->no_ether_link
) && phydev
->link
)
2015 sh_eth_rcv_snd_enable(ndev
);
2017 spin_unlock_irqrestore(&mdp
->lock
, flags
);
2019 if (new_state
&& netif_msg_link(mdp
))
2020 phy_print_status(phydev
);
2023 /* PHY init function */
2024 static int sh_eth_phy_init(struct net_device
*ndev
)
2026 struct device_node
*np
= ndev
->dev
.parent
->of_node
;
2027 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2028 struct phy_device
*phydev
;
2034 /* Try connect to PHY */
2036 struct device_node
*pn
;
2038 pn
= of_parse_phandle(np
, "phy-handle", 0);
2039 phydev
= of_phy_connect(ndev
, pn
,
2040 sh_eth_adjust_link
, 0,
2041 mdp
->phy_interface
);
2045 phydev
= ERR_PTR(-ENOENT
);
2047 char phy_id
[MII_BUS_ID_SIZE
+ 3];
2049 snprintf(phy_id
, sizeof(phy_id
), PHY_ID_FMT
,
2050 mdp
->mii_bus
->id
, mdp
->phy_id
);
2052 phydev
= phy_connect(ndev
, phy_id
, sh_eth_adjust_link
,
2053 mdp
->phy_interface
);
2056 if (IS_ERR(phydev
)) {
2057 netdev_err(ndev
, "failed to connect PHY\n");
2058 return PTR_ERR(phydev
);
2061 /* mask with MAC supported features */
2062 if (mdp
->cd
->register_type
!= SH_ETH_REG_GIGABIT
) {
2063 int err
= phy_set_max_speed(phydev
, SPEED_100
);
2065 netdev_err(ndev
, "failed to limit PHY to 100 Mbit/s\n");
2066 phy_disconnect(phydev
);
2071 phy_attached_info(phydev
);
2076 /* PHY control start function */
2077 static int sh_eth_phy_start(struct net_device
*ndev
)
2081 ret
= sh_eth_phy_init(ndev
);
2085 phy_start(ndev
->phydev
);
2090 /* If it is ever necessary to increase SH_ETH_REG_DUMP_MAX_REGS, the
2091 * version must be bumped as well. Just adding registers up to that
2092 * limit is fine, as long as the existing register indices don't
2095 #define SH_ETH_REG_DUMP_VERSION 1
2096 #define SH_ETH_REG_DUMP_MAX_REGS 256
2098 static size_t __sh_eth_get_regs(struct net_device
*ndev
, u32
*buf
)
2100 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2101 struct sh_eth_cpu_data
*cd
= mdp
->cd
;
2105 BUILD_BUG_ON(SH_ETH_MAX_REGISTER_OFFSET
> SH_ETH_REG_DUMP_MAX_REGS
);
2107 /* Dump starts with a bitmap that tells ethtool which
2108 * registers are defined for this chip.
2110 len
= DIV_ROUND_UP(SH_ETH_REG_DUMP_MAX_REGS
, 32);
2118 /* Add a register to the dump, if it has a defined offset.
2119 * This automatically skips most undefined registers, but for
2120 * some it is also necessary to check a capability flag in
2121 * struct sh_eth_cpu_data.
2123 #define mark_reg_valid(reg) valid_map[reg / 32] |= 1U << (reg % 32)
2124 #define add_reg_from(reg, read_expr) do { \
2125 if (mdp->reg_offset[reg] != SH_ETH_OFFSET_INVALID) { \
2127 mark_reg_valid(reg); \
2128 *buf++ = read_expr; \
2133 #define add_reg(reg) add_reg_from(reg, sh_eth_read(ndev, reg))
2134 #define add_tsu_reg(reg) add_reg_from(reg, sh_eth_tsu_read(mdp, reg))
2206 add_tsu_reg(TSU_CTRST
);
2207 if (cd
->dual_port
) {
2208 add_tsu_reg(TSU_FWEN0
);
2209 add_tsu_reg(TSU_FWEN1
);
2210 add_tsu_reg(TSU_FCM
);
2211 add_tsu_reg(TSU_BSYSL0
);
2212 add_tsu_reg(TSU_BSYSL1
);
2213 add_tsu_reg(TSU_PRISL0
);
2214 add_tsu_reg(TSU_PRISL1
);
2215 add_tsu_reg(TSU_FWSL0
);
2216 add_tsu_reg(TSU_FWSL1
);
2218 add_tsu_reg(TSU_FWSLC
);
2219 if (cd
->dual_port
) {
2220 add_tsu_reg(TSU_QTAGM0
);
2221 add_tsu_reg(TSU_QTAGM1
);
2222 add_tsu_reg(TSU_FWSR
);
2223 add_tsu_reg(TSU_FWINMK
);
2224 add_tsu_reg(TSU_ADQT0
);
2225 add_tsu_reg(TSU_ADQT1
);
2226 add_tsu_reg(TSU_VTAG0
);
2227 add_tsu_reg(TSU_VTAG1
);
2229 add_tsu_reg(TSU_ADSBSY
);
2230 add_tsu_reg(TSU_TEN
);
2231 add_tsu_reg(TSU_POST1
);
2232 add_tsu_reg(TSU_POST2
);
2233 add_tsu_reg(TSU_POST3
);
2234 add_tsu_reg(TSU_POST4
);
2235 /* This is the start of a table, not just a single register. */
2239 mark_reg_valid(TSU_ADRH0
);
2240 for (i
= 0; i
< SH_ETH_TSU_CAM_ENTRIES
* 2; i
++)
2241 *buf
++ = ioread32(mdp
->tsu_addr
+
2242 mdp
->reg_offset
[TSU_ADRH0
] +
2245 len
+= SH_ETH_TSU_CAM_ENTRIES
* 2;
2248 #undef mark_reg_valid
2256 static int sh_eth_get_regs_len(struct net_device
*ndev
)
2258 return __sh_eth_get_regs(ndev
, NULL
);
2261 static void sh_eth_get_regs(struct net_device
*ndev
, struct ethtool_regs
*regs
,
2264 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2266 regs
->version
= SH_ETH_REG_DUMP_VERSION
;
2268 pm_runtime_get_sync(&mdp
->pdev
->dev
);
2269 __sh_eth_get_regs(ndev
, buf
);
2270 pm_runtime_put_sync(&mdp
->pdev
->dev
);
2273 static u32
sh_eth_get_msglevel(struct net_device
*ndev
)
2275 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2276 return mdp
->msg_enable
;
2279 static void sh_eth_set_msglevel(struct net_device
*ndev
, u32 value
)
2281 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2282 mdp
->msg_enable
= value
;
2285 static const char sh_eth_gstrings_stats
[][ETH_GSTRING_LEN
] = {
2286 "rx_current", "tx_current",
2287 "rx_dirty", "tx_dirty",
2289 #define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats)
2291 static int sh_eth_get_sset_count(struct net_device
*netdev
, int sset
)
2295 return SH_ETH_STATS_LEN
;
2301 static void sh_eth_get_ethtool_stats(struct net_device
*ndev
,
2302 struct ethtool_stats
*stats
, u64
*data
)
2304 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2307 /* device-specific stats */
2308 data
[i
++] = mdp
->cur_rx
;
2309 data
[i
++] = mdp
->cur_tx
;
2310 data
[i
++] = mdp
->dirty_rx
;
2311 data
[i
++] = mdp
->dirty_tx
;
2314 static void sh_eth_get_strings(struct net_device
*ndev
, u32 stringset
, u8
*data
)
2316 switch (stringset
) {
2318 memcpy(data
, *sh_eth_gstrings_stats
,
2319 sizeof(sh_eth_gstrings_stats
));
2324 static void sh_eth_get_ringparam(struct net_device
*ndev
,
2325 struct ethtool_ringparam
*ring
)
2327 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2329 ring
->rx_max_pending
= RX_RING_MAX
;
2330 ring
->tx_max_pending
= TX_RING_MAX
;
2331 ring
->rx_pending
= mdp
->num_rx_ring
;
2332 ring
->tx_pending
= mdp
->num_tx_ring
;
2335 static int sh_eth_set_ringparam(struct net_device
*ndev
,
2336 struct ethtool_ringparam
*ring
)
2338 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2341 if (ring
->tx_pending
> TX_RING_MAX
||
2342 ring
->rx_pending
> RX_RING_MAX
||
2343 ring
->tx_pending
< TX_RING_MIN
||
2344 ring
->rx_pending
< RX_RING_MIN
)
2346 if (ring
->rx_mini_pending
|| ring
->rx_jumbo_pending
)
2349 if (netif_running(ndev
)) {
2350 netif_device_detach(ndev
);
2351 netif_tx_disable(ndev
);
2353 /* Serialise with the interrupt handler and NAPI, then
2354 * disable interrupts. We have to clear the
2355 * irq_enabled flag first to ensure that interrupts
2356 * won't be re-enabled.
2358 mdp
->irq_enabled
= false;
2359 synchronize_irq(ndev
->irq
);
2360 napi_synchronize(&mdp
->napi
);
2361 sh_eth_write(ndev
, 0x0000, EESIPR
);
2363 sh_eth_dev_exit(ndev
);
2365 /* Free all the skbuffs in the Rx queue and the DMA buffers. */
2366 sh_eth_ring_free(ndev
);
2369 /* Set new parameters */
2370 mdp
->num_rx_ring
= ring
->rx_pending
;
2371 mdp
->num_tx_ring
= ring
->tx_pending
;
2373 if (netif_running(ndev
)) {
2374 ret
= sh_eth_ring_init(ndev
);
2376 netdev_err(ndev
, "%s: sh_eth_ring_init failed.\n",
2380 ret
= sh_eth_dev_init(ndev
);
2382 netdev_err(ndev
, "%s: sh_eth_dev_init failed.\n",
2387 netif_device_attach(ndev
);
2393 static void sh_eth_get_wol(struct net_device
*ndev
, struct ethtool_wolinfo
*wol
)
2395 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2400 if (mdp
->cd
->magic
) {
2401 wol
->supported
= WAKE_MAGIC
;
2402 wol
->wolopts
= mdp
->wol_enabled
? WAKE_MAGIC
: 0;
2406 static int sh_eth_set_wol(struct net_device
*ndev
, struct ethtool_wolinfo
*wol
)
2408 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2410 if (!mdp
->cd
->magic
|| wol
->wolopts
& ~WAKE_MAGIC
)
2413 mdp
->wol_enabled
= !!(wol
->wolopts
& WAKE_MAGIC
);
2415 device_set_wakeup_enable(&mdp
->pdev
->dev
, mdp
->wol_enabled
);
2420 static const struct ethtool_ops sh_eth_ethtool_ops
= {
2421 .get_regs_len
= sh_eth_get_regs_len
,
2422 .get_regs
= sh_eth_get_regs
,
2423 .nway_reset
= phy_ethtool_nway_reset
,
2424 .get_msglevel
= sh_eth_get_msglevel
,
2425 .set_msglevel
= sh_eth_set_msglevel
,
2426 .get_link
= ethtool_op_get_link
,
2427 .get_strings
= sh_eth_get_strings
,
2428 .get_ethtool_stats
= sh_eth_get_ethtool_stats
,
2429 .get_sset_count
= sh_eth_get_sset_count
,
2430 .get_ringparam
= sh_eth_get_ringparam
,
2431 .set_ringparam
= sh_eth_set_ringparam
,
2432 .get_link_ksettings
= phy_ethtool_get_link_ksettings
,
2433 .set_link_ksettings
= phy_ethtool_set_link_ksettings
,
2434 .get_wol
= sh_eth_get_wol
,
2435 .set_wol
= sh_eth_set_wol
,
2438 /* network device open function */
2439 static int sh_eth_open(struct net_device
*ndev
)
2441 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2444 pm_runtime_get_sync(&mdp
->pdev
->dev
);
2446 napi_enable(&mdp
->napi
);
2448 ret
= request_irq(ndev
->irq
, sh_eth_interrupt
,
2449 mdp
->cd
->irq_flags
, ndev
->name
, ndev
);
2451 netdev_err(ndev
, "Can not assign IRQ number\n");
2455 /* Descriptor set */
2456 ret
= sh_eth_ring_init(ndev
);
2461 ret
= sh_eth_dev_init(ndev
);
2465 /* PHY control start*/
2466 ret
= sh_eth_phy_start(ndev
);
2470 netif_start_queue(ndev
);
2477 free_irq(ndev
->irq
, ndev
);
2479 napi_disable(&mdp
->napi
);
2480 pm_runtime_put_sync(&mdp
->pdev
->dev
);
2484 /* Timeout function */
2485 static void sh_eth_tx_timeout(struct net_device
*ndev
, unsigned int txqueue
)
2487 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2488 struct sh_eth_rxdesc
*rxdesc
;
2491 netif_stop_queue(ndev
);
2493 netif_err(mdp
, timer
, ndev
,
2494 "transmit timed out, status %8.8x, resetting...\n",
2495 sh_eth_read(ndev
, EESR
));
2497 /* tx_errors count up */
2498 ndev
->stats
.tx_errors
++;
2500 /* Free all the skbuffs in the Rx queue. */
2501 for (i
= 0; i
< mdp
->num_rx_ring
; i
++) {
2502 rxdesc
= &mdp
->rx_ring
[i
];
2503 rxdesc
->status
= cpu_to_le32(0);
2504 rxdesc
->addr
= cpu_to_le32(0xBADF00D0);
2505 dev_kfree_skb(mdp
->rx_skbuff
[i
]);
2506 mdp
->rx_skbuff
[i
] = NULL
;
2508 for (i
= 0; i
< mdp
->num_tx_ring
; i
++) {
2509 dev_kfree_skb(mdp
->tx_skbuff
[i
]);
2510 mdp
->tx_skbuff
[i
] = NULL
;
2514 sh_eth_dev_init(ndev
);
2516 netif_start_queue(ndev
);
2519 /* Packet transmit function */
2520 static int sh_eth_start_xmit(struct sk_buff
*skb
, struct net_device
*ndev
)
2522 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2523 struct sh_eth_txdesc
*txdesc
;
2524 dma_addr_t dma_addr
;
2526 unsigned long flags
;
2528 spin_lock_irqsave(&mdp
->lock
, flags
);
2529 if ((mdp
->cur_tx
- mdp
->dirty_tx
) >= (mdp
->num_tx_ring
- 4)) {
2530 if (!sh_eth_tx_free(ndev
, true)) {
2531 netif_warn(mdp
, tx_queued
, ndev
, "TxFD exhausted.\n");
2532 netif_stop_queue(ndev
);
2533 spin_unlock_irqrestore(&mdp
->lock
, flags
);
2534 return NETDEV_TX_BUSY
;
2537 spin_unlock_irqrestore(&mdp
->lock
, flags
);
2539 if (skb_put_padto(skb
, ETH_ZLEN
))
2540 return NETDEV_TX_OK
;
2542 entry
= mdp
->cur_tx
% mdp
->num_tx_ring
;
2543 mdp
->tx_skbuff
[entry
] = skb
;
2544 txdesc
= &mdp
->tx_ring
[entry
];
2546 if (!mdp
->cd
->hw_swap
)
2547 sh_eth_soft_swap(PTR_ALIGN(skb
->data
, 4), skb
->len
+ 2);
2548 dma_addr
= dma_map_single(&mdp
->pdev
->dev
, skb
->data
, skb
->len
,
2550 if (dma_mapping_error(&mdp
->pdev
->dev
, dma_addr
)) {
2552 return NETDEV_TX_OK
;
2554 txdesc
->addr
= cpu_to_le32(dma_addr
);
2555 txdesc
->len
= cpu_to_le32(skb
->len
<< 16);
2557 dma_wmb(); /* TACT bit must be set after all the above writes */
2558 if (entry
>= mdp
->num_tx_ring
- 1)
2559 txdesc
->status
|= cpu_to_le32(TD_TACT
| TD_TDLE
);
2561 txdesc
->status
|= cpu_to_le32(TD_TACT
);
2565 if (!(sh_eth_read(ndev
, EDTRR
) & mdp
->cd
->edtrr_trns
))
2566 sh_eth_write(ndev
, mdp
->cd
->edtrr_trns
, EDTRR
);
2568 return NETDEV_TX_OK
;
2571 /* The statistics registers have write-clear behaviour, which means we
2572 * will lose any increment between the read and write. We mitigate
2573 * this by only clearing when we read a non-zero value, so we will
2574 * never falsely report a total of zero.
2577 sh_eth_update_stat(struct net_device
*ndev
, unsigned long *stat
, int reg
)
2579 u32 delta
= sh_eth_read(ndev
, reg
);
2583 sh_eth_write(ndev
, 0, reg
);
2587 static struct net_device_stats
*sh_eth_get_stats(struct net_device
*ndev
)
2589 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2591 if (mdp
->cd
->no_tx_cntrs
)
2592 return &ndev
->stats
;
2594 if (!mdp
->is_opened
)
2595 return &ndev
->stats
;
2597 sh_eth_update_stat(ndev
, &ndev
->stats
.tx_dropped
, TROCR
);
2598 sh_eth_update_stat(ndev
, &ndev
->stats
.collisions
, CDCR
);
2599 sh_eth_update_stat(ndev
, &ndev
->stats
.tx_carrier_errors
, LCCR
);
2601 if (mdp
->cd
->cexcr
) {
2602 sh_eth_update_stat(ndev
, &ndev
->stats
.tx_carrier_errors
,
2604 sh_eth_update_stat(ndev
, &ndev
->stats
.tx_carrier_errors
,
2607 sh_eth_update_stat(ndev
, &ndev
->stats
.tx_carrier_errors
,
2611 return &ndev
->stats
;
2614 /* device close function */
2615 static int sh_eth_close(struct net_device
*ndev
)
2617 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2619 netif_stop_queue(ndev
);
2621 /* Serialise with the interrupt handler and NAPI, then disable
2622 * interrupts. We have to clear the irq_enabled flag first to
2623 * ensure that interrupts won't be re-enabled.
2625 mdp
->irq_enabled
= false;
2626 synchronize_irq(ndev
->irq
);
2627 napi_disable(&mdp
->napi
);
2628 sh_eth_write(ndev
, 0x0000, EESIPR
);
2630 sh_eth_dev_exit(ndev
);
2632 /* PHY Disconnect */
2634 phy_stop(ndev
->phydev
);
2635 phy_disconnect(ndev
->phydev
);
2638 free_irq(ndev
->irq
, ndev
);
2640 /* Free all the skbuffs in the Rx queue and the DMA buffer. */
2641 sh_eth_ring_free(ndev
);
2643 pm_runtime_put_sync(&mdp
->pdev
->dev
);
2650 static int sh_eth_change_mtu(struct net_device
*ndev
, int new_mtu
)
2652 if (netif_running(ndev
))
2655 ndev
->mtu
= new_mtu
;
2656 netdev_update_features(ndev
);
2661 /* For TSU_POSTn. Please refer to the manual about this (strange) bitfields */
2662 static u32
sh_eth_tsu_get_post_mask(int entry
)
2664 return 0x0f << (28 - ((entry
% 8) * 4));
2667 static u32
sh_eth_tsu_get_post_bit(struct sh_eth_private
*mdp
, int entry
)
2669 return (0x08 >> (mdp
->port
<< 1)) << (28 - ((entry
% 8) * 4));
2672 static void sh_eth_tsu_enable_cam_entry_post(struct net_device
*ndev
,
2675 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2676 int reg
= TSU_POST1
+ entry
/ 8;
2679 tmp
= sh_eth_tsu_read(mdp
, reg
);
2680 sh_eth_tsu_write(mdp
, tmp
| sh_eth_tsu_get_post_bit(mdp
, entry
), reg
);
2683 static bool sh_eth_tsu_disable_cam_entry_post(struct net_device
*ndev
,
2686 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2687 int reg
= TSU_POST1
+ entry
/ 8;
2688 u32 post_mask
, ref_mask
, tmp
;
2690 post_mask
= sh_eth_tsu_get_post_mask(entry
);
2691 ref_mask
= sh_eth_tsu_get_post_bit(mdp
, entry
) & ~post_mask
;
2693 tmp
= sh_eth_tsu_read(mdp
, reg
);
2694 sh_eth_tsu_write(mdp
, tmp
& ~post_mask
, reg
);
2696 /* If other port enables, the function returns "true" */
2697 return tmp
& ref_mask
;
2700 static int sh_eth_tsu_busy(struct net_device
*ndev
)
2702 int timeout
= SH_ETH_TSU_TIMEOUT_MS
* 100;
2703 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2705 while ((sh_eth_tsu_read(mdp
, TSU_ADSBSY
) & TSU_ADSBSY_0
)) {
2709 netdev_err(ndev
, "%s: timeout\n", __func__
);
2717 static int sh_eth_tsu_write_entry(struct net_device
*ndev
, u16 offset
,
2720 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2723 val
= addr
[0] << 24 | addr
[1] << 16 | addr
[2] << 8 | addr
[3];
2724 iowrite32(val
, mdp
->tsu_addr
+ offset
);
2725 if (sh_eth_tsu_busy(ndev
) < 0)
2728 val
= addr
[4] << 8 | addr
[5];
2729 iowrite32(val
, mdp
->tsu_addr
+ offset
+ 4);
2730 if (sh_eth_tsu_busy(ndev
) < 0)
2736 static void sh_eth_tsu_read_entry(struct net_device
*ndev
, u16 offset
, u8
*addr
)
2738 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2741 val
= ioread32(mdp
->tsu_addr
+ offset
);
2742 addr
[0] = (val
>> 24) & 0xff;
2743 addr
[1] = (val
>> 16) & 0xff;
2744 addr
[2] = (val
>> 8) & 0xff;
2745 addr
[3] = val
& 0xff;
2746 val
= ioread32(mdp
->tsu_addr
+ offset
+ 4);
2747 addr
[4] = (val
>> 8) & 0xff;
2748 addr
[5] = val
& 0xff;
2752 static int sh_eth_tsu_find_entry(struct net_device
*ndev
, const u8
*addr
)
2754 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2755 u16 reg_offset
= sh_eth_tsu_get_offset(mdp
, TSU_ADRH0
);
2757 u8 c_addr
[ETH_ALEN
];
2759 for (i
= 0; i
< SH_ETH_TSU_CAM_ENTRIES
; i
++, reg_offset
+= 8) {
2760 sh_eth_tsu_read_entry(ndev
, reg_offset
, c_addr
);
2761 if (ether_addr_equal(addr
, c_addr
))
2768 static int sh_eth_tsu_find_empty(struct net_device
*ndev
)
2773 memset(blank
, 0, sizeof(blank
));
2774 entry
= sh_eth_tsu_find_entry(ndev
, blank
);
2775 return (entry
< 0) ? -ENOMEM
: entry
;
2778 static int sh_eth_tsu_disable_cam_entry_table(struct net_device
*ndev
,
2781 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2782 u16 reg_offset
= sh_eth_tsu_get_offset(mdp
, TSU_ADRH0
);
2786 sh_eth_tsu_write(mdp
, sh_eth_tsu_read(mdp
, TSU_TEN
) &
2787 ~(1 << (31 - entry
)), TSU_TEN
);
2789 memset(blank
, 0, sizeof(blank
));
2790 ret
= sh_eth_tsu_write_entry(ndev
, reg_offset
+ entry
* 8, blank
);
2796 static int sh_eth_tsu_add_entry(struct net_device
*ndev
, const u8
*addr
)
2798 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2799 u16 reg_offset
= sh_eth_tsu_get_offset(mdp
, TSU_ADRH0
);
2805 i
= sh_eth_tsu_find_entry(ndev
, addr
);
2807 /* No entry found, create one */
2808 i
= sh_eth_tsu_find_empty(ndev
);
2811 ret
= sh_eth_tsu_write_entry(ndev
, reg_offset
+ i
* 8, addr
);
2815 /* Enable the entry */
2816 sh_eth_tsu_write(mdp
, sh_eth_tsu_read(mdp
, TSU_TEN
) |
2817 (1 << (31 - i
)), TSU_TEN
);
2820 /* Entry found or created, enable POST */
2821 sh_eth_tsu_enable_cam_entry_post(ndev
, i
);
2826 static int sh_eth_tsu_del_entry(struct net_device
*ndev
, const u8
*addr
)
2828 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2834 i
= sh_eth_tsu_find_entry(ndev
, addr
);
2837 if (sh_eth_tsu_disable_cam_entry_post(ndev
, i
))
2840 /* Disable the entry if both ports was disabled */
2841 ret
= sh_eth_tsu_disable_cam_entry_table(ndev
, i
);
2849 static int sh_eth_tsu_purge_all(struct net_device
*ndev
)
2851 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2857 for (i
= 0; i
< SH_ETH_TSU_CAM_ENTRIES
; i
++) {
2858 if (sh_eth_tsu_disable_cam_entry_post(ndev
, i
))
2861 /* Disable the entry if both ports was disabled */
2862 ret
= sh_eth_tsu_disable_cam_entry_table(ndev
, i
);
2870 static void sh_eth_tsu_purge_mcast(struct net_device
*ndev
)
2872 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2873 u16 reg_offset
= sh_eth_tsu_get_offset(mdp
, TSU_ADRH0
);
2880 for (i
= 0; i
< SH_ETH_TSU_CAM_ENTRIES
; i
++, reg_offset
+= 8) {
2881 sh_eth_tsu_read_entry(ndev
, reg_offset
, addr
);
2882 if (is_multicast_ether_addr(addr
))
2883 sh_eth_tsu_del_entry(ndev
, addr
);
2887 /* Update promiscuous flag and multicast filter */
2888 static void sh_eth_set_rx_mode(struct net_device
*ndev
)
2890 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2893 unsigned long flags
;
2895 spin_lock_irqsave(&mdp
->lock
, flags
);
2896 /* Initial condition is MCT = 1, PRM = 0.
2897 * Depending on ndev->flags, set PRM or clear MCT
2899 ecmr_bits
= sh_eth_read(ndev
, ECMR
) & ~ECMR_PRM
;
2901 ecmr_bits
|= ECMR_MCT
;
2903 if (!(ndev
->flags
& IFF_MULTICAST
)) {
2904 sh_eth_tsu_purge_mcast(ndev
);
2907 if (ndev
->flags
& IFF_ALLMULTI
) {
2908 sh_eth_tsu_purge_mcast(ndev
);
2909 ecmr_bits
&= ~ECMR_MCT
;
2913 if (ndev
->flags
& IFF_PROMISC
) {
2914 sh_eth_tsu_purge_all(ndev
);
2915 ecmr_bits
= (ecmr_bits
& ~ECMR_MCT
) | ECMR_PRM
;
2916 } else if (mdp
->cd
->tsu
) {
2917 struct netdev_hw_addr
*ha
;
2918 netdev_for_each_mc_addr(ha
, ndev
) {
2919 if (mcast_all
&& is_multicast_ether_addr(ha
->addr
))
2922 if (sh_eth_tsu_add_entry(ndev
, ha
->addr
) < 0) {
2924 sh_eth_tsu_purge_mcast(ndev
);
2925 ecmr_bits
&= ~ECMR_MCT
;
2932 /* update the ethernet mode */
2933 sh_eth_write(ndev
, ecmr_bits
, ECMR
);
2935 spin_unlock_irqrestore(&mdp
->lock
, flags
);
2938 static void sh_eth_set_rx_csum(struct net_device
*ndev
, bool enable
)
2940 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2941 unsigned long flags
;
2943 spin_lock_irqsave(&mdp
->lock
, flags
);
2945 /* Disable TX and RX */
2946 sh_eth_rcv_snd_disable(ndev
);
2948 /* Modify RX Checksum setting */
2949 sh_eth_modify(ndev
, ECMR
, ECMR_RCSC
, enable
? ECMR_RCSC
: 0);
2951 /* Enable TX and RX */
2952 sh_eth_rcv_snd_enable(ndev
);
2954 spin_unlock_irqrestore(&mdp
->lock
, flags
);
2957 static int sh_eth_set_features(struct net_device
*ndev
,
2958 netdev_features_t features
)
2960 netdev_features_t changed
= ndev
->features
^ features
;
2961 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2963 if (changed
& NETIF_F_RXCSUM
&& mdp
->cd
->rx_csum
)
2964 sh_eth_set_rx_csum(ndev
, features
& NETIF_F_RXCSUM
);
2966 ndev
->features
= features
;
2971 static int sh_eth_get_vtag_index(struct sh_eth_private
*mdp
)
2979 static int sh_eth_vlan_rx_add_vid(struct net_device
*ndev
,
2980 __be16 proto
, u16 vid
)
2982 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2983 int vtag_reg_index
= sh_eth_get_vtag_index(mdp
);
2985 if (unlikely(!mdp
->cd
->tsu
))
2988 /* No filtering if vid = 0 */
2992 mdp
->vlan_num_ids
++;
2994 /* The controller has one VLAN tag HW filter. So, if the filter is
2995 * already enabled, the driver disables it and the filte
2997 if (mdp
->vlan_num_ids
> 1) {
2998 /* disable VLAN filter */
2999 sh_eth_tsu_write(mdp
, 0, vtag_reg_index
);
3003 sh_eth_tsu_write(mdp
, TSU_VTAG_ENABLE
| (vid
& TSU_VTAG_VID_MASK
),
3009 static int sh_eth_vlan_rx_kill_vid(struct net_device
*ndev
,
3010 __be16 proto
, u16 vid
)
3012 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
3013 int vtag_reg_index
= sh_eth_get_vtag_index(mdp
);
3015 if (unlikely(!mdp
->cd
->tsu
))
3018 /* No filtering if vid = 0 */
3022 mdp
->vlan_num_ids
--;
3023 sh_eth_tsu_write(mdp
, 0, vtag_reg_index
);
3028 /* SuperH's TSU register init function */
3029 static void sh_eth_tsu_init(struct sh_eth_private
*mdp
)
3031 if (!mdp
->cd
->dual_port
) {
3032 sh_eth_tsu_write(mdp
, 0, TSU_TEN
); /* Disable all CAM entry */
3033 sh_eth_tsu_write(mdp
, TSU_FWSLC_POSTENU
| TSU_FWSLC_POSTENL
,
3034 TSU_FWSLC
); /* Enable POST registers */
3038 sh_eth_tsu_write(mdp
, 0, TSU_FWEN0
); /* Disable forward(0->1) */
3039 sh_eth_tsu_write(mdp
, 0, TSU_FWEN1
); /* Disable forward(1->0) */
3040 sh_eth_tsu_write(mdp
, 0, TSU_FCM
); /* forward fifo 3k-3k */
3041 sh_eth_tsu_write(mdp
, 0xc, TSU_BSYSL0
);
3042 sh_eth_tsu_write(mdp
, 0xc, TSU_BSYSL1
);
3043 sh_eth_tsu_write(mdp
, 0, TSU_PRISL0
);
3044 sh_eth_tsu_write(mdp
, 0, TSU_PRISL1
);
3045 sh_eth_tsu_write(mdp
, 0, TSU_FWSL0
);
3046 sh_eth_tsu_write(mdp
, 0, TSU_FWSL1
);
3047 sh_eth_tsu_write(mdp
, TSU_FWSLC_POSTENU
| TSU_FWSLC_POSTENL
, TSU_FWSLC
);
3048 sh_eth_tsu_write(mdp
, 0, TSU_QTAGM0
); /* Disable QTAG(0->1) */
3049 sh_eth_tsu_write(mdp
, 0, TSU_QTAGM1
); /* Disable QTAG(1->0) */
3050 sh_eth_tsu_write(mdp
, 0, TSU_FWSR
); /* all interrupt status clear */
3051 sh_eth_tsu_write(mdp
, 0, TSU_FWINMK
); /* Disable all interrupt */
3052 sh_eth_tsu_write(mdp
, 0, TSU_TEN
); /* Disable all CAM entry */
3053 sh_eth_tsu_write(mdp
, 0, TSU_POST1
); /* Disable CAM entry [ 0- 7] */
3054 sh_eth_tsu_write(mdp
, 0, TSU_POST2
); /* Disable CAM entry [ 8-15] */
3055 sh_eth_tsu_write(mdp
, 0, TSU_POST3
); /* Disable CAM entry [16-23] */
3056 sh_eth_tsu_write(mdp
, 0, TSU_POST4
); /* Disable CAM entry [24-31] */
3059 /* MDIO bus release function */
3060 static int sh_mdio_release(struct sh_eth_private
*mdp
)
3062 /* unregister mdio bus */
3063 mdiobus_unregister(mdp
->mii_bus
);
3065 /* free bitbang info */
3066 free_mdio_bitbang(mdp
->mii_bus
);
3071 /* MDIO bus init function */
3072 static int sh_mdio_init(struct sh_eth_private
*mdp
,
3073 struct sh_eth_plat_data
*pd
)
3076 struct bb_info
*bitbang
;
3077 struct platform_device
*pdev
= mdp
->pdev
;
3078 struct device
*dev
= &mdp
->pdev
->dev
;
3080 /* create bit control struct for PHY */
3081 bitbang
= devm_kzalloc(dev
, sizeof(struct bb_info
), GFP_KERNEL
);
3086 bitbang
->addr
= mdp
->addr
+ mdp
->reg_offset
[PIR
];
3087 bitbang
->set_gate
= pd
->set_mdio_gate
;
3088 bitbang
->ctrl
.ops
= &bb_ops
;
3090 /* MII controller setting */
3091 mdp
->mii_bus
= alloc_mdio_bitbang(&bitbang
->ctrl
);
3095 /* Hook up MII support for ethtool */
3096 mdp
->mii_bus
->name
= "sh_mii";
3097 mdp
->mii_bus
->parent
= dev
;
3098 snprintf(mdp
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%s-%x",
3099 pdev
->name
, pdev
->id
);
3101 /* register MDIO bus */
3102 if (pd
->phy_irq
> 0)
3103 mdp
->mii_bus
->irq
[pd
->phy
] = pd
->phy_irq
;
3105 ret
= of_mdiobus_register(mdp
->mii_bus
, dev
->of_node
);
3112 free_mdio_bitbang(mdp
->mii_bus
);
3116 static const u16
*sh_eth_get_register_offset(int register_type
)
3118 const u16
*reg_offset
= NULL
;
3120 switch (register_type
) {
3121 case SH_ETH_REG_GIGABIT
:
3122 reg_offset
= sh_eth_offset_gigabit
;
3124 case SH_ETH_REG_FAST_RZ
:
3125 reg_offset
= sh_eth_offset_fast_rz
;
3127 case SH_ETH_REG_FAST_RCAR
:
3128 reg_offset
= sh_eth_offset_fast_rcar
;
3130 case SH_ETH_REG_FAST_SH4
:
3131 reg_offset
= sh_eth_offset_fast_sh4
;
3133 case SH_ETH_REG_FAST_SH3_SH2
:
3134 reg_offset
= sh_eth_offset_fast_sh3_sh2
;
3141 static const struct net_device_ops sh_eth_netdev_ops
= {
3142 .ndo_open
= sh_eth_open
,
3143 .ndo_stop
= sh_eth_close
,
3144 .ndo_start_xmit
= sh_eth_start_xmit
,
3145 .ndo_get_stats
= sh_eth_get_stats
,
3146 .ndo_set_rx_mode
= sh_eth_set_rx_mode
,
3147 .ndo_tx_timeout
= sh_eth_tx_timeout
,
3148 .ndo_do_ioctl
= phy_do_ioctl_running
,
3149 .ndo_change_mtu
= sh_eth_change_mtu
,
3150 .ndo_validate_addr
= eth_validate_addr
,
3151 .ndo_set_mac_address
= eth_mac_addr
,
3152 .ndo_set_features
= sh_eth_set_features
,
3155 static const struct net_device_ops sh_eth_netdev_ops_tsu
= {
3156 .ndo_open
= sh_eth_open
,
3157 .ndo_stop
= sh_eth_close
,
3158 .ndo_start_xmit
= sh_eth_start_xmit
,
3159 .ndo_get_stats
= sh_eth_get_stats
,
3160 .ndo_set_rx_mode
= sh_eth_set_rx_mode
,
3161 .ndo_vlan_rx_add_vid
= sh_eth_vlan_rx_add_vid
,
3162 .ndo_vlan_rx_kill_vid
= sh_eth_vlan_rx_kill_vid
,
3163 .ndo_tx_timeout
= sh_eth_tx_timeout
,
3164 .ndo_do_ioctl
= phy_do_ioctl_running
,
3165 .ndo_change_mtu
= sh_eth_change_mtu
,
3166 .ndo_validate_addr
= eth_validate_addr
,
3167 .ndo_set_mac_address
= eth_mac_addr
,
3168 .ndo_set_features
= sh_eth_set_features
,
3172 static struct sh_eth_plat_data
*sh_eth_parse_dt(struct device
*dev
)
3174 struct device_node
*np
= dev
->of_node
;
3175 struct sh_eth_plat_data
*pdata
;
3176 phy_interface_t interface
;
3177 const char *mac_addr
;
3180 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
3184 ret
= of_get_phy_mode(np
, &interface
);
3187 pdata
->phy_interface
= interface
;
3189 mac_addr
= of_get_mac_address(np
);
3190 if (!IS_ERR(mac_addr
))
3191 ether_addr_copy(pdata
->mac_addr
, mac_addr
);
3193 pdata
->no_ether_link
=
3194 of_property_read_bool(np
, "renesas,no-ether-link");
3195 pdata
->ether_link_active_low
=
3196 of_property_read_bool(np
, "renesas,ether-link-active-low");
3201 static const struct of_device_id sh_eth_match_table
[] = {
3202 { .compatible
= "renesas,gether-r8a7740", .data
= &r8a7740_data
},
3203 { .compatible
= "renesas,ether-r8a7743", .data
= &rcar_gen2_data
},
3204 { .compatible
= "renesas,ether-r8a7745", .data
= &rcar_gen2_data
},
3205 { .compatible
= "renesas,ether-r8a7778", .data
= &rcar_gen1_data
},
3206 { .compatible
= "renesas,ether-r8a7779", .data
= &rcar_gen1_data
},
3207 { .compatible
= "renesas,ether-r8a7790", .data
= &rcar_gen2_data
},
3208 { .compatible
= "renesas,ether-r8a7791", .data
= &rcar_gen2_data
},
3209 { .compatible
= "renesas,ether-r8a7793", .data
= &rcar_gen2_data
},
3210 { .compatible
= "renesas,ether-r8a7794", .data
= &rcar_gen2_data
},
3211 { .compatible
= "renesas,gether-r8a77980", .data
= &r8a77980_data
},
3212 { .compatible
= "renesas,ether-r7s72100", .data
= &r7s72100_data
},
3213 { .compatible
= "renesas,ether-r7s9210", .data
= &r7s9210_data
},
3214 { .compatible
= "renesas,rcar-gen1-ether", .data
= &rcar_gen1_data
},
3215 { .compatible
= "renesas,rcar-gen2-ether", .data
= &rcar_gen2_data
},
3218 MODULE_DEVICE_TABLE(of
, sh_eth_match_table
);
3220 static inline struct sh_eth_plat_data
*sh_eth_parse_dt(struct device
*dev
)
3226 static int sh_eth_drv_probe(struct platform_device
*pdev
)
3228 struct resource
*res
;
3229 struct sh_eth_plat_data
*pd
= dev_get_platdata(&pdev
->dev
);
3230 const struct platform_device_id
*id
= platform_get_device_id(pdev
);
3231 struct sh_eth_private
*mdp
;
3232 struct net_device
*ndev
;
3236 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
3238 ndev
= alloc_etherdev(sizeof(struct sh_eth_private
));
3242 pm_runtime_enable(&pdev
->dev
);
3243 pm_runtime_get_sync(&pdev
->dev
);
3245 ret
= platform_get_irq(pdev
, 0);
3250 SET_NETDEV_DEV(ndev
, &pdev
->dev
);
3252 mdp
= netdev_priv(ndev
);
3253 mdp
->num_tx_ring
= TX_RING_SIZE
;
3254 mdp
->num_rx_ring
= RX_RING_SIZE
;
3255 mdp
->addr
= devm_ioremap_resource(&pdev
->dev
, res
);
3256 if (IS_ERR(mdp
->addr
)) {
3257 ret
= PTR_ERR(mdp
->addr
);
3261 ndev
->base_addr
= res
->start
;
3263 spin_lock_init(&mdp
->lock
);
3266 if (pdev
->dev
.of_node
)
3267 pd
= sh_eth_parse_dt(&pdev
->dev
);
3269 dev_err(&pdev
->dev
, "no platform data\n");
3275 mdp
->phy_id
= pd
->phy
;
3276 mdp
->phy_interface
= pd
->phy_interface
;
3277 mdp
->no_ether_link
= pd
->no_ether_link
;
3278 mdp
->ether_link_active_low
= pd
->ether_link_active_low
;
3282 mdp
->cd
= (struct sh_eth_cpu_data
*)id
->driver_data
;
3284 mdp
->cd
= (struct sh_eth_cpu_data
*)of_device_get_match_data(&pdev
->dev
);
3286 mdp
->reg_offset
= sh_eth_get_register_offset(mdp
->cd
->register_type
);
3287 if (!mdp
->reg_offset
) {
3288 dev_err(&pdev
->dev
, "Unknown register type (%d)\n",
3289 mdp
->cd
->register_type
);
3293 sh_eth_set_default_cpu_data(mdp
->cd
);
3295 /* User's manual states max MTU should be 2048 but due to the
3296 * alignment calculations in sh_eth_ring_init() the practical
3297 * MTU is a bit less. Maybe this can be optimized some more.
3299 ndev
->max_mtu
= 2000 - (ETH_HLEN
+ VLAN_HLEN
+ ETH_FCS_LEN
);
3300 ndev
->min_mtu
= ETH_MIN_MTU
;
3302 if (mdp
->cd
->rx_csum
) {
3303 ndev
->features
= NETIF_F_RXCSUM
;
3304 ndev
->hw_features
= NETIF_F_RXCSUM
;
3309 ndev
->netdev_ops
= &sh_eth_netdev_ops_tsu
;
3311 ndev
->netdev_ops
= &sh_eth_netdev_ops
;
3312 ndev
->ethtool_ops
= &sh_eth_ethtool_ops
;
3313 ndev
->watchdog_timeo
= TX_TIMEOUT
;
3315 /* debug message level */
3316 mdp
->msg_enable
= SH_ETH_DEF_MSG_ENABLE
;
3318 /* read and set MAC address */
3319 read_mac_address(ndev
, pd
->mac_addr
);
3320 if (!is_valid_ether_addr(ndev
->dev_addr
)) {
3321 dev_warn(&pdev
->dev
,
3322 "no valid MAC address supplied, using a random one.\n");
3323 eth_hw_addr_random(ndev
);
3327 int port
= pdev
->id
< 0 ? 0 : pdev
->id
% 2;
3328 struct resource
*rtsu
;
3330 rtsu
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
3332 dev_err(&pdev
->dev
, "no TSU resource\n");
3336 /* We can only request the TSU region for the first port
3337 * of the two sharing this TSU for the probe to succeed...
3340 !devm_request_mem_region(&pdev
->dev
, rtsu
->start
,
3341 resource_size(rtsu
),
3342 dev_name(&pdev
->dev
))) {
3343 dev_err(&pdev
->dev
, "can't request TSU resource.\n");
3347 /* ioremap the TSU registers */
3348 mdp
->tsu_addr
= devm_ioremap(&pdev
->dev
, rtsu
->start
,
3349 resource_size(rtsu
));
3350 if (!mdp
->tsu_addr
) {
3351 dev_err(&pdev
->dev
, "TSU region ioremap() failed.\n");
3356 ndev
->features
|= NETIF_F_HW_VLAN_CTAG_FILTER
;
3358 /* Need to init only the first port of the two sharing a TSU */
3360 if (mdp
->cd
->chip_reset
)
3361 mdp
->cd
->chip_reset(ndev
);
3363 /* TSU init (Init only)*/
3364 sh_eth_tsu_init(mdp
);
3368 if (mdp
->cd
->rmiimode
)
3369 sh_eth_write(ndev
, 0x1, RMIIMODE
);
3372 ret
= sh_mdio_init(mdp
, pd
);
3374 if (ret
!= -EPROBE_DEFER
)
3375 dev_err(&pdev
->dev
, "MDIO init failed: %d\n", ret
);
3379 netif_napi_add(ndev
, &mdp
->napi
, sh_eth_poll
, 64);
3381 /* network device register */
3382 ret
= register_netdev(ndev
);
3387 device_set_wakeup_capable(&pdev
->dev
, 1);
3389 /* print device information */
3390 netdev_info(ndev
, "Base address at 0x%x, %pM, IRQ %d.\n",
3391 (u32
)ndev
->base_addr
, ndev
->dev_addr
, ndev
->irq
);
3393 pm_runtime_put(&pdev
->dev
);
3394 platform_set_drvdata(pdev
, ndev
);
3399 netif_napi_del(&mdp
->napi
);
3400 sh_mdio_release(mdp
);
3406 pm_runtime_put(&pdev
->dev
);
3407 pm_runtime_disable(&pdev
->dev
);
3411 static int sh_eth_drv_remove(struct platform_device
*pdev
)
3413 struct net_device
*ndev
= platform_get_drvdata(pdev
);
3414 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
3416 unregister_netdev(ndev
);
3417 netif_napi_del(&mdp
->napi
);
3418 sh_mdio_release(mdp
);
3419 pm_runtime_disable(&pdev
->dev
);
3426 #ifdef CONFIG_PM_SLEEP
3427 static int sh_eth_wol_setup(struct net_device
*ndev
)
3429 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
3431 /* Only allow ECI interrupts */
3432 synchronize_irq(ndev
->irq
);
3433 napi_disable(&mdp
->napi
);
3434 sh_eth_write(ndev
, EESIPR_ECIIP
, EESIPR
);
3436 /* Enable MagicPacket */
3437 sh_eth_modify(ndev
, ECMR
, ECMR_MPDE
, ECMR_MPDE
);
3439 return enable_irq_wake(ndev
->irq
);
3442 static int sh_eth_wol_restore(struct net_device
*ndev
)
3444 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
3447 napi_enable(&mdp
->napi
);
3449 /* Disable MagicPacket */
3450 sh_eth_modify(ndev
, ECMR
, ECMR_MPDE
, 0);
3452 /* The device needs to be reset to restore MagicPacket logic
3453 * for next wakeup. If we close and open the device it will
3454 * both be reset and all registers restored. This is what
3455 * happens during suspend and resume without WoL enabled.
3457 ret
= sh_eth_close(ndev
);
3460 ret
= sh_eth_open(ndev
);
3464 return disable_irq_wake(ndev
->irq
);
3467 static int sh_eth_suspend(struct device
*dev
)
3469 struct net_device
*ndev
= dev_get_drvdata(dev
);
3470 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
3473 if (!netif_running(ndev
))
3476 netif_device_detach(ndev
);
3478 if (mdp
->wol_enabled
)
3479 ret
= sh_eth_wol_setup(ndev
);
3481 ret
= sh_eth_close(ndev
);
3486 static int sh_eth_resume(struct device
*dev
)
3488 struct net_device
*ndev
= dev_get_drvdata(dev
);
3489 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
3492 if (!netif_running(ndev
))
3495 if (mdp
->wol_enabled
)
3496 ret
= sh_eth_wol_restore(ndev
);
3498 ret
= sh_eth_open(ndev
);
3503 netif_device_attach(ndev
);
3509 static int sh_eth_runtime_nop(struct device
*dev
)
3511 /* Runtime PM callback shared between ->runtime_suspend()
3512 * and ->runtime_resume(). Simply returns success.
3514 * This driver re-initializes all registers after
3515 * pm_runtime_get_sync() anyway so there is no need
3516 * to save and restore registers here.
3521 static const struct dev_pm_ops sh_eth_dev_pm_ops
= {
3522 SET_SYSTEM_SLEEP_PM_OPS(sh_eth_suspend
, sh_eth_resume
)
3523 SET_RUNTIME_PM_OPS(sh_eth_runtime_nop
, sh_eth_runtime_nop
, NULL
)
3525 #define SH_ETH_PM_OPS (&sh_eth_dev_pm_ops)
3527 #define SH_ETH_PM_OPS NULL
3530 static const struct platform_device_id sh_eth_id_table
[] = {
3531 { "sh7619-ether", (kernel_ulong_t
)&sh7619_data
},
3532 { "sh771x-ether", (kernel_ulong_t
)&sh771x_data
},
3533 { "sh7724-ether", (kernel_ulong_t
)&sh7724_data
},
3534 { "sh7734-gether", (kernel_ulong_t
)&sh7734_data
},
3535 { "sh7757-ether", (kernel_ulong_t
)&sh7757_data
},
3536 { "sh7757-gether", (kernel_ulong_t
)&sh7757_data_giga
},
3537 { "sh7763-gether", (kernel_ulong_t
)&sh7763_data
},
3540 MODULE_DEVICE_TABLE(platform
, sh_eth_id_table
);
3542 static struct platform_driver sh_eth_driver
= {
3543 .probe
= sh_eth_drv_probe
,
3544 .remove
= sh_eth_drv_remove
,
3545 .id_table
= sh_eth_id_table
,
3548 .pm
= SH_ETH_PM_OPS
,
3549 .of_match_table
= of_match_ptr(sh_eth_match_table
),
3553 module_platform_driver(sh_eth_driver
);
3555 MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
3556 MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
3557 MODULE_LICENSE("GPL v2");