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_rcar
[SH_ETH_MAX_REGISTER_OFFSET
] = {
146 SH_ETH_OFFSET_DEFAULTS
,
193 static const u16 sh_eth_offset_fast_sh4
[SH_ETH_MAX_REGISTER_OFFSET
] = {
194 SH_ETH_OFFSET_DEFAULTS
,
247 static const u16 sh_eth_offset_fast_sh3_sh2
[SH_ETH_MAX_REGISTER_OFFSET
] = {
248 SH_ETH_OFFSET_DEFAULTS
,
296 [TSU_CTRST
] = 0x0004,
297 [TSU_FWEN0
] = 0x0010,
298 [TSU_FWEN1
] = 0x0014,
300 [TSU_BSYSL0
] = 0x0020,
301 [TSU_BSYSL1
] = 0x0024,
302 [TSU_PRISL0
] = 0x0028,
303 [TSU_PRISL1
] = 0x002c,
304 [TSU_FWSL0
] = 0x0030,
305 [TSU_FWSL1
] = 0x0034,
306 [TSU_FWSLC
] = 0x0038,
307 [TSU_QTAGM0
] = 0x0040,
308 [TSU_QTAGM1
] = 0x0044,
309 [TSU_ADQT0
] = 0x0048,
310 [TSU_ADQT1
] = 0x004c,
312 [TSU_FWINMK
] = 0x0054,
313 [TSU_ADSBSY
] = 0x0060,
315 [TSU_POST1
] = 0x0070,
316 [TSU_POST2
] = 0x0074,
317 [TSU_POST3
] = 0x0078,
318 [TSU_POST4
] = 0x007c,
333 [TSU_ADRH0
] = 0x0100,
336 static void sh_eth_rcv_snd_disable(struct net_device
*ndev
);
337 static struct net_device_stats
*sh_eth_get_stats(struct net_device
*ndev
);
339 static void sh_eth_write(struct net_device
*ndev
, u32 data
, int enum_index
)
341 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
342 u16 offset
= mdp
->reg_offset
[enum_index
];
344 if (WARN_ON(offset
== SH_ETH_OFFSET_INVALID
))
347 iowrite32(data
, mdp
->addr
+ offset
);
350 static u32
sh_eth_read(struct net_device
*ndev
, int enum_index
)
352 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
353 u16 offset
= mdp
->reg_offset
[enum_index
];
355 if (WARN_ON(offset
== SH_ETH_OFFSET_INVALID
))
358 return ioread32(mdp
->addr
+ offset
);
361 static void sh_eth_modify(struct net_device
*ndev
, int enum_index
, u32 clear
,
364 sh_eth_write(ndev
, (sh_eth_read(ndev
, enum_index
) & ~clear
) | set
,
368 static u16
sh_eth_tsu_get_offset(struct sh_eth_private
*mdp
, int enum_index
)
370 return mdp
->reg_offset
[enum_index
];
373 static void sh_eth_tsu_write(struct sh_eth_private
*mdp
, u32 data
,
376 u16 offset
= sh_eth_tsu_get_offset(mdp
, enum_index
);
378 if (WARN_ON(offset
== SH_ETH_OFFSET_INVALID
))
381 iowrite32(data
, mdp
->tsu_addr
+ offset
);
384 static u32
sh_eth_tsu_read(struct sh_eth_private
*mdp
, int enum_index
)
386 u16 offset
= sh_eth_tsu_get_offset(mdp
, enum_index
);
388 if (WARN_ON(offset
== SH_ETH_OFFSET_INVALID
))
391 return ioread32(mdp
->tsu_addr
+ offset
);
394 static void sh_eth_soft_swap(char *src
, int len
)
396 #ifdef __LITTLE_ENDIAN
398 u32
*maxp
= p
+ DIV_ROUND_UP(len
, sizeof(u32
));
400 for (; p
< maxp
; p
++)
405 static void sh_eth_select_mii(struct net_device
*ndev
)
407 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
410 switch (mdp
->phy_interface
) {
411 case PHY_INTERFACE_MODE_RGMII
... PHY_INTERFACE_MODE_RGMII_TXID
:
414 case PHY_INTERFACE_MODE_GMII
:
417 case PHY_INTERFACE_MODE_MII
:
420 case PHY_INTERFACE_MODE_RMII
:
425 "PHY interface mode was not setup. Set to MII.\n");
430 sh_eth_write(ndev
, value
, RMII_MII
);
433 static void sh_eth_set_duplex(struct net_device
*ndev
)
435 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
437 sh_eth_modify(ndev
, ECMR
, ECMR_DM
, mdp
->duplex
? ECMR_DM
: 0);
440 static void sh_eth_chip_reset(struct net_device
*ndev
)
442 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
445 sh_eth_tsu_write(mdp
, ARSTR_ARST
, ARSTR
);
449 static int sh_eth_soft_reset(struct net_device
*ndev
)
451 sh_eth_modify(ndev
, EDMR
, EDMR_SRST_ETHER
, EDMR_SRST_ETHER
);
453 sh_eth_modify(ndev
, EDMR
, EDMR_SRST_ETHER
, 0);
458 static int sh_eth_check_soft_reset(struct net_device
*ndev
)
462 for (cnt
= 100; cnt
> 0; cnt
--) {
463 if (!(sh_eth_read(ndev
, EDMR
) & EDMR_SRST_GETHER
))
468 netdev_err(ndev
, "Device reset failed\n");
472 static int sh_eth_soft_reset_gether(struct net_device
*ndev
)
474 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
477 sh_eth_write(ndev
, EDSR_ENALL
, EDSR
);
478 sh_eth_modify(ndev
, EDMR
, EDMR_SRST_GETHER
, EDMR_SRST_GETHER
);
480 ret
= sh_eth_check_soft_reset(ndev
);
485 sh_eth_write(ndev
, 0, TDLAR
);
486 sh_eth_write(ndev
, 0, TDFAR
);
487 sh_eth_write(ndev
, 0, TDFXR
);
488 sh_eth_write(ndev
, 0, TDFFR
);
489 sh_eth_write(ndev
, 0, RDLAR
);
490 sh_eth_write(ndev
, 0, RDFAR
);
491 sh_eth_write(ndev
, 0, RDFXR
);
492 sh_eth_write(ndev
, 0, RDFFR
);
494 /* Reset HW CRC register */
496 sh_eth_write(ndev
, 0, CSMR
);
498 /* Select MII mode */
499 if (mdp
->cd
->select_mii
)
500 sh_eth_select_mii(ndev
);
505 static void sh_eth_set_rate_gether(struct net_device
*ndev
)
507 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
509 if (WARN_ON(!mdp
->cd
->gecmr
))
512 switch (mdp
->speed
) {
513 case 10: /* 10BASE */
514 sh_eth_write(ndev
, GECMR_10
, GECMR
);
516 case 100:/* 100BASE */
517 sh_eth_write(ndev
, GECMR_100
, GECMR
);
519 case 1000: /* 1000BASE */
520 sh_eth_write(ndev
, GECMR_1000
, GECMR
);
527 static struct sh_eth_cpu_data r7s72100_data
= {
528 .soft_reset
= sh_eth_soft_reset_gether
,
530 .chip_reset
= sh_eth_chip_reset
,
531 .set_duplex
= sh_eth_set_duplex
,
533 .register_type
= SH_ETH_REG_GIGABIT
,
535 .edtrr_trns
= EDTRR_TRNS_GETHER
,
536 .ecsr_value
= ECSR_ICD
,
537 .ecsipr_value
= ECSIPR_ICDIP
,
538 .eesipr_value
= EESIPR_TWB1IP
| EESIPR_TWBIP
| EESIPR_TC1IP
|
539 EESIPR_TABTIP
| EESIPR_RABTIP
| EESIPR_RFCOFIP
|
541 EESIPR_FTCIP
| EESIPR_TDEIP
| EESIPR_TFUFIP
|
542 EESIPR_FRIP
| EESIPR_RDEIP
| EESIPR_RFOFIP
|
543 EESIPR_RMAFIP
| EESIPR_RRFIP
|
544 EESIPR_RTLFIP
| EESIPR_RTSFIP
|
545 EESIPR_PREIP
| EESIPR_CERFIP
,
547 .tx_check
= EESR_TC1
| EESR_FTC
,
548 .eesr_err_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_RABT
|
549 EESR_RFE
| EESR_RDE
| EESR_RFRMER
| EESR_TFE
|
551 .fdr_value
= 0x0000070f,
568 static void sh_eth_chip_reset_r8a7740(struct net_device
*ndev
)
570 sh_eth_chip_reset(ndev
);
572 sh_eth_select_mii(ndev
);
576 static struct sh_eth_cpu_data r8a7740_data
= {
577 .soft_reset
= sh_eth_soft_reset_gether
,
579 .chip_reset
= sh_eth_chip_reset_r8a7740
,
580 .set_duplex
= sh_eth_set_duplex
,
581 .set_rate
= sh_eth_set_rate_gether
,
583 .register_type
= SH_ETH_REG_GIGABIT
,
585 .edtrr_trns
= EDTRR_TRNS_GETHER
,
586 .ecsr_value
= ECSR_ICD
| ECSR_MPD
,
587 .ecsipr_value
= ECSIPR_LCHNGIP
| ECSIPR_ICDIP
| ECSIPR_MPDIP
,
588 .eesipr_value
= EESIPR_RFCOFIP
| EESIPR_ECIIP
|
589 EESIPR_FTCIP
| EESIPR_TDEIP
| EESIPR_TFUFIP
|
590 EESIPR_FRIP
| EESIPR_RDEIP
| EESIPR_RFOFIP
|
591 0x0000f000 | EESIPR_CNDIP
| EESIPR_DLCIP
|
592 EESIPR_CDIP
| EESIPR_TROIP
| EESIPR_RMAFIP
|
593 EESIPR_CEEFIP
| EESIPR_CELFIP
|
594 EESIPR_RRFIP
| EESIPR_RTLFIP
| EESIPR_RTSFIP
|
595 EESIPR_PREIP
| EESIPR_CERFIP
,
597 .tx_check
= EESR_TC1
| EESR_FTC
,
598 .eesr_err_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_RABT
|
599 EESR_RFE
| EESR_RDE
| EESR_RFRMER
| EESR_TFE
|
601 .fdr_value
= 0x0000070f,
621 /* There is CPU dependent code */
622 static void sh_eth_set_rate_rcar(struct net_device
*ndev
)
624 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
626 switch (mdp
->speed
) {
627 case 10: /* 10BASE */
628 sh_eth_modify(ndev
, ECMR
, ECMR_ELB
, 0);
630 case 100:/* 100BASE */
631 sh_eth_modify(ndev
, ECMR
, ECMR_ELB
, ECMR_ELB
);
637 static struct sh_eth_cpu_data rcar_gen1_data
= {
638 .soft_reset
= sh_eth_soft_reset
,
640 .set_duplex
= sh_eth_set_duplex
,
641 .set_rate
= sh_eth_set_rate_rcar
,
643 .register_type
= SH_ETH_REG_FAST_RCAR
,
645 .edtrr_trns
= EDTRR_TRNS_ETHER
,
646 .ecsr_value
= ECSR_PSRTO
| ECSR_LCHNG
| ECSR_ICD
,
647 .ecsipr_value
= ECSIPR_PSRTOIP
| ECSIPR_LCHNGIP
| ECSIPR_ICDIP
,
648 .eesipr_value
= EESIPR_RFCOFIP
| EESIPR_ADEIP
| EESIPR_ECIIP
|
649 EESIPR_FTCIP
| EESIPR_TDEIP
| EESIPR_TFUFIP
|
650 EESIPR_FRIP
| EESIPR_RDEIP
| EESIPR_RFOFIP
|
651 EESIPR_RMAFIP
| EESIPR_RRFIP
|
652 EESIPR_RTLFIP
| EESIPR_RTSFIP
|
653 EESIPR_PREIP
| EESIPR_CERFIP
,
655 .tx_check
= EESR_FTC
| EESR_CND
| EESR_DLC
| EESR_CD
| EESR_TRO
,
656 .eesr_err_check
= EESR_TWB
| EESR_TABT
| EESR_RABT
| EESR_RFE
|
657 EESR_RDE
| EESR_RFRMER
| EESR_TFE
| EESR_TDE
,
658 .fdr_value
= 0x00000f0f,
667 /* R-Car Gen2 and RZ/G1 */
668 static struct sh_eth_cpu_data rcar_gen2_data
= {
669 .soft_reset
= sh_eth_soft_reset
,
671 .set_duplex
= sh_eth_set_duplex
,
672 .set_rate
= sh_eth_set_rate_rcar
,
674 .register_type
= SH_ETH_REG_FAST_RCAR
,
676 .edtrr_trns
= EDTRR_TRNS_ETHER
,
677 .ecsr_value
= ECSR_PSRTO
| ECSR_LCHNG
| ECSR_ICD
| ECSR_MPD
,
678 .ecsipr_value
= ECSIPR_PSRTOIP
| ECSIPR_LCHNGIP
| ECSIPR_ICDIP
|
680 .eesipr_value
= EESIPR_RFCOFIP
| EESIPR_ADEIP
| EESIPR_ECIIP
|
681 EESIPR_FTCIP
| EESIPR_TDEIP
| EESIPR_TFUFIP
|
682 EESIPR_FRIP
| EESIPR_RDEIP
| EESIPR_RFOFIP
|
683 EESIPR_RMAFIP
| EESIPR_RRFIP
|
684 EESIPR_RTLFIP
| EESIPR_RTSFIP
|
685 EESIPR_PREIP
| EESIPR_CERFIP
,
687 .tx_check
= EESR_FTC
| EESR_CND
| EESR_DLC
| EESR_CD
| EESR_TRO
,
688 .eesr_err_check
= EESR_TWB
| EESR_TABT
| EESR_RABT
| EESR_RFE
|
689 EESR_RDE
| EESR_RFRMER
| EESR_TFE
| EESR_TDE
,
690 .fdr_value
= 0x00000f0f,
692 .trscer_err_mask
= DESC_I_RINT8
,
704 static struct sh_eth_cpu_data r8a77980_data
= {
705 .soft_reset
= sh_eth_soft_reset_gether
,
707 .set_duplex
= sh_eth_set_duplex
,
708 .set_rate
= sh_eth_set_rate_gether
,
710 .register_type
= SH_ETH_REG_GIGABIT
,
712 .edtrr_trns
= EDTRR_TRNS_GETHER
,
713 .ecsr_value
= ECSR_PSRTO
| ECSR_LCHNG
| ECSR_ICD
| ECSR_MPD
,
714 .ecsipr_value
= ECSIPR_PSRTOIP
| ECSIPR_LCHNGIP
| ECSIPR_ICDIP
|
716 .eesipr_value
= EESIPR_RFCOFIP
| EESIPR_ECIIP
|
717 EESIPR_FTCIP
| EESIPR_TDEIP
| EESIPR_TFUFIP
|
718 EESIPR_FRIP
| EESIPR_RDEIP
| EESIPR_RFOFIP
|
719 EESIPR_RMAFIP
| EESIPR_RRFIP
|
720 EESIPR_RTLFIP
| EESIPR_RTSFIP
|
721 EESIPR_PREIP
| EESIPR_CERFIP
,
723 .tx_check
= EESR_FTC
| EESR_CD
| EESR_TRO
,
724 .eesr_err_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_RABT
|
725 EESR_RFE
| EESR_RDE
| EESR_RFRMER
|
726 EESR_TFE
| EESR_TDE
| EESR_ECI
,
727 .fdr_value
= 0x0000070f,
748 static struct sh_eth_cpu_data r7s9210_data
= {
749 .soft_reset
= sh_eth_soft_reset
,
751 .set_duplex
= sh_eth_set_duplex
,
752 .set_rate
= sh_eth_set_rate_rcar
,
754 .register_type
= SH_ETH_REG_FAST_SH4
,
756 .edtrr_trns
= EDTRR_TRNS_ETHER
,
757 .ecsr_value
= ECSR_ICD
,
758 .ecsipr_value
= ECSIPR_ICDIP
,
759 .eesipr_value
= EESIPR_TWBIP
| EESIPR_TABTIP
| EESIPR_RABTIP
|
760 EESIPR_RFCOFIP
| EESIPR_ECIIP
| EESIPR_FTCIP
|
761 EESIPR_TDEIP
| EESIPR_TFUFIP
| EESIPR_FRIP
|
762 EESIPR_RDEIP
| EESIPR_RFOFIP
| EESIPR_CNDIP
|
763 EESIPR_DLCIP
| EESIPR_CDIP
| EESIPR_TROIP
|
764 EESIPR_RMAFIP
| EESIPR_RRFIP
| EESIPR_RTLFIP
|
765 EESIPR_RTSFIP
| EESIPR_PREIP
| EESIPR_CERFIP
,
767 .tx_check
= EESR_FTC
| EESR_CND
| EESR_DLC
| EESR_CD
| EESR_TRO
,
768 .eesr_err_check
= EESR_TWB
| EESR_TABT
| EESR_RABT
| EESR_RFE
|
769 EESR_RDE
| EESR_RFRMER
| EESR_TFE
| EESR_TDE
,
771 .fdr_value
= 0x0000070f,
781 #endif /* CONFIG_OF */
783 static void sh_eth_set_rate_sh7724(struct net_device
*ndev
)
785 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
787 switch (mdp
->speed
) {
788 case 10: /* 10BASE */
789 sh_eth_modify(ndev
, ECMR
, ECMR_RTM
, 0);
791 case 100:/* 100BASE */
792 sh_eth_modify(ndev
, ECMR
, ECMR_RTM
, ECMR_RTM
);
798 static struct sh_eth_cpu_data sh7724_data
= {
799 .soft_reset
= sh_eth_soft_reset
,
801 .set_duplex
= sh_eth_set_duplex
,
802 .set_rate
= sh_eth_set_rate_sh7724
,
804 .register_type
= SH_ETH_REG_FAST_SH4
,
806 .edtrr_trns
= EDTRR_TRNS_ETHER
,
807 .ecsr_value
= ECSR_PSRTO
| ECSR_LCHNG
| ECSR_ICD
,
808 .ecsipr_value
= ECSIPR_PSRTOIP
| ECSIPR_LCHNGIP
| ECSIPR_ICDIP
,
809 .eesipr_value
= EESIPR_RFCOFIP
| EESIPR_ADEIP
| EESIPR_ECIIP
|
810 EESIPR_FTCIP
| EESIPR_TDEIP
| EESIPR_TFUFIP
|
811 EESIPR_FRIP
| EESIPR_RDEIP
| EESIPR_RFOFIP
|
812 EESIPR_RMAFIP
| EESIPR_RRFIP
|
813 EESIPR_RTLFIP
| EESIPR_RTSFIP
|
814 EESIPR_PREIP
| EESIPR_CERFIP
,
816 .tx_check
= EESR_FTC
| EESR_CND
| EESR_DLC
| EESR_CD
| EESR_TRO
,
817 .eesr_err_check
= EESR_TWB
| EESR_TABT
| EESR_RABT
| EESR_RFE
|
818 EESR_RDE
| EESR_RFRMER
| EESR_TFE
| EESR_TDE
,
827 static void sh_eth_set_rate_sh7757(struct net_device
*ndev
)
829 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
831 switch (mdp
->speed
) {
832 case 10: /* 10BASE */
833 sh_eth_write(ndev
, 0, RTRATE
);
835 case 100:/* 100BASE */
836 sh_eth_write(ndev
, 1, RTRATE
);
842 static struct sh_eth_cpu_data sh7757_data
= {
843 .soft_reset
= sh_eth_soft_reset
,
845 .set_duplex
= sh_eth_set_duplex
,
846 .set_rate
= sh_eth_set_rate_sh7757
,
848 .register_type
= SH_ETH_REG_FAST_SH4
,
850 .edtrr_trns
= EDTRR_TRNS_ETHER
,
851 .eesipr_value
= EESIPR_RFCOFIP
| EESIPR_ECIIP
|
852 EESIPR_FTCIP
| EESIPR_TDEIP
| EESIPR_TFUFIP
|
853 EESIPR_FRIP
| EESIPR_RDEIP
| EESIPR_RFOFIP
|
854 0x0000f000 | EESIPR_CNDIP
| EESIPR_DLCIP
|
855 EESIPR_CDIP
| EESIPR_TROIP
| EESIPR_RMAFIP
|
856 EESIPR_CEEFIP
| EESIPR_CELFIP
|
857 EESIPR_RRFIP
| EESIPR_RTLFIP
| EESIPR_RTSFIP
|
858 EESIPR_PREIP
| EESIPR_CERFIP
,
860 .tx_check
= EESR_FTC
| EESR_CND
| EESR_DLC
| EESR_CD
| EESR_TRO
,
861 .eesr_err_check
= EESR_TWB
| EESR_TABT
| EESR_RABT
| EESR_RFE
|
862 EESR_RDE
| EESR_RFRMER
| EESR_TFE
| EESR_TDE
,
864 .irq_flags
= IRQF_SHARED
,
875 #define SH_GIGA_ETH_BASE 0xfee00000UL
876 #define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8)
877 #define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0)
878 static void sh_eth_chip_reset_giga(struct net_device
*ndev
)
880 u32 mahr
[2], malr
[2];
883 /* save MAHR and MALR */
884 for (i
= 0; i
< 2; i
++) {
885 malr
[i
] = ioread32((void *)GIGA_MALR(i
));
886 mahr
[i
] = ioread32((void *)GIGA_MAHR(i
));
889 sh_eth_chip_reset(ndev
);
891 /* restore MAHR and MALR */
892 for (i
= 0; i
< 2; i
++) {
893 iowrite32(malr
[i
], (void *)GIGA_MALR(i
));
894 iowrite32(mahr
[i
], (void *)GIGA_MAHR(i
));
898 static void sh_eth_set_rate_giga(struct net_device
*ndev
)
900 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
902 if (WARN_ON(!mdp
->cd
->gecmr
))
905 switch (mdp
->speed
) {
906 case 10: /* 10BASE */
907 sh_eth_write(ndev
, 0x00000000, GECMR
);
909 case 100:/* 100BASE */
910 sh_eth_write(ndev
, 0x00000010, GECMR
);
912 case 1000: /* 1000BASE */
913 sh_eth_write(ndev
, 0x00000020, GECMR
);
918 /* SH7757(GETHERC) */
919 static struct sh_eth_cpu_data sh7757_data_giga
= {
920 .soft_reset
= sh_eth_soft_reset_gether
,
922 .chip_reset
= sh_eth_chip_reset_giga
,
923 .set_duplex
= sh_eth_set_duplex
,
924 .set_rate
= sh_eth_set_rate_giga
,
926 .register_type
= SH_ETH_REG_GIGABIT
,
928 .edtrr_trns
= EDTRR_TRNS_GETHER
,
929 .ecsr_value
= ECSR_ICD
| ECSR_MPD
,
930 .ecsipr_value
= ECSIPR_LCHNGIP
| ECSIPR_ICDIP
| ECSIPR_MPDIP
,
931 .eesipr_value
= EESIPR_RFCOFIP
| EESIPR_ECIIP
|
932 EESIPR_FTCIP
| EESIPR_TDEIP
| EESIPR_TFUFIP
|
933 EESIPR_FRIP
| EESIPR_RDEIP
| EESIPR_RFOFIP
|
934 0x0000f000 | EESIPR_CNDIP
| EESIPR_DLCIP
|
935 EESIPR_CDIP
| EESIPR_TROIP
| EESIPR_RMAFIP
|
936 EESIPR_CEEFIP
| EESIPR_CELFIP
|
937 EESIPR_RRFIP
| EESIPR_RTLFIP
| EESIPR_RTSFIP
|
938 EESIPR_PREIP
| EESIPR_CERFIP
,
940 .tx_check
= EESR_TC1
| EESR_FTC
,
941 .eesr_err_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_RABT
|
942 EESR_RFE
| EESR_RDE
| EESR_RFRMER
| EESR_TFE
|
944 .fdr_value
= 0x0000072f,
946 .irq_flags
= IRQF_SHARED
,
963 static struct sh_eth_cpu_data sh7734_data
= {
964 .soft_reset
= sh_eth_soft_reset_gether
,
966 .chip_reset
= sh_eth_chip_reset
,
967 .set_duplex
= sh_eth_set_duplex
,
968 .set_rate
= sh_eth_set_rate_gether
,
970 .register_type
= SH_ETH_REG_GIGABIT
,
972 .edtrr_trns
= EDTRR_TRNS_GETHER
,
973 .ecsr_value
= ECSR_ICD
| ECSR_MPD
,
974 .ecsipr_value
= ECSIPR_LCHNGIP
| ECSIPR_ICDIP
| ECSIPR_MPDIP
,
975 .eesipr_value
= EESIPR_RFCOFIP
| EESIPR_ECIIP
|
976 EESIPR_FTCIP
| EESIPR_TDEIP
| EESIPR_TFUFIP
|
977 EESIPR_FRIP
| EESIPR_RDEIP
| EESIPR_RFOFIP
|
978 EESIPR_DLCIP
| EESIPR_CDIP
| EESIPR_TROIP
|
979 EESIPR_RMAFIP
| EESIPR_CEEFIP
| EESIPR_CELFIP
|
980 EESIPR_RRFIP
| EESIPR_RTLFIP
| EESIPR_RTSFIP
|
981 EESIPR_PREIP
| EESIPR_CERFIP
,
983 .tx_check
= EESR_TC1
| EESR_FTC
,
984 .eesr_err_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_RABT
|
985 EESR_RFE
| EESR_RDE
| EESR_RFRMER
| EESR_TFE
|
1006 static struct sh_eth_cpu_data sh7763_data
= {
1007 .soft_reset
= sh_eth_soft_reset_gether
,
1009 .chip_reset
= sh_eth_chip_reset
,
1010 .set_duplex
= sh_eth_set_duplex
,
1011 .set_rate
= sh_eth_set_rate_gether
,
1013 .register_type
= SH_ETH_REG_GIGABIT
,
1015 .edtrr_trns
= EDTRR_TRNS_GETHER
,
1016 .ecsr_value
= ECSR_ICD
| ECSR_MPD
,
1017 .ecsipr_value
= ECSIPR_LCHNGIP
| ECSIPR_ICDIP
| ECSIPR_MPDIP
,
1018 .eesipr_value
= EESIPR_RFCOFIP
| EESIPR_ECIIP
|
1019 EESIPR_FTCIP
| EESIPR_TDEIP
| EESIPR_TFUFIP
|
1020 EESIPR_FRIP
| EESIPR_RDEIP
| EESIPR_RFOFIP
|
1021 EESIPR_DLCIP
| EESIPR_CDIP
| EESIPR_TROIP
|
1022 EESIPR_RMAFIP
| EESIPR_CEEFIP
| EESIPR_CELFIP
|
1023 EESIPR_RRFIP
| EESIPR_RTLFIP
| EESIPR_RTSFIP
|
1024 EESIPR_PREIP
| EESIPR_CERFIP
,
1026 .tx_check
= EESR_TC1
| EESR_FTC
,
1027 .eesr_err_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_RABT
|
1028 EESR_RDE
| EESR_RFRMER
| EESR_TFE
| EESR_TDE
,
1040 .irq_flags
= IRQF_SHARED
,
1047 static struct sh_eth_cpu_data sh7619_data
= {
1048 .soft_reset
= sh_eth_soft_reset
,
1050 .register_type
= SH_ETH_REG_FAST_SH3_SH2
,
1052 .edtrr_trns
= EDTRR_TRNS_ETHER
,
1053 .eesipr_value
= EESIPR_RFCOFIP
| EESIPR_ECIIP
|
1054 EESIPR_FTCIP
| EESIPR_TDEIP
| EESIPR_TFUFIP
|
1055 EESIPR_FRIP
| EESIPR_RDEIP
| EESIPR_RFOFIP
|
1056 0x0000f000 | EESIPR_CNDIP
| EESIPR_DLCIP
|
1057 EESIPR_CDIP
| EESIPR_TROIP
| EESIPR_RMAFIP
|
1058 EESIPR_CEEFIP
| EESIPR_CELFIP
|
1059 EESIPR_RRFIP
| EESIPR_RTLFIP
| EESIPR_RTSFIP
|
1060 EESIPR_PREIP
| EESIPR_CERFIP
,
1068 static struct sh_eth_cpu_data sh771x_data
= {
1069 .soft_reset
= sh_eth_soft_reset
,
1071 .register_type
= SH_ETH_REG_FAST_SH3_SH2
,
1073 .edtrr_trns
= EDTRR_TRNS_ETHER
,
1074 .eesipr_value
= EESIPR_RFCOFIP
| EESIPR_ECIIP
|
1075 EESIPR_FTCIP
| EESIPR_TDEIP
| EESIPR_TFUFIP
|
1076 EESIPR_FRIP
| EESIPR_RDEIP
| EESIPR_RFOFIP
|
1077 0x0000f000 | EESIPR_CNDIP
| EESIPR_DLCIP
|
1078 EESIPR_CDIP
| EESIPR_TROIP
| EESIPR_RMAFIP
|
1079 EESIPR_CEEFIP
| EESIPR_CELFIP
|
1080 EESIPR_RRFIP
| EESIPR_RTLFIP
| EESIPR_RTSFIP
|
1081 EESIPR_PREIP
| EESIPR_CERFIP
,
1086 static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data
*cd
)
1088 if (!cd
->ecsr_value
)
1089 cd
->ecsr_value
= DEFAULT_ECSR_INIT
;
1091 if (!cd
->ecsipr_value
)
1092 cd
->ecsipr_value
= DEFAULT_ECSIPR_INIT
;
1094 if (!cd
->fcftr_value
)
1095 cd
->fcftr_value
= DEFAULT_FIFO_F_D_RFF
|
1096 DEFAULT_FIFO_F_D_RFD
;
1099 cd
->fdr_value
= DEFAULT_FDR_INIT
;
1102 cd
->tx_check
= DEFAULT_TX_CHECK
;
1104 if (!cd
->eesr_err_check
)
1105 cd
->eesr_err_check
= DEFAULT_EESR_ERR_CHECK
;
1107 if (!cd
->trscer_err_mask
)
1108 cd
->trscer_err_mask
= DEFAULT_TRSCER_ERR_MASK
;
1111 static void sh_eth_set_receive_align(struct sk_buff
*skb
)
1113 uintptr_t reserve
= (uintptr_t)skb
->data
& (SH_ETH_RX_ALIGN
- 1);
1116 skb_reserve(skb
, SH_ETH_RX_ALIGN
- reserve
);
1119 /* Program the hardware MAC address from dev->dev_addr. */
1120 static void update_mac_address(struct net_device
*ndev
)
1123 (ndev
->dev_addr
[0] << 24) | (ndev
->dev_addr
[1] << 16) |
1124 (ndev
->dev_addr
[2] << 8) | (ndev
->dev_addr
[3]), MAHR
);
1126 (ndev
->dev_addr
[4] << 8) | (ndev
->dev_addr
[5]), MALR
);
1129 /* Get MAC address from SuperH MAC address register
1131 * SuperH's Ethernet device doesn't have 'ROM' to MAC address.
1132 * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g).
1133 * When you want use this device, you must set MAC address in bootloader.
1136 static void read_mac_address(struct net_device
*ndev
, unsigned char *mac
)
1138 if (mac
[0] || mac
[1] || mac
[2] || mac
[3] || mac
[4] || mac
[5]) {
1139 memcpy(ndev
->dev_addr
, mac
, ETH_ALEN
);
1141 u32 mahr
= sh_eth_read(ndev
, MAHR
);
1142 u32 malr
= sh_eth_read(ndev
, MALR
);
1144 ndev
->dev_addr
[0] = (mahr
>> 24) & 0xFF;
1145 ndev
->dev_addr
[1] = (mahr
>> 16) & 0xFF;
1146 ndev
->dev_addr
[2] = (mahr
>> 8) & 0xFF;
1147 ndev
->dev_addr
[3] = (mahr
>> 0) & 0xFF;
1148 ndev
->dev_addr
[4] = (malr
>> 8) & 0xFF;
1149 ndev
->dev_addr
[5] = (malr
>> 0) & 0xFF;
1154 void (*set_gate
)(void *addr
);
1155 struct mdiobb_ctrl ctrl
;
1159 static void sh_mdio_ctrl(struct mdiobb_ctrl
*ctrl
, u32 mask
, int set
)
1161 struct bb_info
*bitbang
= container_of(ctrl
, struct bb_info
, ctrl
);
1164 if (bitbang
->set_gate
)
1165 bitbang
->set_gate(bitbang
->addr
);
1167 pir
= ioread32(bitbang
->addr
);
1172 iowrite32(pir
, bitbang
->addr
);
1175 /* Data I/O pin control */
1176 static void sh_mmd_ctrl(struct mdiobb_ctrl
*ctrl
, int bit
)
1178 sh_mdio_ctrl(ctrl
, PIR_MMD
, bit
);
1182 static void sh_set_mdio(struct mdiobb_ctrl
*ctrl
, int bit
)
1184 sh_mdio_ctrl(ctrl
, PIR_MDO
, bit
);
1188 static int sh_get_mdio(struct mdiobb_ctrl
*ctrl
)
1190 struct bb_info
*bitbang
= container_of(ctrl
, struct bb_info
, ctrl
);
1192 if (bitbang
->set_gate
)
1193 bitbang
->set_gate(bitbang
->addr
);
1195 return (ioread32(bitbang
->addr
) & PIR_MDI
) != 0;
1198 /* MDC pin control */
1199 static void sh_mdc_ctrl(struct mdiobb_ctrl
*ctrl
, int bit
)
1201 sh_mdio_ctrl(ctrl
, PIR_MDC
, bit
);
1204 /* mdio bus control struct */
1205 static struct mdiobb_ops bb_ops
= {
1206 .owner
= THIS_MODULE
,
1207 .set_mdc
= sh_mdc_ctrl
,
1208 .set_mdio_dir
= sh_mmd_ctrl
,
1209 .set_mdio_data
= sh_set_mdio
,
1210 .get_mdio_data
= sh_get_mdio
,
1213 /* free Tx skb function */
1214 static int sh_eth_tx_free(struct net_device
*ndev
, bool sent_only
)
1216 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1217 struct sh_eth_txdesc
*txdesc
;
1222 for (; mdp
->cur_tx
- mdp
->dirty_tx
> 0; mdp
->dirty_tx
++) {
1223 entry
= mdp
->dirty_tx
% mdp
->num_tx_ring
;
1224 txdesc
= &mdp
->tx_ring
[entry
];
1225 sent
= !(txdesc
->status
& cpu_to_le32(TD_TACT
));
1226 if (sent_only
&& !sent
)
1228 /* TACT bit must be checked before all the following reads */
1230 netif_info(mdp
, tx_done
, ndev
,
1231 "tx entry %d status 0x%08x\n",
1232 entry
, le32_to_cpu(txdesc
->status
));
1233 /* Free the original skb. */
1234 if (mdp
->tx_skbuff
[entry
]) {
1235 dma_unmap_single(&mdp
->pdev
->dev
,
1236 le32_to_cpu(txdesc
->addr
),
1237 le32_to_cpu(txdesc
->len
) >> 16,
1239 dev_kfree_skb_irq(mdp
->tx_skbuff
[entry
]);
1240 mdp
->tx_skbuff
[entry
] = NULL
;
1243 txdesc
->status
= cpu_to_le32(TD_TFP
);
1244 if (entry
>= mdp
->num_tx_ring
- 1)
1245 txdesc
->status
|= cpu_to_le32(TD_TDLE
);
1248 ndev
->stats
.tx_packets
++;
1249 ndev
->stats
.tx_bytes
+= le32_to_cpu(txdesc
->len
) >> 16;
1255 /* free skb and descriptor buffer */
1256 static void sh_eth_ring_free(struct net_device
*ndev
)
1258 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1262 for (i
= 0; i
< mdp
->num_rx_ring
; i
++) {
1263 if (mdp
->rx_skbuff
[i
]) {
1264 struct sh_eth_rxdesc
*rxdesc
= &mdp
->rx_ring
[i
];
1266 dma_unmap_single(&mdp
->pdev
->dev
,
1267 le32_to_cpu(rxdesc
->addr
),
1268 ALIGN(mdp
->rx_buf_sz
, 32),
1272 ringsize
= sizeof(struct sh_eth_rxdesc
) * mdp
->num_rx_ring
;
1273 dma_free_coherent(&mdp
->pdev
->dev
, ringsize
, mdp
->rx_ring
,
1275 mdp
->rx_ring
= NULL
;
1278 /* Free Rx skb ringbuffer */
1279 if (mdp
->rx_skbuff
) {
1280 for (i
= 0; i
< mdp
->num_rx_ring
; i
++)
1281 dev_kfree_skb(mdp
->rx_skbuff
[i
]);
1283 kfree(mdp
->rx_skbuff
);
1284 mdp
->rx_skbuff
= NULL
;
1287 sh_eth_tx_free(ndev
, false);
1289 ringsize
= sizeof(struct sh_eth_txdesc
) * mdp
->num_tx_ring
;
1290 dma_free_coherent(&mdp
->pdev
->dev
, ringsize
, mdp
->tx_ring
,
1292 mdp
->tx_ring
= NULL
;
1295 /* Free Tx skb ringbuffer */
1296 kfree(mdp
->tx_skbuff
);
1297 mdp
->tx_skbuff
= NULL
;
1300 /* format skb and descriptor buffer */
1301 static void sh_eth_ring_format(struct net_device
*ndev
)
1303 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1305 struct sk_buff
*skb
;
1306 struct sh_eth_rxdesc
*rxdesc
= NULL
;
1307 struct sh_eth_txdesc
*txdesc
= NULL
;
1308 int rx_ringsize
= sizeof(*rxdesc
) * mdp
->num_rx_ring
;
1309 int tx_ringsize
= sizeof(*txdesc
) * mdp
->num_tx_ring
;
1310 int skbuff_size
= mdp
->rx_buf_sz
+ SH_ETH_RX_ALIGN
+ 32 - 1;
1311 dma_addr_t dma_addr
;
1319 memset(mdp
->rx_ring
, 0, rx_ringsize
);
1321 /* build Rx ring buffer */
1322 for (i
= 0; i
< mdp
->num_rx_ring
; i
++) {
1324 mdp
->rx_skbuff
[i
] = NULL
;
1325 skb
= netdev_alloc_skb(ndev
, skbuff_size
);
1328 sh_eth_set_receive_align(skb
);
1330 /* The size of the buffer is a multiple of 32 bytes. */
1331 buf_len
= ALIGN(mdp
->rx_buf_sz
, 32);
1332 dma_addr
= dma_map_single(&mdp
->pdev
->dev
, skb
->data
, buf_len
,
1334 if (dma_mapping_error(&mdp
->pdev
->dev
, dma_addr
)) {
1338 mdp
->rx_skbuff
[i
] = skb
;
1341 rxdesc
= &mdp
->rx_ring
[i
];
1342 rxdesc
->len
= cpu_to_le32(buf_len
<< 16);
1343 rxdesc
->addr
= cpu_to_le32(dma_addr
);
1344 rxdesc
->status
= cpu_to_le32(RD_RACT
| RD_RFP
);
1346 /* Rx descriptor address set */
1348 sh_eth_write(ndev
, mdp
->rx_desc_dma
, RDLAR
);
1349 if (mdp
->cd
->xdfar_rw
)
1350 sh_eth_write(ndev
, mdp
->rx_desc_dma
, RDFAR
);
1354 mdp
->dirty_rx
= (u32
) (i
- mdp
->num_rx_ring
);
1356 /* Mark the last entry as wrapping the ring. */
1358 rxdesc
->status
|= cpu_to_le32(RD_RDLE
);
1360 memset(mdp
->tx_ring
, 0, tx_ringsize
);
1362 /* build Tx ring buffer */
1363 for (i
= 0; i
< mdp
->num_tx_ring
; i
++) {
1364 mdp
->tx_skbuff
[i
] = NULL
;
1365 txdesc
= &mdp
->tx_ring
[i
];
1366 txdesc
->status
= cpu_to_le32(TD_TFP
);
1367 txdesc
->len
= cpu_to_le32(0);
1369 /* Tx descriptor address set */
1370 sh_eth_write(ndev
, mdp
->tx_desc_dma
, TDLAR
);
1371 if (mdp
->cd
->xdfar_rw
)
1372 sh_eth_write(ndev
, mdp
->tx_desc_dma
, TDFAR
);
1376 txdesc
->status
|= cpu_to_le32(TD_TDLE
);
1379 /* Get skb and descriptor buffer */
1380 static int sh_eth_ring_init(struct net_device
*ndev
)
1382 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1383 int rx_ringsize
, tx_ringsize
;
1385 /* +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
1386 * card needs room to do 8 byte alignment, +2 so we can reserve
1387 * the first 2 bytes, and +16 gets room for the status word from the
1390 mdp
->rx_buf_sz
= (ndev
->mtu
<= 1492 ? PKT_BUF_SZ
:
1391 (((ndev
->mtu
+ 26 + 7) & ~7) + 2 + 16));
1392 if (mdp
->cd
->rpadir
)
1393 mdp
->rx_buf_sz
+= NET_IP_ALIGN
;
1395 /* Allocate RX and TX skb rings */
1396 mdp
->rx_skbuff
= kcalloc(mdp
->num_rx_ring
, sizeof(*mdp
->rx_skbuff
),
1398 if (!mdp
->rx_skbuff
)
1401 mdp
->tx_skbuff
= kcalloc(mdp
->num_tx_ring
, sizeof(*mdp
->tx_skbuff
),
1403 if (!mdp
->tx_skbuff
)
1406 /* Allocate all Rx descriptors. */
1407 rx_ringsize
= sizeof(struct sh_eth_rxdesc
) * mdp
->num_rx_ring
;
1408 mdp
->rx_ring
= dma_alloc_coherent(&mdp
->pdev
->dev
, rx_ringsize
,
1409 &mdp
->rx_desc_dma
, GFP_KERNEL
);
1415 /* Allocate all Tx descriptors. */
1416 tx_ringsize
= sizeof(struct sh_eth_txdesc
) * mdp
->num_tx_ring
;
1417 mdp
->tx_ring
= dma_alloc_coherent(&mdp
->pdev
->dev
, tx_ringsize
,
1418 &mdp
->tx_desc_dma
, GFP_KERNEL
);
1424 /* Free Rx and Tx skb ring buffer and DMA buffer */
1425 sh_eth_ring_free(ndev
);
1430 static int sh_eth_dev_init(struct net_device
*ndev
)
1432 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1436 ret
= mdp
->cd
->soft_reset(ndev
);
1440 if (mdp
->cd
->rmiimode
)
1441 sh_eth_write(ndev
, 0x1, RMIIMODE
);
1443 /* Descriptor format */
1444 sh_eth_ring_format(ndev
);
1445 if (mdp
->cd
->rpadir
)
1446 sh_eth_write(ndev
, NET_IP_ALIGN
<< 16, RPADIR
);
1448 /* all sh_eth int mask */
1449 sh_eth_write(ndev
, 0, EESIPR
);
1451 #if defined(__LITTLE_ENDIAN)
1452 if (mdp
->cd
->hw_swap
)
1453 sh_eth_write(ndev
, EDMR_EL
, EDMR
);
1456 sh_eth_write(ndev
, 0, EDMR
);
1459 sh_eth_write(ndev
, mdp
->cd
->fdr_value
, FDR
);
1460 sh_eth_write(ndev
, 0, TFTR
);
1462 /* Frame recv control (enable multiple-packets per rx irq) */
1463 sh_eth_write(ndev
, RMCR_RNC
, RMCR
);
1465 sh_eth_write(ndev
, mdp
->cd
->trscer_err_mask
, TRSCER
);
1467 /* DMA transfer burst mode */
1469 sh_eth_modify(ndev
, EDMR
, EDMR_NBST
, EDMR_NBST
);
1471 /* Burst cycle count upper-limit */
1473 sh_eth_write(ndev
, 0x800, BCULR
);
1475 sh_eth_write(ndev
, mdp
->cd
->fcftr_value
, FCFTR
);
1477 if (!mdp
->cd
->no_trimd
)
1478 sh_eth_write(ndev
, 0, TRIMD
);
1480 /* Recv frame limit set register */
1481 sh_eth_write(ndev
, ndev
->mtu
+ ETH_HLEN
+ VLAN_HLEN
+ ETH_FCS_LEN
,
1484 sh_eth_modify(ndev
, EESR
, 0, 0);
1485 mdp
->irq_enabled
= true;
1486 sh_eth_write(ndev
, mdp
->cd
->eesipr_value
, EESIPR
);
1488 /* EMAC Mode: PAUSE prohibition; Duplex; RX Checksum; TX; RX */
1489 sh_eth_write(ndev
, ECMR_ZPF
| (mdp
->duplex
? ECMR_DM
: 0) |
1490 (ndev
->features
& NETIF_F_RXCSUM
? ECMR_RCSC
: 0) |
1491 ECMR_TE
| ECMR_RE
, ECMR
);
1493 if (mdp
->cd
->set_rate
)
1494 mdp
->cd
->set_rate(ndev
);
1496 /* E-MAC Status Register clear */
1497 sh_eth_write(ndev
, mdp
->cd
->ecsr_value
, ECSR
);
1499 /* E-MAC Interrupt Enable register */
1500 sh_eth_write(ndev
, mdp
->cd
->ecsipr_value
, ECSIPR
);
1502 /* Set MAC address */
1503 update_mac_address(ndev
);
1507 sh_eth_write(ndev
, 1, APR
);
1509 sh_eth_write(ndev
, 1, MPR
);
1510 if (mdp
->cd
->tpauser
)
1511 sh_eth_write(ndev
, TPAUSER_UNLIMITED
, TPAUSER
);
1513 /* Setting the Rx mode will start the Rx process. */
1514 sh_eth_write(ndev
, EDRRR_R
, EDRRR
);
1519 static void sh_eth_dev_exit(struct net_device
*ndev
)
1521 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1524 /* Deactivate all TX descriptors, so DMA should stop at next
1525 * packet boundary if it's currently running
1527 for (i
= 0; i
< mdp
->num_tx_ring
; i
++)
1528 mdp
->tx_ring
[i
].status
&= ~cpu_to_le32(TD_TACT
);
1530 /* Disable TX FIFO egress to MAC */
1531 sh_eth_rcv_snd_disable(ndev
);
1533 /* Stop RX DMA at next packet boundary */
1534 sh_eth_write(ndev
, 0, EDRRR
);
1536 /* Aside from TX DMA, we can't tell when the hardware is
1537 * really stopped, so we need to reset to make sure.
1538 * Before doing that, wait for long enough to *probably*
1539 * finish transmitting the last packet and poll stats.
1541 msleep(2); /* max frame time at 10 Mbps < 1250 us */
1542 sh_eth_get_stats(ndev
);
1543 mdp
->cd
->soft_reset(ndev
);
1545 /* Set the RMII mode again if required */
1546 if (mdp
->cd
->rmiimode
)
1547 sh_eth_write(ndev
, 0x1, RMIIMODE
);
1549 /* Set MAC address again */
1550 update_mac_address(ndev
);
1553 static void sh_eth_rx_csum(struct sk_buff
*skb
)
1557 /* The hardware checksum is 2 bytes appended to packet data */
1558 if (unlikely(skb
->len
< sizeof(__sum16
)))
1560 hw_csum
= skb_tail_pointer(skb
) - sizeof(__sum16
);
1561 skb
->csum
= csum_unfold((__force __sum16
)get_unaligned_le16(hw_csum
));
1562 skb
->ip_summed
= CHECKSUM_COMPLETE
;
1563 skb_trim(skb
, skb
->len
- sizeof(__sum16
));
1566 /* Packet receive function */
1567 static int sh_eth_rx(struct net_device
*ndev
, u32 intr_status
, int *quota
)
1569 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1570 struct sh_eth_rxdesc
*rxdesc
;
1572 int entry
= mdp
->cur_rx
% mdp
->num_rx_ring
;
1573 int boguscnt
= (mdp
->dirty_rx
+ mdp
->num_rx_ring
) - mdp
->cur_rx
;
1575 struct sk_buff
*skb
;
1577 int skbuff_size
= mdp
->rx_buf_sz
+ SH_ETH_RX_ALIGN
+ 32 - 1;
1578 dma_addr_t dma_addr
;
1582 boguscnt
= min(boguscnt
, *quota
);
1584 rxdesc
= &mdp
->rx_ring
[entry
];
1585 while (!(rxdesc
->status
& cpu_to_le32(RD_RACT
))) {
1586 /* RACT bit must be checked before all the following reads */
1588 desc_status
= le32_to_cpu(rxdesc
->status
);
1589 pkt_len
= le32_to_cpu(rxdesc
->len
) & RD_RFL
;
1594 netif_info(mdp
, rx_status
, ndev
,
1595 "rx entry %d status 0x%08x len %d\n",
1596 entry
, desc_status
, pkt_len
);
1598 if (!(desc_status
& RDFEND
))
1599 ndev
->stats
.rx_length_errors
++;
1601 /* In case of almost all GETHER/ETHERs, the Receive Frame State
1602 * (RFS) bits in the Receive Descriptor 0 are from bit 9 to
1603 * bit 0. However, in case of the R8A7740 and R7S72100
1604 * the RFS bits are from bit 25 to bit 16. So, the
1605 * driver needs right shifting by 16.
1610 skb
= mdp
->rx_skbuff
[entry
];
1611 if (desc_status
& (RD_RFS1
| RD_RFS2
| RD_RFS3
| RD_RFS4
|
1612 RD_RFS5
| RD_RFS6
| RD_RFS10
)) {
1613 ndev
->stats
.rx_errors
++;
1614 if (desc_status
& RD_RFS1
)
1615 ndev
->stats
.rx_crc_errors
++;
1616 if (desc_status
& RD_RFS2
)
1617 ndev
->stats
.rx_frame_errors
++;
1618 if (desc_status
& RD_RFS3
)
1619 ndev
->stats
.rx_length_errors
++;
1620 if (desc_status
& RD_RFS4
)
1621 ndev
->stats
.rx_length_errors
++;
1622 if (desc_status
& RD_RFS6
)
1623 ndev
->stats
.rx_missed_errors
++;
1624 if (desc_status
& RD_RFS10
)
1625 ndev
->stats
.rx_over_errors
++;
1627 dma_addr
= le32_to_cpu(rxdesc
->addr
);
1628 if (!mdp
->cd
->hw_swap
)
1630 phys_to_virt(ALIGN(dma_addr
, 4)),
1632 mdp
->rx_skbuff
[entry
] = NULL
;
1633 if (mdp
->cd
->rpadir
)
1634 skb_reserve(skb
, NET_IP_ALIGN
);
1635 dma_unmap_single(&mdp
->pdev
->dev
, dma_addr
,
1636 ALIGN(mdp
->rx_buf_sz
, 32),
1638 skb_put(skb
, pkt_len
);
1639 skb
->protocol
= eth_type_trans(skb
, ndev
);
1640 if (ndev
->features
& NETIF_F_RXCSUM
)
1641 sh_eth_rx_csum(skb
);
1642 netif_receive_skb(skb
);
1643 ndev
->stats
.rx_packets
++;
1644 ndev
->stats
.rx_bytes
+= pkt_len
;
1645 if (desc_status
& RD_RFS8
)
1646 ndev
->stats
.multicast
++;
1648 entry
= (++mdp
->cur_rx
) % mdp
->num_rx_ring
;
1649 rxdesc
= &mdp
->rx_ring
[entry
];
1652 /* Refill the Rx ring buffers. */
1653 for (; mdp
->cur_rx
- mdp
->dirty_rx
> 0; mdp
->dirty_rx
++) {
1654 entry
= mdp
->dirty_rx
% mdp
->num_rx_ring
;
1655 rxdesc
= &mdp
->rx_ring
[entry
];
1656 /* The size of the buffer is 32 byte boundary. */
1657 buf_len
= ALIGN(mdp
->rx_buf_sz
, 32);
1658 rxdesc
->len
= cpu_to_le32(buf_len
<< 16);
1660 if (mdp
->rx_skbuff
[entry
] == NULL
) {
1661 skb
= netdev_alloc_skb(ndev
, skbuff_size
);
1663 break; /* Better luck next round. */
1664 sh_eth_set_receive_align(skb
);
1665 dma_addr
= dma_map_single(&mdp
->pdev
->dev
, skb
->data
,
1666 buf_len
, DMA_FROM_DEVICE
);
1667 if (dma_mapping_error(&mdp
->pdev
->dev
, dma_addr
)) {
1671 mdp
->rx_skbuff
[entry
] = skb
;
1673 skb_checksum_none_assert(skb
);
1674 rxdesc
->addr
= cpu_to_le32(dma_addr
);
1676 dma_wmb(); /* RACT bit must be set after all the above writes */
1677 if (entry
>= mdp
->num_rx_ring
- 1)
1679 cpu_to_le32(RD_RACT
| RD_RFP
| RD_RDLE
);
1681 rxdesc
->status
|= cpu_to_le32(RD_RACT
| RD_RFP
);
1684 /* Restart Rx engine if stopped. */
1685 /* If we don't need to check status, don't. -KDU */
1686 if (!(sh_eth_read(ndev
, EDRRR
) & EDRRR_R
)) {
1687 /* fix the values for the next receiving if RDE is set */
1688 if (intr_status
& EESR_RDE
&& !mdp
->cd
->no_xdfar
) {
1689 u32 count
= (sh_eth_read(ndev
, RDFAR
) -
1690 sh_eth_read(ndev
, RDLAR
)) >> 4;
1692 mdp
->cur_rx
= count
;
1693 mdp
->dirty_rx
= count
;
1695 sh_eth_write(ndev
, EDRRR_R
, EDRRR
);
1698 *quota
-= limit
- boguscnt
- 1;
1703 static void sh_eth_rcv_snd_disable(struct net_device
*ndev
)
1705 /* disable tx and rx */
1706 sh_eth_modify(ndev
, ECMR
, ECMR_RE
| ECMR_TE
, 0);
1709 static void sh_eth_rcv_snd_enable(struct net_device
*ndev
)
1711 /* enable tx and rx */
1712 sh_eth_modify(ndev
, ECMR
, ECMR_RE
| ECMR_TE
, ECMR_RE
| ECMR_TE
);
1715 /* E-MAC interrupt handler */
1716 static void sh_eth_emac_interrupt(struct net_device
*ndev
)
1718 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1722 felic_stat
= sh_eth_read(ndev
, ECSR
) & sh_eth_read(ndev
, ECSIPR
);
1723 sh_eth_write(ndev
, felic_stat
, ECSR
); /* clear int */
1724 if (felic_stat
& ECSR_ICD
)
1725 ndev
->stats
.tx_carrier_errors
++;
1726 if (felic_stat
& ECSR_MPD
)
1727 pm_wakeup_event(&mdp
->pdev
->dev
, 0);
1728 if (felic_stat
& ECSR_LCHNG
) {
1730 if (mdp
->cd
->no_psr
|| mdp
->no_ether_link
)
1732 link_stat
= sh_eth_read(ndev
, PSR
);
1733 if (mdp
->ether_link_active_low
)
1734 link_stat
= ~link_stat
;
1735 if (!(link_stat
& PHY_ST_LINK
)) {
1736 sh_eth_rcv_snd_disable(ndev
);
1739 sh_eth_modify(ndev
, EESIPR
, EESIPR_ECIIP
, 0);
1741 sh_eth_modify(ndev
, ECSR
, 0, 0);
1742 sh_eth_modify(ndev
, EESIPR
, EESIPR_ECIIP
, EESIPR_ECIIP
);
1743 /* enable tx and rx */
1744 sh_eth_rcv_snd_enable(ndev
);
1749 /* error control function */
1750 static void sh_eth_error(struct net_device
*ndev
, u32 intr_status
)
1752 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1755 if (intr_status
& EESR_TWB
) {
1756 /* Unused write back interrupt */
1757 if (intr_status
& EESR_TABT
) { /* Transmit Abort int */
1758 ndev
->stats
.tx_aborted_errors
++;
1759 netif_err(mdp
, tx_err
, ndev
, "Transmit Abort\n");
1763 if (intr_status
& EESR_RABT
) {
1764 /* Receive Abort int */
1765 if (intr_status
& EESR_RFRMER
) {
1766 /* Receive Frame Overflow int */
1767 ndev
->stats
.rx_frame_errors
++;
1771 if (intr_status
& EESR_TDE
) {
1772 /* Transmit Descriptor Empty int */
1773 ndev
->stats
.tx_fifo_errors
++;
1774 netif_err(mdp
, tx_err
, ndev
, "Transmit Descriptor Empty\n");
1777 if (intr_status
& EESR_TFE
) {
1778 /* FIFO under flow */
1779 ndev
->stats
.tx_fifo_errors
++;
1780 netif_err(mdp
, tx_err
, ndev
, "Transmit FIFO Under flow\n");
1783 if (intr_status
& EESR_RDE
) {
1784 /* Receive Descriptor Empty int */
1785 ndev
->stats
.rx_over_errors
++;
1788 if (intr_status
& EESR_RFE
) {
1789 /* Receive FIFO Overflow int */
1790 ndev
->stats
.rx_fifo_errors
++;
1793 if (!mdp
->cd
->no_ade
&& (intr_status
& EESR_ADE
)) {
1795 ndev
->stats
.tx_fifo_errors
++;
1796 netif_err(mdp
, tx_err
, ndev
, "Address Error\n");
1799 mask
= EESR_TWB
| EESR_TABT
| EESR_ADE
| EESR_TDE
| EESR_TFE
;
1800 if (mdp
->cd
->no_ade
)
1802 if (intr_status
& mask
) {
1804 u32 edtrr
= sh_eth_read(ndev
, EDTRR
);
1807 netdev_err(ndev
, "TX error. status=%8.8x cur_tx=%8.8x dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
1808 intr_status
, mdp
->cur_tx
, mdp
->dirty_tx
,
1809 (u32
)ndev
->state
, edtrr
);
1810 /* dirty buffer free */
1811 sh_eth_tx_free(ndev
, true);
1814 if (edtrr
^ mdp
->cd
->edtrr_trns
) {
1816 sh_eth_write(ndev
, mdp
->cd
->edtrr_trns
, EDTRR
);
1819 netif_wake_queue(ndev
);
1823 static irqreturn_t
sh_eth_interrupt(int irq
, void *netdev
)
1825 struct net_device
*ndev
= netdev
;
1826 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1827 struct sh_eth_cpu_data
*cd
= mdp
->cd
;
1828 irqreturn_t ret
= IRQ_NONE
;
1829 u32 intr_status
, intr_enable
;
1831 spin_lock(&mdp
->lock
);
1833 /* Get interrupt status */
1834 intr_status
= sh_eth_read(ndev
, EESR
);
1835 /* Mask it with the interrupt mask, forcing ECI interrupt to be always
1836 * enabled since it's the one that comes thru regardless of the mask,
1837 * and we need to fully handle it in sh_eth_emac_interrupt() in order
1838 * to quench it as it doesn't get cleared by just writing 1 to the ECI
1841 intr_enable
= sh_eth_read(ndev
, EESIPR
);
1842 intr_status
&= intr_enable
| EESIPR_ECIIP
;
1843 if (intr_status
& (EESR_RX_CHECK
| cd
->tx_check
| EESR_ECI
|
1844 cd
->eesr_err_check
))
1849 if (unlikely(!mdp
->irq_enabled
)) {
1850 sh_eth_write(ndev
, 0, EESIPR
);
1854 if (intr_status
& EESR_RX_CHECK
) {
1855 if (napi_schedule_prep(&mdp
->napi
)) {
1856 /* Mask Rx interrupts */
1857 sh_eth_write(ndev
, intr_enable
& ~EESR_RX_CHECK
,
1859 __napi_schedule(&mdp
->napi
);
1862 "ignoring interrupt, status 0x%08x, mask 0x%08x.\n",
1863 intr_status
, intr_enable
);
1868 if (intr_status
& cd
->tx_check
) {
1869 /* Clear Tx interrupts */
1870 sh_eth_write(ndev
, intr_status
& cd
->tx_check
, EESR
);
1872 sh_eth_tx_free(ndev
, true);
1873 netif_wake_queue(ndev
);
1876 /* E-MAC interrupt */
1877 if (intr_status
& EESR_ECI
)
1878 sh_eth_emac_interrupt(ndev
);
1880 if (intr_status
& cd
->eesr_err_check
) {
1881 /* Clear error interrupts */
1882 sh_eth_write(ndev
, intr_status
& cd
->eesr_err_check
, EESR
);
1884 sh_eth_error(ndev
, intr_status
);
1888 spin_unlock(&mdp
->lock
);
1893 static int sh_eth_poll(struct napi_struct
*napi
, int budget
)
1895 struct sh_eth_private
*mdp
= container_of(napi
, struct sh_eth_private
,
1897 struct net_device
*ndev
= napi
->dev
;
1902 intr_status
= sh_eth_read(ndev
, EESR
);
1903 if (!(intr_status
& EESR_RX_CHECK
))
1905 /* Clear Rx interrupts */
1906 sh_eth_write(ndev
, intr_status
& EESR_RX_CHECK
, EESR
);
1908 if (sh_eth_rx(ndev
, intr_status
, "a
))
1912 napi_complete(napi
);
1914 /* Reenable Rx interrupts */
1915 if (mdp
->irq_enabled
)
1916 sh_eth_write(ndev
, mdp
->cd
->eesipr_value
, EESIPR
);
1918 return budget
- quota
;
1921 /* PHY state control function */
1922 static void sh_eth_adjust_link(struct net_device
*ndev
)
1924 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1925 struct phy_device
*phydev
= ndev
->phydev
;
1926 unsigned long flags
;
1929 spin_lock_irqsave(&mdp
->lock
, flags
);
1931 /* Disable TX and RX right over here, if E-MAC change is ignored */
1932 if (mdp
->cd
->no_psr
|| mdp
->no_ether_link
)
1933 sh_eth_rcv_snd_disable(ndev
);
1936 if (phydev
->duplex
!= mdp
->duplex
) {
1938 mdp
->duplex
= phydev
->duplex
;
1939 if (mdp
->cd
->set_duplex
)
1940 mdp
->cd
->set_duplex(ndev
);
1943 if (phydev
->speed
!= mdp
->speed
) {
1945 mdp
->speed
= phydev
->speed
;
1946 if (mdp
->cd
->set_rate
)
1947 mdp
->cd
->set_rate(ndev
);
1950 sh_eth_modify(ndev
, ECMR
, ECMR_TXF
, 0);
1952 mdp
->link
= phydev
->link
;
1954 } else if (mdp
->link
) {
1961 /* Enable TX and RX right over here, if E-MAC change is ignored */
1962 if ((mdp
->cd
->no_psr
|| mdp
->no_ether_link
) && phydev
->link
)
1963 sh_eth_rcv_snd_enable(ndev
);
1965 spin_unlock_irqrestore(&mdp
->lock
, flags
);
1967 if (new_state
&& netif_msg_link(mdp
))
1968 phy_print_status(phydev
);
1971 /* PHY init function */
1972 static int sh_eth_phy_init(struct net_device
*ndev
)
1974 struct device_node
*np
= ndev
->dev
.parent
->of_node
;
1975 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1976 struct phy_device
*phydev
;
1982 /* Try connect to PHY */
1984 struct device_node
*pn
;
1986 pn
= of_parse_phandle(np
, "phy-handle", 0);
1987 phydev
= of_phy_connect(ndev
, pn
,
1988 sh_eth_adjust_link
, 0,
1989 mdp
->phy_interface
);
1993 phydev
= ERR_PTR(-ENOENT
);
1995 char phy_id
[MII_BUS_ID_SIZE
+ 3];
1997 snprintf(phy_id
, sizeof(phy_id
), PHY_ID_FMT
,
1998 mdp
->mii_bus
->id
, mdp
->phy_id
);
2000 phydev
= phy_connect(ndev
, phy_id
, sh_eth_adjust_link
,
2001 mdp
->phy_interface
);
2004 if (IS_ERR(phydev
)) {
2005 netdev_err(ndev
, "failed to connect PHY\n");
2006 return PTR_ERR(phydev
);
2009 /* mask with MAC supported features */
2010 if (mdp
->cd
->register_type
!= SH_ETH_REG_GIGABIT
) {
2011 int err
= phy_set_max_speed(phydev
, SPEED_100
);
2013 netdev_err(ndev
, "failed to limit PHY to 100 Mbit/s\n");
2014 phy_disconnect(phydev
);
2019 phy_attached_info(phydev
);
2024 /* PHY control start function */
2025 static int sh_eth_phy_start(struct net_device
*ndev
)
2029 ret
= sh_eth_phy_init(ndev
);
2033 phy_start(ndev
->phydev
);
2038 /* If it is ever necessary to increase SH_ETH_REG_DUMP_MAX_REGS, the
2039 * version must be bumped as well. Just adding registers up to that
2040 * limit is fine, as long as the existing register indices don't
2043 #define SH_ETH_REG_DUMP_VERSION 1
2044 #define SH_ETH_REG_DUMP_MAX_REGS 256
2046 static size_t __sh_eth_get_regs(struct net_device
*ndev
, u32
*buf
)
2048 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2049 struct sh_eth_cpu_data
*cd
= mdp
->cd
;
2053 BUILD_BUG_ON(SH_ETH_MAX_REGISTER_OFFSET
> SH_ETH_REG_DUMP_MAX_REGS
);
2055 /* Dump starts with a bitmap that tells ethtool which
2056 * registers are defined for this chip.
2058 len
= DIV_ROUND_UP(SH_ETH_REG_DUMP_MAX_REGS
, 32);
2066 /* Add a register to the dump, if it has a defined offset.
2067 * This automatically skips most undefined registers, but for
2068 * some it is also necessary to check a capability flag in
2069 * struct sh_eth_cpu_data.
2071 #define mark_reg_valid(reg) valid_map[reg / 32] |= 1U << (reg % 32)
2072 #define add_reg_from(reg, read_expr) do { \
2073 if (mdp->reg_offset[reg] != SH_ETH_OFFSET_INVALID) { \
2075 mark_reg_valid(reg); \
2076 *buf++ = read_expr; \
2081 #define add_reg(reg) add_reg_from(reg, sh_eth_read(ndev, reg))
2082 #define add_tsu_reg(reg) add_reg_from(reg, sh_eth_tsu_read(mdp, reg))
2138 if (!cd
->no_tx_cntrs
) {
2161 add_tsu_reg(TSU_CTRST
);
2162 if (cd
->dual_port
) {
2163 add_tsu_reg(TSU_FWEN0
);
2164 add_tsu_reg(TSU_FWEN1
);
2165 add_tsu_reg(TSU_FCM
);
2166 add_tsu_reg(TSU_BSYSL0
);
2167 add_tsu_reg(TSU_BSYSL1
);
2168 add_tsu_reg(TSU_PRISL0
);
2169 add_tsu_reg(TSU_PRISL1
);
2170 add_tsu_reg(TSU_FWSL0
);
2171 add_tsu_reg(TSU_FWSL1
);
2173 add_tsu_reg(TSU_FWSLC
);
2174 if (cd
->dual_port
) {
2175 add_tsu_reg(TSU_QTAGM0
);
2176 add_tsu_reg(TSU_QTAGM1
);
2177 add_tsu_reg(TSU_FWSR
);
2178 add_tsu_reg(TSU_FWINMK
);
2179 add_tsu_reg(TSU_ADQT0
);
2180 add_tsu_reg(TSU_ADQT1
);
2181 add_tsu_reg(TSU_VTAG0
);
2182 add_tsu_reg(TSU_VTAG1
);
2184 add_tsu_reg(TSU_ADSBSY
);
2185 add_tsu_reg(TSU_TEN
);
2186 add_tsu_reg(TSU_POST1
);
2187 add_tsu_reg(TSU_POST2
);
2188 add_tsu_reg(TSU_POST3
);
2189 add_tsu_reg(TSU_POST4
);
2190 /* This is the start of a table, not just a single register. */
2194 mark_reg_valid(TSU_ADRH0
);
2195 for (i
= 0; i
< SH_ETH_TSU_CAM_ENTRIES
* 2; i
++)
2196 *buf
++ = ioread32(mdp
->tsu_addr
+
2197 mdp
->reg_offset
[TSU_ADRH0
] +
2200 len
+= SH_ETH_TSU_CAM_ENTRIES
* 2;
2203 #undef mark_reg_valid
2211 static int sh_eth_get_regs_len(struct net_device
*ndev
)
2213 return __sh_eth_get_regs(ndev
, NULL
);
2216 static void sh_eth_get_regs(struct net_device
*ndev
, struct ethtool_regs
*regs
,
2219 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2221 regs
->version
= SH_ETH_REG_DUMP_VERSION
;
2223 pm_runtime_get_sync(&mdp
->pdev
->dev
);
2224 __sh_eth_get_regs(ndev
, buf
);
2225 pm_runtime_put_sync(&mdp
->pdev
->dev
);
2228 static u32
sh_eth_get_msglevel(struct net_device
*ndev
)
2230 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2231 return mdp
->msg_enable
;
2234 static void sh_eth_set_msglevel(struct net_device
*ndev
, u32 value
)
2236 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2237 mdp
->msg_enable
= value
;
2240 static const char sh_eth_gstrings_stats
[][ETH_GSTRING_LEN
] = {
2241 "rx_current", "tx_current",
2242 "rx_dirty", "tx_dirty",
2244 #define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats)
2246 static int sh_eth_get_sset_count(struct net_device
*netdev
, int sset
)
2250 return SH_ETH_STATS_LEN
;
2256 static void sh_eth_get_ethtool_stats(struct net_device
*ndev
,
2257 struct ethtool_stats
*stats
, u64
*data
)
2259 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2262 /* device-specific stats */
2263 data
[i
++] = mdp
->cur_rx
;
2264 data
[i
++] = mdp
->cur_tx
;
2265 data
[i
++] = mdp
->dirty_rx
;
2266 data
[i
++] = mdp
->dirty_tx
;
2269 static void sh_eth_get_strings(struct net_device
*ndev
, u32 stringset
, u8
*data
)
2271 switch (stringset
) {
2273 memcpy(data
, *sh_eth_gstrings_stats
,
2274 sizeof(sh_eth_gstrings_stats
));
2279 static void sh_eth_get_ringparam(struct net_device
*ndev
,
2280 struct ethtool_ringparam
*ring
)
2282 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2284 ring
->rx_max_pending
= RX_RING_MAX
;
2285 ring
->tx_max_pending
= TX_RING_MAX
;
2286 ring
->rx_pending
= mdp
->num_rx_ring
;
2287 ring
->tx_pending
= mdp
->num_tx_ring
;
2290 static int sh_eth_set_ringparam(struct net_device
*ndev
,
2291 struct ethtool_ringparam
*ring
)
2293 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2296 if (ring
->tx_pending
> TX_RING_MAX
||
2297 ring
->rx_pending
> RX_RING_MAX
||
2298 ring
->tx_pending
< TX_RING_MIN
||
2299 ring
->rx_pending
< RX_RING_MIN
)
2301 if (ring
->rx_mini_pending
|| ring
->rx_jumbo_pending
)
2304 if (netif_running(ndev
)) {
2305 netif_device_detach(ndev
);
2306 netif_tx_disable(ndev
);
2308 /* Serialise with the interrupt handler and NAPI, then
2309 * disable interrupts. We have to clear the
2310 * irq_enabled flag first to ensure that interrupts
2311 * won't be re-enabled.
2313 mdp
->irq_enabled
= false;
2314 synchronize_irq(ndev
->irq
);
2315 napi_synchronize(&mdp
->napi
);
2316 sh_eth_write(ndev
, 0x0000, EESIPR
);
2318 sh_eth_dev_exit(ndev
);
2320 /* Free all the skbuffs in the Rx queue and the DMA buffers. */
2321 sh_eth_ring_free(ndev
);
2324 /* Set new parameters */
2325 mdp
->num_rx_ring
= ring
->rx_pending
;
2326 mdp
->num_tx_ring
= ring
->tx_pending
;
2328 if (netif_running(ndev
)) {
2329 ret
= sh_eth_ring_init(ndev
);
2331 netdev_err(ndev
, "%s: sh_eth_ring_init failed.\n",
2335 ret
= sh_eth_dev_init(ndev
);
2337 netdev_err(ndev
, "%s: sh_eth_dev_init failed.\n",
2342 netif_device_attach(ndev
);
2348 static void sh_eth_get_wol(struct net_device
*ndev
, struct ethtool_wolinfo
*wol
)
2350 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2355 if (mdp
->cd
->magic
) {
2356 wol
->supported
= WAKE_MAGIC
;
2357 wol
->wolopts
= mdp
->wol_enabled
? WAKE_MAGIC
: 0;
2361 static int sh_eth_set_wol(struct net_device
*ndev
, struct ethtool_wolinfo
*wol
)
2363 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2365 if (!mdp
->cd
->magic
|| wol
->wolopts
& ~WAKE_MAGIC
)
2368 mdp
->wol_enabled
= !!(wol
->wolopts
& WAKE_MAGIC
);
2370 device_set_wakeup_enable(&mdp
->pdev
->dev
, mdp
->wol_enabled
);
2375 static const struct ethtool_ops sh_eth_ethtool_ops
= {
2376 .get_regs_len
= sh_eth_get_regs_len
,
2377 .get_regs
= sh_eth_get_regs
,
2378 .nway_reset
= phy_ethtool_nway_reset
,
2379 .get_msglevel
= sh_eth_get_msglevel
,
2380 .set_msglevel
= sh_eth_set_msglevel
,
2381 .get_link
= ethtool_op_get_link
,
2382 .get_strings
= sh_eth_get_strings
,
2383 .get_ethtool_stats
= sh_eth_get_ethtool_stats
,
2384 .get_sset_count
= sh_eth_get_sset_count
,
2385 .get_ringparam
= sh_eth_get_ringparam
,
2386 .set_ringparam
= sh_eth_set_ringparam
,
2387 .get_link_ksettings
= phy_ethtool_get_link_ksettings
,
2388 .set_link_ksettings
= phy_ethtool_set_link_ksettings
,
2389 .get_wol
= sh_eth_get_wol
,
2390 .set_wol
= sh_eth_set_wol
,
2393 /* network device open function */
2394 static int sh_eth_open(struct net_device
*ndev
)
2396 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2399 pm_runtime_get_sync(&mdp
->pdev
->dev
);
2401 napi_enable(&mdp
->napi
);
2403 ret
= request_irq(ndev
->irq
, sh_eth_interrupt
,
2404 mdp
->cd
->irq_flags
, ndev
->name
, ndev
);
2406 netdev_err(ndev
, "Can not assign IRQ number\n");
2410 /* Descriptor set */
2411 ret
= sh_eth_ring_init(ndev
);
2416 ret
= sh_eth_dev_init(ndev
);
2420 /* PHY control start*/
2421 ret
= sh_eth_phy_start(ndev
);
2425 netif_start_queue(ndev
);
2432 free_irq(ndev
->irq
, ndev
);
2434 napi_disable(&mdp
->napi
);
2435 pm_runtime_put_sync(&mdp
->pdev
->dev
);
2439 /* Timeout function */
2440 static void sh_eth_tx_timeout(struct net_device
*ndev
, unsigned int txqueue
)
2442 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2443 struct sh_eth_rxdesc
*rxdesc
;
2446 netif_stop_queue(ndev
);
2448 netif_err(mdp
, timer
, ndev
,
2449 "transmit timed out, status %8.8x, resetting...\n",
2450 sh_eth_read(ndev
, EESR
));
2452 /* tx_errors count up */
2453 ndev
->stats
.tx_errors
++;
2455 /* Free all the skbuffs in the Rx queue. */
2456 for (i
= 0; i
< mdp
->num_rx_ring
; i
++) {
2457 rxdesc
= &mdp
->rx_ring
[i
];
2458 rxdesc
->status
= cpu_to_le32(0);
2459 rxdesc
->addr
= cpu_to_le32(0xBADF00D0);
2460 dev_kfree_skb(mdp
->rx_skbuff
[i
]);
2461 mdp
->rx_skbuff
[i
] = NULL
;
2463 for (i
= 0; i
< mdp
->num_tx_ring
; i
++) {
2464 dev_kfree_skb(mdp
->tx_skbuff
[i
]);
2465 mdp
->tx_skbuff
[i
] = NULL
;
2469 sh_eth_dev_init(ndev
);
2471 netif_start_queue(ndev
);
2474 /* Packet transmit function */
2475 static int sh_eth_start_xmit(struct sk_buff
*skb
, struct net_device
*ndev
)
2477 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2478 struct sh_eth_txdesc
*txdesc
;
2479 dma_addr_t dma_addr
;
2481 unsigned long flags
;
2483 spin_lock_irqsave(&mdp
->lock
, flags
);
2484 if ((mdp
->cur_tx
- mdp
->dirty_tx
) >= (mdp
->num_tx_ring
- 4)) {
2485 if (!sh_eth_tx_free(ndev
, true)) {
2486 netif_warn(mdp
, tx_queued
, ndev
, "TxFD exhausted.\n");
2487 netif_stop_queue(ndev
);
2488 spin_unlock_irqrestore(&mdp
->lock
, flags
);
2489 return NETDEV_TX_BUSY
;
2492 spin_unlock_irqrestore(&mdp
->lock
, flags
);
2494 if (skb_put_padto(skb
, ETH_ZLEN
))
2495 return NETDEV_TX_OK
;
2497 entry
= mdp
->cur_tx
% mdp
->num_tx_ring
;
2498 mdp
->tx_skbuff
[entry
] = skb
;
2499 txdesc
= &mdp
->tx_ring
[entry
];
2501 if (!mdp
->cd
->hw_swap
)
2502 sh_eth_soft_swap(PTR_ALIGN(skb
->data
, 4), skb
->len
+ 2);
2503 dma_addr
= dma_map_single(&mdp
->pdev
->dev
, skb
->data
, skb
->len
,
2505 if (dma_mapping_error(&mdp
->pdev
->dev
, dma_addr
)) {
2507 return NETDEV_TX_OK
;
2509 txdesc
->addr
= cpu_to_le32(dma_addr
);
2510 txdesc
->len
= cpu_to_le32(skb
->len
<< 16);
2512 dma_wmb(); /* TACT bit must be set after all the above writes */
2513 if (entry
>= mdp
->num_tx_ring
- 1)
2514 txdesc
->status
|= cpu_to_le32(TD_TACT
| TD_TDLE
);
2516 txdesc
->status
|= cpu_to_le32(TD_TACT
);
2520 if (!(sh_eth_read(ndev
, EDTRR
) & mdp
->cd
->edtrr_trns
))
2521 sh_eth_write(ndev
, mdp
->cd
->edtrr_trns
, EDTRR
);
2523 return NETDEV_TX_OK
;
2526 /* The statistics registers have write-clear behaviour, which means we
2527 * will lose any increment between the read and write. We mitigate
2528 * this by only clearing when we read a non-zero value, so we will
2529 * never falsely report a total of zero.
2532 sh_eth_update_stat(struct net_device
*ndev
, unsigned long *stat
, int reg
)
2534 u32 delta
= sh_eth_read(ndev
, reg
);
2538 sh_eth_write(ndev
, 0, reg
);
2542 static struct net_device_stats
*sh_eth_get_stats(struct net_device
*ndev
)
2544 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2546 if (mdp
->cd
->no_tx_cntrs
)
2547 return &ndev
->stats
;
2549 if (!mdp
->is_opened
)
2550 return &ndev
->stats
;
2552 sh_eth_update_stat(ndev
, &ndev
->stats
.tx_dropped
, TROCR
);
2553 sh_eth_update_stat(ndev
, &ndev
->stats
.collisions
, CDCR
);
2554 sh_eth_update_stat(ndev
, &ndev
->stats
.tx_carrier_errors
, LCCR
);
2556 if (mdp
->cd
->cexcr
) {
2557 sh_eth_update_stat(ndev
, &ndev
->stats
.tx_carrier_errors
,
2559 sh_eth_update_stat(ndev
, &ndev
->stats
.tx_carrier_errors
,
2562 sh_eth_update_stat(ndev
, &ndev
->stats
.tx_carrier_errors
,
2566 return &ndev
->stats
;
2569 /* device close function */
2570 static int sh_eth_close(struct net_device
*ndev
)
2572 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2574 netif_stop_queue(ndev
);
2576 /* Serialise with the interrupt handler and NAPI, then disable
2577 * interrupts. We have to clear the irq_enabled flag first to
2578 * ensure that interrupts won't be re-enabled.
2580 mdp
->irq_enabled
= false;
2581 synchronize_irq(ndev
->irq
);
2582 napi_disable(&mdp
->napi
);
2583 sh_eth_write(ndev
, 0x0000, EESIPR
);
2585 sh_eth_dev_exit(ndev
);
2587 /* PHY Disconnect */
2589 phy_stop(ndev
->phydev
);
2590 phy_disconnect(ndev
->phydev
);
2593 free_irq(ndev
->irq
, ndev
);
2595 /* Free all the skbuffs in the Rx queue and the DMA buffer. */
2596 sh_eth_ring_free(ndev
);
2598 pm_runtime_put_sync(&mdp
->pdev
->dev
);
2605 static int sh_eth_change_mtu(struct net_device
*ndev
, int new_mtu
)
2607 if (netif_running(ndev
))
2610 ndev
->mtu
= new_mtu
;
2611 netdev_update_features(ndev
);
2616 /* For TSU_POSTn. Please refer to the manual about this (strange) bitfields */
2617 static u32
sh_eth_tsu_get_post_mask(int entry
)
2619 return 0x0f << (28 - ((entry
% 8) * 4));
2622 static u32
sh_eth_tsu_get_post_bit(struct sh_eth_private
*mdp
, int entry
)
2624 return (0x08 >> (mdp
->port
<< 1)) << (28 - ((entry
% 8) * 4));
2627 static void sh_eth_tsu_enable_cam_entry_post(struct net_device
*ndev
,
2630 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2631 int reg
= TSU_POST1
+ entry
/ 8;
2634 tmp
= sh_eth_tsu_read(mdp
, reg
);
2635 sh_eth_tsu_write(mdp
, tmp
| sh_eth_tsu_get_post_bit(mdp
, entry
), reg
);
2638 static bool sh_eth_tsu_disable_cam_entry_post(struct net_device
*ndev
,
2641 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2642 int reg
= TSU_POST1
+ entry
/ 8;
2643 u32 post_mask
, ref_mask
, tmp
;
2645 post_mask
= sh_eth_tsu_get_post_mask(entry
);
2646 ref_mask
= sh_eth_tsu_get_post_bit(mdp
, entry
) & ~post_mask
;
2648 tmp
= sh_eth_tsu_read(mdp
, reg
);
2649 sh_eth_tsu_write(mdp
, tmp
& ~post_mask
, reg
);
2651 /* If other port enables, the function returns "true" */
2652 return tmp
& ref_mask
;
2655 static int sh_eth_tsu_busy(struct net_device
*ndev
)
2657 int timeout
= SH_ETH_TSU_TIMEOUT_MS
* 100;
2658 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2660 while ((sh_eth_tsu_read(mdp
, TSU_ADSBSY
) & TSU_ADSBSY_0
)) {
2664 netdev_err(ndev
, "%s: timeout\n", __func__
);
2672 static int sh_eth_tsu_write_entry(struct net_device
*ndev
, u16 offset
,
2675 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2678 val
= addr
[0] << 24 | addr
[1] << 16 | addr
[2] << 8 | addr
[3];
2679 iowrite32(val
, mdp
->tsu_addr
+ offset
);
2680 if (sh_eth_tsu_busy(ndev
) < 0)
2683 val
= addr
[4] << 8 | addr
[5];
2684 iowrite32(val
, mdp
->tsu_addr
+ offset
+ 4);
2685 if (sh_eth_tsu_busy(ndev
) < 0)
2691 static void sh_eth_tsu_read_entry(struct net_device
*ndev
, u16 offset
, u8
*addr
)
2693 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2696 val
= ioread32(mdp
->tsu_addr
+ offset
);
2697 addr
[0] = (val
>> 24) & 0xff;
2698 addr
[1] = (val
>> 16) & 0xff;
2699 addr
[2] = (val
>> 8) & 0xff;
2700 addr
[3] = val
& 0xff;
2701 val
= ioread32(mdp
->tsu_addr
+ offset
+ 4);
2702 addr
[4] = (val
>> 8) & 0xff;
2703 addr
[5] = val
& 0xff;
2707 static int sh_eth_tsu_find_entry(struct net_device
*ndev
, const u8
*addr
)
2709 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2710 u16 reg_offset
= sh_eth_tsu_get_offset(mdp
, TSU_ADRH0
);
2712 u8 c_addr
[ETH_ALEN
];
2714 for (i
= 0; i
< SH_ETH_TSU_CAM_ENTRIES
; i
++, reg_offset
+= 8) {
2715 sh_eth_tsu_read_entry(ndev
, reg_offset
, c_addr
);
2716 if (ether_addr_equal(addr
, c_addr
))
2723 static int sh_eth_tsu_find_empty(struct net_device
*ndev
)
2728 memset(blank
, 0, sizeof(blank
));
2729 entry
= sh_eth_tsu_find_entry(ndev
, blank
);
2730 return (entry
< 0) ? -ENOMEM
: entry
;
2733 static int sh_eth_tsu_disable_cam_entry_table(struct net_device
*ndev
,
2736 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2737 u16 reg_offset
= sh_eth_tsu_get_offset(mdp
, TSU_ADRH0
);
2741 sh_eth_tsu_write(mdp
, sh_eth_tsu_read(mdp
, TSU_TEN
) &
2742 ~(1 << (31 - entry
)), TSU_TEN
);
2744 memset(blank
, 0, sizeof(blank
));
2745 ret
= sh_eth_tsu_write_entry(ndev
, reg_offset
+ entry
* 8, blank
);
2751 static int sh_eth_tsu_add_entry(struct net_device
*ndev
, const u8
*addr
)
2753 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2754 u16 reg_offset
= sh_eth_tsu_get_offset(mdp
, TSU_ADRH0
);
2760 i
= sh_eth_tsu_find_entry(ndev
, addr
);
2762 /* No entry found, create one */
2763 i
= sh_eth_tsu_find_empty(ndev
);
2766 ret
= sh_eth_tsu_write_entry(ndev
, reg_offset
+ i
* 8, addr
);
2770 /* Enable the entry */
2771 sh_eth_tsu_write(mdp
, sh_eth_tsu_read(mdp
, TSU_TEN
) |
2772 (1 << (31 - i
)), TSU_TEN
);
2775 /* Entry found or created, enable POST */
2776 sh_eth_tsu_enable_cam_entry_post(ndev
, i
);
2781 static int sh_eth_tsu_del_entry(struct net_device
*ndev
, const u8
*addr
)
2783 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2789 i
= sh_eth_tsu_find_entry(ndev
, addr
);
2792 if (sh_eth_tsu_disable_cam_entry_post(ndev
, i
))
2795 /* Disable the entry if both ports was disabled */
2796 ret
= sh_eth_tsu_disable_cam_entry_table(ndev
, i
);
2804 static int sh_eth_tsu_purge_all(struct net_device
*ndev
)
2806 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2812 for (i
= 0; i
< SH_ETH_TSU_CAM_ENTRIES
; i
++) {
2813 if (sh_eth_tsu_disable_cam_entry_post(ndev
, i
))
2816 /* Disable the entry if both ports was disabled */
2817 ret
= sh_eth_tsu_disable_cam_entry_table(ndev
, i
);
2825 static void sh_eth_tsu_purge_mcast(struct net_device
*ndev
)
2827 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2828 u16 reg_offset
= sh_eth_tsu_get_offset(mdp
, TSU_ADRH0
);
2835 for (i
= 0; i
< SH_ETH_TSU_CAM_ENTRIES
; i
++, reg_offset
+= 8) {
2836 sh_eth_tsu_read_entry(ndev
, reg_offset
, addr
);
2837 if (is_multicast_ether_addr(addr
))
2838 sh_eth_tsu_del_entry(ndev
, addr
);
2842 /* Update promiscuous flag and multicast filter */
2843 static void sh_eth_set_rx_mode(struct net_device
*ndev
)
2845 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2848 unsigned long flags
;
2850 spin_lock_irqsave(&mdp
->lock
, flags
);
2851 /* Initial condition is MCT = 1, PRM = 0.
2852 * Depending on ndev->flags, set PRM or clear MCT
2854 ecmr_bits
= sh_eth_read(ndev
, ECMR
) & ~ECMR_PRM
;
2856 ecmr_bits
|= ECMR_MCT
;
2858 if (!(ndev
->flags
& IFF_MULTICAST
)) {
2859 sh_eth_tsu_purge_mcast(ndev
);
2862 if (ndev
->flags
& IFF_ALLMULTI
) {
2863 sh_eth_tsu_purge_mcast(ndev
);
2864 ecmr_bits
&= ~ECMR_MCT
;
2868 if (ndev
->flags
& IFF_PROMISC
) {
2869 sh_eth_tsu_purge_all(ndev
);
2870 ecmr_bits
= (ecmr_bits
& ~ECMR_MCT
) | ECMR_PRM
;
2871 } else if (mdp
->cd
->tsu
) {
2872 struct netdev_hw_addr
*ha
;
2873 netdev_for_each_mc_addr(ha
, ndev
) {
2874 if (mcast_all
&& is_multicast_ether_addr(ha
->addr
))
2877 if (sh_eth_tsu_add_entry(ndev
, ha
->addr
) < 0) {
2879 sh_eth_tsu_purge_mcast(ndev
);
2880 ecmr_bits
&= ~ECMR_MCT
;
2887 /* update the ethernet mode */
2888 sh_eth_write(ndev
, ecmr_bits
, ECMR
);
2890 spin_unlock_irqrestore(&mdp
->lock
, flags
);
2893 static void sh_eth_set_rx_csum(struct net_device
*ndev
, bool enable
)
2895 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2896 unsigned long flags
;
2898 spin_lock_irqsave(&mdp
->lock
, flags
);
2900 /* Disable TX and RX */
2901 sh_eth_rcv_snd_disable(ndev
);
2903 /* Modify RX Checksum setting */
2904 sh_eth_modify(ndev
, ECMR
, ECMR_RCSC
, enable
? ECMR_RCSC
: 0);
2906 /* Enable TX and RX */
2907 sh_eth_rcv_snd_enable(ndev
);
2909 spin_unlock_irqrestore(&mdp
->lock
, flags
);
2912 static int sh_eth_set_features(struct net_device
*ndev
,
2913 netdev_features_t features
)
2915 netdev_features_t changed
= ndev
->features
^ features
;
2916 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2918 if (changed
& NETIF_F_RXCSUM
&& mdp
->cd
->rx_csum
)
2919 sh_eth_set_rx_csum(ndev
, features
& NETIF_F_RXCSUM
);
2921 ndev
->features
= features
;
2926 static int sh_eth_get_vtag_index(struct sh_eth_private
*mdp
)
2934 static int sh_eth_vlan_rx_add_vid(struct net_device
*ndev
,
2935 __be16 proto
, u16 vid
)
2937 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2938 int vtag_reg_index
= sh_eth_get_vtag_index(mdp
);
2940 if (unlikely(!mdp
->cd
->tsu
))
2943 /* No filtering if vid = 0 */
2947 mdp
->vlan_num_ids
++;
2949 /* The controller has one VLAN tag HW filter. So, if the filter is
2950 * already enabled, the driver disables it and the filte
2952 if (mdp
->vlan_num_ids
> 1) {
2953 /* disable VLAN filter */
2954 sh_eth_tsu_write(mdp
, 0, vtag_reg_index
);
2958 sh_eth_tsu_write(mdp
, TSU_VTAG_ENABLE
| (vid
& TSU_VTAG_VID_MASK
),
2964 static int sh_eth_vlan_rx_kill_vid(struct net_device
*ndev
,
2965 __be16 proto
, u16 vid
)
2967 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2968 int vtag_reg_index
= sh_eth_get_vtag_index(mdp
);
2970 if (unlikely(!mdp
->cd
->tsu
))
2973 /* No filtering if vid = 0 */
2977 mdp
->vlan_num_ids
--;
2978 sh_eth_tsu_write(mdp
, 0, vtag_reg_index
);
2983 /* SuperH's TSU register init function */
2984 static void sh_eth_tsu_init(struct sh_eth_private
*mdp
)
2986 if (!mdp
->cd
->dual_port
) {
2987 sh_eth_tsu_write(mdp
, 0, TSU_TEN
); /* Disable all CAM entry */
2988 sh_eth_tsu_write(mdp
, TSU_FWSLC_POSTENU
| TSU_FWSLC_POSTENL
,
2989 TSU_FWSLC
); /* Enable POST registers */
2993 sh_eth_tsu_write(mdp
, 0, TSU_FWEN0
); /* Disable forward(0->1) */
2994 sh_eth_tsu_write(mdp
, 0, TSU_FWEN1
); /* Disable forward(1->0) */
2995 sh_eth_tsu_write(mdp
, 0, TSU_FCM
); /* forward fifo 3k-3k */
2996 sh_eth_tsu_write(mdp
, 0xc, TSU_BSYSL0
);
2997 sh_eth_tsu_write(mdp
, 0xc, TSU_BSYSL1
);
2998 sh_eth_tsu_write(mdp
, 0, TSU_PRISL0
);
2999 sh_eth_tsu_write(mdp
, 0, TSU_PRISL1
);
3000 sh_eth_tsu_write(mdp
, 0, TSU_FWSL0
);
3001 sh_eth_tsu_write(mdp
, 0, TSU_FWSL1
);
3002 sh_eth_tsu_write(mdp
, TSU_FWSLC_POSTENU
| TSU_FWSLC_POSTENL
, TSU_FWSLC
);
3003 sh_eth_tsu_write(mdp
, 0, TSU_QTAGM0
); /* Disable QTAG(0->1) */
3004 sh_eth_tsu_write(mdp
, 0, TSU_QTAGM1
); /* Disable QTAG(1->0) */
3005 sh_eth_tsu_write(mdp
, 0, TSU_FWSR
); /* all interrupt status clear */
3006 sh_eth_tsu_write(mdp
, 0, TSU_FWINMK
); /* Disable all interrupt */
3007 sh_eth_tsu_write(mdp
, 0, TSU_TEN
); /* Disable all CAM entry */
3008 sh_eth_tsu_write(mdp
, 0, TSU_POST1
); /* Disable CAM entry [ 0- 7] */
3009 sh_eth_tsu_write(mdp
, 0, TSU_POST2
); /* Disable CAM entry [ 8-15] */
3010 sh_eth_tsu_write(mdp
, 0, TSU_POST3
); /* Disable CAM entry [16-23] */
3011 sh_eth_tsu_write(mdp
, 0, TSU_POST4
); /* Disable CAM entry [24-31] */
3014 /* MDIO bus release function */
3015 static int sh_mdio_release(struct sh_eth_private
*mdp
)
3017 /* unregister mdio bus */
3018 mdiobus_unregister(mdp
->mii_bus
);
3020 /* free bitbang info */
3021 free_mdio_bitbang(mdp
->mii_bus
);
3026 /* MDIO bus init function */
3027 static int sh_mdio_init(struct sh_eth_private
*mdp
,
3028 struct sh_eth_plat_data
*pd
)
3031 struct bb_info
*bitbang
;
3032 struct platform_device
*pdev
= mdp
->pdev
;
3033 struct device
*dev
= &mdp
->pdev
->dev
;
3035 /* create bit control struct for PHY */
3036 bitbang
= devm_kzalloc(dev
, sizeof(struct bb_info
), GFP_KERNEL
);
3041 bitbang
->addr
= mdp
->addr
+ mdp
->reg_offset
[PIR
];
3042 bitbang
->set_gate
= pd
->set_mdio_gate
;
3043 bitbang
->ctrl
.ops
= &bb_ops
;
3045 /* MII controller setting */
3046 mdp
->mii_bus
= alloc_mdio_bitbang(&bitbang
->ctrl
);
3050 /* Hook up MII support for ethtool */
3051 mdp
->mii_bus
->name
= "sh_mii";
3052 mdp
->mii_bus
->parent
= dev
;
3053 snprintf(mdp
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%s-%x",
3054 pdev
->name
, pdev
->id
);
3056 /* register MDIO bus */
3057 if (pd
->phy_irq
> 0)
3058 mdp
->mii_bus
->irq
[pd
->phy
] = pd
->phy_irq
;
3060 ret
= of_mdiobus_register(mdp
->mii_bus
, dev
->of_node
);
3067 free_mdio_bitbang(mdp
->mii_bus
);
3071 static const u16
*sh_eth_get_register_offset(int register_type
)
3073 const u16
*reg_offset
= NULL
;
3075 switch (register_type
) {
3076 case SH_ETH_REG_GIGABIT
:
3077 reg_offset
= sh_eth_offset_gigabit
;
3079 case SH_ETH_REG_FAST_RCAR
:
3080 reg_offset
= sh_eth_offset_fast_rcar
;
3082 case SH_ETH_REG_FAST_SH4
:
3083 reg_offset
= sh_eth_offset_fast_sh4
;
3085 case SH_ETH_REG_FAST_SH3_SH2
:
3086 reg_offset
= sh_eth_offset_fast_sh3_sh2
;
3093 static const struct net_device_ops sh_eth_netdev_ops
= {
3094 .ndo_open
= sh_eth_open
,
3095 .ndo_stop
= sh_eth_close
,
3096 .ndo_start_xmit
= sh_eth_start_xmit
,
3097 .ndo_get_stats
= sh_eth_get_stats
,
3098 .ndo_set_rx_mode
= sh_eth_set_rx_mode
,
3099 .ndo_tx_timeout
= sh_eth_tx_timeout
,
3100 .ndo_do_ioctl
= phy_do_ioctl_running
,
3101 .ndo_change_mtu
= sh_eth_change_mtu
,
3102 .ndo_validate_addr
= eth_validate_addr
,
3103 .ndo_set_mac_address
= eth_mac_addr
,
3104 .ndo_set_features
= sh_eth_set_features
,
3107 static const struct net_device_ops sh_eth_netdev_ops_tsu
= {
3108 .ndo_open
= sh_eth_open
,
3109 .ndo_stop
= sh_eth_close
,
3110 .ndo_start_xmit
= sh_eth_start_xmit
,
3111 .ndo_get_stats
= sh_eth_get_stats
,
3112 .ndo_set_rx_mode
= sh_eth_set_rx_mode
,
3113 .ndo_vlan_rx_add_vid
= sh_eth_vlan_rx_add_vid
,
3114 .ndo_vlan_rx_kill_vid
= sh_eth_vlan_rx_kill_vid
,
3115 .ndo_tx_timeout
= sh_eth_tx_timeout
,
3116 .ndo_do_ioctl
= phy_do_ioctl_running
,
3117 .ndo_change_mtu
= sh_eth_change_mtu
,
3118 .ndo_validate_addr
= eth_validate_addr
,
3119 .ndo_set_mac_address
= eth_mac_addr
,
3120 .ndo_set_features
= sh_eth_set_features
,
3124 static struct sh_eth_plat_data
*sh_eth_parse_dt(struct device
*dev
)
3126 struct device_node
*np
= dev
->of_node
;
3127 struct sh_eth_plat_data
*pdata
;
3128 phy_interface_t interface
;
3129 const char *mac_addr
;
3132 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
3136 ret
= of_get_phy_mode(np
, &interface
);
3139 pdata
->phy_interface
= interface
;
3141 mac_addr
= of_get_mac_address(np
);
3142 if (!IS_ERR(mac_addr
))
3143 ether_addr_copy(pdata
->mac_addr
, mac_addr
);
3145 pdata
->no_ether_link
=
3146 of_property_read_bool(np
, "renesas,no-ether-link");
3147 pdata
->ether_link_active_low
=
3148 of_property_read_bool(np
, "renesas,ether-link-active-low");
3153 static const struct of_device_id sh_eth_match_table
[] = {
3154 { .compatible
= "renesas,gether-r8a7740", .data
= &r8a7740_data
},
3155 { .compatible
= "renesas,ether-r8a7743", .data
= &rcar_gen2_data
},
3156 { .compatible
= "renesas,ether-r8a7745", .data
= &rcar_gen2_data
},
3157 { .compatible
= "renesas,ether-r8a7778", .data
= &rcar_gen1_data
},
3158 { .compatible
= "renesas,ether-r8a7779", .data
= &rcar_gen1_data
},
3159 { .compatible
= "renesas,ether-r8a7790", .data
= &rcar_gen2_data
},
3160 { .compatible
= "renesas,ether-r8a7791", .data
= &rcar_gen2_data
},
3161 { .compatible
= "renesas,ether-r8a7793", .data
= &rcar_gen2_data
},
3162 { .compatible
= "renesas,ether-r8a7794", .data
= &rcar_gen2_data
},
3163 { .compatible
= "renesas,gether-r8a77980", .data
= &r8a77980_data
},
3164 { .compatible
= "renesas,ether-r7s72100", .data
= &r7s72100_data
},
3165 { .compatible
= "renesas,ether-r7s9210", .data
= &r7s9210_data
},
3166 { .compatible
= "renesas,rcar-gen1-ether", .data
= &rcar_gen1_data
},
3167 { .compatible
= "renesas,rcar-gen2-ether", .data
= &rcar_gen2_data
},
3170 MODULE_DEVICE_TABLE(of
, sh_eth_match_table
);
3172 static inline struct sh_eth_plat_data
*sh_eth_parse_dt(struct device
*dev
)
3178 static int sh_eth_drv_probe(struct platform_device
*pdev
)
3180 struct resource
*res
;
3181 struct sh_eth_plat_data
*pd
= dev_get_platdata(&pdev
->dev
);
3182 const struct platform_device_id
*id
= platform_get_device_id(pdev
);
3183 struct sh_eth_private
*mdp
;
3184 struct net_device
*ndev
;
3188 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
3190 ndev
= alloc_etherdev(sizeof(struct sh_eth_private
));
3194 pm_runtime_enable(&pdev
->dev
);
3195 pm_runtime_get_sync(&pdev
->dev
);
3197 ret
= platform_get_irq(pdev
, 0);
3202 SET_NETDEV_DEV(ndev
, &pdev
->dev
);
3204 mdp
= netdev_priv(ndev
);
3205 mdp
->num_tx_ring
= TX_RING_SIZE
;
3206 mdp
->num_rx_ring
= RX_RING_SIZE
;
3207 mdp
->addr
= devm_ioremap_resource(&pdev
->dev
, res
);
3208 if (IS_ERR(mdp
->addr
)) {
3209 ret
= PTR_ERR(mdp
->addr
);
3213 ndev
->base_addr
= res
->start
;
3215 spin_lock_init(&mdp
->lock
);
3218 if (pdev
->dev
.of_node
)
3219 pd
= sh_eth_parse_dt(&pdev
->dev
);
3221 dev_err(&pdev
->dev
, "no platform data\n");
3227 mdp
->phy_id
= pd
->phy
;
3228 mdp
->phy_interface
= pd
->phy_interface
;
3229 mdp
->no_ether_link
= pd
->no_ether_link
;
3230 mdp
->ether_link_active_low
= pd
->ether_link_active_low
;
3234 mdp
->cd
= (struct sh_eth_cpu_data
*)id
->driver_data
;
3236 mdp
->cd
= (struct sh_eth_cpu_data
*)of_device_get_match_data(&pdev
->dev
);
3238 mdp
->reg_offset
= sh_eth_get_register_offset(mdp
->cd
->register_type
);
3239 if (!mdp
->reg_offset
) {
3240 dev_err(&pdev
->dev
, "Unknown register type (%d)\n",
3241 mdp
->cd
->register_type
);
3245 sh_eth_set_default_cpu_data(mdp
->cd
);
3247 /* User's manual states max MTU should be 2048 but due to the
3248 * alignment calculations in sh_eth_ring_init() the practical
3249 * MTU is a bit less. Maybe this can be optimized some more.
3251 ndev
->max_mtu
= 2000 - (ETH_HLEN
+ VLAN_HLEN
+ ETH_FCS_LEN
);
3252 ndev
->min_mtu
= ETH_MIN_MTU
;
3254 if (mdp
->cd
->rx_csum
) {
3255 ndev
->features
= NETIF_F_RXCSUM
;
3256 ndev
->hw_features
= NETIF_F_RXCSUM
;
3261 ndev
->netdev_ops
= &sh_eth_netdev_ops_tsu
;
3263 ndev
->netdev_ops
= &sh_eth_netdev_ops
;
3264 ndev
->ethtool_ops
= &sh_eth_ethtool_ops
;
3265 ndev
->watchdog_timeo
= TX_TIMEOUT
;
3267 /* debug message level */
3268 mdp
->msg_enable
= SH_ETH_DEF_MSG_ENABLE
;
3270 /* read and set MAC address */
3271 read_mac_address(ndev
, pd
->mac_addr
);
3272 if (!is_valid_ether_addr(ndev
->dev_addr
)) {
3273 dev_warn(&pdev
->dev
,
3274 "no valid MAC address supplied, using a random one.\n");
3275 eth_hw_addr_random(ndev
);
3279 int port
= pdev
->id
< 0 ? 0 : pdev
->id
% 2;
3280 struct resource
*rtsu
;
3282 rtsu
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
3284 dev_err(&pdev
->dev
, "no TSU resource\n");
3288 /* We can only request the TSU region for the first port
3289 * of the two sharing this TSU for the probe to succeed...
3292 !devm_request_mem_region(&pdev
->dev
, rtsu
->start
,
3293 resource_size(rtsu
),
3294 dev_name(&pdev
->dev
))) {
3295 dev_err(&pdev
->dev
, "can't request TSU resource.\n");
3299 /* ioremap the TSU registers */
3300 mdp
->tsu_addr
= devm_ioremap(&pdev
->dev
, rtsu
->start
,
3301 resource_size(rtsu
));
3302 if (!mdp
->tsu_addr
) {
3303 dev_err(&pdev
->dev
, "TSU region ioremap() failed.\n");
3308 ndev
->features
|= NETIF_F_HW_VLAN_CTAG_FILTER
;
3310 /* Need to init only the first port of the two sharing a TSU */
3312 if (mdp
->cd
->chip_reset
)
3313 mdp
->cd
->chip_reset(ndev
);
3315 /* TSU init (Init only)*/
3316 sh_eth_tsu_init(mdp
);
3320 if (mdp
->cd
->rmiimode
)
3321 sh_eth_write(ndev
, 0x1, RMIIMODE
);
3324 ret
= sh_mdio_init(mdp
, pd
);
3326 if (ret
!= -EPROBE_DEFER
)
3327 dev_err(&pdev
->dev
, "MDIO init failed: %d\n", ret
);
3331 netif_napi_add(ndev
, &mdp
->napi
, sh_eth_poll
, 64);
3333 /* network device register */
3334 ret
= register_netdev(ndev
);
3339 device_set_wakeup_capable(&pdev
->dev
, 1);
3341 /* print device information */
3342 netdev_info(ndev
, "Base address at 0x%x, %pM, IRQ %d.\n",
3343 (u32
)ndev
->base_addr
, ndev
->dev_addr
, ndev
->irq
);
3345 pm_runtime_put(&pdev
->dev
);
3346 platform_set_drvdata(pdev
, ndev
);
3351 netif_napi_del(&mdp
->napi
);
3352 sh_mdio_release(mdp
);
3358 pm_runtime_put(&pdev
->dev
);
3359 pm_runtime_disable(&pdev
->dev
);
3363 static int sh_eth_drv_remove(struct platform_device
*pdev
)
3365 struct net_device
*ndev
= platform_get_drvdata(pdev
);
3366 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
3368 unregister_netdev(ndev
);
3369 netif_napi_del(&mdp
->napi
);
3370 sh_mdio_release(mdp
);
3371 pm_runtime_disable(&pdev
->dev
);
3378 #ifdef CONFIG_PM_SLEEP
3379 static int sh_eth_wol_setup(struct net_device
*ndev
)
3381 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
3383 /* Only allow ECI interrupts */
3384 synchronize_irq(ndev
->irq
);
3385 napi_disable(&mdp
->napi
);
3386 sh_eth_write(ndev
, EESIPR_ECIIP
, EESIPR
);
3388 /* Enable MagicPacket */
3389 sh_eth_modify(ndev
, ECMR
, ECMR_MPDE
, ECMR_MPDE
);
3391 return enable_irq_wake(ndev
->irq
);
3394 static int sh_eth_wol_restore(struct net_device
*ndev
)
3396 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
3399 napi_enable(&mdp
->napi
);
3401 /* Disable MagicPacket */
3402 sh_eth_modify(ndev
, ECMR
, ECMR_MPDE
, 0);
3404 /* The device needs to be reset to restore MagicPacket logic
3405 * for next wakeup. If we close and open the device it will
3406 * both be reset and all registers restored. This is what
3407 * happens during suspend and resume without WoL enabled.
3409 ret
= sh_eth_close(ndev
);
3412 ret
= sh_eth_open(ndev
);
3416 return disable_irq_wake(ndev
->irq
);
3419 static int sh_eth_suspend(struct device
*dev
)
3421 struct net_device
*ndev
= dev_get_drvdata(dev
);
3422 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
3425 if (!netif_running(ndev
))
3428 netif_device_detach(ndev
);
3430 if (mdp
->wol_enabled
)
3431 ret
= sh_eth_wol_setup(ndev
);
3433 ret
= sh_eth_close(ndev
);
3438 static int sh_eth_resume(struct device
*dev
)
3440 struct net_device
*ndev
= dev_get_drvdata(dev
);
3441 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
3444 if (!netif_running(ndev
))
3447 if (mdp
->wol_enabled
)
3448 ret
= sh_eth_wol_restore(ndev
);
3450 ret
= sh_eth_open(ndev
);
3455 netif_device_attach(ndev
);
3461 static int sh_eth_runtime_nop(struct device
*dev
)
3463 /* Runtime PM callback shared between ->runtime_suspend()
3464 * and ->runtime_resume(). Simply returns success.
3466 * This driver re-initializes all registers after
3467 * pm_runtime_get_sync() anyway so there is no need
3468 * to save and restore registers here.
3473 static const struct dev_pm_ops sh_eth_dev_pm_ops
= {
3474 SET_SYSTEM_SLEEP_PM_OPS(sh_eth_suspend
, sh_eth_resume
)
3475 SET_RUNTIME_PM_OPS(sh_eth_runtime_nop
, sh_eth_runtime_nop
, NULL
)
3477 #define SH_ETH_PM_OPS (&sh_eth_dev_pm_ops)
3479 #define SH_ETH_PM_OPS NULL
3482 static const struct platform_device_id sh_eth_id_table
[] = {
3483 { "sh7619-ether", (kernel_ulong_t
)&sh7619_data
},
3484 { "sh771x-ether", (kernel_ulong_t
)&sh771x_data
},
3485 { "sh7724-ether", (kernel_ulong_t
)&sh7724_data
},
3486 { "sh7734-gether", (kernel_ulong_t
)&sh7734_data
},
3487 { "sh7757-ether", (kernel_ulong_t
)&sh7757_data
},
3488 { "sh7757-gether", (kernel_ulong_t
)&sh7757_data_giga
},
3489 { "sh7763-gether", (kernel_ulong_t
)&sh7763_data
},
3492 MODULE_DEVICE_TABLE(platform
, sh_eth_id_table
);
3494 static struct platform_driver sh_eth_driver
= {
3495 .probe
= sh_eth_drv_probe
,
3496 .remove
= sh_eth_drv_remove
,
3497 .id_table
= sh_eth_id_table
,
3500 .pm
= SH_ETH_PM_OPS
,
3501 .of_match_table
= of_match_ptr(sh_eth_match_table
),
3505 module_platform_driver(sh_eth_driver
);
3507 MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
3508 MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
3509 MODULE_LICENSE("GPL v2");