1 /* SuperH Ethernet device driver
3 * Copyright (C) 2014 Renesas Electronics Corporation
4 * Copyright (C) 2006-2012 Nobuhiro Iwamatsu
5 * Copyright (C) 2008-2014 Renesas Solutions Corp.
6 * Copyright (C) 2013-2017 Cogent Embedded, Inc.
7 * Copyright (C) 2014 Codethink Limited
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms and conditions of the GNU General Public License,
11 * version 2, as published by the Free Software Foundation.
13 * This program is distributed in the hope it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/spinlock.h>
25 #include <linux/interrupt.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/etherdevice.h>
28 #include <linux/delay.h>
29 #include <linux/platform_device.h>
30 #include <linux/mdio-bitbang.h>
31 #include <linux/netdevice.h>
33 #include <linux/of_device.h>
34 #include <linux/of_irq.h>
35 #include <linux/of_net.h>
36 #include <linux/phy.h>
37 #include <linux/cache.h>
39 #include <linux/pm_runtime.h>
40 #include <linux/slab.h>
41 #include <linux/ethtool.h>
42 #include <linux/if_vlan.h>
43 #include <linux/sh_eth.h>
44 #include <linux/of_mdio.h>
48 #define SH_ETH_DEF_MSG_ENABLE \
54 #define SH_ETH_OFFSET_INVALID ((u16)~0)
56 #define SH_ETH_OFFSET_DEFAULTS \
57 [0 ... SH_ETH_MAX_REGISTER_OFFSET - 1] = SH_ETH_OFFSET_INVALID
59 static const u16 sh_eth_offset_gigabit[SH_ETH_MAX_REGISTER_OFFSET] = {
60 SH_ETH_OFFSET_DEFAULTS,
115 [TSU_CTRST] = 0x0004,
116 [TSU_FWEN0] = 0x0010,
117 [TSU_FWEN1] = 0x0014,
119 [TSU_BSYSL0] = 0x0020,
120 [TSU_BSYSL1] = 0x0024,
121 [TSU_PRISL0] = 0x0028,
122 [TSU_PRISL1] = 0x002c,
123 [TSU_FWSL0] = 0x0030,
124 [TSU_FWSL1] = 0x0034,
125 [TSU_FWSLC] = 0x0038,
126 [TSU_QTAG0] = 0x0040,
127 [TSU_QTAG1] = 0x0044,
129 [TSU_FWINMK] = 0x0054,
130 [TSU_ADQT0] = 0x0048,
131 [TSU_ADQT1] = 0x004c,
132 [TSU_VTAG0] = 0x0058,
133 [TSU_VTAG1] = 0x005c,
134 [TSU_ADSBSY] = 0x0060,
136 [TSU_POST1] = 0x0070,
137 [TSU_POST2] = 0x0074,
138 [TSU_POST3] = 0x0078,
139 [TSU_POST4] = 0x007c,
140 [TSU_ADRH0] = 0x0100,
156 static const u16 sh_eth_offset_fast_rz[SH_ETH_MAX_REGISTER_OFFSET] = {
157 SH_ETH_OFFSET_DEFAULTS,
202 [TSU_CTRST] = 0x0004,
203 [TSU_FWSLC] = 0x0038,
204 [TSU_VTAG0] = 0x0058,
205 [TSU_ADSBSY] = 0x0060,
207 [TSU_POST1] = 0x0070,
208 [TSU_POST2] = 0x0074,
209 [TSU_POST3] = 0x0078,
210 [TSU_POST4] = 0x007c,
211 [TSU_ADRH0] = 0x0100,
219 static const u16 sh_eth_offset_fast_rcar[SH_ETH_MAX_REGISTER_OFFSET] = {
220 SH_ETH_OFFSET_DEFAULTS,
267 static const u16 sh_eth_offset_fast_sh4[SH_ETH_MAX_REGISTER_OFFSET] = {
268 SH_ETH_OFFSET_DEFAULTS,
321 static const u16 sh_eth_offset_fast_sh3_sh2[SH_ETH_MAX_REGISTER_OFFSET] = {
322 SH_ETH_OFFSET_DEFAULTS,
370 [TSU_CTRST] = 0x0004,
371 [TSU_FWEN0] = 0x0010,
372 [TSU_FWEN1] = 0x0014,
374 [TSU_BSYSL0] = 0x0020,
375 [TSU_BSYSL1] = 0x0024,
376 [TSU_PRISL0] = 0x0028,
377 [TSU_PRISL1] = 0x002c,
378 [TSU_FWSL0] = 0x0030,
379 [TSU_FWSL1] = 0x0034,
380 [TSU_FWSLC] = 0x0038,
381 [TSU_QTAGM0] = 0x0040,
382 [TSU_QTAGM1] = 0x0044,
383 [TSU_ADQT0] = 0x0048,
384 [TSU_ADQT1] = 0x004c,
386 [TSU_FWINMK] = 0x0054,
387 [TSU_ADSBSY] = 0x0060,
389 [TSU_POST1] = 0x0070,
390 [TSU_POST2] = 0x0074,
391 [TSU_POST3] = 0x0078,
392 [TSU_POST4] = 0x007c,
407 [TSU_ADRH0] = 0x0100,
410 static void sh_eth_rcv_snd_disable(struct net_device *ndev);
411 static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev);
413 static void sh_eth_write(struct net_device *ndev, u32 data, 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 iowrite32(data, mdp->addr + offset);
424 static u32 sh_eth_read(struct net_device *ndev, int enum_index)
426 struct sh_eth_private *mdp = netdev_priv(ndev);
427 u16 offset = mdp->reg_offset[enum_index];
429 if (WARN_ON(offset == SH_ETH_OFFSET_INVALID))
432 return ioread32(mdp->addr + offset);
435 static void sh_eth_modify(struct net_device *ndev, int enum_index, u32 clear,
438 sh_eth_write(ndev, (sh_eth_read(ndev, enum_index) & ~clear) | set,
442 static bool sh_eth_is_gether(struct sh_eth_private *mdp)
444 return mdp->reg_offset == sh_eth_offset_gigabit;
447 static bool sh_eth_is_rz_fast_ether(struct sh_eth_private *mdp)
449 return mdp->reg_offset == sh_eth_offset_fast_rz;
452 static void sh_eth_select_mii(struct net_device *ndev)
454 struct sh_eth_private *mdp = netdev_priv(ndev);
457 switch (mdp->phy_interface) {
458 case PHY_INTERFACE_MODE_GMII:
461 case PHY_INTERFACE_MODE_MII:
464 case PHY_INTERFACE_MODE_RMII:
469 "PHY interface mode was not setup. Set to MII.\n");
474 sh_eth_write(ndev, value, RMII_MII);
477 static void sh_eth_set_duplex(struct net_device *ndev)
479 struct sh_eth_private *mdp = netdev_priv(ndev);
481 sh_eth_modify(ndev, ECMR, ECMR_DM, mdp->duplex ? ECMR_DM : 0);
484 static void sh_eth_chip_reset(struct net_device *ndev)
486 struct sh_eth_private *mdp = netdev_priv(ndev);
489 sh_eth_tsu_write(mdp, ARSTR_ARST, ARSTR);
493 static void sh_eth_set_rate_gether(struct net_device *ndev)
495 struct sh_eth_private *mdp = netdev_priv(ndev);
497 switch (mdp->speed) {
498 case 10: /* 10BASE */
499 sh_eth_write(ndev, GECMR_10, GECMR);
501 case 100:/* 100BASE */
502 sh_eth_write(ndev, GECMR_100, GECMR);
504 case 1000: /* 1000BASE */
505 sh_eth_write(ndev, GECMR_1000, GECMR);
512 static struct sh_eth_cpu_data r7s72100_data = {
513 .chip_reset = sh_eth_chip_reset,
514 .set_duplex = sh_eth_set_duplex,
516 .register_type = SH_ETH_REG_FAST_RZ,
518 .ecsr_value = ECSR_ICD,
519 .ecsipr_value = ECSIPR_ICDIP,
520 .eesipr_value = EESIPR_TWB1IP | EESIPR_TWBIP | EESIPR_TC1IP |
521 EESIPR_TABTIP | EESIPR_RABTIP | EESIPR_RFCOFIP |
523 EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
524 EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
525 EESIPR_RMAFIP | EESIPR_RRFIP |
526 EESIPR_RTLFIP | EESIPR_RTSFIP |
527 EESIPR_PREIP | EESIPR_CERFIP,
529 .tx_check = EESR_TC1 | EESR_FTC,
530 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
531 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
533 .fdr_value = 0x0000070f,
541 .rpadir_value = 2 << 16,
548 static void sh_eth_chip_reset_r8a7740(struct net_device *ndev)
550 sh_eth_chip_reset(ndev);
552 sh_eth_select_mii(ndev);
556 static struct sh_eth_cpu_data r8a7740_data = {
557 .chip_reset = sh_eth_chip_reset_r8a7740,
558 .set_duplex = sh_eth_set_duplex,
559 .set_rate = sh_eth_set_rate_gether,
561 .register_type = SH_ETH_REG_GIGABIT,
563 .ecsr_value = ECSR_ICD | ECSR_MPD,
564 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
565 .eesipr_value = EESIPR_RFCOFIP | EESIPR_ECIIP |
566 EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
567 EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
568 0x0000f000 | EESIPR_CNDIP | EESIPR_DLCIP |
569 EESIPR_CDIP | EESIPR_TROIP | EESIPR_RMAFIP |
570 EESIPR_CEEFIP | EESIPR_CELFIP |
571 EESIPR_RRFIP | EESIPR_RTLFIP | EESIPR_RTSFIP |
572 EESIPR_PREIP | EESIPR_CERFIP,
574 .tx_check = EESR_TC1 | EESR_FTC,
575 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
576 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
578 .fdr_value = 0x0000070f,
586 .rpadir_value = 2 << 16,
595 /* There is CPU dependent code */
596 static void sh_eth_set_rate_rcar(struct net_device *ndev)
598 struct sh_eth_private *mdp = netdev_priv(ndev);
600 switch (mdp->speed) {
601 case 10: /* 10BASE */
602 sh_eth_modify(ndev, ECMR, ECMR_ELB, 0);
604 case 100:/* 100BASE */
605 sh_eth_modify(ndev, ECMR, ECMR_ELB, ECMR_ELB);
611 static struct sh_eth_cpu_data rcar_gen1_data = {
612 .set_duplex = sh_eth_set_duplex,
613 .set_rate = sh_eth_set_rate_rcar,
615 .register_type = SH_ETH_REG_FAST_RCAR,
617 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
618 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
619 .eesipr_value = EESIPR_RFCOFIP | EESIPR_ADEIP | EESIPR_ECIIP |
620 EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
621 EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
622 EESIPR_RMAFIP | EESIPR_RRFIP |
623 EESIPR_RTLFIP | EESIPR_RTSFIP |
624 EESIPR_PREIP | EESIPR_CERFIP,
626 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
627 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
628 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE,
629 .fdr_value = 0x00000f0f,
637 /* R-Car Gen2 and RZ/G1 */
638 static struct sh_eth_cpu_data rcar_gen2_data = {
639 .set_duplex = sh_eth_set_duplex,
640 .set_rate = sh_eth_set_rate_rcar,
642 .register_type = SH_ETH_REG_FAST_RCAR,
644 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD | ECSR_MPD,
645 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP |
647 .eesipr_value = EESIPR_RFCOFIP | EESIPR_ADEIP | EESIPR_ECIIP |
648 EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
649 EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
650 EESIPR_RMAFIP | EESIPR_RRFIP |
651 EESIPR_RTLFIP | EESIPR_RTSFIP |
652 EESIPR_PREIP | EESIPR_CERFIP,
654 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
655 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
656 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE,
657 .fdr_value = 0x00000f0f,
659 .trscer_err_mask = DESC_I_RINT8,
668 #endif /* CONFIG_OF */
670 static void sh_eth_set_rate_sh7724(struct net_device *ndev)
672 struct sh_eth_private *mdp = netdev_priv(ndev);
674 switch (mdp->speed) {
675 case 10: /* 10BASE */
676 sh_eth_modify(ndev, ECMR, ECMR_RTM, 0);
678 case 100:/* 100BASE */
679 sh_eth_modify(ndev, ECMR, ECMR_RTM, ECMR_RTM);
685 static struct sh_eth_cpu_data sh7724_data = {
686 .set_duplex = sh_eth_set_duplex,
687 .set_rate = sh_eth_set_rate_sh7724,
689 .register_type = SH_ETH_REG_FAST_SH4,
691 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
692 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
693 .eesipr_value = EESIPR_RFCOFIP | EESIPR_ADEIP | EESIPR_ECIIP |
694 EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
695 EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
696 EESIPR_RMAFIP | EESIPR_RRFIP |
697 EESIPR_RTLFIP | EESIPR_RTSFIP |
698 EESIPR_PREIP | EESIPR_CERFIP,
700 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
701 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
702 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE,
709 .rpadir_value = 0x00020000, /* NET_IP_ALIGN assumed to be 2 */
712 static void sh_eth_set_rate_sh7757(struct net_device *ndev)
714 struct sh_eth_private *mdp = netdev_priv(ndev);
716 switch (mdp->speed) {
717 case 10: /* 10BASE */
718 sh_eth_write(ndev, 0, RTRATE);
720 case 100:/* 100BASE */
721 sh_eth_write(ndev, 1, RTRATE);
727 static struct sh_eth_cpu_data sh7757_data = {
728 .set_duplex = sh_eth_set_duplex,
729 .set_rate = sh_eth_set_rate_sh7757,
731 .register_type = SH_ETH_REG_FAST_SH4,
733 .eesipr_value = EESIPR_RFCOFIP | EESIPR_ECIIP |
734 EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
735 EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
736 0x0000f000 | EESIPR_CNDIP | EESIPR_DLCIP |
737 EESIPR_CDIP | EESIPR_TROIP | EESIPR_RMAFIP |
738 EESIPR_CEEFIP | EESIPR_CELFIP |
739 EESIPR_RRFIP | EESIPR_RTLFIP | EESIPR_RTSFIP |
740 EESIPR_PREIP | EESIPR_CERFIP,
742 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
743 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
744 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE,
746 .irq_flags = IRQF_SHARED,
753 .rpadir_value = 2 << 16,
757 #define SH_GIGA_ETH_BASE 0xfee00000UL
758 #define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8)
759 #define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0)
760 static void sh_eth_chip_reset_giga(struct net_device *ndev)
762 u32 mahr[2], malr[2];
765 /* save MAHR and MALR */
766 for (i = 0; i < 2; i++) {
767 malr[i] = ioread32((void *)GIGA_MALR(i));
768 mahr[i] = ioread32((void *)GIGA_MAHR(i));
771 sh_eth_chip_reset(ndev);
773 /* restore MAHR and MALR */
774 for (i = 0; i < 2; i++) {
775 iowrite32(malr[i], (void *)GIGA_MALR(i));
776 iowrite32(mahr[i], (void *)GIGA_MAHR(i));
780 static void sh_eth_set_rate_giga(struct net_device *ndev)
782 struct sh_eth_private *mdp = netdev_priv(ndev);
784 switch (mdp->speed) {
785 case 10: /* 10BASE */
786 sh_eth_write(ndev, 0x00000000, GECMR);
788 case 100:/* 100BASE */
789 sh_eth_write(ndev, 0x00000010, GECMR);
791 case 1000: /* 1000BASE */
792 sh_eth_write(ndev, 0x00000020, GECMR);
797 /* SH7757(GETHERC) */
798 static struct sh_eth_cpu_data sh7757_data_giga = {
799 .chip_reset = sh_eth_chip_reset_giga,
800 .set_duplex = sh_eth_set_duplex,
801 .set_rate = sh_eth_set_rate_giga,
803 .register_type = SH_ETH_REG_GIGABIT,
805 .ecsr_value = ECSR_ICD | ECSR_MPD,
806 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
807 .eesipr_value = EESIPR_RFCOFIP | EESIPR_ECIIP |
808 EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
809 EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
810 0x0000f000 | EESIPR_CNDIP | EESIPR_DLCIP |
811 EESIPR_CDIP | EESIPR_TROIP | EESIPR_RMAFIP |
812 EESIPR_CEEFIP | EESIPR_CELFIP |
813 EESIPR_RRFIP | EESIPR_RTLFIP | EESIPR_RTSFIP |
814 EESIPR_PREIP | EESIPR_CERFIP,
816 .tx_check = EESR_TC1 | EESR_FTC,
817 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
818 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
820 .fdr_value = 0x0000072f,
822 .irq_flags = IRQF_SHARED,
829 .rpadir_value = 2 << 16,
836 static struct sh_eth_cpu_data sh7734_data = {
837 .chip_reset = sh_eth_chip_reset,
838 .set_duplex = sh_eth_set_duplex,
839 .set_rate = sh_eth_set_rate_gether,
841 .register_type = SH_ETH_REG_GIGABIT,
843 .ecsr_value = ECSR_ICD | ECSR_MPD,
844 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
845 .eesipr_value = EESIPR_RFCOFIP | EESIPR_ECIIP |
846 EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
847 EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
848 EESIPR_DLCIP | EESIPR_CDIP | EESIPR_TROIP |
849 EESIPR_RMAFIP | EESIPR_CEEFIP | EESIPR_CELFIP |
850 EESIPR_RRFIP | EESIPR_RTLFIP | EESIPR_RTSFIP |
851 EESIPR_PREIP | EESIPR_CERFIP,
853 .tx_check = EESR_TC1 | EESR_FTC,
854 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
855 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
872 static struct sh_eth_cpu_data sh7763_data = {
873 .chip_reset = sh_eth_chip_reset,
874 .set_duplex = sh_eth_set_duplex,
875 .set_rate = sh_eth_set_rate_gether,
877 .register_type = SH_ETH_REG_GIGABIT,
879 .ecsr_value = ECSR_ICD | ECSR_MPD,
880 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
881 .eesipr_value = EESIPR_RFCOFIP | EESIPR_ECIIP |
882 EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
883 EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
884 EESIPR_DLCIP | EESIPR_CDIP | EESIPR_TROIP |
885 EESIPR_RMAFIP | EESIPR_CEEFIP | EESIPR_CELFIP |
886 EESIPR_RRFIP | EESIPR_RTLFIP | EESIPR_RTSFIP |
887 EESIPR_PREIP | EESIPR_CERFIP,
889 .tx_check = EESR_TC1 | EESR_FTC,
890 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
891 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE,
901 .irq_flags = IRQF_SHARED,
905 static struct sh_eth_cpu_data sh7619_data = {
906 .register_type = SH_ETH_REG_FAST_SH3_SH2,
908 .eesipr_value = EESIPR_RFCOFIP | EESIPR_ECIIP |
909 EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
910 EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
911 0x0000f000 | EESIPR_CNDIP | EESIPR_DLCIP |
912 EESIPR_CDIP | EESIPR_TROIP | EESIPR_RMAFIP |
913 EESIPR_CEEFIP | EESIPR_CELFIP |
914 EESIPR_RRFIP | EESIPR_RTLFIP | EESIPR_RTSFIP |
915 EESIPR_PREIP | EESIPR_CERFIP,
923 static struct sh_eth_cpu_data sh771x_data = {
924 .register_type = SH_ETH_REG_FAST_SH3_SH2,
926 .eesipr_value = EESIPR_RFCOFIP | EESIPR_ECIIP |
927 EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
928 EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
929 0x0000f000 | EESIPR_CNDIP | EESIPR_DLCIP |
930 EESIPR_CDIP | EESIPR_TROIP | EESIPR_RMAFIP |
931 EESIPR_CEEFIP | EESIPR_CELFIP |
932 EESIPR_RRFIP | EESIPR_RTLFIP | EESIPR_RTSFIP |
933 EESIPR_PREIP | EESIPR_CERFIP,
937 static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data *cd)
940 cd->ecsr_value = DEFAULT_ECSR_INIT;
942 if (!cd->ecsipr_value)
943 cd->ecsipr_value = DEFAULT_ECSIPR_INIT;
945 if (!cd->fcftr_value)
946 cd->fcftr_value = DEFAULT_FIFO_F_D_RFF |
947 DEFAULT_FIFO_F_D_RFD;
950 cd->fdr_value = DEFAULT_FDR_INIT;
953 cd->tx_check = DEFAULT_TX_CHECK;
955 if (!cd->eesr_err_check)
956 cd->eesr_err_check = DEFAULT_EESR_ERR_CHECK;
958 if (!cd->trscer_err_mask)
959 cd->trscer_err_mask = DEFAULT_TRSCER_ERR_MASK;
962 static int sh_eth_check_reset(struct net_device *ndev)
966 for (cnt = 100; cnt > 0; cnt--) {
967 if (!(sh_eth_read(ndev, EDMR) & EDMR_SRST_GETHER))
972 netdev_err(ndev, "Device reset failed\n");
976 static int sh_eth_reset(struct net_device *ndev)
978 struct sh_eth_private *mdp = netdev_priv(ndev);
981 if (sh_eth_is_gether(mdp) || sh_eth_is_rz_fast_ether(mdp)) {
982 sh_eth_write(ndev, EDSR_ENALL, EDSR);
983 sh_eth_modify(ndev, EDMR, EDMR_SRST_GETHER, EDMR_SRST_GETHER);
985 ret = sh_eth_check_reset(ndev);
990 sh_eth_write(ndev, 0x0, TDLAR);
991 sh_eth_write(ndev, 0x0, TDFAR);
992 sh_eth_write(ndev, 0x0, TDFXR);
993 sh_eth_write(ndev, 0x0, TDFFR);
994 sh_eth_write(ndev, 0x0, RDLAR);
995 sh_eth_write(ndev, 0x0, RDFAR);
996 sh_eth_write(ndev, 0x0, RDFXR);
997 sh_eth_write(ndev, 0x0, RDFFR);
999 /* Reset HW CRC register */
1000 if (mdp->cd->hw_checksum)
1001 sh_eth_write(ndev, 0x0, CSMR);
1003 /* Select MII mode */
1004 if (mdp->cd->select_mii)
1005 sh_eth_select_mii(ndev);
1007 sh_eth_modify(ndev, EDMR, EDMR_SRST_ETHER, EDMR_SRST_ETHER);
1009 sh_eth_modify(ndev, EDMR, EDMR_SRST_ETHER, 0);
1015 static void sh_eth_set_receive_align(struct sk_buff *skb)
1017 uintptr_t reserve = (uintptr_t)skb->data & (SH_ETH_RX_ALIGN - 1);
1020 skb_reserve(skb, SH_ETH_RX_ALIGN - reserve);
1023 /* Program the hardware MAC address from dev->dev_addr. */
1024 static void update_mac_address(struct net_device *ndev)
1027 (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
1028 (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
1030 (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
1033 /* Get MAC address from SuperH MAC address register
1035 * SuperH's Ethernet device doesn't have 'ROM' to MAC address.
1036 * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g).
1037 * When you want use this device, you must set MAC address in bootloader.
1040 static void read_mac_address(struct net_device *ndev, unsigned char *mac)
1042 if (mac[0] || mac[1] || mac[2] || mac[3] || mac[4] || mac[5]) {
1043 memcpy(ndev->dev_addr, mac, ETH_ALEN);
1045 u32 mahr = sh_eth_read(ndev, MAHR);
1046 u32 malr = sh_eth_read(ndev, MALR);
1048 ndev->dev_addr[0] = (mahr >> 24) & 0xFF;
1049 ndev->dev_addr[1] = (mahr >> 16) & 0xFF;
1050 ndev->dev_addr[2] = (mahr >> 8) & 0xFF;
1051 ndev->dev_addr[3] = (mahr >> 0) & 0xFF;
1052 ndev->dev_addr[4] = (malr >> 8) & 0xFF;
1053 ndev->dev_addr[5] = (malr >> 0) & 0xFF;
1057 static u32 sh_eth_get_edtrr_trns(struct sh_eth_private *mdp)
1059 if (sh_eth_is_gether(mdp) || sh_eth_is_rz_fast_ether(mdp))
1060 return EDTRR_TRNS_GETHER;
1062 return EDTRR_TRNS_ETHER;
1066 void (*set_gate)(void *addr);
1067 struct mdiobb_ctrl ctrl;
1071 static void sh_mdio_ctrl(struct mdiobb_ctrl *ctrl, u32 mask, int set)
1073 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
1076 if (bitbang->set_gate)
1077 bitbang->set_gate(bitbang->addr);
1079 pir = ioread32(bitbang->addr);
1084 iowrite32(pir, bitbang->addr);
1087 /* Data I/O pin control */
1088 static void sh_mmd_ctrl(struct mdiobb_ctrl *ctrl, int bit)
1090 sh_mdio_ctrl(ctrl, PIR_MMD, bit);
1094 static void sh_set_mdio(struct mdiobb_ctrl *ctrl, int bit)
1096 sh_mdio_ctrl(ctrl, PIR_MDO, bit);
1100 static int sh_get_mdio(struct mdiobb_ctrl *ctrl)
1102 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
1104 if (bitbang->set_gate)
1105 bitbang->set_gate(bitbang->addr);
1107 return (ioread32(bitbang->addr) & PIR_MDI) != 0;
1110 /* MDC pin control */
1111 static void sh_mdc_ctrl(struct mdiobb_ctrl *ctrl, int bit)
1113 sh_mdio_ctrl(ctrl, PIR_MDC, bit);
1116 /* mdio bus control struct */
1117 static struct mdiobb_ops bb_ops = {
1118 .owner = THIS_MODULE,
1119 .set_mdc = sh_mdc_ctrl,
1120 .set_mdio_dir = sh_mmd_ctrl,
1121 .set_mdio_data = sh_set_mdio,
1122 .get_mdio_data = sh_get_mdio,
1125 /* free Tx skb function */
1126 static int sh_eth_tx_free(struct net_device *ndev, bool sent_only)
1128 struct sh_eth_private *mdp = netdev_priv(ndev);
1129 struct sh_eth_txdesc *txdesc;
1134 for (; mdp->cur_tx - mdp->dirty_tx > 0; mdp->dirty_tx++) {
1135 entry = mdp->dirty_tx % mdp->num_tx_ring;
1136 txdesc = &mdp->tx_ring[entry];
1137 sent = !(txdesc->status & cpu_to_le32(TD_TACT));
1138 if (sent_only && !sent)
1140 /* TACT bit must be checked before all the following reads */
1142 netif_info(mdp, tx_done, ndev,
1143 "tx entry %d status 0x%08x\n",
1144 entry, le32_to_cpu(txdesc->status));
1145 /* Free the original skb. */
1146 if (mdp->tx_skbuff[entry]) {
1147 dma_unmap_single(&mdp->pdev->dev,
1148 le32_to_cpu(txdesc->addr),
1149 le32_to_cpu(txdesc->len) >> 16,
1151 dev_kfree_skb_irq(mdp->tx_skbuff[entry]);
1152 mdp->tx_skbuff[entry] = NULL;
1155 txdesc->status = cpu_to_le32(TD_TFP);
1156 if (entry >= mdp->num_tx_ring - 1)
1157 txdesc->status |= cpu_to_le32(TD_TDLE);
1160 ndev->stats.tx_packets++;
1161 ndev->stats.tx_bytes += le32_to_cpu(txdesc->len) >> 16;
1167 /* free skb and descriptor buffer */
1168 static void sh_eth_ring_free(struct net_device *ndev)
1170 struct sh_eth_private *mdp = netdev_priv(ndev);
1174 for (i = 0; i < mdp->num_rx_ring; i++) {
1175 if (mdp->rx_skbuff[i]) {
1176 struct sh_eth_rxdesc *rxdesc = &mdp->rx_ring[i];
1178 dma_unmap_single(&mdp->pdev->dev,
1179 le32_to_cpu(rxdesc->addr),
1180 ALIGN(mdp->rx_buf_sz, 32),
1184 ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
1185 dma_free_coherent(&mdp->pdev->dev, ringsize, mdp->rx_ring,
1187 mdp->rx_ring = NULL;
1190 /* Free Rx skb ringbuffer */
1191 if (mdp->rx_skbuff) {
1192 for (i = 0; i < mdp->num_rx_ring; i++)
1193 dev_kfree_skb(mdp->rx_skbuff[i]);
1195 kfree(mdp->rx_skbuff);
1196 mdp->rx_skbuff = NULL;
1199 sh_eth_tx_free(ndev, false);
1201 ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
1202 dma_free_coherent(&mdp->pdev->dev, ringsize, mdp->tx_ring,
1204 mdp->tx_ring = NULL;
1207 /* Free Tx skb ringbuffer */
1208 kfree(mdp->tx_skbuff);
1209 mdp->tx_skbuff = NULL;
1212 /* format skb and descriptor buffer */
1213 static void sh_eth_ring_format(struct net_device *ndev)
1215 struct sh_eth_private *mdp = netdev_priv(ndev);
1217 struct sk_buff *skb;
1218 struct sh_eth_rxdesc *rxdesc = NULL;
1219 struct sh_eth_txdesc *txdesc = NULL;
1220 int rx_ringsize = sizeof(*rxdesc) * mdp->num_rx_ring;
1221 int tx_ringsize = sizeof(*txdesc) * mdp->num_tx_ring;
1222 int skbuff_size = mdp->rx_buf_sz + SH_ETH_RX_ALIGN + 32 - 1;
1223 dma_addr_t dma_addr;
1231 memset(mdp->rx_ring, 0, rx_ringsize);
1233 /* build Rx ring buffer */
1234 for (i = 0; i < mdp->num_rx_ring; i++) {
1236 mdp->rx_skbuff[i] = NULL;
1237 skb = netdev_alloc_skb(ndev, skbuff_size);
1240 sh_eth_set_receive_align(skb);
1242 /* The size of the buffer is a multiple of 32 bytes. */
1243 buf_len = ALIGN(mdp->rx_buf_sz, 32);
1244 dma_addr = dma_map_single(&mdp->pdev->dev, skb->data, buf_len,
1246 if (dma_mapping_error(&mdp->pdev->dev, dma_addr)) {
1250 mdp->rx_skbuff[i] = skb;
1253 rxdesc = &mdp->rx_ring[i];
1254 rxdesc->len = cpu_to_le32(buf_len << 16);
1255 rxdesc->addr = cpu_to_le32(dma_addr);
1256 rxdesc->status = cpu_to_le32(RD_RACT | RD_RFP);
1258 /* Rx descriptor address set */
1260 sh_eth_write(ndev, mdp->rx_desc_dma, RDLAR);
1261 if (sh_eth_is_gether(mdp) ||
1262 sh_eth_is_rz_fast_ether(mdp))
1263 sh_eth_write(ndev, mdp->rx_desc_dma, RDFAR);
1267 mdp->dirty_rx = (u32) (i - mdp->num_rx_ring);
1269 /* Mark the last entry as wrapping the ring. */
1271 rxdesc->status |= cpu_to_le32(RD_RDLE);
1273 memset(mdp->tx_ring, 0, tx_ringsize);
1275 /* build Tx ring buffer */
1276 for (i = 0; i < mdp->num_tx_ring; i++) {
1277 mdp->tx_skbuff[i] = NULL;
1278 txdesc = &mdp->tx_ring[i];
1279 txdesc->status = cpu_to_le32(TD_TFP);
1280 txdesc->len = cpu_to_le32(0);
1282 /* Tx descriptor address set */
1283 sh_eth_write(ndev, mdp->tx_desc_dma, TDLAR);
1284 if (sh_eth_is_gether(mdp) ||
1285 sh_eth_is_rz_fast_ether(mdp))
1286 sh_eth_write(ndev, mdp->tx_desc_dma, TDFAR);
1290 txdesc->status |= cpu_to_le32(TD_TDLE);
1293 /* Get skb and descriptor buffer */
1294 static int sh_eth_ring_init(struct net_device *ndev)
1296 struct sh_eth_private *mdp = netdev_priv(ndev);
1297 int rx_ringsize, tx_ringsize;
1299 /* +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
1300 * card needs room to do 8 byte alignment, +2 so we can reserve
1301 * the first 2 bytes, and +16 gets room for the status word from the
1304 mdp->rx_buf_sz = (ndev->mtu <= 1492 ? PKT_BUF_SZ :
1305 (((ndev->mtu + 26 + 7) & ~7) + 2 + 16));
1306 if (mdp->cd->rpadir)
1307 mdp->rx_buf_sz += NET_IP_ALIGN;
1309 /* Allocate RX and TX skb rings */
1310 mdp->rx_skbuff = kcalloc(mdp->num_rx_ring, sizeof(*mdp->rx_skbuff),
1312 if (!mdp->rx_skbuff)
1315 mdp->tx_skbuff = kcalloc(mdp->num_tx_ring, sizeof(*mdp->tx_skbuff),
1317 if (!mdp->tx_skbuff)
1320 /* Allocate all Rx descriptors. */
1321 rx_ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
1322 mdp->rx_ring = dma_alloc_coherent(&mdp->pdev->dev, rx_ringsize,
1323 &mdp->rx_desc_dma, GFP_KERNEL);
1329 /* Allocate all Tx descriptors. */
1330 tx_ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
1331 mdp->tx_ring = dma_alloc_coherent(&mdp->pdev->dev, tx_ringsize,
1332 &mdp->tx_desc_dma, GFP_KERNEL);
1338 /* Free Rx and Tx skb ring buffer and DMA buffer */
1339 sh_eth_ring_free(ndev);
1344 static int sh_eth_dev_init(struct net_device *ndev)
1346 struct sh_eth_private *mdp = netdev_priv(ndev);
1350 ret = sh_eth_reset(ndev);
1354 if (mdp->cd->rmiimode)
1355 sh_eth_write(ndev, 0x1, RMIIMODE);
1357 /* Descriptor format */
1358 sh_eth_ring_format(ndev);
1359 if (mdp->cd->rpadir)
1360 sh_eth_write(ndev, mdp->cd->rpadir_value, RPADIR);
1362 /* all sh_eth int mask */
1363 sh_eth_write(ndev, 0, EESIPR);
1365 #if defined(__LITTLE_ENDIAN)
1366 if (mdp->cd->hw_swap)
1367 sh_eth_write(ndev, EDMR_EL, EDMR);
1370 sh_eth_write(ndev, 0, EDMR);
1373 sh_eth_write(ndev, mdp->cd->fdr_value, FDR);
1374 sh_eth_write(ndev, 0, TFTR);
1376 /* Frame recv control (enable multiple-packets per rx irq) */
1377 sh_eth_write(ndev, RMCR_RNC, RMCR);
1379 sh_eth_write(ndev, mdp->cd->trscer_err_mask, TRSCER);
1382 sh_eth_write(ndev, 0x800, BCULR); /* Burst sycle set */
1384 sh_eth_write(ndev, mdp->cd->fcftr_value, FCFTR);
1386 if (!mdp->cd->no_trimd)
1387 sh_eth_write(ndev, 0, TRIMD);
1389 /* Recv frame limit set register */
1390 sh_eth_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN,
1393 sh_eth_modify(ndev, EESR, 0, 0);
1394 mdp->irq_enabled = true;
1395 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
1397 /* PAUSE Prohibition */
1398 sh_eth_write(ndev, ECMR_ZPF | (mdp->duplex ? ECMR_DM : 0) |
1399 ECMR_TE | ECMR_RE, ECMR);
1401 if (mdp->cd->set_rate)
1402 mdp->cd->set_rate(ndev);
1404 /* E-MAC Status Register clear */
1405 sh_eth_write(ndev, mdp->cd->ecsr_value, ECSR);
1407 /* E-MAC Interrupt Enable register */
1408 sh_eth_write(ndev, mdp->cd->ecsipr_value, ECSIPR);
1410 /* Set MAC address */
1411 update_mac_address(ndev);
1415 sh_eth_write(ndev, APR_AP, APR);
1417 sh_eth_write(ndev, MPR_MP, MPR);
1418 if (mdp->cd->tpauser)
1419 sh_eth_write(ndev, TPAUSER_UNLIMITED, TPAUSER);
1421 /* Setting the Rx mode will start the Rx process. */
1422 sh_eth_write(ndev, EDRRR_R, EDRRR);
1427 static void sh_eth_dev_exit(struct net_device *ndev)
1429 struct sh_eth_private *mdp = netdev_priv(ndev);
1432 /* Deactivate all TX descriptors, so DMA should stop at next
1433 * packet boundary if it's currently running
1435 for (i = 0; i < mdp->num_tx_ring; i++)
1436 mdp->tx_ring[i].status &= ~cpu_to_le32(TD_TACT);
1438 /* Disable TX FIFO egress to MAC */
1439 sh_eth_rcv_snd_disable(ndev);
1441 /* Stop RX DMA at next packet boundary */
1442 sh_eth_write(ndev, 0, EDRRR);
1444 /* Aside from TX DMA, we can't tell when the hardware is
1445 * really stopped, so we need to reset to make sure.
1446 * Before doing that, wait for long enough to *probably*
1447 * finish transmitting the last packet and poll stats.
1449 msleep(2); /* max frame time at 10 Mbps < 1250 us */
1450 sh_eth_get_stats(ndev);
1453 /* Set MAC address again */
1454 update_mac_address(ndev);
1457 /* Packet receive function */
1458 static int sh_eth_rx(struct net_device *ndev, u32 intr_status, int *quota)
1460 struct sh_eth_private *mdp = netdev_priv(ndev);
1461 struct sh_eth_rxdesc *rxdesc;
1463 int entry = mdp->cur_rx % mdp->num_rx_ring;
1464 int boguscnt = (mdp->dirty_rx + mdp->num_rx_ring) - mdp->cur_rx;
1466 struct sk_buff *skb;
1468 int skbuff_size = mdp->rx_buf_sz + SH_ETH_RX_ALIGN + 32 - 1;
1469 dma_addr_t dma_addr;
1473 boguscnt = min(boguscnt, *quota);
1475 rxdesc = &mdp->rx_ring[entry];
1476 while (!(rxdesc->status & cpu_to_le32(RD_RACT))) {
1477 /* RACT bit must be checked before all the following reads */
1479 desc_status = le32_to_cpu(rxdesc->status);
1480 pkt_len = le32_to_cpu(rxdesc->len) & RD_RFL;
1485 netif_info(mdp, rx_status, ndev,
1486 "rx entry %d status 0x%08x len %d\n",
1487 entry, desc_status, pkt_len);
1489 if (!(desc_status & RDFEND))
1490 ndev->stats.rx_length_errors++;
1492 /* In case of almost all GETHER/ETHERs, the Receive Frame State
1493 * (RFS) bits in the Receive Descriptor 0 are from bit 9 to
1494 * bit 0. However, in case of the R8A7740 and R7S72100
1495 * the RFS bits are from bit 25 to bit 16. So, the
1496 * driver needs right shifting by 16.
1498 if (mdp->cd->hw_checksum)
1501 skb = mdp->rx_skbuff[entry];
1502 if (desc_status & (RD_RFS1 | RD_RFS2 | RD_RFS3 | RD_RFS4 |
1503 RD_RFS5 | RD_RFS6 | RD_RFS10)) {
1504 ndev->stats.rx_errors++;
1505 if (desc_status & RD_RFS1)
1506 ndev->stats.rx_crc_errors++;
1507 if (desc_status & RD_RFS2)
1508 ndev->stats.rx_frame_errors++;
1509 if (desc_status & RD_RFS3)
1510 ndev->stats.rx_length_errors++;
1511 if (desc_status & RD_RFS4)
1512 ndev->stats.rx_length_errors++;
1513 if (desc_status & RD_RFS6)
1514 ndev->stats.rx_missed_errors++;
1515 if (desc_status & RD_RFS10)
1516 ndev->stats.rx_over_errors++;
1518 dma_addr = le32_to_cpu(rxdesc->addr);
1519 if (!mdp->cd->hw_swap)
1521 phys_to_virt(ALIGN(dma_addr, 4)),
1523 mdp->rx_skbuff[entry] = NULL;
1524 if (mdp->cd->rpadir)
1525 skb_reserve(skb, NET_IP_ALIGN);
1526 dma_unmap_single(&mdp->pdev->dev, dma_addr,
1527 ALIGN(mdp->rx_buf_sz, 32),
1529 skb_put(skb, pkt_len);
1530 skb->protocol = eth_type_trans(skb, ndev);
1531 netif_receive_skb(skb);
1532 ndev->stats.rx_packets++;
1533 ndev->stats.rx_bytes += pkt_len;
1534 if (desc_status & RD_RFS8)
1535 ndev->stats.multicast++;
1537 entry = (++mdp->cur_rx) % mdp->num_rx_ring;
1538 rxdesc = &mdp->rx_ring[entry];
1541 /* Refill the Rx ring buffers. */
1542 for (; mdp->cur_rx - mdp->dirty_rx > 0; mdp->dirty_rx++) {
1543 entry = mdp->dirty_rx % mdp->num_rx_ring;
1544 rxdesc = &mdp->rx_ring[entry];
1545 /* The size of the buffer is 32 byte boundary. */
1546 buf_len = ALIGN(mdp->rx_buf_sz, 32);
1547 rxdesc->len = cpu_to_le32(buf_len << 16);
1549 if (mdp->rx_skbuff[entry] == NULL) {
1550 skb = netdev_alloc_skb(ndev, skbuff_size);
1552 break; /* Better luck next round. */
1553 sh_eth_set_receive_align(skb);
1554 dma_addr = dma_map_single(&mdp->pdev->dev, skb->data,
1555 buf_len, DMA_FROM_DEVICE);
1556 if (dma_mapping_error(&mdp->pdev->dev, dma_addr)) {
1560 mdp->rx_skbuff[entry] = skb;
1562 skb_checksum_none_assert(skb);
1563 rxdesc->addr = cpu_to_le32(dma_addr);
1565 dma_wmb(); /* RACT bit must be set after all the above writes */
1566 if (entry >= mdp->num_rx_ring - 1)
1568 cpu_to_le32(RD_RACT | RD_RFP | RD_RDLE);
1570 rxdesc->status |= cpu_to_le32(RD_RACT | RD_RFP);
1573 /* Restart Rx engine if stopped. */
1574 /* If we don't need to check status, don't. -KDU */
1575 if (!(sh_eth_read(ndev, EDRRR) & EDRRR_R)) {
1576 /* fix the values for the next receiving if RDE is set */
1577 if (intr_status & EESR_RDE &&
1578 mdp->reg_offset[RDFAR] != SH_ETH_OFFSET_INVALID) {
1579 u32 count = (sh_eth_read(ndev, RDFAR) -
1580 sh_eth_read(ndev, RDLAR)) >> 4;
1582 mdp->cur_rx = count;
1583 mdp->dirty_rx = count;
1585 sh_eth_write(ndev, EDRRR_R, EDRRR);
1588 *quota -= limit - boguscnt - 1;
1593 static void sh_eth_rcv_snd_disable(struct net_device *ndev)
1595 /* disable tx and rx */
1596 sh_eth_modify(ndev, ECMR, ECMR_RE | ECMR_TE, 0);
1599 static void sh_eth_rcv_snd_enable(struct net_device *ndev)
1601 /* enable tx and rx */
1602 sh_eth_modify(ndev, ECMR, ECMR_RE | ECMR_TE, ECMR_RE | ECMR_TE);
1605 /* E-MAC interrupt handler */
1606 static void sh_eth_emac_interrupt(struct net_device *ndev)
1608 struct sh_eth_private *mdp = netdev_priv(ndev);
1612 felic_stat = sh_eth_read(ndev, ECSR) & sh_eth_read(ndev, ECSIPR);
1613 sh_eth_write(ndev, felic_stat, ECSR); /* clear int */
1614 if (felic_stat & ECSR_ICD)
1615 ndev->stats.tx_carrier_errors++;
1616 if (felic_stat & ECSR_MPD)
1617 pm_wakeup_event(&mdp->pdev->dev, 0);
1618 if (felic_stat & ECSR_LCHNG) {
1620 if (mdp->cd->no_psr || mdp->no_ether_link)
1622 link_stat = sh_eth_read(ndev, PSR);
1623 if (mdp->ether_link_active_low)
1624 link_stat = ~link_stat;
1625 if (!(link_stat & PHY_ST_LINK)) {
1626 sh_eth_rcv_snd_disable(ndev);
1629 sh_eth_modify(ndev, EESIPR, EESIPR_ECIIP, 0);
1631 sh_eth_modify(ndev, ECSR, 0, 0);
1632 sh_eth_modify(ndev, EESIPR, EESIPR_ECIIP, EESIPR_ECIIP);
1633 /* enable tx and rx */
1634 sh_eth_rcv_snd_enable(ndev);
1639 /* error control function */
1640 static void sh_eth_error(struct net_device *ndev, u32 intr_status)
1642 struct sh_eth_private *mdp = netdev_priv(ndev);
1645 if (intr_status & EESR_TWB) {
1646 /* Unused write back interrupt */
1647 if (intr_status & EESR_TABT) { /* Transmit Abort int */
1648 ndev->stats.tx_aborted_errors++;
1649 netif_err(mdp, tx_err, ndev, "Transmit Abort\n");
1653 if (intr_status & EESR_RABT) {
1654 /* Receive Abort int */
1655 if (intr_status & EESR_RFRMER) {
1656 /* Receive Frame Overflow int */
1657 ndev->stats.rx_frame_errors++;
1661 if (intr_status & EESR_TDE) {
1662 /* Transmit Descriptor Empty int */
1663 ndev->stats.tx_fifo_errors++;
1664 netif_err(mdp, tx_err, ndev, "Transmit Descriptor Empty\n");
1667 if (intr_status & EESR_TFE) {
1668 /* FIFO under flow */
1669 ndev->stats.tx_fifo_errors++;
1670 netif_err(mdp, tx_err, ndev, "Transmit FIFO Under flow\n");
1673 if (intr_status & EESR_RDE) {
1674 /* Receive Descriptor Empty int */
1675 ndev->stats.rx_over_errors++;
1678 if (intr_status & EESR_RFE) {
1679 /* Receive FIFO Overflow int */
1680 ndev->stats.rx_fifo_errors++;
1683 if (!mdp->cd->no_ade && (intr_status & EESR_ADE)) {
1685 ndev->stats.tx_fifo_errors++;
1686 netif_err(mdp, tx_err, ndev, "Address Error\n");
1689 mask = EESR_TWB | EESR_TABT | EESR_ADE | EESR_TDE | EESR_TFE;
1690 if (mdp->cd->no_ade)
1692 if (intr_status & mask) {
1694 u32 edtrr = sh_eth_read(ndev, EDTRR);
1697 netdev_err(ndev, "TX error. status=%8.8x cur_tx=%8.8x dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
1698 intr_status, mdp->cur_tx, mdp->dirty_tx,
1699 (u32)ndev->state, edtrr);
1700 /* dirty buffer free */
1701 sh_eth_tx_free(ndev, true);
1704 if (edtrr ^ sh_eth_get_edtrr_trns(mdp)) {
1706 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
1709 netif_wake_queue(ndev);
1713 static irqreturn_t sh_eth_interrupt(int irq, void *netdev)
1715 struct net_device *ndev = netdev;
1716 struct sh_eth_private *mdp = netdev_priv(ndev);
1717 struct sh_eth_cpu_data *cd = mdp->cd;
1718 irqreturn_t ret = IRQ_NONE;
1719 u32 intr_status, intr_enable;
1721 spin_lock(&mdp->lock);
1723 /* Get interrupt status */
1724 intr_status = sh_eth_read(ndev, EESR);
1725 /* Mask it with the interrupt mask, forcing ECI interrupt to be always
1726 * enabled since it's the one that comes thru regardless of the mask,
1727 * and we need to fully handle it in sh_eth_emac_interrupt() in order
1728 * to quench it as it doesn't get cleared by just writing 1 to the ECI
1731 intr_enable = sh_eth_read(ndev, EESIPR);
1732 intr_status &= intr_enable | EESIPR_ECIIP;
1733 if (intr_status & (EESR_RX_CHECK | cd->tx_check | EESR_ECI |
1734 cd->eesr_err_check))
1739 if (unlikely(!mdp->irq_enabled)) {
1740 sh_eth_write(ndev, 0, EESIPR);
1744 if (intr_status & EESR_RX_CHECK) {
1745 if (napi_schedule_prep(&mdp->napi)) {
1746 /* Mask Rx interrupts */
1747 sh_eth_write(ndev, intr_enable & ~EESR_RX_CHECK,
1749 __napi_schedule(&mdp->napi);
1752 "ignoring interrupt, status 0x%08x, mask 0x%08x.\n",
1753 intr_status, intr_enable);
1758 if (intr_status & cd->tx_check) {
1759 /* Clear Tx interrupts */
1760 sh_eth_write(ndev, intr_status & cd->tx_check, EESR);
1762 sh_eth_tx_free(ndev, true);
1763 netif_wake_queue(ndev);
1766 /* E-MAC interrupt */
1767 if (intr_status & EESR_ECI)
1768 sh_eth_emac_interrupt(ndev);
1770 if (intr_status & cd->eesr_err_check) {
1771 /* Clear error interrupts */
1772 sh_eth_write(ndev, intr_status & cd->eesr_err_check, EESR);
1774 sh_eth_error(ndev, intr_status);
1778 spin_unlock(&mdp->lock);
1783 static int sh_eth_poll(struct napi_struct *napi, int budget)
1785 struct sh_eth_private *mdp = container_of(napi, struct sh_eth_private,
1787 struct net_device *ndev = napi->dev;
1792 intr_status = sh_eth_read(ndev, EESR);
1793 if (!(intr_status & EESR_RX_CHECK))
1795 /* Clear Rx interrupts */
1796 sh_eth_write(ndev, intr_status & EESR_RX_CHECK, EESR);
1798 if (sh_eth_rx(ndev, intr_status, "a))
1802 napi_complete(napi);
1804 /* Reenable Rx interrupts */
1805 if (mdp->irq_enabled)
1806 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
1808 return budget - quota;
1811 /* PHY state control function */
1812 static void sh_eth_adjust_link(struct net_device *ndev)
1814 struct sh_eth_private *mdp = netdev_priv(ndev);
1815 struct phy_device *phydev = ndev->phydev;
1819 if (phydev->duplex != mdp->duplex) {
1821 mdp->duplex = phydev->duplex;
1822 if (mdp->cd->set_duplex)
1823 mdp->cd->set_duplex(ndev);
1826 if (phydev->speed != mdp->speed) {
1828 mdp->speed = phydev->speed;
1829 if (mdp->cd->set_rate)
1830 mdp->cd->set_rate(ndev);
1833 sh_eth_modify(ndev, ECMR, ECMR_TXF, 0);
1835 mdp->link = phydev->link;
1836 if (mdp->cd->no_psr || mdp->no_ether_link)
1837 sh_eth_rcv_snd_enable(ndev);
1839 } else if (mdp->link) {
1844 if (mdp->cd->no_psr || mdp->no_ether_link)
1845 sh_eth_rcv_snd_disable(ndev);
1848 if (new_state && netif_msg_link(mdp))
1849 phy_print_status(phydev);
1852 /* PHY init function */
1853 static int sh_eth_phy_init(struct net_device *ndev)
1855 struct device_node *np = ndev->dev.parent->of_node;
1856 struct sh_eth_private *mdp = netdev_priv(ndev);
1857 struct phy_device *phydev;
1863 /* Try connect to PHY */
1865 struct device_node *pn;
1867 pn = of_parse_phandle(np, "phy-handle", 0);
1868 phydev = of_phy_connect(ndev, pn,
1869 sh_eth_adjust_link, 0,
1870 mdp->phy_interface);
1874 phydev = ERR_PTR(-ENOENT);
1876 char phy_id[MII_BUS_ID_SIZE + 3];
1878 snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
1879 mdp->mii_bus->id, mdp->phy_id);
1881 phydev = phy_connect(ndev, phy_id, sh_eth_adjust_link,
1882 mdp->phy_interface);
1885 if (IS_ERR(phydev)) {
1886 netdev_err(ndev, "failed to connect PHY\n");
1887 return PTR_ERR(phydev);
1890 /* mask with MAC supported features */
1891 if (mdp->cd->register_type != SH_ETH_REG_GIGABIT) {
1892 int err = phy_set_max_speed(phydev, SPEED_100);
1894 netdev_err(ndev, "failed to limit PHY to 100 Mbit/s\n");
1895 phy_disconnect(phydev);
1900 phy_attached_info(phydev);
1905 /* PHY control start function */
1906 static int sh_eth_phy_start(struct net_device *ndev)
1910 ret = sh_eth_phy_init(ndev);
1914 phy_start(ndev->phydev);
1919 static int sh_eth_get_link_ksettings(struct net_device *ndev,
1920 struct ethtool_link_ksettings *cmd)
1922 struct sh_eth_private *mdp = netdev_priv(ndev);
1923 unsigned long flags;
1928 spin_lock_irqsave(&mdp->lock, flags);
1929 phy_ethtool_ksettings_get(ndev->phydev, cmd);
1930 spin_unlock_irqrestore(&mdp->lock, flags);
1935 static int sh_eth_set_link_ksettings(struct net_device *ndev,
1936 const struct ethtool_link_ksettings *cmd)
1938 struct sh_eth_private *mdp = netdev_priv(ndev);
1939 unsigned long flags;
1945 spin_lock_irqsave(&mdp->lock, flags);
1947 /* disable tx and rx */
1948 sh_eth_rcv_snd_disable(ndev);
1950 ret = phy_ethtool_ksettings_set(ndev->phydev, cmd);
1954 if (cmd->base.duplex == DUPLEX_FULL)
1959 if (mdp->cd->set_duplex)
1960 mdp->cd->set_duplex(ndev);
1965 /* enable tx and rx */
1966 sh_eth_rcv_snd_enable(ndev);
1968 spin_unlock_irqrestore(&mdp->lock, flags);
1973 /* If it is ever necessary to increase SH_ETH_REG_DUMP_MAX_REGS, the
1974 * version must be bumped as well. Just adding registers up to that
1975 * limit is fine, as long as the existing register indices don't
1978 #define SH_ETH_REG_DUMP_VERSION 1
1979 #define SH_ETH_REG_DUMP_MAX_REGS 256
1981 static size_t __sh_eth_get_regs(struct net_device *ndev, u32 *buf)
1983 struct sh_eth_private *mdp = netdev_priv(ndev);
1984 struct sh_eth_cpu_data *cd = mdp->cd;
1988 BUILD_BUG_ON(SH_ETH_MAX_REGISTER_OFFSET > SH_ETH_REG_DUMP_MAX_REGS);
1990 /* Dump starts with a bitmap that tells ethtool which
1991 * registers are defined for this chip.
1993 len = DIV_ROUND_UP(SH_ETH_REG_DUMP_MAX_REGS, 32);
2001 /* Add a register to the dump, if it has a defined offset.
2002 * This automatically skips most undefined registers, but for
2003 * some it is also necessary to check a capability flag in
2004 * struct sh_eth_cpu_data.
2006 #define mark_reg_valid(reg) valid_map[reg / 32] |= 1U << (reg % 32)
2007 #define add_reg_from(reg, read_expr) do { \
2008 if (mdp->reg_offset[reg] != SH_ETH_OFFSET_INVALID) { \
2010 mark_reg_valid(reg); \
2011 *buf++ = read_expr; \
2016 #define add_reg(reg) add_reg_from(reg, sh_eth_read(ndev, reg))
2017 #define add_tsu_reg(reg) add_reg_from(reg, sh_eth_tsu_read(mdp, reg))
2083 if (cd->hw_checksum)
2089 add_tsu_reg(TSU_CTRST);
2090 add_tsu_reg(TSU_FWEN0);
2091 add_tsu_reg(TSU_FWEN1);
2092 add_tsu_reg(TSU_FCM);
2093 add_tsu_reg(TSU_BSYSL0);
2094 add_tsu_reg(TSU_BSYSL1);
2095 add_tsu_reg(TSU_PRISL0);
2096 add_tsu_reg(TSU_PRISL1);
2097 add_tsu_reg(TSU_FWSL0);
2098 add_tsu_reg(TSU_FWSL1);
2099 add_tsu_reg(TSU_FWSLC);
2100 add_tsu_reg(TSU_QTAG0);
2101 add_tsu_reg(TSU_QTAG1);
2102 add_tsu_reg(TSU_QTAGM0);
2103 add_tsu_reg(TSU_QTAGM1);
2104 add_tsu_reg(TSU_FWSR);
2105 add_tsu_reg(TSU_FWINMK);
2106 add_tsu_reg(TSU_ADQT0);
2107 add_tsu_reg(TSU_ADQT1);
2108 add_tsu_reg(TSU_VTAG0);
2109 add_tsu_reg(TSU_VTAG1);
2110 add_tsu_reg(TSU_ADSBSY);
2111 add_tsu_reg(TSU_TEN);
2112 add_tsu_reg(TSU_POST1);
2113 add_tsu_reg(TSU_POST2);
2114 add_tsu_reg(TSU_POST3);
2115 add_tsu_reg(TSU_POST4);
2116 if (mdp->reg_offset[TSU_ADRH0] != SH_ETH_OFFSET_INVALID) {
2117 /* This is the start of a table, not just a single
2123 mark_reg_valid(TSU_ADRH0);
2124 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES * 2; i++)
2127 mdp->reg_offset[TSU_ADRH0] +
2130 len += SH_ETH_TSU_CAM_ENTRIES * 2;
2134 #undef mark_reg_valid
2142 static int sh_eth_get_regs_len(struct net_device *ndev)
2144 return __sh_eth_get_regs(ndev, NULL);
2147 static void sh_eth_get_regs(struct net_device *ndev, struct ethtool_regs *regs,
2150 struct sh_eth_private *mdp = netdev_priv(ndev);
2152 regs->version = SH_ETH_REG_DUMP_VERSION;
2154 pm_runtime_get_sync(&mdp->pdev->dev);
2155 __sh_eth_get_regs(ndev, buf);
2156 pm_runtime_put_sync(&mdp->pdev->dev);
2159 static int sh_eth_nway_reset(struct net_device *ndev)
2161 struct sh_eth_private *mdp = netdev_priv(ndev);
2162 unsigned long flags;
2168 spin_lock_irqsave(&mdp->lock, flags);
2169 ret = phy_start_aneg(ndev->phydev);
2170 spin_unlock_irqrestore(&mdp->lock, flags);
2175 static u32 sh_eth_get_msglevel(struct net_device *ndev)
2177 struct sh_eth_private *mdp = netdev_priv(ndev);
2178 return mdp->msg_enable;
2181 static void sh_eth_set_msglevel(struct net_device *ndev, u32 value)
2183 struct sh_eth_private *mdp = netdev_priv(ndev);
2184 mdp->msg_enable = value;
2187 static const char sh_eth_gstrings_stats[][ETH_GSTRING_LEN] = {
2188 "rx_current", "tx_current",
2189 "rx_dirty", "tx_dirty",
2191 #define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats)
2193 static int sh_eth_get_sset_count(struct net_device *netdev, int sset)
2197 return SH_ETH_STATS_LEN;
2203 static void sh_eth_get_ethtool_stats(struct net_device *ndev,
2204 struct ethtool_stats *stats, u64 *data)
2206 struct sh_eth_private *mdp = netdev_priv(ndev);
2209 /* device-specific stats */
2210 data[i++] = mdp->cur_rx;
2211 data[i++] = mdp->cur_tx;
2212 data[i++] = mdp->dirty_rx;
2213 data[i++] = mdp->dirty_tx;
2216 static void sh_eth_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
2218 switch (stringset) {
2220 memcpy(data, *sh_eth_gstrings_stats,
2221 sizeof(sh_eth_gstrings_stats));
2226 static void sh_eth_get_ringparam(struct net_device *ndev,
2227 struct ethtool_ringparam *ring)
2229 struct sh_eth_private *mdp = netdev_priv(ndev);
2231 ring->rx_max_pending = RX_RING_MAX;
2232 ring->tx_max_pending = TX_RING_MAX;
2233 ring->rx_pending = mdp->num_rx_ring;
2234 ring->tx_pending = mdp->num_tx_ring;
2237 static int sh_eth_set_ringparam(struct net_device *ndev,
2238 struct ethtool_ringparam *ring)
2240 struct sh_eth_private *mdp = netdev_priv(ndev);
2243 if (ring->tx_pending > TX_RING_MAX ||
2244 ring->rx_pending > RX_RING_MAX ||
2245 ring->tx_pending < TX_RING_MIN ||
2246 ring->rx_pending < RX_RING_MIN)
2248 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
2251 if (netif_running(ndev)) {
2252 netif_device_detach(ndev);
2253 netif_tx_disable(ndev);
2255 /* Serialise with the interrupt handler and NAPI, then
2256 * disable interrupts. We have to clear the
2257 * irq_enabled flag first to ensure that interrupts
2258 * won't be re-enabled.
2260 mdp->irq_enabled = false;
2261 synchronize_irq(ndev->irq);
2262 napi_synchronize(&mdp->napi);
2263 sh_eth_write(ndev, 0x0000, EESIPR);
2265 sh_eth_dev_exit(ndev);
2267 /* Free all the skbuffs in the Rx queue and the DMA buffers. */
2268 sh_eth_ring_free(ndev);
2271 /* Set new parameters */
2272 mdp->num_rx_ring = ring->rx_pending;
2273 mdp->num_tx_ring = ring->tx_pending;
2275 if (netif_running(ndev)) {
2276 ret = sh_eth_ring_init(ndev);
2278 netdev_err(ndev, "%s: sh_eth_ring_init failed.\n",
2282 ret = sh_eth_dev_init(ndev);
2284 netdev_err(ndev, "%s: sh_eth_dev_init failed.\n",
2289 netif_device_attach(ndev);
2295 static void sh_eth_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2297 struct sh_eth_private *mdp = netdev_priv(ndev);
2302 if (mdp->cd->magic) {
2303 wol->supported = WAKE_MAGIC;
2304 wol->wolopts = mdp->wol_enabled ? WAKE_MAGIC : 0;
2308 static int sh_eth_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2310 struct sh_eth_private *mdp = netdev_priv(ndev);
2312 if (!mdp->cd->magic || wol->wolopts & ~WAKE_MAGIC)
2315 mdp->wol_enabled = !!(wol->wolopts & WAKE_MAGIC);
2317 device_set_wakeup_enable(&mdp->pdev->dev, mdp->wol_enabled);
2322 static const struct ethtool_ops sh_eth_ethtool_ops = {
2323 .get_regs_len = sh_eth_get_regs_len,
2324 .get_regs = sh_eth_get_regs,
2325 .nway_reset = sh_eth_nway_reset,
2326 .get_msglevel = sh_eth_get_msglevel,
2327 .set_msglevel = sh_eth_set_msglevel,
2328 .get_link = ethtool_op_get_link,
2329 .get_strings = sh_eth_get_strings,
2330 .get_ethtool_stats = sh_eth_get_ethtool_stats,
2331 .get_sset_count = sh_eth_get_sset_count,
2332 .get_ringparam = sh_eth_get_ringparam,
2333 .set_ringparam = sh_eth_set_ringparam,
2334 .get_link_ksettings = sh_eth_get_link_ksettings,
2335 .set_link_ksettings = sh_eth_set_link_ksettings,
2336 .get_wol = sh_eth_get_wol,
2337 .set_wol = sh_eth_set_wol,
2340 /* network device open function */
2341 static int sh_eth_open(struct net_device *ndev)
2343 struct sh_eth_private *mdp = netdev_priv(ndev);
2346 pm_runtime_get_sync(&mdp->pdev->dev);
2348 napi_enable(&mdp->napi);
2350 ret = request_irq(ndev->irq, sh_eth_interrupt,
2351 mdp->cd->irq_flags, ndev->name, ndev);
2353 netdev_err(ndev, "Can not assign IRQ number\n");
2357 /* Descriptor set */
2358 ret = sh_eth_ring_init(ndev);
2363 ret = sh_eth_dev_init(ndev);
2367 /* PHY control start*/
2368 ret = sh_eth_phy_start(ndev);
2372 netif_start_queue(ndev);
2379 free_irq(ndev->irq, ndev);
2381 napi_disable(&mdp->napi);
2382 pm_runtime_put_sync(&mdp->pdev->dev);
2386 /* Timeout function */
2387 static void sh_eth_tx_timeout(struct net_device *ndev)
2389 struct sh_eth_private *mdp = netdev_priv(ndev);
2390 struct sh_eth_rxdesc *rxdesc;
2393 netif_stop_queue(ndev);
2395 netif_err(mdp, timer, ndev,
2396 "transmit timed out, status %8.8x, resetting...\n",
2397 sh_eth_read(ndev, EESR));
2399 /* tx_errors count up */
2400 ndev->stats.tx_errors++;
2402 /* Free all the skbuffs in the Rx queue. */
2403 for (i = 0; i < mdp->num_rx_ring; i++) {
2404 rxdesc = &mdp->rx_ring[i];
2405 rxdesc->status = cpu_to_le32(0);
2406 rxdesc->addr = cpu_to_le32(0xBADF00D0);
2407 dev_kfree_skb(mdp->rx_skbuff[i]);
2408 mdp->rx_skbuff[i] = NULL;
2410 for (i = 0; i < mdp->num_tx_ring; i++) {
2411 dev_kfree_skb(mdp->tx_skbuff[i]);
2412 mdp->tx_skbuff[i] = NULL;
2416 sh_eth_dev_init(ndev);
2418 netif_start_queue(ndev);
2421 /* Packet transmit function */
2422 static int sh_eth_start_xmit(struct sk_buff *skb, struct net_device *ndev)
2424 struct sh_eth_private *mdp = netdev_priv(ndev);
2425 struct sh_eth_txdesc *txdesc;
2426 dma_addr_t dma_addr;
2428 unsigned long flags;
2430 spin_lock_irqsave(&mdp->lock, flags);
2431 if ((mdp->cur_tx - mdp->dirty_tx) >= (mdp->num_tx_ring - 4)) {
2432 if (!sh_eth_tx_free(ndev, true)) {
2433 netif_warn(mdp, tx_queued, ndev, "TxFD exhausted.\n");
2434 netif_stop_queue(ndev);
2435 spin_unlock_irqrestore(&mdp->lock, flags);
2436 return NETDEV_TX_BUSY;
2439 spin_unlock_irqrestore(&mdp->lock, flags);
2441 if (skb_put_padto(skb, ETH_ZLEN))
2442 return NETDEV_TX_OK;
2444 entry = mdp->cur_tx % mdp->num_tx_ring;
2445 mdp->tx_skbuff[entry] = skb;
2446 txdesc = &mdp->tx_ring[entry];
2448 if (!mdp->cd->hw_swap)
2449 sh_eth_soft_swap(PTR_ALIGN(skb->data, 4), skb->len + 2);
2450 dma_addr = dma_map_single(&mdp->pdev->dev, skb->data, skb->len,
2452 if (dma_mapping_error(&mdp->pdev->dev, dma_addr)) {
2454 return NETDEV_TX_OK;
2456 txdesc->addr = cpu_to_le32(dma_addr);
2457 txdesc->len = cpu_to_le32(skb->len << 16);
2459 dma_wmb(); /* TACT bit must be set after all the above writes */
2460 if (entry >= mdp->num_tx_ring - 1)
2461 txdesc->status |= cpu_to_le32(TD_TACT | TD_TDLE);
2463 txdesc->status |= cpu_to_le32(TD_TACT);
2467 if (!(sh_eth_read(ndev, EDTRR) & sh_eth_get_edtrr_trns(mdp)))
2468 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
2470 return NETDEV_TX_OK;
2473 /* The statistics registers have write-clear behaviour, which means we
2474 * will lose any increment between the read and write. We mitigate
2475 * this by only clearing when we read a non-zero value, so we will
2476 * never falsely report a total of zero.
2479 sh_eth_update_stat(struct net_device *ndev, unsigned long *stat, int reg)
2481 u32 delta = sh_eth_read(ndev, reg);
2485 sh_eth_write(ndev, 0, reg);
2489 static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev)
2491 struct sh_eth_private *mdp = netdev_priv(ndev);
2493 if (sh_eth_is_rz_fast_ether(mdp))
2494 return &ndev->stats;
2496 if (!mdp->is_opened)
2497 return &ndev->stats;
2499 sh_eth_update_stat(ndev, &ndev->stats.tx_dropped, TROCR);
2500 sh_eth_update_stat(ndev, &ndev->stats.collisions, CDCR);
2501 sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors, LCCR);
2503 if (sh_eth_is_gether(mdp)) {
2504 sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors,
2506 sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors,
2509 sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors,
2513 return &ndev->stats;
2516 /* device close function */
2517 static int sh_eth_close(struct net_device *ndev)
2519 struct sh_eth_private *mdp = netdev_priv(ndev);
2521 netif_stop_queue(ndev);
2523 /* Serialise with the interrupt handler and NAPI, then disable
2524 * interrupts. We have to clear the irq_enabled flag first to
2525 * ensure that interrupts won't be re-enabled.
2527 mdp->irq_enabled = false;
2528 synchronize_irq(ndev->irq);
2529 napi_disable(&mdp->napi);
2530 sh_eth_write(ndev, 0x0000, EESIPR);
2532 sh_eth_dev_exit(ndev);
2534 /* PHY Disconnect */
2536 phy_stop(ndev->phydev);
2537 phy_disconnect(ndev->phydev);
2540 free_irq(ndev->irq, ndev);
2542 /* Free all the skbuffs in the Rx queue and the DMA buffer. */
2543 sh_eth_ring_free(ndev);
2545 pm_runtime_put_sync(&mdp->pdev->dev);
2552 /* ioctl to device function */
2553 static int sh_eth_do_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
2555 struct phy_device *phydev = ndev->phydev;
2557 if (!netif_running(ndev))
2563 return phy_mii_ioctl(phydev, rq, cmd);
2566 static int sh_eth_change_mtu(struct net_device *ndev, int new_mtu)
2568 if (netif_running(ndev))
2571 ndev->mtu = new_mtu;
2572 netdev_update_features(ndev);
2577 /* For TSU_POSTn. Please refer to the manual about this (strange) bitfields */
2578 static void *sh_eth_tsu_get_post_reg_offset(struct sh_eth_private *mdp,
2581 return sh_eth_tsu_get_offset(mdp, TSU_POST1) + (entry / 8 * 4);
2584 static u32 sh_eth_tsu_get_post_mask(int entry)
2586 return 0x0f << (28 - ((entry % 8) * 4));
2589 static u32 sh_eth_tsu_get_post_bit(struct sh_eth_private *mdp, int entry)
2591 return (0x08 >> (mdp->port << 1)) << (28 - ((entry % 8) * 4));
2594 static void sh_eth_tsu_enable_cam_entry_post(struct net_device *ndev,
2597 struct sh_eth_private *mdp = netdev_priv(ndev);
2601 reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
2602 tmp = ioread32(reg_offset);
2603 iowrite32(tmp | sh_eth_tsu_get_post_bit(mdp, entry), reg_offset);
2606 static bool sh_eth_tsu_disable_cam_entry_post(struct net_device *ndev,
2609 struct sh_eth_private *mdp = netdev_priv(ndev);
2610 u32 post_mask, ref_mask, tmp;
2613 reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
2614 post_mask = sh_eth_tsu_get_post_mask(entry);
2615 ref_mask = sh_eth_tsu_get_post_bit(mdp, entry) & ~post_mask;
2617 tmp = ioread32(reg_offset);
2618 iowrite32(tmp & ~post_mask, reg_offset);
2620 /* If other port enables, the function returns "true" */
2621 return tmp & ref_mask;
2624 static int sh_eth_tsu_busy(struct net_device *ndev)
2626 int timeout = SH_ETH_TSU_TIMEOUT_MS * 100;
2627 struct sh_eth_private *mdp = netdev_priv(ndev);
2629 while ((sh_eth_tsu_read(mdp, TSU_ADSBSY) & TSU_ADSBSY_0)) {
2633 netdev_err(ndev, "%s: timeout\n", __func__);
2641 static int sh_eth_tsu_write_entry(struct net_device *ndev, void *reg,
2646 val = addr[0] << 24 | addr[1] << 16 | addr[2] << 8 | addr[3];
2647 iowrite32(val, reg);
2648 if (sh_eth_tsu_busy(ndev) < 0)
2651 val = addr[4] << 8 | addr[5];
2652 iowrite32(val, reg + 4);
2653 if (sh_eth_tsu_busy(ndev) < 0)
2659 static void sh_eth_tsu_read_entry(void *reg, u8 *addr)
2663 val = ioread32(reg);
2664 addr[0] = (val >> 24) & 0xff;
2665 addr[1] = (val >> 16) & 0xff;
2666 addr[2] = (val >> 8) & 0xff;
2667 addr[3] = val & 0xff;
2668 val = ioread32(reg + 4);
2669 addr[4] = (val >> 8) & 0xff;
2670 addr[5] = val & 0xff;
2674 static int sh_eth_tsu_find_entry(struct net_device *ndev, const u8 *addr)
2676 struct sh_eth_private *mdp = netdev_priv(ndev);
2677 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2679 u8 c_addr[ETH_ALEN];
2681 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
2682 sh_eth_tsu_read_entry(reg_offset, c_addr);
2683 if (ether_addr_equal(addr, c_addr))
2690 static int sh_eth_tsu_find_empty(struct net_device *ndev)
2695 memset(blank, 0, sizeof(blank));
2696 entry = sh_eth_tsu_find_entry(ndev, blank);
2697 return (entry < 0) ? -ENOMEM : entry;
2700 static int sh_eth_tsu_disable_cam_entry_table(struct net_device *ndev,
2703 struct sh_eth_private *mdp = netdev_priv(ndev);
2704 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2708 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) &
2709 ~(1 << (31 - entry)), TSU_TEN);
2711 memset(blank, 0, sizeof(blank));
2712 ret = sh_eth_tsu_write_entry(ndev, reg_offset + entry * 8, blank);
2718 static int sh_eth_tsu_add_entry(struct net_device *ndev, const u8 *addr)
2720 struct sh_eth_private *mdp = netdev_priv(ndev);
2721 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2727 i = sh_eth_tsu_find_entry(ndev, addr);
2729 /* No entry found, create one */
2730 i = sh_eth_tsu_find_empty(ndev);
2733 ret = sh_eth_tsu_write_entry(ndev, reg_offset + i * 8, addr);
2737 /* Enable the entry */
2738 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) |
2739 (1 << (31 - i)), TSU_TEN);
2742 /* Entry found or created, enable POST */
2743 sh_eth_tsu_enable_cam_entry_post(ndev, i);
2748 static int sh_eth_tsu_del_entry(struct net_device *ndev, const u8 *addr)
2750 struct sh_eth_private *mdp = netdev_priv(ndev);
2756 i = sh_eth_tsu_find_entry(ndev, addr);
2759 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2762 /* Disable the entry if both ports was disabled */
2763 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2771 static int sh_eth_tsu_purge_all(struct net_device *ndev)
2773 struct sh_eth_private *mdp = netdev_priv(ndev);
2779 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++) {
2780 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2783 /* Disable the entry if both ports was disabled */
2784 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2792 static void sh_eth_tsu_purge_mcast(struct net_device *ndev)
2794 struct sh_eth_private *mdp = netdev_priv(ndev);
2796 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2802 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
2803 sh_eth_tsu_read_entry(reg_offset, addr);
2804 if (is_multicast_ether_addr(addr))
2805 sh_eth_tsu_del_entry(ndev, addr);
2809 /* Update promiscuous flag and multicast filter */
2810 static void sh_eth_set_rx_mode(struct net_device *ndev)
2812 struct sh_eth_private *mdp = netdev_priv(ndev);
2815 unsigned long flags;
2817 spin_lock_irqsave(&mdp->lock, flags);
2818 /* Initial condition is MCT = 1, PRM = 0.
2819 * Depending on ndev->flags, set PRM or clear MCT
2821 ecmr_bits = sh_eth_read(ndev, ECMR) & ~ECMR_PRM;
2823 ecmr_bits |= ECMR_MCT;
2825 if (!(ndev->flags & IFF_MULTICAST)) {
2826 sh_eth_tsu_purge_mcast(ndev);
2829 if (ndev->flags & IFF_ALLMULTI) {
2830 sh_eth_tsu_purge_mcast(ndev);
2831 ecmr_bits &= ~ECMR_MCT;
2835 if (ndev->flags & IFF_PROMISC) {
2836 sh_eth_tsu_purge_all(ndev);
2837 ecmr_bits = (ecmr_bits & ~ECMR_MCT) | ECMR_PRM;
2838 } else if (mdp->cd->tsu) {
2839 struct netdev_hw_addr *ha;
2840 netdev_for_each_mc_addr(ha, ndev) {
2841 if (mcast_all && is_multicast_ether_addr(ha->addr))
2844 if (sh_eth_tsu_add_entry(ndev, ha->addr) < 0) {
2846 sh_eth_tsu_purge_mcast(ndev);
2847 ecmr_bits &= ~ECMR_MCT;
2854 /* update the ethernet mode */
2855 sh_eth_write(ndev, ecmr_bits, ECMR);
2857 spin_unlock_irqrestore(&mdp->lock, flags);
2860 static int sh_eth_get_vtag_index(struct sh_eth_private *mdp)
2868 static int sh_eth_vlan_rx_add_vid(struct net_device *ndev,
2869 __be16 proto, u16 vid)
2871 struct sh_eth_private *mdp = netdev_priv(ndev);
2872 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2874 if (unlikely(!mdp->cd->tsu))
2877 /* No filtering if vid = 0 */
2881 mdp->vlan_num_ids++;
2883 /* The controller has one VLAN tag HW filter. So, if the filter is
2884 * already enabled, the driver disables it and the filte
2886 if (mdp->vlan_num_ids > 1) {
2887 /* disable VLAN filter */
2888 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2892 sh_eth_tsu_write(mdp, TSU_VTAG_ENABLE | (vid & TSU_VTAG_VID_MASK),
2898 static int sh_eth_vlan_rx_kill_vid(struct net_device *ndev,
2899 __be16 proto, u16 vid)
2901 struct sh_eth_private *mdp = netdev_priv(ndev);
2902 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2904 if (unlikely(!mdp->cd->tsu))
2907 /* No filtering if vid = 0 */
2911 mdp->vlan_num_ids--;
2912 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2917 /* SuperH's TSU register init function */
2918 static void sh_eth_tsu_init(struct sh_eth_private *mdp)
2920 if (sh_eth_is_rz_fast_ether(mdp)) {
2921 sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
2922 sh_eth_tsu_write(mdp, TSU_FWSLC_POSTENU | TSU_FWSLC_POSTENL,
2923 TSU_FWSLC); /* Enable POST registers */
2927 sh_eth_tsu_write(mdp, 0, TSU_FWEN0); /* Disable forward(0->1) */
2928 sh_eth_tsu_write(mdp, 0, TSU_FWEN1); /* Disable forward(1->0) */
2929 sh_eth_tsu_write(mdp, 0, TSU_FCM); /* forward fifo 3k-3k */
2930 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL0);
2931 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL1);
2932 sh_eth_tsu_write(mdp, 0, TSU_PRISL0);
2933 sh_eth_tsu_write(mdp, 0, TSU_PRISL1);
2934 sh_eth_tsu_write(mdp, 0, TSU_FWSL0);
2935 sh_eth_tsu_write(mdp, 0, TSU_FWSL1);
2936 sh_eth_tsu_write(mdp, TSU_FWSLC_POSTENU | TSU_FWSLC_POSTENL, TSU_FWSLC);
2937 if (sh_eth_is_gether(mdp)) {
2938 sh_eth_tsu_write(mdp, 0, TSU_QTAG0); /* Disable QTAG(0->1) */
2939 sh_eth_tsu_write(mdp, 0, TSU_QTAG1); /* Disable QTAG(1->0) */
2941 sh_eth_tsu_write(mdp, 0, TSU_QTAGM0); /* Disable QTAG(0->1) */
2942 sh_eth_tsu_write(mdp, 0, TSU_QTAGM1); /* Disable QTAG(1->0) */
2944 sh_eth_tsu_write(mdp, 0, TSU_FWSR); /* all interrupt status clear */
2945 sh_eth_tsu_write(mdp, 0, TSU_FWINMK); /* Disable all interrupt */
2946 sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
2947 sh_eth_tsu_write(mdp, 0, TSU_POST1); /* Disable CAM entry [ 0- 7] */
2948 sh_eth_tsu_write(mdp, 0, TSU_POST2); /* Disable CAM entry [ 8-15] */
2949 sh_eth_tsu_write(mdp, 0, TSU_POST3); /* Disable CAM entry [16-23] */
2950 sh_eth_tsu_write(mdp, 0, TSU_POST4); /* Disable CAM entry [24-31] */
2953 /* MDIO bus release function */
2954 static int sh_mdio_release(struct sh_eth_private *mdp)
2956 /* unregister mdio bus */
2957 mdiobus_unregister(mdp->mii_bus);
2959 /* free bitbang info */
2960 free_mdio_bitbang(mdp->mii_bus);
2965 /* MDIO bus init function */
2966 static int sh_mdio_init(struct sh_eth_private *mdp,
2967 struct sh_eth_plat_data *pd)
2970 struct bb_info *bitbang;
2971 struct platform_device *pdev = mdp->pdev;
2972 struct device *dev = &mdp->pdev->dev;
2974 /* create bit control struct for PHY */
2975 bitbang = devm_kzalloc(dev, sizeof(struct bb_info), GFP_KERNEL);
2980 bitbang->addr = mdp->addr + mdp->reg_offset[PIR];
2981 bitbang->set_gate = pd->set_mdio_gate;
2982 bitbang->ctrl.ops = &bb_ops;
2984 /* MII controller setting */
2985 mdp->mii_bus = alloc_mdio_bitbang(&bitbang->ctrl);
2989 /* Hook up MII support for ethtool */
2990 mdp->mii_bus->name = "sh_mii";
2991 mdp->mii_bus->parent = dev;
2992 snprintf(mdp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
2993 pdev->name, pdev->id);
2995 /* register MDIO bus */
2997 ret = of_mdiobus_register(mdp->mii_bus, dev->of_node);
2999 if (pd->phy_irq > 0)
3000 mdp->mii_bus->irq[pd->phy] = pd->phy_irq;
3002 ret = mdiobus_register(mdp->mii_bus);
3011 free_mdio_bitbang(mdp->mii_bus);
3015 static const u16 *sh_eth_get_register_offset(int register_type)
3017 const u16 *reg_offset = NULL;
3019 switch (register_type) {
3020 case SH_ETH_REG_GIGABIT:
3021 reg_offset = sh_eth_offset_gigabit;
3023 case SH_ETH_REG_FAST_RZ:
3024 reg_offset = sh_eth_offset_fast_rz;
3026 case SH_ETH_REG_FAST_RCAR:
3027 reg_offset = sh_eth_offset_fast_rcar;
3029 case SH_ETH_REG_FAST_SH4:
3030 reg_offset = sh_eth_offset_fast_sh4;
3032 case SH_ETH_REG_FAST_SH3_SH2:
3033 reg_offset = sh_eth_offset_fast_sh3_sh2;
3040 static const struct net_device_ops sh_eth_netdev_ops = {
3041 .ndo_open = sh_eth_open,
3042 .ndo_stop = sh_eth_close,
3043 .ndo_start_xmit = sh_eth_start_xmit,
3044 .ndo_get_stats = sh_eth_get_stats,
3045 .ndo_set_rx_mode = sh_eth_set_rx_mode,
3046 .ndo_tx_timeout = sh_eth_tx_timeout,
3047 .ndo_do_ioctl = sh_eth_do_ioctl,
3048 .ndo_change_mtu = sh_eth_change_mtu,
3049 .ndo_validate_addr = eth_validate_addr,
3050 .ndo_set_mac_address = eth_mac_addr,
3053 static const struct net_device_ops sh_eth_netdev_ops_tsu = {
3054 .ndo_open = sh_eth_open,
3055 .ndo_stop = sh_eth_close,
3056 .ndo_start_xmit = sh_eth_start_xmit,
3057 .ndo_get_stats = sh_eth_get_stats,
3058 .ndo_set_rx_mode = sh_eth_set_rx_mode,
3059 .ndo_vlan_rx_add_vid = sh_eth_vlan_rx_add_vid,
3060 .ndo_vlan_rx_kill_vid = sh_eth_vlan_rx_kill_vid,
3061 .ndo_tx_timeout = sh_eth_tx_timeout,
3062 .ndo_do_ioctl = sh_eth_do_ioctl,
3063 .ndo_change_mtu = sh_eth_change_mtu,
3064 .ndo_validate_addr = eth_validate_addr,
3065 .ndo_set_mac_address = eth_mac_addr,
3069 static struct sh_eth_plat_data *sh_eth_parse_dt(struct device *dev)
3071 struct device_node *np = dev->of_node;
3072 struct sh_eth_plat_data *pdata;
3073 const char *mac_addr;
3075 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
3079 pdata->phy_interface = of_get_phy_mode(np);
3081 mac_addr = of_get_mac_address(np);
3083 memcpy(pdata->mac_addr, mac_addr, ETH_ALEN);
3085 pdata->no_ether_link =
3086 of_property_read_bool(np, "renesas,no-ether-link");
3087 pdata->ether_link_active_low =
3088 of_property_read_bool(np, "renesas,ether-link-active-low");
3093 static const struct of_device_id sh_eth_match_table[] = {
3094 { .compatible = "renesas,gether-r8a7740", .data = &r8a7740_data },
3095 { .compatible = "renesas,ether-r8a7743", .data = &rcar_gen2_data },
3096 { .compatible = "renesas,ether-r8a7745", .data = &rcar_gen2_data },
3097 { .compatible = "renesas,ether-r8a7778", .data = &rcar_gen1_data },
3098 { .compatible = "renesas,ether-r8a7779", .data = &rcar_gen1_data },
3099 { .compatible = "renesas,ether-r8a7790", .data = &rcar_gen2_data },
3100 { .compatible = "renesas,ether-r8a7791", .data = &rcar_gen2_data },
3101 { .compatible = "renesas,ether-r8a7793", .data = &rcar_gen2_data },
3102 { .compatible = "renesas,ether-r8a7794", .data = &rcar_gen2_data },
3103 { .compatible = "renesas,ether-r7s72100", .data = &r7s72100_data },
3104 { .compatible = "renesas,rcar-gen1-ether", .data = &rcar_gen1_data },
3105 { .compatible = "renesas,rcar-gen2-ether", .data = &rcar_gen2_data },
3108 MODULE_DEVICE_TABLE(of, sh_eth_match_table);
3110 static inline struct sh_eth_plat_data *sh_eth_parse_dt(struct device *dev)
3116 static int sh_eth_drv_probe(struct platform_device *pdev)
3118 struct resource *res;
3119 struct sh_eth_plat_data *pd = dev_get_platdata(&pdev->dev);
3120 const struct platform_device_id *id = platform_get_device_id(pdev);
3121 struct sh_eth_private *mdp;
3122 struct net_device *ndev;
3126 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3128 ndev = alloc_etherdev(sizeof(struct sh_eth_private));
3132 pm_runtime_enable(&pdev->dev);
3133 pm_runtime_get_sync(&pdev->dev);
3135 ret = platform_get_irq(pdev, 0);
3140 SET_NETDEV_DEV(ndev, &pdev->dev);
3142 mdp = netdev_priv(ndev);
3143 mdp->num_tx_ring = TX_RING_SIZE;
3144 mdp->num_rx_ring = RX_RING_SIZE;
3145 mdp->addr = devm_ioremap_resource(&pdev->dev, res);
3146 if (IS_ERR(mdp->addr)) {
3147 ret = PTR_ERR(mdp->addr);
3151 ndev->base_addr = res->start;
3153 spin_lock_init(&mdp->lock);
3156 if (pdev->dev.of_node)
3157 pd = sh_eth_parse_dt(&pdev->dev);
3159 dev_err(&pdev->dev, "no platform data\n");
3165 mdp->phy_id = pd->phy;
3166 mdp->phy_interface = pd->phy_interface;
3167 mdp->no_ether_link = pd->no_ether_link;
3168 mdp->ether_link_active_low = pd->ether_link_active_low;
3172 mdp->cd = (struct sh_eth_cpu_data *)id->driver_data;
3174 mdp->cd = (struct sh_eth_cpu_data *)of_device_get_match_data(&pdev->dev);
3176 mdp->reg_offset = sh_eth_get_register_offset(mdp->cd->register_type);
3177 if (!mdp->reg_offset) {
3178 dev_err(&pdev->dev, "Unknown register type (%d)\n",
3179 mdp->cd->register_type);
3183 sh_eth_set_default_cpu_data(mdp->cd);
3185 /* User's manual states max MTU should be 2048 but due to the
3186 * alignment calculations in sh_eth_ring_init() the practical
3187 * MTU is a bit less. Maybe this can be optimized some more.
3189 ndev->max_mtu = 2000 - (ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN);
3190 ndev->min_mtu = ETH_MIN_MTU;
3194 ndev->netdev_ops = &sh_eth_netdev_ops_tsu;
3196 ndev->netdev_ops = &sh_eth_netdev_ops;
3197 ndev->ethtool_ops = &sh_eth_ethtool_ops;
3198 ndev->watchdog_timeo = TX_TIMEOUT;
3200 /* debug message level */
3201 mdp->msg_enable = SH_ETH_DEF_MSG_ENABLE;
3203 /* read and set MAC address */
3204 read_mac_address(ndev, pd->mac_addr);
3205 if (!is_valid_ether_addr(ndev->dev_addr)) {
3206 dev_warn(&pdev->dev,
3207 "no valid MAC address supplied, using a random one.\n");
3208 eth_hw_addr_random(ndev);
3212 int port = pdev->id < 0 ? 0 : pdev->id % 2;
3213 struct resource *rtsu;
3215 rtsu = platform_get_resource(pdev, IORESOURCE_MEM, 1);
3217 dev_err(&pdev->dev, "no TSU resource\n");
3221 /* We can only request the TSU region for the first port
3222 * of the two sharing this TSU for the probe to succeed...
3225 !devm_request_mem_region(&pdev->dev, rtsu->start,
3226 resource_size(rtsu),
3227 dev_name(&pdev->dev))) {
3228 dev_err(&pdev->dev, "can't request TSU resource.\n");
3232 /* ioremap the TSU registers */
3233 mdp->tsu_addr = devm_ioremap(&pdev->dev, rtsu->start,
3234 resource_size(rtsu));
3235 if (!mdp->tsu_addr) {
3236 dev_err(&pdev->dev, "TSU region ioremap() failed.\n");
3241 ndev->features = NETIF_F_HW_VLAN_CTAG_FILTER;
3243 /* Need to init only the first port of the two sharing a TSU */
3245 if (mdp->cd->chip_reset)
3246 mdp->cd->chip_reset(ndev);
3248 /* TSU init (Init only)*/
3249 sh_eth_tsu_init(mdp);
3253 if (mdp->cd->rmiimode)
3254 sh_eth_write(ndev, 0x1, RMIIMODE);
3257 ret = sh_mdio_init(mdp, pd);
3259 if (ret != -EPROBE_DEFER)
3260 dev_err(&pdev->dev, "MDIO init failed: %d\n", ret);
3264 netif_napi_add(ndev, &mdp->napi, sh_eth_poll, 64);
3266 /* network device register */
3267 ret = register_netdev(ndev);
3272 device_set_wakeup_capable(&pdev->dev, 1);
3274 /* print device information */
3275 netdev_info(ndev, "Base address at 0x%x, %pM, IRQ %d.\n",
3276 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
3278 pm_runtime_put(&pdev->dev);
3279 platform_set_drvdata(pdev, ndev);
3284 netif_napi_del(&mdp->napi);
3285 sh_mdio_release(mdp);
3291 pm_runtime_put(&pdev->dev);
3292 pm_runtime_disable(&pdev->dev);
3296 static int sh_eth_drv_remove(struct platform_device *pdev)
3298 struct net_device *ndev = platform_get_drvdata(pdev);
3299 struct sh_eth_private *mdp = netdev_priv(ndev);
3301 unregister_netdev(ndev);
3302 netif_napi_del(&mdp->napi);
3303 sh_mdio_release(mdp);
3304 pm_runtime_disable(&pdev->dev);
3311 #ifdef CONFIG_PM_SLEEP
3312 static int sh_eth_wol_setup(struct net_device *ndev)
3314 struct sh_eth_private *mdp = netdev_priv(ndev);
3316 /* Only allow ECI interrupts */
3317 synchronize_irq(ndev->irq);
3318 napi_disable(&mdp->napi);
3319 sh_eth_write(ndev, EESIPR_ECIIP, EESIPR);
3321 /* Enable MagicPacket */
3322 sh_eth_modify(ndev, ECMR, ECMR_MPDE, ECMR_MPDE);
3324 return enable_irq_wake(ndev->irq);
3327 static int sh_eth_wol_restore(struct net_device *ndev)
3329 struct sh_eth_private *mdp = netdev_priv(ndev);
3332 napi_enable(&mdp->napi);
3334 /* Disable MagicPacket */
3335 sh_eth_modify(ndev, ECMR, ECMR_MPDE, 0);
3337 /* The device needs to be reset to restore MagicPacket logic
3338 * for next wakeup. If we close and open the device it will
3339 * both be reset and all registers restored. This is what
3340 * happens during suspend and resume without WoL enabled.
3342 ret = sh_eth_close(ndev);
3345 ret = sh_eth_open(ndev);
3349 return disable_irq_wake(ndev->irq);
3352 static int sh_eth_suspend(struct device *dev)
3354 struct net_device *ndev = dev_get_drvdata(dev);
3355 struct sh_eth_private *mdp = netdev_priv(ndev);
3358 if (!netif_running(ndev))
3361 netif_device_detach(ndev);
3363 if (mdp->wol_enabled)
3364 ret = sh_eth_wol_setup(ndev);
3366 ret = sh_eth_close(ndev);
3371 static int sh_eth_resume(struct device *dev)
3373 struct net_device *ndev = dev_get_drvdata(dev);
3374 struct sh_eth_private *mdp = netdev_priv(ndev);
3377 if (!netif_running(ndev))
3380 if (mdp->wol_enabled)
3381 ret = sh_eth_wol_restore(ndev);
3383 ret = sh_eth_open(ndev);
3388 netif_device_attach(ndev);
3394 static int sh_eth_runtime_nop(struct device *dev)
3396 /* Runtime PM callback shared between ->runtime_suspend()
3397 * and ->runtime_resume(). Simply returns success.
3399 * This driver re-initializes all registers after
3400 * pm_runtime_get_sync() anyway so there is no need
3401 * to save and restore registers here.
3406 static const struct dev_pm_ops sh_eth_dev_pm_ops = {
3407 SET_SYSTEM_SLEEP_PM_OPS(sh_eth_suspend, sh_eth_resume)
3408 SET_RUNTIME_PM_OPS(sh_eth_runtime_nop, sh_eth_runtime_nop, NULL)
3410 #define SH_ETH_PM_OPS (&sh_eth_dev_pm_ops)
3412 #define SH_ETH_PM_OPS NULL
3415 static const struct platform_device_id sh_eth_id_table[] = {
3416 { "sh7619-ether", (kernel_ulong_t)&sh7619_data },
3417 { "sh771x-ether", (kernel_ulong_t)&sh771x_data },
3418 { "sh7724-ether", (kernel_ulong_t)&sh7724_data },
3419 { "sh7734-gether", (kernel_ulong_t)&sh7734_data },
3420 { "sh7757-ether", (kernel_ulong_t)&sh7757_data },
3421 { "sh7757-gether", (kernel_ulong_t)&sh7757_data_giga },
3422 { "sh7763-gether", (kernel_ulong_t)&sh7763_data },
3425 MODULE_DEVICE_TABLE(platform, sh_eth_id_table);
3427 static struct platform_driver sh_eth_driver = {
3428 .probe = sh_eth_drv_probe,
3429 .remove = sh_eth_drv_remove,
3430 .id_table = sh_eth_id_table,
3433 .pm = SH_ETH_PM_OPS,
3434 .of_match_table = of_match_ptr(sh_eth_match_table),
3438 module_platform_driver(sh_eth_driver);
3440 MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
3441 MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
3442 MODULE_LICENSE("GPL v2");